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