1 //===-- ObjectFilePECOFF.cpp ------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "ObjectFilePECOFF.h"
11 #include "WindowsMiniDump.h"
12 
13 #include "llvm/BinaryFormat/COFF.h"
14 
15 #include "lldb/Core/ArchSpec.h"
16 #include "lldb/Core/FileSpecList.h"
17 #include "lldb/Core/Module.h"
18 #include "lldb/Core/ModuleSpec.h"
19 #include "lldb/Core/PluginManager.h"
20 #include "lldb/Core/Section.h"
21 #include "lldb/Core/StreamFile.h"
22 #include "lldb/Symbol/ObjectFile.h"
23 #include "lldb/Target/Process.h"
24 #include "lldb/Target/SectionLoadList.h"
25 #include "lldb/Target/Target.h"
26 #include "lldb/Utility/DataBufferHeap.h"
27 #include "lldb/Utility/DataBufferLLVM.h"
28 #include "lldb/Utility/FileSpec.h"
29 #include "lldb/Utility/StreamString.h"
30 #include "lldb/Utility/Timer.h"
31 #include "lldb/Utility/UUID.h"
32 
33 #include "llvm/Support/MemoryBuffer.h"
34 
35 #define IMAGE_DOS_SIGNATURE 0x5A4D    // MZ
36 #define IMAGE_NT_SIGNATURE 0x00004550 // PE00
37 #define OPT_HEADER_MAGIC_PE32 0x010b
38 #define OPT_HEADER_MAGIC_PE32_PLUS 0x020b
39 
40 using namespace lldb;
41 using namespace lldb_private;
42 
43 void ObjectFilePECOFF::Initialize() {
44   PluginManager::RegisterPlugin(
45       GetPluginNameStatic(), GetPluginDescriptionStatic(), CreateInstance,
46       CreateMemoryInstance, GetModuleSpecifications, SaveCore);
47 }
48 
49 void ObjectFilePECOFF::Terminate() {
50   PluginManager::UnregisterPlugin(CreateInstance);
51 }
52 
53 lldb_private::ConstString ObjectFilePECOFF::GetPluginNameStatic() {
54   static ConstString g_name("pe-coff");
55   return g_name;
56 }
57 
58 const char *ObjectFilePECOFF::GetPluginDescriptionStatic() {
59   return "Portable Executable and Common Object File Format object file reader "
60          "(32 and 64 bit)";
61 }
62 
63 ObjectFile *ObjectFilePECOFF::CreateInstance(const lldb::ModuleSP &module_sp,
64                                              DataBufferSP &data_sp,
65                                              lldb::offset_t data_offset,
66                                              const lldb_private::FileSpec *file,
67                                              lldb::offset_t file_offset,
68                                              lldb::offset_t length) {
69   if (!data_sp) {
70     data_sp =
71         DataBufferLLVM::CreateSliceFromPath(file->GetPath(), length, file_offset);
72     if (!data_sp)
73       return nullptr;
74     data_offset = 0;
75   }
76 
77   if (!ObjectFilePECOFF::MagicBytesMatch(data_sp))
78     return nullptr;
79 
80   // Update the data to contain the entire file if it doesn't already
81   if (data_sp->GetByteSize() < length) {
82     data_sp =
83         DataBufferLLVM::CreateSliceFromPath(file->GetPath(), length, file_offset);
84     if (!data_sp)
85       return nullptr;
86   }
87 
88   auto objfile_ap = llvm::make_unique<ObjectFilePECOFF>(
89       module_sp, data_sp, data_offset, file, file_offset, length);
90   if (!objfile_ap || !objfile_ap->ParseHeader())
91     return nullptr;
92 
93   return objfile_ap.release();
94 }
95 
96 ObjectFile *ObjectFilePECOFF::CreateMemoryInstance(
97     const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp,
98     const lldb::ProcessSP &process_sp, lldb::addr_t header_addr) {
99   if (!data_sp || !ObjectFilePECOFF::MagicBytesMatch(data_sp))
100     return nullptr;
101   auto objfile_ap = llvm::make_unique<ObjectFilePECOFF>(
102       module_sp, data_sp, process_sp, header_addr);
103   if (objfile_ap.get() && objfile_ap->ParseHeader()) {
104     return objfile_ap.release();
105   }
106   return nullptr;
107 }
108 
109 size_t ObjectFilePECOFF::GetModuleSpecifications(
110     const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp,
111     lldb::offset_t data_offset, lldb::offset_t file_offset,
112     lldb::offset_t length, lldb_private::ModuleSpecList &specs) {
113   const size_t initial_count = specs.GetSize();
114 
115   if (ObjectFilePECOFF::MagicBytesMatch(data_sp)) {
116     DataExtractor data;
117     data.SetData(data_sp, data_offset, length);
118     data.SetByteOrder(eByteOrderLittle);
119 
120     dos_header_t dos_header;
121     coff_header_t coff_header;
122 
123     if (ParseDOSHeader(data, dos_header)) {
124       lldb::offset_t offset = dos_header.e_lfanew;
125       uint32_t pe_signature = data.GetU32(&offset);
126       if (pe_signature != IMAGE_NT_SIGNATURE)
127         return false;
128       if (ParseCOFFHeader(data, &offset, coff_header)) {
129         ArchSpec spec;
130         if (coff_header.machine == MachineAmd64) {
131           spec.SetTriple("x86_64-pc-windows");
132           specs.Append(ModuleSpec(file, spec));
133         } else if (coff_header.machine == MachineX86) {
134           spec.SetTriple("i386-pc-windows");
135           specs.Append(ModuleSpec(file, spec));
136           spec.SetTriple("i686-pc-windows");
137           specs.Append(ModuleSpec(file, spec));
138         }
139         else if (coff_header.machine == MachineArmNt)
140         {
141           spec.SetTriple("arm-pc-windows");
142           specs.Append(ModuleSpec(file, spec));
143         }
144       }
145     }
146   }
147 
148   return specs.GetSize() - initial_count;
149 }
150 
151 bool ObjectFilePECOFF::SaveCore(const lldb::ProcessSP &process_sp,
152                                 const lldb_private::FileSpec &outfile,
153                                 lldb_private::Status &error) {
154   return SaveMiniDump(process_sp, outfile, error);
155 }
156 
157 bool ObjectFilePECOFF::MagicBytesMatch(DataBufferSP &data_sp) {
158   DataExtractor data(data_sp, eByteOrderLittle, 4);
159   lldb::offset_t offset = 0;
160   uint16_t magic = data.GetU16(&offset);
161   return magic == IMAGE_DOS_SIGNATURE;
162 }
163 
164 lldb::SymbolType ObjectFilePECOFF::MapSymbolType(uint16_t coff_symbol_type) {
165   // TODO:  We need to complete this mapping of COFF symbol types to LLDB ones.
166   // For now, here's a hack to make sure our function have types.
167   const auto complex_type =
168       coff_symbol_type >> llvm::COFF::SCT_COMPLEX_TYPE_SHIFT;
169   if (complex_type == llvm::COFF::IMAGE_SYM_DTYPE_FUNCTION) {
170     return lldb::eSymbolTypeCode;
171   }
172   return lldb::eSymbolTypeInvalid;
173 }
174 
175 ObjectFilePECOFF::ObjectFilePECOFF(const lldb::ModuleSP &module_sp,
176                                    DataBufferSP &data_sp,
177                                    lldb::offset_t data_offset,
178                                    const FileSpec *file,
179                                    lldb::offset_t file_offset,
180                                    lldb::offset_t length)
181     : ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset),
182       m_dos_header(), m_coff_header(), m_coff_header_opt(), m_sect_headers(),
183       m_entry_point_address() {
184   ::memset(&m_dos_header, 0, sizeof(m_dos_header));
185   ::memset(&m_coff_header, 0, sizeof(m_coff_header));
186   ::memset(&m_coff_header_opt, 0, sizeof(m_coff_header_opt));
187 }
188 
189 ObjectFilePECOFF::ObjectFilePECOFF(const lldb::ModuleSP &module_sp,
190                                    DataBufferSP &header_data_sp,
191                                    const lldb::ProcessSP &process_sp,
192                                    addr_t header_addr)
193     : ObjectFile(module_sp, process_sp, header_addr, header_data_sp),
194       m_dos_header(), m_coff_header(), m_coff_header_opt(), m_sect_headers(),
195       m_entry_point_address() {
196   ::memset(&m_dos_header, 0, sizeof(m_dos_header));
197   ::memset(&m_coff_header, 0, sizeof(m_coff_header));
198   ::memset(&m_coff_header_opt, 0, sizeof(m_coff_header_opt));
199 }
200 
201 ObjectFilePECOFF::~ObjectFilePECOFF() {}
202 
203 bool ObjectFilePECOFF::ParseHeader() {
204   ModuleSP module_sp(GetModule());
205   if (module_sp) {
206     std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
207     m_sect_headers.clear();
208     m_data.SetByteOrder(eByteOrderLittle);
209     lldb::offset_t offset = 0;
210 
211     if (ParseDOSHeader(m_data, m_dos_header)) {
212       offset = m_dos_header.e_lfanew;
213       uint32_t pe_signature = m_data.GetU32(&offset);
214       if (pe_signature != IMAGE_NT_SIGNATURE)
215         return false;
216       if (ParseCOFFHeader(m_data, &offset, m_coff_header)) {
217         if (m_coff_header.hdrsize > 0)
218           ParseCOFFOptionalHeader(&offset);
219         ParseSectionHeaders(offset);
220       }
221       return true;
222     }
223   }
224   return false;
225 }
226 
227 bool ObjectFilePECOFF::SetLoadAddress(Target &target, addr_t value,
228                                       bool value_is_offset) {
229   bool changed = false;
230   ModuleSP module_sp = GetModule();
231   if (module_sp) {
232     size_t num_loaded_sections = 0;
233     SectionList *section_list = GetSectionList();
234     if (section_list) {
235       if (!value_is_offset) {
236         value -= m_image_base;
237       }
238 
239       const size_t num_sections = section_list->GetSize();
240       size_t sect_idx = 0;
241 
242       for (sect_idx = 0; sect_idx < num_sections; ++sect_idx) {
243         // Iterate through the object file sections to find all
244         // of the sections that have SHF_ALLOC in their flag bits.
245         SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx));
246         if (section_sp && !section_sp->IsThreadSpecific()) {
247           if (target.GetSectionLoadList().SetSectionLoadAddress(
248                   section_sp, section_sp->GetFileAddress() + value))
249             ++num_loaded_sections;
250         }
251       }
252       changed = num_loaded_sections > 0;
253     }
254   }
255   return changed;
256 }
257 
258 ByteOrder ObjectFilePECOFF::GetByteOrder() const { return eByteOrderLittle; }
259 
260 bool ObjectFilePECOFF::IsExecutable() const {
261   return (m_coff_header.flags & llvm::COFF::IMAGE_FILE_DLL) == 0;
262 }
263 
264 uint32_t ObjectFilePECOFF::GetAddressByteSize() const {
265   if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32_PLUS)
266     return 8;
267   else if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32)
268     return 4;
269   return 4;
270 }
271 
272 //----------------------------------------------------------------------
273 // NeedsEndianSwap
274 //
275 // Return true if an endian swap needs to occur when extracting data
276 // from this file.
277 //----------------------------------------------------------------------
278 bool ObjectFilePECOFF::NeedsEndianSwap() const {
279 #if defined(__LITTLE_ENDIAN__)
280   return false;
281 #else
282   return true;
283 #endif
284 }
285 //----------------------------------------------------------------------
286 // ParseDOSHeader
287 //----------------------------------------------------------------------
288 bool ObjectFilePECOFF::ParseDOSHeader(DataExtractor &data,
289                                       dos_header_t &dos_header) {
290   bool success = false;
291   lldb::offset_t offset = 0;
292   success = data.ValidOffsetForDataOfSize(0, sizeof(dos_header));
293 
294   if (success) {
295     dos_header.e_magic = data.GetU16(&offset); // Magic number
296     success = dos_header.e_magic == IMAGE_DOS_SIGNATURE;
297 
298     if (success) {
299       dos_header.e_cblp = data.GetU16(&offset); // Bytes on last page of file
300       dos_header.e_cp = data.GetU16(&offset);   // Pages in file
301       dos_header.e_crlc = data.GetU16(&offset); // Relocations
302       dos_header.e_cparhdr =
303           data.GetU16(&offset); // Size of header in paragraphs
304       dos_header.e_minalloc =
305           data.GetU16(&offset); // Minimum extra paragraphs needed
306       dos_header.e_maxalloc =
307           data.GetU16(&offset);               // Maximum extra paragraphs needed
308       dos_header.e_ss = data.GetU16(&offset); // Initial (relative) SS value
309       dos_header.e_sp = data.GetU16(&offset); // Initial SP value
310       dos_header.e_csum = data.GetU16(&offset); // Checksum
311       dos_header.e_ip = data.GetU16(&offset);   // Initial IP value
312       dos_header.e_cs = data.GetU16(&offset);   // Initial (relative) CS value
313       dos_header.e_lfarlc =
314           data.GetU16(&offset); // File address of relocation table
315       dos_header.e_ovno = data.GetU16(&offset); // Overlay number
316 
317       dos_header.e_res[0] = data.GetU16(&offset); // Reserved words
318       dos_header.e_res[1] = data.GetU16(&offset); // Reserved words
319       dos_header.e_res[2] = data.GetU16(&offset); // Reserved words
320       dos_header.e_res[3] = data.GetU16(&offset); // Reserved words
321 
322       dos_header.e_oemid =
323           data.GetU16(&offset); // OEM identifier (for e_oeminfo)
324       dos_header.e_oeminfo =
325           data.GetU16(&offset); // OEM information; e_oemid specific
326       dos_header.e_res2[0] = data.GetU16(&offset); // Reserved words
327       dos_header.e_res2[1] = data.GetU16(&offset); // Reserved words
328       dos_header.e_res2[2] = data.GetU16(&offset); // Reserved words
329       dos_header.e_res2[3] = data.GetU16(&offset); // Reserved words
330       dos_header.e_res2[4] = data.GetU16(&offset); // Reserved words
331       dos_header.e_res2[5] = data.GetU16(&offset); // Reserved words
332       dos_header.e_res2[6] = data.GetU16(&offset); // Reserved words
333       dos_header.e_res2[7] = data.GetU16(&offset); // Reserved words
334       dos_header.e_res2[8] = data.GetU16(&offset); // Reserved words
335       dos_header.e_res2[9] = data.GetU16(&offset); // Reserved words
336 
337       dos_header.e_lfanew =
338           data.GetU32(&offset); // File address of new exe header
339     }
340   }
341   if (!success)
342     memset(&dos_header, 0, sizeof(dos_header));
343   return success;
344 }
345 
346 //----------------------------------------------------------------------
347 // ParserCOFFHeader
348 //----------------------------------------------------------------------
349 bool ObjectFilePECOFF::ParseCOFFHeader(DataExtractor &data,
350                                        lldb::offset_t *offset_ptr,
351                                        coff_header_t &coff_header) {
352   bool success =
353       data.ValidOffsetForDataOfSize(*offset_ptr, sizeof(coff_header));
354   if (success) {
355     coff_header.machine = data.GetU16(offset_ptr);
356     coff_header.nsects = data.GetU16(offset_ptr);
357     coff_header.modtime = data.GetU32(offset_ptr);
358     coff_header.symoff = data.GetU32(offset_ptr);
359     coff_header.nsyms = data.GetU32(offset_ptr);
360     coff_header.hdrsize = data.GetU16(offset_ptr);
361     coff_header.flags = data.GetU16(offset_ptr);
362   }
363   if (!success)
364     memset(&coff_header, 0, sizeof(coff_header));
365   return success;
366 }
367 
368 bool ObjectFilePECOFF::ParseCOFFOptionalHeader(lldb::offset_t *offset_ptr) {
369   bool success = false;
370   const lldb::offset_t end_offset = *offset_ptr + m_coff_header.hdrsize;
371   if (*offset_ptr < end_offset) {
372     success = true;
373     m_coff_header_opt.magic = m_data.GetU16(offset_ptr);
374     m_coff_header_opt.major_linker_version = m_data.GetU8(offset_ptr);
375     m_coff_header_opt.minor_linker_version = m_data.GetU8(offset_ptr);
376     m_coff_header_opt.code_size = m_data.GetU32(offset_ptr);
377     m_coff_header_opt.data_size = m_data.GetU32(offset_ptr);
378     m_coff_header_opt.bss_size = m_data.GetU32(offset_ptr);
379     m_coff_header_opt.entry = m_data.GetU32(offset_ptr);
380     m_coff_header_opt.code_offset = m_data.GetU32(offset_ptr);
381 
382     const uint32_t addr_byte_size = GetAddressByteSize();
383 
384     if (*offset_ptr < end_offset) {
385       if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32) {
386         // PE32 only
387         m_coff_header_opt.data_offset = m_data.GetU32(offset_ptr);
388       } else
389         m_coff_header_opt.data_offset = 0;
390 
391       if (*offset_ptr < end_offset) {
392         m_coff_header_opt.image_base =
393             m_data.GetMaxU64(offset_ptr, addr_byte_size);
394         m_coff_header_opt.sect_alignment = m_data.GetU32(offset_ptr);
395         m_coff_header_opt.file_alignment = m_data.GetU32(offset_ptr);
396         m_coff_header_opt.major_os_system_version = m_data.GetU16(offset_ptr);
397         m_coff_header_opt.minor_os_system_version = m_data.GetU16(offset_ptr);
398         m_coff_header_opt.major_image_version = m_data.GetU16(offset_ptr);
399         m_coff_header_opt.minor_image_version = m_data.GetU16(offset_ptr);
400         m_coff_header_opt.major_subsystem_version = m_data.GetU16(offset_ptr);
401         m_coff_header_opt.minor_subsystem_version = m_data.GetU16(offset_ptr);
402         m_coff_header_opt.reserved1 = m_data.GetU32(offset_ptr);
403         m_coff_header_opt.image_size = m_data.GetU32(offset_ptr);
404         m_coff_header_opt.header_size = m_data.GetU32(offset_ptr);
405         m_coff_header_opt.checksum = m_data.GetU32(offset_ptr);
406         m_coff_header_opt.subsystem = m_data.GetU16(offset_ptr);
407         m_coff_header_opt.dll_flags = m_data.GetU16(offset_ptr);
408         m_coff_header_opt.stack_reserve_size =
409             m_data.GetMaxU64(offset_ptr, addr_byte_size);
410         m_coff_header_opt.stack_commit_size =
411             m_data.GetMaxU64(offset_ptr, addr_byte_size);
412         m_coff_header_opt.heap_reserve_size =
413             m_data.GetMaxU64(offset_ptr, addr_byte_size);
414         m_coff_header_opt.heap_commit_size =
415             m_data.GetMaxU64(offset_ptr, addr_byte_size);
416         m_coff_header_opt.loader_flags = m_data.GetU32(offset_ptr);
417         uint32_t num_data_dir_entries = m_data.GetU32(offset_ptr);
418         m_coff_header_opt.data_dirs.clear();
419         m_coff_header_opt.data_dirs.resize(num_data_dir_entries);
420         uint32_t i;
421         for (i = 0; i < num_data_dir_entries; i++) {
422           m_coff_header_opt.data_dirs[i].vmaddr = m_data.GetU32(offset_ptr);
423           m_coff_header_opt.data_dirs[i].vmsize = m_data.GetU32(offset_ptr);
424         }
425 
426         m_file_offset = m_coff_header_opt.image_base;
427         m_image_base = m_coff_header_opt.image_base;
428       }
429     }
430   }
431   // Make sure we are on track for section data which follows
432   *offset_ptr = end_offset;
433   return success;
434 }
435 
436 DataExtractor ObjectFilePECOFF::ReadImageData(uint32_t offset, size_t size) {
437   if (m_file) {
438     // A bit of a hack, but we intend to write to this buffer, so we can't
439     // mmap it.
440     auto buffer_sp =
441         DataBufferLLVM::CreateSliceFromPath(m_file.GetPath(), size, offset, true);
442     return DataExtractor(buffer_sp, GetByteOrder(), GetAddressByteSize());
443   }
444   ProcessSP process_sp(m_process_wp.lock());
445   DataExtractor data;
446   if (process_sp) {
447     auto data_ap = llvm::make_unique<DataBufferHeap>(size, 0);
448     Status readmem_error;
449     size_t bytes_read =
450         process_sp->ReadMemory(m_image_base + offset, data_ap->GetBytes(),
451                                data_ap->GetByteSize(), readmem_error);
452     if (bytes_read == size) {
453       DataBufferSP buffer_sp(data_ap.release());
454       data.SetData(buffer_sp, 0, buffer_sp->GetByteSize());
455     }
456   }
457   return data;
458 }
459 
460 //----------------------------------------------------------------------
461 // ParseSectionHeaders
462 //----------------------------------------------------------------------
463 bool ObjectFilePECOFF::ParseSectionHeaders(
464     uint32_t section_header_data_offset) {
465   const uint32_t nsects = m_coff_header.nsects;
466   m_sect_headers.clear();
467 
468   if (nsects > 0) {
469     const size_t section_header_byte_size = nsects * sizeof(section_header_t);
470     DataExtractor section_header_data =
471         ReadImageData(section_header_data_offset, section_header_byte_size);
472 
473     lldb::offset_t offset = 0;
474     if (section_header_data.ValidOffsetForDataOfSize(
475             offset, section_header_byte_size)) {
476       m_sect_headers.resize(nsects);
477 
478       for (uint32_t idx = 0; idx < nsects; ++idx) {
479         const void *name_data = section_header_data.GetData(&offset, 8);
480         if (name_data) {
481           memcpy(m_sect_headers[idx].name, name_data, 8);
482           m_sect_headers[idx].vmsize = section_header_data.GetU32(&offset);
483           m_sect_headers[idx].vmaddr = section_header_data.GetU32(&offset);
484           m_sect_headers[idx].size = section_header_data.GetU32(&offset);
485           m_sect_headers[idx].offset = section_header_data.GetU32(&offset);
486           m_sect_headers[idx].reloff = section_header_data.GetU32(&offset);
487           m_sect_headers[idx].lineoff = section_header_data.GetU32(&offset);
488           m_sect_headers[idx].nreloc = section_header_data.GetU16(&offset);
489           m_sect_headers[idx].nline = section_header_data.GetU16(&offset);
490           m_sect_headers[idx].flags = section_header_data.GetU32(&offset);
491         }
492       }
493     }
494   }
495 
496   return m_sect_headers.empty() == false;
497 }
498 
499 bool ObjectFilePECOFF::GetSectionName(std::string &sect_name,
500                                       const section_header_t &sect) {
501   if (sect.name[0] == '/') {
502     lldb::offset_t stroff = strtoul(&sect.name[1], NULL, 10);
503     lldb::offset_t string_file_offset =
504         m_coff_header.symoff + (m_coff_header.nsyms * 18) + stroff;
505     const char *name = m_data.GetCStr(&string_file_offset);
506     if (name) {
507       sect_name = name;
508       return true;
509     }
510 
511     return false;
512   }
513   sect_name = sect.name;
514   return true;
515 }
516 
517 //----------------------------------------------------------------------
518 // GetNListSymtab
519 //----------------------------------------------------------------------
520 Symtab *ObjectFilePECOFF::GetSymtab() {
521   ModuleSP module_sp(GetModule());
522   if (module_sp) {
523     std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
524     if (m_symtab_ap.get() == NULL) {
525       SectionList *sect_list = GetSectionList();
526       m_symtab_ap.reset(new Symtab(this));
527       std::lock_guard<std::recursive_mutex> guard(m_symtab_ap->GetMutex());
528 
529       const uint32_t num_syms = m_coff_header.nsyms;
530 
531       if (m_file && num_syms > 0 && m_coff_header.symoff > 0) {
532         const uint32_t symbol_size = 18;
533         const size_t symbol_data_size = num_syms * symbol_size;
534         // Include the 4-byte string table size at the end of the symbols
535         DataExtractor symtab_data =
536             ReadImageData(m_coff_header.symoff, symbol_data_size + 4);
537         lldb::offset_t offset = symbol_data_size;
538         const uint32_t strtab_size = symtab_data.GetU32(&offset);
539         if (strtab_size > 0) {
540           DataExtractor strtab_data = ReadImageData(
541               m_coff_header.symoff + symbol_data_size, strtab_size);
542 
543           // First 4 bytes should be zeroed after strtab_size has been read,
544           // because it is used as offset 0 to encode a NULL string.
545           uint32_t *strtab_data_start = const_cast<uint32_t *>(
546               reinterpret_cast<const uint32_t *>(strtab_data.GetDataStart()));
547           strtab_data_start[0] = 0;
548 
549           offset = 0;
550           std::string symbol_name;
551           Symbol *symbols = m_symtab_ap->Resize(num_syms);
552           for (uint32_t i = 0; i < num_syms; ++i) {
553             coff_symbol_t symbol;
554             const uint32_t symbol_offset = offset;
555             const char *symbol_name_cstr = NULL;
556             // If the first 4 bytes of the symbol string are zero, then they
557             // are followed by a 4-byte string table offset. Else these
558             // 8 bytes contain the symbol name
559             if (symtab_data.GetU32(&offset) == 0) {
560               // Long string that doesn't fit into the symbol table name,
561               // so now we must read the 4 byte string table offset
562               uint32_t strtab_offset = symtab_data.GetU32(&offset);
563               symbol_name_cstr = strtab_data.PeekCStr(strtab_offset);
564               symbol_name.assign(symbol_name_cstr);
565             } else {
566               // Short string that fits into the symbol table name which is 8
567               // bytes
568               offset += sizeof(symbol.name) - 4; // Skip remaining
569               symbol_name_cstr = symtab_data.PeekCStr(symbol_offset);
570               if (symbol_name_cstr == NULL)
571                 break;
572               symbol_name.assign(symbol_name_cstr, sizeof(symbol.name));
573             }
574             symbol.value = symtab_data.GetU32(&offset);
575             symbol.sect = symtab_data.GetU16(&offset);
576             symbol.type = symtab_data.GetU16(&offset);
577             symbol.storage = symtab_data.GetU8(&offset);
578             symbol.naux = symtab_data.GetU8(&offset);
579             symbols[i].GetMangled().SetValue(ConstString(symbol_name.c_str()));
580             if ((int16_t)symbol.sect >= 1) {
581               Address symbol_addr(sect_list->GetSectionAtIndex(symbol.sect - 1),
582                                   symbol.value);
583               symbols[i].GetAddressRef() = symbol_addr;
584               symbols[i].SetType(MapSymbolType(symbol.type));
585             }
586 
587             if (symbol.naux > 0) {
588               i += symbol.naux;
589               offset += symbol_size;
590             }
591           }
592         }
593       }
594 
595       // Read export header
596       if (coff_data_dir_export_table < m_coff_header_opt.data_dirs.size() &&
597           m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmsize > 0 &&
598           m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmaddr > 0) {
599         export_directory_entry export_table;
600         uint32_t data_start =
601             m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmaddr;
602 
603         uint32_t address_rva = data_start;
604         if (m_file) {
605           Address address(m_coff_header_opt.image_base + data_start, sect_list);
606           address_rva =
607               address.GetSection()->GetFileOffset() + address.GetOffset();
608         }
609         DataExtractor symtab_data =
610             ReadImageData(address_rva, m_coff_header_opt.data_dirs[0].vmsize);
611         lldb::offset_t offset = 0;
612 
613         // Read export_table header
614         export_table.characteristics = symtab_data.GetU32(&offset);
615         export_table.time_date_stamp = symtab_data.GetU32(&offset);
616         export_table.major_version = symtab_data.GetU16(&offset);
617         export_table.minor_version = symtab_data.GetU16(&offset);
618         export_table.name = symtab_data.GetU32(&offset);
619         export_table.base = symtab_data.GetU32(&offset);
620         export_table.number_of_functions = symtab_data.GetU32(&offset);
621         export_table.number_of_names = symtab_data.GetU32(&offset);
622         export_table.address_of_functions = symtab_data.GetU32(&offset);
623         export_table.address_of_names = symtab_data.GetU32(&offset);
624         export_table.address_of_name_ordinals = symtab_data.GetU32(&offset);
625 
626         bool has_ordinal = export_table.address_of_name_ordinals != 0;
627 
628         lldb::offset_t name_offset = export_table.address_of_names - data_start;
629         lldb::offset_t name_ordinal_offset =
630             export_table.address_of_name_ordinals - data_start;
631 
632         Symbol *symbols = m_symtab_ap->Resize(export_table.number_of_names);
633 
634         std::string symbol_name;
635 
636         // Read each export table entry
637         for (size_t i = 0; i < export_table.number_of_names; ++i) {
638           uint32_t name_ordinal =
639               has_ordinal ? symtab_data.GetU16(&name_ordinal_offset) : i;
640           uint32_t name_address = symtab_data.GetU32(&name_offset);
641 
642           const char *symbol_name_cstr =
643               symtab_data.PeekCStr(name_address - data_start);
644           symbol_name.assign(symbol_name_cstr);
645 
646           lldb::offset_t function_offset = export_table.address_of_functions -
647                                            data_start +
648                                            sizeof(uint32_t) * name_ordinal;
649           uint32_t function_rva = symtab_data.GetU32(&function_offset);
650 
651           Address symbol_addr(m_coff_header_opt.image_base + function_rva,
652                               sect_list);
653           symbols[i].GetMangled().SetValue(ConstString(symbol_name.c_str()));
654           symbols[i].GetAddressRef() = symbol_addr;
655           symbols[i].SetType(lldb::eSymbolTypeCode);
656           symbols[i].SetDebug(true);
657         }
658       }
659       m_symtab_ap->CalculateSymbolSizes();
660     }
661   }
662   return m_symtab_ap.get();
663 }
664 
665 bool ObjectFilePECOFF::IsStripped() {
666   // TODO: determine this for COFF
667   return false;
668 }
669 
670 void ObjectFilePECOFF::CreateSections(SectionList &unified_section_list) {
671   if (!m_sections_ap.get()) {
672     m_sections_ap.reset(new SectionList());
673 
674     ModuleSP module_sp(GetModule());
675     if (module_sp) {
676       std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
677       const uint32_t nsects = m_sect_headers.size();
678       ModuleSP module_sp(GetModule());
679       for (uint32_t idx = 0; idx < nsects; ++idx) {
680         std::string sect_name;
681         GetSectionName(sect_name, m_sect_headers[idx]);
682         ConstString const_sect_name(sect_name.c_str());
683         static ConstString g_code_sect_name(".code");
684         static ConstString g_CODE_sect_name("CODE");
685         static ConstString g_data_sect_name(".data");
686         static ConstString g_DATA_sect_name("DATA");
687         static ConstString g_bss_sect_name(".bss");
688         static ConstString g_BSS_sect_name("BSS");
689         static ConstString g_debug_sect_name(".debug");
690         static ConstString g_reloc_sect_name(".reloc");
691         static ConstString g_stab_sect_name(".stab");
692         static ConstString g_stabstr_sect_name(".stabstr");
693         static ConstString g_sect_name_dwarf_debug_abbrev(".debug_abbrev");
694         static ConstString g_sect_name_dwarf_debug_aranges(".debug_aranges");
695         static ConstString g_sect_name_dwarf_debug_frame(".debug_frame");
696         static ConstString g_sect_name_dwarf_debug_info(".debug_info");
697         static ConstString g_sect_name_dwarf_debug_line(".debug_line");
698         static ConstString g_sect_name_dwarf_debug_loc(".debug_loc");
699         static ConstString g_sect_name_dwarf_debug_macinfo(".debug_macinfo");
700         static ConstString g_sect_name_dwarf_debug_pubnames(".debug_pubnames");
701         static ConstString g_sect_name_dwarf_debug_pubtypes(".debug_pubtypes");
702         static ConstString g_sect_name_dwarf_debug_ranges(".debug_ranges");
703         static ConstString g_sect_name_dwarf_debug_str(".debug_str");
704         static ConstString g_sect_name_eh_frame(".eh_frame");
705         static ConstString g_sect_name_go_symtab(".gosymtab");
706         SectionType section_type = eSectionTypeOther;
707         if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_CODE &&
708             ((const_sect_name == g_code_sect_name) ||
709              (const_sect_name == g_CODE_sect_name))) {
710           section_type = eSectionTypeCode;
711         } else if (m_sect_headers[idx].flags &
712                        llvm::COFF::IMAGE_SCN_CNT_INITIALIZED_DATA &&
713                    ((const_sect_name == g_data_sect_name) ||
714                     (const_sect_name == g_DATA_sect_name))) {
715           section_type = eSectionTypeData;
716         } else if (m_sect_headers[idx].flags &
717                        llvm::COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA &&
718                    ((const_sect_name == g_bss_sect_name) ||
719                     (const_sect_name == g_BSS_sect_name))) {
720           if (m_sect_headers[idx].size == 0)
721             section_type = eSectionTypeZeroFill;
722           else
723             section_type = eSectionTypeData;
724         } else if (const_sect_name == g_debug_sect_name) {
725           section_type = eSectionTypeDebug;
726         } else if (const_sect_name == g_stabstr_sect_name) {
727           section_type = eSectionTypeDataCString;
728         } else if (const_sect_name == g_reloc_sect_name) {
729           section_type = eSectionTypeOther;
730         } else if (const_sect_name == g_sect_name_dwarf_debug_abbrev)
731           section_type = eSectionTypeDWARFDebugAbbrev;
732         else if (const_sect_name == g_sect_name_dwarf_debug_aranges)
733           section_type = eSectionTypeDWARFDebugAranges;
734         else if (const_sect_name == g_sect_name_dwarf_debug_frame)
735           section_type = eSectionTypeDWARFDebugFrame;
736         else if (const_sect_name == g_sect_name_dwarf_debug_info)
737           section_type = eSectionTypeDWARFDebugInfo;
738         else if (const_sect_name == g_sect_name_dwarf_debug_line)
739           section_type = eSectionTypeDWARFDebugLine;
740         else if (const_sect_name == g_sect_name_dwarf_debug_loc)
741           section_type = eSectionTypeDWARFDebugLoc;
742         else if (const_sect_name == g_sect_name_dwarf_debug_macinfo)
743           section_type = eSectionTypeDWARFDebugMacInfo;
744         else if (const_sect_name == g_sect_name_dwarf_debug_pubnames)
745           section_type = eSectionTypeDWARFDebugPubNames;
746         else if (const_sect_name == g_sect_name_dwarf_debug_pubtypes)
747           section_type = eSectionTypeDWARFDebugPubTypes;
748         else if (const_sect_name == g_sect_name_dwarf_debug_ranges)
749           section_type = eSectionTypeDWARFDebugRanges;
750         else if (const_sect_name == g_sect_name_dwarf_debug_str)
751           section_type = eSectionTypeDWARFDebugStr;
752         else if (const_sect_name == g_sect_name_eh_frame)
753           section_type = eSectionTypeEHFrame;
754         else if (const_sect_name == g_sect_name_go_symtab)
755           section_type = eSectionTypeGoSymtab;
756         else if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_CODE) {
757           section_type = eSectionTypeCode;
758         } else if (m_sect_headers[idx].flags &
759                    llvm::COFF::IMAGE_SCN_CNT_INITIALIZED_DATA) {
760           section_type = eSectionTypeData;
761         } else if (m_sect_headers[idx].flags &
762                    llvm::COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) {
763           if (m_sect_headers[idx].size == 0)
764             section_type = eSectionTypeZeroFill;
765           else
766             section_type = eSectionTypeData;
767         }
768 
769         // Use a segment ID of the segment index shifted left by 8 so they
770         // never conflict with any of the sections.
771         SectionSP section_sp(new Section(
772             module_sp, // Module to which this section belongs
773             this,      // Object file to which this section belongs
774             idx + 1, // Section ID is the 1 based segment index shifted right by
775                      // 8 bits as not to collide with any of the 256 section IDs
776                      // that are possible
777             const_sect_name, // Name of this section
778             section_type,    // This section is a container of other sections.
779             m_coff_header_opt.image_base +
780                 m_sect_headers[idx].vmaddr, // File VM address == addresses as
781                                             // they are found in the object file
782             m_sect_headers[idx].vmsize,     // VM size in bytes of this section
783             m_sect_headers[idx]
784                 .offset, // Offset to the data for this section in the file
785             m_sect_headers[idx]
786                 .size, // Size in bytes of this section as found in the file
787             m_coff_header_opt.sect_alignment, // Section alignment
788             m_sect_headers[idx].flags));      // Flags for this section
789 
790         // section_sp->SetIsEncrypted (segment_is_encrypted);
791 
792         unified_section_list.AddSection(section_sp);
793         m_sections_ap->AddSection(section_sp);
794       }
795     }
796   }
797 }
798 
799 bool ObjectFilePECOFF::GetUUID(UUID *uuid) { return false; }
800 
801 uint32_t ObjectFilePECOFF::GetDependentModules(FileSpecList &files) {
802   return 0;
803 }
804 
805 lldb_private::Address ObjectFilePECOFF::GetEntryPointAddress() {
806   if (m_entry_point_address.IsValid())
807     return m_entry_point_address;
808 
809   if (!ParseHeader() || !IsExecutable())
810     return m_entry_point_address;
811 
812   SectionList *section_list = GetSectionList();
813   addr_t offset = m_coff_header_opt.entry;
814 
815   if (!section_list)
816     m_entry_point_address.SetOffset(offset);
817   else
818     m_entry_point_address.ResolveAddressUsingFileSections(offset, section_list);
819   return m_entry_point_address;
820 }
821 
822 //----------------------------------------------------------------------
823 // Dump
824 //
825 // Dump the specifics of the runtime file container (such as any headers
826 // segments, sections, etc).
827 //----------------------------------------------------------------------
828 void ObjectFilePECOFF::Dump(Stream *s) {
829   ModuleSP module_sp(GetModule());
830   if (module_sp) {
831     std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
832     s->Printf("%p: ", static_cast<void *>(this));
833     s->Indent();
834     s->PutCString("ObjectFilePECOFF");
835 
836     ArchSpec header_arch;
837     GetArchitecture(header_arch);
838 
839     *s << ", file = '" << m_file
840        << "', arch = " << header_arch.GetArchitectureName() << "\n";
841 
842     SectionList *sections = GetSectionList();
843     if (sections)
844       sections->Dump(s, NULL, true, UINT32_MAX);
845 
846     if (m_symtab_ap.get())
847       m_symtab_ap->Dump(s, NULL, eSortOrderNone);
848 
849     if (m_dos_header.e_magic)
850       DumpDOSHeader(s, m_dos_header);
851     if (m_coff_header.machine) {
852       DumpCOFFHeader(s, m_coff_header);
853       if (m_coff_header.hdrsize)
854         DumpOptCOFFHeader(s, m_coff_header_opt);
855     }
856     s->EOL();
857     DumpSectionHeaders(s);
858     s->EOL();
859   }
860 }
861 
862 //----------------------------------------------------------------------
863 // DumpDOSHeader
864 //
865 // Dump the MS-DOS header to the specified output stream
866 //----------------------------------------------------------------------
867 void ObjectFilePECOFF::DumpDOSHeader(Stream *s, const dos_header_t &header) {
868   s->PutCString("MSDOS Header\n");
869   s->Printf("  e_magic    = 0x%4.4x\n", header.e_magic);
870   s->Printf("  e_cblp     = 0x%4.4x\n", header.e_cblp);
871   s->Printf("  e_cp       = 0x%4.4x\n", header.e_cp);
872   s->Printf("  e_crlc     = 0x%4.4x\n", header.e_crlc);
873   s->Printf("  e_cparhdr  = 0x%4.4x\n", header.e_cparhdr);
874   s->Printf("  e_minalloc = 0x%4.4x\n", header.e_minalloc);
875   s->Printf("  e_maxalloc = 0x%4.4x\n", header.e_maxalloc);
876   s->Printf("  e_ss       = 0x%4.4x\n", header.e_ss);
877   s->Printf("  e_sp       = 0x%4.4x\n", header.e_sp);
878   s->Printf("  e_csum     = 0x%4.4x\n", header.e_csum);
879   s->Printf("  e_ip       = 0x%4.4x\n", header.e_ip);
880   s->Printf("  e_cs       = 0x%4.4x\n", header.e_cs);
881   s->Printf("  e_lfarlc   = 0x%4.4x\n", header.e_lfarlc);
882   s->Printf("  e_ovno     = 0x%4.4x\n", header.e_ovno);
883   s->Printf("  e_res[4]   = { 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x }\n",
884             header.e_res[0], header.e_res[1], header.e_res[2], header.e_res[3]);
885   s->Printf("  e_oemid    = 0x%4.4x\n", header.e_oemid);
886   s->Printf("  e_oeminfo  = 0x%4.4x\n", header.e_oeminfo);
887   s->Printf("  e_res2[10] = { 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, "
888             "0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x }\n",
889             header.e_res2[0], header.e_res2[1], header.e_res2[2],
890             header.e_res2[3], header.e_res2[4], header.e_res2[5],
891             header.e_res2[6], header.e_res2[7], header.e_res2[8],
892             header.e_res2[9]);
893   s->Printf("  e_lfanew   = 0x%8.8x\n", header.e_lfanew);
894 }
895 
896 //----------------------------------------------------------------------
897 // DumpCOFFHeader
898 //
899 // Dump the COFF header to the specified output stream
900 //----------------------------------------------------------------------
901 void ObjectFilePECOFF::DumpCOFFHeader(Stream *s, const coff_header_t &header) {
902   s->PutCString("COFF Header\n");
903   s->Printf("  machine = 0x%4.4x\n", header.machine);
904   s->Printf("  nsects  = 0x%4.4x\n", header.nsects);
905   s->Printf("  modtime = 0x%8.8x\n", header.modtime);
906   s->Printf("  symoff  = 0x%8.8x\n", header.symoff);
907   s->Printf("  nsyms   = 0x%8.8x\n", header.nsyms);
908   s->Printf("  hdrsize = 0x%4.4x\n", header.hdrsize);
909 }
910 
911 //----------------------------------------------------------------------
912 // DumpOptCOFFHeader
913 //
914 // Dump the optional COFF header to the specified output stream
915 //----------------------------------------------------------------------
916 void ObjectFilePECOFF::DumpOptCOFFHeader(Stream *s,
917                                          const coff_opt_header_t &header) {
918   s->PutCString("Optional COFF Header\n");
919   s->Printf("  magic                   = 0x%4.4x\n", header.magic);
920   s->Printf("  major_linker_version    = 0x%2.2x\n",
921             header.major_linker_version);
922   s->Printf("  minor_linker_version    = 0x%2.2x\n",
923             header.minor_linker_version);
924   s->Printf("  code_size               = 0x%8.8x\n", header.code_size);
925   s->Printf("  data_size               = 0x%8.8x\n", header.data_size);
926   s->Printf("  bss_size                = 0x%8.8x\n", header.bss_size);
927   s->Printf("  entry                   = 0x%8.8x\n", header.entry);
928   s->Printf("  code_offset             = 0x%8.8x\n", header.code_offset);
929   s->Printf("  data_offset             = 0x%8.8x\n", header.data_offset);
930   s->Printf("  image_base              = 0x%16.16" PRIx64 "\n",
931             header.image_base);
932   s->Printf("  sect_alignment          = 0x%8.8x\n", header.sect_alignment);
933   s->Printf("  file_alignment          = 0x%8.8x\n", header.file_alignment);
934   s->Printf("  major_os_system_version = 0x%4.4x\n",
935             header.major_os_system_version);
936   s->Printf("  minor_os_system_version = 0x%4.4x\n",
937             header.minor_os_system_version);
938   s->Printf("  major_image_version     = 0x%4.4x\n",
939             header.major_image_version);
940   s->Printf("  minor_image_version     = 0x%4.4x\n",
941             header.minor_image_version);
942   s->Printf("  major_subsystem_version = 0x%4.4x\n",
943             header.major_subsystem_version);
944   s->Printf("  minor_subsystem_version = 0x%4.4x\n",
945             header.minor_subsystem_version);
946   s->Printf("  reserved1               = 0x%8.8x\n", header.reserved1);
947   s->Printf("  image_size              = 0x%8.8x\n", header.image_size);
948   s->Printf("  header_size             = 0x%8.8x\n", header.header_size);
949   s->Printf("  checksum                = 0x%8.8x\n", header.checksum);
950   s->Printf("  subsystem               = 0x%4.4x\n", header.subsystem);
951   s->Printf("  dll_flags               = 0x%4.4x\n", header.dll_flags);
952   s->Printf("  stack_reserve_size      = 0x%16.16" PRIx64 "\n",
953             header.stack_reserve_size);
954   s->Printf("  stack_commit_size       = 0x%16.16" PRIx64 "\n",
955             header.stack_commit_size);
956   s->Printf("  heap_reserve_size       = 0x%16.16" PRIx64 "\n",
957             header.heap_reserve_size);
958   s->Printf("  heap_commit_size        = 0x%16.16" PRIx64 "\n",
959             header.heap_commit_size);
960   s->Printf("  loader_flags            = 0x%8.8x\n", header.loader_flags);
961   s->Printf("  num_data_dir_entries    = 0x%8.8x\n",
962             (uint32_t)header.data_dirs.size());
963   uint32_t i;
964   for (i = 0; i < header.data_dirs.size(); i++) {
965     s->Printf("  data_dirs[%2u] vmaddr = 0x%8.8x, vmsize = 0x%8.8x\n", i,
966               header.data_dirs[i].vmaddr, header.data_dirs[i].vmsize);
967   }
968 }
969 //----------------------------------------------------------------------
970 // DumpSectionHeader
971 //
972 // Dump a single ELF section header to the specified output stream
973 //----------------------------------------------------------------------
974 void ObjectFilePECOFF::DumpSectionHeader(Stream *s,
975                                          const section_header_t &sh) {
976   std::string name;
977   GetSectionName(name, sh);
978   s->Printf("%-16s 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x 0x%4.4x "
979             "0x%4.4x 0x%8.8x\n",
980             name.c_str(), sh.vmaddr, sh.vmsize, sh.offset, sh.size, sh.reloff,
981             sh.lineoff, sh.nreloc, sh.nline, sh.flags);
982 }
983 
984 //----------------------------------------------------------------------
985 // DumpSectionHeaders
986 //
987 // Dump all of the ELF section header to the specified output stream
988 //----------------------------------------------------------------------
989 void ObjectFilePECOFF::DumpSectionHeaders(Stream *s) {
990 
991   s->PutCString("Section Headers\n");
992   s->PutCString("IDX  name             vm addr    vm size    file off   file "
993                 "size  reloc off  line off   nreloc nline  flags\n");
994   s->PutCString("==== ---------------- ---------- ---------- ---------- "
995                 "---------- ---------- ---------- ------ ------ ----------\n");
996 
997   uint32_t idx = 0;
998   SectionHeaderCollIter pos, end = m_sect_headers.end();
999 
1000   for (pos = m_sect_headers.begin(); pos != end; ++pos, ++idx) {
1001     s->Printf("[%2u] ", idx);
1002     ObjectFilePECOFF::DumpSectionHeader(s, *pos);
1003   }
1004 }
1005 
1006 bool ObjectFilePECOFF::IsWindowsSubsystem() {
1007   switch (m_coff_header_opt.subsystem) {
1008   case llvm::COFF::IMAGE_SUBSYSTEM_NATIVE:
1009   case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_GUI:
1010   case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_CUI:
1011   case llvm::COFF::IMAGE_SUBSYSTEM_NATIVE_WINDOWS:
1012   case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_CE_GUI:
1013   case llvm::COFF::IMAGE_SUBSYSTEM_XBOX:
1014   case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_BOOT_APPLICATION:
1015     return true;
1016   default:
1017     return false;
1018   }
1019 }
1020 
1021 bool ObjectFilePECOFF::GetArchitecture(ArchSpec &arch) {
1022   uint16_t machine = m_coff_header.machine;
1023   switch (machine) {
1024   case llvm::COFF::IMAGE_FILE_MACHINE_AMD64:
1025   case llvm::COFF::IMAGE_FILE_MACHINE_I386:
1026   case llvm::COFF::IMAGE_FILE_MACHINE_POWERPC:
1027   case llvm::COFF::IMAGE_FILE_MACHINE_POWERPCFP:
1028   case llvm::COFF::IMAGE_FILE_MACHINE_ARM:
1029   case llvm::COFF::IMAGE_FILE_MACHINE_ARMNT:
1030   case llvm::COFF::IMAGE_FILE_MACHINE_THUMB:
1031     arch.SetArchitecture(eArchTypeCOFF, machine, LLDB_INVALID_CPUTYPE,
1032                          IsWindowsSubsystem() ? llvm::Triple::Win32
1033                                               : llvm::Triple::UnknownOS);
1034     return true;
1035   default:
1036     break;
1037   }
1038   return false;
1039 }
1040 
1041 ObjectFile::Type ObjectFilePECOFF::CalculateType() {
1042   if (m_coff_header.machine != 0) {
1043     if ((m_coff_header.flags & llvm::COFF::IMAGE_FILE_DLL) == 0)
1044       return eTypeExecutable;
1045     else
1046       return eTypeSharedLibrary;
1047   }
1048   return eTypeExecutable;
1049 }
1050 
1051 ObjectFile::Strata ObjectFilePECOFF::CalculateStrata() { return eStrataUser; }
1052 //------------------------------------------------------------------
1053 // PluginInterface protocol
1054 //------------------------------------------------------------------
1055 ConstString ObjectFilePECOFF::GetPluginName() { return GetPluginNameStatic(); }
1056 
1057 uint32_t ObjectFilePECOFF::GetPluginVersion() { return 1; }
1058