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