1f754f88fSGreg Clayton //===-- ObjectFilePECOFF.cpp ------------------------------------*- C++ -*-===//
2f754f88fSGreg Clayton //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler 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 
86d5b44036SJonas Devlieghere   auto objfile_up = llvm::make_unique<ObjectFilePECOFF>(
873f4a4b36SZachary Turner       module_sp, data_sp, data_offset, file, file_offset, length);
88d5b44036SJonas Devlieghere   if (!objfile_up || !objfile_up->ParseHeader())
893f4a4b36SZachary Turner     return nullptr;
903f4a4b36SZachary Turner 
91037ed1beSAaron Smith   // Cache coff binary.
92d5b44036SJonas Devlieghere   if (!objfile_up->CreateBinary())
93037ed1beSAaron Smith     return nullptr;
94037ed1beSAaron Smith 
95d5b44036SJonas Devlieghere   return objfile_up.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;
103d5b44036SJonas Devlieghere   auto objfile_up = llvm::make_unique<ObjectFilePECOFF>(
104344546bdSWalter Erquinigo       module_sp, data_sp, process_sp, header_addr);
105d5b44036SJonas Devlieghere   if (objfile_up.get() && objfile_up->ParseHeader()) {
106d5b44036SJonas Devlieghere     return objfile_up.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_image_base = m_coff_header_opt.image_base;
461f754f88fSGreg Clayton       }
462f754f88fSGreg Clayton     }
463f754f88fSGreg Clayton   }
464f754f88fSGreg Clayton   // Make sure we are on track for section data which follows
465f754f88fSGreg Clayton   *offset_ptr = end_offset;
466f754f88fSGreg Clayton   return success;
467f754f88fSGreg Clayton }
468f754f88fSGreg Clayton 
469344546bdSWalter Erquinigo DataExtractor ObjectFilePECOFF::ReadImageData(uint32_t offset, size_t size) {
470344546bdSWalter Erquinigo   if (m_file) {
4717f6a7a37SZachary Turner     // A bit of a hack, but we intend to write to this buffer, so we can't
4727f6a7a37SZachary Turner     // mmap it.
47350251fc7SPavel Labath     auto buffer_sp = MapFileData(m_file, size, offset);
474344546bdSWalter Erquinigo     return DataExtractor(buffer_sp, GetByteOrder(), GetAddressByteSize());
475344546bdSWalter Erquinigo   }
476344546bdSWalter Erquinigo   ProcessSP process_sp(m_process_wp.lock());
477344546bdSWalter Erquinigo   DataExtractor data;
478344546bdSWalter Erquinigo   if (process_sp) {
479d5b44036SJonas Devlieghere     auto data_up = llvm::make_unique<DataBufferHeap>(size, 0);
48097206d57SZachary Turner     Status readmem_error;
481344546bdSWalter Erquinigo     size_t bytes_read =
482d5b44036SJonas Devlieghere         process_sp->ReadMemory(m_image_base + offset, data_up->GetBytes(),
483d5b44036SJonas Devlieghere                                data_up->GetByteSize(), readmem_error);
484344546bdSWalter Erquinigo     if (bytes_read == size) {
485d5b44036SJonas Devlieghere       DataBufferSP buffer_sp(data_up.release());
486344546bdSWalter Erquinigo       data.SetData(buffer_sp, 0, buffer_sp->GetByteSize());
487344546bdSWalter Erquinigo     }
488344546bdSWalter Erquinigo   }
489344546bdSWalter Erquinigo   return data;
490344546bdSWalter Erquinigo }
491344546bdSWalter Erquinigo 
492f754f88fSGreg Clayton //----------------------------------------------------------------------
493f754f88fSGreg Clayton // ParseSectionHeaders
494f754f88fSGreg Clayton //----------------------------------------------------------------------
495b9c1b51eSKate Stone bool ObjectFilePECOFF::ParseSectionHeaders(
496b9c1b51eSKate Stone     uint32_t section_header_data_offset) {
497f754f88fSGreg Clayton   const uint32_t nsects = m_coff_header.nsects;
498f754f88fSGreg Clayton   m_sect_headers.clear();
499f754f88fSGreg Clayton 
500b9c1b51eSKate Stone   if (nsects > 0) {
501f754f88fSGreg Clayton     const size_t section_header_byte_size = nsects * sizeof(section_header_t);
502344546bdSWalter Erquinigo     DataExtractor section_header_data =
503344546bdSWalter Erquinigo         ReadImageData(section_header_data_offset, section_header_byte_size);
504f754f88fSGreg Clayton 
505c7bece56SGreg Clayton     lldb::offset_t offset = 0;
506b9c1b51eSKate Stone     if (section_header_data.ValidOffsetForDataOfSize(
507b9c1b51eSKate Stone             offset, section_header_byte_size)) {
508f754f88fSGreg Clayton       m_sect_headers.resize(nsects);
509f754f88fSGreg Clayton 
510b9c1b51eSKate Stone       for (uint32_t idx = 0; idx < nsects; ++idx) {
511f754f88fSGreg Clayton         const void *name_data = section_header_data.GetData(&offset, 8);
512b9c1b51eSKate Stone         if (name_data) {
513f754f88fSGreg Clayton           memcpy(m_sect_headers[idx].name, name_data, 8);
514f754f88fSGreg Clayton           m_sect_headers[idx].vmsize = section_header_data.GetU32(&offset);
515f754f88fSGreg Clayton           m_sect_headers[idx].vmaddr = section_header_data.GetU32(&offset);
516f754f88fSGreg Clayton           m_sect_headers[idx].size = section_header_data.GetU32(&offset);
517f754f88fSGreg Clayton           m_sect_headers[idx].offset = section_header_data.GetU32(&offset);
518f754f88fSGreg Clayton           m_sect_headers[idx].reloff = section_header_data.GetU32(&offset);
519f754f88fSGreg Clayton           m_sect_headers[idx].lineoff = section_header_data.GetU32(&offset);
520f754f88fSGreg Clayton           m_sect_headers[idx].nreloc = section_header_data.GetU16(&offset);
521f754f88fSGreg Clayton           m_sect_headers[idx].nline = section_header_data.GetU16(&offset);
522f754f88fSGreg Clayton           m_sect_headers[idx].flags = section_header_data.GetU32(&offset);
523f754f88fSGreg Clayton         }
524f754f88fSGreg Clayton       }
525f754f88fSGreg Clayton     }
526f754f88fSGreg Clayton   }
527f754f88fSGreg Clayton 
528a6682a41SJonas Devlieghere   return !m_sect_headers.empty();
529f754f88fSGreg Clayton }
530f754f88fSGreg Clayton 
5312886e4a0SPavel Labath llvm::StringRef ObjectFilePECOFF::GetSectionName(const section_header_t &sect) {
5322886e4a0SPavel Labath   llvm::StringRef hdr_name(sect.name, llvm::array_lengthof(sect.name));
5332886e4a0SPavel Labath   hdr_name = hdr_name.split('\0').first;
5342886e4a0SPavel Labath   if (hdr_name.consume_front("/")) {
5352886e4a0SPavel Labath     lldb::offset_t stroff;
5362886e4a0SPavel Labath     if (!to_integer(hdr_name, stroff, 10))
5372886e4a0SPavel Labath       return "";
538b9c1b51eSKate Stone     lldb::offset_t string_file_offset =
539b9c1b51eSKate Stone         m_coff_header.symoff + (m_coff_header.nsyms * 18) + stroff;
5402886e4a0SPavel Labath     if (const char *name = m_data.GetCStr(&string_file_offset))
5412886e4a0SPavel Labath       return name;
5422886e4a0SPavel Labath     return "";
543f754f88fSGreg Clayton   }
5442886e4a0SPavel Labath   return hdr_name;
545f754f88fSGreg Clayton }
546f754f88fSGreg Clayton 
547f754f88fSGreg Clayton //----------------------------------------------------------------------
548f754f88fSGreg Clayton // GetNListSymtab
549f754f88fSGreg Clayton //----------------------------------------------------------------------
550b9c1b51eSKate Stone Symtab *ObjectFilePECOFF::GetSymtab() {
551a1743499SGreg Clayton   ModuleSP module_sp(GetModule());
552b9c1b51eSKate Stone   if (module_sp) {
55316ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
554d5b44036SJonas Devlieghere     if (m_symtab_up == NULL) {
555f754f88fSGreg Clayton       SectionList *sect_list = GetSectionList();
556d5b44036SJonas Devlieghere       m_symtab_up.reset(new Symtab(this));
557d5b44036SJonas Devlieghere       std::lock_guard<std::recursive_mutex> guard(m_symtab_up->GetMutex());
55828469ca3SGreg Clayton 
55928469ca3SGreg Clayton       const uint32_t num_syms = m_coff_header.nsyms;
56028469ca3SGreg Clayton 
561344546bdSWalter Erquinigo       if (m_file && num_syms > 0 && m_coff_header.symoff > 0) {
5620076e715SGreg Clayton         const uint32_t symbol_size = 18;
56328469ca3SGreg Clayton         const size_t symbol_data_size = num_syms * symbol_size;
564c35b91ceSAdrian McCarthy         // Include the 4-byte string table size at the end of the symbols
565344546bdSWalter Erquinigo         DataExtractor symtab_data =
566344546bdSWalter Erquinigo             ReadImageData(m_coff_header.symoff, symbol_data_size + 4);
567c7bece56SGreg Clayton         lldb::offset_t offset = symbol_data_size;
56828469ca3SGreg Clayton         const uint32_t strtab_size = symtab_data.GetU32(&offset);
569344546bdSWalter Erquinigo         if (strtab_size > 0) {
570344546bdSWalter Erquinigo           DataExtractor strtab_data = ReadImageData(
571344546bdSWalter Erquinigo               m_coff_header.symoff + symbol_data_size, strtab_size);
57228469ca3SGreg Clayton 
5730076e715SGreg Clayton           // First 4 bytes should be zeroed after strtab_size has been read,
5740076e715SGreg Clayton           // because it is used as offset 0 to encode a NULL string.
575f76e099cSSaleem Abdulrasool           uint32_t *strtab_data_start = const_cast<uint32_t *>(
576f76e099cSSaleem Abdulrasool               reinterpret_cast<const uint32_t *>(strtab_data.GetDataStart()));
5770076e715SGreg Clayton           strtab_data_start[0] = 0;
5780076e715SGreg Clayton 
57928469ca3SGreg Clayton           offset = 0;
58028469ca3SGreg Clayton           std::string symbol_name;
581d5b44036SJonas Devlieghere           Symbol *symbols = m_symtab_up->Resize(num_syms);
582b9c1b51eSKate Stone           for (uint32_t i = 0; i < num_syms; ++i) {
583f754f88fSGreg Clayton             coff_symbol_t symbol;
58428469ca3SGreg Clayton             const uint32_t symbol_offset = offset;
58528469ca3SGreg Clayton             const char *symbol_name_cstr = NULL;
586c35b91ceSAdrian McCarthy             // If the first 4 bytes of the symbol string are zero, then they
587c35b91ceSAdrian McCarthy             // are followed by a 4-byte string table offset. Else these
58828469ca3SGreg Clayton             // 8 bytes contain the symbol name
589b9c1b51eSKate Stone             if (symtab_data.GetU32(&offset) == 0) {
59005097246SAdrian Prantl               // Long string that doesn't fit into the symbol table name, so
59105097246SAdrian Prantl               // now we must read the 4 byte string table offset
59228469ca3SGreg Clayton               uint32_t strtab_offset = symtab_data.GetU32(&offset);
59328469ca3SGreg Clayton               symbol_name_cstr = strtab_data.PeekCStr(strtab_offset);
59428469ca3SGreg Clayton               symbol_name.assign(symbol_name_cstr);
595b9c1b51eSKate Stone             } else {
596b9c1b51eSKate Stone               // Short string that fits into the symbol table name which is 8
597b9c1b51eSKate Stone               // bytes
59828469ca3SGreg Clayton               offset += sizeof(symbol.name) - 4; // Skip remaining
59928469ca3SGreg Clayton               symbol_name_cstr = symtab_data.PeekCStr(symbol_offset);
60028469ca3SGreg Clayton               if (symbol_name_cstr == NULL)
601f754f88fSGreg Clayton                 break;
60228469ca3SGreg Clayton               symbol_name.assign(symbol_name_cstr, sizeof(symbol.name));
60328469ca3SGreg Clayton             }
60428469ca3SGreg Clayton             symbol.value = symtab_data.GetU32(&offset);
60528469ca3SGreg Clayton             symbol.sect = symtab_data.GetU16(&offset);
60628469ca3SGreg Clayton             symbol.type = symtab_data.GetU16(&offset);
60728469ca3SGreg Clayton             symbol.storage = symtab_data.GetU8(&offset);
60828469ca3SGreg Clayton             symbol.naux = symtab_data.GetU8(&offset);
609037520e9SGreg Clayton             symbols[i].GetMangled().SetValue(ConstString(symbol_name.c_str()));
610b9c1b51eSKate Stone             if ((int16_t)symbol.sect >= 1) {
611b9c1b51eSKate Stone               Address symbol_addr(sect_list->GetSectionAtIndex(symbol.sect - 1),
612b9c1b51eSKate Stone                                   symbol.value);
613358cf1eaSGreg Clayton               symbols[i].GetAddressRef() = symbol_addr;
614c35b91ceSAdrian McCarthy               symbols[i].SetType(MapSymbolType(symbol.type));
6150076e715SGreg Clayton             }
616f754f88fSGreg Clayton 
617b9c1b51eSKate Stone             if (symbol.naux > 0) {
618f754f88fSGreg Clayton               i += symbol.naux;
6190076e715SGreg Clayton               offset += symbol_size;
6200076e715SGreg Clayton             }
621f754f88fSGreg Clayton           }
622f754f88fSGreg Clayton         }
623344546bdSWalter Erquinigo       }
624a4fe3a12SVirgile Bello 
625a4fe3a12SVirgile Bello       // Read export header
626b9c1b51eSKate Stone       if (coff_data_dir_export_table < m_coff_header_opt.data_dirs.size() &&
627b9c1b51eSKate Stone           m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmsize > 0 &&
628b9c1b51eSKate Stone           m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmaddr > 0) {
629a4fe3a12SVirgile Bello         export_directory_entry export_table;
630b9c1b51eSKate Stone         uint32_t data_start =
631b9c1b51eSKate Stone             m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmaddr;
632344546bdSWalter Erquinigo 
633344546bdSWalter Erquinigo         uint32_t address_rva = data_start;
634344546bdSWalter Erquinigo         if (m_file) {
635a4fe3a12SVirgile Bello           Address address(m_coff_header_opt.image_base + data_start, sect_list);
636344546bdSWalter Erquinigo           address_rva =
637344546bdSWalter Erquinigo               address.GetSection()->GetFileOffset() + address.GetOffset();
638344546bdSWalter Erquinigo         }
639344546bdSWalter Erquinigo         DataExtractor symtab_data =
640344546bdSWalter Erquinigo             ReadImageData(address_rva, m_coff_header_opt.data_dirs[0].vmsize);
641a4fe3a12SVirgile Bello         lldb::offset_t offset = 0;
642a4fe3a12SVirgile Bello 
643a4fe3a12SVirgile Bello         // Read export_table header
644a4fe3a12SVirgile Bello         export_table.characteristics = symtab_data.GetU32(&offset);
645a4fe3a12SVirgile Bello         export_table.time_date_stamp = symtab_data.GetU32(&offset);
646a4fe3a12SVirgile Bello         export_table.major_version = symtab_data.GetU16(&offset);
647a4fe3a12SVirgile Bello         export_table.minor_version = symtab_data.GetU16(&offset);
648a4fe3a12SVirgile Bello         export_table.name = symtab_data.GetU32(&offset);
649a4fe3a12SVirgile Bello         export_table.base = symtab_data.GetU32(&offset);
650a4fe3a12SVirgile Bello         export_table.number_of_functions = symtab_data.GetU32(&offset);
651a4fe3a12SVirgile Bello         export_table.number_of_names = symtab_data.GetU32(&offset);
652a4fe3a12SVirgile Bello         export_table.address_of_functions = symtab_data.GetU32(&offset);
653a4fe3a12SVirgile Bello         export_table.address_of_names = symtab_data.GetU32(&offset);
654a4fe3a12SVirgile Bello         export_table.address_of_name_ordinals = symtab_data.GetU32(&offset);
655a4fe3a12SVirgile Bello 
656a4fe3a12SVirgile Bello         bool has_ordinal = export_table.address_of_name_ordinals != 0;
657a4fe3a12SVirgile Bello 
658a4fe3a12SVirgile Bello         lldb::offset_t name_offset = export_table.address_of_names - data_start;
659b9c1b51eSKate Stone         lldb::offset_t name_ordinal_offset =
660b9c1b51eSKate Stone             export_table.address_of_name_ordinals - data_start;
661a4fe3a12SVirgile Bello 
662d5b44036SJonas Devlieghere         Symbol *symbols = m_symtab_up->Resize(export_table.number_of_names);
663a4fe3a12SVirgile Bello 
664a4fe3a12SVirgile Bello         std::string symbol_name;
665a4fe3a12SVirgile Bello 
666a4fe3a12SVirgile Bello         // Read each export table entry
667b9c1b51eSKate Stone         for (size_t i = 0; i < export_table.number_of_names; ++i) {
668b9c1b51eSKate Stone           uint32_t name_ordinal =
669b9c1b51eSKate Stone               has_ordinal ? symtab_data.GetU16(&name_ordinal_offset) : i;
670a4fe3a12SVirgile Bello           uint32_t name_address = symtab_data.GetU32(&name_offset);
671a4fe3a12SVirgile Bello 
672b9c1b51eSKate Stone           const char *symbol_name_cstr =
673b9c1b51eSKate Stone               symtab_data.PeekCStr(name_address - data_start);
674a4fe3a12SVirgile Bello           symbol_name.assign(symbol_name_cstr);
675a4fe3a12SVirgile Bello 
676b9c1b51eSKate Stone           lldb::offset_t function_offset = export_table.address_of_functions -
677b9c1b51eSKate Stone                                            data_start +
678b9c1b51eSKate Stone                                            sizeof(uint32_t) * name_ordinal;
679a4fe3a12SVirgile Bello           uint32_t function_rva = symtab_data.GetU32(&function_offset);
680a4fe3a12SVirgile Bello 
681b9c1b51eSKate Stone           Address symbol_addr(m_coff_header_opt.image_base + function_rva,
682b9c1b51eSKate Stone                               sect_list);
683a4fe3a12SVirgile Bello           symbols[i].GetMangled().SetValue(ConstString(symbol_name.c_str()));
684358cf1eaSGreg Clayton           symbols[i].GetAddressRef() = symbol_addr;
685a4fe3a12SVirgile Bello           symbols[i].SetType(lldb::eSymbolTypeCode);
686a4fe3a12SVirgile Bello           symbols[i].SetDebug(true);
687a4fe3a12SVirgile Bello         }
688a4fe3a12SVirgile Bello       }
689d5b44036SJonas Devlieghere       m_symtab_up->CalculateSymbolSizes();
690f754f88fSGreg Clayton     }
691a1743499SGreg Clayton   }
692d5b44036SJonas Devlieghere   return m_symtab_up.get();
693f754f88fSGreg Clayton }
694f754f88fSGreg Clayton 
695b9c1b51eSKate Stone bool ObjectFilePECOFF::IsStripped() {
6963046e668SGreg Clayton   // TODO: determine this for COFF
6973046e668SGreg Clayton   return false;
6983046e668SGreg Clayton }
6993046e668SGreg Clayton 
700b9c1b51eSKate Stone void ObjectFilePECOFF::CreateSections(SectionList &unified_section_list) {
701d5b44036SJonas Devlieghere   if (m_sections_up)
70288a2c2a4SPavel Labath     return;
703d5b44036SJonas Devlieghere   m_sections_up.reset(new SectionList());
7043046e668SGreg Clayton 
705a1743499SGreg Clayton   ModuleSP module_sp(GetModule());
706b9c1b51eSKate Stone   if (module_sp) {
70716ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
7087db8b5c4SPavel Labath 
7097db8b5c4SPavel Labath     SectionSP image_sp = std::make_shared<Section>(
7107db8b5c4SPavel Labath         module_sp, this, ~user_id_t(0), ConstString(), eSectionTypeContainer,
7117db8b5c4SPavel Labath         m_coff_header_opt.image_base, m_coff_header_opt.image_size,
7127db8b5c4SPavel Labath         /*file_offset*/ 0, /*file_size*/ 0, m_coff_header_opt.sect_alignment,
7137db8b5c4SPavel Labath         /*flags*/ 0);
7147db8b5c4SPavel Labath     m_sections_up->AddSection(image_sp);
7157db8b5c4SPavel Labath     unified_section_list.AddSection(image_sp);
7167db8b5c4SPavel Labath 
717f754f88fSGreg Clayton     const uint32_t nsects = m_sect_headers.size();
718e72dfb32SGreg Clayton     ModuleSP module_sp(GetModule());
719b9c1b51eSKate Stone     for (uint32_t idx = 0; idx < nsects; ++idx) {
7202886e4a0SPavel Labath       ConstString const_sect_name(GetSectionName(m_sect_headers[idx]));
72128469ca3SGreg Clayton       static ConstString g_code_sect_name(".code");
72228469ca3SGreg Clayton       static ConstString g_CODE_sect_name("CODE");
72328469ca3SGreg Clayton       static ConstString g_data_sect_name(".data");
72428469ca3SGreg Clayton       static ConstString g_DATA_sect_name("DATA");
72528469ca3SGreg Clayton       static ConstString g_bss_sect_name(".bss");
72628469ca3SGreg Clayton       static ConstString g_BSS_sect_name("BSS");
72728469ca3SGreg Clayton       static ConstString g_debug_sect_name(".debug");
72828469ca3SGreg Clayton       static ConstString g_reloc_sect_name(".reloc");
72928469ca3SGreg Clayton       static ConstString g_stab_sect_name(".stab");
73028469ca3SGreg Clayton       static ConstString g_stabstr_sect_name(".stabstr");
7310076e715SGreg Clayton       static ConstString g_sect_name_dwarf_debug_abbrev(".debug_abbrev");
7320076e715SGreg Clayton       static ConstString g_sect_name_dwarf_debug_aranges(".debug_aranges");
7330076e715SGreg Clayton       static ConstString g_sect_name_dwarf_debug_frame(".debug_frame");
7340076e715SGreg Clayton       static ConstString g_sect_name_dwarf_debug_info(".debug_info");
7350076e715SGreg Clayton       static ConstString g_sect_name_dwarf_debug_line(".debug_line");
7360076e715SGreg Clayton       static ConstString g_sect_name_dwarf_debug_loc(".debug_loc");
737e4dee269SGeorge Rimar       static ConstString g_sect_name_dwarf_debug_loclists(".debug_loclists");
7380076e715SGreg Clayton       static ConstString g_sect_name_dwarf_debug_macinfo(".debug_macinfo");
739a041d848SPavel Labath       static ConstString g_sect_name_dwarf_debug_names(".debug_names");
7400076e715SGreg Clayton       static ConstString g_sect_name_dwarf_debug_pubnames(".debug_pubnames");
7410076e715SGreg Clayton       static ConstString g_sect_name_dwarf_debug_pubtypes(".debug_pubtypes");
7420076e715SGreg Clayton       static ConstString g_sect_name_dwarf_debug_ranges(".debug_ranges");
7430076e715SGreg Clayton       static ConstString g_sect_name_dwarf_debug_str(".debug_str");
7442550ca1eSGreg Clayton       static ConstString g_sect_name_dwarf_debug_types(".debug_types");
7450076e715SGreg Clayton       static ConstString g_sect_name_eh_frame(".eh_frame");
74665d4d5c3SRyan Brown       static ConstString g_sect_name_go_symtab(".gosymtab");
74728469ca3SGreg Clayton       SectionType section_type = eSectionTypeOther;
748237ad974SCharles Davis       if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_CODE &&
749b9c1b51eSKate Stone           ((const_sect_name == g_code_sect_name) ||
750b9c1b51eSKate Stone            (const_sect_name == g_CODE_sect_name))) {
75128469ca3SGreg Clayton         section_type = eSectionTypeCode;
752b9c1b51eSKate Stone       } else if (m_sect_headers[idx].flags &
753b9c1b51eSKate Stone                      llvm::COFF::IMAGE_SCN_CNT_INITIALIZED_DATA &&
754b9c1b51eSKate Stone                  ((const_sect_name == g_data_sect_name) ||
755b9c1b51eSKate Stone                   (const_sect_name == g_DATA_sect_name))) {
7569cad24a7SZachary Turner         if (m_sect_headers[idx].size == 0 && m_sect_headers[idx].offset == 0)
7579cad24a7SZachary Turner           section_type = eSectionTypeZeroFill;
7589cad24a7SZachary Turner         else
75928469ca3SGreg Clayton           section_type = eSectionTypeData;
760b9c1b51eSKate Stone       } else if (m_sect_headers[idx].flags &
761b9c1b51eSKate Stone                      llvm::COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA &&
762b9c1b51eSKate Stone                  ((const_sect_name == g_bss_sect_name) ||
763b9c1b51eSKate Stone                   (const_sect_name == g_BSS_sect_name))) {
76428469ca3SGreg Clayton         if (m_sect_headers[idx].size == 0)
76528469ca3SGreg Clayton           section_type = eSectionTypeZeroFill;
76628469ca3SGreg Clayton         else
76728469ca3SGreg Clayton           section_type = eSectionTypeData;
768b9c1b51eSKate Stone       } else if (const_sect_name == g_debug_sect_name) {
76928469ca3SGreg Clayton         section_type = eSectionTypeDebug;
770b9c1b51eSKate Stone       } else if (const_sect_name == g_stabstr_sect_name) {
77128469ca3SGreg Clayton         section_type = eSectionTypeDataCString;
772b9c1b51eSKate Stone       } else if (const_sect_name == g_reloc_sect_name) {
77328469ca3SGreg Clayton         section_type = eSectionTypeOther;
774b9c1b51eSKate Stone       } else if (const_sect_name == g_sect_name_dwarf_debug_abbrev)
775b9c1b51eSKate Stone         section_type = eSectionTypeDWARFDebugAbbrev;
776b9c1b51eSKate Stone       else if (const_sect_name == g_sect_name_dwarf_debug_aranges)
777b9c1b51eSKate Stone         section_type = eSectionTypeDWARFDebugAranges;
778b9c1b51eSKate Stone       else if (const_sect_name == g_sect_name_dwarf_debug_frame)
779b9c1b51eSKate Stone         section_type = eSectionTypeDWARFDebugFrame;
780b9c1b51eSKate Stone       else if (const_sect_name == g_sect_name_dwarf_debug_info)
781b9c1b51eSKate Stone         section_type = eSectionTypeDWARFDebugInfo;
782b9c1b51eSKate Stone       else if (const_sect_name == g_sect_name_dwarf_debug_line)
783b9c1b51eSKate Stone         section_type = eSectionTypeDWARFDebugLine;
784b9c1b51eSKate Stone       else if (const_sect_name == g_sect_name_dwarf_debug_loc)
785b9c1b51eSKate Stone         section_type = eSectionTypeDWARFDebugLoc;
786e4dee269SGeorge Rimar       else if (const_sect_name == g_sect_name_dwarf_debug_loclists)
787e4dee269SGeorge Rimar         section_type = eSectionTypeDWARFDebugLocLists;
788b9c1b51eSKate Stone       else if (const_sect_name == g_sect_name_dwarf_debug_macinfo)
789b9c1b51eSKate Stone         section_type = eSectionTypeDWARFDebugMacInfo;
790a041d848SPavel Labath       else if (const_sect_name == g_sect_name_dwarf_debug_names)
791a041d848SPavel Labath         section_type = eSectionTypeDWARFDebugNames;
792b9c1b51eSKate Stone       else if (const_sect_name == g_sect_name_dwarf_debug_pubnames)
793b9c1b51eSKate Stone         section_type = eSectionTypeDWARFDebugPubNames;
794b9c1b51eSKate Stone       else if (const_sect_name == g_sect_name_dwarf_debug_pubtypes)
795b9c1b51eSKate Stone         section_type = eSectionTypeDWARFDebugPubTypes;
796b9c1b51eSKate Stone       else if (const_sect_name == g_sect_name_dwarf_debug_ranges)
797b9c1b51eSKate Stone         section_type = eSectionTypeDWARFDebugRanges;
798b9c1b51eSKate Stone       else if (const_sect_name == g_sect_name_dwarf_debug_str)
799b9c1b51eSKate Stone         section_type = eSectionTypeDWARFDebugStr;
8002550ca1eSGreg Clayton       else if (const_sect_name == g_sect_name_dwarf_debug_types)
8012550ca1eSGreg Clayton         section_type = eSectionTypeDWARFDebugTypes;
802b9c1b51eSKate Stone       else if (const_sect_name == g_sect_name_eh_frame)
803b9c1b51eSKate Stone         section_type = eSectionTypeEHFrame;
804b9c1b51eSKate Stone       else if (const_sect_name == g_sect_name_go_symtab)
805b9c1b51eSKate Stone         section_type = eSectionTypeGoSymtab;
806b9c1b51eSKate Stone       else if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_CODE) {
80728469ca3SGreg Clayton         section_type = eSectionTypeCode;
808b9c1b51eSKate Stone       } else if (m_sect_headers[idx].flags &
809b9c1b51eSKate Stone                  llvm::COFF::IMAGE_SCN_CNT_INITIALIZED_DATA) {
81028469ca3SGreg Clayton         section_type = eSectionTypeData;
811b9c1b51eSKate Stone       } else if (m_sect_headers[idx].flags &
812b9c1b51eSKate Stone                  llvm::COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) {
81328469ca3SGreg Clayton         if (m_sect_headers[idx].size == 0)
81428469ca3SGreg Clayton           section_type = eSectionTypeZeroFill;
81528469ca3SGreg Clayton         else
81628469ca3SGreg Clayton           section_type = eSectionTypeData;
81728469ca3SGreg Clayton       }
818f754f88fSGreg Clayton 
819b9c1b51eSKate Stone       SectionSP section_sp(new Section(
8207db8b5c4SPavel Labath           image_sp,        // Parent section
821b9c1b51eSKate Stone           module_sp,       // Module to which this section belongs
822a7499c98SMichael Sartain           this,            // Object file to which this section belongs
8237db8b5c4SPavel Labath           idx + 1,         // Section ID is the 1 based section index.
824f754f88fSGreg Clayton           const_sect_name, // Name of this section
8257db8b5c4SPavel Labath           section_type,
826b9c1b51eSKate Stone           m_sect_headers[idx].vmaddr, // File VM address == addresses as
827b9c1b51eSKate Stone                                       // they are found in the object file
828f754f88fSGreg Clayton           m_sect_headers[idx].vmsize, // VM size in bytes of this section
829b9c1b51eSKate Stone           m_sect_headers[idx]
830b9c1b51eSKate Stone               .offset, // Offset to the data for this section in the file
831b9c1b51eSKate Stone           m_sect_headers[idx]
832b9c1b51eSKate Stone               .size, // Size in bytes of this section as found in the file
83348672afbSGreg Clayton           m_coff_header_opt.sect_alignment, // Section alignment
834f754f88fSGreg Clayton           m_sect_headers[idx].flags));      // Flags for this section
835f754f88fSGreg Clayton 
8367db8b5c4SPavel Labath       image_sp->GetChildren().AddSection(std::move(section_sp));
837f754f88fSGreg Clayton     }
838f754f88fSGreg Clayton   }
839a1743499SGreg Clayton }
840f754f88fSGreg Clayton 
841bd334efdSPavel Labath UUID ObjectFilePECOFF::GetUUID() { return UUID(); }
842f754f88fSGreg Clayton 
843037ed1beSAaron Smith uint32_t ObjectFilePECOFF::ParseDependentModules() {
844037ed1beSAaron Smith   ModuleSP module_sp(GetModule());
845037ed1beSAaron Smith   if (!module_sp)
846f754f88fSGreg Clayton     return 0;
847037ed1beSAaron Smith 
848037ed1beSAaron Smith   std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
849037ed1beSAaron Smith   if (m_deps_filespec)
850037ed1beSAaron Smith     return m_deps_filespec->GetSize();
851037ed1beSAaron Smith 
852037ed1beSAaron Smith   // Cache coff binary if it is not done yet.
853037ed1beSAaron Smith   if (!CreateBinary())
854037ed1beSAaron Smith     return 0;
855037ed1beSAaron Smith 
856037ed1beSAaron Smith   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
857037ed1beSAaron Smith   if (log)
858037ed1beSAaron Smith     log->Printf("%p ObjectFilePECOFF::ParseDependentModules() module = %p "
859037ed1beSAaron Smith                 "(%s), binary = %p (Bin = %p)",
860037ed1beSAaron Smith                 static_cast<void *>(this), static_cast<void *>(module_sp.get()),
861037ed1beSAaron Smith                 module_sp->GetSpecificationDescription().c_str(),
862037ed1beSAaron Smith                 static_cast<void *>(m_owningbin.getPointer()),
863037ed1beSAaron Smith                 m_owningbin ? static_cast<void *>(m_owningbin->getBinary())
864037ed1beSAaron Smith                             : nullptr);
865037ed1beSAaron Smith 
866037ed1beSAaron Smith   auto COFFObj =
867037ed1beSAaron Smith       llvm::dyn_cast<llvm::object::COFFObjectFile>(m_owningbin->getBinary());
868037ed1beSAaron Smith   if (!COFFObj)
869037ed1beSAaron Smith     return 0;
870037ed1beSAaron Smith 
871037ed1beSAaron Smith   m_deps_filespec = FileSpecList();
872037ed1beSAaron Smith 
873037ed1beSAaron Smith   for (const auto &entry : COFFObj->import_directories()) {
874037ed1beSAaron Smith     llvm::StringRef dll_name;
875037ed1beSAaron Smith     auto ec = entry.getName(dll_name);
876037ed1beSAaron Smith     // Report a bogus entry.
877037ed1beSAaron Smith     if (ec != std::error_code()) {
878037ed1beSAaron Smith       if (log)
879037ed1beSAaron Smith         log->Printf("ObjectFilePECOFF::ParseDependentModules() - failed to get "
880037ed1beSAaron Smith                     "import directory entry name: %s",
881037ed1beSAaron Smith                     ec.message().c_str());
882037ed1beSAaron Smith       continue;
883037ed1beSAaron Smith     }
884037ed1beSAaron Smith 
885037ed1beSAaron Smith     // At this moment we only have the base name of the DLL. The full path can
886037ed1beSAaron Smith     // only be seen after the dynamic loading.  Our best guess is Try to get it
887037ed1beSAaron Smith     // with the help of the object file's directory.
888b3f44ad9SStella Stamenova     llvm::SmallString<128> dll_fullpath;
889037ed1beSAaron Smith     FileSpec dll_specs(dll_name);
890037ed1beSAaron Smith     dll_specs.GetDirectory().SetString(m_file.GetDirectory().GetCString());
891037ed1beSAaron Smith 
892037ed1beSAaron Smith     if (!llvm::sys::fs::real_path(dll_specs.GetPath(), dll_fullpath))
893037ed1beSAaron Smith       m_deps_filespec->Append(FileSpec(dll_fullpath));
894037ed1beSAaron Smith     else {
895037ed1beSAaron Smith       // Known DLLs or DLL not found in the object file directory.
896037ed1beSAaron Smith       m_deps_filespec->Append(FileSpec(dll_name));
897037ed1beSAaron Smith     }
898037ed1beSAaron Smith   }
899037ed1beSAaron Smith   return m_deps_filespec->GetSize();
900037ed1beSAaron Smith }
901037ed1beSAaron Smith 
902037ed1beSAaron Smith uint32_t ObjectFilePECOFF::GetDependentModules(FileSpecList &files) {
903037ed1beSAaron Smith   auto num_modules = ParseDependentModules();
904037ed1beSAaron Smith   auto original_size = files.GetSize();
905037ed1beSAaron Smith 
906037ed1beSAaron Smith   for (unsigned i = 0; i < num_modules; ++i)
907037ed1beSAaron Smith     files.AppendIfUnique(m_deps_filespec->GetFileSpecAtIndex(i));
908037ed1beSAaron Smith 
909037ed1beSAaron Smith   return files.GetSize() - original_size;
910f754f88fSGreg Clayton }
911f754f88fSGreg Clayton 
912b9c1b51eSKate Stone lldb_private::Address ObjectFilePECOFF::GetEntryPointAddress() {
9138e38c666SStephane Sezer   if (m_entry_point_address.IsValid())
9148e38c666SStephane Sezer     return m_entry_point_address;
9158e38c666SStephane Sezer 
9168e38c666SStephane Sezer   if (!ParseHeader() || !IsExecutable())
9178e38c666SStephane Sezer     return m_entry_point_address;
9188e38c666SStephane Sezer 
9198e38c666SStephane Sezer   SectionList *section_list = GetSectionList();
920a5235af9SAleksandr Urakov   addr_t file_addr = m_coff_header_opt.entry + m_coff_header_opt.image_base;
9218e38c666SStephane Sezer 
9228e38c666SStephane Sezer   if (!section_list)
923a5235af9SAleksandr Urakov     m_entry_point_address.SetOffset(file_addr);
9248e38c666SStephane Sezer   else
925a5235af9SAleksandr Urakov     m_entry_point_address.ResolveAddressUsingFileSections(file_addr, section_list);
9268e38c666SStephane Sezer   return m_entry_point_address;
9278e38c666SStephane Sezer }
9288e38c666SStephane Sezer 
929*d1304bbaSPavel Labath Address ObjectFilePECOFF::GetBaseAddress() {
930*d1304bbaSPavel Labath   return Address(GetSectionList()->GetSectionAtIndex(0), 0);
931*d1304bbaSPavel Labath }
932*d1304bbaSPavel Labath 
933f754f88fSGreg Clayton //----------------------------------------------------------------------
934f754f88fSGreg Clayton // Dump
935f754f88fSGreg Clayton //
936f754f88fSGreg Clayton // Dump the specifics of the runtime file container (such as any headers
937f754f88fSGreg Clayton // segments, sections, etc).
938f754f88fSGreg Clayton //----------------------------------------------------------------------
939b9c1b51eSKate Stone void ObjectFilePECOFF::Dump(Stream *s) {
940a1743499SGreg Clayton   ModuleSP module_sp(GetModule());
941b9c1b51eSKate Stone   if (module_sp) {
94216ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
943324a1036SSaleem Abdulrasool     s->Printf("%p: ", static_cast<void *>(this));
944f754f88fSGreg Clayton     s->Indent();
945f754f88fSGreg Clayton     s->PutCString("ObjectFilePECOFF");
946f754f88fSGreg Clayton 
947f760f5aeSPavel Labath     ArchSpec header_arch = GetArchitecture();
948f754f88fSGreg Clayton 
949b9c1b51eSKate Stone     *s << ", file = '" << m_file
950b9c1b51eSKate Stone        << "', arch = " << header_arch.GetArchitectureName() << "\n";
951f754f88fSGreg Clayton 
9523046e668SGreg Clayton     SectionList *sections = GetSectionList();
9533046e668SGreg Clayton     if (sections)
9543046e668SGreg Clayton       sections->Dump(s, NULL, true, UINT32_MAX);
955f754f88fSGreg Clayton 
956d5b44036SJonas Devlieghere     if (m_symtab_up)
957d5b44036SJonas Devlieghere       m_symtab_up->Dump(s, NULL, eSortOrderNone);
958f754f88fSGreg Clayton 
959f754f88fSGreg Clayton     if (m_dos_header.e_magic)
960f754f88fSGreg Clayton       DumpDOSHeader(s, m_dos_header);
961b9c1b51eSKate Stone     if (m_coff_header.machine) {
962f754f88fSGreg Clayton       DumpCOFFHeader(s, m_coff_header);
963f754f88fSGreg Clayton       if (m_coff_header.hdrsize)
964f754f88fSGreg Clayton         DumpOptCOFFHeader(s, m_coff_header_opt);
965f754f88fSGreg Clayton     }
966f754f88fSGreg Clayton     s->EOL();
967f754f88fSGreg Clayton     DumpSectionHeaders(s);
968f754f88fSGreg Clayton     s->EOL();
969037ed1beSAaron Smith 
970037ed1beSAaron Smith     DumpDependentModules(s);
971037ed1beSAaron Smith     s->EOL();
972f754f88fSGreg Clayton   }
973a1743499SGreg Clayton }
974f754f88fSGreg Clayton 
975f754f88fSGreg Clayton //----------------------------------------------------------------------
976f754f88fSGreg Clayton // DumpDOSHeader
977f754f88fSGreg Clayton //
978f754f88fSGreg Clayton // Dump the MS-DOS header to the specified output stream
979f754f88fSGreg Clayton //----------------------------------------------------------------------
980b9c1b51eSKate Stone void ObjectFilePECOFF::DumpDOSHeader(Stream *s, const dos_header_t &header) {
981f754f88fSGreg Clayton   s->PutCString("MSDOS Header\n");
982f754f88fSGreg Clayton   s->Printf("  e_magic    = 0x%4.4x\n", header.e_magic);
983f754f88fSGreg Clayton   s->Printf("  e_cblp     = 0x%4.4x\n", header.e_cblp);
984f754f88fSGreg Clayton   s->Printf("  e_cp       = 0x%4.4x\n", header.e_cp);
985f754f88fSGreg Clayton   s->Printf("  e_crlc     = 0x%4.4x\n", header.e_crlc);
986f754f88fSGreg Clayton   s->Printf("  e_cparhdr  = 0x%4.4x\n", header.e_cparhdr);
987f754f88fSGreg Clayton   s->Printf("  e_minalloc = 0x%4.4x\n", header.e_minalloc);
988f754f88fSGreg Clayton   s->Printf("  e_maxalloc = 0x%4.4x\n", header.e_maxalloc);
989f754f88fSGreg Clayton   s->Printf("  e_ss       = 0x%4.4x\n", header.e_ss);
990f754f88fSGreg Clayton   s->Printf("  e_sp       = 0x%4.4x\n", header.e_sp);
991f754f88fSGreg Clayton   s->Printf("  e_csum     = 0x%4.4x\n", header.e_csum);
992f754f88fSGreg Clayton   s->Printf("  e_ip       = 0x%4.4x\n", header.e_ip);
993f754f88fSGreg Clayton   s->Printf("  e_cs       = 0x%4.4x\n", header.e_cs);
994f754f88fSGreg Clayton   s->Printf("  e_lfarlc   = 0x%4.4x\n", header.e_lfarlc);
995f754f88fSGreg Clayton   s->Printf("  e_ovno     = 0x%4.4x\n", header.e_ovno);
996f754f88fSGreg Clayton   s->Printf("  e_res[4]   = { 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x }\n",
997b9c1b51eSKate Stone             header.e_res[0], header.e_res[1], header.e_res[2], header.e_res[3]);
998f754f88fSGreg Clayton   s->Printf("  e_oemid    = 0x%4.4x\n", header.e_oemid);
999f754f88fSGreg Clayton   s->Printf("  e_oeminfo  = 0x%4.4x\n", header.e_oeminfo);
1000b9c1b51eSKate Stone   s->Printf("  e_res2[10] = { 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, "
1001b9c1b51eSKate Stone             "0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x }\n",
1002b9c1b51eSKate Stone             header.e_res2[0], header.e_res2[1], header.e_res2[2],
1003b9c1b51eSKate Stone             header.e_res2[3], header.e_res2[4], header.e_res2[5],
1004b9c1b51eSKate Stone             header.e_res2[6], header.e_res2[7], header.e_res2[8],
1005f754f88fSGreg Clayton             header.e_res2[9]);
1006f754f88fSGreg Clayton   s->Printf("  e_lfanew   = 0x%8.8x\n", header.e_lfanew);
1007f754f88fSGreg Clayton }
1008f754f88fSGreg Clayton 
1009f754f88fSGreg Clayton //----------------------------------------------------------------------
1010f754f88fSGreg Clayton // DumpCOFFHeader
1011f754f88fSGreg Clayton //
1012f754f88fSGreg Clayton // Dump the COFF header to the specified output stream
1013f754f88fSGreg Clayton //----------------------------------------------------------------------
1014b9c1b51eSKate Stone void ObjectFilePECOFF::DumpCOFFHeader(Stream *s, const coff_header_t &header) {
1015f754f88fSGreg Clayton   s->PutCString("COFF Header\n");
1016f754f88fSGreg Clayton   s->Printf("  machine = 0x%4.4x\n", header.machine);
1017f754f88fSGreg Clayton   s->Printf("  nsects  = 0x%4.4x\n", header.nsects);
1018f754f88fSGreg Clayton   s->Printf("  modtime = 0x%8.8x\n", header.modtime);
1019f754f88fSGreg Clayton   s->Printf("  symoff  = 0x%8.8x\n", header.symoff);
1020f754f88fSGreg Clayton   s->Printf("  nsyms   = 0x%8.8x\n", header.nsyms);
1021f754f88fSGreg Clayton   s->Printf("  hdrsize = 0x%4.4x\n", header.hdrsize);
1022f754f88fSGreg Clayton }
1023f754f88fSGreg Clayton 
1024f754f88fSGreg Clayton //----------------------------------------------------------------------
1025f754f88fSGreg Clayton // DumpOptCOFFHeader
1026f754f88fSGreg Clayton //
1027f754f88fSGreg Clayton // Dump the optional COFF header to the specified output stream
1028f754f88fSGreg Clayton //----------------------------------------------------------------------
1029b9c1b51eSKate Stone void ObjectFilePECOFF::DumpOptCOFFHeader(Stream *s,
1030b9c1b51eSKate Stone                                          const coff_opt_header_t &header) {
1031f754f88fSGreg Clayton   s->PutCString("Optional COFF Header\n");
1032f754f88fSGreg Clayton   s->Printf("  magic                   = 0x%4.4x\n", header.magic);
1033b9c1b51eSKate Stone   s->Printf("  major_linker_version    = 0x%2.2x\n",
1034b9c1b51eSKate Stone             header.major_linker_version);
1035b9c1b51eSKate Stone   s->Printf("  minor_linker_version    = 0x%2.2x\n",
1036b9c1b51eSKate Stone             header.minor_linker_version);
1037f754f88fSGreg Clayton   s->Printf("  code_size               = 0x%8.8x\n", header.code_size);
1038f754f88fSGreg Clayton   s->Printf("  data_size               = 0x%8.8x\n", header.data_size);
1039f754f88fSGreg Clayton   s->Printf("  bss_size                = 0x%8.8x\n", header.bss_size);
1040f754f88fSGreg Clayton   s->Printf("  entry                   = 0x%8.8x\n", header.entry);
1041f754f88fSGreg Clayton   s->Printf("  code_offset             = 0x%8.8x\n", header.code_offset);
1042f754f88fSGreg Clayton   s->Printf("  data_offset             = 0x%8.8x\n", header.data_offset);
1043b9c1b51eSKate Stone   s->Printf("  image_base              = 0x%16.16" PRIx64 "\n",
1044b9c1b51eSKate Stone             header.image_base);
1045f754f88fSGreg Clayton   s->Printf("  sect_alignment          = 0x%8.8x\n", header.sect_alignment);
1046f754f88fSGreg Clayton   s->Printf("  file_alignment          = 0x%8.8x\n", header.file_alignment);
1047b9c1b51eSKate Stone   s->Printf("  major_os_system_version = 0x%4.4x\n",
1048b9c1b51eSKate Stone             header.major_os_system_version);
1049b9c1b51eSKate Stone   s->Printf("  minor_os_system_version = 0x%4.4x\n",
1050b9c1b51eSKate Stone             header.minor_os_system_version);
1051b9c1b51eSKate Stone   s->Printf("  major_image_version     = 0x%4.4x\n",
1052b9c1b51eSKate Stone             header.major_image_version);
1053b9c1b51eSKate Stone   s->Printf("  minor_image_version     = 0x%4.4x\n",
1054b9c1b51eSKate Stone             header.minor_image_version);
1055b9c1b51eSKate Stone   s->Printf("  major_subsystem_version = 0x%4.4x\n",
1056b9c1b51eSKate Stone             header.major_subsystem_version);
1057b9c1b51eSKate Stone   s->Printf("  minor_subsystem_version = 0x%4.4x\n",
1058b9c1b51eSKate Stone             header.minor_subsystem_version);
1059f754f88fSGreg Clayton   s->Printf("  reserved1               = 0x%8.8x\n", header.reserved1);
1060f754f88fSGreg Clayton   s->Printf("  image_size              = 0x%8.8x\n", header.image_size);
1061f754f88fSGreg Clayton   s->Printf("  header_size             = 0x%8.8x\n", header.header_size);
106228469ca3SGreg Clayton   s->Printf("  checksum                = 0x%8.8x\n", header.checksum);
1063f754f88fSGreg Clayton   s->Printf("  subsystem               = 0x%4.4x\n", header.subsystem);
1064f754f88fSGreg Clayton   s->Printf("  dll_flags               = 0x%4.4x\n", header.dll_flags);
1065b9c1b51eSKate Stone   s->Printf("  stack_reserve_size      = 0x%16.16" PRIx64 "\n",
1066b9c1b51eSKate Stone             header.stack_reserve_size);
1067b9c1b51eSKate Stone   s->Printf("  stack_commit_size       = 0x%16.16" PRIx64 "\n",
1068b9c1b51eSKate Stone             header.stack_commit_size);
1069b9c1b51eSKate Stone   s->Printf("  heap_reserve_size       = 0x%16.16" PRIx64 "\n",
1070b9c1b51eSKate Stone             header.heap_reserve_size);
1071b9c1b51eSKate Stone   s->Printf("  heap_commit_size        = 0x%16.16" PRIx64 "\n",
1072b9c1b51eSKate Stone             header.heap_commit_size);
1073f754f88fSGreg Clayton   s->Printf("  loader_flags            = 0x%8.8x\n", header.loader_flags);
1074b9c1b51eSKate Stone   s->Printf("  num_data_dir_entries    = 0x%8.8x\n",
1075b9c1b51eSKate Stone             (uint32_t)header.data_dirs.size());
1076f754f88fSGreg Clayton   uint32_t i;
1077b9c1b51eSKate Stone   for (i = 0; i < header.data_dirs.size(); i++) {
1078b9c1b51eSKate Stone     s->Printf("  data_dirs[%2u] vmaddr = 0x%8.8x, vmsize = 0x%8.8x\n", i,
1079b9c1b51eSKate Stone               header.data_dirs[i].vmaddr, header.data_dirs[i].vmsize);
1080f754f88fSGreg Clayton   }
1081f754f88fSGreg Clayton }
1082f754f88fSGreg Clayton //----------------------------------------------------------------------
1083f754f88fSGreg Clayton // DumpSectionHeader
1084f754f88fSGreg Clayton //
1085f754f88fSGreg Clayton // Dump a single ELF section header to the specified output stream
1086f754f88fSGreg Clayton //----------------------------------------------------------------------
1087b9c1b51eSKate Stone void ObjectFilePECOFF::DumpSectionHeader(Stream *s,
1088b9c1b51eSKate Stone                                          const section_header_t &sh) {
10892886e4a0SPavel Labath   std::string name = GetSectionName(sh);
1090b9c1b51eSKate 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 "
1091b9c1b51eSKate Stone             "0x%4.4x 0x%8.8x\n",
1092b9c1b51eSKate Stone             name.c_str(), sh.vmaddr, sh.vmsize, sh.offset, sh.size, sh.reloff,
1093b9c1b51eSKate Stone             sh.lineoff, sh.nreloc, sh.nline, sh.flags);
1094f754f88fSGreg Clayton }
1095f754f88fSGreg Clayton 
1096f754f88fSGreg Clayton //----------------------------------------------------------------------
1097f754f88fSGreg Clayton // DumpSectionHeaders
1098f754f88fSGreg Clayton //
1099f754f88fSGreg Clayton // Dump all of the ELF section header to the specified output stream
1100f754f88fSGreg Clayton //----------------------------------------------------------------------
1101b9c1b51eSKate Stone void ObjectFilePECOFF::DumpSectionHeaders(Stream *s) {
1102f754f88fSGreg Clayton 
1103f754f88fSGreg Clayton   s->PutCString("Section Headers\n");
1104b9c1b51eSKate Stone   s->PutCString("IDX  name             vm addr    vm size    file off   file "
1105b9c1b51eSKate Stone                 "size  reloc off  line off   nreloc nline  flags\n");
1106b9c1b51eSKate Stone   s->PutCString("==== ---------------- ---------- ---------- ---------- "
1107b9c1b51eSKate Stone                 "---------- ---------- ---------- ------ ------ ----------\n");
1108f754f88fSGreg Clayton 
1109f754f88fSGreg Clayton   uint32_t idx = 0;
1110f754f88fSGreg Clayton   SectionHeaderCollIter pos, end = m_sect_headers.end();
1111f754f88fSGreg Clayton 
1112b9c1b51eSKate Stone   for (pos = m_sect_headers.begin(); pos != end; ++pos, ++idx) {
1113f754f88fSGreg Clayton     s->Printf("[%2u] ", idx);
1114f754f88fSGreg Clayton     ObjectFilePECOFF::DumpSectionHeader(s, *pos);
1115f754f88fSGreg Clayton   }
1116f754f88fSGreg Clayton }
1117f754f88fSGreg Clayton 
1118037ed1beSAaron Smith //----------------------------------------------------------------------
1119037ed1beSAaron Smith // DumpDependentModules
1120037ed1beSAaron Smith //
1121037ed1beSAaron Smith // Dump all of the dependent modules to the specified output stream
1122037ed1beSAaron Smith //----------------------------------------------------------------------
1123037ed1beSAaron Smith void ObjectFilePECOFF::DumpDependentModules(lldb_private::Stream *s) {
1124037ed1beSAaron Smith   auto num_modules = ParseDependentModules();
1125037ed1beSAaron Smith   if (num_modules > 0) {
1126037ed1beSAaron Smith     s->PutCString("Dependent Modules\n");
1127037ed1beSAaron Smith     for (unsigned i = 0; i < num_modules; ++i) {
1128037ed1beSAaron Smith       auto spec = m_deps_filespec->GetFileSpecAtIndex(i);
1129037ed1beSAaron Smith       s->Printf("  %s\n", spec.GetFilename().GetCString());
1130037ed1beSAaron Smith     }
1131037ed1beSAaron Smith   }
1132037ed1beSAaron Smith }
1133037ed1beSAaron Smith 
1134fb3b3bd1SZachary Turner bool ObjectFilePECOFF::IsWindowsSubsystem() {
1135fb3b3bd1SZachary Turner   switch (m_coff_header_opt.subsystem) {
1136fb3b3bd1SZachary Turner   case llvm::COFF::IMAGE_SUBSYSTEM_NATIVE:
1137fb3b3bd1SZachary Turner   case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_GUI:
1138fb3b3bd1SZachary Turner   case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_CUI:
1139fb3b3bd1SZachary Turner   case llvm::COFF::IMAGE_SUBSYSTEM_NATIVE_WINDOWS:
1140fb3b3bd1SZachary Turner   case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_CE_GUI:
1141fb3b3bd1SZachary Turner   case llvm::COFF::IMAGE_SUBSYSTEM_XBOX:
1142fb3b3bd1SZachary Turner   case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_BOOT_APPLICATION:
1143fb3b3bd1SZachary Turner     return true;
1144fb3b3bd1SZachary Turner   default:
1145fb3b3bd1SZachary Turner     return false;
1146fb3b3bd1SZachary Turner   }
1147fb3b3bd1SZachary Turner }
1148fb3b3bd1SZachary Turner 
1149f760f5aeSPavel Labath ArchSpec ObjectFilePECOFF::GetArchitecture() {
1150237ad974SCharles Davis   uint16_t machine = m_coff_header.machine;
1151b9c1b51eSKate Stone   switch (machine) {
1152f760f5aeSPavel Labath   default:
1153f760f5aeSPavel Labath     break;
1154237ad974SCharles Davis   case llvm::COFF::IMAGE_FILE_MACHINE_AMD64:
1155237ad974SCharles Davis   case llvm::COFF::IMAGE_FILE_MACHINE_I386:
1156237ad974SCharles Davis   case llvm::COFF::IMAGE_FILE_MACHINE_POWERPC:
1157237ad974SCharles Davis   case llvm::COFF::IMAGE_FILE_MACHINE_POWERPCFP:
1158237ad974SCharles Davis   case llvm::COFF::IMAGE_FILE_MACHINE_ARM:
11591108cb36SSaleem Abdulrasool   case llvm::COFF::IMAGE_FILE_MACHINE_ARMNT:
1160237ad974SCharles Davis   case llvm::COFF::IMAGE_FILE_MACHINE_THUMB:
1161f760f5aeSPavel Labath     ArchSpec arch;
1162fb3b3bd1SZachary Turner     arch.SetArchitecture(eArchTypeCOFF, machine, LLDB_INVALID_CPUTYPE,
1163fb3b3bd1SZachary Turner                          IsWindowsSubsystem() ? llvm::Triple::Win32
1164fb3b3bd1SZachary Turner                                               : llvm::Triple::UnknownOS);
1165f760f5aeSPavel Labath     return arch;
1166237ad974SCharles Davis   }
1167f760f5aeSPavel Labath   return ArchSpec();
1168f754f88fSGreg Clayton }
1169f754f88fSGreg Clayton 
1170b9c1b51eSKate Stone ObjectFile::Type ObjectFilePECOFF::CalculateType() {
1171b9c1b51eSKate Stone   if (m_coff_header.machine != 0) {
1172237ad974SCharles Davis     if ((m_coff_header.flags & llvm::COFF::IMAGE_FILE_DLL) == 0)
1173f754f88fSGreg Clayton       return eTypeExecutable;
1174f754f88fSGreg Clayton     else
1175f754f88fSGreg Clayton       return eTypeSharedLibrary;
1176f754f88fSGreg Clayton   }
1177f754f88fSGreg Clayton   return eTypeExecutable;
1178f754f88fSGreg Clayton }
1179f754f88fSGreg Clayton 
1180b9c1b51eSKate Stone ObjectFile::Strata ObjectFilePECOFF::CalculateStrata() { return eStrataUser; }
11819cad24a7SZachary Turner 
1182f754f88fSGreg Clayton //------------------------------------------------------------------
1183f754f88fSGreg Clayton // PluginInterface protocol
1184f754f88fSGreg Clayton //------------------------------------------------------------------
1185b9c1b51eSKate Stone ConstString ObjectFilePECOFF::GetPluginName() { return GetPluginNameStatic(); }
1186f754f88fSGreg Clayton 
1187b9c1b51eSKate Stone uint32_t ObjectFilePECOFF::GetPluginVersion() { return 1; }
1188