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"
11f754f88fSGreg Clayton 
12237ad974SCharles Davis #include "llvm/Support/COFF.h"
13f754f88fSGreg Clayton 
14f754f88fSGreg Clayton #include "lldb/Core/ArchSpec.h"
15f754f88fSGreg Clayton #include "lldb/Core/DataBuffer.h"
16f754f88fSGreg Clayton #include "lldb/Host/FileSpec.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"
26f754f88fSGreg Clayton #include "lldb/Symbol/ObjectFile.h"
272756adf3SVirgile Bello #include "lldb/Target/SectionLoadList.h"
282756adf3SVirgile Bello #include "lldb/Target/Target.h"
29f754f88fSGreg Clayton 
30f754f88fSGreg Clayton #define IMAGE_DOS_SIGNATURE             0x5A4D      // MZ
31f754f88fSGreg Clayton #define IMAGE_NT_SIGNATURE              0x00004550  // PE00
32f754f88fSGreg Clayton #define OPT_HEADER_MAGIC_PE32           0x010b
33f754f88fSGreg Clayton #define OPT_HEADER_MAGIC_PE32_PLUS      0x020b
34f754f88fSGreg Clayton 
35f754f88fSGreg Clayton using namespace lldb;
36f754f88fSGreg Clayton using namespace lldb_private;
37f754f88fSGreg Clayton 
38f754f88fSGreg Clayton void
39f754f88fSGreg Clayton ObjectFilePECOFF::Initialize()
40f754f88fSGreg Clayton {
41f754f88fSGreg Clayton     PluginManager::RegisterPlugin (GetPluginNameStatic(),
42f754f88fSGreg Clayton                                    GetPluginDescriptionStatic(),
43c9660546SGreg Clayton                                    CreateInstance,
44f4d6de6aSGreg Clayton                                    CreateMemoryInstance,
45f4d6de6aSGreg Clayton                                    GetModuleSpecifications);
46f754f88fSGreg Clayton }
47f754f88fSGreg Clayton 
48f754f88fSGreg Clayton void
49f754f88fSGreg Clayton ObjectFilePECOFF::Terminate()
50f754f88fSGreg Clayton {
51f754f88fSGreg Clayton     PluginManager::UnregisterPlugin (CreateInstance);
52f754f88fSGreg Clayton }
53f754f88fSGreg Clayton 
54f754f88fSGreg Clayton 
5557abc5d6SGreg Clayton lldb_private::ConstString
56f754f88fSGreg Clayton ObjectFilePECOFF::GetPluginNameStatic()
57f754f88fSGreg Clayton {
5857abc5d6SGreg Clayton     static ConstString g_name("pe-coff");
5957abc5d6SGreg Clayton     return g_name;
60f754f88fSGreg Clayton }
61f754f88fSGreg Clayton 
62f754f88fSGreg Clayton const char *
63f754f88fSGreg Clayton ObjectFilePECOFF::GetPluginDescriptionStatic()
64f754f88fSGreg Clayton {
65f754f88fSGreg Clayton     return "Portable Executable and Common Object File Format object file reader (32 and 64 bit)";
66f754f88fSGreg Clayton }
67f754f88fSGreg Clayton 
68f754f88fSGreg Clayton 
69f754f88fSGreg Clayton ObjectFile *
705ce9c565SGreg Clayton ObjectFilePECOFF::CreateInstance (const lldb::ModuleSP &module_sp,
715ce9c565SGreg Clayton                                   DataBufferSP& data_sp,
725ce9c565SGreg Clayton                                   lldb::offset_t data_offset,
735ce9c565SGreg Clayton                                   const lldb_private::FileSpec* file,
745ce9c565SGreg Clayton                                   lldb::offset_t file_offset,
755ce9c565SGreg Clayton                                   lldb::offset_t length)
76f754f88fSGreg Clayton {
775ce9c565SGreg Clayton     if (!data_sp)
78f754f88fSGreg Clayton     {
795ce9c565SGreg Clayton         data_sp = file->MemoryMapFileContents(file_offset, length);
805ce9c565SGreg Clayton         data_offset = 0;
815ce9c565SGreg Clayton     }
825ce9c565SGreg Clayton 
835ce9c565SGreg Clayton     if (ObjectFilePECOFF::MagicBytesMatch(data_sp))
845ce9c565SGreg Clayton     {
855ce9c565SGreg Clayton         // Update the data to contain the entire file if it doesn't already
865ce9c565SGreg Clayton         if (data_sp->GetByteSize() < length)
875ce9c565SGreg Clayton             data_sp = file->MemoryMapFileContents(file_offset, length);
887b0992d9SGreg Clayton         std::unique_ptr<ObjectFile> objfile_ap(new ObjectFilePECOFF (module_sp, data_sp, data_offset, file, file_offset, length));
89f754f88fSGreg Clayton         if (objfile_ap.get() && objfile_ap->ParseHeader())
90f754f88fSGreg Clayton             return objfile_ap.release();
91f754f88fSGreg Clayton     }
92f754f88fSGreg Clayton     return NULL;
93f754f88fSGreg Clayton }
94f754f88fSGreg Clayton 
95c9660546SGreg Clayton ObjectFile *
96e72dfb32SGreg Clayton ObjectFilePECOFF::CreateMemoryInstance (const lldb::ModuleSP &module_sp,
97c9660546SGreg Clayton                                         lldb::DataBufferSP& data_sp,
98c9660546SGreg Clayton                                         const lldb::ProcessSP &process_sp,
99c9660546SGreg Clayton                                         lldb::addr_t header_addr)
100c9660546SGreg Clayton {
101c9660546SGreg Clayton     return NULL;
102c9660546SGreg Clayton }
103c9660546SGreg Clayton 
104f4d6de6aSGreg Clayton size_t
105f4d6de6aSGreg Clayton ObjectFilePECOFF::GetModuleSpecifications (const lldb_private::FileSpec& file,
106f4d6de6aSGreg Clayton                                            lldb::DataBufferSP& data_sp,
107f4d6de6aSGreg Clayton                                            lldb::offset_t data_offset,
108f4d6de6aSGreg Clayton                                            lldb::offset_t file_offset,
109f4d6de6aSGreg Clayton                                            lldb::offset_t length,
110f4d6de6aSGreg Clayton                                            lldb_private::ModuleSpecList &specs)
111f4d6de6aSGreg Clayton {
11289eb1baeSVirgile Bello     const size_t initial_count = specs.GetSize();
11389eb1baeSVirgile Bello 
11489eb1baeSVirgile Bello     if (ObjectFilePECOFF::MagicBytesMatch(data_sp))
11589eb1baeSVirgile Bello     {
11689eb1baeSVirgile Bello         DataExtractor data;
11789eb1baeSVirgile Bello         data.SetData(data_sp, data_offset, length);
11889eb1baeSVirgile Bello         data.SetByteOrder(eByteOrderLittle);
11989eb1baeSVirgile Bello 
12089eb1baeSVirgile Bello         dos_header_t dos_header;
12189eb1baeSVirgile Bello         coff_header_t coff_header;
12289eb1baeSVirgile Bello 
12389eb1baeSVirgile Bello         if (ParseDOSHeader(data, dos_header))
12489eb1baeSVirgile Bello         {
12589eb1baeSVirgile Bello             lldb::offset_t offset = dos_header.e_lfanew;
12689eb1baeSVirgile Bello             uint32_t pe_signature = data.GetU32(&offset);
12789eb1baeSVirgile Bello             if (pe_signature != IMAGE_NT_SIGNATURE)
12889eb1baeSVirgile Bello                 return false;
12989eb1baeSVirgile Bello             if (ParseCOFFHeader(data, &offset, coff_header))
13089eb1baeSVirgile Bello             {
13189eb1baeSVirgile Bello                 ArchSpec spec;
132*ad587ae4SZachary Turner                 llvm::Triple::ArchType archType = llvm::Triple::UnknownArch;
133*ad587ae4SZachary Turner                 if (coff_header.machine == MachineAmd64)
134*ad587ae4SZachary Turner                     spec.SetTriple("x86_64-pc-windows");
135*ad587ae4SZachary Turner                 else if (coff_header.machine == MachineX86)
136*ad587ae4SZachary Turner                     spec.SetTriple("i386-pc-windows");
13789eb1baeSVirgile Bello                 specs.Append(ModuleSpec(file, spec));
13889eb1baeSVirgile Bello             }
13989eb1baeSVirgile Bello         }
14089eb1baeSVirgile Bello     }
14189eb1baeSVirgile Bello 
14289eb1baeSVirgile Bello     return specs.GetSize() - initial_count;
143f4d6de6aSGreg Clayton }
144f4d6de6aSGreg Clayton 
145f4d6de6aSGreg Clayton 
146f754f88fSGreg Clayton bool
1475ce9c565SGreg Clayton ObjectFilePECOFF::MagicBytesMatch (DataBufferSP& data_sp)
148f754f88fSGreg Clayton {
1495ce9c565SGreg Clayton     DataExtractor data(data_sp, eByteOrderLittle, 4);
150c7bece56SGreg Clayton     lldb::offset_t offset = 0;
151f754f88fSGreg Clayton     uint16_t magic = data.GetU16 (&offset);
152f754f88fSGreg Clayton     return magic == IMAGE_DOS_SIGNATURE;
153f754f88fSGreg Clayton }
154f754f88fSGreg Clayton 
155f754f88fSGreg Clayton 
156e72dfb32SGreg Clayton ObjectFilePECOFF::ObjectFilePECOFF (const lldb::ModuleSP &module_sp,
1575ce9c565SGreg Clayton                                     DataBufferSP& data_sp,
1585ce9c565SGreg Clayton                                     lldb::offset_t data_offset,
159f754f88fSGreg Clayton                                     const FileSpec* file,
1605ce9c565SGreg Clayton                                     lldb::offset_t file_offset,
1615ce9c565SGreg Clayton                                     lldb::offset_t length) :
1625ce9c565SGreg Clayton     ObjectFile (module_sp, file, file_offset, length, data_sp, data_offset),
163f754f88fSGreg Clayton     m_dos_header (),
164f754f88fSGreg Clayton     m_coff_header (),
165f754f88fSGreg Clayton     m_coff_header_opt (),
166f754f88fSGreg Clayton     m_sect_headers ()
167f754f88fSGreg Clayton {
168f754f88fSGreg Clayton     ::memset (&m_dos_header, 0, sizeof(m_dos_header));
169f754f88fSGreg Clayton     ::memset (&m_coff_header, 0, sizeof(m_coff_header));
170f754f88fSGreg Clayton     ::memset (&m_coff_header_opt, 0, sizeof(m_coff_header_opt));
171f754f88fSGreg Clayton }
172f754f88fSGreg Clayton 
173f754f88fSGreg Clayton 
174f754f88fSGreg Clayton ObjectFilePECOFF::~ObjectFilePECOFF()
175f754f88fSGreg Clayton {
176f754f88fSGreg Clayton }
177f754f88fSGreg Clayton 
178f754f88fSGreg Clayton 
179f754f88fSGreg Clayton bool
180f754f88fSGreg Clayton ObjectFilePECOFF::ParseHeader ()
181f754f88fSGreg Clayton {
182a1743499SGreg Clayton     ModuleSP module_sp(GetModule());
183a1743499SGreg Clayton     if (module_sp)
184a1743499SGreg Clayton     {
185a1743499SGreg Clayton         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
186f754f88fSGreg Clayton         m_sect_headers.clear();
187f754f88fSGreg Clayton         m_data.SetByteOrder (eByteOrderLittle);
188c7bece56SGreg Clayton         lldb::offset_t offset = 0;
189f754f88fSGreg Clayton 
19089eb1baeSVirgile Bello         if (ParseDOSHeader(m_data, m_dos_header))
191f754f88fSGreg Clayton         {
192f754f88fSGreg Clayton             offset = m_dos_header.e_lfanew;
193f754f88fSGreg Clayton             uint32_t pe_signature = m_data.GetU32 (&offset);
194f754f88fSGreg Clayton             if (pe_signature != IMAGE_NT_SIGNATURE)
195f754f88fSGreg Clayton                 return false;
19689eb1baeSVirgile Bello             if (ParseCOFFHeader(m_data, &offset, m_coff_header))
197f754f88fSGreg Clayton             {
198f754f88fSGreg Clayton                 if (m_coff_header.hdrsize > 0)
199f754f88fSGreg Clayton                     ParseCOFFOptionalHeader(&offset);
200f754f88fSGreg Clayton                 ParseSectionHeaders (offset);
20128469ca3SGreg Clayton             }
202f754f88fSGreg Clayton             return true;
203f754f88fSGreg Clayton         }
204a1743499SGreg Clayton     }
205f754f88fSGreg Clayton     return false;
206f754f88fSGreg Clayton }
207f754f88fSGreg Clayton 
2082756adf3SVirgile Bello bool
2092756adf3SVirgile Bello ObjectFilePECOFF::SetLoadAddress(Target &target, addr_t value, bool value_is_offset)
2102756adf3SVirgile Bello {
2112756adf3SVirgile Bello     bool changed = false;
2122756adf3SVirgile Bello     ModuleSP module_sp = GetModule();
2132756adf3SVirgile Bello     if (module_sp)
2142756adf3SVirgile Bello     {
2152756adf3SVirgile Bello         size_t num_loaded_sections = 0;
2162756adf3SVirgile Bello         SectionList *section_list = GetSectionList ();
2172756adf3SVirgile Bello         if (section_list)
2182756adf3SVirgile Bello         {
2192756adf3SVirgile Bello             if (!value_is_offset)
2202756adf3SVirgile Bello             {
2212756adf3SVirgile Bello                 value -= m_image_base;
2222756adf3SVirgile Bello             }
2232756adf3SVirgile Bello 
2242756adf3SVirgile Bello             const size_t num_sections = section_list->GetSize();
2252756adf3SVirgile Bello             size_t sect_idx = 0;
2262756adf3SVirgile Bello 
2272756adf3SVirgile Bello             for (sect_idx = 0; sect_idx < num_sections; ++sect_idx)
2282756adf3SVirgile Bello             {
2292756adf3SVirgile Bello                 // Iterate through the object file sections to find all
2302756adf3SVirgile Bello                 // of the sections that have SHF_ALLOC in their flag bits.
2312756adf3SVirgile Bello                 SectionSP section_sp (section_list->GetSectionAtIndex (sect_idx));
2322756adf3SVirgile Bello                 if (section_sp && !section_sp->IsThreadSpecific())
2332756adf3SVirgile Bello                 {
2342756adf3SVirgile Bello                     if (target.GetSectionLoadList().SetSectionLoadAddress (section_sp, section_sp->GetFileAddress() + value))
2352756adf3SVirgile Bello                         ++num_loaded_sections;
2362756adf3SVirgile Bello                 }
2372756adf3SVirgile Bello             }
2382756adf3SVirgile Bello             changed = num_loaded_sections > 0;
2392756adf3SVirgile Bello         }
2402756adf3SVirgile Bello     }
2412756adf3SVirgile Bello     return changed;
2422756adf3SVirgile Bello }
2432756adf3SVirgile Bello 
244f754f88fSGreg Clayton 
245f754f88fSGreg Clayton ByteOrder
246f754f88fSGreg Clayton ObjectFilePECOFF::GetByteOrder () const
247f754f88fSGreg Clayton {
248f754f88fSGreg Clayton     return eByteOrderLittle;
249f754f88fSGreg Clayton }
250f754f88fSGreg Clayton 
251f754f88fSGreg Clayton bool
252f754f88fSGreg Clayton ObjectFilePECOFF::IsExecutable() const
253f754f88fSGreg Clayton {
254237ad974SCharles Davis     return (m_coff_header.flags & llvm::COFF::IMAGE_FILE_DLL) == 0;
255f754f88fSGreg Clayton }
256f754f88fSGreg Clayton 
257c7bece56SGreg Clayton uint32_t
258f754f88fSGreg Clayton ObjectFilePECOFF::GetAddressByteSize () const
259f754f88fSGreg Clayton {
260f754f88fSGreg Clayton     if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32_PLUS)
261f754f88fSGreg Clayton         return 8;
262f754f88fSGreg Clayton     else if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32)
263f754f88fSGreg Clayton         return 4;
264f754f88fSGreg Clayton     return 4;
265f754f88fSGreg Clayton }
266f754f88fSGreg Clayton 
267f754f88fSGreg Clayton //----------------------------------------------------------------------
268f754f88fSGreg Clayton // NeedsEndianSwap
269f754f88fSGreg Clayton //
270f754f88fSGreg Clayton // Return true if an endian swap needs to occur when extracting data
271f754f88fSGreg Clayton // from this file.
272f754f88fSGreg Clayton //----------------------------------------------------------------------
273f754f88fSGreg Clayton bool
274f754f88fSGreg Clayton ObjectFilePECOFF::NeedsEndianSwap() const
275f754f88fSGreg Clayton {
276f754f88fSGreg Clayton #if defined(__LITTLE_ENDIAN__)
277f754f88fSGreg Clayton     return false;
278f754f88fSGreg Clayton #else
279f754f88fSGreg Clayton     return true;
280f754f88fSGreg Clayton #endif
281f754f88fSGreg Clayton }
282f754f88fSGreg Clayton //----------------------------------------------------------------------
283f754f88fSGreg Clayton // ParseDOSHeader
284f754f88fSGreg Clayton //----------------------------------------------------------------------
285f754f88fSGreg Clayton bool
28689eb1baeSVirgile Bello ObjectFilePECOFF::ParseDOSHeader (DataExtractor &data, dos_header_t &dos_header)
287f754f88fSGreg Clayton {
288f754f88fSGreg Clayton     bool success = false;
289c7bece56SGreg Clayton     lldb::offset_t offset = 0;
29089eb1baeSVirgile Bello     success = data.ValidOffsetForDataOfSize(0, sizeof(dos_header));
291f754f88fSGreg Clayton 
292f754f88fSGreg Clayton     if (success)
293f754f88fSGreg Clayton     {
29489eb1baeSVirgile Bello         dos_header.e_magic = data.GetU16(&offset); // Magic number
29589eb1baeSVirgile Bello         success = dos_header.e_magic == IMAGE_DOS_SIGNATURE;
296f754f88fSGreg Clayton 
297f754f88fSGreg Clayton         if (success)
298f754f88fSGreg Clayton         {
29989eb1baeSVirgile Bello             dos_header.e_cblp     = data.GetU16(&offset); // Bytes on last page of file
30089eb1baeSVirgile Bello             dos_header.e_cp       = data.GetU16(&offset); // Pages in file
30189eb1baeSVirgile Bello             dos_header.e_crlc     = data.GetU16(&offset); // Relocations
30289eb1baeSVirgile Bello             dos_header.e_cparhdr  = data.GetU16(&offset); // Size of header in paragraphs
30389eb1baeSVirgile Bello             dos_header.e_minalloc = data.GetU16(&offset); // Minimum extra paragraphs needed
30489eb1baeSVirgile Bello             dos_header.e_maxalloc = data.GetU16(&offset); // Maximum extra paragraphs needed
30589eb1baeSVirgile Bello             dos_header.e_ss       = data.GetU16(&offset); // Initial (relative) SS value
30689eb1baeSVirgile Bello             dos_header.e_sp       = data.GetU16(&offset); // Initial SP value
30789eb1baeSVirgile Bello             dos_header.e_csum     = data.GetU16(&offset); // Checksum
30889eb1baeSVirgile Bello             dos_header.e_ip       = data.GetU16(&offset); // Initial IP value
30989eb1baeSVirgile Bello             dos_header.e_cs       = data.GetU16(&offset); // Initial (relative) CS value
31089eb1baeSVirgile Bello             dos_header.e_lfarlc   = data.GetU16(&offset); // File address of relocation table
31189eb1baeSVirgile Bello             dos_header.e_ovno     = data.GetU16(&offset); // Overlay number
312f754f88fSGreg Clayton 
31389eb1baeSVirgile Bello             dos_header.e_res[0]   = data.GetU16(&offset); // Reserved words
31489eb1baeSVirgile Bello             dos_header.e_res[1]   = data.GetU16(&offset); // Reserved words
31589eb1baeSVirgile Bello             dos_header.e_res[2]   = data.GetU16(&offset); // Reserved words
31689eb1baeSVirgile Bello             dos_header.e_res[3]   = data.GetU16(&offset); // Reserved words
317f754f88fSGreg Clayton 
31889eb1baeSVirgile Bello             dos_header.e_oemid    = data.GetU16(&offset); // OEM identifier (for e_oeminfo)
31989eb1baeSVirgile Bello             dos_header.e_oeminfo  = data.GetU16(&offset); // OEM information; e_oemid specific
32089eb1baeSVirgile Bello             dos_header.e_res2[0]  = data.GetU16(&offset); // Reserved words
32189eb1baeSVirgile Bello             dos_header.e_res2[1]  = data.GetU16(&offset); // Reserved words
32289eb1baeSVirgile Bello             dos_header.e_res2[2]  = data.GetU16(&offset); // Reserved words
32389eb1baeSVirgile Bello             dos_header.e_res2[3]  = data.GetU16(&offset); // Reserved words
32489eb1baeSVirgile Bello             dos_header.e_res2[4]  = data.GetU16(&offset); // Reserved words
32589eb1baeSVirgile Bello             dos_header.e_res2[5]  = data.GetU16(&offset); // Reserved words
32689eb1baeSVirgile Bello             dos_header.e_res2[6]  = data.GetU16(&offset); // Reserved words
32789eb1baeSVirgile Bello             dos_header.e_res2[7]  = data.GetU16(&offset); // Reserved words
32889eb1baeSVirgile Bello             dos_header.e_res2[8]  = data.GetU16(&offset); // Reserved words
32989eb1baeSVirgile Bello             dos_header.e_res2[9]  = data.GetU16(&offset); // Reserved words
330f754f88fSGreg Clayton 
33189eb1baeSVirgile Bello             dos_header.e_lfanew   = data.GetU32(&offset); // File address of new exe header
332f754f88fSGreg Clayton         }
333f754f88fSGreg Clayton     }
334f754f88fSGreg Clayton     if (!success)
33589eb1baeSVirgile Bello         memset(&dos_header, 0, sizeof(dos_header));
336f754f88fSGreg Clayton     return success;
337f754f88fSGreg Clayton }
338f754f88fSGreg Clayton 
339f754f88fSGreg Clayton 
340f754f88fSGreg Clayton //----------------------------------------------------------------------
341f754f88fSGreg Clayton // ParserCOFFHeader
342f754f88fSGreg Clayton //----------------------------------------------------------------------
343f754f88fSGreg Clayton bool
34489eb1baeSVirgile Bello ObjectFilePECOFF::ParseCOFFHeader(DataExtractor &data, lldb::offset_t *offset_ptr, coff_header_t &coff_header)
345f754f88fSGreg Clayton {
34689eb1baeSVirgile Bello     bool success = data.ValidOffsetForDataOfSize (*offset_ptr, sizeof(coff_header));
347f754f88fSGreg Clayton     if (success)
348f754f88fSGreg Clayton     {
34989eb1baeSVirgile Bello         coff_header.machine   = data.GetU16(offset_ptr);
35089eb1baeSVirgile Bello         coff_header.nsects    = data.GetU16(offset_ptr);
35189eb1baeSVirgile Bello         coff_header.modtime   = data.GetU32(offset_ptr);
35289eb1baeSVirgile Bello         coff_header.symoff    = data.GetU32(offset_ptr);
35389eb1baeSVirgile Bello         coff_header.nsyms     = data.GetU32(offset_ptr);
35489eb1baeSVirgile Bello         coff_header.hdrsize   = data.GetU16(offset_ptr);
35589eb1baeSVirgile Bello         coff_header.flags     = data.GetU16(offset_ptr);
356f754f88fSGreg Clayton     }
357f754f88fSGreg Clayton     if (!success)
35889eb1baeSVirgile Bello         memset(&coff_header, 0, sizeof(coff_header));
359f754f88fSGreg Clayton     return success;
360f754f88fSGreg Clayton }
361f754f88fSGreg Clayton 
362f754f88fSGreg Clayton bool
363c7bece56SGreg Clayton ObjectFilePECOFF::ParseCOFFOptionalHeader(lldb::offset_t *offset_ptr)
364f754f88fSGreg Clayton {
365f754f88fSGreg Clayton     bool success = false;
366c7bece56SGreg Clayton     const lldb::offset_t end_offset = *offset_ptr + m_coff_header.hdrsize;
367f754f88fSGreg Clayton     if (*offset_ptr < end_offset)
368f754f88fSGreg Clayton     {
369f754f88fSGreg Clayton         success = true;
370f754f88fSGreg Clayton         m_coff_header_opt.magic                         = m_data.GetU16(offset_ptr);
371f754f88fSGreg Clayton         m_coff_header_opt.major_linker_version          = m_data.GetU8 (offset_ptr);
372f754f88fSGreg Clayton         m_coff_header_opt.minor_linker_version          = m_data.GetU8 (offset_ptr);
373f754f88fSGreg Clayton         m_coff_header_opt.code_size                     = m_data.GetU32(offset_ptr);
374f754f88fSGreg Clayton         m_coff_header_opt.data_size                     = m_data.GetU32(offset_ptr);
375f754f88fSGreg Clayton         m_coff_header_opt.bss_size                      = m_data.GetU32(offset_ptr);
376f754f88fSGreg Clayton         m_coff_header_opt.entry                         = m_data.GetU32(offset_ptr);
377f754f88fSGreg Clayton         m_coff_header_opt.code_offset                   = m_data.GetU32(offset_ptr);
378f754f88fSGreg Clayton 
379f754f88fSGreg Clayton         const uint32_t addr_byte_size = GetAddressByteSize ();
380f754f88fSGreg Clayton 
381f754f88fSGreg Clayton         if (*offset_ptr < end_offset)
382f754f88fSGreg Clayton         {
383f754f88fSGreg Clayton             if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32)
384f754f88fSGreg Clayton             {
385f754f88fSGreg Clayton                 // PE32 only
386f754f88fSGreg Clayton                 m_coff_header_opt.data_offset               = m_data.GetU32(offset_ptr);
387f754f88fSGreg Clayton             }
388f754f88fSGreg Clayton             else
389f754f88fSGreg Clayton                 m_coff_header_opt.data_offset = 0;
390f754f88fSGreg Clayton 
391f754f88fSGreg Clayton             if (*offset_ptr < end_offset)
392f754f88fSGreg Clayton             {
393f754f88fSGreg Clayton                 m_coff_header_opt.image_base                    = m_data.GetMaxU64 (offset_ptr, addr_byte_size);
394f754f88fSGreg Clayton                 m_coff_header_opt.sect_alignment                = m_data.GetU32(offset_ptr);
395f754f88fSGreg Clayton                 m_coff_header_opt.file_alignment                = m_data.GetU32(offset_ptr);
396f754f88fSGreg Clayton                 m_coff_header_opt.major_os_system_version       = m_data.GetU16(offset_ptr);
397f754f88fSGreg Clayton                 m_coff_header_opt.minor_os_system_version       = m_data.GetU16(offset_ptr);
398f754f88fSGreg Clayton                 m_coff_header_opt.major_image_version           = m_data.GetU16(offset_ptr);
399f754f88fSGreg Clayton                 m_coff_header_opt.minor_image_version           = m_data.GetU16(offset_ptr);
400f754f88fSGreg Clayton                 m_coff_header_opt.major_subsystem_version       = m_data.GetU16(offset_ptr);
401f754f88fSGreg Clayton                 m_coff_header_opt.minor_subsystem_version       = m_data.GetU16(offset_ptr);
402f754f88fSGreg Clayton                 m_coff_header_opt.reserved1                     = m_data.GetU32(offset_ptr);
403f754f88fSGreg Clayton                 m_coff_header_opt.image_size                    = m_data.GetU32(offset_ptr);
404f754f88fSGreg Clayton                 m_coff_header_opt.header_size                   = m_data.GetU32(offset_ptr);
40528469ca3SGreg Clayton                 m_coff_header_opt.checksum                      = m_data.GetU32(offset_ptr);
406f754f88fSGreg Clayton                 m_coff_header_opt.subsystem                     = m_data.GetU16(offset_ptr);
407f754f88fSGreg Clayton                 m_coff_header_opt.dll_flags                     = m_data.GetU16(offset_ptr);
408f754f88fSGreg Clayton                 m_coff_header_opt.stack_reserve_size            = m_data.GetMaxU64 (offset_ptr, addr_byte_size);
409f754f88fSGreg Clayton                 m_coff_header_opt.stack_commit_size             = m_data.GetMaxU64 (offset_ptr, addr_byte_size);
410f754f88fSGreg Clayton                 m_coff_header_opt.heap_reserve_size             = m_data.GetMaxU64 (offset_ptr, addr_byte_size);
411f754f88fSGreg Clayton                 m_coff_header_opt.heap_commit_size              = m_data.GetMaxU64 (offset_ptr, addr_byte_size);
412f754f88fSGreg Clayton                 m_coff_header_opt.loader_flags                  = m_data.GetU32(offset_ptr);
413f754f88fSGreg Clayton                 uint32_t num_data_dir_entries = m_data.GetU32(offset_ptr);
414f754f88fSGreg Clayton                 m_coff_header_opt.data_dirs.clear();
415f754f88fSGreg Clayton                 m_coff_header_opt.data_dirs.resize(num_data_dir_entries);
416f754f88fSGreg Clayton                 uint32_t i;
417f754f88fSGreg Clayton                 for (i=0; i<num_data_dir_entries; i++)
418f754f88fSGreg Clayton                 {
419f754f88fSGreg Clayton                     m_coff_header_opt.data_dirs[i].vmaddr = m_data.GetU32(offset_ptr);
420f754f88fSGreg Clayton                     m_coff_header_opt.data_dirs[i].vmsize = m_data.GetU32(offset_ptr);
421f754f88fSGreg Clayton                 }
4222756adf3SVirgile Bello 
4232756adf3SVirgile Bello                 m_file_offset = m_coff_header_opt.image_base;
4242756adf3SVirgile Bello                 m_image_base = m_coff_header_opt.image_base;
425f754f88fSGreg Clayton             }
426f754f88fSGreg Clayton         }
427f754f88fSGreg Clayton     }
428f754f88fSGreg Clayton     // Make sure we are on track for section data which follows
429f754f88fSGreg Clayton     *offset_ptr = end_offset;
430f754f88fSGreg Clayton     return success;
431f754f88fSGreg Clayton }
432f754f88fSGreg Clayton 
433f754f88fSGreg Clayton 
434f754f88fSGreg Clayton //----------------------------------------------------------------------
435f754f88fSGreg Clayton // ParseSectionHeaders
436f754f88fSGreg Clayton //----------------------------------------------------------------------
437f754f88fSGreg Clayton bool
438f754f88fSGreg Clayton ObjectFilePECOFF::ParseSectionHeaders (uint32_t section_header_data_offset)
439f754f88fSGreg Clayton {
440f754f88fSGreg Clayton     const uint32_t nsects = m_coff_header.nsects;
441f754f88fSGreg Clayton     m_sect_headers.clear();
442f754f88fSGreg Clayton 
443f754f88fSGreg Clayton     if (nsects > 0)
444f754f88fSGreg Clayton     {
445f754f88fSGreg Clayton         const uint32_t addr_byte_size = GetAddressByteSize ();
446f754f88fSGreg Clayton         const size_t section_header_byte_size = nsects * sizeof(section_header_t);
447f754f88fSGreg Clayton         DataBufferSP section_header_data_sp(m_file.ReadFileContents (section_header_data_offset, section_header_byte_size));
448f754f88fSGreg Clayton         DataExtractor section_header_data (section_header_data_sp, GetByteOrder(), addr_byte_size);
449f754f88fSGreg Clayton 
450c7bece56SGreg Clayton         lldb::offset_t offset = 0;
451f754f88fSGreg Clayton         if (section_header_data.ValidOffsetForDataOfSize (offset, section_header_byte_size))
452f754f88fSGreg Clayton         {
453f754f88fSGreg Clayton             m_sect_headers.resize(nsects);
454f754f88fSGreg Clayton 
455f754f88fSGreg Clayton             for (uint32_t idx = 0; idx<nsects; ++idx)
456f754f88fSGreg Clayton             {
457f754f88fSGreg Clayton                 const void *name_data = section_header_data.GetData(&offset, 8);
458f754f88fSGreg Clayton                 if (name_data)
459f754f88fSGreg Clayton                 {
460f754f88fSGreg Clayton                     memcpy(m_sect_headers[idx].name, name_data, 8);
461f754f88fSGreg Clayton                     m_sect_headers[idx].vmsize  = section_header_data.GetU32(&offset);
462f754f88fSGreg Clayton                     m_sect_headers[idx].vmaddr  = section_header_data.GetU32(&offset);
463f754f88fSGreg Clayton                     m_sect_headers[idx].size    = section_header_data.GetU32(&offset);
464f754f88fSGreg Clayton                     m_sect_headers[idx].offset  = section_header_data.GetU32(&offset);
465f754f88fSGreg Clayton                     m_sect_headers[idx].reloff  = section_header_data.GetU32(&offset);
466f754f88fSGreg Clayton                     m_sect_headers[idx].lineoff = section_header_data.GetU32(&offset);
467f754f88fSGreg Clayton                     m_sect_headers[idx].nreloc  = section_header_data.GetU16(&offset);
468f754f88fSGreg Clayton                     m_sect_headers[idx].nline   = section_header_data.GetU16(&offset);
469f754f88fSGreg Clayton                     m_sect_headers[idx].flags   = section_header_data.GetU32(&offset);
470f754f88fSGreg Clayton                 }
471f754f88fSGreg Clayton             }
472f754f88fSGreg Clayton         }
473f754f88fSGreg Clayton     }
474f754f88fSGreg Clayton 
475f754f88fSGreg Clayton     return m_sect_headers.empty() == false;
476f754f88fSGreg Clayton }
477f754f88fSGreg Clayton 
478f754f88fSGreg Clayton bool
479f754f88fSGreg Clayton ObjectFilePECOFF::GetSectionName(std::string& sect_name, const section_header_t& sect)
480f754f88fSGreg Clayton {
481f754f88fSGreg Clayton     if (sect.name[0] == '/')
482f754f88fSGreg Clayton     {
483c7bece56SGreg Clayton         lldb::offset_t stroff = strtoul(&sect.name[1], NULL, 10);
484c7bece56SGreg Clayton         lldb::offset_t string_file_offset = m_coff_header.symoff + (m_coff_header.nsyms * 18) + stroff;
485f754f88fSGreg Clayton         const char *name = m_data.GetCStr (&string_file_offset);
486f754f88fSGreg Clayton         if (name)
487f754f88fSGreg Clayton         {
488f754f88fSGreg Clayton             sect_name = name;
489f754f88fSGreg Clayton             return true;
490f754f88fSGreg Clayton         }
491f754f88fSGreg Clayton 
492f754f88fSGreg Clayton         return false;
493f754f88fSGreg Clayton     }
494f754f88fSGreg Clayton     sect_name = sect.name;
495f754f88fSGreg Clayton     return true;
496f754f88fSGreg Clayton }
497f754f88fSGreg Clayton 
498f754f88fSGreg Clayton //----------------------------------------------------------------------
499f754f88fSGreg Clayton // GetNListSymtab
500f754f88fSGreg Clayton //----------------------------------------------------------------------
501f754f88fSGreg Clayton Symtab *
5023046e668SGreg Clayton ObjectFilePECOFF::GetSymtab()
503f754f88fSGreg Clayton {
504a1743499SGreg Clayton     ModuleSP module_sp(GetModule());
505a1743499SGreg Clayton     if (module_sp)
506a1743499SGreg Clayton     {
507a1743499SGreg Clayton         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
508f754f88fSGreg Clayton         if (m_symtab_ap.get() == NULL)
509f754f88fSGreg Clayton         {
510f754f88fSGreg Clayton             SectionList *sect_list = GetSectionList();
511f754f88fSGreg Clayton             m_symtab_ap.reset(new Symtab(this));
512f754f88fSGreg Clayton             Mutex::Locker symtab_locker (m_symtab_ap->GetMutex());
51328469ca3SGreg Clayton 
51428469ca3SGreg Clayton             const uint32_t num_syms = m_coff_header.nsyms;
51528469ca3SGreg Clayton 
51628469ca3SGreg Clayton             if (num_syms > 0 && m_coff_header.symoff > 0)
517f754f88fSGreg Clayton             {
5180076e715SGreg Clayton                 const uint32_t symbol_size = 18;
51928469ca3SGreg Clayton                 const uint32_t addr_byte_size = GetAddressByteSize ();
52028469ca3SGreg Clayton                 const size_t symbol_data_size = num_syms * symbol_size;
52128469ca3SGreg Clayton                 // Include the 4 bytes string table size at the end of the symbols
52228469ca3SGreg Clayton                 DataBufferSP symtab_data_sp(m_file.ReadFileContents (m_coff_header.symoff, symbol_data_size + 4));
52328469ca3SGreg Clayton                 DataExtractor symtab_data (symtab_data_sp, GetByteOrder(), addr_byte_size);
524c7bece56SGreg Clayton                 lldb::offset_t offset = symbol_data_size;
52528469ca3SGreg Clayton                 const uint32_t strtab_size = symtab_data.GetU32 (&offset);
5260076e715SGreg Clayton                 DataBufferSP strtab_data_sp(m_file.ReadFileContents (m_coff_header.symoff + symbol_data_size, strtab_size));
52728469ca3SGreg Clayton                 DataExtractor strtab_data (strtab_data_sp, GetByteOrder(), addr_byte_size);
52828469ca3SGreg Clayton 
5290076e715SGreg Clayton                 // First 4 bytes should be zeroed after strtab_size has been read,
5300076e715SGreg Clayton                 // because it is used as offset 0 to encode a NULL string.
5310076e715SGreg Clayton                 uint32_t* strtab_data_start = (uint32_t*)strtab_data_sp->GetBytes();
5320076e715SGreg Clayton                 strtab_data_start[0] = 0;
5330076e715SGreg Clayton 
53428469ca3SGreg Clayton                 offset = 0;
53528469ca3SGreg Clayton                 std::string symbol_name;
536f754f88fSGreg Clayton                 Symbol *symbols = m_symtab_ap->Resize (num_syms);
537f754f88fSGreg Clayton                 for (uint32_t i=0; i<num_syms; ++i)
538f754f88fSGreg Clayton                 {
539f754f88fSGreg Clayton                     coff_symbol_t symbol;
54028469ca3SGreg Clayton                     const uint32_t symbol_offset = offset;
54128469ca3SGreg Clayton                     const char *symbol_name_cstr = NULL;
54228469ca3SGreg Clayton                     // If the first 4 bytes of the symbol string are zero, then we
54328469ca3SGreg Clayton                     // it is followed by a 4 byte string table offset. Else these
54428469ca3SGreg Clayton                     // 8 bytes contain the symbol name
54528469ca3SGreg Clayton                     if (symtab_data.GetU32 (&offset) == 0)
54628469ca3SGreg Clayton                     {
54728469ca3SGreg Clayton                         // Long string that doesn't fit into the symbol table name,
54828469ca3SGreg Clayton                         // so now we must read the 4 byte string table offset
54928469ca3SGreg Clayton                         uint32_t strtab_offset = symtab_data.GetU32 (&offset);
55028469ca3SGreg Clayton                         symbol_name_cstr = strtab_data.PeekCStr (strtab_offset);
55128469ca3SGreg Clayton                         symbol_name.assign (symbol_name_cstr);
55228469ca3SGreg Clayton                     }
55328469ca3SGreg Clayton                     else
55428469ca3SGreg Clayton                     {
55528469ca3SGreg Clayton                         // Short string that fits into the symbol table name which is 8 bytes
55628469ca3SGreg Clayton                         offset += sizeof(symbol.name) - 4; // Skip remaining
55728469ca3SGreg Clayton                         symbol_name_cstr = symtab_data.PeekCStr (symbol_offset);
55828469ca3SGreg Clayton                         if (symbol_name_cstr == NULL)
559f754f88fSGreg Clayton                             break;
56028469ca3SGreg Clayton                         symbol_name.assign (symbol_name_cstr, sizeof(symbol.name));
56128469ca3SGreg Clayton                     }
56228469ca3SGreg Clayton                     symbol.value    = symtab_data.GetU32 (&offset);
56328469ca3SGreg Clayton                     symbol.sect     = symtab_data.GetU16 (&offset);
56428469ca3SGreg Clayton                     symbol.type     = symtab_data.GetU16 (&offset);
56528469ca3SGreg Clayton                     symbol.storage  = symtab_data.GetU8  (&offset);
56628469ca3SGreg Clayton                     symbol.naux     = symtab_data.GetU8  (&offset);
567037520e9SGreg Clayton                     symbols[i].GetMangled ().SetValue (ConstString(symbol_name.c_str()));
5680076e715SGreg Clayton                     if ((int16_t)symbol.sect >= 1)
5690076e715SGreg Clayton                     {
5700076e715SGreg Clayton                         Address symbol_addr(sect_list->GetSectionAtIndex(symbol.sect-1), symbol.value);
571e7612134SGreg Clayton                         symbols[i].GetAddress() = symbol_addr;
5720076e715SGreg Clayton                     }
573f754f88fSGreg Clayton 
574f754f88fSGreg Clayton                     if (symbol.naux > 0)
5750076e715SGreg Clayton                     {
576f754f88fSGreg Clayton                         i += symbol.naux;
5770076e715SGreg Clayton                         offset += symbol_size;
5780076e715SGreg Clayton                     }
579f754f88fSGreg Clayton                 }
580f754f88fSGreg Clayton 
581f754f88fSGreg Clayton             }
582a4fe3a12SVirgile Bello 
583a4fe3a12SVirgile Bello             // Read export header
584a4fe3a12SVirgile Bello             if (coff_data_dir_export_table < m_coff_header_opt.data_dirs.size()
585a4fe3a12SVirgile 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)
586a4fe3a12SVirgile Bello             {
587a4fe3a12SVirgile Bello                 export_directory_entry export_table;
588a4fe3a12SVirgile Bello                 uint32_t data_start = m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmaddr;
589a4fe3a12SVirgile Bello                 Address address(m_coff_header_opt.image_base + data_start, sect_list);
590a4fe3a12SVirgile Bello                 DataBufferSP symtab_data_sp(m_file.ReadFileContents(address.GetSection()->GetFileOffset() + address.GetOffset(), m_coff_header_opt.data_dirs[0].vmsize));
591a4fe3a12SVirgile Bello                 DataExtractor symtab_data (symtab_data_sp, GetByteOrder(), GetAddressByteSize());
592a4fe3a12SVirgile Bello                 lldb::offset_t offset = 0;
593a4fe3a12SVirgile Bello 
594a4fe3a12SVirgile Bello                 // Read export_table header
595a4fe3a12SVirgile Bello                 export_table.characteristics = symtab_data.GetU32(&offset);
596a4fe3a12SVirgile Bello                 export_table.time_date_stamp = symtab_data.GetU32(&offset);
597a4fe3a12SVirgile Bello                 export_table.major_version = symtab_data.GetU16(&offset);
598a4fe3a12SVirgile Bello                 export_table.minor_version = symtab_data.GetU16(&offset);
599a4fe3a12SVirgile Bello                 export_table.name = symtab_data.GetU32(&offset);
600a4fe3a12SVirgile Bello                 export_table.base = symtab_data.GetU32(&offset);
601a4fe3a12SVirgile Bello                 export_table.number_of_functions = symtab_data.GetU32(&offset);
602a4fe3a12SVirgile Bello                 export_table.number_of_names = symtab_data.GetU32(&offset);
603a4fe3a12SVirgile Bello                 export_table.address_of_functions = symtab_data.GetU32(&offset);
604a4fe3a12SVirgile Bello                 export_table.address_of_names = symtab_data.GetU32(&offset);
605a4fe3a12SVirgile Bello                 export_table.address_of_name_ordinals = symtab_data.GetU32(&offset);
606a4fe3a12SVirgile Bello 
607a4fe3a12SVirgile Bello                 bool has_ordinal = export_table.address_of_name_ordinals != 0;
608a4fe3a12SVirgile Bello 
609a4fe3a12SVirgile Bello                 lldb::offset_t name_offset = export_table.address_of_names - data_start;
610a4fe3a12SVirgile Bello                 lldb::offset_t name_ordinal_offset = export_table.address_of_name_ordinals - data_start;
611a4fe3a12SVirgile Bello 
612a4fe3a12SVirgile Bello                 Symbol *symbols = m_symtab_ap->Resize(export_table.number_of_names);
613a4fe3a12SVirgile Bello 
614a4fe3a12SVirgile Bello                 std::string symbol_name;
615a4fe3a12SVirgile Bello 
616a4fe3a12SVirgile Bello                 // Read each export table entry
617a4fe3a12SVirgile Bello                 for (size_t i = 0; i < export_table.number_of_names; ++i)
618a4fe3a12SVirgile Bello                 {
619a4fe3a12SVirgile Bello                     uint32_t name_ordinal = has_ordinal ? symtab_data.GetU16(&name_ordinal_offset) : i;
620a4fe3a12SVirgile Bello                     uint32_t name_address = symtab_data.GetU32(&name_offset);
621a4fe3a12SVirgile Bello 
622a4fe3a12SVirgile Bello                     const char* symbol_name_cstr = symtab_data.PeekCStr(name_address - data_start);
623a4fe3a12SVirgile Bello                     symbol_name.assign(symbol_name_cstr);
624a4fe3a12SVirgile Bello 
625a4fe3a12SVirgile Bello                     lldb::offset_t function_offset = export_table.address_of_functions - data_start + sizeof(uint32_t) * name_ordinal;
626a4fe3a12SVirgile Bello                     uint32_t function_rva = symtab_data.GetU32(&function_offset);
627a4fe3a12SVirgile Bello 
628a4fe3a12SVirgile Bello                     Address symbol_addr(m_coff_header_opt.image_base + function_rva, sect_list);
629a4fe3a12SVirgile Bello                     symbols[i].GetMangled().SetValue(ConstString(symbol_name.c_str()));
630a4fe3a12SVirgile Bello                     symbols[i].GetAddress() = symbol_addr;
631a4fe3a12SVirgile Bello                     symbols[i].SetType(lldb::eSymbolTypeCode);
632a4fe3a12SVirgile Bello                     symbols[i].SetDebug(true);
633a4fe3a12SVirgile Bello                 }
634a4fe3a12SVirgile Bello             }
635f754f88fSGreg Clayton         }
636a1743499SGreg Clayton     }
637f754f88fSGreg Clayton     return m_symtab_ap.get();
638f754f88fSGreg Clayton 
639f754f88fSGreg Clayton }
640f754f88fSGreg Clayton 
6413046e668SGreg Clayton bool
6423046e668SGreg Clayton ObjectFilePECOFF::IsStripped ()
643f754f88fSGreg Clayton {
6443046e668SGreg Clayton     // TODO: determine this for COFF
6453046e668SGreg Clayton     return false;
6463046e668SGreg Clayton }
6473046e668SGreg Clayton 
6483046e668SGreg Clayton 
6493046e668SGreg Clayton 
6503046e668SGreg Clayton void
6513046e668SGreg Clayton ObjectFilePECOFF::CreateSections (SectionList &unified_section_list)
6523046e668SGreg Clayton {
6533046e668SGreg Clayton     if (!m_sections_ap.get())
6543046e668SGreg Clayton     {
6553046e668SGreg Clayton         m_sections_ap.reset(new SectionList());
6563046e668SGreg Clayton 
657a1743499SGreg Clayton         ModuleSP module_sp(GetModule());
658a1743499SGreg Clayton         if (module_sp)
659a1743499SGreg Clayton         {
660a1743499SGreg Clayton             lldb_private::Mutex::Locker locker(module_sp->GetMutex());
661f754f88fSGreg Clayton             const uint32_t nsects = m_sect_headers.size();
662e72dfb32SGreg Clayton             ModuleSP module_sp (GetModule());
663f754f88fSGreg Clayton             for (uint32_t idx = 0; idx<nsects; ++idx)
664f754f88fSGreg Clayton             {
665f754f88fSGreg Clayton                 std::string sect_name;
666f754f88fSGreg Clayton                 GetSectionName (sect_name, m_sect_headers[idx]);
667f754f88fSGreg Clayton                 ConstString const_sect_name (sect_name.c_str());
66828469ca3SGreg Clayton                 static ConstString g_code_sect_name (".code");
66928469ca3SGreg Clayton                 static ConstString g_CODE_sect_name ("CODE");
67028469ca3SGreg Clayton                 static ConstString g_data_sect_name (".data");
67128469ca3SGreg Clayton                 static ConstString g_DATA_sect_name ("DATA");
67228469ca3SGreg Clayton                 static ConstString g_bss_sect_name (".bss");
67328469ca3SGreg Clayton                 static ConstString g_BSS_sect_name ("BSS");
67428469ca3SGreg Clayton                 static ConstString g_debug_sect_name (".debug");
67528469ca3SGreg Clayton                 static ConstString g_reloc_sect_name (".reloc");
67628469ca3SGreg Clayton                 static ConstString g_stab_sect_name (".stab");
67728469ca3SGreg Clayton                 static ConstString g_stabstr_sect_name (".stabstr");
6780076e715SGreg Clayton                 static ConstString g_sect_name_dwarf_debug_abbrev (".debug_abbrev");
6790076e715SGreg Clayton                 static ConstString g_sect_name_dwarf_debug_aranges (".debug_aranges");
6800076e715SGreg Clayton                 static ConstString g_sect_name_dwarf_debug_frame (".debug_frame");
6810076e715SGreg Clayton                 static ConstString g_sect_name_dwarf_debug_info (".debug_info");
6820076e715SGreg Clayton                 static ConstString g_sect_name_dwarf_debug_line (".debug_line");
6830076e715SGreg Clayton                 static ConstString g_sect_name_dwarf_debug_loc (".debug_loc");
6840076e715SGreg Clayton                 static ConstString g_sect_name_dwarf_debug_macinfo (".debug_macinfo");
6850076e715SGreg Clayton                 static ConstString g_sect_name_dwarf_debug_pubnames (".debug_pubnames");
6860076e715SGreg Clayton                 static ConstString g_sect_name_dwarf_debug_pubtypes (".debug_pubtypes");
6870076e715SGreg Clayton                 static ConstString g_sect_name_dwarf_debug_ranges (".debug_ranges");
6880076e715SGreg Clayton                 static ConstString g_sect_name_dwarf_debug_str (".debug_str");
6890076e715SGreg Clayton                 static ConstString g_sect_name_eh_frame (".eh_frame");
69028469ca3SGreg Clayton                 SectionType section_type = eSectionTypeOther;
691237ad974SCharles Davis                 if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_CODE &&
69228469ca3SGreg Clayton                     ((const_sect_name == g_code_sect_name) || (const_sect_name == g_CODE_sect_name)))
69328469ca3SGreg Clayton                 {
69428469ca3SGreg Clayton                     section_type = eSectionTypeCode;
69528469ca3SGreg Clayton                 }
696237ad974SCharles Davis                 else if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_INITIALIZED_DATA &&
69728469ca3SGreg Clayton                          ((const_sect_name == g_data_sect_name) || (const_sect_name == g_DATA_sect_name)))
69828469ca3SGreg Clayton                 {
69928469ca3SGreg Clayton                     section_type = eSectionTypeData;
70028469ca3SGreg Clayton                 }
701237ad974SCharles Davis                 else if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA &&
70228469ca3SGreg Clayton                          ((const_sect_name == g_bss_sect_name) || (const_sect_name == g_BSS_sect_name)))
70328469ca3SGreg Clayton                 {
70428469ca3SGreg Clayton                     if (m_sect_headers[idx].size == 0)
70528469ca3SGreg Clayton                         section_type = eSectionTypeZeroFill;
70628469ca3SGreg Clayton                     else
70728469ca3SGreg Clayton                         section_type = eSectionTypeData;
70828469ca3SGreg Clayton                 }
70928469ca3SGreg Clayton                 else if (const_sect_name == g_debug_sect_name)
71028469ca3SGreg Clayton                 {
71128469ca3SGreg Clayton                     section_type = eSectionTypeDebug;
71228469ca3SGreg Clayton                 }
71328469ca3SGreg Clayton                 else if (const_sect_name == g_stabstr_sect_name)
71428469ca3SGreg Clayton                 {
71528469ca3SGreg Clayton                     section_type = eSectionTypeDataCString;
71628469ca3SGreg Clayton                 }
71728469ca3SGreg Clayton                 else if (const_sect_name == g_reloc_sect_name)
71828469ca3SGreg Clayton                 {
71928469ca3SGreg Clayton                     section_type = eSectionTypeOther;
72028469ca3SGreg Clayton                 }
7210076e715SGreg Clayton                 else if (const_sect_name == g_sect_name_dwarf_debug_abbrev)    section_type = eSectionTypeDWARFDebugAbbrev;
7220076e715SGreg Clayton                 else if (const_sect_name == g_sect_name_dwarf_debug_aranges)   section_type = eSectionTypeDWARFDebugAranges;
7230076e715SGreg Clayton                 else if (const_sect_name == g_sect_name_dwarf_debug_frame)     section_type = eSectionTypeDWARFDebugFrame;
7240076e715SGreg Clayton                 else if (const_sect_name == g_sect_name_dwarf_debug_info)      section_type = eSectionTypeDWARFDebugInfo;
7250076e715SGreg Clayton                 else if (const_sect_name == g_sect_name_dwarf_debug_line)      section_type = eSectionTypeDWARFDebugLine;
7260076e715SGreg Clayton                 else if (const_sect_name == g_sect_name_dwarf_debug_loc)       section_type = eSectionTypeDWARFDebugLoc;
7270076e715SGreg Clayton                 else if (const_sect_name == g_sect_name_dwarf_debug_macinfo)   section_type = eSectionTypeDWARFDebugMacInfo;
7280076e715SGreg Clayton                 else if (const_sect_name == g_sect_name_dwarf_debug_pubnames)  section_type = eSectionTypeDWARFDebugPubNames;
7290076e715SGreg Clayton                 else if (const_sect_name == g_sect_name_dwarf_debug_pubtypes)  section_type = eSectionTypeDWARFDebugPubTypes;
7300076e715SGreg Clayton                 else if (const_sect_name == g_sect_name_dwarf_debug_ranges)    section_type = eSectionTypeDWARFDebugRanges;
7310076e715SGreg Clayton                 else if (const_sect_name == g_sect_name_dwarf_debug_str)       section_type = eSectionTypeDWARFDebugStr;
7320076e715SGreg Clayton                 else if (const_sect_name == g_sect_name_eh_frame)              section_type = eSectionTypeEHFrame;
733237ad974SCharles Davis                 else if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_CODE)
73428469ca3SGreg Clayton                 {
73528469ca3SGreg Clayton                     section_type = eSectionTypeCode;
73628469ca3SGreg Clayton                 }
737237ad974SCharles Davis                 else if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)
73828469ca3SGreg Clayton                 {
73928469ca3SGreg Clayton                     section_type = eSectionTypeData;
74028469ca3SGreg Clayton                 }
741237ad974SCharles Davis                 else if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)
74228469ca3SGreg Clayton                 {
74328469ca3SGreg Clayton                     if (m_sect_headers[idx].size == 0)
74428469ca3SGreg Clayton                         section_type = eSectionTypeZeroFill;
74528469ca3SGreg Clayton                     else
74628469ca3SGreg Clayton                         section_type = eSectionTypeData;
74728469ca3SGreg Clayton                 }
748f754f88fSGreg Clayton 
749f754f88fSGreg Clayton                 // Use a segment ID of the segment index shifted left by 8 so they
750f754f88fSGreg Clayton                 // never conflict with any of the sections.
751e72dfb32SGreg Clayton                 SectionSP section_sp (new Section (module_sp,                    // Module to which this section belongs
752a7499c98SMichael Sartain                                                    this,                         // Object file to which this section belongs
753f754f88fSGreg 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
754f754f88fSGreg Clayton                                                    const_sect_name,              // Name of this section
75528469ca3SGreg Clayton                                                    section_type,                 // This section is a container of other sections.
7560076e715SGreg Clayton                                                    m_coff_header_opt.image_base + m_sect_headers[idx].vmaddr,   // File VM address == addresses as they are found in the object file
757f754f88fSGreg Clayton                                                    m_sect_headers[idx].vmsize,   // VM size in bytes of this section
758f754f88fSGreg Clayton                                                    m_sect_headers[idx].offset,   // Offset to the data for this section in the file
759aaa0ba31SBruce Mitchener                                                    m_sect_headers[idx].size,     // Size in bytes of this section as found in the file
76048672afbSGreg Clayton                                                    m_coff_header_opt.sect_alignment, // Section alignment
761f754f88fSGreg Clayton                                                    m_sect_headers[idx].flags));  // Flags for this section
762f754f88fSGreg Clayton 
763f754f88fSGreg Clayton                 //section_sp->SetIsEncrypted (segment_is_encrypted);
764f754f88fSGreg Clayton 
7653046e668SGreg Clayton                 unified_section_list.AddSection(section_sp);
766f754f88fSGreg Clayton                 m_sections_ap->AddSection (section_sp);
767f754f88fSGreg Clayton             }
768f754f88fSGreg Clayton         }
769a1743499SGreg Clayton     }
770f754f88fSGreg Clayton }
771f754f88fSGreg Clayton 
772f754f88fSGreg Clayton bool
773f754f88fSGreg Clayton ObjectFilePECOFF::GetUUID (UUID* uuid)
774f754f88fSGreg Clayton {
775f754f88fSGreg Clayton     return false;
776f754f88fSGreg Clayton }
777f754f88fSGreg Clayton 
778f754f88fSGreg Clayton uint32_t
779f754f88fSGreg Clayton ObjectFilePECOFF::GetDependentModules (FileSpecList& files)
780f754f88fSGreg Clayton {
781f754f88fSGreg Clayton     return 0;
782f754f88fSGreg Clayton }
783f754f88fSGreg Clayton 
784f754f88fSGreg Clayton 
785f754f88fSGreg Clayton //----------------------------------------------------------------------
786f754f88fSGreg Clayton // Dump
787f754f88fSGreg Clayton //
788f754f88fSGreg Clayton // Dump the specifics of the runtime file container (such as any headers
789f754f88fSGreg Clayton // segments, sections, etc).
790f754f88fSGreg Clayton //----------------------------------------------------------------------
791f754f88fSGreg Clayton void
792f754f88fSGreg Clayton ObjectFilePECOFF::Dump(Stream *s)
793f754f88fSGreg Clayton {
794a1743499SGreg Clayton     ModuleSP module_sp(GetModule());
795a1743499SGreg Clayton     if (module_sp)
796a1743499SGreg Clayton     {
797a1743499SGreg Clayton         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
798324a1036SSaleem Abdulrasool         s->Printf("%p: ", static_cast<void*>(this));
799f754f88fSGreg Clayton         s->Indent();
800f754f88fSGreg Clayton         s->PutCString("ObjectFilePECOFF");
801f754f88fSGreg Clayton 
802f754f88fSGreg Clayton         ArchSpec header_arch;
803f754f88fSGreg Clayton         GetArchitecture (header_arch);
804f754f88fSGreg Clayton 
805f754f88fSGreg Clayton         *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
806f754f88fSGreg Clayton 
8073046e668SGreg Clayton         SectionList *sections = GetSectionList();
8083046e668SGreg Clayton         if (sections)
8093046e668SGreg Clayton             sections->Dump(s, NULL, true, UINT32_MAX);
810f754f88fSGreg Clayton 
811f754f88fSGreg Clayton         if (m_symtab_ap.get())
812f754f88fSGreg Clayton             m_symtab_ap->Dump(s, NULL, eSortOrderNone);
813f754f88fSGreg Clayton 
814f754f88fSGreg Clayton         if (m_dos_header.e_magic)
815f754f88fSGreg Clayton             DumpDOSHeader (s, m_dos_header);
816f754f88fSGreg Clayton         if (m_coff_header.machine)
817f754f88fSGreg Clayton         {
818f754f88fSGreg Clayton             DumpCOFFHeader (s, m_coff_header);
819f754f88fSGreg Clayton             if (m_coff_header.hdrsize)
820f754f88fSGreg Clayton                 DumpOptCOFFHeader (s, m_coff_header_opt);
821f754f88fSGreg Clayton         }
822f754f88fSGreg Clayton         s->EOL();
823f754f88fSGreg Clayton         DumpSectionHeaders(s);
824f754f88fSGreg Clayton         s->EOL();
825f754f88fSGreg Clayton     }
826a1743499SGreg Clayton }
827f754f88fSGreg Clayton 
828f754f88fSGreg Clayton //----------------------------------------------------------------------
829f754f88fSGreg Clayton // DumpDOSHeader
830f754f88fSGreg Clayton //
831f754f88fSGreg Clayton // Dump the MS-DOS header to the specified output stream
832f754f88fSGreg Clayton //----------------------------------------------------------------------
833f754f88fSGreg Clayton void
834f754f88fSGreg Clayton ObjectFilePECOFF::DumpDOSHeader(Stream *s, const dos_header_t& header)
835f754f88fSGreg Clayton {
836f754f88fSGreg Clayton     s->PutCString ("MSDOS Header\n");
837f754f88fSGreg Clayton     s->Printf ("  e_magic    = 0x%4.4x\n", header.e_magic);
838f754f88fSGreg Clayton     s->Printf ("  e_cblp     = 0x%4.4x\n", header.e_cblp);
839f754f88fSGreg Clayton     s->Printf ("  e_cp       = 0x%4.4x\n", header.e_cp);
840f754f88fSGreg Clayton     s->Printf ("  e_crlc     = 0x%4.4x\n", header.e_crlc);
841f754f88fSGreg Clayton     s->Printf ("  e_cparhdr  = 0x%4.4x\n", header.e_cparhdr);
842f754f88fSGreg Clayton     s->Printf ("  e_minalloc = 0x%4.4x\n", header.e_minalloc);
843f754f88fSGreg Clayton     s->Printf ("  e_maxalloc = 0x%4.4x\n", header.e_maxalloc);
844f754f88fSGreg Clayton     s->Printf ("  e_ss       = 0x%4.4x\n", header.e_ss);
845f754f88fSGreg Clayton     s->Printf ("  e_sp       = 0x%4.4x\n", header.e_sp);
846f754f88fSGreg Clayton     s->Printf ("  e_csum     = 0x%4.4x\n", header.e_csum);
847f754f88fSGreg Clayton     s->Printf ("  e_ip       = 0x%4.4x\n", header.e_ip);
848f754f88fSGreg Clayton     s->Printf ("  e_cs       = 0x%4.4x\n", header.e_cs);
849f754f88fSGreg Clayton     s->Printf ("  e_lfarlc   = 0x%4.4x\n", header.e_lfarlc);
850f754f88fSGreg Clayton     s->Printf ("  e_ovno     = 0x%4.4x\n", header.e_ovno);
851f754f88fSGreg Clayton     s->Printf ("  e_res[4]   = { 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x }\n",
852f754f88fSGreg Clayton                header.e_res[0],
853f754f88fSGreg Clayton                header.e_res[1],
854f754f88fSGreg Clayton                header.e_res[2],
855f754f88fSGreg Clayton                header.e_res[3]);
856f754f88fSGreg Clayton     s->Printf ("  e_oemid    = 0x%4.4x\n", header.e_oemid);
857f754f88fSGreg Clayton     s->Printf ("  e_oeminfo  = 0x%4.4x\n", header.e_oeminfo);
858f754f88fSGreg 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",
859f754f88fSGreg Clayton                header.e_res2[0],
860f754f88fSGreg Clayton                header.e_res2[1],
861f754f88fSGreg Clayton                header.e_res2[2],
862f754f88fSGreg Clayton                header.e_res2[3],
863f754f88fSGreg Clayton                header.e_res2[4],
864f754f88fSGreg Clayton                header.e_res2[5],
865f754f88fSGreg Clayton                header.e_res2[6],
866f754f88fSGreg Clayton                header.e_res2[7],
867f754f88fSGreg Clayton                header.e_res2[8],
868f754f88fSGreg Clayton                header.e_res2[9]);
869f754f88fSGreg Clayton     s->Printf ("  e_lfanew   = 0x%8.8x\n", header.e_lfanew);
870f754f88fSGreg Clayton }
871f754f88fSGreg Clayton 
872f754f88fSGreg Clayton //----------------------------------------------------------------------
873f754f88fSGreg Clayton // DumpCOFFHeader
874f754f88fSGreg Clayton //
875f754f88fSGreg Clayton // Dump the COFF header to the specified output stream
876f754f88fSGreg Clayton //----------------------------------------------------------------------
877f754f88fSGreg Clayton void
878f754f88fSGreg Clayton ObjectFilePECOFF::DumpCOFFHeader(Stream *s, const coff_header_t& header)
879f754f88fSGreg Clayton {
880f754f88fSGreg Clayton     s->PutCString ("COFF Header\n");
881f754f88fSGreg Clayton     s->Printf ("  machine = 0x%4.4x\n", header.machine);
882f754f88fSGreg Clayton     s->Printf ("  nsects  = 0x%4.4x\n", header.nsects);
883f754f88fSGreg Clayton     s->Printf ("  modtime = 0x%8.8x\n", header.modtime);
884f754f88fSGreg Clayton     s->Printf ("  symoff  = 0x%8.8x\n", header.symoff);
885f754f88fSGreg Clayton     s->Printf ("  nsyms   = 0x%8.8x\n", header.nsyms);
886f754f88fSGreg Clayton     s->Printf ("  hdrsize = 0x%4.4x\n", header.hdrsize);
887f754f88fSGreg Clayton }
888f754f88fSGreg Clayton 
889f754f88fSGreg Clayton //----------------------------------------------------------------------
890f754f88fSGreg Clayton // DumpOptCOFFHeader
891f754f88fSGreg Clayton //
892f754f88fSGreg Clayton // Dump the optional COFF header to the specified output stream
893f754f88fSGreg Clayton //----------------------------------------------------------------------
894f754f88fSGreg Clayton void
895f754f88fSGreg Clayton ObjectFilePECOFF::DumpOptCOFFHeader(Stream *s, const coff_opt_header_t& header)
896f754f88fSGreg Clayton {
897f754f88fSGreg Clayton     s->PutCString ("Optional COFF Header\n");
898f754f88fSGreg Clayton     s->Printf ("  magic                   = 0x%4.4x\n", header.magic);
899f754f88fSGreg Clayton     s->Printf ("  major_linker_version    = 0x%2.2x\n", header.major_linker_version);
900f754f88fSGreg Clayton     s->Printf ("  minor_linker_version    = 0x%2.2x\n", header.minor_linker_version);
901f754f88fSGreg Clayton     s->Printf ("  code_size               = 0x%8.8x\n", header.code_size);
902f754f88fSGreg Clayton     s->Printf ("  data_size               = 0x%8.8x\n", header.data_size);
903f754f88fSGreg Clayton     s->Printf ("  bss_size                = 0x%8.8x\n", header.bss_size);
904f754f88fSGreg Clayton     s->Printf ("  entry                   = 0x%8.8x\n", header.entry);
905f754f88fSGreg Clayton     s->Printf ("  code_offset             = 0x%8.8x\n", header.code_offset);
906f754f88fSGreg Clayton     s->Printf ("  data_offset             = 0x%8.8x\n", header.data_offset);
907d01b2953SDaniel Malea     s->Printf ("  image_base              = 0x%16.16" PRIx64 "\n", header.image_base);
908f754f88fSGreg Clayton     s->Printf ("  sect_alignment          = 0x%8.8x\n", header.sect_alignment);
909f754f88fSGreg Clayton     s->Printf ("  file_alignment          = 0x%8.8x\n", header.file_alignment);
910f754f88fSGreg Clayton     s->Printf ("  major_os_system_version = 0x%4.4x\n", header.major_os_system_version);
911f754f88fSGreg Clayton     s->Printf ("  minor_os_system_version = 0x%4.4x\n", header.minor_os_system_version);
912f754f88fSGreg Clayton     s->Printf ("  major_image_version     = 0x%4.4x\n", header.major_image_version);
913f754f88fSGreg Clayton     s->Printf ("  minor_image_version     = 0x%4.4x\n", header.minor_image_version);
914f754f88fSGreg Clayton     s->Printf ("  major_subsystem_version = 0x%4.4x\n", header.major_subsystem_version);
915f754f88fSGreg Clayton     s->Printf ("  minor_subsystem_version = 0x%4.4x\n", header.minor_subsystem_version);
916f754f88fSGreg Clayton     s->Printf ("  reserved1               = 0x%8.8x\n", header.reserved1);
917f754f88fSGreg Clayton     s->Printf ("  image_size              = 0x%8.8x\n", header.image_size);
918f754f88fSGreg Clayton     s->Printf ("  header_size             = 0x%8.8x\n", header.header_size);
91928469ca3SGreg Clayton     s->Printf ("  checksum                = 0x%8.8x\n", header.checksum);
920f754f88fSGreg Clayton     s->Printf ("  subsystem               = 0x%4.4x\n", header.subsystem);
921f754f88fSGreg Clayton     s->Printf ("  dll_flags               = 0x%4.4x\n", header.dll_flags);
922d01b2953SDaniel Malea     s->Printf ("  stack_reserve_size      = 0x%16.16" PRIx64 "\n", header.stack_reserve_size);
923d01b2953SDaniel Malea     s->Printf ("  stack_commit_size       = 0x%16.16" PRIx64 "\n", header.stack_commit_size);
924d01b2953SDaniel Malea     s->Printf ("  heap_reserve_size       = 0x%16.16" PRIx64 "\n", header.heap_reserve_size);
925d01b2953SDaniel Malea     s->Printf ("  heap_commit_size        = 0x%16.16" PRIx64 "\n", header.heap_commit_size);
926f754f88fSGreg Clayton     s->Printf ("  loader_flags            = 0x%8.8x\n", header.loader_flags);
927ffeba256SVirgile Bello     s->Printf ("  num_data_dir_entries    = 0x%8.8x\n", (uint32_t)header.data_dirs.size());
928f754f88fSGreg Clayton     uint32_t i;
929f754f88fSGreg Clayton     for (i=0; i<header.data_dirs.size(); i++)
930f754f88fSGreg Clayton     {
93128469ca3SGreg Clayton         s->Printf ("  data_dirs[%2u] vmaddr = 0x%8.8x, vmsize = 0x%8.8x\n",
932f754f88fSGreg Clayton                    i,
933f754f88fSGreg Clayton                    header.data_dirs[i].vmaddr,
934f754f88fSGreg Clayton                    header.data_dirs[i].vmsize);
935f754f88fSGreg Clayton     }
936f754f88fSGreg Clayton }
937f754f88fSGreg Clayton //----------------------------------------------------------------------
938f754f88fSGreg Clayton // DumpSectionHeader
939f754f88fSGreg Clayton //
940f754f88fSGreg Clayton // Dump a single ELF section header to the specified output stream
941f754f88fSGreg Clayton //----------------------------------------------------------------------
942f754f88fSGreg Clayton void
943f754f88fSGreg Clayton ObjectFilePECOFF::DumpSectionHeader(Stream *s, const section_header_t& sh)
944f754f88fSGreg Clayton {
945f754f88fSGreg Clayton     std::string name;
946f754f88fSGreg Clayton     GetSectionName(name, sh);
947f754f88fSGreg 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",
948f754f88fSGreg Clayton                name.c_str(),
949f754f88fSGreg Clayton                sh.vmaddr,
95028469ca3SGreg Clayton                sh.vmsize,
951f754f88fSGreg Clayton                sh.offset,
95228469ca3SGreg Clayton                sh.size,
953f754f88fSGreg Clayton                sh.reloff,
954f754f88fSGreg Clayton                sh.lineoff,
955f754f88fSGreg Clayton                sh.nreloc,
956f754f88fSGreg Clayton                sh.nline,
957f754f88fSGreg Clayton                sh.flags);
958f754f88fSGreg Clayton }
959f754f88fSGreg Clayton 
960f754f88fSGreg Clayton 
961f754f88fSGreg Clayton //----------------------------------------------------------------------
962f754f88fSGreg Clayton // DumpSectionHeaders
963f754f88fSGreg Clayton //
964f754f88fSGreg Clayton // Dump all of the ELF section header to the specified output stream
965f754f88fSGreg Clayton //----------------------------------------------------------------------
966f754f88fSGreg Clayton void
967f754f88fSGreg Clayton ObjectFilePECOFF::DumpSectionHeaders(Stream *s)
968f754f88fSGreg Clayton {
969f754f88fSGreg Clayton 
970f754f88fSGreg Clayton     s->PutCString ("Section Headers\n");
97128469ca3SGreg Clayton     s->PutCString ("IDX  name             vm addr    vm size    file off   file size  reloc off  line off   nreloc nline  flags\n");
97228469ca3SGreg Clayton     s->PutCString ("==== ---------------- ---------- ---------- ---------- ---------- ---------- ---------- ------ ------ ----------\n");
973f754f88fSGreg Clayton 
974f754f88fSGreg Clayton     uint32_t idx = 0;
975f754f88fSGreg Clayton     SectionHeaderCollIter pos, end = m_sect_headers.end();
976f754f88fSGreg Clayton 
977f754f88fSGreg Clayton     for (pos = m_sect_headers.begin(); pos != end; ++pos, ++idx)
978f754f88fSGreg Clayton     {
979f754f88fSGreg Clayton         s->Printf ("[%2u] ", idx);
980f754f88fSGreg Clayton         ObjectFilePECOFF::DumpSectionHeader(s, *pos);
981f754f88fSGreg Clayton     }
982f754f88fSGreg Clayton }
983f754f88fSGreg Clayton 
984f754f88fSGreg Clayton bool
985f754f88fSGreg Clayton ObjectFilePECOFF::GetArchitecture (ArchSpec &arch)
986f754f88fSGreg Clayton {
987237ad974SCharles Davis     uint16_t machine = m_coff_header.machine;
988237ad974SCharles Davis     switch (machine)
989237ad974SCharles Davis     {
990237ad974SCharles Davis         case llvm::COFF::IMAGE_FILE_MACHINE_AMD64:
991237ad974SCharles Davis         case llvm::COFF::IMAGE_FILE_MACHINE_I386:
992237ad974SCharles Davis         case llvm::COFF::IMAGE_FILE_MACHINE_POWERPC:
993237ad974SCharles Davis         case llvm::COFF::IMAGE_FILE_MACHINE_POWERPCFP:
994237ad974SCharles Davis         case llvm::COFF::IMAGE_FILE_MACHINE_ARM:
9951108cb36SSaleem Abdulrasool         case llvm::COFF::IMAGE_FILE_MACHINE_ARMNT:
996237ad974SCharles Davis         case llvm::COFF::IMAGE_FILE_MACHINE_THUMB:
9976c970420SColin Riley             arch.SetArchitecture (eArchTypeCOFF, machine, LLDB_INVALID_CPUTYPE);
998237ad974SCharles Davis             return true;
999237ad974SCharles Davis         default:
1000237ad974SCharles Davis             break;
1001237ad974SCharles Davis     }
1002237ad974SCharles Davis     return false;
1003f754f88fSGreg Clayton }
1004f754f88fSGreg Clayton 
1005f754f88fSGreg Clayton ObjectFile::Type
1006f754f88fSGreg Clayton ObjectFilePECOFF::CalculateType()
1007f754f88fSGreg Clayton {
1008f754f88fSGreg Clayton     if (m_coff_header.machine != 0)
1009f754f88fSGreg Clayton     {
1010237ad974SCharles Davis         if ((m_coff_header.flags & llvm::COFF::IMAGE_FILE_DLL) == 0)
1011f754f88fSGreg Clayton             return eTypeExecutable;
1012f754f88fSGreg Clayton         else
1013f754f88fSGreg Clayton             return eTypeSharedLibrary;
1014f754f88fSGreg Clayton     }
1015f754f88fSGreg Clayton     return eTypeExecutable;
1016f754f88fSGreg Clayton }
1017f754f88fSGreg Clayton 
1018f754f88fSGreg Clayton ObjectFile::Strata
1019f754f88fSGreg Clayton ObjectFilePECOFF::CalculateStrata()
1020f754f88fSGreg Clayton {
1021f754f88fSGreg Clayton     return eStrataUser;
1022f754f88fSGreg Clayton }
1023f754f88fSGreg Clayton //------------------------------------------------------------------
1024f754f88fSGreg Clayton // PluginInterface protocol
1025f754f88fSGreg Clayton //------------------------------------------------------------------
102657abc5d6SGreg Clayton ConstString
1027f754f88fSGreg Clayton ObjectFilePECOFF::GetPluginName()
1028f754f88fSGreg Clayton {
1029f754f88fSGreg Clayton     return GetPluginNameStatic();
1030f754f88fSGreg Clayton }
1031f754f88fSGreg Clayton 
1032f754f88fSGreg Clayton uint32_t
1033f754f88fSGreg Clayton ObjectFilePECOFF::GetPluginVersion()
1034f754f88fSGreg Clayton {
1035f754f88fSGreg Clayton     return 1;
1036f754f88fSGreg Clayton }
1037f754f88fSGreg Clayton 
1038