180814287SRaphael Isemann //===-- ObjectFilePECOFF.cpp ----------------------------------------------===//
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"
1030c2441aSAleksandr Urakov #include "PECallFrameInfo.h"
11f7d1893fSAdrian McCarthy #include "WindowsMiniDump.h"
12f754f88fSGreg Clayton 
13f754f88fSGreg Clayton #include "lldb/Core/FileSpecList.h"
14f754f88fSGreg Clayton #include "lldb/Core/Module.h"
15f4d6de6aSGreg Clayton #include "lldb/Core/ModuleSpec.h"
16f754f88fSGreg Clayton #include "lldb/Core/PluginManager.h"
17f754f88fSGreg Clayton #include "lldb/Core/Section.h"
18f754f88fSGreg Clayton #include "lldb/Core/StreamFile.h"
19f754f88fSGreg Clayton #include "lldb/Symbol/ObjectFile.h"
20f7d1893fSAdrian McCarthy #include "lldb/Target/Process.h"
212756adf3SVirgile Bello #include "lldb/Target/SectionLoadList.h"
222756adf3SVirgile Bello #include "lldb/Target/Target.h"
235f19b907SPavel Labath #include "lldb/Utility/ArchSpec.h"
24666cc0b2SZachary Turner #include "lldb/Utility/DataBufferHeap.h"
255713a05bSZachary Turner #include "lldb/Utility/FileSpec.h"
26037ed1beSAaron Smith #include "lldb/Utility/Log.h"
27bf9a7730SZachary Turner #include "lldb/Utility/StreamString.h"
2838d0632eSPavel Labath #include "lldb/Utility/Timer.h"
29666cc0b2SZachary Turner #include "lldb/Utility/UUID.h"
305f19b907SPavel Labath #include "llvm/BinaryFormat/COFF.h"
31f754f88fSGreg Clayton 
32037ed1beSAaron Smith #include "llvm/Object/COFFImportFile.h"
33037ed1beSAaron Smith #include "llvm/Support/Error.h"
343f4a4b36SZachary Turner #include "llvm/Support/MemoryBuffer.h"
353f4a4b36SZachary Turner 
36f754f88fSGreg Clayton #define IMAGE_DOS_SIGNATURE 0x5A4D    // MZ
37f754f88fSGreg Clayton #define IMAGE_NT_SIGNATURE 0x00004550 // PE00
38f754f88fSGreg Clayton #define OPT_HEADER_MAGIC_PE32 0x010b
39f754f88fSGreg Clayton #define OPT_HEADER_MAGIC_PE32_PLUS 0x020b
40f754f88fSGreg Clayton 
41f754f88fSGreg Clayton using namespace lldb;
42f754f88fSGreg Clayton using namespace lldb_private;
43f754f88fSGreg Clayton 
44bba9ba8dSJonas Devlieghere LLDB_PLUGIN_DEFINE(ObjectFilePECOFF)
45fbb4d1e4SJonas Devlieghere 
46b8d03935SAaron Smith struct CVInfoPdb70 {
47b8d03935SAaron Smith   // 16-byte GUID
48b8d03935SAaron Smith   struct _Guid {
49b8d03935SAaron Smith     llvm::support::ulittle32_t Data1;
50b8d03935SAaron Smith     llvm::support::ulittle16_t Data2;
51b8d03935SAaron Smith     llvm::support::ulittle16_t Data3;
52b8d03935SAaron Smith     uint8_t Data4[8];
53b8d03935SAaron Smith   } Guid;
54b8d03935SAaron Smith 
55b8d03935SAaron Smith   llvm::support::ulittle32_t Age;
56b8d03935SAaron Smith };
57b8d03935SAaron Smith 
58b8d03935SAaron Smith static UUID GetCoffUUID(llvm::object::COFFObjectFile *coff_obj) {
59b8d03935SAaron Smith   if (!coff_obj)
60b8d03935SAaron Smith     return UUID();
61b8d03935SAaron Smith 
62b8d03935SAaron Smith   const llvm::codeview::DebugInfo *pdb_info = nullptr;
63b8d03935SAaron Smith   llvm::StringRef pdb_file;
64b8d03935SAaron Smith 
65b8d03935SAaron Smith   // This part is similar with what has done in minidump parser.
66b8d03935SAaron Smith   if (!coff_obj->getDebugPDBInfo(pdb_info, pdb_file) && pdb_info) {
67b8d03935SAaron Smith     if (pdb_info->PDB70.CVSignature == llvm::OMF::Signature::PDB70) {
68b8d03935SAaron Smith       using llvm::support::endian::read16be;
69b8d03935SAaron Smith       using llvm::support::endian::read32be;
70b8d03935SAaron Smith 
71b8d03935SAaron Smith       const uint8_t *sig = pdb_info->PDB70.Signature;
72b8d03935SAaron Smith       struct CVInfoPdb70 info;
73b8d03935SAaron Smith       info.Guid.Data1 = read32be(sig);
74b8d03935SAaron Smith       sig += 4;
75b8d03935SAaron Smith       info.Guid.Data2 = read16be(sig);
76b8d03935SAaron Smith       sig += 2;
77b8d03935SAaron Smith       info.Guid.Data3 = read16be(sig);
78b8d03935SAaron Smith       sig += 2;
79b8d03935SAaron Smith       memcpy(info.Guid.Data4, sig, 8);
80b8d03935SAaron Smith 
81b8d03935SAaron Smith       // Return 20-byte UUID if the Age is not zero
82b8d03935SAaron Smith       if (pdb_info->PDB70.Age) {
83b8d03935SAaron Smith         info.Age = read32be(&pdb_info->PDB70.Age);
84b8d03935SAaron Smith         return UUID::fromOptionalData(&info, sizeof(info));
85b8d03935SAaron Smith       }
86b8d03935SAaron Smith       // Otherwise return 16-byte GUID
87b8d03935SAaron Smith       return UUID::fromOptionalData(&info.Guid, sizeof(info.Guid));
88b8d03935SAaron Smith     }
89b8d03935SAaron Smith   }
90b8d03935SAaron Smith 
91b8d03935SAaron Smith   return UUID();
92b8d03935SAaron Smith }
93b8d03935SAaron Smith 
94e84f7841SPavel Labath char ObjectFilePECOFF::ID;
95e84f7841SPavel Labath 
96b9c1b51eSKate Stone void ObjectFilePECOFF::Initialize() {
97b9c1b51eSKate Stone   PluginManager::RegisterPlugin(
98b9c1b51eSKate Stone       GetPluginNameStatic(), GetPluginDescriptionStatic(), CreateInstance,
99b9c1b51eSKate Stone       CreateMemoryInstance, GetModuleSpecifications, SaveCore);
100f754f88fSGreg Clayton }
101f754f88fSGreg Clayton 
102b9c1b51eSKate Stone void ObjectFilePECOFF::Terminate() {
103f754f88fSGreg Clayton   PluginManager::UnregisterPlugin(CreateInstance);
104f754f88fSGreg Clayton }
105f754f88fSGreg Clayton 
106b9c1b51eSKate Stone lldb_private::ConstString ObjectFilePECOFF::GetPluginNameStatic() {
10757abc5d6SGreg Clayton   static ConstString g_name("pe-coff");
10857abc5d6SGreg Clayton   return g_name;
109f754f88fSGreg Clayton }
110f754f88fSGreg Clayton 
111b9c1b51eSKate Stone const char *ObjectFilePECOFF::GetPluginDescriptionStatic() {
112b9c1b51eSKate Stone   return "Portable Executable and Common Object File Format object file reader "
113b9c1b51eSKate Stone          "(32 and 64 bit)";
114f754f88fSGreg Clayton }
115f754f88fSGreg Clayton 
116b9c1b51eSKate Stone ObjectFile *ObjectFilePECOFF::CreateInstance(const lldb::ModuleSP &module_sp,
1175ce9c565SGreg Clayton                                              DataBufferSP &data_sp,
1185ce9c565SGreg Clayton                                              lldb::offset_t data_offset,
11928e4942bSPavel Labath                                              const lldb_private::FileSpec *file_p,
1205ce9c565SGreg Clayton                                              lldb::offset_t file_offset,
121b9c1b51eSKate Stone                                              lldb::offset_t length) {
12228e4942bSPavel Labath   FileSpec file = file_p ? *file_p : FileSpec();
123b9c1b51eSKate Stone   if (!data_sp) {
12450251fc7SPavel Labath     data_sp = MapFileData(file, length, file_offset);
1253f4a4b36SZachary Turner     if (!data_sp)
1263f4a4b36SZachary Turner       return nullptr;
1275ce9c565SGreg Clayton     data_offset = 0;
1285ce9c565SGreg Clayton   }
1295ce9c565SGreg Clayton 
1303f4a4b36SZachary Turner   if (!ObjectFilePECOFF::MagicBytesMatch(data_sp))
1313f4a4b36SZachary Turner     return nullptr;
1323f4a4b36SZachary Turner 
1335ce9c565SGreg Clayton   // Update the data to contain the entire file if it doesn't already
1343f4a4b36SZachary Turner   if (data_sp->GetByteSize() < length) {
13550251fc7SPavel Labath     data_sp = MapFileData(file, length, file_offset);
1363f4a4b36SZachary Turner     if (!data_sp)
1373f4a4b36SZachary Turner       return nullptr;
138f754f88fSGreg Clayton   }
1393f4a4b36SZachary Turner 
140a8f3ae7cSJonas Devlieghere   auto objfile_up = std::make_unique<ObjectFilePECOFF>(
14128e4942bSPavel Labath       module_sp, data_sp, data_offset, file_p, file_offset, length);
142d5b44036SJonas Devlieghere   if (!objfile_up || !objfile_up->ParseHeader())
1433f4a4b36SZachary Turner     return nullptr;
1443f4a4b36SZachary Turner 
145037ed1beSAaron Smith   // Cache coff binary.
146d5b44036SJonas Devlieghere   if (!objfile_up->CreateBinary())
147037ed1beSAaron Smith     return nullptr;
148037ed1beSAaron Smith 
149d5b44036SJonas Devlieghere   return objfile_up.release();
150f754f88fSGreg Clayton }
151f754f88fSGreg Clayton 
152b9c1b51eSKate Stone ObjectFile *ObjectFilePECOFF::CreateMemoryInstance(
153b9c1b51eSKate Stone     const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp,
154b9c1b51eSKate Stone     const lldb::ProcessSP &process_sp, lldb::addr_t header_addr) {
155344546bdSWalter Erquinigo   if (!data_sp || !ObjectFilePECOFF::MagicBytesMatch(data_sp))
156344546bdSWalter Erquinigo     return nullptr;
157a8f3ae7cSJonas Devlieghere   auto objfile_up = std::make_unique<ObjectFilePECOFF>(
158344546bdSWalter Erquinigo       module_sp, data_sp, process_sp, header_addr);
159d5b44036SJonas Devlieghere   if (objfile_up.get() && objfile_up->ParseHeader()) {
160d5b44036SJonas Devlieghere     return objfile_up.release();
161344546bdSWalter Erquinigo   }
162344546bdSWalter Erquinigo   return nullptr;
163c9660546SGreg Clayton }
164c9660546SGreg Clayton 
165b9c1b51eSKate Stone size_t ObjectFilePECOFF::GetModuleSpecifications(
166b9c1b51eSKate Stone     const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp,
167b9c1b51eSKate Stone     lldb::offset_t data_offset, lldb::offset_t file_offset,
168b9c1b51eSKate Stone     lldb::offset_t length, lldb_private::ModuleSpecList &specs) {
16989eb1baeSVirgile Bello   const size_t initial_count = specs.GetSize();
170b8d03935SAaron Smith   if (!data_sp || !ObjectFilePECOFF::MagicBytesMatch(data_sp))
171b8d03935SAaron Smith     return initial_count;
17289eb1baeSVirgile Bello 
1733db1d138SMartin Storsjö   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
1743db1d138SMartin Storsjö 
175b8d03935SAaron Smith   auto binary = llvm::object::createBinary(file.GetPath());
1763db1d138SMartin Storsjö 
1773db1d138SMartin Storsjö   if (!binary) {
1783db1d138SMartin Storsjö     LLDB_LOG_ERROR(log, binary.takeError(),
1793db1d138SMartin Storsjö                    "Failed to create binary for file ({1}): {0}", file);
180b8d03935SAaron Smith     return initial_count;
1813db1d138SMartin Storsjö   }
18289eb1baeSVirgile Bello 
183b8d03935SAaron Smith   if (!binary->getBinary()->isCOFF() &&
184b8d03935SAaron Smith       !binary->getBinary()->isCOFFImportFile())
185b8d03935SAaron Smith     return initial_count;
18689eb1baeSVirgile Bello 
187b8d03935SAaron Smith   auto COFFObj =
188b8d03935SAaron Smith     llvm::cast<llvm::object::COFFObjectFile>(binary->getBinary());
189b8d03935SAaron Smith 
190b8d03935SAaron Smith   ModuleSpec module_spec(file);
191b8d03935SAaron Smith   ArchSpec &spec = module_spec.GetArchitecture();
192b8d03935SAaron Smith   lldb_private::UUID &uuid = module_spec.GetUUID();
193b8d03935SAaron Smith   if (!uuid.IsValid())
194b8d03935SAaron Smith     uuid = GetCoffUUID(COFFObj);
195b8d03935SAaron Smith 
196b8d03935SAaron Smith   switch (COFFObj->getMachine()) {
197b8d03935SAaron Smith   case MachineAmd64:
198ad587ae4SZachary Turner     spec.SetTriple("x86_64-pc-windows");
199b8d03935SAaron Smith     specs.Append(module_spec);
200b8d03935SAaron Smith     break;
201b8d03935SAaron Smith   case MachineX86:
202ad587ae4SZachary Turner     spec.SetTriple("i386-pc-windows");
203b8d03935SAaron Smith     specs.Append(module_spec);
2045e6f4520SZachary Turner     spec.SetTriple("i686-pc-windows");
205b8d03935SAaron Smith     specs.Append(module_spec);
206b8d03935SAaron Smith     break;
207b8d03935SAaron Smith   case MachineArmNt:
208544c8f48SMartin Storsjo     spec.SetTriple("armv7-pc-windows");
209b8d03935SAaron Smith     specs.Append(module_spec);
210b8d03935SAaron Smith     break;
211638f072fSMartin Storsjo   case MachineArm64:
212674d5543SMartin Storsjo     spec.SetTriple("aarch64-pc-windows");
213638f072fSMartin Storsjo     specs.Append(module_spec);
214638f072fSMartin Storsjo     break;
215b8d03935SAaron Smith   default:
216b8d03935SAaron Smith     break;
21789eb1baeSVirgile Bello   }
21889eb1baeSVirgile Bello 
21989eb1baeSVirgile Bello   return specs.GetSize() - initial_count;
220f4d6de6aSGreg Clayton }
221f4d6de6aSGreg Clayton 
222b9c1b51eSKate Stone bool ObjectFilePECOFF::SaveCore(const lldb::ProcessSP &process_sp,
223f7d1893fSAdrian McCarthy                                 const lldb_private::FileSpec &outfile,
22497206d57SZachary Turner                                 lldb_private::Status &error) {
225f7d1893fSAdrian McCarthy   return SaveMiniDump(process_sp, outfile, error);
226f7d1893fSAdrian McCarthy }
227f7d1893fSAdrian McCarthy 
228b9c1b51eSKate Stone bool ObjectFilePECOFF::MagicBytesMatch(DataBufferSP &data_sp) {
2295ce9c565SGreg Clayton   DataExtractor data(data_sp, eByteOrderLittle, 4);
230c7bece56SGreg Clayton   lldb::offset_t offset = 0;
231f754f88fSGreg Clayton   uint16_t magic = data.GetU16(&offset);
232f754f88fSGreg Clayton   return magic == IMAGE_DOS_SIGNATURE;
233f754f88fSGreg Clayton }
234f754f88fSGreg Clayton 
235b9c1b51eSKate Stone lldb::SymbolType ObjectFilePECOFF::MapSymbolType(uint16_t coff_symbol_type) {
236c35b91ceSAdrian McCarthy   // TODO:  We need to complete this mapping of COFF symbol types to LLDB ones.
237c35b91ceSAdrian McCarthy   // For now, here's a hack to make sure our function have types.
238b9c1b51eSKate Stone   const auto complex_type =
239b9c1b51eSKate Stone       coff_symbol_type >> llvm::COFF::SCT_COMPLEX_TYPE_SHIFT;
240b9c1b51eSKate Stone   if (complex_type == llvm::COFF::IMAGE_SYM_DTYPE_FUNCTION) {
241c35b91ceSAdrian McCarthy     return lldb::eSymbolTypeCode;
242c35b91ceSAdrian McCarthy   }
243c35b91ceSAdrian McCarthy   return lldb::eSymbolTypeInvalid;
244c35b91ceSAdrian McCarthy }
245f754f88fSGreg Clayton 
246037ed1beSAaron Smith bool ObjectFilePECOFF::CreateBinary() {
247037ed1beSAaron Smith   if (m_owningbin)
248037ed1beSAaron Smith     return true;
249037ed1beSAaron Smith 
250037ed1beSAaron Smith   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
251037ed1beSAaron Smith 
252037ed1beSAaron Smith   auto binary = llvm::object::createBinary(m_file.GetPath());
253037ed1beSAaron Smith   if (!binary) {
2543db1d138SMartin Storsjö     LLDB_LOG_ERROR(log, binary.takeError(),
2553db1d138SMartin Storsjö                    "Failed to create binary for file ({1}): {0}", m_file);
256037ed1beSAaron Smith     return false;
257037ed1beSAaron Smith   }
258037ed1beSAaron Smith 
259037ed1beSAaron Smith   // Make sure we only handle COFF format.
260037ed1beSAaron Smith   if (!binary->getBinary()->isCOFF() &&
261037ed1beSAaron Smith       !binary->getBinary()->isCOFFImportFile())
262037ed1beSAaron Smith     return false;
263037ed1beSAaron Smith 
264037ed1beSAaron Smith   m_owningbin = OWNBINType(std::move(*binary));
26563e5fb76SJonas Devlieghere   LLDB_LOGF(log,
26663e5fb76SJonas Devlieghere             "%p ObjectFilePECOFF::CreateBinary() module = %p (%s), file = "
267037ed1beSAaron Smith             "%s, binary = %p (Bin = %p)",
26863e5fb76SJonas Devlieghere             static_cast<void *>(this), static_cast<void *>(GetModule().get()),
269037ed1beSAaron Smith             GetModule()->GetSpecificationDescription().c_str(),
270037ed1beSAaron Smith             m_file ? m_file.GetPath().c_str() : "<NULL>",
271037ed1beSAaron Smith             static_cast<void *>(m_owningbin.getPointer()),
272037ed1beSAaron Smith             static_cast<void *>(m_owningbin->getBinary()));
273037ed1beSAaron Smith   return true;
274037ed1beSAaron Smith }
275037ed1beSAaron Smith 
276e72dfb32SGreg Clayton ObjectFilePECOFF::ObjectFilePECOFF(const lldb::ModuleSP &module_sp,
2775ce9c565SGreg Clayton                                    DataBufferSP &data_sp,
2785ce9c565SGreg Clayton                                    lldb::offset_t data_offset,
279f754f88fSGreg Clayton                                    const FileSpec *file,
2805ce9c565SGreg Clayton                                    lldb::offset_t file_offset,
281b9c1b51eSKate Stone                                    lldb::offset_t length)
282b9c1b51eSKate Stone     : ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset),
2835c43ffd6SPavel Labath       m_dos_header(), m_coff_header(), m_sect_headers(),
284037ed1beSAaron Smith       m_entry_point_address(), m_deps_filespec(), m_owningbin() {
285f754f88fSGreg Clayton   ::memset(&m_dos_header, 0, sizeof(m_dos_header));
286f754f88fSGreg Clayton   ::memset(&m_coff_header, 0, sizeof(m_coff_header));
287f754f88fSGreg Clayton }
288f754f88fSGreg Clayton 
289344546bdSWalter Erquinigo ObjectFilePECOFF::ObjectFilePECOFF(const lldb::ModuleSP &module_sp,
290344546bdSWalter Erquinigo                                    DataBufferSP &header_data_sp,
291344546bdSWalter Erquinigo                                    const lldb::ProcessSP &process_sp,
292344546bdSWalter Erquinigo                                    addr_t header_addr)
293344546bdSWalter Erquinigo     : ObjectFile(module_sp, process_sp, header_addr, header_data_sp),
2945c43ffd6SPavel Labath       m_dos_header(), m_coff_header(), m_sect_headers(),
295037ed1beSAaron Smith       m_entry_point_address(), m_deps_filespec(), m_owningbin() {
296344546bdSWalter Erquinigo   ::memset(&m_dos_header, 0, sizeof(m_dos_header));
297344546bdSWalter Erquinigo   ::memset(&m_coff_header, 0, sizeof(m_coff_header));
298344546bdSWalter Erquinigo }
299344546bdSWalter Erquinigo 
300b9c1b51eSKate Stone ObjectFilePECOFF::~ObjectFilePECOFF() {}
301f754f88fSGreg Clayton 
302b9c1b51eSKate Stone bool ObjectFilePECOFF::ParseHeader() {
303a1743499SGreg Clayton   ModuleSP module_sp(GetModule());
304b9c1b51eSKate Stone   if (module_sp) {
30516ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
306f754f88fSGreg Clayton     m_sect_headers.clear();
307f754f88fSGreg Clayton     m_data.SetByteOrder(eByteOrderLittle);
308c7bece56SGreg Clayton     lldb::offset_t offset = 0;
309f754f88fSGreg Clayton 
310b9c1b51eSKate Stone     if (ParseDOSHeader(m_data, m_dos_header)) {
311f754f88fSGreg Clayton       offset = m_dos_header.e_lfanew;
312f754f88fSGreg Clayton       uint32_t pe_signature = m_data.GetU32(&offset);
313f754f88fSGreg Clayton       if (pe_signature != IMAGE_NT_SIGNATURE)
314f754f88fSGreg Clayton         return false;
315b9c1b51eSKate Stone       if (ParseCOFFHeader(m_data, &offset, m_coff_header)) {
316f754f88fSGreg Clayton         if (m_coff_header.hdrsize > 0)
317f754f88fSGreg Clayton           ParseCOFFOptionalHeader(&offset);
318f754f88fSGreg Clayton         ParseSectionHeaders(offset);
31928469ca3SGreg Clayton       }
320a0f72441SMartin Storsjö       m_data.SetAddressByteSize(GetAddressByteSize());
321f754f88fSGreg Clayton       return true;
322f754f88fSGreg Clayton     }
323a1743499SGreg Clayton   }
324f754f88fSGreg Clayton   return false;
325f754f88fSGreg Clayton }
326f754f88fSGreg Clayton 
327b9c1b51eSKate Stone bool ObjectFilePECOFF::SetLoadAddress(Target &target, addr_t value,
328b9c1b51eSKate Stone                                       bool value_is_offset) {
3292756adf3SVirgile Bello   bool changed = false;
3302756adf3SVirgile Bello   ModuleSP module_sp = GetModule();
331b9c1b51eSKate Stone   if (module_sp) {
3322756adf3SVirgile Bello     size_t num_loaded_sections = 0;
3332756adf3SVirgile Bello     SectionList *section_list = GetSectionList();
334b9c1b51eSKate Stone     if (section_list) {
335b9c1b51eSKate Stone       if (!value_is_offset) {
3362756adf3SVirgile Bello         value -= m_image_base;
3372756adf3SVirgile Bello       }
3382756adf3SVirgile Bello 
3392756adf3SVirgile Bello       const size_t num_sections = section_list->GetSize();
3402756adf3SVirgile Bello       size_t sect_idx = 0;
3412756adf3SVirgile Bello 
342b9c1b51eSKate Stone       for (sect_idx = 0; sect_idx < num_sections; ++sect_idx) {
34305097246SAdrian Prantl         // Iterate through the object file sections to find all of the sections
34405097246SAdrian Prantl         // that have SHF_ALLOC in their flag bits.
3452756adf3SVirgile Bello         SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx));
346b9c1b51eSKate Stone         if (section_sp && !section_sp->IsThreadSpecific()) {
347b9c1b51eSKate Stone           if (target.GetSectionLoadList().SetSectionLoadAddress(
348b9c1b51eSKate Stone                   section_sp, section_sp->GetFileAddress() + value))
3492756adf3SVirgile Bello             ++num_loaded_sections;
3502756adf3SVirgile Bello         }
3512756adf3SVirgile Bello       }
3522756adf3SVirgile Bello       changed = num_loaded_sections > 0;
3532756adf3SVirgile Bello     }
3542756adf3SVirgile Bello   }
3552756adf3SVirgile Bello   return changed;
3562756adf3SVirgile Bello }
3572756adf3SVirgile Bello 
358b9c1b51eSKate Stone ByteOrder ObjectFilePECOFF::GetByteOrder() const { return eByteOrderLittle; }
359f754f88fSGreg Clayton 
360b9c1b51eSKate Stone bool ObjectFilePECOFF::IsExecutable() const {
361237ad974SCharles Davis   return (m_coff_header.flags & llvm::COFF::IMAGE_FILE_DLL) == 0;
362f754f88fSGreg Clayton }
363f754f88fSGreg Clayton 
364b9c1b51eSKate Stone uint32_t ObjectFilePECOFF::GetAddressByteSize() const {
365f754f88fSGreg Clayton   if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32_PLUS)
366f754f88fSGreg Clayton     return 8;
367f754f88fSGreg Clayton   else if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32)
368f754f88fSGreg Clayton     return 4;
369f754f88fSGreg Clayton   return 4;
370f754f88fSGreg Clayton }
371f754f88fSGreg Clayton 
372f754f88fSGreg Clayton // NeedsEndianSwap
373f754f88fSGreg Clayton //
37405097246SAdrian Prantl // Return true if an endian swap needs to occur when extracting data from this
37505097246SAdrian Prantl // file.
376b9c1b51eSKate Stone bool ObjectFilePECOFF::NeedsEndianSwap() const {
377f754f88fSGreg Clayton #if defined(__LITTLE_ENDIAN__)
378f754f88fSGreg Clayton   return false;
379f754f88fSGreg Clayton #else
380f754f88fSGreg Clayton   return true;
381f754f88fSGreg Clayton #endif
382f754f88fSGreg Clayton }
383f754f88fSGreg Clayton // ParseDOSHeader
384b9c1b51eSKate Stone bool ObjectFilePECOFF::ParseDOSHeader(DataExtractor &data,
385b9c1b51eSKate Stone                                       dos_header_t &dos_header) {
386f754f88fSGreg Clayton   bool success = false;
387c7bece56SGreg Clayton   lldb::offset_t offset = 0;
38889eb1baeSVirgile Bello   success = data.ValidOffsetForDataOfSize(0, sizeof(dos_header));
389f754f88fSGreg Clayton 
390b9c1b51eSKate Stone   if (success) {
39189eb1baeSVirgile Bello     dos_header.e_magic = data.GetU16(&offset); // Magic number
39289eb1baeSVirgile Bello     success = dos_header.e_magic == IMAGE_DOS_SIGNATURE;
393f754f88fSGreg Clayton 
394b9c1b51eSKate Stone     if (success) {
39589eb1baeSVirgile Bello       dos_header.e_cblp = data.GetU16(&offset); // Bytes on last page of file
39689eb1baeSVirgile Bello       dos_header.e_cp = data.GetU16(&offset);   // Pages in file
39789eb1baeSVirgile Bello       dos_header.e_crlc = data.GetU16(&offset); // Relocations
398b9c1b51eSKate Stone       dos_header.e_cparhdr =
399b9c1b51eSKate Stone           data.GetU16(&offset); // Size of header in paragraphs
400b9c1b51eSKate Stone       dos_header.e_minalloc =
401b9c1b51eSKate Stone           data.GetU16(&offset); // Minimum extra paragraphs needed
402b9c1b51eSKate Stone       dos_header.e_maxalloc =
403b9c1b51eSKate Stone           data.GetU16(&offset);               // Maximum extra paragraphs needed
40489eb1baeSVirgile Bello       dos_header.e_ss = data.GetU16(&offset); // Initial (relative) SS value
40589eb1baeSVirgile Bello       dos_header.e_sp = data.GetU16(&offset); // Initial SP value
40689eb1baeSVirgile Bello       dos_header.e_csum = data.GetU16(&offset); // Checksum
40789eb1baeSVirgile Bello       dos_header.e_ip = data.GetU16(&offset);   // Initial IP value
40889eb1baeSVirgile Bello       dos_header.e_cs = data.GetU16(&offset);   // Initial (relative) CS value
409b9c1b51eSKate Stone       dos_header.e_lfarlc =
410b9c1b51eSKate Stone           data.GetU16(&offset); // File address of relocation table
41189eb1baeSVirgile Bello       dos_header.e_ovno = data.GetU16(&offset); // Overlay number
412f754f88fSGreg Clayton 
41389eb1baeSVirgile Bello       dos_header.e_res[0] = data.GetU16(&offset); // Reserved words
41489eb1baeSVirgile Bello       dos_header.e_res[1] = data.GetU16(&offset); // Reserved words
41589eb1baeSVirgile Bello       dos_header.e_res[2] = data.GetU16(&offset); // Reserved words
41689eb1baeSVirgile Bello       dos_header.e_res[3] = data.GetU16(&offset); // Reserved words
417f754f88fSGreg Clayton 
418b9c1b51eSKate Stone       dos_header.e_oemid =
419b9c1b51eSKate Stone           data.GetU16(&offset); // OEM identifier (for e_oeminfo)
420b9c1b51eSKate Stone       dos_header.e_oeminfo =
421b9c1b51eSKate Stone           data.GetU16(&offset); // OEM information; e_oemid specific
42289eb1baeSVirgile Bello       dos_header.e_res2[0] = data.GetU16(&offset); // Reserved words
42389eb1baeSVirgile Bello       dos_header.e_res2[1] = data.GetU16(&offset); // Reserved words
42489eb1baeSVirgile Bello       dos_header.e_res2[2] = data.GetU16(&offset); // Reserved words
42589eb1baeSVirgile Bello       dos_header.e_res2[3] = data.GetU16(&offset); // Reserved words
42689eb1baeSVirgile Bello       dos_header.e_res2[4] = data.GetU16(&offset); // Reserved words
42789eb1baeSVirgile Bello       dos_header.e_res2[5] = data.GetU16(&offset); // Reserved words
42889eb1baeSVirgile Bello       dos_header.e_res2[6] = data.GetU16(&offset); // Reserved words
42989eb1baeSVirgile Bello       dos_header.e_res2[7] = data.GetU16(&offset); // Reserved words
43089eb1baeSVirgile Bello       dos_header.e_res2[8] = data.GetU16(&offset); // Reserved words
43189eb1baeSVirgile Bello       dos_header.e_res2[9] = data.GetU16(&offset); // Reserved words
432f754f88fSGreg Clayton 
433b9c1b51eSKate Stone       dos_header.e_lfanew =
434b9c1b51eSKate Stone           data.GetU32(&offset); // File address of new exe header
435f754f88fSGreg Clayton     }
436f754f88fSGreg Clayton   }
437f754f88fSGreg Clayton   if (!success)
43889eb1baeSVirgile Bello     memset(&dos_header, 0, sizeof(dos_header));
439f754f88fSGreg Clayton   return success;
440f754f88fSGreg Clayton }
441f754f88fSGreg Clayton 
442f754f88fSGreg Clayton // ParserCOFFHeader
443b9c1b51eSKate Stone bool ObjectFilePECOFF::ParseCOFFHeader(DataExtractor &data,
444b9c1b51eSKate Stone                                        lldb::offset_t *offset_ptr,
445b9c1b51eSKate Stone                                        coff_header_t &coff_header) {
446b9c1b51eSKate Stone   bool success =
447b9c1b51eSKate Stone       data.ValidOffsetForDataOfSize(*offset_ptr, sizeof(coff_header));
448b9c1b51eSKate Stone   if (success) {
44989eb1baeSVirgile Bello     coff_header.machine = data.GetU16(offset_ptr);
45089eb1baeSVirgile Bello     coff_header.nsects = data.GetU16(offset_ptr);
45189eb1baeSVirgile Bello     coff_header.modtime = data.GetU32(offset_ptr);
45289eb1baeSVirgile Bello     coff_header.symoff = data.GetU32(offset_ptr);
45389eb1baeSVirgile Bello     coff_header.nsyms = data.GetU32(offset_ptr);
45489eb1baeSVirgile Bello     coff_header.hdrsize = data.GetU16(offset_ptr);
45589eb1baeSVirgile Bello     coff_header.flags = data.GetU16(offset_ptr);
456f754f88fSGreg Clayton   }
457f754f88fSGreg Clayton   if (!success)
45889eb1baeSVirgile Bello     memset(&coff_header, 0, sizeof(coff_header));
459f754f88fSGreg Clayton   return success;
460f754f88fSGreg Clayton }
461f754f88fSGreg Clayton 
462b9c1b51eSKate Stone bool ObjectFilePECOFF::ParseCOFFOptionalHeader(lldb::offset_t *offset_ptr) {
463f754f88fSGreg Clayton   bool success = false;
464c7bece56SGreg Clayton   const lldb::offset_t end_offset = *offset_ptr + m_coff_header.hdrsize;
465b9c1b51eSKate Stone   if (*offset_ptr < end_offset) {
466f754f88fSGreg Clayton     success = true;
467f754f88fSGreg Clayton     m_coff_header_opt.magic = m_data.GetU16(offset_ptr);
468f754f88fSGreg Clayton     m_coff_header_opt.major_linker_version = m_data.GetU8(offset_ptr);
469f754f88fSGreg Clayton     m_coff_header_opt.minor_linker_version = m_data.GetU8(offset_ptr);
470f754f88fSGreg Clayton     m_coff_header_opt.code_size = m_data.GetU32(offset_ptr);
471f754f88fSGreg Clayton     m_coff_header_opt.data_size = m_data.GetU32(offset_ptr);
472f754f88fSGreg Clayton     m_coff_header_opt.bss_size = m_data.GetU32(offset_ptr);
473f754f88fSGreg Clayton     m_coff_header_opt.entry = m_data.GetU32(offset_ptr);
474f754f88fSGreg Clayton     m_coff_header_opt.code_offset = m_data.GetU32(offset_ptr);
475f754f88fSGreg Clayton 
476f754f88fSGreg Clayton     const uint32_t addr_byte_size = GetAddressByteSize();
477f754f88fSGreg Clayton 
478b9c1b51eSKate Stone     if (*offset_ptr < end_offset) {
479b9c1b51eSKate Stone       if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32) {
480f754f88fSGreg Clayton         // PE32 only
481f754f88fSGreg Clayton         m_coff_header_opt.data_offset = m_data.GetU32(offset_ptr);
482b9c1b51eSKate Stone       } else
483f754f88fSGreg Clayton         m_coff_header_opt.data_offset = 0;
484f754f88fSGreg Clayton 
485b9c1b51eSKate Stone       if (*offset_ptr < end_offset) {
486b9c1b51eSKate Stone         m_coff_header_opt.image_base =
487b9c1b51eSKate Stone             m_data.GetMaxU64(offset_ptr, addr_byte_size);
488f754f88fSGreg Clayton         m_coff_header_opt.sect_alignment = m_data.GetU32(offset_ptr);
489f754f88fSGreg Clayton         m_coff_header_opt.file_alignment = m_data.GetU32(offset_ptr);
490f754f88fSGreg Clayton         m_coff_header_opt.major_os_system_version = m_data.GetU16(offset_ptr);
491f754f88fSGreg Clayton         m_coff_header_opt.minor_os_system_version = m_data.GetU16(offset_ptr);
492f754f88fSGreg Clayton         m_coff_header_opt.major_image_version = m_data.GetU16(offset_ptr);
493f754f88fSGreg Clayton         m_coff_header_opt.minor_image_version = m_data.GetU16(offset_ptr);
494f754f88fSGreg Clayton         m_coff_header_opt.major_subsystem_version = m_data.GetU16(offset_ptr);
495f754f88fSGreg Clayton         m_coff_header_opt.minor_subsystem_version = m_data.GetU16(offset_ptr);
496f754f88fSGreg Clayton         m_coff_header_opt.reserved1 = m_data.GetU32(offset_ptr);
497f754f88fSGreg Clayton         m_coff_header_opt.image_size = m_data.GetU32(offset_ptr);
498f754f88fSGreg Clayton         m_coff_header_opt.header_size = m_data.GetU32(offset_ptr);
49928469ca3SGreg Clayton         m_coff_header_opt.checksum = m_data.GetU32(offset_ptr);
500f754f88fSGreg Clayton         m_coff_header_opt.subsystem = m_data.GetU16(offset_ptr);
501f754f88fSGreg Clayton         m_coff_header_opt.dll_flags = m_data.GetU16(offset_ptr);
502b9c1b51eSKate Stone         m_coff_header_opt.stack_reserve_size =
503b9c1b51eSKate Stone             m_data.GetMaxU64(offset_ptr, addr_byte_size);
504b9c1b51eSKate Stone         m_coff_header_opt.stack_commit_size =
505b9c1b51eSKate Stone             m_data.GetMaxU64(offset_ptr, addr_byte_size);
506b9c1b51eSKate Stone         m_coff_header_opt.heap_reserve_size =
507b9c1b51eSKate Stone             m_data.GetMaxU64(offset_ptr, addr_byte_size);
508b9c1b51eSKate Stone         m_coff_header_opt.heap_commit_size =
509b9c1b51eSKate Stone             m_data.GetMaxU64(offset_ptr, addr_byte_size);
510f754f88fSGreg Clayton         m_coff_header_opt.loader_flags = m_data.GetU32(offset_ptr);
511f754f88fSGreg Clayton         uint32_t num_data_dir_entries = m_data.GetU32(offset_ptr);
512f754f88fSGreg Clayton         m_coff_header_opt.data_dirs.clear();
513f754f88fSGreg Clayton         m_coff_header_opt.data_dirs.resize(num_data_dir_entries);
514f754f88fSGreg Clayton         uint32_t i;
515b9c1b51eSKate Stone         for (i = 0; i < num_data_dir_entries; i++) {
516f754f88fSGreg Clayton           m_coff_header_opt.data_dirs[i].vmaddr = m_data.GetU32(offset_ptr);
517f754f88fSGreg Clayton           m_coff_header_opt.data_dirs[i].vmsize = m_data.GetU32(offset_ptr);
518f754f88fSGreg Clayton         }
5192756adf3SVirgile Bello 
5202756adf3SVirgile Bello         m_image_base = m_coff_header_opt.image_base;
521f754f88fSGreg Clayton       }
522f754f88fSGreg Clayton     }
523f754f88fSGreg Clayton   }
524f754f88fSGreg Clayton   // Make sure we are on track for section data which follows
525f754f88fSGreg Clayton   *offset_ptr = end_offset;
526f754f88fSGreg Clayton   return success;
527f754f88fSGreg Clayton }
528f754f88fSGreg Clayton 
52930c2441aSAleksandr Urakov uint32_t ObjectFilePECOFF::GetRVA(const Address &addr) const {
53030c2441aSAleksandr Urakov   return addr.GetFileAddress() - m_image_base;
53130c2441aSAleksandr Urakov }
53230c2441aSAleksandr Urakov 
53330c2441aSAleksandr Urakov Address ObjectFilePECOFF::GetAddress(uint32_t rva) {
53430c2441aSAleksandr Urakov   SectionList *sect_list = GetSectionList();
53530c2441aSAleksandr Urakov   if (!sect_list)
53630c2441aSAleksandr Urakov     return Address(GetFileAddress(rva));
53730c2441aSAleksandr Urakov 
53830c2441aSAleksandr Urakov   return Address(GetFileAddress(rva), sect_list);
53930c2441aSAleksandr Urakov }
54030c2441aSAleksandr Urakov 
54130c2441aSAleksandr Urakov lldb::addr_t ObjectFilePECOFF::GetFileAddress(uint32_t rva) const {
54230c2441aSAleksandr Urakov   return m_image_base + rva;
54330c2441aSAleksandr Urakov }
54430c2441aSAleksandr Urakov 
545344546bdSWalter Erquinigo DataExtractor ObjectFilePECOFF::ReadImageData(uint32_t offset, size_t size) {
54630c2441aSAleksandr Urakov   if (!size)
54730c2441aSAleksandr Urakov     return {};
54830c2441aSAleksandr Urakov 
549344546bdSWalter Erquinigo   if (m_file) {
5507f6a7a37SZachary Turner     // A bit of a hack, but we intend to write to this buffer, so we can't
5517f6a7a37SZachary Turner     // mmap it.
55250251fc7SPavel Labath     auto buffer_sp = MapFileData(m_file, size, offset);
553344546bdSWalter Erquinigo     return DataExtractor(buffer_sp, GetByteOrder(), GetAddressByteSize());
554344546bdSWalter Erquinigo   }
555344546bdSWalter Erquinigo   ProcessSP process_sp(m_process_wp.lock());
556344546bdSWalter Erquinigo   DataExtractor data;
557344546bdSWalter Erquinigo   if (process_sp) {
558a8f3ae7cSJonas Devlieghere     auto data_up = std::make_unique<DataBufferHeap>(size, 0);
55997206d57SZachary Turner     Status readmem_error;
560344546bdSWalter Erquinigo     size_t bytes_read =
561d5b44036SJonas Devlieghere         process_sp->ReadMemory(m_image_base + offset, data_up->GetBytes(),
562d5b44036SJonas Devlieghere                                data_up->GetByteSize(), readmem_error);
563344546bdSWalter Erquinigo     if (bytes_read == size) {
564d5b44036SJonas Devlieghere       DataBufferSP buffer_sp(data_up.release());
565344546bdSWalter Erquinigo       data.SetData(buffer_sp, 0, buffer_sp->GetByteSize());
566344546bdSWalter Erquinigo     }
567344546bdSWalter Erquinigo   }
568344546bdSWalter Erquinigo   return data;
569344546bdSWalter Erquinigo }
570344546bdSWalter Erquinigo 
57130c2441aSAleksandr Urakov DataExtractor ObjectFilePECOFF::ReadImageDataByRVA(uint32_t rva, size_t size) {
57230c2441aSAleksandr Urakov   if (m_file) {
57330c2441aSAleksandr Urakov     Address addr = GetAddress(rva);
5747e1a3076SMartin Storsjö     SectionSP sect = addr.GetSection();
5757e1a3076SMartin Storsjö     if (!sect)
5767e1a3076SMartin Storsjö       return {};
5777e1a3076SMartin Storsjö     rva = sect->GetFileOffset() + addr.GetOffset();
57830c2441aSAleksandr Urakov   }
57930c2441aSAleksandr Urakov 
58030c2441aSAleksandr Urakov   return ReadImageData(rva, size);
58130c2441aSAleksandr Urakov }
58230c2441aSAleksandr Urakov 
583f754f88fSGreg Clayton // ParseSectionHeaders
584b9c1b51eSKate Stone bool ObjectFilePECOFF::ParseSectionHeaders(
585b9c1b51eSKate Stone     uint32_t section_header_data_offset) {
586f754f88fSGreg Clayton   const uint32_t nsects = m_coff_header.nsects;
587f754f88fSGreg Clayton   m_sect_headers.clear();
588f754f88fSGreg Clayton 
589b9c1b51eSKate Stone   if (nsects > 0) {
590f754f88fSGreg Clayton     const size_t section_header_byte_size = nsects * sizeof(section_header_t);
591344546bdSWalter Erquinigo     DataExtractor section_header_data =
592344546bdSWalter Erquinigo         ReadImageData(section_header_data_offset, section_header_byte_size);
593f754f88fSGreg Clayton 
594c7bece56SGreg Clayton     lldb::offset_t offset = 0;
595b9c1b51eSKate Stone     if (section_header_data.ValidOffsetForDataOfSize(
596b9c1b51eSKate Stone             offset, section_header_byte_size)) {
597f754f88fSGreg Clayton       m_sect_headers.resize(nsects);
598f754f88fSGreg Clayton 
599b9c1b51eSKate Stone       for (uint32_t idx = 0; idx < nsects; ++idx) {
600f754f88fSGreg Clayton         const void *name_data = section_header_data.GetData(&offset, 8);
601b9c1b51eSKate Stone         if (name_data) {
602f754f88fSGreg Clayton           memcpy(m_sect_headers[idx].name, name_data, 8);
603f754f88fSGreg Clayton           m_sect_headers[idx].vmsize = section_header_data.GetU32(&offset);
604f754f88fSGreg Clayton           m_sect_headers[idx].vmaddr = section_header_data.GetU32(&offset);
605f754f88fSGreg Clayton           m_sect_headers[idx].size = section_header_data.GetU32(&offset);
606f754f88fSGreg Clayton           m_sect_headers[idx].offset = section_header_data.GetU32(&offset);
607f754f88fSGreg Clayton           m_sect_headers[idx].reloff = section_header_data.GetU32(&offset);
608f754f88fSGreg Clayton           m_sect_headers[idx].lineoff = section_header_data.GetU32(&offset);
609f754f88fSGreg Clayton           m_sect_headers[idx].nreloc = section_header_data.GetU16(&offset);
610f754f88fSGreg Clayton           m_sect_headers[idx].nline = section_header_data.GetU16(&offset);
611f754f88fSGreg Clayton           m_sect_headers[idx].flags = section_header_data.GetU32(&offset);
612f754f88fSGreg Clayton         }
613f754f88fSGreg Clayton       }
614f754f88fSGreg Clayton     }
615f754f88fSGreg Clayton   }
616f754f88fSGreg Clayton 
617a6682a41SJonas Devlieghere   return !m_sect_headers.empty();
618f754f88fSGreg Clayton }
619f754f88fSGreg Clayton 
6202886e4a0SPavel Labath llvm::StringRef ObjectFilePECOFF::GetSectionName(const section_header_t &sect) {
6212886e4a0SPavel Labath   llvm::StringRef hdr_name(sect.name, llvm::array_lengthof(sect.name));
6222886e4a0SPavel Labath   hdr_name = hdr_name.split('\0').first;
6232886e4a0SPavel Labath   if (hdr_name.consume_front("/")) {
6242886e4a0SPavel Labath     lldb::offset_t stroff;
6252886e4a0SPavel Labath     if (!to_integer(hdr_name, stroff, 10))
6262886e4a0SPavel Labath       return "";
627b9c1b51eSKate Stone     lldb::offset_t string_file_offset =
628b9c1b51eSKate Stone         m_coff_header.symoff + (m_coff_header.nsyms * 18) + stroff;
6292886e4a0SPavel Labath     if (const char *name = m_data.GetCStr(&string_file_offset))
6302886e4a0SPavel Labath       return name;
6312886e4a0SPavel Labath     return "";
632f754f88fSGreg Clayton   }
6332886e4a0SPavel Labath   return hdr_name;
634f754f88fSGreg Clayton }
635f754f88fSGreg Clayton 
636f754f88fSGreg Clayton // GetNListSymtab
637b9c1b51eSKate Stone Symtab *ObjectFilePECOFF::GetSymtab() {
638a1743499SGreg Clayton   ModuleSP module_sp(GetModule());
639b9c1b51eSKate Stone   if (module_sp) {
64016ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
641248a1305SKonrad Kleine     if (m_symtab_up == nullptr) {
642f754f88fSGreg Clayton       SectionList *sect_list = GetSectionList();
643d5b44036SJonas Devlieghere       m_symtab_up.reset(new Symtab(this));
644d5b44036SJonas Devlieghere       std::lock_guard<std::recursive_mutex> guard(m_symtab_up->GetMutex());
64528469ca3SGreg Clayton 
64628469ca3SGreg Clayton       const uint32_t num_syms = m_coff_header.nsyms;
64728469ca3SGreg Clayton 
648344546bdSWalter Erquinigo       if (m_file && num_syms > 0 && m_coff_header.symoff > 0) {
6490076e715SGreg Clayton         const uint32_t symbol_size = 18;
65028469ca3SGreg Clayton         const size_t symbol_data_size = num_syms * symbol_size;
651c35b91ceSAdrian McCarthy         // Include the 4-byte string table size at the end of the symbols
652344546bdSWalter Erquinigo         DataExtractor symtab_data =
653344546bdSWalter Erquinigo             ReadImageData(m_coff_header.symoff, symbol_data_size + 4);
654c7bece56SGreg Clayton         lldb::offset_t offset = symbol_data_size;
65528469ca3SGreg Clayton         const uint32_t strtab_size = symtab_data.GetU32(&offset);
656344546bdSWalter Erquinigo         if (strtab_size > 0) {
657344546bdSWalter Erquinigo           DataExtractor strtab_data = ReadImageData(
658344546bdSWalter Erquinigo               m_coff_header.symoff + symbol_data_size, strtab_size);
65928469ca3SGreg Clayton 
6600076e715SGreg Clayton           // First 4 bytes should be zeroed after strtab_size has been read,
6610076e715SGreg Clayton           // because it is used as offset 0 to encode a NULL string.
662f76e099cSSaleem Abdulrasool           uint32_t *strtab_data_start = const_cast<uint32_t *>(
663f76e099cSSaleem Abdulrasool               reinterpret_cast<const uint32_t *>(strtab_data.GetDataStart()));
6640076e715SGreg Clayton           strtab_data_start[0] = 0;
6650076e715SGreg Clayton 
66628469ca3SGreg Clayton           offset = 0;
66728469ca3SGreg Clayton           std::string symbol_name;
668d5b44036SJonas Devlieghere           Symbol *symbols = m_symtab_up->Resize(num_syms);
669b9c1b51eSKate Stone           for (uint32_t i = 0; i < num_syms; ++i) {
670f754f88fSGreg Clayton             coff_symbol_t symbol;
67128469ca3SGreg Clayton             const uint32_t symbol_offset = offset;
672248a1305SKonrad Kleine             const char *symbol_name_cstr = nullptr;
673c35b91ceSAdrian McCarthy             // If the first 4 bytes of the symbol string are zero, then they
674c35b91ceSAdrian McCarthy             // are followed by a 4-byte string table offset. Else these
67528469ca3SGreg Clayton             // 8 bytes contain the symbol name
676b9c1b51eSKate Stone             if (symtab_data.GetU32(&offset) == 0) {
67705097246SAdrian Prantl               // Long string that doesn't fit into the symbol table name, so
67805097246SAdrian Prantl               // now we must read the 4 byte string table offset
67928469ca3SGreg Clayton               uint32_t strtab_offset = symtab_data.GetU32(&offset);
68028469ca3SGreg Clayton               symbol_name_cstr = strtab_data.PeekCStr(strtab_offset);
68128469ca3SGreg Clayton               symbol_name.assign(symbol_name_cstr);
682b9c1b51eSKate Stone             } else {
683b9c1b51eSKate Stone               // Short string that fits into the symbol table name which is 8
684b9c1b51eSKate Stone               // bytes
68528469ca3SGreg Clayton               offset += sizeof(symbol.name) - 4; // Skip remaining
68628469ca3SGreg Clayton               symbol_name_cstr = symtab_data.PeekCStr(symbol_offset);
687248a1305SKonrad Kleine               if (symbol_name_cstr == nullptr)
688f754f88fSGreg Clayton                 break;
68928469ca3SGreg Clayton               symbol_name.assign(symbol_name_cstr, sizeof(symbol.name));
69028469ca3SGreg Clayton             }
69128469ca3SGreg Clayton             symbol.value = symtab_data.GetU32(&offset);
69228469ca3SGreg Clayton             symbol.sect = symtab_data.GetU16(&offset);
69328469ca3SGreg Clayton             symbol.type = symtab_data.GetU16(&offset);
69428469ca3SGreg Clayton             symbol.storage = symtab_data.GetU8(&offset);
69528469ca3SGreg Clayton             symbol.naux = symtab_data.GetU8(&offset);
696037520e9SGreg Clayton             symbols[i].GetMangled().SetValue(ConstString(symbol_name.c_str()));
697b9c1b51eSKate Stone             if ((int16_t)symbol.sect >= 1) {
6984394b5beSMartin Storsjö               Address symbol_addr(sect_list->FindSectionByID(symbol.sect),
699b9c1b51eSKate Stone                                   symbol.value);
700358cf1eaSGreg Clayton               symbols[i].GetAddressRef() = symbol_addr;
701c35b91ceSAdrian McCarthy               symbols[i].SetType(MapSymbolType(symbol.type));
7020076e715SGreg Clayton             }
703f754f88fSGreg Clayton 
704b9c1b51eSKate Stone             if (symbol.naux > 0) {
705f754f88fSGreg Clayton               i += symbol.naux;
7060076e715SGreg Clayton               offset += symbol_size;
7070076e715SGreg Clayton             }
708f754f88fSGreg Clayton           }
709f754f88fSGreg Clayton         }
710344546bdSWalter Erquinigo       }
711a4fe3a12SVirgile Bello 
712a4fe3a12SVirgile Bello       // Read export header
713b9c1b51eSKate Stone       if (coff_data_dir_export_table < m_coff_header_opt.data_dirs.size() &&
714b9c1b51eSKate Stone           m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmsize > 0 &&
715b9c1b51eSKate Stone           m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmaddr > 0) {
716a4fe3a12SVirgile Bello         export_directory_entry export_table;
717b9c1b51eSKate Stone         uint32_t data_start =
718b9c1b51eSKate Stone             m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmaddr;
719344546bdSWalter Erquinigo 
72030c2441aSAleksandr Urakov         DataExtractor symtab_data = ReadImageDataByRVA(
72130c2441aSAleksandr Urakov             data_start, m_coff_header_opt.data_dirs[0].vmsize);
722a4fe3a12SVirgile Bello         lldb::offset_t offset = 0;
723a4fe3a12SVirgile Bello 
724a4fe3a12SVirgile Bello         // Read export_table header
725a4fe3a12SVirgile Bello         export_table.characteristics = symtab_data.GetU32(&offset);
726a4fe3a12SVirgile Bello         export_table.time_date_stamp = symtab_data.GetU32(&offset);
727a4fe3a12SVirgile Bello         export_table.major_version = symtab_data.GetU16(&offset);
728a4fe3a12SVirgile Bello         export_table.minor_version = symtab_data.GetU16(&offset);
729a4fe3a12SVirgile Bello         export_table.name = symtab_data.GetU32(&offset);
730a4fe3a12SVirgile Bello         export_table.base = symtab_data.GetU32(&offset);
731a4fe3a12SVirgile Bello         export_table.number_of_functions = symtab_data.GetU32(&offset);
732a4fe3a12SVirgile Bello         export_table.number_of_names = symtab_data.GetU32(&offset);
733a4fe3a12SVirgile Bello         export_table.address_of_functions = symtab_data.GetU32(&offset);
734a4fe3a12SVirgile Bello         export_table.address_of_names = symtab_data.GetU32(&offset);
735a4fe3a12SVirgile Bello         export_table.address_of_name_ordinals = symtab_data.GetU32(&offset);
736a4fe3a12SVirgile Bello 
737a4fe3a12SVirgile Bello         bool has_ordinal = export_table.address_of_name_ordinals != 0;
738a4fe3a12SVirgile Bello 
739a4fe3a12SVirgile Bello         lldb::offset_t name_offset = export_table.address_of_names - data_start;
740b9c1b51eSKate Stone         lldb::offset_t name_ordinal_offset =
741b9c1b51eSKate Stone             export_table.address_of_name_ordinals - data_start;
742a4fe3a12SVirgile Bello 
743d5b44036SJonas Devlieghere         Symbol *symbols = m_symtab_up->Resize(export_table.number_of_names);
744a4fe3a12SVirgile Bello 
745a4fe3a12SVirgile Bello         std::string symbol_name;
746a4fe3a12SVirgile Bello 
747a4fe3a12SVirgile Bello         // Read each export table entry
748b9c1b51eSKate Stone         for (size_t i = 0; i < export_table.number_of_names; ++i) {
749b9c1b51eSKate Stone           uint32_t name_ordinal =
750b9c1b51eSKate Stone               has_ordinal ? symtab_data.GetU16(&name_ordinal_offset) : i;
751a4fe3a12SVirgile Bello           uint32_t name_address = symtab_data.GetU32(&name_offset);
752a4fe3a12SVirgile Bello 
753b9c1b51eSKate Stone           const char *symbol_name_cstr =
754b9c1b51eSKate Stone               symtab_data.PeekCStr(name_address - data_start);
755a4fe3a12SVirgile Bello           symbol_name.assign(symbol_name_cstr);
756a4fe3a12SVirgile Bello 
757b9c1b51eSKate Stone           lldb::offset_t function_offset = export_table.address_of_functions -
758b9c1b51eSKate Stone                                            data_start +
759b9c1b51eSKate Stone                                            sizeof(uint32_t) * name_ordinal;
760a4fe3a12SVirgile Bello           uint32_t function_rva = symtab_data.GetU32(&function_offset);
761a4fe3a12SVirgile Bello 
762b9c1b51eSKate Stone           Address symbol_addr(m_coff_header_opt.image_base + function_rva,
763b9c1b51eSKate Stone                               sect_list);
764a4fe3a12SVirgile Bello           symbols[i].GetMangled().SetValue(ConstString(symbol_name.c_str()));
765358cf1eaSGreg Clayton           symbols[i].GetAddressRef() = symbol_addr;
766a4fe3a12SVirgile Bello           symbols[i].SetType(lldb::eSymbolTypeCode);
767a4fe3a12SVirgile Bello           symbols[i].SetDebug(true);
768a4fe3a12SVirgile Bello         }
769a4fe3a12SVirgile Bello       }
770d5b44036SJonas Devlieghere       m_symtab_up->CalculateSymbolSizes();
771f754f88fSGreg Clayton     }
772a1743499SGreg Clayton   }
773d5b44036SJonas Devlieghere   return m_symtab_up.get();
774f754f88fSGreg Clayton }
775f754f88fSGreg Clayton 
77630c2441aSAleksandr Urakov std::unique_ptr<CallFrameInfo> ObjectFilePECOFF::CreateCallFrameInfo() {
77730c2441aSAleksandr Urakov   if (coff_data_dir_exception_table >= m_coff_header_opt.data_dirs.size())
77830c2441aSAleksandr Urakov     return {};
77930c2441aSAleksandr Urakov 
78030c2441aSAleksandr Urakov   data_directory data_dir_exception =
78130c2441aSAleksandr Urakov       m_coff_header_opt.data_dirs[coff_data_dir_exception_table];
78230c2441aSAleksandr Urakov   if (!data_dir_exception.vmaddr)
78330c2441aSAleksandr Urakov     return {};
78430c2441aSAleksandr Urakov 
785*aa786b88SMartin Storsjö   if (m_coff_header.machine != llvm::COFF::IMAGE_FILE_MACHINE_AMD64)
786*aa786b88SMartin Storsjö     return {};
787*aa786b88SMartin Storsjö 
78830c2441aSAleksandr Urakov   return std::make_unique<PECallFrameInfo>(*this, data_dir_exception.vmaddr,
78930c2441aSAleksandr Urakov                                            data_dir_exception.vmsize);
79030c2441aSAleksandr Urakov }
79130c2441aSAleksandr Urakov 
792b9c1b51eSKate Stone bool ObjectFilePECOFF::IsStripped() {
7933046e668SGreg Clayton   // TODO: determine this for COFF
7943046e668SGreg Clayton   return false;
7953046e668SGreg Clayton }
7963046e668SGreg Clayton 
7972e5bb6d8SMartin Storsjö SectionType ObjectFilePECOFF::GetSectionType(llvm::StringRef sect_name,
7982e5bb6d8SMartin Storsjö                                              const section_header_t &sect) {
7992e5bb6d8SMartin Storsjö   ConstString const_sect_name(sect_name);
8002e5bb6d8SMartin Storsjö   static ConstString g_code_sect_name(".code");
8012e5bb6d8SMartin Storsjö   static ConstString g_CODE_sect_name("CODE");
8022e5bb6d8SMartin Storsjö   static ConstString g_data_sect_name(".data");
8032e5bb6d8SMartin Storsjö   static ConstString g_DATA_sect_name("DATA");
8042e5bb6d8SMartin Storsjö   static ConstString g_bss_sect_name(".bss");
8052e5bb6d8SMartin Storsjö   static ConstString g_BSS_sect_name("BSS");
8062e5bb6d8SMartin Storsjö 
8072e5bb6d8SMartin Storsjö   if (sect.flags & llvm::COFF::IMAGE_SCN_CNT_CODE &&
8082e5bb6d8SMartin Storsjö       ((const_sect_name == g_code_sect_name) ||
8092e5bb6d8SMartin Storsjö        (const_sect_name == g_CODE_sect_name))) {
8102e5bb6d8SMartin Storsjö     return eSectionTypeCode;
8112e5bb6d8SMartin Storsjö   }
8122e5bb6d8SMartin Storsjö   if (sect.flags & llvm::COFF::IMAGE_SCN_CNT_INITIALIZED_DATA &&
8132e5bb6d8SMartin Storsjö              ((const_sect_name == g_data_sect_name) ||
8142e5bb6d8SMartin Storsjö               (const_sect_name == g_DATA_sect_name))) {
8152e5bb6d8SMartin Storsjö     if (sect.size == 0 && sect.offset == 0)
8162e5bb6d8SMartin Storsjö       return eSectionTypeZeroFill;
8172e5bb6d8SMartin Storsjö     else
8182e5bb6d8SMartin Storsjö       return eSectionTypeData;
8192e5bb6d8SMartin Storsjö   }
8202e5bb6d8SMartin Storsjö   if (sect.flags & llvm::COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA &&
8212e5bb6d8SMartin Storsjö              ((const_sect_name == g_bss_sect_name) ||
8222e5bb6d8SMartin Storsjö               (const_sect_name == g_BSS_sect_name))) {
8232e5bb6d8SMartin Storsjö     if (sect.size == 0)
8242e5bb6d8SMartin Storsjö       return eSectionTypeZeroFill;
8252e5bb6d8SMartin Storsjö     else
8262e5bb6d8SMartin Storsjö       return eSectionTypeData;
8272e5bb6d8SMartin Storsjö   }
8282e5bb6d8SMartin Storsjö 
8292e5bb6d8SMartin Storsjö   SectionType section_type =
8302e5bb6d8SMartin Storsjö       llvm::StringSwitch<SectionType>(sect_name)
8312e5bb6d8SMartin Storsjö           .Case(".debug", eSectionTypeDebug)
8322e5bb6d8SMartin Storsjö           .Case(".stabstr", eSectionTypeDataCString)
8332e5bb6d8SMartin Storsjö           .Case(".reloc", eSectionTypeOther)
8342e5bb6d8SMartin Storsjö           .Case(".debug_abbrev", eSectionTypeDWARFDebugAbbrev)
8352e5bb6d8SMartin Storsjö           .Case(".debug_aranges", eSectionTypeDWARFDebugAranges)
8362e5bb6d8SMartin Storsjö           .Case(".debug_frame", eSectionTypeDWARFDebugFrame)
8372e5bb6d8SMartin Storsjö           .Case(".debug_info", eSectionTypeDWARFDebugInfo)
8382e5bb6d8SMartin Storsjö           .Case(".debug_line", eSectionTypeDWARFDebugLine)
8392e5bb6d8SMartin Storsjö           .Case(".debug_loc", eSectionTypeDWARFDebugLoc)
8402e5bb6d8SMartin Storsjö           .Case(".debug_loclists", eSectionTypeDWARFDebugLocLists)
8412e5bb6d8SMartin Storsjö           .Case(".debug_macinfo", eSectionTypeDWARFDebugMacInfo)
8422e5bb6d8SMartin Storsjö           .Case(".debug_names", eSectionTypeDWARFDebugNames)
8432e5bb6d8SMartin Storsjö           .Case(".debug_pubnames", eSectionTypeDWARFDebugPubNames)
8442e5bb6d8SMartin Storsjö           .Case(".debug_pubtypes", eSectionTypeDWARFDebugPubTypes)
8452e5bb6d8SMartin Storsjö           .Case(".debug_ranges", eSectionTypeDWARFDebugRanges)
8462e5bb6d8SMartin Storsjö           .Case(".debug_str", eSectionTypeDWARFDebugStr)
8472e5bb6d8SMartin Storsjö           .Case(".debug_types", eSectionTypeDWARFDebugTypes)
848934c025eSMartin Storsjö           // .eh_frame can be truncated to 8 chars.
849934c025eSMartin Storsjö           .Cases(".eh_frame", ".eh_fram", eSectionTypeEHFrame)
8502e5bb6d8SMartin Storsjö           .Case(".gosymtab", eSectionTypeGoSymtab)
8512e5bb6d8SMartin Storsjö           .Default(eSectionTypeInvalid);
8522e5bb6d8SMartin Storsjö   if (section_type != eSectionTypeInvalid)
8532e5bb6d8SMartin Storsjö     return section_type;
8542e5bb6d8SMartin Storsjö 
8552e5bb6d8SMartin Storsjö   if (sect.flags & llvm::COFF::IMAGE_SCN_CNT_CODE)
8562e5bb6d8SMartin Storsjö     return eSectionTypeCode;
8572e5bb6d8SMartin Storsjö   if (sect.flags & llvm::COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)
8582e5bb6d8SMartin Storsjö     return eSectionTypeData;
8592e5bb6d8SMartin Storsjö   if (sect.flags & llvm::COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) {
8602e5bb6d8SMartin Storsjö     if (sect.size == 0)
8612e5bb6d8SMartin Storsjö       return eSectionTypeZeroFill;
8622e5bb6d8SMartin Storsjö     else
8632e5bb6d8SMartin Storsjö       return eSectionTypeData;
8642e5bb6d8SMartin Storsjö   }
8652e5bb6d8SMartin Storsjö   return eSectionTypeOther;
8662e5bb6d8SMartin Storsjö }
8672e5bb6d8SMartin Storsjö 
868b9c1b51eSKate Stone void ObjectFilePECOFF::CreateSections(SectionList &unified_section_list) {
869d5b44036SJonas Devlieghere   if (m_sections_up)
87088a2c2a4SPavel Labath     return;
871d5b44036SJonas Devlieghere   m_sections_up.reset(new SectionList());
8723046e668SGreg Clayton 
873a1743499SGreg Clayton   ModuleSP module_sp(GetModule());
874b9c1b51eSKate Stone   if (module_sp) {
87516ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
8767db8b5c4SPavel Labath 
87773a7a55cSPavel Labath     SectionSP header_sp = std::make_shared<Section>(
87873a7a55cSPavel Labath         module_sp, this, ~user_id_t(0), ConstString("PECOFF header"),
87973a7a55cSPavel Labath         eSectionTypeOther, m_coff_header_opt.image_base,
88073a7a55cSPavel Labath         m_coff_header_opt.header_size,
88173a7a55cSPavel Labath         /*file_offset*/ 0, m_coff_header_opt.header_size,
88273a7a55cSPavel Labath         m_coff_header_opt.sect_alignment,
8837db8b5c4SPavel Labath         /*flags*/ 0);
884f1e0ae34SPavel Labath     header_sp->SetPermissions(ePermissionsReadable);
88573a7a55cSPavel Labath     m_sections_up->AddSection(header_sp);
88673a7a55cSPavel Labath     unified_section_list.AddSection(header_sp);
8877db8b5c4SPavel Labath 
888f754f88fSGreg Clayton     const uint32_t nsects = m_sect_headers.size();
889e72dfb32SGreg Clayton     ModuleSP module_sp(GetModule());
890b9c1b51eSKate Stone     for (uint32_t idx = 0; idx < nsects; ++idx) {
8912e5bb6d8SMartin Storsjö       llvm::StringRef sect_name = GetSectionName(m_sect_headers[idx]);
8922e5bb6d8SMartin Storsjö       ConstString const_sect_name(sect_name);
8932e5bb6d8SMartin Storsjö       SectionType section_type = GetSectionType(sect_name, m_sect_headers[idx]);
894f754f88fSGreg Clayton 
895b9c1b51eSKate Stone       SectionSP section_sp(new Section(
896b9c1b51eSKate Stone           module_sp,       // Module to which this section belongs
897a7499c98SMichael Sartain           this,            // Object file to which this section belongs
8987db8b5c4SPavel Labath           idx + 1,         // Section ID is the 1 based section index.
899f754f88fSGreg Clayton           const_sect_name, // Name of this section
9007db8b5c4SPavel Labath           section_type,
90173a7a55cSPavel Labath           m_coff_header_opt.image_base +
902b9c1b51eSKate Stone               m_sect_headers[idx].vmaddr, // File VM address == addresses as
903b9c1b51eSKate Stone                                           // they are found in the object file
904f754f88fSGreg Clayton           m_sect_headers[idx].vmsize,     // VM size in bytes of this section
905b9c1b51eSKate Stone           m_sect_headers[idx]
906b9c1b51eSKate Stone               .offset, // Offset to the data for this section in the file
907b9c1b51eSKate Stone           m_sect_headers[idx]
908b9c1b51eSKate Stone               .size, // Size in bytes of this section as found in the file
90948672afbSGreg Clayton           m_coff_header_opt.sect_alignment, // Section alignment
910f754f88fSGreg Clayton           m_sect_headers[idx].flags));      // Flags for this section
911f754f88fSGreg Clayton 
912f1e0ae34SPavel Labath       uint32_t permissions = 0;
913f1e0ae34SPavel Labath       if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_MEM_EXECUTE)
914f1e0ae34SPavel Labath         permissions |= ePermissionsExecutable;
915f1e0ae34SPavel Labath       if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_MEM_READ)
916f1e0ae34SPavel Labath         permissions |= ePermissionsReadable;
917f1e0ae34SPavel Labath       if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_MEM_WRITE)
918f1e0ae34SPavel Labath         permissions |= ePermissionsWritable;
919f1e0ae34SPavel Labath       section_sp->SetPermissions(permissions);
920f1e0ae34SPavel Labath 
92173a7a55cSPavel Labath       m_sections_up->AddSection(section_sp);
92273a7a55cSPavel Labath       unified_section_list.AddSection(section_sp);
923f754f88fSGreg Clayton     }
924f754f88fSGreg Clayton   }
925a1743499SGreg Clayton }
926f754f88fSGreg Clayton 
927b8d03935SAaron Smith UUID ObjectFilePECOFF::GetUUID() {
928b8d03935SAaron Smith   if (m_uuid.IsValid())
929b8d03935SAaron Smith     return m_uuid;
930b8d03935SAaron Smith 
931b8d03935SAaron Smith   if (!CreateBinary())
932b8d03935SAaron Smith     return UUID();
933b8d03935SAaron Smith 
934b8d03935SAaron Smith   auto COFFObj =
935b8d03935SAaron Smith     llvm::cast<llvm::object::COFFObjectFile>(m_owningbin->getBinary());
936b8d03935SAaron Smith 
937b8d03935SAaron Smith   m_uuid = GetCoffUUID(COFFObj);
938b8d03935SAaron Smith   return m_uuid;
939b8d03935SAaron Smith }
940f754f88fSGreg Clayton 
941037ed1beSAaron Smith uint32_t ObjectFilePECOFF::ParseDependentModules() {
942037ed1beSAaron Smith   ModuleSP module_sp(GetModule());
943037ed1beSAaron Smith   if (!module_sp)
944f754f88fSGreg Clayton     return 0;
945037ed1beSAaron Smith 
946037ed1beSAaron Smith   std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
947037ed1beSAaron Smith   if (m_deps_filespec)
948037ed1beSAaron Smith     return m_deps_filespec->GetSize();
949037ed1beSAaron Smith 
950037ed1beSAaron Smith   // Cache coff binary if it is not done yet.
951037ed1beSAaron Smith   if (!CreateBinary())
952037ed1beSAaron Smith     return 0;
953037ed1beSAaron Smith 
954037ed1beSAaron Smith   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
95563e5fb76SJonas Devlieghere   LLDB_LOGF(log,
95663e5fb76SJonas Devlieghere             "%p ObjectFilePECOFF::ParseDependentModules() module = %p "
957037ed1beSAaron Smith             "(%s), binary = %p (Bin = %p)",
958037ed1beSAaron Smith             static_cast<void *>(this), static_cast<void *>(module_sp.get()),
959037ed1beSAaron Smith             module_sp->GetSpecificationDescription().c_str(),
960037ed1beSAaron Smith             static_cast<void *>(m_owningbin.getPointer()),
961b8d03935SAaron Smith             static_cast<void *>(m_owningbin->getBinary()));
962037ed1beSAaron Smith 
963037ed1beSAaron Smith   auto COFFObj =
964037ed1beSAaron Smith       llvm::dyn_cast<llvm::object::COFFObjectFile>(m_owningbin->getBinary());
965037ed1beSAaron Smith   if (!COFFObj)
966037ed1beSAaron Smith     return 0;
967037ed1beSAaron Smith 
968037ed1beSAaron Smith   m_deps_filespec = FileSpecList();
969037ed1beSAaron Smith 
970037ed1beSAaron Smith   for (const auto &entry : COFFObj->import_directories()) {
971037ed1beSAaron Smith     llvm::StringRef dll_name;
972037ed1beSAaron Smith     auto ec = entry.getName(dll_name);
973037ed1beSAaron Smith     // Report a bogus entry.
974037ed1beSAaron Smith     if (ec != std::error_code()) {
97563e5fb76SJonas Devlieghere       LLDB_LOGF(log,
97663e5fb76SJonas Devlieghere                 "ObjectFilePECOFF::ParseDependentModules() - failed to get "
977037ed1beSAaron Smith                 "import directory entry name: %s",
978037ed1beSAaron Smith                 ec.message().c_str());
979037ed1beSAaron Smith       continue;
980037ed1beSAaron Smith     }
981037ed1beSAaron Smith 
982037ed1beSAaron Smith     // At this moment we only have the base name of the DLL. The full path can
983037ed1beSAaron Smith     // only be seen after the dynamic loading.  Our best guess is Try to get it
984037ed1beSAaron Smith     // with the help of the object file's directory.
985b3f44ad9SStella Stamenova     llvm::SmallString<128> dll_fullpath;
986037ed1beSAaron Smith     FileSpec dll_specs(dll_name);
987037ed1beSAaron Smith     dll_specs.GetDirectory().SetString(m_file.GetDirectory().GetCString());
988037ed1beSAaron Smith 
989037ed1beSAaron Smith     if (!llvm::sys::fs::real_path(dll_specs.GetPath(), dll_fullpath))
990f893d5bfSJonas Devlieghere       m_deps_filespec->EmplaceBack(dll_fullpath);
991037ed1beSAaron Smith     else {
992037ed1beSAaron Smith       // Known DLLs or DLL not found in the object file directory.
993f893d5bfSJonas Devlieghere       m_deps_filespec->EmplaceBack(dll_name);
994037ed1beSAaron Smith     }
995037ed1beSAaron Smith   }
996037ed1beSAaron Smith   return m_deps_filespec->GetSize();
997037ed1beSAaron Smith }
998037ed1beSAaron Smith 
999037ed1beSAaron Smith uint32_t ObjectFilePECOFF::GetDependentModules(FileSpecList &files) {
1000037ed1beSAaron Smith   auto num_modules = ParseDependentModules();
1001037ed1beSAaron Smith   auto original_size = files.GetSize();
1002037ed1beSAaron Smith 
1003037ed1beSAaron Smith   for (unsigned i = 0; i < num_modules; ++i)
1004037ed1beSAaron Smith     files.AppendIfUnique(m_deps_filespec->GetFileSpecAtIndex(i));
1005037ed1beSAaron Smith 
1006037ed1beSAaron Smith   return files.GetSize() - original_size;
1007f754f88fSGreg Clayton }
1008f754f88fSGreg Clayton 
1009b9c1b51eSKate Stone lldb_private::Address ObjectFilePECOFF::GetEntryPointAddress() {
10108e38c666SStephane Sezer   if (m_entry_point_address.IsValid())
10118e38c666SStephane Sezer     return m_entry_point_address;
10128e38c666SStephane Sezer 
10138e38c666SStephane Sezer   if (!ParseHeader() || !IsExecutable())
10148e38c666SStephane Sezer     return m_entry_point_address;
10158e38c666SStephane Sezer 
10168e38c666SStephane Sezer   SectionList *section_list = GetSectionList();
1017a5235af9SAleksandr Urakov   addr_t file_addr = m_coff_header_opt.entry + m_coff_header_opt.image_base;
10188e38c666SStephane Sezer 
10198e38c666SStephane Sezer   if (!section_list)
1020a5235af9SAleksandr Urakov     m_entry_point_address.SetOffset(file_addr);
10218e38c666SStephane Sezer   else
1022b8d03935SAaron Smith     m_entry_point_address.ResolveAddressUsingFileSections(file_addr,
1023b8d03935SAaron Smith                                                           section_list);
10248e38c666SStephane Sezer   return m_entry_point_address;
10258e38c666SStephane Sezer }
10268e38c666SStephane Sezer 
1027d1304bbaSPavel Labath Address ObjectFilePECOFF::GetBaseAddress() {
1028d1304bbaSPavel Labath   return Address(GetSectionList()->GetSectionAtIndex(0), 0);
1029d1304bbaSPavel Labath }
1030d1304bbaSPavel Labath 
1031f754f88fSGreg Clayton // Dump
1032f754f88fSGreg Clayton //
1033f754f88fSGreg Clayton // Dump the specifics of the runtime file container (such as any headers
1034f754f88fSGreg Clayton // segments, sections, etc).
1035b9c1b51eSKate Stone void ObjectFilePECOFF::Dump(Stream *s) {
1036a1743499SGreg Clayton   ModuleSP module_sp(GetModule());
1037b9c1b51eSKate Stone   if (module_sp) {
103816ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
1039324a1036SSaleem Abdulrasool     s->Printf("%p: ", static_cast<void *>(this));
1040f754f88fSGreg Clayton     s->Indent();
1041f754f88fSGreg Clayton     s->PutCString("ObjectFilePECOFF");
1042f754f88fSGreg Clayton 
1043f760f5aeSPavel Labath     ArchSpec header_arch = GetArchitecture();
1044f754f88fSGreg Clayton 
1045b9c1b51eSKate Stone     *s << ", file = '" << m_file
1046b9c1b51eSKate Stone        << "', arch = " << header_arch.GetArchitectureName() << "\n";
1047f754f88fSGreg Clayton 
10483046e668SGreg Clayton     SectionList *sections = GetSectionList();
10493046e668SGreg Clayton     if (sections)
1050248a1305SKonrad Kleine       sections->Dump(s, nullptr, true, UINT32_MAX);
1051f754f88fSGreg Clayton 
1052d5b44036SJonas Devlieghere     if (m_symtab_up)
1053248a1305SKonrad Kleine       m_symtab_up->Dump(s, nullptr, eSortOrderNone);
1054f754f88fSGreg Clayton 
1055f754f88fSGreg Clayton     if (m_dos_header.e_magic)
1056f754f88fSGreg Clayton       DumpDOSHeader(s, m_dos_header);
1057b9c1b51eSKate Stone     if (m_coff_header.machine) {
1058f754f88fSGreg Clayton       DumpCOFFHeader(s, m_coff_header);
1059f754f88fSGreg Clayton       if (m_coff_header.hdrsize)
1060f754f88fSGreg Clayton         DumpOptCOFFHeader(s, m_coff_header_opt);
1061f754f88fSGreg Clayton     }
1062f754f88fSGreg Clayton     s->EOL();
1063f754f88fSGreg Clayton     DumpSectionHeaders(s);
1064f754f88fSGreg Clayton     s->EOL();
1065037ed1beSAaron Smith 
1066037ed1beSAaron Smith     DumpDependentModules(s);
1067037ed1beSAaron Smith     s->EOL();
1068f754f88fSGreg Clayton   }
1069a1743499SGreg Clayton }
1070f754f88fSGreg Clayton 
1071f754f88fSGreg Clayton // DumpDOSHeader
1072f754f88fSGreg Clayton //
1073f754f88fSGreg Clayton // Dump the MS-DOS header to the specified output stream
1074b9c1b51eSKate Stone void ObjectFilePECOFF::DumpDOSHeader(Stream *s, const dos_header_t &header) {
1075f754f88fSGreg Clayton   s->PutCString("MSDOS Header\n");
1076f754f88fSGreg Clayton   s->Printf("  e_magic    = 0x%4.4x\n", header.e_magic);
1077f754f88fSGreg Clayton   s->Printf("  e_cblp     = 0x%4.4x\n", header.e_cblp);
1078f754f88fSGreg Clayton   s->Printf("  e_cp       = 0x%4.4x\n", header.e_cp);
1079f754f88fSGreg Clayton   s->Printf("  e_crlc     = 0x%4.4x\n", header.e_crlc);
1080f754f88fSGreg Clayton   s->Printf("  e_cparhdr  = 0x%4.4x\n", header.e_cparhdr);
1081f754f88fSGreg Clayton   s->Printf("  e_minalloc = 0x%4.4x\n", header.e_minalloc);
1082f754f88fSGreg Clayton   s->Printf("  e_maxalloc = 0x%4.4x\n", header.e_maxalloc);
1083f754f88fSGreg Clayton   s->Printf("  e_ss       = 0x%4.4x\n", header.e_ss);
1084f754f88fSGreg Clayton   s->Printf("  e_sp       = 0x%4.4x\n", header.e_sp);
1085f754f88fSGreg Clayton   s->Printf("  e_csum     = 0x%4.4x\n", header.e_csum);
1086f754f88fSGreg Clayton   s->Printf("  e_ip       = 0x%4.4x\n", header.e_ip);
1087f754f88fSGreg Clayton   s->Printf("  e_cs       = 0x%4.4x\n", header.e_cs);
1088f754f88fSGreg Clayton   s->Printf("  e_lfarlc   = 0x%4.4x\n", header.e_lfarlc);
1089f754f88fSGreg Clayton   s->Printf("  e_ovno     = 0x%4.4x\n", header.e_ovno);
1090f754f88fSGreg Clayton   s->Printf("  e_res[4]   = { 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x }\n",
1091b9c1b51eSKate Stone             header.e_res[0], header.e_res[1], header.e_res[2], header.e_res[3]);
1092f754f88fSGreg Clayton   s->Printf("  e_oemid    = 0x%4.4x\n", header.e_oemid);
1093f754f88fSGreg Clayton   s->Printf("  e_oeminfo  = 0x%4.4x\n", header.e_oeminfo);
1094b9c1b51eSKate Stone   s->Printf("  e_res2[10] = { 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, "
1095b9c1b51eSKate Stone             "0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x }\n",
1096b9c1b51eSKate Stone             header.e_res2[0], header.e_res2[1], header.e_res2[2],
1097b9c1b51eSKate Stone             header.e_res2[3], header.e_res2[4], header.e_res2[5],
1098b9c1b51eSKate Stone             header.e_res2[6], header.e_res2[7], header.e_res2[8],
1099f754f88fSGreg Clayton             header.e_res2[9]);
1100f754f88fSGreg Clayton   s->Printf("  e_lfanew   = 0x%8.8x\n", header.e_lfanew);
1101f754f88fSGreg Clayton }
1102f754f88fSGreg Clayton 
1103f754f88fSGreg Clayton // DumpCOFFHeader
1104f754f88fSGreg Clayton //
1105f754f88fSGreg Clayton // Dump the COFF header to the specified output stream
1106b9c1b51eSKate Stone void ObjectFilePECOFF::DumpCOFFHeader(Stream *s, const coff_header_t &header) {
1107f754f88fSGreg Clayton   s->PutCString("COFF Header\n");
1108f754f88fSGreg Clayton   s->Printf("  machine = 0x%4.4x\n", header.machine);
1109f754f88fSGreg Clayton   s->Printf("  nsects  = 0x%4.4x\n", header.nsects);
1110f754f88fSGreg Clayton   s->Printf("  modtime = 0x%8.8x\n", header.modtime);
1111f754f88fSGreg Clayton   s->Printf("  symoff  = 0x%8.8x\n", header.symoff);
1112f754f88fSGreg Clayton   s->Printf("  nsyms   = 0x%8.8x\n", header.nsyms);
1113f754f88fSGreg Clayton   s->Printf("  hdrsize = 0x%4.4x\n", header.hdrsize);
1114f754f88fSGreg Clayton }
1115f754f88fSGreg Clayton 
1116f754f88fSGreg Clayton // DumpOptCOFFHeader
1117f754f88fSGreg Clayton //
1118f754f88fSGreg Clayton // Dump the optional COFF header to the specified output stream
1119b9c1b51eSKate Stone void ObjectFilePECOFF::DumpOptCOFFHeader(Stream *s,
1120b9c1b51eSKate Stone                                          const coff_opt_header_t &header) {
1121f754f88fSGreg Clayton   s->PutCString("Optional COFF Header\n");
1122f754f88fSGreg Clayton   s->Printf("  magic                   = 0x%4.4x\n", header.magic);
1123b9c1b51eSKate Stone   s->Printf("  major_linker_version    = 0x%2.2x\n",
1124b9c1b51eSKate Stone             header.major_linker_version);
1125b9c1b51eSKate Stone   s->Printf("  minor_linker_version    = 0x%2.2x\n",
1126b9c1b51eSKate Stone             header.minor_linker_version);
1127f754f88fSGreg Clayton   s->Printf("  code_size               = 0x%8.8x\n", header.code_size);
1128f754f88fSGreg Clayton   s->Printf("  data_size               = 0x%8.8x\n", header.data_size);
1129f754f88fSGreg Clayton   s->Printf("  bss_size                = 0x%8.8x\n", header.bss_size);
1130f754f88fSGreg Clayton   s->Printf("  entry                   = 0x%8.8x\n", header.entry);
1131f754f88fSGreg Clayton   s->Printf("  code_offset             = 0x%8.8x\n", header.code_offset);
1132f754f88fSGreg Clayton   s->Printf("  data_offset             = 0x%8.8x\n", header.data_offset);
1133b9c1b51eSKate Stone   s->Printf("  image_base              = 0x%16.16" PRIx64 "\n",
1134b9c1b51eSKate Stone             header.image_base);
1135f754f88fSGreg Clayton   s->Printf("  sect_alignment          = 0x%8.8x\n", header.sect_alignment);
1136f754f88fSGreg Clayton   s->Printf("  file_alignment          = 0x%8.8x\n", header.file_alignment);
1137b9c1b51eSKate Stone   s->Printf("  major_os_system_version = 0x%4.4x\n",
1138b9c1b51eSKate Stone             header.major_os_system_version);
1139b9c1b51eSKate Stone   s->Printf("  minor_os_system_version = 0x%4.4x\n",
1140b9c1b51eSKate Stone             header.minor_os_system_version);
1141b9c1b51eSKate Stone   s->Printf("  major_image_version     = 0x%4.4x\n",
1142b9c1b51eSKate Stone             header.major_image_version);
1143b9c1b51eSKate Stone   s->Printf("  minor_image_version     = 0x%4.4x\n",
1144b9c1b51eSKate Stone             header.minor_image_version);
1145b9c1b51eSKate Stone   s->Printf("  major_subsystem_version = 0x%4.4x\n",
1146b9c1b51eSKate Stone             header.major_subsystem_version);
1147b9c1b51eSKate Stone   s->Printf("  minor_subsystem_version = 0x%4.4x\n",
1148b9c1b51eSKate Stone             header.minor_subsystem_version);
1149f754f88fSGreg Clayton   s->Printf("  reserved1               = 0x%8.8x\n", header.reserved1);
1150f754f88fSGreg Clayton   s->Printf("  image_size              = 0x%8.8x\n", header.image_size);
1151f754f88fSGreg Clayton   s->Printf("  header_size             = 0x%8.8x\n", header.header_size);
115228469ca3SGreg Clayton   s->Printf("  checksum                = 0x%8.8x\n", header.checksum);
1153f754f88fSGreg Clayton   s->Printf("  subsystem               = 0x%4.4x\n", header.subsystem);
1154f754f88fSGreg Clayton   s->Printf("  dll_flags               = 0x%4.4x\n", header.dll_flags);
1155b9c1b51eSKate Stone   s->Printf("  stack_reserve_size      = 0x%16.16" PRIx64 "\n",
1156b9c1b51eSKate Stone             header.stack_reserve_size);
1157b9c1b51eSKate Stone   s->Printf("  stack_commit_size       = 0x%16.16" PRIx64 "\n",
1158b9c1b51eSKate Stone             header.stack_commit_size);
1159b9c1b51eSKate Stone   s->Printf("  heap_reserve_size       = 0x%16.16" PRIx64 "\n",
1160b9c1b51eSKate Stone             header.heap_reserve_size);
1161b9c1b51eSKate Stone   s->Printf("  heap_commit_size        = 0x%16.16" PRIx64 "\n",
1162b9c1b51eSKate Stone             header.heap_commit_size);
1163f754f88fSGreg Clayton   s->Printf("  loader_flags            = 0x%8.8x\n", header.loader_flags);
1164b9c1b51eSKate Stone   s->Printf("  num_data_dir_entries    = 0x%8.8x\n",
1165b9c1b51eSKate Stone             (uint32_t)header.data_dirs.size());
1166f754f88fSGreg Clayton   uint32_t i;
1167b9c1b51eSKate Stone   for (i = 0; i < header.data_dirs.size(); i++) {
1168b9c1b51eSKate Stone     s->Printf("  data_dirs[%2u] vmaddr = 0x%8.8x, vmsize = 0x%8.8x\n", i,
1169b9c1b51eSKate Stone               header.data_dirs[i].vmaddr, header.data_dirs[i].vmsize);
1170f754f88fSGreg Clayton   }
1171f754f88fSGreg Clayton }
1172f754f88fSGreg Clayton // DumpSectionHeader
1173f754f88fSGreg Clayton //
1174f754f88fSGreg Clayton // Dump a single ELF section header to the specified output stream
1175b9c1b51eSKate Stone void ObjectFilePECOFF::DumpSectionHeader(Stream *s,
1176b9c1b51eSKate Stone                                          const section_header_t &sh) {
1177adcd0268SBenjamin Kramer   std::string name = std::string(GetSectionName(sh));
1178b9c1b51eSKate 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 "
1179b9c1b51eSKate Stone             "0x%4.4x 0x%8.8x\n",
1180b9c1b51eSKate Stone             name.c_str(), sh.vmaddr, sh.vmsize, sh.offset, sh.size, sh.reloff,
1181b9c1b51eSKate Stone             sh.lineoff, sh.nreloc, sh.nline, sh.flags);
1182f754f88fSGreg Clayton }
1183f754f88fSGreg Clayton 
1184f754f88fSGreg Clayton // DumpSectionHeaders
1185f754f88fSGreg Clayton //
1186f754f88fSGreg Clayton // Dump all of the ELF section header to the specified output stream
1187b9c1b51eSKate Stone void ObjectFilePECOFF::DumpSectionHeaders(Stream *s) {
1188f754f88fSGreg Clayton 
1189f754f88fSGreg Clayton   s->PutCString("Section Headers\n");
1190b9c1b51eSKate Stone   s->PutCString("IDX  name             vm addr    vm size    file off   file "
1191b9c1b51eSKate Stone                 "size  reloc off  line off   nreloc nline  flags\n");
1192b9c1b51eSKate Stone   s->PutCString("==== ---------------- ---------- ---------- ---------- "
1193b9c1b51eSKate Stone                 "---------- ---------- ---------- ------ ------ ----------\n");
1194f754f88fSGreg Clayton 
1195f754f88fSGreg Clayton   uint32_t idx = 0;
1196f754f88fSGreg Clayton   SectionHeaderCollIter pos, end = m_sect_headers.end();
1197f754f88fSGreg Clayton 
1198b9c1b51eSKate Stone   for (pos = m_sect_headers.begin(); pos != end; ++pos, ++idx) {
1199f754f88fSGreg Clayton     s->Printf("[%2u] ", idx);
1200f754f88fSGreg Clayton     ObjectFilePECOFF::DumpSectionHeader(s, *pos);
1201f754f88fSGreg Clayton   }
1202f754f88fSGreg Clayton }
1203f754f88fSGreg Clayton 
1204037ed1beSAaron Smith // DumpDependentModules
1205037ed1beSAaron Smith //
1206037ed1beSAaron Smith // Dump all of the dependent modules to the specified output stream
1207037ed1beSAaron Smith void ObjectFilePECOFF::DumpDependentModules(lldb_private::Stream *s) {
1208037ed1beSAaron Smith   auto num_modules = ParseDependentModules();
1209037ed1beSAaron Smith   if (num_modules > 0) {
1210037ed1beSAaron Smith     s->PutCString("Dependent Modules\n");
1211037ed1beSAaron Smith     for (unsigned i = 0; i < num_modules; ++i) {
1212037ed1beSAaron Smith       auto spec = m_deps_filespec->GetFileSpecAtIndex(i);
1213037ed1beSAaron Smith       s->Printf("  %s\n", spec.GetFilename().GetCString());
1214037ed1beSAaron Smith     }
1215037ed1beSAaron Smith   }
1216037ed1beSAaron Smith }
1217037ed1beSAaron Smith 
1218fb3b3bd1SZachary Turner bool ObjectFilePECOFF::IsWindowsSubsystem() {
1219fb3b3bd1SZachary Turner   switch (m_coff_header_opt.subsystem) {
1220fb3b3bd1SZachary Turner   case llvm::COFF::IMAGE_SUBSYSTEM_NATIVE:
1221fb3b3bd1SZachary Turner   case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_GUI:
1222fb3b3bd1SZachary Turner   case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_CUI:
1223fb3b3bd1SZachary Turner   case llvm::COFF::IMAGE_SUBSYSTEM_NATIVE_WINDOWS:
1224fb3b3bd1SZachary Turner   case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_CE_GUI:
1225fb3b3bd1SZachary Turner   case llvm::COFF::IMAGE_SUBSYSTEM_XBOX:
1226fb3b3bd1SZachary Turner   case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_BOOT_APPLICATION:
1227fb3b3bd1SZachary Turner     return true;
1228fb3b3bd1SZachary Turner   default:
1229fb3b3bd1SZachary Turner     return false;
1230fb3b3bd1SZachary Turner   }
1231fb3b3bd1SZachary Turner }
1232fb3b3bd1SZachary Turner 
1233f760f5aeSPavel Labath ArchSpec ObjectFilePECOFF::GetArchitecture() {
1234237ad974SCharles Davis   uint16_t machine = m_coff_header.machine;
1235b9c1b51eSKate Stone   switch (machine) {
1236f760f5aeSPavel Labath   default:
1237f760f5aeSPavel Labath     break;
1238237ad974SCharles Davis   case llvm::COFF::IMAGE_FILE_MACHINE_AMD64:
1239237ad974SCharles Davis   case llvm::COFF::IMAGE_FILE_MACHINE_I386:
1240237ad974SCharles Davis   case llvm::COFF::IMAGE_FILE_MACHINE_POWERPC:
1241237ad974SCharles Davis   case llvm::COFF::IMAGE_FILE_MACHINE_POWERPCFP:
1242237ad974SCharles Davis   case llvm::COFF::IMAGE_FILE_MACHINE_ARM:
12431108cb36SSaleem Abdulrasool   case llvm::COFF::IMAGE_FILE_MACHINE_ARMNT:
1244237ad974SCharles Davis   case llvm::COFF::IMAGE_FILE_MACHINE_THUMB:
1245638f072fSMartin Storsjo   case llvm::COFF::IMAGE_FILE_MACHINE_ARM64:
1246f760f5aeSPavel Labath     ArchSpec arch;
1247fb3b3bd1SZachary Turner     arch.SetArchitecture(eArchTypeCOFF, machine, LLDB_INVALID_CPUTYPE,
1248fb3b3bd1SZachary Turner                          IsWindowsSubsystem() ? llvm::Triple::Win32
1249fb3b3bd1SZachary Turner                                               : llvm::Triple::UnknownOS);
1250f760f5aeSPavel Labath     return arch;
1251237ad974SCharles Davis   }
1252f760f5aeSPavel Labath   return ArchSpec();
1253f754f88fSGreg Clayton }
1254f754f88fSGreg Clayton 
1255b9c1b51eSKate Stone ObjectFile::Type ObjectFilePECOFF::CalculateType() {
1256b9c1b51eSKate Stone   if (m_coff_header.machine != 0) {
1257237ad974SCharles Davis     if ((m_coff_header.flags & llvm::COFF::IMAGE_FILE_DLL) == 0)
1258f754f88fSGreg Clayton       return eTypeExecutable;
1259f754f88fSGreg Clayton     else
1260f754f88fSGreg Clayton       return eTypeSharedLibrary;
1261f754f88fSGreg Clayton   }
1262f754f88fSGreg Clayton   return eTypeExecutable;
1263f754f88fSGreg Clayton }
1264f754f88fSGreg Clayton 
1265b9c1b51eSKate Stone ObjectFile::Strata ObjectFilePECOFF::CalculateStrata() { return eStrataUser; }
12669cad24a7SZachary Turner 
1267f754f88fSGreg Clayton // PluginInterface protocol
1268b9c1b51eSKate Stone ConstString ObjectFilePECOFF::GetPluginName() { return GetPluginNameStatic(); }
1269f754f88fSGreg Clayton 
1270b9c1b51eSKate Stone uint32_t ObjectFilePECOFF::GetPluginVersion() { return 1; }
1271