1f754f88fSGreg Clayton //===-- ObjectFilePECOFF.cpp ------------------------------------*- C++ -*-===// 2f754f88fSGreg Clayton // 32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6f754f88fSGreg Clayton // 7f754f88fSGreg Clayton //===----------------------------------------------------------------------===// 8f754f88fSGreg Clayton 9f754f88fSGreg Clayton #include "ObjectFilePECOFF.h" 1030c2441aSAleksandr Urakov #include "PECallFrameInfo.h" 11f7d1893fSAdrian McCarthy #include "WindowsMiniDump.h" 12f754f88fSGreg Clayton 13f754f88fSGreg Clayton #include "lldb/Core/FileSpecList.h" 14f754f88fSGreg Clayton #include "lldb/Core/Module.h" 15f4d6de6aSGreg Clayton #include "lldb/Core/ModuleSpec.h" 16f754f88fSGreg Clayton #include "lldb/Core/PluginManager.h" 17f754f88fSGreg Clayton #include "lldb/Core/Section.h" 18f754f88fSGreg Clayton #include "lldb/Core/StreamFile.h" 19f754f88fSGreg Clayton #include "lldb/Symbol/ObjectFile.h" 20f7d1893fSAdrian McCarthy #include "lldb/Target/Process.h" 212756adf3SVirgile Bello #include "lldb/Target/SectionLoadList.h" 222756adf3SVirgile Bello #include "lldb/Target/Target.h" 235f19b907SPavel Labath #include "lldb/Utility/ArchSpec.h" 24666cc0b2SZachary Turner #include "lldb/Utility/DataBufferHeap.h" 255713a05bSZachary Turner #include "lldb/Utility/FileSpec.h" 26037ed1beSAaron Smith #include "lldb/Utility/Log.h" 27bf9a7730SZachary Turner #include "lldb/Utility/StreamString.h" 2838d0632eSPavel Labath #include "lldb/Utility/Timer.h" 29666cc0b2SZachary Turner #include "lldb/Utility/UUID.h" 305f19b907SPavel Labath #include "llvm/BinaryFormat/COFF.h" 31f754f88fSGreg Clayton 32037ed1beSAaron Smith #include "llvm/Object/COFFImportFile.h" 33037ed1beSAaron Smith #include "llvm/Support/Error.h" 343f4a4b36SZachary Turner #include "llvm/Support/MemoryBuffer.h" 353f4a4b36SZachary Turner 36f754f88fSGreg Clayton #define IMAGE_DOS_SIGNATURE 0x5A4D // MZ 37f754f88fSGreg Clayton #define IMAGE_NT_SIGNATURE 0x00004550 // PE00 38f754f88fSGreg Clayton #define OPT_HEADER_MAGIC_PE32 0x010b 39f754f88fSGreg Clayton #define OPT_HEADER_MAGIC_PE32_PLUS 0x020b 40f754f88fSGreg Clayton 41f754f88fSGreg Clayton using namespace lldb; 42f754f88fSGreg Clayton using namespace lldb_private; 43f754f88fSGreg Clayton 44b8d03935SAaron Smith struct CVInfoPdb70 { 45b8d03935SAaron Smith // 16-byte GUID 46b8d03935SAaron Smith struct _Guid { 47b8d03935SAaron Smith llvm::support::ulittle32_t Data1; 48b8d03935SAaron Smith llvm::support::ulittle16_t Data2; 49b8d03935SAaron Smith llvm::support::ulittle16_t Data3; 50b8d03935SAaron Smith uint8_t Data4[8]; 51b8d03935SAaron Smith } Guid; 52b8d03935SAaron Smith 53b8d03935SAaron Smith llvm::support::ulittle32_t Age; 54b8d03935SAaron Smith }; 55b8d03935SAaron Smith 56b8d03935SAaron Smith static UUID GetCoffUUID(llvm::object::COFFObjectFile *coff_obj) { 57b8d03935SAaron Smith if (!coff_obj) 58b8d03935SAaron Smith return UUID(); 59b8d03935SAaron Smith 60b8d03935SAaron Smith const llvm::codeview::DebugInfo *pdb_info = nullptr; 61b8d03935SAaron Smith llvm::StringRef pdb_file; 62b8d03935SAaron Smith 63b8d03935SAaron Smith // This part is similar with what has done in minidump parser. 64b8d03935SAaron Smith if (!coff_obj->getDebugPDBInfo(pdb_info, pdb_file) && pdb_info) { 65b8d03935SAaron Smith if (pdb_info->PDB70.CVSignature == llvm::OMF::Signature::PDB70) { 66b8d03935SAaron Smith using llvm::support::endian::read16be; 67b8d03935SAaron Smith using llvm::support::endian::read32be; 68b8d03935SAaron Smith 69b8d03935SAaron Smith const uint8_t *sig = pdb_info->PDB70.Signature; 70b8d03935SAaron Smith struct CVInfoPdb70 info; 71b8d03935SAaron Smith info.Guid.Data1 = read32be(sig); 72b8d03935SAaron Smith sig += 4; 73b8d03935SAaron Smith info.Guid.Data2 = read16be(sig); 74b8d03935SAaron Smith sig += 2; 75b8d03935SAaron Smith info.Guid.Data3 = read16be(sig); 76b8d03935SAaron Smith sig += 2; 77b8d03935SAaron Smith memcpy(info.Guid.Data4, sig, 8); 78b8d03935SAaron Smith 79b8d03935SAaron Smith // Return 20-byte UUID if the Age is not zero 80b8d03935SAaron Smith if (pdb_info->PDB70.Age) { 81b8d03935SAaron Smith info.Age = read32be(&pdb_info->PDB70.Age); 82b8d03935SAaron Smith return UUID::fromOptionalData(&info, sizeof(info)); 83b8d03935SAaron Smith } 84b8d03935SAaron Smith // Otherwise return 16-byte GUID 85b8d03935SAaron Smith return UUID::fromOptionalData(&info.Guid, sizeof(info.Guid)); 86b8d03935SAaron Smith } 87b8d03935SAaron Smith } 88b8d03935SAaron Smith 89b8d03935SAaron Smith return UUID(); 90b8d03935SAaron Smith } 91b8d03935SAaron Smith 92e84f7841SPavel Labath char ObjectFilePECOFF::ID; 93e84f7841SPavel Labath 94b9c1b51eSKate Stone void ObjectFilePECOFF::Initialize() { 95b9c1b51eSKate Stone PluginManager::RegisterPlugin( 96b9c1b51eSKate Stone GetPluginNameStatic(), GetPluginDescriptionStatic(), CreateInstance, 97b9c1b51eSKate Stone CreateMemoryInstance, GetModuleSpecifications, SaveCore); 98f754f88fSGreg Clayton } 99f754f88fSGreg Clayton 100b9c1b51eSKate Stone void ObjectFilePECOFF::Terminate() { 101f754f88fSGreg Clayton PluginManager::UnregisterPlugin(CreateInstance); 102f754f88fSGreg Clayton } 103f754f88fSGreg Clayton 104b9c1b51eSKate Stone lldb_private::ConstString ObjectFilePECOFF::GetPluginNameStatic() { 10557abc5d6SGreg Clayton static ConstString g_name("pe-coff"); 10657abc5d6SGreg Clayton return g_name; 107f754f88fSGreg Clayton } 108f754f88fSGreg Clayton 109b9c1b51eSKate Stone const char *ObjectFilePECOFF::GetPluginDescriptionStatic() { 110b9c1b51eSKate Stone return "Portable Executable and Common Object File Format object file reader " 111b9c1b51eSKate Stone "(32 and 64 bit)"; 112f754f88fSGreg Clayton } 113f754f88fSGreg Clayton 114b9c1b51eSKate Stone ObjectFile *ObjectFilePECOFF::CreateInstance(const lldb::ModuleSP &module_sp, 1155ce9c565SGreg Clayton DataBufferSP &data_sp, 1165ce9c565SGreg Clayton lldb::offset_t data_offset, 1175ce9c565SGreg Clayton const lldb_private::FileSpec *file, 1185ce9c565SGreg Clayton lldb::offset_t file_offset, 119b9c1b51eSKate Stone lldb::offset_t length) { 120b9c1b51eSKate Stone if (!data_sp) { 12150251fc7SPavel Labath data_sp = MapFileData(file, length, file_offset); 1223f4a4b36SZachary Turner if (!data_sp) 1233f4a4b36SZachary Turner return nullptr; 1245ce9c565SGreg Clayton data_offset = 0; 1255ce9c565SGreg Clayton } 1265ce9c565SGreg Clayton 1273f4a4b36SZachary Turner if (!ObjectFilePECOFF::MagicBytesMatch(data_sp)) 1283f4a4b36SZachary Turner return nullptr; 1293f4a4b36SZachary Turner 1305ce9c565SGreg Clayton // Update the data to contain the entire file if it doesn't already 1313f4a4b36SZachary Turner if (data_sp->GetByteSize() < length) { 13250251fc7SPavel Labath data_sp = MapFileData(file, length, file_offset); 1333f4a4b36SZachary Turner if (!data_sp) 1343f4a4b36SZachary Turner return nullptr; 135f754f88fSGreg Clayton } 1363f4a4b36SZachary Turner 137a8f3ae7cSJonas Devlieghere auto objfile_up = std::make_unique<ObjectFilePECOFF>( 1383f4a4b36SZachary Turner module_sp, data_sp, data_offset, file, file_offset, length); 139d5b44036SJonas Devlieghere if (!objfile_up || !objfile_up->ParseHeader()) 1403f4a4b36SZachary Turner return nullptr; 1413f4a4b36SZachary Turner 142037ed1beSAaron Smith // Cache coff binary. 143d5b44036SJonas Devlieghere if (!objfile_up->CreateBinary()) 144037ed1beSAaron Smith return nullptr; 145037ed1beSAaron Smith 146d5b44036SJonas Devlieghere return objfile_up.release(); 147f754f88fSGreg Clayton } 148f754f88fSGreg Clayton 149b9c1b51eSKate Stone ObjectFile *ObjectFilePECOFF::CreateMemoryInstance( 150b9c1b51eSKate Stone const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp, 151b9c1b51eSKate Stone const lldb::ProcessSP &process_sp, lldb::addr_t header_addr) { 152344546bdSWalter Erquinigo if (!data_sp || !ObjectFilePECOFF::MagicBytesMatch(data_sp)) 153344546bdSWalter Erquinigo return nullptr; 154a8f3ae7cSJonas Devlieghere auto objfile_up = std::make_unique<ObjectFilePECOFF>( 155344546bdSWalter Erquinigo module_sp, data_sp, process_sp, header_addr); 156d5b44036SJonas Devlieghere if (objfile_up.get() && objfile_up->ParseHeader()) { 157d5b44036SJonas Devlieghere return objfile_up.release(); 158344546bdSWalter Erquinigo } 159344546bdSWalter Erquinigo return nullptr; 160c9660546SGreg Clayton } 161c9660546SGreg Clayton 162b9c1b51eSKate Stone size_t ObjectFilePECOFF::GetModuleSpecifications( 163b9c1b51eSKate Stone const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp, 164b9c1b51eSKate Stone lldb::offset_t data_offset, lldb::offset_t file_offset, 165b9c1b51eSKate Stone lldb::offset_t length, lldb_private::ModuleSpecList &specs) { 16689eb1baeSVirgile Bello const size_t initial_count = specs.GetSize(); 167b8d03935SAaron Smith if (!data_sp || !ObjectFilePECOFF::MagicBytesMatch(data_sp)) 168b8d03935SAaron Smith return initial_count; 16989eb1baeSVirgile Bello 170b8d03935SAaron Smith auto binary = llvm::object::createBinary(file.GetPath()); 171b8d03935SAaron Smith if (!binary) 172b8d03935SAaron Smith return initial_count; 17389eb1baeSVirgile Bello 174b8d03935SAaron Smith if (!binary->getBinary()->isCOFF() && 175b8d03935SAaron Smith !binary->getBinary()->isCOFFImportFile()) 176b8d03935SAaron Smith return initial_count; 17789eb1baeSVirgile Bello 178b8d03935SAaron Smith auto COFFObj = 179b8d03935SAaron Smith llvm::cast<llvm::object::COFFObjectFile>(binary->getBinary()); 180b8d03935SAaron Smith 181b8d03935SAaron Smith ModuleSpec module_spec(file); 182b8d03935SAaron Smith ArchSpec &spec = module_spec.GetArchitecture(); 183b8d03935SAaron Smith lldb_private::UUID &uuid = module_spec.GetUUID(); 184b8d03935SAaron Smith if (!uuid.IsValid()) 185b8d03935SAaron Smith uuid = GetCoffUUID(COFFObj); 186b8d03935SAaron Smith 187b8d03935SAaron Smith switch (COFFObj->getMachine()) { 188b8d03935SAaron Smith case MachineAmd64: 189ad587ae4SZachary Turner spec.SetTriple("x86_64-pc-windows"); 190b8d03935SAaron Smith specs.Append(module_spec); 191b8d03935SAaron Smith break; 192b8d03935SAaron Smith case MachineX86: 193ad587ae4SZachary Turner spec.SetTriple("i386-pc-windows"); 194b8d03935SAaron Smith specs.Append(module_spec); 1955e6f4520SZachary Turner spec.SetTriple("i686-pc-windows"); 196b8d03935SAaron Smith specs.Append(module_spec); 197b8d03935SAaron Smith break; 198b8d03935SAaron Smith case MachineArmNt: 199544c8f48SMartin Storsjo spec.SetTriple("armv7-pc-windows"); 200b8d03935SAaron Smith specs.Append(module_spec); 201b8d03935SAaron Smith break; 202638f072fSMartin Storsjo case MachineArm64: 203674d5543SMartin Storsjo spec.SetTriple("aarch64-pc-windows"); 204638f072fSMartin Storsjo specs.Append(module_spec); 205638f072fSMartin Storsjo break; 206b8d03935SAaron Smith default: 207b8d03935SAaron Smith break; 20889eb1baeSVirgile Bello } 20989eb1baeSVirgile Bello 21089eb1baeSVirgile Bello return specs.GetSize() - initial_count; 211f4d6de6aSGreg Clayton } 212f4d6de6aSGreg Clayton 213b9c1b51eSKate Stone bool ObjectFilePECOFF::SaveCore(const lldb::ProcessSP &process_sp, 214f7d1893fSAdrian McCarthy const lldb_private::FileSpec &outfile, 21597206d57SZachary Turner lldb_private::Status &error) { 216f7d1893fSAdrian McCarthy return SaveMiniDump(process_sp, outfile, error); 217f7d1893fSAdrian McCarthy } 218f7d1893fSAdrian McCarthy 219b9c1b51eSKate Stone bool ObjectFilePECOFF::MagicBytesMatch(DataBufferSP &data_sp) { 2205ce9c565SGreg Clayton DataExtractor data(data_sp, eByteOrderLittle, 4); 221c7bece56SGreg Clayton lldb::offset_t offset = 0; 222f754f88fSGreg Clayton uint16_t magic = data.GetU16(&offset); 223f754f88fSGreg Clayton return magic == IMAGE_DOS_SIGNATURE; 224f754f88fSGreg Clayton } 225f754f88fSGreg Clayton 226b9c1b51eSKate Stone lldb::SymbolType ObjectFilePECOFF::MapSymbolType(uint16_t coff_symbol_type) { 227c35b91ceSAdrian McCarthy // TODO: We need to complete this mapping of COFF symbol types to LLDB ones. 228c35b91ceSAdrian McCarthy // For now, here's a hack to make sure our function have types. 229b9c1b51eSKate Stone const auto complex_type = 230b9c1b51eSKate Stone coff_symbol_type >> llvm::COFF::SCT_COMPLEX_TYPE_SHIFT; 231b9c1b51eSKate Stone if (complex_type == llvm::COFF::IMAGE_SYM_DTYPE_FUNCTION) { 232c35b91ceSAdrian McCarthy return lldb::eSymbolTypeCode; 233c35b91ceSAdrian McCarthy } 234c35b91ceSAdrian McCarthy return lldb::eSymbolTypeInvalid; 235c35b91ceSAdrian McCarthy } 236f754f88fSGreg Clayton 237037ed1beSAaron Smith bool ObjectFilePECOFF::CreateBinary() { 238037ed1beSAaron Smith if (m_owningbin) 239037ed1beSAaron Smith return true; 240037ed1beSAaron Smith 241037ed1beSAaron Smith Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT)); 242037ed1beSAaron Smith 243037ed1beSAaron Smith auto binary = llvm::object::createBinary(m_file.GetPath()); 244037ed1beSAaron Smith if (!binary) { 24563e5fb76SJonas Devlieghere LLDB_LOGF(log, 24663e5fb76SJonas Devlieghere "ObjectFilePECOFF::CreateBinary() - failed to create binary " 247037ed1beSAaron Smith "for file (%s): %s", 248037ed1beSAaron Smith m_file ? m_file.GetPath().c_str() : "<NULL>", 249037ed1beSAaron Smith errorToErrorCode(binary.takeError()).message().c_str()); 250037ed1beSAaron Smith return false; 251037ed1beSAaron Smith } 252037ed1beSAaron Smith 253037ed1beSAaron Smith // Make sure we only handle COFF format. 254037ed1beSAaron Smith if (!binary->getBinary()->isCOFF() && 255037ed1beSAaron Smith !binary->getBinary()->isCOFFImportFile()) 256037ed1beSAaron Smith return false; 257037ed1beSAaron Smith 258037ed1beSAaron Smith m_owningbin = OWNBINType(std::move(*binary)); 25963e5fb76SJonas Devlieghere LLDB_LOGF(log, 26063e5fb76SJonas Devlieghere "%p ObjectFilePECOFF::CreateBinary() module = %p (%s), file = " 261037ed1beSAaron Smith "%s, binary = %p (Bin = %p)", 26263e5fb76SJonas Devlieghere static_cast<void *>(this), static_cast<void *>(GetModule().get()), 263037ed1beSAaron Smith GetModule()->GetSpecificationDescription().c_str(), 264037ed1beSAaron Smith m_file ? m_file.GetPath().c_str() : "<NULL>", 265037ed1beSAaron Smith static_cast<void *>(m_owningbin.getPointer()), 266037ed1beSAaron Smith static_cast<void *>(m_owningbin->getBinary())); 267037ed1beSAaron Smith return true; 268037ed1beSAaron Smith } 269037ed1beSAaron Smith 270e72dfb32SGreg Clayton ObjectFilePECOFF::ObjectFilePECOFF(const lldb::ModuleSP &module_sp, 2715ce9c565SGreg Clayton DataBufferSP &data_sp, 2725ce9c565SGreg Clayton lldb::offset_t data_offset, 273f754f88fSGreg Clayton const FileSpec *file, 2745ce9c565SGreg Clayton lldb::offset_t file_offset, 275b9c1b51eSKate Stone lldb::offset_t length) 276b9c1b51eSKate Stone : ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset), 2775c43ffd6SPavel Labath m_dos_header(), m_coff_header(), m_sect_headers(), 278037ed1beSAaron Smith m_entry_point_address(), m_deps_filespec(), m_owningbin() { 279f754f88fSGreg Clayton ::memset(&m_dos_header, 0, sizeof(m_dos_header)); 280f754f88fSGreg Clayton ::memset(&m_coff_header, 0, sizeof(m_coff_header)); 281f754f88fSGreg Clayton } 282f754f88fSGreg Clayton 283344546bdSWalter Erquinigo ObjectFilePECOFF::ObjectFilePECOFF(const lldb::ModuleSP &module_sp, 284344546bdSWalter Erquinigo DataBufferSP &header_data_sp, 285344546bdSWalter Erquinigo const lldb::ProcessSP &process_sp, 286344546bdSWalter Erquinigo addr_t header_addr) 287344546bdSWalter Erquinigo : ObjectFile(module_sp, process_sp, header_addr, header_data_sp), 2885c43ffd6SPavel Labath m_dos_header(), m_coff_header(), m_sect_headers(), 289037ed1beSAaron Smith m_entry_point_address(), m_deps_filespec(), m_owningbin() { 290344546bdSWalter Erquinigo ::memset(&m_dos_header, 0, sizeof(m_dos_header)); 291344546bdSWalter Erquinigo ::memset(&m_coff_header, 0, sizeof(m_coff_header)); 292344546bdSWalter Erquinigo } 293344546bdSWalter Erquinigo 294b9c1b51eSKate Stone ObjectFilePECOFF::~ObjectFilePECOFF() {} 295f754f88fSGreg Clayton 296b9c1b51eSKate Stone bool ObjectFilePECOFF::ParseHeader() { 297a1743499SGreg Clayton ModuleSP module_sp(GetModule()); 298b9c1b51eSKate Stone if (module_sp) { 29916ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 300f754f88fSGreg Clayton m_sect_headers.clear(); 301f754f88fSGreg Clayton m_data.SetByteOrder(eByteOrderLittle); 302c7bece56SGreg Clayton lldb::offset_t offset = 0; 303f754f88fSGreg Clayton 304b9c1b51eSKate Stone if (ParseDOSHeader(m_data, m_dos_header)) { 305f754f88fSGreg Clayton offset = m_dos_header.e_lfanew; 306f754f88fSGreg Clayton uint32_t pe_signature = m_data.GetU32(&offset); 307f754f88fSGreg Clayton if (pe_signature != IMAGE_NT_SIGNATURE) 308f754f88fSGreg Clayton return false; 309b9c1b51eSKate Stone if (ParseCOFFHeader(m_data, &offset, m_coff_header)) { 310f754f88fSGreg Clayton if (m_coff_header.hdrsize > 0) 311f754f88fSGreg Clayton ParseCOFFOptionalHeader(&offset); 312f754f88fSGreg Clayton ParseSectionHeaders(offset); 31328469ca3SGreg Clayton } 314f754f88fSGreg Clayton return true; 315f754f88fSGreg Clayton } 316a1743499SGreg Clayton } 317f754f88fSGreg Clayton return false; 318f754f88fSGreg Clayton } 319f754f88fSGreg Clayton 320b9c1b51eSKate Stone bool ObjectFilePECOFF::SetLoadAddress(Target &target, addr_t value, 321b9c1b51eSKate Stone bool value_is_offset) { 3222756adf3SVirgile Bello bool changed = false; 3232756adf3SVirgile Bello ModuleSP module_sp = GetModule(); 324b9c1b51eSKate Stone if (module_sp) { 3252756adf3SVirgile Bello size_t num_loaded_sections = 0; 3262756adf3SVirgile Bello SectionList *section_list = GetSectionList(); 327b9c1b51eSKate Stone if (section_list) { 328b9c1b51eSKate Stone if (!value_is_offset) { 3292756adf3SVirgile Bello value -= m_image_base; 3302756adf3SVirgile Bello } 3312756adf3SVirgile Bello 3322756adf3SVirgile Bello const size_t num_sections = section_list->GetSize(); 3332756adf3SVirgile Bello size_t sect_idx = 0; 3342756adf3SVirgile Bello 335b9c1b51eSKate Stone for (sect_idx = 0; sect_idx < num_sections; ++sect_idx) { 33605097246SAdrian Prantl // Iterate through the object file sections to find all of the sections 33705097246SAdrian Prantl // that have SHF_ALLOC in their flag bits. 3382756adf3SVirgile Bello SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx)); 339b9c1b51eSKate Stone if (section_sp && !section_sp->IsThreadSpecific()) { 340b9c1b51eSKate Stone if (target.GetSectionLoadList().SetSectionLoadAddress( 341b9c1b51eSKate Stone section_sp, section_sp->GetFileAddress() + value)) 3422756adf3SVirgile Bello ++num_loaded_sections; 3432756adf3SVirgile Bello } 3442756adf3SVirgile Bello } 3452756adf3SVirgile Bello changed = num_loaded_sections > 0; 3462756adf3SVirgile Bello } 3472756adf3SVirgile Bello } 3482756adf3SVirgile Bello return changed; 3492756adf3SVirgile Bello } 3502756adf3SVirgile Bello 351b9c1b51eSKate Stone ByteOrder ObjectFilePECOFF::GetByteOrder() const { return eByteOrderLittle; } 352f754f88fSGreg Clayton 353b9c1b51eSKate Stone bool ObjectFilePECOFF::IsExecutable() const { 354237ad974SCharles Davis return (m_coff_header.flags & llvm::COFF::IMAGE_FILE_DLL) == 0; 355f754f88fSGreg Clayton } 356f754f88fSGreg Clayton 357b9c1b51eSKate Stone uint32_t ObjectFilePECOFF::GetAddressByteSize() const { 358f754f88fSGreg Clayton if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32_PLUS) 359f754f88fSGreg Clayton return 8; 360f754f88fSGreg Clayton else if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32) 361f754f88fSGreg Clayton return 4; 362f754f88fSGreg Clayton return 4; 363f754f88fSGreg Clayton } 364f754f88fSGreg Clayton 365f754f88fSGreg Clayton // NeedsEndianSwap 366f754f88fSGreg Clayton // 36705097246SAdrian Prantl // Return true if an endian swap needs to occur when extracting data from this 36805097246SAdrian Prantl // file. 369b9c1b51eSKate Stone bool ObjectFilePECOFF::NeedsEndianSwap() const { 370f754f88fSGreg Clayton #if defined(__LITTLE_ENDIAN__) 371f754f88fSGreg Clayton return false; 372f754f88fSGreg Clayton #else 373f754f88fSGreg Clayton return true; 374f754f88fSGreg Clayton #endif 375f754f88fSGreg Clayton } 376f754f88fSGreg Clayton // ParseDOSHeader 377b9c1b51eSKate Stone bool ObjectFilePECOFF::ParseDOSHeader(DataExtractor &data, 378b9c1b51eSKate Stone dos_header_t &dos_header) { 379f754f88fSGreg Clayton bool success = false; 380c7bece56SGreg Clayton lldb::offset_t offset = 0; 38189eb1baeSVirgile Bello success = data.ValidOffsetForDataOfSize(0, sizeof(dos_header)); 382f754f88fSGreg Clayton 383b9c1b51eSKate Stone if (success) { 38489eb1baeSVirgile Bello dos_header.e_magic = data.GetU16(&offset); // Magic number 38589eb1baeSVirgile Bello success = dos_header.e_magic == IMAGE_DOS_SIGNATURE; 386f754f88fSGreg Clayton 387b9c1b51eSKate Stone if (success) { 38889eb1baeSVirgile Bello dos_header.e_cblp = data.GetU16(&offset); // Bytes on last page of file 38989eb1baeSVirgile Bello dos_header.e_cp = data.GetU16(&offset); // Pages in file 39089eb1baeSVirgile Bello dos_header.e_crlc = data.GetU16(&offset); // Relocations 391b9c1b51eSKate Stone dos_header.e_cparhdr = 392b9c1b51eSKate Stone data.GetU16(&offset); // Size of header in paragraphs 393b9c1b51eSKate Stone dos_header.e_minalloc = 394b9c1b51eSKate Stone data.GetU16(&offset); // Minimum extra paragraphs needed 395b9c1b51eSKate Stone dos_header.e_maxalloc = 396b9c1b51eSKate Stone data.GetU16(&offset); // Maximum extra paragraphs needed 39789eb1baeSVirgile Bello dos_header.e_ss = data.GetU16(&offset); // Initial (relative) SS value 39889eb1baeSVirgile Bello dos_header.e_sp = data.GetU16(&offset); // Initial SP value 39989eb1baeSVirgile Bello dos_header.e_csum = data.GetU16(&offset); // Checksum 40089eb1baeSVirgile Bello dos_header.e_ip = data.GetU16(&offset); // Initial IP value 40189eb1baeSVirgile Bello dos_header.e_cs = data.GetU16(&offset); // Initial (relative) CS value 402b9c1b51eSKate Stone dos_header.e_lfarlc = 403b9c1b51eSKate Stone data.GetU16(&offset); // File address of relocation table 40489eb1baeSVirgile Bello dos_header.e_ovno = data.GetU16(&offset); // Overlay number 405f754f88fSGreg Clayton 40689eb1baeSVirgile Bello dos_header.e_res[0] = data.GetU16(&offset); // Reserved words 40789eb1baeSVirgile Bello dos_header.e_res[1] = data.GetU16(&offset); // Reserved words 40889eb1baeSVirgile Bello dos_header.e_res[2] = data.GetU16(&offset); // Reserved words 40989eb1baeSVirgile Bello dos_header.e_res[3] = data.GetU16(&offset); // Reserved words 410f754f88fSGreg Clayton 411b9c1b51eSKate Stone dos_header.e_oemid = 412b9c1b51eSKate Stone data.GetU16(&offset); // OEM identifier (for e_oeminfo) 413b9c1b51eSKate Stone dos_header.e_oeminfo = 414b9c1b51eSKate Stone data.GetU16(&offset); // OEM information; e_oemid specific 41589eb1baeSVirgile Bello dos_header.e_res2[0] = data.GetU16(&offset); // Reserved words 41689eb1baeSVirgile Bello dos_header.e_res2[1] = data.GetU16(&offset); // Reserved words 41789eb1baeSVirgile Bello dos_header.e_res2[2] = data.GetU16(&offset); // Reserved words 41889eb1baeSVirgile Bello dos_header.e_res2[3] = data.GetU16(&offset); // Reserved words 41989eb1baeSVirgile Bello dos_header.e_res2[4] = data.GetU16(&offset); // Reserved words 42089eb1baeSVirgile Bello dos_header.e_res2[5] = data.GetU16(&offset); // Reserved words 42189eb1baeSVirgile Bello dos_header.e_res2[6] = data.GetU16(&offset); // Reserved words 42289eb1baeSVirgile Bello dos_header.e_res2[7] = data.GetU16(&offset); // Reserved words 42389eb1baeSVirgile Bello dos_header.e_res2[8] = data.GetU16(&offset); // Reserved words 42489eb1baeSVirgile Bello dos_header.e_res2[9] = data.GetU16(&offset); // Reserved words 425f754f88fSGreg Clayton 426b9c1b51eSKate Stone dos_header.e_lfanew = 427b9c1b51eSKate Stone data.GetU32(&offset); // File address of new exe header 428f754f88fSGreg Clayton } 429f754f88fSGreg Clayton } 430f754f88fSGreg Clayton if (!success) 43189eb1baeSVirgile Bello memset(&dos_header, 0, sizeof(dos_header)); 432f754f88fSGreg Clayton return success; 433f754f88fSGreg Clayton } 434f754f88fSGreg Clayton 435f754f88fSGreg Clayton // ParserCOFFHeader 436b9c1b51eSKate Stone bool ObjectFilePECOFF::ParseCOFFHeader(DataExtractor &data, 437b9c1b51eSKate Stone lldb::offset_t *offset_ptr, 438b9c1b51eSKate Stone coff_header_t &coff_header) { 439b9c1b51eSKate Stone bool success = 440b9c1b51eSKate Stone data.ValidOffsetForDataOfSize(*offset_ptr, sizeof(coff_header)); 441b9c1b51eSKate Stone if (success) { 44289eb1baeSVirgile Bello coff_header.machine = data.GetU16(offset_ptr); 44389eb1baeSVirgile Bello coff_header.nsects = data.GetU16(offset_ptr); 44489eb1baeSVirgile Bello coff_header.modtime = data.GetU32(offset_ptr); 44589eb1baeSVirgile Bello coff_header.symoff = data.GetU32(offset_ptr); 44689eb1baeSVirgile Bello coff_header.nsyms = data.GetU32(offset_ptr); 44789eb1baeSVirgile Bello coff_header.hdrsize = data.GetU16(offset_ptr); 44889eb1baeSVirgile Bello coff_header.flags = data.GetU16(offset_ptr); 449f754f88fSGreg Clayton } 450f754f88fSGreg Clayton if (!success) 45189eb1baeSVirgile Bello memset(&coff_header, 0, sizeof(coff_header)); 452f754f88fSGreg Clayton return success; 453f754f88fSGreg Clayton } 454f754f88fSGreg Clayton 455b9c1b51eSKate Stone bool ObjectFilePECOFF::ParseCOFFOptionalHeader(lldb::offset_t *offset_ptr) { 456f754f88fSGreg Clayton bool success = false; 457c7bece56SGreg Clayton const lldb::offset_t end_offset = *offset_ptr + m_coff_header.hdrsize; 458b9c1b51eSKate Stone if (*offset_ptr < end_offset) { 459f754f88fSGreg Clayton success = true; 460f754f88fSGreg Clayton m_coff_header_opt.magic = m_data.GetU16(offset_ptr); 461f754f88fSGreg Clayton m_coff_header_opt.major_linker_version = m_data.GetU8(offset_ptr); 462f754f88fSGreg Clayton m_coff_header_opt.minor_linker_version = m_data.GetU8(offset_ptr); 463f754f88fSGreg Clayton m_coff_header_opt.code_size = m_data.GetU32(offset_ptr); 464f754f88fSGreg Clayton m_coff_header_opt.data_size = m_data.GetU32(offset_ptr); 465f754f88fSGreg Clayton m_coff_header_opt.bss_size = m_data.GetU32(offset_ptr); 466f754f88fSGreg Clayton m_coff_header_opt.entry = m_data.GetU32(offset_ptr); 467f754f88fSGreg Clayton m_coff_header_opt.code_offset = m_data.GetU32(offset_ptr); 468f754f88fSGreg Clayton 469f754f88fSGreg Clayton const uint32_t addr_byte_size = GetAddressByteSize(); 470f754f88fSGreg Clayton 471b9c1b51eSKate Stone if (*offset_ptr < end_offset) { 472b9c1b51eSKate Stone if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32) { 473f754f88fSGreg Clayton // PE32 only 474f754f88fSGreg Clayton m_coff_header_opt.data_offset = m_data.GetU32(offset_ptr); 475b9c1b51eSKate Stone } else 476f754f88fSGreg Clayton m_coff_header_opt.data_offset = 0; 477f754f88fSGreg Clayton 478b9c1b51eSKate Stone if (*offset_ptr < end_offset) { 479b9c1b51eSKate Stone m_coff_header_opt.image_base = 480b9c1b51eSKate Stone m_data.GetMaxU64(offset_ptr, addr_byte_size); 481f754f88fSGreg Clayton m_coff_header_opt.sect_alignment = m_data.GetU32(offset_ptr); 482f754f88fSGreg Clayton m_coff_header_opt.file_alignment = m_data.GetU32(offset_ptr); 483f754f88fSGreg Clayton m_coff_header_opt.major_os_system_version = m_data.GetU16(offset_ptr); 484f754f88fSGreg Clayton m_coff_header_opt.minor_os_system_version = m_data.GetU16(offset_ptr); 485f754f88fSGreg Clayton m_coff_header_opt.major_image_version = m_data.GetU16(offset_ptr); 486f754f88fSGreg Clayton m_coff_header_opt.minor_image_version = m_data.GetU16(offset_ptr); 487f754f88fSGreg Clayton m_coff_header_opt.major_subsystem_version = m_data.GetU16(offset_ptr); 488f754f88fSGreg Clayton m_coff_header_opt.minor_subsystem_version = m_data.GetU16(offset_ptr); 489f754f88fSGreg Clayton m_coff_header_opt.reserved1 = m_data.GetU32(offset_ptr); 490f754f88fSGreg Clayton m_coff_header_opt.image_size = m_data.GetU32(offset_ptr); 491f754f88fSGreg Clayton m_coff_header_opt.header_size = m_data.GetU32(offset_ptr); 49228469ca3SGreg Clayton m_coff_header_opt.checksum = m_data.GetU32(offset_ptr); 493f754f88fSGreg Clayton m_coff_header_opt.subsystem = m_data.GetU16(offset_ptr); 494f754f88fSGreg Clayton m_coff_header_opt.dll_flags = m_data.GetU16(offset_ptr); 495b9c1b51eSKate Stone m_coff_header_opt.stack_reserve_size = 496b9c1b51eSKate Stone m_data.GetMaxU64(offset_ptr, addr_byte_size); 497b9c1b51eSKate Stone m_coff_header_opt.stack_commit_size = 498b9c1b51eSKate Stone m_data.GetMaxU64(offset_ptr, addr_byte_size); 499b9c1b51eSKate Stone m_coff_header_opt.heap_reserve_size = 500b9c1b51eSKate Stone m_data.GetMaxU64(offset_ptr, addr_byte_size); 501b9c1b51eSKate Stone m_coff_header_opt.heap_commit_size = 502b9c1b51eSKate Stone m_data.GetMaxU64(offset_ptr, addr_byte_size); 503f754f88fSGreg Clayton m_coff_header_opt.loader_flags = m_data.GetU32(offset_ptr); 504f754f88fSGreg Clayton uint32_t num_data_dir_entries = m_data.GetU32(offset_ptr); 505f754f88fSGreg Clayton m_coff_header_opt.data_dirs.clear(); 506f754f88fSGreg Clayton m_coff_header_opt.data_dirs.resize(num_data_dir_entries); 507f754f88fSGreg Clayton uint32_t i; 508b9c1b51eSKate Stone for (i = 0; i < num_data_dir_entries; i++) { 509f754f88fSGreg Clayton m_coff_header_opt.data_dirs[i].vmaddr = m_data.GetU32(offset_ptr); 510f754f88fSGreg Clayton m_coff_header_opt.data_dirs[i].vmsize = m_data.GetU32(offset_ptr); 511f754f88fSGreg Clayton } 5122756adf3SVirgile Bello 5132756adf3SVirgile Bello m_image_base = m_coff_header_opt.image_base; 514f754f88fSGreg Clayton } 515f754f88fSGreg Clayton } 516f754f88fSGreg Clayton } 517f754f88fSGreg Clayton // Make sure we are on track for section data which follows 518f754f88fSGreg Clayton *offset_ptr = end_offset; 519f754f88fSGreg Clayton return success; 520f754f88fSGreg Clayton } 521f754f88fSGreg Clayton 52230c2441aSAleksandr Urakov uint32_t ObjectFilePECOFF::GetRVA(const Address &addr) const { 52330c2441aSAleksandr Urakov return addr.GetFileAddress() - m_image_base; 52430c2441aSAleksandr Urakov } 52530c2441aSAleksandr Urakov 52630c2441aSAleksandr Urakov Address ObjectFilePECOFF::GetAddress(uint32_t rva) { 52730c2441aSAleksandr Urakov SectionList *sect_list = GetSectionList(); 52830c2441aSAleksandr Urakov if (!sect_list) 52930c2441aSAleksandr Urakov return Address(GetFileAddress(rva)); 53030c2441aSAleksandr Urakov 53130c2441aSAleksandr Urakov return Address(GetFileAddress(rva), sect_list); 53230c2441aSAleksandr Urakov } 53330c2441aSAleksandr Urakov 53430c2441aSAleksandr Urakov lldb::addr_t ObjectFilePECOFF::GetFileAddress(uint32_t rva) const { 53530c2441aSAleksandr Urakov return m_image_base + rva; 53630c2441aSAleksandr Urakov } 53730c2441aSAleksandr Urakov 538344546bdSWalter Erquinigo DataExtractor ObjectFilePECOFF::ReadImageData(uint32_t offset, size_t size) { 53930c2441aSAleksandr Urakov if (!size) 54030c2441aSAleksandr Urakov return {}; 54130c2441aSAleksandr Urakov 542344546bdSWalter Erquinigo if (m_file) { 5437f6a7a37SZachary Turner // A bit of a hack, but we intend to write to this buffer, so we can't 5447f6a7a37SZachary Turner // mmap it. 54550251fc7SPavel Labath auto buffer_sp = MapFileData(m_file, size, offset); 546344546bdSWalter Erquinigo return DataExtractor(buffer_sp, GetByteOrder(), GetAddressByteSize()); 547344546bdSWalter Erquinigo } 548344546bdSWalter Erquinigo ProcessSP process_sp(m_process_wp.lock()); 549344546bdSWalter Erquinigo DataExtractor data; 550344546bdSWalter Erquinigo if (process_sp) { 551a8f3ae7cSJonas Devlieghere auto data_up = std::make_unique<DataBufferHeap>(size, 0); 55297206d57SZachary Turner Status readmem_error; 553344546bdSWalter Erquinigo size_t bytes_read = 554d5b44036SJonas Devlieghere process_sp->ReadMemory(m_image_base + offset, data_up->GetBytes(), 555d5b44036SJonas Devlieghere data_up->GetByteSize(), readmem_error); 556344546bdSWalter Erquinigo if (bytes_read == size) { 557d5b44036SJonas Devlieghere DataBufferSP buffer_sp(data_up.release()); 558344546bdSWalter Erquinigo data.SetData(buffer_sp, 0, buffer_sp->GetByteSize()); 559344546bdSWalter Erquinigo } 560344546bdSWalter Erquinigo } 561344546bdSWalter Erquinigo return data; 562344546bdSWalter Erquinigo } 563344546bdSWalter Erquinigo 56430c2441aSAleksandr Urakov DataExtractor ObjectFilePECOFF::ReadImageDataByRVA(uint32_t rva, size_t size) { 56530c2441aSAleksandr Urakov if (m_file) { 56630c2441aSAleksandr Urakov Address addr = GetAddress(rva); 567*7e1a3076SMartin Storsjö SectionSP sect = addr.GetSection(); 568*7e1a3076SMartin Storsjö if (!sect) 569*7e1a3076SMartin Storsjö return {}; 570*7e1a3076SMartin Storsjö rva = sect->GetFileOffset() + addr.GetOffset(); 57130c2441aSAleksandr Urakov } 57230c2441aSAleksandr Urakov 57330c2441aSAleksandr Urakov return ReadImageData(rva, size); 57430c2441aSAleksandr Urakov } 57530c2441aSAleksandr Urakov 576f754f88fSGreg Clayton // ParseSectionHeaders 577b9c1b51eSKate Stone bool ObjectFilePECOFF::ParseSectionHeaders( 578b9c1b51eSKate Stone uint32_t section_header_data_offset) { 579f754f88fSGreg Clayton const uint32_t nsects = m_coff_header.nsects; 580f754f88fSGreg Clayton m_sect_headers.clear(); 581f754f88fSGreg Clayton 582b9c1b51eSKate Stone if (nsects > 0) { 583f754f88fSGreg Clayton const size_t section_header_byte_size = nsects * sizeof(section_header_t); 584344546bdSWalter Erquinigo DataExtractor section_header_data = 585344546bdSWalter Erquinigo ReadImageData(section_header_data_offset, section_header_byte_size); 586f754f88fSGreg Clayton 587c7bece56SGreg Clayton lldb::offset_t offset = 0; 588b9c1b51eSKate Stone if (section_header_data.ValidOffsetForDataOfSize( 589b9c1b51eSKate Stone offset, section_header_byte_size)) { 590f754f88fSGreg Clayton m_sect_headers.resize(nsects); 591f754f88fSGreg Clayton 592b9c1b51eSKate Stone for (uint32_t idx = 0; idx < nsects; ++idx) { 593f754f88fSGreg Clayton const void *name_data = section_header_data.GetData(&offset, 8); 594b9c1b51eSKate Stone if (name_data) { 595f754f88fSGreg Clayton memcpy(m_sect_headers[idx].name, name_data, 8); 596f754f88fSGreg Clayton m_sect_headers[idx].vmsize = section_header_data.GetU32(&offset); 597f754f88fSGreg Clayton m_sect_headers[idx].vmaddr = section_header_data.GetU32(&offset); 598f754f88fSGreg Clayton m_sect_headers[idx].size = section_header_data.GetU32(&offset); 599f754f88fSGreg Clayton m_sect_headers[idx].offset = section_header_data.GetU32(&offset); 600f754f88fSGreg Clayton m_sect_headers[idx].reloff = section_header_data.GetU32(&offset); 601f754f88fSGreg Clayton m_sect_headers[idx].lineoff = section_header_data.GetU32(&offset); 602f754f88fSGreg Clayton m_sect_headers[idx].nreloc = section_header_data.GetU16(&offset); 603f754f88fSGreg Clayton m_sect_headers[idx].nline = section_header_data.GetU16(&offset); 604f754f88fSGreg Clayton m_sect_headers[idx].flags = section_header_data.GetU32(&offset); 605f754f88fSGreg Clayton } 606f754f88fSGreg Clayton } 607f754f88fSGreg Clayton } 608f754f88fSGreg Clayton } 609f754f88fSGreg Clayton 610a6682a41SJonas Devlieghere return !m_sect_headers.empty(); 611f754f88fSGreg Clayton } 612f754f88fSGreg Clayton 6132886e4a0SPavel Labath llvm::StringRef ObjectFilePECOFF::GetSectionName(const section_header_t §) { 6142886e4a0SPavel Labath llvm::StringRef hdr_name(sect.name, llvm::array_lengthof(sect.name)); 6152886e4a0SPavel Labath hdr_name = hdr_name.split('\0').first; 6162886e4a0SPavel Labath if (hdr_name.consume_front("/")) { 6172886e4a0SPavel Labath lldb::offset_t stroff; 6182886e4a0SPavel Labath if (!to_integer(hdr_name, stroff, 10)) 6192886e4a0SPavel Labath return ""; 620b9c1b51eSKate Stone lldb::offset_t string_file_offset = 621b9c1b51eSKate Stone m_coff_header.symoff + (m_coff_header.nsyms * 18) + stroff; 6222886e4a0SPavel Labath if (const char *name = m_data.GetCStr(&string_file_offset)) 6232886e4a0SPavel Labath return name; 6242886e4a0SPavel Labath return ""; 625f754f88fSGreg Clayton } 6262886e4a0SPavel Labath return hdr_name; 627f754f88fSGreg Clayton } 628f754f88fSGreg Clayton 629f754f88fSGreg Clayton // GetNListSymtab 630b9c1b51eSKate Stone Symtab *ObjectFilePECOFF::GetSymtab() { 631a1743499SGreg Clayton ModuleSP module_sp(GetModule()); 632b9c1b51eSKate Stone if (module_sp) { 63316ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 634248a1305SKonrad Kleine if (m_symtab_up == nullptr) { 635f754f88fSGreg Clayton SectionList *sect_list = GetSectionList(); 636d5b44036SJonas Devlieghere m_symtab_up.reset(new Symtab(this)); 637d5b44036SJonas Devlieghere std::lock_guard<std::recursive_mutex> guard(m_symtab_up->GetMutex()); 63828469ca3SGreg Clayton 63928469ca3SGreg Clayton const uint32_t num_syms = m_coff_header.nsyms; 64028469ca3SGreg Clayton 641344546bdSWalter Erquinigo if (m_file && num_syms > 0 && m_coff_header.symoff > 0) { 6420076e715SGreg Clayton const uint32_t symbol_size = 18; 64328469ca3SGreg Clayton const size_t symbol_data_size = num_syms * symbol_size; 644c35b91ceSAdrian McCarthy // Include the 4-byte string table size at the end of the symbols 645344546bdSWalter Erquinigo DataExtractor symtab_data = 646344546bdSWalter Erquinigo ReadImageData(m_coff_header.symoff, symbol_data_size + 4); 647c7bece56SGreg Clayton lldb::offset_t offset = symbol_data_size; 64828469ca3SGreg Clayton const uint32_t strtab_size = symtab_data.GetU32(&offset); 649344546bdSWalter Erquinigo if (strtab_size > 0) { 650344546bdSWalter Erquinigo DataExtractor strtab_data = ReadImageData( 651344546bdSWalter Erquinigo m_coff_header.symoff + symbol_data_size, strtab_size); 65228469ca3SGreg Clayton 6530076e715SGreg Clayton // First 4 bytes should be zeroed after strtab_size has been read, 6540076e715SGreg Clayton // because it is used as offset 0 to encode a NULL string. 655f76e099cSSaleem Abdulrasool uint32_t *strtab_data_start = const_cast<uint32_t *>( 656f76e099cSSaleem Abdulrasool reinterpret_cast<const uint32_t *>(strtab_data.GetDataStart())); 6570076e715SGreg Clayton strtab_data_start[0] = 0; 6580076e715SGreg Clayton 65928469ca3SGreg Clayton offset = 0; 66028469ca3SGreg Clayton std::string symbol_name; 661d5b44036SJonas Devlieghere Symbol *symbols = m_symtab_up->Resize(num_syms); 662b9c1b51eSKate Stone for (uint32_t i = 0; i < num_syms; ++i) { 663f754f88fSGreg Clayton coff_symbol_t symbol; 66428469ca3SGreg Clayton const uint32_t symbol_offset = offset; 665248a1305SKonrad Kleine const char *symbol_name_cstr = nullptr; 666c35b91ceSAdrian McCarthy // If the first 4 bytes of the symbol string are zero, then they 667c35b91ceSAdrian McCarthy // are followed by a 4-byte string table offset. Else these 66828469ca3SGreg Clayton // 8 bytes contain the symbol name 669b9c1b51eSKate Stone if (symtab_data.GetU32(&offset) == 0) { 67005097246SAdrian Prantl // Long string that doesn't fit into the symbol table name, so 67105097246SAdrian Prantl // now we must read the 4 byte string table offset 67228469ca3SGreg Clayton uint32_t strtab_offset = symtab_data.GetU32(&offset); 67328469ca3SGreg Clayton symbol_name_cstr = strtab_data.PeekCStr(strtab_offset); 67428469ca3SGreg Clayton symbol_name.assign(symbol_name_cstr); 675b9c1b51eSKate Stone } else { 676b9c1b51eSKate Stone // Short string that fits into the symbol table name which is 8 677b9c1b51eSKate Stone // bytes 67828469ca3SGreg Clayton offset += sizeof(symbol.name) - 4; // Skip remaining 67928469ca3SGreg Clayton symbol_name_cstr = symtab_data.PeekCStr(symbol_offset); 680248a1305SKonrad Kleine if (symbol_name_cstr == nullptr) 681f754f88fSGreg Clayton break; 68228469ca3SGreg Clayton symbol_name.assign(symbol_name_cstr, sizeof(symbol.name)); 68328469ca3SGreg Clayton } 68428469ca3SGreg Clayton symbol.value = symtab_data.GetU32(&offset); 68528469ca3SGreg Clayton symbol.sect = symtab_data.GetU16(&offset); 68628469ca3SGreg Clayton symbol.type = symtab_data.GetU16(&offset); 68728469ca3SGreg Clayton symbol.storage = symtab_data.GetU8(&offset); 68828469ca3SGreg Clayton symbol.naux = symtab_data.GetU8(&offset); 689037520e9SGreg Clayton symbols[i].GetMangled().SetValue(ConstString(symbol_name.c_str())); 690b9c1b51eSKate Stone if ((int16_t)symbol.sect >= 1) { 6914394b5beSMartin Storsjö Address symbol_addr(sect_list->FindSectionByID(symbol.sect), 692b9c1b51eSKate Stone symbol.value); 693358cf1eaSGreg Clayton symbols[i].GetAddressRef() = symbol_addr; 694c35b91ceSAdrian McCarthy symbols[i].SetType(MapSymbolType(symbol.type)); 6950076e715SGreg Clayton } 696f754f88fSGreg Clayton 697b9c1b51eSKate Stone if (symbol.naux > 0) { 698f754f88fSGreg Clayton i += symbol.naux; 6990076e715SGreg Clayton offset += symbol_size; 7000076e715SGreg Clayton } 701f754f88fSGreg Clayton } 702f754f88fSGreg Clayton } 703344546bdSWalter Erquinigo } 704a4fe3a12SVirgile Bello 705a4fe3a12SVirgile Bello // Read export header 706b9c1b51eSKate Stone if (coff_data_dir_export_table < m_coff_header_opt.data_dirs.size() && 707b9c1b51eSKate Stone m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmsize > 0 && 708b9c1b51eSKate Stone m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmaddr > 0) { 709a4fe3a12SVirgile Bello export_directory_entry export_table; 710b9c1b51eSKate Stone uint32_t data_start = 711b9c1b51eSKate Stone m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmaddr; 712344546bdSWalter Erquinigo 71330c2441aSAleksandr Urakov DataExtractor symtab_data = ReadImageDataByRVA( 71430c2441aSAleksandr Urakov data_start, m_coff_header_opt.data_dirs[0].vmsize); 715a4fe3a12SVirgile Bello lldb::offset_t offset = 0; 716a4fe3a12SVirgile Bello 717a4fe3a12SVirgile Bello // Read export_table header 718a4fe3a12SVirgile Bello export_table.characteristics = symtab_data.GetU32(&offset); 719a4fe3a12SVirgile Bello export_table.time_date_stamp = symtab_data.GetU32(&offset); 720a4fe3a12SVirgile Bello export_table.major_version = symtab_data.GetU16(&offset); 721a4fe3a12SVirgile Bello export_table.minor_version = symtab_data.GetU16(&offset); 722a4fe3a12SVirgile Bello export_table.name = symtab_data.GetU32(&offset); 723a4fe3a12SVirgile Bello export_table.base = symtab_data.GetU32(&offset); 724a4fe3a12SVirgile Bello export_table.number_of_functions = symtab_data.GetU32(&offset); 725a4fe3a12SVirgile Bello export_table.number_of_names = symtab_data.GetU32(&offset); 726a4fe3a12SVirgile Bello export_table.address_of_functions = symtab_data.GetU32(&offset); 727a4fe3a12SVirgile Bello export_table.address_of_names = symtab_data.GetU32(&offset); 728a4fe3a12SVirgile Bello export_table.address_of_name_ordinals = symtab_data.GetU32(&offset); 729a4fe3a12SVirgile Bello 730a4fe3a12SVirgile Bello bool has_ordinal = export_table.address_of_name_ordinals != 0; 731a4fe3a12SVirgile Bello 732a4fe3a12SVirgile Bello lldb::offset_t name_offset = export_table.address_of_names - data_start; 733b9c1b51eSKate Stone lldb::offset_t name_ordinal_offset = 734b9c1b51eSKate Stone export_table.address_of_name_ordinals - data_start; 735a4fe3a12SVirgile Bello 736d5b44036SJonas Devlieghere Symbol *symbols = m_symtab_up->Resize(export_table.number_of_names); 737a4fe3a12SVirgile Bello 738a4fe3a12SVirgile Bello std::string symbol_name; 739a4fe3a12SVirgile Bello 740a4fe3a12SVirgile Bello // Read each export table entry 741b9c1b51eSKate Stone for (size_t i = 0; i < export_table.number_of_names; ++i) { 742b9c1b51eSKate Stone uint32_t name_ordinal = 743b9c1b51eSKate Stone has_ordinal ? symtab_data.GetU16(&name_ordinal_offset) : i; 744a4fe3a12SVirgile Bello uint32_t name_address = symtab_data.GetU32(&name_offset); 745a4fe3a12SVirgile Bello 746b9c1b51eSKate Stone const char *symbol_name_cstr = 747b9c1b51eSKate Stone symtab_data.PeekCStr(name_address - data_start); 748a4fe3a12SVirgile Bello symbol_name.assign(symbol_name_cstr); 749a4fe3a12SVirgile Bello 750b9c1b51eSKate Stone lldb::offset_t function_offset = export_table.address_of_functions - 751b9c1b51eSKate Stone data_start + 752b9c1b51eSKate Stone sizeof(uint32_t) * name_ordinal; 753a4fe3a12SVirgile Bello uint32_t function_rva = symtab_data.GetU32(&function_offset); 754a4fe3a12SVirgile Bello 755b9c1b51eSKate Stone Address symbol_addr(m_coff_header_opt.image_base + function_rva, 756b9c1b51eSKate Stone sect_list); 757a4fe3a12SVirgile Bello symbols[i].GetMangled().SetValue(ConstString(symbol_name.c_str())); 758358cf1eaSGreg Clayton symbols[i].GetAddressRef() = symbol_addr; 759a4fe3a12SVirgile Bello symbols[i].SetType(lldb::eSymbolTypeCode); 760a4fe3a12SVirgile Bello symbols[i].SetDebug(true); 761a4fe3a12SVirgile Bello } 762a4fe3a12SVirgile Bello } 763d5b44036SJonas Devlieghere m_symtab_up->CalculateSymbolSizes(); 764f754f88fSGreg Clayton } 765a1743499SGreg Clayton } 766d5b44036SJonas Devlieghere return m_symtab_up.get(); 767f754f88fSGreg Clayton } 768f754f88fSGreg Clayton 76930c2441aSAleksandr Urakov std::unique_ptr<CallFrameInfo> ObjectFilePECOFF::CreateCallFrameInfo() { 77030c2441aSAleksandr Urakov if (coff_data_dir_exception_table >= m_coff_header_opt.data_dirs.size()) 77130c2441aSAleksandr Urakov return {}; 77230c2441aSAleksandr Urakov 77330c2441aSAleksandr Urakov data_directory data_dir_exception = 77430c2441aSAleksandr Urakov m_coff_header_opt.data_dirs[coff_data_dir_exception_table]; 77530c2441aSAleksandr Urakov if (!data_dir_exception.vmaddr) 77630c2441aSAleksandr Urakov return {}; 77730c2441aSAleksandr Urakov 77830c2441aSAleksandr Urakov return std::make_unique<PECallFrameInfo>(*this, data_dir_exception.vmaddr, 77930c2441aSAleksandr Urakov data_dir_exception.vmsize); 78030c2441aSAleksandr Urakov } 78130c2441aSAleksandr Urakov 782b9c1b51eSKate Stone bool ObjectFilePECOFF::IsStripped() { 7833046e668SGreg Clayton // TODO: determine this for COFF 7843046e668SGreg Clayton return false; 7853046e668SGreg Clayton } 7863046e668SGreg Clayton 787b9c1b51eSKate Stone void ObjectFilePECOFF::CreateSections(SectionList &unified_section_list) { 788d5b44036SJonas Devlieghere if (m_sections_up) 78988a2c2a4SPavel Labath return; 790d5b44036SJonas Devlieghere m_sections_up.reset(new SectionList()); 7913046e668SGreg Clayton 792a1743499SGreg Clayton ModuleSP module_sp(GetModule()); 793b9c1b51eSKate Stone if (module_sp) { 79416ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 7957db8b5c4SPavel Labath 79673a7a55cSPavel Labath SectionSP header_sp = std::make_shared<Section>( 79773a7a55cSPavel Labath module_sp, this, ~user_id_t(0), ConstString("PECOFF header"), 79873a7a55cSPavel Labath eSectionTypeOther, m_coff_header_opt.image_base, 79973a7a55cSPavel Labath m_coff_header_opt.header_size, 80073a7a55cSPavel Labath /*file_offset*/ 0, m_coff_header_opt.header_size, 80173a7a55cSPavel Labath m_coff_header_opt.sect_alignment, 8027db8b5c4SPavel Labath /*flags*/ 0); 803f1e0ae34SPavel Labath header_sp->SetPermissions(ePermissionsReadable); 80473a7a55cSPavel Labath m_sections_up->AddSection(header_sp); 80573a7a55cSPavel Labath unified_section_list.AddSection(header_sp); 8067db8b5c4SPavel Labath 807f754f88fSGreg Clayton const uint32_t nsects = m_sect_headers.size(); 808e72dfb32SGreg Clayton ModuleSP module_sp(GetModule()); 809b9c1b51eSKate Stone for (uint32_t idx = 0; idx < nsects; ++idx) { 8102886e4a0SPavel Labath ConstString const_sect_name(GetSectionName(m_sect_headers[idx])); 81128469ca3SGreg Clayton static ConstString g_code_sect_name(".code"); 81228469ca3SGreg Clayton static ConstString g_CODE_sect_name("CODE"); 81328469ca3SGreg Clayton static ConstString g_data_sect_name(".data"); 81428469ca3SGreg Clayton static ConstString g_DATA_sect_name("DATA"); 81528469ca3SGreg Clayton static ConstString g_bss_sect_name(".bss"); 81628469ca3SGreg Clayton static ConstString g_BSS_sect_name("BSS"); 81728469ca3SGreg Clayton static ConstString g_debug_sect_name(".debug"); 81828469ca3SGreg Clayton static ConstString g_reloc_sect_name(".reloc"); 81928469ca3SGreg Clayton static ConstString g_stab_sect_name(".stab"); 82028469ca3SGreg Clayton static ConstString g_stabstr_sect_name(".stabstr"); 8210076e715SGreg Clayton static ConstString g_sect_name_dwarf_debug_abbrev(".debug_abbrev"); 8220076e715SGreg Clayton static ConstString g_sect_name_dwarf_debug_aranges(".debug_aranges"); 8230076e715SGreg Clayton static ConstString g_sect_name_dwarf_debug_frame(".debug_frame"); 8240076e715SGreg Clayton static ConstString g_sect_name_dwarf_debug_info(".debug_info"); 8250076e715SGreg Clayton static ConstString g_sect_name_dwarf_debug_line(".debug_line"); 8260076e715SGreg Clayton static ConstString g_sect_name_dwarf_debug_loc(".debug_loc"); 827e4dee269SGeorge Rimar static ConstString g_sect_name_dwarf_debug_loclists(".debug_loclists"); 8280076e715SGreg Clayton static ConstString g_sect_name_dwarf_debug_macinfo(".debug_macinfo"); 829a041d848SPavel Labath static ConstString g_sect_name_dwarf_debug_names(".debug_names"); 8300076e715SGreg Clayton static ConstString g_sect_name_dwarf_debug_pubnames(".debug_pubnames"); 8310076e715SGreg Clayton static ConstString g_sect_name_dwarf_debug_pubtypes(".debug_pubtypes"); 8320076e715SGreg Clayton static ConstString g_sect_name_dwarf_debug_ranges(".debug_ranges"); 8330076e715SGreg Clayton static ConstString g_sect_name_dwarf_debug_str(".debug_str"); 8342550ca1eSGreg Clayton static ConstString g_sect_name_dwarf_debug_types(".debug_types"); 8350076e715SGreg Clayton static ConstString g_sect_name_eh_frame(".eh_frame"); 83665d4d5c3SRyan Brown static ConstString g_sect_name_go_symtab(".gosymtab"); 83728469ca3SGreg Clayton SectionType section_type = eSectionTypeOther; 838237ad974SCharles Davis if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_CODE && 839b9c1b51eSKate Stone ((const_sect_name == g_code_sect_name) || 840b9c1b51eSKate Stone (const_sect_name == g_CODE_sect_name))) { 84128469ca3SGreg Clayton section_type = eSectionTypeCode; 842b9c1b51eSKate Stone } else if (m_sect_headers[idx].flags & 843b9c1b51eSKate Stone llvm::COFF::IMAGE_SCN_CNT_INITIALIZED_DATA && 844b9c1b51eSKate Stone ((const_sect_name == g_data_sect_name) || 845b9c1b51eSKate Stone (const_sect_name == g_DATA_sect_name))) { 8469cad24a7SZachary Turner if (m_sect_headers[idx].size == 0 && m_sect_headers[idx].offset == 0) 8479cad24a7SZachary Turner section_type = eSectionTypeZeroFill; 8489cad24a7SZachary Turner else 84928469ca3SGreg Clayton section_type = eSectionTypeData; 850b9c1b51eSKate Stone } else if (m_sect_headers[idx].flags & 851b9c1b51eSKate Stone llvm::COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA && 852b9c1b51eSKate Stone ((const_sect_name == g_bss_sect_name) || 853b9c1b51eSKate Stone (const_sect_name == g_BSS_sect_name))) { 85428469ca3SGreg Clayton if (m_sect_headers[idx].size == 0) 85528469ca3SGreg Clayton section_type = eSectionTypeZeroFill; 85628469ca3SGreg Clayton else 85728469ca3SGreg Clayton section_type = eSectionTypeData; 858b9c1b51eSKate Stone } else if (const_sect_name == g_debug_sect_name) { 85928469ca3SGreg Clayton section_type = eSectionTypeDebug; 860b9c1b51eSKate Stone } else if (const_sect_name == g_stabstr_sect_name) { 86128469ca3SGreg Clayton section_type = eSectionTypeDataCString; 862b9c1b51eSKate Stone } else if (const_sect_name == g_reloc_sect_name) { 86328469ca3SGreg Clayton section_type = eSectionTypeOther; 864b9c1b51eSKate Stone } else if (const_sect_name == g_sect_name_dwarf_debug_abbrev) 865b9c1b51eSKate Stone section_type = eSectionTypeDWARFDebugAbbrev; 866b9c1b51eSKate Stone else if (const_sect_name == g_sect_name_dwarf_debug_aranges) 867b9c1b51eSKate Stone section_type = eSectionTypeDWARFDebugAranges; 868b9c1b51eSKate Stone else if (const_sect_name == g_sect_name_dwarf_debug_frame) 869b9c1b51eSKate Stone section_type = eSectionTypeDWARFDebugFrame; 870b9c1b51eSKate Stone else if (const_sect_name == g_sect_name_dwarf_debug_info) 871b9c1b51eSKate Stone section_type = eSectionTypeDWARFDebugInfo; 872b9c1b51eSKate Stone else if (const_sect_name == g_sect_name_dwarf_debug_line) 873b9c1b51eSKate Stone section_type = eSectionTypeDWARFDebugLine; 874b9c1b51eSKate Stone else if (const_sect_name == g_sect_name_dwarf_debug_loc) 875b9c1b51eSKate Stone section_type = eSectionTypeDWARFDebugLoc; 876e4dee269SGeorge Rimar else if (const_sect_name == g_sect_name_dwarf_debug_loclists) 877e4dee269SGeorge Rimar section_type = eSectionTypeDWARFDebugLocLists; 878b9c1b51eSKate Stone else if (const_sect_name == g_sect_name_dwarf_debug_macinfo) 879b9c1b51eSKate Stone section_type = eSectionTypeDWARFDebugMacInfo; 880a041d848SPavel Labath else if (const_sect_name == g_sect_name_dwarf_debug_names) 881a041d848SPavel Labath section_type = eSectionTypeDWARFDebugNames; 882b9c1b51eSKate Stone else if (const_sect_name == g_sect_name_dwarf_debug_pubnames) 883b9c1b51eSKate Stone section_type = eSectionTypeDWARFDebugPubNames; 884b9c1b51eSKate Stone else if (const_sect_name == g_sect_name_dwarf_debug_pubtypes) 885b9c1b51eSKate Stone section_type = eSectionTypeDWARFDebugPubTypes; 886b9c1b51eSKate Stone else if (const_sect_name == g_sect_name_dwarf_debug_ranges) 887b9c1b51eSKate Stone section_type = eSectionTypeDWARFDebugRanges; 888b9c1b51eSKate Stone else if (const_sect_name == g_sect_name_dwarf_debug_str) 889b9c1b51eSKate Stone section_type = eSectionTypeDWARFDebugStr; 8902550ca1eSGreg Clayton else if (const_sect_name == g_sect_name_dwarf_debug_types) 8912550ca1eSGreg Clayton section_type = eSectionTypeDWARFDebugTypes; 892b9c1b51eSKate Stone else if (const_sect_name == g_sect_name_eh_frame) 893b9c1b51eSKate Stone section_type = eSectionTypeEHFrame; 894b9c1b51eSKate Stone else if (const_sect_name == g_sect_name_go_symtab) 895b9c1b51eSKate Stone section_type = eSectionTypeGoSymtab; 896b9c1b51eSKate Stone else if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_CODE) { 89728469ca3SGreg Clayton section_type = eSectionTypeCode; 898b9c1b51eSKate Stone } else if (m_sect_headers[idx].flags & 899b9c1b51eSKate Stone llvm::COFF::IMAGE_SCN_CNT_INITIALIZED_DATA) { 90028469ca3SGreg Clayton section_type = eSectionTypeData; 901b9c1b51eSKate Stone } else if (m_sect_headers[idx].flags & 902b9c1b51eSKate Stone llvm::COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) { 90328469ca3SGreg Clayton if (m_sect_headers[idx].size == 0) 90428469ca3SGreg Clayton section_type = eSectionTypeZeroFill; 90528469ca3SGreg Clayton else 90628469ca3SGreg Clayton section_type = eSectionTypeData; 90728469ca3SGreg Clayton } 908f754f88fSGreg Clayton 909b9c1b51eSKate Stone SectionSP section_sp(new Section( 910b9c1b51eSKate Stone module_sp, // Module to which this section belongs 911a7499c98SMichael Sartain this, // Object file to which this section belongs 9127db8b5c4SPavel Labath idx + 1, // Section ID is the 1 based section index. 913f754f88fSGreg Clayton const_sect_name, // Name of this section 9147db8b5c4SPavel Labath section_type, 91573a7a55cSPavel Labath m_coff_header_opt.image_base + 916b9c1b51eSKate Stone m_sect_headers[idx].vmaddr, // File VM address == addresses as 917b9c1b51eSKate Stone // they are found in the object file 918f754f88fSGreg Clayton m_sect_headers[idx].vmsize, // VM size in bytes of this section 919b9c1b51eSKate Stone m_sect_headers[idx] 920b9c1b51eSKate Stone .offset, // Offset to the data for this section in the file 921b9c1b51eSKate Stone m_sect_headers[idx] 922b9c1b51eSKate Stone .size, // Size in bytes of this section as found in the file 92348672afbSGreg Clayton m_coff_header_opt.sect_alignment, // Section alignment 924f754f88fSGreg Clayton m_sect_headers[idx].flags)); // Flags for this section 925f754f88fSGreg Clayton 926f1e0ae34SPavel Labath uint32_t permissions = 0; 927f1e0ae34SPavel Labath if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_MEM_EXECUTE) 928f1e0ae34SPavel Labath permissions |= ePermissionsExecutable; 929f1e0ae34SPavel Labath if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_MEM_READ) 930f1e0ae34SPavel Labath permissions |= ePermissionsReadable; 931f1e0ae34SPavel Labath if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_MEM_WRITE) 932f1e0ae34SPavel Labath permissions |= ePermissionsWritable; 933f1e0ae34SPavel Labath section_sp->SetPermissions(permissions); 934f1e0ae34SPavel Labath 93573a7a55cSPavel Labath m_sections_up->AddSection(section_sp); 93673a7a55cSPavel Labath unified_section_list.AddSection(section_sp); 937f754f88fSGreg Clayton } 938f754f88fSGreg Clayton } 939a1743499SGreg Clayton } 940f754f88fSGreg Clayton 941b8d03935SAaron Smith UUID ObjectFilePECOFF::GetUUID() { 942b8d03935SAaron Smith if (m_uuid.IsValid()) 943b8d03935SAaron Smith return m_uuid; 944b8d03935SAaron Smith 945b8d03935SAaron Smith if (!CreateBinary()) 946b8d03935SAaron Smith return UUID(); 947b8d03935SAaron Smith 948b8d03935SAaron Smith auto COFFObj = 949b8d03935SAaron Smith llvm::cast<llvm::object::COFFObjectFile>(m_owningbin->getBinary()); 950b8d03935SAaron Smith 951b8d03935SAaron Smith m_uuid = GetCoffUUID(COFFObj); 952b8d03935SAaron Smith return m_uuid; 953b8d03935SAaron Smith } 954f754f88fSGreg Clayton 955037ed1beSAaron Smith uint32_t ObjectFilePECOFF::ParseDependentModules() { 956037ed1beSAaron Smith ModuleSP module_sp(GetModule()); 957037ed1beSAaron Smith if (!module_sp) 958f754f88fSGreg Clayton return 0; 959037ed1beSAaron Smith 960037ed1beSAaron Smith std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 961037ed1beSAaron Smith if (m_deps_filespec) 962037ed1beSAaron Smith return m_deps_filespec->GetSize(); 963037ed1beSAaron Smith 964037ed1beSAaron Smith // Cache coff binary if it is not done yet. 965037ed1beSAaron Smith if (!CreateBinary()) 966037ed1beSAaron Smith return 0; 967037ed1beSAaron Smith 968037ed1beSAaron Smith Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT)); 96963e5fb76SJonas Devlieghere LLDB_LOGF(log, 97063e5fb76SJonas Devlieghere "%p ObjectFilePECOFF::ParseDependentModules() module = %p " 971037ed1beSAaron Smith "(%s), binary = %p (Bin = %p)", 972037ed1beSAaron Smith static_cast<void *>(this), static_cast<void *>(module_sp.get()), 973037ed1beSAaron Smith module_sp->GetSpecificationDescription().c_str(), 974037ed1beSAaron Smith static_cast<void *>(m_owningbin.getPointer()), 975b8d03935SAaron Smith static_cast<void *>(m_owningbin->getBinary())); 976037ed1beSAaron Smith 977037ed1beSAaron Smith auto COFFObj = 978037ed1beSAaron Smith llvm::dyn_cast<llvm::object::COFFObjectFile>(m_owningbin->getBinary()); 979037ed1beSAaron Smith if (!COFFObj) 980037ed1beSAaron Smith return 0; 981037ed1beSAaron Smith 982037ed1beSAaron Smith m_deps_filespec = FileSpecList(); 983037ed1beSAaron Smith 984037ed1beSAaron Smith for (const auto &entry : COFFObj->import_directories()) { 985037ed1beSAaron Smith llvm::StringRef dll_name; 986037ed1beSAaron Smith auto ec = entry.getName(dll_name); 987037ed1beSAaron Smith // Report a bogus entry. 988037ed1beSAaron Smith if (ec != std::error_code()) { 98963e5fb76SJonas Devlieghere LLDB_LOGF(log, 99063e5fb76SJonas Devlieghere "ObjectFilePECOFF::ParseDependentModules() - failed to get " 991037ed1beSAaron Smith "import directory entry name: %s", 992037ed1beSAaron Smith ec.message().c_str()); 993037ed1beSAaron Smith continue; 994037ed1beSAaron Smith } 995037ed1beSAaron Smith 996037ed1beSAaron Smith // At this moment we only have the base name of the DLL. The full path can 997037ed1beSAaron Smith // only be seen after the dynamic loading. Our best guess is Try to get it 998037ed1beSAaron Smith // with the help of the object file's directory. 999b3f44ad9SStella Stamenova llvm::SmallString<128> dll_fullpath; 1000037ed1beSAaron Smith FileSpec dll_specs(dll_name); 1001037ed1beSAaron Smith dll_specs.GetDirectory().SetString(m_file.GetDirectory().GetCString()); 1002037ed1beSAaron Smith 1003037ed1beSAaron Smith if (!llvm::sys::fs::real_path(dll_specs.GetPath(), dll_fullpath)) 1004f893d5bfSJonas Devlieghere m_deps_filespec->EmplaceBack(dll_fullpath); 1005037ed1beSAaron Smith else { 1006037ed1beSAaron Smith // Known DLLs or DLL not found in the object file directory. 1007f893d5bfSJonas Devlieghere m_deps_filespec->EmplaceBack(dll_name); 1008037ed1beSAaron Smith } 1009037ed1beSAaron Smith } 1010037ed1beSAaron Smith return m_deps_filespec->GetSize(); 1011037ed1beSAaron Smith } 1012037ed1beSAaron Smith 1013037ed1beSAaron Smith uint32_t ObjectFilePECOFF::GetDependentModules(FileSpecList &files) { 1014037ed1beSAaron Smith auto num_modules = ParseDependentModules(); 1015037ed1beSAaron Smith auto original_size = files.GetSize(); 1016037ed1beSAaron Smith 1017037ed1beSAaron Smith for (unsigned i = 0; i < num_modules; ++i) 1018037ed1beSAaron Smith files.AppendIfUnique(m_deps_filespec->GetFileSpecAtIndex(i)); 1019037ed1beSAaron Smith 1020037ed1beSAaron Smith return files.GetSize() - original_size; 1021f754f88fSGreg Clayton } 1022f754f88fSGreg Clayton 1023b9c1b51eSKate Stone lldb_private::Address ObjectFilePECOFF::GetEntryPointAddress() { 10248e38c666SStephane Sezer if (m_entry_point_address.IsValid()) 10258e38c666SStephane Sezer return m_entry_point_address; 10268e38c666SStephane Sezer 10278e38c666SStephane Sezer if (!ParseHeader() || !IsExecutable()) 10288e38c666SStephane Sezer return m_entry_point_address; 10298e38c666SStephane Sezer 10308e38c666SStephane Sezer SectionList *section_list = GetSectionList(); 1031a5235af9SAleksandr Urakov addr_t file_addr = m_coff_header_opt.entry + m_coff_header_opt.image_base; 10328e38c666SStephane Sezer 10338e38c666SStephane Sezer if (!section_list) 1034a5235af9SAleksandr Urakov m_entry_point_address.SetOffset(file_addr); 10358e38c666SStephane Sezer else 1036b8d03935SAaron Smith m_entry_point_address.ResolveAddressUsingFileSections(file_addr, 1037b8d03935SAaron Smith section_list); 10388e38c666SStephane Sezer return m_entry_point_address; 10398e38c666SStephane Sezer } 10408e38c666SStephane Sezer 1041d1304bbaSPavel Labath Address ObjectFilePECOFF::GetBaseAddress() { 1042d1304bbaSPavel Labath return Address(GetSectionList()->GetSectionAtIndex(0), 0); 1043d1304bbaSPavel Labath } 1044d1304bbaSPavel Labath 1045f754f88fSGreg Clayton // Dump 1046f754f88fSGreg Clayton // 1047f754f88fSGreg Clayton // Dump the specifics of the runtime file container (such as any headers 1048f754f88fSGreg Clayton // segments, sections, etc). 1049b9c1b51eSKate Stone void ObjectFilePECOFF::Dump(Stream *s) { 1050a1743499SGreg Clayton ModuleSP module_sp(GetModule()); 1051b9c1b51eSKate Stone if (module_sp) { 105216ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 1053324a1036SSaleem Abdulrasool s->Printf("%p: ", static_cast<void *>(this)); 1054f754f88fSGreg Clayton s->Indent(); 1055f754f88fSGreg Clayton s->PutCString("ObjectFilePECOFF"); 1056f754f88fSGreg Clayton 1057f760f5aeSPavel Labath ArchSpec header_arch = GetArchitecture(); 1058f754f88fSGreg Clayton 1059b9c1b51eSKate Stone *s << ", file = '" << m_file 1060b9c1b51eSKate Stone << "', arch = " << header_arch.GetArchitectureName() << "\n"; 1061f754f88fSGreg Clayton 10623046e668SGreg Clayton SectionList *sections = GetSectionList(); 10633046e668SGreg Clayton if (sections) 1064248a1305SKonrad Kleine sections->Dump(s, nullptr, true, UINT32_MAX); 1065f754f88fSGreg Clayton 1066d5b44036SJonas Devlieghere if (m_symtab_up) 1067248a1305SKonrad Kleine m_symtab_up->Dump(s, nullptr, eSortOrderNone); 1068f754f88fSGreg Clayton 1069f754f88fSGreg Clayton if (m_dos_header.e_magic) 1070f754f88fSGreg Clayton DumpDOSHeader(s, m_dos_header); 1071b9c1b51eSKate Stone if (m_coff_header.machine) { 1072f754f88fSGreg Clayton DumpCOFFHeader(s, m_coff_header); 1073f754f88fSGreg Clayton if (m_coff_header.hdrsize) 1074f754f88fSGreg Clayton DumpOptCOFFHeader(s, m_coff_header_opt); 1075f754f88fSGreg Clayton } 1076f754f88fSGreg Clayton s->EOL(); 1077f754f88fSGreg Clayton DumpSectionHeaders(s); 1078f754f88fSGreg Clayton s->EOL(); 1079037ed1beSAaron Smith 1080037ed1beSAaron Smith DumpDependentModules(s); 1081037ed1beSAaron Smith s->EOL(); 1082f754f88fSGreg Clayton } 1083a1743499SGreg Clayton } 1084f754f88fSGreg Clayton 1085f754f88fSGreg Clayton // DumpDOSHeader 1086f754f88fSGreg Clayton // 1087f754f88fSGreg Clayton // Dump the MS-DOS header to the specified output stream 1088b9c1b51eSKate Stone void ObjectFilePECOFF::DumpDOSHeader(Stream *s, const dos_header_t &header) { 1089f754f88fSGreg Clayton s->PutCString("MSDOS Header\n"); 1090f754f88fSGreg Clayton s->Printf(" e_magic = 0x%4.4x\n", header.e_magic); 1091f754f88fSGreg Clayton s->Printf(" e_cblp = 0x%4.4x\n", header.e_cblp); 1092f754f88fSGreg Clayton s->Printf(" e_cp = 0x%4.4x\n", header.e_cp); 1093f754f88fSGreg Clayton s->Printf(" e_crlc = 0x%4.4x\n", header.e_crlc); 1094f754f88fSGreg Clayton s->Printf(" e_cparhdr = 0x%4.4x\n", header.e_cparhdr); 1095f754f88fSGreg Clayton s->Printf(" e_minalloc = 0x%4.4x\n", header.e_minalloc); 1096f754f88fSGreg Clayton s->Printf(" e_maxalloc = 0x%4.4x\n", header.e_maxalloc); 1097f754f88fSGreg Clayton s->Printf(" e_ss = 0x%4.4x\n", header.e_ss); 1098f754f88fSGreg Clayton s->Printf(" e_sp = 0x%4.4x\n", header.e_sp); 1099f754f88fSGreg Clayton s->Printf(" e_csum = 0x%4.4x\n", header.e_csum); 1100f754f88fSGreg Clayton s->Printf(" e_ip = 0x%4.4x\n", header.e_ip); 1101f754f88fSGreg Clayton s->Printf(" e_cs = 0x%4.4x\n", header.e_cs); 1102f754f88fSGreg Clayton s->Printf(" e_lfarlc = 0x%4.4x\n", header.e_lfarlc); 1103f754f88fSGreg Clayton s->Printf(" e_ovno = 0x%4.4x\n", header.e_ovno); 1104f754f88fSGreg Clayton s->Printf(" e_res[4] = { 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x }\n", 1105b9c1b51eSKate Stone header.e_res[0], header.e_res[1], header.e_res[2], header.e_res[3]); 1106f754f88fSGreg Clayton s->Printf(" e_oemid = 0x%4.4x\n", header.e_oemid); 1107f754f88fSGreg Clayton s->Printf(" e_oeminfo = 0x%4.4x\n", header.e_oeminfo); 1108b9c1b51eSKate Stone s->Printf(" e_res2[10] = { 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, " 1109b9c1b51eSKate Stone "0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x }\n", 1110b9c1b51eSKate Stone header.e_res2[0], header.e_res2[1], header.e_res2[2], 1111b9c1b51eSKate Stone header.e_res2[3], header.e_res2[4], header.e_res2[5], 1112b9c1b51eSKate Stone header.e_res2[6], header.e_res2[7], header.e_res2[8], 1113f754f88fSGreg Clayton header.e_res2[9]); 1114f754f88fSGreg Clayton s->Printf(" e_lfanew = 0x%8.8x\n", header.e_lfanew); 1115f754f88fSGreg Clayton } 1116f754f88fSGreg Clayton 1117f754f88fSGreg Clayton // DumpCOFFHeader 1118f754f88fSGreg Clayton // 1119f754f88fSGreg Clayton // Dump the COFF header to the specified output stream 1120b9c1b51eSKate Stone void ObjectFilePECOFF::DumpCOFFHeader(Stream *s, const coff_header_t &header) { 1121f754f88fSGreg Clayton s->PutCString("COFF Header\n"); 1122f754f88fSGreg Clayton s->Printf(" machine = 0x%4.4x\n", header.machine); 1123f754f88fSGreg Clayton s->Printf(" nsects = 0x%4.4x\n", header.nsects); 1124f754f88fSGreg Clayton s->Printf(" modtime = 0x%8.8x\n", header.modtime); 1125f754f88fSGreg Clayton s->Printf(" symoff = 0x%8.8x\n", header.symoff); 1126f754f88fSGreg Clayton s->Printf(" nsyms = 0x%8.8x\n", header.nsyms); 1127f754f88fSGreg Clayton s->Printf(" hdrsize = 0x%4.4x\n", header.hdrsize); 1128f754f88fSGreg Clayton } 1129f754f88fSGreg Clayton 1130f754f88fSGreg Clayton // DumpOptCOFFHeader 1131f754f88fSGreg Clayton // 1132f754f88fSGreg Clayton // Dump the optional COFF header to the specified output stream 1133b9c1b51eSKate Stone void ObjectFilePECOFF::DumpOptCOFFHeader(Stream *s, 1134b9c1b51eSKate Stone const coff_opt_header_t &header) { 1135f754f88fSGreg Clayton s->PutCString("Optional COFF Header\n"); 1136f754f88fSGreg Clayton s->Printf(" magic = 0x%4.4x\n", header.magic); 1137b9c1b51eSKate Stone s->Printf(" major_linker_version = 0x%2.2x\n", 1138b9c1b51eSKate Stone header.major_linker_version); 1139b9c1b51eSKate Stone s->Printf(" minor_linker_version = 0x%2.2x\n", 1140b9c1b51eSKate Stone header.minor_linker_version); 1141f754f88fSGreg Clayton s->Printf(" code_size = 0x%8.8x\n", header.code_size); 1142f754f88fSGreg Clayton s->Printf(" data_size = 0x%8.8x\n", header.data_size); 1143f754f88fSGreg Clayton s->Printf(" bss_size = 0x%8.8x\n", header.bss_size); 1144f754f88fSGreg Clayton s->Printf(" entry = 0x%8.8x\n", header.entry); 1145f754f88fSGreg Clayton s->Printf(" code_offset = 0x%8.8x\n", header.code_offset); 1146f754f88fSGreg Clayton s->Printf(" data_offset = 0x%8.8x\n", header.data_offset); 1147b9c1b51eSKate Stone s->Printf(" image_base = 0x%16.16" PRIx64 "\n", 1148b9c1b51eSKate Stone header.image_base); 1149f754f88fSGreg Clayton s->Printf(" sect_alignment = 0x%8.8x\n", header.sect_alignment); 1150f754f88fSGreg Clayton s->Printf(" file_alignment = 0x%8.8x\n", header.file_alignment); 1151b9c1b51eSKate Stone s->Printf(" major_os_system_version = 0x%4.4x\n", 1152b9c1b51eSKate Stone header.major_os_system_version); 1153b9c1b51eSKate Stone s->Printf(" minor_os_system_version = 0x%4.4x\n", 1154b9c1b51eSKate Stone header.minor_os_system_version); 1155b9c1b51eSKate Stone s->Printf(" major_image_version = 0x%4.4x\n", 1156b9c1b51eSKate Stone header.major_image_version); 1157b9c1b51eSKate Stone s->Printf(" minor_image_version = 0x%4.4x\n", 1158b9c1b51eSKate Stone header.minor_image_version); 1159b9c1b51eSKate Stone s->Printf(" major_subsystem_version = 0x%4.4x\n", 1160b9c1b51eSKate Stone header.major_subsystem_version); 1161b9c1b51eSKate Stone s->Printf(" minor_subsystem_version = 0x%4.4x\n", 1162b9c1b51eSKate Stone header.minor_subsystem_version); 1163f754f88fSGreg Clayton s->Printf(" reserved1 = 0x%8.8x\n", header.reserved1); 1164f754f88fSGreg Clayton s->Printf(" image_size = 0x%8.8x\n", header.image_size); 1165f754f88fSGreg Clayton s->Printf(" header_size = 0x%8.8x\n", header.header_size); 116628469ca3SGreg Clayton s->Printf(" checksum = 0x%8.8x\n", header.checksum); 1167f754f88fSGreg Clayton s->Printf(" subsystem = 0x%4.4x\n", header.subsystem); 1168f754f88fSGreg Clayton s->Printf(" dll_flags = 0x%4.4x\n", header.dll_flags); 1169b9c1b51eSKate Stone s->Printf(" stack_reserve_size = 0x%16.16" PRIx64 "\n", 1170b9c1b51eSKate Stone header.stack_reserve_size); 1171b9c1b51eSKate Stone s->Printf(" stack_commit_size = 0x%16.16" PRIx64 "\n", 1172b9c1b51eSKate Stone header.stack_commit_size); 1173b9c1b51eSKate Stone s->Printf(" heap_reserve_size = 0x%16.16" PRIx64 "\n", 1174b9c1b51eSKate Stone header.heap_reserve_size); 1175b9c1b51eSKate Stone s->Printf(" heap_commit_size = 0x%16.16" PRIx64 "\n", 1176b9c1b51eSKate Stone header.heap_commit_size); 1177f754f88fSGreg Clayton s->Printf(" loader_flags = 0x%8.8x\n", header.loader_flags); 1178b9c1b51eSKate Stone s->Printf(" num_data_dir_entries = 0x%8.8x\n", 1179b9c1b51eSKate Stone (uint32_t)header.data_dirs.size()); 1180f754f88fSGreg Clayton uint32_t i; 1181b9c1b51eSKate Stone for (i = 0; i < header.data_dirs.size(); i++) { 1182b9c1b51eSKate Stone s->Printf(" data_dirs[%2u] vmaddr = 0x%8.8x, vmsize = 0x%8.8x\n", i, 1183b9c1b51eSKate Stone header.data_dirs[i].vmaddr, header.data_dirs[i].vmsize); 1184f754f88fSGreg Clayton } 1185f754f88fSGreg Clayton } 1186f754f88fSGreg Clayton // DumpSectionHeader 1187f754f88fSGreg Clayton // 1188f754f88fSGreg Clayton // Dump a single ELF section header to the specified output stream 1189b9c1b51eSKate Stone void ObjectFilePECOFF::DumpSectionHeader(Stream *s, 1190b9c1b51eSKate Stone const section_header_t &sh) { 11912886e4a0SPavel Labath std::string name = GetSectionName(sh); 1192b9c1b51eSKate Stone s->Printf("%-16s 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x 0x%4.4x " 1193b9c1b51eSKate Stone "0x%4.4x 0x%8.8x\n", 1194b9c1b51eSKate Stone name.c_str(), sh.vmaddr, sh.vmsize, sh.offset, sh.size, sh.reloff, 1195b9c1b51eSKate Stone sh.lineoff, sh.nreloc, sh.nline, sh.flags); 1196f754f88fSGreg Clayton } 1197f754f88fSGreg Clayton 1198f754f88fSGreg Clayton // DumpSectionHeaders 1199f754f88fSGreg Clayton // 1200f754f88fSGreg Clayton // Dump all of the ELF section header to the specified output stream 1201b9c1b51eSKate Stone void ObjectFilePECOFF::DumpSectionHeaders(Stream *s) { 1202f754f88fSGreg Clayton 1203f754f88fSGreg Clayton s->PutCString("Section Headers\n"); 1204b9c1b51eSKate Stone s->PutCString("IDX name vm addr vm size file off file " 1205b9c1b51eSKate Stone "size reloc off line off nreloc nline flags\n"); 1206b9c1b51eSKate Stone s->PutCString("==== ---------------- ---------- ---------- ---------- " 1207b9c1b51eSKate Stone "---------- ---------- ---------- ------ ------ ----------\n"); 1208f754f88fSGreg Clayton 1209f754f88fSGreg Clayton uint32_t idx = 0; 1210f754f88fSGreg Clayton SectionHeaderCollIter pos, end = m_sect_headers.end(); 1211f754f88fSGreg Clayton 1212b9c1b51eSKate Stone for (pos = m_sect_headers.begin(); pos != end; ++pos, ++idx) { 1213f754f88fSGreg Clayton s->Printf("[%2u] ", idx); 1214f754f88fSGreg Clayton ObjectFilePECOFF::DumpSectionHeader(s, *pos); 1215f754f88fSGreg Clayton } 1216f754f88fSGreg Clayton } 1217f754f88fSGreg Clayton 1218037ed1beSAaron Smith // DumpDependentModules 1219037ed1beSAaron Smith // 1220037ed1beSAaron Smith // Dump all of the dependent modules to the specified output stream 1221037ed1beSAaron Smith void ObjectFilePECOFF::DumpDependentModules(lldb_private::Stream *s) { 1222037ed1beSAaron Smith auto num_modules = ParseDependentModules(); 1223037ed1beSAaron Smith if (num_modules > 0) { 1224037ed1beSAaron Smith s->PutCString("Dependent Modules\n"); 1225037ed1beSAaron Smith for (unsigned i = 0; i < num_modules; ++i) { 1226037ed1beSAaron Smith auto spec = m_deps_filespec->GetFileSpecAtIndex(i); 1227037ed1beSAaron Smith s->Printf(" %s\n", spec.GetFilename().GetCString()); 1228037ed1beSAaron Smith } 1229037ed1beSAaron Smith } 1230037ed1beSAaron Smith } 1231037ed1beSAaron Smith 1232fb3b3bd1SZachary Turner bool ObjectFilePECOFF::IsWindowsSubsystem() { 1233fb3b3bd1SZachary Turner switch (m_coff_header_opt.subsystem) { 1234fb3b3bd1SZachary Turner case llvm::COFF::IMAGE_SUBSYSTEM_NATIVE: 1235fb3b3bd1SZachary Turner case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_GUI: 1236fb3b3bd1SZachary Turner case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_CUI: 1237fb3b3bd1SZachary Turner case llvm::COFF::IMAGE_SUBSYSTEM_NATIVE_WINDOWS: 1238fb3b3bd1SZachary Turner case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_CE_GUI: 1239fb3b3bd1SZachary Turner case llvm::COFF::IMAGE_SUBSYSTEM_XBOX: 1240fb3b3bd1SZachary Turner case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_BOOT_APPLICATION: 1241fb3b3bd1SZachary Turner return true; 1242fb3b3bd1SZachary Turner default: 1243fb3b3bd1SZachary Turner return false; 1244fb3b3bd1SZachary Turner } 1245fb3b3bd1SZachary Turner } 1246fb3b3bd1SZachary Turner 1247f760f5aeSPavel Labath ArchSpec ObjectFilePECOFF::GetArchitecture() { 1248237ad974SCharles Davis uint16_t machine = m_coff_header.machine; 1249b9c1b51eSKate Stone switch (machine) { 1250f760f5aeSPavel Labath default: 1251f760f5aeSPavel Labath break; 1252237ad974SCharles Davis case llvm::COFF::IMAGE_FILE_MACHINE_AMD64: 1253237ad974SCharles Davis case llvm::COFF::IMAGE_FILE_MACHINE_I386: 1254237ad974SCharles Davis case llvm::COFF::IMAGE_FILE_MACHINE_POWERPC: 1255237ad974SCharles Davis case llvm::COFF::IMAGE_FILE_MACHINE_POWERPCFP: 1256237ad974SCharles Davis case llvm::COFF::IMAGE_FILE_MACHINE_ARM: 12571108cb36SSaleem Abdulrasool case llvm::COFF::IMAGE_FILE_MACHINE_ARMNT: 1258237ad974SCharles Davis case llvm::COFF::IMAGE_FILE_MACHINE_THUMB: 1259638f072fSMartin Storsjo case llvm::COFF::IMAGE_FILE_MACHINE_ARM64: 1260f760f5aeSPavel Labath ArchSpec arch; 1261fb3b3bd1SZachary Turner arch.SetArchitecture(eArchTypeCOFF, machine, LLDB_INVALID_CPUTYPE, 1262fb3b3bd1SZachary Turner IsWindowsSubsystem() ? llvm::Triple::Win32 1263fb3b3bd1SZachary Turner : llvm::Triple::UnknownOS); 1264f760f5aeSPavel Labath return arch; 1265237ad974SCharles Davis } 1266f760f5aeSPavel Labath return ArchSpec(); 1267f754f88fSGreg Clayton } 1268f754f88fSGreg Clayton 1269b9c1b51eSKate Stone ObjectFile::Type ObjectFilePECOFF::CalculateType() { 1270b9c1b51eSKate Stone if (m_coff_header.machine != 0) { 1271237ad974SCharles Davis if ((m_coff_header.flags & llvm::COFF::IMAGE_FILE_DLL) == 0) 1272f754f88fSGreg Clayton return eTypeExecutable; 1273f754f88fSGreg Clayton else 1274f754f88fSGreg Clayton return eTypeSharedLibrary; 1275f754f88fSGreg Clayton } 1276f754f88fSGreg Clayton return eTypeExecutable; 1277f754f88fSGreg Clayton } 1278f754f88fSGreg Clayton 1279b9c1b51eSKate Stone ObjectFile::Strata ObjectFilePECOFF::CalculateStrata() { return eStrataUser; } 12809cad24a7SZachary Turner 1281f754f88fSGreg Clayton // PluginInterface protocol 1282b9c1b51eSKate Stone ConstString ObjectFilePECOFF::GetPluginName() { return GetPluginNameStatic(); } 1283f754f88fSGreg Clayton 1284b9c1b51eSKate Stone uint32_t ObjectFilePECOFF::GetPluginVersion() { return 1; } 1285