180814287SRaphael Isemann //===-- ObjectFilePECOFF.cpp ----------------------------------------------===// 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" 19*3c867898SAlvin Wong #include "lldb/Interpreter/OptionValueDictionary.h" 2025c8a061SAlvin Wong #include "lldb/Interpreter/OptionValueProperties.h" 21f754f88fSGreg Clayton #include "lldb/Symbol/ObjectFile.h" 22f7d1893fSAdrian McCarthy #include "lldb/Target/Process.h" 232756adf3SVirgile Bello #include "lldb/Target/SectionLoadList.h" 242756adf3SVirgile Bello #include "lldb/Target/Target.h" 255f19b907SPavel Labath #include "lldb/Utility/ArchSpec.h" 26666cc0b2SZachary Turner #include "lldb/Utility/DataBufferHeap.h" 275713a05bSZachary Turner #include "lldb/Utility/FileSpec.h" 28c34698a8SPavel Labath #include "lldb/Utility/LLDBLog.h" 29037ed1beSAaron Smith #include "lldb/Utility/Log.h" 30bf9a7730SZachary Turner #include "lldb/Utility/StreamString.h" 3138d0632eSPavel Labath #include "lldb/Utility/Timer.h" 32666cc0b2SZachary Turner #include "lldb/Utility/UUID.h" 335f19b907SPavel Labath #include "llvm/BinaryFormat/COFF.h" 34f754f88fSGreg Clayton 35037ed1beSAaron Smith #include "llvm/Object/COFFImportFile.h" 36c8daf4a7SAlvin Wong #include "llvm/Support/CRC.h" 37037ed1beSAaron Smith #include "llvm/Support/Error.h" 3825c8a061SAlvin Wong #include "llvm/Support/Host.h" 393f4a4b36SZachary Turner #include "llvm/Support/MemoryBuffer.h" 403f4a4b36SZachary Turner 41f754f88fSGreg Clayton #define IMAGE_DOS_SIGNATURE 0x5A4D // MZ 42f754f88fSGreg Clayton #define IMAGE_NT_SIGNATURE 0x00004550 // PE00 43f754f88fSGreg Clayton #define OPT_HEADER_MAGIC_PE32 0x010b 44f754f88fSGreg Clayton #define OPT_HEADER_MAGIC_PE32_PLUS 0x020b 45f754f88fSGreg Clayton 46f754f88fSGreg Clayton using namespace lldb; 47f754f88fSGreg Clayton using namespace lldb_private; 48f754f88fSGreg Clayton 49bba9ba8dSJonas Devlieghere LLDB_PLUGIN_DEFINE(ObjectFilePECOFF) 50fbb4d1e4SJonas Devlieghere 5125c8a061SAlvin Wong namespace { 5225c8a061SAlvin Wong 5325c8a061SAlvin Wong static constexpr OptionEnumValueElement g_abi_enums[] = { 5425c8a061SAlvin Wong { 5525c8a061SAlvin Wong llvm::Triple::UnknownEnvironment, 5625c8a061SAlvin Wong "default", 5725c8a061SAlvin Wong "Use default target (if it is Windows) or MSVC", 5825c8a061SAlvin Wong }, 5925c8a061SAlvin Wong { 6025c8a061SAlvin Wong llvm::Triple::MSVC, 6125c8a061SAlvin Wong "msvc", 6225c8a061SAlvin Wong "MSVC ABI", 6325c8a061SAlvin Wong }, 6425c8a061SAlvin Wong { 6525c8a061SAlvin Wong llvm::Triple::GNU, 6625c8a061SAlvin Wong "gnu", 6725c8a061SAlvin Wong "MinGW / Itanium ABI", 6825c8a061SAlvin Wong }, 6925c8a061SAlvin Wong }; 7025c8a061SAlvin Wong 7125c8a061SAlvin Wong #define LLDB_PROPERTIES_objectfilepecoff 7225c8a061SAlvin Wong #include "ObjectFilePECOFFProperties.inc" 7325c8a061SAlvin Wong 7425c8a061SAlvin Wong enum { 7525c8a061SAlvin Wong #define LLDB_PROPERTIES_objectfilepecoff 7625c8a061SAlvin Wong #include "ObjectFilePECOFFPropertiesEnum.inc" 7725c8a061SAlvin Wong }; 7825c8a061SAlvin Wong 7925c8a061SAlvin Wong class PluginProperties : public Properties { 8025c8a061SAlvin Wong public: 8125c8a061SAlvin Wong static ConstString GetSettingName() { 8225c8a061SAlvin Wong return ConstString(ObjectFilePECOFF::GetPluginNameStatic()); 8325c8a061SAlvin Wong } 8425c8a061SAlvin Wong 8525c8a061SAlvin Wong PluginProperties() { 8625c8a061SAlvin Wong m_collection_sp = std::make_shared<OptionValueProperties>(GetSettingName()); 8725c8a061SAlvin Wong m_collection_sp->Initialize(g_objectfilepecoff_properties); 8825c8a061SAlvin Wong } 8925c8a061SAlvin Wong 9025c8a061SAlvin Wong llvm::Triple::EnvironmentType ABI() const { 9125c8a061SAlvin Wong return (llvm::Triple::EnvironmentType) 9225c8a061SAlvin Wong m_collection_sp->GetPropertyAtIndexAsEnumeration( 9325c8a061SAlvin Wong nullptr, ePropertyABI, llvm::Triple::UnknownEnvironment); 9425c8a061SAlvin Wong } 95*3c867898SAlvin Wong 96*3c867898SAlvin Wong OptionValueDictionary *ModuleABIMap() const { 97*3c867898SAlvin Wong return m_collection_sp->GetPropertyAtIndexAsOptionValueDictionary( 98*3c867898SAlvin Wong nullptr, ePropertyModuleABIMap); 99*3c867898SAlvin Wong } 10025c8a061SAlvin Wong }; 10125c8a061SAlvin Wong 10225c8a061SAlvin Wong static PluginProperties &GetGlobalPluginProperties() { 10325c8a061SAlvin Wong static PluginProperties g_settings; 10425c8a061SAlvin Wong return g_settings; 10525c8a061SAlvin Wong } 10625c8a061SAlvin Wong 10725c8a061SAlvin Wong } // namespace 10825c8a061SAlvin Wong 109c8daf4a7SAlvin Wong static bool GetDebugLinkContents(const llvm::object::COFFObjectFile &coff_obj, 110c8daf4a7SAlvin Wong std::string &gnu_debuglink_file, 111c8daf4a7SAlvin Wong uint32_t &gnu_debuglink_crc) { 112c8daf4a7SAlvin Wong static ConstString g_sect_name_gnu_debuglink(".gnu_debuglink"); 113c8daf4a7SAlvin Wong for (const auto §ion : coff_obj.sections()) { 114c8daf4a7SAlvin Wong auto name = section.getName(); 115c8daf4a7SAlvin Wong if (!name) { 116c8daf4a7SAlvin Wong llvm::consumeError(name.takeError()); 117c8daf4a7SAlvin Wong continue; 118c8daf4a7SAlvin Wong } 119c8daf4a7SAlvin Wong if (*name == g_sect_name_gnu_debuglink.GetStringRef()) { 120c8daf4a7SAlvin Wong auto content = section.getContents(); 121c8daf4a7SAlvin Wong if (!content) { 122c8daf4a7SAlvin Wong llvm::consumeError(content.takeError()); 123c8daf4a7SAlvin Wong return false; 124c8daf4a7SAlvin Wong } 125c8daf4a7SAlvin Wong DataExtractor data( 126c8daf4a7SAlvin Wong content->data(), content->size(), 127c8daf4a7SAlvin Wong coff_obj.isLittleEndian() ? eByteOrderLittle : eByteOrderBig, 4); 128c8daf4a7SAlvin Wong lldb::offset_t gnu_debuglink_offset = 0; 129c8daf4a7SAlvin Wong gnu_debuglink_file = data.GetCStr(&gnu_debuglink_offset); 130c8daf4a7SAlvin Wong // Align to the next 4-byte offset 131c8daf4a7SAlvin Wong gnu_debuglink_offset = llvm::alignTo(gnu_debuglink_offset, 4); 132c8daf4a7SAlvin Wong data.GetU32(&gnu_debuglink_offset, &gnu_debuglink_crc, 1); 133c8daf4a7SAlvin Wong return true; 134c8daf4a7SAlvin Wong } 135c8daf4a7SAlvin Wong } 136c8daf4a7SAlvin Wong return false; 137c8daf4a7SAlvin Wong } 138c8daf4a7SAlvin Wong 139d372a8e8SPavel Labath static UUID GetCoffUUID(llvm::object::COFFObjectFile &coff_obj) { 140b8d03935SAaron Smith const llvm::codeview::DebugInfo *pdb_info = nullptr; 141b8d03935SAaron Smith llvm::StringRef pdb_file; 142b8d03935SAaron Smith 143c8daf4a7SAlvin Wong // First, prefer to use the PDB build id. LLD generates this even for mingw 144c8daf4a7SAlvin Wong // targets without PDB output, and it does not get stripped either. 145d372a8e8SPavel Labath if (!coff_obj.getDebugPDBInfo(pdb_info, pdb_file) && pdb_info) { 146b8d03935SAaron Smith if (pdb_info->PDB70.CVSignature == llvm::OMF::Signature::PDB70) { 1474348e0eeSZequan Wu UUID::CvRecordPdb70 info; 1484348e0eeSZequan Wu memcpy(&info.Uuid, pdb_info->PDB70.Signature, sizeof(info.Uuid)); 1494348e0eeSZequan Wu info.Age = pdb_info->PDB70.Age; 1504348e0eeSZequan Wu return UUID::fromCvRecord(info); 151b8d03935SAaron Smith } 152b8d03935SAaron Smith } 153b8d03935SAaron Smith 154c8daf4a7SAlvin Wong std::string gnu_debuglink_file; 155c8daf4a7SAlvin Wong uint32_t gnu_debuglink_crc; 156c8daf4a7SAlvin Wong 157c8daf4a7SAlvin Wong // The GNU linker normally does not write a PDB build id (unless requested 158c8daf4a7SAlvin Wong // with the --build-id option), so we should fall back to using the crc 159c8daf4a7SAlvin Wong // from .gnu_debuglink if it exists, just like how ObjectFileELF does it. 160c8daf4a7SAlvin Wong if (!GetDebugLinkContents(coff_obj, gnu_debuglink_file, gnu_debuglink_crc)) { 161c8daf4a7SAlvin Wong // If there is no .gnu_debuglink section, then this may be an object 162c8daf4a7SAlvin Wong // containing DWARF debug info for .gnu_debuglink, so calculate the crc of 163c8daf4a7SAlvin Wong // the object itself. 164c8daf4a7SAlvin Wong auto raw_data = coff_obj.getData(); 165c8daf4a7SAlvin Wong LLDB_SCOPED_TIMERF( 166c8daf4a7SAlvin Wong "Calculating module crc32 %s with size %" PRIu64 " KiB", 167c8daf4a7SAlvin Wong FileSpec(coff_obj.getFileName()).GetLastPathComponent().AsCString(), 168c8daf4a7SAlvin Wong static_cast<lldb::offset_t>(raw_data.size()) / 1024); 169c8daf4a7SAlvin Wong gnu_debuglink_crc = llvm::crc32(0, llvm::arrayRefFromStringRef(raw_data)); 170c8daf4a7SAlvin Wong } 171c8daf4a7SAlvin Wong // Use 4 bytes of crc from the .gnu_debuglink section. 172c8daf4a7SAlvin Wong llvm::support::ulittle32_t data(gnu_debuglink_crc); 173c8daf4a7SAlvin Wong return UUID::fromData(&data, sizeof(data)); 174b8d03935SAaron Smith } 175b8d03935SAaron Smith 176e84f7841SPavel Labath char ObjectFilePECOFF::ID; 177e84f7841SPavel Labath 178b9c1b51eSKate Stone void ObjectFilePECOFF::Initialize() { 17925c8a061SAlvin Wong PluginManager::RegisterPlugin(GetPluginNameStatic(), 18025c8a061SAlvin Wong GetPluginDescriptionStatic(), CreateInstance, 18125c8a061SAlvin Wong CreateMemoryInstance, GetModuleSpecifications, 18225c8a061SAlvin Wong SaveCore, DebuggerInitialize); 18325c8a061SAlvin Wong } 18425c8a061SAlvin Wong 18525c8a061SAlvin Wong void ObjectFilePECOFF::DebuggerInitialize(Debugger &debugger) { 18625c8a061SAlvin Wong if (!PluginManager::GetSettingForObjectFilePlugin( 18725c8a061SAlvin Wong debugger, PluginProperties::GetSettingName())) { 18825c8a061SAlvin Wong const bool is_global_setting = true; 18925c8a061SAlvin Wong PluginManager::CreateSettingForObjectFilePlugin( 19025c8a061SAlvin Wong debugger, GetGlobalPluginProperties().GetValueProperties(), 19125c8a061SAlvin Wong ConstString("Properties for the PE/COFF object-file plug-in."), 19225c8a061SAlvin Wong is_global_setting); 19325c8a061SAlvin Wong } 194f754f88fSGreg Clayton } 195f754f88fSGreg Clayton 196b9c1b51eSKate Stone void ObjectFilePECOFF::Terminate() { 197f754f88fSGreg Clayton PluginManager::UnregisterPlugin(CreateInstance); 198f754f88fSGreg Clayton } 199f754f88fSGreg Clayton 2002ace1e57SPavel Labath llvm::StringRef ObjectFilePECOFF::GetPluginDescriptionStatic() { 201b9c1b51eSKate Stone return "Portable Executable and Common Object File Format object file reader " 202b9c1b51eSKate Stone "(32 and 64 bit)"; 203f754f88fSGreg Clayton } 204f754f88fSGreg Clayton 205c69307e5SJonas Devlieghere ObjectFile *ObjectFilePECOFF::CreateInstance( 206c69307e5SJonas Devlieghere const lldb::ModuleSP &module_sp, DataBufferSP data_sp, 207c69307e5SJonas Devlieghere lldb::offset_t data_offset, const lldb_private::FileSpec *file_p, 208c69307e5SJonas Devlieghere lldb::offset_t file_offset, lldb::offset_t length) { 20928e4942bSPavel Labath FileSpec file = file_p ? *file_p : FileSpec(); 210b9c1b51eSKate Stone if (!data_sp) { 21150251fc7SPavel Labath data_sp = MapFileData(file, length, file_offset); 2123f4a4b36SZachary Turner if (!data_sp) 2133f4a4b36SZachary Turner return nullptr; 2145ce9c565SGreg Clayton data_offset = 0; 2155ce9c565SGreg Clayton } 2165ce9c565SGreg Clayton 2173f4a4b36SZachary Turner if (!ObjectFilePECOFF::MagicBytesMatch(data_sp)) 2183f4a4b36SZachary Turner return nullptr; 2193f4a4b36SZachary Turner 2205ce9c565SGreg Clayton // Update the data to contain the entire file if it doesn't already 2213f4a4b36SZachary Turner if (data_sp->GetByteSize() < length) { 22250251fc7SPavel Labath data_sp = MapFileData(file, length, file_offset); 2233f4a4b36SZachary Turner if (!data_sp) 2243f4a4b36SZachary Turner return nullptr; 225f754f88fSGreg Clayton } 2263f4a4b36SZachary Turner 227a8f3ae7cSJonas Devlieghere auto objfile_up = std::make_unique<ObjectFilePECOFF>( 22828e4942bSPavel Labath module_sp, data_sp, data_offset, file_p, file_offset, length); 229d5b44036SJonas Devlieghere if (!objfile_up || !objfile_up->ParseHeader()) 2303f4a4b36SZachary Turner return nullptr; 2313f4a4b36SZachary Turner 232037ed1beSAaron Smith // Cache coff binary. 233d5b44036SJonas Devlieghere if (!objfile_up->CreateBinary()) 234037ed1beSAaron Smith return nullptr; 235d5b44036SJonas Devlieghere return objfile_up.release(); 236f754f88fSGreg Clayton } 237f754f88fSGreg Clayton 238b9c1b51eSKate Stone ObjectFile *ObjectFilePECOFF::CreateMemoryInstance( 239f2ea125eSJonas Devlieghere const lldb::ModuleSP &module_sp, lldb::WritableDataBufferSP data_sp, 240b9c1b51eSKate Stone const lldb::ProcessSP &process_sp, lldb::addr_t header_addr) { 241344546bdSWalter Erquinigo if (!data_sp || !ObjectFilePECOFF::MagicBytesMatch(data_sp)) 242344546bdSWalter Erquinigo return nullptr; 243a8f3ae7cSJonas Devlieghere auto objfile_up = std::make_unique<ObjectFilePECOFF>( 244344546bdSWalter Erquinigo module_sp, data_sp, process_sp, header_addr); 245d5b44036SJonas Devlieghere if (objfile_up.get() && objfile_up->ParseHeader()) { 246d5b44036SJonas Devlieghere return objfile_up.release(); 247344546bdSWalter Erquinigo } 248344546bdSWalter Erquinigo return nullptr; 249c9660546SGreg Clayton } 250c9660546SGreg Clayton 251b9c1b51eSKate Stone size_t ObjectFilePECOFF::GetModuleSpecifications( 252b9c1b51eSKate Stone const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp, 253b9c1b51eSKate Stone lldb::offset_t data_offset, lldb::offset_t file_offset, 254b9c1b51eSKate Stone lldb::offset_t length, lldb_private::ModuleSpecList &specs) { 25589eb1baeSVirgile Bello const size_t initial_count = specs.GetSize(); 256b8d03935SAaron Smith if (!data_sp || !ObjectFilePECOFF::MagicBytesMatch(data_sp)) 257b8d03935SAaron Smith return initial_count; 25889eb1baeSVirgile Bello 259a007a6d8SPavel Labath Log *log = GetLog(LLDBLog::Object); 2603db1d138SMartin Storsjö 261a4a00cedSFred Riss if (data_sp->GetByteSize() < length) 262d372a8e8SPavel Labath if (DataBufferSP full_sp = MapFileData(file, -1, file_offset)) 263d372a8e8SPavel Labath data_sp = std::move(full_sp); 264d372a8e8SPavel Labath auto binary = llvm::object::createBinary(llvm::MemoryBufferRef( 265d372a8e8SPavel Labath toStringRef(data_sp->GetData()), file.GetFilename().GetStringRef())); 2663db1d138SMartin Storsjö 2673db1d138SMartin Storsjö if (!binary) { 2683db1d138SMartin Storsjö LLDB_LOG_ERROR(log, binary.takeError(), 2693db1d138SMartin Storsjö "Failed to create binary for file ({1}): {0}", file); 270b8d03935SAaron Smith return initial_count; 2713db1d138SMartin Storsjö } 27289eb1baeSVirgile Bello 273d372a8e8SPavel Labath auto *COFFObj = llvm::dyn_cast<llvm::object::COFFObjectFile>(binary->get()); 274d372a8e8SPavel Labath if (!COFFObj) 275b8d03935SAaron Smith return initial_count; 27689eb1baeSVirgile Bello 277b8d03935SAaron Smith ModuleSpec module_spec(file); 278b8d03935SAaron Smith ArchSpec &spec = module_spec.GetArchitecture(); 279b8d03935SAaron Smith lldb_private::UUID &uuid = module_spec.GetUUID(); 280b8d03935SAaron Smith if (!uuid.IsValid()) 281d372a8e8SPavel Labath uuid = GetCoffUUID(*COFFObj); 282b8d03935SAaron Smith 28325c8a061SAlvin Wong static llvm::Triple::EnvironmentType default_env = [] { 28425c8a061SAlvin Wong auto def_target = llvm::Triple( 28525c8a061SAlvin Wong llvm::Triple::normalize(llvm::sys::getDefaultTargetTriple())); 28625c8a061SAlvin Wong if (def_target.getOS() == llvm::Triple::Win32 && 28725c8a061SAlvin Wong def_target.getEnvironment() != llvm::Triple::UnknownEnvironment) 28825c8a061SAlvin Wong return def_target.getEnvironment(); 28925c8a061SAlvin Wong return llvm::Triple::MSVC; 29025c8a061SAlvin Wong }(); 29125c8a061SAlvin Wong 292*3c867898SAlvin Wong // Check for a module-specific override. 293*3c867898SAlvin Wong OptionValueSP module_env_option; 294*3c867898SAlvin Wong const auto *map = GetGlobalPluginProperties().ModuleABIMap(); 295*3c867898SAlvin Wong if (map->GetNumValues() > 0) { 296*3c867898SAlvin Wong // Step 1: Try with the exact file name. 297*3c867898SAlvin Wong auto name = file.GetLastPathComponent(); 298*3c867898SAlvin Wong module_env_option = map->GetValueForKey(name); 299*3c867898SAlvin Wong if (!module_env_option) { 300*3c867898SAlvin Wong // Step 2: Try with the file name in lowercase. 301*3c867898SAlvin Wong auto name_lower = name.GetStringRef().lower(); 302*3c867898SAlvin Wong module_env_option = 303*3c867898SAlvin Wong map->GetValueForKey(ConstString(llvm::StringRef(name_lower))); 304*3c867898SAlvin Wong } 305*3c867898SAlvin Wong if (!module_env_option) { 306*3c867898SAlvin Wong // Step 3: Try with the file name with ".debug" suffix stripped. 307*3c867898SAlvin Wong auto name_stripped = name.GetStringRef(); 308*3c867898SAlvin Wong if (name_stripped.consume_back_insensitive(".debug")) { 309*3c867898SAlvin Wong module_env_option = map->GetValueForKey(ConstString(name_stripped)); 310*3c867898SAlvin Wong if (!module_env_option) { 311*3c867898SAlvin Wong // Step 4: Try with the file name in lowercase with ".debug" suffix 312*3c867898SAlvin Wong // stripped. 313*3c867898SAlvin Wong auto name_lower = name_stripped.lower(); 314*3c867898SAlvin Wong module_env_option = 315*3c867898SAlvin Wong map->GetValueForKey(ConstString(llvm::StringRef(name_lower))); 316*3c867898SAlvin Wong } 317*3c867898SAlvin Wong } 318*3c867898SAlvin Wong } 319*3c867898SAlvin Wong } 320*3c867898SAlvin Wong llvm::Triple::EnvironmentType env; 321*3c867898SAlvin Wong if (module_env_option) 322*3c867898SAlvin Wong env = 323*3c867898SAlvin Wong (llvm::Triple::EnvironmentType)module_env_option->GetEnumerationValue(); 324*3c867898SAlvin Wong else 325*3c867898SAlvin Wong env = GetGlobalPluginProperties().ABI(); 326*3c867898SAlvin Wong 32725c8a061SAlvin Wong if (env == llvm::Triple::UnknownEnvironment) 32825c8a061SAlvin Wong env = default_env; 32925c8a061SAlvin Wong 330b8d03935SAaron Smith switch (COFFObj->getMachine()) { 331b8d03935SAaron Smith case MachineAmd64: 332ad587ae4SZachary Turner spec.SetTriple("x86_64-pc-windows"); 33325c8a061SAlvin Wong spec.GetTriple().setEnvironment(env); 334b8d03935SAaron Smith specs.Append(module_spec); 335b8d03935SAaron Smith break; 336b8d03935SAaron Smith case MachineX86: 337ad587ae4SZachary Turner spec.SetTriple("i386-pc-windows"); 33825c8a061SAlvin Wong spec.GetTriple().setEnvironment(env); 339b8d03935SAaron Smith specs.Append(module_spec); 3405e6f4520SZachary Turner spec.SetTriple("i686-pc-windows"); 34125c8a061SAlvin Wong spec.GetTriple().setEnvironment(env); 342b8d03935SAaron Smith specs.Append(module_spec); 343b8d03935SAaron Smith break; 344b8d03935SAaron Smith case MachineArmNt: 345544c8f48SMartin Storsjo spec.SetTriple("armv7-pc-windows"); 34625c8a061SAlvin Wong spec.GetTriple().setEnvironment(env); 347b8d03935SAaron Smith specs.Append(module_spec); 348b8d03935SAaron Smith break; 349638f072fSMartin Storsjo case MachineArm64: 350674d5543SMartin Storsjo spec.SetTriple("aarch64-pc-windows"); 35125c8a061SAlvin Wong spec.GetTriple().setEnvironment(env); 352638f072fSMartin Storsjo specs.Append(module_spec); 353638f072fSMartin Storsjo break; 354b8d03935SAaron Smith default: 355b8d03935SAaron Smith break; 35689eb1baeSVirgile Bello } 35789eb1baeSVirgile Bello 35889eb1baeSVirgile Bello return specs.GetSize() - initial_count; 359f4d6de6aSGreg Clayton } 360f4d6de6aSGreg Clayton 361b9c1b51eSKate Stone bool ObjectFilePECOFF::SaveCore(const lldb::ProcessSP &process_sp, 362f7d1893fSAdrian McCarthy const lldb_private::FileSpec &outfile, 3639ea6dd5cSJason Molenda lldb::SaveCoreStyle &core_style, 36497206d57SZachary Turner lldb_private::Status &error) { 3659ea6dd5cSJason Molenda core_style = eSaveCoreFull; 366f7d1893fSAdrian McCarthy return SaveMiniDump(process_sp, outfile, error); 367f7d1893fSAdrian McCarthy } 368f7d1893fSAdrian McCarthy 369f2ea125eSJonas Devlieghere bool ObjectFilePECOFF::MagicBytesMatch(DataBufferSP data_sp) { 3705ce9c565SGreg Clayton DataExtractor data(data_sp, eByteOrderLittle, 4); 371c7bece56SGreg Clayton lldb::offset_t offset = 0; 372f754f88fSGreg Clayton uint16_t magic = data.GetU16(&offset); 373f754f88fSGreg Clayton return magic == IMAGE_DOS_SIGNATURE; 374f754f88fSGreg Clayton } 375f754f88fSGreg Clayton 376b9c1b51eSKate Stone lldb::SymbolType ObjectFilePECOFF::MapSymbolType(uint16_t coff_symbol_type) { 377c35b91ceSAdrian McCarthy // TODO: We need to complete this mapping of COFF symbol types to LLDB ones. 378c35b91ceSAdrian McCarthy // For now, here's a hack to make sure our function have types. 379b9c1b51eSKate Stone const auto complex_type = 380b9c1b51eSKate Stone coff_symbol_type >> llvm::COFF::SCT_COMPLEX_TYPE_SHIFT; 381b9c1b51eSKate Stone if (complex_type == llvm::COFF::IMAGE_SYM_DTYPE_FUNCTION) { 382c35b91ceSAdrian McCarthy return lldb::eSymbolTypeCode; 383c35b91ceSAdrian McCarthy } 384c35b91ceSAdrian McCarthy return lldb::eSymbolTypeInvalid; 385c35b91ceSAdrian McCarthy } 386f754f88fSGreg Clayton 387037ed1beSAaron Smith bool ObjectFilePECOFF::CreateBinary() { 388d372a8e8SPavel Labath if (m_binary) 389037ed1beSAaron Smith return true; 390037ed1beSAaron Smith 391a007a6d8SPavel Labath Log *log = GetLog(LLDBLog::Object); 392037ed1beSAaron Smith 393d372a8e8SPavel Labath auto binary = llvm::object::createBinary(llvm::MemoryBufferRef( 394d372a8e8SPavel Labath toStringRef(m_data.GetData()), m_file.GetFilename().GetStringRef())); 395037ed1beSAaron Smith if (!binary) { 3963db1d138SMartin Storsjö LLDB_LOG_ERROR(log, binary.takeError(), 3973db1d138SMartin Storsjö "Failed to create binary for file ({1}): {0}", m_file); 398037ed1beSAaron Smith return false; 399037ed1beSAaron Smith } 400037ed1beSAaron Smith 401037ed1beSAaron Smith // Make sure we only handle COFF format. 402d372a8e8SPavel Labath m_binary = 403d372a8e8SPavel Labath llvm::unique_dyn_cast<llvm::object::COFFObjectFile>(std::move(*binary)); 404d372a8e8SPavel Labath if (!m_binary) 405037ed1beSAaron Smith return false; 406037ed1beSAaron Smith 407d372a8e8SPavel Labath LLDB_LOG(log, "this = {0}, module = {1} ({2}), file = {3}, binary = {4}", 408d372a8e8SPavel Labath this, GetModule().get(), GetModule()->GetSpecificationDescription(), 409d372a8e8SPavel Labath m_file.GetPath(), m_binary.get()); 410037ed1beSAaron Smith return true; 411037ed1beSAaron Smith } 412037ed1beSAaron Smith 413e72dfb32SGreg Clayton ObjectFilePECOFF::ObjectFilePECOFF(const lldb::ModuleSP &module_sp, 414f2ea125eSJonas Devlieghere DataBufferSP data_sp, 4155ce9c565SGreg Clayton lldb::offset_t data_offset, 416f754f88fSGreg Clayton const FileSpec *file, 4175ce9c565SGreg Clayton lldb::offset_t file_offset, 418b9c1b51eSKate Stone lldb::offset_t length) 419b9c1b51eSKate Stone : ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset), 4205c43ffd6SPavel Labath m_dos_header(), m_coff_header(), m_sect_headers(), 421d372a8e8SPavel Labath m_entry_point_address(), m_deps_filespec() { 422f754f88fSGreg Clayton ::memset(&m_dos_header, 0, sizeof(m_dos_header)); 423f754f88fSGreg Clayton ::memset(&m_coff_header, 0, sizeof(m_coff_header)); 424f754f88fSGreg Clayton } 425f754f88fSGreg Clayton 426344546bdSWalter Erquinigo ObjectFilePECOFF::ObjectFilePECOFF(const lldb::ModuleSP &module_sp, 427f2ea125eSJonas Devlieghere WritableDataBufferSP header_data_sp, 428344546bdSWalter Erquinigo const lldb::ProcessSP &process_sp, 429344546bdSWalter Erquinigo addr_t header_addr) 430344546bdSWalter Erquinigo : ObjectFile(module_sp, process_sp, header_addr, header_data_sp), 4315c43ffd6SPavel Labath m_dos_header(), m_coff_header(), m_sect_headers(), 432d372a8e8SPavel Labath m_entry_point_address(), m_deps_filespec() { 433344546bdSWalter Erquinigo ::memset(&m_dos_header, 0, sizeof(m_dos_header)); 434344546bdSWalter Erquinigo ::memset(&m_coff_header, 0, sizeof(m_coff_header)); 435344546bdSWalter Erquinigo } 436344546bdSWalter Erquinigo 437fd2433e1SJonas Devlieghere ObjectFilePECOFF::~ObjectFilePECOFF() = default; 438f754f88fSGreg Clayton 439b9c1b51eSKate Stone bool ObjectFilePECOFF::ParseHeader() { 440a1743499SGreg Clayton ModuleSP module_sp(GetModule()); 441b9c1b51eSKate Stone if (module_sp) { 44216ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 443f754f88fSGreg Clayton m_sect_headers.clear(); 444f754f88fSGreg Clayton m_data.SetByteOrder(eByteOrderLittle); 445c7bece56SGreg Clayton lldb::offset_t offset = 0; 446f754f88fSGreg Clayton 447b9c1b51eSKate Stone if (ParseDOSHeader(m_data, m_dos_header)) { 448f754f88fSGreg Clayton offset = m_dos_header.e_lfanew; 449f754f88fSGreg Clayton uint32_t pe_signature = m_data.GetU32(&offset); 450f754f88fSGreg Clayton if (pe_signature != IMAGE_NT_SIGNATURE) 451f754f88fSGreg Clayton return false; 452b9c1b51eSKate Stone if (ParseCOFFHeader(m_data, &offset, m_coff_header)) { 453f754f88fSGreg Clayton if (m_coff_header.hdrsize > 0) 454f754f88fSGreg Clayton ParseCOFFOptionalHeader(&offset); 455f754f88fSGreg Clayton ParseSectionHeaders(offset); 45628469ca3SGreg Clayton } 457a0f72441SMartin Storsjö m_data.SetAddressByteSize(GetAddressByteSize()); 458f754f88fSGreg Clayton return true; 459f754f88fSGreg Clayton } 460a1743499SGreg Clayton } 461f754f88fSGreg Clayton return false; 462f754f88fSGreg Clayton } 463f754f88fSGreg Clayton 464b9c1b51eSKate Stone bool ObjectFilePECOFF::SetLoadAddress(Target &target, addr_t value, 465b9c1b51eSKate Stone bool value_is_offset) { 4662756adf3SVirgile Bello bool changed = false; 4672756adf3SVirgile Bello ModuleSP module_sp = GetModule(); 468b9c1b51eSKate Stone if (module_sp) { 4692756adf3SVirgile Bello size_t num_loaded_sections = 0; 4702756adf3SVirgile Bello SectionList *section_list = GetSectionList(); 471b9c1b51eSKate Stone if (section_list) { 472b9c1b51eSKate Stone if (!value_is_offset) { 4732756adf3SVirgile Bello value -= m_image_base; 4742756adf3SVirgile Bello } 4752756adf3SVirgile Bello 4762756adf3SVirgile Bello const size_t num_sections = section_list->GetSize(); 4772756adf3SVirgile Bello size_t sect_idx = 0; 4782756adf3SVirgile Bello 479b9c1b51eSKate Stone for (sect_idx = 0; sect_idx < num_sections; ++sect_idx) { 48005097246SAdrian Prantl // Iterate through the object file sections to find all of the sections 48105097246SAdrian Prantl // that have SHF_ALLOC in their flag bits. 4822756adf3SVirgile Bello SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx)); 483b9c1b51eSKate Stone if (section_sp && !section_sp->IsThreadSpecific()) { 484b9c1b51eSKate Stone if (target.GetSectionLoadList().SetSectionLoadAddress( 485b9c1b51eSKate Stone section_sp, section_sp->GetFileAddress() + value)) 4862756adf3SVirgile Bello ++num_loaded_sections; 4872756adf3SVirgile Bello } 4882756adf3SVirgile Bello } 4892756adf3SVirgile Bello changed = num_loaded_sections > 0; 4902756adf3SVirgile Bello } 4912756adf3SVirgile Bello } 4922756adf3SVirgile Bello return changed; 4932756adf3SVirgile Bello } 4942756adf3SVirgile Bello 495b9c1b51eSKate Stone ByteOrder ObjectFilePECOFF::GetByteOrder() const { return eByteOrderLittle; } 496f754f88fSGreg Clayton 497b9c1b51eSKate Stone bool ObjectFilePECOFF::IsExecutable() const { 498237ad974SCharles Davis return (m_coff_header.flags & llvm::COFF::IMAGE_FILE_DLL) == 0; 499f754f88fSGreg Clayton } 500f754f88fSGreg Clayton 501b9c1b51eSKate Stone uint32_t ObjectFilePECOFF::GetAddressByteSize() const { 502f754f88fSGreg Clayton if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32_PLUS) 503f754f88fSGreg Clayton return 8; 504f754f88fSGreg Clayton else if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32) 505f754f88fSGreg Clayton return 4; 506f754f88fSGreg Clayton return 4; 507f754f88fSGreg Clayton } 508f754f88fSGreg Clayton 509f754f88fSGreg Clayton // NeedsEndianSwap 510f754f88fSGreg Clayton // 51105097246SAdrian Prantl // Return true if an endian swap needs to occur when extracting data from this 51205097246SAdrian Prantl // file. 513b9c1b51eSKate Stone bool ObjectFilePECOFF::NeedsEndianSwap() const { 514f754f88fSGreg Clayton #if defined(__LITTLE_ENDIAN__) 515f754f88fSGreg Clayton return false; 516f754f88fSGreg Clayton #else 517f754f88fSGreg Clayton return true; 518f754f88fSGreg Clayton #endif 519f754f88fSGreg Clayton } 520f754f88fSGreg Clayton // ParseDOSHeader 521b9c1b51eSKate Stone bool ObjectFilePECOFF::ParseDOSHeader(DataExtractor &data, 522b9c1b51eSKate Stone dos_header_t &dos_header) { 523f754f88fSGreg Clayton bool success = false; 524c7bece56SGreg Clayton lldb::offset_t offset = 0; 52589eb1baeSVirgile Bello success = data.ValidOffsetForDataOfSize(0, sizeof(dos_header)); 526f754f88fSGreg Clayton 527b9c1b51eSKate Stone if (success) { 52889eb1baeSVirgile Bello dos_header.e_magic = data.GetU16(&offset); // Magic number 52989eb1baeSVirgile Bello success = dos_header.e_magic == IMAGE_DOS_SIGNATURE; 530f754f88fSGreg Clayton 531b9c1b51eSKate Stone if (success) { 53289eb1baeSVirgile Bello dos_header.e_cblp = data.GetU16(&offset); // Bytes on last page of file 53389eb1baeSVirgile Bello dos_header.e_cp = data.GetU16(&offset); // Pages in file 53489eb1baeSVirgile Bello dos_header.e_crlc = data.GetU16(&offset); // Relocations 535b9c1b51eSKate Stone dos_header.e_cparhdr = 536b9c1b51eSKate Stone data.GetU16(&offset); // Size of header in paragraphs 537b9c1b51eSKate Stone dos_header.e_minalloc = 538b9c1b51eSKate Stone data.GetU16(&offset); // Minimum extra paragraphs needed 539b9c1b51eSKate Stone dos_header.e_maxalloc = 540b9c1b51eSKate Stone data.GetU16(&offset); // Maximum extra paragraphs needed 54189eb1baeSVirgile Bello dos_header.e_ss = data.GetU16(&offset); // Initial (relative) SS value 54289eb1baeSVirgile Bello dos_header.e_sp = data.GetU16(&offset); // Initial SP value 54389eb1baeSVirgile Bello dos_header.e_csum = data.GetU16(&offset); // Checksum 54489eb1baeSVirgile Bello dos_header.e_ip = data.GetU16(&offset); // Initial IP value 54589eb1baeSVirgile Bello dos_header.e_cs = data.GetU16(&offset); // Initial (relative) CS value 546b9c1b51eSKate Stone dos_header.e_lfarlc = 547b9c1b51eSKate Stone data.GetU16(&offset); // File address of relocation table 54889eb1baeSVirgile Bello dos_header.e_ovno = data.GetU16(&offset); // Overlay number 549f754f88fSGreg Clayton 55089eb1baeSVirgile Bello dos_header.e_res[0] = data.GetU16(&offset); // Reserved words 55189eb1baeSVirgile Bello dos_header.e_res[1] = data.GetU16(&offset); // Reserved words 55289eb1baeSVirgile Bello dos_header.e_res[2] = data.GetU16(&offset); // Reserved words 55389eb1baeSVirgile Bello dos_header.e_res[3] = data.GetU16(&offset); // Reserved words 554f754f88fSGreg Clayton 555b9c1b51eSKate Stone dos_header.e_oemid = 556b9c1b51eSKate Stone data.GetU16(&offset); // OEM identifier (for e_oeminfo) 557b9c1b51eSKate Stone dos_header.e_oeminfo = 558b9c1b51eSKate Stone data.GetU16(&offset); // OEM information; e_oemid specific 55989eb1baeSVirgile Bello dos_header.e_res2[0] = data.GetU16(&offset); // Reserved words 56089eb1baeSVirgile Bello dos_header.e_res2[1] = data.GetU16(&offset); // Reserved words 56189eb1baeSVirgile Bello dos_header.e_res2[2] = data.GetU16(&offset); // Reserved words 56289eb1baeSVirgile Bello dos_header.e_res2[3] = data.GetU16(&offset); // Reserved words 56389eb1baeSVirgile Bello dos_header.e_res2[4] = data.GetU16(&offset); // Reserved words 56489eb1baeSVirgile Bello dos_header.e_res2[5] = data.GetU16(&offset); // Reserved words 56589eb1baeSVirgile Bello dos_header.e_res2[6] = data.GetU16(&offset); // Reserved words 56689eb1baeSVirgile Bello dos_header.e_res2[7] = data.GetU16(&offset); // Reserved words 56789eb1baeSVirgile Bello dos_header.e_res2[8] = data.GetU16(&offset); // Reserved words 56889eb1baeSVirgile Bello dos_header.e_res2[9] = data.GetU16(&offset); // Reserved words 569f754f88fSGreg Clayton 570b9c1b51eSKate Stone dos_header.e_lfanew = 571b9c1b51eSKate Stone data.GetU32(&offset); // File address of new exe header 572f754f88fSGreg Clayton } 573f754f88fSGreg Clayton } 574f754f88fSGreg Clayton if (!success) 57589eb1baeSVirgile Bello memset(&dos_header, 0, sizeof(dos_header)); 576f754f88fSGreg Clayton return success; 577f754f88fSGreg Clayton } 578f754f88fSGreg Clayton 579f754f88fSGreg Clayton // ParserCOFFHeader 580b9c1b51eSKate Stone bool ObjectFilePECOFF::ParseCOFFHeader(DataExtractor &data, 581b9c1b51eSKate Stone lldb::offset_t *offset_ptr, 582b9c1b51eSKate Stone coff_header_t &coff_header) { 583b9c1b51eSKate Stone bool success = 584b9c1b51eSKate Stone data.ValidOffsetForDataOfSize(*offset_ptr, sizeof(coff_header)); 585b9c1b51eSKate Stone if (success) { 58689eb1baeSVirgile Bello coff_header.machine = data.GetU16(offset_ptr); 58789eb1baeSVirgile Bello coff_header.nsects = data.GetU16(offset_ptr); 58889eb1baeSVirgile Bello coff_header.modtime = data.GetU32(offset_ptr); 58989eb1baeSVirgile Bello coff_header.symoff = data.GetU32(offset_ptr); 59089eb1baeSVirgile Bello coff_header.nsyms = data.GetU32(offset_ptr); 59189eb1baeSVirgile Bello coff_header.hdrsize = data.GetU16(offset_ptr); 59289eb1baeSVirgile Bello coff_header.flags = data.GetU16(offset_ptr); 593f754f88fSGreg Clayton } 594f754f88fSGreg Clayton if (!success) 59589eb1baeSVirgile Bello memset(&coff_header, 0, sizeof(coff_header)); 596f754f88fSGreg Clayton return success; 597f754f88fSGreg Clayton } 598f754f88fSGreg Clayton 599b9c1b51eSKate Stone bool ObjectFilePECOFF::ParseCOFFOptionalHeader(lldb::offset_t *offset_ptr) { 600f754f88fSGreg Clayton bool success = false; 601c7bece56SGreg Clayton const lldb::offset_t end_offset = *offset_ptr + m_coff_header.hdrsize; 602b9c1b51eSKate Stone if (*offset_ptr < end_offset) { 603f754f88fSGreg Clayton success = true; 604f754f88fSGreg Clayton m_coff_header_opt.magic = m_data.GetU16(offset_ptr); 605f754f88fSGreg Clayton m_coff_header_opt.major_linker_version = m_data.GetU8(offset_ptr); 606f754f88fSGreg Clayton m_coff_header_opt.minor_linker_version = m_data.GetU8(offset_ptr); 607f754f88fSGreg Clayton m_coff_header_opt.code_size = m_data.GetU32(offset_ptr); 608f754f88fSGreg Clayton m_coff_header_opt.data_size = m_data.GetU32(offset_ptr); 609f754f88fSGreg Clayton m_coff_header_opt.bss_size = m_data.GetU32(offset_ptr); 610f754f88fSGreg Clayton m_coff_header_opt.entry = m_data.GetU32(offset_ptr); 611f754f88fSGreg Clayton m_coff_header_opt.code_offset = m_data.GetU32(offset_ptr); 612f754f88fSGreg Clayton 613f754f88fSGreg Clayton const uint32_t addr_byte_size = GetAddressByteSize(); 614f754f88fSGreg Clayton 615b9c1b51eSKate Stone if (*offset_ptr < end_offset) { 616b9c1b51eSKate Stone if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32) { 617f754f88fSGreg Clayton // PE32 only 618f754f88fSGreg Clayton m_coff_header_opt.data_offset = m_data.GetU32(offset_ptr); 619b9c1b51eSKate Stone } else 620f754f88fSGreg Clayton m_coff_header_opt.data_offset = 0; 621f754f88fSGreg Clayton 622b9c1b51eSKate Stone if (*offset_ptr < end_offset) { 623b9c1b51eSKate Stone m_coff_header_opt.image_base = 624b9c1b51eSKate Stone m_data.GetMaxU64(offset_ptr, addr_byte_size); 625f754f88fSGreg Clayton m_coff_header_opt.sect_alignment = m_data.GetU32(offset_ptr); 626f754f88fSGreg Clayton m_coff_header_opt.file_alignment = m_data.GetU32(offset_ptr); 627f754f88fSGreg Clayton m_coff_header_opt.major_os_system_version = m_data.GetU16(offset_ptr); 628f754f88fSGreg Clayton m_coff_header_opt.minor_os_system_version = m_data.GetU16(offset_ptr); 629f754f88fSGreg Clayton m_coff_header_opt.major_image_version = m_data.GetU16(offset_ptr); 630f754f88fSGreg Clayton m_coff_header_opt.minor_image_version = m_data.GetU16(offset_ptr); 631f754f88fSGreg Clayton m_coff_header_opt.major_subsystem_version = m_data.GetU16(offset_ptr); 632f754f88fSGreg Clayton m_coff_header_opt.minor_subsystem_version = m_data.GetU16(offset_ptr); 633f754f88fSGreg Clayton m_coff_header_opt.reserved1 = m_data.GetU32(offset_ptr); 634f754f88fSGreg Clayton m_coff_header_opt.image_size = m_data.GetU32(offset_ptr); 635f754f88fSGreg Clayton m_coff_header_opt.header_size = m_data.GetU32(offset_ptr); 63628469ca3SGreg Clayton m_coff_header_opt.checksum = m_data.GetU32(offset_ptr); 637f754f88fSGreg Clayton m_coff_header_opt.subsystem = m_data.GetU16(offset_ptr); 638f754f88fSGreg Clayton m_coff_header_opt.dll_flags = m_data.GetU16(offset_ptr); 639b9c1b51eSKate Stone m_coff_header_opt.stack_reserve_size = 640b9c1b51eSKate Stone m_data.GetMaxU64(offset_ptr, addr_byte_size); 641b9c1b51eSKate Stone m_coff_header_opt.stack_commit_size = 642b9c1b51eSKate Stone m_data.GetMaxU64(offset_ptr, addr_byte_size); 643b9c1b51eSKate Stone m_coff_header_opt.heap_reserve_size = 644b9c1b51eSKate Stone m_data.GetMaxU64(offset_ptr, addr_byte_size); 645b9c1b51eSKate Stone m_coff_header_opt.heap_commit_size = 646b9c1b51eSKate Stone m_data.GetMaxU64(offset_ptr, addr_byte_size); 647f754f88fSGreg Clayton m_coff_header_opt.loader_flags = m_data.GetU32(offset_ptr); 648f754f88fSGreg Clayton uint32_t num_data_dir_entries = m_data.GetU32(offset_ptr); 649f754f88fSGreg Clayton m_coff_header_opt.data_dirs.clear(); 650f754f88fSGreg Clayton m_coff_header_opt.data_dirs.resize(num_data_dir_entries); 651f754f88fSGreg Clayton uint32_t i; 652b9c1b51eSKate Stone for (i = 0; i < num_data_dir_entries; i++) { 653f754f88fSGreg Clayton m_coff_header_opt.data_dirs[i].vmaddr = m_data.GetU32(offset_ptr); 654f754f88fSGreg Clayton m_coff_header_opt.data_dirs[i].vmsize = m_data.GetU32(offset_ptr); 655f754f88fSGreg Clayton } 6562756adf3SVirgile Bello 6572756adf3SVirgile Bello m_image_base = m_coff_header_opt.image_base; 658f754f88fSGreg Clayton } 659f754f88fSGreg Clayton } 660f754f88fSGreg Clayton } 661f754f88fSGreg Clayton // Make sure we are on track for section data which follows 662f754f88fSGreg Clayton *offset_ptr = end_offset; 663f754f88fSGreg Clayton return success; 664f754f88fSGreg Clayton } 665f754f88fSGreg Clayton 66630c2441aSAleksandr Urakov uint32_t ObjectFilePECOFF::GetRVA(const Address &addr) const { 66730c2441aSAleksandr Urakov return addr.GetFileAddress() - m_image_base; 66830c2441aSAleksandr Urakov } 66930c2441aSAleksandr Urakov 67030c2441aSAleksandr Urakov Address ObjectFilePECOFF::GetAddress(uint32_t rva) { 67130c2441aSAleksandr Urakov SectionList *sect_list = GetSectionList(); 67230c2441aSAleksandr Urakov if (!sect_list) 67330c2441aSAleksandr Urakov return Address(GetFileAddress(rva)); 67430c2441aSAleksandr Urakov 67530c2441aSAleksandr Urakov return Address(GetFileAddress(rva), sect_list); 67630c2441aSAleksandr Urakov } 67730c2441aSAleksandr Urakov 67830c2441aSAleksandr Urakov lldb::addr_t ObjectFilePECOFF::GetFileAddress(uint32_t rva) const { 67930c2441aSAleksandr Urakov return m_image_base + rva; 68030c2441aSAleksandr Urakov } 68130c2441aSAleksandr Urakov 682344546bdSWalter Erquinigo DataExtractor ObjectFilePECOFF::ReadImageData(uint32_t offset, size_t size) { 68330c2441aSAleksandr Urakov if (!size) 68430c2441aSAleksandr Urakov return {}; 68530c2441aSAleksandr Urakov 686a4a00cedSFred Riss if (m_data.ValidOffsetForDataOfSize(offset, size)) 687a4a00cedSFred Riss return DataExtractor(m_data, offset, size); 688a4a00cedSFred Riss 689344546bdSWalter Erquinigo ProcessSP process_sp(m_process_wp.lock()); 690344546bdSWalter Erquinigo DataExtractor data; 691344546bdSWalter Erquinigo if (process_sp) { 692a8f3ae7cSJonas Devlieghere auto data_up = std::make_unique<DataBufferHeap>(size, 0); 69397206d57SZachary Turner Status readmem_error; 694344546bdSWalter Erquinigo size_t bytes_read = 695d5b44036SJonas Devlieghere process_sp->ReadMemory(m_image_base + offset, data_up->GetBytes(), 696d5b44036SJonas Devlieghere data_up->GetByteSize(), readmem_error); 697344546bdSWalter Erquinigo if (bytes_read == size) { 698d5b44036SJonas Devlieghere DataBufferSP buffer_sp(data_up.release()); 699344546bdSWalter Erquinigo data.SetData(buffer_sp, 0, buffer_sp->GetByteSize()); 700344546bdSWalter Erquinigo } 701344546bdSWalter Erquinigo } 702344546bdSWalter Erquinigo return data; 703344546bdSWalter Erquinigo } 704344546bdSWalter Erquinigo 70530c2441aSAleksandr Urakov DataExtractor ObjectFilePECOFF::ReadImageDataByRVA(uint32_t rva, size_t size) { 70630c2441aSAleksandr Urakov Address addr = GetAddress(rva); 7077e1a3076SMartin Storsjö SectionSP sect = addr.GetSection(); 7087e1a3076SMartin Storsjö if (!sect) 7097e1a3076SMartin Storsjö return {}; 7107e1a3076SMartin Storsjö rva = sect->GetFileOffset() + addr.GetOffset(); 71130c2441aSAleksandr Urakov 71230c2441aSAleksandr Urakov return ReadImageData(rva, size); 71330c2441aSAleksandr Urakov } 71430c2441aSAleksandr Urakov 715f754f88fSGreg Clayton // ParseSectionHeaders 716b9c1b51eSKate Stone bool ObjectFilePECOFF::ParseSectionHeaders( 717b9c1b51eSKate Stone uint32_t section_header_data_offset) { 718f754f88fSGreg Clayton const uint32_t nsects = m_coff_header.nsects; 719f754f88fSGreg Clayton m_sect_headers.clear(); 720f754f88fSGreg Clayton 721b9c1b51eSKate Stone if (nsects > 0) { 722f754f88fSGreg Clayton const size_t section_header_byte_size = nsects * sizeof(section_header_t); 723344546bdSWalter Erquinigo DataExtractor section_header_data = 724344546bdSWalter Erquinigo ReadImageData(section_header_data_offset, section_header_byte_size); 725f754f88fSGreg Clayton 726c7bece56SGreg Clayton lldb::offset_t offset = 0; 727b9c1b51eSKate Stone if (section_header_data.ValidOffsetForDataOfSize( 728b9c1b51eSKate Stone offset, section_header_byte_size)) { 729f754f88fSGreg Clayton m_sect_headers.resize(nsects); 730f754f88fSGreg Clayton 731b9c1b51eSKate Stone for (uint32_t idx = 0; idx < nsects; ++idx) { 732f754f88fSGreg Clayton const void *name_data = section_header_data.GetData(&offset, 8); 733b9c1b51eSKate Stone if (name_data) { 734f754f88fSGreg Clayton memcpy(m_sect_headers[idx].name, name_data, 8); 735f754f88fSGreg Clayton m_sect_headers[idx].vmsize = section_header_data.GetU32(&offset); 736f754f88fSGreg Clayton m_sect_headers[idx].vmaddr = section_header_data.GetU32(&offset); 737f754f88fSGreg Clayton m_sect_headers[idx].size = section_header_data.GetU32(&offset); 738f754f88fSGreg Clayton m_sect_headers[idx].offset = section_header_data.GetU32(&offset); 739f754f88fSGreg Clayton m_sect_headers[idx].reloff = section_header_data.GetU32(&offset); 740f754f88fSGreg Clayton m_sect_headers[idx].lineoff = section_header_data.GetU32(&offset); 741f754f88fSGreg Clayton m_sect_headers[idx].nreloc = section_header_data.GetU16(&offset); 742f754f88fSGreg Clayton m_sect_headers[idx].nline = section_header_data.GetU16(&offset); 743f754f88fSGreg Clayton m_sect_headers[idx].flags = section_header_data.GetU32(&offset); 744f754f88fSGreg Clayton } 745f754f88fSGreg Clayton } 746f754f88fSGreg Clayton } 747f754f88fSGreg Clayton } 748f754f88fSGreg Clayton 749a6682a41SJonas Devlieghere return !m_sect_headers.empty(); 750f754f88fSGreg Clayton } 751f754f88fSGreg Clayton 7522886e4a0SPavel Labath llvm::StringRef ObjectFilePECOFF::GetSectionName(const section_header_t §) { 753f15014ffSBenjamin Kramer llvm::StringRef hdr_name(sect.name, llvm::array_lengthof(sect.name)); 7542886e4a0SPavel Labath hdr_name = hdr_name.split('\0').first; 7552886e4a0SPavel Labath if (hdr_name.consume_front("/")) { 7562886e4a0SPavel Labath lldb::offset_t stroff; 7572886e4a0SPavel Labath if (!to_integer(hdr_name, stroff, 10)) 7582886e4a0SPavel Labath return ""; 759b9c1b51eSKate Stone lldb::offset_t string_file_offset = 760b9c1b51eSKate Stone m_coff_header.symoff + (m_coff_header.nsyms * 18) + stroff; 7612886e4a0SPavel Labath if (const char *name = m_data.GetCStr(&string_file_offset)) 7622886e4a0SPavel Labath return name; 7632886e4a0SPavel Labath return ""; 764f754f88fSGreg Clayton } 7652886e4a0SPavel Labath return hdr_name; 766f754f88fSGreg Clayton } 767f754f88fSGreg Clayton 7687e6df41fSGreg Clayton void ObjectFilePECOFF::ParseSymtab(Symtab &symtab) { 769f754f88fSGreg Clayton SectionList *sect_list = GetSectionList(); 77028469ca3SGreg Clayton const uint32_t num_syms = m_coff_header.nsyms; 771344546bdSWalter Erquinigo if (m_file && num_syms > 0 && m_coff_header.symoff > 0) { 7720076e715SGreg Clayton const uint32_t symbol_size = 18; 77328469ca3SGreg Clayton const size_t symbol_data_size = num_syms * symbol_size; 774c35b91ceSAdrian McCarthy // Include the 4-byte string table size at the end of the symbols 775344546bdSWalter Erquinigo DataExtractor symtab_data = 776344546bdSWalter Erquinigo ReadImageData(m_coff_header.symoff, symbol_data_size + 4); 777c7bece56SGreg Clayton lldb::offset_t offset = symbol_data_size; 77828469ca3SGreg Clayton const uint32_t strtab_size = symtab_data.GetU32(&offset); 779344546bdSWalter Erquinigo if (strtab_size > 0) { 780344546bdSWalter Erquinigo DataExtractor strtab_data = ReadImageData( 781344546bdSWalter Erquinigo m_coff_header.symoff + symbol_data_size, strtab_size); 78228469ca3SGreg Clayton 78328469ca3SGreg Clayton offset = 0; 78428469ca3SGreg Clayton std::string symbol_name; 7857e6df41fSGreg Clayton Symbol *symbols = symtab.Resize(num_syms); 786b9c1b51eSKate Stone for (uint32_t i = 0; i < num_syms; ++i) { 787f754f88fSGreg Clayton coff_symbol_t symbol; 78828469ca3SGreg Clayton const uint32_t symbol_offset = offset; 789248a1305SKonrad Kleine const char *symbol_name_cstr = nullptr; 790c35b91ceSAdrian McCarthy // If the first 4 bytes of the symbol string are zero, then they 791c35b91ceSAdrian McCarthy // are followed by a 4-byte string table offset. Else these 79228469ca3SGreg Clayton // 8 bytes contain the symbol name 793b9c1b51eSKate Stone if (symtab_data.GetU32(&offset) == 0) { 79405097246SAdrian Prantl // Long string that doesn't fit into the symbol table name, so 79505097246SAdrian Prantl // now we must read the 4 byte string table offset 79628469ca3SGreg Clayton uint32_t strtab_offset = symtab_data.GetU32(&offset); 79728469ca3SGreg Clayton symbol_name_cstr = strtab_data.PeekCStr(strtab_offset); 79828469ca3SGreg Clayton symbol_name.assign(symbol_name_cstr); 799b9c1b51eSKate Stone } else { 800b9c1b51eSKate Stone // Short string that fits into the symbol table name which is 8 801b9c1b51eSKate Stone // bytes 80228469ca3SGreg Clayton offset += sizeof(symbol.name) - 4; // Skip remaining 80328469ca3SGreg Clayton symbol_name_cstr = symtab_data.PeekCStr(symbol_offset); 804248a1305SKonrad Kleine if (symbol_name_cstr == nullptr) 805f754f88fSGreg Clayton break; 80628469ca3SGreg Clayton symbol_name.assign(symbol_name_cstr, sizeof(symbol.name)); 80728469ca3SGreg Clayton } 80828469ca3SGreg Clayton symbol.value = symtab_data.GetU32(&offset); 80928469ca3SGreg Clayton symbol.sect = symtab_data.GetU16(&offset); 81028469ca3SGreg Clayton symbol.type = symtab_data.GetU16(&offset); 81128469ca3SGreg Clayton symbol.storage = symtab_data.GetU8(&offset); 81228469ca3SGreg Clayton symbol.naux = symtab_data.GetU8(&offset); 813037520e9SGreg Clayton symbols[i].GetMangled().SetValue(ConstString(symbol_name.c_str())); 814b9c1b51eSKate Stone if ((int16_t)symbol.sect >= 1) { 8154394b5beSMartin Storsjö Address symbol_addr(sect_list->FindSectionByID(symbol.sect), 816b9c1b51eSKate Stone symbol.value); 817358cf1eaSGreg Clayton symbols[i].GetAddressRef() = symbol_addr; 818c35b91ceSAdrian McCarthy symbols[i].SetType(MapSymbolType(symbol.type)); 8190076e715SGreg Clayton } 820f754f88fSGreg Clayton 821b9c1b51eSKate Stone if (symbol.naux > 0) { 822f754f88fSGreg Clayton i += symbol.naux; 823f07ddbc9SMartin Storsjö offset += symbol.naux * symbol_size; 8240076e715SGreg Clayton } 825f754f88fSGreg Clayton } 826f754f88fSGreg Clayton } 827344546bdSWalter Erquinigo } 828a4fe3a12SVirgile Bello 829a4fe3a12SVirgile Bello // Read export header 830b9c1b51eSKate Stone if (coff_data_dir_export_table < m_coff_header_opt.data_dirs.size() && 831b9c1b51eSKate Stone m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmsize > 0 && 832b9c1b51eSKate Stone m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmaddr > 0) { 833a4fe3a12SVirgile Bello export_directory_entry export_table; 834b9c1b51eSKate Stone uint32_t data_start = 835b9c1b51eSKate Stone m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmaddr; 836344546bdSWalter Erquinigo 83730c2441aSAleksandr Urakov DataExtractor symtab_data = ReadImageDataByRVA( 83830c2441aSAleksandr Urakov data_start, m_coff_header_opt.data_dirs[0].vmsize); 839a4fe3a12SVirgile Bello lldb::offset_t offset = 0; 840a4fe3a12SVirgile Bello 841a4fe3a12SVirgile Bello // Read export_table header 842a4fe3a12SVirgile Bello export_table.characteristics = symtab_data.GetU32(&offset); 843a4fe3a12SVirgile Bello export_table.time_date_stamp = symtab_data.GetU32(&offset); 844a4fe3a12SVirgile Bello export_table.major_version = symtab_data.GetU16(&offset); 845a4fe3a12SVirgile Bello export_table.minor_version = symtab_data.GetU16(&offset); 846a4fe3a12SVirgile Bello export_table.name = symtab_data.GetU32(&offset); 847a4fe3a12SVirgile Bello export_table.base = symtab_data.GetU32(&offset); 848a4fe3a12SVirgile Bello export_table.number_of_functions = symtab_data.GetU32(&offset); 849a4fe3a12SVirgile Bello export_table.number_of_names = symtab_data.GetU32(&offset); 850a4fe3a12SVirgile Bello export_table.address_of_functions = symtab_data.GetU32(&offset); 851a4fe3a12SVirgile Bello export_table.address_of_names = symtab_data.GetU32(&offset); 852a4fe3a12SVirgile Bello export_table.address_of_name_ordinals = symtab_data.GetU32(&offset); 853a4fe3a12SVirgile Bello 854a4fe3a12SVirgile Bello bool has_ordinal = export_table.address_of_name_ordinals != 0; 855a4fe3a12SVirgile Bello 856a4fe3a12SVirgile Bello lldb::offset_t name_offset = export_table.address_of_names - data_start; 857b9c1b51eSKate Stone lldb::offset_t name_ordinal_offset = 858b9c1b51eSKate Stone export_table.address_of_name_ordinals - data_start; 859a4fe3a12SVirgile Bello 8607e6df41fSGreg Clayton Symbol *symbols = symtab.Resize(export_table.number_of_names); 861a4fe3a12SVirgile Bello 862a4fe3a12SVirgile Bello std::string symbol_name; 863a4fe3a12SVirgile Bello 864a4fe3a12SVirgile Bello // Read each export table entry 865b9c1b51eSKate Stone for (size_t i = 0; i < export_table.number_of_names; ++i) { 866b9c1b51eSKate Stone uint32_t name_ordinal = 867b9c1b51eSKate Stone has_ordinal ? symtab_data.GetU16(&name_ordinal_offset) : i; 868a4fe3a12SVirgile Bello uint32_t name_address = symtab_data.GetU32(&name_offset); 869a4fe3a12SVirgile Bello 870b9c1b51eSKate Stone const char *symbol_name_cstr = 871b9c1b51eSKate Stone symtab_data.PeekCStr(name_address - data_start); 872a4fe3a12SVirgile Bello symbol_name.assign(symbol_name_cstr); 873a4fe3a12SVirgile Bello 874b9c1b51eSKate Stone lldb::offset_t function_offset = export_table.address_of_functions - 875b9c1b51eSKate Stone data_start + 876b9c1b51eSKate Stone sizeof(uint32_t) * name_ordinal; 877a4fe3a12SVirgile Bello uint32_t function_rva = symtab_data.GetU32(&function_offset); 878a4fe3a12SVirgile Bello 879b9c1b51eSKate Stone Address symbol_addr(m_coff_header_opt.image_base + function_rva, 880b9c1b51eSKate Stone sect_list); 881a4fe3a12SVirgile Bello symbols[i].GetMangled().SetValue(ConstString(symbol_name.c_str())); 882358cf1eaSGreg Clayton symbols[i].GetAddressRef() = symbol_addr; 883a4fe3a12SVirgile Bello symbols[i].SetType(lldb::eSymbolTypeCode); 884a4fe3a12SVirgile Bello symbols[i].SetDebug(true); 885a4fe3a12SVirgile Bello } 886a4fe3a12SVirgile Bello } 887f754f88fSGreg Clayton } 888f754f88fSGreg Clayton 88930c2441aSAleksandr Urakov std::unique_ptr<CallFrameInfo> ObjectFilePECOFF::CreateCallFrameInfo() { 89030c2441aSAleksandr Urakov if (coff_data_dir_exception_table >= m_coff_header_opt.data_dirs.size()) 89130c2441aSAleksandr Urakov return {}; 89230c2441aSAleksandr Urakov 89330c2441aSAleksandr Urakov data_directory data_dir_exception = 89430c2441aSAleksandr Urakov m_coff_header_opt.data_dirs[coff_data_dir_exception_table]; 89530c2441aSAleksandr Urakov if (!data_dir_exception.vmaddr) 89630c2441aSAleksandr Urakov return {}; 89730c2441aSAleksandr Urakov 898aa786b88SMartin Storsjö if (m_coff_header.machine != llvm::COFF::IMAGE_FILE_MACHINE_AMD64) 899aa786b88SMartin Storsjö return {}; 900aa786b88SMartin Storsjö 90130c2441aSAleksandr Urakov return std::make_unique<PECallFrameInfo>(*this, data_dir_exception.vmaddr, 90230c2441aSAleksandr Urakov data_dir_exception.vmsize); 90330c2441aSAleksandr Urakov } 90430c2441aSAleksandr Urakov 905b9c1b51eSKate Stone bool ObjectFilePECOFF::IsStripped() { 9063046e668SGreg Clayton // TODO: determine this for COFF 9073046e668SGreg Clayton return false; 9083046e668SGreg Clayton } 9093046e668SGreg Clayton 9102e5bb6d8SMartin Storsjö SectionType ObjectFilePECOFF::GetSectionType(llvm::StringRef sect_name, 9112e5bb6d8SMartin Storsjö const section_header_t §) { 9122e5bb6d8SMartin Storsjö ConstString const_sect_name(sect_name); 9132e5bb6d8SMartin Storsjö static ConstString g_code_sect_name(".code"); 9142e5bb6d8SMartin Storsjö static ConstString g_CODE_sect_name("CODE"); 9152e5bb6d8SMartin Storsjö static ConstString g_data_sect_name(".data"); 9162e5bb6d8SMartin Storsjö static ConstString g_DATA_sect_name("DATA"); 9172e5bb6d8SMartin Storsjö static ConstString g_bss_sect_name(".bss"); 9182e5bb6d8SMartin Storsjö static ConstString g_BSS_sect_name("BSS"); 9192e5bb6d8SMartin Storsjö 9202e5bb6d8SMartin Storsjö if (sect.flags & llvm::COFF::IMAGE_SCN_CNT_CODE && 9212e5bb6d8SMartin Storsjö ((const_sect_name == g_code_sect_name) || 9222e5bb6d8SMartin Storsjö (const_sect_name == g_CODE_sect_name))) { 9232e5bb6d8SMartin Storsjö return eSectionTypeCode; 9242e5bb6d8SMartin Storsjö } 9252e5bb6d8SMartin Storsjö if (sect.flags & llvm::COFF::IMAGE_SCN_CNT_INITIALIZED_DATA && 9262e5bb6d8SMartin Storsjö ((const_sect_name == g_data_sect_name) || 9272e5bb6d8SMartin Storsjö (const_sect_name == g_DATA_sect_name))) { 9282e5bb6d8SMartin Storsjö if (sect.size == 0 && sect.offset == 0) 9292e5bb6d8SMartin Storsjö return eSectionTypeZeroFill; 9302e5bb6d8SMartin Storsjö else 9312e5bb6d8SMartin Storsjö return eSectionTypeData; 9322e5bb6d8SMartin Storsjö } 9332e5bb6d8SMartin Storsjö if (sect.flags & llvm::COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA && 9342e5bb6d8SMartin Storsjö ((const_sect_name == g_bss_sect_name) || 9352e5bb6d8SMartin Storsjö (const_sect_name == g_BSS_sect_name))) { 9362e5bb6d8SMartin Storsjö if (sect.size == 0) 9372e5bb6d8SMartin Storsjö return eSectionTypeZeroFill; 9382e5bb6d8SMartin Storsjö else 9392e5bb6d8SMartin Storsjö return eSectionTypeData; 9402e5bb6d8SMartin Storsjö } 9412e5bb6d8SMartin Storsjö 9422e5bb6d8SMartin Storsjö SectionType section_type = 9432e5bb6d8SMartin Storsjö llvm::StringSwitch<SectionType>(sect_name) 9442e5bb6d8SMartin Storsjö .Case(".debug", eSectionTypeDebug) 9452e5bb6d8SMartin Storsjö .Case(".stabstr", eSectionTypeDataCString) 9462e5bb6d8SMartin Storsjö .Case(".reloc", eSectionTypeOther) 9472e5bb6d8SMartin Storsjö .Case(".debug_abbrev", eSectionTypeDWARFDebugAbbrev) 9482e5bb6d8SMartin Storsjö .Case(".debug_aranges", eSectionTypeDWARFDebugAranges) 9492e5bb6d8SMartin Storsjö .Case(".debug_frame", eSectionTypeDWARFDebugFrame) 9502e5bb6d8SMartin Storsjö .Case(".debug_info", eSectionTypeDWARFDebugInfo) 9512e5bb6d8SMartin Storsjö .Case(".debug_line", eSectionTypeDWARFDebugLine) 9522e5bb6d8SMartin Storsjö .Case(".debug_loc", eSectionTypeDWARFDebugLoc) 9532e5bb6d8SMartin Storsjö .Case(".debug_loclists", eSectionTypeDWARFDebugLocLists) 9542e5bb6d8SMartin Storsjö .Case(".debug_macinfo", eSectionTypeDWARFDebugMacInfo) 9552e5bb6d8SMartin Storsjö .Case(".debug_names", eSectionTypeDWARFDebugNames) 9562e5bb6d8SMartin Storsjö .Case(".debug_pubnames", eSectionTypeDWARFDebugPubNames) 9572e5bb6d8SMartin Storsjö .Case(".debug_pubtypes", eSectionTypeDWARFDebugPubTypes) 9582e5bb6d8SMartin Storsjö .Case(".debug_ranges", eSectionTypeDWARFDebugRanges) 9592e5bb6d8SMartin Storsjö .Case(".debug_str", eSectionTypeDWARFDebugStr) 9602e5bb6d8SMartin Storsjö .Case(".debug_types", eSectionTypeDWARFDebugTypes) 961934c025eSMartin Storsjö // .eh_frame can be truncated to 8 chars. 962934c025eSMartin Storsjö .Cases(".eh_frame", ".eh_fram", eSectionTypeEHFrame) 9632e5bb6d8SMartin Storsjö .Case(".gosymtab", eSectionTypeGoSymtab) 9642e5bb6d8SMartin Storsjö .Default(eSectionTypeInvalid); 9652e5bb6d8SMartin Storsjö if (section_type != eSectionTypeInvalid) 9662e5bb6d8SMartin Storsjö return section_type; 9672e5bb6d8SMartin Storsjö 9682e5bb6d8SMartin Storsjö if (sect.flags & llvm::COFF::IMAGE_SCN_CNT_CODE) 9692e5bb6d8SMartin Storsjö return eSectionTypeCode; 9702e5bb6d8SMartin Storsjö if (sect.flags & llvm::COFF::IMAGE_SCN_CNT_INITIALIZED_DATA) 9712e5bb6d8SMartin Storsjö return eSectionTypeData; 9722e5bb6d8SMartin Storsjö if (sect.flags & llvm::COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) { 9732e5bb6d8SMartin Storsjö if (sect.size == 0) 9742e5bb6d8SMartin Storsjö return eSectionTypeZeroFill; 9752e5bb6d8SMartin Storsjö else 9762e5bb6d8SMartin Storsjö return eSectionTypeData; 9772e5bb6d8SMartin Storsjö } 9782e5bb6d8SMartin Storsjö return eSectionTypeOther; 9792e5bb6d8SMartin Storsjö } 9802e5bb6d8SMartin Storsjö 981b9c1b51eSKate Stone void ObjectFilePECOFF::CreateSections(SectionList &unified_section_list) { 982d5b44036SJonas Devlieghere if (m_sections_up) 98388a2c2a4SPavel Labath return; 98406412daeSJonas Devlieghere m_sections_up = std::make_unique<SectionList>(); 985a1743499SGreg Clayton ModuleSP module_sp(GetModule()); 986b9c1b51eSKate Stone if (module_sp) { 98716ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 9887db8b5c4SPavel Labath 98973a7a55cSPavel Labath SectionSP header_sp = std::make_shared<Section>( 99073a7a55cSPavel Labath module_sp, this, ~user_id_t(0), ConstString("PECOFF header"), 99173a7a55cSPavel Labath eSectionTypeOther, m_coff_header_opt.image_base, 99273a7a55cSPavel Labath m_coff_header_opt.header_size, 99373a7a55cSPavel Labath /*file_offset*/ 0, m_coff_header_opt.header_size, 99473a7a55cSPavel Labath m_coff_header_opt.sect_alignment, 9957db8b5c4SPavel Labath /*flags*/ 0); 996f1e0ae34SPavel Labath header_sp->SetPermissions(ePermissionsReadable); 99773a7a55cSPavel Labath m_sections_up->AddSection(header_sp); 99873a7a55cSPavel Labath unified_section_list.AddSection(header_sp); 9997db8b5c4SPavel Labath 1000f754f88fSGreg Clayton const uint32_t nsects = m_sect_headers.size(); 1001e72dfb32SGreg Clayton ModuleSP module_sp(GetModule()); 1002b9c1b51eSKate Stone for (uint32_t idx = 0; idx < nsects; ++idx) { 10032e5bb6d8SMartin Storsjö llvm::StringRef sect_name = GetSectionName(m_sect_headers[idx]); 10042e5bb6d8SMartin Storsjö ConstString const_sect_name(sect_name); 10052e5bb6d8SMartin Storsjö SectionType section_type = GetSectionType(sect_name, m_sect_headers[idx]); 1006f754f88fSGreg Clayton 1007b9c1b51eSKate Stone SectionSP section_sp(new Section( 1008b9c1b51eSKate Stone module_sp, // Module to which this section belongs 1009a7499c98SMichael Sartain this, // Object file to which this section belongs 10107db8b5c4SPavel Labath idx + 1, // Section ID is the 1 based section index. 1011f754f88fSGreg Clayton const_sect_name, // Name of this section 10127db8b5c4SPavel Labath section_type, 101373a7a55cSPavel Labath m_coff_header_opt.image_base + 1014b9c1b51eSKate Stone m_sect_headers[idx].vmaddr, // File VM address == addresses as 1015b9c1b51eSKate Stone // they are found in the object file 1016f754f88fSGreg Clayton m_sect_headers[idx].vmsize, // VM size in bytes of this section 1017b9c1b51eSKate Stone m_sect_headers[idx] 1018b9c1b51eSKate Stone .offset, // Offset to the data for this section in the file 1019b9c1b51eSKate Stone m_sect_headers[idx] 1020b9c1b51eSKate Stone .size, // Size in bytes of this section as found in the file 102148672afbSGreg Clayton m_coff_header_opt.sect_alignment, // Section alignment 1022f754f88fSGreg Clayton m_sect_headers[idx].flags)); // Flags for this section 1023f754f88fSGreg Clayton 1024f1e0ae34SPavel Labath uint32_t permissions = 0; 1025f1e0ae34SPavel Labath if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_MEM_EXECUTE) 1026f1e0ae34SPavel Labath permissions |= ePermissionsExecutable; 1027f1e0ae34SPavel Labath if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_MEM_READ) 1028f1e0ae34SPavel Labath permissions |= ePermissionsReadable; 1029f1e0ae34SPavel Labath if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_MEM_WRITE) 1030f1e0ae34SPavel Labath permissions |= ePermissionsWritable; 1031f1e0ae34SPavel Labath section_sp->SetPermissions(permissions); 1032f1e0ae34SPavel Labath 103373a7a55cSPavel Labath m_sections_up->AddSection(section_sp); 103473a7a55cSPavel Labath unified_section_list.AddSection(section_sp); 1035f754f88fSGreg Clayton } 1036f754f88fSGreg Clayton } 1037a1743499SGreg Clayton } 1038f754f88fSGreg Clayton 1039b8d03935SAaron Smith UUID ObjectFilePECOFF::GetUUID() { 1040b8d03935SAaron Smith if (m_uuid.IsValid()) 1041b8d03935SAaron Smith return m_uuid; 1042b8d03935SAaron Smith 1043b8d03935SAaron Smith if (!CreateBinary()) 1044b8d03935SAaron Smith return UUID(); 1045b8d03935SAaron Smith 1046d372a8e8SPavel Labath m_uuid = GetCoffUUID(*m_binary); 1047b8d03935SAaron Smith return m_uuid; 1048b8d03935SAaron Smith } 1049f754f88fSGreg Clayton 1050c8daf4a7SAlvin Wong llvm::Optional<FileSpec> ObjectFilePECOFF::GetDebugLink() { 1051c8daf4a7SAlvin Wong std::string gnu_debuglink_file; 1052c8daf4a7SAlvin Wong uint32_t gnu_debuglink_crc; 1053c8daf4a7SAlvin Wong if (GetDebugLinkContents(*m_binary, gnu_debuglink_file, gnu_debuglink_crc)) 1054c8daf4a7SAlvin Wong return FileSpec(gnu_debuglink_file); 1055c8daf4a7SAlvin Wong return llvm::None; 1056c8daf4a7SAlvin Wong } 1057c8daf4a7SAlvin Wong 1058037ed1beSAaron Smith uint32_t ObjectFilePECOFF::ParseDependentModules() { 1059037ed1beSAaron Smith ModuleSP module_sp(GetModule()); 1060037ed1beSAaron Smith if (!module_sp) 1061f754f88fSGreg Clayton return 0; 1062037ed1beSAaron Smith 1063037ed1beSAaron Smith std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 1064037ed1beSAaron Smith if (m_deps_filespec) 1065037ed1beSAaron Smith return m_deps_filespec->GetSize(); 1066037ed1beSAaron Smith 1067037ed1beSAaron Smith // Cache coff binary if it is not done yet. 1068037ed1beSAaron Smith if (!CreateBinary()) 1069037ed1beSAaron Smith return 0; 1070037ed1beSAaron Smith 1071a007a6d8SPavel Labath Log *log = GetLog(LLDBLog::Object); 1072d372a8e8SPavel Labath LLDB_LOG(log, "this = {0}, module = {1} ({2}), file = {3}, binary = {4}", 1073d372a8e8SPavel Labath this, GetModule().get(), GetModule()->GetSpecificationDescription(), 1074d372a8e8SPavel Labath m_file.GetPath(), m_binary.get()); 1075037ed1beSAaron Smith 1076037ed1beSAaron Smith m_deps_filespec = FileSpecList(); 1077037ed1beSAaron Smith 1078d372a8e8SPavel Labath for (const auto &entry : m_binary->import_directories()) { 1079037ed1beSAaron Smith llvm::StringRef dll_name; 1080037ed1beSAaron Smith // Report a bogus entry. 10811c03389cSReid Kleckner if (llvm::Error e = entry.getName(dll_name)) { 108263e5fb76SJonas Devlieghere LLDB_LOGF(log, 108363e5fb76SJonas Devlieghere "ObjectFilePECOFF::ParseDependentModules() - failed to get " 1084037ed1beSAaron Smith "import directory entry name: %s", 10851c03389cSReid Kleckner llvm::toString(std::move(e)).c_str()); 1086037ed1beSAaron Smith continue; 1087037ed1beSAaron Smith } 1088037ed1beSAaron Smith 1089037ed1beSAaron Smith // At this moment we only have the base name of the DLL. The full path can 1090037ed1beSAaron Smith // only be seen after the dynamic loading. Our best guess is Try to get it 1091037ed1beSAaron Smith // with the help of the object file's directory. 1092b3f44ad9SStella Stamenova llvm::SmallString<128> dll_fullpath; 1093037ed1beSAaron Smith FileSpec dll_specs(dll_name); 1094037ed1beSAaron Smith dll_specs.GetDirectory().SetString(m_file.GetDirectory().GetCString()); 1095037ed1beSAaron Smith 1096037ed1beSAaron Smith if (!llvm::sys::fs::real_path(dll_specs.GetPath(), dll_fullpath)) 1097f893d5bfSJonas Devlieghere m_deps_filespec->EmplaceBack(dll_fullpath); 1098037ed1beSAaron Smith else { 1099037ed1beSAaron Smith // Known DLLs or DLL not found in the object file directory. 1100f893d5bfSJonas Devlieghere m_deps_filespec->EmplaceBack(dll_name); 1101037ed1beSAaron Smith } 1102037ed1beSAaron Smith } 1103037ed1beSAaron Smith return m_deps_filespec->GetSize(); 1104037ed1beSAaron Smith } 1105037ed1beSAaron Smith 1106037ed1beSAaron Smith uint32_t ObjectFilePECOFF::GetDependentModules(FileSpecList &files) { 1107037ed1beSAaron Smith auto num_modules = ParseDependentModules(); 1108037ed1beSAaron Smith auto original_size = files.GetSize(); 1109037ed1beSAaron Smith 1110037ed1beSAaron Smith for (unsigned i = 0; i < num_modules; ++i) 1111037ed1beSAaron Smith files.AppendIfUnique(m_deps_filespec->GetFileSpecAtIndex(i)); 1112037ed1beSAaron Smith 1113037ed1beSAaron Smith return files.GetSize() - original_size; 1114f754f88fSGreg Clayton } 1115f754f88fSGreg Clayton 1116b9c1b51eSKate Stone lldb_private::Address ObjectFilePECOFF::GetEntryPointAddress() { 11178e38c666SStephane Sezer if (m_entry_point_address.IsValid()) 11188e38c666SStephane Sezer return m_entry_point_address; 11198e38c666SStephane Sezer 11208e38c666SStephane Sezer if (!ParseHeader() || !IsExecutable()) 11218e38c666SStephane Sezer return m_entry_point_address; 11228e38c666SStephane Sezer 11238e38c666SStephane Sezer SectionList *section_list = GetSectionList(); 1124a5235af9SAleksandr Urakov addr_t file_addr = m_coff_header_opt.entry + m_coff_header_opt.image_base; 11258e38c666SStephane Sezer 11268e38c666SStephane Sezer if (!section_list) 1127a5235af9SAleksandr Urakov m_entry_point_address.SetOffset(file_addr); 11288e38c666SStephane Sezer else 1129b8d03935SAaron Smith m_entry_point_address.ResolveAddressUsingFileSections(file_addr, 1130b8d03935SAaron Smith section_list); 11318e38c666SStephane Sezer return m_entry_point_address; 11328e38c666SStephane Sezer } 11338e38c666SStephane Sezer 1134d1304bbaSPavel Labath Address ObjectFilePECOFF::GetBaseAddress() { 1135d1304bbaSPavel Labath return Address(GetSectionList()->GetSectionAtIndex(0), 0); 1136d1304bbaSPavel Labath } 1137d1304bbaSPavel Labath 1138f754f88fSGreg Clayton // Dump 1139f754f88fSGreg Clayton // 1140f754f88fSGreg Clayton // Dump the specifics of the runtime file container (such as any headers 1141f754f88fSGreg Clayton // segments, sections, etc). 1142b9c1b51eSKate Stone void ObjectFilePECOFF::Dump(Stream *s) { 1143a1743499SGreg Clayton ModuleSP module_sp(GetModule()); 1144b9c1b51eSKate Stone if (module_sp) { 114516ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 1146324a1036SSaleem Abdulrasool s->Printf("%p: ", static_cast<void *>(this)); 1147f754f88fSGreg Clayton s->Indent(); 1148f754f88fSGreg Clayton s->PutCString("ObjectFilePECOFF"); 1149f754f88fSGreg Clayton 1150f760f5aeSPavel Labath ArchSpec header_arch = GetArchitecture(); 1151f754f88fSGreg Clayton 1152b9c1b51eSKate Stone *s << ", file = '" << m_file 1153b9c1b51eSKate Stone << "', arch = " << header_arch.GetArchitectureName() << "\n"; 1154f754f88fSGreg Clayton 11553046e668SGreg Clayton SectionList *sections = GetSectionList(); 11563046e668SGreg Clayton if (sections) 11573a168297SPavel Labath sections->Dump(s->AsRawOstream(), s->GetIndentLevel(), nullptr, true, 11583a168297SPavel Labath UINT32_MAX); 1159f754f88fSGreg Clayton 1160d5b44036SJonas Devlieghere if (m_symtab_up) 1161248a1305SKonrad Kleine m_symtab_up->Dump(s, nullptr, eSortOrderNone); 1162f754f88fSGreg Clayton 1163f754f88fSGreg Clayton if (m_dos_header.e_magic) 1164f754f88fSGreg Clayton DumpDOSHeader(s, m_dos_header); 1165b9c1b51eSKate Stone if (m_coff_header.machine) { 1166f754f88fSGreg Clayton DumpCOFFHeader(s, m_coff_header); 1167f754f88fSGreg Clayton if (m_coff_header.hdrsize) 1168f754f88fSGreg Clayton DumpOptCOFFHeader(s, m_coff_header_opt); 1169f754f88fSGreg Clayton } 1170f754f88fSGreg Clayton s->EOL(); 1171f754f88fSGreg Clayton DumpSectionHeaders(s); 1172f754f88fSGreg Clayton s->EOL(); 1173037ed1beSAaron Smith 1174037ed1beSAaron Smith DumpDependentModules(s); 1175037ed1beSAaron Smith s->EOL(); 1176f754f88fSGreg Clayton } 1177a1743499SGreg Clayton } 1178f754f88fSGreg Clayton 1179f754f88fSGreg Clayton // DumpDOSHeader 1180f754f88fSGreg Clayton // 1181f754f88fSGreg Clayton // Dump the MS-DOS header to the specified output stream 1182b9c1b51eSKate Stone void ObjectFilePECOFF::DumpDOSHeader(Stream *s, const dos_header_t &header) { 1183f754f88fSGreg Clayton s->PutCString("MSDOS Header\n"); 1184f754f88fSGreg Clayton s->Printf(" e_magic = 0x%4.4x\n", header.e_magic); 1185f754f88fSGreg Clayton s->Printf(" e_cblp = 0x%4.4x\n", header.e_cblp); 1186f754f88fSGreg Clayton s->Printf(" e_cp = 0x%4.4x\n", header.e_cp); 1187f754f88fSGreg Clayton s->Printf(" e_crlc = 0x%4.4x\n", header.e_crlc); 1188f754f88fSGreg Clayton s->Printf(" e_cparhdr = 0x%4.4x\n", header.e_cparhdr); 1189f754f88fSGreg Clayton s->Printf(" e_minalloc = 0x%4.4x\n", header.e_minalloc); 1190f754f88fSGreg Clayton s->Printf(" e_maxalloc = 0x%4.4x\n", header.e_maxalloc); 1191f754f88fSGreg Clayton s->Printf(" e_ss = 0x%4.4x\n", header.e_ss); 1192f754f88fSGreg Clayton s->Printf(" e_sp = 0x%4.4x\n", header.e_sp); 1193f754f88fSGreg Clayton s->Printf(" e_csum = 0x%4.4x\n", header.e_csum); 1194f754f88fSGreg Clayton s->Printf(" e_ip = 0x%4.4x\n", header.e_ip); 1195f754f88fSGreg Clayton s->Printf(" e_cs = 0x%4.4x\n", header.e_cs); 1196f754f88fSGreg Clayton s->Printf(" e_lfarlc = 0x%4.4x\n", header.e_lfarlc); 1197f754f88fSGreg Clayton s->Printf(" e_ovno = 0x%4.4x\n", header.e_ovno); 1198f754f88fSGreg Clayton s->Printf(" e_res[4] = { 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x }\n", 1199b9c1b51eSKate Stone header.e_res[0], header.e_res[1], header.e_res[2], header.e_res[3]); 1200f754f88fSGreg Clayton s->Printf(" e_oemid = 0x%4.4x\n", header.e_oemid); 1201f754f88fSGreg Clayton s->Printf(" e_oeminfo = 0x%4.4x\n", header.e_oeminfo); 1202b9c1b51eSKate Stone s->Printf(" e_res2[10] = { 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, " 1203b9c1b51eSKate Stone "0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x }\n", 1204b9c1b51eSKate Stone header.e_res2[0], header.e_res2[1], header.e_res2[2], 1205b9c1b51eSKate Stone header.e_res2[3], header.e_res2[4], header.e_res2[5], 1206b9c1b51eSKate Stone header.e_res2[6], header.e_res2[7], header.e_res2[8], 1207f754f88fSGreg Clayton header.e_res2[9]); 1208f754f88fSGreg Clayton s->Printf(" e_lfanew = 0x%8.8x\n", header.e_lfanew); 1209f754f88fSGreg Clayton } 1210f754f88fSGreg Clayton 1211f754f88fSGreg Clayton // DumpCOFFHeader 1212f754f88fSGreg Clayton // 1213f754f88fSGreg Clayton // Dump the COFF header to the specified output stream 1214b9c1b51eSKate Stone void ObjectFilePECOFF::DumpCOFFHeader(Stream *s, const coff_header_t &header) { 1215f754f88fSGreg Clayton s->PutCString("COFF Header\n"); 1216f754f88fSGreg Clayton s->Printf(" machine = 0x%4.4x\n", header.machine); 1217f754f88fSGreg Clayton s->Printf(" nsects = 0x%4.4x\n", header.nsects); 1218f754f88fSGreg Clayton s->Printf(" modtime = 0x%8.8x\n", header.modtime); 1219f754f88fSGreg Clayton s->Printf(" symoff = 0x%8.8x\n", header.symoff); 1220f754f88fSGreg Clayton s->Printf(" nsyms = 0x%8.8x\n", header.nsyms); 1221f754f88fSGreg Clayton s->Printf(" hdrsize = 0x%4.4x\n", header.hdrsize); 1222f754f88fSGreg Clayton } 1223f754f88fSGreg Clayton 1224f754f88fSGreg Clayton // DumpOptCOFFHeader 1225f754f88fSGreg Clayton // 1226f754f88fSGreg Clayton // Dump the optional COFF header to the specified output stream 1227b9c1b51eSKate Stone void ObjectFilePECOFF::DumpOptCOFFHeader(Stream *s, 1228b9c1b51eSKate Stone const coff_opt_header_t &header) { 1229f754f88fSGreg Clayton s->PutCString("Optional COFF Header\n"); 1230f754f88fSGreg Clayton s->Printf(" magic = 0x%4.4x\n", header.magic); 1231b9c1b51eSKate Stone s->Printf(" major_linker_version = 0x%2.2x\n", 1232b9c1b51eSKate Stone header.major_linker_version); 1233b9c1b51eSKate Stone s->Printf(" minor_linker_version = 0x%2.2x\n", 1234b9c1b51eSKate Stone header.minor_linker_version); 1235f754f88fSGreg Clayton s->Printf(" code_size = 0x%8.8x\n", header.code_size); 1236f754f88fSGreg Clayton s->Printf(" data_size = 0x%8.8x\n", header.data_size); 1237f754f88fSGreg Clayton s->Printf(" bss_size = 0x%8.8x\n", header.bss_size); 1238f754f88fSGreg Clayton s->Printf(" entry = 0x%8.8x\n", header.entry); 1239f754f88fSGreg Clayton s->Printf(" code_offset = 0x%8.8x\n", header.code_offset); 1240f754f88fSGreg Clayton s->Printf(" data_offset = 0x%8.8x\n", header.data_offset); 1241b9c1b51eSKate Stone s->Printf(" image_base = 0x%16.16" PRIx64 "\n", 1242b9c1b51eSKate Stone header.image_base); 1243f754f88fSGreg Clayton s->Printf(" sect_alignment = 0x%8.8x\n", header.sect_alignment); 1244f754f88fSGreg Clayton s->Printf(" file_alignment = 0x%8.8x\n", header.file_alignment); 1245b9c1b51eSKate Stone s->Printf(" major_os_system_version = 0x%4.4x\n", 1246b9c1b51eSKate Stone header.major_os_system_version); 1247b9c1b51eSKate Stone s->Printf(" minor_os_system_version = 0x%4.4x\n", 1248b9c1b51eSKate Stone header.minor_os_system_version); 1249b9c1b51eSKate Stone s->Printf(" major_image_version = 0x%4.4x\n", 1250b9c1b51eSKate Stone header.major_image_version); 1251b9c1b51eSKate Stone s->Printf(" minor_image_version = 0x%4.4x\n", 1252b9c1b51eSKate Stone header.minor_image_version); 1253b9c1b51eSKate Stone s->Printf(" major_subsystem_version = 0x%4.4x\n", 1254b9c1b51eSKate Stone header.major_subsystem_version); 1255b9c1b51eSKate Stone s->Printf(" minor_subsystem_version = 0x%4.4x\n", 1256b9c1b51eSKate Stone header.minor_subsystem_version); 1257f754f88fSGreg Clayton s->Printf(" reserved1 = 0x%8.8x\n", header.reserved1); 1258f754f88fSGreg Clayton s->Printf(" image_size = 0x%8.8x\n", header.image_size); 1259f754f88fSGreg Clayton s->Printf(" header_size = 0x%8.8x\n", header.header_size); 126028469ca3SGreg Clayton s->Printf(" checksum = 0x%8.8x\n", header.checksum); 1261f754f88fSGreg Clayton s->Printf(" subsystem = 0x%4.4x\n", header.subsystem); 1262f754f88fSGreg Clayton s->Printf(" dll_flags = 0x%4.4x\n", header.dll_flags); 1263b9c1b51eSKate Stone s->Printf(" stack_reserve_size = 0x%16.16" PRIx64 "\n", 1264b9c1b51eSKate Stone header.stack_reserve_size); 1265b9c1b51eSKate Stone s->Printf(" stack_commit_size = 0x%16.16" PRIx64 "\n", 1266b9c1b51eSKate Stone header.stack_commit_size); 1267b9c1b51eSKate Stone s->Printf(" heap_reserve_size = 0x%16.16" PRIx64 "\n", 1268b9c1b51eSKate Stone header.heap_reserve_size); 1269b9c1b51eSKate Stone s->Printf(" heap_commit_size = 0x%16.16" PRIx64 "\n", 1270b9c1b51eSKate Stone header.heap_commit_size); 1271f754f88fSGreg Clayton s->Printf(" loader_flags = 0x%8.8x\n", header.loader_flags); 1272b9c1b51eSKate Stone s->Printf(" num_data_dir_entries = 0x%8.8x\n", 1273b9c1b51eSKate Stone (uint32_t)header.data_dirs.size()); 1274f754f88fSGreg Clayton uint32_t i; 1275b9c1b51eSKate Stone for (i = 0; i < header.data_dirs.size(); i++) { 1276b9c1b51eSKate Stone s->Printf(" data_dirs[%2u] vmaddr = 0x%8.8x, vmsize = 0x%8.8x\n", i, 1277b9c1b51eSKate Stone header.data_dirs[i].vmaddr, header.data_dirs[i].vmsize); 1278f754f88fSGreg Clayton } 1279f754f88fSGreg Clayton } 1280f754f88fSGreg Clayton // DumpSectionHeader 1281f754f88fSGreg Clayton // 1282f754f88fSGreg Clayton // Dump a single ELF section header to the specified output stream 1283b9c1b51eSKate Stone void ObjectFilePECOFF::DumpSectionHeader(Stream *s, 1284b9c1b51eSKate Stone const section_header_t &sh) { 1285adcd0268SBenjamin Kramer std::string name = std::string(GetSectionName(sh)); 1286b9c1b51eSKate 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 " 1287b9c1b51eSKate Stone "0x%4.4x 0x%8.8x\n", 1288b9c1b51eSKate Stone name.c_str(), sh.vmaddr, sh.vmsize, sh.offset, sh.size, sh.reloff, 1289b9c1b51eSKate Stone sh.lineoff, sh.nreloc, sh.nline, sh.flags); 1290f754f88fSGreg Clayton } 1291f754f88fSGreg Clayton 1292f754f88fSGreg Clayton // DumpSectionHeaders 1293f754f88fSGreg Clayton // 1294f754f88fSGreg Clayton // Dump all of the ELF section header to the specified output stream 1295b9c1b51eSKate Stone void ObjectFilePECOFF::DumpSectionHeaders(Stream *s) { 1296f754f88fSGreg Clayton 1297f754f88fSGreg Clayton s->PutCString("Section Headers\n"); 1298b9c1b51eSKate Stone s->PutCString("IDX name vm addr vm size file off file " 1299b9c1b51eSKate Stone "size reloc off line off nreloc nline flags\n"); 1300b9c1b51eSKate Stone s->PutCString("==== ---------------- ---------- ---------- ---------- " 1301b9c1b51eSKate Stone "---------- ---------- ---------- ------ ------ ----------\n"); 1302f754f88fSGreg Clayton 1303f754f88fSGreg Clayton uint32_t idx = 0; 1304f754f88fSGreg Clayton SectionHeaderCollIter pos, end = m_sect_headers.end(); 1305f754f88fSGreg Clayton 1306b9c1b51eSKate Stone for (pos = m_sect_headers.begin(); pos != end; ++pos, ++idx) { 1307f754f88fSGreg Clayton s->Printf("[%2u] ", idx); 1308f754f88fSGreg Clayton ObjectFilePECOFF::DumpSectionHeader(s, *pos); 1309f754f88fSGreg Clayton } 1310f754f88fSGreg Clayton } 1311f754f88fSGreg Clayton 1312037ed1beSAaron Smith // DumpDependentModules 1313037ed1beSAaron Smith // 1314037ed1beSAaron Smith // Dump all of the dependent modules to the specified output stream 1315037ed1beSAaron Smith void ObjectFilePECOFF::DumpDependentModules(lldb_private::Stream *s) { 1316037ed1beSAaron Smith auto num_modules = ParseDependentModules(); 1317037ed1beSAaron Smith if (num_modules > 0) { 1318037ed1beSAaron Smith s->PutCString("Dependent Modules\n"); 1319037ed1beSAaron Smith for (unsigned i = 0; i < num_modules; ++i) { 1320037ed1beSAaron Smith auto spec = m_deps_filespec->GetFileSpecAtIndex(i); 1321037ed1beSAaron Smith s->Printf(" %s\n", spec.GetFilename().GetCString()); 1322037ed1beSAaron Smith } 1323037ed1beSAaron Smith } 1324037ed1beSAaron Smith } 1325037ed1beSAaron Smith 1326fb3b3bd1SZachary Turner bool ObjectFilePECOFF::IsWindowsSubsystem() { 1327fb3b3bd1SZachary Turner switch (m_coff_header_opt.subsystem) { 1328fb3b3bd1SZachary Turner case llvm::COFF::IMAGE_SUBSYSTEM_NATIVE: 1329fb3b3bd1SZachary Turner case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_GUI: 1330fb3b3bd1SZachary Turner case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_CUI: 1331fb3b3bd1SZachary Turner case llvm::COFF::IMAGE_SUBSYSTEM_NATIVE_WINDOWS: 1332fb3b3bd1SZachary Turner case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_CE_GUI: 1333fb3b3bd1SZachary Turner case llvm::COFF::IMAGE_SUBSYSTEM_XBOX: 1334fb3b3bd1SZachary Turner case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_BOOT_APPLICATION: 1335fb3b3bd1SZachary Turner return true; 1336fb3b3bd1SZachary Turner default: 1337fb3b3bd1SZachary Turner return false; 1338fb3b3bd1SZachary Turner } 1339fb3b3bd1SZachary Turner } 1340fb3b3bd1SZachary Turner 1341f760f5aeSPavel Labath ArchSpec ObjectFilePECOFF::GetArchitecture() { 1342237ad974SCharles Davis uint16_t machine = m_coff_header.machine; 1343b9c1b51eSKate Stone switch (machine) { 1344f760f5aeSPavel Labath default: 1345f760f5aeSPavel Labath break; 1346237ad974SCharles Davis case llvm::COFF::IMAGE_FILE_MACHINE_AMD64: 1347237ad974SCharles Davis case llvm::COFF::IMAGE_FILE_MACHINE_I386: 1348237ad974SCharles Davis case llvm::COFF::IMAGE_FILE_MACHINE_POWERPC: 1349237ad974SCharles Davis case llvm::COFF::IMAGE_FILE_MACHINE_POWERPCFP: 1350237ad974SCharles Davis case llvm::COFF::IMAGE_FILE_MACHINE_ARM: 13511108cb36SSaleem Abdulrasool case llvm::COFF::IMAGE_FILE_MACHINE_ARMNT: 1352237ad974SCharles Davis case llvm::COFF::IMAGE_FILE_MACHINE_THUMB: 1353638f072fSMartin Storsjo case llvm::COFF::IMAGE_FILE_MACHINE_ARM64: 1354f760f5aeSPavel Labath ArchSpec arch; 1355fb3b3bd1SZachary Turner arch.SetArchitecture(eArchTypeCOFF, machine, LLDB_INVALID_CPUTYPE, 1356fb3b3bd1SZachary Turner IsWindowsSubsystem() ? llvm::Triple::Win32 1357fb3b3bd1SZachary Turner : llvm::Triple::UnknownOS); 1358f760f5aeSPavel Labath return arch; 1359237ad974SCharles Davis } 1360f760f5aeSPavel Labath return ArchSpec(); 1361f754f88fSGreg Clayton } 1362f754f88fSGreg Clayton 1363b9c1b51eSKate Stone ObjectFile::Type ObjectFilePECOFF::CalculateType() { 1364b9c1b51eSKate Stone if (m_coff_header.machine != 0) { 1365237ad974SCharles Davis if ((m_coff_header.flags & llvm::COFF::IMAGE_FILE_DLL) == 0) 1366f754f88fSGreg Clayton return eTypeExecutable; 1367f754f88fSGreg Clayton else 1368f754f88fSGreg Clayton return eTypeSharedLibrary; 1369f754f88fSGreg Clayton } 1370f754f88fSGreg Clayton return eTypeExecutable; 1371f754f88fSGreg Clayton } 1372f754f88fSGreg Clayton 1373b9c1b51eSKate Stone ObjectFile::Strata ObjectFilePECOFF::CalculateStrata() { return eStrataUser; } 1374