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