1f754f88fSGreg Clayton //===-- ObjectFilePECOFF.cpp ------------------------------------*- C++ -*-===//
2f754f88fSGreg Clayton //
3*2946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*2946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
5*2946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f754f88fSGreg Clayton //
7f754f88fSGreg Clayton //===----------------------------------------------------------------------===//
8f754f88fSGreg Clayton 
9f754f88fSGreg Clayton #include "ObjectFilePECOFF.h"
10f7d1893fSAdrian McCarthy #include "WindowsMiniDump.h"
11f754f88fSGreg Clayton 
12f754f88fSGreg Clayton #include "lldb/Core/FileSpecList.h"
13f754f88fSGreg Clayton #include "lldb/Core/Module.h"
14f4d6de6aSGreg Clayton #include "lldb/Core/ModuleSpec.h"
15f754f88fSGreg Clayton #include "lldb/Core/PluginManager.h"
16f754f88fSGreg Clayton #include "lldb/Core/Section.h"
17f754f88fSGreg Clayton #include "lldb/Core/StreamFile.h"
18f754f88fSGreg Clayton #include "lldb/Symbol/ObjectFile.h"
19f7d1893fSAdrian McCarthy #include "lldb/Target/Process.h"
202756adf3SVirgile Bello #include "lldb/Target/SectionLoadList.h"
212756adf3SVirgile Bello #include "lldb/Target/Target.h"
225f19b907SPavel Labath #include "lldb/Utility/ArchSpec.h"
23666cc0b2SZachary Turner #include "lldb/Utility/DataBufferHeap.h"
245713a05bSZachary Turner #include "lldb/Utility/FileSpec.h"
25037ed1beSAaron Smith #include "lldb/Utility/Log.h"
26bf9a7730SZachary Turner #include "lldb/Utility/StreamString.h"
2738d0632eSPavel Labath #include "lldb/Utility/Timer.h"
28666cc0b2SZachary Turner #include "lldb/Utility/UUID.h"
295f19b907SPavel Labath #include "llvm/BinaryFormat/COFF.h"
30f754f88fSGreg Clayton 
31037ed1beSAaron Smith #include "llvm/Object/COFFImportFile.h"
32037ed1beSAaron Smith #include "llvm/Support/Error.h"
333f4a4b36SZachary Turner #include "llvm/Support/MemoryBuffer.h"
343f4a4b36SZachary Turner 
35f754f88fSGreg Clayton #define IMAGE_DOS_SIGNATURE 0x5A4D    // MZ
36f754f88fSGreg Clayton #define IMAGE_NT_SIGNATURE 0x00004550 // PE00
37f754f88fSGreg Clayton #define OPT_HEADER_MAGIC_PE32 0x010b
38f754f88fSGreg Clayton #define OPT_HEADER_MAGIC_PE32_PLUS 0x020b
39f754f88fSGreg Clayton 
40f754f88fSGreg Clayton using namespace lldb;
41f754f88fSGreg Clayton using namespace lldb_private;
42f754f88fSGreg Clayton 
43b9c1b51eSKate Stone void ObjectFilePECOFF::Initialize() {
44b9c1b51eSKate Stone   PluginManager::RegisterPlugin(
45b9c1b51eSKate Stone       GetPluginNameStatic(), GetPluginDescriptionStatic(), CreateInstance,
46b9c1b51eSKate Stone       CreateMemoryInstance, GetModuleSpecifications, SaveCore);
47f754f88fSGreg Clayton }
48f754f88fSGreg Clayton 
49b9c1b51eSKate Stone void ObjectFilePECOFF::Terminate() {
50f754f88fSGreg Clayton   PluginManager::UnregisterPlugin(CreateInstance);
51f754f88fSGreg Clayton }
52f754f88fSGreg Clayton 
53b9c1b51eSKate Stone lldb_private::ConstString ObjectFilePECOFF::GetPluginNameStatic() {
5457abc5d6SGreg Clayton   static ConstString g_name("pe-coff");
5557abc5d6SGreg Clayton   return g_name;
56f754f88fSGreg Clayton }
57f754f88fSGreg Clayton 
58b9c1b51eSKate Stone const char *ObjectFilePECOFF::GetPluginDescriptionStatic() {
59b9c1b51eSKate Stone   return "Portable Executable and Common Object File Format object file reader "
60b9c1b51eSKate Stone          "(32 and 64 bit)";
61f754f88fSGreg Clayton }
62f754f88fSGreg Clayton 
63b9c1b51eSKate Stone ObjectFile *ObjectFilePECOFF::CreateInstance(const lldb::ModuleSP &module_sp,
645ce9c565SGreg Clayton                                              DataBufferSP &data_sp,
655ce9c565SGreg Clayton                                              lldb::offset_t data_offset,
665ce9c565SGreg Clayton                                              const lldb_private::FileSpec *file,
675ce9c565SGreg Clayton                                              lldb::offset_t file_offset,
68b9c1b51eSKate Stone                                              lldb::offset_t length) {
69b9c1b51eSKate Stone   if (!data_sp) {
7050251fc7SPavel Labath     data_sp = MapFileData(file, length, file_offset);
713f4a4b36SZachary Turner     if (!data_sp)
723f4a4b36SZachary Turner       return nullptr;
735ce9c565SGreg Clayton     data_offset = 0;
745ce9c565SGreg Clayton   }
755ce9c565SGreg Clayton 
763f4a4b36SZachary Turner   if (!ObjectFilePECOFF::MagicBytesMatch(data_sp))
773f4a4b36SZachary Turner     return nullptr;
783f4a4b36SZachary Turner 
795ce9c565SGreg Clayton   // Update the data to contain the entire file if it doesn't already
803f4a4b36SZachary Turner   if (data_sp->GetByteSize() < length) {
8150251fc7SPavel Labath     data_sp = MapFileData(file, length, file_offset);
823f4a4b36SZachary Turner     if (!data_sp)
833f4a4b36SZachary Turner       return nullptr;
84f754f88fSGreg Clayton   }
853f4a4b36SZachary Turner 
863f4a4b36SZachary Turner   auto objfile_ap = llvm::make_unique<ObjectFilePECOFF>(
873f4a4b36SZachary Turner       module_sp, data_sp, data_offset, file, file_offset, length);
883f4a4b36SZachary Turner   if (!objfile_ap || !objfile_ap->ParseHeader())
893f4a4b36SZachary Turner     return nullptr;
903f4a4b36SZachary Turner 
91037ed1beSAaron Smith   // Cache coff binary.
92037ed1beSAaron Smith   if (!objfile_ap->CreateBinary())
93037ed1beSAaron Smith     return nullptr;
94037ed1beSAaron Smith 
953f4a4b36SZachary Turner   return objfile_ap.release();
96f754f88fSGreg Clayton }
97f754f88fSGreg Clayton 
98b9c1b51eSKate Stone ObjectFile *ObjectFilePECOFF::CreateMemoryInstance(
99b9c1b51eSKate Stone     const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp,
100b9c1b51eSKate Stone     const lldb::ProcessSP &process_sp, lldb::addr_t header_addr) {
101344546bdSWalter Erquinigo   if (!data_sp || !ObjectFilePECOFF::MagicBytesMatch(data_sp))
102344546bdSWalter Erquinigo     return nullptr;
103344546bdSWalter Erquinigo   auto objfile_ap = llvm::make_unique<ObjectFilePECOFF>(
104344546bdSWalter Erquinigo       module_sp, data_sp, process_sp, header_addr);
105344546bdSWalter Erquinigo   if (objfile_ap.get() && objfile_ap->ParseHeader()) {
106344546bdSWalter Erquinigo     return objfile_ap.release();
107344546bdSWalter Erquinigo   }
108344546bdSWalter Erquinigo   return nullptr;
109c9660546SGreg Clayton }
110c9660546SGreg Clayton 
111b9c1b51eSKate Stone size_t ObjectFilePECOFF::GetModuleSpecifications(
112b9c1b51eSKate Stone     const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp,
113b9c1b51eSKate Stone     lldb::offset_t data_offset, lldb::offset_t file_offset,
114b9c1b51eSKate Stone     lldb::offset_t length, lldb_private::ModuleSpecList &specs) {
11589eb1baeSVirgile Bello   const size_t initial_count = specs.GetSize();
11689eb1baeSVirgile Bello 
117b9c1b51eSKate Stone   if (ObjectFilePECOFF::MagicBytesMatch(data_sp)) {
11889eb1baeSVirgile Bello     DataExtractor data;
11989eb1baeSVirgile Bello     data.SetData(data_sp, data_offset, length);
12089eb1baeSVirgile Bello     data.SetByteOrder(eByteOrderLittle);
12189eb1baeSVirgile Bello 
12289eb1baeSVirgile Bello     dos_header_t dos_header;
12389eb1baeSVirgile Bello     coff_header_t coff_header;
12489eb1baeSVirgile Bello 
125b9c1b51eSKate Stone     if (ParseDOSHeader(data, dos_header)) {
12689eb1baeSVirgile Bello       lldb::offset_t offset = dos_header.e_lfanew;
12789eb1baeSVirgile Bello       uint32_t pe_signature = data.GetU32(&offset);
12889eb1baeSVirgile Bello       if (pe_signature != IMAGE_NT_SIGNATURE)
12989eb1baeSVirgile Bello         return false;
130b9c1b51eSKate Stone       if (ParseCOFFHeader(data, &offset, coff_header)) {
13189eb1baeSVirgile Bello         ArchSpec spec;
132b9c1b51eSKate Stone         if (coff_header.machine == MachineAmd64) {
133ad587ae4SZachary Turner           spec.SetTriple("x86_64-pc-windows");
1345e6f4520SZachary Turner           specs.Append(ModuleSpec(file, spec));
135b9c1b51eSKate Stone         } else if (coff_header.machine == MachineX86) {
136ad587ae4SZachary Turner           spec.SetTriple("i386-pc-windows");
13789eb1baeSVirgile Bello           specs.Append(ModuleSpec(file, spec));
1385e6f4520SZachary Turner           spec.SetTriple("i686-pc-windows");
1395e6f4520SZachary Turner           specs.Append(ModuleSpec(file, spec));
140037ed1beSAaron Smith         } else if (coff_header.machine == MachineArmNt) {
1417b6e8ef6SStephane Sezer           spec.SetTriple("arm-pc-windows");
1427b6e8ef6SStephane Sezer           specs.Append(ModuleSpec(file, spec));
1437b6e8ef6SStephane Sezer         }
14489eb1baeSVirgile Bello       }
14589eb1baeSVirgile Bello     }
14689eb1baeSVirgile Bello   }
14789eb1baeSVirgile Bello 
14889eb1baeSVirgile Bello   return specs.GetSize() - initial_count;
149f4d6de6aSGreg Clayton }
150f4d6de6aSGreg Clayton 
151b9c1b51eSKate Stone bool ObjectFilePECOFF::SaveCore(const lldb::ProcessSP &process_sp,
152f7d1893fSAdrian McCarthy                                 const lldb_private::FileSpec &outfile,
15397206d57SZachary Turner                                 lldb_private::Status &error) {
154f7d1893fSAdrian McCarthy   return SaveMiniDump(process_sp, outfile, error);
155f7d1893fSAdrian McCarthy }
156f7d1893fSAdrian McCarthy 
157b9c1b51eSKate Stone bool ObjectFilePECOFF::MagicBytesMatch(DataBufferSP &data_sp) {
1585ce9c565SGreg Clayton   DataExtractor data(data_sp, eByteOrderLittle, 4);
159c7bece56SGreg Clayton   lldb::offset_t offset = 0;
160f754f88fSGreg Clayton   uint16_t magic = data.GetU16(&offset);
161f754f88fSGreg Clayton   return magic == IMAGE_DOS_SIGNATURE;
162f754f88fSGreg Clayton }
163f754f88fSGreg Clayton 
164b9c1b51eSKate Stone lldb::SymbolType ObjectFilePECOFF::MapSymbolType(uint16_t coff_symbol_type) {
165c35b91ceSAdrian McCarthy   // TODO:  We need to complete this mapping of COFF symbol types to LLDB ones.
166c35b91ceSAdrian McCarthy   // For now, here's a hack to make sure our function have types.
167b9c1b51eSKate Stone   const auto complex_type =
168b9c1b51eSKate Stone       coff_symbol_type >> llvm::COFF::SCT_COMPLEX_TYPE_SHIFT;
169b9c1b51eSKate Stone   if (complex_type == llvm::COFF::IMAGE_SYM_DTYPE_FUNCTION) {
170c35b91ceSAdrian McCarthy     return lldb::eSymbolTypeCode;
171c35b91ceSAdrian McCarthy   }
172c35b91ceSAdrian McCarthy   return lldb::eSymbolTypeInvalid;
173c35b91ceSAdrian McCarthy }
174f754f88fSGreg Clayton 
175037ed1beSAaron Smith bool ObjectFilePECOFF::CreateBinary() {
176037ed1beSAaron Smith   if (m_owningbin)
177037ed1beSAaron Smith     return true;
178037ed1beSAaron Smith 
179037ed1beSAaron Smith   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
180037ed1beSAaron Smith 
181037ed1beSAaron Smith   auto binary = llvm::object::createBinary(m_file.GetPath());
182037ed1beSAaron Smith   if (!binary) {
183037ed1beSAaron Smith     if (log)
184037ed1beSAaron Smith       log->Printf("ObjectFilePECOFF::CreateBinary() - failed to create binary "
185037ed1beSAaron Smith                   "for file (%s): %s",
186037ed1beSAaron Smith                   m_file ? m_file.GetPath().c_str() : "<NULL>",
187037ed1beSAaron Smith                   errorToErrorCode(binary.takeError()).message().c_str());
188037ed1beSAaron Smith     return false;
189037ed1beSAaron Smith   }
190037ed1beSAaron Smith 
191037ed1beSAaron Smith   // Make sure we only handle COFF format.
192037ed1beSAaron Smith   if (!binary->getBinary()->isCOFF() &&
193037ed1beSAaron Smith       !binary->getBinary()->isCOFFImportFile())
194037ed1beSAaron Smith     return false;
195037ed1beSAaron Smith 
196037ed1beSAaron Smith   m_owningbin = OWNBINType(std::move(*binary));
197037ed1beSAaron Smith   if (log)
198037ed1beSAaron Smith     log->Printf("%p ObjectFilePECOFF::CreateBinary() module = %p (%s), file = "
199037ed1beSAaron Smith                 "%s, binary = %p (Bin = %p)",
200037ed1beSAaron Smith                 static_cast<void *>(this),
201037ed1beSAaron Smith                 static_cast<void *>(GetModule().get()),
202037ed1beSAaron Smith                 GetModule()->GetSpecificationDescription().c_str(),
203037ed1beSAaron Smith                 m_file ? m_file.GetPath().c_str() : "<NULL>",
204037ed1beSAaron Smith                 static_cast<void *>(m_owningbin.getPointer()),
205037ed1beSAaron Smith                 static_cast<void *>(m_owningbin->getBinary()));
206037ed1beSAaron Smith   return true;
207037ed1beSAaron Smith }
208037ed1beSAaron Smith 
209e72dfb32SGreg Clayton ObjectFilePECOFF::ObjectFilePECOFF(const lldb::ModuleSP &module_sp,
2105ce9c565SGreg Clayton                                    DataBufferSP &data_sp,
2115ce9c565SGreg Clayton                                    lldb::offset_t data_offset,
212f754f88fSGreg Clayton                                    const FileSpec *file,
2135ce9c565SGreg Clayton                                    lldb::offset_t file_offset,
214b9c1b51eSKate Stone                                    lldb::offset_t length)
215b9c1b51eSKate Stone     : ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset),
216b9c1b51eSKate Stone       m_dos_header(), m_coff_header(), m_coff_header_opt(), m_sect_headers(),
217037ed1beSAaron Smith       m_entry_point_address(), m_deps_filespec(), m_owningbin() {
218f754f88fSGreg Clayton   ::memset(&m_dos_header, 0, sizeof(m_dos_header));
219f754f88fSGreg Clayton   ::memset(&m_coff_header, 0, sizeof(m_coff_header));
220f754f88fSGreg Clayton   ::memset(&m_coff_header_opt, 0, sizeof(m_coff_header_opt));
221f754f88fSGreg Clayton }
222f754f88fSGreg Clayton 
223344546bdSWalter Erquinigo ObjectFilePECOFF::ObjectFilePECOFF(const lldb::ModuleSP &module_sp,
224344546bdSWalter Erquinigo                                    DataBufferSP &header_data_sp,
225344546bdSWalter Erquinigo                                    const lldb::ProcessSP &process_sp,
226344546bdSWalter Erquinigo                                    addr_t header_addr)
227344546bdSWalter Erquinigo     : ObjectFile(module_sp, process_sp, header_addr, header_data_sp),
228344546bdSWalter Erquinigo       m_dos_header(), m_coff_header(), m_coff_header_opt(), m_sect_headers(),
229037ed1beSAaron Smith       m_entry_point_address(), m_deps_filespec(), m_owningbin() {
230344546bdSWalter Erquinigo   ::memset(&m_dos_header, 0, sizeof(m_dos_header));
231344546bdSWalter Erquinigo   ::memset(&m_coff_header, 0, sizeof(m_coff_header));
232344546bdSWalter Erquinigo   ::memset(&m_coff_header_opt, 0, sizeof(m_coff_header_opt));
233344546bdSWalter Erquinigo }
234344546bdSWalter Erquinigo 
235b9c1b51eSKate Stone ObjectFilePECOFF::~ObjectFilePECOFF() {}
236f754f88fSGreg Clayton 
237b9c1b51eSKate Stone bool ObjectFilePECOFF::ParseHeader() {
238a1743499SGreg Clayton   ModuleSP module_sp(GetModule());
239b9c1b51eSKate Stone   if (module_sp) {
24016ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
241f754f88fSGreg Clayton     m_sect_headers.clear();
242f754f88fSGreg Clayton     m_data.SetByteOrder(eByteOrderLittle);
243c7bece56SGreg Clayton     lldb::offset_t offset = 0;
244f754f88fSGreg Clayton 
245b9c1b51eSKate Stone     if (ParseDOSHeader(m_data, m_dos_header)) {
246f754f88fSGreg Clayton       offset = m_dos_header.e_lfanew;
247f754f88fSGreg Clayton       uint32_t pe_signature = m_data.GetU32(&offset);
248f754f88fSGreg Clayton       if (pe_signature != IMAGE_NT_SIGNATURE)
249f754f88fSGreg Clayton         return false;
250b9c1b51eSKate Stone       if (ParseCOFFHeader(m_data, &offset, m_coff_header)) {
251f754f88fSGreg Clayton         if (m_coff_header.hdrsize > 0)
252f754f88fSGreg Clayton           ParseCOFFOptionalHeader(&offset);
253f754f88fSGreg Clayton         ParseSectionHeaders(offset);
25428469ca3SGreg Clayton       }
255f754f88fSGreg Clayton       return true;
256f754f88fSGreg Clayton     }
257a1743499SGreg Clayton   }
258f754f88fSGreg Clayton   return false;
259f754f88fSGreg Clayton }
260f754f88fSGreg Clayton 
261b9c1b51eSKate Stone bool ObjectFilePECOFF::SetLoadAddress(Target &target, addr_t value,
262b9c1b51eSKate Stone                                       bool value_is_offset) {
2632756adf3SVirgile Bello   bool changed = false;
2642756adf3SVirgile Bello   ModuleSP module_sp = GetModule();
265b9c1b51eSKate Stone   if (module_sp) {
2662756adf3SVirgile Bello     size_t num_loaded_sections = 0;
2672756adf3SVirgile Bello     SectionList *section_list = GetSectionList();
268b9c1b51eSKate Stone     if (section_list) {
269b9c1b51eSKate Stone       if (!value_is_offset) {
2702756adf3SVirgile Bello         value -= m_image_base;
2712756adf3SVirgile Bello       }
2722756adf3SVirgile Bello 
2732756adf3SVirgile Bello       const size_t num_sections = section_list->GetSize();
2742756adf3SVirgile Bello       size_t sect_idx = 0;
2752756adf3SVirgile Bello 
276b9c1b51eSKate Stone       for (sect_idx = 0; sect_idx < num_sections; ++sect_idx) {
27705097246SAdrian Prantl         // Iterate through the object file sections to find all of the sections
27805097246SAdrian Prantl         // that have SHF_ALLOC in their flag bits.
2792756adf3SVirgile Bello         SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx));
280b9c1b51eSKate Stone         if (section_sp && !section_sp->IsThreadSpecific()) {
281b9c1b51eSKate Stone           if (target.GetSectionLoadList().SetSectionLoadAddress(
282b9c1b51eSKate Stone                   section_sp, section_sp->GetFileAddress() + value))
2832756adf3SVirgile Bello             ++num_loaded_sections;
2842756adf3SVirgile Bello         }
2852756adf3SVirgile Bello       }
2862756adf3SVirgile Bello       changed = num_loaded_sections > 0;
2872756adf3SVirgile Bello     }
2882756adf3SVirgile Bello   }
2892756adf3SVirgile Bello   return changed;
2902756adf3SVirgile Bello }
2912756adf3SVirgile Bello 
292b9c1b51eSKate Stone ByteOrder ObjectFilePECOFF::GetByteOrder() const { return eByteOrderLittle; }
293f754f88fSGreg Clayton 
294b9c1b51eSKate Stone bool ObjectFilePECOFF::IsExecutable() const {
295237ad974SCharles Davis   return (m_coff_header.flags & llvm::COFF::IMAGE_FILE_DLL) == 0;
296f754f88fSGreg Clayton }
297f754f88fSGreg Clayton 
298b9c1b51eSKate Stone uint32_t ObjectFilePECOFF::GetAddressByteSize() const {
299f754f88fSGreg Clayton   if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32_PLUS)
300f754f88fSGreg Clayton     return 8;
301f754f88fSGreg Clayton   else if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32)
302f754f88fSGreg Clayton     return 4;
303f754f88fSGreg Clayton   return 4;
304f754f88fSGreg Clayton }
305f754f88fSGreg Clayton 
306f754f88fSGreg Clayton //----------------------------------------------------------------------
307f754f88fSGreg Clayton // NeedsEndianSwap
308f754f88fSGreg Clayton //
30905097246SAdrian Prantl // Return true if an endian swap needs to occur when extracting data from this
31005097246SAdrian Prantl // file.
311f754f88fSGreg Clayton //----------------------------------------------------------------------
312b9c1b51eSKate Stone bool ObjectFilePECOFF::NeedsEndianSwap() const {
313f754f88fSGreg Clayton #if defined(__LITTLE_ENDIAN__)
314f754f88fSGreg Clayton   return false;
315f754f88fSGreg Clayton #else
316f754f88fSGreg Clayton   return true;
317f754f88fSGreg Clayton #endif
318f754f88fSGreg Clayton }
319f754f88fSGreg Clayton //----------------------------------------------------------------------
320f754f88fSGreg Clayton // ParseDOSHeader
321f754f88fSGreg Clayton //----------------------------------------------------------------------
322b9c1b51eSKate Stone bool ObjectFilePECOFF::ParseDOSHeader(DataExtractor &data,
323b9c1b51eSKate Stone                                       dos_header_t &dos_header) {
324f754f88fSGreg Clayton   bool success = false;
325c7bece56SGreg Clayton   lldb::offset_t offset = 0;
32689eb1baeSVirgile Bello   success = data.ValidOffsetForDataOfSize(0, sizeof(dos_header));
327f754f88fSGreg Clayton 
328b9c1b51eSKate Stone   if (success) {
32989eb1baeSVirgile Bello     dos_header.e_magic = data.GetU16(&offset); // Magic number
33089eb1baeSVirgile Bello     success = dos_header.e_magic == IMAGE_DOS_SIGNATURE;
331f754f88fSGreg Clayton 
332b9c1b51eSKate Stone     if (success) {
33389eb1baeSVirgile Bello       dos_header.e_cblp = data.GetU16(&offset); // Bytes on last page of file
33489eb1baeSVirgile Bello       dos_header.e_cp = data.GetU16(&offset);   // Pages in file
33589eb1baeSVirgile Bello       dos_header.e_crlc = data.GetU16(&offset); // Relocations
336b9c1b51eSKate Stone       dos_header.e_cparhdr =
337b9c1b51eSKate Stone           data.GetU16(&offset); // Size of header in paragraphs
338b9c1b51eSKate Stone       dos_header.e_minalloc =
339b9c1b51eSKate Stone           data.GetU16(&offset); // Minimum extra paragraphs needed
340b9c1b51eSKate Stone       dos_header.e_maxalloc =
341b9c1b51eSKate Stone           data.GetU16(&offset);               // Maximum extra paragraphs needed
34289eb1baeSVirgile Bello       dos_header.e_ss = data.GetU16(&offset); // Initial (relative) SS value
34389eb1baeSVirgile Bello       dos_header.e_sp = data.GetU16(&offset); // Initial SP value
34489eb1baeSVirgile Bello       dos_header.e_csum = data.GetU16(&offset); // Checksum
34589eb1baeSVirgile Bello       dos_header.e_ip = data.GetU16(&offset);   // Initial IP value
34689eb1baeSVirgile Bello       dos_header.e_cs = data.GetU16(&offset);   // Initial (relative) CS value
347b9c1b51eSKate Stone       dos_header.e_lfarlc =
348b9c1b51eSKate Stone           data.GetU16(&offset); // File address of relocation table
34989eb1baeSVirgile Bello       dos_header.e_ovno = data.GetU16(&offset); // Overlay number
350f754f88fSGreg Clayton 
35189eb1baeSVirgile Bello       dos_header.e_res[0] = data.GetU16(&offset); // Reserved words
35289eb1baeSVirgile Bello       dos_header.e_res[1] = data.GetU16(&offset); // Reserved words
35389eb1baeSVirgile Bello       dos_header.e_res[2] = data.GetU16(&offset); // Reserved words
35489eb1baeSVirgile Bello       dos_header.e_res[3] = data.GetU16(&offset); // Reserved words
355f754f88fSGreg Clayton 
356b9c1b51eSKate Stone       dos_header.e_oemid =
357b9c1b51eSKate Stone           data.GetU16(&offset); // OEM identifier (for e_oeminfo)
358b9c1b51eSKate Stone       dos_header.e_oeminfo =
359b9c1b51eSKate Stone           data.GetU16(&offset); // OEM information; e_oemid specific
36089eb1baeSVirgile Bello       dos_header.e_res2[0] = data.GetU16(&offset); // Reserved words
36189eb1baeSVirgile Bello       dos_header.e_res2[1] = data.GetU16(&offset); // Reserved words
36289eb1baeSVirgile Bello       dos_header.e_res2[2] = data.GetU16(&offset); // Reserved words
36389eb1baeSVirgile Bello       dos_header.e_res2[3] = data.GetU16(&offset); // Reserved words
36489eb1baeSVirgile Bello       dos_header.e_res2[4] = data.GetU16(&offset); // Reserved words
36589eb1baeSVirgile Bello       dos_header.e_res2[5] = data.GetU16(&offset); // Reserved words
36689eb1baeSVirgile Bello       dos_header.e_res2[6] = data.GetU16(&offset); // Reserved words
36789eb1baeSVirgile Bello       dos_header.e_res2[7] = data.GetU16(&offset); // Reserved words
36889eb1baeSVirgile Bello       dos_header.e_res2[8] = data.GetU16(&offset); // Reserved words
36989eb1baeSVirgile Bello       dos_header.e_res2[9] = data.GetU16(&offset); // Reserved words
370f754f88fSGreg Clayton 
371b9c1b51eSKate Stone       dos_header.e_lfanew =
372b9c1b51eSKate Stone           data.GetU32(&offset); // File address of new exe header
373f754f88fSGreg Clayton     }
374f754f88fSGreg Clayton   }
375f754f88fSGreg Clayton   if (!success)
37689eb1baeSVirgile Bello     memset(&dos_header, 0, sizeof(dos_header));
377f754f88fSGreg Clayton   return success;
378f754f88fSGreg Clayton }
379f754f88fSGreg Clayton 
380f754f88fSGreg Clayton //----------------------------------------------------------------------
381f754f88fSGreg Clayton // ParserCOFFHeader
382f754f88fSGreg Clayton //----------------------------------------------------------------------
383b9c1b51eSKate Stone bool ObjectFilePECOFF::ParseCOFFHeader(DataExtractor &data,
384b9c1b51eSKate Stone                                        lldb::offset_t *offset_ptr,
385b9c1b51eSKate Stone                                        coff_header_t &coff_header) {
386b9c1b51eSKate Stone   bool success =
387b9c1b51eSKate Stone       data.ValidOffsetForDataOfSize(*offset_ptr, sizeof(coff_header));
388b9c1b51eSKate Stone   if (success) {
38989eb1baeSVirgile Bello     coff_header.machine = data.GetU16(offset_ptr);
39089eb1baeSVirgile Bello     coff_header.nsects = data.GetU16(offset_ptr);
39189eb1baeSVirgile Bello     coff_header.modtime = data.GetU32(offset_ptr);
39289eb1baeSVirgile Bello     coff_header.symoff = data.GetU32(offset_ptr);
39389eb1baeSVirgile Bello     coff_header.nsyms = data.GetU32(offset_ptr);
39489eb1baeSVirgile Bello     coff_header.hdrsize = data.GetU16(offset_ptr);
39589eb1baeSVirgile Bello     coff_header.flags = data.GetU16(offset_ptr);
396f754f88fSGreg Clayton   }
397f754f88fSGreg Clayton   if (!success)
39889eb1baeSVirgile Bello     memset(&coff_header, 0, sizeof(coff_header));
399f754f88fSGreg Clayton   return success;
400f754f88fSGreg Clayton }
401f754f88fSGreg Clayton 
402b9c1b51eSKate Stone bool ObjectFilePECOFF::ParseCOFFOptionalHeader(lldb::offset_t *offset_ptr) {
403f754f88fSGreg Clayton   bool success = false;
404c7bece56SGreg Clayton   const lldb::offset_t end_offset = *offset_ptr + m_coff_header.hdrsize;
405b9c1b51eSKate Stone   if (*offset_ptr < end_offset) {
406f754f88fSGreg Clayton     success = true;
407f754f88fSGreg Clayton     m_coff_header_opt.magic = m_data.GetU16(offset_ptr);
408f754f88fSGreg Clayton     m_coff_header_opt.major_linker_version = m_data.GetU8(offset_ptr);
409f754f88fSGreg Clayton     m_coff_header_opt.minor_linker_version = m_data.GetU8(offset_ptr);
410f754f88fSGreg Clayton     m_coff_header_opt.code_size = m_data.GetU32(offset_ptr);
411f754f88fSGreg Clayton     m_coff_header_opt.data_size = m_data.GetU32(offset_ptr);
412f754f88fSGreg Clayton     m_coff_header_opt.bss_size = m_data.GetU32(offset_ptr);
413f754f88fSGreg Clayton     m_coff_header_opt.entry = m_data.GetU32(offset_ptr);
414f754f88fSGreg Clayton     m_coff_header_opt.code_offset = m_data.GetU32(offset_ptr);
415f754f88fSGreg Clayton 
416f754f88fSGreg Clayton     const uint32_t addr_byte_size = GetAddressByteSize();
417f754f88fSGreg Clayton 
418b9c1b51eSKate Stone     if (*offset_ptr < end_offset) {
419b9c1b51eSKate Stone       if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32) {
420f754f88fSGreg Clayton         // PE32 only
421f754f88fSGreg Clayton         m_coff_header_opt.data_offset = m_data.GetU32(offset_ptr);
422b9c1b51eSKate Stone       } else
423f754f88fSGreg Clayton         m_coff_header_opt.data_offset = 0;
424f754f88fSGreg Clayton 
425b9c1b51eSKate Stone       if (*offset_ptr < end_offset) {
426b9c1b51eSKate Stone         m_coff_header_opt.image_base =
427b9c1b51eSKate Stone             m_data.GetMaxU64(offset_ptr, addr_byte_size);
428f754f88fSGreg Clayton         m_coff_header_opt.sect_alignment = m_data.GetU32(offset_ptr);
429f754f88fSGreg Clayton         m_coff_header_opt.file_alignment = m_data.GetU32(offset_ptr);
430f754f88fSGreg Clayton         m_coff_header_opt.major_os_system_version = m_data.GetU16(offset_ptr);
431f754f88fSGreg Clayton         m_coff_header_opt.minor_os_system_version = m_data.GetU16(offset_ptr);
432f754f88fSGreg Clayton         m_coff_header_opt.major_image_version = m_data.GetU16(offset_ptr);
433f754f88fSGreg Clayton         m_coff_header_opt.minor_image_version = m_data.GetU16(offset_ptr);
434f754f88fSGreg Clayton         m_coff_header_opt.major_subsystem_version = m_data.GetU16(offset_ptr);
435f754f88fSGreg Clayton         m_coff_header_opt.minor_subsystem_version = m_data.GetU16(offset_ptr);
436f754f88fSGreg Clayton         m_coff_header_opt.reserved1 = m_data.GetU32(offset_ptr);
437f754f88fSGreg Clayton         m_coff_header_opt.image_size = m_data.GetU32(offset_ptr);
438f754f88fSGreg Clayton         m_coff_header_opt.header_size = m_data.GetU32(offset_ptr);
43928469ca3SGreg Clayton         m_coff_header_opt.checksum = m_data.GetU32(offset_ptr);
440f754f88fSGreg Clayton         m_coff_header_opt.subsystem = m_data.GetU16(offset_ptr);
441f754f88fSGreg Clayton         m_coff_header_opt.dll_flags = m_data.GetU16(offset_ptr);
442b9c1b51eSKate Stone         m_coff_header_opt.stack_reserve_size =
443b9c1b51eSKate Stone             m_data.GetMaxU64(offset_ptr, addr_byte_size);
444b9c1b51eSKate Stone         m_coff_header_opt.stack_commit_size =
445b9c1b51eSKate Stone             m_data.GetMaxU64(offset_ptr, addr_byte_size);
446b9c1b51eSKate Stone         m_coff_header_opt.heap_reserve_size =
447b9c1b51eSKate Stone             m_data.GetMaxU64(offset_ptr, addr_byte_size);
448b9c1b51eSKate Stone         m_coff_header_opt.heap_commit_size =
449b9c1b51eSKate Stone             m_data.GetMaxU64(offset_ptr, addr_byte_size);
450f754f88fSGreg Clayton         m_coff_header_opt.loader_flags = m_data.GetU32(offset_ptr);
451f754f88fSGreg Clayton         uint32_t num_data_dir_entries = m_data.GetU32(offset_ptr);
452f754f88fSGreg Clayton         m_coff_header_opt.data_dirs.clear();
453f754f88fSGreg Clayton         m_coff_header_opt.data_dirs.resize(num_data_dir_entries);
454f754f88fSGreg Clayton         uint32_t i;
455b9c1b51eSKate Stone         for (i = 0; i < num_data_dir_entries; i++) {
456f754f88fSGreg Clayton           m_coff_header_opt.data_dirs[i].vmaddr = m_data.GetU32(offset_ptr);
457f754f88fSGreg Clayton           m_coff_header_opt.data_dirs[i].vmsize = m_data.GetU32(offset_ptr);
458f754f88fSGreg Clayton         }
4592756adf3SVirgile Bello 
4602756adf3SVirgile Bello         m_file_offset = m_coff_header_opt.image_base;
4612756adf3SVirgile Bello         m_image_base = m_coff_header_opt.image_base;
462f754f88fSGreg Clayton       }
463f754f88fSGreg Clayton     }
464f754f88fSGreg Clayton   }
465f754f88fSGreg Clayton   // Make sure we are on track for section data which follows
466f754f88fSGreg Clayton   *offset_ptr = end_offset;
467f754f88fSGreg Clayton   return success;
468f754f88fSGreg Clayton }
469f754f88fSGreg Clayton 
470344546bdSWalter Erquinigo DataExtractor ObjectFilePECOFF::ReadImageData(uint32_t offset, size_t size) {
471344546bdSWalter Erquinigo   if (m_file) {
4727f6a7a37SZachary Turner     // A bit of a hack, but we intend to write to this buffer, so we can't
4737f6a7a37SZachary Turner     // mmap it.
47450251fc7SPavel Labath     auto buffer_sp = MapFileData(m_file, size, offset);
475344546bdSWalter Erquinigo     return DataExtractor(buffer_sp, GetByteOrder(), GetAddressByteSize());
476344546bdSWalter Erquinigo   }
477344546bdSWalter Erquinigo   ProcessSP process_sp(m_process_wp.lock());
478344546bdSWalter Erquinigo   DataExtractor data;
479344546bdSWalter Erquinigo   if (process_sp) {
480344546bdSWalter Erquinigo     auto data_ap = llvm::make_unique<DataBufferHeap>(size, 0);
48197206d57SZachary Turner     Status readmem_error;
482344546bdSWalter Erquinigo     size_t bytes_read =
483344546bdSWalter Erquinigo         process_sp->ReadMemory(m_image_base + offset, data_ap->GetBytes(),
484344546bdSWalter Erquinigo                                data_ap->GetByteSize(), readmem_error);
485344546bdSWalter Erquinigo     if (bytes_read == size) {
486344546bdSWalter Erquinigo       DataBufferSP buffer_sp(data_ap.release());
487344546bdSWalter Erquinigo       data.SetData(buffer_sp, 0, buffer_sp->GetByteSize());
488344546bdSWalter Erquinigo     }
489344546bdSWalter Erquinigo   }
490344546bdSWalter Erquinigo   return data;
491344546bdSWalter Erquinigo }
492344546bdSWalter Erquinigo 
493f754f88fSGreg Clayton //----------------------------------------------------------------------
494f754f88fSGreg Clayton // ParseSectionHeaders
495f754f88fSGreg Clayton //----------------------------------------------------------------------
496b9c1b51eSKate Stone bool ObjectFilePECOFF::ParseSectionHeaders(
497b9c1b51eSKate Stone     uint32_t section_header_data_offset) {
498f754f88fSGreg Clayton   const uint32_t nsects = m_coff_header.nsects;
499f754f88fSGreg Clayton   m_sect_headers.clear();
500f754f88fSGreg Clayton 
501b9c1b51eSKate Stone   if (nsects > 0) {
502f754f88fSGreg Clayton     const size_t section_header_byte_size = nsects * sizeof(section_header_t);
503344546bdSWalter Erquinigo     DataExtractor section_header_data =
504344546bdSWalter Erquinigo         ReadImageData(section_header_data_offset, section_header_byte_size);
505f754f88fSGreg Clayton 
506c7bece56SGreg Clayton     lldb::offset_t offset = 0;
507b9c1b51eSKate Stone     if (section_header_data.ValidOffsetForDataOfSize(
508b9c1b51eSKate Stone             offset, section_header_byte_size)) {
509f754f88fSGreg Clayton       m_sect_headers.resize(nsects);
510f754f88fSGreg Clayton 
511b9c1b51eSKate Stone       for (uint32_t idx = 0; idx < nsects; ++idx) {
512f754f88fSGreg Clayton         const void *name_data = section_header_data.GetData(&offset, 8);
513b9c1b51eSKate Stone         if (name_data) {
514f754f88fSGreg Clayton           memcpy(m_sect_headers[idx].name, name_data, 8);
515f754f88fSGreg Clayton           m_sect_headers[idx].vmsize = section_header_data.GetU32(&offset);
516f754f88fSGreg Clayton           m_sect_headers[idx].vmaddr = section_header_data.GetU32(&offset);
517f754f88fSGreg Clayton           m_sect_headers[idx].size = section_header_data.GetU32(&offset);
518f754f88fSGreg Clayton           m_sect_headers[idx].offset = section_header_data.GetU32(&offset);
519f754f88fSGreg Clayton           m_sect_headers[idx].reloff = section_header_data.GetU32(&offset);
520f754f88fSGreg Clayton           m_sect_headers[idx].lineoff = section_header_data.GetU32(&offset);
521f754f88fSGreg Clayton           m_sect_headers[idx].nreloc = section_header_data.GetU16(&offset);
522f754f88fSGreg Clayton           m_sect_headers[idx].nline = section_header_data.GetU16(&offset);
523f754f88fSGreg Clayton           m_sect_headers[idx].flags = section_header_data.GetU32(&offset);
524f754f88fSGreg Clayton         }
525f754f88fSGreg Clayton       }
526f754f88fSGreg Clayton     }
527f754f88fSGreg Clayton   }
528f754f88fSGreg Clayton 
529a6682a41SJonas Devlieghere   return !m_sect_headers.empty();
530f754f88fSGreg Clayton }
531f754f88fSGreg Clayton 
5322886e4a0SPavel Labath llvm::StringRef ObjectFilePECOFF::GetSectionName(const section_header_t &sect) {
5332886e4a0SPavel Labath   llvm::StringRef hdr_name(sect.name, llvm::array_lengthof(sect.name));
5342886e4a0SPavel Labath   hdr_name = hdr_name.split('\0').first;
5352886e4a0SPavel Labath   if (hdr_name.consume_front("/")) {
5362886e4a0SPavel Labath     lldb::offset_t stroff;
5372886e4a0SPavel Labath     if (!to_integer(hdr_name, stroff, 10))
5382886e4a0SPavel Labath       return "";
539b9c1b51eSKate Stone     lldb::offset_t string_file_offset =
540b9c1b51eSKate Stone         m_coff_header.symoff + (m_coff_header.nsyms * 18) + stroff;
5412886e4a0SPavel Labath     if (const char *name = m_data.GetCStr(&string_file_offset))
5422886e4a0SPavel Labath       return name;
5432886e4a0SPavel Labath     return "";
544f754f88fSGreg Clayton   }
5452886e4a0SPavel Labath   return hdr_name;
546f754f88fSGreg Clayton }
547f754f88fSGreg Clayton 
548f754f88fSGreg Clayton //----------------------------------------------------------------------
549f754f88fSGreg Clayton // GetNListSymtab
550f754f88fSGreg Clayton //----------------------------------------------------------------------
551b9c1b51eSKate Stone Symtab *ObjectFilePECOFF::GetSymtab() {
552a1743499SGreg Clayton   ModuleSP module_sp(GetModule());
553b9c1b51eSKate Stone   if (module_sp) {
55416ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
555b9c1b51eSKate Stone     if (m_symtab_ap.get() == NULL) {
556f754f88fSGreg Clayton       SectionList *sect_list = GetSectionList();
557f754f88fSGreg Clayton       m_symtab_ap.reset(new Symtab(this));
558bb19a13cSSaleem Abdulrasool       std::lock_guard<std::recursive_mutex> guard(m_symtab_ap->GetMutex());
55928469ca3SGreg Clayton 
56028469ca3SGreg Clayton       const uint32_t num_syms = m_coff_header.nsyms;
56128469ca3SGreg Clayton 
562344546bdSWalter Erquinigo       if (m_file && num_syms > 0 && m_coff_header.symoff > 0) {
5630076e715SGreg Clayton         const uint32_t symbol_size = 18;
56428469ca3SGreg Clayton         const size_t symbol_data_size = num_syms * symbol_size;
565c35b91ceSAdrian McCarthy         // Include the 4-byte string table size at the end of the symbols
566344546bdSWalter Erquinigo         DataExtractor symtab_data =
567344546bdSWalter Erquinigo             ReadImageData(m_coff_header.symoff, symbol_data_size + 4);
568c7bece56SGreg Clayton         lldb::offset_t offset = symbol_data_size;
56928469ca3SGreg Clayton         const uint32_t strtab_size = symtab_data.GetU32(&offset);
570344546bdSWalter Erquinigo         if (strtab_size > 0) {
571344546bdSWalter Erquinigo           DataExtractor strtab_data = ReadImageData(
572344546bdSWalter Erquinigo               m_coff_header.symoff + symbol_data_size, strtab_size);
57328469ca3SGreg Clayton 
5740076e715SGreg Clayton           // First 4 bytes should be zeroed after strtab_size has been read,
5750076e715SGreg Clayton           // because it is used as offset 0 to encode a NULL string.
576f76e099cSSaleem Abdulrasool           uint32_t *strtab_data_start = const_cast<uint32_t *>(
577f76e099cSSaleem Abdulrasool               reinterpret_cast<const uint32_t *>(strtab_data.GetDataStart()));
5780076e715SGreg Clayton           strtab_data_start[0] = 0;
5790076e715SGreg Clayton 
58028469ca3SGreg Clayton           offset = 0;
58128469ca3SGreg Clayton           std::string symbol_name;
582f754f88fSGreg Clayton           Symbol *symbols = m_symtab_ap->Resize(num_syms);
583b9c1b51eSKate Stone           for (uint32_t i = 0; i < num_syms; ++i) {
584f754f88fSGreg Clayton             coff_symbol_t symbol;
58528469ca3SGreg Clayton             const uint32_t symbol_offset = offset;
58628469ca3SGreg Clayton             const char *symbol_name_cstr = NULL;
587c35b91ceSAdrian McCarthy             // If the first 4 bytes of the symbol string are zero, then they
588c35b91ceSAdrian McCarthy             // are followed by a 4-byte string table offset. Else these
58928469ca3SGreg Clayton             // 8 bytes contain the symbol name
590b9c1b51eSKate Stone             if (symtab_data.GetU32(&offset) == 0) {
59105097246SAdrian Prantl               // Long string that doesn't fit into the symbol table name, so
59205097246SAdrian Prantl               // now we must read the 4 byte string table offset
59328469ca3SGreg Clayton               uint32_t strtab_offset = symtab_data.GetU32(&offset);
59428469ca3SGreg Clayton               symbol_name_cstr = strtab_data.PeekCStr(strtab_offset);
59528469ca3SGreg Clayton               symbol_name.assign(symbol_name_cstr);
596b9c1b51eSKate Stone             } else {
597b9c1b51eSKate Stone               // Short string that fits into the symbol table name which is 8
598b9c1b51eSKate Stone               // bytes
59928469ca3SGreg Clayton               offset += sizeof(symbol.name) - 4; // Skip remaining
60028469ca3SGreg Clayton               symbol_name_cstr = symtab_data.PeekCStr(symbol_offset);
60128469ca3SGreg Clayton               if (symbol_name_cstr == NULL)
602f754f88fSGreg Clayton                 break;
60328469ca3SGreg Clayton               symbol_name.assign(symbol_name_cstr, sizeof(symbol.name));
60428469ca3SGreg Clayton             }
60528469ca3SGreg Clayton             symbol.value = symtab_data.GetU32(&offset);
60628469ca3SGreg Clayton             symbol.sect = symtab_data.GetU16(&offset);
60728469ca3SGreg Clayton             symbol.type = symtab_data.GetU16(&offset);
60828469ca3SGreg Clayton             symbol.storage = symtab_data.GetU8(&offset);
60928469ca3SGreg Clayton             symbol.naux = symtab_data.GetU8(&offset);
610037520e9SGreg Clayton             symbols[i].GetMangled().SetValue(ConstString(symbol_name.c_str()));
611b9c1b51eSKate Stone             if ((int16_t)symbol.sect >= 1) {
612b9c1b51eSKate Stone               Address symbol_addr(sect_list->GetSectionAtIndex(symbol.sect - 1),
613b9c1b51eSKate Stone                                   symbol.value);
614358cf1eaSGreg Clayton               symbols[i].GetAddressRef() = symbol_addr;
615c35b91ceSAdrian McCarthy               symbols[i].SetType(MapSymbolType(symbol.type));
6160076e715SGreg Clayton             }
617f754f88fSGreg Clayton 
618b9c1b51eSKate Stone             if (symbol.naux > 0) {
619f754f88fSGreg Clayton               i += symbol.naux;
6200076e715SGreg Clayton               offset += symbol_size;
6210076e715SGreg Clayton             }
622f754f88fSGreg Clayton           }
623f754f88fSGreg Clayton         }
624344546bdSWalter Erquinigo       }
625a4fe3a12SVirgile Bello 
626a4fe3a12SVirgile Bello       // Read export header
627b9c1b51eSKate Stone       if (coff_data_dir_export_table < m_coff_header_opt.data_dirs.size() &&
628b9c1b51eSKate Stone           m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmsize > 0 &&
629b9c1b51eSKate Stone           m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmaddr > 0) {
630a4fe3a12SVirgile Bello         export_directory_entry export_table;
631b9c1b51eSKate Stone         uint32_t data_start =
632b9c1b51eSKate Stone             m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmaddr;
633344546bdSWalter Erquinigo 
634344546bdSWalter Erquinigo         uint32_t address_rva = data_start;
635344546bdSWalter Erquinigo         if (m_file) {
636a4fe3a12SVirgile Bello           Address address(m_coff_header_opt.image_base + data_start, sect_list);
637344546bdSWalter Erquinigo           address_rva =
638344546bdSWalter Erquinigo               address.GetSection()->GetFileOffset() + address.GetOffset();
639344546bdSWalter Erquinigo         }
640344546bdSWalter Erquinigo         DataExtractor symtab_data =
641344546bdSWalter Erquinigo             ReadImageData(address_rva, m_coff_header_opt.data_dirs[0].vmsize);
642a4fe3a12SVirgile Bello         lldb::offset_t offset = 0;
643a4fe3a12SVirgile Bello 
644a4fe3a12SVirgile Bello         // Read export_table header
645a4fe3a12SVirgile Bello         export_table.characteristics = symtab_data.GetU32(&offset);
646a4fe3a12SVirgile Bello         export_table.time_date_stamp = symtab_data.GetU32(&offset);
647a4fe3a12SVirgile Bello         export_table.major_version = symtab_data.GetU16(&offset);
648a4fe3a12SVirgile Bello         export_table.minor_version = symtab_data.GetU16(&offset);
649a4fe3a12SVirgile Bello         export_table.name = symtab_data.GetU32(&offset);
650a4fe3a12SVirgile Bello         export_table.base = symtab_data.GetU32(&offset);
651a4fe3a12SVirgile Bello         export_table.number_of_functions = symtab_data.GetU32(&offset);
652a4fe3a12SVirgile Bello         export_table.number_of_names = symtab_data.GetU32(&offset);
653a4fe3a12SVirgile Bello         export_table.address_of_functions = symtab_data.GetU32(&offset);
654a4fe3a12SVirgile Bello         export_table.address_of_names = symtab_data.GetU32(&offset);
655a4fe3a12SVirgile Bello         export_table.address_of_name_ordinals = symtab_data.GetU32(&offset);
656a4fe3a12SVirgile Bello 
657a4fe3a12SVirgile Bello         bool has_ordinal = export_table.address_of_name_ordinals != 0;
658a4fe3a12SVirgile Bello 
659a4fe3a12SVirgile Bello         lldb::offset_t name_offset = export_table.address_of_names - data_start;
660b9c1b51eSKate Stone         lldb::offset_t name_ordinal_offset =
661b9c1b51eSKate Stone             export_table.address_of_name_ordinals - data_start;
662a4fe3a12SVirgile Bello 
663a4fe3a12SVirgile Bello         Symbol *symbols = m_symtab_ap->Resize(export_table.number_of_names);
664a4fe3a12SVirgile Bello 
665a4fe3a12SVirgile Bello         std::string symbol_name;
666a4fe3a12SVirgile Bello 
667a4fe3a12SVirgile Bello         // Read each export table entry
668b9c1b51eSKate Stone         for (size_t i = 0; i < export_table.number_of_names; ++i) {
669b9c1b51eSKate Stone           uint32_t name_ordinal =
670b9c1b51eSKate Stone               has_ordinal ? symtab_data.GetU16(&name_ordinal_offset) : i;
671a4fe3a12SVirgile Bello           uint32_t name_address = symtab_data.GetU32(&name_offset);
672a4fe3a12SVirgile Bello 
673b9c1b51eSKate Stone           const char *symbol_name_cstr =
674b9c1b51eSKate Stone               symtab_data.PeekCStr(name_address - data_start);
675a4fe3a12SVirgile Bello           symbol_name.assign(symbol_name_cstr);
676a4fe3a12SVirgile Bello 
677b9c1b51eSKate Stone           lldb::offset_t function_offset = export_table.address_of_functions -
678b9c1b51eSKate Stone                                            data_start +
679b9c1b51eSKate Stone                                            sizeof(uint32_t) * name_ordinal;
680a4fe3a12SVirgile Bello           uint32_t function_rva = symtab_data.GetU32(&function_offset);
681a4fe3a12SVirgile Bello 
682b9c1b51eSKate Stone           Address symbol_addr(m_coff_header_opt.image_base + function_rva,
683b9c1b51eSKate Stone                               sect_list);
684a4fe3a12SVirgile Bello           symbols[i].GetMangled().SetValue(ConstString(symbol_name.c_str()));
685358cf1eaSGreg Clayton           symbols[i].GetAddressRef() = symbol_addr;
686a4fe3a12SVirgile Bello           symbols[i].SetType(lldb::eSymbolTypeCode);
687a4fe3a12SVirgile Bello           symbols[i].SetDebug(true);
688a4fe3a12SVirgile Bello         }
689a4fe3a12SVirgile Bello       }
690407c6910SDavide Italiano       m_symtab_ap->CalculateSymbolSizes();
691f754f88fSGreg Clayton     }
692a1743499SGreg Clayton   }
693f754f88fSGreg Clayton   return m_symtab_ap.get();
694f754f88fSGreg Clayton }
695f754f88fSGreg Clayton 
696b9c1b51eSKate Stone bool ObjectFilePECOFF::IsStripped() {
6973046e668SGreg Clayton   // TODO: determine this for COFF
6983046e668SGreg Clayton   return false;
6993046e668SGreg Clayton }
7003046e668SGreg Clayton 
701b9c1b51eSKate Stone void ObjectFilePECOFF::CreateSections(SectionList &unified_section_list) {
70288a2c2a4SPavel Labath   if (m_sections_ap)
70388a2c2a4SPavel Labath     return;
7043046e668SGreg Clayton   m_sections_ap.reset(new SectionList());
7053046e668SGreg Clayton 
706a1743499SGreg Clayton   ModuleSP module_sp(GetModule());
707b9c1b51eSKate Stone   if (module_sp) {
70816ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
709f754f88fSGreg Clayton     const uint32_t nsects = m_sect_headers.size();
710e72dfb32SGreg Clayton     ModuleSP module_sp(GetModule());
711b9c1b51eSKate Stone     for (uint32_t idx = 0; idx < nsects; ++idx) {
7122886e4a0SPavel Labath       ConstString const_sect_name(GetSectionName(m_sect_headers[idx]));
71328469ca3SGreg Clayton       static ConstString g_code_sect_name(".code");
71428469ca3SGreg Clayton       static ConstString g_CODE_sect_name("CODE");
71528469ca3SGreg Clayton       static ConstString g_data_sect_name(".data");
71628469ca3SGreg Clayton       static ConstString g_DATA_sect_name("DATA");
71728469ca3SGreg Clayton       static ConstString g_bss_sect_name(".bss");
71828469ca3SGreg Clayton       static ConstString g_BSS_sect_name("BSS");
71928469ca3SGreg Clayton       static ConstString g_debug_sect_name(".debug");
72028469ca3SGreg Clayton       static ConstString g_reloc_sect_name(".reloc");
72128469ca3SGreg Clayton       static ConstString g_stab_sect_name(".stab");
72228469ca3SGreg Clayton       static ConstString g_stabstr_sect_name(".stabstr");
7230076e715SGreg Clayton       static ConstString g_sect_name_dwarf_debug_abbrev(".debug_abbrev");
7240076e715SGreg Clayton       static ConstString g_sect_name_dwarf_debug_aranges(".debug_aranges");
7250076e715SGreg Clayton       static ConstString g_sect_name_dwarf_debug_frame(".debug_frame");
7260076e715SGreg Clayton       static ConstString g_sect_name_dwarf_debug_info(".debug_info");
7270076e715SGreg Clayton       static ConstString g_sect_name_dwarf_debug_line(".debug_line");
7280076e715SGreg Clayton       static ConstString g_sect_name_dwarf_debug_loc(".debug_loc");
729e4dee269SGeorge Rimar       static ConstString g_sect_name_dwarf_debug_loclists(".debug_loclists");
7300076e715SGreg Clayton       static ConstString g_sect_name_dwarf_debug_macinfo(".debug_macinfo");
731a041d848SPavel Labath       static ConstString g_sect_name_dwarf_debug_names(".debug_names");
7320076e715SGreg Clayton       static ConstString g_sect_name_dwarf_debug_pubnames(".debug_pubnames");
7330076e715SGreg Clayton       static ConstString g_sect_name_dwarf_debug_pubtypes(".debug_pubtypes");
7340076e715SGreg Clayton       static ConstString g_sect_name_dwarf_debug_ranges(".debug_ranges");
7350076e715SGreg Clayton       static ConstString g_sect_name_dwarf_debug_str(".debug_str");
7362550ca1eSGreg Clayton       static ConstString g_sect_name_dwarf_debug_types(".debug_types");
7370076e715SGreg Clayton       static ConstString g_sect_name_eh_frame(".eh_frame");
73865d4d5c3SRyan Brown       static ConstString g_sect_name_go_symtab(".gosymtab");
73928469ca3SGreg Clayton       SectionType section_type = eSectionTypeOther;
740237ad974SCharles Davis       if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_CODE &&
741b9c1b51eSKate Stone           ((const_sect_name == g_code_sect_name) ||
742b9c1b51eSKate Stone            (const_sect_name == g_CODE_sect_name))) {
74328469ca3SGreg Clayton         section_type = eSectionTypeCode;
744b9c1b51eSKate Stone       } else if (m_sect_headers[idx].flags &
745b9c1b51eSKate Stone                      llvm::COFF::IMAGE_SCN_CNT_INITIALIZED_DATA &&
746b9c1b51eSKate Stone                  ((const_sect_name == g_data_sect_name) ||
747b9c1b51eSKate Stone                   (const_sect_name == g_DATA_sect_name))) {
7489cad24a7SZachary Turner         if (m_sect_headers[idx].size == 0 && m_sect_headers[idx].offset == 0)
7499cad24a7SZachary Turner           section_type = eSectionTypeZeroFill;
7509cad24a7SZachary Turner         else
75128469ca3SGreg Clayton           section_type = eSectionTypeData;
752b9c1b51eSKate Stone       } else if (m_sect_headers[idx].flags &
753b9c1b51eSKate Stone                      llvm::COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA &&
754b9c1b51eSKate Stone                  ((const_sect_name == g_bss_sect_name) ||
755b9c1b51eSKate Stone                   (const_sect_name == g_BSS_sect_name))) {
75628469ca3SGreg Clayton         if (m_sect_headers[idx].size == 0)
75728469ca3SGreg Clayton           section_type = eSectionTypeZeroFill;
75828469ca3SGreg Clayton         else
75928469ca3SGreg Clayton           section_type = eSectionTypeData;
760b9c1b51eSKate Stone       } else if (const_sect_name == g_debug_sect_name) {
76128469ca3SGreg Clayton         section_type = eSectionTypeDebug;
762b9c1b51eSKate Stone       } else if (const_sect_name == g_stabstr_sect_name) {
76328469ca3SGreg Clayton         section_type = eSectionTypeDataCString;
764b9c1b51eSKate Stone       } else if (const_sect_name == g_reloc_sect_name) {
76528469ca3SGreg Clayton         section_type = eSectionTypeOther;
766b9c1b51eSKate Stone       } else if (const_sect_name == g_sect_name_dwarf_debug_abbrev)
767b9c1b51eSKate Stone         section_type = eSectionTypeDWARFDebugAbbrev;
768b9c1b51eSKate Stone       else if (const_sect_name == g_sect_name_dwarf_debug_aranges)
769b9c1b51eSKate Stone         section_type = eSectionTypeDWARFDebugAranges;
770b9c1b51eSKate Stone       else if (const_sect_name == g_sect_name_dwarf_debug_frame)
771b9c1b51eSKate Stone         section_type = eSectionTypeDWARFDebugFrame;
772b9c1b51eSKate Stone       else if (const_sect_name == g_sect_name_dwarf_debug_info)
773b9c1b51eSKate Stone         section_type = eSectionTypeDWARFDebugInfo;
774b9c1b51eSKate Stone       else if (const_sect_name == g_sect_name_dwarf_debug_line)
775b9c1b51eSKate Stone         section_type = eSectionTypeDWARFDebugLine;
776b9c1b51eSKate Stone       else if (const_sect_name == g_sect_name_dwarf_debug_loc)
777b9c1b51eSKate Stone         section_type = eSectionTypeDWARFDebugLoc;
778e4dee269SGeorge Rimar       else if (const_sect_name == g_sect_name_dwarf_debug_loclists)
779e4dee269SGeorge Rimar         section_type = eSectionTypeDWARFDebugLocLists;
780b9c1b51eSKate Stone       else if (const_sect_name == g_sect_name_dwarf_debug_macinfo)
781b9c1b51eSKate Stone         section_type = eSectionTypeDWARFDebugMacInfo;
782a041d848SPavel Labath       else if (const_sect_name == g_sect_name_dwarf_debug_names)
783a041d848SPavel Labath         section_type = eSectionTypeDWARFDebugNames;
784b9c1b51eSKate Stone       else if (const_sect_name == g_sect_name_dwarf_debug_pubnames)
785b9c1b51eSKate Stone         section_type = eSectionTypeDWARFDebugPubNames;
786b9c1b51eSKate Stone       else if (const_sect_name == g_sect_name_dwarf_debug_pubtypes)
787b9c1b51eSKate Stone         section_type = eSectionTypeDWARFDebugPubTypes;
788b9c1b51eSKate Stone       else if (const_sect_name == g_sect_name_dwarf_debug_ranges)
789b9c1b51eSKate Stone         section_type = eSectionTypeDWARFDebugRanges;
790b9c1b51eSKate Stone       else if (const_sect_name == g_sect_name_dwarf_debug_str)
791b9c1b51eSKate Stone         section_type = eSectionTypeDWARFDebugStr;
7922550ca1eSGreg Clayton       else if (const_sect_name == g_sect_name_dwarf_debug_types)
7932550ca1eSGreg Clayton         section_type = eSectionTypeDWARFDebugTypes;
794b9c1b51eSKate Stone       else if (const_sect_name == g_sect_name_eh_frame)
795b9c1b51eSKate Stone         section_type = eSectionTypeEHFrame;
796b9c1b51eSKate Stone       else if (const_sect_name == g_sect_name_go_symtab)
797b9c1b51eSKate Stone         section_type = eSectionTypeGoSymtab;
798b9c1b51eSKate Stone       else if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_CODE) {
79928469ca3SGreg Clayton         section_type = eSectionTypeCode;
800b9c1b51eSKate Stone       } else if (m_sect_headers[idx].flags &
801b9c1b51eSKate Stone                  llvm::COFF::IMAGE_SCN_CNT_INITIALIZED_DATA) {
80228469ca3SGreg Clayton         section_type = eSectionTypeData;
803b9c1b51eSKate Stone       } else if (m_sect_headers[idx].flags &
804b9c1b51eSKate Stone                  llvm::COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) {
80528469ca3SGreg Clayton         if (m_sect_headers[idx].size == 0)
80628469ca3SGreg Clayton           section_type = eSectionTypeZeroFill;
80728469ca3SGreg Clayton         else
80828469ca3SGreg Clayton           section_type = eSectionTypeData;
80928469ca3SGreg Clayton       }
810f754f88fSGreg Clayton 
811f754f88fSGreg Clayton       // Use a segment ID of the segment index shifted left by 8 so they
812f754f88fSGreg Clayton       // never conflict with any of the sections.
813b9c1b51eSKate Stone       SectionSP section_sp(new Section(
814b9c1b51eSKate Stone           module_sp, // Module to which this section belongs
815a7499c98SMichael Sartain           this,      // Object file to which this section belongs
816b9c1b51eSKate Stone           idx + 1, // Section ID is the 1 based segment index shifted right by
817b9c1b51eSKate Stone                    // 8 bits as not to collide with any of the 256 section IDs
818b9c1b51eSKate Stone                    // that are possible
819f754f88fSGreg Clayton           const_sect_name, // Name of this section
82028469ca3SGreg Clayton           section_type,    // This section is a container of other sections.
821b9c1b51eSKate Stone           m_coff_header_opt.image_base +
822b9c1b51eSKate Stone               m_sect_headers[idx].vmaddr, // File VM address == addresses as
823b9c1b51eSKate Stone                                           // they are found in the object file
824f754f88fSGreg Clayton           m_sect_headers[idx].vmsize,     // VM size in bytes of this section
825b9c1b51eSKate Stone           m_sect_headers[idx]
826b9c1b51eSKate Stone               .offset, // Offset to the data for this section in the file
827b9c1b51eSKate Stone           m_sect_headers[idx]
828b9c1b51eSKate Stone               .size, // Size in bytes of this section as found in the file
82948672afbSGreg Clayton           m_coff_header_opt.sect_alignment, // Section alignment
830f754f88fSGreg Clayton           m_sect_headers[idx].flags));      // Flags for this section
831f754f88fSGreg Clayton 
832f754f88fSGreg Clayton       // section_sp->SetIsEncrypted (segment_is_encrypted);
833f754f88fSGreg Clayton 
8343046e668SGreg Clayton       unified_section_list.AddSection(section_sp);
835f754f88fSGreg Clayton       m_sections_ap->AddSection(section_sp);
836f754f88fSGreg Clayton     }
837f754f88fSGreg Clayton   }
838a1743499SGreg Clayton }
839f754f88fSGreg Clayton 
840b9c1b51eSKate Stone bool ObjectFilePECOFF::GetUUID(UUID *uuid) { return false; }
841f754f88fSGreg Clayton 
842037ed1beSAaron Smith uint32_t ObjectFilePECOFF::ParseDependentModules() {
843037ed1beSAaron Smith   ModuleSP module_sp(GetModule());
844037ed1beSAaron Smith   if (!module_sp)
845f754f88fSGreg Clayton     return 0;
846037ed1beSAaron Smith 
847037ed1beSAaron Smith   std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
848037ed1beSAaron Smith   if (m_deps_filespec)
849037ed1beSAaron Smith     return m_deps_filespec->GetSize();
850037ed1beSAaron Smith 
851037ed1beSAaron Smith   // Cache coff binary if it is not done yet.
852037ed1beSAaron Smith   if (!CreateBinary())
853037ed1beSAaron Smith     return 0;
854037ed1beSAaron Smith 
855037ed1beSAaron Smith   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
856037ed1beSAaron Smith   if (log)
857037ed1beSAaron Smith     log->Printf("%p ObjectFilePECOFF::ParseDependentModules() module = %p "
858037ed1beSAaron Smith                 "(%s), binary = %p (Bin = %p)",
859037ed1beSAaron Smith                 static_cast<void *>(this), static_cast<void *>(module_sp.get()),
860037ed1beSAaron Smith                 module_sp->GetSpecificationDescription().c_str(),
861037ed1beSAaron Smith                 static_cast<void *>(m_owningbin.getPointer()),
862037ed1beSAaron Smith                 m_owningbin ? static_cast<void *>(m_owningbin->getBinary())
863037ed1beSAaron Smith                             : nullptr);
864037ed1beSAaron Smith 
865037ed1beSAaron Smith   auto COFFObj =
866037ed1beSAaron Smith       llvm::dyn_cast<llvm::object::COFFObjectFile>(m_owningbin->getBinary());
867037ed1beSAaron Smith   if (!COFFObj)
868037ed1beSAaron Smith     return 0;
869037ed1beSAaron Smith 
870037ed1beSAaron Smith   m_deps_filespec = FileSpecList();
871037ed1beSAaron Smith 
872037ed1beSAaron Smith   for (const auto &entry : COFFObj->import_directories()) {
873037ed1beSAaron Smith     llvm::StringRef dll_name;
874037ed1beSAaron Smith     auto ec = entry.getName(dll_name);
875037ed1beSAaron Smith     // Report a bogus entry.
876037ed1beSAaron Smith     if (ec != std::error_code()) {
877037ed1beSAaron Smith       if (log)
878037ed1beSAaron Smith         log->Printf("ObjectFilePECOFF::ParseDependentModules() - failed to get "
879037ed1beSAaron Smith                     "import directory entry name: %s",
880037ed1beSAaron Smith                     ec.message().c_str());
881037ed1beSAaron Smith       continue;
882037ed1beSAaron Smith     }
883037ed1beSAaron Smith 
884037ed1beSAaron Smith     // At this moment we only have the base name of the DLL. The full path can
885037ed1beSAaron Smith     // only be seen after the dynamic loading.  Our best guess is Try to get it
886037ed1beSAaron Smith     // with the help of the object file's directory.
887b3f44ad9SStella Stamenova     llvm::SmallString<128> dll_fullpath;
888037ed1beSAaron Smith     FileSpec dll_specs(dll_name);
889037ed1beSAaron Smith     dll_specs.GetDirectory().SetString(m_file.GetDirectory().GetCString());
890037ed1beSAaron Smith 
891037ed1beSAaron Smith     if (!llvm::sys::fs::real_path(dll_specs.GetPath(), dll_fullpath))
892037ed1beSAaron Smith       m_deps_filespec->Append(FileSpec(dll_fullpath));
893037ed1beSAaron Smith     else {
894037ed1beSAaron Smith       // Known DLLs or DLL not found in the object file directory.
895037ed1beSAaron Smith       m_deps_filespec->Append(FileSpec(dll_name));
896037ed1beSAaron Smith     }
897037ed1beSAaron Smith   }
898037ed1beSAaron Smith   return m_deps_filespec->GetSize();
899037ed1beSAaron Smith }
900037ed1beSAaron Smith 
901037ed1beSAaron Smith uint32_t ObjectFilePECOFF::GetDependentModules(FileSpecList &files) {
902037ed1beSAaron Smith   auto num_modules = ParseDependentModules();
903037ed1beSAaron Smith   auto original_size = files.GetSize();
904037ed1beSAaron Smith 
905037ed1beSAaron Smith   for (unsigned i = 0; i < num_modules; ++i)
906037ed1beSAaron Smith     files.AppendIfUnique(m_deps_filespec->GetFileSpecAtIndex(i));
907037ed1beSAaron Smith 
908037ed1beSAaron Smith   return files.GetSize() - original_size;
909f754f88fSGreg Clayton }
910f754f88fSGreg Clayton 
911b9c1b51eSKate Stone lldb_private::Address ObjectFilePECOFF::GetEntryPointAddress() {
9128e38c666SStephane Sezer   if (m_entry_point_address.IsValid())
9138e38c666SStephane Sezer     return m_entry_point_address;
9148e38c666SStephane Sezer 
9158e38c666SStephane Sezer   if (!ParseHeader() || !IsExecutable())
9168e38c666SStephane Sezer     return m_entry_point_address;
9178e38c666SStephane Sezer 
9188e38c666SStephane Sezer   SectionList *section_list = GetSectionList();
919a5235af9SAleksandr Urakov   addr_t file_addr = m_coff_header_opt.entry + m_coff_header_opt.image_base;
9208e38c666SStephane Sezer 
9218e38c666SStephane Sezer   if (!section_list)
922a5235af9SAleksandr Urakov     m_entry_point_address.SetOffset(file_addr);
9238e38c666SStephane Sezer   else
924a5235af9SAleksandr Urakov     m_entry_point_address.ResolveAddressUsingFileSections(file_addr, section_list);
9258e38c666SStephane Sezer   return m_entry_point_address;
9268e38c666SStephane Sezer }
9278e38c666SStephane Sezer 
928f754f88fSGreg Clayton //----------------------------------------------------------------------
929f754f88fSGreg Clayton // Dump
930f754f88fSGreg Clayton //
931f754f88fSGreg Clayton // Dump the specifics of the runtime file container (such as any headers
932f754f88fSGreg Clayton // segments, sections, etc).
933f754f88fSGreg Clayton //----------------------------------------------------------------------
934b9c1b51eSKate Stone void ObjectFilePECOFF::Dump(Stream *s) {
935a1743499SGreg Clayton   ModuleSP module_sp(GetModule());
936b9c1b51eSKate Stone   if (module_sp) {
93716ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
938324a1036SSaleem Abdulrasool     s->Printf("%p: ", static_cast<void *>(this));
939f754f88fSGreg Clayton     s->Indent();
940f754f88fSGreg Clayton     s->PutCString("ObjectFilePECOFF");
941f754f88fSGreg Clayton 
942f760f5aeSPavel Labath     ArchSpec header_arch = GetArchitecture();
943f754f88fSGreg Clayton 
944b9c1b51eSKate Stone     *s << ", file = '" << m_file
945b9c1b51eSKate Stone        << "', arch = " << header_arch.GetArchitectureName() << "\n";
946f754f88fSGreg Clayton 
9473046e668SGreg Clayton     SectionList *sections = GetSectionList();
9483046e668SGreg Clayton     if (sections)
9493046e668SGreg Clayton       sections->Dump(s, NULL, true, UINT32_MAX);
950f754f88fSGreg Clayton 
951f754f88fSGreg Clayton     if (m_symtab_ap.get())
952f754f88fSGreg Clayton       m_symtab_ap->Dump(s, NULL, eSortOrderNone);
953f754f88fSGreg Clayton 
954f754f88fSGreg Clayton     if (m_dos_header.e_magic)
955f754f88fSGreg Clayton       DumpDOSHeader(s, m_dos_header);
956b9c1b51eSKate Stone     if (m_coff_header.machine) {
957f754f88fSGreg Clayton       DumpCOFFHeader(s, m_coff_header);
958f754f88fSGreg Clayton       if (m_coff_header.hdrsize)
959f754f88fSGreg Clayton         DumpOptCOFFHeader(s, m_coff_header_opt);
960f754f88fSGreg Clayton     }
961f754f88fSGreg Clayton     s->EOL();
962f754f88fSGreg Clayton     DumpSectionHeaders(s);
963f754f88fSGreg Clayton     s->EOL();
964037ed1beSAaron Smith 
965037ed1beSAaron Smith     DumpDependentModules(s);
966037ed1beSAaron Smith     s->EOL();
967f754f88fSGreg Clayton   }
968a1743499SGreg Clayton }
969f754f88fSGreg Clayton 
970f754f88fSGreg Clayton //----------------------------------------------------------------------
971f754f88fSGreg Clayton // DumpDOSHeader
972f754f88fSGreg Clayton //
973f754f88fSGreg Clayton // Dump the MS-DOS header to the specified output stream
974f754f88fSGreg Clayton //----------------------------------------------------------------------
975b9c1b51eSKate Stone void ObjectFilePECOFF::DumpDOSHeader(Stream *s, const dos_header_t &header) {
976f754f88fSGreg Clayton   s->PutCString("MSDOS Header\n");
977f754f88fSGreg Clayton   s->Printf("  e_magic    = 0x%4.4x\n", header.e_magic);
978f754f88fSGreg Clayton   s->Printf("  e_cblp     = 0x%4.4x\n", header.e_cblp);
979f754f88fSGreg Clayton   s->Printf("  e_cp       = 0x%4.4x\n", header.e_cp);
980f754f88fSGreg Clayton   s->Printf("  e_crlc     = 0x%4.4x\n", header.e_crlc);
981f754f88fSGreg Clayton   s->Printf("  e_cparhdr  = 0x%4.4x\n", header.e_cparhdr);
982f754f88fSGreg Clayton   s->Printf("  e_minalloc = 0x%4.4x\n", header.e_minalloc);
983f754f88fSGreg Clayton   s->Printf("  e_maxalloc = 0x%4.4x\n", header.e_maxalloc);
984f754f88fSGreg Clayton   s->Printf("  e_ss       = 0x%4.4x\n", header.e_ss);
985f754f88fSGreg Clayton   s->Printf("  e_sp       = 0x%4.4x\n", header.e_sp);
986f754f88fSGreg Clayton   s->Printf("  e_csum     = 0x%4.4x\n", header.e_csum);
987f754f88fSGreg Clayton   s->Printf("  e_ip       = 0x%4.4x\n", header.e_ip);
988f754f88fSGreg Clayton   s->Printf("  e_cs       = 0x%4.4x\n", header.e_cs);
989f754f88fSGreg Clayton   s->Printf("  e_lfarlc   = 0x%4.4x\n", header.e_lfarlc);
990f754f88fSGreg Clayton   s->Printf("  e_ovno     = 0x%4.4x\n", header.e_ovno);
991f754f88fSGreg Clayton   s->Printf("  e_res[4]   = { 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x }\n",
992b9c1b51eSKate Stone             header.e_res[0], header.e_res[1], header.e_res[2], header.e_res[3]);
993f754f88fSGreg Clayton   s->Printf("  e_oemid    = 0x%4.4x\n", header.e_oemid);
994f754f88fSGreg Clayton   s->Printf("  e_oeminfo  = 0x%4.4x\n", header.e_oeminfo);
995b9c1b51eSKate Stone   s->Printf("  e_res2[10] = { 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, "
996b9c1b51eSKate Stone             "0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x }\n",
997b9c1b51eSKate Stone             header.e_res2[0], header.e_res2[1], header.e_res2[2],
998b9c1b51eSKate Stone             header.e_res2[3], header.e_res2[4], header.e_res2[5],
999b9c1b51eSKate Stone             header.e_res2[6], header.e_res2[7], header.e_res2[8],
1000f754f88fSGreg Clayton             header.e_res2[9]);
1001f754f88fSGreg Clayton   s->Printf("  e_lfanew   = 0x%8.8x\n", header.e_lfanew);
1002f754f88fSGreg Clayton }
1003f754f88fSGreg Clayton 
1004f754f88fSGreg Clayton //----------------------------------------------------------------------
1005f754f88fSGreg Clayton // DumpCOFFHeader
1006f754f88fSGreg Clayton //
1007f754f88fSGreg Clayton // Dump the COFF header to the specified output stream
1008f754f88fSGreg Clayton //----------------------------------------------------------------------
1009b9c1b51eSKate Stone void ObjectFilePECOFF::DumpCOFFHeader(Stream *s, const coff_header_t &header) {
1010f754f88fSGreg Clayton   s->PutCString("COFF Header\n");
1011f754f88fSGreg Clayton   s->Printf("  machine = 0x%4.4x\n", header.machine);
1012f754f88fSGreg Clayton   s->Printf("  nsects  = 0x%4.4x\n", header.nsects);
1013f754f88fSGreg Clayton   s->Printf("  modtime = 0x%8.8x\n", header.modtime);
1014f754f88fSGreg Clayton   s->Printf("  symoff  = 0x%8.8x\n", header.symoff);
1015f754f88fSGreg Clayton   s->Printf("  nsyms   = 0x%8.8x\n", header.nsyms);
1016f754f88fSGreg Clayton   s->Printf("  hdrsize = 0x%4.4x\n", header.hdrsize);
1017f754f88fSGreg Clayton }
1018f754f88fSGreg Clayton 
1019f754f88fSGreg Clayton //----------------------------------------------------------------------
1020f754f88fSGreg Clayton // DumpOptCOFFHeader
1021f754f88fSGreg Clayton //
1022f754f88fSGreg Clayton // Dump the optional COFF header to the specified output stream
1023f754f88fSGreg Clayton //----------------------------------------------------------------------
1024b9c1b51eSKate Stone void ObjectFilePECOFF::DumpOptCOFFHeader(Stream *s,
1025b9c1b51eSKate Stone                                          const coff_opt_header_t &header) {
1026f754f88fSGreg Clayton   s->PutCString("Optional COFF Header\n");
1027f754f88fSGreg Clayton   s->Printf("  magic                   = 0x%4.4x\n", header.magic);
1028b9c1b51eSKate Stone   s->Printf("  major_linker_version    = 0x%2.2x\n",
1029b9c1b51eSKate Stone             header.major_linker_version);
1030b9c1b51eSKate Stone   s->Printf("  minor_linker_version    = 0x%2.2x\n",
1031b9c1b51eSKate Stone             header.minor_linker_version);
1032f754f88fSGreg Clayton   s->Printf("  code_size               = 0x%8.8x\n", header.code_size);
1033f754f88fSGreg Clayton   s->Printf("  data_size               = 0x%8.8x\n", header.data_size);
1034f754f88fSGreg Clayton   s->Printf("  bss_size                = 0x%8.8x\n", header.bss_size);
1035f754f88fSGreg Clayton   s->Printf("  entry                   = 0x%8.8x\n", header.entry);
1036f754f88fSGreg Clayton   s->Printf("  code_offset             = 0x%8.8x\n", header.code_offset);
1037f754f88fSGreg Clayton   s->Printf("  data_offset             = 0x%8.8x\n", header.data_offset);
1038b9c1b51eSKate Stone   s->Printf("  image_base              = 0x%16.16" PRIx64 "\n",
1039b9c1b51eSKate Stone             header.image_base);
1040f754f88fSGreg Clayton   s->Printf("  sect_alignment          = 0x%8.8x\n", header.sect_alignment);
1041f754f88fSGreg Clayton   s->Printf("  file_alignment          = 0x%8.8x\n", header.file_alignment);
1042b9c1b51eSKate Stone   s->Printf("  major_os_system_version = 0x%4.4x\n",
1043b9c1b51eSKate Stone             header.major_os_system_version);
1044b9c1b51eSKate Stone   s->Printf("  minor_os_system_version = 0x%4.4x\n",
1045b9c1b51eSKate Stone             header.minor_os_system_version);
1046b9c1b51eSKate Stone   s->Printf("  major_image_version     = 0x%4.4x\n",
1047b9c1b51eSKate Stone             header.major_image_version);
1048b9c1b51eSKate Stone   s->Printf("  minor_image_version     = 0x%4.4x\n",
1049b9c1b51eSKate Stone             header.minor_image_version);
1050b9c1b51eSKate Stone   s->Printf("  major_subsystem_version = 0x%4.4x\n",
1051b9c1b51eSKate Stone             header.major_subsystem_version);
1052b9c1b51eSKate Stone   s->Printf("  minor_subsystem_version = 0x%4.4x\n",
1053b9c1b51eSKate Stone             header.minor_subsystem_version);
1054f754f88fSGreg Clayton   s->Printf("  reserved1               = 0x%8.8x\n", header.reserved1);
1055f754f88fSGreg Clayton   s->Printf("  image_size              = 0x%8.8x\n", header.image_size);
1056f754f88fSGreg Clayton   s->Printf("  header_size             = 0x%8.8x\n", header.header_size);
105728469ca3SGreg Clayton   s->Printf("  checksum                = 0x%8.8x\n", header.checksum);
1058f754f88fSGreg Clayton   s->Printf("  subsystem               = 0x%4.4x\n", header.subsystem);
1059f754f88fSGreg Clayton   s->Printf("  dll_flags               = 0x%4.4x\n", header.dll_flags);
1060b9c1b51eSKate Stone   s->Printf("  stack_reserve_size      = 0x%16.16" PRIx64 "\n",
1061b9c1b51eSKate Stone             header.stack_reserve_size);
1062b9c1b51eSKate Stone   s->Printf("  stack_commit_size       = 0x%16.16" PRIx64 "\n",
1063b9c1b51eSKate Stone             header.stack_commit_size);
1064b9c1b51eSKate Stone   s->Printf("  heap_reserve_size       = 0x%16.16" PRIx64 "\n",
1065b9c1b51eSKate Stone             header.heap_reserve_size);
1066b9c1b51eSKate Stone   s->Printf("  heap_commit_size        = 0x%16.16" PRIx64 "\n",
1067b9c1b51eSKate Stone             header.heap_commit_size);
1068f754f88fSGreg Clayton   s->Printf("  loader_flags            = 0x%8.8x\n", header.loader_flags);
1069b9c1b51eSKate Stone   s->Printf("  num_data_dir_entries    = 0x%8.8x\n",
1070b9c1b51eSKate Stone             (uint32_t)header.data_dirs.size());
1071f754f88fSGreg Clayton   uint32_t i;
1072b9c1b51eSKate Stone   for (i = 0; i < header.data_dirs.size(); i++) {
1073b9c1b51eSKate Stone     s->Printf("  data_dirs[%2u] vmaddr = 0x%8.8x, vmsize = 0x%8.8x\n", i,
1074b9c1b51eSKate Stone               header.data_dirs[i].vmaddr, header.data_dirs[i].vmsize);
1075f754f88fSGreg Clayton   }
1076f754f88fSGreg Clayton }
1077f754f88fSGreg Clayton //----------------------------------------------------------------------
1078f754f88fSGreg Clayton // DumpSectionHeader
1079f754f88fSGreg Clayton //
1080f754f88fSGreg Clayton // Dump a single ELF section header to the specified output stream
1081f754f88fSGreg Clayton //----------------------------------------------------------------------
1082b9c1b51eSKate Stone void ObjectFilePECOFF::DumpSectionHeader(Stream *s,
1083b9c1b51eSKate Stone                                          const section_header_t &sh) {
10842886e4a0SPavel Labath   std::string name = GetSectionName(sh);
1085b9c1b51eSKate Stone   s->Printf("%-16s 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x 0x%4.4x "
1086b9c1b51eSKate Stone             "0x%4.4x 0x%8.8x\n",
1087b9c1b51eSKate Stone             name.c_str(), sh.vmaddr, sh.vmsize, sh.offset, sh.size, sh.reloff,
1088b9c1b51eSKate Stone             sh.lineoff, sh.nreloc, sh.nline, sh.flags);
1089f754f88fSGreg Clayton }
1090f754f88fSGreg Clayton 
1091f754f88fSGreg Clayton //----------------------------------------------------------------------
1092f754f88fSGreg Clayton // DumpSectionHeaders
1093f754f88fSGreg Clayton //
1094f754f88fSGreg Clayton // Dump all of the ELF section header to the specified output stream
1095f754f88fSGreg Clayton //----------------------------------------------------------------------
1096b9c1b51eSKate Stone void ObjectFilePECOFF::DumpSectionHeaders(Stream *s) {
1097f754f88fSGreg Clayton 
1098f754f88fSGreg Clayton   s->PutCString("Section Headers\n");
1099b9c1b51eSKate Stone   s->PutCString("IDX  name             vm addr    vm size    file off   file "
1100b9c1b51eSKate Stone                 "size  reloc off  line off   nreloc nline  flags\n");
1101b9c1b51eSKate Stone   s->PutCString("==== ---------------- ---------- ---------- ---------- "
1102b9c1b51eSKate Stone                 "---------- ---------- ---------- ------ ------ ----------\n");
1103f754f88fSGreg Clayton 
1104f754f88fSGreg Clayton   uint32_t idx = 0;
1105f754f88fSGreg Clayton   SectionHeaderCollIter pos, end = m_sect_headers.end();
1106f754f88fSGreg Clayton 
1107b9c1b51eSKate Stone   for (pos = m_sect_headers.begin(); pos != end; ++pos, ++idx) {
1108f754f88fSGreg Clayton     s->Printf("[%2u] ", idx);
1109f754f88fSGreg Clayton     ObjectFilePECOFF::DumpSectionHeader(s, *pos);
1110f754f88fSGreg Clayton   }
1111f754f88fSGreg Clayton }
1112f754f88fSGreg Clayton 
1113037ed1beSAaron Smith //----------------------------------------------------------------------
1114037ed1beSAaron Smith // DumpDependentModules
1115037ed1beSAaron Smith //
1116037ed1beSAaron Smith // Dump all of the dependent modules to the specified output stream
1117037ed1beSAaron Smith //----------------------------------------------------------------------
1118037ed1beSAaron Smith void ObjectFilePECOFF::DumpDependentModules(lldb_private::Stream *s) {
1119037ed1beSAaron Smith   auto num_modules = ParseDependentModules();
1120037ed1beSAaron Smith   if (num_modules > 0) {
1121037ed1beSAaron Smith     s->PutCString("Dependent Modules\n");
1122037ed1beSAaron Smith     for (unsigned i = 0; i < num_modules; ++i) {
1123037ed1beSAaron Smith       auto spec = m_deps_filespec->GetFileSpecAtIndex(i);
1124037ed1beSAaron Smith       s->Printf("  %s\n", spec.GetFilename().GetCString());
1125037ed1beSAaron Smith     }
1126037ed1beSAaron Smith   }
1127037ed1beSAaron Smith }
1128037ed1beSAaron Smith 
1129fb3b3bd1SZachary Turner bool ObjectFilePECOFF::IsWindowsSubsystem() {
1130fb3b3bd1SZachary Turner   switch (m_coff_header_opt.subsystem) {
1131fb3b3bd1SZachary Turner   case llvm::COFF::IMAGE_SUBSYSTEM_NATIVE:
1132fb3b3bd1SZachary Turner   case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_GUI:
1133fb3b3bd1SZachary Turner   case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_CUI:
1134fb3b3bd1SZachary Turner   case llvm::COFF::IMAGE_SUBSYSTEM_NATIVE_WINDOWS:
1135fb3b3bd1SZachary Turner   case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_CE_GUI:
1136fb3b3bd1SZachary Turner   case llvm::COFF::IMAGE_SUBSYSTEM_XBOX:
1137fb3b3bd1SZachary Turner   case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_BOOT_APPLICATION:
1138fb3b3bd1SZachary Turner     return true;
1139fb3b3bd1SZachary Turner   default:
1140fb3b3bd1SZachary Turner     return false;
1141fb3b3bd1SZachary Turner   }
1142fb3b3bd1SZachary Turner }
1143fb3b3bd1SZachary Turner 
1144f760f5aeSPavel Labath ArchSpec ObjectFilePECOFF::GetArchitecture() {
1145237ad974SCharles Davis   uint16_t machine = m_coff_header.machine;
1146b9c1b51eSKate Stone   switch (machine) {
1147f760f5aeSPavel Labath   default:
1148f760f5aeSPavel Labath     break;
1149237ad974SCharles Davis   case llvm::COFF::IMAGE_FILE_MACHINE_AMD64:
1150237ad974SCharles Davis   case llvm::COFF::IMAGE_FILE_MACHINE_I386:
1151237ad974SCharles Davis   case llvm::COFF::IMAGE_FILE_MACHINE_POWERPC:
1152237ad974SCharles Davis   case llvm::COFF::IMAGE_FILE_MACHINE_POWERPCFP:
1153237ad974SCharles Davis   case llvm::COFF::IMAGE_FILE_MACHINE_ARM:
11541108cb36SSaleem Abdulrasool   case llvm::COFF::IMAGE_FILE_MACHINE_ARMNT:
1155237ad974SCharles Davis   case llvm::COFF::IMAGE_FILE_MACHINE_THUMB:
1156f760f5aeSPavel Labath     ArchSpec arch;
1157fb3b3bd1SZachary Turner     arch.SetArchitecture(eArchTypeCOFF, machine, LLDB_INVALID_CPUTYPE,
1158fb3b3bd1SZachary Turner                          IsWindowsSubsystem() ? llvm::Triple::Win32
1159fb3b3bd1SZachary Turner                                               : llvm::Triple::UnknownOS);
1160f760f5aeSPavel Labath     return arch;
1161237ad974SCharles Davis   }
1162f760f5aeSPavel Labath   return ArchSpec();
1163f754f88fSGreg Clayton }
1164f754f88fSGreg Clayton 
1165b9c1b51eSKate Stone ObjectFile::Type ObjectFilePECOFF::CalculateType() {
1166b9c1b51eSKate Stone   if (m_coff_header.machine != 0) {
1167237ad974SCharles Davis     if ((m_coff_header.flags & llvm::COFF::IMAGE_FILE_DLL) == 0)
1168f754f88fSGreg Clayton       return eTypeExecutable;
1169f754f88fSGreg Clayton     else
1170f754f88fSGreg Clayton       return eTypeSharedLibrary;
1171f754f88fSGreg Clayton   }
1172f754f88fSGreg Clayton   return eTypeExecutable;
1173f754f88fSGreg Clayton }
1174f754f88fSGreg Clayton 
1175b9c1b51eSKate Stone ObjectFile::Strata ObjectFilePECOFF::CalculateStrata() { return eStrataUser; }
11769cad24a7SZachary Turner 
1177f754f88fSGreg Clayton //------------------------------------------------------------------
1178f754f88fSGreg Clayton // PluginInterface protocol
1179f754f88fSGreg Clayton //------------------------------------------------------------------
1180b9c1b51eSKate Stone ConstString ObjectFilePECOFF::GetPluginName() { return GetPluginNameStatic(); }
1181f754f88fSGreg Clayton 
1182b9c1b51eSKate Stone uint32_t ObjectFilePECOFF::GetPluginVersion() { return 1; }
1183