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 1703db1d138SMartin Storsjö Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT)); 1713db1d138SMartin Storsjö 172b8d03935SAaron Smith auto binary = llvm::object::createBinary(file.GetPath()); 1733db1d138SMartin Storsjö 1743db1d138SMartin Storsjö if (!binary) { 1753db1d138SMartin Storsjö LLDB_LOG_ERROR(log, binary.takeError(), 1763db1d138SMartin Storsjö "Failed to create binary for file ({1}): {0}", file); 177b8d03935SAaron Smith return initial_count; 1783db1d138SMartin Storsjö } 17989eb1baeSVirgile Bello 180b8d03935SAaron Smith if (!binary->getBinary()->isCOFF() && 181b8d03935SAaron Smith !binary->getBinary()->isCOFFImportFile()) 182b8d03935SAaron Smith return initial_count; 18389eb1baeSVirgile Bello 184b8d03935SAaron Smith auto COFFObj = 185b8d03935SAaron Smith llvm::cast<llvm::object::COFFObjectFile>(binary->getBinary()); 186b8d03935SAaron Smith 187b8d03935SAaron Smith ModuleSpec module_spec(file); 188b8d03935SAaron Smith ArchSpec &spec = module_spec.GetArchitecture(); 189b8d03935SAaron Smith lldb_private::UUID &uuid = module_spec.GetUUID(); 190b8d03935SAaron Smith if (!uuid.IsValid()) 191b8d03935SAaron Smith uuid = GetCoffUUID(COFFObj); 192b8d03935SAaron Smith 193b8d03935SAaron Smith switch (COFFObj->getMachine()) { 194b8d03935SAaron Smith case MachineAmd64: 195ad587ae4SZachary Turner spec.SetTriple("x86_64-pc-windows"); 196b8d03935SAaron Smith specs.Append(module_spec); 197b8d03935SAaron Smith break; 198b8d03935SAaron Smith case MachineX86: 199ad587ae4SZachary Turner spec.SetTriple("i386-pc-windows"); 200b8d03935SAaron Smith specs.Append(module_spec); 2015e6f4520SZachary Turner spec.SetTriple("i686-pc-windows"); 202b8d03935SAaron Smith specs.Append(module_spec); 203b8d03935SAaron Smith break; 204b8d03935SAaron Smith case MachineArmNt: 205544c8f48SMartin Storsjo spec.SetTriple("armv7-pc-windows"); 206b8d03935SAaron Smith specs.Append(module_spec); 207b8d03935SAaron Smith break; 208638f072fSMartin Storsjo case MachineArm64: 209674d5543SMartin Storsjo spec.SetTriple("aarch64-pc-windows"); 210638f072fSMartin Storsjo specs.Append(module_spec); 211638f072fSMartin Storsjo break; 212b8d03935SAaron Smith default: 213b8d03935SAaron Smith break; 21489eb1baeSVirgile Bello } 21589eb1baeSVirgile Bello 21689eb1baeSVirgile Bello return specs.GetSize() - initial_count; 217f4d6de6aSGreg Clayton } 218f4d6de6aSGreg Clayton 219b9c1b51eSKate Stone bool ObjectFilePECOFF::SaveCore(const lldb::ProcessSP &process_sp, 220f7d1893fSAdrian McCarthy const lldb_private::FileSpec &outfile, 22197206d57SZachary Turner lldb_private::Status &error) { 222f7d1893fSAdrian McCarthy return SaveMiniDump(process_sp, outfile, error); 223f7d1893fSAdrian McCarthy } 224f7d1893fSAdrian McCarthy 225b9c1b51eSKate Stone bool ObjectFilePECOFF::MagicBytesMatch(DataBufferSP &data_sp) { 2265ce9c565SGreg Clayton DataExtractor data(data_sp, eByteOrderLittle, 4); 227c7bece56SGreg Clayton lldb::offset_t offset = 0; 228f754f88fSGreg Clayton uint16_t magic = data.GetU16(&offset); 229f754f88fSGreg Clayton return magic == IMAGE_DOS_SIGNATURE; 230f754f88fSGreg Clayton } 231f754f88fSGreg Clayton 232b9c1b51eSKate Stone lldb::SymbolType ObjectFilePECOFF::MapSymbolType(uint16_t coff_symbol_type) { 233c35b91ceSAdrian McCarthy // TODO: We need to complete this mapping of COFF symbol types to LLDB ones. 234c35b91ceSAdrian McCarthy // For now, here's a hack to make sure our function have types. 235b9c1b51eSKate Stone const auto complex_type = 236b9c1b51eSKate Stone coff_symbol_type >> llvm::COFF::SCT_COMPLEX_TYPE_SHIFT; 237b9c1b51eSKate Stone if (complex_type == llvm::COFF::IMAGE_SYM_DTYPE_FUNCTION) { 238c35b91ceSAdrian McCarthy return lldb::eSymbolTypeCode; 239c35b91ceSAdrian McCarthy } 240c35b91ceSAdrian McCarthy return lldb::eSymbolTypeInvalid; 241c35b91ceSAdrian McCarthy } 242f754f88fSGreg Clayton 243037ed1beSAaron Smith bool ObjectFilePECOFF::CreateBinary() { 244037ed1beSAaron Smith if (m_owningbin) 245037ed1beSAaron Smith return true; 246037ed1beSAaron Smith 247037ed1beSAaron Smith Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT)); 248037ed1beSAaron Smith 249037ed1beSAaron Smith auto binary = llvm::object::createBinary(m_file.GetPath()); 250037ed1beSAaron Smith if (!binary) { 2513db1d138SMartin Storsjö LLDB_LOG_ERROR(log, binary.takeError(), 2523db1d138SMartin Storsjö "Failed to create binary for file ({1}): {0}", m_file); 253037ed1beSAaron Smith return false; 254037ed1beSAaron Smith } 255037ed1beSAaron Smith 256037ed1beSAaron Smith // Make sure we only handle COFF format. 257037ed1beSAaron Smith if (!binary->getBinary()->isCOFF() && 258037ed1beSAaron Smith !binary->getBinary()->isCOFFImportFile()) 259037ed1beSAaron Smith return false; 260037ed1beSAaron Smith 261037ed1beSAaron Smith m_owningbin = OWNBINType(std::move(*binary)); 26263e5fb76SJonas Devlieghere LLDB_LOGF(log, 26363e5fb76SJonas Devlieghere "%p ObjectFilePECOFF::CreateBinary() module = %p (%s), file = " 264037ed1beSAaron Smith "%s, binary = %p (Bin = %p)", 26563e5fb76SJonas Devlieghere static_cast<void *>(this), static_cast<void *>(GetModule().get()), 266037ed1beSAaron Smith GetModule()->GetSpecificationDescription().c_str(), 267037ed1beSAaron Smith m_file ? m_file.GetPath().c_str() : "<NULL>", 268037ed1beSAaron Smith static_cast<void *>(m_owningbin.getPointer()), 269037ed1beSAaron Smith static_cast<void *>(m_owningbin->getBinary())); 270037ed1beSAaron Smith return true; 271037ed1beSAaron Smith } 272037ed1beSAaron Smith 273e72dfb32SGreg Clayton ObjectFilePECOFF::ObjectFilePECOFF(const lldb::ModuleSP &module_sp, 2745ce9c565SGreg Clayton DataBufferSP &data_sp, 2755ce9c565SGreg Clayton lldb::offset_t data_offset, 276f754f88fSGreg Clayton const FileSpec *file, 2775ce9c565SGreg Clayton lldb::offset_t file_offset, 278b9c1b51eSKate Stone lldb::offset_t length) 279b9c1b51eSKate Stone : ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset), 2805c43ffd6SPavel Labath m_dos_header(), m_coff_header(), m_sect_headers(), 281037ed1beSAaron Smith m_entry_point_address(), m_deps_filespec(), m_owningbin() { 282f754f88fSGreg Clayton ::memset(&m_dos_header, 0, sizeof(m_dos_header)); 283f754f88fSGreg Clayton ::memset(&m_coff_header, 0, sizeof(m_coff_header)); 284f754f88fSGreg Clayton } 285f754f88fSGreg Clayton 286344546bdSWalter Erquinigo ObjectFilePECOFF::ObjectFilePECOFF(const lldb::ModuleSP &module_sp, 287344546bdSWalter Erquinigo DataBufferSP &header_data_sp, 288344546bdSWalter Erquinigo const lldb::ProcessSP &process_sp, 289344546bdSWalter Erquinigo addr_t header_addr) 290344546bdSWalter Erquinigo : ObjectFile(module_sp, process_sp, header_addr, header_data_sp), 2915c43ffd6SPavel Labath m_dos_header(), m_coff_header(), m_sect_headers(), 292037ed1beSAaron Smith m_entry_point_address(), m_deps_filespec(), m_owningbin() { 293344546bdSWalter Erquinigo ::memset(&m_dos_header, 0, sizeof(m_dos_header)); 294344546bdSWalter Erquinigo ::memset(&m_coff_header, 0, sizeof(m_coff_header)); 295344546bdSWalter Erquinigo } 296344546bdSWalter Erquinigo 297b9c1b51eSKate Stone ObjectFilePECOFF::~ObjectFilePECOFF() {} 298f754f88fSGreg Clayton 299b9c1b51eSKate Stone bool ObjectFilePECOFF::ParseHeader() { 300a1743499SGreg Clayton ModuleSP module_sp(GetModule()); 301b9c1b51eSKate Stone if (module_sp) { 30216ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 303f754f88fSGreg Clayton m_sect_headers.clear(); 304f754f88fSGreg Clayton m_data.SetByteOrder(eByteOrderLittle); 305c7bece56SGreg Clayton lldb::offset_t offset = 0; 306f754f88fSGreg Clayton 307b9c1b51eSKate Stone if (ParseDOSHeader(m_data, m_dos_header)) { 308f754f88fSGreg Clayton offset = m_dos_header.e_lfanew; 309f754f88fSGreg Clayton uint32_t pe_signature = m_data.GetU32(&offset); 310f754f88fSGreg Clayton if (pe_signature != IMAGE_NT_SIGNATURE) 311f754f88fSGreg Clayton return false; 312b9c1b51eSKate Stone if (ParseCOFFHeader(m_data, &offset, m_coff_header)) { 313f754f88fSGreg Clayton if (m_coff_header.hdrsize > 0) 314f754f88fSGreg Clayton ParseCOFFOptionalHeader(&offset); 315f754f88fSGreg Clayton ParseSectionHeaders(offset); 31628469ca3SGreg Clayton } 317f754f88fSGreg Clayton return true; 318f754f88fSGreg Clayton } 319a1743499SGreg Clayton } 320f754f88fSGreg Clayton return false; 321f754f88fSGreg Clayton } 322f754f88fSGreg Clayton 323b9c1b51eSKate Stone bool ObjectFilePECOFF::SetLoadAddress(Target &target, addr_t value, 324b9c1b51eSKate Stone bool value_is_offset) { 3252756adf3SVirgile Bello bool changed = false; 3262756adf3SVirgile Bello ModuleSP module_sp = GetModule(); 327b9c1b51eSKate Stone if (module_sp) { 3282756adf3SVirgile Bello size_t num_loaded_sections = 0; 3292756adf3SVirgile Bello SectionList *section_list = GetSectionList(); 330b9c1b51eSKate Stone if (section_list) { 331b9c1b51eSKate Stone if (!value_is_offset) { 3322756adf3SVirgile Bello value -= m_image_base; 3332756adf3SVirgile Bello } 3342756adf3SVirgile Bello 3352756adf3SVirgile Bello const size_t num_sections = section_list->GetSize(); 3362756adf3SVirgile Bello size_t sect_idx = 0; 3372756adf3SVirgile Bello 338b9c1b51eSKate Stone for (sect_idx = 0; sect_idx < num_sections; ++sect_idx) { 33905097246SAdrian Prantl // Iterate through the object file sections to find all of the sections 34005097246SAdrian Prantl // that have SHF_ALLOC in their flag bits. 3412756adf3SVirgile Bello SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx)); 342b9c1b51eSKate Stone if (section_sp && !section_sp->IsThreadSpecific()) { 343b9c1b51eSKate Stone if (target.GetSectionLoadList().SetSectionLoadAddress( 344b9c1b51eSKate Stone section_sp, section_sp->GetFileAddress() + value)) 3452756adf3SVirgile Bello ++num_loaded_sections; 3462756adf3SVirgile Bello } 3472756adf3SVirgile Bello } 3482756adf3SVirgile Bello changed = num_loaded_sections > 0; 3492756adf3SVirgile Bello } 3502756adf3SVirgile Bello } 3512756adf3SVirgile Bello return changed; 3522756adf3SVirgile Bello } 3532756adf3SVirgile Bello 354b9c1b51eSKate Stone ByteOrder ObjectFilePECOFF::GetByteOrder() const { return eByteOrderLittle; } 355f754f88fSGreg Clayton 356b9c1b51eSKate Stone bool ObjectFilePECOFF::IsExecutable() const { 357237ad974SCharles Davis return (m_coff_header.flags & llvm::COFF::IMAGE_FILE_DLL) == 0; 358f754f88fSGreg Clayton } 359f754f88fSGreg Clayton 360b9c1b51eSKate Stone uint32_t ObjectFilePECOFF::GetAddressByteSize() const { 361f754f88fSGreg Clayton if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32_PLUS) 362f754f88fSGreg Clayton return 8; 363f754f88fSGreg Clayton else if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32) 364f754f88fSGreg Clayton return 4; 365f754f88fSGreg Clayton return 4; 366f754f88fSGreg Clayton } 367f754f88fSGreg Clayton 368f754f88fSGreg Clayton // NeedsEndianSwap 369f754f88fSGreg Clayton // 37005097246SAdrian Prantl // Return true if an endian swap needs to occur when extracting data from this 37105097246SAdrian Prantl // file. 372b9c1b51eSKate Stone bool ObjectFilePECOFF::NeedsEndianSwap() const { 373f754f88fSGreg Clayton #if defined(__LITTLE_ENDIAN__) 374f754f88fSGreg Clayton return false; 375f754f88fSGreg Clayton #else 376f754f88fSGreg Clayton return true; 377f754f88fSGreg Clayton #endif 378f754f88fSGreg Clayton } 379f754f88fSGreg Clayton // ParseDOSHeader 380b9c1b51eSKate Stone bool ObjectFilePECOFF::ParseDOSHeader(DataExtractor &data, 381b9c1b51eSKate Stone dos_header_t &dos_header) { 382f754f88fSGreg Clayton bool success = false; 383c7bece56SGreg Clayton lldb::offset_t offset = 0; 38489eb1baeSVirgile Bello success = data.ValidOffsetForDataOfSize(0, sizeof(dos_header)); 385f754f88fSGreg Clayton 386b9c1b51eSKate Stone if (success) { 38789eb1baeSVirgile Bello dos_header.e_magic = data.GetU16(&offset); // Magic number 38889eb1baeSVirgile Bello success = dos_header.e_magic == IMAGE_DOS_SIGNATURE; 389f754f88fSGreg Clayton 390b9c1b51eSKate Stone if (success) { 39189eb1baeSVirgile Bello dos_header.e_cblp = data.GetU16(&offset); // Bytes on last page of file 39289eb1baeSVirgile Bello dos_header.e_cp = data.GetU16(&offset); // Pages in file 39389eb1baeSVirgile Bello dos_header.e_crlc = data.GetU16(&offset); // Relocations 394b9c1b51eSKate Stone dos_header.e_cparhdr = 395b9c1b51eSKate Stone data.GetU16(&offset); // Size of header in paragraphs 396b9c1b51eSKate Stone dos_header.e_minalloc = 397b9c1b51eSKate Stone data.GetU16(&offset); // Minimum extra paragraphs needed 398b9c1b51eSKate Stone dos_header.e_maxalloc = 399b9c1b51eSKate Stone data.GetU16(&offset); // Maximum extra paragraphs needed 40089eb1baeSVirgile Bello dos_header.e_ss = data.GetU16(&offset); // Initial (relative) SS value 40189eb1baeSVirgile Bello dos_header.e_sp = data.GetU16(&offset); // Initial SP value 40289eb1baeSVirgile Bello dos_header.e_csum = data.GetU16(&offset); // Checksum 40389eb1baeSVirgile Bello dos_header.e_ip = data.GetU16(&offset); // Initial IP value 40489eb1baeSVirgile Bello dos_header.e_cs = data.GetU16(&offset); // Initial (relative) CS value 405b9c1b51eSKate Stone dos_header.e_lfarlc = 406b9c1b51eSKate Stone data.GetU16(&offset); // File address of relocation table 40789eb1baeSVirgile Bello dos_header.e_ovno = data.GetU16(&offset); // Overlay number 408f754f88fSGreg Clayton 40989eb1baeSVirgile Bello dos_header.e_res[0] = data.GetU16(&offset); // Reserved words 41089eb1baeSVirgile Bello dos_header.e_res[1] = data.GetU16(&offset); // Reserved words 41189eb1baeSVirgile Bello dos_header.e_res[2] = data.GetU16(&offset); // Reserved words 41289eb1baeSVirgile Bello dos_header.e_res[3] = data.GetU16(&offset); // Reserved words 413f754f88fSGreg Clayton 414b9c1b51eSKate Stone dos_header.e_oemid = 415b9c1b51eSKate Stone data.GetU16(&offset); // OEM identifier (for e_oeminfo) 416b9c1b51eSKate Stone dos_header.e_oeminfo = 417b9c1b51eSKate Stone data.GetU16(&offset); // OEM information; e_oemid specific 41889eb1baeSVirgile Bello dos_header.e_res2[0] = data.GetU16(&offset); // Reserved words 41989eb1baeSVirgile Bello dos_header.e_res2[1] = data.GetU16(&offset); // Reserved words 42089eb1baeSVirgile Bello dos_header.e_res2[2] = data.GetU16(&offset); // Reserved words 42189eb1baeSVirgile Bello dos_header.e_res2[3] = data.GetU16(&offset); // Reserved words 42289eb1baeSVirgile Bello dos_header.e_res2[4] = data.GetU16(&offset); // Reserved words 42389eb1baeSVirgile Bello dos_header.e_res2[5] = data.GetU16(&offset); // Reserved words 42489eb1baeSVirgile Bello dos_header.e_res2[6] = data.GetU16(&offset); // Reserved words 42589eb1baeSVirgile Bello dos_header.e_res2[7] = data.GetU16(&offset); // Reserved words 42689eb1baeSVirgile Bello dos_header.e_res2[8] = data.GetU16(&offset); // Reserved words 42789eb1baeSVirgile Bello dos_header.e_res2[9] = data.GetU16(&offset); // Reserved words 428f754f88fSGreg Clayton 429b9c1b51eSKate Stone dos_header.e_lfanew = 430b9c1b51eSKate Stone data.GetU32(&offset); // File address of new exe header 431f754f88fSGreg Clayton } 432f754f88fSGreg Clayton } 433f754f88fSGreg Clayton if (!success) 43489eb1baeSVirgile Bello memset(&dos_header, 0, sizeof(dos_header)); 435f754f88fSGreg Clayton return success; 436f754f88fSGreg Clayton } 437f754f88fSGreg Clayton 438f754f88fSGreg Clayton // ParserCOFFHeader 439b9c1b51eSKate Stone bool ObjectFilePECOFF::ParseCOFFHeader(DataExtractor &data, 440b9c1b51eSKate Stone lldb::offset_t *offset_ptr, 441b9c1b51eSKate Stone coff_header_t &coff_header) { 442b9c1b51eSKate Stone bool success = 443b9c1b51eSKate Stone data.ValidOffsetForDataOfSize(*offset_ptr, sizeof(coff_header)); 444b9c1b51eSKate Stone if (success) { 44589eb1baeSVirgile Bello coff_header.machine = data.GetU16(offset_ptr); 44689eb1baeSVirgile Bello coff_header.nsects = data.GetU16(offset_ptr); 44789eb1baeSVirgile Bello coff_header.modtime = data.GetU32(offset_ptr); 44889eb1baeSVirgile Bello coff_header.symoff = data.GetU32(offset_ptr); 44989eb1baeSVirgile Bello coff_header.nsyms = data.GetU32(offset_ptr); 45089eb1baeSVirgile Bello coff_header.hdrsize = data.GetU16(offset_ptr); 45189eb1baeSVirgile Bello coff_header.flags = data.GetU16(offset_ptr); 452f754f88fSGreg Clayton } 453f754f88fSGreg Clayton if (!success) 45489eb1baeSVirgile Bello memset(&coff_header, 0, sizeof(coff_header)); 455f754f88fSGreg Clayton return success; 456f754f88fSGreg Clayton } 457f754f88fSGreg Clayton 458b9c1b51eSKate Stone bool ObjectFilePECOFF::ParseCOFFOptionalHeader(lldb::offset_t *offset_ptr) { 459f754f88fSGreg Clayton bool success = false; 460c7bece56SGreg Clayton const lldb::offset_t end_offset = *offset_ptr + m_coff_header.hdrsize; 461b9c1b51eSKate Stone if (*offset_ptr < end_offset) { 462f754f88fSGreg Clayton success = true; 463f754f88fSGreg Clayton m_coff_header_opt.magic = m_data.GetU16(offset_ptr); 464f754f88fSGreg Clayton m_coff_header_opt.major_linker_version = m_data.GetU8(offset_ptr); 465f754f88fSGreg Clayton m_coff_header_opt.minor_linker_version = m_data.GetU8(offset_ptr); 466f754f88fSGreg Clayton m_coff_header_opt.code_size = m_data.GetU32(offset_ptr); 467f754f88fSGreg Clayton m_coff_header_opt.data_size = m_data.GetU32(offset_ptr); 468f754f88fSGreg Clayton m_coff_header_opt.bss_size = m_data.GetU32(offset_ptr); 469f754f88fSGreg Clayton m_coff_header_opt.entry = m_data.GetU32(offset_ptr); 470f754f88fSGreg Clayton m_coff_header_opt.code_offset = m_data.GetU32(offset_ptr); 471f754f88fSGreg Clayton 472f754f88fSGreg Clayton const uint32_t addr_byte_size = GetAddressByteSize(); 473f754f88fSGreg Clayton 474b9c1b51eSKate Stone if (*offset_ptr < end_offset) { 475b9c1b51eSKate Stone if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32) { 476f754f88fSGreg Clayton // PE32 only 477f754f88fSGreg Clayton m_coff_header_opt.data_offset = m_data.GetU32(offset_ptr); 478b9c1b51eSKate Stone } else 479f754f88fSGreg Clayton m_coff_header_opt.data_offset = 0; 480f754f88fSGreg Clayton 481b9c1b51eSKate Stone if (*offset_ptr < end_offset) { 482b9c1b51eSKate Stone m_coff_header_opt.image_base = 483b9c1b51eSKate Stone m_data.GetMaxU64(offset_ptr, addr_byte_size); 484f754f88fSGreg Clayton m_coff_header_opt.sect_alignment = m_data.GetU32(offset_ptr); 485f754f88fSGreg Clayton m_coff_header_opt.file_alignment = m_data.GetU32(offset_ptr); 486f754f88fSGreg Clayton m_coff_header_opt.major_os_system_version = m_data.GetU16(offset_ptr); 487f754f88fSGreg Clayton m_coff_header_opt.minor_os_system_version = m_data.GetU16(offset_ptr); 488f754f88fSGreg Clayton m_coff_header_opt.major_image_version = m_data.GetU16(offset_ptr); 489f754f88fSGreg Clayton m_coff_header_opt.minor_image_version = m_data.GetU16(offset_ptr); 490f754f88fSGreg Clayton m_coff_header_opt.major_subsystem_version = m_data.GetU16(offset_ptr); 491f754f88fSGreg Clayton m_coff_header_opt.minor_subsystem_version = m_data.GetU16(offset_ptr); 492f754f88fSGreg Clayton m_coff_header_opt.reserved1 = m_data.GetU32(offset_ptr); 493f754f88fSGreg Clayton m_coff_header_opt.image_size = m_data.GetU32(offset_ptr); 494f754f88fSGreg Clayton m_coff_header_opt.header_size = m_data.GetU32(offset_ptr); 49528469ca3SGreg Clayton m_coff_header_opt.checksum = m_data.GetU32(offset_ptr); 496f754f88fSGreg Clayton m_coff_header_opt.subsystem = m_data.GetU16(offset_ptr); 497f754f88fSGreg Clayton m_coff_header_opt.dll_flags = m_data.GetU16(offset_ptr); 498b9c1b51eSKate Stone m_coff_header_opt.stack_reserve_size = 499b9c1b51eSKate Stone m_data.GetMaxU64(offset_ptr, addr_byte_size); 500b9c1b51eSKate Stone m_coff_header_opt.stack_commit_size = 501b9c1b51eSKate Stone m_data.GetMaxU64(offset_ptr, addr_byte_size); 502b9c1b51eSKate Stone m_coff_header_opt.heap_reserve_size = 503b9c1b51eSKate Stone m_data.GetMaxU64(offset_ptr, addr_byte_size); 504b9c1b51eSKate Stone m_coff_header_opt.heap_commit_size = 505b9c1b51eSKate Stone m_data.GetMaxU64(offset_ptr, addr_byte_size); 506f754f88fSGreg Clayton m_coff_header_opt.loader_flags = m_data.GetU32(offset_ptr); 507f754f88fSGreg Clayton uint32_t num_data_dir_entries = m_data.GetU32(offset_ptr); 508f754f88fSGreg Clayton m_coff_header_opt.data_dirs.clear(); 509f754f88fSGreg Clayton m_coff_header_opt.data_dirs.resize(num_data_dir_entries); 510f754f88fSGreg Clayton uint32_t i; 511b9c1b51eSKate Stone for (i = 0; i < num_data_dir_entries; i++) { 512f754f88fSGreg Clayton m_coff_header_opt.data_dirs[i].vmaddr = m_data.GetU32(offset_ptr); 513f754f88fSGreg Clayton m_coff_header_opt.data_dirs[i].vmsize = m_data.GetU32(offset_ptr); 514f754f88fSGreg Clayton } 5152756adf3SVirgile Bello 5162756adf3SVirgile Bello m_image_base = m_coff_header_opt.image_base; 517f754f88fSGreg Clayton } 518f754f88fSGreg Clayton } 519f754f88fSGreg Clayton } 520f754f88fSGreg Clayton // Make sure we are on track for section data which follows 521f754f88fSGreg Clayton *offset_ptr = end_offset; 522f754f88fSGreg Clayton return success; 523f754f88fSGreg Clayton } 524f754f88fSGreg Clayton 52530c2441aSAleksandr Urakov uint32_t ObjectFilePECOFF::GetRVA(const Address &addr) const { 52630c2441aSAleksandr Urakov return addr.GetFileAddress() - m_image_base; 52730c2441aSAleksandr Urakov } 52830c2441aSAleksandr Urakov 52930c2441aSAleksandr Urakov Address ObjectFilePECOFF::GetAddress(uint32_t rva) { 53030c2441aSAleksandr Urakov SectionList *sect_list = GetSectionList(); 53130c2441aSAleksandr Urakov if (!sect_list) 53230c2441aSAleksandr Urakov return Address(GetFileAddress(rva)); 53330c2441aSAleksandr Urakov 53430c2441aSAleksandr Urakov return Address(GetFileAddress(rva), sect_list); 53530c2441aSAleksandr Urakov } 53630c2441aSAleksandr Urakov 53730c2441aSAleksandr Urakov lldb::addr_t ObjectFilePECOFF::GetFileAddress(uint32_t rva) const { 53830c2441aSAleksandr Urakov return m_image_base + rva; 53930c2441aSAleksandr Urakov } 54030c2441aSAleksandr Urakov 541344546bdSWalter Erquinigo DataExtractor ObjectFilePECOFF::ReadImageData(uint32_t offset, size_t size) { 54230c2441aSAleksandr Urakov if (!size) 54330c2441aSAleksandr Urakov return {}; 54430c2441aSAleksandr Urakov 545344546bdSWalter Erquinigo if (m_file) { 5467f6a7a37SZachary Turner // A bit of a hack, but we intend to write to this buffer, so we can't 5477f6a7a37SZachary Turner // mmap it. 54850251fc7SPavel Labath auto buffer_sp = MapFileData(m_file, size, offset); 549344546bdSWalter Erquinigo return DataExtractor(buffer_sp, GetByteOrder(), GetAddressByteSize()); 550344546bdSWalter Erquinigo } 551344546bdSWalter Erquinigo ProcessSP process_sp(m_process_wp.lock()); 552344546bdSWalter Erquinigo DataExtractor data; 553344546bdSWalter Erquinigo if (process_sp) { 554a8f3ae7cSJonas Devlieghere auto data_up = std::make_unique<DataBufferHeap>(size, 0); 55597206d57SZachary Turner Status readmem_error; 556344546bdSWalter Erquinigo size_t bytes_read = 557d5b44036SJonas Devlieghere process_sp->ReadMemory(m_image_base + offset, data_up->GetBytes(), 558d5b44036SJonas Devlieghere data_up->GetByteSize(), readmem_error); 559344546bdSWalter Erquinigo if (bytes_read == size) { 560d5b44036SJonas Devlieghere DataBufferSP buffer_sp(data_up.release()); 561344546bdSWalter Erquinigo data.SetData(buffer_sp, 0, buffer_sp->GetByteSize()); 562344546bdSWalter Erquinigo } 563344546bdSWalter Erquinigo } 564344546bdSWalter Erquinigo return data; 565344546bdSWalter Erquinigo } 566344546bdSWalter Erquinigo 56730c2441aSAleksandr Urakov DataExtractor ObjectFilePECOFF::ReadImageDataByRVA(uint32_t rva, size_t size) { 56830c2441aSAleksandr Urakov if (m_file) { 56930c2441aSAleksandr Urakov Address addr = GetAddress(rva); 5707e1a3076SMartin Storsjö SectionSP sect = addr.GetSection(); 5717e1a3076SMartin Storsjö if (!sect) 5727e1a3076SMartin Storsjö return {}; 5737e1a3076SMartin Storsjö rva = sect->GetFileOffset() + addr.GetOffset(); 57430c2441aSAleksandr Urakov } 57530c2441aSAleksandr Urakov 57630c2441aSAleksandr Urakov return ReadImageData(rva, size); 57730c2441aSAleksandr Urakov } 57830c2441aSAleksandr Urakov 579f754f88fSGreg Clayton // ParseSectionHeaders 580b9c1b51eSKate Stone bool ObjectFilePECOFF::ParseSectionHeaders( 581b9c1b51eSKate Stone uint32_t section_header_data_offset) { 582f754f88fSGreg Clayton const uint32_t nsects = m_coff_header.nsects; 583f754f88fSGreg Clayton m_sect_headers.clear(); 584f754f88fSGreg Clayton 585b9c1b51eSKate Stone if (nsects > 0) { 586f754f88fSGreg Clayton const size_t section_header_byte_size = nsects * sizeof(section_header_t); 587344546bdSWalter Erquinigo DataExtractor section_header_data = 588344546bdSWalter Erquinigo ReadImageData(section_header_data_offset, section_header_byte_size); 589f754f88fSGreg Clayton 590c7bece56SGreg Clayton lldb::offset_t offset = 0; 591b9c1b51eSKate Stone if (section_header_data.ValidOffsetForDataOfSize( 592b9c1b51eSKate Stone offset, section_header_byte_size)) { 593f754f88fSGreg Clayton m_sect_headers.resize(nsects); 594f754f88fSGreg Clayton 595b9c1b51eSKate Stone for (uint32_t idx = 0; idx < nsects; ++idx) { 596f754f88fSGreg Clayton const void *name_data = section_header_data.GetData(&offset, 8); 597b9c1b51eSKate Stone if (name_data) { 598f754f88fSGreg Clayton memcpy(m_sect_headers[idx].name, name_data, 8); 599f754f88fSGreg Clayton m_sect_headers[idx].vmsize = section_header_data.GetU32(&offset); 600f754f88fSGreg Clayton m_sect_headers[idx].vmaddr = section_header_data.GetU32(&offset); 601f754f88fSGreg Clayton m_sect_headers[idx].size = section_header_data.GetU32(&offset); 602f754f88fSGreg Clayton m_sect_headers[idx].offset = section_header_data.GetU32(&offset); 603f754f88fSGreg Clayton m_sect_headers[idx].reloff = section_header_data.GetU32(&offset); 604f754f88fSGreg Clayton m_sect_headers[idx].lineoff = section_header_data.GetU32(&offset); 605f754f88fSGreg Clayton m_sect_headers[idx].nreloc = section_header_data.GetU16(&offset); 606f754f88fSGreg Clayton m_sect_headers[idx].nline = section_header_data.GetU16(&offset); 607f754f88fSGreg Clayton m_sect_headers[idx].flags = section_header_data.GetU32(&offset); 608f754f88fSGreg Clayton } 609f754f88fSGreg Clayton } 610f754f88fSGreg Clayton } 611f754f88fSGreg Clayton } 612f754f88fSGreg Clayton 613a6682a41SJonas Devlieghere return !m_sect_headers.empty(); 614f754f88fSGreg Clayton } 615f754f88fSGreg Clayton 6162886e4a0SPavel Labath llvm::StringRef ObjectFilePECOFF::GetSectionName(const section_header_t §) { 6172886e4a0SPavel Labath llvm::StringRef hdr_name(sect.name, llvm::array_lengthof(sect.name)); 6182886e4a0SPavel Labath hdr_name = hdr_name.split('\0').first; 6192886e4a0SPavel Labath if (hdr_name.consume_front("/")) { 6202886e4a0SPavel Labath lldb::offset_t stroff; 6212886e4a0SPavel Labath if (!to_integer(hdr_name, stroff, 10)) 6222886e4a0SPavel Labath return ""; 623b9c1b51eSKate Stone lldb::offset_t string_file_offset = 624b9c1b51eSKate Stone m_coff_header.symoff + (m_coff_header.nsyms * 18) + stroff; 6252886e4a0SPavel Labath if (const char *name = m_data.GetCStr(&string_file_offset)) 6262886e4a0SPavel Labath return name; 6272886e4a0SPavel Labath return ""; 628f754f88fSGreg Clayton } 6292886e4a0SPavel Labath return hdr_name; 630f754f88fSGreg Clayton } 631f754f88fSGreg Clayton 632f754f88fSGreg Clayton // GetNListSymtab 633b9c1b51eSKate Stone Symtab *ObjectFilePECOFF::GetSymtab() { 634a1743499SGreg Clayton ModuleSP module_sp(GetModule()); 635b9c1b51eSKate Stone if (module_sp) { 63616ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 637248a1305SKonrad Kleine if (m_symtab_up == nullptr) { 638f754f88fSGreg Clayton SectionList *sect_list = GetSectionList(); 639d5b44036SJonas Devlieghere m_symtab_up.reset(new Symtab(this)); 640d5b44036SJonas Devlieghere std::lock_guard<std::recursive_mutex> guard(m_symtab_up->GetMutex()); 64128469ca3SGreg Clayton 64228469ca3SGreg Clayton const uint32_t num_syms = m_coff_header.nsyms; 64328469ca3SGreg Clayton 644344546bdSWalter Erquinigo if (m_file && num_syms > 0 && m_coff_header.symoff > 0) { 6450076e715SGreg Clayton const uint32_t symbol_size = 18; 64628469ca3SGreg Clayton const size_t symbol_data_size = num_syms * symbol_size; 647c35b91ceSAdrian McCarthy // Include the 4-byte string table size at the end of the symbols 648344546bdSWalter Erquinigo DataExtractor symtab_data = 649344546bdSWalter Erquinigo ReadImageData(m_coff_header.symoff, symbol_data_size + 4); 650c7bece56SGreg Clayton lldb::offset_t offset = symbol_data_size; 65128469ca3SGreg Clayton const uint32_t strtab_size = symtab_data.GetU32(&offset); 652344546bdSWalter Erquinigo if (strtab_size > 0) { 653344546bdSWalter Erquinigo DataExtractor strtab_data = ReadImageData( 654344546bdSWalter Erquinigo m_coff_header.symoff + symbol_data_size, strtab_size); 65528469ca3SGreg Clayton 6560076e715SGreg Clayton // First 4 bytes should be zeroed after strtab_size has been read, 6570076e715SGreg Clayton // because it is used as offset 0 to encode a NULL string. 658f76e099cSSaleem Abdulrasool uint32_t *strtab_data_start = const_cast<uint32_t *>( 659f76e099cSSaleem Abdulrasool reinterpret_cast<const uint32_t *>(strtab_data.GetDataStart())); 6600076e715SGreg Clayton strtab_data_start[0] = 0; 6610076e715SGreg Clayton 66228469ca3SGreg Clayton offset = 0; 66328469ca3SGreg Clayton std::string symbol_name; 664d5b44036SJonas Devlieghere Symbol *symbols = m_symtab_up->Resize(num_syms); 665b9c1b51eSKate Stone for (uint32_t i = 0; i < num_syms; ++i) { 666f754f88fSGreg Clayton coff_symbol_t symbol; 66728469ca3SGreg Clayton const uint32_t symbol_offset = offset; 668248a1305SKonrad Kleine const char *symbol_name_cstr = nullptr; 669c35b91ceSAdrian McCarthy // If the first 4 bytes of the symbol string are zero, then they 670c35b91ceSAdrian McCarthy // are followed by a 4-byte string table offset. Else these 67128469ca3SGreg Clayton // 8 bytes contain the symbol name 672b9c1b51eSKate Stone if (symtab_data.GetU32(&offset) == 0) { 67305097246SAdrian Prantl // Long string that doesn't fit into the symbol table name, so 67405097246SAdrian Prantl // now we must read the 4 byte string table offset 67528469ca3SGreg Clayton uint32_t strtab_offset = symtab_data.GetU32(&offset); 67628469ca3SGreg Clayton symbol_name_cstr = strtab_data.PeekCStr(strtab_offset); 67728469ca3SGreg Clayton symbol_name.assign(symbol_name_cstr); 678b9c1b51eSKate Stone } else { 679b9c1b51eSKate Stone // Short string that fits into the symbol table name which is 8 680b9c1b51eSKate Stone // bytes 68128469ca3SGreg Clayton offset += sizeof(symbol.name) - 4; // Skip remaining 68228469ca3SGreg Clayton symbol_name_cstr = symtab_data.PeekCStr(symbol_offset); 683248a1305SKonrad Kleine if (symbol_name_cstr == nullptr) 684f754f88fSGreg Clayton break; 68528469ca3SGreg Clayton symbol_name.assign(symbol_name_cstr, sizeof(symbol.name)); 68628469ca3SGreg Clayton } 68728469ca3SGreg Clayton symbol.value = symtab_data.GetU32(&offset); 68828469ca3SGreg Clayton symbol.sect = symtab_data.GetU16(&offset); 68928469ca3SGreg Clayton symbol.type = symtab_data.GetU16(&offset); 69028469ca3SGreg Clayton symbol.storage = symtab_data.GetU8(&offset); 69128469ca3SGreg Clayton symbol.naux = symtab_data.GetU8(&offset); 692037520e9SGreg Clayton symbols[i].GetMangled().SetValue(ConstString(symbol_name.c_str())); 693b9c1b51eSKate Stone if ((int16_t)symbol.sect >= 1) { 6944394b5beSMartin Storsjö Address symbol_addr(sect_list->FindSectionByID(symbol.sect), 695b9c1b51eSKate Stone symbol.value); 696358cf1eaSGreg Clayton symbols[i].GetAddressRef() = symbol_addr; 697c35b91ceSAdrian McCarthy symbols[i].SetType(MapSymbolType(symbol.type)); 6980076e715SGreg Clayton } 699f754f88fSGreg Clayton 700b9c1b51eSKate Stone if (symbol.naux > 0) { 701f754f88fSGreg Clayton i += symbol.naux; 7020076e715SGreg Clayton offset += symbol_size; 7030076e715SGreg Clayton } 704f754f88fSGreg Clayton } 705f754f88fSGreg Clayton } 706344546bdSWalter Erquinigo } 707a4fe3a12SVirgile Bello 708a4fe3a12SVirgile Bello // Read export header 709b9c1b51eSKate Stone if (coff_data_dir_export_table < m_coff_header_opt.data_dirs.size() && 710b9c1b51eSKate Stone m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmsize > 0 && 711b9c1b51eSKate Stone m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmaddr > 0) { 712a4fe3a12SVirgile Bello export_directory_entry export_table; 713b9c1b51eSKate Stone uint32_t data_start = 714b9c1b51eSKate Stone m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmaddr; 715344546bdSWalter Erquinigo 71630c2441aSAleksandr Urakov DataExtractor symtab_data = ReadImageDataByRVA( 71730c2441aSAleksandr Urakov data_start, m_coff_header_opt.data_dirs[0].vmsize); 718a4fe3a12SVirgile Bello lldb::offset_t offset = 0; 719a4fe3a12SVirgile Bello 720a4fe3a12SVirgile Bello // Read export_table header 721a4fe3a12SVirgile Bello export_table.characteristics = symtab_data.GetU32(&offset); 722a4fe3a12SVirgile Bello export_table.time_date_stamp = symtab_data.GetU32(&offset); 723a4fe3a12SVirgile Bello export_table.major_version = symtab_data.GetU16(&offset); 724a4fe3a12SVirgile Bello export_table.minor_version = symtab_data.GetU16(&offset); 725a4fe3a12SVirgile Bello export_table.name = symtab_data.GetU32(&offset); 726a4fe3a12SVirgile Bello export_table.base = symtab_data.GetU32(&offset); 727a4fe3a12SVirgile Bello export_table.number_of_functions = symtab_data.GetU32(&offset); 728a4fe3a12SVirgile Bello export_table.number_of_names = symtab_data.GetU32(&offset); 729a4fe3a12SVirgile Bello export_table.address_of_functions = symtab_data.GetU32(&offset); 730a4fe3a12SVirgile Bello export_table.address_of_names = symtab_data.GetU32(&offset); 731a4fe3a12SVirgile Bello export_table.address_of_name_ordinals = symtab_data.GetU32(&offset); 732a4fe3a12SVirgile Bello 733a4fe3a12SVirgile Bello bool has_ordinal = export_table.address_of_name_ordinals != 0; 734a4fe3a12SVirgile Bello 735a4fe3a12SVirgile Bello lldb::offset_t name_offset = export_table.address_of_names - data_start; 736b9c1b51eSKate Stone lldb::offset_t name_ordinal_offset = 737b9c1b51eSKate Stone export_table.address_of_name_ordinals - data_start; 738a4fe3a12SVirgile Bello 739d5b44036SJonas Devlieghere Symbol *symbols = m_symtab_up->Resize(export_table.number_of_names); 740a4fe3a12SVirgile Bello 741a4fe3a12SVirgile Bello std::string symbol_name; 742a4fe3a12SVirgile Bello 743a4fe3a12SVirgile Bello // Read each export table entry 744b9c1b51eSKate Stone for (size_t i = 0; i < export_table.number_of_names; ++i) { 745b9c1b51eSKate Stone uint32_t name_ordinal = 746b9c1b51eSKate Stone has_ordinal ? symtab_data.GetU16(&name_ordinal_offset) : i; 747a4fe3a12SVirgile Bello uint32_t name_address = symtab_data.GetU32(&name_offset); 748a4fe3a12SVirgile Bello 749b9c1b51eSKate Stone const char *symbol_name_cstr = 750b9c1b51eSKate Stone symtab_data.PeekCStr(name_address - data_start); 751a4fe3a12SVirgile Bello symbol_name.assign(symbol_name_cstr); 752a4fe3a12SVirgile Bello 753b9c1b51eSKate Stone lldb::offset_t function_offset = export_table.address_of_functions - 754b9c1b51eSKate Stone data_start + 755b9c1b51eSKate Stone sizeof(uint32_t) * name_ordinal; 756a4fe3a12SVirgile Bello uint32_t function_rva = symtab_data.GetU32(&function_offset); 757a4fe3a12SVirgile Bello 758b9c1b51eSKate Stone Address symbol_addr(m_coff_header_opt.image_base + function_rva, 759b9c1b51eSKate Stone sect_list); 760a4fe3a12SVirgile Bello symbols[i].GetMangled().SetValue(ConstString(symbol_name.c_str())); 761358cf1eaSGreg Clayton symbols[i].GetAddressRef() = symbol_addr; 762a4fe3a12SVirgile Bello symbols[i].SetType(lldb::eSymbolTypeCode); 763a4fe3a12SVirgile Bello symbols[i].SetDebug(true); 764a4fe3a12SVirgile Bello } 765a4fe3a12SVirgile Bello } 766d5b44036SJonas Devlieghere m_symtab_up->CalculateSymbolSizes(); 767f754f88fSGreg Clayton } 768a1743499SGreg Clayton } 769d5b44036SJonas Devlieghere return m_symtab_up.get(); 770f754f88fSGreg Clayton } 771f754f88fSGreg Clayton 77230c2441aSAleksandr Urakov std::unique_ptr<CallFrameInfo> ObjectFilePECOFF::CreateCallFrameInfo() { 77330c2441aSAleksandr Urakov if (coff_data_dir_exception_table >= m_coff_header_opt.data_dirs.size()) 77430c2441aSAleksandr Urakov return {}; 77530c2441aSAleksandr Urakov 77630c2441aSAleksandr Urakov data_directory data_dir_exception = 77730c2441aSAleksandr Urakov m_coff_header_opt.data_dirs[coff_data_dir_exception_table]; 77830c2441aSAleksandr Urakov if (!data_dir_exception.vmaddr) 77930c2441aSAleksandr Urakov return {}; 78030c2441aSAleksandr Urakov 78130c2441aSAleksandr Urakov return std::make_unique<PECallFrameInfo>(*this, data_dir_exception.vmaddr, 78230c2441aSAleksandr Urakov data_dir_exception.vmsize); 78330c2441aSAleksandr Urakov } 78430c2441aSAleksandr Urakov 785b9c1b51eSKate Stone bool ObjectFilePECOFF::IsStripped() { 7863046e668SGreg Clayton // TODO: determine this for COFF 7873046e668SGreg Clayton return false; 7883046e668SGreg Clayton } 7893046e668SGreg Clayton 790*2e5bb6d8SMartin Storsjö SectionType ObjectFilePECOFF::GetSectionType(llvm::StringRef sect_name, 791*2e5bb6d8SMartin Storsjö const section_header_t §) { 792*2e5bb6d8SMartin Storsjö ConstString const_sect_name(sect_name); 793*2e5bb6d8SMartin Storsjö static ConstString g_code_sect_name(".code"); 794*2e5bb6d8SMartin Storsjö static ConstString g_CODE_sect_name("CODE"); 795*2e5bb6d8SMartin Storsjö static ConstString g_data_sect_name(".data"); 796*2e5bb6d8SMartin Storsjö static ConstString g_DATA_sect_name("DATA"); 797*2e5bb6d8SMartin Storsjö static ConstString g_bss_sect_name(".bss"); 798*2e5bb6d8SMartin Storsjö static ConstString g_BSS_sect_name("BSS"); 799*2e5bb6d8SMartin Storsjö 800*2e5bb6d8SMartin Storsjö if (sect.flags & llvm::COFF::IMAGE_SCN_CNT_CODE && 801*2e5bb6d8SMartin Storsjö ((const_sect_name == g_code_sect_name) || 802*2e5bb6d8SMartin Storsjö (const_sect_name == g_CODE_sect_name))) { 803*2e5bb6d8SMartin Storsjö return eSectionTypeCode; 804*2e5bb6d8SMartin Storsjö } 805*2e5bb6d8SMartin Storsjö if (sect.flags & llvm::COFF::IMAGE_SCN_CNT_INITIALIZED_DATA && 806*2e5bb6d8SMartin Storsjö ((const_sect_name == g_data_sect_name) || 807*2e5bb6d8SMartin Storsjö (const_sect_name == g_DATA_sect_name))) { 808*2e5bb6d8SMartin Storsjö if (sect.size == 0 && sect.offset == 0) 809*2e5bb6d8SMartin Storsjö return eSectionTypeZeroFill; 810*2e5bb6d8SMartin Storsjö else 811*2e5bb6d8SMartin Storsjö return eSectionTypeData; 812*2e5bb6d8SMartin Storsjö } 813*2e5bb6d8SMartin Storsjö if (sect.flags & llvm::COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA && 814*2e5bb6d8SMartin Storsjö ((const_sect_name == g_bss_sect_name) || 815*2e5bb6d8SMartin Storsjö (const_sect_name == g_BSS_sect_name))) { 816*2e5bb6d8SMartin Storsjö if (sect.size == 0) 817*2e5bb6d8SMartin Storsjö return eSectionTypeZeroFill; 818*2e5bb6d8SMartin Storsjö else 819*2e5bb6d8SMartin Storsjö return eSectionTypeData; 820*2e5bb6d8SMartin Storsjö } 821*2e5bb6d8SMartin Storsjö 822*2e5bb6d8SMartin Storsjö SectionType section_type = 823*2e5bb6d8SMartin Storsjö llvm::StringSwitch<SectionType>(sect_name) 824*2e5bb6d8SMartin Storsjö .Case(".debug", eSectionTypeDebug) 825*2e5bb6d8SMartin Storsjö .Case(".stabstr", eSectionTypeDataCString) 826*2e5bb6d8SMartin Storsjö .Case(".reloc", eSectionTypeOther) 827*2e5bb6d8SMartin Storsjö .Case(".debug_abbrev", eSectionTypeDWARFDebugAbbrev) 828*2e5bb6d8SMartin Storsjö .Case(".debug_aranges", eSectionTypeDWARFDebugAranges) 829*2e5bb6d8SMartin Storsjö .Case(".debug_frame", eSectionTypeDWARFDebugFrame) 830*2e5bb6d8SMartin Storsjö .Case(".debug_info", eSectionTypeDWARFDebugInfo) 831*2e5bb6d8SMartin Storsjö .Case(".debug_line", eSectionTypeDWARFDebugLine) 832*2e5bb6d8SMartin Storsjö .Case(".debug_loc", eSectionTypeDWARFDebugLoc) 833*2e5bb6d8SMartin Storsjö .Case(".debug_loclists", eSectionTypeDWARFDebugLocLists) 834*2e5bb6d8SMartin Storsjö .Case(".debug_macinfo", eSectionTypeDWARFDebugMacInfo) 835*2e5bb6d8SMartin Storsjö .Case(".debug_names", eSectionTypeDWARFDebugNames) 836*2e5bb6d8SMartin Storsjö .Case(".debug_pubnames", eSectionTypeDWARFDebugPubNames) 837*2e5bb6d8SMartin Storsjö .Case(".debug_pubtypes", eSectionTypeDWARFDebugPubTypes) 838*2e5bb6d8SMartin Storsjö .Case(".debug_ranges", eSectionTypeDWARFDebugRanges) 839*2e5bb6d8SMartin Storsjö .Case(".debug_str", eSectionTypeDWARFDebugStr) 840*2e5bb6d8SMartin Storsjö .Case(".debug_types", eSectionTypeDWARFDebugTypes) 841*2e5bb6d8SMartin Storsjö .Case(".eh_frame", eSectionTypeEHFrame) 842*2e5bb6d8SMartin Storsjö .Case(".gosymtab", eSectionTypeGoSymtab) 843*2e5bb6d8SMartin Storsjö .Default(eSectionTypeInvalid); 844*2e5bb6d8SMartin Storsjö if (section_type != eSectionTypeInvalid) 845*2e5bb6d8SMartin Storsjö return section_type; 846*2e5bb6d8SMartin Storsjö 847*2e5bb6d8SMartin Storsjö if (sect.flags & llvm::COFF::IMAGE_SCN_CNT_CODE) 848*2e5bb6d8SMartin Storsjö return eSectionTypeCode; 849*2e5bb6d8SMartin Storsjö if (sect.flags & llvm::COFF::IMAGE_SCN_CNT_INITIALIZED_DATA) 850*2e5bb6d8SMartin Storsjö return eSectionTypeData; 851*2e5bb6d8SMartin Storsjö if (sect.flags & llvm::COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) { 852*2e5bb6d8SMartin Storsjö if (sect.size == 0) 853*2e5bb6d8SMartin Storsjö return eSectionTypeZeroFill; 854*2e5bb6d8SMartin Storsjö else 855*2e5bb6d8SMartin Storsjö return eSectionTypeData; 856*2e5bb6d8SMartin Storsjö } 857*2e5bb6d8SMartin Storsjö return eSectionTypeOther; 858*2e5bb6d8SMartin Storsjö } 859*2e5bb6d8SMartin Storsjö 860b9c1b51eSKate Stone void ObjectFilePECOFF::CreateSections(SectionList &unified_section_list) { 861d5b44036SJonas Devlieghere if (m_sections_up) 86288a2c2a4SPavel Labath return; 863d5b44036SJonas Devlieghere m_sections_up.reset(new SectionList()); 8643046e668SGreg Clayton 865a1743499SGreg Clayton ModuleSP module_sp(GetModule()); 866b9c1b51eSKate Stone if (module_sp) { 86716ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 8687db8b5c4SPavel Labath 86973a7a55cSPavel Labath SectionSP header_sp = std::make_shared<Section>( 87073a7a55cSPavel Labath module_sp, this, ~user_id_t(0), ConstString("PECOFF header"), 87173a7a55cSPavel Labath eSectionTypeOther, m_coff_header_opt.image_base, 87273a7a55cSPavel Labath m_coff_header_opt.header_size, 87373a7a55cSPavel Labath /*file_offset*/ 0, m_coff_header_opt.header_size, 87473a7a55cSPavel Labath m_coff_header_opt.sect_alignment, 8757db8b5c4SPavel Labath /*flags*/ 0); 876f1e0ae34SPavel Labath header_sp->SetPermissions(ePermissionsReadable); 87773a7a55cSPavel Labath m_sections_up->AddSection(header_sp); 87873a7a55cSPavel Labath unified_section_list.AddSection(header_sp); 8797db8b5c4SPavel Labath 880f754f88fSGreg Clayton const uint32_t nsects = m_sect_headers.size(); 881e72dfb32SGreg Clayton ModuleSP module_sp(GetModule()); 882b9c1b51eSKate Stone for (uint32_t idx = 0; idx < nsects; ++idx) { 883*2e5bb6d8SMartin Storsjö llvm::StringRef sect_name = GetSectionName(m_sect_headers[idx]); 884*2e5bb6d8SMartin Storsjö ConstString const_sect_name(sect_name); 885*2e5bb6d8SMartin Storsjö SectionType section_type = GetSectionType(sect_name, m_sect_headers[idx]); 886f754f88fSGreg Clayton 887b9c1b51eSKate Stone SectionSP section_sp(new Section( 888b9c1b51eSKate Stone module_sp, // Module to which this section belongs 889a7499c98SMichael Sartain this, // Object file to which this section belongs 8907db8b5c4SPavel Labath idx + 1, // Section ID is the 1 based section index. 891f754f88fSGreg Clayton const_sect_name, // Name of this section 8927db8b5c4SPavel Labath section_type, 89373a7a55cSPavel Labath m_coff_header_opt.image_base + 894b9c1b51eSKate Stone m_sect_headers[idx].vmaddr, // File VM address == addresses as 895b9c1b51eSKate Stone // they are found in the object file 896f754f88fSGreg Clayton m_sect_headers[idx].vmsize, // VM size in bytes of this section 897b9c1b51eSKate Stone m_sect_headers[idx] 898b9c1b51eSKate Stone .offset, // Offset to the data for this section in the file 899b9c1b51eSKate Stone m_sect_headers[idx] 900b9c1b51eSKate Stone .size, // Size in bytes of this section as found in the file 90148672afbSGreg Clayton m_coff_header_opt.sect_alignment, // Section alignment 902f754f88fSGreg Clayton m_sect_headers[idx].flags)); // Flags for this section 903f754f88fSGreg Clayton 904f1e0ae34SPavel Labath uint32_t permissions = 0; 905f1e0ae34SPavel Labath if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_MEM_EXECUTE) 906f1e0ae34SPavel Labath permissions |= ePermissionsExecutable; 907f1e0ae34SPavel Labath if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_MEM_READ) 908f1e0ae34SPavel Labath permissions |= ePermissionsReadable; 909f1e0ae34SPavel Labath if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_MEM_WRITE) 910f1e0ae34SPavel Labath permissions |= ePermissionsWritable; 911f1e0ae34SPavel Labath section_sp->SetPermissions(permissions); 912f1e0ae34SPavel Labath 91373a7a55cSPavel Labath m_sections_up->AddSection(section_sp); 91473a7a55cSPavel Labath unified_section_list.AddSection(section_sp); 915f754f88fSGreg Clayton } 916f754f88fSGreg Clayton } 917a1743499SGreg Clayton } 918f754f88fSGreg Clayton 919b8d03935SAaron Smith UUID ObjectFilePECOFF::GetUUID() { 920b8d03935SAaron Smith if (m_uuid.IsValid()) 921b8d03935SAaron Smith return m_uuid; 922b8d03935SAaron Smith 923b8d03935SAaron Smith if (!CreateBinary()) 924b8d03935SAaron Smith return UUID(); 925b8d03935SAaron Smith 926b8d03935SAaron Smith auto COFFObj = 927b8d03935SAaron Smith llvm::cast<llvm::object::COFFObjectFile>(m_owningbin->getBinary()); 928b8d03935SAaron Smith 929b8d03935SAaron Smith m_uuid = GetCoffUUID(COFFObj); 930b8d03935SAaron Smith return m_uuid; 931b8d03935SAaron Smith } 932f754f88fSGreg Clayton 933037ed1beSAaron Smith uint32_t ObjectFilePECOFF::ParseDependentModules() { 934037ed1beSAaron Smith ModuleSP module_sp(GetModule()); 935037ed1beSAaron Smith if (!module_sp) 936f754f88fSGreg Clayton return 0; 937037ed1beSAaron Smith 938037ed1beSAaron Smith std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 939037ed1beSAaron Smith if (m_deps_filespec) 940037ed1beSAaron Smith return m_deps_filespec->GetSize(); 941037ed1beSAaron Smith 942037ed1beSAaron Smith // Cache coff binary if it is not done yet. 943037ed1beSAaron Smith if (!CreateBinary()) 944037ed1beSAaron Smith return 0; 945037ed1beSAaron Smith 946037ed1beSAaron Smith Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT)); 94763e5fb76SJonas Devlieghere LLDB_LOGF(log, 94863e5fb76SJonas Devlieghere "%p ObjectFilePECOFF::ParseDependentModules() module = %p " 949037ed1beSAaron Smith "(%s), binary = %p (Bin = %p)", 950037ed1beSAaron Smith static_cast<void *>(this), static_cast<void *>(module_sp.get()), 951037ed1beSAaron Smith module_sp->GetSpecificationDescription().c_str(), 952037ed1beSAaron Smith static_cast<void *>(m_owningbin.getPointer()), 953b8d03935SAaron Smith static_cast<void *>(m_owningbin->getBinary())); 954037ed1beSAaron Smith 955037ed1beSAaron Smith auto COFFObj = 956037ed1beSAaron Smith llvm::dyn_cast<llvm::object::COFFObjectFile>(m_owningbin->getBinary()); 957037ed1beSAaron Smith if (!COFFObj) 958037ed1beSAaron Smith return 0; 959037ed1beSAaron Smith 960037ed1beSAaron Smith m_deps_filespec = FileSpecList(); 961037ed1beSAaron Smith 962037ed1beSAaron Smith for (const auto &entry : COFFObj->import_directories()) { 963037ed1beSAaron Smith llvm::StringRef dll_name; 964037ed1beSAaron Smith auto ec = entry.getName(dll_name); 965037ed1beSAaron Smith // Report a bogus entry. 966037ed1beSAaron Smith if (ec != std::error_code()) { 96763e5fb76SJonas Devlieghere LLDB_LOGF(log, 96863e5fb76SJonas Devlieghere "ObjectFilePECOFF::ParseDependentModules() - failed to get " 969037ed1beSAaron Smith "import directory entry name: %s", 970037ed1beSAaron Smith ec.message().c_str()); 971037ed1beSAaron Smith continue; 972037ed1beSAaron Smith } 973037ed1beSAaron Smith 974037ed1beSAaron Smith // At this moment we only have the base name of the DLL. The full path can 975037ed1beSAaron Smith // only be seen after the dynamic loading. Our best guess is Try to get it 976037ed1beSAaron Smith // with the help of the object file's directory. 977b3f44ad9SStella Stamenova llvm::SmallString<128> dll_fullpath; 978037ed1beSAaron Smith FileSpec dll_specs(dll_name); 979037ed1beSAaron Smith dll_specs.GetDirectory().SetString(m_file.GetDirectory().GetCString()); 980037ed1beSAaron Smith 981037ed1beSAaron Smith if (!llvm::sys::fs::real_path(dll_specs.GetPath(), dll_fullpath)) 982f893d5bfSJonas Devlieghere m_deps_filespec->EmplaceBack(dll_fullpath); 983037ed1beSAaron Smith else { 984037ed1beSAaron Smith // Known DLLs or DLL not found in the object file directory. 985f893d5bfSJonas Devlieghere m_deps_filespec->EmplaceBack(dll_name); 986037ed1beSAaron Smith } 987037ed1beSAaron Smith } 988037ed1beSAaron Smith return m_deps_filespec->GetSize(); 989037ed1beSAaron Smith } 990037ed1beSAaron Smith 991037ed1beSAaron Smith uint32_t ObjectFilePECOFF::GetDependentModules(FileSpecList &files) { 992037ed1beSAaron Smith auto num_modules = ParseDependentModules(); 993037ed1beSAaron Smith auto original_size = files.GetSize(); 994037ed1beSAaron Smith 995037ed1beSAaron Smith for (unsigned i = 0; i < num_modules; ++i) 996037ed1beSAaron Smith files.AppendIfUnique(m_deps_filespec->GetFileSpecAtIndex(i)); 997037ed1beSAaron Smith 998037ed1beSAaron Smith return files.GetSize() - original_size; 999f754f88fSGreg Clayton } 1000f754f88fSGreg Clayton 1001b9c1b51eSKate Stone lldb_private::Address ObjectFilePECOFF::GetEntryPointAddress() { 10028e38c666SStephane Sezer if (m_entry_point_address.IsValid()) 10038e38c666SStephane Sezer return m_entry_point_address; 10048e38c666SStephane Sezer 10058e38c666SStephane Sezer if (!ParseHeader() || !IsExecutable()) 10068e38c666SStephane Sezer return m_entry_point_address; 10078e38c666SStephane Sezer 10088e38c666SStephane Sezer SectionList *section_list = GetSectionList(); 1009a5235af9SAleksandr Urakov addr_t file_addr = m_coff_header_opt.entry + m_coff_header_opt.image_base; 10108e38c666SStephane Sezer 10118e38c666SStephane Sezer if (!section_list) 1012a5235af9SAleksandr Urakov m_entry_point_address.SetOffset(file_addr); 10138e38c666SStephane Sezer else 1014b8d03935SAaron Smith m_entry_point_address.ResolveAddressUsingFileSections(file_addr, 1015b8d03935SAaron Smith section_list); 10168e38c666SStephane Sezer return m_entry_point_address; 10178e38c666SStephane Sezer } 10188e38c666SStephane Sezer 1019d1304bbaSPavel Labath Address ObjectFilePECOFF::GetBaseAddress() { 1020d1304bbaSPavel Labath return Address(GetSectionList()->GetSectionAtIndex(0), 0); 1021d1304bbaSPavel Labath } 1022d1304bbaSPavel Labath 1023f754f88fSGreg Clayton // Dump 1024f754f88fSGreg Clayton // 1025f754f88fSGreg Clayton // Dump the specifics of the runtime file container (such as any headers 1026f754f88fSGreg Clayton // segments, sections, etc). 1027b9c1b51eSKate Stone void ObjectFilePECOFF::Dump(Stream *s) { 1028a1743499SGreg Clayton ModuleSP module_sp(GetModule()); 1029b9c1b51eSKate Stone if (module_sp) { 103016ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 1031324a1036SSaleem Abdulrasool s->Printf("%p: ", static_cast<void *>(this)); 1032f754f88fSGreg Clayton s->Indent(); 1033f754f88fSGreg Clayton s->PutCString("ObjectFilePECOFF"); 1034f754f88fSGreg Clayton 1035f760f5aeSPavel Labath ArchSpec header_arch = GetArchitecture(); 1036f754f88fSGreg Clayton 1037b9c1b51eSKate Stone *s << ", file = '" << m_file 1038b9c1b51eSKate Stone << "', arch = " << header_arch.GetArchitectureName() << "\n"; 1039f754f88fSGreg Clayton 10403046e668SGreg Clayton SectionList *sections = GetSectionList(); 10413046e668SGreg Clayton if (sections) 1042248a1305SKonrad Kleine sections->Dump(s, nullptr, true, UINT32_MAX); 1043f754f88fSGreg Clayton 1044d5b44036SJonas Devlieghere if (m_symtab_up) 1045248a1305SKonrad Kleine m_symtab_up->Dump(s, nullptr, eSortOrderNone); 1046f754f88fSGreg Clayton 1047f754f88fSGreg Clayton if (m_dos_header.e_magic) 1048f754f88fSGreg Clayton DumpDOSHeader(s, m_dos_header); 1049b9c1b51eSKate Stone if (m_coff_header.machine) { 1050f754f88fSGreg Clayton DumpCOFFHeader(s, m_coff_header); 1051f754f88fSGreg Clayton if (m_coff_header.hdrsize) 1052f754f88fSGreg Clayton DumpOptCOFFHeader(s, m_coff_header_opt); 1053f754f88fSGreg Clayton } 1054f754f88fSGreg Clayton s->EOL(); 1055f754f88fSGreg Clayton DumpSectionHeaders(s); 1056f754f88fSGreg Clayton s->EOL(); 1057037ed1beSAaron Smith 1058037ed1beSAaron Smith DumpDependentModules(s); 1059037ed1beSAaron Smith s->EOL(); 1060f754f88fSGreg Clayton } 1061a1743499SGreg Clayton } 1062f754f88fSGreg Clayton 1063f754f88fSGreg Clayton // DumpDOSHeader 1064f754f88fSGreg Clayton // 1065f754f88fSGreg Clayton // Dump the MS-DOS header to the specified output stream 1066b9c1b51eSKate Stone void ObjectFilePECOFF::DumpDOSHeader(Stream *s, const dos_header_t &header) { 1067f754f88fSGreg Clayton s->PutCString("MSDOS Header\n"); 1068f754f88fSGreg Clayton s->Printf(" e_magic = 0x%4.4x\n", header.e_magic); 1069f754f88fSGreg Clayton s->Printf(" e_cblp = 0x%4.4x\n", header.e_cblp); 1070f754f88fSGreg Clayton s->Printf(" e_cp = 0x%4.4x\n", header.e_cp); 1071f754f88fSGreg Clayton s->Printf(" e_crlc = 0x%4.4x\n", header.e_crlc); 1072f754f88fSGreg Clayton s->Printf(" e_cparhdr = 0x%4.4x\n", header.e_cparhdr); 1073f754f88fSGreg Clayton s->Printf(" e_minalloc = 0x%4.4x\n", header.e_minalloc); 1074f754f88fSGreg Clayton s->Printf(" e_maxalloc = 0x%4.4x\n", header.e_maxalloc); 1075f754f88fSGreg Clayton s->Printf(" e_ss = 0x%4.4x\n", header.e_ss); 1076f754f88fSGreg Clayton s->Printf(" e_sp = 0x%4.4x\n", header.e_sp); 1077f754f88fSGreg Clayton s->Printf(" e_csum = 0x%4.4x\n", header.e_csum); 1078f754f88fSGreg Clayton s->Printf(" e_ip = 0x%4.4x\n", header.e_ip); 1079f754f88fSGreg Clayton s->Printf(" e_cs = 0x%4.4x\n", header.e_cs); 1080f754f88fSGreg Clayton s->Printf(" e_lfarlc = 0x%4.4x\n", header.e_lfarlc); 1081f754f88fSGreg Clayton s->Printf(" e_ovno = 0x%4.4x\n", header.e_ovno); 1082f754f88fSGreg Clayton s->Printf(" e_res[4] = { 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x }\n", 1083b9c1b51eSKate Stone header.e_res[0], header.e_res[1], header.e_res[2], header.e_res[3]); 1084f754f88fSGreg Clayton s->Printf(" e_oemid = 0x%4.4x\n", header.e_oemid); 1085f754f88fSGreg Clayton s->Printf(" e_oeminfo = 0x%4.4x\n", header.e_oeminfo); 1086b9c1b51eSKate Stone s->Printf(" e_res2[10] = { 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, " 1087b9c1b51eSKate Stone "0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x }\n", 1088b9c1b51eSKate Stone header.e_res2[0], header.e_res2[1], header.e_res2[2], 1089b9c1b51eSKate Stone header.e_res2[3], header.e_res2[4], header.e_res2[5], 1090b9c1b51eSKate Stone header.e_res2[6], header.e_res2[7], header.e_res2[8], 1091f754f88fSGreg Clayton header.e_res2[9]); 1092f754f88fSGreg Clayton s->Printf(" e_lfanew = 0x%8.8x\n", header.e_lfanew); 1093f754f88fSGreg Clayton } 1094f754f88fSGreg Clayton 1095f754f88fSGreg Clayton // DumpCOFFHeader 1096f754f88fSGreg Clayton // 1097f754f88fSGreg Clayton // Dump the COFF header to the specified output stream 1098b9c1b51eSKate Stone void ObjectFilePECOFF::DumpCOFFHeader(Stream *s, const coff_header_t &header) { 1099f754f88fSGreg Clayton s->PutCString("COFF Header\n"); 1100f754f88fSGreg Clayton s->Printf(" machine = 0x%4.4x\n", header.machine); 1101f754f88fSGreg Clayton s->Printf(" nsects = 0x%4.4x\n", header.nsects); 1102f754f88fSGreg Clayton s->Printf(" modtime = 0x%8.8x\n", header.modtime); 1103f754f88fSGreg Clayton s->Printf(" symoff = 0x%8.8x\n", header.symoff); 1104f754f88fSGreg Clayton s->Printf(" nsyms = 0x%8.8x\n", header.nsyms); 1105f754f88fSGreg Clayton s->Printf(" hdrsize = 0x%4.4x\n", header.hdrsize); 1106f754f88fSGreg Clayton } 1107f754f88fSGreg Clayton 1108f754f88fSGreg Clayton // DumpOptCOFFHeader 1109f754f88fSGreg Clayton // 1110f754f88fSGreg Clayton // Dump the optional COFF header to the specified output stream 1111b9c1b51eSKate Stone void ObjectFilePECOFF::DumpOptCOFFHeader(Stream *s, 1112b9c1b51eSKate Stone const coff_opt_header_t &header) { 1113f754f88fSGreg Clayton s->PutCString("Optional COFF Header\n"); 1114f754f88fSGreg Clayton s->Printf(" magic = 0x%4.4x\n", header.magic); 1115b9c1b51eSKate Stone s->Printf(" major_linker_version = 0x%2.2x\n", 1116b9c1b51eSKate Stone header.major_linker_version); 1117b9c1b51eSKate Stone s->Printf(" minor_linker_version = 0x%2.2x\n", 1118b9c1b51eSKate Stone header.minor_linker_version); 1119f754f88fSGreg Clayton s->Printf(" code_size = 0x%8.8x\n", header.code_size); 1120f754f88fSGreg Clayton s->Printf(" data_size = 0x%8.8x\n", header.data_size); 1121f754f88fSGreg Clayton s->Printf(" bss_size = 0x%8.8x\n", header.bss_size); 1122f754f88fSGreg Clayton s->Printf(" entry = 0x%8.8x\n", header.entry); 1123f754f88fSGreg Clayton s->Printf(" code_offset = 0x%8.8x\n", header.code_offset); 1124f754f88fSGreg Clayton s->Printf(" data_offset = 0x%8.8x\n", header.data_offset); 1125b9c1b51eSKate Stone s->Printf(" image_base = 0x%16.16" PRIx64 "\n", 1126b9c1b51eSKate Stone header.image_base); 1127f754f88fSGreg Clayton s->Printf(" sect_alignment = 0x%8.8x\n", header.sect_alignment); 1128f754f88fSGreg Clayton s->Printf(" file_alignment = 0x%8.8x\n", header.file_alignment); 1129b9c1b51eSKate Stone s->Printf(" major_os_system_version = 0x%4.4x\n", 1130b9c1b51eSKate Stone header.major_os_system_version); 1131b9c1b51eSKate Stone s->Printf(" minor_os_system_version = 0x%4.4x\n", 1132b9c1b51eSKate Stone header.minor_os_system_version); 1133b9c1b51eSKate Stone s->Printf(" major_image_version = 0x%4.4x\n", 1134b9c1b51eSKate Stone header.major_image_version); 1135b9c1b51eSKate Stone s->Printf(" minor_image_version = 0x%4.4x\n", 1136b9c1b51eSKate Stone header.minor_image_version); 1137b9c1b51eSKate Stone s->Printf(" major_subsystem_version = 0x%4.4x\n", 1138b9c1b51eSKate Stone header.major_subsystem_version); 1139b9c1b51eSKate Stone s->Printf(" minor_subsystem_version = 0x%4.4x\n", 1140b9c1b51eSKate Stone header.minor_subsystem_version); 1141f754f88fSGreg Clayton s->Printf(" reserved1 = 0x%8.8x\n", header.reserved1); 1142f754f88fSGreg Clayton s->Printf(" image_size = 0x%8.8x\n", header.image_size); 1143f754f88fSGreg Clayton s->Printf(" header_size = 0x%8.8x\n", header.header_size); 114428469ca3SGreg Clayton s->Printf(" checksum = 0x%8.8x\n", header.checksum); 1145f754f88fSGreg Clayton s->Printf(" subsystem = 0x%4.4x\n", header.subsystem); 1146f754f88fSGreg Clayton s->Printf(" dll_flags = 0x%4.4x\n", header.dll_flags); 1147b9c1b51eSKate Stone s->Printf(" stack_reserve_size = 0x%16.16" PRIx64 "\n", 1148b9c1b51eSKate Stone header.stack_reserve_size); 1149b9c1b51eSKate Stone s->Printf(" stack_commit_size = 0x%16.16" PRIx64 "\n", 1150b9c1b51eSKate Stone header.stack_commit_size); 1151b9c1b51eSKate Stone s->Printf(" heap_reserve_size = 0x%16.16" PRIx64 "\n", 1152b9c1b51eSKate Stone header.heap_reserve_size); 1153b9c1b51eSKate Stone s->Printf(" heap_commit_size = 0x%16.16" PRIx64 "\n", 1154b9c1b51eSKate Stone header.heap_commit_size); 1155f754f88fSGreg Clayton s->Printf(" loader_flags = 0x%8.8x\n", header.loader_flags); 1156b9c1b51eSKate Stone s->Printf(" num_data_dir_entries = 0x%8.8x\n", 1157b9c1b51eSKate Stone (uint32_t)header.data_dirs.size()); 1158f754f88fSGreg Clayton uint32_t i; 1159b9c1b51eSKate Stone for (i = 0; i < header.data_dirs.size(); i++) { 1160b9c1b51eSKate Stone s->Printf(" data_dirs[%2u] vmaddr = 0x%8.8x, vmsize = 0x%8.8x\n", i, 1161b9c1b51eSKate Stone header.data_dirs[i].vmaddr, header.data_dirs[i].vmsize); 1162f754f88fSGreg Clayton } 1163f754f88fSGreg Clayton } 1164f754f88fSGreg Clayton // DumpSectionHeader 1165f754f88fSGreg Clayton // 1166f754f88fSGreg Clayton // Dump a single ELF section header to the specified output stream 1167b9c1b51eSKate Stone void ObjectFilePECOFF::DumpSectionHeader(Stream *s, 1168b9c1b51eSKate Stone const section_header_t &sh) { 11692886e4a0SPavel Labath std::string name = GetSectionName(sh); 1170b9c1b51eSKate 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 " 1171b9c1b51eSKate Stone "0x%4.4x 0x%8.8x\n", 1172b9c1b51eSKate Stone name.c_str(), sh.vmaddr, sh.vmsize, sh.offset, sh.size, sh.reloff, 1173b9c1b51eSKate Stone sh.lineoff, sh.nreloc, sh.nline, sh.flags); 1174f754f88fSGreg Clayton } 1175f754f88fSGreg Clayton 1176f754f88fSGreg Clayton // DumpSectionHeaders 1177f754f88fSGreg Clayton // 1178f754f88fSGreg Clayton // Dump all of the ELF section header to the specified output stream 1179b9c1b51eSKate Stone void ObjectFilePECOFF::DumpSectionHeaders(Stream *s) { 1180f754f88fSGreg Clayton 1181f754f88fSGreg Clayton s->PutCString("Section Headers\n"); 1182b9c1b51eSKate Stone s->PutCString("IDX name vm addr vm size file off file " 1183b9c1b51eSKate Stone "size reloc off line off nreloc nline flags\n"); 1184b9c1b51eSKate Stone s->PutCString("==== ---------------- ---------- ---------- ---------- " 1185b9c1b51eSKate Stone "---------- ---------- ---------- ------ ------ ----------\n"); 1186f754f88fSGreg Clayton 1187f754f88fSGreg Clayton uint32_t idx = 0; 1188f754f88fSGreg Clayton SectionHeaderCollIter pos, end = m_sect_headers.end(); 1189f754f88fSGreg Clayton 1190b9c1b51eSKate Stone for (pos = m_sect_headers.begin(); pos != end; ++pos, ++idx) { 1191f754f88fSGreg Clayton s->Printf("[%2u] ", idx); 1192f754f88fSGreg Clayton ObjectFilePECOFF::DumpSectionHeader(s, *pos); 1193f754f88fSGreg Clayton } 1194f754f88fSGreg Clayton } 1195f754f88fSGreg Clayton 1196037ed1beSAaron Smith // DumpDependentModules 1197037ed1beSAaron Smith // 1198037ed1beSAaron Smith // Dump all of the dependent modules to the specified output stream 1199037ed1beSAaron Smith void ObjectFilePECOFF::DumpDependentModules(lldb_private::Stream *s) { 1200037ed1beSAaron Smith auto num_modules = ParseDependentModules(); 1201037ed1beSAaron Smith if (num_modules > 0) { 1202037ed1beSAaron Smith s->PutCString("Dependent Modules\n"); 1203037ed1beSAaron Smith for (unsigned i = 0; i < num_modules; ++i) { 1204037ed1beSAaron Smith auto spec = m_deps_filespec->GetFileSpecAtIndex(i); 1205037ed1beSAaron Smith s->Printf(" %s\n", spec.GetFilename().GetCString()); 1206037ed1beSAaron Smith } 1207037ed1beSAaron Smith } 1208037ed1beSAaron Smith } 1209037ed1beSAaron Smith 1210fb3b3bd1SZachary Turner bool ObjectFilePECOFF::IsWindowsSubsystem() { 1211fb3b3bd1SZachary Turner switch (m_coff_header_opt.subsystem) { 1212fb3b3bd1SZachary Turner case llvm::COFF::IMAGE_SUBSYSTEM_NATIVE: 1213fb3b3bd1SZachary Turner case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_GUI: 1214fb3b3bd1SZachary Turner case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_CUI: 1215fb3b3bd1SZachary Turner case llvm::COFF::IMAGE_SUBSYSTEM_NATIVE_WINDOWS: 1216fb3b3bd1SZachary Turner case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_CE_GUI: 1217fb3b3bd1SZachary Turner case llvm::COFF::IMAGE_SUBSYSTEM_XBOX: 1218fb3b3bd1SZachary Turner case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_BOOT_APPLICATION: 1219fb3b3bd1SZachary Turner return true; 1220fb3b3bd1SZachary Turner default: 1221fb3b3bd1SZachary Turner return false; 1222fb3b3bd1SZachary Turner } 1223fb3b3bd1SZachary Turner } 1224fb3b3bd1SZachary Turner 1225f760f5aeSPavel Labath ArchSpec ObjectFilePECOFF::GetArchitecture() { 1226237ad974SCharles Davis uint16_t machine = m_coff_header.machine; 1227b9c1b51eSKate Stone switch (machine) { 1228f760f5aeSPavel Labath default: 1229f760f5aeSPavel Labath break; 1230237ad974SCharles Davis case llvm::COFF::IMAGE_FILE_MACHINE_AMD64: 1231237ad974SCharles Davis case llvm::COFF::IMAGE_FILE_MACHINE_I386: 1232237ad974SCharles Davis case llvm::COFF::IMAGE_FILE_MACHINE_POWERPC: 1233237ad974SCharles Davis case llvm::COFF::IMAGE_FILE_MACHINE_POWERPCFP: 1234237ad974SCharles Davis case llvm::COFF::IMAGE_FILE_MACHINE_ARM: 12351108cb36SSaleem Abdulrasool case llvm::COFF::IMAGE_FILE_MACHINE_ARMNT: 1236237ad974SCharles Davis case llvm::COFF::IMAGE_FILE_MACHINE_THUMB: 1237638f072fSMartin Storsjo case llvm::COFF::IMAGE_FILE_MACHINE_ARM64: 1238f760f5aeSPavel Labath ArchSpec arch; 1239fb3b3bd1SZachary Turner arch.SetArchitecture(eArchTypeCOFF, machine, LLDB_INVALID_CPUTYPE, 1240fb3b3bd1SZachary Turner IsWindowsSubsystem() ? llvm::Triple::Win32 1241fb3b3bd1SZachary Turner : llvm::Triple::UnknownOS); 1242f760f5aeSPavel Labath return arch; 1243237ad974SCharles Davis } 1244f760f5aeSPavel Labath return ArchSpec(); 1245f754f88fSGreg Clayton } 1246f754f88fSGreg Clayton 1247b9c1b51eSKate Stone ObjectFile::Type ObjectFilePECOFF::CalculateType() { 1248b9c1b51eSKate Stone if (m_coff_header.machine != 0) { 1249237ad974SCharles Davis if ((m_coff_header.flags & llvm::COFF::IMAGE_FILE_DLL) == 0) 1250f754f88fSGreg Clayton return eTypeExecutable; 1251f754f88fSGreg Clayton else 1252f754f88fSGreg Clayton return eTypeSharedLibrary; 1253f754f88fSGreg Clayton } 1254f754f88fSGreg Clayton return eTypeExecutable; 1255f754f88fSGreg Clayton } 1256f754f88fSGreg Clayton 1257b9c1b51eSKate Stone ObjectFile::Strata ObjectFilePECOFF::CalculateStrata() { return eStrataUser; } 12589cad24a7SZachary Turner 1259f754f88fSGreg Clayton // PluginInterface protocol 1260b9c1b51eSKate Stone ConstString ObjectFilePECOFF::GetPluginName() { return GetPluginNameStatic(); } 1261f754f88fSGreg Clayton 1262b9c1b51eSKate Stone uint32_t ObjectFilePECOFF::GetPluginVersion() { return 1; } 1263