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