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 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 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 CreateMemoryInstance); 125 } 126 127 void 128 ObjectFilePECOFF::Terminate() 129 { 130 PluginManager::UnregisterPlugin (CreateInstance); 131 } 132 133 134 const char * 135 ObjectFilePECOFF::GetPluginNameStatic() 136 { 137 return "object-file.pe-coff"; 138 } 139 140 const char * 141 ObjectFilePECOFF::GetPluginDescriptionStatic() 142 { 143 return "Portable Executable and Common Object File Format object file reader (32 and 64 bit)"; 144 } 145 146 147 ObjectFile * 148 ObjectFilePECOFF::CreateInstance (Module* module, DataBufferSP& dataSP, const FileSpec* file, addr_t offset, addr_t length) 149 { 150 if (ObjectFilePECOFF::MagicBytesMatch(dataSP)) 151 { 152 std::auto_ptr<ObjectFile> objfile_ap(new ObjectFilePECOFF (module, dataSP, file, offset, length)); 153 if (objfile_ap.get() && objfile_ap->ParseHeader()) 154 return objfile_ap.release(); 155 } 156 return NULL; 157 } 158 159 ObjectFile * 160 ObjectFilePECOFF::CreateMemoryInstance (lldb_private::Module* module, 161 lldb::DataBufferSP& data_sp, 162 const lldb::ProcessSP &process_sp, 163 lldb::addr_t header_addr) 164 { 165 return NULL; 166 } 167 168 bool 169 ObjectFilePECOFF::MagicBytesMatch (DataBufferSP& dataSP) 170 { 171 DataExtractor data(dataSP, eByteOrderLittle, 4); 172 uint32_t offset = 0; 173 uint16_t magic = data.GetU16 (&offset); 174 return magic == IMAGE_DOS_SIGNATURE; 175 } 176 177 178 ObjectFilePECOFF::ObjectFilePECOFF (Module* module, 179 DataBufferSP& dataSP, 180 const FileSpec* file, 181 addr_t offset, 182 addr_t length) : 183 ObjectFile (module, file, offset, length, dataSP), 184 m_mutex (Mutex::eMutexTypeRecursive), 185 m_dos_header (), 186 m_coff_header (), 187 m_coff_header_opt (), 188 m_sect_headers () 189 { 190 ::memset (&m_dos_header, 0, sizeof(m_dos_header)); 191 ::memset (&m_coff_header, 0, sizeof(m_coff_header)); 192 ::memset (&m_coff_header_opt, 0, sizeof(m_coff_header_opt)); 193 } 194 195 196 ObjectFilePECOFF::~ObjectFilePECOFF() 197 { 198 } 199 200 201 bool 202 ObjectFilePECOFF::ParseHeader () 203 { 204 Mutex::Locker locker(m_mutex); 205 m_sect_headers.clear(); 206 m_data.SetByteOrder (eByteOrderLittle); 207 uint32_t offset = 0; 208 209 if (ParseDOSHeader()) 210 { 211 offset = m_dos_header.e_lfanew; 212 uint32_t pe_signature = m_data.GetU32 (&offset); 213 if (pe_signature != IMAGE_NT_SIGNATURE) 214 return false; 215 if (ParseCOFFHeader(&offset)) 216 { 217 if (m_coff_header.hdrsize > 0) 218 ParseCOFFOptionalHeader(&offset); 219 ParseSectionHeaders (offset); 220 } 221 StreamFile s(stdout, false);// REMOVE THIS LINE!!! 222 Dump(&s);// REMOVE THIS LINE!!! 223 return true; 224 } 225 return false; 226 } 227 228 229 ByteOrder 230 ObjectFilePECOFF::GetByteOrder () const 231 { 232 return eByteOrderLittle; 233 } 234 235 bool 236 ObjectFilePECOFF::IsExecutable() const 237 { 238 return (m_coff_header.flags & IMAGE_FILE_DLL) == 0; 239 } 240 241 size_t 242 ObjectFilePECOFF::GetAddressByteSize () const 243 { 244 if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32_PLUS) 245 return 8; 246 else if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32) 247 return 4; 248 return 4; 249 } 250 251 //---------------------------------------------------------------------- 252 // NeedsEndianSwap 253 // 254 // Return true if an endian swap needs to occur when extracting data 255 // from this file. 256 //---------------------------------------------------------------------- 257 bool 258 ObjectFilePECOFF::NeedsEndianSwap() const 259 { 260 #if defined(__LITTLE_ENDIAN__) 261 return false; 262 #else 263 return true; 264 #endif 265 } 266 //---------------------------------------------------------------------- 267 // ParseDOSHeader 268 //---------------------------------------------------------------------- 269 bool 270 ObjectFilePECOFF::ParseDOSHeader () 271 { 272 bool success = false; 273 uint32_t offset = 0; 274 success = m_data.ValidOffsetForDataOfSize(0, sizeof(m_dos_header)); 275 276 if (success) 277 { 278 m_dos_header.e_magic = m_data.GetU16(&offset); // Magic number 279 success = m_dos_header.e_magic == IMAGE_DOS_SIGNATURE; 280 281 if (success) 282 { 283 m_dos_header.e_cblp = m_data.GetU16(&offset); // Bytes on last page of file 284 m_dos_header.e_cp = m_data.GetU16(&offset); // Pages in file 285 m_dos_header.e_crlc = m_data.GetU16(&offset); // Relocations 286 m_dos_header.e_cparhdr = m_data.GetU16(&offset); // Size of header in paragraphs 287 m_dos_header.e_minalloc = m_data.GetU16(&offset); // Minimum extra paragraphs needed 288 m_dos_header.e_maxalloc = m_data.GetU16(&offset); // Maximum extra paragraphs needed 289 m_dos_header.e_ss = m_data.GetU16(&offset); // Initial (relative) SS value 290 m_dos_header.e_sp = m_data.GetU16(&offset); // Initial SP value 291 m_dos_header.e_csum = m_data.GetU16(&offset); // Checksum 292 m_dos_header.e_ip = m_data.GetU16(&offset); // Initial IP value 293 m_dos_header.e_cs = m_data.GetU16(&offset); // Initial (relative) CS value 294 m_dos_header.e_lfarlc = m_data.GetU16(&offset); // File address of relocation table 295 m_dos_header.e_ovno = m_data.GetU16(&offset); // Overlay number 296 297 m_dos_header.e_res[0] = m_data.GetU16(&offset); // Reserved words 298 m_dos_header.e_res[1] = m_data.GetU16(&offset); // Reserved words 299 m_dos_header.e_res[2] = m_data.GetU16(&offset); // Reserved words 300 m_dos_header.e_res[3] = m_data.GetU16(&offset); // Reserved words 301 302 m_dos_header.e_oemid = m_data.GetU16(&offset); // OEM identifier (for e_oeminfo) 303 m_dos_header.e_oeminfo = m_data.GetU16(&offset); // OEM information; e_oemid specific 304 m_dos_header.e_res2[0] = m_data.GetU16(&offset); // Reserved words 305 m_dos_header.e_res2[1] = m_data.GetU16(&offset); // Reserved words 306 m_dos_header.e_res2[2] = m_data.GetU16(&offset); // Reserved words 307 m_dos_header.e_res2[3] = m_data.GetU16(&offset); // Reserved words 308 m_dos_header.e_res2[4] = m_data.GetU16(&offset); // Reserved words 309 m_dos_header.e_res2[5] = m_data.GetU16(&offset); // Reserved words 310 m_dos_header.e_res2[6] = m_data.GetU16(&offset); // Reserved words 311 m_dos_header.e_res2[7] = m_data.GetU16(&offset); // Reserved words 312 m_dos_header.e_res2[8] = m_data.GetU16(&offset); // Reserved words 313 m_dos_header.e_res2[9] = m_data.GetU16(&offset); // Reserved words 314 315 m_dos_header.e_lfanew = m_data.GetU32(&offset); // File address of new exe header 316 } 317 } 318 if (!success) 319 memset(&m_dos_header, 0, sizeof(m_dos_header)); 320 return success; 321 } 322 323 324 //---------------------------------------------------------------------- 325 // ParserCOFFHeader 326 //---------------------------------------------------------------------- 327 bool 328 ObjectFilePECOFF::ParseCOFFHeader(uint32_t* offset_ptr) 329 { 330 bool success = m_data.ValidOffsetForDataOfSize (*offset_ptr, sizeof(m_coff_header)); 331 if (success) 332 { 333 m_coff_header.machine = m_data.GetU16(offset_ptr); 334 m_coff_header.nsects = m_data.GetU16(offset_ptr); 335 m_coff_header.modtime = m_data.GetU32(offset_ptr); 336 m_coff_header.symoff = m_data.GetU32(offset_ptr); 337 m_coff_header.nsyms = m_data.GetU32(offset_ptr); 338 m_coff_header.hdrsize = m_data.GetU16(offset_ptr); 339 m_coff_header.flags = m_data.GetU16(offset_ptr); 340 } 341 if (!success) 342 memset(&m_coff_header, 0, sizeof(m_coff_header)); 343 return success; 344 } 345 346 bool 347 ObjectFilePECOFF::ParseCOFFOptionalHeader(uint32_t* offset_ptr) 348 { 349 bool success = false; 350 const uint32_t end_offset = *offset_ptr + m_coff_header.hdrsize; 351 if (*offset_ptr < end_offset) 352 { 353 success = true; 354 m_coff_header_opt.magic = m_data.GetU16(offset_ptr); 355 m_coff_header_opt.major_linker_version = m_data.GetU8 (offset_ptr); 356 m_coff_header_opt.minor_linker_version = m_data.GetU8 (offset_ptr); 357 m_coff_header_opt.code_size = m_data.GetU32(offset_ptr); 358 m_coff_header_opt.data_size = m_data.GetU32(offset_ptr); 359 m_coff_header_opt.bss_size = m_data.GetU32(offset_ptr); 360 m_coff_header_opt.entry = m_data.GetU32(offset_ptr); 361 m_coff_header_opt.code_offset = m_data.GetU32(offset_ptr); 362 363 const uint32_t addr_byte_size = GetAddressByteSize (); 364 365 if (*offset_ptr < end_offset) 366 { 367 if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32) 368 { 369 // PE32 only 370 m_coff_header_opt.data_offset = m_data.GetU32(offset_ptr); 371 } 372 else 373 m_coff_header_opt.data_offset = 0; 374 375 if (*offset_ptr < end_offset) 376 { 377 m_coff_header_opt.image_base = m_data.GetMaxU64 (offset_ptr, addr_byte_size); 378 m_coff_header_opt.sect_alignment = m_data.GetU32(offset_ptr); 379 m_coff_header_opt.file_alignment = m_data.GetU32(offset_ptr); 380 m_coff_header_opt.major_os_system_version = m_data.GetU16(offset_ptr); 381 m_coff_header_opt.minor_os_system_version = m_data.GetU16(offset_ptr); 382 m_coff_header_opt.major_image_version = m_data.GetU16(offset_ptr); 383 m_coff_header_opt.minor_image_version = m_data.GetU16(offset_ptr); 384 m_coff_header_opt.major_subsystem_version = m_data.GetU16(offset_ptr); 385 m_coff_header_opt.minor_subsystem_version = m_data.GetU16(offset_ptr); 386 m_coff_header_opt.reserved1 = m_data.GetU32(offset_ptr); 387 m_coff_header_opt.image_size = m_data.GetU32(offset_ptr); 388 m_coff_header_opt.header_size = m_data.GetU32(offset_ptr); 389 m_coff_header_opt.checksum = m_data.GetU32(offset_ptr); 390 m_coff_header_opt.subsystem = m_data.GetU16(offset_ptr); 391 m_coff_header_opt.dll_flags = m_data.GetU16(offset_ptr); 392 m_coff_header_opt.stack_reserve_size = m_data.GetMaxU64 (offset_ptr, addr_byte_size); 393 m_coff_header_opt.stack_commit_size = m_data.GetMaxU64 (offset_ptr, addr_byte_size); 394 m_coff_header_opt.heap_reserve_size = m_data.GetMaxU64 (offset_ptr, addr_byte_size); 395 m_coff_header_opt.heap_commit_size = m_data.GetMaxU64 (offset_ptr, addr_byte_size); 396 m_coff_header_opt.loader_flags = m_data.GetU32(offset_ptr); 397 uint32_t num_data_dir_entries = m_data.GetU32(offset_ptr); 398 m_coff_header_opt.data_dirs.clear(); 399 m_coff_header_opt.data_dirs.resize(num_data_dir_entries); 400 uint32_t i; 401 for (i=0; i<num_data_dir_entries; i++) 402 { 403 m_coff_header_opt.data_dirs[i].vmaddr = m_data.GetU32(offset_ptr); 404 m_coff_header_opt.data_dirs[i].vmsize = m_data.GetU32(offset_ptr); 405 } 406 } 407 } 408 } 409 // Make sure we are on track for section data which follows 410 *offset_ptr = end_offset; 411 return success; 412 } 413 414 415 //---------------------------------------------------------------------- 416 // ParseSectionHeaders 417 //---------------------------------------------------------------------- 418 bool 419 ObjectFilePECOFF::ParseSectionHeaders (uint32_t section_header_data_offset) 420 { 421 const uint32_t nsects = m_coff_header.nsects; 422 m_sect_headers.clear(); 423 424 if (nsects > 0) 425 { 426 const uint32_t addr_byte_size = GetAddressByteSize (); 427 const size_t section_header_byte_size = nsects * sizeof(section_header_t); 428 DataBufferSP section_header_data_sp(m_file.ReadFileContents (section_header_data_offset, section_header_byte_size)); 429 DataExtractor section_header_data (section_header_data_sp, GetByteOrder(), addr_byte_size); 430 431 uint32_t offset = 0; 432 if (section_header_data.ValidOffsetForDataOfSize (offset, section_header_byte_size)) 433 { 434 m_sect_headers.resize(nsects); 435 436 for (uint32_t idx = 0; idx<nsects; ++idx) 437 { 438 const void *name_data = section_header_data.GetData(&offset, 8); 439 if (name_data) 440 { 441 memcpy(m_sect_headers[idx].name, name_data, 8); 442 m_sect_headers[idx].vmsize = section_header_data.GetU32(&offset); 443 m_sect_headers[idx].vmaddr = section_header_data.GetU32(&offset); 444 m_sect_headers[idx].size = section_header_data.GetU32(&offset); 445 m_sect_headers[idx].offset = section_header_data.GetU32(&offset); 446 m_sect_headers[idx].reloff = section_header_data.GetU32(&offset); 447 m_sect_headers[idx].lineoff = section_header_data.GetU32(&offset); 448 m_sect_headers[idx].nreloc = section_header_data.GetU16(&offset); 449 m_sect_headers[idx].nline = section_header_data.GetU16(&offset); 450 m_sect_headers[idx].flags = section_header_data.GetU32(&offset); 451 } 452 } 453 } 454 } 455 456 return m_sect_headers.empty() == false; 457 } 458 459 bool 460 ObjectFilePECOFF::GetSectionName(std::string& sect_name, const section_header_t& sect) 461 { 462 if (sect.name[0] == '/') 463 { 464 uint32_t stroff = strtoul(§.name[1], NULL, 10); 465 uint32_t string_file_offset = m_coff_header.symoff + (m_coff_header.nsyms * 18) + stroff; 466 const char *name = m_data.GetCStr (&string_file_offset); 467 if (name) 468 { 469 sect_name = name; 470 return true; 471 } 472 473 return false; 474 } 475 sect_name = sect.name; 476 return true; 477 } 478 479 //---------------------------------------------------------------------- 480 // GetNListSymtab 481 //---------------------------------------------------------------------- 482 Symtab * 483 ObjectFilePECOFF::GetSymtab() 484 { 485 Mutex::Locker symfile_locker(m_mutex); 486 if (m_symtab_ap.get() == NULL) 487 { 488 SectionList *sect_list = GetSectionList(); 489 m_symtab_ap.reset(new Symtab(this)); 490 Mutex::Locker symtab_locker (m_symtab_ap->GetMutex()); 491 492 const uint32_t num_syms = m_coff_header.nsyms; 493 494 if (num_syms > 0 && m_coff_header.symoff > 0) 495 { 496 const uint32_t symbol_size = sizeof(section_header_t); 497 const uint32_t addr_byte_size = GetAddressByteSize (); 498 const size_t symbol_data_size = num_syms * symbol_size; 499 // Include the 4 bytes string table size at the end of the symbols 500 DataBufferSP symtab_data_sp(m_file.ReadFileContents (m_coff_header.symoff, symbol_data_size + 4)); 501 DataExtractor symtab_data (symtab_data_sp, GetByteOrder(), addr_byte_size); 502 uint32_t offset = symbol_data_size; 503 const uint32_t strtab_size = symtab_data.GetU32 (&offset); 504 DataBufferSP strtab_data_sp(m_file.ReadFileContents (m_coff_header.symoff + symbol_data_size + 4, strtab_size)); 505 DataExtractor strtab_data (strtab_data_sp, GetByteOrder(), addr_byte_size); 506 507 offset = 0; 508 std::string symbol_name; 509 Symbol *symbols = m_symtab_ap->Resize (num_syms); 510 for (uint32_t i=0; i<num_syms; ++i) 511 { 512 coff_symbol_t symbol; 513 const uint32_t symbol_offset = offset; 514 const char *symbol_name_cstr = NULL; 515 // If the first 4 bytes of the symbol string are zero, then we 516 // it is followed by a 4 byte string table offset. Else these 517 // 8 bytes contain the symbol name 518 if (symtab_data.GetU32 (&offset) == 0) 519 { 520 // Long string that doesn't fit into the symbol table name, 521 // so now we must read the 4 byte string table offset 522 uint32_t strtab_offset = symtab_data.GetU32 (&offset); 523 symbol_name_cstr = strtab_data.PeekCStr (strtab_offset); 524 symbol_name.assign (symbol_name_cstr); 525 } 526 else 527 { 528 // Short string that fits into the symbol table name which is 8 bytes 529 offset += sizeof(symbol.name) - 4; // Skip remaining 530 symbol_name_cstr = symtab_data.PeekCStr (symbol_offset); 531 if (symbol_name_cstr == NULL) 532 break; 533 symbol_name.assign (symbol_name_cstr, sizeof(symbol.name)); 534 } 535 symbol.value = symtab_data.GetU32 (&offset); 536 symbol.sect = symtab_data.GetU16 (&offset); 537 symbol.type = symtab_data.GetU16 (&offset); 538 symbol.storage = symtab_data.GetU8 (&offset); 539 symbol.naux = symtab_data.GetU8 (&offset); 540 Address symbol_addr(sect_list->GetSectionAtIndex(symbol.sect-1).get(), symbol.value); 541 symbols[i].GetMangled ().SetValue (symbol_name.c_str(), symbol_name[0]=='_' && symbol_name[1] == 'Z'); 542 symbols[i].SetValue(symbol_addr); 543 544 if (symbol.naux > 0) 545 i += symbol.naux; 546 } 547 548 } 549 } 550 return m_symtab_ap.get(); 551 552 } 553 554 SectionList * 555 ObjectFilePECOFF::GetSectionList() 556 { 557 Mutex::Locker symfile_locker(m_mutex); 558 if (m_sections_ap.get() == NULL) 559 { 560 m_sections_ap.reset(new SectionList()); 561 const uint32_t nsects = m_sect_headers.size(); 562 Module *module = GetModule(); 563 for (uint32_t idx = 0; idx<nsects; ++idx) 564 { 565 std::string sect_name; 566 GetSectionName (sect_name, m_sect_headers[idx]); 567 ConstString const_sect_name (sect_name.c_str()); 568 static ConstString g_code_sect_name (".code"); 569 static ConstString g_CODE_sect_name ("CODE"); 570 static ConstString g_data_sect_name (".data"); 571 static ConstString g_DATA_sect_name ("DATA"); 572 static ConstString g_bss_sect_name (".bss"); 573 static ConstString g_BSS_sect_name ("BSS"); 574 static ConstString g_debug_sect_name (".debug"); 575 static ConstString g_reloc_sect_name (".reloc"); 576 static ConstString g_stab_sect_name (".stab"); 577 static ConstString g_stabstr_sect_name (".stabstr"); 578 SectionType section_type = eSectionTypeOther; 579 if (m_sect_headers[idx].flags & IMAGE_SCN_CNT_CODE && 580 ((const_sect_name == g_code_sect_name) || (const_sect_name == g_CODE_sect_name))) 581 { 582 section_type = eSectionTypeCode; 583 } 584 else if (m_sect_headers[idx].flags & IMAGE_SCN_CNT_INITIALIZED_DATA && 585 ((const_sect_name == g_data_sect_name) || (const_sect_name == g_DATA_sect_name))) 586 { 587 section_type = eSectionTypeData; 588 } 589 else if (m_sect_headers[idx].flags & IMAGE_SCN_CNT_UNINITIALIZED_DATA && 590 ((const_sect_name == g_bss_sect_name) || (const_sect_name == g_BSS_sect_name))) 591 { 592 if (m_sect_headers[idx].size == 0) 593 section_type = eSectionTypeZeroFill; 594 else 595 section_type = eSectionTypeData; 596 } 597 else if (const_sect_name == g_debug_sect_name) 598 { 599 section_type = eSectionTypeDebug; 600 } 601 else if (const_sect_name == g_stabstr_sect_name) 602 { 603 section_type = eSectionTypeDataCString; 604 } 605 else if (const_sect_name == g_reloc_sect_name) 606 { 607 section_type = eSectionTypeOther; 608 } 609 else if (m_sect_headers[idx].flags & IMAGE_SCN_CNT_CODE) 610 { 611 section_type = eSectionTypeCode; 612 } 613 else if (m_sect_headers[idx].flags & IMAGE_SCN_CNT_INITIALIZED_DATA) 614 { 615 section_type = eSectionTypeData; 616 } 617 else if (m_sect_headers[idx].flags & IMAGE_SCN_CNT_UNINITIALIZED_DATA) 618 { 619 if (m_sect_headers[idx].size == 0) 620 section_type = eSectionTypeZeroFill; 621 else 622 section_type = eSectionTypeData; 623 } 624 625 // Use a segment ID of the segment index shifted left by 8 so they 626 // never conflict with any of the sections. 627 SectionSP section_sp (new Section (NULL, 628 module, // Module to which this section belongs 629 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 630 const_sect_name, // Name of this section 631 section_type, // This section is a container of other sections. 632 m_sect_headers[idx].vmaddr, // File VM address == addresses as they are found in the object file 633 m_sect_headers[idx].vmsize, // VM size in bytes of this section 634 m_sect_headers[idx].offset, // Offset to the data for this section in the file 635 m_sect_headers[idx].size, // Size in bytes of this section as found in the the file 636 m_sect_headers[idx].flags)); // Flags for this section 637 638 //section_sp->SetIsEncrypted (segment_is_encrypted); 639 640 m_sections_ap->AddSection(section_sp); 641 } 642 } 643 return m_sections_ap.get(); 644 } 645 646 bool 647 ObjectFilePECOFF::GetUUID (UUID* uuid) 648 { 649 return false; 650 } 651 652 uint32_t 653 ObjectFilePECOFF::GetDependentModules (FileSpecList& files) 654 { 655 return 0; 656 } 657 658 659 //---------------------------------------------------------------------- 660 // Dump 661 // 662 // Dump the specifics of the runtime file container (such as any headers 663 // segments, sections, etc). 664 //---------------------------------------------------------------------- 665 void 666 ObjectFilePECOFF::Dump(Stream *s) 667 { 668 Mutex::Locker locker(m_mutex); 669 s->Printf("%p: ", this); 670 s->Indent(); 671 s->PutCString("ObjectFilePECOFF"); 672 673 ArchSpec header_arch; 674 GetArchitecture (header_arch); 675 676 *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n"; 677 678 if (m_sections_ap.get()) 679 m_sections_ap->Dump(s, NULL, true, UINT32_MAX); 680 681 if (m_symtab_ap.get()) 682 m_symtab_ap->Dump(s, NULL, eSortOrderNone); 683 684 if (m_dos_header.e_magic) 685 DumpDOSHeader (s, m_dos_header); 686 if (m_coff_header.machine) 687 { 688 DumpCOFFHeader (s, m_coff_header); 689 if (m_coff_header.hdrsize) 690 DumpOptCOFFHeader (s, m_coff_header_opt); 691 } 692 s->EOL(); 693 DumpSectionHeaders(s); 694 s->EOL(); 695 } 696 697 //---------------------------------------------------------------------- 698 // DumpDOSHeader 699 // 700 // Dump the MS-DOS header to the specified output stream 701 //---------------------------------------------------------------------- 702 void 703 ObjectFilePECOFF::DumpDOSHeader(Stream *s, const dos_header_t& header) 704 { 705 s->PutCString ("MSDOS Header\n"); 706 s->Printf (" e_magic = 0x%4.4x\n", header.e_magic); 707 s->Printf (" e_cblp = 0x%4.4x\n", header.e_cblp); 708 s->Printf (" e_cp = 0x%4.4x\n", header.e_cp); 709 s->Printf (" e_crlc = 0x%4.4x\n", header.e_crlc); 710 s->Printf (" e_cparhdr = 0x%4.4x\n", header.e_cparhdr); 711 s->Printf (" e_minalloc = 0x%4.4x\n", header.e_minalloc); 712 s->Printf (" e_maxalloc = 0x%4.4x\n", header.e_maxalloc); 713 s->Printf (" e_ss = 0x%4.4x\n", header.e_ss); 714 s->Printf (" e_sp = 0x%4.4x\n", header.e_sp); 715 s->Printf (" e_csum = 0x%4.4x\n", header.e_csum); 716 s->Printf (" e_ip = 0x%4.4x\n", header.e_ip); 717 s->Printf (" e_cs = 0x%4.4x\n", header.e_cs); 718 s->Printf (" e_lfarlc = 0x%4.4x\n", header.e_lfarlc); 719 s->Printf (" e_ovno = 0x%4.4x\n", header.e_ovno); 720 s->Printf (" e_res[4] = { 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x }\n", 721 header.e_res[0], 722 header.e_res[1], 723 header.e_res[2], 724 header.e_res[3]); 725 s->Printf (" e_oemid = 0x%4.4x\n", header.e_oemid); 726 s->Printf (" e_oeminfo = 0x%4.4x\n", header.e_oeminfo); 727 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", 728 header.e_res2[0], 729 header.e_res2[1], 730 header.e_res2[2], 731 header.e_res2[3], 732 header.e_res2[4], 733 header.e_res2[5], 734 header.e_res2[6], 735 header.e_res2[7], 736 header.e_res2[8], 737 header.e_res2[9]); 738 s->Printf (" e_lfanew = 0x%8.8x\n", header.e_lfanew); 739 } 740 741 //---------------------------------------------------------------------- 742 // DumpCOFFHeader 743 // 744 // Dump the COFF header to the specified output stream 745 //---------------------------------------------------------------------- 746 void 747 ObjectFilePECOFF::DumpCOFFHeader(Stream *s, const coff_header_t& header) 748 { 749 s->PutCString ("COFF Header\n"); 750 s->Printf (" machine = 0x%4.4x\n", header.machine); 751 s->Printf (" nsects = 0x%4.4x\n", header.nsects); 752 s->Printf (" modtime = 0x%8.8x\n", header.modtime); 753 s->Printf (" symoff = 0x%8.8x\n", header.symoff); 754 s->Printf (" nsyms = 0x%8.8x\n", header.nsyms); 755 s->Printf (" hdrsize = 0x%4.4x\n", header.hdrsize); 756 } 757 758 //---------------------------------------------------------------------- 759 // DumpOptCOFFHeader 760 // 761 // Dump the optional COFF header to the specified output stream 762 //---------------------------------------------------------------------- 763 void 764 ObjectFilePECOFF::DumpOptCOFFHeader(Stream *s, const coff_opt_header_t& header) 765 { 766 s->PutCString ("Optional COFF Header\n"); 767 s->Printf (" magic = 0x%4.4x\n", header.magic); 768 s->Printf (" major_linker_version = 0x%2.2x\n", header.major_linker_version); 769 s->Printf (" minor_linker_version = 0x%2.2x\n", header.minor_linker_version); 770 s->Printf (" code_size = 0x%8.8x\n", header.code_size); 771 s->Printf (" data_size = 0x%8.8x\n", header.data_size); 772 s->Printf (" bss_size = 0x%8.8x\n", header.bss_size); 773 s->Printf (" entry = 0x%8.8x\n", header.entry); 774 s->Printf (" code_offset = 0x%8.8x\n", header.code_offset); 775 s->Printf (" data_offset = 0x%8.8x\n", header.data_offset); 776 s->Printf (" image_base = 0x%16.16llx\n", header.image_base); 777 s->Printf (" sect_alignment = 0x%8.8x\n", header.sect_alignment); 778 s->Printf (" file_alignment = 0x%8.8x\n", header.file_alignment); 779 s->Printf (" major_os_system_version = 0x%4.4x\n", header.major_os_system_version); 780 s->Printf (" minor_os_system_version = 0x%4.4x\n", header.minor_os_system_version); 781 s->Printf (" major_image_version = 0x%4.4x\n", header.major_image_version); 782 s->Printf (" minor_image_version = 0x%4.4x\n", header.minor_image_version); 783 s->Printf (" major_subsystem_version = 0x%4.4x\n", header.major_subsystem_version); 784 s->Printf (" minor_subsystem_version = 0x%4.4x\n", header.minor_subsystem_version); 785 s->Printf (" reserved1 = 0x%8.8x\n", header.reserved1); 786 s->Printf (" image_size = 0x%8.8x\n", header.image_size); 787 s->Printf (" header_size = 0x%8.8x\n", header.header_size); 788 s->Printf (" checksum = 0x%8.8x\n", header.checksum); 789 s->Printf (" subsystem = 0x%4.4x\n", header.subsystem); 790 s->Printf (" dll_flags = 0x%4.4x\n", header.dll_flags); 791 s->Printf (" stack_reserve_size = 0x%16.16llx\n", header.stack_reserve_size); 792 s->Printf (" stack_commit_size = 0x%16.16llx\n", header.stack_commit_size); 793 s->Printf (" heap_reserve_size = 0x%16.16llx\n", header.heap_reserve_size); 794 s->Printf (" heap_commit_size = 0x%16.16llx\n", header.heap_commit_size); 795 s->Printf (" loader_flags = 0x%8.8x\n", header.loader_flags); 796 s->Printf (" num_data_dir_entries = 0x%8.8zx\n", header.data_dirs.size()); 797 uint32_t i; 798 for (i=0; i<header.data_dirs.size(); i++) 799 { 800 s->Printf (" data_dirs[%2u] vmaddr = 0x%8.8x, vmsize = 0x%8.8x\n", 801 i, 802 header.data_dirs[i].vmaddr, 803 header.data_dirs[i].vmsize); 804 } 805 } 806 //---------------------------------------------------------------------- 807 // DumpSectionHeader 808 // 809 // Dump a single ELF section header to the specified output stream 810 //---------------------------------------------------------------------- 811 void 812 ObjectFilePECOFF::DumpSectionHeader(Stream *s, const section_header_t& sh) 813 { 814 std::string name; 815 GetSectionName(name, sh); 816 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", 817 name.c_str(), 818 sh.vmaddr, 819 sh.vmsize, 820 sh.offset, 821 sh.size, 822 sh.reloff, 823 sh.lineoff, 824 sh.nreloc, 825 sh.nline, 826 sh.flags); 827 } 828 829 830 //---------------------------------------------------------------------- 831 // DumpSectionHeaders 832 // 833 // Dump all of the ELF section header to the specified output stream 834 //---------------------------------------------------------------------- 835 void 836 ObjectFilePECOFF::DumpSectionHeaders(Stream *s) 837 { 838 839 s->PutCString ("Section Headers\n"); 840 s->PutCString ("IDX name vm addr vm size file off file size reloc off line off nreloc nline flags\n"); 841 s->PutCString ("==== ---------------- ---------- ---------- ---------- ---------- ---------- ---------- ------ ------ ----------\n"); 842 843 uint32_t idx = 0; 844 SectionHeaderCollIter pos, end = m_sect_headers.end(); 845 846 for (pos = m_sect_headers.begin(); pos != end; ++pos, ++idx) 847 { 848 s->Printf ("[%2u] ", idx); 849 ObjectFilePECOFF::DumpSectionHeader(s, *pos); 850 } 851 } 852 853 static bool 854 COFFMachineToMachCPU (uint16_t machine, ArchSpec &arch) 855 { 856 switch (machine) 857 { 858 case IMAGE_FILE_MACHINE_AMD64: 859 case IMAGE_FILE_MACHINE_IA64: 860 arch.SetArchitecture (eArchTypeMachO, 861 llvm::MachO::CPUTypeX86_64, 862 llvm::MachO::CPUSubType_X86_64_ALL); 863 return true; 864 865 case IMAGE_FILE_MACHINE_I386: 866 arch.SetArchitecture (eArchTypeMachO, 867 llvm::MachO::CPUTypeI386, 868 llvm::MachO::CPUSubType_I386_ALL); 869 return true; 870 871 case IMAGE_FILE_MACHINE_POWERPC: 872 case IMAGE_FILE_MACHINE_POWERPCFP: 873 arch.SetArchitecture (eArchTypeMachO, 874 llvm::MachO::CPUTypePowerPC, 875 llvm::MachO::CPUSubType_POWERPC_ALL); 876 return true; 877 case IMAGE_FILE_MACHINE_ARM: 878 case IMAGE_FILE_MACHINE_THUMB: 879 arch.SetArchitecture (eArchTypeMachO, 880 llvm::MachO::CPUTypeARM, 881 llvm::MachO::CPUSubType_ARM_V7); 882 return true; 883 } 884 return false; 885 } 886 bool 887 ObjectFilePECOFF::GetArchitecture (ArchSpec &arch) 888 { 889 // For index zero return our cpu type 890 return COFFMachineToMachCPU (m_coff_header.machine, arch); 891 } 892 893 ObjectFile::Type 894 ObjectFilePECOFF::CalculateType() 895 { 896 if (m_coff_header.machine != 0) 897 { 898 if ((m_coff_header.flags & IMAGE_FILE_DLL) == 0) 899 return eTypeExecutable; 900 else 901 return eTypeSharedLibrary; 902 } 903 return eTypeExecutable; 904 } 905 906 ObjectFile::Strata 907 ObjectFilePECOFF::CalculateStrata() 908 { 909 return eStrataUser; 910 } 911 //------------------------------------------------------------------ 912 // PluginInterface protocol 913 //------------------------------------------------------------------ 914 const char * 915 ObjectFilePECOFF::GetPluginName() 916 { 917 return "ObjectFilePECOFF"; 918 } 919 920 const char * 921 ObjectFilePECOFF::GetShortPluginName() 922 { 923 return GetPluginNameStatic(); 924 } 925 926 uint32_t 927 ObjectFilePECOFF::GetPluginVersion() 928 { 929 return 1; 930 } 931 932