Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 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" |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 19 | #include "lldb/Core/ModuleSpec.h" |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 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 | |
Greg Clayton | 28469ca | 2011-09-10 01:04:42 +0000 | [diff] [blame] | 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 | |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 116 | using namespace lldb; |
| 117 | using namespace lldb_private; |
| 118 | |
| 119 | void |
| 120 | ObjectFilePECOFF::Initialize() |
| 121 | { |
| 122 | PluginManager::RegisterPlugin (GetPluginNameStatic(), |
| 123 | GetPluginDescriptionStatic(), |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 124 | CreateInstance, |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 125 | CreateMemoryInstance, |
| 126 | GetModuleSpecifications); |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | void |
| 130 | ObjectFilePECOFF::Terminate() |
| 131 | { |
| 132 | PluginManager::UnregisterPlugin (CreateInstance); |
| 133 | } |
| 134 | |
| 135 | |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame^] | 136 | lldb_private::ConstString |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 137 | ObjectFilePECOFF::GetPluginNameStatic() |
| 138 | { |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame^] | 139 | static ConstString g_name("pe-coff"); |
| 140 | return g_name; |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 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 * |
Greg Clayton | 5ce9c56 | 2013-02-06 17:22:03 +0000 | [diff] [blame] | 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) |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 157 | { |
Greg Clayton | 5ce9c56 | 2013-02-06 17:22:03 +0000 | [diff] [blame] | 158 | if (!data_sp) |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 159 | { |
Greg Clayton | 5ce9c56 | 2013-02-06 17:22:03 +0000 | [diff] [blame] | 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); |
Greg Clayton | 7b0992d | 2013-04-18 22:45:39 +0000 | [diff] [blame] | 169 | std::unique_ptr<ObjectFile> objfile_ap(new ObjectFilePECOFF (module_sp, data_sp, data_offset, file, file_offset, length)); |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 170 | if (objfile_ap.get() && objfile_ap->ParseHeader()) |
| 171 | return objfile_ap.release(); |
| 172 | } |
| 173 | return NULL; |
| 174 | } |
| 175 | |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 176 | ObjectFile * |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 177 | ObjectFilePECOFF::CreateMemoryInstance (const lldb::ModuleSP &module_sp, |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 178 | lldb::DataBufferSP& data_sp, |
| 179 | const lldb::ProcessSP &process_sp, |
| 180 | lldb::addr_t header_addr) |
| 181 | { |
| 182 | return NULL; |
| 183 | } |
| 184 | |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 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 | |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 197 | bool |
Greg Clayton | 5ce9c56 | 2013-02-06 17:22:03 +0000 | [diff] [blame] | 198 | ObjectFilePECOFF::MagicBytesMatch (DataBufferSP& data_sp) |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 199 | { |
Greg Clayton | 5ce9c56 | 2013-02-06 17:22:03 +0000 | [diff] [blame] | 200 | DataExtractor data(data_sp, eByteOrderLittle, 4); |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 201 | lldb::offset_t offset = 0; |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 202 | uint16_t magic = data.GetU16 (&offset); |
| 203 | return magic == IMAGE_DOS_SIGNATURE; |
| 204 | } |
| 205 | |
| 206 | |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 207 | ObjectFilePECOFF::ObjectFilePECOFF (const lldb::ModuleSP &module_sp, |
Greg Clayton | 5ce9c56 | 2013-02-06 17:22:03 +0000 | [diff] [blame] | 208 | DataBufferSP& data_sp, |
| 209 | lldb::offset_t data_offset, |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 210 | const FileSpec* file, |
Greg Clayton | 5ce9c56 | 2013-02-06 17:22:03 +0000 | [diff] [blame] | 211 | lldb::offset_t file_offset, |
| 212 | lldb::offset_t length) : |
| 213 | ObjectFile (module_sp, file, file_offset, length, data_sp, data_offset), |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 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 | { |
Greg Clayton | a174349 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 233 | ModuleSP module_sp(GetModule()); |
| 234 | if (module_sp) |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 235 | { |
Greg Clayton | a174349 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 236 | lldb_private::Mutex::Locker locker(module_sp->GetMutex()); |
| 237 | m_sect_headers.clear(); |
| 238 | m_data.SetByteOrder (eByteOrderLittle); |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 239 | lldb::offset_t offset = 0; |
Greg Clayton | a174349 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 240 | |
| 241 | if (ParseDOSHeader()) |
Greg Clayton | 28469ca | 2011-09-10 01:04:42 +0000 | [diff] [blame] | 242 | { |
Greg Clayton | a174349 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 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 | } |
Greg Clayton | a174349 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 253 | return true; |
Greg Clayton | 28469ca | 2011-09-10 01:04:42 +0000 | [diff] [blame] | 254 | } |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 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 | |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 272 | uint32_t |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 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; |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 304 | lldb::offset_t offset = 0; |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 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 |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 359 | ObjectFilePECOFF::ParseCOFFHeader(lldb::offset_t *offset_ptr) |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 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 |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 378 | ObjectFilePECOFF::ParseCOFFOptionalHeader(lldb::offset_t *offset_ptr) |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 379 | { |
| 380 | bool success = false; |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 381 | const lldb::offset_t end_offset = *offset_ptr + m_coff_header.hdrsize; |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 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); |
Greg Clayton | 28469ca | 2011-09-10 01:04:42 +0000 | [diff] [blame] | 420 | m_coff_header_opt.checksum = m_data.GetU32(offset_ptr); |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 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 | |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 462 | lldb::offset_t offset = 0; |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 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 | { |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 495 | lldb::offset_t stroff = strtoul(§.name[1], NULL, 10); |
| 496 | lldb::offset_t string_file_offset = m_coff_header.symoff + (m_coff_header.nsyms * 18) + stroff; |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 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 | { |
Greg Clayton | a174349 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 516 | ModuleSP module_sp(GetModule()); |
| 517 | if (module_sp) |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 518 | { |
Greg Clayton | a174349 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 519 | lldb_private::Mutex::Locker locker(module_sp->GetMutex()); |
| 520 | if (m_symtab_ap.get() == NULL) |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 521 | { |
Greg Clayton | a174349 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 522 | SectionList *sect_list = GetSectionList(); |
| 523 | m_symtab_ap.reset(new Symtab(this)); |
| 524 | Mutex::Locker symtab_locker (m_symtab_ap->GetMutex()); |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 525 | |
Greg Clayton | a174349 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 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 = sizeof(section_header_t); |
| 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); |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 536 | lldb::offset_t offset = symbol_data_size; |
Greg Clayton | a174349 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 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 + 4, strtab_size)); |
| 539 | DataExtractor strtab_data (strtab_data_sp, GetByteOrder(), addr_byte_size); |
| 540 | |
| 541 | offset = 0; |
| 542 | std::string symbol_name; |
| 543 | Symbol *symbols = m_symtab_ap->Resize (num_syms); |
| 544 | for (uint32_t i=0; i<num_syms; ++i) |
| 545 | { |
| 546 | coff_symbol_t symbol; |
| 547 | const uint32_t symbol_offset = offset; |
| 548 | const char *symbol_name_cstr = NULL; |
| 549 | // If the first 4 bytes of the symbol string are zero, then we |
| 550 | // it is followed by a 4 byte string table offset. Else these |
| 551 | // 8 bytes contain the symbol name |
| 552 | if (symtab_data.GetU32 (&offset) == 0) |
| 553 | { |
| 554 | // Long string that doesn't fit into the symbol table name, |
| 555 | // so now we must read the 4 byte string table offset |
| 556 | uint32_t strtab_offset = symtab_data.GetU32 (&offset); |
| 557 | symbol_name_cstr = strtab_data.PeekCStr (strtab_offset); |
| 558 | symbol_name.assign (symbol_name_cstr); |
| 559 | } |
| 560 | else |
| 561 | { |
| 562 | // Short string that fits into the symbol table name which is 8 bytes |
| 563 | offset += sizeof(symbol.name) - 4; // Skip remaining |
| 564 | symbol_name_cstr = symtab_data.PeekCStr (symbol_offset); |
| 565 | if (symbol_name_cstr == NULL) |
| 566 | break; |
| 567 | symbol_name.assign (symbol_name_cstr, sizeof(symbol.name)); |
| 568 | } |
| 569 | symbol.value = symtab_data.GetU32 (&offset); |
| 570 | symbol.sect = symtab_data.GetU16 (&offset); |
| 571 | symbol.type = symtab_data.GetU16 (&offset); |
| 572 | symbol.storage = symtab_data.GetU8 (&offset); |
| 573 | symbol.naux = symtab_data.GetU8 (&offset); |
| 574 | Address symbol_addr(sect_list->GetSectionAtIndex(symbol.sect-1), symbol.value); |
Greg Clayton | 037520e | 2012-07-18 23:18:10 +0000 | [diff] [blame] | 575 | symbols[i].GetMangled ().SetValue (ConstString(symbol_name.c_str())); |
Greg Clayton | a174349 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 576 | symbols[i].GetAddress() = symbol_addr; |
| 577 | |
| 578 | if (symbol.naux > 0) |
| 579 | i += symbol.naux; |
| 580 | } |
| 581 | |
| 582 | } |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 583 | } |
| 584 | } |
| 585 | return m_symtab_ap.get(); |
| 586 | |
| 587 | } |
| 588 | |
| 589 | SectionList * |
| 590 | ObjectFilePECOFF::GetSectionList() |
| 591 | { |
Greg Clayton | a174349 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 592 | ModuleSP module_sp(GetModule()); |
| 593 | if (module_sp) |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 594 | { |
Greg Clayton | a174349 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 595 | lldb_private::Mutex::Locker locker(module_sp->GetMutex()); |
| 596 | if (m_sections_ap.get() == NULL) |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 597 | { |
Greg Clayton | a174349 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 598 | m_sections_ap.reset(new SectionList()); |
| 599 | const uint32_t nsects = m_sect_headers.size(); |
| 600 | ModuleSP module_sp (GetModule()); |
| 601 | for (uint32_t idx = 0; idx<nsects; ++idx) |
Greg Clayton | 28469ca | 2011-09-10 01:04:42 +0000 | [diff] [blame] | 602 | { |
Greg Clayton | a174349 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 603 | std::string sect_name; |
| 604 | GetSectionName (sect_name, m_sect_headers[idx]); |
| 605 | ConstString const_sect_name (sect_name.c_str()); |
| 606 | static ConstString g_code_sect_name (".code"); |
| 607 | static ConstString g_CODE_sect_name ("CODE"); |
| 608 | static ConstString g_data_sect_name (".data"); |
| 609 | static ConstString g_DATA_sect_name ("DATA"); |
| 610 | static ConstString g_bss_sect_name (".bss"); |
| 611 | static ConstString g_BSS_sect_name ("BSS"); |
| 612 | static ConstString g_debug_sect_name (".debug"); |
| 613 | static ConstString g_reloc_sect_name (".reloc"); |
| 614 | static ConstString g_stab_sect_name (".stab"); |
| 615 | static ConstString g_stabstr_sect_name (".stabstr"); |
| 616 | SectionType section_type = eSectionTypeOther; |
| 617 | if (m_sect_headers[idx].flags & IMAGE_SCN_CNT_CODE && |
| 618 | ((const_sect_name == g_code_sect_name) || (const_sect_name == g_CODE_sect_name))) |
| 619 | { |
| 620 | section_type = eSectionTypeCode; |
| 621 | } |
| 622 | else if (m_sect_headers[idx].flags & IMAGE_SCN_CNT_INITIALIZED_DATA && |
| 623 | ((const_sect_name == g_data_sect_name) || (const_sect_name == g_DATA_sect_name))) |
| 624 | { |
Greg Clayton | 28469ca | 2011-09-10 01:04:42 +0000 | [diff] [blame] | 625 | section_type = eSectionTypeData; |
Greg Clayton | a174349 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 626 | } |
| 627 | else if (m_sect_headers[idx].flags & IMAGE_SCN_CNT_UNINITIALIZED_DATA && |
| 628 | ((const_sect_name == g_bss_sect_name) || (const_sect_name == g_BSS_sect_name))) |
| 629 | { |
| 630 | if (m_sect_headers[idx].size == 0) |
| 631 | section_type = eSectionTypeZeroFill; |
| 632 | else |
| 633 | section_type = eSectionTypeData; |
| 634 | } |
| 635 | else if (const_sect_name == g_debug_sect_name) |
| 636 | { |
| 637 | section_type = eSectionTypeDebug; |
| 638 | } |
| 639 | else if (const_sect_name == g_stabstr_sect_name) |
| 640 | { |
| 641 | section_type = eSectionTypeDataCString; |
| 642 | } |
| 643 | else if (const_sect_name == g_reloc_sect_name) |
| 644 | { |
| 645 | section_type = eSectionTypeOther; |
| 646 | } |
| 647 | else if (m_sect_headers[idx].flags & IMAGE_SCN_CNT_CODE) |
| 648 | { |
| 649 | section_type = eSectionTypeCode; |
| 650 | } |
| 651 | else if (m_sect_headers[idx].flags & IMAGE_SCN_CNT_INITIALIZED_DATA) |
| 652 | { |
Greg Clayton | 28469ca | 2011-09-10 01:04:42 +0000 | [diff] [blame] | 653 | section_type = eSectionTypeData; |
Greg Clayton | a174349 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 654 | } |
| 655 | else if (m_sect_headers[idx].flags & IMAGE_SCN_CNT_UNINITIALIZED_DATA) |
| 656 | { |
| 657 | if (m_sect_headers[idx].size == 0) |
| 658 | section_type = eSectionTypeZeroFill; |
| 659 | else |
| 660 | section_type = eSectionTypeData; |
| 661 | } |
| 662 | |
| 663 | // Use a segment ID of the segment index shifted left by 8 so they |
| 664 | // never conflict with any of the sections. |
| 665 | SectionSP section_sp (new Section (module_sp, // Module to which this section belongs |
| 666 | 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 |
| 667 | const_sect_name, // Name of this section |
| 668 | section_type, // This section is a container of other sections. |
| 669 | m_sect_headers[idx].vmaddr, // File VM address == addresses as they are found in the object file |
| 670 | m_sect_headers[idx].vmsize, // VM size in bytes of this section |
| 671 | m_sect_headers[idx].offset, // Offset to the data for this section in the file |
| 672 | m_sect_headers[idx].size, // Size in bytes of this section as found in the the file |
| 673 | m_sect_headers[idx].flags)); // Flags for this section |
| 674 | |
| 675 | //section_sp->SetIsEncrypted (segment_is_encrypted); |
| 676 | |
| 677 | m_sections_ap->AddSection(section_sp); |
Greg Clayton | 28469ca | 2011-09-10 01:04:42 +0000 | [diff] [blame] | 678 | } |
Sean Callanan | 5677536 | 2012-06-08 02:16:08 +0000 | [diff] [blame] | 679 | |
| 680 | m_sections_ap->Finalize(); // Now that we're done adding sections, finalize to build fast-lookup caches |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 681 | } |
| 682 | } |
| 683 | return m_sections_ap.get(); |
| 684 | } |
| 685 | |
| 686 | bool |
| 687 | ObjectFilePECOFF::GetUUID (UUID* uuid) |
| 688 | { |
| 689 | return false; |
| 690 | } |
| 691 | |
| 692 | uint32_t |
| 693 | ObjectFilePECOFF::GetDependentModules (FileSpecList& files) |
| 694 | { |
| 695 | return 0; |
| 696 | } |
| 697 | |
| 698 | |
| 699 | //---------------------------------------------------------------------- |
| 700 | // Dump |
| 701 | // |
| 702 | // Dump the specifics of the runtime file container (such as any headers |
| 703 | // segments, sections, etc). |
| 704 | //---------------------------------------------------------------------- |
| 705 | void |
| 706 | ObjectFilePECOFF::Dump(Stream *s) |
| 707 | { |
Greg Clayton | a174349 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 708 | ModuleSP module_sp(GetModule()); |
| 709 | if (module_sp) |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 710 | { |
Greg Clayton | a174349 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 711 | lldb_private::Mutex::Locker locker(module_sp->GetMutex()); |
| 712 | s->Printf("%p: ", this); |
| 713 | s->Indent(); |
| 714 | s->PutCString("ObjectFilePECOFF"); |
| 715 | |
| 716 | ArchSpec header_arch; |
| 717 | GetArchitecture (header_arch); |
| 718 | |
| 719 | *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n"; |
| 720 | |
| 721 | if (m_sections_ap.get()) |
| 722 | m_sections_ap->Dump(s, NULL, true, UINT32_MAX); |
| 723 | |
| 724 | if (m_symtab_ap.get()) |
| 725 | m_symtab_ap->Dump(s, NULL, eSortOrderNone); |
| 726 | |
| 727 | if (m_dos_header.e_magic) |
| 728 | DumpDOSHeader (s, m_dos_header); |
| 729 | if (m_coff_header.machine) |
| 730 | { |
| 731 | DumpCOFFHeader (s, m_coff_header); |
| 732 | if (m_coff_header.hdrsize) |
| 733 | DumpOptCOFFHeader (s, m_coff_header_opt); |
| 734 | } |
| 735 | s->EOL(); |
| 736 | DumpSectionHeaders(s); |
| 737 | s->EOL(); |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 738 | } |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | //---------------------------------------------------------------------- |
| 742 | // DumpDOSHeader |
| 743 | // |
| 744 | // Dump the MS-DOS header to the specified output stream |
| 745 | //---------------------------------------------------------------------- |
| 746 | void |
| 747 | ObjectFilePECOFF::DumpDOSHeader(Stream *s, const dos_header_t& header) |
| 748 | { |
| 749 | s->PutCString ("MSDOS Header\n"); |
| 750 | s->Printf (" e_magic = 0x%4.4x\n", header.e_magic); |
| 751 | s->Printf (" e_cblp = 0x%4.4x\n", header.e_cblp); |
| 752 | s->Printf (" e_cp = 0x%4.4x\n", header.e_cp); |
| 753 | s->Printf (" e_crlc = 0x%4.4x\n", header.e_crlc); |
| 754 | s->Printf (" e_cparhdr = 0x%4.4x\n", header.e_cparhdr); |
| 755 | s->Printf (" e_minalloc = 0x%4.4x\n", header.e_minalloc); |
| 756 | s->Printf (" e_maxalloc = 0x%4.4x\n", header.e_maxalloc); |
| 757 | s->Printf (" e_ss = 0x%4.4x\n", header.e_ss); |
| 758 | s->Printf (" e_sp = 0x%4.4x\n", header.e_sp); |
| 759 | s->Printf (" e_csum = 0x%4.4x\n", header.e_csum); |
| 760 | s->Printf (" e_ip = 0x%4.4x\n", header.e_ip); |
| 761 | s->Printf (" e_cs = 0x%4.4x\n", header.e_cs); |
| 762 | s->Printf (" e_lfarlc = 0x%4.4x\n", header.e_lfarlc); |
| 763 | s->Printf (" e_ovno = 0x%4.4x\n", header.e_ovno); |
| 764 | s->Printf (" e_res[4] = { 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x }\n", |
| 765 | header.e_res[0], |
| 766 | header.e_res[1], |
| 767 | header.e_res[2], |
| 768 | header.e_res[3]); |
| 769 | s->Printf (" e_oemid = 0x%4.4x\n", header.e_oemid); |
| 770 | s->Printf (" e_oeminfo = 0x%4.4x\n", header.e_oeminfo); |
| 771 | 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", |
| 772 | header.e_res2[0], |
| 773 | header.e_res2[1], |
| 774 | header.e_res2[2], |
| 775 | header.e_res2[3], |
| 776 | header.e_res2[4], |
| 777 | header.e_res2[5], |
| 778 | header.e_res2[6], |
| 779 | header.e_res2[7], |
| 780 | header.e_res2[8], |
| 781 | header.e_res2[9]); |
| 782 | s->Printf (" e_lfanew = 0x%8.8x\n", header.e_lfanew); |
| 783 | } |
| 784 | |
| 785 | //---------------------------------------------------------------------- |
| 786 | // DumpCOFFHeader |
| 787 | // |
| 788 | // Dump the COFF header to the specified output stream |
| 789 | //---------------------------------------------------------------------- |
| 790 | void |
| 791 | ObjectFilePECOFF::DumpCOFFHeader(Stream *s, const coff_header_t& header) |
| 792 | { |
| 793 | s->PutCString ("COFF Header\n"); |
| 794 | s->Printf (" machine = 0x%4.4x\n", header.machine); |
| 795 | s->Printf (" nsects = 0x%4.4x\n", header.nsects); |
| 796 | s->Printf (" modtime = 0x%8.8x\n", header.modtime); |
| 797 | s->Printf (" symoff = 0x%8.8x\n", header.symoff); |
| 798 | s->Printf (" nsyms = 0x%8.8x\n", header.nsyms); |
| 799 | s->Printf (" hdrsize = 0x%4.4x\n", header.hdrsize); |
| 800 | } |
| 801 | |
| 802 | //---------------------------------------------------------------------- |
| 803 | // DumpOptCOFFHeader |
| 804 | // |
| 805 | // Dump the optional COFF header to the specified output stream |
| 806 | //---------------------------------------------------------------------- |
| 807 | void |
| 808 | ObjectFilePECOFF::DumpOptCOFFHeader(Stream *s, const coff_opt_header_t& header) |
| 809 | { |
| 810 | s->PutCString ("Optional COFF Header\n"); |
| 811 | s->Printf (" magic = 0x%4.4x\n", header.magic); |
| 812 | s->Printf (" major_linker_version = 0x%2.2x\n", header.major_linker_version); |
| 813 | s->Printf (" minor_linker_version = 0x%2.2x\n", header.minor_linker_version); |
| 814 | s->Printf (" code_size = 0x%8.8x\n", header.code_size); |
| 815 | s->Printf (" data_size = 0x%8.8x\n", header.data_size); |
| 816 | s->Printf (" bss_size = 0x%8.8x\n", header.bss_size); |
| 817 | s->Printf (" entry = 0x%8.8x\n", header.entry); |
| 818 | s->Printf (" code_offset = 0x%8.8x\n", header.code_offset); |
| 819 | s->Printf (" data_offset = 0x%8.8x\n", header.data_offset); |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 820 | s->Printf (" image_base = 0x%16.16" PRIx64 "\n", header.image_base); |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 821 | s->Printf (" sect_alignment = 0x%8.8x\n", header.sect_alignment); |
| 822 | s->Printf (" file_alignment = 0x%8.8x\n", header.file_alignment); |
| 823 | s->Printf (" major_os_system_version = 0x%4.4x\n", header.major_os_system_version); |
| 824 | s->Printf (" minor_os_system_version = 0x%4.4x\n", header.minor_os_system_version); |
| 825 | s->Printf (" major_image_version = 0x%4.4x\n", header.major_image_version); |
| 826 | s->Printf (" minor_image_version = 0x%4.4x\n", header.minor_image_version); |
| 827 | s->Printf (" major_subsystem_version = 0x%4.4x\n", header.major_subsystem_version); |
| 828 | s->Printf (" minor_subsystem_version = 0x%4.4x\n", header.minor_subsystem_version); |
| 829 | s->Printf (" reserved1 = 0x%8.8x\n", header.reserved1); |
| 830 | s->Printf (" image_size = 0x%8.8x\n", header.image_size); |
| 831 | s->Printf (" header_size = 0x%8.8x\n", header.header_size); |
Greg Clayton | 28469ca | 2011-09-10 01:04:42 +0000 | [diff] [blame] | 832 | s->Printf (" checksum = 0x%8.8x\n", header.checksum); |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 833 | s->Printf (" subsystem = 0x%4.4x\n", header.subsystem); |
| 834 | s->Printf (" dll_flags = 0x%4.4x\n", header.dll_flags); |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 835 | s->Printf (" stack_reserve_size = 0x%16.16" PRIx64 "\n", header.stack_reserve_size); |
| 836 | s->Printf (" stack_commit_size = 0x%16.16" PRIx64 "\n", header.stack_commit_size); |
| 837 | s->Printf (" heap_reserve_size = 0x%16.16" PRIx64 "\n", header.heap_reserve_size); |
| 838 | s->Printf (" heap_commit_size = 0x%16.16" PRIx64 "\n", header.heap_commit_size); |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 839 | s->Printf (" loader_flags = 0x%8.8x\n", header.loader_flags); |
| 840 | s->Printf (" num_data_dir_entries = 0x%8.8zx\n", header.data_dirs.size()); |
| 841 | uint32_t i; |
| 842 | for (i=0; i<header.data_dirs.size(); i++) |
| 843 | { |
Greg Clayton | 28469ca | 2011-09-10 01:04:42 +0000 | [diff] [blame] | 844 | s->Printf (" data_dirs[%2u] vmaddr = 0x%8.8x, vmsize = 0x%8.8x\n", |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 845 | i, |
| 846 | header.data_dirs[i].vmaddr, |
| 847 | header.data_dirs[i].vmsize); |
| 848 | } |
| 849 | } |
| 850 | //---------------------------------------------------------------------- |
| 851 | // DumpSectionHeader |
| 852 | // |
| 853 | // Dump a single ELF section header to the specified output stream |
| 854 | //---------------------------------------------------------------------- |
| 855 | void |
| 856 | ObjectFilePECOFF::DumpSectionHeader(Stream *s, const section_header_t& sh) |
| 857 | { |
| 858 | std::string name; |
| 859 | GetSectionName(name, sh); |
| 860 | 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", |
| 861 | name.c_str(), |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 862 | sh.vmaddr, |
Greg Clayton | 28469ca | 2011-09-10 01:04:42 +0000 | [diff] [blame] | 863 | sh.vmsize, |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 864 | sh.offset, |
Greg Clayton | 28469ca | 2011-09-10 01:04:42 +0000 | [diff] [blame] | 865 | sh.size, |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 866 | sh.reloff, |
| 867 | sh.lineoff, |
| 868 | sh.nreloc, |
| 869 | sh.nline, |
| 870 | sh.flags); |
| 871 | } |
| 872 | |
| 873 | |
| 874 | //---------------------------------------------------------------------- |
| 875 | // DumpSectionHeaders |
| 876 | // |
| 877 | // Dump all of the ELF section header to the specified output stream |
| 878 | //---------------------------------------------------------------------- |
| 879 | void |
| 880 | ObjectFilePECOFF::DumpSectionHeaders(Stream *s) |
| 881 | { |
| 882 | |
| 883 | s->PutCString ("Section Headers\n"); |
Greg Clayton | 28469ca | 2011-09-10 01:04:42 +0000 | [diff] [blame] | 884 | s->PutCString ("IDX name vm addr vm size file off file size reloc off line off nreloc nline flags\n"); |
| 885 | s->PutCString ("==== ---------------- ---------- ---------- ---------- ---------- ---------- ---------- ------ ------ ----------\n"); |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 886 | |
| 887 | uint32_t idx = 0; |
| 888 | SectionHeaderCollIter pos, end = m_sect_headers.end(); |
| 889 | |
| 890 | for (pos = m_sect_headers.begin(); pos != end; ++pos, ++idx) |
| 891 | { |
Greg Clayton | 28469ca | 2011-09-10 01:04:42 +0000 | [diff] [blame] | 892 | s->Printf ("[%2u] ", idx); |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 893 | ObjectFilePECOFF::DumpSectionHeader(s, *pos); |
| 894 | } |
| 895 | } |
| 896 | |
| 897 | static bool |
| 898 | COFFMachineToMachCPU (uint16_t machine, ArchSpec &arch) |
| 899 | { |
| 900 | switch (machine) |
| 901 | { |
| 902 | case IMAGE_FILE_MACHINE_AMD64: |
| 903 | case IMAGE_FILE_MACHINE_IA64: |
| 904 | arch.SetArchitecture (eArchTypeMachO, |
| 905 | llvm::MachO::CPUTypeX86_64, |
| 906 | llvm::MachO::CPUSubType_X86_64_ALL); |
| 907 | return true; |
| 908 | |
| 909 | case IMAGE_FILE_MACHINE_I386: |
| 910 | arch.SetArchitecture (eArchTypeMachO, |
| 911 | llvm::MachO::CPUTypeI386, |
| 912 | llvm::MachO::CPUSubType_I386_ALL); |
| 913 | return true; |
| 914 | |
| 915 | case IMAGE_FILE_MACHINE_POWERPC: |
| 916 | case IMAGE_FILE_MACHINE_POWERPCFP: |
| 917 | arch.SetArchitecture (eArchTypeMachO, |
| 918 | llvm::MachO::CPUTypePowerPC, |
| 919 | llvm::MachO::CPUSubType_POWERPC_ALL); |
| 920 | return true; |
| 921 | case IMAGE_FILE_MACHINE_ARM: |
| 922 | case IMAGE_FILE_MACHINE_THUMB: |
| 923 | arch.SetArchitecture (eArchTypeMachO, |
| 924 | llvm::MachO::CPUTypeARM, |
| 925 | llvm::MachO::CPUSubType_ARM_V7); |
| 926 | return true; |
| 927 | } |
| 928 | return false; |
| 929 | } |
| 930 | bool |
| 931 | ObjectFilePECOFF::GetArchitecture (ArchSpec &arch) |
| 932 | { |
| 933 | // For index zero return our cpu type |
| 934 | return COFFMachineToMachCPU (m_coff_header.machine, arch); |
| 935 | } |
| 936 | |
| 937 | ObjectFile::Type |
| 938 | ObjectFilePECOFF::CalculateType() |
| 939 | { |
| 940 | if (m_coff_header.machine != 0) |
| 941 | { |
| 942 | if ((m_coff_header.flags & IMAGE_FILE_DLL) == 0) |
| 943 | return eTypeExecutable; |
| 944 | else |
| 945 | return eTypeSharedLibrary; |
| 946 | } |
| 947 | return eTypeExecutable; |
| 948 | } |
| 949 | |
| 950 | ObjectFile::Strata |
| 951 | ObjectFilePECOFF::CalculateStrata() |
| 952 | { |
| 953 | return eStrataUser; |
| 954 | } |
| 955 | //------------------------------------------------------------------ |
| 956 | // PluginInterface protocol |
| 957 | //------------------------------------------------------------------ |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame^] | 958 | ConstString |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 959 | ObjectFilePECOFF::GetPluginName() |
| 960 | { |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 961 | return GetPluginNameStatic(); |
| 962 | } |
| 963 | |
| 964 | uint32_t |
| 965 | ObjectFilePECOFF::GetPluginVersion() |
| 966 | { |
| 967 | return 1; |
| 968 | } |
| 969 | |