1f754f88fSGreg Clayton //===-- ObjectFilePECOFF.cpp ------------------------------------*- C++ -*-===//
2f754f88fSGreg Clayton //
3f754f88fSGreg Clayton //                     The LLVM Compiler Infrastructure
4f754f88fSGreg Clayton //
5f754f88fSGreg Clayton // This file is distributed under the University of Illinois Open Source
6f754f88fSGreg Clayton // License. See LICENSE.TXT for details.
7f754f88fSGreg Clayton //
8f754f88fSGreg Clayton //===----------------------------------------------------------------------===//
9f754f88fSGreg Clayton 
10f754f88fSGreg Clayton #include "ObjectFilePECOFF.h"
11f7d1893fSAdrian McCarthy #include "WindowsMiniDump.h"
12f754f88fSGreg Clayton 
13237ad974SCharles Davis #include "llvm/Support/COFF.h"
14f754f88fSGreg Clayton 
15f754f88fSGreg Clayton #include "lldb/Core/ArchSpec.h"
16f754f88fSGreg Clayton #include "lldb/Core/DataBuffer.h"
17f754f88fSGreg Clayton #include "lldb/Core/FileSpecList.h"
18f754f88fSGreg Clayton #include "lldb/Core/Module.h"
19f4d6de6aSGreg Clayton #include "lldb/Core/ModuleSpec.h"
20f754f88fSGreg Clayton #include "lldb/Core/PluginManager.h"
21f754f88fSGreg Clayton #include "lldb/Core/Section.h"
22f754f88fSGreg Clayton #include "lldb/Core/StreamFile.h"
23f754f88fSGreg Clayton #include "lldb/Core/StreamString.h"
24f754f88fSGreg Clayton #include "lldb/Core/Timer.h"
25f754f88fSGreg Clayton #include "lldb/Core/UUID.h"
26b9c1b51eSKate Stone #include "lldb/Host/FileSpec.h"
27f754f88fSGreg Clayton #include "lldb/Symbol/ObjectFile.h"
28f7d1893fSAdrian McCarthy #include "lldb/Target/Process.h"
292756adf3SVirgile Bello #include "lldb/Target/SectionLoadList.h"
302756adf3SVirgile Bello #include "lldb/Target/Target.h"
31f754f88fSGreg Clayton 
32f754f88fSGreg Clayton #define IMAGE_DOS_SIGNATURE 0x5A4D    // MZ
33f754f88fSGreg Clayton #define IMAGE_NT_SIGNATURE 0x00004550 // PE00
34f754f88fSGreg Clayton #define OPT_HEADER_MAGIC_PE32 0x010b
35f754f88fSGreg Clayton #define OPT_HEADER_MAGIC_PE32_PLUS 0x020b
36f754f88fSGreg Clayton 
37f754f88fSGreg Clayton using namespace lldb;
38f754f88fSGreg Clayton using namespace lldb_private;
39f754f88fSGreg Clayton 
40b9c1b51eSKate Stone void ObjectFilePECOFF::Initialize() {
41b9c1b51eSKate Stone   PluginManager::RegisterPlugin(
42b9c1b51eSKate Stone       GetPluginNameStatic(), GetPluginDescriptionStatic(), CreateInstance,
43b9c1b51eSKate Stone       CreateMemoryInstance, GetModuleSpecifications, SaveCore);
44f754f88fSGreg Clayton }
45f754f88fSGreg Clayton 
46b9c1b51eSKate Stone void ObjectFilePECOFF::Terminate() {
47f754f88fSGreg Clayton   PluginManager::UnregisterPlugin(CreateInstance);
48f754f88fSGreg Clayton }
49f754f88fSGreg Clayton 
50b9c1b51eSKate Stone lldb_private::ConstString ObjectFilePECOFF::GetPluginNameStatic() {
5157abc5d6SGreg Clayton   static ConstString g_name("pe-coff");
5257abc5d6SGreg Clayton   return g_name;
53f754f88fSGreg Clayton }
54f754f88fSGreg Clayton 
55b9c1b51eSKate Stone const char *ObjectFilePECOFF::GetPluginDescriptionStatic() {
56b9c1b51eSKate Stone   return "Portable Executable and Common Object File Format object file reader "
57b9c1b51eSKate Stone          "(32 and 64 bit)";
58f754f88fSGreg Clayton }
59f754f88fSGreg Clayton 
60b9c1b51eSKate Stone ObjectFile *ObjectFilePECOFF::CreateInstance(const lldb::ModuleSP &module_sp,
615ce9c565SGreg Clayton                                              DataBufferSP &data_sp,
625ce9c565SGreg Clayton                                              lldb::offset_t data_offset,
635ce9c565SGreg Clayton                                              const lldb_private::FileSpec *file,
645ce9c565SGreg Clayton                                              lldb::offset_t file_offset,
65b9c1b51eSKate Stone                                              lldb::offset_t length) {
66b9c1b51eSKate Stone   if (!data_sp) {
67736888c8SGreg Clayton     data_sp = file->MemoryMapFileContentsIfLocal(file_offset, length);
685ce9c565SGreg Clayton     data_offset = 0;
695ce9c565SGreg Clayton   }
705ce9c565SGreg Clayton 
71b9c1b51eSKate Stone   if (ObjectFilePECOFF::MagicBytesMatch(data_sp)) {
725ce9c565SGreg Clayton     // Update the data to contain the entire file if it doesn't already
735ce9c565SGreg Clayton     if (data_sp->GetByteSize() < length)
74736888c8SGreg Clayton       data_sp = file->MemoryMapFileContentsIfLocal(file_offset, length);
75b9c1b51eSKate Stone     std::unique_ptr<ObjectFile> objfile_ap(new ObjectFilePECOFF(
76b9c1b51eSKate Stone         module_sp, data_sp, data_offset, file, file_offset, length));
77f754f88fSGreg Clayton     if (objfile_ap.get() && objfile_ap->ParseHeader())
78f754f88fSGreg Clayton       return objfile_ap.release();
79f754f88fSGreg Clayton   }
80f754f88fSGreg Clayton   return NULL;
81f754f88fSGreg Clayton }
82f754f88fSGreg Clayton 
83b9c1b51eSKate Stone ObjectFile *ObjectFilePECOFF::CreateMemoryInstance(
84b9c1b51eSKate Stone     const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp,
85b9c1b51eSKate Stone     const lldb::ProcessSP &process_sp, lldb::addr_t header_addr) {
86c9660546SGreg Clayton   return NULL;
87c9660546SGreg Clayton }
88c9660546SGreg Clayton 
89b9c1b51eSKate Stone size_t ObjectFilePECOFF::GetModuleSpecifications(
90b9c1b51eSKate Stone     const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp,
91b9c1b51eSKate Stone     lldb::offset_t data_offset, lldb::offset_t file_offset,
92b9c1b51eSKate Stone     lldb::offset_t length, lldb_private::ModuleSpecList &specs) {
9389eb1baeSVirgile Bello   const size_t initial_count = specs.GetSize();
9489eb1baeSVirgile Bello 
95b9c1b51eSKate Stone   if (ObjectFilePECOFF::MagicBytesMatch(data_sp)) {
9689eb1baeSVirgile Bello     DataExtractor data;
9789eb1baeSVirgile Bello     data.SetData(data_sp, data_offset, length);
9889eb1baeSVirgile Bello     data.SetByteOrder(eByteOrderLittle);
9989eb1baeSVirgile Bello 
10089eb1baeSVirgile Bello     dos_header_t dos_header;
10189eb1baeSVirgile Bello     coff_header_t coff_header;
10289eb1baeSVirgile Bello 
103b9c1b51eSKate Stone     if (ParseDOSHeader(data, dos_header)) {
10489eb1baeSVirgile Bello       lldb::offset_t offset = dos_header.e_lfanew;
10589eb1baeSVirgile Bello       uint32_t pe_signature = data.GetU32(&offset);
10689eb1baeSVirgile Bello       if (pe_signature != IMAGE_NT_SIGNATURE)
10789eb1baeSVirgile Bello         return false;
108b9c1b51eSKate Stone       if (ParseCOFFHeader(data, &offset, coff_header)) {
10989eb1baeSVirgile Bello         ArchSpec spec;
110b9c1b51eSKate Stone         if (coff_header.machine == MachineAmd64) {
111ad587ae4SZachary Turner           spec.SetTriple("x86_64-pc-windows");
1125e6f4520SZachary Turner           specs.Append(ModuleSpec(file, spec));
113b9c1b51eSKate Stone         } else if (coff_header.machine == MachineX86) {
114ad587ae4SZachary Turner           spec.SetTriple("i386-pc-windows");
11589eb1baeSVirgile Bello           specs.Append(ModuleSpec(file, spec));
1165e6f4520SZachary Turner           spec.SetTriple("i686-pc-windows");
1175e6f4520SZachary Turner           specs.Append(ModuleSpec(file, spec));
1185e6f4520SZachary Turner         }
11989eb1baeSVirgile Bello       }
12089eb1baeSVirgile Bello     }
12189eb1baeSVirgile Bello   }
12289eb1baeSVirgile Bello 
12389eb1baeSVirgile Bello   return specs.GetSize() - initial_count;
124f4d6de6aSGreg Clayton }
125f4d6de6aSGreg Clayton 
126b9c1b51eSKate Stone bool ObjectFilePECOFF::SaveCore(const lldb::ProcessSP &process_sp,
127f7d1893fSAdrian McCarthy                                 const lldb_private::FileSpec &outfile,
128b9c1b51eSKate Stone                                 lldb_private::Error &error) {
129f7d1893fSAdrian McCarthy   return SaveMiniDump(process_sp, outfile, error);
130f7d1893fSAdrian McCarthy }
131f7d1893fSAdrian McCarthy 
132b9c1b51eSKate Stone bool ObjectFilePECOFF::MagicBytesMatch(DataBufferSP &data_sp) {
1335ce9c565SGreg Clayton   DataExtractor data(data_sp, eByteOrderLittle, 4);
134c7bece56SGreg Clayton   lldb::offset_t offset = 0;
135f754f88fSGreg Clayton   uint16_t magic = data.GetU16(&offset);
136f754f88fSGreg Clayton   return magic == IMAGE_DOS_SIGNATURE;
137f754f88fSGreg Clayton }
138f754f88fSGreg Clayton 
139b9c1b51eSKate Stone lldb::SymbolType ObjectFilePECOFF::MapSymbolType(uint16_t coff_symbol_type) {
140c35b91ceSAdrian McCarthy   // TODO:  We need to complete this mapping of COFF symbol types to LLDB ones.
141c35b91ceSAdrian McCarthy   // For now, here's a hack to make sure our function have types.
142b9c1b51eSKate Stone   const auto complex_type =
143b9c1b51eSKate Stone       coff_symbol_type >> llvm::COFF::SCT_COMPLEX_TYPE_SHIFT;
144b9c1b51eSKate Stone   if (complex_type == llvm::COFF::IMAGE_SYM_DTYPE_FUNCTION) {
145c35b91ceSAdrian McCarthy     return lldb::eSymbolTypeCode;
146c35b91ceSAdrian McCarthy   }
147c35b91ceSAdrian McCarthy   return lldb::eSymbolTypeInvalid;
148c35b91ceSAdrian McCarthy }
149f754f88fSGreg Clayton 
150e72dfb32SGreg Clayton ObjectFilePECOFF::ObjectFilePECOFF(const lldb::ModuleSP &module_sp,
1515ce9c565SGreg Clayton                                    DataBufferSP &data_sp,
1525ce9c565SGreg Clayton                                    lldb::offset_t data_offset,
153f754f88fSGreg Clayton                                    const FileSpec *file,
1545ce9c565SGreg Clayton                                    lldb::offset_t file_offset,
155b9c1b51eSKate Stone                                    lldb::offset_t length)
156b9c1b51eSKate Stone     : ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset),
157b9c1b51eSKate Stone       m_dos_header(), m_coff_header(), m_coff_header_opt(), m_sect_headers(),
158b9c1b51eSKate Stone       m_entry_point_address() {
159f754f88fSGreg Clayton   ::memset(&m_dos_header, 0, sizeof(m_dos_header));
160f754f88fSGreg Clayton   ::memset(&m_coff_header, 0, sizeof(m_coff_header));
161f754f88fSGreg Clayton   ::memset(&m_coff_header_opt, 0, sizeof(m_coff_header_opt));
162f754f88fSGreg Clayton }
163f754f88fSGreg Clayton 
164b9c1b51eSKate Stone ObjectFilePECOFF::~ObjectFilePECOFF() {}
165f754f88fSGreg Clayton 
166b9c1b51eSKate Stone bool ObjectFilePECOFF::ParseHeader() {
167a1743499SGreg Clayton   ModuleSP module_sp(GetModule());
168b9c1b51eSKate Stone   if (module_sp) {
16916ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
170f754f88fSGreg Clayton     m_sect_headers.clear();
171f754f88fSGreg Clayton     m_data.SetByteOrder(eByteOrderLittle);
172c7bece56SGreg Clayton     lldb::offset_t offset = 0;
173f754f88fSGreg Clayton 
174b9c1b51eSKate Stone     if (ParseDOSHeader(m_data, m_dos_header)) {
175f754f88fSGreg Clayton       offset = m_dos_header.e_lfanew;
176f754f88fSGreg Clayton       uint32_t pe_signature = m_data.GetU32(&offset);
177f754f88fSGreg Clayton       if (pe_signature != IMAGE_NT_SIGNATURE)
178f754f88fSGreg Clayton         return false;
179b9c1b51eSKate Stone       if (ParseCOFFHeader(m_data, &offset, m_coff_header)) {
180f754f88fSGreg Clayton         if (m_coff_header.hdrsize > 0)
181f754f88fSGreg Clayton           ParseCOFFOptionalHeader(&offset);
182f754f88fSGreg Clayton         ParseSectionHeaders(offset);
18328469ca3SGreg Clayton       }
184f754f88fSGreg Clayton       return true;
185f754f88fSGreg Clayton     }
186a1743499SGreg Clayton   }
187f754f88fSGreg Clayton   return false;
188f754f88fSGreg Clayton }
189f754f88fSGreg Clayton 
190b9c1b51eSKate Stone bool ObjectFilePECOFF::SetLoadAddress(Target &target, addr_t value,
191b9c1b51eSKate Stone                                       bool value_is_offset) {
1922756adf3SVirgile Bello   bool changed = false;
1932756adf3SVirgile Bello   ModuleSP module_sp = GetModule();
194b9c1b51eSKate Stone   if (module_sp) {
1952756adf3SVirgile Bello     size_t num_loaded_sections = 0;
1962756adf3SVirgile Bello     SectionList *section_list = GetSectionList();
197b9c1b51eSKate Stone     if (section_list) {
198b9c1b51eSKate Stone       if (!value_is_offset) {
1992756adf3SVirgile Bello         value -= m_image_base;
2002756adf3SVirgile Bello       }
2012756adf3SVirgile Bello 
2022756adf3SVirgile Bello       const size_t num_sections = section_list->GetSize();
2032756adf3SVirgile Bello       size_t sect_idx = 0;
2042756adf3SVirgile Bello 
205b9c1b51eSKate Stone       for (sect_idx = 0; sect_idx < num_sections; ++sect_idx) {
2062756adf3SVirgile Bello         // Iterate through the object file sections to find all
2072756adf3SVirgile Bello         // of the sections that have SHF_ALLOC in their flag bits.
2082756adf3SVirgile Bello         SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx));
209b9c1b51eSKate Stone         if (section_sp && !section_sp->IsThreadSpecific()) {
210b9c1b51eSKate Stone           if (target.GetSectionLoadList().SetSectionLoadAddress(
211b9c1b51eSKate Stone                   section_sp, section_sp->GetFileAddress() + value))
2122756adf3SVirgile Bello             ++num_loaded_sections;
2132756adf3SVirgile Bello         }
2142756adf3SVirgile Bello       }
2152756adf3SVirgile Bello       changed = num_loaded_sections > 0;
2162756adf3SVirgile Bello     }
2172756adf3SVirgile Bello   }
2182756adf3SVirgile Bello   return changed;
2192756adf3SVirgile Bello }
2202756adf3SVirgile Bello 
221b9c1b51eSKate Stone ByteOrder ObjectFilePECOFF::GetByteOrder() const { return eByteOrderLittle; }
222f754f88fSGreg Clayton 
223b9c1b51eSKate Stone bool ObjectFilePECOFF::IsExecutable() const {
224237ad974SCharles Davis   return (m_coff_header.flags & llvm::COFF::IMAGE_FILE_DLL) == 0;
225f754f88fSGreg Clayton }
226f754f88fSGreg Clayton 
227b9c1b51eSKate Stone uint32_t ObjectFilePECOFF::GetAddressByteSize() const {
228f754f88fSGreg Clayton   if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32_PLUS)
229f754f88fSGreg Clayton     return 8;
230f754f88fSGreg Clayton   else if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32)
231f754f88fSGreg Clayton     return 4;
232f754f88fSGreg Clayton   return 4;
233f754f88fSGreg Clayton }
234f754f88fSGreg Clayton 
235f754f88fSGreg Clayton //----------------------------------------------------------------------
236f754f88fSGreg Clayton // NeedsEndianSwap
237f754f88fSGreg Clayton //
238f754f88fSGreg Clayton // Return true if an endian swap needs to occur when extracting data
239f754f88fSGreg Clayton // from this file.
240f754f88fSGreg Clayton //----------------------------------------------------------------------
241b9c1b51eSKate Stone bool ObjectFilePECOFF::NeedsEndianSwap() const {
242f754f88fSGreg Clayton #if defined(__LITTLE_ENDIAN__)
243f754f88fSGreg Clayton   return false;
244f754f88fSGreg Clayton #else
245f754f88fSGreg Clayton   return true;
246f754f88fSGreg Clayton #endif
247f754f88fSGreg Clayton }
248f754f88fSGreg Clayton //----------------------------------------------------------------------
249f754f88fSGreg Clayton // ParseDOSHeader
250f754f88fSGreg Clayton //----------------------------------------------------------------------
251b9c1b51eSKate Stone bool ObjectFilePECOFF::ParseDOSHeader(DataExtractor &data,
252b9c1b51eSKate Stone                                       dos_header_t &dos_header) {
253f754f88fSGreg Clayton   bool success = false;
254c7bece56SGreg Clayton   lldb::offset_t offset = 0;
25589eb1baeSVirgile Bello   success = data.ValidOffsetForDataOfSize(0, sizeof(dos_header));
256f754f88fSGreg Clayton 
257b9c1b51eSKate Stone   if (success) {
25889eb1baeSVirgile Bello     dos_header.e_magic = data.GetU16(&offset); // Magic number
25989eb1baeSVirgile Bello     success = dos_header.e_magic == IMAGE_DOS_SIGNATURE;
260f754f88fSGreg Clayton 
261b9c1b51eSKate Stone     if (success) {
26289eb1baeSVirgile Bello       dos_header.e_cblp = data.GetU16(&offset); // Bytes on last page of file
26389eb1baeSVirgile Bello       dos_header.e_cp = data.GetU16(&offset);   // Pages in file
26489eb1baeSVirgile Bello       dos_header.e_crlc = data.GetU16(&offset); // Relocations
265b9c1b51eSKate Stone       dos_header.e_cparhdr =
266b9c1b51eSKate Stone           data.GetU16(&offset); // Size of header in paragraphs
267b9c1b51eSKate Stone       dos_header.e_minalloc =
268b9c1b51eSKate Stone           data.GetU16(&offset); // Minimum extra paragraphs needed
269b9c1b51eSKate Stone       dos_header.e_maxalloc =
270b9c1b51eSKate Stone           data.GetU16(&offset);               // Maximum extra paragraphs needed
27189eb1baeSVirgile Bello       dos_header.e_ss = data.GetU16(&offset); // Initial (relative) SS value
27289eb1baeSVirgile Bello       dos_header.e_sp = data.GetU16(&offset); // Initial SP value
27389eb1baeSVirgile Bello       dos_header.e_csum = data.GetU16(&offset); // Checksum
27489eb1baeSVirgile Bello       dos_header.e_ip = data.GetU16(&offset);   // Initial IP value
27589eb1baeSVirgile Bello       dos_header.e_cs = data.GetU16(&offset);   // Initial (relative) CS value
276b9c1b51eSKate Stone       dos_header.e_lfarlc =
277b9c1b51eSKate Stone           data.GetU16(&offset); // File address of relocation table
27889eb1baeSVirgile Bello       dos_header.e_ovno = data.GetU16(&offset); // Overlay number
279f754f88fSGreg Clayton 
28089eb1baeSVirgile Bello       dos_header.e_res[0] = data.GetU16(&offset); // Reserved words
28189eb1baeSVirgile Bello       dos_header.e_res[1] = data.GetU16(&offset); // Reserved words
28289eb1baeSVirgile Bello       dos_header.e_res[2] = data.GetU16(&offset); // Reserved words
28389eb1baeSVirgile Bello       dos_header.e_res[3] = data.GetU16(&offset); // Reserved words
284f754f88fSGreg Clayton 
285b9c1b51eSKate Stone       dos_header.e_oemid =
286b9c1b51eSKate Stone           data.GetU16(&offset); // OEM identifier (for e_oeminfo)
287b9c1b51eSKate Stone       dos_header.e_oeminfo =
288b9c1b51eSKate Stone           data.GetU16(&offset); // OEM information; e_oemid specific
28989eb1baeSVirgile Bello       dos_header.e_res2[0] = data.GetU16(&offset); // Reserved words
29089eb1baeSVirgile Bello       dos_header.e_res2[1] = data.GetU16(&offset); // Reserved words
29189eb1baeSVirgile Bello       dos_header.e_res2[2] = data.GetU16(&offset); // Reserved words
29289eb1baeSVirgile Bello       dos_header.e_res2[3] = data.GetU16(&offset); // Reserved words
29389eb1baeSVirgile Bello       dos_header.e_res2[4] = data.GetU16(&offset); // Reserved words
29489eb1baeSVirgile Bello       dos_header.e_res2[5] = data.GetU16(&offset); // Reserved words
29589eb1baeSVirgile Bello       dos_header.e_res2[6] = data.GetU16(&offset); // Reserved words
29689eb1baeSVirgile Bello       dos_header.e_res2[7] = data.GetU16(&offset); // Reserved words
29789eb1baeSVirgile Bello       dos_header.e_res2[8] = data.GetU16(&offset); // Reserved words
29889eb1baeSVirgile Bello       dos_header.e_res2[9] = data.GetU16(&offset); // Reserved words
299f754f88fSGreg Clayton 
300b9c1b51eSKate Stone       dos_header.e_lfanew =
301b9c1b51eSKate Stone           data.GetU32(&offset); // File address of new exe header
302f754f88fSGreg Clayton     }
303f754f88fSGreg Clayton   }
304f754f88fSGreg Clayton   if (!success)
30589eb1baeSVirgile Bello     memset(&dos_header, 0, sizeof(dos_header));
306f754f88fSGreg Clayton   return success;
307f754f88fSGreg Clayton }
308f754f88fSGreg Clayton 
309f754f88fSGreg Clayton //----------------------------------------------------------------------
310f754f88fSGreg Clayton // ParserCOFFHeader
311f754f88fSGreg Clayton //----------------------------------------------------------------------
312b9c1b51eSKate Stone bool ObjectFilePECOFF::ParseCOFFHeader(DataExtractor &data,
313b9c1b51eSKate Stone                                        lldb::offset_t *offset_ptr,
314b9c1b51eSKate Stone                                        coff_header_t &coff_header) {
315b9c1b51eSKate Stone   bool success =
316b9c1b51eSKate Stone       data.ValidOffsetForDataOfSize(*offset_ptr, sizeof(coff_header));
317b9c1b51eSKate Stone   if (success) {
31889eb1baeSVirgile Bello     coff_header.machine = data.GetU16(offset_ptr);
31989eb1baeSVirgile Bello     coff_header.nsects = data.GetU16(offset_ptr);
32089eb1baeSVirgile Bello     coff_header.modtime = data.GetU32(offset_ptr);
32189eb1baeSVirgile Bello     coff_header.symoff = data.GetU32(offset_ptr);
32289eb1baeSVirgile Bello     coff_header.nsyms = data.GetU32(offset_ptr);
32389eb1baeSVirgile Bello     coff_header.hdrsize = data.GetU16(offset_ptr);
32489eb1baeSVirgile Bello     coff_header.flags = data.GetU16(offset_ptr);
325f754f88fSGreg Clayton   }
326f754f88fSGreg Clayton   if (!success)
32789eb1baeSVirgile Bello     memset(&coff_header, 0, sizeof(coff_header));
328f754f88fSGreg Clayton   return success;
329f754f88fSGreg Clayton }
330f754f88fSGreg Clayton 
331b9c1b51eSKate Stone bool ObjectFilePECOFF::ParseCOFFOptionalHeader(lldb::offset_t *offset_ptr) {
332f754f88fSGreg Clayton   bool success = false;
333c7bece56SGreg Clayton   const lldb::offset_t end_offset = *offset_ptr + m_coff_header.hdrsize;
334b9c1b51eSKate Stone   if (*offset_ptr < end_offset) {
335f754f88fSGreg Clayton     success = true;
336f754f88fSGreg Clayton     m_coff_header_opt.magic = m_data.GetU16(offset_ptr);
337f754f88fSGreg Clayton     m_coff_header_opt.major_linker_version = m_data.GetU8(offset_ptr);
338f754f88fSGreg Clayton     m_coff_header_opt.minor_linker_version = m_data.GetU8(offset_ptr);
339f754f88fSGreg Clayton     m_coff_header_opt.code_size = m_data.GetU32(offset_ptr);
340f754f88fSGreg Clayton     m_coff_header_opt.data_size = m_data.GetU32(offset_ptr);
341f754f88fSGreg Clayton     m_coff_header_opt.bss_size = m_data.GetU32(offset_ptr);
342f754f88fSGreg Clayton     m_coff_header_opt.entry = m_data.GetU32(offset_ptr);
343f754f88fSGreg Clayton     m_coff_header_opt.code_offset = m_data.GetU32(offset_ptr);
344f754f88fSGreg Clayton 
345f754f88fSGreg Clayton     const uint32_t addr_byte_size = GetAddressByteSize();
346f754f88fSGreg Clayton 
347b9c1b51eSKate Stone     if (*offset_ptr < end_offset) {
348b9c1b51eSKate Stone       if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32) {
349f754f88fSGreg Clayton         // PE32 only
350f754f88fSGreg Clayton         m_coff_header_opt.data_offset = m_data.GetU32(offset_ptr);
351b9c1b51eSKate Stone       } else
352f754f88fSGreg Clayton         m_coff_header_opt.data_offset = 0;
353f754f88fSGreg Clayton 
354b9c1b51eSKate Stone       if (*offset_ptr < end_offset) {
355b9c1b51eSKate Stone         m_coff_header_opt.image_base =
356b9c1b51eSKate Stone             m_data.GetMaxU64(offset_ptr, addr_byte_size);
357f754f88fSGreg Clayton         m_coff_header_opt.sect_alignment = m_data.GetU32(offset_ptr);
358f754f88fSGreg Clayton         m_coff_header_opt.file_alignment = m_data.GetU32(offset_ptr);
359f754f88fSGreg Clayton         m_coff_header_opt.major_os_system_version = m_data.GetU16(offset_ptr);
360f754f88fSGreg Clayton         m_coff_header_opt.minor_os_system_version = m_data.GetU16(offset_ptr);
361f754f88fSGreg Clayton         m_coff_header_opt.major_image_version = m_data.GetU16(offset_ptr);
362f754f88fSGreg Clayton         m_coff_header_opt.minor_image_version = m_data.GetU16(offset_ptr);
363f754f88fSGreg Clayton         m_coff_header_opt.major_subsystem_version = m_data.GetU16(offset_ptr);
364f754f88fSGreg Clayton         m_coff_header_opt.minor_subsystem_version = m_data.GetU16(offset_ptr);
365f754f88fSGreg Clayton         m_coff_header_opt.reserved1 = m_data.GetU32(offset_ptr);
366f754f88fSGreg Clayton         m_coff_header_opt.image_size = m_data.GetU32(offset_ptr);
367f754f88fSGreg Clayton         m_coff_header_opt.header_size = m_data.GetU32(offset_ptr);
36828469ca3SGreg Clayton         m_coff_header_opt.checksum = m_data.GetU32(offset_ptr);
369f754f88fSGreg Clayton         m_coff_header_opt.subsystem = m_data.GetU16(offset_ptr);
370f754f88fSGreg Clayton         m_coff_header_opt.dll_flags = m_data.GetU16(offset_ptr);
371b9c1b51eSKate Stone         m_coff_header_opt.stack_reserve_size =
372b9c1b51eSKate Stone             m_data.GetMaxU64(offset_ptr, addr_byte_size);
373b9c1b51eSKate Stone         m_coff_header_opt.stack_commit_size =
374b9c1b51eSKate Stone             m_data.GetMaxU64(offset_ptr, addr_byte_size);
375b9c1b51eSKate Stone         m_coff_header_opt.heap_reserve_size =
376b9c1b51eSKate Stone             m_data.GetMaxU64(offset_ptr, addr_byte_size);
377b9c1b51eSKate Stone         m_coff_header_opt.heap_commit_size =
378b9c1b51eSKate Stone             m_data.GetMaxU64(offset_ptr, addr_byte_size);
379f754f88fSGreg Clayton         m_coff_header_opt.loader_flags = m_data.GetU32(offset_ptr);
380f754f88fSGreg Clayton         uint32_t num_data_dir_entries = m_data.GetU32(offset_ptr);
381f754f88fSGreg Clayton         m_coff_header_opt.data_dirs.clear();
382f754f88fSGreg Clayton         m_coff_header_opt.data_dirs.resize(num_data_dir_entries);
383f754f88fSGreg Clayton         uint32_t i;
384b9c1b51eSKate Stone         for (i = 0; i < num_data_dir_entries; i++) {
385f754f88fSGreg Clayton           m_coff_header_opt.data_dirs[i].vmaddr = m_data.GetU32(offset_ptr);
386f754f88fSGreg Clayton           m_coff_header_opt.data_dirs[i].vmsize = m_data.GetU32(offset_ptr);
387f754f88fSGreg Clayton         }
3882756adf3SVirgile Bello 
3892756adf3SVirgile Bello         m_file_offset = m_coff_header_opt.image_base;
3902756adf3SVirgile Bello         m_image_base = m_coff_header_opt.image_base;
391f754f88fSGreg Clayton       }
392f754f88fSGreg Clayton     }
393f754f88fSGreg Clayton   }
394f754f88fSGreg Clayton   // Make sure we are on track for section data which follows
395f754f88fSGreg Clayton   *offset_ptr = end_offset;
396f754f88fSGreg Clayton   return success;
397f754f88fSGreg Clayton }
398f754f88fSGreg Clayton 
399f754f88fSGreg Clayton //----------------------------------------------------------------------
400f754f88fSGreg Clayton // ParseSectionHeaders
401f754f88fSGreg Clayton //----------------------------------------------------------------------
402b9c1b51eSKate Stone bool ObjectFilePECOFF::ParseSectionHeaders(
403b9c1b51eSKate Stone     uint32_t section_header_data_offset) {
404f754f88fSGreg Clayton   const uint32_t nsects = m_coff_header.nsects;
405f754f88fSGreg Clayton   m_sect_headers.clear();
406f754f88fSGreg Clayton 
407b9c1b51eSKate Stone   if (nsects > 0) {
408f754f88fSGreg Clayton     const uint32_t addr_byte_size = GetAddressByteSize();
409f754f88fSGreg Clayton     const size_t section_header_byte_size = nsects * sizeof(section_header_t);
410b9c1b51eSKate Stone     DataBufferSP section_header_data_sp(m_file.ReadFileContents(
411b9c1b51eSKate Stone         section_header_data_offset, section_header_byte_size));
412b9c1b51eSKate Stone     DataExtractor section_header_data(section_header_data_sp, GetByteOrder(),
413b9c1b51eSKate Stone                                       addr_byte_size);
414f754f88fSGreg Clayton 
415c7bece56SGreg Clayton     lldb::offset_t offset = 0;
416b9c1b51eSKate Stone     if (section_header_data.ValidOffsetForDataOfSize(
417b9c1b51eSKate Stone             offset, section_header_byte_size)) {
418f754f88fSGreg Clayton       m_sect_headers.resize(nsects);
419f754f88fSGreg Clayton 
420b9c1b51eSKate Stone       for (uint32_t idx = 0; idx < nsects; ++idx) {
421f754f88fSGreg Clayton         const void *name_data = section_header_data.GetData(&offset, 8);
422b9c1b51eSKate Stone         if (name_data) {
423f754f88fSGreg Clayton           memcpy(m_sect_headers[idx].name, name_data, 8);
424f754f88fSGreg Clayton           m_sect_headers[idx].vmsize = section_header_data.GetU32(&offset);
425f754f88fSGreg Clayton           m_sect_headers[idx].vmaddr = section_header_data.GetU32(&offset);
426f754f88fSGreg Clayton           m_sect_headers[idx].size = section_header_data.GetU32(&offset);
427f754f88fSGreg Clayton           m_sect_headers[idx].offset = section_header_data.GetU32(&offset);
428f754f88fSGreg Clayton           m_sect_headers[idx].reloff = section_header_data.GetU32(&offset);
429f754f88fSGreg Clayton           m_sect_headers[idx].lineoff = section_header_data.GetU32(&offset);
430f754f88fSGreg Clayton           m_sect_headers[idx].nreloc = section_header_data.GetU16(&offset);
431f754f88fSGreg Clayton           m_sect_headers[idx].nline = section_header_data.GetU16(&offset);
432f754f88fSGreg Clayton           m_sect_headers[idx].flags = section_header_data.GetU32(&offset);
433f754f88fSGreg Clayton         }
434f754f88fSGreg Clayton       }
435f754f88fSGreg Clayton     }
436f754f88fSGreg Clayton   }
437f754f88fSGreg Clayton 
438f754f88fSGreg Clayton   return m_sect_headers.empty() == false;
439f754f88fSGreg Clayton }
440f754f88fSGreg Clayton 
441b9c1b51eSKate Stone bool ObjectFilePECOFF::GetSectionName(std::string &sect_name,
442b9c1b51eSKate Stone                                       const section_header_t &sect) {
443b9c1b51eSKate Stone   if (sect.name[0] == '/') {
444c7bece56SGreg Clayton     lldb::offset_t stroff = strtoul(&sect.name[1], NULL, 10);
445b9c1b51eSKate Stone     lldb::offset_t string_file_offset =
446b9c1b51eSKate Stone         m_coff_header.symoff + (m_coff_header.nsyms * 18) + stroff;
447f754f88fSGreg Clayton     const char *name = m_data.GetCStr(&string_file_offset);
448b9c1b51eSKate Stone     if (name) {
449f754f88fSGreg Clayton       sect_name = name;
450f754f88fSGreg Clayton       return true;
451f754f88fSGreg Clayton     }
452f754f88fSGreg Clayton 
453f754f88fSGreg Clayton     return false;
454f754f88fSGreg Clayton   }
455f754f88fSGreg Clayton   sect_name = sect.name;
456f754f88fSGreg Clayton   return true;
457f754f88fSGreg Clayton }
458f754f88fSGreg Clayton 
459f754f88fSGreg Clayton //----------------------------------------------------------------------
460f754f88fSGreg Clayton // GetNListSymtab
461f754f88fSGreg Clayton //----------------------------------------------------------------------
462b9c1b51eSKate Stone Symtab *ObjectFilePECOFF::GetSymtab() {
463a1743499SGreg Clayton   ModuleSP module_sp(GetModule());
464b9c1b51eSKate Stone   if (module_sp) {
46516ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
466b9c1b51eSKate Stone     if (m_symtab_ap.get() == NULL) {
467f754f88fSGreg Clayton       SectionList *sect_list = GetSectionList();
468f754f88fSGreg Clayton       m_symtab_ap.reset(new Symtab(this));
469bb19a13cSSaleem Abdulrasool       std::lock_guard<std::recursive_mutex> guard(m_symtab_ap->GetMutex());
47028469ca3SGreg Clayton 
47128469ca3SGreg Clayton       const uint32_t num_syms = m_coff_header.nsyms;
47228469ca3SGreg Clayton 
473b9c1b51eSKate Stone       if (num_syms > 0 && m_coff_header.symoff > 0) {
4740076e715SGreg Clayton         const uint32_t symbol_size = 18;
47528469ca3SGreg Clayton         const uint32_t addr_byte_size = GetAddressByteSize();
47628469ca3SGreg Clayton         const size_t symbol_data_size = num_syms * symbol_size;
477c35b91ceSAdrian McCarthy         // Include the 4-byte string table size at the end of the symbols
478b9c1b51eSKate Stone         DataBufferSP symtab_data_sp(m_file.ReadFileContents(
479b9c1b51eSKate Stone             m_coff_header.symoff, symbol_data_size + 4));
480b9c1b51eSKate Stone         DataExtractor symtab_data(symtab_data_sp, GetByteOrder(),
481b9c1b51eSKate Stone                                   addr_byte_size);
482c7bece56SGreg Clayton         lldb::offset_t offset = symbol_data_size;
48328469ca3SGreg Clayton         const uint32_t strtab_size = symtab_data.GetU32(&offset);
484b9c1b51eSKate Stone         DataBufferSP strtab_data_sp(m_file.ReadFileContents(
485b9c1b51eSKate Stone             m_coff_header.symoff + symbol_data_size, strtab_size));
486b9c1b51eSKate Stone         DataExtractor strtab_data(strtab_data_sp, GetByteOrder(),
487b9c1b51eSKate Stone                                   addr_byte_size);
48828469ca3SGreg Clayton 
4890076e715SGreg Clayton         // First 4 bytes should be zeroed after strtab_size has been read,
4900076e715SGreg Clayton         // because it is used as offset 0 to encode a NULL string.
4910076e715SGreg Clayton         uint32_t *strtab_data_start = (uint32_t *)strtab_data_sp->GetBytes();
4920076e715SGreg Clayton         strtab_data_start[0] = 0;
4930076e715SGreg Clayton 
49428469ca3SGreg Clayton         offset = 0;
49528469ca3SGreg Clayton         std::string symbol_name;
496f754f88fSGreg Clayton         Symbol *symbols = m_symtab_ap->Resize(num_syms);
497b9c1b51eSKate Stone         for (uint32_t i = 0; i < num_syms; ++i) {
498f754f88fSGreg Clayton           coff_symbol_t symbol;
49928469ca3SGreg Clayton           const uint32_t symbol_offset = offset;
50028469ca3SGreg Clayton           const char *symbol_name_cstr = NULL;
501c35b91ceSAdrian McCarthy           // If the first 4 bytes of the symbol string are zero, then they
502c35b91ceSAdrian McCarthy           // are followed by a 4-byte string table offset. Else these
50328469ca3SGreg Clayton           // 8 bytes contain the symbol name
504b9c1b51eSKate Stone           if (symtab_data.GetU32(&offset) == 0) {
50528469ca3SGreg Clayton             // Long string that doesn't fit into the symbol table name,
50628469ca3SGreg Clayton             // so now we must read the 4 byte string table offset
50728469ca3SGreg Clayton             uint32_t strtab_offset = symtab_data.GetU32(&offset);
50828469ca3SGreg Clayton             symbol_name_cstr = strtab_data.PeekCStr(strtab_offset);
50928469ca3SGreg Clayton             symbol_name.assign(symbol_name_cstr);
510b9c1b51eSKate Stone           } else {
511b9c1b51eSKate Stone             // Short string that fits into the symbol table name which is 8
512b9c1b51eSKate Stone             // bytes
51328469ca3SGreg Clayton             offset += sizeof(symbol.name) - 4; // Skip remaining
51428469ca3SGreg Clayton             symbol_name_cstr = symtab_data.PeekCStr(symbol_offset);
51528469ca3SGreg Clayton             if (symbol_name_cstr == NULL)
516f754f88fSGreg Clayton               break;
51728469ca3SGreg Clayton             symbol_name.assign(symbol_name_cstr, sizeof(symbol.name));
51828469ca3SGreg Clayton           }
51928469ca3SGreg Clayton           symbol.value = symtab_data.GetU32(&offset);
52028469ca3SGreg Clayton           symbol.sect = symtab_data.GetU16(&offset);
52128469ca3SGreg Clayton           symbol.type = symtab_data.GetU16(&offset);
52228469ca3SGreg Clayton           symbol.storage = symtab_data.GetU8(&offset);
52328469ca3SGreg Clayton           symbol.naux = symtab_data.GetU8(&offset);
524037520e9SGreg Clayton           symbols[i].GetMangled().SetValue(ConstString(symbol_name.c_str()));
525b9c1b51eSKate Stone           if ((int16_t)symbol.sect >= 1) {
526b9c1b51eSKate Stone             Address symbol_addr(sect_list->GetSectionAtIndex(symbol.sect - 1),
527b9c1b51eSKate Stone                                 symbol.value);
528358cf1eaSGreg Clayton             symbols[i].GetAddressRef() = symbol_addr;
529c35b91ceSAdrian McCarthy             symbols[i].SetType(MapSymbolType(symbol.type));
5300076e715SGreg Clayton           }
531f754f88fSGreg Clayton 
532b9c1b51eSKate Stone           if (symbol.naux > 0) {
533f754f88fSGreg Clayton             i += symbol.naux;
5340076e715SGreg Clayton             offset += symbol_size;
5350076e715SGreg Clayton           }
536f754f88fSGreg Clayton         }
537f754f88fSGreg Clayton       }
538a4fe3a12SVirgile Bello 
539a4fe3a12SVirgile Bello       // Read export header
540b9c1b51eSKate Stone       if (coff_data_dir_export_table < m_coff_header_opt.data_dirs.size() &&
541b9c1b51eSKate Stone           m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmsize > 0 &&
542b9c1b51eSKate Stone           m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmaddr > 0) {
543a4fe3a12SVirgile Bello         export_directory_entry export_table;
544b9c1b51eSKate Stone         uint32_t data_start =
545b9c1b51eSKate Stone             m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmaddr;
546a4fe3a12SVirgile Bello         Address address(m_coff_header_opt.image_base + data_start, sect_list);
547b9c1b51eSKate Stone         DataBufferSP symtab_data_sp(m_file.ReadFileContents(
548b9c1b51eSKate Stone             address.GetSection()->GetFileOffset() + address.GetOffset(),
549b9c1b51eSKate Stone             m_coff_header_opt.data_dirs[0].vmsize));
550b9c1b51eSKate Stone         DataExtractor symtab_data(symtab_data_sp, GetByteOrder(),
551b9c1b51eSKate Stone                                   GetAddressByteSize());
552a4fe3a12SVirgile Bello         lldb::offset_t offset = 0;
553a4fe3a12SVirgile Bello 
554a4fe3a12SVirgile Bello         // Read export_table header
555a4fe3a12SVirgile Bello         export_table.characteristics = symtab_data.GetU32(&offset);
556a4fe3a12SVirgile Bello         export_table.time_date_stamp = symtab_data.GetU32(&offset);
557a4fe3a12SVirgile Bello         export_table.major_version = symtab_data.GetU16(&offset);
558a4fe3a12SVirgile Bello         export_table.minor_version = symtab_data.GetU16(&offset);
559a4fe3a12SVirgile Bello         export_table.name = symtab_data.GetU32(&offset);
560a4fe3a12SVirgile Bello         export_table.base = symtab_data.GetU32(&offset);
561a4fe3a12SVirgile Bello         export_table.number_of_functions = symtab_data.GetU32(&offset);
562a4fe3a12SVirgile Bello         export_table.number_of_names = symtab_data.GetU32(&offset);
563a4fe3a12SVirgile Bello         export_table.address_of_functions = symtab_data.GetU32(&offset);
564a4fe3a12SVirgile Bello         export_table.address_of_names = symtab_data.GetU32(&offset);
565a4fe3a12SVirgile Bello         export_table.address_of_name_ordinals = symtab_data.GetU32(&offset);
566a4fe3a12SVirgile Bello 
567a4fe3a12SVirgile Bello         bool has_ordinal = export_table.address_of_name_ordinals != 0;
568a4fe3a12SVirgile Bello 
569a4fe3a12SVirgile Bello         lldb::offset_t name_offset = export_table.address_of_names - data_start;
570b9c1b51eSKate Stone         lldb::offset_t name_ordinal_offset =
571b9c1b51eSKate Stone             export_table.address_of_name_ordinals - data_start;
572a4fe3a12SVirgile Bello 
573a4fe3a12SVirgile Bello         Symbol *symbols = m_symtab_ap->Resize(export_table.number_of_names);
574a4fe3a12SVirgile Bello 
575a4fe3a12SVirgile Bello         std::string symbol_name;
576a4fe3a12SVirgile Bello 
577a4fe3a12SVirgile Bello         // Read each export table entry
578b9c1b51eSKate Stone         for (size_t i = 0; i < export_table.number_of_names; ++i) {
579b9c1b51eSKate Stone           uint32_t name_ordinal =
580b9c1b51eSKate Stone               has_ordinal ? symtab_data.GetU16(&name_ordinal_offset) : i;
581a4fe3a12SVirgile Bello           uint32_t name_address = symtab_data.GetU32(&name_offset);
582a4fe3a12SVirgile Bello 
583b9c1b51eSKate Stone           const char *symbol_name_cstr =
584b9c1b51eSKate Stone               symtab_data.PeekCStr(name_address - data_start);
585a4fe3a12SVirgile Bello           symbol_name.assign(symbol_name_cstr);
586a4fe3a12SVirgile Bello 
587b9c1b51eSKate Stone           lldb::offset_t function_offset = export_table.address_of_functions -
588b9c1b51eSKate Stone                                            data_start +
589b9c1b51eSKate Stone                                            sizeof(uint32_t) * name_ordinal;
590a4fe3a12SVirgile Bello           uint32_t function_rva = symtab_data.GetU32(&function_offset);
591a4fe3a12SVirgile Bello 
592b9c1b51eSKate Stone           Address symbol_addr(m_coff_header_opt.image_base + function_rva,
593b9c1b51eSKate Stone                               sect_list);
594a4fe3a12SVirgile Bello           symbols[i].GetMangled().SetValue(ConstString(symbol_name.c_str()));
595358cf1eaSGreg Clayton           symbols[i].GetAddressRef() = symbol_addr;
596a4fe3a12SVirgile Bello           symbols[i].SetType(lldb::eSymbolTypeCode);
597a4fe3a12SVirgile Bello           symbols[i].SetDebug(true);
598a4fe3a12SVirgile Bello         }
599a4fe3a12SVirgile Bello       }
600225d3ea3SAdrian McCarthy       m_symtab_ap->CalculateSymbolSizes();
601f754f88fSGreg Clayton     }
602a1743499SGreg Clayton   }
603f754f88fSGreg Clayton   return m_symtab_ap.get();
604f754f88fSGreg Clayton }
605f754f88fSGreg Clayton 
606b9c1b51eSKate Stone bool ObjectFilePECOFF::IsStripped() {
6073046e668SGreg Clayton   // TODO: determine this for COFF
6083046e668SGreg Clayton   return false;
6093046e668SGreg Clayton }
6103046e668SGreg Clayton 
611b9c1b51eSKate Stone void ObjectFilePECOFF::CreateSections(SectionList &unified_section_list) {
612b9c1b51eSKate Stone   if (!m_sections_ap.get()) {
6133046e668SGreg Clayton     m_sections_ap.reset(new SectionList());
6143046e668SGreg Clayton 
615a1743499SGreg Clayton     ModuleSP module_sp(GetModule());
616b9c1b51eSKate Stone     if (module_sp) {
61716ff8604SSaleem Abdulrasool       std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
618f754f88fSGreg Clayton       const uint32_t nsects = m_sect_headers.size();
619e72dfb32SGreg Clayton       ModuleSP module_sp(GetModule());
620b9c1b51eSKate Stone       for (uint32_t idx = 0; idx < nsects; ++idx) {
621f754f88fSGreg Clayton         std::string sect_name;
622f754f88fSGreg Clayton         GetSectionName(sect_name, m_sect_headers[idx]);
623f754f88fSGreg Clayton         ConstString const_sect_name(sect_name.c_str());
62428469ca3SGreg Clayton         static ConstString g_code_sect_name(".code");
62528469ca3SGreg Clayton         static ConstString g_CODE_sect_name("CODE");
62628469ca3SGreg Clayton         static ConstString g_data_sect_name(".data");
62728469ca3SGreg Clayton         static ConstString g_DATA_sect_name("DATA");
62828469ca3SGreg Clayton         static ConstString g_bss_sect_name(".bss");
62928469ca3SGreg Clayton         static ConstString g_BSS_sect_name("BSS");
63028469ca3SGreg Clayton         static ConstString g_debug_sect_name(".debug");
63128469ca3SGreg Clayton         static ConstString g_reloc_sect_name(".reloc");
63228469ca3SGreg Clayton         static ConstString g_stab_sect_name(".stab");
63328469ca3SGreg Clayton         static ConstString g_stabstr_sect_name(".stabstr");
6340076e715SGreg Clayton         static ConstString g_sect_name_dwarf_debug_abbrev(".debug_abbrev");
6350076e715SGreg Clayton         static ConstString g_sect_name_dwarf_debug_aranges(".debug_aranges");
6360076e715SGreg Clayton         static ConstString g_sect_name_dwarf_debug_frame(".debug_frame");
6370076e715SGreg Clayton         static ConstString g_sect_name_dwarf_debug_info(".debug_info");
6380076e715SGreg Clayton         static ConstString g_sect_name_dwarf_debug_line(".debug_line");
6390076e715SGreg Clayton         static ConstString g_sect_name_dwarf_debug_loc(".debug_loc");
6400076e715SGreg Clayton         static ConstString g_sect_name_dwarf_debug_macinfo(".debug_macinfo");
6410076e715SGreg Clayton         static ConstString g_sect_name_dwarf_debug_pubnames(".debug_pubnames");
6420076e715SGreg Clayton         static ConstString g_sect_name_dwarf_debug_pubtypes(".debug_pubtypes");
6430076e715SGreg Clayton         static ConstString g_sect_name_dwarf_debug_ranges(".debug_ranges");
6440076e715SGreg Clayton         static ConstString g_sect_name_dwarf_debug_str(".debug_str");
6450076e715SGreg Clayton         static ConstString g_sect_name_eh_frame(".eh_frame");
64665d4d5c3SRyan Brown         static ConstString g_sect_name_go_symtab(".gosymtab");
64728469ca3SGreg Clayton         SectionType section_type = eSectionTypeOther;
648237ad974SCharles Davis         if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_CODE &&
649b9c1b51eSKate Stone             ((const_sect_name == g_code_sect_name) ||
650b9c1b51eSKate Stone              (const_sect_name == g_CODE_sect_name))) {
65128469ca3SGreg Clayton           section_type = eSectionTypeCode;
652b9c1b51eSKate Stone         } else if (m_sect_headers[idx].flags &
653b9c1b51eSKate Stone                        llvm::COFF::IMAGE_SCN_CNT_INITIALIZED_DATA &&
654b9c1b51eSKate Stone                    ((const_sect_name == g_data_sect_name) ||
655b9c1b51eSKate Stone                     (const_sect_name == g_DATA_sect_name))) {
65628469ca3SGreg Clayton           section_type = eSectionTypeData;
657b9c1b51eSKate Stone         } else if (m_sect_headers[idx].flags &
658b9c1b51eSKate Stone                        llvm::COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA &&
659b9c1b51eSKate Stone                    ((const_sect_name == g_bss_sect_name) ||
660b9c1b51eSKate Stone                     (const_sect_name == g_BSS_sect_name))) {
66128469ca3SGreg Clayton           if (m_sect_headers[idx].size == 0)
66228469ca3SGreg Clayton             section_type = eSectionTypeZeroFill;
66328469ca3SGreg Clayton           else
66428469ca3SGreg Clayton             section_type = eSectionTypeData;
665b9c1b51eSKate Stone         } else if (const_sect_name == g_debug_sect_name) {
66628469ca3SGreg Clayton           section_type = eSectionTypeDebug;
667b9c1b51eSKate Stone         } else if (const_sect_name == g_stabstr_sect_name) {
66828469ca3SGreg Clayton           section_type = eSectionTypeDataCString;
669b9c1b51eSKate Stone         } else if (const_sect_name == g_reloc_sect_name) {
67028469ca3SGreg Clayton           section_type = eSectionTypeOther;
671b9c1b51eSKate Stone         } else if (const_sect_name == g_sect_name_dwarf_debug_abbrev)
672b9c1b51eSKate Stone           section_type = eSectionTypeDWARFDebugAbbrev;
673b9c1b51eSKate Stone         else if (const_sect_name == g_sect_name_dwarf_debug_aranges)
674b9c1b51eSKate Stone           section_type = eSectionTypeDWARFDebugAranges;
675b9c1b51eSKate Stone         else if (const_sect_name == g_sect_name_dwarf_debug_frame)
676b9c1b51eSKate Stone           section_type = eSectionTypeDWARFDebugFrame;
677b9c1b51eSKate Stone         else if (const_sect_name == g_sect_name_dwarf_debug_info)
678b9c1b51eSKate Stone           section_type = eSectionTypeDWARFDebugInfo;
679b9c1b51eSKate Stone         else if (const_sect_name == g_sect_name_dwarf_debug_line)
680b9c1b51eSKate Stone           section_type = eSectionTypeDWARFDebugLine;
681b9c1b51eSKate Stone         else if (const_sect_name == g_sect_name_dwarf_debug_loc)
682b9c1b51eSKate Stone           section_type = eSectionTypeDWARFDebugLoc;
683b9c1b51eSKate Stone         else if (const_sect_name == g_sect_name_dwarf_debug_macinfo)
684b9c1b51eSKate Stone           section_type = eSectionTypeDWARFDebugMacInfo;
685b9c1b51eSKate Stone         else if (const_sect_name == g_sect_name_dwarf_debug_pubnames)
686b9c1b51eSKate Stone           section_type = eSectionTypeDWARFDebugPubNames;
687b9c1b51eSKate Stone         else if (const_sect_name == g_sect_name_dwarf_debug_pubtypes)
688b9c1b51eSKate Stone           section_type = eSectionTypeDWARFDebugPubTypes;
689b9c1b51eSKate Stone         else if (const_sect_name == g_sect_name_dwarf_debug_ranges)
690b9c1b51eSKate Stone           section_type = eSectionTypeDWARFDebugRanges;
691b9c1b51eSKate Stone         else if (const_sect_name == g_sect_name_dwarf_debug_str)
692b9c1b51eSKate Stone           section_type = eSectionTypeDWARFDebugStr;
693b9c1b51eSKate Stone         else if (const_sect_name == g_sect_name_eh_frame)
694b9c1b51eSKate Stone           section_type = eSectionTypeEHFrame;
695b9c1b51eSKate Stone         else if (const_sect_name == g_sect_name_go_symtab)
696b9c1b51eSKate Stone           section_type = eSectionTypeGoSymtab;
697b9c1b51eSKate Stone         else if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_CODE) {
69828469ca3SGreg Clayton           section_type = eSectionTypeCode;
699b9c1b51eSKate Stone         } else if (m_sect_headers[idx].flags &
700b9c1b51eSKate Stone                    llvm::COFF::IMAGE_SCN_CNT_INITIALIZED_DATA) {
70128469ca3SGreg Clayton           section_type = eSectionTypeData;
702b9c1b51eSKate Stone         } else if (m_sect_headers[idx].flags &
703b9c1b51eSKate Stone                    llvm::COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) {
70428469ca3SGreg Clayton           if (m_sect_headers[idx].size == 0)
70528469ca3SGreg Clayton             section_type = eSectionTypeZeroFill;
70628469ca3SGreg Clayton           else
70728469ca3SGreg Clayton             section_type = eSectionTypeData;
70828469ca3SGreg Clayton         }
709f754f88fSGreg Clayton 
710f754f88fSGreg Clayton         // Use a segment ID of the segment index shifted left by 8 so they
711f754f88fSGreg Clayton         // never conflict with any of the sections.
712b9c1b51eSKate Stone         SectionSP section_sp(new Section(
713b9c1b51eSKate Stone             module_sp, // Module to which this section belongs
714a7499c98SMichael Sartain             this,      // Object file to which this section belongs
715b9c1b51eSKate Stone             idx + 1, // Section ID is the 1 based segment index shifted right by
716b9c1b51eSKate Stone                      // 8 bits as not to collide with any of the 256 section IDs
717b9c1b51eSKate Stone                      // that are possible
718f754f88fSGreg Clayton             const_sect_name, // Name of this section
71928469ca3SGreg Clayton             section_type,    // This section is a container of other sections.
720b9c1b51eSKate Stone             m_coff_header_opt.image_base +
721b9c1b51eSKate Stone                 m_sect_headers[idx].vmaddr, // File VM address == addresses as
722b9c1b51eSKate Stone                                             // they are found in the object file
723f754f88fSGreg Clayton             m_sect_headers[idx].vmsize,     // VM size in bytes of this section
724b9c1b51eSKate Stone             m_sect_headers[idx]
725b9c1b51eSKate Stone                 .offset, // Offset to the data for this section in the file
726b9c1b51eSKate Stone             m_sect_headers[idx]
727b9c1b51eSKate Stone                 .size, // Size in bytes of this section as found in the file
72848672afbSGreg Clayton             m_coff_header_opt.sect_alignment, // Section alignment
729f754f88fSGreg Clayton             m_sect_headers[idx].flags));      // Flags for this section
730f754f88fSGreg Clayton 
731f754f88fSGreg Clayton         // section_sp->SetIsEncrypted (segment_is_encrypted);
732f754f88fSGreg Clayton 
7333046e668SGreg Clayton         unified_section_list.AddSection(section_sp);
734f754f88fSGreg Clayton         m_sections_ap->AddSection(section_sp);
735f754f88fSGreg Clayton       }
736f754f88fSGreg Clayton     }
737a1743499SGreg Clayton   }
738f754f88fSGreg Clayton }
739f754f88fSGreg Clayton 
740b9c1b51eSKate Stone bool ObjectFilePECOFF::GetUUID(UUID *uuid) { return false; }
741f754f88fSGreg Clayton 
742b9c1b51eSKate Stone uint32_t ObjectFilePECOFF::GetDependentModules(FileSpecList &files) {
743f754f88fSGreg Clayton   return 0;
744f754f88fSGreg Clayton }
745f754f88fSGreg Clayton 
746b9c1b51eSKate Stone lldb_private::Address ObjectFilePECOFF::GetEntryPointAddress() {
7478e38c666SStephane Sezer   if (m_entry_point_address.IsValid())
7488e38c666SStephane Sezer     return m_entry_point_address;
7498e38c666SStephane Sezer 
7508e38c666SStephane Sezer   if (!ParseHeader() || !IsExecutable())
7518e38c666SStephane Sezer     return m_entry_point_address;
7528e38c666SStephane Sezer 
7538e38c666SStephane Sezer   SectionList *section_list = GetSectionList();
7548e38c666SStephane Sezer   addr_t offset = m_coff_header_opt.entry;
7558e38c666SStephane Sezer 
7568e38c666SStephane Sezer   if (!section_list)
7578e38c666SStephane Sezer     m_entry_point_address.SetOffset(offset);
7588e38c666SStephane Sezer   else
7598e38c666SStephane Sezer     m_entry_point_address.ResolveAddressUsingFileSections(offset, section_list);
7608e38c666SStephane Sezer   return m_entry_point_address;
7618e38c666SStephane Sezer }
7628e38c666SStephane Sezer 
763f754f88fSGreg Clayton //----------------------------------------------------------------------
764f754f88fSGreg Clayton // Dump
765f754f88fSGreg Clayton //
766f754f88fSGreg Clayton // Dump the specifics of the runtime file container (such as any headers
767f754f88fSGreg Clayton // segments, sections, etc).
768f754f88fSGreg Clayton //----------------------------------------------------------------------
769b9c1b51eSKate Stone void ObjectFilePECOFF::Dump(Stream *s) {
770a1743499SGreg Clayton   ModuleSP module_sp(GetModule());
771b9c1b51eSKate Stone   if (module_sp) {
77216ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
773324a1036SSaleem Abdulrasool     s->Printf("%p: ", static_cast<void *>(this));
774f754f88fSGreg Clayton     s->Indent();
775f754f88fSGreg Clayton     s->PutCString("ObjectFilePECOFF");
776f754f88fSGreg Clayton 
777f754f88fSGreg Clayton     ArchSpec header_arch;
778f754f88fSGreg Clayton     GetArchitecture(header_arch);
779f754f88fSGreg Clayton 
780b9c1b51eSKate Stone     *s << ", file = '" << m_file
781b9c1b51eSKate Stone        << "', arch = " << header_arch.GetArchitectureName() << "\n";
782f754f88fSGreg Clayton 
7833046e668SGreg Clayton     SectionList *sections = GetSectionList();
7843046e668SGreg Clayton     if (sections)
7853046e668SGreg Clayton       sections->Dump(s, NULL, true, UINT32_MAX);
786f754f88fSGreg Clayton 
787f754f88fSGreg Clayton     if (m_symtab_ap.get())
788f754f88fSGreg Clayton       m_symtab_ap->Dump(s, NULL, eSortOrderNone);
789f754f88fSGreg Clayton 
790f754f88fSGreg Clayton     if (m_dos_header.e_magic)
791f754f88fSGreg Clayton       DumpDOSHeader(s, m_dos_header);
792b9c1b51eSKate Stone     if (m_coff_header.machine) {
793f754f88fSGreg Clayton       DumpCOFFHeader(s, m_coff_header);
794f754f88fSGreg Clayton       if (m_coff_header.hdrsize)
795f754f88fSGreg Clayton         DumpOptCOFFHeader(s, m_coff_header_opt);
796f754f88fSGreg Clayton     }
797f754f88fSGreg Clayton     s->EOL();
798f754f88fSGreg Clayton     DumpSectionHeaders(s);
799f754f88fSGreg Clayton     s->EOL();
800f754f88fSGreg Clayton   }
801a1743499SGreg Clayton }
802f754f88fSGreg Clayton 
803f754f88fSGreg Clayton //----------------------------------------------------------------------
804f754f88fSGreg Clayton // DumpDOSHeader
805f754f88fSGreg Clayton //
806f754f88fSGreg Clayton // Dump the MS-DOS header to the specified output stream
807f754f88fSGreg Clayton //----------------------------------------------------------------------
808b9c1b51eSKate Stone void ObjectFilePECOFF::DumpDOSHeader(Stream *s, const dos_header_t &header) {
809f754f88fSGreg Clayton   s->PutCString("MSDOS Header\n");
810f754f88fSGreg Clayton   s->Printf("  e_magic    = 0x%4.4x\n", header.e_magic);
811f754f88fSGreg Clayton   s->Printf("  e_cblp     = 0x%4.4x\n", header.e_cblp);
812f754f88fSGreg Clayton   s->Printf("  e_cp       = 0x%4.4x\n", header.e_cp);
813f754f88fSGreg Clayton   s->Printf("  e_crlc     = 0x%4.4x\n", header.e_crlc);
814f754f88fSGreg Clayton   s->Printf("  e_cparhdr  = 0x%4.4x\n", header.e_cparhdr);
815f754f88fSGreg Clayton   s->Printf("  e_minalloc = 0x%4.4x\n", header.e_minalloc);
816f754f88fSGreg Clayton   s->Printf("  e_maxalloc = 0x%4.4x\n", header.e_maxalloc);
817f754f88fSGreg Clayton   s->Printf("  e_ss       = 0x%4.4x\n", header.e_ss);
818f754f88fSGreg Clayton   s->Printf("  e_sp       = 0x%4.4x\n", header.e_sp);
819f754f88fSGreg Clayton   s->Printf("  e_csum     = 0x%4.4x\n", header.e_csum);
820f754f88fSGreg Clayton   s->Printf("  e_ip       = 0x%4.4x\n", header.e_ip);
821f754f88fSGreg Clayton   s->Printf("  e_cs       = 0x%4.4x\n", header.e_cs);
822f754f88fSGreg Clayton   s->Printf("  e_lfarlc   = 0x%4.4x\n", header.e_lfarlc);
823f754f88fSGreg Clayton   s->Printf("  e_ovno     = 0x%4.4x\n", header.e_ovno);
824f754f88fSGreg Clayton   s->Printf("  e_res[4]   = { 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x }\n",
825b9c1b51eSKate Stone             header.e_res[0], header.e_res[1], header.e_res[2], header.e_res[3]);
826f754f88fSGreg Clayton   s->Printf("  e_oemid    = 0x%4.4x\n", header.e_oemid);
827f754f88fSGreg Clayton   s->Printf("  e_oeminfo  = 0x%4.4x\n", header.e_oeminfo);
828b9c1b51eSKate Stone   s->Printf("  e_res2[10] = { 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, "
829b9c1b51eSKate Stone             "0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x }\n",
830b9c1b51eSKate Stone             header.e_res2[0], header.e_res2[1], header.e_res2[2],
831b9c1b51eSKate Stone             header.e_res2[3], header.e_res2[4], header.e_res2[5],
832b9c1b51eSKate Stone             header.e_res2[6], header.e_res2[7], header.e_res2[8],
833f754f88fSGreg Clayton             header.e_res2[9]);
834f754f88fSGreg Clayton   s->Printf("  e_lfanew   = 0x%8.8x\n", header.e_lfanew);
835f754f88fSGreg Clayton }
836f754f88fSGreg Clayton 
837f754f88fSGreg Clayton //----------------------------------------------------------------------
838f754f88fSGreg Clayton // DumpCOFFHeader
839f754f88fSGreg Clayton //
840f754f88fSGreg Clayton // Dump the COFF header to the specified output stream
841f754f88fSGreg Clayton //----------------------------------------------------------------------
842b9c1b51eSKate Stone void ObjectFilePECOFF::DumpCOFFHeader(Stream *s, const coff_header_t &header) {
843f754f88fSGreg Clayton   s->PutCString("COFF Header\n");
844f754f88fSGreg Clayton   s->Printf("  machine = 0x%4.4x\n", header.machine);
845f754f88fSGreg Clayton   s->Printf("  nsects  = 0x%4.4x\n", header.nsects);
846f754f88fSGreg Clayton   s->Printf("  modtime = 0x%8.8x\n", header.modtime);
847f754f88fSGreg Clayton   s->Printf("  symoff  = 0x%8.8x\n", header.symoff);
848f754f88fSGreg Clayton   s->Printf("  nsyms   = 0x%8.8x\n", header.nsyms);
849f754f88fSGreg Clayton   s->Printf("  hdrsize = 0x%4.4x\n", header.hdrsize);
850f754f88fSGreg Clayton }
851f754f88fSGreg Clayton 
852f754f88fSGreg Clayton //----------------------------------------------------------------------
853f754f88fSGreg Clayton // DumpOptCOFFHeader
854f754f88fSGreg Clayton //
855f754f88fSGreg Clayton // Dump the optional COFF header to the specified output stream
856f754f88fSGreg Clayton //----------------------------------------------------------------------
857b9c1b51eSKate Stone void ObjectFilePECOFF::DumpOptCOFFHeader(Stream *s,
858b9c1b51eSKate Stone                                          const coff_opt_header_t &header) {
859f754f88fSGreg Clayton   s->PutCString("Optional COFF Header\n");
860f754f88fSGreg Clayton   s->Printf("  magic                   = 0x%4.4x\n", header.magic);
861b9c1b51eSKate Stone   s->Printf("  major_linker_version    = 0x%2.2x\n",
862b9c1b51eSKate Stone             header.major_linker_version);
863b9c1b51eSKate Stone   s->Printf("  minor_linker_version    = 0x%2.2x\n",
864b9c1b51eSKate Stone             header.minor_linker_version);
865f754f88fSGreg Clayton   s->Printf("  code_size               = 0x%8.8x\n", header.code_size);
866f754f88fSGreg Clayton   s->Printf("  data_size               = 0x%8.8x\n", header.data_size);
867f754f88fSGreg Clayton   s->Printf("  bss_size                = 0x%8.8x\n", header.bss_size);
868f754f88fSGreg Clayton   s->Printf("  entry                   = 0x%8.8x\n", header.entry);
869f754f88fSGreg Clayton   s->Printf("  code_offset             = 0x%8.8x\n", header.code_offset);
870f754f88fSGreg Clayton   s->Printf("  data_offset             = 0x%8.8x\n", header.data_offset);
871b9c1b51eSKate Stone   s->Printf("  image_base              = 0x%16.16" PRIx64 "\n",
872b9c1b51eSKate Stone             header.image_base);
873f754f88fSGreg Clayton   s->Printf("  sect_alignment          = 0x%8.8x\n", header.sect_alignment);
874f754f88fSGreg Clayton   s->Printf("  file_alignment          = 0x%8.8x\n", header.file_alignment);
875b9c1b51eSKate Stone   s->Printf("  major_os_system_version = 0x%4.4x\n",
876b9c1b51eSKate Stone             header.major_os_system_version);
877b9c1b51eSKate Stone   s->Printf("  minor_os_system_version = 0x%4.4x\n",
878b9c1b51eSKate Stone             header.minor_os_system_version);
879b9c1b51eSKate Stone   s->Printf("  major_image_version     = 0x%4.4x\n",
880b9c1b51eSKate Stone             header.major_image_version);
881b9c1b51eSKate Stone   s->Printf("  minor_image_version     = 0x%4.4x\n",
882b9c1b51eSKate Stone             header.minor_image_version);
883b9c1b51eSKate Stone   s->Printf("  major_subsystem_version = 0x%4.4x\n",
884b9c1b51eSKate Stone             header.major_subsystem_version);
885b9c1b51eSKate Stone   s->Printf("  minor_subsystem_version = 0x%4.4x\n",
886b9c1b51eSKate Stone             header.minor_subsystem_version);
887f754f88fSGreg Clayton   s->Printf("  reserved1               = 0x%8.8x\n", header.reserved1);
888f754f88fSGreg Clayton   s->Printf("  image_size              = 0x%8.8x\n", header.image_size);
889f754f88fSGreg Clayton   s->Printf("  header_size             = 0x%8.8x\n", header.header_size);
89028469ca3SGreg Clayton   s->Printf("  checksum                = 0x%8.8x\n", header.checksum);
891f754f88fSGreg Clayton   s->Printf("  subsystem               = 0x%4.4x\n", header.subsystem);
892f754f88fSGreg Clayton   s->Printf("  dll_flags               = 0x%4.4x\n", header.dll_flags);
893b9c1b51eSKate Stone   s->Printf("  stack_reserve_size      = 0x%16.16" PRIx64 "\n",
894b9c1b51eSKate Stone             header.stack_reserve_size);
895b9c1b51eSKate Stone   s->Printf("  stack_commit_size       = 0x%16.16" PRIx64 "\n",
896b9c1b51eSKate Stone             header.stack_commit_size);
897b9c1b51eSKate Stone   s->Printf("  heap_reserve_size       = 0x%16.16" PRIx64 "\n",
898b9c1b51eSKate Stone             header.heap_reserve_size);
899b9c1b51eSKate Stone   s->Printf("  heap_commit_size        = 0x%16.16" PRIx64 "\n",
900b9c1b51eSKate Stone             header.heap_commit_size);
901f754f88fSGreg Clayton   s->Printf("  loader_flags            = 0x%8.8x\n", header.loader_flags);
902b9c1b51eSKate Stone   s->Printf("  num_data_dir_entries    = 0x%8.8x\n",
903b9c1b51eSKate Stone             (uint32_t)header.data_dirs.size());
904f754f88fSGreg Clayton   uint32_t i;
905b9c1b51eSKate Stone   for (i = 0; i < header.data_dirs.size(); i++) {
906b9c1b51eSKate Stone     s->Printf("  data_dirs[%2u] vmaddr = 0x%8.8x, vmsize = 0x%8.8x\n", i,
907b9c1b51eSKate Stone               header.data_dirs[i].vmaddr, header.data_dirs[i].vmsize);
908f754f88fSGreg Clayton   }
909f754f88fSGreg Clayton }
910f754f88fSGreg Clayton //----------------------------------------------------------------------
911f754f88fSGreg Clayton // DumpSectionHeader
912f754f88fSGreg Clayton //
913f754f88fSGreg Clayton // Dump a single ELF section header to the specified output stream
914f754f88fSGreg Clayton //----------------------------------------------------------------------
915b9c1b51eSKate Stone void ObjectFilePECOFF::DumpSectionHeader(Stream *s,
916b9c1b51eSKate Stone                                          const section_header_t &sh) {
917f754f88fSGreg Clayton   std::string name;
918f754f88fSGreg Clayton   GetSectionName(name, sh);
919b9c1b51eSKate 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 "
920b9c1b51eSKate Stone             "0x%4.4x 0x%8.8x\n",
921b9c1b51eSKate Stone             name.c_str(), sh.vmaddr, sh.vmsize, sh.offset, sh.size, sh.reloff,
922b9c1b51eSKate Stone             sh.lineoff, sh.nreloc, sh.nline, sh.flags);
923f754f88fSGreg Clayton }
924f754f88fSGreg Clayton 
925f754f88fSGreg Clayton //----------------------------------------------------------------------
926f754f88fSGreg Clayton // DumpSectionHeaders
927f754f88fSGreg Clayton //
928f754f88fSGreg Clayton // Dump all of the ELF section header to the specified output stream
929f754f88fSGreg Clayton //----------------------------------------------------------------------
930b9c1b51eSKate Stone void ObjectFilePECOFF::DumpSectionHeaders(Stream *s) {
931f754f88fSGreg Clayton 
932f754f88fSGreg Clayton   s->PutCString("Section Headers\n");
933b9c1b51eSKate Stone   s->PutCString("IDX  name             vm addr    vm size    file off   file "
934b9c1b51eSKate Stone                 "size  reloc off  line off   nreloc nline  flags\n");
935b9c1b51eSKate Stone   s->PutCString("==== ---------------- ---------- ---------- ---------- "
936b9c1b51eSKate Stone                 "---------- ---------- ---------- ------ ------ ----------\n");
937f754f88fSGreg Clayton 
938f754f88fSGreg Clayton   uint32_t idx = 0;
939f754f88fSGreg Clayton   SectionHeaderCollIter pos, end = m_sect_headers.end();
940f754f88fSGreg Clayton 
941b9c1b51eSKate Stone   for (pos = m_sect_headers.begin(); pos != end; ++pos, ++idx) {
942f754f88fSGreg Clayton     s->Printf("[%2u] ", idx);
943f754f88fSGreg Clayton     ObjectFilePECOFF::DumpSectionHeader(s, *pos);
944f754f88fSGreg Clayton   }
945f754f88fSGreg Clayton }
946f754f88fSGreg Clayton 
947*fb3b3bd1SZachary Turner bool ObjectFilePECOFF::IsWindowsSubsystem() {
948*fb3b3bd1SZachary Turner   switch (m_coff_header_opt.subsystem) {
949*fb3b3bd1SZachary Turner   case llvm::COFF::IMAGE_SUBSYSTEM_NATIVE:
950*fb3b3bd1SZachary Turner   case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_GUI:
951*fb3b3bd1SZachary Turner   case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_CUI:
952*fb3b3bd1SZachary Turner   case llvm::COFF::IMAGE_SUBSYSTEM_NATIVE_WINDOWS:
953*fb3b3bd1SZachary Turner   case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_CE_GUI:
954*fb3b3bd1SZachary Turner   case llvm::COFF::IMAGE_SUBSYSTEM_XBOX:
955*fb3b3bd1SZachary Turner   case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_BOOT_APPLICATION:
956*fb3b3bd1SZachary Turner     return true;
957*fb3b3bd1SZachary Turner   default:
958*fb3b3bd1SZachary Turner     return false;
959*fb3b3bd1SZachary Turner   }
960*fb3b3bd1SZachary Turner }
961*fb3b3bd1SZachary Turner 
962b9c1b51eSKate Stone bool ObjectFilePECOFF::GetArchitecture(ArchSpec &arch) {
963237ad974SCharles Davis   uint16_t machine = m_coff_header.machine;
964b9c1b51eSKate Stone   switch (machine) {
965237ad974SCharles Davis   case llvm::COFF::IMAGE_FILE_MACHINE_AMD64:
966237ad974SCharles Davis   case llvm::COFF::IMAGE_FILE_MACHINE_I386:
967237ad974SCharles Davis   case llvm::COFF::IMAGE_FILE_MACHINE_POWERPC:
968237ad974SCharles Davis   case llvm::COFF::IMAGE_FILE_MACHINE_POWERPCFP:
969237ad974SCharles Davis   case llvm::COFF::IMAGE_FILE_MACHINE_ARM:
9701108cb36SSaleem Abdulrasool   case llvm::COFF::IMAGE_FILE_MACHINE_ARMNT:
971237ad974SCharles Davis   case llvm::COFF::IMAGE_FILE_MACHINE_THUMB:
972*fb3b3bd1SZachary Turner     arch.SetArchitecture(eArchTypeCOFF, machine, LLDB_INVALID_CPUTYPE,
973*fb3b3bd1SZachary Turner                          IsWindowsSubsystem() ? llvm::Triple::Win32
974*fb3b3bd1SZachary Turner                                               : llvm::Triple::UnknownOS);
975237ad974SCharles Davis     return true;
976237ad974SCharles Davis   default:
977237ad974SCharles Davis     break;
978237ad974SCharles Davis   }
979237ad974SCharles Davis   return false;
980f754f88fSGreg Clayton }
981f754f88fSGreg Clayton 
982b9c1b51eSKate Stone ObjectFile::Type ObjectFilePECOFF::CalculateType() {
983b9c1b51eSKate Stone   if (m_coff_header.machine != 0) {
984237ad974SCharles Davis     if ((m_coff_header.flags & llvm::COFF::IMAGE_FILE_DLL) == 0)
985f754f88fSGreg Clayton       return eTypeExecutable;
986f754f88fSGreg Clayton     else
987f754f88fSGreg Clayton       return eTypeSharedLibrary;
988f754f88fSGreg Clayton   }
989f754f88fSGreg Clayton   return eTypeExecutable;
990f754f88fSGreg Clayton }
991f754f88fSGreg Clayton 
992b9c1b51eSKate Stone ObjectFile::Strata ObjectFilePECOFF::CalculateStrata() { return eStrataUser; }
993f754f88fSGreg Clayton //------------------------------------------------------------------
994f754f88fSGreg Clayton // PluginInterface protocol
995f754f88fSGreg Clayton //------------------------------------------------------------------
996b9c1b51eSKate Stone ConstString ObjectFilePECOFF::GetPluginName() { return GetPluginNameStatic(); }
997f754f88fSGreg Clayton 
998b9c1b51eSKate Stone uint32_t ObjectFilePECOFF::GetPluginVersion() { return 1; }
999