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 #include "WindowsMiniDump.h" 12 13 #include "lldb/Core/FileSpecList.h" 14 #include "lldb/Core/Module.h" 15 #include "lldb/Core/ModuleSpec.h" 16 #include "lldb/Core/PluginManager.h" 17 #include "lldb/Core/Section.h" 18 #include "lldb/Core/StreamFile.h" 19 #include "lldb/Symbol/ObjectFile.h" 20 #include "lldb/Target/Process.h" 21 #include "lldb/Target/SectionLoadList.h" 22 #include "lldb/Target/Target.h" 23 #include "lldb/Utility/ArchSpec.h" 24 #include "lldb/Utility/DataBufferHeap.h" 25 #include "lldb/Utility/FileSpec.h" 26 #include "lldb/Utility/Log.h" 27 #include "lldb/Utility/StreamString.h" 28 #include "lldb/Utility/Timer.h" 29 #include "lldb/Utility/UUID.h" 30 #include "llvm/BinaryFormat/COFF.h" 31 32 #include "llvm/Object/COFFImportFile.h" 33 #include "llvm/Support/Error.h" 34 #include "llvm/Support/MemoryBuffer.h" 35 36 #define IMAGE_DOS_SIGNATURE 0x5A4D // MZ 37 #define IMAGE_NT_SIGNATURE 0x00004550 // PE00 38 #define OPT_HEADER_MAGIC_PE32 0x010b 39 #define OPT_HEADER_MAGIC_PE32_PLUS 0x020b 40 41 using namespace lldb; 42 using namespace lldb_private; 43 44 void ObjectFilePECOFF::Initialize() { 45 PluginManager::RegisterPlugin( 46 GetPluginNameStatic(), GetPluginDescriptionStatic(), CreateInstance, 47 CreateMemoryInstance, GetModuleSpecifications, SaveCore); 48 } 49 50 void ObjectFilePECOFF::Terminate() { 51 PluginManager::UnregisterPlugin(CreateInstance); 52 } 53 54 lldb_private::ConstString ObjectFilePECOFF::GetPluginNameStatic() { 55 static ConstString g_name("pe-coff"); 56 return g_name; 57 } 58 59 const char *ObjectFilePECOFF::GetPluginDescriptionStatic() { 60 return "Portable Executable and Common Object File Format object file reader " 61 "(32 and 64 bit)"; 62 } 63 64 ObjectFile *ObjectFilePECOFF::CreateInstance(const lldb::ModuleSP &module_sp, 65 DataBufferSP &data_sp, 66 lldb::offset_t data_offset, 67 const lldb_private::FileSpec *file, 68 lldb::offset_t file_offset, 69 lldb::offset_t length) { 70 if (!data_sp) { 71 data_sp = MapFileData(file, length, file_offset); 72 if (!data_sp) 73 return nullptr; 74 data_offset = 0; 75 } 76 77 if (!ObjectFilePECOFF::MagicBytesMatch(data_sp)) 78 return nullptr; 79 80 // Update the data to contain the entire file if it doesn't already 81 if (data_sp->GetByteSize() < length) { 82 data_sp = MapFileData(file, length, file_offset); 83 if (!data_sp) 84 return nullptr; 85 } 86 87 auto objfile_ap = llvm::make_unique<ObjectFilePECOFF>( 88 module_sp, data_sp, data_offset, file, file_offset, length); 89 if (!objfile_ap || !objfile_ap->ParseHeader()) 90 return nullptr; 91 92 // Cache coff binary. 93 if (!objfile_ap->CreateBinary()) 94 return nullptr; 95 96 return objfile_ap.release(); 97 } 98 99 ObjectFile *ObjectFilePECOFF::CreateMemoryInstance( 100 const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp, 101 const lldb::ProcessSP &process_sp, lldb::addr_t header_addr) { 102 if (!data_sp || !ObjectFilePECOFF::MagicBytesMatch(data_sp)) 103 return nullptr; 104 auto objfile_ap = llvm::make_unique<ObjectFilePECOFF>( 105 module_sp, data_sp, process_sp, header_addr); 106 if (objfile_ap.get() && objfile_ap->ParseHeader()) { 107 return objfile_ap.release(); 108 } 109 return nullptr; 110 } 111 112 size_t ObjectFilePECOFF::GetModuleSpecifications( 113 const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp, 114 lldb::offset_t data_offset, lldb::offset_t file_offset, 115 lldb::offset_t length, lldb_private::ModuleSpecList &specs) { 116 const size_t initial_count = specs.GetSize(); 117 118 if (ObjectFilePECOFF::MagicBytesMatch(data_sp)) { 119 DataExtractor data; 120 data.SetData(data_sp, data_offset, length); 121 data.SetByteOrder(eByteOrderLittle); 122 123 dos_header_t dos_header; 124 coff_header_t coff_header; 125 126 if (ParseDOSHeader(data, dos_header)) { 127 lldb::offset_t offset = dos_header.e_lfanew; 128 uint32_t pe_signature = data.GetU32(&offset); 129 if (pe_signature != IMAGE_NT_SIGNATURE) 130 return false; 131 if (ParseCOFFHeader(data, &offset, coff_header)) { 132 ArchSpec spec; 133 if (coff_header.machine == MachineAmd64) { 134 spec.SetTriple("x86_64-pc-windows"); 135 specs.Append(ModuleSpec(file, spec)); 136 } else if (coff_header.machine == MachineX86) { 137 spec.SetTriple("i386-pc-windows"); 138 specs.Append(ModuleSpec(file, spec)); 139 spec.SetTriple("i686-pc-windows"); 140 specs.Append(ModuleSpec(file, spec)); 141 } else if (coff_header.machine == MachineArmNt) { 142 spec.SetTriple("arm-pc-windows"); 143 specs.Append(ModuleSpec(file, spec)); 144 } 145 } 146 } 147 } 148 149 return specs.GetSize() - initial_count; 150 } 151 152 bool ObjectFilePECOFF::SaveCore(const lldb::ProcessSP &process_sp, 153 const lldb_private::FileSpec &outfile, 154 lldb_private::Status &error) { 155 return SaveMiniDump(process_sp, outfile, error); 156 } 157 158 bool ObjectFilePECOFF::MagicBytesMatch(DataBufferSP &data_sp) { 159 DataExtractor data(data_sp, eByteOrderLittle, 4); 160 lldb::offset_t offset = 0; 161 uint16_t magic = data.GetU16(&offset); 162 return magic == IMAGE_DOS_SIGNATURE; 163 } 164 165 lldb::SymbolType ObjectFilePECOFF::MapSymbolType(uint16_t coff_symbol_type) { 166 // TODO: We need to complete this mapping of COFF symbol types to LLDB ones. 167 // For now, here's a hack to make sure our function have types. 168 const auto complex_type = 169 coff_symbol_type >> llvm::COFF::SCT_COMPLEX_TYPE_SHIFT; 170 if (complex_type == llvm::COFF::IMAGE_SYM_DTYPE_FUNCTION) { 171 return lldb::eSymbolTypeCode; 172 } 173 return lldb::eSymbolTypeInvalid; 174 } 175 176 bool ObjectFilePECOFF::CreateBinary() { 177 if (m_owningbin) 178 return true; 179 180 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT)); 181 182 auto binary = llvm::object::createBinary(m_file.GetPath()); 183 if (!binary) { 184 if (log) 185 log->Printf("ObjectFilePECOFF::CreateBinary() - failed to create binary " 186 "for file (%s): %s", 187 m_file ? m_file.GetPath().c_str() : "<NULL>", 188 errorToErrorCode(binary.takeError()).message().c_str()); 189 return false; 190 } 191 192 // Make sure we only handle COFF format. 193 if (!binary->getBinary()->isCOFF() && 194 !binary->getBinary()->isCOFFImportFile()) 195 return false; 196 197 m_owningbin = OWNBINType(std::move(*binary)); 198 if (log) 199 log->Printf("%p ObjectFilePECOFF::CreateBinary() module = %p (%s), file = " 200 "%s, binary = %p (Bin = %p)", 201 static_cast<void *>(this), 202 static_cast<void *>(GetModule().get()), 203 GetModule()->GetSpecificationDescription().c_str(), 204 m_file ? m_file.GetPath().c_str() : "<NULL>", 205 static_cast<void *>(m_owningbin.getPointer()), 206 static_cast<void *>(m_owningbin->getBinary())); 207 return true; 208 } 209 210 ObjectFilePECOFF::ObjectFilePECOFF(const lldb::ModuleSP &module_sp, 211 DataBufferSP &data_sp, 212 lldb::offset_t data_offset, 213 const FileSpec *file, 214 lldb::offset_t file_offset, 215 lldb::offset_t length) 216 : ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset), 217 m_dos_header(), m_coff_header(), m_coff_header_opt(), m_sect_headers(), 218 m_entry_point_address(), m_deps_filespec(), m_owningbin() { 219 ::memset(&m_dos_header, 0, sizeof(m_dos_header)); 220 ::memset(&m_coff_header, 0, sizeof(m_coff_header)); 221 ::memset(&m_coff_header_opt, 0, sizeof(m_coff_header_opt)); 222 } 223 224 ObjectFilePECOFF::ObjectFilePECOFF(const lldb::ModuleSP &module_sp, 225 DataBufferSP &header_data_sp, 226 const lldb::ProcessSP &process_sp, 227 addr_t header_addr) 228 : ObjectFile(module_sp, process_sp, header_addr, header_data_sp), 229 m_dos_header(), m_coff_header(), m_coff_header_opt(), m_sect_headers(), 230 m_entry_point_address(), m_deps_filespec(), m_owningbin() { 231 ::memset(&m_dos_header, 0, sizeof(m_dos_header)); 232 ::memset(&m_coff_header, 0, sizeof(m_coff_header)); 233 ::memset(&m_coff_header_opt, 0, sizeof(m_coff_header_opt)); 234 } 235 236 ObjectFilePECOFF::~ObjectFilePECOFF() {} 237 238 bool ObjectFilePECOFF::ParseHeader() { 239 ModuleSP module_sp(GetModule()); 240 if (module_sp) { 241 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 242 m_sect_headers.clear(); 243 m_data.SetByteOrder(eByteOrderLittle); 244 lldb::offset_t offset = 0; 245 246 if (ParseDOSHeader(m_data, m_dos_header)) { 247 offset = m_dos_header.e_lfanew; 248 uint32_t pe_signature = m_data.GetU32(&offset); 249 if (pe_signature != IMAGE_NT_SIGNATURE) 250 return false; 251 if (ParseCOFFHeader(m_data, &offset, m_coff_header)) { 252 if (m_coff_header.hdrsize > 0) 253 ParseCOFFOptionalHeader(&offset); 254 ParseSectionHeaders(offset); 255 } 256 return true; 257 } 258 } 259 return false; 260 } 261 262 bool ObjectFilePECOFF::SetLoadAddress(Target &target, addr_t value, 263 bool value_is_offset) { 264 bool changed = false; 265 ModuleSP module_sp = GetModule(); 266 if (module_sp) { 267 size_t num_loaded_sections = 0; 268 SectionList *section_list = GetSectionList(); 269 if (section_list) { 270 if (!value_is_offset) { 271 value -= m_image_base; 272 } 273 274 const size_t num_sections = section_list->GetSize(); 275 size_t sect_idx = 0; 276 277 for (sect_idx = 0; sect_idx < num_sections; ++sect_idx) { 278 // Iterate through the object file sections to find all of the sections 279 // that have SHF_ALLOC in their flag bits. 280 SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx)); 281 if (section_sp && !section_sp->IsThreadSpecific()) { 282 if (target.GetSectionLoadList().SetSectionLoadAddress( 283 section_sp, section_sp->GetFileAddress() + value)) 284 ++num_loaded_sections; 285 } 286 } 287 changed = num_loaded_sections > 0; 288 } 289 } 290 return changed; 291 } 292 293 ByteOrder ObjectFilePECOFF::GetByteOrder() const { return eByteOrderLittle; } 294 295 bool ObjectFilePECOFF::IsExecutable() const { 296 return (m_coff_header.flags & llvm::COFF::IMAGE_FILE_DLL) == 0; 297 } 298 299 uint32_t ObjectFilePECOFF::GetAddressByteSize() const { 300 if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32_PLUS) 301 return 8; 302 else if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32) 303 return 4; 304 return 4; 305 } 306 307 //---------------------------------------------------------------------- 308 // NeedsEndianSwap 309 // 310 // Return true if an endian swap needs to occur when extracting data from this 311 // file. 312 //---------------------------------------------------------------------- 313 bool ObjectFilePECOFF::NeedsEndianSwap() const { 314 #if defined(__LITTLE_ENDIAN__) 315 return false; 316 #else 317 return true; 318 #endif 319 } 320 //---------------------------------------------------------------------- 321 // ParseDOSHeader 322 //---------------------------------------------------------------------- 323 bool ObjectFilePECOFF::ParseDOSHeader(DataExtractor &data, 324 dos_header_t &dos_header) { 325 bool success = false; 326 lldb::offset_t offset = 0; 327 success = data.ValidOffsetForDataOfSize(0, sizeof(dos_header)); 328 329 if (success) { 330 dos_header.e_magic = data.GetU16(&offset); // Magic number 331 success = dos_header.e_magic == IMAGE_DOS_SIGNATURE; 332 333 if (success) { 334 dos_header.e_cblp = data.GetU16(&offset); // Bytes on last page of file 335 dos_header.e_cp = data.GetU16(&offset); // Pages in file 336 dos_header.e_crlc = data.GetU16(&offset); // Relocations 337 dos_header.e_cparhdr = 338 data.GetU16(&offset); // Size of header in paragraphs 339 dos_header.e_minalloc = 340 data.GetU16(&offset); // Minimum extra paragraphs needed 341 dos_header.e_maxalloc = 342 data.GetU16(&offset); // Maximum extra paragraphs needed 343 dos_header.e_ss = data.GetU16(&offset); // Initial (relative) SS value 344 dos_header.e_sp = data.GetU16(&offset); // Initial SP value 345 dos_header.e_csum = data.GetU16(&offset); // Checksum 346 dos_header.e_ip = data.GetU16(&offset); // Initial IP value 347 dos_header.e_cs = data.GetU16(&offset); // Initial (relative) CS value 348 dos_header.e_lfarlc = 349 data.GetU16(&offset); // File address of relocation table 350 dos_header.e_ovno = data.GetU16(&offset); // Overlay number 351 352 dos_header.e_res[0] = data.GetU16(&offset); // Reserved words 353 dos_header.e_res[1] = data.GetU16(&offset); // Reserved words 354 dos_header.e_res[2] = data.GetU16(&offset); // Reserved words 355 dos_header.e_res[3] = data.GetU16(&offset); // Reserved words 356 357 dos_header.e_oemid = 358 data.GetU16(&offset); // OEM identifier (for e_oeminfo) 359 dos_header.e_oeminfo = 360 data.GetU16(&offset); // OEM information; e_oemid specific 361 dos_header.e_res2[0] = data.GetU16(&offset); // Reserved words 362 dos_header.e_res2[1] = data.GetU16(&offset); // Reserved words 363 dos_header.e_res2[2] = data.GetU16(&offset); // Reserved words 364 dos_header.e_res2[3] = data.GetU16(&offset); // Reserved words 365 dos_header.e_res2[4] = data.GetU16(&offset); // Reserved words 366 dos_header.e_res2[5] = data.GetU16(&offset); // Reserved words 367 dos_header.e_res2[6] = data.GetU16(&offset); // Reserved words 368 dos_header.e_res2[7] = data.GetU16(&offset); // Reserved words 369 dos_header.e_res2[8] = data.GetU16(&offset); // Reserved words 370 dos_header.e_res2[9] = data.GetU16(&offset); // Reserved words 371 372 dos_header.e_lfanew = 373 data.GetU32(&offset); // File address of new exe header 374 } 375 } 376 if (!success) 377 memset(&dos_header, 0, sizeof(dos_header)); 378 return success; 379 } 380 381 //---------------------------------------------------------------------- 382 // ParserCOFFHeader 383 //---------------------------------------------------------------------- 384 bool ObjectFilePECOFF::ParseCOFFHeader(DataExtractor &data, 385 lldb::offset_t *offset_ptr, 386 coff_header_t &coff_header) { 387 bool success = 388 data.ValidOffsetForDataOfSize(*offset_ptr, sizeof(coff_header)); 389 if (success) { 390 coff_header.machine = data.GetU16(offset_ptr); 391 coff_header.nsects = data.GetU16(offset_ptr); 392 coff_header.modtime = data.GetU32(offset_ptr); 393 coff_header.symoff = data.GetU32(offset_ptr); 394 coff_header.nsyms = data.GetU32(offset_ptr); 395 coff_header.hdrsize = data.GetU16(offset_ptr); 396 coff_header.flags = data.GetU16(offset_ptr); 397 } 398 if (!success) 399 memset(&coff_header, 0, sizeof(coff_header)); 400 return success; 401 } 402 403 bool ObjectFilePECOFF::ParseCOFFOptionalHeader(lldb::offset_t *offset_ptr) { 404 bool success = false; 405 const lldb::offset_t end_offset = *offset_ptr + m_coff_header.hdrsize; 406 if (*offset_ptr < end_offset) { 407 success = true; 408 m_coff_header_opt.magic = m_data.GetU16(offset_ptr); 409 m_coff_header_opt.major_linker_version = m_data.GetU8(offset_ptr); 410 m_coff_header_opt.minor_linker_version = m_data.GetU8(offset_ptr); 411 m_coff_header_opt.code_size = m_data.GetU32(offset_ptr); 412 m_coff_header_opt.data_size = m_data.GetU32(offset_ptr); 413 m_coff_header_opt.bss_size = m_data.GetU32(offset_ptr); 414 m_coff_header_opt.entry = m_data.GetU32(offset_ptr); 415 m_coff_header_opt.code_offset = m_data.GetU32(offset_ptr); 416 417 const uint32_t addr_byte_size = GetAddressByteSize(); 418 419 if (*offset_ptr < end_offset) { 420 if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32) { 421 // PE32 only 422 m_coff_header_opt.data_offset = m_data.GetU32(offset_ptr); 423 } else 424 m_coff_header_opt.data_offset = 0; 425 426 if (*offset_ptr < end_offset) { 427 m_coff_header_opt.image_base = 428 m_data.GetMaxU64(offset_ptr, addr_byte_size); 429 m_coff_header_opt.sect_alignment = m_data.GetU32(offset_ptr); 430 m_coff_header_opt.file_alignment = m_data.GetU32(offset_ptr); 431 m_coff_header_opt.major_os_system_version = m_data.GetU16(offset_ptr); 432 m_coff_header_opt.minor_os_system_version = m_data.GetU16(offset_ptr); 433 m_coff_header_opt.major_image_version = m_data.GetU16(offset_ptr); 434 m_coff_header_opt.minor_image_version = m_data.GetU16(offset_ptr); 435 m_coff_header_opt.major_subsystem_version = m_data.GetU16(offset_ptr); 436 m_coff_header_opt.minor_subsystem_version = m_data.GetU16(offset_ptr); 437 m_coff_header_opt.reserved1 = m_data.GetU32(offset_ptr); 438 m_coff_header_opt.image_size = m_data.GetU32(offset_ptr); 439 m_coff_header_opt.header_size = m_data.GetU32(offset_ptr); 440 m_coff_header_opt.checksum = m_data.GetU32(offset_ptr); 441 m_coff_header_opt.subsystem = m_data.GetU16(offset_ptr); 442 m_coff_header_opt.dll_flags = m_data.GetU16(offset_ptr); 443 m_coff_header_opt.stack_reserve_size = 444 m_data.GetMaxU64(offset_ptr, addr_byte_size); 445 m_coff_header_opt.stack_commit_size = 446 m_data.GetMaxU64(offset_ptr, addr_byte_size); 447 m_coff_header_opt.heap_reserve_size = 448 m_data.GetMaxU64(offset_ptr, addr_byte_size); 449 m_coff_header_opt.heap_commit_size = 450 m_data.GetMaxU64(offset_ptr, addr_byte_size); 451 m_coff_header_opt.loader_flags = m_data.GetU32(offset_ptr); 452 uint32_t num_data_dir_entries = m_data.GetU32(offset_ptr); 453 m_coff_header_opt.data_dirs.clear(); 454 m_coff_header_opt.data_dirs.resize(num_data_dir_entries); 455 uint32_t i; 456 for (i = 0; i < num_data_dir_entries; i++) { 457 m_coff_header_opt.data_dirs[i].vmaddr = m_data.GetU32(offset_ptr); 458 m_coff_header_opt.data_dirs[i].vmsize = m_data.GetU32(offset_ptr); 459 } 460 461 m_file_offset = m_coff_header_opt.image_base; 462 m_image_base = m_coff_header_opt.image_base; 463 } 464 } 465 } 466 // Make sure we are on track for section data which follows 467 *offset_ptr = end_offset; 468 return success; 469 } 470 471 DataExtractor ObjectFilePECOFF::ReadImageData(uint32_t offset, size_t size) { 472 if (m_file) { 473 // A bit of a hack, but we intend to write to this buffer, so we can't 474 // mmap it. 475 auto buffer_sp = MapFileData(m_file, size, offset); 476 return DataExtractor(buffer_sp, GetByteOrder(), GetAddressByteSize()); 477 } 478 ProcessSP process_sp(m_process_wp.lock()); 479 DataExtractor data; 480 if (process_sp) { 481 auto data_ap = llvm::make_unique<DataBufferHeap>(size, 0); 482 Status readmem_error; 483 size_t bytes_read = 484 process_sp->ReadMemory(m_image_base + offset, data_ap->GetBytes(), 485 data_ap->GetByteSize(), readmem_error); 486 if (bytes_read == size) { 487 DataBufferSP buffer_sp(data_ap.release()); 488 data.SetData(buffer_sp, 0, buffer_sp->GetByteSize()); 489 } 490 } 491 return data; 492 } 493 494 //---------------------------------------------------------------------- 495 // ParseSectionHeaders 496 //---------------------------------------------------------------------- 497 bool ObjectFilePECOFF::ParseSectionHeaders( 498 uint32_t section_header_data_offset) { 499 const uint32_t nsects = m_coff_header.nsects; 500 m_sect_headers.clear(); 501 502 if (nsects > 0) { 503 const size_t section_header_byte_size = nsects * sizeof(section_header_t); 504 DataExtractor section_header_data = 505 ReadImageData(section_header_data_offset, section_header_byte_size); 506 507 lldb::offset_t offset = 0; 508 if (section_header_data.ValidOffsetForDataOfSize( 509 offset, section_header_byte_size)) { 510 m_sect_headers.resize(nsects); 511 512 for (uint32_t idx = 0; idx < nsects; ++idx) { 513 const void *name_data = section_header_data.GetData(&offset, 8); 514 if (name_data) { 515 memcpy(m_sect_headers[idx].name, name_data, 8); 516 m_sect_headers[idx].vmsize = section_header_data.GetU32(&offset); 517 m_sect_headers[idx].vmaddr = section_header_data.GetU32(&offset); 518 m_sect_headers[idx].size = section_header_data.GetU32(&offset); 519 m_sect_headers[idx].offset = section_header_data.GetU32(&offset); 520 m_sect_headers[idx].reloff = section_header_data.GetU32(&offset); 521 m_sect_headers[idx].lineoff = section_header_data.GetU32(&offset); 522 m_sect_headers[idx].nreloc = section_header_data.GetU16(&offset); 523 m_sect_headers[idx].nline = section_header_data.GetU16(&offset); 524 m_sect_headers[idx].flags = section_header_data.GetU32(&offset); 525 } 526 } 527 } 528 } 529 530 return !m_sect_headers.empty(); 531 } 532 533 llvm::StringRef ObjectFilePECOFF::GetSectionName(const section_header_t §) { 534 llvm::StringRef hdr_name(sect.name, llvm::array_lengthof(sect.name)); 535 hdr_name = hdr_name.split('\0').first; 536 if (hdr_name.consume_front("/")) { 537 lldb::offset_t stroff; 538 if (!to_integer(hdr_name, stroff, 10)) 539 return ""; 540 lldb::offset_t string_file_offset = 541 m_coff_header.symoff + (m_coff_header.nsyms * 18) + stroff; 542 if (const char *name = m_data.GetCStr(&string_file_offset)) 543 return name; 544 return ""; 545 } 546 return hdr_name; 547 } 548 549 //---------------------------------------------------------------------- 550 // GetNListSymtab 551 //---------------------------------------------------------------------- 552 Symtab *ObjectFilePECOFF::GetSymtab() { 553 ModuleSP module_sp(GetModule()); 554 if (module_sp) { 555 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 556 if (m_symtab_ap.get() == NULL) { 557 SectionList *sect_list = GetSectionList(); 558 m_symtab_ap.reset(new Symtab(this)); 559 std::lock_guard<std::recursive_mutex> guard(m_symtab_ap->GetMutex()); 560 561 const uint32_t num_syms = m_coff_header.nsyms; 562 563 if (m_file && num_syms > 0 && m_coff_header.symoff > 0) { 564 const uint32_t symbol_size = 18; 565 const size_t symbol_data_size = num_syms * symbol_size; 566 // Include the 4-byte string table size at the end of the symbols 567 DataExtractor symtab_data = 568 ReadImageData(m_coff_header.symoff, symbol_data_size + 4); 569 lldb::offset_t offset = symbol_data_size; 570 const uint32_t strtab_size = symtab_data.GetU32(&offset); 571 if (strtab_size > 0) { 572 DataExtractor strtab_data = ReadImageData( 573 m_coff_header.symoff + symbol_data_size, strtab_size); 574 575 // First 4 bytes should be zeroed after strtab_size has been read, 576 // because it is used as offset 0 to encode a NULL string. 577 uint32_t *strtab_data_start = const_cast<uint32_t *>( 578 reinterpret_cast<const uint32_t *>(strtab_data.GetDataStart())); 579 strtab_data_start[0] = 0; 580 581 offset = 0; 582 std::string symbol_name; 583 Symbol *symbols = m_symtab_ap->Resize(num_syms); 584 for (uint32_t i = 0; i < num_syms; ++i) { 585 coff_symbol_t symbol; 586 const uint32_t symbol_offset = offset; 587 const char *symbol_name_cstr = NULL; 588 // If the first 4 bytes of the symbol string are zero, then they 589 // are followed by a 4-byte string table offset. Else these 590 // 8 bytes contain the symbol name 591 if (symtab_data.GetU32(&offset) == 0) { 592 // Long string that doesn't fit into the symbol table name, so 593 // now we must read the 4 byte string table offset 594 uint32_t strtab_offset = symtab_data.GetU32(&offset); 595 symbol_name_cstr = strtab_data.PeekCStr(strtab_offset); 596 symbol_name.assign(symbol_name_cstr); 597 } else { 598 // Short string that fits into the symbol table name which is 8 599 // bytes 600 offset += sizeof(symbol.name) - 4; // Skip remaining 601 symbol_name_cstr = symtab_data.PeekCStr(symbol_offset); 602 if (symbol_name_cstr == NULL) 603 break; 604 symbol_name.assign(symbol_name_cstr, sizeof(symbol.name)); 605 } 606 symbol.value = symtab_data.GetU32(&offset); 607 symbol.sect = symtab_data.GetU16(&offset); 608 symbol.type = symtab_data.GetU16(&offset); 609 symbol.storage = symtab_data.GetU8(&offset); 610 symbol.naux = symtab_data.GetU8(&offset); 611 symbols[i].GetMangled().SetValue(ConstString(symbol_name.c_str())); 612 if ((int16_t)symbol.sect >= 1) { 613 Address symbol_addr(sect_list->GetSectionAtIndex(symbol.sect - 1), 614 symbol.value); 615 symbols[i].GetAddressRef() = symbol_addr; 616 symbols[i].SetType(MapSymbolType(symbol.type)); 617 } 618 619 if (symbol.naux > 0) { 620 i += symbol.naux; 621 offset += symbol_size; 622 } 623 } 624 } 625 } 626 627 // Read export header 628 if (coff_data_dir_export_table < m_coff_header_opt.data_dirs.size() && 629 m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmsize > 0 && 630 m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmaddr > 0) { 631 export_directory_entry export_table; 632 uint32_t data_start = 633 m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmaddr; 634 635 uint32_t address_rva = data_start; 636 if (m_file) { 637 Address address(m_coff_header_opt.image_base + data_start, sect_list); 638 address_rva = 639 address.GetSection()->GetFileOffset() + address.GetOffset(); 640 } 641 DataExtractor symtab_data = 642 ReadImageData(address_rva, m_coff_header_opt.data_dirs[0].vmsize); 643 lldb::offset_t offset = 0; 644 645 // Read export_table header 646 export_table.characteristics = symtab_data.GetU32(&offset); 647 export_table.time_date_stamp = symtab_data.GetU32(&offset); 648 export_table.major_version = symtab_data.GetU16(&offset); 649 export_table.minor_version = symtab_data.GetU16(&offset); 650 export_table.name = symtab_data.GetU32(&offset); 651 export_table.base = symtab_data.GetU32(&offset); 652 export_table.number_of_functions = symtab_data.GetU32(&offset); 653 export_table.number_of_names = symtab_data.GetU32(&offset); 654 export_table.address_of_functions = symtab_data.GetU32(&offset); 655 export_table.address_of_names = symtab_data.GetU32(&offset); 656 export_table.address_of_name_ordinals = symtab_data.GetU32(&offset); 657 658 bool has_ordinal = export_table.address_of_name_ordinals != 0; 659 660 lldb::offset_t name_offset = export_table.address_of_names - data_start; 661 lldb::offset_t name_ordinal_offset = 662 export_table.address_of_name_ordinals - data_start; 663 664 Symbol *symbols = m_symtab_ap->Resize(export_table.number_of_names); 665 666 std::string symbol_name; 667 668 // Read each export table entry 669 for (size_t i = 0; i < export_table.number_of_names; ++i) { 670 uint32_t name_ordinal = 671 has_ordinal ? symtab_data.GetU16(&name_ordinal_offset) : i; 672 uint32_t name_address = symtab_data.GetU32(&name_offset); 673 674 const char *symbol_name_cstr = 675 symtab_data.PeekCStr(name_address - data_start); 676 symbol_name.assign(symbol_name_cstr); 677 678 lldb::offset_t function_offset = export_table.address_of_functions - 679 data_start + 680 sizeof(uint32_t) * name_ordinal; 681 uint32_t function_rva = symtab_data.GetU32(&function_offset); 682 683 Address symbol_addr(m_coff_header_opt.image_base + function_rva, 684 sect_list); 685 symbols[i].GetMangled().SetValue(ConstString(symbol_name.c_str())); 686 symbols[i].GetAddressRef() = symbol_addr; 687 symbols[i].SetType(lldb::eSymbolTypeCode); 688 symbols[i].SetDebug(true); 689 } 690 } 691 m_symtab_ap->CalculateSymbolSizes(); 692 } 693 } 694 return m_symtab_ap.get(); 695 } 696 697 bool ObjectFilePECOFF::IsStripped() { 698 // TODO: determine this for COFF 699 return false; 700 } 701 702 void ObjectFilePECOFF::CreateSections(SectionList &unified_section_list) { 703 if (m_sections_ap) 704 return; 705 m_sections_ap.reset(new SectionList()); 706 707 ModuleSP module_sp(GetModule()); 708 if (module_sp) { 709 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 710 const uint32_t nsects = m_sect_headers.size(); 711 ModuleSP module_sp(GetModule()); 712 for (uint32_t idx = 0; idx < nsects; ++idx) { 713 ConstString const_sect_name(GetSectionName(m_sect_headers[idx])); 714 static ConstString g_code_sect_name(".code"); 715 static ConstString g_CODE_sect_name("CODE"); 716 static ConstString g_data_sect_name(".data"); 717 static ConstString g_DATA_sect_name("DATA"); 718 static ConstString g_bss_sect_name(".bss"); 719 static ConstString g_BSS_sect_name("BSS"); 720 static ConstString g_debug_sect_name(".debug"); 721 static ConstString g_reloc_sect_name(".reloc"); 722 static ConstString g_stab_sect_name(".stab"); 723 static ConstString g_stabstr_sect_name(".stabstr"); 724 static ConstString g_sect_name_dwarf_debug_abbrev(".debug_abbrev"); 725 static ConstString g_sect_name_dwarf_debug_aranges(".debug_aranges"); 726 static ConstString g_sect_name_dwarf_debug_frame(".debug_frame"); 727 static ConstString g_sect_name_dwarf_debug_info(".debug_info"); 728 static ConstString g_sect_name_dwarf_debug_line(".debug_line"); 729 static ConstString g_sect_name_dwarf_debug_loc(".debug_loc"); 730 static ConstString g_sect_name_dwarf_debug_loclists(".debug_loclists"); 731 static ConstString g_sect_name_dwarf_debug_macinfo(".debug_macinfo"); 732 static ConstString g_sect_name_dwarf_debug_names(".debug_names"); 733 static ConstString g_sect_name_dwarf_debug_pubnames(".debug_pubnames"); 734 static ConstString g_sect_name_dwarf_debug_pubtypes(".debug_pubtypes"); 735 static ConstString g_sect_name_dwarf_debug_ranges(".debug_ranges"); 736 static ConstString g_sect_name_dwarf_debug_str(".debug_str"); 737 static ConstString g_sect_name_dwarf_debug_types(".debug_types"); 738 static ConstString g_sect_name_eh_frame(".eh_frame"); 739 static ConstString g_sect_name_go_symtab(".gosymtab"); 740 SectionType section_type = eSectionTypeOther; 741 if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_CODE && 742 ((const_sect_name == g_code_sect_name) || 743 (const_sect_name == g_CODE_sect_name))) { 744 section_type = eSectionTypeCode; 745 } else if (m_sect_headers[idx].flags & 746 llvm::COFF::IMAGE_SCN_CNT_INITIALIZED_DATA && 747 ((const_sect_name == g_data_sect_name) || 748 (const_sect_name == g_DATA_sect_name))) { 749 if (m_sect_headers[idx].size == 0 && m_sect_headers[idx].offset == 0) 750 section_type = eSectionTypeZeroFill; 751 else 752 section_type = eSectionTypeData; 753 } else if (m_sect_headers[idx].flags & 754 llvm::COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA && 755 ((const_sect_name == g_bss_sect_name) || 756 (const_sect_name == g_BSS_sect_name))) { 757 if (m_sect_headers[idx].size == 0) 758 section_type = eSectionTypeZeroFill; 759 else 760 section_type = eSectionTypeData; 761 } else if (const_sect_name == g_debug_sect_name) { 762 section_type = eSectionTypeDebug; 763 } else if (const_sect_name == g_stabstr_sect_name) { 764 section_type = eSectionTypeDataCString; 765 } else if (const_sect_name == g_reloc_sect_name) { 766 section_type = eSectionTypeOther; 767 } else if (const_sect_name == g_sect_name_dwarf_debug_abbrev) 768 section_type = eSectionTypeDWARFDebugAbbrev; 769 else if (const_sect_name == g_sect_name_dwarf_debug_aranges) 770 section_type = eSectionTypeDWARFDebugAranges; 771 else if (const_sect_name == g_sect_name_dwarf_debug_frame) 772 section_type = eSectionTypeDWARFDebugFrame; 773 else if (const_sect_name == g_sect_name_dwarf_debug_info) 774 section_type = eSectionTypeDWARFDebugInfo; 775 else if (const_sect_name == g_sect_name_dwarf_debug_line) 776 section_type = eSectionTypeDWARFDebugLine; 777 else if (const_sect_name == g_sect_name_dwarf_debug_loc) 778 section_type = eSectionTypeDWARFDebugLoc; 779 else if (const_sect_name == g_sect_name_dwarf_debug_loclists) 780 section_type = eSectionTypeDWARFDebugLocLists; 781 else if (const_sect_name == g_sect_name_dwarf_debug_macinfo) 782 section_type = eSectionTypeDWARFDebugMacInfo; 783 else if (const_sect_name == g_sect_name_dwarf_debug_names) 784 section_type = eSectionTypeDWARFDebugNames; 785 else if (const_sect_name == g_sect_name_dwarf_debug_pubnames) 786 section_type = eSectionTypeDWARFDebugPubNames; 787 else if (const_sect_name == g_sect_name_dwarf_debug_pubtypes) 788 section_type = eSectionTypeDWARFDebugPubTypes; 789 else if (const_sect_name == g_sect_name_dwarf_debug_ranges) 790 section_type = eSectionTypeDWARFDebugRanges; 791 else if (const_sect_name == g_sect_name_dwarf_debug_str) 792 section_type = eSectionTypeDWARFDebugStr; 793 else if (const_sect_name == g_sect_name_dwarf_debug_types) 794 section_type = eSectionTypeDWARFDebugTypes; 795 else if (const_sect_name == g_sect_name_eh_frame) 796 section_type = eSectionTypeEHFrame; 797 else if (const_sect_name == g_sect_name_go_symtab) 798 section_type = eSectionTypeGoSymtab; 799 else if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_CODE) { 800 section_type = eSectionTypeCode; 801 } else if (m_sect_headers[idx].flags & 802 llvm::COFF::IMAGE_SCN_CNT_INITIALIZED_DATA) { 803 section_type = eSectionTypeData; 804 } else if (m_sect_headers[idx].flags & 805 llvm::COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) { 806 if (m_sect_headers[idx].size == 0) 807 section_type = eSectionTypeZeroFill; 808 else 809 section_type = eSectionTypeData; 810 } 811 812 // Use a segment ID of the segment index shifted left by 8 so they 813 // never conflict with any of the sections. 814 SectionSP section_sp(new Section( 815 module_sp, // Module to which this section belongs 816 this, // Object file to which this section belongs 817 idx + 1, // Section ID is the 1 based segment index shifted right by 818 // 8 bits as not to collide with any of the 256 section IDs 819 // that are possible 820 const_sect_name, // Name of this section 821 section_type, // This section is a container of other sections. 822 m_coff_header_opt.image_base + 823 m_sect_headers[idx].vmaddr, // File VM address == addresses as 824 // they are found in the object file 825 m_sect_headers[idx].vmsize, // VM size in bytes of this section 826 m_sect_headers[idx] 827 .offset, // Offset to the data for this section in the file 828 m_sect_headers[idx] 829 .size, // Size in bytes of this section as found in the file 830 m_coff_header_opt.sect_alignment, // Section alignment 831 m_sect_headers[idx].flags)); // Flags for this section 832 833 // section_sp->SetIsEncrypted (segment_is_encrypted); 834 835 unified_section_list.AddSection(section_sp); 836 m_sections_ap->AddSection(section_sp); 837 } 838 } 839 } 840 841 bool ObjectFilePECOFF::GetUUID(UUID *uuid) { return false; } 842 843 uint32_t ObjectFilePECOFF::ParseDependentModules() { 844 ModuleSP module_sp(GetModule()); 845 if (!module_sp) 846 return 0; 847 848 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 849 if (m_deps_filespec) 850 return m_deps_filespec->GetSize(); 851 852 // Cache coff binary if it is not done yet. 853 if (!CreateBinary()) 854 return 0; 855 856 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT)); 857 if (log) 858 log->Printf("%p ObjectFilePECOFF::ParseDependentModules() module = %p " 859 "(%s), binary = %p (Bin = %p)", 860 static_cast<void *>(this), static_cast<void *>(module_sp.get()), 861 module_sp->GetSpecificationDescription().c_str(), 862 static_cast<void *>(m_owningbin.getPointer()), 863 m_owningbin ? static_cast<void *>(m_owningbin->getBinary()) 864 : nullptr); 865 866 auto COFFObj = 867 llvm::dyn_cast<llvm::object::COFFObjectFile>(m_owningbin->getBinary()); 868 if (!COFFObj) 869 return 0; 870 871 m_deps_filespec = FileSpecList(); 872 873 for (const auto &entry : COFFObj->import_directories()) { 874 llvm::StringRef dll_name; 875 auto ec = entry.getName(dll_name); 876 // Report a bogus entry. 877 if (ec != std::error_code()) { 878 if (log) 879 log->Printf("ObjectFilePECOFF::ParseDependentModules() - failed to get " 880 "import directory entry name: %s", 881 ec.message().c_str()); 882 continue; 883 } 884 885 // At this moment we only have the base name of the DLL. The full path can 886 // only be seen after the dynamic loading. Our best guess is Try to get it 887 // with the help of the object file's directory. 888 llvm::SmallString<128> dll_fullpath; 889 FileSpec dll_specs(dll_name); 890 dll_specs.GetDirectory().SetString(m_file.GetDirectory().GetCString()); 891 892 if (!llvm::sys::fs::real_path(dll_specs.GetPath(), dll_fullpath)) 893 m_deps_filespec->Append(FileSpec(dll_fullpath)); 894 else { 895 // Known DLLs or DLL not found in the object file directory. 896 m_deps_filespec->Append(FileSpec(dll_name)); 897 } 898 } 899 return m_deps_filespec->GetSize(); 900 } 901 902 uint32_t ObjectFilePECOFF::GetDependentModules(FileSpecList &files) { 903 auto num_modules = ParseDependentModules(); 904 auto original_size = files.GetSize(); 905 906 for (unsigned i = 0; i < num_modules; ++i) 907 files.AppendIfUnique(m_deps_filespec->GetFileSpecAtIndex(i)); 908 909 return files.GetSize() - original_size; 910 } 911 912 lldb_private::Address ObjectFilePECOFF::GetEntryPointAddress() { 913 if (m_entry_point_address.IsValid()) 914 return m_entry_point_address; 915 916 if (!ParseHeader() || !IsExecutable()) 917 return m_entry_point_address; 918 919 SectionList *section_list = GetSectionList(); 920 addr_t file_addr = m_coff_header_opt.entry + m_coff_header_opt.image_base; 921 922 if (!section_list) 923 m_entry_point_address.SetOffset(file_addr); 924 else 925 m_entry_point_address.ResolveAddressUsingFileSections(file_addr, section_list); 926 return m_entry_point_address; 927 } 928 929 //---------------------------------------------------------------------- 930 // Dump 931 // 932 // Dump the specifics of the runtime file container (such as any headers 933 // segments, sections, etc). 934 //---------------------------------------------------------------------- 935 void ObjectFilePECOFF::Dump(Stream *s) { 936 ModuleSP module_sp(GetModule()); 937 if (module_sp) { 938 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 939 s->Printf("%p: ", static_cast<void *>(this)); 940 s->Indent(); 941 s->PutCString("ObjectFilePECOFF"); 942 943 ArchSpec header_arch = GetArchitecture(); 944 945 *s << ", file = '" << m_file 946 << "', arch = " << header_arch.GetArchitectureName() << "\n"; 947 948 SectionList *sections = GetSectionList(); 949 if (sections) 950 sections->Dump(s, NULL, true, UINT32_MAX); 951 952 if (m_symtab_ap.get()) 953 m_symtab_ap->Dump(s, NULL, eSortOrderNone); 954 955 if (m_dos_header.e_magic) 956 DumpDOSHeader(s, m_dos_header); 957 if (m_coff_header.machine) { 958 DumpCOFFHeader(s, m_coff_header); 959 if (m_coff_header.hdrsize) 960 DumpOptCOFFHeader(s, m_coff_header_opt); 961 } 962 s->EOL(); 963 DumpSectionHeaders(s); 964 s->EOL(); 965 966 DumpDependentModules(s); 967 s->EOL(); 968 } 969 } 970 971 //---------------------------------------------------------------------- 972 // DumpDOSHeader 973 // 974 // Dump the MS-DOS header to the specified output stream 975 //---------------------------------------------------------------------- 976 void ObjectFilePECOFF::DumpDOSHeader(Stream *s, const dos_header_t &header) { 977 s->PutCString("MSDOS Header\n"); 978 s->Printf(" e_magic = 0x%4.4x\n", header.e_magic); 979 s->Printf(" e_cblp = 0x%4.4x\n", header.e_cblp); 980 s->Printf(" e_cp = 0x%4.4x\n", header.e_cp); 981 s->Printf(" e_crlc = 0x%4.4x\n", header.e_crlc); 982 s->Printf(" e_cparhdr = 0x%4.4x\n", header.e_cparhdr); 983 s->Printf(" e_minalloc = 0x%4.4x\n", header.e_minalloc); 984 s->Printf(" e_maxalloc = 0x%4.4x\n", header.e_maxalloc); 985 s->Printf(" e_ss = 0x%4.4x\n", header.e_ss); 986 s->Printf(" e_sp = 0x%4.4x\n", header.e_sp); 987 s->Printf(" e_csum = 0x%4.4x\n", header.e_csum); 988 s->Printf(" e_ip = 0x%4.4x\n", header.e_ip); 989 s->Printf(" e_cs = 0x%4.4x\n", header.e_cs); 990 s->Printf(" e_lfarlc = 0x%4.4x\n", header.e_lfarlc); 991 s->Printf(" e_ovno = 0x%4.4x\n", header.e_ovno); 992 s->Printf(" e_res[4] = { 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x }\n", 993 header.e_res[0], header.e_res[1], header.e_res[2], header.e_res[3]); 994 s->Printf(" e_oemid = 0x%4.4x\n", header.e_oemid); 995 s->Printf(" e_oeminfo = 0x%4.4x\n", header.e_oeminfo); 996 s->Printf(" e_res2[10] = { 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, " 997 "0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x }\n", 998 header.e_res2[0], header.e_res2[1], header.e_res2[2], 999 header.e_res2[3], header.e_res2[4], header.e_res2[5], 1000 header.e_res2[6], header.e_res2[7], header.e_res2[8], 1001 header.e_res2[9]); 1002 s->Printf(" e_lfanew = 0x%8.8x\n", header.e_lfanew); 1003 } 1004 1005 //---------------------------------------------------------------------- 1006 // DumpCOFFHeader 1007 // 1008 // Dump the COFF header to the specified output stream 1009 //---------------------------------------------------------------------- 1010 void ObjectFilePECOFF::DumpCOFFHeader(Stream *s, const coff_header_t &header) { 1011 s->PutCString("COFF Header\n"); 1012 s->Printf(" machine = 0x%4.4x\n", header.machine); 1013 s->Printf(" nsects = 0x%4.4x\n", header.nsects); 1014 s->Printf(" modtime = 0x%8.8x\n", header.modtime); 1015 s->Printf(" symoff = 0x%8.8x\n", header.symoff); 1016 s->Printf(" nsyms = 0x%8.8x\n", header.nsyms); 1017 s->Printf(" hdrsize = 0x%4.4x\n", header.hdrsize); 1018 } 1019 1020 //---------------------------------------------------------------------- 1021 // DumpOptCOFFHeader 1022 // 1023 // Dump the optional COFF header to the specified output stream 1024 //---------------------------------------------------------------------- 1025 void ObjectFilePECOFF::DumpOptCOFFHeader(Stream *s, 1026 const coff_opt_header_t &header) { 1027 s->PutCString("Optional COFF Header\n"); 1028 s->Printf(" magic = 0x%4.4x\n", header.magic); 1029 s->Printf(" major_linker_version = 0x%2.2x\n", 1030 header.major_linker_version); 1031 s->Printf(" minor_linker_version = 0x%2.2x\n", 1032 header.minor_linker_version); 1033 s->Printf(" code_size = 0x%8.8x\n", header.code_size); 1034 s->Printf(" data_size = 0x%8.8x\n", header.data_size); 1035 s->Printf(" bss_size = 0x%8.8x\n", header.bss_size); 1036 s->Printf(" entry = 0x%8.8x\n", header.entry); 1037 s->Printf(" code_offset = 0x%8.8x\n", header.code_offset); 1038 s->Printf(" data_offset = 0x%8.8x\n", header.data_offset); 1039 s->Printf(" image_base = 0x%16.16" PRIx64 "\n", 1040 header.image_base); 1041 s->Printf(" sect_alignment = 0x%8.8x\n", header.sect_alignment); 1042 s->Printf(" file_alignment = 0x%8.8x\n", header.file_alignment); 1043 s->Printf(" major_os_system_version = 0x%4.4x\n", 1044 header.major_os_system_version); 1045 s->Printf(" minor_os_system_version = 0x%4.4x\n", 1046 header.minor_os_system_version); 1047 s->Printf(" major_image_version = 0x%4.4x\n", 1048 header.major_image_version); 1049 s->Printf(" minor_image_version = 0x%4.4x\n", 1050 header.minor_image_version); 1051 s->Printf(" major_subsystem_version = 0x%4.4x\n", 1052 header.major_subsystem_version); 1053 s->Printf(" minor_subsystem_version = 0x%4.4x\n", 1054 header.minor_subsystem_version); 1055 s->Printf(" reserved1 = 0x%8.8x\n", header.reserved1); 1056 s->Printf(" image_size = 0x%8.8x\n", header.image_size); 1057 s->Printf(" header_size = 0x%8.8x\n", header.header_size); 1058 s->Printf(" checksum = 0x%8.8x\n", header.checksum); 1059 s->Printf(" subsystem = 0x%4.4x\n", header.subsystem); 1060 s->Printf(" dll_flags = 0x%4.4x\n", header.dll_flags); 1061 s->Printf(" stack_reserve_size = 0x%16.16" PRIx64 "\n", 1062 header.stack_reserve_size); 1063 s->Printf(" stack_commit_size = 0x%16.16" PRIx64 "\n", 1064 header.stack_commit_size); 1065 s->Printf(" heap_reserve_size = 0x%16.16" PRIx64 "\n", 1066 header.heap_reserve_size); 1067 s->Printf(" heap_commit_size = 0x%16.16" PRIx64 "\n", 1068 header.heap_commit_size); 1069 s->Printf(" loader_flags = 0x%8.8x\n", header.loader_flags); 1070 s->Printf(" num_data_dir_entries = 0x%8.8x\n", 1071 (uint32_t)header.data_dirs.size()); 1072 uint32_t i; 1073 for (i = 0; i < header.data_dirs.size(); i++) { 1074 s->Printf(" data_dirs[%2u] vmaddr = 0x%8.8x, vmsize = 0x%8.8x\n", i, 1075 header.data_dirs[i].vmaddr, header.data_dirs[i].vmsize); 1076 } 1077 } 1078 //---------------------------------------------------------------------- 1079 // DumpSectionHeader 1080 // 1081 // Dump a single ELF section header to the specified output stream 1082 //---------------------------------------------------------------------- 1083 void ObjectFilePECOFF::DumpSectionHeader(Stream *s, 1084 const section_header_t &sh) { 1085 std::string name = GetSectionName(sh); 1086 s->Printf("%-16s 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x 0x%4.4x " 1087 "0x%4.4x 0x%8.8x\n", 1088 name.c_str(), sh.vmaddr, sh.vmsize, sh.offset, sh.size, sh.reloff, 1089 sh.lineoff, sh.nreloc, sh.nline, sh.flags); 1090 } 1091 1092 //---------------------------------------------------------------------- 1093 // DumpSectionHeaders 1094 // 1095 // Dump all of the ELF section header to the specified output stream 1096 //---------------------------------------------------------------------- 1097 void ObjectFilePECOFF::DumpSectionHeaders(Stream *s) { 1098 1099 s->PutCString("Section Headers\n"); 1100 s->PutCString("IDX name vm addr vm size file off file " 1101 "size reloc off line off nreloc nline flags\n"); 1102 s->PutCString("==== ---------------- ---------- ---------- ---------- " 1103 "---------- ---------- ---------- ------ ------ ----------\n"); 1104 1105 uint32_t idx = 0; 1106 SectionHeaderCollIter pos, end = m_sect_headers.end(); 1107 1108 for (pos = m_sect_headers.begin(); pos != end; ++pos, ++idx) { 1109 s->Printf("[%2u] ", idx); 1110 ObjectFilePECOFF::DumpSectionHeader(s, *pos); 1111 } 1112 } 1113 1114 //---------------------------------------------------------------------- 1115 // DumpDependentModules 1116 // 1117 // Dump all of the dependent modules to the specified output stream 1118 //---------------------------------------------------------------------- 1119 void ObjectFilePECOFF::DumpDependentModules(lldb_private::Stream *s) { 1120 auto num_modules = ParseDependentModules(); 1121 if (num_modules > 0) { 1122 s->PutCString("Dependent Modules\n"); 1123 for (unsigned i = 0; i < num_modules; ++i) { 1124 auto spec = m_deps_filespec->GetFileSpecAtIndex(i); 1125 s->Printf(" %s\n", spec.GetFilename().GetCString()); 1126 } 1127 } 1128 } 1129 1130 bool ObjectFilePECOFF::IsWindowsSubsystem() { 1131 switch (m_coff_header_opt.subsystem) { 1132 case llvm::COFF::IMAGE_SUBSYSTEM_NATIVE: 1133 case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_GUI: 1134 case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_CUI: 1135 case llvm::COFF::IMAGE_SUBSYSTEM_NATIVE_WINDOWS: 1136 case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_CE_GUI: 1137 case llvm::COFF::IMAGE_SUBSYSTEM_XBOX: 1138 case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_BOOT_APPLICATION: 1139 return true; 1140 default: 1141 return false; 1142 } 1143 } 1144 1145 ArchSpec ObjectFilePECOFF::GetArchitecture() { 1146 uint16_t machine = m_coff_header.machine; 1147 switch (machine) { 1148 default: 1149 break; 1150 case llvm::COFF::IMAGE_FILE_MACHINE_AMD64: 1151 case llvm::COFF::IMAGE_FILE_MACHINE_I386: 1152 case llvm::COFF::IMAGE_FILE_MACHINE_POWERPC: 1153 case llvm::COFF::IMAGE_FILE_MACHINE_POWERPCFP: 1154 case llvm::COFF::IMAGE_FILE_MACHINE_ARM: 1155 case llvm::COFF::IMAGE_FILE_MACHINE_ARMNT: 1156 case llvm::COFF::IMAGE_FILE_MACHINE_THUMB: 1157 ArchSpec arch; 1158 arch.SetArchitecture(eArchTypeCOFF, machine, LLDB_INVALID_CPUTYPE, 1159 IsWindowsSubsystem() ? llvm::Triple::Win32 1160 : llvm::Triple::UnknownOS); 1161 return arch; 1162 } 1163 return ArchSpec(); 1164 } 1165 1166 ObjectFile::Type ObjectFilePECOFF::CalculateType() { 1167 if (m_coff_header.machine != 0) { 1168 if ((m_coff_header.flags & llvm::COFF::IMAGE_FILE_DLL) == 0) 1169 return eTypeExecutable; 1170 else 1171 return eTypeSharedLibrary; 1172 } 1173 return eTypeExecutable; 1174 } 1175 1176 ObjectFile::Strata ObjectFilePECOFF::CalculateStrata() { return eStrataUser; } 1177 1178 //------------------------------------------------------------------ 1179 // PluginInterface protocol 1180 //------------------------------------------------------------------ 1181 ConstString ObjectFilePECOFF::GetPluginName() { return GetPluginNameStatic(); } 1182 1183 uint32_t ObjectFilePECOFF::GetPluginVersion() { return 1; } 1184