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*25c8a061SAlvin Wong #include "lldb/Interpreter/OptionValueProperties.h" 20f754f88fSGreg Clayton #include "lldb/Symbol/ObjectFile.h" 21f7d1893fSAdrian McCarthy #include "lldb/Target/Process.h" 222756adf3SVirgile Bello #include "lldb/Target/SectionLoadList.h" 232756adf3SVirgile Bello #include "lldb/Target/Target.h" 245f19b907SPavel Labath #include "lldb/Utility/ArchSpec.h" 25666cc0b2SZachary Turner #include "lldb/Utility/DataBufferHeap.h" 265713a05bSZachary Turner #include "lldb/Utility/FileSpec.h" 27c34698a8SPavel Labath #include "lldb/Utility/LLDBLog.h" 28037ed1beSAaron Smith #include "lldb/Utility/Log.h" 29bf9a7730SZachary Turner #include "lldb/Utility/StreamString.h" 3038d0632eSPavel Labath #include "lldb/Utility/Timer.h" 31666cc0b2SZachary Turner #include "lldb/Utility/UUID.h" 325f19b907SPavel Labath #include "llvm/BinaryFormat/COFF.h" 33f754f88fSGreg Clayton 34037ed1beSAaron Smith #include "llvm/Object/COFFImportFile.h" 35c8daf4a7SAlvin Wong #include "llvm/Support/CRC.h" 36037ed1beSAaron Smith #include "llvm/Support/Error.h" 37*25c8a061SAlvin Wong #include "llvm/Support/Host.h" 383f4a4b36SZachary Turner #include "llvm/Support/MemoryBuffer.h" 393f4a4b36SZachary Turner 40f754f88fSGreg Clayton #define IMAGE_DOS_SIGNATURE 0x5A4D // MZ 41f754f88fSGreg Clayton #define IMAGE_NT_SIGNATURE 0x00004550 // PE00 42f754f88fSGreg Clayton #define OPT_HEADER_MAGIC_PE32 0x010b 43f754f88fSGreg Clayton #define OPT_HEADER_MAGIC_PE32_PLUS 0x020b 44f754f88fSGreg Clayton 45f754f88fSGreg Clayton using namespace lldb; 46f754f88fSGreg Clayton using namespace lldb_private; 47f754f88fSGreg Clayton 48bba9ba8dSJonas Devlieghere LLDB_PLUGIN_DEFINE(ObjectFilePECOFF) 49fbb4d1e4SJonas Devlieghere 50*25c8a061SAlvin Wong namespace { 51*25c8a061SAlvin Wong 52*25c8a061SAlvin Wong static constexpr OptionEnumValueElement g_abi_enums[] = { 53*25c8a061SAlvin Wong { 54*25c8a061SAlvin Wong llvm::Triple::UnknownEnvironment, 55*25c8a061SAlvin Wong "default", 56*25c8a061SAlvin Wong "Use default target (if it is Windows) or MSVC", 57*25c8a061SAlvin Wong }, 58*25c8a061SAlvin Wong { 59*25c8a061SAlvin Wong llvm::Triple::MSVC, 60*25c8a061SAlvin Wong "msvc", 61*25c8a061SAlvin Wong "MSVC ABI", 62*25c8a061SAlvin Wong }, 63*25c8a061SAlvin Wong { 64*25c8a061SAlvin Wong llvm::Triple::GNU, 65*25c8a061SAlvin Wong "gnu", 66*25c8a061SAlvin Wong "MinGW / Itanium ABI", 67*25c8a061SAlvin Wong }, 68*25c8a061SAlvin Wong }; 69*25c8a061SAlvin Wong 70*25c8a061SAlvin Wong #define LLDB_PROPERTIES_objectfilepecoff 71*25c8a061SAlvin Wong #include "ObjectFilePECOFFProperties.inc" 72*25c8a061SAlvin Wong 73*25c8a061SAlvin Wong enum { 74*25c8a061SAlvin Wong #define LLDB_PROPERTIES_objectfilepecoff 75*25c8a061SAlvin Wong #include "ObjectFilePECOFFPropertiesEnum.inc" 76*25c8a061SAlvin Wong }; 77*25c8a061SAlvin Wong 78*25c8a061SAlvin Wong class PluginProperties : public Properties { 79*25c8a061SAlvin Wong public: 80*25c8a061SAlvin Wong static ConstString GetSettingName() { 81*25c8a061SAlvin Wong return ConstString(ObjectFilePECOFF::GetPluginNameStatic()); 82*25c8a061SAlvin Wong } 83*25c8a061SAlvin Wong 84*25c8a061SAlvin Wong PluginProperties() { 85*25c8a061SAlvin Wong m_collection_sp = std::make_shared<OptionValueProperties>(GetSettingName()); 86*25c8a061SAlvin Wong m_collection_sp->Initialize(g_objectfilepecoff_properties); 87*25c8a061SAlvin Wong } 88*25c8a061SAlvin Wong 89*25c8a061SAlvin Wong llvm::Triple::EnvironmentType ABI() const { 90*25c8a061SAlvin Wong return (llvm::Triple::EnvironmentType) 91*25c8a061SAlvin Wong m_collection_sp->GetPropertyAtIndexAsEnumeration( 92*25c8a061SAlvin Wong nullptr, ePropertyABI, llvm::Triple::UnknownEnvironment); 93*25c8a061SAlvin Wong } 94*25c8a061SAlvin Wong }; 95*25c8a061SAlvin Wong 96*25c8a061SAlvin Wong static PluginProperties &GetGlobalPluginProperties() { 97*25c8a061SAlvin Wong static PluginProperties g_settings; 98*25c8a061SAlvin Wong return g_settings; 99*25c8a061SAlvin Wong } 100*25c8a061SAlvin Wong 101*25c8a061SAlvin Wong } // namespace 102*25c8a061SAlvin Wong 103c8daf4a7SAlvin Wong static bool GetDebugLinkContents(const llvm::object::COFFObjectFile &coff_obj, 104c8daf4a7SAlvin Wong std::string &gnu_debuglink_file, 105c8daf4a7SAlvin Wong uint32_t &gnu_debuglink_crc) { 106c8daf4a7SAlvin Wong static ConstString g_sect_name_gnu_debuglink(".gnu_debuglink"); 107c8daf4a7SAlvin Wong for (const auto §ion : coff_obj.sections()) { 108c8daf4a7SAlvin Wong auto name = section.getName(); 109c8daf4a7SAlvin Wong if (!name) { 110c8daf4a7SAlvin Wong llvm::consumeError(name.takeError()); 111c8daf4a7SAlvin Wong continue; 112c8daf4a7SAlvin Wong } 113c8daf4a7SAlvin Wong if (*name == g_sect_name_gnu_debuglink.GetStringRef()) { 114c8daf4a7SAlvin Wong auto content = section.getContents(); 115c8daf4a7SAlvin Wong if (!content) { 116c8daf4a7SAlvin Wong llvm::consumeError(content.takeError()); 117c8daf4a7SAlvin Wong return false; 118c8daf4a7SAlvin Wong } 119c8daf4a7SAlvin Wong DataExtractor data( 120c8daf4a7SAlvin Wong content->data(), content->size(), 121c8daf4a7SAlvin Wong coff_obj.isLittleEndian() ? eByteOrderLittle : eByteOrderBig, 4); 122c8daf4a7SAlvin Wong lldb::offset_t gnu_debuglink_offset = 0; 123c8daf4a7SAlvin Wong gnu_debuglink_file = data.GetCStr(&gnu_debuglink_offset); 124c8daf4a7SAlvin Wong // Align to the next 4-byte offset 125c8daf4a7SAlvin Wong gnu_debuglink_offset = llvm::alignTo(gnu_debuglink_offset, 4); 126c8daf4a7SAlvin Wong data.GetU32(&gnu_debuglink_offset, &gnu_debuglink_crc, 1); 127c8daf4a7SAlvin Wong return true; 128c8daf4a7SAlvin Wong } 129c8daf4a7SAlvin Wong } 130c8daf4a7SAlvin Wong return false; 131c8daf4a7SAlvin Wong } 132c8daf4a7SAlvin Wong 133d372a8e8SPavel Labath static UUID GetCoffUUID(llvm::object::COFFObjectFile &coff_obj) { 134b8d03935SAaron Smith const llvm::codeview::DebugInfo *pdb_info = nullptr; 135b8d03935SAaron Smith llvm::StringRef pdb_file; 136b8d03935SAaron Smith 137c8daf4a7SAlvin Wong // First, prefer to use the PDB build id. LLD generates this even for mingw 138c8daf4a7SAlvin Wong // targets without PDB output, and it does not get stripped either. 139d372a8e8SPavel Labath if (!coff_obj.getDebugPDBInfo(pdb_info, pdb_file) && pdb_info) { 140b8d03935SAaron Smith if (pdb_info->PDB70.CVSignature == llvm::OMF::Signature::PDB70) { 1414348e0eeSZequan Wu UUID::CvRecordPdb70 info; 1424348e0eeSZequan Wu memcpy(&info.Uuid, pdb_info->PDB70.Signature, sizeof(info.Uuid)); 1434348e0eeSZequan Wu info.Age = pdb_info->PDB70.Age; 1444348e0eeSZequan Wu return UUID::fromCvRecord(info); 145b8d03935SAaron Smith } 146b8d03935SAaron Smith } 147b8d03935SAaron Smith 148c8daf4a7SAlvin Wong std::string gnu_debuglink_file; 149c8daf4a7SAlvin Wong uint32_t gnu_debuglink_crc; 150c8daf4a7SAlvin Wong 151c8daf4a7SAlvin Wong // The GNU linker normally does not write a PDB build id (unless requested 152c8daf4a7SAlvin Wong // with the --build-id option), so we should fall back to using the crc 153c8daf4a7SAlvin Wong // from .gnu_debuglink if it exists, just like how ObjectFileELF does it. 154c8daf4a7SAlvin Wong if (!GetDebugLinkContents(coff_obj, gnu_debuglink_file, gnu_debuglink_crc)) { 155c8daf4a7SAlvin Wong // If there is no .gnu_debuglink section, then this may be an object 156c8daf4a7SAlvin Wong // containing DWARF debug info for .gnu_debuglink, so calculate the crc of 157c8daf4a7SAlvin Wong // the object itself. 158c8daf4a7SAlvin Wong auto raw_data = coff_obj.getData(); 159c8daf4a7SAlvin Wong LLDB_SCOPED_TIMERF( 160c8daf4a7SAlvin Wong "Calculating module crc32 %s with size %" PRIu64 " KiB", 161c8daf4a7SAlvin Wong FileSpec(coff_obj.getFileName()).GetLastPathComponent().AsCString(), 162c8daf4a7SAlvin Wong static_cast<lldb::offset_t>(raw_data.size()) / 1024); 163c8daf4a7SAlvin Wong gnu_debuglink_crc = llvm::crc32(0, llvm::arrayRefFromStringRef(raw_data)); 164c8daf4a7SAlvin Wong } 165c8daf4a7SAlvin Wong // Use 4 bytes of crc from the .gnu_debuglink section. 166c8daf4a7SAlvin Wong llvm::support::ulittle32_t data(gnu_debuglink_crc); 167c8daf4a7SAlvin Wong return UUID::fromData(&data, sizeof(data)); 168b8d03935SAaron Smith } 169b8d03935SAaron Smith 170e84f7841SPavel Labath char ObjectFilePECOFF::ID; 171e84f7841SPavel Labath 172b9c1b51eSKate Stone void ObjectFilePECOFF::Initialize() { 173*25c8a061SAlvin Wong PluginManager::RegisterPlugin(GetPluginNameStatic(), 174*25c8a061SAlvin Wong GetPluginDescriptionStatic(), CreateInstance, 175*25c8a061SAlvin Wong CreateMemoryInstance, GetModuleSpecifications, 176*25c8a061SAlvin Wong SaveCore, DebuggerInitialize); 177*25c8a061SAlvin Wong } 178*25c8a061SAlvin Wong 179*25c8a061SAlvin Wong void ObjectFilePECOFF::DebuggerInitialize(Debugger &debugger) { 180*25c8a061SAlvin Wong if (!PluginManager::GetSettingForObjectFilePlugin( 181*25c8a061SAlvin Wong debugger, PluginProperties::GetSettingName())) { 182*25c8a061SAlvin Wong const bool is_global_setting = true; 183*25c8a061SAlvin Wong PluginManager::CreateSettingForObjectFilePlugin( 184*25c8a061SAlvin Wong debugger, GetGlobalPluginProperties().GetValueProperties(), 185*25c8a061SAlvin Wong ConstString("Properties for the PE/COFF object-file plug-in."), 186*25c8a061SAlvin Wong is_global_setting); 187*25c8a061SAlvin Wong } 188f754f88fSGreg Clayton } 189f754f88fSGreg Clayton 190b9c1b51eSKate Stone void ObjectFilePECOFF::Terminate() { 191f754f88fSGreg Clayton PluginManager::UnregisterPlugin(CreateInstance); 192f754f88fSGreg Clayton } 193f754f88fSGreg Clayton 1942ace1e57SPavel Labath llvm::StringRef ObjectFilePECOFF::GetPluginDescriptionStatic() { 195b9c1b51eSKate Stone return "Portable Executable and Common Object File Format object file reader " 196b9c1b51eSKate Stone "(32 and 64 bit)"; 197f754f88fSGreg Clayton } 198f754f88fSGreg Clayton 199c69307e5SJonas Devlieghere ObjectFile *ObjectFilePECOFF::CreateInstance( 200c69307e5SJonas Devlieghere const lldb::ModuleSP &module_sp, DataBufferSP data_sp, 201c69307e5SJonas Devlieghere lldb::offset_t data_offset, const lldb_private::FileSpec *file_p, 202c69307e5SJonas Devlieghere lldb::offset_t file_offset, lldb::offset_t length) { 20328e4942bSPavel Labath FileSpec file = file_p ? *file_p : FileSpec(); 204b9c1b51eSKate Stone if (!data_sp) { 20550251fc7SPavel Labath data_sp = MapFileData(file, length, file_offset); 2063f4a4b36SZachary Turner if (!data_sp) 2073f4a4b36SZachary Turner return nullptr; 2085ce9c565SGreg Clayton data_offset = 0; 2095ce9c565SGreg Clayton } 2105ce9c565SGreg Clayton 2113f4a4b36SZachary Turner if (!ObjectFilePECOFF::MagicBytesMatch(data_sp)) 2123f4a4b36SZachary Turner return nullptr; 2133f4a4b36SZachary Turner 2145ce9c565SGreg Clayton // Update the data to contain the entire file if it doesn't already 2153f4a4b36SZachary Turner if (data_sp->GetByteSize() < length) { 21650251fc7SPavel Labath data_sp = MapFileData(file, length, file_offset); 2173f4a4b36SZachary Turner if (!data_sp) 2183f4a4b36SZachary Turner return nullptr; 219f754f88fSGreg Clayton } 2203f4a4b36SZachary Turner 221a8f3ae7cSJonas Devlieghere auto objfile_up = std::make_unique<ObjectFilePECOFF>( 22228e4942bSPavel Labath module_sp, data_sp, data_offset, file_p, file_offset, length); 223d5b44036SJonas Devlieghere if (!objfile_up || !objfile_up->ParseHeader()) 2243f4a4b36SZachary Turner return nullptr; 2253f4a4b36SZachary Turner 226037ed1beSAaron Smith // Cache coff binary. 227d5b44036SJonas Devlieghere if (!objfile_up->CreateBinary()) 228037ed1beSAaron Smith return nullptr; 229d5b44036SJonas Devlieghere return objfile_up.release(); 230f754f88fSGreg Clayton } 231f754f88fSGreg Clayton 232b9c1b51eSKate Stone ObjectFile *ObjectFilePECOFF::CreateMemoryInstance( 233f2ea125eSJonas Devlieghere const lldb::ModuleSP &module_sp, lldb::WritableDataBufferSP data_sp, 234b9c1b51eSKate Stone const lldb::ProcessSP &process_sp, lldb::addr_t header_addr) { 235344546bdSWalter Erquinigo if (!data_sp || !ObjectFilePECOFF::MagicBytesMatch(data_sp)) 236344546bdSWalter Erquinigo return nullptr; 237a8f3ae7cSJonas Devlieghere auto objfile_up = std::make_unique<ObjectFilePECOFF>( 238344546bdSWalter Erquinigo module_sp, data_sp, process_sp, header_addr); 239d5b44036SJonas Devlieghere if (objfile_up.get() && objfile_up->ParseHeader()) { 240d5b44036SJonas Devlieghere return objfile_up.release(); 241344546bdSWalter Erquinigo } 242344546bdSWalter Erquinigo return nullptr; 243c9660546SGreg Clayton } 244c9660546SGreg Clayton 245b9c1b51eSKate Stone size_t ObjectFilePECOFF::GetModuleSpecifications( 246b9c1b51eSKate Stone const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp, 247b9c1b51eSKate Stone lldb::offset_t data_offset, lldb::offset_t file_offset, 248b9c1b51eSKate Stone lldb::offset_t length, lldb_private::ModuleSpecList &specs) { 24989eb1baeSVirgile Bello const size_t initial_count = specs.GetSize(); 250b8d03935SAaron Smith if (!data_sp || !ObjectFilePECOFF::MagicBytesMatch(data_sp)) 251b8d03935SAaron Smith return initial_count; 25289eb1baeSVirgile Bello 253a007a6d8SPavel Labath Log *log = GetLog(LLDBLog::Object); 2543db1d138SMartin Storsjö 255a4a00cedSFred Riss if (data_sp->GetByteSize() < length) 256d372a8e8SPavel Labath if (DataBufferSP full_sp = MapFileData(file, -1, file_offset)) 257d372a8e8SPavel Labath data_sp = std::move(full_sp); 258d372a8e8SPavel Labath auto binary = llvm::object::createBinary(llvm::MemoryBufferRef( 259d372a8e8SPavel Labath toStringRef(data_sp->GetData()), file.GetFilename().GetStringRef())); 2603db1d138SMartin Storsjö 2613db1d138SMartin Storsjö if (!binary) { 2623db1d138SMartin Storsjö LLDB_LOG_ERROR(log, binary.takeError(), 2633db1d138SMartin Storsjö "Failed to create binary for file ({1}): {0}", file); 264b8d03935SAaron Smith return initial_count; 2653db1d138SMartin Storsjö } 26689eb1baeSVirgile Bello 267d372a8e8SPavel Labath auto *COFFObj = llvm::dyn_cast<llvm::object::COFFObjectFile>(binary->get()); 268d372a8e8SPavel Labath if (!COFFObj) 269b8d03935SAaron Smith return initial_count; 27089eb1baeSVirgile Bello 271b8d03935SAaron Smith ModuleSpec module_spec(file); 272b8d03935SAaron Smith ArchSpec &spec = module_spec.GetArchitecture(); 273b8d03935SAaron Smith lldb_private::UUID &uuid = module_spec.GetUUID(); 274b8d03935SAaron Smith if (!uuid.IsValid()) 275d372a8e8SPavel Labath uuid = GetCoffUUID(*COFFObj); 276b8d03935SAaron Smith 277*25c8a061SAlvin Wong static llvm::Triple::EnvironmentType default_env = [] { 278*25c8a061SAlvin Wong auto def_target = llvm::Triple( 279*25c8a061SAlvin Wong llvm::Triple::normalize(llvm::sys::getDefaultTargetTriple())); 280*25c8a061SAlvin Wong if (def_target.getOS() == llvm::Triple::Win32 && 281*25c8a061SAlvin Wong def_target.getEnvironment() != llvm::Triple::UnknownEnvironment) 282*25c8a061SAlvin Wong return def_target.getEnvironment(); 283*25c8a061SAlvin Wong return llvm::Triple::MSVC; 284*25c8a061SAlvin Wong }(); 285*25c8a061SAlvin Wong 286*25c8a061SAlvin Wong llvm::Triple::EnvironmentType env = GetGlobalPluginProperties().ABI(); 287*25c8a061SAlvin Wong if (env == llvm::Triple::UnknownEnvironment) 288*25c8a061SAlvin Wong env = default_env; 289*25c8a061SAlvin Wong 290b8d03935SAaron Smith switch (COFFObj->getMachine()) { 291b8d03935SAaron Smith case MachineAmd64: 292ad587ae4SZachary Turner spec.SetTriple("x86_64-pc-windows"); 293*25c8a061SAlvin Wong spec.GetTriple().setEnvironment(env); 294b8d03935SAaron Smith specs.Append(module_spec); 295b8d03935SAaron Smith break; 296b8d03935SAaron Smith case MachineX86: 297ad587ae4SZachary Turner spec.SetTriple("i386-pc-windows"); 298*25c8a061SAlvin Wong spec.GetTriple().setEnvironment(env); 299b8d03935SAaron Smith specs.Append(module_spec); 3005e6f4520SZachary Turner spec.SetTriple("i686-pc-windows"); 301*25c8a061SAlvin Wong spec.GetTriple().setEnvironment(env); 302b8d03935SAaron Smith specs.Append(module_spec); 303b8d03935SAaron Smith break; 304b8d03935SAaron Smith case MachineArmNt: 305544c8f48SMartin Storsjo spec.SetTriple("armv7-pc-windows"); 306*25c8a061SAlvin Wong spec.GetTriple().setEnvironment(env); 307b8d03935SAaron Smith specs.Append(module_spec); 308b8d03935SAaron Smith break; 309638f072fSMartin Storsjo case MachineArm64: 310674d5543SMartin Storsjo spec.SetTriple("aarch64-pc-windows"); 311*25c8a061SAlvin Wong spec.GetTriple().setEnvironment(env); 312638f072fSMartin Storsjo specs.Append(module_spec); 313638f072fSMartin Storsjo break; 314b8d03935SAaron Smith default: 315b8d03935SAaron Smith break; 31689eb1baeSVirgile Bello } 31789eb1baeSVirgile Bello 31889eb1baeSVirgile Bello return specs.GetSize() - initial_count; 319f4d6de6aSGreg Clayton } 320f4d6de6aSGreg Clayton 321b9c1b51eSKate Stone bool ObjectFilePECOFF::SaveCore(const lldb::ProcessSP &process_sp, 322f7d1893fSAdrian McCarthy const lldb_private::FileSpec &outfile, 3239ea6dd5cSJason Molenda lldb::SaveCoreStyle &core_style, 32497206d57SZachary Turner lldb_private::Status &error) { 3259ea6dd5cSJason Molenda core_style = eSaveCoreFull; 326f7d1893fSAdrian McCarthy return SaveMiniDump(process_sp, outfile, error); 327f7d1893fSAdrian McCarthy } 328f7d1893fSAdrian McCarthy 329f2ea125eSJonas Devlieghere bool ObjectFilePECOFF::MagicBytesMatch(DataBufferSP data_sp) { 3305ce9c565SGreg Clayton DataExtractor data(data_sp, eByteOrderLittle, 4); 331c7bece56SGreg Clayton lldb::offset_t offset = 0; 332f754f88fSGreg Clayton uint16_t magic = data.GetU16(&offset); 333f754f88fSGreg Clayton return magic == IMAGE_DOS_SIGNATURE; 334f754f88fSGreg Clayton } 335f754f88fSGreg Clayton 336b9c1b51eSKate Stone lldb::SymbolType ObjectFilePECOFF::MapSymbolType(uint16_t coff_symbol_type) { 337c35b91ceSAdrian McCarthy // TODO: We need to complete this mapping of COFF symbol types to LLDB ones. 338c35b91ceSAdrian McCarthy // For now, here's a hack to make sure our function have types. 339b9c1b51eSKate Stone const auto complex_type = 340b9c1b51eSKate Stone coff_symbol_type >> llvm::COFF::SCT_COMPLEX_TYPE_SHIFT; 341b9c1b51eSKate Stone if (complex_type == llvm::COFF::IMAGE_SYM_DTYPE_FUNCTION) { 342c35b91ceSAdrian McCarthy return lldb::eSymbolTypeCode; 343c35b91ceSAdrian McCarthy } 344c35b91ceSAdrian McCarthy return lldb::eSymbolTypeInvalid; 345c35b91ceSAdrian McCarthy } 346f754f88fSGreg Clayton 347037ed1beSAaron Smith bool ObjectFilePECOFF::CreateBinary() { 348d372a8e8SPavel Labath if (m_binary) 349037ed1beSAaron Smith return true; 350037ed1beSAaron Smith 351a007a6d8SPavel Labath Log *log = GetLog(LLDBLog::Object); 352037ed1beSAaron Smith 353d372a8e8SPavel Labath auto binary = llvm::object::createBinary(llvm::MemoryBufferRef( 354d372a8e8SPavel Labath toStringRef(m_data.GetData()), m_file.GetFilename().GetStringRef())); 355037ed1beSAaron Smith if (!binary) { 3563db1d138SMartin Storsjö LLDB_LOG_ERROR(log, binary.takeError(), 3573db1d138SMartin Storsjö "Failed to create binary for file ({1}): {0}", m_file); 358037ed1beSAaron Smith return false; 359037ed1beSAaron Smith } 360037ed1beSAaron Smith 361037ed1beSAaron Smith // Make sure we only handle COFF format. 362d372a8e8SPavel Labath m_binary = 363d372a8e8SPavel Labath llvm::unique_dyn_cast<llvm::object::COFFObjectFile>(std::move(*binary)); 364d372a8e8SPavel Labath if (!m_binary) 365037ed1beSAaron Smith return false; 366037ed1beSAaron Smith 367d372a8e8SPavel Labath LLDB_LOG(log, "this = {0}, module = {1} ({2}), file = {3}, binary = {4}", 368d372a8e8SPavel Labath this, GetModule().get(), GetModule()->GetSpecificationDescription(), 369d372a8e8SPavel Labath m_file.GetPath(), m_binary.get()); 370037ed1beSAaron Smith return true; 371037ed1beSAaron Smith } 372037ed1beSAaron Smith 373e72dfb32SGreg Clayton ObjectFilePECOFF::ObjectFilePECOFF(const lldb::ModuleSP &module_sp, 374f2ea125eSJonas Devlieghere DataBufferSP data_sp, 3755ce9c565SGreg Clayton lldb::offset_t data_offset, 376f754f88fSGreg Clayton const FileSpec *file, 3775ce9c565SGreg Clayton lldb::offset_t file_offset, 378b9c1b51eSKate Stone lldb::offset_t length) 379b9c1b51eSKate Stone : ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset), 3805c43ffd6SPavel Labath m_dos_header(), m_coff_header(), m_sect_headers(), 381d372a8e8SPavel Labath m_entry_point_address(), m_deps_filespec() { 382f754f88fSGreg Clayton ::memset(&m_dos_header, 0, sizeof(m_dos_header)); 383f754f88fSGreg Clayton ::memset(&m_coff_header, 0, sizeof(m_coff_header)); 384f754f88fSGreg Clayton } 385f754f88fSGreg Clayton 386344546bdSWalter Erquinigo ObjectFilePECOFF::ObjectFilePECOFF(const lldb::ModuleSP &module_sp, 387f2ea125eSJonas Devlieghere WritableDataBufferSP header_data_sp, 388344546bdSWalter Erquinigo const lldb::ProcessSP &process_sp, 389344546bdSWalter Erquinigo addr_t header_addr) 390344546bdSWalter Erquinigo : ObjectFile(module_sp, process_sp, header_addr, header_data_sp), 3915c43ffd6SPavel Labath m_dos_header(), m_coff_header(), m_sect_headers(), 392d372a8e8SPavel Labath m_entry_point_address(), m_deps_filespec() { 393344546bdSWalter Erquinigo ::memset(&m_dos_header, 0, sizeof(m_dos_header)); 394344546bdSWalter Erquinigo ::memset(&m_coff_header, 0, sizeof(m_coff_header)); 395344546bdSWalter Erquinigo } 396344546bdSWalter Erquinigo 397fd2433e1SJonas Devlieghere ObjectFilePECOFF::~ObjectFilePECOFF() = default; 398f754f88fSGreg Clayton 399b9c1b51eSKate Stone bool ObjectFilePECOFF::ParseHeader() { 400a1743499SGreg Clayton ModuleSP module_sp(GetModule()); 401b9c1b51eSKate Stone if (module_sp) { 40216ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 403f754f88fSGreg Clayton m_sect_headers.clear(); 404f754f88fSGreg Clayton m_data.SetByteOrder(eByteOrderLittle); 405c7bece56SGreg Clayton lldb::offset_t offset = 0; 406f754f88fSGreg Clayton 407b9c1b51eSKate Stone if (ParseDOSHeader(m_data, m_dos_header)) { 408f754f88fSGreg Clayton offset = m_dos_header.e_lfanew; 409f754f88fSGreg Clayton uint32_t pe_signature = m_data.GetU32(&offset); 410f754f88fSGreg Clayton if (pe_signature != IMAGE_NT_SIGNATURE) 411f754f88fSGreg Clayton return false; 412b9c1b51eSKate Stone if (ParseCOFFHeader(m_data, &offset, m_coff_header)) { 413f754f88fSGreg Clayton if (m_coff_header.hdrsize > 0) 414f754f88fSGreg Clayton ParseCOFFOptionalHeader(&offset); 415f754f88fSGreg Clayton ParseSectionHeaders(offset); 41628469ca3SGreg Clayton } 417a0f72441SMartin Storsjö m_data.SetAddressByteSize(GetAddressByteSize()); 418f754f88fSGreg Clayton return true; 419f754f88fSGreg Clayton } 420a1743499SGreg Clayton } 421f754f88fSGreg Clayton return false; 422f754f88fSGreg Clayton } 423f754f88fSGreg Clayton 424b9c1b51eSKate Stone bool ObjectFilePECOFF::SetLoadAddress(Target &target, addr_t value, 425b9c1b51eSKate Stone bool value_is_offset) { 4262756adf3SVirgile Bello bool changed = false; 4272756adf3SVirgile Bello ModuleSP module_sp = GetModule(); 428b9c1b51eSKate Stone if (module_sp) { 4292756adf3SVirgile Bello size_t num_loaded_sections = 0; 4302756adf3SVirgile Bello SectionList *section_list = GetSectionList(); 431b9c1b51eSKate Stone if (section_list) { 432b9c1b51eSKate Stone if (!value_is_offset) { 4332756adf3SVirgile Bello value -= m_image_base; 4342756adf3SVirgile Bello } 4352756adf3SVirgile Bello 4362756adf3SVirgile Bello const size_t num_sections = section_list->GetSize(); 4372756adf3SVirgile Bello size_t sect_idx = 0; 4382756adf3SVirgile Bello 439b9c1b51eSKate Stone for (sect_idx = 0; sect_idx < num_sections; ++sect_idx) { 44005097246SAdrian Prantl // Iterate through the object file sections to find all of the sections 44105097246SAdrian Prantl // that have SHF_ALLOC in their flag bits. 4422756adf3SVirgile Bello SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx)); 443b9c1b51eSKate Stone if (section_sp && !section_sp->IsThreadSpecific()) { 444b9c1b51eSKate Stone if (target.GetSectionLoadList().SetSectionLoadAddress( 445b9c1b51eSKate Stone section_sp, section_sp->GetFileAddress() + value)) 4462756adf3SVirgile Bello ++num_loaded_sections; 4472756adf3SVirgile Bello } 4482756adf3SVirgile Bello } 4492756adf3SVirgile Bello changed = num_loaded_sections > 0; 4502756adf3SVirgile Bello } 4512756adf3SVirgile Bello } 4522756adf3SVirgile Bello return changed; 4532756adf3SVirgile Bello } 4542756adf3SVirgile Bello 455b9c1b51eSKate Stone ByteOrder ObjectFilePECOFF::GetByteOrder() const { return eByteOrderLittle; } 456f754f88fSGreg Clayton 457b9c1b51eSKate Stone bool ObjectFilePECOFF::IsExecutable() const { 458237ad974SCharles Davis return (m_coff_header.flags & llvm::COFF::IMAGE_FILE_DLL) == 0; 459f754f88fSGreg Clayton } 460f754f88fSGreg Clayton 461b9c1b51eSKate Stone uint32_t ObjectFilePECOFF::GetAddressByteSize() const { 462f754f88fSGreg Clayton if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32_PLUS) 463f754f88fSGreg Clayton return 8; 464f754f88fSGreg Clayton else if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32) 465f754f88fSGreg Clayton return 4; 466f754f88fSGreg Clayton return 4; 467f754f88fSGreg Clayton } 468f754f88fSGreg Clayton 469f754f88fSGreg Clayton // NeedsEndianSwap 470f754f88fSGreg Clayton // 47105097246SAdrian Prantl // Return true if an endian swap needs to occur when extracting data from this 47205097246SAdrian Prantl // file. 473b9c1b51eSKate Stone bool ObjectFilePECOFF::NeedsEndianSwap() const { 474f754f88fSGreg Clayton #if defined(__LITTLE_ENDIAN__) 475f754f88fSGreg Clayton return false; 476f754f88fSGreg Clayton #else 477f754f88fSGreg Clayton return true; 478f754f88fSGreg Clayton #endif 479f754f88fSGreg Clayton } 480f754f88fSGreg Clayton // ParseDOSHeader 481b9c1b51eSKate Stone bool ObjectFilePECOFF::ParseDOSHeader(DataExtractor &data, 482b9c1b51eSKate Stone dos_header_t &dos_header) { 483f754f88fSGreg Clayton bool success = false; 484c7bece56SGreg Clayton lldb::offset_t offset = 0; 48589eb1baeSVirgile Bello success = data.ValidOffsetForDataOfSize(0, sizeof(dos_header)); 486f754f88fSGreg Clayton 487b9c1b51eSKate Stone if (success) { 48889eb1baeSVirgile Bello dos_header.e_magic = data.GetU16(&offset); // Magic number 48989eb1baeSVirgile Bello success = dos_header.e_magic == IMAGE_DOS_SIGNATURE; 490f754f88fSGreg Clayton 491b9c1b51eSKate Stone if (success) { 49289eb1baeSVirgile Bello dos_header.e_cblp = data.GetU16(&offset); // Bytes on last page of file 49389eb1baeSVirgile Bello dos_header.e_cp = data.GetU16(&offset); // Pages in file 49489eb1baeSVirgile Bello dos_header.e_crlc = data.GetU16(&offset); // Relocations 495b9c1b51eSKate Stone dos_header.e_cparhdr = 496b9c1b51eSKate Stone data.GetU16(&offset); // Size of header in paragraphs 497b9c1b51eSKate Stone dos_header.e_minalloc = 498b9c1b51eSKate Stone data.GetU16(&offset); // Minimum extra paragraphs needed 499b9c1b51eSKate Stone dos_header.e_maxalloc = 500b9c1b51eSKate Stone data.GetU16(&offset); // Maximum extra paragraphs needed 50189eb1baeSVirgile Bello dos_header.e_ss = data.GetU16(&offset); // Initial (relative) SS value 50289eb1baeSVirgile Bello dos_header.e_sp = data.GetU16(&offset); // Initial SP value 50389eb1baeSVirgile Bello dos_header.e_csum = data.GetU16(&offset); // Checksum 50489eb1baeSVirgile Bello dos_header.e_ip = data.GetU16(&offset); // Initial IP value 50589eb1baeSVirgile Bello dos_header.e_cs = data.GetU16(&offset); // Initial (relative) CS value 506b9c1b51eSKate Stone dos_header.e_lfarlc = 507b9c1b51eSKate Stone data.GetU16(&offset); // File address of relocation table 50889eb1baeSVirgile Bello dos_header.e_ovno = data.GetU16(&offset); // Overlay number 509f754f88fSGreg Clayton 51089eb1baeSVirgile Bello dos_header.e_res[0] = data.GetU16(&offset); // Reserved words 51189eb1baeSVirgile Bello dos_header.e_res[1] = data.GetU16(&offset); // Reserved words 51289eb1baeSVirgile Bello dos_header.e_res[2] = data.GetU16(&offset); // Reserved words 51389eb1baeSVirgile Bello dos_header.e_res[3] = data.GetU16(&offset); // Reserved words 514f754f88fSGreg Clayton 515b9c1b51eSKate Stone dos_header.e_oemid = 516b9c1b51eSKate Stone data.GetU16(&offset); // OEM identifier (for e_oeminfo) 517b9c1b51eSKate Stone dos_header.e_oeminfo = 518b9c1b51eSKate Stone data.GetU16(&offset); // OEM information; e_oemid specific 51989eb1baeSVirgile Bello dos_header.e_res2[0] = data.GetU16(&offset); // Reserved words 52089eb1baeSVirgile Bello dos_header.e_res2[1] = data.GetU16(&offset); // Reserved words 52189eb1baeSVirgile Bello dos_header.e_res2[2] = data.GetU16(&offset); // Reserved words 52289eb1baeSVirgile Bello dos_header.e_res2[3] = data.GetU16(&offset); // Reserved words 52389eb1baeSVirgile Bello dos_header.e_res2[4] = data.GetU16(&offset); // Reserved words 52489eb1baeSVirgile Bello dos_header.e_res2[5] = data.GetU16(&offset); // Reserved words 52589eb1baeSVirgile Bello dos_header.e_res2[6] = data.GetU16(&offset); // Reserved words 52689eb1baeSVirgile Bello dos_header.e_res2[7] = data.GetU16(&offset); // Reserved words 52789eb1baeSVirgile Bello dos_header.e_res2[8] = data.GetU16(&offset); // Reserved words 52889eb1baeSVirgile Bello dos_header.e_res2[9] = data.GetU16(&offset); // Reserved words 529f754f88fSGreg Clayton 530b9c1b51eSKate Stone dos_header.e_lfanew = 531b9c1b51eSKate Stone data.GetU32(&offset); // File address of new exe header 532f754f88fSGreg Clayton } 533f754f88fSGreg Clayton } 534f754f88fSGreg Clayton if (!success) 53589eb1baeSVirgile Bello memset(&dos_header, 0, sizeof(dos_header)); 536f754f88fSGreg Clayton return success; 537f754f88fSGreg Clayton } 538f754f88fSGreg Clayton 539f754f88fSGreg Clayton // ParserCOFFHeader 540b9c1b51eSKate Stone bool ObjectFilePECOFF::ParseCOFFHeader(DataExtractor &data, 541b9c1b51eSKate Stone lldb::offset_t *offset_ptr, 542b9c1b51eSKate Stone coff_header_t &coff_header) { 543b9c1b51eSKate Stone bool success = 544b9c1b51eSKate Stone data.ValidOffsetForDataOfSize(*offset_ptr, sizeof(coff_header)); 545b9c1b51eSKate Stone if (success) { 54689eb1baeSVirgile Bello coff_header.machine = data.GetU16(offset_ptr); 54789eb1baeSVirgile Bello coff_header.nsects = data.GetU16(offset_ptr); 54889eb1baeSVirgile Bello coff_header.modtime = data.GetU32(offset_ptr); 54989eb1baeSVirgile Bello coff_header.symoff = data.GetU32(offset_ptr); 55089eb1baeSVirgile Bello coff_header.nsyms = data.GetU32(offset_ptr); 55189eb1baeSVirgile Bello coff_header.hdrsize = data.GetU16(offset_ptr); 55289eb1baeSVirgile Bello coff_header.flags = data.GetU16(offset_ptr); 553f754f88fSGreg Clayton } 554f754f88fSGreg Clayton if (!success) 55589eb1baeSVirgile Bello memset(&coff_header, 0, sizeof(coff_header)); 556f754f88fSGreg Clayton return success; 557f754f88fSGreg Clayton } 558f754f88fSGreg Clayton 559b9c1b51eSKate Stone bool ObjectFilePECOFF::ParseCOFFOptionalHeader(lldb::offset_t *offset_ptr) { 560f754f88fSGreg Clayton bool success = false; 561c7bece56SGreg Clayton const lldb::offset_t end_offset = *offset_ptr + m_coff_header.hdrsize; 562b9c1b51eSKate Stone if (*offset_ptr < end_offset) { 563f754f88fSGreg Clayton success = true; 564f754f88fSGreg Clayton m_coff_header_opt.magic = m_data.GetU16(offset_ptr); 565f754f88fSGreg Clayton m_coff_header_opt.major_linker_version = m_data.GetU8(offset_ptr); 566f754f88fSGreg Clayton m_coff_header_opt.minor_linker_version = m_data.GetU8(offset_ptr); 567f754f88fSGreg Clayton m_coff_header_opt.code_size = m_data.GetU32(offset_ptr); 568f754f88fSGreg Clayton m_coff_header_opt.data_size = m_data.GetU32(offset_ptr); 569f754f88fSGreg Clayton m_coff_header_opt.bss_size = m_data.GetU32(offset_ptr); 570f754f88fSGreg Clayton m_coff_header_opt.entry = m_data.GetU32(offset_ptr); 571f754f88fSGreg Clayton m_coff_header_opt.code_offset = m_data.GetU32(offset_ptr); 572f754f88fSGreg Clayton 573f754f88fSGreg Clayton const uint32_t addr_byte_size = GetAddressByteSize(); 574f754f88fSGreg Clayton 575b9c1b51eSKate Stone if (*offset_ptr < end_offset) { 576b9c1b51eSKate Stone if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32) { 577f754f88fSGreg Clayton // PE32 only 578f754f88fSGreg Clayton m_coff_header_opt.data_offset = m_data.GetU32(offset_ptr); 579b9c1b51eSKate Stone } else 580f754f88fSGreg Clayton m_coff_header_opt.data_offset = 0; 581f754f88fSGreg Clayton 582b9c1b51eSKate Stone if (*offset_ptr < end_offset) { 583b9c1b51eSKate Stone m_coff_header_opt.image_base = 584b9c1b51eSKate Stone m_data.GetMaxU64(offset_ptr, addr_byte_size); 585f754f88fSGreg Clayton m_coff_header_opt.sect_alignment = m_data.GetU32(offset_ptr); 586f754f88fSGreg Clayton m_coff_header_opt.file_alignment = m_data.GetU32(offset_ptr); 587f754f88fSGreg Clayton m_coff_header_opt.major_os_system_version = m_data.GetU16(offset_ptr); 588f754f88fSGreg Clayton m_coff_header_opt.minor_os_system_version = m_data.GetU16(offset_ptr); 589f754f88fSGreg Clayton m_coff_header_opt.major_image_version = m_data.GetU16(offset_ptr); 590f754f88fSGreg Clayton m_coff_header_opt.minor_image_version = m_data.GetU16(offset_ptr); 591f754f88fSGreg Clayton m_coff_header_opt.major_subsystem_version = m_data.GetU16(offset_ptr); 592f754f88fSGreg Clayton m_coff_header_opt.minor_subsystem_version = m_data.GetU16(offset_ptr); 593f754f88fSGreg Clayton m_coff_header_opt.reserved1 = m_data.GetU32(offset_ptr); 594f754f88fSGreg Clayton m_coff_header_opt.image_size = m_data.GetU32(offset_ptr); 595f754f88fSGreg Clayton m_coff_header_opt.header_size = m_data.GetU32(offset_ptr); 59628469ca3SGreg Clayton m_coff_header_opt.checksum = m_data.GetU32(offset_ptr); 597f754f88fSGreg Clayton m_coff_header_opt.subsystem = m_data.GetU16(offset_ptr); 598f754f88fSGreg Clayton m_coff_header_opt.dll_flags = m_data.GetU16(offset_ptr); 599b9c1b51eSKate Stone m_coff_header_opt.stack_reserve_size = 600b9c1b51eSKate Stone m_data.GetMaxU64(offset_ptr, addr_byte_size); 601b9c1b51eSKate Stone m_coff_header_opt.stack_commit_size = 602b9c1b51eSKate Stone m_data.GetMaxU64(offset_ptr, addr_byte_size); 603b9c1b51eSKate Stone m_coff_header_opt.heap_reserve_size = 604b9c1b51eSKate Stone m_data.GetMaxU64(offset_ptr, addr_byte_size); 605b9c1b51eSKate Stone m_coff_header_opt.heap_commit_size = 606b9c1b51eSKate Stone m_data.GetMaxU64(offset_ptr, addr_byte_size); 607f754f88fSGreg Clayton m_coff_header_opt.loader_flags = m_data.GetU32(offset_ptr); 608f754f88fSGreg Clayton uint32_t num_data_dir_entries = m_data.GetU32(offset_ptr); 609f754f88fSGreg Clayton m_coff_header_opt.data_dirs.clear(); 610f754f88fSGreg Clayton m_coff_header_opt.data_dirs.resize(num_data_dir_entries); 611f754f88fSGreg Clayton uint32_t i; 612b9c1b51eSKate Stone for (i = 0; i < num_data_dir_entries; i++) { 613f754f88fSGreg Clayton m_coff_header_opt.data_dirs[i].vmaddr = m_data.GetU32(offset_ptr); 614f754f88fSGreg Clayton m_coff_header_opt.data_dirs[i].vmsize = m_data.GetU32(offset_ptr); 615f754f88fSGreg Clayton } 6162756adf3SVirgile Bello 6172756adf3SVirgile Bello m_image_base = m_coff_header_opt.image_base; 618f754f88fSGreg Clayton } 619f754f88fSGreg Clayton } 620f754f88fSGreg Clayton } 621f754f88fSGreg Clayton // Make sure we are on track for section data which follows 622f754f88fSGreg Clayton *offset_ptr = end_offset; 623f754f88fSGreg Clayton return success; 624f754f88fSGreg Clayton } 625f754f88fSGreg Clayton 62630c2441aSAleksandr Urakov uint32_t ObjectFilePECOFF::GetRVA(const Address &addr) const { 62730c2441aSAleksandr Urakov return addr.GetFileAddress() - m_image_base; 62830c2441aSAleksandr Urakov } 62930c2441aSAleksandr Urakov 63030c2441aSAleksandr Urakov Address ObjectFilePECOFF::GetAddress(uint32_t rva) { 63130c2441aSAleksandr Urakov SectionList *sect_list = GetSectionList(); 63230c2441aSAleksandr Urakov if (!sect_list) 63330c2441aSAleksandr Urakov return Address(GetFileAddress(rva)); 63430c2441aSAleksandr Urakov 63530c2441aSAleksandr Urakov return Address(GetFileAddress(rva), sect_list); 63630c2441aSAleksandr Urakov } 63730c2441aSAleksandr Urakov 63830c2441aSAleksandr Urakov lldb::addr_t ObjectFilePECOFF::GetFileAddress(uint32_t rva) const { 63930c2441aSAleksandr Urakov return m_image_base + rva; 64030c2441aSAleksandr Urakov } 64130c2441aSAleksandr Urakov 642344546bdSWalter Erquinigo DataExtractor ObjectFilePECOFF::ReadImageData(uint32_t offset, size_t size) { 64330c2441aSAleksandr Urakov if (!size) 64430c2441aSAleksandr Urakov return {}; 64530c2441aSAleksandr Urakov 646a4a00cedSFred Riss if (m_data.ValidOffsetForDataOfSize(offset, size)) 647a4a00cedSFred Riss return DataExtractor(m_data, offset, size); 648a4a00cedSFred Riss 649344546bdSWalter Erquinigo ProcessSP process_sp(m_process_wp.lock()); 650344546bdSWalter Erquinigo DataExtractor data; 651344546bdSWalter Erquinigo if (process_sp) { 652a8f3ae7cSJonas Devlieghere auto data_up = std::make_unique<DataBufferHeap>(size, 0); 65397206d57SZachary Turner Status readmem_error; 654344546bdSWalter Erquinigo size_t bytes_read = 655d5b44036SJonas Devlieghere process_sp->ReadMemory(m_image_base + offset, data_up->GetBytes(), 656d5b44036SJonas Devlieghere data_up->GetByteSize(), readmem_error); 657344546bdSWalter Erquinigo if (bytes_read == size) { 658d5b44036SJonas Devlieghere DataBufferSP buffer_sp(data_up.release()); 659344546bdSWalter Erquinigo data.SetData(buffer_sp, 0, buffer_sp->GetByteSize()); 660344546bdSWalter Erquinigo } 661344546bdSWalter Erquinigo } 662344546bdSWalter Erquinigo return data; 663344546bdSWalter Erquinigo } 664344546bdSWalter Erquinigo 66530c2441aSAleksandr Urakov DataExtractor ObjectFilePECOFF::ReadImageDataByRVA(uint32_t rva, size_t size) { 66630c2441aSAleksandr Urakov Address addr = GetAddress(rva); 6677e1a3076SMartin Storsjö SectionSP sect = addr.GetSection(); 6687e1a3076SMartin Storsjö if (!sect) 6697e1a3076SMartin Storsjö return {}; 6707e1a3076SMartin Storsjö rva = sect->GetFileOffset() + addr.GetOffset(); 67130c2441aSAleksandr Urakov 67230c2441aSAleksandr Urakov return ReadImageData(rva, size); 67330c2441aSAleksandr Urakov } 67430c2441aSAleksandr Urakov 675f754f88fSGreg Clayton // ParseSectionHeaders 676b9c1b51eSKate Stone bool ObjectFilePECOFF::ParseSectionHeaders( 677b9c1b51eSKate Stone uint32_t section_header_data_offset) { 678f754f88fSGreg Clayton const uint32_t nsects = m_coff_header.nsects; 679f754f88fSGreg Clayton m_sect_headers.clear(); 680f754f88fSGreg Clayton 681b9c1b51eSKate Stone if (nsects > 0) { 682f754f88fSGreg Clayton const size_t section_header_byte_size = nsects * sizeof(section_header_t); 683344546bdSWalter Erquinigo DataExtractor section_header_data = 684344546bdSWalter Erquinigo ReadImageData(section_header_data_offset, section_header_byte_size); 685f754f88fSGreg Clayton 686c7bece56SGreg Clayton lldb::offset_t offset = 0; 687b9c1b51eSKate Stone if (section_header_data.ValidOffsetForDataOfSize( 688b9c1b51eSKate Stone offset, section_header_byte_size)) { 689f754f88fSGreg Clayton m_sect_headers.resize(nsects); 690f754f88fSGreg Clayton 691b9c1b51eSKate Stone for (uint32_t idx = 0; idx < nsects; ++idx) { 692f754f88fSGreg Clayton const void *name_data = section_header_data.GetData(&offset, 8); 693b9c1b51eSKate Stone if (name_data) { 694f754f88fSGreg Clayton memcpy(m_sect_headers[idx].name, name_data, 8); 695f754f88fSGreg Clayton m_sect_headers[idx].vmsize = section_header_data.GetU32(&offset); 696f754f88fSGreg Clayton m_sect_headers[idx].vmaddr = section_header_data.GetU32(&offset); 697f754f88fSGreg Clayton m_sect_headers[idx].size = section_header_data.GetU32(&offset); 698f754f88fSGreg Clayton m_sect_headers[idx].offset = section_header_data.GetU32(&offset); 699f754f88fSGreg Clayton m_sect_headers[idx].reloff = section_header_data.GetU32(&offset); 700f754f88fSGreg Clayton m_sect_headers[idx].lineoff = section_header_data.GetU32(&offset); 701f754f88fSGreg Clayton m_sect_headers[idx].nreloc = section_header_data.GetU16(&offset); 702f754f88fSGreg Clayton m_sect_headers[idx].nline = section_header_data.GetU16(&offset); 703f754f88fSGreg Clayton m_sect_headers[idx].flags = section_header_data.GetU32(&offset); 704f754f88fSGreg Clayton } 705f754f88fSGreg Clayton } 706f754f88fSGreg Clayton } 707f754f88fSGreg Clayton } 708f754f88fSGreg Clayton 709a6682a41SJonas Devlieghere return !m_sect_headers.empty(); 710f754f88fSGreg Clayton } 711f754f88fSGreg Clayton 7122886e4a0SPavel Labath llvm::StringRef ObjectFilePECOFF::GetSectionName(const section_header_t §) { 713f15014ffSBenjamin Kramer llvm::StringRef hdr_name(sect.name, llvm::array_lengthof(sect.name)); 7142886e4a0SPavel Labath hdr_name = hdr_name.split('\0').first; 7152886e4a0SPavel Labath if (hdr_name.consume_front("/")) { 7162886e4a0SPavel Labath lldb::offset_t stroff; 7172886e4a0SPavel Labath if (!to_integer(hdr_name, stroff, 10)) 7182886e4a0SPavel Labath return ""; 719b9c1b51eSKate Stone lldb::offset_t string_file_offset = 720b9c1b51eSKate Stone m_coff_header.symoff + (m_coff_header.nsyms * 18) + stroff; 7212886e4a0SPavel Labath if (const char *name = m_data.GetCStr(&string_file_offset)) 7222886e4a0SPavel Labath return name; 7232886e4a0SPavel Labath return ""; 724f754f88fSGreg Clayton } 7252886e4a0SPavel Labath return hdr_name; 726f754f88fSGreg Clayton } 727f754f88fSGreg Clayton 7287e6df41fSGreg Clayton void ObjectFilePECOFF::ParseSymtab(Symtab &symtab) { 729f754f88fSGreg Clayton SectionList *sect_list = GetSectionList(); 73028469ca3SGreg Clayton const uint32_t num_syms = m_coff_header.nsyms; 731344546bdSWalter Erquinigo if (m_file && num_syms > 0 && m_coff_header.symoff > 0) { 7320076e715SGreg Clayton const uint32_t symbol_size = 18; 73328469ca3SGreg Clayton const size_t symbol_data_size = num_syms * symbol_size; 734c35b91ceSAdrian McCarthy // Include the 4-byte string table size at the end of the symbols 735344546bdSWalter Erquinigo DataExtractor symtab_data = 736344546bdSWalter Erquinigo ReadImageData(m_coff_header.symoff, symbol_data_size + 4); 737c7bece56SGreg Clayton lldb::offset_t offset = symbol_data_size; 73828469ca3SGreg Clayton const uint32_t strtab_size = symtab_data.GetU32(&offset); 739344546bdSWalter Erquinigo if (strtab_size > 0) { 740344546bdSWalter Erquinigo DataExtractor strtab_data = ReadImageData( 741344546bdSWalter Erquinigo m_coff_header.symoff + symbol_data_size, strtab_size); 74228469ca3SGreg Clayton 74328469ca3SGreg Clayton offset = 0; 74428469ca3SGreg Clayton std::string symbol_name; 7457e6df41fSGreg Clayton Symbol *symbols = symtab.Resize(num_syms); 746b9c1b51eSKate Stone for (uint32_t i = 0; i < num_syms; ++i) { 747f754f88fSGreg Clayton coff_symbol_t symbol; 74828469ca3SGreg Clayton const uint32_t symbol_offset = offset; 749248a1305SKonrad Kleine const char *symbol_name_cstr = nullptr; 750c35b91ceSAdrian McCarthy // If the first 4 bytes of the symbol string are zero, then they 751c35b91ceSAdrian McCarthy // are followed by a 4-byte string table offset. Else these 75228469ca3SGreg Clayton // 8 bytes contain the symbol name 753b9c1b51eSKate Stone if (symtab_data.GetU32(&offset) == 0) { 75405097246SAdrian Prantl // Long string that doesn't fit into the symbol table name, so 75505097246SAdrian Prantl // now we must read the 4 byte string table offset 75628469ca3SGreg Clayton uint32_t strtab_offset = symtab_data.GetU32(&offset); 75728469ca3SGreg Clayton symbol_name_cstr = strtab_data.PeekCStr(strtab_offset); 75828469ca3SGreg Clayton symbol_name.assign(symbol_name_cstr); 759b9c1b51eSKate Stone } else { 760b9c1b51eSKate Stone // Short string that fits into the symbol table name which is 8 761b9c1b51eSKate Stone // bytes 76228469ca3SGreg Clayton offset += sizeof(symbol.name) - 4; // Skip remaining 76328469ca3SGreg Clayton symbol_name_cstr = symtab_data.PeekCStr(symbol_offset); 764248a1305SKonrad Kleine if (symbol_name_cstr == nullptr) 765f754f88fSGreg Clayton break; 76628469ca3SGreg Clayton symbol_name.assign(symbol_name_cstr, sizeof(symbol.name)); 76728469ca3SGreg Clayton } 76828469ca3SGreg Clayton symbol.value = symtab_data.GetU32(&offset); 76928469ca3SGreg Clayton symbol.sect = symtab_data.GetU16(&offset); 77028469ca3SGreg Clayton symbol.type = symtab_data.GetU16(&offset); 77128469ca3SGreg Clayton symbol.storage = symtab_data.GetU8(&offset); 77228469ca3SGreg Clayton symbol.naux = symtab_data.GetU8(&offset); 773037520e9SGreg Clayton symbols[i].GetMangled().SetValue(ConstString(symbol_name.c_str())); 774b9c1b51eSKate Stone if ((int16_t)symbol.sect >= 1) { 7754394b5beSMartin Storsjö Address symbol_addr(sect_list->FindSectionByID(symbol.sect), 776b9c1b51eSKate Stone symbol.value); 777358cf1eaSGreg Clayton symbols[i].GetAddressRef() = symbol_addr; 778c35b91ceSAdrian McCarthy symbols[i].SetType(MapSymbolType(symbol.type)); 7790076e715SGreg Clayton } 780f754f88fSGreg Clayton 781b9c1b51eSKate Stone if (symbol.naux > 0) { 782f754f88fSGreg Clayton i += symbol.naux; 783f07ddbc9SMartin Storsjö offset += symbol.naux * symbol_size; 7840076e715SGreg Clayton } 785f754f88fSGreg Clayton } 786f754f88fSGreg Clayton } 787344546bdSWalter Erquinigo } 788a4fe3a12SVirgile Bello 789a4fe3a12SVirgile Bello // Read export header 790b9c1b51eSKate Stone if (coff_data_dir_export_table < m_coff_header_opt.data_dirs.size() && 791b9c1b51eSKate Stone m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmsize > 0 && 792b9c1b51eSKate Stone m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmaddr > 0) { 793a4fe3a12SVirgile Bello export_directory_entry export_table; 794b9c1b51eSKate Stone uint32_t data_start = 795b9c1b51eSKate Stone m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmaddr; 796344546bdSWalter Erquinigo 79730c2441aSAleksandr Urakov DataExtractor symtab_data = ReadImageDataByRVA( 79830c2441aSAleksandr Urakov data_start, m_coff_header_opt.data_dirs[0].vmsize); 799a4fe3a12SVirgile Bello lldb::offset_t offset = 0; 800a4fe3a12SVirgile Bello 801a4fe3a12SVirgile Bello // Read export_table header 802a4fe3a12SVirgile Bello export_table.characteristics = symtab_data.GetU32(&offset); 803a4fe3a12SVirgile Bello export_table.time_date_stamp = symtab_data.GetU32(&offset); 804a4fe3a12SVirgile Bello export_table.major_version = symtab_data.GetU16(&offset); 805a4fe3a12SVirgile Bello export_table.minor_version = symtab_data.GetU16(&offset); 806a4fe3a12SVirgile Bello export_table.name = symtab_data.GetU32(&offset); 807a4fe3a12SVirgile Bello export_table.base = symtab_data.GetU32(&offset); 808a4fe3a12SVirgile Bello export_table.number_of_functions = symtab_data.GetU32(&offset); 809a4fe3a12SVirgile Bello export_table.number_of_names = symtab_data.GetU32(&offset); 810a4fe3a12SVirgile Bello export_table.address_of_functions = symtab_data.GetU32(&offset); 811a4fe3a12SVirgile Bello export_table.address_of_names = symtab_data.GetU32(&offset); 812a4fe3a12SVirgile Bello export_table.address_of_name_ordinals = symtab_data.GetU32(&offset); 813a4fe3a12SVirgile Bello 814a4fe3a12SVirgile Bello bool has_ordinal = export_table.address_of_name_ordinals != 0; 815a4fe3a12SVirgile Bello 816a4fe3a12SVirgile Bello lldb::offset_t name_offset = export_table.address_of_names - data_start; 817b9c1b51eSKate Stone lldb::offset_t name_ordinal_offset = 818b9c1b51eSKate Stone export_table.address_of_name_ordinals - data_start; 819a4fe3a12SVirgile Bello 8207e6df41fSGreg Clayton Symbol *symbols = symtab.Resize(export_table.number_of_names); 821a4fe3a12SVirgile Bello 822a4fe3a12SVirgile Bello std::string symbol_name; 823a4fe3a12SVirgile Bello 824a4fe3a12SVirgile Bello // Read each export table entry 825b9c1b51eSKate Stone for (size_t i = 0; i < export_table.number_of_names; ++i) { 826b9c1b51eSKate Stone uint32_t name_ordinal = 827b9c1b51eSKate Stone has_ordinal ? symtab_data.GetU16(&name_ordinal_offset) : i; 828a4fe3a12SVirgile Bello uint32_t name_address = symtab_data.GetU32(&name_offset); 829a4fe3a12SVirgile Bello 830b9c1b51eSKate Stone const char *symbol_name_cstr = 831b9c1b51eSKate Stone symtab_data.PeekCStr(name_address - data_start); 832a4fe3a12SVirgile Bello symbol_name.assign(symbol_name_cstr); 833a4fe3a12SVirgile Bello 834b9c1b51eSKate Stone lldb::offset_t function_offset = export_table.address_of_functions - 835b9c1b51eSKate Stone data_start + 836b9c1b51eSKate Stone sizeof(uint32_t) * name_ordinal; 837a4fe3a12SVirgile Bello uint32_t function_rva = symtab_data.GetU32(&function_offset); 838a4fe3a12SVirgile Bello 839b9c1b51eSKate Stone Address symbol_addr(m_coff_header_opt.image_base + function_rva, 840b9c1b51eSKate Stone sect_list); 841a4fe3a12SVirgile Bello symbols[i].GetMangled().SetValue(ConstString(symbol_name.c_str())); 842358cf1eaSGreg Clayton symbols[i].GetAddressRef() = symbol_addr; 843a4fe3a12SVirgile Bello symbols[i].SetType(lldb::eSymbolTypeCode); 844a4fe3a12SVirgile Bello symbols[i].SetDebug(true); 845a4fe3a12SVirgile Bello } 846a4fe3a12SVirgile Bello } 847f754f88fSGreg Clayton } 848f754f88fSGreg Clayton 84930c2441aSAleksandr Urakov std::unique_ptr<CallFrameInfo> ObjectFilePECOFF::CreateCallFrameInfo() { 85030c2441aSAleksandr Urakov if (coff_data_dir_exception_table >= m_coff_header_opt.data_dirs.size()) 85130c2441aSAleksandr Urakov return {}; 85230c2441aSAleksandr Urakov 85330c2441aSAleksandr Urakov data_directory data_dir_exception = 85430c2441aSAleksandr Urakov m_coff_header_opt.data_dirs[coff_data_dir_exception_table]; 85530c2441aSAleksandr Urakov if (!data_dir_exception.vmaddr) 85630c2441aSAleksandr Urakov return {}; 85730c2441aSAleksandr Urakov 858aa786b88SMartin Storsjö if (m_coff_header.machine != llvm::COFF::IMAGE_FILE_MACHINE_AMD64) 859aa786b88SMartin Storsjö return {}; 860aa786b88SMartin Storsjö 86130c2441aSAleksandr Urakov return std::make_unique<PECallFrameInfo>(*this, data_dir_exception.vmaddr, 86230c2441aSAleksandr Urakov data_dir_exception.vmsize); 86330c2441aSAleksandr Urakov } 86430c2441aSAleksandr Urakov 865b9c1b51eSKate Stone bool ObjectFilePECOFF::IsStripped() { 8663046e668SGreg Clayton // TODO: determine this for COFF 8673046e668SGreg Clayton return false; 8683046e668SGreg Clayton } 8693046e668SGreg Clayton 8702e5bb6d8SMartin Storsjö SectionType ObjectFilePECOFF::GetSectionType(llvm::StringRef sect_name, 8712e5bb6d8SMartin Storsjö const section_header_t §) { 8722e5bb6d8SMartin Storsjö ConstString const_sect_name(sect_name); 8732e5bb6d8SMartin Storsjö static ConstString g_code_sect_name(".code"); 8742e5bb6d8SMartin Storsjö static ConstString g_CODE_sect_name("CODE"); 8752e5bb6d8SMartin Storsjö static ConstString g_data_sect_name(".data"); 8762e5bb6d8SMartin Storsjö static ConstString g_DATA_sect_name("DATA"); 8772e5bb6d8SMartin Storsjö static ConstString g_bss_sect_name(".bss"); 8782e5bb6d8SMartin Storsjö static ConstString g_BSS_sect_name("BSS"); 8792e5bb6d8SMartin Storsjö 8802e5bb6d8SMartin Storsjö if (sect.flags & llvm::COFF::IMAGE_SCN_CNT_CODE && 8812e5bb6d8SMartin Storsjö ((const_sect_name == g_code_sect_name) || 8822e5bb6d8SMartin Storsjö (const_sect_name == g_CODE_sect_name))) { 8832e5bb6d8SMartin Storsjö return eSectionTypeCode; 8842e5bb6d8SMartin Storsjö } 8852e5bb6d8SMartin Storsjö if (sect.flags & llvm::COFF::IMAGE_SCN_CNT_INITIALIZED_DATA && 8862e5bb6d8SMartin Storsjö ((const_sect_name == g_data_sect_name) || 8872e5bb6d8SMartin Storsjö (const_sect_name == g_DATA_sect_name))) { 8882e5bb6d8SMartin Storsjö if (sect.size == 0 && sect.offset == 0) 8892e5bb6d8SMartin Storsjö return eSectionTypeZeroFill; 8902e5bb6d8SMartin Storsjö else 8912e5bb6d8SMartin Storsjö return eSectionTypeData; 8922e5bb6d8SMartin Storsjö } 8932e5bb6d8SMartin Storsjö if (sect.flags & llvm::COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA && 8942e5bb6d8SMartin Storsjö ((const_sect_name == g_bss_sect_name) || 8952e5bb6d8SMartin Storsjö (const_sect_name == g_BSS_sect_name))) { 8962e5bb6d8SMartin Storsjö if (sect.size == 0) 8972e5bb6d8SMartin Storsjö return eSectionTypeZeroFill; 8982e5bb6d8SMartin Storsjö else 8992e5bb6d8SMartin Storsjö return eSectionTypeData; 9002e5bb6d8SMartin Storsjö } 9012e5bb6d8SMartin Storsjö 9022e5bb6d8SMartin Storsjö SectionType section_type = 9032e5bb6d8SMartin Storsjö llvm::StringSwitch<SectionType>(sect_name) 9042e5bb6d8SMartin Storsjö .Case(".debug", eSectionTypeDebug) 9052e5bb6d8SMartin Storsjö .Case(".stabstr", eSectionTypeDataCString) 9062e5bb6d8SMartin Storsjö .Case(".reloc", eSectionTypeOther) 9072e5bb6d8SMartin Storsjö .Case(".debug_abbrev", eSectionTypeDWARFDebugAbbrev) 9082e5bb6d8SMartin Storsjö .Case(".debug_aranges", eSectionTypeDWARFDebugAranges) 9092e5bb6d8SMartin Storsjö .Case(".debug_frame", eSectionTypeDWARFDebugFrame) 9102e5bb6d8SMartin Storsjö .Case(".debug_info", eSectionTypeDWARFDebugInfo) 9112e5bb6d8SMartin Storsjö .Case(".debug_line", eSectionTypeDWARFDebugLine) 9122e5bb6d8SMartin Storsjö .Case(".debug_loc", eSectionTypeDWARFDebugLoc) 9132e5bb6d8SMartin Storsjö .Case(".debug_loclists", eSectionTypeDWARFDebugLocLists) 9142e5bb6d8SMartin Storsjö .Case(".debug_macinfo", eSectionTypeDWARFDebugMacInfo) 9152e5bb6d8SMartin Storsjö .Case(".debug_names", eSectionTypeDWARFDebugNames) 9162e5bb6d8SMartin Storsjö .Case(".debug_pubnames", eSectionTypeDWARFDebugPubNames) 9172e5bb6d8SMartin Storsjö .Case(".debug_pubtypes", eSectionTypeDWARFDebugPubTypes) 9182e5bb6d8SMartin Storsjö .Case(".debug_ranges", eSectionTypeDWARFDebugRanges) 9192e5bb6d8SMartin Storsjö .Case(".debug_str", eSectionTypeDWARFDebugStr) 9202e5bb6d8SMartin Storsjö .Case(".debug_types", eSectionTypeDWARFDebugTypes) 921934c025eSMartin Storsjö // .eh_frame can be truncated to 8 chars. 922934c025eSMartin Storsjö .Cases(".eh_frame", ".eh_fram", eSectionTypeEHFrame) 9232e5bb6d8SMartin Storsjö .Case(".gosymtab", eSectionTypeGoSymtab) 9242e5bb6d8SMartin Storsjö .Default(eSectionTypeInvalid); 9252e5bb6d8SMartin Storsjö if (section_type != eSectionTypeInvalid) 9262e5bb6d8SMartin Storsjö return section_type; 9272e5bb6d8SMartin Storsjö 9282e5bb6d8SMartin Storsjö if (sect.flags & llvm::COFF::IMAGE_SCN_CNT_CODE) 9292e5bb6d8SMartin Storsjö return eSectionTypeCode; 9302e5bb6d8SMartin Storsjö if (sect.flags & llvm::COFF::IMAGE_SCN_CNT_INITIALIZED_DATA) 9312e5bb6d8SMartin Storsjö return eSectionTypeData; 9322e5bb6d8SMartin Storsjö if (sect.flags & llvm::COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) { 9332e5bb6d8SMartin Storsjö if (sect.size == 0) 9342e5bb6d8SMartin Storsjö return eSectionTypeZeroFill; 9352e5bb6d8SMartin Storsjö else 9362e5bb6d8SMartin Storsjö return eSectionTypeData; 9372e5bb6d8SMartin Storsjö } 9382e5bb6d8SMartin Storsjö return eSectionTypeOther; 9392e5bb6d8SMartin Storsjö } 9402e5bb6d8SMartin Storsjö 941b9c1b51eSKate Stone void ObjectFilePECOFF::CreateSections(SectionList &unified_section_list) { 942d5b44036SJonas Devlieghere if (m_sections_up) 94388a2c2a4SPavel Labath return; 94406412daeSJonas Devlieghere m_sections_up = std::make_unique<SectionList>(); 945a1743499SGreg Clayton ModuleSP module_sp(GetModule()); 946b9c1b51eSKate Stone if (module_sp) { 94716ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 9487db8b5c4SPavel Labath 94973a7a55cSPavel Labath SectionSP header_sp = std::make_shared<Section>( 95073a7a55cSPavel Labath module_sp, this, ~user_id_t(0), ConstString("PECOFF header"), 95173a7a55cSPavel Labath eSectionTypeOther, m_coff_header_opt.image_base, 95273a7a55cSPavel Labath m_coff_header_opt.header_size, 95373a7a55cSPavel Labath /*file_offset*/ 0, m_coff_header_opt.header_size, 95473a7a55cSPavel Labath m_coff_header_opt.sect_alignment, 9557db8b5c4SPavel Labath /*flags*/ 0); 956f1e0ae34SPavel Labath header_sp->SetPermissions(ePermissionsReadable); 95773a7a55cSPavel Labath m_sections_up->AddSection(header_sp); 95873a7a55cSPavel Labath unified_section_list.AddSection(header_sp); 9597db8b5c4SPavel Labath 960f754f88fSGreg Clayton const uint32_t nsects = m_sect_headers.size(); 961e72dfb32SGreg Clayton ModuleSP module_sp(GetModule()); 962b9c1b51eSKate Stone for (uint32_t idx = 0; idx < nsects; ++idx) { 9632e5bb6d8SMartin Storsjö llvm::StringRef sect_name = GetSectionName(m_sect_headers[idx]); 9642e5bb6d8SMartin Storsjö ConstString const_sect_name(sect_name); 9652e5bb6d8SMartin Storsjö SectionType section_type = GetSectionType(sect_name, m_sect_headers[idx]); 966f754f88fSGreg Clayton 967b9c1b51eSKate Stone SectionSP section_sp(new Section( 968b9c1b51eSKate Stone module_sp, // Module to which this section belongs 969a7499c98SMichael Sartain this, // Object file to which this section belongs 9707db8b5c4SPavel Labath idx + 1, // Section ID is the 1 based section index. 971f754f88fSGreg Clayton const_sect_name, // Name of this section 9727db8b5c4SPavel Labath section_type, 97373a7a55cSPavel Labath m_coff_header_opt.image_base + 974b9c1b51eSKate Stone m_sect_headers[idx].vmaddr, // File VM address == addresses as 975b9c1b51eSKate Stone // they are found in the object file 976f754f88fSGreg Clayton m_sect_headers[idx].vmsize, // VM size in bytes of this section 977b9c1b51eSKate Stone m_sect_headers[idx] 978b9c1b51eSKate Stone .offset, // Offset to the data for this section in the file 979b9c1b51eSKate Stone m_sect_headers[idx] 980b9c1b51eSKate Stone .size, // Size in bytes of this section as found in the file 98148672afbSGreg Clayton m_coff_header_opt.sect_alignment, // Section alignment 982f754f88fSGreg Clayton m_sect_headers[idx].flags)); // Flags for this section 983f754f88fSGreg Clayton 984f1e0ae34SPavel Labath uint32_t permissions = 0; 985f1e0ae34SPavel Labath if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_MEM_EXECUTE) 986f1e0ae34SPavel Labath permissions |= ePermissionsExecutable; 987f1e0ae34SPavel Labath if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_MEM_READ) 988f1e0ae34SPavel Labath permissions |= ePermissionsReadable; 989f1e0ae34SPavel Labath if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_MEM_WRITE) 990f1e0ae34SPavel Labath permissions |= ePermissionsWritable; 991f1e0ae34SPavel Labath section_sp->SetPermissions(permissions); 992f1e0ae34SPavel Labath 99373a7a55cSPavel Labath m_sections_up->AddSection(section_sp); 99473a7a55cSPavel Labath unified_section_list.AddSection(section_sp); 995f754f88fSGreg Clayton } 996f754f88fSGreg Clayton } 997a1743499SGreg Clayton } 998f754f88fSGreg Clayton 999b8d03935SAaron Smith UUID ObjectFilePECOFF::GetUUID() { 1000b8d03935SAaron Smith if (m_uuid.IsValid()) 1001b8d03935SAaron Smith return m_uuid; 1002b8d03935SAaron Smith 1003b8d03935SAaron Smith if (!CreateBinary()) 1004b8d03935SAaron Smith return UUID(); 1005b8d03935SAaron Smith 1006d372a8e8SPavel Labath m_uuid = GetCoffUUID(*m_binary); 1007b8d03935SAaron Smith return m_uuid; 1008b8d03935SAaron Smith } 1009f754f88fSGreg Clayton 1010c8daf4a7SAlvin Wong llvm::Optional<FileSpec> ObjectFilePECOFF::GetDebugLink() { 1011c8daf4a7SAlvin Wong std::string gnu_debuglink_file; 1012c8daf4a7SAlvin Wong uint32_t gnu_debuglink_crc; 1013c8daf4a7SAlvin Wong if (GetDebugLinkContents(*m_binary, gnu_debuglink_file, gnu_debuglink_crc)) 1014c8daf4a7SAlvin Wong return FileSpec(gnu_debuglink_file); 1015c8daf4a7SAlvin Wong return llvm::None; 1016c8daf4a7SAlvin Wong } 1017c8daf4a7SAlvin Wong 1018037ed1beSAaron Smith uint32_t ObjectFilePECOFF::ParseDependentModules() { 1019037ed1beSAaron Smith ModuleSP module_sp(GetModule()); 1020037ed1beSAaron Smith if (!module_sp) 1021f754f88fSGreg Clayton return 0; 1022037ed1beSAaron Smith 1023037ed1beSAaron Smith std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 1024037ed1beSAaron Smith if (m_deps_filespec) 1025037ed1beSAaron Smith return m_deps_filespec->GetSize(); 1026037ed1beSAaron Smith 1027037ed1beSAaron Smith // Cache coff binary if it is not done yet. 1028037ed1beSAaron Smith if (!CreateBinary()) 1029037ed1beSAaron Smith return 0; 1030037ed1beSAaron Smith 1031a007a6d8SPavel Labath Log *log = GetLog(LLDBLog::Object); 1032d372a8e8SPavel Labath LLDB_LOG(log, "this = {0}, module = {1} ({2}), file = {3}, binary = {4}", 1033d372a8e8SPavel Labath this, GetModule().get(), GetModule()->GetSpecificationDescription(), 1034d372a8e8SPavel Labath m_file.GetPath(), m_binary.get()); 1035037ed1beSAaron Smith 1036037ed1beSAaron Smith m_deps_filespec = FileSpecList(); 1037037ed1beSAaron Smith 1038d372a8e8SPavel Labath for (const auto &entry : m_binary->import_directories()) { 1039037ed1beSAaron Smith llvm::StringRef dll_name; 1040037ed1beSAaron Smith // Report a bogus entry. 10411c03389cSReid Kleckner if (llvm::Error e = entry.getName(dll_name)) { 104263e5fb76SJonas Devlieghere LLDB_LOGF(log, 104363e5fb76SJonas Devlieghere "ObjectFilePECOFF::ParseDependentModules() - failed to get " 1044037ed1beSAaron Smith "import directory entry name: %s", 10451c03389cSReid Kleckner llvm::toString(std::move(e)).c_str()); 1046037ed1beSAaron Smith continue; 1047037ed1beSAaron Smith } 1048037ed1beSAaron Smith 1049037ed1beSAaron Smith // At this moment we only have the base name of the DLL. The full path can 1050037ed1beSAaron Smith // only be seen after the dynamic loading. Our best guess is Try to get it 1051037ed1beSAaron Smith // with the help of the object file's directory. 1052b3f44ad9SStella Stamenova llvm::SmallString<128> dll_fullpath; 1053037ed1beSAaron Smith FileSpec dll_specs(dll_name); 1054037ed1beSAaron Smith dll_specs.GetDirectory().SetString(m_file.GetDirectory().GetCString()); 1055037ed1beSAaron Smith 1056037ed1beSAaron Smith if (!llvm::sys::fs::real_path(dll_specs.GetPath(), dll_fullpath)) 1057f893d5bfSJonas Devlieghere m_deps_filespec->EmplaceBack(dll_fullpath); 1058037ed1beSAaron Smith else { 1059037ed1beSAaron Smith // Known DLLs or DLL not found in the object file directory. 1060f893d5bfSJonas Devlieghere m_deps_filespec->EmplaceBack(dll_name); 1061037ed1beSAaron Smith } 1062037ed1beSAaron Smith } 1063037ed1beSAaron Smith return m_deps_filespec->GetSize(); 1064037ed1beSAaron Smith } 1065037ed1beSAaron Smith 1066037ed1beSAaron Smith uint32_t ObjectFilePECOFF::GetDependentModules(FileSpecList &files) { 1067037ed1beSAaron Smith auto num_modules = ParseDependentModules(); 1068037ed1beSAaron Smith auto original_size = files.GetSize(); 1069037ed1beSAaron Smith 1070037ed1beSAaron Smith for (unsigned i = 0; i < num_modules; ++i) 1071037ed1beSAaron Smith files.AppendIfUnique(m_deps_filespec->GetFileSpecAtIndex(i)); 1072037ed1beSAaron Smith 1073037ed1beSAaron Smith return files.GetSize() - original_size; 1074f754f88fSGreg Clayton } 1075f754f88fSGreg Clayton 1076b9c1b51eSKate Stone lldb_private::Address ObjectFilePECOFF::GetEntryPointAddress() { 10778e38c666SStephane Sezer if (m_entry_point_address.IsValid()) 10788e38c666SStephane Sezer return m_entry_point_address; 10798e38c666SStephane Sezer 10808e38c666SStephane Sezer if (!ParseHeader() || !IsExecutable()) 10818e38c666SStephane Sezer return m_entry_point_address; 10828e38c666SStephane Sezer 10838e38c666SStephane Sezer SectionList *section_list = GetSectionList(); 1084a5235af9SAleksandr Urakov addr_t file_addr = m_coff_header_opt.entry + m_coff_header_opt.image_base; 10858e38c666SStephane Sezer 10868e38c666SStephane Sezer if (!section_list) 1087a5235af9SAleksandr Urakov m_entry_point_address.SetOffset(file_addr); 10888e38c666SStephane Sezer else 1089b8d03935SAaron Smith m_entry_point_address.ResolveAddressUsingFileSections(file_addr, 1090b8d03935SAaron Smith section_list); 10918e38c666SStephane Sezer return m_entry_point_address; 10928e38c666SStephane Sezer } 10938e38c666SStephane Sezer 1094d1304bbaSPavel Labath Address ObjectFilePECOFF::GetBaseAddress() { 1095d1304bbaSPavel Labath return Address(GetSectionList()->GetSectionAtIndex(0), 0); 1096d1304bbaSPavel Labath } 1097d1304bbaSPavel Labath 1098f754f88fSGreg Clayton // Dump 1099f754f88fSGreg Clayton // 1100f754f88fSGreg Clayton // Dump the specifics of the runtime file container (such as any headers 1101f754f88fSGreg Clayton // segments, sections, etc). 1102b9c1b51eSKate Stone void ObjectFilePECOFF::Dump(Stream *s) { 1103a1743499SGreg Clayton ModuleSP module_sp(GetModule()); 1104b9c1b51eSKate Stone if (module_sp) { 110516ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 1106324a1036SSaleem Abdulrasool s->Printf("%p: ", static_cast<void *>(this)); 1107f754f88fSGreg Clayton s->Indent(); 1108f754f88fSGreg Clayton s->PutCString("ObjectFilePECOFF"); 1109f754f88fSGreg Clayton 1110f760f5aeSPavel Labath ArchSpec header_arch = GetArchitecture(); 1111f754f88fSGreg Clayton 1112b9c1b51eSKate Stone *s << ", file = '" << m_file 1113b9c1b51eSKate Stone << "', arch = " << header_arch.GetArchitectureName() << "\n"; 1114f754f88fSGreg Clayton 11153046e668SGreg Clayton SectionList *sections = GetSectionList(); 11163046e668SGreg Clayton if (sections) 11173a168297SPavel Labath sections->Dump(s->AsRawOstream(), s->GetIndentLevel(), nullptr, true, 11183a168297SPavel Labath UINT32_MAX); 1119f754f88fSGreg Clayton 1120d5b44036SJonas Devlieghere if (m_symtab_up) 1121248a1305SKonrad Kleine m_symtab_up->Dump(s, nullptr, eSortOrderNone); 1122f754f88fSGreg Clayton 1123f754f88fSGreg Clayton if (m_dos_header.e_magic) 1124f754f88fSGreg Clayton DumpDOSHeader(s, m_dos_header); 1125b9c1b51eSKate Stone if (m_coff_header.machine) { 1126f754f88fSGreg Clayton DumpCOFFHeader(s, m_coff_header); 1127f754f88fSGreg Clayton if (m_coff_header.hdrsize) 1128f754f88fSGreg Clayton DumpOptCOFFHeader(s, m_coff_header_opt); 1129f754f88fSGreg Clayton } 1130f754f88fSGreg Clayton s->EOL(); 1131f754f88fSGreg Clayton DumpSectionHeaders(s); 1132f754f88fSGreg Clayton s->EOL(); 1133037ed1beSAaron Smith 1134037ed1beSAaron Smith DumpDependentModules(s); 1135037ed1beSAaron Smith s->EOL(); 1136f754f88fSGreg Clayton } 1137a1743499SGreg Clayton } 1138f754f88fSGreg Clayton 1139f754f88fSGreg Clayton // DumpDOSHeader 1140f754f88fSGreg Clayton // 1141f754f88fSGreg Clayton // Dump the MS-DOS header to the specified output stream 1142b9c1b51eSKate Stone void ObjectFilePECOFF::DumpDOSHeader(Stream *s, const dos_header_t &header) { 1143f754f88fSGreg Clayton s->PutCString("MSDOS Header\n"); 1144f754f88fSGreg Clayton s->Printf(" e_magic = 0x%4.4x\n", header.e_magic); 1145f754f88fSGreg Clayton s->Printf(" e_cblp = 0x%4.4x\n", header.e_cblp); 1146f754f88fSGreg Clayton s->Printf(" e_cp = 0x%4.4x\n", header.e_cp); 1147f754f88fSGreg Clayton s->Printf(" e_crlc = 0x%4.4x\n", header.e_crlc); 1148f754f88fSGreg Clayton s->Printf(" e_cparhdr = 0x%4.4x\n", header.e_cparhdr); 1149f754f88fSGreg Clayton s->Printf(" e_minalloc = 0x%4.4x\n", header.e_minalloc); 1150f754f88fSGreg Clayton s->Printf(" e_maxalloc = 0x%4.4x\n", header.e_maxalloc); 1151f754f88fSGreg Clayton s->Printf(" e_ss = 0x%4.4x\n", header.e_ss); 1152f754f88fSGreg Clayton s->Printf(" e_sp = 0x%4.4x\n", header.e_sp); 1153f754f88fSGreg Clayton s->Printf(" e_csum = 0x%4.4x\n", header.e_csum); 1154f754f88fSGreg Clayton s->Printf(" e_ip = 0x%4.4x\n", header.e_ip); 1155f754f88fSGreg Clayton s->Printf(" e_cs = 0x%4.4x\n", header.e_cs); 1156f754f88fSGreg Clayton s->Printf(" e_lfarlc = 0x%4.4x\n", header.e_lfarlc); 1157f754f88fSGreg Clayton s->Printf(" e_ovno = 0x%4.4x\n", header.e_ovno); 1158f754f88fSGreg Clayton s->Printf(" e_res[4] = { 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x }\n", 1159b9c1b51eSKate Stone header.e_res[0], header.e_res[1], header.e_res[2], header.e_res[3]); 1160f754f88fSGreg Clayton s->Printf(" e_oemid = 0x%4.4x\n", header.e_oemid); 1161f754f88fSGreg Clayton s->Printf(" e_oeminfo = 0x%4.4x\n", header.e_oeminfo); 1162b9c1b51eSKate Stone s->Printf(" e_res2[10] = { 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, " 1163b9c1b51eSKate Stone "0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x }\n", 1164b9c1b51eSKate Stone header.e_res2[0], header.e_res2[1], header.e_res2[2], 1165b9c1b51eSKate Stone header.e_res2[3], header.e_res2[4], header.e_res2[5], 1166b9c1b51eSKate Stone header.e_res2[6], header.e_res2[7], header.e_res2[8], 1167f754f88fSGreg Clayton header.e_res2[9]); 1168f754f88fSGreg Clayton s->Printf(" e_lfanew = 0x%8.8x\n", header.e_lfanew); 1169f754f88fSGreg Clayton } 1170f754f88fSGreg Clayton 1171f754f88fSGreg Clayton // DumpCOFFHeader 1172f754f88fSGreg Clayton // 1173f754f88fSGreg Clayton // Dump the COFF header to the specified output stream 1174b9c1b51eSKate Stone void ObjectFilePECOFF::DumpCOFFHeader(Stream *s, const coff_header_t &header) { 1175f754f88fSGreg Clayton s->PutCString("COFF Header\n"); 1176f754f88fSGreg Clayton s->Printf(" machine = 0x%4.4x\n", header.machine); 1177f754f88fSGreg Clayton s->Printf(" nsects = 0x%4.4x\n", header.nsects); 1178f754f88fSGreg Clayton s->Printf(" modtime = 0x%8.8x\n", header.modtime); 1179f754f88fSGreg Clayton s->Printf(" symoff = 0x%8.8x\n", header.symoff); 1180f754f88fSGreg Clayton s->Printf(" nsyms = 0x%8.8x\n", header.nsyms); 1181f754f88fSGreg Clayton s->Printf(" hdrsize = 0x%4.4x\n", header.hdrsize); 1182f754f88fSGreg Clayton } 1183f754f88fSGreg Clayton 1184f754f88fSGreg Clayton // DumpOptCOFFHeader 1185f754f88fSGreg Clayton // 1186f754f88fSGreg Clayton // Dump the optional COFF header to the specified output stream 1187b9c1b51eSKate Stone void ObjectFilePECOFF::DumpOptCOFFHeader(Stream *s, 1188b9c1b51eSKate Stone const coff_opt_header_t &header) { 1189f754f88fSGreg Clayton s->PutCString("Optional COFF Header\n"); 1190f754f88fSGreg Clayton s->Printf(" magic = 0x%4.4x\n", header.magic); 1191b9c1b51eSKate Stone s->Printf(" major_linker_version = 0x%2.2x\n", 1192b9c1b51eSKate Stone header.major_linker_version); 1193b9c1b51eSKate Stone s->Printf(" minor_linker_version = 0x%2.2x\n", 1194b9c1b51eSKate Stone header.minor_linker_version); 1195f754f88fSGreg Clayton s->Printf(" code_size = 0x%8.8x\n", header.code_size); 1196f754f88fSGreg Clayton s->Printf(" data_size = 0x%8.8x\n", header.data_size); 1197f754f88fSGreg Clayton s->Printf(" bss_size = 0x%8.8x\n", header.bss_size); 1198f754f88fSGreg Clayton s->Printf(" entry = 0x%8.8x\n", header.entry); 1199f754f88fSGreg Clayton s->Printf(" code_offset = 0x%8.8x\n", header.code_offset); 1200f754f88fSGreg Clayton s->Printf(" data_offset = 0x%8.8x\n", header.data_offset); 1201b9c1b51eSKate Stone s->Printf(" image_base = 0x%16.16" PRIx64 "\n", 1202b9c1b51eSKate Stone header.image_base); 1203f754f88fSGreg Clayton s->Printf(" sect_alignment = 0x%8.8x\n", header.sect_alignment); 1204f754f88fSGreg Clayton s->Printf(" file_alignment = 0x%8.8x\n", header.file_alignment); 1205b9c1b51eSKate Stone s->Printf(" major_os_system_version = 0x%4.4x\n", 1206b9c1b51eSKate Stone header.major_os_system_version); 1207b9c1b51eSKate Stone s->Printf(" minor_os_system_version = 0x%4.4x\n", 1208b9c1b51eSKate Stone header.minor_os_system_version); 1209b9c1b51eSKate Stone s->Printf(" major_image_version = 0x%4.4x\n", 1210b9c1b51eSKate Stone header.major_image_version); 1211b9c1b51eSKate Stone s->Printf(" minor_image_version = 0x%4.4x\n", 1212b9c1b51eSKate Stone header.minor_image_version); 1213b9c1b51eSKate Stone s->Printf(" major_subsystem_version = 0x%4.4x\n", 1214b9c1b51eSKate Stone header.major_subsystem_version); 1215b9c1b51eSKate Stone s->Printf(" minor_subsystem_version = 0x%4.4x\n", 1216b9c1b51eSKate Stone header.minor_subsystem_version); 1217f754f88fSGreg Clayton s->Printf(" reserved1 = 0x%8.8x\n", header.reserved1); 1218f754f88fSGreg Clayton s->Printf(" image_size = 0x%8.8x\n", header.image_size); 1219f754f88fSGreg Clayton s->Printf(" header_size = 0x%8.8x\n", header.header_size); 122028469ca3SGreg Clayton s->Printf(" checksum = 0x%8.8x\n", header.checksum); 1221f754f88fSGreg Clayton s->Printf(" subsystem = 0x%4.4x\n", header.subsystem); 1222f754f88fSGreg Clayton s->Printf(" dll_flags = 0x%4.4x\n", header.dll_flags); 1223b9c1b51eSKate Stone s->Printf(" stack_reserve_size = 0x%16.16" PRIx64 "\n", 1224b9c1b51eSKate Stone header.stack_reserve_size); 1225b9c1b51eSKate Stone s->Printf(" stack_commit_size = 0x%16.16" PRIx64 "\n", 1226b9c1b51eSKate Stone header.stack_commit_size); 1227b9c1b51eSKate Stone s->Printf(" heap_reserve_size = 0x%16.16" PRIx64 "\n", 1228b9c1b51eSKate Stone header.heap_reserve_size); 1229b9c1b51eSKate Stone s->Printf(" heap_commit_size = 0x%16.16" PRIx64 "\n", 1230b9c1b51eSKate Stone header.heap_commit_size); 1231f754f88fSGreg Clayton s->Printf(" loader_flags = 0x%8.8x\n", header.loader_flags); 1232b9c1b51eSKate Stone s->Printf(" num_data_dir_entries = 0x%8.8x\n", 1233b9c1b51eSKate Stone (uint32_t)header.data_dirs.size()); 1234f754f88fSGreg Clayton uint32_t i; 1235b9c1b51eSKate Stone for (i = 0; i < header.data_dirs.size(); i++) { 1236b9c1b51eSKate Stone s->Printf(" data_dirs[%2u] vmaddr = 0x%8.8x, vmsize = 0x%8.8x\n", i, 1237b9c1b51eSKate Stone header.data_dirs[i].vmaddr, header.data_dirs[i].vmsize); 1238f754f88fSGreg Clayton } 1239f754f88fSGreg Clayton } 1240f754f88fSGreg Clayton // DumpSectionHeader 1241f754f88fSGreg Clayton // 1242f754f88fSGreg Clayton // Dump a single ELF section header to the specified output stream 1243b9c1b51eSKate Stone void ObjectFilePECOFF::DumpSectionHeader(Stream *s, 1244b9c1b51eSKate Stone const section_header_t &sh) { 1245adcd0268SBenjamin Kramer std::string name = std::string(GetSectionName(sh)); 1246b9c1b51eSKate 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 " 1247b9c1b51eSKate Stone "0x%4.4x 0x%8.8x\n", 1248b9c1b51eSKate Stone name.c_str(), sh.vmaddr, sh.vmsize, sh.offset, sh.size, sh.reloff, 1249b9c1b51eSKate Stone sh.lineoff, sh.nreloc, sh.nline, sh.flags); 1250f754f88fSGreg Clayton } 1251f754f88fSGreg Clayton 1252f754f88fSGreg Clayton // DumpSectionHeaders 1253f754f88fSGreg Clayton // 1254f754f88fSGreg Clayton // Dump all of the ELF section header to the specified output stream 1255b9c1b51eSKate Stone void ObjectFilePECOFF::DumpSectionHeaders(Stream *s) { 1256f754f88fSGreg Clayton 1257f754f88fSGreg Clayton s->PutCString("Section Headers\n"); 1258b9c1b51eSKate Stone s->PutCString("IDX name vm addr vm size file off file " 1259b9c1b51eSKate Stone "size reloc off line off nreloc nline flags\n"); 1260b9c1b51eSKate Stone s->PutCString("==== ---------------- ---------- ---------- ---------- " 1261b9c1b51eSKate Stone "---------- ---------- ---------- ------ ------ ----------\n"); 1262f754f88fSGreg Clayton 1263f754f88fSGreg Clayton uint32_t idx = 0; 1264f754f88fSGreg Clayton SectionHeaderCollIter pos, end = m_sect_headers.end(); 1265f754f88fSGreg Clayton 1266b9c1b51eSKate Stone for (pos = m_sect_headers.begin(); pos != end; ++pos, ++idx) { 1267f754f88fSGreg Clayton s->Printf("[%2u] ", idx); 1268f754f88fSGreg Clayton ObjectFilePECOFF::DumpSectionHeader(s, *pos); 1269f754f88fSGreg Clayton } 1270f754f88fSGreg Clayton } 1271f754f88fSGreg Clayton 1272037ed1beSAaron Smith // DumpDependentModules 1273037ed1beSAaron Smith // 1274037ed1beSAaron Smith // Dump all of the dependent modules to the specified output stream 1275037ed1beSAaron Smith void ObjectFilePECOFF::DumpDependentModules(lldb_private::Stream *s) { 1276037ed1beSAaron Smith auto num_modules = ParseDependentModules(); 1277037ed1beSAaron Smith if (num_modules > 0) { 1278037ed1beSAaron Smith s->PutCString("Dependent Modules\n"); 1279037ed1beSAaron Smith for (unsigned i = 0; i < num_modules; ++i) { 1280037ed1beSAaron Smith auto spec = m_deps_filespec->GetFileSpecAtIndex(i); 1281037ed1beSAaron Smith s->Printf(" %s\n", spec.GetFilename().GetCString()); 1282037ed1beSAaron Smith } 1283037ed1beSAaron Smith } 1284037ed1beSAaron Smith } 1285037ed1beSAaron Smith 1286fb3b3bd1SZachary Turner bool ObjectFilePECOFF::IsWindowsSubsystem() { 1287fb3b3bd1SZachary Turner switch (m_coff_header_opt.subsystem) { 1288fb3b3bd1SZachary Turner case llvm::COFF::IMAGE_SUBSYSTEM_NATIVE: 1289fb3b3bd1SZachary Turner case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_GUI: 1290fb3b3bd1SZachary Turner case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_CUI: 1291fb3b3bd1SZachary Turner case llvm::COFF::IMAGE_SUBSYSTEM_NATIVE_WINDOWS: 1292fb3b3bd1SZachary Turner case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_CE_GUI: 1293fb3b3bd1SZachary Turner case llvm::COFF::IMAGE_SUBSYSTEM_XBOX: 1294fb3b3bd1SZachary Turner case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_BOOT_APPLICATION: 1295fb3b3bd1SZachary Turner return true; 1296fb3b3bd1SZachary Turner default: 1297fb3b3bd1SZachary Turner return false; 1298fb3b3bd1SZachary Turner } 1299fb3b3bd1SZachary Turner } 1300fb3b3bd1SZachary Turner 1301f760f5aeSPavel Labath ArchSpec ObjectFilePECOFF::GetArchitecture() { 1302237ad974SCharles Davis uint16_t machine = m_coff_header.machine; 1303b9c1b51eSKate Stone switch (machine) { 1304f760f5aeSPavel Labath default: 1305f760f5aeSPavel Labath break; 1306237ad974SCharles Davis case llvm::COFF::IMAGE_FILE_MACHINE_AMD64: 1307237ad974SCharles Davis case llvm::COFF::IMAGE_FILE_MACHINE_I386: 1308237ad974SCharles Davis case llvm::COFF::IMAGE_FILE_MACHINE_POWERPC: 1309237ad974SCharles Davis case llvm::COFF::IMAGE_FILE_MACHINE_POWERPCFP: 1310237ad974SCharles Davis case llvm::COFF::IMAGE_FILE_MACHINE_ARM: 13111108cb36SSaleem Abdulrasool case llvm::COFF::IMAGE_FILE_MACHINE_ARMNT: 1312237ad974SCharles Davis case llvm::COFF::IMAGE_FILE_MACHINE_THUMB: 1313638f072fSMartin Storsjo case llvm::COFF::IMAGE_FILE_MACHINE_ARM64: 1314f760f5aeSPavel Labath ArchSpec arch; 1315fb3b3bd1SZachary Turner arch.SetArchitecture(eArchTypeCOFF, machine, LLDB_INVALID_CPUTYPE, 1316fb3b3bd1SZachary Turner IsWindowsSubsystem() ? llvm::Triple::Win32 1317fb3b3bd1SZachary Turner : llvm::Triple::UnknownOS); 1318f760f5aeSPavel Labath return arch; 1319237ad974SCharles Davis } 1320f760f5aeSPavel Labath return ArchSpec(); 1321f754f88fSGreg Clayton } 1322f754f88fSGreg Clayton 1323b9c1b51eSKate Stone ObjectFile::Type ObjectFilePECOFF::CalculateType() { 1324b9c1b51eSKate Stone if (m_coff_header.machine != 0) { 1325237ad974SCharles Davis if ((m_coff_header.flags & llvm::COFF::IMAGE_FILE_DLL) == 0) 1326f754f88fSGreg Clayton return eTypeExecutable; 1327f754f88fSGreg Clayton else 1328f754f88fSGreg Clayton return eTypeSharedLibrary; 1329f754f88fSGreg Clayton } 1330f754f88fSGreg Clayton return eTypeExecutable; 1331f754f88fSGreg Clayton } 1332f754f88fSGreg Clayton 1333b9c1b51eSKate Stone ObjectFile::Strata ObjectFilePECOFF::CalculateStrata() { return eStrataUser; } 1334