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/Host/FileSpec.h"
18f754f88fSGreg Clayton #include "lldb/Core/FileSpecList.h"
19f754f88fSGreg Clayton #include "lldb/Core/Module.h"
20f4d6de6aSGreg Clayton #include "lldb/Core/ModuleSpec.h"
21f754f88fSGreg Clayton #include "lldb/Core/PluginManager.h"
22f754f88fSGreg Clayton #include "lldb/Core/Section.h"
23f754f88fSGreg Clayton #include "lldb/Core/StreamFile.h"
24f754f88fSGreg Clayton #include "lldb/Core/StreamString.h"
25f754f88fSGreg Clayton #include "lldb/Core/Timer.h"
26f754f88fSGreg Clayton #include "lldb/Core/UUID.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 
40f754f88fSGreg Clayton void
41f754f88fSGreg Clayton ObjectFilePECOFF::Initialize()
42f754f88fSGreg Clayton {
43f754f88fSGreg Clayton     PluginManager::RegisterPlugin (GetPluginNameStatic(),
44f754f88fSGreg Clayton                                    GetPluginDescriptionStatic(),
45c9660546SGreg Clayton                                    CreateInstance,
46f4d6de6aSGreg Clayton                                    CreateMemoryInstance,
47f7d1893fSAdrian McCarthy                                    GetModuleSpecifications,
48f7d1893fSAdrian McCarthy                                    SaveCore);
49f754f88fSGreg Clayton }
50f754f88fSGreg Clayton 
51f754f88fSGreg Clayton void
52f754f88fSGreg Clayton ObjectFilePECOFF::Terminate()
53f754f88fSGreg Clayton {
54f754f88fSGreg Clayton     PluginManager::UnregisterPlugin (CreateInstance);
55f754f88fSGreg Clayton }
56f754f88fSGreg Clayton 
57f754f88fSGreg Clayton 
5857abc5d6SGreg Clayton lldb_private::ConstString
59f754f88fSGreg Clayton ObjectFilePECOFF::GetPluginNameStatic()
60f754f88fSGreg Clayton {
6157abc5d6SGreg Clayton     static ConstString g_name("pe-coff");
6257abc5d6SGreg Clayton     return g_name;
63f754f88fSGreg Clayton }
64f754f88fSGreg Clayton 
65f754f88fSGreg Clayton const char *
66f754f88fSGreg Clayton ObjectFilePECOFF::GetPluginDescriptionStatic()
67f754f88fSGreg Clayton {
68f754f88fSGreg Clayton     return "Portable Executable and Common Object File Format object file reader (32 and 64 bit)";
69f754f88fSGreg Clayton }
70f754f88fSGreg Clayton 
71f754f88fSGreg Clayton 
72f754f88fSGreg Clayton ObjectFile *
735ce9c565SGreg Clayton ObjectFilePECOFF::CreateInstance (const lldb::ModuleSP &module_sp,
745ce9c565SGreg Clayton                                   DataBufferSP& data_sp,
755ce9c565SGreg Clayton                                   lldb::offset_t data_offset,
765ce9c565SGreg Clayton                                   const lldb_private::FileSpec* file,
775ce9c565SGreg Clayton                                   lldb::offset_t file_offset,
785ce9c565SGreg Clayton                                   lldb::offset_t length)
79f754f88fSGreg Clayton {
805ce9c565SGreg Clayton     if (!data_sp)
81f754f88fSGreg Clayton     {
82736888c8SGreg Clayton         data_sp = file->MemoryMapFileContentsIfLocal(file_offset, length);
835ce9c565SGreg Clayton         data_offset = 0;
845ce9c565SGreg Clayton     }
855ce9c565SGreg Clayton 
865ce9c565SGreg Clayton     if (ObjectFilePECOFF::MagicBytesMatch(data_sp))
875ce9c565SGreg Clayton     {
885ce9c565SGreg Clayton         // Update the data to contain the entire file if it doesn't already
895ce9c565SGreg Clayton         if (data_sp->GetByteSize() < length)
90736888c8SGreg Clayton             data_sp = file->MemoryMapFileContentsIfLocal(file_offset, length);
917b0992d9SGreg Clayton         std::unique_ptr<ObjectFile> objfile_ap(new ObjectFilePECOFF (module_sp, data_sp, data_offset, file, file_offset, length));
92f754f88fSGreg Clayton         if (objfile_ap.get() && objfile_ap->ParseHeader())
93f754f88fSGreg Clayton             return objfile_ap.release();
94f754f88fSGreg Clayton     }
95f754f88fSGreg Clayton     return NULL;
96f754f88fSGreg Clayton }
97f754f88fSGreg Clayton 
98c9660546SGreg Clayton ObjectFile *
99e72dfb32SGreg Clayton ObjectFilePECOFF::CreateMemoryInstance (const lldb::ModuleSP &module_sp,
100c9660546SGreg Clayton                                         lldb::DataBufferSP& data_sp,
101c9660546SGreg Clayton                                         const lldb::ProcessSP &process_sp,
102c9660546SGreg Clayton                                         lldb::addr_t header_addr)
103c9660546SGreg Clayton {
104c9660546SGreg Clayton     return NULL;
105c9660546SGreg Clayton }
106c9660546SGreg Clayton 
107f4d6de6aSGreg Clayton size_t
108f4d6de6aSGreg Clayton ObjectFilePECOFF::GetModuleSpecifications (const lldb_private::FileSpec& file,
109f4d6de6aSGreg Clayton                                            lldb::DataBufferSP& data_sp,
110f4d6de6aSGreg Clayton                                            lldb::offset_t data_offset,
111f4d6de6aSGreg Clayton                                            lldb::offset_t file_offset,
112f4d6de6aSGreg Clayton                                            lldb::offset_t length,
113f4d6de6aSGreg Clayton                                            lldb_private::ModuleSpecList &specs)
114f4d6de6aSGreg Clayton {
11589eb1baeSVirgile Bello     const size_t initial_count = specs.GetSize();
11689eb1baeSVirgile Bello 
11789eb1baeSVirgile Bello     if (ObjectFilePECOFF::MagicBytesMatch(data_sp))
11889eb1baeSVirgile Bello     {
11989eb1baeSVirgile Bello         DataExtractor data;
12089eb1baeSVirgile Bello         data.SetData(data_sp, data_offset, length);
12189eb1baeSVirgile Bello         data.SetByteOrder(eByteOrderLittle);
12289eb1baeSVirgile Bello 
12389eb1baeSVirgile Bello         dos_header_t dos_header;
12489eb1baeSVirgile Bello         coff_header_t coff_header;
12589eb1baeSVirgile Bello 
12689eb1baeSVirgile Bello         if (ParseDOSHeader(data, dos_header))
12789eb1baeSVirgile Bello         {
12889eb1baeSVirgile Bello             lldb::offset_t offset = dos_header.e_lfanew;
12989eb1baeSVirgile Bello             uint32_t pe_signature = data.GetU32(&offset);
13089eb1baeSVirgile Bello             if (pe_signature != IMAGE_NT_SIGNATURE)
13189eb1baeSVirgile Bello                 return false;
13289eb1baeSVirgile Bello             if (ParseCOFFHeader(data, &offset, coff_header))
13389eb1baeSVirgile Bello             {
13489eb1baeSVirgile Bello                 ArchSpec spec;
135ad587ae4SZachary Turner                 if (coff_header.machine == MachineAmd64)
1365e6f4520SZachary Turner                 {
137ad587ae4SZachary Turner                     spec.SetTriple("x86_64-pc-windows");
1385e6f4520SZachary Turner                     specs.Append(ModuleSpec(file, spec));
1395e6f4520SZachary Turner                 }
140ad587ae4SZachary Turner                 else if (coff_header.machine == MachineX86)
1415e6f4520SZachary Turner                 {
142ad587ae4SZachary Turner                     spec.SetTriple("i386-pc-windows");
14389eb1baeSVirgile Bello                     specs.Append(ModuleSpec(file, spec));
1445e6f4520SZachary Turner                     spec.SetTriple("i686-pc-windows");
1455e6f4520SZachary Turner                     specs.Append(ModuleSpec(file, spec));
1465e6f4520SZachary Turner                 }
14789eb1baeSVirgile Bello             }
14889eb1baeSVirgile Bello         }
14989eb1baeSVirgile Bello     }
15089eb1baeSVirgile Bello 
15189eb1baeSVirgile Bello     return specs.GetSize() - initial_count;
152f4d6de6aSGreg Clayton }
153f4d6de6aSGreg Clayton 
154f7d1893fSAdrian McCarthy bool
155f7d1893fSAdrian McCarthy ObjectFilePECOFF::SaveCore(const lldb::ProcessSP &process_sp,
156f7d1893fSAdrian McCarthy                            const lldb_private::FileSpec &outfile,
157f7d1893fSAdrian McCarthy                            lldb_private::Error &error)
158f7d1893fSAdrian McCarthy {
159f7d1893fSAdrian McCarthy     return SaveMiniDump(process_sp, outfile, error);
160f7d1893fSAdrian McCarthy }
161f7d1893fSAdrian McCarthy 
162f4d6de6aSGreg Clayton 
163f754f88fSGreg Clayton bool
1645ce9c565SGreg Clayton ObjectFilePECOFF::MagicBytesMatch (DataBufferSP& data_sp)
165f754f88fSGreg Clayton {
1665ce9c565SGreg Clayton     DataExtractor data(data_sp, eByteOrderLittle, 4);
167c7bece56SGreg Clayton     lldb::offset_t offset = 0;
168f754f88fSGreg Clayton     uint16_t magic = data.GetU16 (&offset);
169f754f88fSGreg Clayton     return magic == IMAGE_DOS_SIGNATURE;
170f754f88fSGreg Clayton }
171f754f88fSGreg Clayton 
172c35b91ceSAdrian McCarthy lldb::SymbolType
173c35b91ceSAdrian McCarthy ObjectFilePECOFF::MapSymbolType(uint16_t coff_symbol_type)
174c35b91ceSAdrian McCarthy {
175c35b91ceSAdrian McCarthy     // TODO:  We need to complete this mapping of COFF symbol types to LLDB ones.
176c35b91ceSAdrian McCarthy     // For now, here's a hack to make sure our function have types.
177c35b91ceSAdrian McCarthy     const auto complex_type = coff_symbol_type >> llvm::COFF::SCT_COMPLEX_TYPE_SHIFT;
178c35b91ceSAdrian McCarthy     if (complex_type == llvm::COFF::IMAGE_SYM_DTYPE_FUNCTION)
179c35b91ceSAdrian McCarthy     {
180c35b91ceSAdrian McCarthy         return lldb::eSymbolTypeCode;
181c35b91ceSAdrian McCarthy     }
182c35b91ceSAdrian McCarthy     return lldb::eSymbolTypeInvalid;
183c35b91ceSAdrian McCarthy }
184f754f88fSGreg Clayton 
185e72dfb32SGreg Clayton ObjectFilePECOFF::ObjectFilePECOFF (const lldb::ModuleSP &module_sp,
1865ce9c565SGreg Clayton                                     DataBufferSP& data_sp,
1875ce9c565SGreg Clayton                                     lldb::offset_t data_offset,
188f754f88fSGreg Clayton                                     const FileSpec* file,
1895ce9c565SGreg Clayton                                     lldb::offset_t file_offset,
1905ce9c565SGreg Clayton                                     lldb::offset_t length) :
1915ce9c565SGreg Clayton     ObjectFile (module_sp, file, file_offset, length, data_sp, data_offset),
192f754f88fSGreg Clayton     m_dos_header (),
193f754f88fSGreg Clayton     m_coff_header (),
194f754f88fSGreg Clayton     m_coff_header_opt (),
1958e38c666SStephane Sezer     m_sect_headers (),
1968e38c666SStephane Sezer     m_entry_point_address ()
197f754f88fSGreg Clayton {
198f754f88fSGreg Clayton     ::memset (&m_dos_header, 0, sizeof(m_dos_header));
199f754f88fSGreg Clayton     ::memset (&m_coff_header, 0, sizeof(m_coff_header));
200f754f88fSGreg Clayton     ::memset (&m_coff_header_opt, 0, sizeof(m_coff_header_opt));
201f754f88fSGreg Clayton }
202f754f88fSGreg Clayton 
203f754f88fSGreg Clayton 
204f754f88fSGreg Clayton ObjectFilePECOFF::~ObjectFilePECOFF()
205f754f88fSGreg Clayton {
206f754f88fSGreg Clayton }
207f754f88fSGreg Clayton 
208f754f88fSGreg Clayton 
209f754f88fSGreg Clayton bool
210f754f88fSGreg Clayton ObjectFilePECOFF::ParseHeader ()
211f754f88fSGreg Clayton {
212a1743499SGreg Clayton     ModuleSP module_sp(GetModule());
213a1743499SGreg Clayton     if (module_sp)
214a1743499SGreg Clayton     {
21516ff8604SSaleem Abdulrasool         std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
216f754f88fSGreg Clayton         m_sect_headers.clear();
217f754f88fSGreg Clayton         m_data.SetByteOrder (eByteOrderLittle);
218c7bece56SGreg Clayton         lldb::offset_t offset = 0;
219f754f88fSGreg Clayton 
22089eb1baeSVirgile Bello         if (ParseDOSHeader(m_data, m_dos_header))
221f754f88fSGreg Clayton         {
222f754f88fSGreg Clayton             offset = m_dos_header.e_lfanew;
223f754f88fSGreg Clayton             uint32_t pe_signature = m_data.GetU32 (&offset);
224f754f88fSGreg Clayton             if (pe_signature != IMAGE_NT_SIGNATURE)
225f754f88fSGreg Clayton                 return false;
22689eb1baeSVirgile Bello             if (ParseCOFFHeader(m_data, &offset, m_coff_header))
227f754f88fSGreg Clayton             {
228f754f88fSGreg Clayton                 if (m_coff_header.hdrsize > 0)
229f754f88fSGreg Clayton                     ParseCOFFOptionalHeader(&offset);
230f754f88fSGreg Clayton                 ParseSectionHeaders (offset);
23128469ca3SGreg Clayton             }
232f754f88fSGreg Clayton             return true;
233f754f88fSGreg Clayton         }
234a1743499SGreg Clayton     }
235f754f88fSGreg Clayton     return false;
236f754f88fSGreg Clayton }
237f754f88fSGreg Clayton 
2382756adf3SVirgile Bello bool
2392756adf3SVirgile Bello ObjectFilePECOFF::SetLoadAddress(Target &target, addr_t value, bool value_is_offset)
2402756adf3SVirgile Bello {
2412756adf3SVirgile Bello     bool changed = false;
2422756adf3SVirgile Bello     ModuleSP module_sp = GetModule();
2432756adf3SVirgile Bello     if (module_sp)
2442756adf3SVirgile Bello     {
2452756adf3SVirgile Bello         size_t num_loaded_sections = 0;
2462756adf3SVirgile Bello         SectionList *section_list = GetSectionList ();
2472756adf3SVirgile Bello         if (section_list)
2482756adf3SVirgile Bello         {
2492756adf3SVirgile Bello             if (!value_is_offset)
2502756adf3SVirgile Bello             {
2512756adf3SVirgile Bello                 value -= m_image_base;
2522756adf3SVirgile Bello             }
2532756adf3SVirgile Bello 
2542756adf3SVirgile Bello             const size_t num_sections = section_list->GetSize();
2552756adf3SVirgile Bello             size_t sect_idx = 0;
2562756adf3SVirgile Bello 
2572756adf3SVirgile Bello             for (sect_idx = 0; sect_idx < num_sections; ++sect_idx)
2582756adf3SVirgile Bello             {
2592756adf3SVirgile Bello                 // Iterate through the object file sections to find all
2602756adf3SVirgile Bello                 // of the sections that have SHF_ALLOC in their flag bits.
2612756adf3SVirgile Bello                 SectionSP section_sp (section_list->GetSectionAtIndex (sect_idx));
2622756adf3SVirgile Bello                 if (section_sp && !section_sp->IsThreadSpecific())
2632756adf3SVirgile Bello                 {
2642756adf3SVirgile Bello                     if (target.GetSectionLoadList().SetSectionLoadAddress (section_sp, section_sp->GetFileAddress() + value))
2652756adf3SVirgile Bello                         ++num_loaded_sections;
2662756adf3SVirgile Bello                 }
2672756adf3SVirgile Bello             }
2682756adf3SVirgile Bello             changed = num_loaded_sections > 0;
2692756adf3SVirgile Bello         }
2702756adf3SVirgile Bello     }
2712756adf3SVirgile Bello     return changed;
2722756adf3SVirgile Bello }
2732756adf3SVirgile Bello 
274f754f88fSGreg Clayton 
275f754f88fSGreg Clayton ByteOrder
276f754f88fSGreg Clayton ObjectFilePECOFF::GetByteOrder () const
277f754f88fSGreg Clayton {
278f754f88fSGreg Clayton     return eByteOrderLittle;
279f754f88fSGreg Clayton }
280f754f88fSGreg Clayton 
281f754f88fSGreg Clayton bool
282f754f88fSGreg Clayton ObjectFilePECOFF::IsExecutable() const
283f754f88fSGreg Clayton {
284237ad974SCharles Davis     return (m_coff_header.flags & llvm::COFF::IMAGE_FILE_DLL) == 0;
285f754f88fSGreg Clayton }
286f754f88fSGreg Clayton 
287c7bece56SGreg Clayton uint32_t
288f754f88fSGreg Clayton ObjectFilePECOFF::GetAddressByteSize () const
289f754f88fSGreg Clayton {
290f754f88fSGreg Clayton     if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32_PLUS)
291f754f88fSGreg Clayton         return 8;
292f754f88fSGreg Clayton     else if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32)
293f754f88fSGreg Clayton         return 4;
294f754f88fSGreg Clayton     return 4;
295f754f88fSGreg Clayton }
296f754f88fSGreg Clayton 
297f754f88fSGreg Clayton //----------------------------------------------------------------------
298f754f88fSGreg Clayton // NeedsEndianSwap
299f754f88fSGreg Clayton //
300f754f88fSGreg Clayton // Return true if an endian swap needs to occur when extracting data
301f754f88fSGreg Clayton // from this file.
302f754f88fSGreg Clayton //----------------------------------------------------------------------
303f754f88fSGreg Clayton bool
304f754f88fSGreg Clayton ObjectFilePECOFF::NeedsEndianSwap() const
305f754f88fSGreg Clayton {
306f754f88fSGreg Clayton #if defined(__LITTLE_ENDIAN__)
307f754f88fSGreg Clayton     return false;
308f754f88fSGreg Clayton #else
309f754f88fSGreg Clayton     return true;
310f754f88fSGreg Clayton #endif
311f754f88fSGreg Clayton }
312f754f88fSGreg Clayton //----------------------------------------------------------------------
313f754f88fSGreg Clayton // ParseDOSHeader
314f754f88fSGreg Clayton //----------------------------------------------------------------------
315f754f88fSGreg Clayton bool
31689eb1baeSVirgile Bello ObjectFilePECOFF::ParseDOSHeader (DataExtractor &data, dos_header_t &dos_header)
317f754f88fSGreg Clayton {
318f754f88fSGreg Clayton     bool success = false;
319c7bece56SGreg Clayton     lldb::offset_t offset = 0;
32089eb1baeSVirgile Bello     success = data.ValidOffsetForDataOfSize(0, sizeof(dos_header));
321f754f88fSGreg Clayton 
322f754f88fSGreg Clayton     if (success)
323f754f88fSGreg Clayton     {
32489eb1baeSVirgile Bello         dos_header.e_magic = data.GetU16(&offset); // Magic number
32589eb1baeSVirgile Bello         success = dos_header.e_magic == IMAGE_DOS_SIGNATURE;
326f754f88fSGreg Clayton 
327f754f88fSGreg Clayton         if (success)
328f754f88fSGreg Clayton         {
32989eb1baeSVirgile Bello             dos_header.e_cblp     = data.GetU16(&offset); // Bytes on last page of file
33089eb1baeSVirgile Bello             dos_header.e_cp       = data.GetU16(&offset); // Pages in file
33189eb1baeSVirgile Bello             dos_header.e_crlc     = data.GetU16(&offset); // Relocations
33289eb1baeSVirgile Bello             dos_header.e_cparhdr  = data.GetU16(&offset); // Size of header in paragraphs
33389eb1baeSVirgile Bello             dos_header.e_minalloc = data.GetU16(&offset); // Minimum extra paragraphs needed
33489eb1baeSVirgile Bello             dos_header.e_maxalloc = data.GetU16(&offset); // Maximum extra paragraphs needed
33589eb1baeSVirgile Bello             dos_header.e_ss       = data.GetU16(&offset); // Initial (relative) SS value
33689eb1baeSVirgile Bello             dos_header.e_sp       = data.GetU16(&offset); // Initial SP value
33789eb1baeSVirgile Bello             dos_header.e_csum     = data.GetU16(&offset); // Checksum
33889eb1baeSVirgile Bello             dos_header.e_ip       = data.GetU16(&offset); // Initial IP value
33989eb1baeSVirgile Bello             dos_header.e_cs       = data.GetU16(&offset); // Initial (relative) CS value
34089eb1baeSVirgile Bello             dos_header.e_lfarlc   = data.GetU16(&offset); // File address of relocation table
34189eb1baeSVirgile Bello             dos_header.e_ovno     = data.GetU16(&offset); // Overlay number
342f754f88fSGreg Clayton 
34389eb1baeSVirgile Bello             dos_header.e_res[0]   = data.GetU16(&offset); // Reserved words
34489eb1baeSVirgile Bello             dos_header.e_res[1]   = data.GetU16(&offset); // Reserved words
34589eb1baeSVirgile Bello             dos_header.e_res[2]   = data.GetU16(&offset); // Reserved words
34689eb1baeSVirgile Bello             dos_header.e_res[3]   = data.GetU16(&offset); // Reserved words
347f754f88fSGreg Clayton 
34889eb1baeSVirgile Bello             dos_header.e_oemid    = data.GetU16(&offset); // OEM identifier (for e_oeminfo)
34989eb1baeSVirgile Bello             dos_header.e_oeminfo  = data.GetU16(&offset); // OEM information; e_oemid specific
35089eb1baeSVirgile Bello             dos_header.e_res2[0]  = data.GetU16(&offset); // Reserved words
35189eb1baeSVirgile Bello             dos_header.e_res2[1]  = data.GetU16(&offset); // Reserved words
35289eb1baeSVirgile Bello             dos_header.e_res2[2]  = data.GetU16(&offset); // Reserved words
35389eb1baeSVirgile Bello             dos_header.e_res2[3]  = data.GetU16(&offset); // Reserved words
35489eb1baeSVirgile Bello             dos_header.e_res2[4]  = data.GetU16(&offset); // Reserved words
35589eb1baeSVirgile Bello             dos_header.e_res2[5]  = data.GetU16(&offset); // Reserved words
35689eb1baeSVirgile Bello             dos_header.e_res2[6]  = data.GetU16(&offset); // Reserved words
35789eb1baeSVirgile Bello             dos_header.e_res2[7]  = data.GetU16(&offset); // Reserved words
35889eb1baeSVirgile Bello             dos_header.e_res2[8]  = data.GetU16(&offset); // Reserved words
35989eb1baeSVirgile Bello             dos_header.e_res2[9]  = data.GetU16(&offset); // Reserved words
360f754f88fSGreg Clayton 
36189eb1baeSVirgile Bello             dos_header.e_lfanew   = data.GetU32(&offset); // File address of new exe header
362f754f88fSGreg Clayton         }
363f754f88fSGreg Clayton     }
364f754f88fSGreg Clayton     if (!success)
36589eb1baeSVirgile Bello         memset(&dos_header, 0, sizeof(dos_header));
366f754f88fSGreg Clayton     return success;
367f754f88fSGreg Clayton }
368f754f88fSGreg Clayton 
369f754f88fSGreg Clayton 
370f754f88fSGreg Clayton //----------------------------------------------------------------------
371f754f88fSGreg Clayton // ParserCOFFHeader
372f754f88fSGreg Clayton //----------------------------------------------------------------------
373f754f88fSGreg Clayton bool
37489eb1baeSVirgile Bello ObjectFilePECOFF::ParseCOFFHeader(DataExtractor &data, lldb::offset_t *offset_ptr, coff_header_t &coff_header)
375f754f88fSGreg Clayton {
37689eb1baeSVirgile Bello     bool success = data.ValidOffsetForDataOfSize (*offset_ptr, sizeof(coff_header));
377f754f88fSGreg Clayton     if (success)
378f754f88fSGreg Clayton     {
37989eb1baeSVirgile Bello         coff_header.machine   = data.GetU16(offset_ptr);
38089eb1baeSVirgile Bello         coff_header.nsects    = data.GetU16(offset_ptr);
38189eb1baeSVirgile Bello         coff_header.modtime   = data.GetU32(offset_ptr);
38289eb1baeSVirgile Bello         coff_header.symoff    = data.GetU32(offset_ptr);
38389eb1baeSVirgile Bello         coff_header.nsyms     = data.GetU32(offset_ptr);
38489eb1baeSVirgile Bello         coff_header.hdrsize   = data.GetU16(offset_ptr);
38589eb1baeSVirgile Bello         coff_header.flags     = data.GetU16(offset_ptr);
386f754f88fSGreg Clayton     }
387f754f88fSGreg Clayton     if (!success)
38889eb1baeSVirgile Bello         memset(&coff_header, 0, sizeof(coff_header));
389f754f88fSGreg Clayton     return success;
390f754f88fSGreg Clayton }
391f754f88fSGreg Clayton 
392f754f88fSGreg Clayton bool
393c7bece56SGreg Clayton ObjectFilePECOFF::ParseCOFFOptionalHeader(lldb::offset_t *offset_ptr)
394f754f88fSGreg Clayton {
395f754f88fSGreg Clayton     bool success = false;
396c7bece56SGreg Clayton     const lldb::offset_t end_offset = *offset_ptr + m_coff_header.hdrsize;
397f754f88fSGreg Clayton     if (*offset_ptr < end_offset)
398f754f88fSGreg Clayton     {
399f754f88fSGreg Clayton         success = true;
400f754f88fSGreg Clayton         m_coff_header_opt.magic                         = m_data.GetU16(offset_ptr);
401f754f88fSGreg Clayton         m_coff_header_opt.major_linker_version          = m_data.GetU8 (offset_ptr);
402f754f88fSGreg Clayton         m_coff_header_opt.minor_linker_version          = m_data.GetU8 (offset_ptr);
403f754f88fSGreg Clayton         m_coff_header_opt.code_size                     = m_data.GetU32(offset_ptr);
404f754f88fSGreg Clayton         m_coff_header_opt.data_size                     = m_data.GetU32(offset_ptr);
405f754f88fSGreg Clayton         m_coff_header_opt.bss_size                      = m_data.GetU32(offset_ptr);
406f754f88fSGreg Clayton         m_coff_header_opt.entry                         = m_data.GetU32(offset_ptr);
407f754f88fSGreg Clayton         m_coff_header_opt.code_offset                   = m_data.GetU32(offset_ptr);
408f754f88fSGreg Clayton 
409f754f88fSGreg Clayton         const uint32_t addr_byte_size = GetAddressByteSize ();
410f754f88fSGreg Clayton 
411f754f88fSGreg Clayton         if (*offset_ptr < end_offset)
412f754f88fSGreg Clayton         {
413f754f88fSGreg Clayton             if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32)
414f754f88fSGreg Clayton             {
415f754f88fSGreg Clayton                 // PE32 only
416f754f88fSGreg Clayton                 m_coff_header_opt.data_offset               = m_data.GetU32(offset_ptr);
417f754f88fSGreg Clayton             }
418f754f88fSGreg Clayton             else
419f754f88fSGreg Clayton                 m_coff_header_opt.data_offset = 0;
420f754f88fSGreg Clayton 
421f754f88fSGreg Clayton             if (*offset_ptr < end_offset)
422f754f88fSGreg Clayton             {
423f754f88fSGreg Clayton                 m_coff_header_opt.image_base                    = m_data.GetMaxU64 (offset_ptr, addr_byte_size);
424f754f88fSGreg Clayton                 m_coff_header_opt.sect_alignment                = m_data.GetU32(offset_ptr);
425f754f88fSGreg Clayton                 m_coff_header_opt.file_alignment                = m_data.GetU32(offset_ptr);
426f754f88fSGreg Clayton                 m_coff_header_opt.major_os_system_version       = m_data.GetU16(offset_ptr);
427f754f88fSGreg Clayton                 m_coff_header_opt.minor_os_system_version       = m_data.GetU16(offset_ptr);
428f754f88fSGreg Clayton                 m_coff_header_opt.major_image_version           = m_data.GetU16(offset_ptr);
429f754f88fSGreg Clayton                 m_coff_header_opt.minor_image_version           = m_data.GetU16(offset_ptr);
430f754f88fSGreg Clayton                 m_coff_header_opt.major_subsystem_version       = m_data.GetU16(offset_ptr);
431f754f88fSGreg Clayton                 m_coff_header_opt.minor_subsystem_version       = m_data.GetU16(offset_ptr);
432f754f88fSGreg Clayton                 m_coff_header_opt.reserved1                     = m_data.GetU32(offset_ptr);
433f754f88fSGreg Clayton                 m_coff_header_opt.image_size                    = m_data.GetU32(offset_ptr);
434f754f88fSGreg Clayton                 m_coff_header_opt.header_size                   = m_data.GetU32(offset_ptr);
43528469ca3SGreg Clayton                 m_coff_header_opt.checksum                      = m_data.GetU32(offset_ptr);
436f754f88fSGreg Clayton                 m_coff_header_opt.subsystem                     = m_data.GetU16(offset_ptr);
437f754f88fSGreg Clayton                 m_coff_header_opt.dll_flags                     = m_data.GetU16(offset_ptr);
438f754f88fSGreg Clayton                 m_coff_header_opt.stack_reserve_size            = m_data.GetMaxU64 (offset_ptr, addr_byte_size);
439f754f88fSGreg Clayton                 m_coff_header_opt.stack_commit_size             = m_data.GetMaxU64 (offset_ptr, addr_byte_size);
440f754f88fSGreg Clayton                 m_coff_header_opt.heap_reserve_size             = m_data.GetMaxU64 (offset_ptr, addr_byte_size);
441f754f88fSGreg Clayton                 m_coff_header_opt.heap_commit_size              = m_data.GetMaxU64 (offset_ptr, addr_byte_size);
442f754f88fSGreg Clayton                 m_coff_header_opt.loader_flags                  = m_data.GetU32(offset_ptr);
443f754f88fSGreg Clayton                 uint32_t num_data_dir_entries = m_data.GetU32(offset_ptr);
444f754f88fSGreg Clayton                 m_coff_header_opt.data_dirs.clear();
445f754f88fSGreg Clayton                 m_coff_header_opt.data_dirs.resize(num_data_dir_entries);
446f754f88fSGreg Clayton                 uint32_t i;
447f754f88fSGreg Clayton                 for (i=0; i<num_data_dir_entries; i++)
448f754f88fSGreg Clayton                 {
449f754f88fSGreg Clayton                     m_coff_header_opt.data_dirs[i].vmaddr = m_data.GetU32(offset_ptr);
450f754f88fSGreg Clayton                     m_coff_header_opt.data_dirs[i].vmsize = m_data.GetU32(offset_ptr);
451f754f88fSGreg Clayton                 }
4522756adf3SVirgile Bello 
4532756adf3SVirgile Bello                 m_file_offset = m_coff_header_opt.image_base;
4542756adf3SVirgile Bello                 m_image_base = m_coff_header_opt.image_base;
455f754f88fSGreg Clayton             }
456f754f88fSGreg Clayton         }
457f754f88fSGreg Clayton     }
458f754f88fSGreg Clayton     // Make sure we are on track for section data which follows
459f754f88fSGreg Clayton     *offset_ptr = end_offset;
460f754f88fSGreg Clayton     return success;
461f754f88fSGreg Clayton }
462f754f88fSGreg Clayton 
463f754f88fSGreg Clayton 
464f754f88fSGreg Clayton //----------------------------------------------------------------------
465f754f88fSGreg Clayton // ParseSectionHeaders
466f754f88fSGreg Clayton //----------------------------------------------------------------------
467f754f88fSGreg Clayton bool
468f754f88fSGreg Clayton ObjectFilePECOFF::ParseSectionHeaders (uint32_t section_header_data_offset)
469f754f88fSGreg Clayton {
470f754f88fSGreg Clayton     const uint32_t nsects = m_coff_header.nsects;
471f754f88fSGreg Clayton     m_sect_headers.clear();
472f754f88fSGreg Clayton 
473f754f88fSGreg Clayton     if (nsects > 0)
474f754f88fSGreg Clayton     {
475f754f88fSGreg Clayton         const uint32_t addr_byte_size = GetAddressByteSize ();
476f754f88fSGreg Clayton         const size_t section_header_byte_size = nsects * sizeof(section_header_t);
477f754f88fSGreg Clayton         DataBufferSP section_header_data_sp(m_file.ReadFileContents (section_header_data_offset, section_header_byte_size));
478f754f88fSGreg Clayton         DataExtractor section_header_data (section_header_data_sp, GetByteOrder(), addr_byte_size);
479f754f88fSGreg Clayton 
480c7bece56SGreg Clayton         lldb::offset_t offset = 0;
481f754f88fSGreg Clayton         if (section_header_data.ValidOffsetForDataOfSize (offset, section_header_byte_size))
482f754f88fSGreg Clayton         {
483f754f88fSGreg Clayton             m_sect_headers.resize(nsects);
484f754f88fSGreg Clayton 
485f754f88fSGreg Clayton             for (uint32_t idx = 0; idx<nsects; ++idx)
486f754f88fSGreg Clayton             {
487f754f88fSGreg Clayton                 const void *name_data = section_header_data.GetData(&offset, 8);
488f754f88fSGreg Clayton                 if (name_data)
489f754f88fSGreg Clayton                 {
490f754f88fSGreg Clayton                     memcpy(m_sect_headers[idx].name, name_data, 8);
491f754f88fSGreg Clayton                     m_sect_headers[idx].vmsize  = section_header_data.GetU32(&offset);
492f754f88fSGreg Clayton                     m_sect_headers[idx].vmaddr  = section_header_data.GetU32(&offset);
493f754f88fSGreg Clayton                     m_sect_headers[idx].size    = section_header_data.GetU32(&offset);
494f754f88fSGreg Clayton                     m_sect_headers[idx].offset  = section_header_data.GetU32(&offset);
495f754f88fSGreg Clayton                     m_sect_headers[idx].reloff  = section_header_data.GetU32(&offset);
496f754f88fSGreg Clayton                     m_sect_headers[idx].lineoff = section_header_data.GetU32(&offset);
497f754f88fSGreg Clayton                     m_sect_headers[idx].nreloc  = section_header_data.GetU16(&offset);
498f754f88fSGreg Clayton                     m_sect_headers[idx].nline   = section_header_data.GetU16(&offset);
499f754f88fSGreg Clayton                     m_sect_headers[idx].flags   = section_header_data.GetU32(&offset);
500f754f88fSGreg Clayton                 }
501f754f88fSGreg Clayton             }
502f754f88fSGreg Clayton         }
503f754f88fSGreg Clayton     }
504f754f88fSGreg Clayton 
505f754f88fSGreg Clayton     return m_sect_headers.empty() == false;
506f754f88fSGreg Clayton }
507f754f88fSGreg Clayton 
508f754f88fSGreg Clayton bool
509f754f88fSGreg Clayton ObjectFilePECOFF::GetSectionName(std::string& sect_name, const section_header_t& sect)
510f754f88fSGreg Clayton {
511f754f88fSGreg Clayton     if (sect.name[0] == '/')
512f754f88fSGreg Clayton     {
513c7bece56SGreg Clayton         lldb::offset_t stroff = strtoul(&sect.name[1], NULL, 10);
514c7bece56SGreg Clayton         lldb::offset_t string_file_offset = m_coff_header.symoff + (m_coff_header.nsyms * 18) + stroff;
515f754f88fSGreg Clayton         const char *name = m_data.GetCStr (&string_file_offset);
516f754f88fSGreg Clayton         if (name)
517f754f88fSGreg Clayton         {
518f754f88fSGreg Clayton             sect_name = name;
519f754f88fSGreg Clayton             return true;
520f754f88fSGreg Clayton         }
521f754f88fSGreg Clayton 
522f754f88fSGreg Clayton         return false;
523f754f88fSGreg Clayton     }
524f754f88fSGreg Clayton     sect_name = sect.name;
525f754f88fSGreg Clayton     return true;
526f754f88fSGreg Clayton }
527f754f88fSGreg Clayton 
528f754f88fSGreg Clayton //----------------------------------------------------------------------
529f754f88fSGreg Clayton // GetNListSymtab
530f754f88fSGreg Clayton //----------------------------------------------------------------------
531f754f88fSGreg Clayton Symtab *
5323046e668SGreg Clayton ObjectFilePECOFF::GetSymtab()
533f754f88fSGreg Clayton {
534a1743499SGreg Clayton     ModuleSP module_sp(GetModule());
535a1743499SGreg Clayton     if (module_sp)
536a1743499SGreg Clayton     {
53716ff8604SSaleem Abdulrasool         std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
538f754f88fSGreg Clayton         if (m_symtab_ap.get() == NULL)
539f754f88fSGreg Clayton         {
540f754f88fSGreg Clayton             SectionList *sect_list = GetSectionList();
541f754f88fSGreg Clayton             m_symtab_ap.reset(new Symtab(this));
542*bb19a13cSSaleem Abdulrasool             std::lock_guard<std::recursive_mutex> guard(m_symtab_ap->GetMutex());
54328469ca3SGreg Clayton 
54428469ca3SGreg Clayton             const uint32_t num_syms = m_coff_header.nsyms;
54528469ca3SGreg Clayton 
54628469ca3SGreg Clayton             if (num_syms > 0 && m_coff_header.symoff > 0)
547f754f88fSGreg Clayton             {
5480076e715SGreg Clayton                 const uint32_t symbol_size = 18;
54928469ca3SGreg Clayton                 const uint32_t addr_byte_size = GetAddressByteSize ();
55028469ca3SGreg Clayton                 const size_t symbol_data_size = num_syms * symbol_size;
551c35b91ceSAdrian McCarthy                 // Include the 4-byte string table size at the end of the symbols
55228469ca3SGreg Clayton                 DataBufferSP symtab_data_sp(m_file.ReadFileContents (m_coff_header.symoff, symbol_data_size + 4));
55328469ca3SGreg Clayton                 DataExtractor symtab_data (symtab_data_sp, GetByteOrder(), addr_byte_size);
554c7bece56SGreg Clayton                 lldb::offset_t offset = symbol_data_size;
55528469ca3SGreg Clayton                 const uint32_t strtab_size = symtab_data.GetU32 (&offset);
5560076e715SGreg Clayton                 DataBufferSP strtab_data_sp(m_file.ReadFileContents (m_coff_header.symoff + symbol_data_size, strtab_size));
55728469ca3SGreg Clayton                 DataExtractor strtab_data (strtab_data_sp, GetByteOrder(), addr_byte_size);
55828469ca3SGreg Clayton 
5590076e715SGreg Clayton                 // First 4 bytes should be zeroed after strtab_size has been read,
5600076e715SGreg Clayton                 // because it is used as offset 0 to encode a NULL string.
5610076e715SGreg Clayton                 uint32_t* strtab_data_start = (uint32_t*)strtab_data_sp->GetBytes();
5620076e715SGreg Clayton                 strtab_data_start[0] = 0;
5630076e715SGreg Clayton 
56428469ca3SGreg Clayton                 offset = 0;
56528469ca3SGreg Clayton                 std::string symbol_name;
566f754f88fSGreg Clayton                 Symbol *symbols = m_symtab_ap->Resize (num_syms);
567f754f88fSGreg Clayton                 for (uint32_t i=0; i<num_syms; ++i)
568f754f88fSGreg Clayton                 {
569f754f88fSGreg Clayton                     coff_symbol_t symbol;
57028469ca3SGreg Clayton                     const uint32_t symbol_offset = offset;
57128469ca3SGreg Clayton                     const char *symbol_name_cstr = NULL;
572c35b91ceSAdrian McCarthy                     // If the first 4 bytes of the symbol string are zero, then they
573c35b91ceSAdrian McCarthy                     // are followed by a 4-byte string table offset. Else these
57428469ca3SGreg Clayton                     // 8 bytes contain the symbol name
57528469ca3SGreg Clayton                     if (symtab_data.GetU32 (&offset) == 0)
57628469ca3SGreg Clayton                     {
57728469ca3SGreg Clayton                         // Long string that doesn't fit into the symbol table name,
57828469ca3SGreg Clayton                         // so now we must read the 4 byte string table offset
57928469ca3SGreg Clayton                         uint32_t strtab_offset = symtab_data.GetU32 (&offset);
58028469ca3SGreg Clayton                         symbol_name_cstr = strtab_data.PeekCStr (strtab_offset);
58128469ca3SGreg Clayton                         symbol_name.assign (symbol_name_cstr);
58228469ca3SGreg Clayton                     }
58328469ca3SGreg Clayton                     else
58428469ca3SGreg Clayton                     {
58528469ca3SGreg Clayton                         // Short string that fits into the symbol table name which is 8 bytes
58628469ca3SGreg Clayton                         offset += sizeof(symbol.name) - 4; // Skip remaining
58728469ca3SGreg Clayton                         symbol_name_cstr = symtab_data.PeekCStr (symbol_offset);
58828469ca3SGreg Clayton                         if (symbol_name_cstr == NULL)
589f754f88fSGreg Clayton                             break;
59028469ca3SGreg Clayton                         symbol_name.assign (symbol_name_cstr, sizeof(symbol.name));
59128469ca3SGreg Clayton                     }
59228469ca3SGreg Clayton                     symbol.value    = symtab_data.GetU32 (&offset);
59328469ca3SGreg Clayton                     symbol.sect     = symtab_data.GetU16 (&offset);
59428469ca3SGreg Clayton                     symbol.type     = symtab_data.GetU16 (&offset);
59528469ca3SGreg Clayton                     symbol.storage  = symtab_data.GetU8  (&offset);
59628469ca3SGreg Clayton                     symbol.naux     = symtab_data.GetU8  (&offset);
597037520e9SGreg Clayton                     symbols[i].GetMangled ().SetValue (ConstString(symbol_name.c_str()));
5980076e715SGreg Clayton                     if ((int16_t)symbol.sect >= 1)
5990076e715SGreg Clayton                     {
6000076e715SGreg Clayton                         Address symbol_addr(sect_list->GetSectionAtIndex(symbol.sect-1), symbol.value);
601358cf1eaSGreg Clayton                         symbols[i].GetAddressRef() = symbol_addr;
602c35b91ceSAdrian McCarthy                         symbols[i].SetType(MapSymbolType(symbol.type));
6030076e715SGreg Clayton                     }
604f754f88fSGreg Clayton 
605f754f88fSGreg Clayton                     if (symbol.naux > 0)
6060076e715SGreg Clayton                     {
607f754f88fSGreg Clayton                         i += symbol.naux;
6080076e715SGreg Clayton                         offset += symbol_size;
6090076e715SGreg Clayton                     }
610f754f88fSGreg Clayton                 }
611f754f88fSGreg Clayton 
612f754f88fSGreg Clayton             }
613a4fe3a12SVirgile Bello 
614a4fe3a12SVirgile Bello             // Read export header
615a4fe3a12SVirgile Bello             if (coff_data_dir_export_table < m_coff_header_opt.data_dirs.size()
616a4fe3a12SVirgile Bello                 && m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmsize > 0 && m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmaddr > 0)
617a4fe3a12SVirgile Bello             {
618a4fe3a12SVirgile Bello                 export_directory_entry export_table;
619a4fe3a12SVirgile Bello                 uint32_t data_start = m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmaddr;
620a4fe3a12SVirgile Bello                 Address address(m_coff_header_opt.image_base + data_start, sect_list);
621a4fe3a12SVirgile Bello                 DataBufferSP symtab_data_sp(m_file.ReadFileContents(address.GetSection()->GetFileOffset() + address.GetOffset(), m_coff_header_opt.data_dirs[0].vmsize));
622a4fe3a12SVirgile Bello                 DataExtractor symtab_data (symtab_data_sp, GetByteOrder(), GetAddressByteSize());
623a4fe3a12SVirgile Bello                 lldb::offset_t offset = 0;
624a4fe3a12SVirgile Bello 
625a4fe3a12SVirgile Bello                 // Read export_table header
626a4fe3a12SVirgile Bello                 export_table.characteristics = symtab_data.GetU32(&offset);
627a4fe3a12SVirgile Bello                 export_table.time_date_stamp = symtab_data.GetU32(&offset);
628a4fe3a12SVirgile Bello                 export_table.major_version = symtab_data.GetU16(&offset);
629a4fe3a12SVirgile Bello                 export_table.minor_version = symtab_data.GetU16(&offset);
630a4fe3a12SVirgile Bello                 export_table.name = symtab_data.GetU32(&offset);
631a4fe3a12SVirgile Bello                 export_table.base = symtab_data.GetU32(&offset);
632a4fe3a12SVirgile Bello                 export_table.number_of_functions = symtab_data.GetU32(&offset);
633a4fe3a12SVirgile Bello                 export_table.number_of_names = symtab_data.GetU32(&offset);
634a4fe3a12SVirgile Bello                 export_table.address_of_functions = symtab_data.GetU32(&offset);
635a4fe3a12SVirgile Bello                 export_table.address_of_names = symtab_data.GetU32(&offset);
636a4fe3a12SVirgile Bello                 export_table.address_of_name_ordinals = symtab_data.GetU32(&offset);
637a4fe3a12SVirgile Bello 
638a4fe3a12SVirgile Bello                 bool has_ordinal = export_table.address_of_name_ordinals != 0;
639a4fe3a12SVirgile Bello 
640a4fe3a12SVirgile Bello                 lldb::offset_t name_offset = export_table.address_of_names - data_start;
641a4fe3a12SVirgile Bello                 lldb::offset_t name_ordinal_offset = export_table.address_of_name_ordinals - data_start;
642a4fe3a12SVirgile Bello 
643a4fe3a12SVirgile Bello                 Symbol *symbols = m_symtab_ap->Resize(export_table.number_of_names);
644a4fe3a12SVirgile Bello 
645a4fe3a12SVirgile Bello                 std::string symbol_name;
646a4fe3a12SVirgile Bello 
647a4fe3a12SVirgile Bello                 // Read each export table entry
648a4fe3a12SVirgile Bello                 for (size_t i = 0; i < export_table.number_of_names; ++i)
649a4fe3a12SVirgile Bello                 {
650a4fe3a12SVirgile Bello                     uint32_t name_ordinal = has_ordinal ? symtab_data.GetU16(&name_ordinal_offset) : i;
651a4fe3a12SVirgile Bello                     uint32_t name_address = symtab_data.GetU32(&name_offset);
652a4fe3a12SVirgile Bello 
653a4fe3a12SVirgile Bello                     const char* symbol_name_cstr = symtab_data.PeekCStr(name_address - data_start);
654a4fe3a12SVirgile Bello                     symbol_name.assign(symbol_name_cstr);
655a4fe3a12SVirgile Bello 
656a4fe3a12SVirgile Bello                     lldb::offset_t function_offset = export_table.address_of_functions - data_start + sizeof(uint32_t) * name_ordinal;
657a4fe3a12SVirgile Bello                     uint32_t function_rva = symtab_data.GetU32(&function_offset);
658a4fe3a12SVirgile Bello 
659a4fe3a12SVirgile Bello                     Address symbol_addr(m_coff_header_opt.image_base + function_rva, sect_list);
660a4fe3a12SVirgile Bello                     symbols[i].GetMangled().SetValue(ConstString(symbol_name.c_str()));
661358cf1eaSGreg Clayton                     symbols[i].GetAddressRef() = symbol_addr;
662a4fe3a12SVirgile Bello                     symbols[i].SetType(lldb::eSymbolTypeCode);
663a4fe3a12SVirgile Bello                     symbols[i].SetDebug(true);
664a4fe3a12SVirgile Bello                 }
665a4fe3a12SVirgile Bello             }
666225d3ea3SAdrian McCarthy             m_symtab_ap->CalculateSymbolSizes();
667f754f88fSGreg Clayton         }
668a1743499SGreg Clayton     }
669f754f88fSGreg Clayton     return m_symtab_ap.get();
670f754f88fSGreg Clayton 
671f754f88fSGreg Clayton }
672f754f88fSGreg Clayton 
6733046e668SGreg Clayton bool
6743046e668SGreg Clayton ObjectFilePECOFF::IsStripped ()
675f754f88fSGreg Clayton {
6763046e668SGreg Clayton     // TODO: determine this for COFF
6773046e668SGreg Clayton     return false;
6783046e668SGreg Clayton }
6793046e668SGreg Clayton 
6803046e668SGreg Clayton 
6813046e668SGreg Clayton 
6823046e668SGreg Clayton void
6833046e668SGreg Clayton ObjectFilePECOFF::CreateSections (SectionList &unified_section_list)
6843046e668SGreg Clayton {
6853046e668SGreg Clayton     if (!m_sections_ap.get())
6863046e668SGreg Clayton     {
6873046e668SGreg Clayton         m_sections_ap.reset(new SectionList());
6883046e668SGreg Clayton 
689a1743499SGreg Clayton         ModuleSP module_sp(GetModule());
690a1743499SGreg Clayton         if (module_sp)
691a1743499SGreg Clayton         {
69216ff8604SSaleem Abdulrasool             std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
693f754f88fSGreg Clayton             const uint32_t nsects = m_sect_headers.size();
694e72dfb32SGreg Clayton             ModuleSP module_sp (GetModule());
695f754f88fSGreg Clayton             for (uint32_t idx = 0; idx<nsects; ++idx)
696f754f88fSGreg Clayton             {
697f754f88fSGreg Clayton                 std::string sect_name;
698f754f88fSGreg Clayton                 GetSectionName (sect_name, m_sect_headers[idx]);
699f754f88fSGreg Clayton                 ConstString const_sect_name (sect_name.c_str());
70028469ca3SGreg Clayton                 static ConstString g_code_sect_name (".code");
70128469ca3SGreg Clayton                 static ConstString g_CODE_sect_name ("CODE");
70228469ca3SGreg Clayton                 static ConstString g_data_sect_name (".data");
70328469ca3SGreg Clayton                 static ConstString g_DATA_sect_name ("DATA");
70428469ca3SGreg Clayton                 static ConstString g_bss_sect_name (".bss");
70528469ca3SGreg Clayton                 static ConstString g_BSS_sect_name ("BSS");
70628469ca3SGreg Clayton                 static ConstString g_debug_sect_name (".debug");
70728469ca3SGreg Clayton                 static ConstString g_reloc_sect_name (".reloc");
70828469ca3SGreg Clayton                 static ConstString g_stab_sect_name (".stab");
70928469ca3SGreg Clayton                 static ConstString g_stabstr_sect_name (".stabstr");
7100076e715SGreg Clayton                 static ConstString g_sect_name_dwarf_debug_abbrev (".debug_abbrev");
7110076e715SGreg Clayton                 static ConstString g_sect_name_dwarf_debug_aranges (".debug_aranges");
7120076e715SGreg Clayton                 static ConstString g_sect_name_dwarf_debug_frame (".debug_frame");
7130076e715SGreg Clayton                 static ConstString g_sect_name_dwarf_debug_info (".debug_info");
7140076e715SGreg Clayton                 static ConstString g_sect_name_dwarf_debug_line (".debug_line");
7150076e715SGreg Clayton                 static ConstString g_sect_name_dwarf_debug_loc (".debug_loc");
7160076e715SGreg Clayton                 static ConstString g_sect_name_dwarf_debug_macinfo (".debug_macinfo");
7170076e715SGreg Clayton                 static ConstString g_sect_name_dwarf_debug_pubnames (".debug_pubnames");
7180076e715SGreg Clayton                 static ConstString g_sect_name_dwarf_debug_pubtypes (".debug_pubtypes");
7190076e715SGreg Clayton                 static ConstString g_sect_name_dwarf_debug_ranges (".debug_ranges");
7200076e715SGreg Clayton                 static ConstString g_sect_name_dwarf_debug_str (".debug_str");
7210076e715SGreg Clayton                 static ConstString g_sect_name_eh_frame (".eh_frame");
72265d4d5c3SRyan Brown                 static ConstString g_sect_name_go_symtab (".gosymtab");
72328469ca3SGreg Clayton                 SectionType section_type = eSectionTypeOther;
724237ad974SCharles Davis                 if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_CODE &&
72528469ca3SGreg Clayton                     ((const_sect_name == g_code_sect_name) || (const_sect_name == g_CODE_sect_name)))
72628469ca3SGreg Clayton                 {
72728469ca3SGreg Clayton                     section_type = eSectionTypeCode;
72828469ca3SGreg Clayton                 }
729237ad974SCharles Davis                 else if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_INITIALIZED_DATA &&
73028469ca3SGreg Clayton                          ((const_sect_name == g_data_sect_name) || (const_sect_name == g_DATA_sect_name)))
73128469ca3SGreg Clayton                 {
73228469ca3SGreg Clayton                     section_type = eSectionTypeData;
73328469ca3SGreg Clayton                 }
734237ad974SCharles Davis                 else if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA &&
73528469ca3SGreg Clayton                          ((const_sect_name == g_bss_sect_name) || (const_sect_name == g_BSS_sect_name)))
73628469ca3SGreg Clayton                 {
73728469ca3SGreg Clayton                     if (m_sect_headers[idx].size == 0)
73828469ca3SGreg Clayton                         section_type = eSectionTypeZeroFill;
73928469ca3SGreg Clayton                     else
74028469ca3SGreg Clayton                         section_type = eSectionTypeData;
74128469ca3SGreg Clayton                 }
74228469ca3SGreg Clayton                 else if (const_sect_name == g_debug_sect_name)
74328469ca3SGreg Clayton                 {
74428469ca3SGreg Clayton                     section_type = eSectionTypeDebug;
74528469ca3SGreg Clayton                 }
74628469ca3SGreg Clayton                 else if (const_sect_name == g_stabstr_sect_name)
74728469ca3SGreg Clayton                 {
74828469ca3SGreg Clayton                     section_type = eSectionTypeDataCString;
74928469ca3SGreg Clayton                 }
75028469ca3SGreg Clayton                 else if (const_sect_name == g_reloc_sect_name)
75128469ca3SGreg Clayton                 {
75228469ca3SGreg Clayton                     section_type = eSectionTypeOther;
75328469ca3SGreg Clayton                 }
7540076e715SGreg Clayton                 else if (const_sect_name == g_sect_name_dwarf_debug_abbrev)    section_type = eSectionTypeDWARFDebugAbbrev;
7550076e715SGreg Clayton                 else if (const_sect_name == g_sect_name_dwarf_debug_aranges)   section_type = eSectionTypeDWARFDebugAranges;
7560076e715SGreg Clayton                 else if (const_sect_name == g_sect_name_dwarf_debug_frame)     section_type = eSectionTypeDWARFDebugFrame;
7570076e715SGreg Clayton                 else if (const_sect_name == g_sect_name_dwarf_debug_info)      section_type = eSectionTypeDWARFDebugInfo;
7580076e715SGreg Clayton                 else if (const_sect_name == g_sect_name_dwarf_debug_line)      section_type = eSectionTypeDWARFDebugLine;
7590076e715SGreg Clayton                 else if (const_sect_name == g_sect_name_dwarf_debug_loc)       section_type = eSectionTypeDWARFDebugLoc;
7600076e715SGreg Clayton                 else if (const_sect_name == g_sect_name_dwarf_debug_macinfo)   section_type = eSectionTypeDWARFDebugMacInfo;
7610076e715SGreg Clayton                 else if (const_sect_name == g_sect_name_dwarf_debug_pubnames)  section_type = eSectionTypeDWARFDebugPubNames;
7620076e715SGreg Clayton                 else if (const_sect_name == g_sect_name_dwarf_debug_pubtypes)  section_type = eSectionTypeDWARFDebugPubTypes;
7630076e715SGreg Clayton                 else if (const_sect_name == g_sect_name_dwarf_debug_ranges)    section_type = eSectionTypeDWARFDebugRanges;
7640076e715SGreg Clayton                 else if (const_sect_name == g_sect_name_dwarf_debug_str)       section_type = eSectionTypeDWARFDebugStr;
7650076e715SGreg Clayton                 else if (const_sect_name == g_sect_name_eh_frame)              section_type = eSectionTypeEHFrame;
76665d4d5c3SRyan Brown                 else if (const_sect_name == g_sect_name_go_symtab)             section_type = eSectionTypeGoSymtab;
767237ad974SCharles Davis                 else if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_CODE)
76828469ca3SGreg Clayton                 {
76928469ca3SGreg Clayton                     section_type = eSectionTypeCode;
77028469ca3SGreg Clayton                 }
771237ad974SCharles Davis                 else if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)
77228469ca3SGreg Clayton                 {
77328469ca3SGreg Clayton                     section_type = eSectionTypeData;
77428469ca3SGreg Clayton                 }
775237ad974SCharles Davis                 else if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)
77628469ca3SGreg Clayton                 {
77728469ca3SGreg Clayton                     if (m_sect_headers[idx].size == 0)
77828469ca3SGreg Clayton                         section_type = eSectionTypeZeroFill;
77928469ca3SGreg Clayton                     else
78028469ca3SGreg Clayton                         section_type = eSectionTypeData;
78128469ca3SGreg Clayton                 }
782f754f88fSGreg Clayton 
783f754f88fSGreg Clayton                 // Use a segment ID of the segment index shifted left by 8 so they
784f754f88fSGreg Clayton                 // never conflict with any of the sections.
785e72dfb32SGreg Clayton                 SectionSP section_sp (new Section (module_sp,                    // Module to which this section belongs
786a7499c98SMichael Sartain                                                    this,                         // Object file to which this section belongs
787f754f88fSGreg Clayton                                                    idx + 1,                      // Section ID is the 1 based segment index shifted right by 8 bits as not to collide with any of the 256 section IDs that are possible
788f754f88fSGreg Clayton                                                    const_sect_name,              // Name of this section
78928469ca3SGreg Clayton                                                    section_type,                 // This section is a container of other sections.
7900076e715SGreg Clayton                                                    m_coff_header_opt.image_base + m_sect_headers[idx].vmaddr,   // File VM address == addresses as they are found in the object file
791f754f88fSGreg Clayton                                                    m_sect_headers[idx].vmsize,   // VM size in bytes of this section
792f754f88fSGreg Clayton                                                    m_sect_headers[idx].offset,   // Offset to the data for this section in the file
793aaa0ba31SBruce Mitchener                                                    m_sect_headers[idx].size,     // Size in bytes of this section as found in the file
79448672afbSGreg Clayton                                                    m_coff_header_opt.sect_alignment, // Section alignment
795f754f88fSGreg Clayton                                                    m_sect_headers[idx].flags));  // Flags for this section
796f754f88fSGreg Clayton 
797f754f88fSGreg Clayton                 //section_sp->SetIsEncrypted (segment_is_encrypted);
798f754f88fSGreg Clayton 
7993046e668SGreg Clayton                 unified_section_list.AddSection(section_sp);
800f754f88fSGreg Clayton                 m_sections_ap->AddSection (section_sp);
801f754f88fSGreg Clayton             }
802f754f88fSGreg Clayton         }
803a1743499SGreg Clayton     }
804f754f88fSGreg Clayton }
805f754f88fSGreg Clayton 
806f754f88fSGreg Clayton bool
807f754f88fSGreg Clayton ObjectFilePECOFF::GetUUID (UUID* uuid)
808f754f88fSGreg Clayton {
809f754f88fSGreg Clayton     return false;
810f754f88fSGreg Clayton }
811f754f88fSGreg Clayton 
812f754f88fSGreg Clayton uint32_t
813f754f88fSGreg Clayton ObjectFilePECOFF::GetDependentModules (FileSpecList& files)
814f754f88fSGreg Clayton {
815f754f88fSGreg Clayton     return 0;
816f754f88fSGreg Clayton }
817f754f88fSGreg Clayton 
8188e38c666SStephane Sezer lldb_private::Address
8198e38c666SStephane Sezer ObjectFilePECOFF::GetEntryPointAddress ()
8208e38c666SStephane Sezer {
8218e38c666SStephane Sezer     if (m_entry_point_address.IsValid())
8228e38c666SStephane Sezer         return m_entry_point_address;
8238e38c666SStephane Sezer 
8248e38c666SStephane Sezer     if (!ParseHeader() || !IsExecutable())
8258e38c666SStephane Sezer         return m_entry_point_address;
8268e38c666SStephane Sezer 
8278e38c666SStephane Sezer     SectionList *section_list = GetSectionList();
8288e38c666SStephane Sezer     addr_t offset = m_coff_header_opt.entry;
8298e38c666SStephane Sezer 
8308e38c666SStephane Sezer     if (!section_list)
8318e38c666SStephane Sezer         m_entry_point_address.SetOffset(offset);
8328e38c666SStephane Sezer     else
8338e38c666SStephane Sezer         m_entry_point_address.ResolveAddressUsingFileSections(offset, section_list);
8348e38c666SStephane Sezer     return m_entry_point_address;
8358e38c666SStephane Sezer }
8368e38c666SStephane Sezer 
837f754f88fSGreg Clayton 
838f754f88fSGreg Clayton //----------------------------------------------------------------------
839f754f88fSGreg Clayton // Dump
840f754f88fSGreg Clayton //
841f754f88fSGreg Clayton // Dump the specifics of the runtime file container (such as any headers
842f754f88fSGreg Clayton // segments, sections, etc).
843f754f88fSGreg Clayton //----------------------------------------------------------------------
844f754f88fSGreg Clayton void
845f754f88fSGreg Clayton ObjectFilePECOFF::Dump(Stream *s)
846f754f88fSGreg Clayton {
847a1743499SGreg Clayton     ModuleSP module_sp(GetModule());
848a1743499SGreg Clayton     if (module_sp)
849a1743499SGreg Clayton     {
85016ff8604SSaleem Abdulrasool         std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
851324a1036SSaleem Abdulrasool         s->Printf("%p: ", static_cast<void*>(this));
852f754f88fSGreg Clayton         s->Indent();
853f754f88fSGreg Clayton         s->PutCString("ObjectFilePECOFF");
854f754f88fSGreg Clayton 
855f754f88fSGreg Clayton         ArchSpec header_arch;
856f754f88fSGreg Clayton         GetArchitecture (header_arch);
857f754f88fSGreg Clayton 
858f754f88fSGreg Clayton         *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
859f754f88fSGreg Clayton 
8603046e668SGreg Clayton         SectionList *sections = GetSectionList();
8613046e668SGreg Clayton         if (sections)
8623046e668SGreg Clayton             sections->Dump(s, NULL, true, UINT32_MAX);
863f754f88fSGreg Clayton 
864f754f88fSGreg Clayton         if (m_symtab_ap.get())
865f754f88fSGreg Clayton             m_symtab_ap->Dump(s, NULL, eSortOrderNone);
866f754f88fSGreg Clayton 
867f754f88fSGreg Clayton         if (m_dos_header.e_magic)
868f754f88fSGreg Clayton             DumpDOSHeader (s, m_dos_header);
869f754f88fSGreg Clayton         if (m_coff_header.machine)
870f754f88fSGreg Clayton         {
871f754f88fSGreg Clayton             DumpCOFFHeader (s, m_coff_header);
872f754f88fSGreg Clayton             if (m_coff_header.hdrsize)
873f754f88fSGreg Clayton                 DumpOptCOFFHeader (s, m_coff_header_opt);
874f754f88fSGreg Clayton         }
875f754f88fSGreg Clayton         s->EOL();
876f754f88fSGreg Clayton         DumpSectionHeaders(s);
877f754f88fSGreg Clayton         s->EOL();
878f754f88fSGreg Clayton     }
879a1743499SGreg Clayton }
880f754f88fSGreg Clayton 
881f754f88fSGreg Clayton //----------------------------------------------------------------------
882f754f88fSGreg Clayton // DumpDOSHeader
883f754f88fSGreg Clayton //
884f754f88fSGreg Clayton // Dump the MS-DOS header to the specified output stream
885f754f88fSGreg Clayton //----------------------------------------------------------------------
886f754f88fSGreg Clayton void
887f754f88fSGreg Clayton ObjectFilePECOFF::DumpDOSHeader(Stream *s, const dos_header_t& header)
888f754f88fSGreg Clayton {
889f754f88fSGreg Clayton     s->PutCString ("MSDOS Header\n");
890f754f88fSGreg Clayton     s->Printf ("  e_magic    = 0x%4.4x\n", header.e_magic);
891f754f88fSGreg Clayton     s->Printf ("  e_cblp     = 0x%4.4x\n", header.e_cblp);
892f754f88fSGreg Clayton     s->Printf ("  e_cp       = 0x%4.4x\n", header.e_cp);
893f754f88fSGreg Clayton     s->Printf ("  e_crlc     = 0x%4.4x\n", header.e_crlc);
894f754f88fSGreg Clayton     s->Printf ("  e_cparhdr  = 0x%4.4x\n", header.e_cparhdr);
895f754f88fSGreg Clayton     s->Printf ("  e_minalloc = 0x%4.4x\n", header.e_minalloc);
896f754f88fSGreg Clayton     s->Printf ("  e_maxalloc = 0x%4.4x\n", header.e_maxalloc);
897f754f88fSGreg Clayton     s->Printf ("  e_ss       = 0x%4.4x\n", header.e_ss);
898f754f88fSGreg Clayton     s->Printf ("  e_sp       = 0x%4.4x\n", header.e_sp);
899f754f88fSGreg Clayton     s->Printf ("  e_csum     = 0x%4.4x\n", header.e_csum);
900f754f88fSGreg Clayton     s->Printf ("  e_ip       = 0x%4.4x\n", header.e_ip);
901f754f88fSGreg Clayton     s->Printf ("  e_cs       = 0x%4.4x\n", header.e_cs);
902f754f88fSGreg Clayton     s->Printf ("  e_lfarlc   = 0x%4.4x\n", header.e_lfarlc);
903f754f88fSGreg Clayton     s->Printf ("  e_ovno     = 0x%4.4x\n", header.e_ovno);
904f754f88fSGreg Clayton     s->Printf ("  e_res[4]   = { 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x }\n",
905f754f88fSGreg Clayton                header.e_res[0],
906f754f88fSGreg Clayton                header.e_res[1],
907f754f88fSGreg Clayton                header.e_res[2],
908f754f88fSGreg Clayton                header.e_res[3]);
909f754f88fSGreg Clayton     s->Printf ("  e_oemid    = 0x%4.4x\n", header.e_oemid);
910f754f88fSGreg Clayton     s->Printf ("  e_oeminfo  = 0x%4.4x\n", header.e_oeminfo);
911f754f88fSGreg Clayton     s->Printf ("  e_res2[10] = { 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x }\n",
912f754f88fSGreg Clayton                header.e_res2[0],
913f754f88fSGreg Clayton                header.e_res2[1],
914f754f88fSGreg Clayton                header.e_res2[2],
915f754f88fSGreg Clayton                header.e_res2[3],
916f754f88fSGreg Clayton                header.e_res2[4],
917f754f88fSGreg Clayton                header.e_res2[5],
918f754f88fSGreg Clayton                header.e_res2[6],
919f754f88fSGreg Clayton                header.e_res2[7],
920f754f88fSGreg Clayton                header.e_res2[8],
921f754f88fSGreg Clayton                header.e_res2[9]);
922f754f88fSGreg Clayton     s->Printf ("  e_lfanew   = 0x%8.8x\n", header.e_lfanew);
923f754f88fSGreg Clayton }
924f754f88fSGreg Clayton 
925f754f88fSGreg Clayton //----------------------------------------------------------------------
926f754f88fSGreg Clayton // DumpCOFFHeader
927f754f88fSGreg Clayton //
928f754f88fSGreg Clayton // Dump the COFF header to the specified output stream
929f754f88fSGreg Clayton //----------------------------------------------------------------------
930f754f88fSGreg Clayton void
931f754f88fSGreg Clayton ObjectFilePECOFF::DumpCOFFHeader(Stream *s, const coff_header_t& header)
932f754f88fSGreg Clayton {
933f754f88fSGreg Clayton     s->PutCString ("COFF Header\n");
934f754f88fSGreg Clayton     s->Printf ("  machine = 0x%4.4x\n", header.machine);
935f754f88fSGreg Clayton     s->Printf ("  nsects  = 0x%4.4x\n", header.nsects);
936f754f88fSGreg Clayton     s->Printf ("  modtime = 0x%8.8x\n", header.modtime);
937f754f88fSGreg Clayton     s->Printf ("  symoff  = 0x%8.8x\n", header.symoff);
938f754f88fSGreg Clayton     s->Printf ("  nsyms   = 0x%8.8x\n", header.nsyms);
939f754f88fSGreg Clayton     s->Printf ("  hdrsize = 0x%4.4x\n", header.hdrsize);
940f754f88fSGreg Clayton }
941f754f88fSGreg Clayton 
942f754f88fSGreg Clayton //----------------------------------------------------------------------
943f754f88fSGreg Clayton // DumpOptCOFFHeader
944f754f88fSGreg Clayton //
945f754f88fSGreg Clayton // Dump the optional COFF header to the specified output stream
946f754f88fSGreg Clayton //----------------------------------------------------------------------
947f754f88fSGreg Clayton void
948f754f88fSGreg Clayton ObjectFilePECOFF::DumpOptCOFFHeader(Stream *s, const coff_opt_header_t& header)
949f754f88fSGreg Clayton {
950f754f88fSGreg Clayton     s->PutCString ("Optional COFF Header\n");
951f754f88fSGreg Clayton     s->Printf ("  magic                   = 0x%4.4x\n", header.magic);
952f754f88fSGreg Clayton     s->Printf ("  major_linker_version    = 0x%2.2x\n", header.major_linker_version);
953f754f88fSGreg Clayton     s->Printf ("  minor_linker_version    = 0x%2.2x\n", header.minor_linker_version);
954f754f88fSGreg Clayton     s->Printf ("  code_size               = 0x%8.8x\n", header.code_size);
955f754f88fSGreg Clayton     s->Printf ("  data_size               = 0x%8.8x\n", header.data_size);
956f754f88fSGreg Clayton     s->Printf ("  bss_size                = 0x%8.8x\n", header.bss_size);
957f754f88fSGreg Clayton     s->Printf ("  entry                   = 0x%8.8x\n", header.entry);
958f754f88fSGreg Clayton     s->Printf ("  code_offset             = 0x%8.8x\n", header.code_offset);
959f754f88fSGreg Clayton     s->Printf ("  data_offset             = 0x%8.8x\n", header.data_offset);
960d01b2953SDaniel Malea     s->Printf ("  image_base              = 0x%16.16" PRIx64 "\n", header.image_base);
961f754f88fSGreg Clayton     s->Printf ("  sect_alignment          = 0x%8.8x\n", header.sect_alignment);
962f754f88fSGreg Clayton     s->Printf ("  file_alignment          = 0x%8.8x\n", header.file_alignment);
963f754f88fSGreg Clayton     s->Printf ("  major_os_system_version = 0x%4.4x\n", header.major_os_system_version);
964f754f88fSGreg Clayton     s->Printf ("  minor_os_system_version = 0x%4.4x\n", header.minor_os_system_version);
965f754f88fSGreg Clayton     s->Printf ("  major_image_version     = 0x%4.4x\n", header.major_image_version);
966f754f88fSGreg Clayton     s->Printf ("  minor_image_version     = 0x%4.4x\n", header.minor_image_version);
967f754f88fSGreg Clayton     s->Printf ("  major_subsystem_version = 0x%4.4x\n", header.major_subsystem_version);
968f754f88fSGreg Clayton     s->Printf ("  minor_subsystem_version = 0x%4.4x\n", header.minor_subsystem_version);
969f754f88fSGreg Clayton     s->Printf ("  reserved1               = 0x%8.8x\n", header.reserved1);
970f754f88fSGreg Clayton     s->Printf ("  image_size              = 0x%8.8x\n", header.image_size);
971f754f88fSGreg Clayton     s->Printf ("  header_size             = 0x%8.8x\n", header.header_size);
97228469ca3SGreg Clayton     s->Printf ("  checksum                = 0x%8.8x\n", header.checksum);
973f754f88fSGreg Clayton     s->Printf ("  subsystem               = 0x%4.4x\n", header.subsystem);
974f754f88fSGreg Clayton     s->Printf ("  dll_flags               = 0x%4.4x\n", header.dll_flags);
975d01b2953SDaniel Malea     s->Printf ("  stack_reserve_size      = 0x%16.16" PRIx64 "\n", header.stack_reserve_size);
976d01b2953SDaniel Malea     s->Printf ("  stack_commit_size       = 0x%16.16" PRIx64 "\n", header.stack_commit_size);
977d01b2953SDaniel Malea     s->Printf ("  heap_reserve_size       = 0x%16.16" PRIx64 "\n", header.heap_reserve_size);
978d01b2953SDaniel Malea     s->Printf ("  heap_commit_size        = 0x%16.16" PRIx64 "\n", header.heap_commit_size);
979f754f88fSGreg Clayton     s->Printf ("  loader_flags            = 0x%8.8x\n", header.loader_flags);
980ffeba256SVirgile Bello     s->Printf ("  num_data_dir_entries    = 0x%8.8x\n", (uint32_t)header.data_dirs.size());
981f754f88fSGreg Clayton     uint32_t i;
982f754f88fSGreg Clayton     for (i=0; i<header.data_dirs.size(); i++)
983f754f88fSGreg Clayton     {
98428469ca3SGreg Clayton         s->Printf ("  data_dirs[%2u] vmaddr = 0x%8.8x, vmsize = 0x%8.8x\n",
985f754f88fSGreg Clayton                    i,
986f754f88fSGreg Clayton                    header.data_dirs[i].vmaddr,
987f754f88fSGreg Clayton                    header.data_dirs[i].vmsize);
988f754f88fSGreg Clayton     }
989f754f88fSGreg Clayton }
990f754f88fSGreg Clayton //----------------------------------------------------------------------
991f754f88fSGreg Clayton // DumpSectionHeader
992f754f88fSGreg Clayton //
993f754f88fSGreg Clayton // Dump a single ELF section header to the specified output stream
994f754f88fSGreg Clayton //----------------------------------------------------------------------
995f754f88fSGreg Clayton void
996f754f88fSGreg Clayton ObjectFilePECOFF::DumpSectionHeader(Stream *s, const section_header_t& sh)
997f754f88fSGreg Clayton {
998f754f88fSGreg Clayton     std::string name;
999f754f88fSGreg Clayton     GetSectionName(name, sh);
1000f754f88fSGreg Clayton     s->Printf ("%-16s 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x 0x%4.4x 0x%4.4x 0x%8.8x\n",
1001f754f88fSGreg Clayton                name.c_str(),
1002f754f88fSGreg Clayton                sh.vmaddr,
100328469ca3SGreg Clayton                sh.vmsize,
1004f754f88fSGreg Clayton                sh.offset,
100528469ca3SGreg Clayton                sh.size,
1006f754f88fSGreg Clayton                sh.reloff,
1007f754f88fSGreg Clayton                sh.lineoff,
1008f754f88fSGreg Clayton                sh.nreloc,
1009f754f88fSGreg Clayton                sh.nline,
1010f754f88fSGreg Clayton                sh.flags);
1011f754f88fSGreg Clayton }
1012f754f88fSGreg Clayton 
1013f754f88fSGreg Clayton 
1014f754f88fSGreg Clayton //----------------------------------------------------------------------
1015f754f88fSGreg Clayton // DumpSectionHeaders
1016f754f88fSGreg Clayton //
1017f754f88fSGreg Clayton // Dump all of the ELF section header to the specified output stream
1018f754f88fSGreg Clayton //----------------------------------------------------------------------
1019f754f88fSGreg Clayton void
1020f754f88fSGreg Clayton ObjectFilePECOFF::DumpSectionHeaders(Stream *s)
1021f754f88fSGreg Clayton {
1022f754f88fSGreg Clayton 
1023f754f88fSGreg Clayton     s->PutCString ("Section Headers\n");
102428469ca3SGreg Clayton     s->PutCString ("IDX  name             vm addr    vm size    file off   file size  reloc off  line off   nreloc nline  flags\n");
102528469ca3SGreg Clayton     s->PutCString ("==== ---------------- ---------- ---------- ---------- ---------- ---------- ---------- ------ ------ ----------\n");
1026f754f88fSGreg Clayton 
1027f754f88fSGreg Clayton     uint32_t idx = 0;
1028f754f88fSGreg Clayton     SectionHeaderCollIter pos, end = m_sect_headers.end();
1029f754f88fSGreg Clayton 
1030f754f88fSGreg Clayton     for (pos = m_sect_headers.begin(); pos != end; ++pos, ++idx)
1031f754f88fSGreg Clayton     {
1032f754f88fSGreg Clayton         s->Printf ("[%2u] ", idx);
1033f754f88fSGreg Clayton         ObjectFilePECOFF::DumpSectionHeader(s, *pos);
1034f754f88fSGreg Clayton     }
1035f754f88fSGreg Clayton }
1036f754f88fSGreg Clayton 
1037f754f88fSGreg Clayton bool
1038f754f88fSGreg Clayton ObjectFilePECOFF::GetArchitecture (ArchSpec &arch)
1039f754f88fSGreg Clayton {
1040237ad974SCharles Davis     uint16_t machine = m_coff_header.machine;
1041237ad974SCharles Davis     switch (machine)
1042237ad974SCharles Davis     {
1043237ad974SCharles Davis         case llvm::COFF::IMAGE_FILE_MACHINE_AMD64:
1044237ad974SCharles Davis         case llvm::COFF::IMAGE_FILE_MACHINE_I386:
1045237ad974SCharles Davis         case llvm::COFF::IMAGE_FILE_MACHINE_POWERPC:
1046237ad974SCharles Davis         case llvm::COFF::IMAGE_FILE_MACHINE_POWERPCFP:
1047237ad974SCharles Davis         case llvm::COFF::IMAGE_FILE_MACHINE_ARM:
10481108cb36SSaleem Abdulrasool         case llvm::COFF::IMAGE_FILE_MACHINE_ARMNT:
1049237ad974SCharles Davis         case llvm::COFF::IMAGE_FILE_MACHINE_THUMB:
10506c970420SColin Riley             arch.SetArchitecture (eArchTypeCOFF, machine, LLDB_INVALID_CPUTYPE);
1051237ad974SCharles Davis             return true;
1052237ad974SCharles Davis         default:
1053237ad974SCharles Davis             break;
1054237ad974SCharles Davis     }
1055237ad974SCharles Davis     return false;
1056f754f88fSGreg Clayton }
1057f754f88fSGreg Clayton 
1058f754f88fSGreg Clayton ObjectFile::Type
1059f754f88fSGreg Clayton ObjectFilePECOFF::CalculateType()
1060f754f88fSGreg Clayton {
1061f754f88fSGreg Clayton     if (m_coff_header.machine != 0)
1062f754f88fSGreg Clayton     {
1063237ad974SCharles Davis         if ((m_coff_header.flags & llvm::COFF::IMAGE_FILE_DLL) == 0)
1064f754f88fSGreg Clayton             return eTypeExecutable;
1065f754f88fSGreg Clayton         else
1066f754f88fSGreg Clayton             return eTypeSharedLibrary;
1067f754f88fSGreg Clayton     }
1068f754f88fSGreg Clayton     return eTypeExecutable;
1069f754f88fSGreg Clayton }
1070f754f88fSGreg Clayton 
1071f754f88fSGreg Clayton ObjectFile::Strata
1072f754f88fSGreg Clayton ObjectFilePECOFF::CalculateStrata()
1073f754f88fSGreg Clayton {
1074f754f88fSGreg Clayton     return eStrataUser;
1075f754f88fSGreg Clayton }
1076f754f88fSGreg Clayton //------------------------------------------------------------------
1077f754f88fSGreg Clayton // PluginInterface protocol
1078f754f88fSGreg Clayton //------------------------------------------------------------------
107957abc5d6SGreg Clayton ConstString
1080f754f88fSGreg Clayton ObjectFilePECOFF::GetPluginName()
1081f754f88fSGreg Clayton {
1082f754f88fSGreg Clayton     return GetPluginNameStatic();
1083f754f88fSGreg Clayton }
1084f754f88fSGreg Clayton 
1085f754f88fSGreg Clayton uint32_t
1086f754f88fSGreg Clayton ObjectFilePECOFF::GetPluginVersion()
1087f754f88fSGreg Clayton {
1088f754f88fSGreg Clayton     return 1;
1089f754f88fSGreg Clayton }
1090f754f88fSGreg Clayton 
1091