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