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