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