Home | History | Annotate | Download | only in PECOFF
      1 //===-- ObjectFilePECOFF.cpp ------------------------------------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 #include "ObjectFilePECOFF.h"
     11 
     12 #include "llvm/Support/MachO.h"
     13 
     14 #include "lldb/Core/ArchSpec.h"
     15 #include "lldb/Core/DataBuffer.h"
     16 #include "lldb/Host/FileSpec.h"
     17 #include "lldb/Core/FileSpecList.h"
     18 #include "lldb/Core/Module.h"
     19 #include "lldb/Core/ModuleSpec.h"
     20 #include "lldb/Core/PluginManager.h"
     21 #include "lldb/Core/Section.h"
     22 #include "lldb/Core/StreamFile.h"
     23 #include "lldb/Core/StreamString.h"
     24 #include "lldb/Core/Timer.h"
     25 #include "lldb/Core/UUID.h"
     26 #include "lldb/Symbol/ObjectFile.h"
     27 
     28 static uint32_t COFFMachineToMachCPU(uint16_t machine);
     29 
     30 #define IMAGE_FILE_MACHINE_UNKNOWN      0x0000
     31 #define IMAGE_FILE_MACHINE_AM33         0x01d3  // Matsushita AM33
     32 #define IMAGE_FILE_MACHINE_AMD64        0x8664  // x64
     33 #define IMAGE_FILE_MACHINE_ARM          0x01c0  // ARM little endian
     34 #define IMAGE_FILE_MACHINE_EBC          0x0ebc  // EFI byte code
     35 #define IMAGE_FILE_MACHINE_I386         0x014c  // Intel 386 or later processors and compatible processors
     36 #define IMAGE_FILE_MACHINE_IA64         0x0200  // Intel Itanium processor family
     37 #define IMAGE_FILE_MACHINE_M32R         0x9041  // Mitsubishi M32R little endian
     38 #define IMAGE_FILE_MACHINE_MIPS16       0x0266  // MIPS16
     39 #define IMAGE_FILE_MACHINE_MIPSFPU      0x0366  // MIPS with FPU
     40 #define IMAGE_FILE_MACHINE_MIPSFPU16    0x0466  // MIPS16 with FPU
     41 #define IMAGE_FILE_MACHINE_POWERPC      0x01f0  // Power PC little endian
     42 #define IMAGE_FILE_MACHINE_POWERPCFP    0x01f1  // Power PC with floating point support
     43 #define IMAGE_FILE_MACHINE_R4000        0x0166  // MIPS little endian
     44 #define IMAGE_FILE_MACHINE_SH3          0x01a2  // Hitachi SH3
     45 #define IMAGE_FILE_MACHINE_SH3DSP       0x01a3  // Hitachi SH3 DSP
     46 #define IMAGE_FILE_MACHINE_SH4          0x01a6  // Hitachi SH4
     47 #define IMAGE_FILE_MACHINE_SH5          0x01a8  // Hitachi SH5
     48 #define IMAGE_FILE_MACHINE_THUMB        0x01c2  // Thumb
     49 #define IMAGE_FILE_MACHINE_WCEMIPSV2    0x0169  // MIPS little-endian WCE v2
     50 
     51 
     52 #define IMAGE_DOS_SIGNATURE             0x5A4D      // MZ
     53 #define IMAGE_OS2_SIGNATURE             0x454E      // NE
     54 #define IMAGE_OS2_SIGNATURE_LE          0x454C      // LE
     55 #define IMAGE_NT_SIGNATURE              0x00004550  // PE00
     56 #define OPT_HEADER_MAGIC_PE32           0x010b
     57 #define OPT_HEADER_MAGIC_PE32_PLUS      0x020b
     58 
     59 #define IMAGE_FILE_RELOCS_STRIPPED          0x0001
     60 #define IMAGE_FILE_EXECUTABLE_IMAGE         0x0002
     61 #define IMAGE_FILE_LINE_NUMS_STRIPPED       0x0004
     62 #define IMAGE_FILE_LOCAL_SYMS_STRIPPED      0x0008
     63 #define IMAGE_FILE_AGGRESSIVE_WS_TRIM       0x0010
     64 #define IMAGE_FILE_LARGE_ADDRESS_AWARE      0x0020
     65 //#define                                   0x0040  // Reserved
     66 #define IMAGE_FILE_BYTES_REVERSED_LO        0x0080
     67 #define IMAGE_FILE_32BIT_MACHINE            0x0100
     68 #define IMAGE_FILE_DEBUG_STRIPPED           0x0200
     69 #define IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP  0x0400
     70 #define IMAGE_FILE_NET_RUN_FROM_SWAP        0x0800
     71 #define IMAGE_FILE_SYSTEM                   0x1000
     72 #define IMAGE_FILE_DLL                      0x2000
     73 #define IMAGE_FILE_UP_SYSTEM_ONLY           0x4000
     74 #define IMAGE_FILE_BYTES_REVERSED_HI        0x8000
     75 
     76 
     77 // Section Flags
     78 // The section flags in the Characteristics field of the section header indicate
     79 // characteristics of the section.
     80 #define IMAGE_SCN_TYPE_NO_PAD               0x00000008 // The section should not be padded to the next boundary. This flag is obsolete and is replaced by IMAGE_SCN_ALIGN_1BYTES. This is valid only for object files.
     81 #define IMAGE_SCN_CNT_CODE                  0x00000020 // The section contains executable code.
     82 #define IMAGE_SCN_CNT_INITIALIZED_DATA      0x00000040 // The section contains initialized data.
     83 #define IMAGE_SCN_CNT_UNINITIALIZED_DATA    0x00000080 // The section contains uninitialized data.
     84 #define IMAGE_SCN_LNK_OTHER                 0x00000100 // Reserved for future use.
     85 #define IMAGE_SCN_LNK_INFO                  0x00000200 // The section contains comments or other information. The .drectve section has this type. This is valid for object files only.
     86 #define IMAGE_SCN_LNK_REMOVE                0x00000800 // The section will not become part of the image. This is valid only for object files.
     87 #define IMAGE_SCN_LNK_COMDAT                0x00001000 // The section contains COMDAT data. For more information, see section 5.5.6, COMDAT Sections (Object Only). This is valid only for object files.
     88 #define IMAGE_SCN_GPREL                     0x00008000 // The section contains data referenced through the global pointer (GP).
     89 #define IMAGE_SCN_MEM_PURGEABLE             0x00020000
     90 #define IMAGE_SCN_MEM_16BIT                 0x00020000 // For ARM machine types, the section contains Thumb code.  Reserved for future use with other machine types.
     91 #define IMAGE_SCN_MEM_LOCKED                0x00040000
     92 #define IMAGE_SCN_MEM_PRELOAD               0x00080000
     93 #define IMAGE_SCN_ALIGN_1BYTES              0x00100000 // Align data on a 1-byte boundary. Valid only for object files.
     94 #define IMAGE_SCN_ALIGN_2BYTES              0x00200000 // Align data on a 2-byte boundary. Valid only for object files.
     95 #define IMAGE_SCN_ALIGN_4BYTES              0x00300000 // Align data on a 4-byte boundary. Valid only for object files.
     96 #define IMAGE_SCN_ALIGN_8BYTES              0x00400000 // Align data on an 8-byte boundary. Valid only for object files.
     97 #define IMAGE_SCN_ALIGN_16BYTES             0x00500000 // Align data on a 16-byte boundary. Valid only for object files.
     98 #define IMAGE_SCN_ALIGN_32BYTES             0x00600000 // Align data on a 32-byte boundary. Valid only for object files.
     99 #define IMAGE_SCN_ALIGN_64BYTES             0x00700000 // Align data on a 64-byte boundary. Valid only for object files.
    100 #define IMAGE_SCN_ALIGN_128BYTES            0x00800000 // Align data on a 128-byte boundary. Valid only for object files.
    101 #define IMAGE_SCN_ALIGN_256BYTES            0x00900000 // Align data on a 256-byte boundary. Valid only for object files.
    102 #define IMAGE_SCN_ALIGN_512BYTES            0x00A00000 // Align data on a 512-byte boundary. Valid only for object files.
    103 #define IMAGE_SCN_ALIGN_1024BYTES           0x00B00000 // Align data on a 1024-byte boundary. Valid only for object files.
    104 #define IMAGE_SCN_ALIGN_2048BYTES           0x00C00000 // Align data on a 2048-byte boundary. Valid only for object files.
    105 #define IMAGE_SCN_ALIGN_4096BYTES           0x00D00000 // Align data on a 4096-byte boundary. Valid only for object files.
    106 #define IMAGE_SCN_ALIGN_8192BYTES           0x00E00000 // Align data on an 8192-byte boundary. Valid only for object files.
    107 #define IMAGE_SCN_LNK_NRELOC_OVFL           0x01000000 // The section contains extended relocations.
    108 #define IMAGE_SCN_MEM_DISCARDABLE           0x02000000 // The section can be discarded as needed.
    109 #define IMAGE_SCN_MEM_NOT_CACHED            0x04000000 // The section cannot be cached.
    110 #define IMAGE_SCN_MEM_NOT_PAGED             0x08000000 // The section is not pageable.
    111 #define IMAGE_SCN_MEM_SHARED                0x10000000 // The section can be shared in memory.
    112 #define IMAGE_SCN_MEM_EXECUTE               0x20000000 // The section can be executed as code.
    113 #define IMAGE_SCN_MEM_READ                  0x40000000 // The section can be read.
    114 #define IMAGE_SCN_MEM_WRITE                 0x80000000 // The section can be written to.
    115 
    116 using namespace lldb;
    117 using namespace lldb_private;
    118 
    119 void
    120 ObjectFilePECOFF::Initialize()
    121 {
    122     PluginManager::RegisterPlugin (GetPluginNameStatic(),
    123                                    GetPluginDescriptionStatic(),
    124                                    CreateInstance,
    125                                    CreateMemoryInstance,
    126                                    GetModuleSpecifications);
    127 }
    128 
    129 void
    130 ObjectFilePECOFF::Terminate()
    131 {
    132     PluginManager::UnregisterPlugin (CreateInstance);
    133 }
    134 
    135 
    136 lldb_private::ConstString
    137 ObjectFilePECOFF::GetPluginNameStatic()
    138 {
    139     static ConstString g_name("pe-coff");
    140     return g_name;
    141 }
    142 
    143 const char *
    144 ObjectFilePECOFF::GetPluginDescriptionStatic()
    145 {
    146     return "Portable Executable and Common Object File Format object file reader (32 and 64 bit)";
    147 }
    148 
    149 
    150 ObjectFile *
    151 ObjectFilePECOFF::CreateInstance (const lldb::ModuleSP &module_sp,
    152                                   DataBufferSP& data_sp,
    153                                   lldb::offset_t data_offset,
    154                                   const lldb_private::FileSpec* file,
    155                                   lldb::offset_t file_offset,
    156                                   lldb::offset_t length)
    157 {
    158     if (!data_sp)
    159     {
    160         data_sp = file->MemoryMapFileContents(file_offset, length);
    161         data_offset = 0;
    162     }
    163 
    164     if (ObjectFilePECOFF::MagicBytesMatch(data_sp))
    165     {
    166         // Update the data to contain the entire file if it doesn't already
    167         if (data_sp->GetByteSize() < length)
    168             data_sp = file->MemoryMapFileContents(file_offset, length);
    169         std::unique_ptr<ObjectFile> objfile_ap(new ObjectFilePECOFF (module_sp, data_sp, data_offset, file, file_offset, length));
    170         if (objfile_ap.get() && objfile_ap->ParseHeader())
    171             return objfile_ap.release();
    172     }
    173     return NULL;
    174 }
    175 
    176 ObjectFile *
    177 ObjectFilePECOFF::CreateMemoryInstance (const lldb::ModuleSP &module_sp,
    178                                         lldb::DataBufferSP& data_sp,
    179                                         const lldb::ProcessSP &process_sp,
    180                                         lldb::addr_t header_addr)
    181 {
    182     return NULL;
    183 }
    184 
    185 size_t
    186 ObjectFilePECOFF::GetModuleSpecifications (const lldb_private::FileSpec& file,
    187                                            lldb::DataBufferSP& data_sp,
    188                                            lldb::offset_t data_offset,
    189                                            lldb::offset_t file_offset,
    190                                            lldb::offset_t length,
    191                                            lldb_private::ModuleSpecList &specs)
    192 {
    193     return 0;
    194 }
    195 
    196 
    197 bool
    198 ObjectFilePECOFF::MagicBytesMatch (DataBufferSP& data_sp)
    199 {
    200     DataExtractor data(data_sp, eByteOrderLittle, 4);
    201     lldb::offset_t offset = 0;
    202     uint16_t magic = data.GetU16 (&offset);
    203     return magic == IMAGE_DOS_SIGNATURE;
    204 }
    205 
    206 
    207 ObjectFilePECOFF::ObjectFilePECOFF (const lldb::ModuleSP &module_sp,
    208                                     DataBufferSP& data_sp,
    209                                     lldb::offset_t data_offset,
    210                                     const FileSpec* file,
    211                                     lldb::offset_t file_offset,
    212                                     lldb::offset_t length) :
    213     ObjectFile (module_sp, file, file_offset, length, data_sp, data_offset),
    214     m_dos_header (),
    215     m_coff_header (),
    216     m_coff_header_opt (),
    217     m_sect_headers ()
    218 {
    219     ::memset (&m_dos_header, 0, sizeof(m_dos_header));
    220     ::memset (&m_coff_header, 0, sizeof(m_coff_header));
    221     ::memset (&m_coff_header_opt, 0, sizeof(m_coff_header_opt));
    222 }
    223 
    224 
    225 ObjectFilePECOFF::~ObjectFilePECOFF()
    226 {
    227 }
    228 
    229 
    230 bool
    231 ObjectFilePECOFF::ParseHeader ()
    232 {
    233     ModuleSP module_sp(GetModule());
    234     if (module_sp)
    235     {
    236         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
    237         m_sect_headers.clear();
    238         m_data.SetByteOrder (eByteOrderLittle);
    239         lldb::offset_t offset = 0;
    240 
    241         if (ParseDOSHeader())
    242         {
    243             offset = m_dos_header.e_lfanew;
    244             uint32_t pe_signature = m_data.GetU32 (&offset);
    245             if (pe_signature != IMAGE_NT_SIGNATURE)
    246                 return false;
    247             if (ParseCOFFHeader(&offset))
    248             {
    249                 if (m_coff_header.hdrsize > 0)
    250                     ParseCOFFOptionalHeader(&offset);
    251                 ParseSectionHeaders (offset);
    252             }
    253             return true;
    254         }
    255     }
    256     return false;
    257 }
    258 
    259 
    260 ByteOrder
    261 ObjectFilePECOFF::GetByteOrder () const
    262 {
    263     return eByteOrderLittle;
    264 }
    265 
    266 bool
    267 ObjectFilePECOFF::IsExecutable() const
    268 {
    269     return (m_coff_header.flags & IMAGE_FILE_DLL) == 0;
    270 }
    271 
    272 uint32_t
    273 ObjectFilePECOFF::GetAddressByteSize () const
    274 {
    275     if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32_PLUS)
    276         return 8;
    277     else if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32)
    278         return 4;
    279     return 4;
    280 }
    281 
    282 //----------------------------------------------------------------------
    283 // NeedsEndianSwap
    284 //
    285 // Return true if an endian swap needs to occur when extracting data
    286 // from this file.
    287 //----------------------------------------------------------------------
    288 bool
    289 ObjectFilePECOFF::NeedsEndianSwap() const
    290 {
    291 #if defined(__LITTLE_ENDIAN__)
    292     return false;
    293 #else
    294     return true;
    295 #endif
    296 }
    297 //----------------------------------------------------------------------
    298 // ParseDOSHeader
    299 //----------------------------------------------------------------------
    300 bool
    301 ObjectFilePECOFF::ParseDOSHeader ()
    302 {
    303     bool success = false;
    304     lldb::offset_t offset = 0;
    305     success = m_data.ValidOffsetForDataOfSize(0, sizeof(m_dos_header));
    306 
    307     if (success)
    308     {
    309         m_dos_header.e_magic = m_data.GetU16(&offset); // Magic number
    310         success = m_dos_header.e_magic == IMAGE_DOS_SIGNATURE;
    311 
    312         if (success)
    313         {
    314             m_dos_header.e_cblp     = m_data.GetU16(&offset); // Bytes on last page of file
    315             m_dos_header.e_cp       = m_data.GetU16(&offset); // Pages in file
    316             m_dos_header.e_crlc     = m_data.GetU16(&offset); // Relocations
    317             m_dos_header.e_cparhdr  = m_data.GetU16(&offset); // Size of header in paragraphs
    318             m_dos_header.e_minalloc = m_data.GetU16(&offset); // Minimum extra paragraphs needed
    319             m_dos_header.e_maxalloc = m_data.GetU16(&offset); // Maximum extra paragraphs needed
    320             m_dos_header.e_ss       = m_data.GetU16(&offset); // Initial (relative) SS value
    321             m_dos_header.e_sp       = m_data.GetU16(&offset); // Initial SP value
    322             m_dos_header.e_csum     = m_data.GetU16(&offset); // Checksum
    323             m_dos_header.e_ip       = m_data.GetU16(&offset); // Initial IP value
    324             m_dos_header.e_cs       = m_data.GetU16(&offset); // Initial (relative) CS value
    325             m_dos_header.e_lfarlc   = m_data.GetU16(&offset); // File address of relocation table
    326             m_dos_header.e_ovno     = m_data.GetU16(&offset); // Overlay number
    327 
    328             m_dos_header.e_res[0]   = m_data.GetU16(&offset); // Reserved words
    329             m_dos_header.e_res[1]   = m_data.GetU16(&offset); // Reserved words
    330             m_dos_header.e_res[2]   = m_data.GetU16(&offset); // Reserved words
    331             m_dos_header.e_res[3]   = m_data.GetU16(&offset); // Reserved words
    332 
    333             m_dos_header.e_oemid    = m_data.GetU16(&offset); // OEM identifier (for e_oeminfo)
    334             m_dos_header.e_oeminfo  = m_data.GetU16(&offset); // OEM information; e_oemid specific
    335             m_dos_header.e_res2[0]  = m_data.GetU16(&offset); // Reserved words
    336             m_dos_header.e_res2[1]  = m_data.GetU16(&offset); // Reserved words
    337             m_dos_header.e_res2[2]  = m_data.GetU16(&offset); // Reserved words
    338             m_dos_header.e_res2[3]  = m_data.GetU16(&offset); // Reserved words
    339             m_dos_header.e_res2[4]  = m_data.GetU16(&offset); // Reserved words
    340             m_dos_header.e_res2[5]  = m_data.GetU16(&offset); // Reserved words
    341             m_dos_header.e_res2[6]  = m_data.GetU16(&offset); // Reserved words
    342             m_dos_header.e_res2[7]  = m_data.GetU16(&offset); // Reserved words
    343             m_dos_header.e_res2[8]  = m_data.GetU16(&offset); // Reserved words
    344             m_dos_header.e_res2[9]  = m_data.GetU16(&offset); // Reserved words
    345 
    346             m_dos_header.e_lfanew   = m_data.GetU32(&offset); // File address of new exe header
    347         }
    348     }
    349     if (!success)
    350         memset(&m_dos_header, 0, sizeof(m_dos_header));
    351     return success;
    352 }
    353 
    354 
    355 //----------------------------------------------------------------------
    356 // ParserCOFFHeader
    357 //----------------------------------------------------------------------
    358 bool
    359 ObjectFilePECOFF::ParseCOFFHeader(lldb::offset_t *offset_ptr)
    360 {
    361     bool success = m_data.ValidOffsetForDataOfSize (*offset_ptr, sizeof(m_coff_header));
    362     if (success)
    363     {
    364         m_coff_header.machine   = m_data.GetU16(offset_ptr);
    365         m_coff_header.nsects    = m_data.GetU16(offset_ptr);
    366         m_coff_header.modtime   = m_data.GetU32(offset_ptr);
    367         m_coff_header.symoff    = m_data.GetU32(offset_ptr);
    368         m_coff_header.nsyms     = m_data.GetU32(offset_ptr);
    369         m_coff_header.hdrsize   = m_data.GetU16(offset_ptr);
    370         m_coff_header.flags     = m_data.GetU16(offset_ptr);
    371     }
    372     if (!success)
    373         memset(&m_coff_header, 0, sizeof(m_coff_header));
    374     return success;
    375 }
    376 
    377 bool
    378 ObjectFilePECOFF::ParseCOFFOptionalHeader(lldb::offset_t *offset_ptr)
    379 {
    380     bool success = false;
    381     const lldb::offset_t end_offset = *offset_ptr + m_coff_header.hdrsize;
    382     if (*offset_ptr < end_offset)
    383     {
    384         success = true;
    385         m_coff_header_opt.magic                         = m_data.GetU16(offset_ptr);
    386         m_coff_header_opt.major_linker_version          = m_data.GetU8 (offset_ptr);
    387         m_coff_header_opt.minor_linker_version          = m_data.GetU8 (offset_ptr);
    388         m_coff_header_opt.code_size                     = m_data.GetU32(offset_ptr);
    389         m_coff_header_opt.data_size                     = m_data.GetU32(offset_ptr);
    390         m_coff_header_opt.bss_size                      = m_data.GetU32(offset_ptr);
    391         m_coff_header_opt.entry                         = m_data.GetU32(offset_ptr);
    392         m_coff_header_opt.code_offset                   = m_data.GetU32(offset_ptr);
    393 
    394         const uint32_t addr_byte_size = GetAddressByteSize ();
    395 
    396         if (*offset_ptr < end_offset)
    397         {
    398             if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32)
    399             {
    400                 // PE32 only
    401                 m_coff_header_opt.data_offset               = m_data.GetU32(offset_ptr);
    402             }
    403             else
    404                 m_coff_header_opt.data_offset = 0;
    405 
    406             if (*offset_ptr < end_offset)
    407             {
    408                 m_coff_header_opt.image_base                    = m_data.GetMaxU64 (offset_ptr, addr_byte_size);
    409                 m_coff_header_opt.sect_alignment                = m_data.GetU32(offset_ptr);
    410                 m_coff_header_opt.file_alignment                = m_data.GetU32(offset_ptr);
    411                 m_coff_header_opt.major_os_system_version       = m_data.GetU16(offset_ptr);
    412                 m_coff_header_opt.minor_os_system_version       = m_data.GetU16(offset_ptr);
    413                 m_coff_header_opt.major_image_version           = m_data.GetU16(offset_ptr);
    414                 m_coff_header_opt.minor_image_version           = m_data.GetU16(offset_ptr);
    415                 m_coff_header_opt.major_subsystem_version       = m_data.GetU16(offset_ptr);
    416                 m_coff_header_opt.minor_subsystem_version       = m_data.GetU16(offset_ptr);
    417                 m_coff_header_opt.reserved1                     = m_data.GetU32(offset_ptr);
    418                 m_coff_header_opt.image_size                    = m_data.GetU32(offset_ptr);
    419                 m_coff_header_opt.header_size                   = m_data.GetU32(offset_ptr);
    420                 m_coff_header_opt.checksum                      = m_data.GetU32(offset_ptr);
    421                 m_coff_header_opt.subsystem                     = m_data.GetU16(offset_ptr);
    422                 m_coff_header_opt.dll_flags                     = m_data.GetU16(offset_ptr);
    423                 m_coff_header_opt.stack_reserve_size            = m_data.GetMaxU64 (offset_ptr, addr_byte_size);
    424                 m_coff_header_opt.stack_commit_size             = m_data.GetMaxU64 (offset_ptr, addr_byte_size);
    425                 m_coff_header_opt.heap_reserve_size             = m_data.GetMaxU64 (offset_ptr, addr_byte_size);
    426                 m_coff_header_opt.heap_commit_size              = m_data.GetMaxU64 (offset_ptr, addr_byte_size);
    427                 m_coff_header_opt.loader_flags                  = m_data.GetU32(offset_ptr);
    428                 uint32_t num_data_dir_entries = m_data.GetU32(offset_ptr);
    429                 m_coff_header_opt.data_dirs.clear();
    430                 m_coff_header_opt.data_dirs.resize(num_data_dir_entries);
    431                 uint32_t i;
    432                 for (i=0; i<num_data_dir_entries; i++)
    433                 {
    434                     m_coff_header_opt.data_dirs[i].vmaddr = m_data.GetU32(offset_ptr);
    435                     m_coff_header_opt.data_dirs[i].vmsize = m_data.GetU32(offset_ptr);
    436                 }
    437             }
    438         }
    439     }
    440     // Make sure we are on track for section data which follows
    441     *offset_ptr = end_offset;
    442     return success;
    443 }
    444 
    445 
    446 //----------------------------------------------------------------------
    447 // ParseSectionHeaders
    448 //----------------------------------------------------------------------
    449 bool
    450 ObjectFilePECOFF::ParseSectionHeaders (uint32_t section_header_data_offset)
    451 {
    452     const uint32_t nsects = m_coff_header.nsects;
    453     m_sect_headers.clear();
    454 
    455     if (nsects > 0)
    456     {
    457         const uint32_t addr_byte_size = GetAddressByteSize ();
    458         const size_t section_header_byte_size = nsects * sizeof(section_header_t);
    459         DataBufferSP section_header_data_sp(m_file.ReadFileContents (section_header_data_offset, section_header_byte_size));
    460         DataExtractor section_header_data (section_header_data_sp, GetByteOrder(), addr_byte_size);
    461 
    462         lldb::offset_t offset = 0;
    463         if (section_header_data.ValidOffsetForDataOfSize (offset, section_header_byte_size))
    464         {
    465             m_sect_headers.resize(nsects);
    466 
    467             for (uint32_t idx = 0; idx<nsects; ++idx)
    468             {
    469                 const void *name_data = section_header_data.GetData(&offset, 8);
    470                 if (name_data)
    471                 {
    472                     memcpy(m_sect_headers[idx].name, name_data, 8);
    473                     m_sect_headers[idx].vmsize  = section_header_data.GetU32(&offset);
    474                     m_sect_headers[idx].vmaddr  = section_header_data.GetU32(&offset);
    475                     m_sect_headers[idx].size    = section_header_data.GetU32(&offset);
    476                     m_sect_headers[idx].offset  = section_header_data.GetU32(&offset);
    477                     m_sect_headers[idx].reloff  = section_header_data.GetU32(&offset);
    478                     m_sect_headers[idx].lineoff = section_header_data.GetU32(&offset);
    479                     m_sect_headers[idx].nreloc  = section_header_data.GetU16(&offset);
    480                     m_sect_headers[idx].nline   = section_header_data.GetU16(&offset);
    481                     m_sect_headers[idx].flags   = section_header_data.GetU32(&offset);
    482                 }
    483             }
    484         }
    485     }
    486 
    487     return m_sect_headers.empty() == false;
    488 }
    489 
    490 bool
    491 ObjectFilePECOFF::GetSectionName(std::string& sect_name, const section_header_t& sect)
    492 {
    493     if (sect.name[0] == '/')
    494     {
    495         lldb::offset_t stroff = strtoul(&sect.name[1], NULL, 10);
    496         lldb::offset_t string_file_offset = m_coff_header.symoff + (m_coff_header.nsyms * 18) + stroff;
    497         const char *name = m_data.GetCStr (&string_file_offset);
    498         if (name)
    499         {
    500             sect_name = name;
    501             return true;
    502         }
    503 
    504         return false;
    505     }
    506     sect_name = sect.name;
    507     return true;
    508 }
    509 
    510 //----------------------------------------------------------------------
    511 // GetNListSymtab
    512 //----------------------------------------------------------------------
    513 Symtab *
    514 ObjectFilePECOFF::GetSymtab()
    515 {
    516     ModuleSP module_sp(GetModule());
    517     if (module_sp)
    518     {
    519         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
    520         if (m_symtab_ap.get() == NULL)
    521         {
    522             SectionList *sect_list = GetSectionList();
    523             m_symtab_ap.reset(new Symtab(this));
    524             Mutex::Locker symtab_locker (m_symtab_ap->GetMutex());
    525 
    526             const uint32_t num_syms = m_coff_header.nsyms;
    527 
    528             if (num_syms > 0 && m_coff_header.symoff > 0)
    529             {
    530                 const uint32_t symbol_size = 18;
    531                 const uint32_t addr_byte_size = GetAddressByteSize ();
    532                 const size_t symbol_data_size = num_syms * symbol_size;
    533                 // Include the 4 bytes string table size at the end of the symbols
    534                 DataBufferSP symtab_data_sp(m_file.ReadFileContents (m_coff_header.symoff, symbol_data_size + 4));
    535                 DataExtractor symtab_data (symtab_data_sp, GetByteOrder(), addr_byte_size);
    536                 lldb::offset_t offset = symbol_data_size;
    537                 const uint32_t strtab_size = symtab_data.GetU32 (&offset);
    538                 DataBufferSP strtab_data_sp(m_file.ReadFileContents (m_coff_header.symoff + symbol_data_size, strtab_size));
    539                 DataExtractor strtab_data (strtab_data_sp, GetByteOrder(), addr_byte_size);
    540 
    541                 // First 4 bytes should be zeroed after strtab_size has been read,
    542                 // because it is used as offset 0 to encode a NULL string.
    543                 uint32_t* strtab_data_start = (uint32_t*)strtab_data_sp->GetBytes();
    544                 strtab_data_start[0] = 0;
    545 
    546                 offset = 0;
    547                 std::string symbol_name;
    548                 Symbol *symbols = m_symtab_ap->Resize (num_syms);
    549                 for (uint32_t i=0; i<num_syms; ++i)
    550                 {
    551                     coff_symbol_t symbol;
    552                     const uint32_t symbol_offset = offset;
    553                     const char *symbol_name_cstr = NULL;
    554                     // If the first 4 bytes of the symbol string are zero, then we
    555                     // it is followed by a 4 byte string table offset. Else these
    556                     // 8 bytes contain the symbol name
    557                     if (symtab_data.GetU32 (&offset) == 0)
    558                     {
    559                         // Long string that doesn't fit into the symbol table name,
    560                         // so now we must read the 4 byte string table offset
    561                         uint32_t strtab_offset = symtab_data.GetU32 (&offset);
    562                         symbol_name_cstr = strtab_data.PeekCStr (strtab_offset);
    563                         symbol_name.assign (symbol_name_cstr);
    564                     }
    565                     else
    566                     {
    567                         // Short string that fits into the symbol table name which is 8 bytes
    568                         offset += sizeof(symbol.name) - 4; // Skip remaining
    569                         symbol_name_cstr = symtab_data.PeekCStr (symbol_offset);
    570                         if (symbol_name_cstr == NULL)
    571                             break;
    572                         symbol_name.assign (symbol_name_cstr, sizeof(symbol.name));
    573                     }
    574                     symbol.value    = symtab_data.GetU32 (&offset);
    575                     symbol.sect     = symtab_data.GetU16 (&offset);
    576                     symbol.type     = symtab_data.GetU16 (&offset);
    577                     symbol.storage  = symtab_data.GetU8  (&offset);
    578                     symbol.naux     = symtab_data.GetU8  (&offset);
    579                     symbols[i].GetMangled ().SetValue (ConstString(symbol_name.c_str()));
    580                     if ((int16_t)symbol.sect >= 1)
    581                     {
    582                         Address symbol_addr(sect_list->GetSectionAtIndex(symbol.sect-1), symbol.value);
    583                         symbols[i].GetAddress() = symbol_addr;
    584                     }
    585 
    586                     if (symbol.naux > 0)
    587                     {
    588                         i += symbol.naux;
    589                         offset += symbol_size;
    590                     }
    591                 }
    592 
    593             }
    594         }
    595     }
    596     return m_symtab_ap.get();
    597 
    598 }
    599 
    600 bool
    601 ObjectFilePECOFF::IsStripped ()
    602 {
    603     // TODO: determine this for COFF
    604     return false;
    605 }
    606 
    607 
    608 
    609 void
    610 ObjectFilePECOFF::CreateSections (SectionList &unified_section_list)
    611 {
    612     if (!m_sections_ap.get())
    613     {
    614         m_sections_ap.reset(new SectionList());
    615 
    616         ModuleSP module_sp(GetModule());
    617         if (module_sp)
    618         {
    619             lldb_private::Mutex::Locker locker(module_sp->GetMutex());
    620             const uint32_t nsects = m_sect_headers.size();
    621             ModuleSP module_sp (GetModule());
    622             for (uint32_t idx = 0; idx<nsects; ++idx)
    623             {
    624                 std::string sect_name;
    625                 GetSectionName (sect_name, m_sect_headers[idx]);
    626                 ConstString const_sect_name (sect_name.c_str());
    627                 static ConstString g_code_sect_name (".code");
    628                 static ConstString g_CODE_sect_name ("CODE");
    629                 static ConstString g_data_sect_name (".data");
    630                 static ConstString g_DATA_sect_name ("DATA");
    631                 static ConstString g_bss_sect_name (".bss");
    632                 static ConstString g_BSS_sect_name ("BSS");
    633                 static ConstString g_debug_sect_name (".debug");
    634                 static ConstString g_reloc_sect_name (".reloc");
    635                 static ConstString g_stab_sect_name (".stab");
    636                 static ConstString g_stabstr_sect_name (".stabstr");
    637                 static ConstString g_sect_name_dwarf_debug_abbrev (".debug_abbrev");
    638                 static ConstString g_sect_name_dwarf_debug_aranges (".debug_aranges");
    639                 static ConstString g_sect_name_dwarf_debug_frame (".debug_frame");
    640                 static ConstString g_sect_name_dwarf_debug_info (".debug_info");
    641                 static ConstString g_sect_name_dwarf_debug_line (".debug_line");
    642                 static ConstString g_sect_name_dwarf_debug_loc (".debug_loc");
    643                 static ConstString g_sect_name_dwarf_debug_macinfo (".debug_macinfo");
    644                 static ConstString g_sect_name_dwarf_debug_pubnames (".debug_pubnames");
    645                 static ConstString g_sect_name_dwarf_debug_pubtypes (".debug_pubtypes");
    646                 static ConstString g_sect_name_dwarf_debug_ranges (".debug_ranges");
    647                 static ConstString g_sect_name_dwarf_debug_str (".debug_str");
    648                 static ConstString g_sect_name_eh_frame (".eh_frame");
    649                 SectionType section_type = eSectionTypeOther;
    650                 if (m_sect_headers[idx].flags & IMAGE_SCN_CNT_CODE &&
    651                     ((const_sect_name == g_code_sect_name) || (const_sect_name == g_CODE_sect_name)))
    652                 {
    653                     section_type = eSectionTypeCode;
    654                 }
    655                 else if (m_sect_headers[idx].flags & IMAGE_SCN_CNT_INITIALIZED_DATA &&
    656                          ((const_sect_name == g_data_sect_name) || (const_sect_name == g_DATA_sect_name)))
    657                 {
    658                     section_type = eSectionTypeData;
    659                 }
    660                 else if (m_sect_headers[idx].flags & IMAGE_SCN_CNT_UNINITIALIZED_DATA &&
    661                          ((const_sect_name == g_bss_sect_name) || (const_sect_name == g_BSS_sect_name)))
    662                 {
    663                     if (m_sect_headers[idx].size == 0)
    664                         section_type = eSectionTypeZeroFill;
    665                     else
    666                         section_type = eSectionTypeData;
    667                 }
    668                 else if (const_sect_name == g_debug_sect_name)
    669                 {
    670                     section_type = eSectionTypeDebug;
    671                 }
    672                 else if (const_sect_name == g_stabstr_sect_name)
    673                 {
    674                     section_type = eSectionTypeDataCString;
    675                 }
    676                 else if (const_sect_name == g_reloc_sect_name)
    677                 {
    678                     section_type = eSectionTypeOther;
    679                 }
    680                 else if (const_sect_name == g_sect_name_dwarf_debug_abbrev)    section_type = eSectionTypeDWARFDebugAbbrev;
    681                 else if (const_sect_name == g_sect_name_dwarf_debug_aranges)   section_type = eSectionTypeDWARFDebugAranges;
    682                 else if (const_sect_name == g_sect_name_dwarf_debug_frame)     section_type = eSectionTypeDWARFDebugFrame;
    683                 else if (const_sect_name == g_sect_name_dwarf_debug_info)      section_type = eSectionTypeDWARFDebugInfo;
    684                 else if (const_sect_name == g_sect_name_dwarf_debug_line)      section_type = eSectionTypeDWARFDebugLine;
    685                 else if (const_sect_name == g_sect_name_dwarf_debug_loc)       section_type = eSectionTypeDWARFDebugLoc;
    686                 else if (const_sect_name == g_sect_name_dwarf_debug_macinfo)   section_type = eSectionTypeDWARFDebugMacInfo;
    687                 else if (const_sect_name == g_sect_name_dwarf_debug_pubnames)  section_type = eSectionTypeDWARFDebugPubNames;
    688                 else if (const_sect_name == g_sect_name_dwarf_debug_pubtypes)  section_type = eSectionTypeDWARFDebugPubTypes;
    689                 else if (const_sect_name == g_sect_name_dwarf_debug_ranges)    section_type = eSectionTypeDWARFDebugRanges;
    690                 else if (const_sect_name == g_sect_name_dwarf_debug_str)       section_type = eSectionTypeDWARFDebugStr;
    691                 else if (const_sect_name == g_sect_name_eh_frame)              section_type = eSectionTypeEHFrame;
    692                 else if (m_sect_headers[idx].flags & IMAGE_SCN_CNT_CODE)
    693                 {
    694                     section_type = eSectionTypeCode;
    695                 }
    696                 else if (m_sect_headers[idx].flags & IMAGE_SCN_CNT_INITIALIZED_DATA)
    697                 {
    698                     section_type = eSectionTypeData;
    699                 }
    700                 else if (m_sect_headers[idx].flags & IMAGE_SCN_CNT_UNINITIALIZED_DATA)
    701                 {
    702                     if (m_sect_headers[idx].size == 0)
    703                         section_type = eSectionTypeZeroFill;
    704                     else
    705                         section_type = eSectionTypeData;
    706                 }
    707 
    708                 // Use a segment ID of the segment index shifted left by 8 so they
    709                 // never conflict with any of the sections.
    710                 SectionSP section_sp (new Section (module_sp,                    // Module to which this section belongs
    711                                                    this,                         // Object file to which this section belongs
    712                                                    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
    713                                                    const_sect_name,              // Name of this section
    714                                                    section_type,                 // This section is a container of other sections.
    715                                                    m_coff_header_opt.image_base + m_sect_headers[idx].vmaddr,   // File VM address == addresses as they are found in the object file
    716                                                    m_sect_headers[idx].vmsize,   // VM size in bytes of this section
    717                                                    m_sect_headers[idx].offset,   // Offset to the data for this section in the file
    718                                                    m_sect_headers[idx].size,     // Size in bytes of this section as found in the the file
    719                                                    m_sect_headers[idx].flags));  // Flags for this section
    720 
    721                 //section_sp->SetIsEncrypted (segment_is_encrypted);
    722 
    723                 unified_section_list.AddSection(section_sp);
    724                 m_sections_ap->AddSection (section_sp);
    725             }
    726         }
    727     }
    728 }
    729 
    730 bool
    731 ObjectFilePECOFF::GetUUID (UUID* uuid)
    732 {
    733     return false;
    734 }
    735 
    736 uint32_t
    737 ObjectFilePECOFF::GetDependentModules (FileSpecList& files)
    738 {
    739     return 0;
    740 }
    741 
    742 
    743 //----------------------------------------------------------------------
    744 // Dump
    745 //
    746 // Dump the specifics of the runtime file container (such as any headers
    747 // segments, sections, etc).
    748 //----------------------------------------------------------------------
    749 void
    750 ObjectFilePECOFF::Dump(Stream *s)
    751 {
    752     ModuleSP module_sp(GetModule());
    753     if (module_sp)
    754     {
    755         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
    756         s->Printf("%p: ", this);
    757         s->Indent();
    758         s->PutCString("ObjectFilePECOFF");
    759 
    760         ArchSpec header_arch;
    761         GetArchitecture (header_arch);
    762 
    763         *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
    764 
    765         SectionList *sections = GetSectionList();
    766         if (sections)
    767             sections->Dump(s, NULL, true, UINT32_MAX);
    768 
    769         if (m_symtab_ap.get())
    770             m_symtab_ap->Dump(s, NULL, eSortOrderNone);
    771 
    772         if (m_dos_header.e_magic)
    773             DumpDOSHeader (s, m_dos_header);
    774         if (m_coff_header.machine)
    775         {
    776             DumpCOFFHeader (s, m_coff_header);
    777             if (m_coff_header.hdrsize)
    778                 DumpOptCOFFHeader (s, m_coff_header_opt);
    779         }
    780         s->EOL();
    781         DumpSectionHeaders(s);
    782         s->EOL();
    783     }
    784 }
    785 
    786 //----------------------------------------------------------------------
    787 // DumpDOSHeader
    788 //
    789 // Dump the MS-DOS header to the specified output stream
    790 //----------------------------------------------------------------------
    791 void
    792 ObjectFilePECOFF::DumpDOSHeader(Stream *s, const dos_header_t& header)
    793 {
    794     s->PutCString ("MSDOS Header\n");
    795     s->Printf ("  e_magic    = 0x%4.4x\n", header.e_magic);
    796     s->Printf ("  e_cblp     = 0x%4.4x\n", header.e_cblp);
    797     s->Printf ("  e_cp       = 0x%4.4x\n", header.e_cp);
    798     s->Printf ("  e_crlc     = 0x%4.4x\n", header.e_crlc);
    799     s->Printf ("  e_cparhdr  = 0x%4.4x\n", header.e_cparhdr);
    800     s->Printf ("  e_minalloc = 0x%4.4x\n", header.e_minalloc);
    801     s->Printf ("  e_maxalloc = 0x%4.4x\n", header.e_maxalloc);
    802     s->Printf ("  e_ss       = 0x%4.4x\n", header.e_ss);
    803     s->Printf ("  e_sp       = 0x%4.4x\n", header.e_sp);
    804     s->Printf ("  e_csum     = 0x%4.4x\n", header.e_csum);
    805     s->Printf ("  e_ip       = 0x%4.4x\n", header.e_ip);
    806     s->Printf ("  e_cs       = 0x%4.4x\n", header.e_cs);
    807     s->Printf ("  e_lfarlc   = 0x%4.4x\n", header.e_lfarlc);
    808     s->Printf ("  e_ovno     = 0x%4.4x\n", header.e_ovno);
    809     s->Printf ("  e_res[4]   = { 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x }\n",
    810                header.e_res[0],
    811                header.e_res[1],
    812                header.e_res[2],
    813                header.e_res[3]);
    814     s->Printf ("  e_oemid    = 0x%4.4x\n", header.e_oemid);
    815     s->Printf ("  e_oeminfo  = 0x%4.4x\n", header.e_oeminfo);
    816     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",
    817                header.e_res2[0],
    818                header.e_res2[1],
    819                header.e_res2[2],
    820                header.e_res2[3],
    821                header.e_res2[4],
    822                header.e_res2[5],
    823                header.e_res2[6],
    824                header.e_res2[7],
    825                header.e_res2[8],
    826                header.e_res2[9]);
    827     s->Printf ("  e_lfanew   = 0x%8.8x\n", header.e_lfanew);
    828 }
    829 
    830 //----------------------------------------------------------------------
    831 // DumpCOFFHeader
    832 //
    833 // Dump the COFF header to the specified output stream
    834 //----------------------------------------------------------------------
    835 void
    836 ObjectFilePECOFF::DumpCOFFHeader(Stream *s, const coff_header_t& header)
    837 {
    838     s->PutCString ("COFF Header\n");
    839     s->Printf ("  machine = 0x%4.4x\n", header.machine);
    840     s->Printf ("  nsects  = 0x%4.4x\n", header.nsects);
    841     s->Printf ("  modtime = 0x%8.8x\n", header.modtime);
    842     s->Printf ("  symoff  = 0x%8.8x\n", header.symoff);
    843     s->Printf ("  nsyms   = 0x%8.8x\n", header.nsyms);
    844     s->Printf ("  hdrsize = 0x%4.4x\n", header.hdrsize);
    845 }
    846 
    847 //----------------------------------------------------------------------
    848 // DumpOptCOFFHeader
    849 //
    850 // Dump the optional COFF header to the specified output stream
    851 //----------------------------------------------------------------------
    852 void
    853 ObjectFilePECOFF::DumpOptCOFFHeader(Stream *s, const coff_opt_header_t& header)
    854 {
    855     s->PutCString ("Optional COFF Header\n");
    856     s->Printf ("  magic                   = 0x%4.4x\n", header.magic);
    857     s->Printf ("  major_linker_version    = 0x%2.2x\n", header.major_linker_version);
    858     s->Printf ("  minor_linker_version    = 0x%2.2x\n", header.minor_linker_version);
    859     s->Printf ("  code_size               = 0x%8.8x\n", header.code_size);
    860     s->Printf ("  data_size               = 0x%8.8x\n", header.data_size);
    861     s->Printf ("  bss_size                = 0x%8.8x\n", header.bss_size);
    862     s->Printf ("  entry                   = 0x%8.8x\n", header.entry);
    863     s->Printf ("  code_offset             = 0x%8.8x\n", header.code_offset);
    864     s->Printf ("  data_offset             = 0x%8.8x\n", header.data_offset);
    865     s->Printf ("  image_base              = 0x%16.16" PRIx64 "\n", header.image_base);
    866     s->Printf ("  sect_alignment          = 0x%8.8x\n", header.sect_alignment);
    867     s->Printf ("  file_alignment          = 0x%8.8x\n", header.file_alignment);
    868     s->Printf ("  major_os_system_version = 0x%4.4x\n", header.major_os_system_version);
    869     s->Printf ("  minor_os_system_version = 0x%4.4x\n", header.minor_os_system_version);
    870     s->Printf ("  major_image_version     = 0x%4.4x\n", header.major_image_version);
    871     s->Printf ("  minor_image_version     = 0x%4.4x\n", header.minor_image_version);
    872     s->Printf ("  major_subsystem_version = 0x%4.4x\n", header.major_subsystem_version);
    873     s->Printf ("  minor_subsystem_version = 0x%4.4x\n", header.minor_subsystem_version);
    874     s->Printf ("  reserved1               = 0x%8.8x\n", header.reserved1);
    875     s->Printf ("  image_size              = 0x%8.8x\n", header.image_size);
    876     s->Printf ("  header_size             = 0x%8.8x\n", header.header_size);
    877     s->Printf ("  checksum                = 0x%8.8x\n", header.checksum);
    878     s->Printf ("  subsystem               = 0x%4.4x\n", header.subsystem);
    879     s->Printf ("  dll_flags               = 0x%4.4x\n", header.dll_flags);
    880     s->Printf ("  stack_reserve_size      = 0x%16.16" PRIx64 "\n", header.stack_reserve_size);
    881     s->Printf ("  stack_commit_size       = 0x%16.16" PRIx64 "\n", header.stack_commit_size);
    882     s->Printf ("  heap_reserve_size       = 0x%16.16" PRIx64 "\n", header.heap_reserve_size);
    883     s->Printf ("  heap_commit_size        = 0x%16.16" PRIx64 "\n", header.heap_commit_size);
    884     s->Printf ("  loader_flags            = 0x%8.8x\n", header.loader_flags);
    885     s->Printf ("  num_data_dir_entries    = 0x%8.8zx\n", header.data_dirs.size());
    886     uint32_t i;
    887     for (i=0; i<header.data_dirs.size(); i++)
    888     {
    889         s->Printf ("  data_dirs[%2u] vmaddr = 0x%8.8x, vmsize = 0x%8.8x\n",
    890                    i,
    891                    header.data_dirs[i].vmaddr,
    892                    header.data_dirs[i].vmsize);
    893     }
    894 }
    895 //----------------------------------------------------------------------
    896 // DumpSectionHeader
    897 //
    898 // Dump a single ELF section header to the specified output stream
    899 //----------------------------------------------------------------------
    900 void
    901 ObjectFilePECOFF::DumpSectionHeader(Stream *s, const section_header_t& sh)
    902 {
    903     std::string name;
    904     GetSectionName(name, sh);
    905     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",
    906                name.c_str(),
    907                sh.vmaddr,
    908                sh.vmsize,
    909                sh.offset,
    910                sh.size,
    911                sh.reloff,
    912                sh.lineoff,
    913                sh.nreloc,
    914                sh.nline,
    915                sh.flags);
    916 }
    917 
    918 
    919 //----------------------------------------------------------------------
    920 // DumpSectionHeaders
    921 //
    922 // Dump all of the ELF section header to the specified output stream
    923 //----------------------------------------------------------------------
    924 void
    925 ObjectFilePECOFF::DumpSectionHeaders(Stream *s)
    926 {
    927 
    928     s->PutCString ("Section Headers\n");
    929     s->PutCString ("IDX  name             vm addr    vm size    file off   file size  reloc off  line off   nreloc nline  flags\n");
    930     s->PutCString ("==== ---------------- ---------- ---------- ---------- ---------- ---------- ---------- ------ ------ ----------\n");
    931 
    932     uint32_t idx = 0;
    933     SectionHeaderCollIter pos, end = m_sect_headers.end();
    934 
    935     for (pos = m_sect_headers.begin(); pos != end; ++pos, ++idx)
    936     {
    937         s->Printf ("[%2u] ", idx);
    938         ObjectFilePECOFF::DumpSectionHeader(s, *pos);
    939     }
    940 }
    941 
    942 static bool
    943 COFFMachineToMachCPU (uint16_t machine, ArchSpec &arch)
    944 {
    945     switch (machine)
    946     {
    947         case IMAGE_FILE_MACHINE_AMD64:
    948         case IMAGE_FILE_MACHINE_IA64:
    949             arch.SetArchitecture (eArchTypeMachO,
    950                                   llvm::MachO::CPUTypeX86_64,
    951                                   llvm::MachO::CPUSubType_X86_64_ALL);
    952             return true;
    953 
    954         case IMAGE_FILE_MACHINE_I386:
    955             arch.SetArchitecture (eArchTypeMachO,
    956                                   llvm::MachO::CPUTypeI386,
    957                                   llvm::MachO::CPUSubType_I386_ALL);
    958             return true;
    959 
    960         case IMAGE_FILE_MACHINE_POWERPC:
    961         case IMAGE_FILE_MACHINE_POWERPCFP:
    962             arch.SetArchitecture (eArchTypeMachO,
    963                                   llvm::MachO::CPUTypePowerPC,
    964                                   llvm::MachO::CPUSubType_POWERPC_ALL);
    965             return true;
    966         case IMAGE_FILE_MACHINE_ARM:
    967         case IMAGE_FILE_MACHINE_THUMB:
    968             arch.SetArchitecture (eArchTypeMachO,
    969                                   llvm::MachO::CPUTypeARM,
    970                                   llvm::MachO::CPUSubType_ARM_V7);
    971             return true;
    972     }
    973     return false;
    974 }
    975 bool
    976 ObjectFilePECOFF::GetArchitecture (ArchSpec &arch)
    977 {
    978     // For index zero return our cpu type
    979     return COFFMachineToMachCPU (m_coff_header.machine, arch);
    980 }
    981 
    982 ObjectFile::Type
    983 ObjectFilePECOFF::CalculateType()
    984 {
    985     if (m_coff_header.machine != 0)
    986     {
    987         if ((m_coff_header.flags & IMAGE_FILE_DLL) == 0)
    988             return eTypeExecutable;
    989         else
    990             return eTypeSharedLibrary;
    991     }
    992     return eTypeExecutable;
    993 }
    994 
    995 ObjectFile::Strata
    996 ObjectFilePECOFF::CalculateStrata()
    997 {
    998     return eStrataUser;
    999 }
   1000 //------------------------------------------------------------------
   1001 // PluginInterface protocol
   1002 //------------------------------------------------------------------
   1003 ConstString
   1004 ObjectFilePECOFF::GetPluginName()
   1005 {
   1006     return GetPluginNameStatic();
   1007 }
   1008 
   1009 uint32_t
   1010 ObjectFilePECOFF::GetPluginVersion()
   1011 {
   1012     return 1;
   1013 }
   1014 
   1015