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" 19f754f88fSGreg Clayton #include "lldb/Symbol/ObjectFile.h" 20f7d1893fSAdrian McCarthy #include "lldb/Target/Process.h" 212756adf3SVirgile Bello #include "lldb/Target/SectionLoadList.h" 222756adf3SVirgile Bello #include "lldb/Target/Target.h" 235f19b907SPavel Labath #include "lldb/Utility/ArchSpec.h" 24666cc0b2SZachary Turner #include "lldb/Utility/DataBufferHeap.h" 255713a05bSZachary Turner #include "lldb/Utility/FileSpec.h" 26c34698a8SPavel Labath #include "lldb/Utility/LLDBLog.h" 27037ed1beSAaron Smith #include "lldb/Utility/Log.h" 28bf9a7730SZachary Turner #include "lldb/Utility/StreamString.h" 2938d0632eSPavel Labath #include "lldb/Utility/Timer.h" 30666cc0b2SZachary Turner #include "lldb/Utility/UUID.h" 315f19b907SPavel Labath #include "llvm/BinaryFormat/COFF.h" 32f754f88fSGreg Clayton 33037ed1beSAaron Smith #include "llvm/Object/COFFImportFile.h" 34*c8daf4a7SAlvin Wong #include "llvm/Support/CRC.h" 35037ed1beSAaron Smith #include "llvm/Support/Error.h" 363f4a4b36SZachary Turner #include "llvm/Support/MemoryBuffer.h" 373f4a4b36SZachary Turner 38f754f88fSGreg Clayton #define IMAGE_DOS_SIGNATURE 0x5A4D // MZ 39f754f88fSGreg Clayton #define IMAGE_NT_SIGNATURE 0x00004550 // PE00 40f754f88fSGreg Clayton #define OPT_HEADER_MAGIC_PE32 0x010b 41f754f88fSGreg Clayton #define OPT_HEADER_MAGIC_PE32_PLUS 0x020b 42f754f88fSGreg Clayton 43f754f88fSGreg Clayton using namespace lldb; 44f754f88fSGreg Clayton using namespace lldb_private; 45f754f88fSGreg Clayton 46bba9ba8dSJonas Devlieghere LLDB_PLUGIN_DEFINE(ObjectFilePECOFF) 47fbb4d1e4SJonas Devlieghere 48*c8daf4a7SAlvin Wong static bool GetDebugLinkContents(const llvm::object::COFFObjectFile &coff_obj, 49*c8daf4a7SAlvin Wong std::string &gnu_debuglink_file, 50*c8daf4a7SAlvin Wong uint32_t &gnu_debuglink_crc) { 51*c8daf4a7SAlvin Wong static ConstString g_sect_name_gnu_debuglink(".gnu_debuglink"); 52*c8daf4a7SAlvin Wong for (const auto §ion : coff_obj.sections()) { 53*c8daf4a7SAlvin Wong auto name = section.getName(); 54*c8daf4a7SAlvin Wong if (!name) { 55*c8daf4a7SAlvin Wong llvm::consumeError(name.takeError()); 56*c8daf4a7SAlvin Wong continue; 57*c8daf4a7SAlvin Wong } 58*c8daf4a7SAlvin Wong if (*name == g_sect_name_gnu_debuglink.GetStringRef()) { 59*c8daf4a7SAlvin Wong auto content = section.getContents(); 60*c8daf4a7SAlvin Wong if (!content) { 61*c8daf4a7SAlvin Wong llvm::consumeError(content.takeError()); 62*c8daf4a7SAlvin Wong return false; 63*c8daf4a7SAlvin Wong } 64*c8daf4a7SAlvin Wong DataExtractor data( 65*c8daf4a7SAlvin Wong content->data(), content->size(), 66*c8daf4a7SAlvin Wong coff_obj.isLittleEndian() ? eByteOrderLittle : eByteOrderBig, 4); 67*c8daf4a7SAlvin Wong lldb::offset_t gnu_debuglink_offset = 0; 68*c8daf4a7SAlvin Wong gnu_debuglink_file = data.GetCStr(&gnu_debuglink_offset); 69*c8daf4a7SAlvin Wong // Align to the next 4-byte offset 70*c8daf4a7SAlvin Wong gnu_debuglink_offset = llvm::alignTo(gnu_debuglink_offset, 4); 71*c8daf4a7SAlvin Wong data.GetU32(&gnu_debuglink_offset, &gnu_debuglink_crc, 1); 72*c8daf4a7SAlvin Wong return true; 73*c8daf4a7SAlvin Wong } 74*c8daf4a7SAlvin Wong } 75*c8daf4a7SAlvin Wong return false; 76*c8daf4a7SAlvin Wong } 77*c8daf4a7SAlvin Wong 78d372a8e8SPavel Labath static UUID GetCoffUUID(llvm::object::COFFObjectFile &coff_obj) { 79b8d03935SAaron Smith const llvm::codeview::DebugInfo *pdb_info = nullptr; 80b8d03935SAaron Smith llvm::StringRef pdb_file; 81b8d03935SAaron Smith 82*c8daf4a7SAlvin Wong // First, prefer to use the PDB build id. LLD generates this even for mingw 83*c8daf4a7SAlvin Wong // targets without PDB output, and it does not get stripped either. 84d372a8e8SPavel Labath if (!coff_obj.getDebugPDBInfo(pdb_info, pdb_file) && pdb_info) { 85b8d03935SAaron Smith if (pdb_info->PDB70.CVSignature == llvm::OMF::Signature::PDB70) { 864348e0eeSZequan Wu UUID::CvRecordPdb70 info; 874348e0eeSZequan Wu memcpy(&info.Uuid, pdb_info->PDB70.Signature, sizeof(info.Uuid)); 884348e0eeSZequan Wu info.Age = pdb_info->PDB70.Age; 894348e0eeSZequan Wu return UUID::fromCvRecord(info); 90b8d03935SAaron Smith } 91b8d03935SAaron Smith } 92b8d03935SAaron Smith 93*c8daf4a7SAlvin Wong std::string gnu_debuglink_file; 94*c8daf4a7SAlvin Wong uint32_t gnu_debuglink_crc; 95*c8daf4a7SAlvin Wong 96*c8daf4a7SAlvin Wong // The GNU linker normally does not write a PDB build id (unless requested 97*c8daf4a7SAlvin Wong // with the --build-id option), so we should fall back to using the crc 98*c8daf4a7SAlvin Wong // from .gnu_debuglink if it exists, just like how ObjectFileELF does it. 99*c8daf4a7SAlvin Wong if (!GetDebugLinkContents(coff_obj, gnu_debuglink_file, gnu_debuglink_crc)) { 100*c8daf4a7SAlvin Wong // If there is no .gnu_debuglink section, then this may be an object 101*c8daf4a7SAlvin Wong // containing DWARF debug info for .gnu_debuglink, so calculate the crc of 102*c8daf4a7SAlvin Wong // the object itself. 103*c8daf4a7SAlvin Wong auto raw_data = coff_obj.getData(); 104*c8daf4a7SAlvin Wong LLDB_SCOPED_TIMERF( 105*c8daf4a7SAlvin Wong "Calculating module crc32 %s with size %" PRIu64 " KiB", 106*c8daf4a7SAlvin Wong FileSpec(coff_obj.getFileName()).GetLastPathComponent().AsCString(), 107*c8daf4a7SAlvin Wong static_cast<lldb::offset_t>(raw_data.size()) / 1024); 108*c8daf4a7SAlvin Wong gnu_debuglink_crc = llvm::crc32(0, llvm::arrayRefFromStringRef(raw_data)); 109*c8daf4a7SAlvin Wong } 110*c8daf4a7SAlvin Wong // Use 4 bytes of crc from the .gnu_debuglink section. 111*c8daf4a7SAlvin Wong llvm::support::ulittle32_t data(gnu_debuglink_crc); 112*c8daf4a7SAlvin Wong return UUID::fromData(&data, sizeof(data)); 113b8d03935SAaron Smith } 114b8d03935SAaron Smith 115e84f7841SPavel Labath char ObjectFilePECOFF::ID; 116e84f7841SPavel Labath 117b9c1b51eSKate Stone void ObjectFilePECOFF::Initialize() { 118b9c1b51eSKate Stone PluginManager::RegisterPlugin( 119b9c1b51eSKate Stone GetPluginNameStatic(), GetPluginDescriptionStatic(), CreateInstance, 120b9c1b51eSKate Stone CreateMemoryInstance, GetModuleSpecifications, SaveCore); 121f754f88fSGreg Clayton } 122f754f88fSGreg Clayton 123b9c1b51eSKate Stone void ObjectFilePECOFF::Terminate() { 124f754f88fSGreg Clayton PluginManager::UnregisterPlugin(CreateInstance); 125f754f88fSGreg Clayton } 126f754f88fSGreg Clayton 1272ace1e57SPavel Labath llvm::StringRef ObjectFilePECOFF::GetPluginDescriptionStatic() { 128b9c1b51eSKate Stone return "Portable Executable and Common Object File Format object file reader " 129b9c1b51eSKate Stone "(32 and 64 bit)"; 130f754f88fSGreg Clayton } 131f754f88fSGreg Clayton 132c69307e5SJonas Devlieghere ObjectFile *ObjectFilePECOFF::CreateInstance( 133c69307e5SJonas Devlieghere const lldb::ModuleSP &module_sp, DataBufferSP data_sp, 134c69307e5SJonas Devlieghere lldb::offset_t data_offset, const lldb_private::FileSpec *file_p, 135c69307e5SJonas Devlieghere lldb::offset_t file_offset, lldb::offset_t length) { 13628e4942bSPavel Labath FileSpec file = file_p ? *file_p : FileSpec(); 137b9c1b51eSKate Stone if (!data_sp) { 13850251fc7SPavel Labath data_sp = MapFileData(file, length, file_offset); 1393f4a4b36SZachary Turner if (!data_sp) 1403f4a4b36SZachary Turner return nullptr; 1415ce9c565SGreg Clayton data_offset = 0; 1425ce9c565SGreg Clayton } 1435ce9c565SGreg Clayton 1443f4a4b36SZachary Turner if (!ObjectFilePECOFF::MagicBytesMatch(data_sp)) 1453f4a4b36SZachary Turner return nullptr; 1463f4a4b36SZachary Turner 1475ce9c565SGreg Clayton // Update the data to contain the entire file if it doesn't already 1483f4a4b36SZachary Turner if (data_sp->GetByteSize() < length) { 14950251fc7SPavel Labath data_sp = MapFileData(file, length, file_offset); 1503f4a4b36SZachary Turner if (!data_sp) 1513f4a4b36SZachary Turner return nullptr; 152f754f88fSGreg Clayton } 1533f4a4b36SZachary Turner 154a8f3ae7cSJonas Devlieghere auto objfile_up = std::make_unique<ObjectFilePECOFF>( 15528e4942bSPavel Labath module_sp, data_sp, data_offset, file_p, file_offset, length); 156d5b44036SJonas Devlieghere if (!objfile_up || !objfile_up->ParseHeader()) 1573f4a4b36SZachary Turner return nullptr; 1583f4a4b36SZachary Turner 159037ed1beSAaron Smith // Cache coff binary. 160d5b44036SJonas Devlieghere if (!objfile_up->CreateBinary()) 161037ed1beSAaron Smith return nullptr; 162d5b44036SJonas Devlieghere return objfile_up.release(); 163f754f88fSGreg Clayton } 164f754f88fSGreg Clayton 165b9c1b51eSKate Stone ObjectFile *ObjectFilePECOFF::CreateMemoryInstance( 166f2ea125eSJonas Devlieghere const lldb::ModuleSP &module_sp, lldb::WritableDataBufferSP data_sp, 167b9c1b51eSKate Stone const lldb::ProcessSP &process_sp, lldb::addr_t header_addr) { 168344546bdSWalter Erquinigo if (!data_sp || !ObjectFilePECOFF::MagicBytesMatch(data_sp)) 169344546bdSWalter Erquinigo return nullptr; 170a8f3ae7cSJonas Devlieghere auto objfile_up = std::make_unique<ObjectFilePECOFF>( 171344546bdSWalter Erquinigo module_sp, data_sp, process_sp, header_addr); 172d5b44036SJonas Devlieghere if (objfile_up.get() && objfile_up->ParseHeader()) { 173d5b44036SJonas Devlieghere return objfile_up.release(); 174344546bdSWalter Erquinigo } 175344546bdSWalter Erquinigo return nullptr; 176c9660546SGreg Clayton } 177c9660546SGreg Clayton 178b9c1b51eSKate Stone size_t ObjectFilePECOFF::GetModuleSpecifications( 179b9c1b51eSKate Stone const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp, 180b9c1b51eSKate Stone lldb::offset_t data_offset, lldb::offset_t file_offset, 181b9c1b51eSKate Stone lldb::offset_t length, lldb_private::ModuleSpecList &specs) { 18289eb1baeSVirgile Bello const size_t initial_count = specs.GetSize(); 183b8d03935SAaron Smith if (!data_sp || !ObjectFilePECOFF::MagicBytesMatch(data_sp)) 184b8d03935SAaron Smith return initial_count; 18589eb1baeSVirgile Bello 186a007a6d8SPavel Labath Log *log = GetLog(LLDBLog::Object); 1873db1d138SMartin Storsjö 188a4a00cedSFred Riss if (data_sp->GetByteSize() < length) 189d372a8e8SPavel Labath if (DataBufferSP full_sp = MapFileData(file, -1, file_offset)) 190d372a8e8SPavel Labath data_sp = std::move(full_sp); 191d372a8e8SPavel Labath auto binary = llvm::object::createBinary(llvm::MemoryBufferRef( 192d372a8e8SPavel Labath toStringRef(data_sp->GetData()), file.GetFilename().GetStringRef())); 1933db1d138SMartin Storsjö 1943db1d138SMartin Storsjö if (!binary) { 1953db1d138SMartin Storsjö LLDB_LOG_ERROR(log, binary.takeError(), 1963db1d138SMartin Storsjö "Failed to create binary for file ({1}): {0}", file); 197b8d03935SAaron Smith return initial_count; 1983db1d138SMartin Storsjö } 19989eb1baeSVirgile Bello 200d372a8e8SPavel Labath auto *COFFObj = llvm::dyn_cast<llvm::object::COFFObjectFile>(binary->get()); 201d372a8e8SPavel Labath if (!COFFObj) 202b8d03935SAaron Smith return initial_count; 20389eb1baeSVirgile Bello 204b8d03935SAaron Smith ModuleSpec module_spec(file); 205b8d03935SAaron Smith ArchSpec &spec = module_spec.GetArchitecture(); 206b8d03935SAaron Smith lldb_private::UUID &uuid = module_spec.GetUUID(); 207b8d03935SAaron Smith if (!uuid.IsValid()) 208d372a8e8SPavel Labath uuid = GetCoffUUID(*COFFObj); 209b8d03935SAaron Smith 210b8d03935SAaron Smith switch (COFFObj->getMachine()) { 211b8d03935SAaron Smith case MachineAmd64: 212ad587ae4SZachary Turner spec.SetTriple("x86_64-pc-windows"); 213b8d03935SAaron Smith specs.Append(module_spec); 214b8d03935SAaron Smith break; 215b8d03935SAaron Smith case MachineX86: 216ad587ae4SZachary Turner spec.SetTriple("i386-pc-windows"); 217b8d03935SAaron Smith specs.Append(module_spec); 2185e6f4520SZachary Turner spec.SetTriple("i686-pc-windows"); 219b8d03935SAaron Smith specs.Append(module_spec); 220b8d03935SAaron Smith break; 221b8d03935SAaron Smith case MachineArmNt: 222544c8f48SMartin Storsjo spec.SetTriple("armv7-pc-windows"); 223b8d03935SAaron Smith specs.Append(module_spec); 224b8d03935SAaron Smith break; 225638f072fSMartin Storsjo case MachineArm64: 226674d5543SMartin Storsjo spec.SetTriple("aarch64-pc-windows"); 227638f072fSMartin Storsjo specs.Append(module_spec); 228638f072fSMartin Storsjo break; 229b8d03935SAaron Smith default: 230b8d03935SAaron Smith break; 23189eb1baeSVirgile Bello } 23289eb1baeSVirgile Bello 23389eb1baeSVirgile Bello return specs.GetSize() - initial_count; 234f4d6de6aSGreg Clayton } 235f4d6de6aSGreg Clayton 236b9c1b51eSKate Stone bool ObjectFilePECOFF::SaveCore(const lldb::ProcessSP &process_sp, 237f7d1893fSAdrian McCarthy const lldb_private::FileSpec &outfile, 2389ea6dd5cSJason Molenda lldb::SaveCoreStyle &core_style, 23997206d57SZachary Turner lldb_private::Status &error) { 2409ea6dd5cSJason Molenda core_style = eSaveCoreFull; 241f7d1893fSAdrian McCarthy return SaveMiniDump(process_sp, outfile, error); 242f7d1893fSAdrian McCarthy } 243f7d1893fSAdrian McCarthy 244f2ea125eSJonas Devlieghere bool ObjectFilePECOFF::MagicBytesMatch(DataBufferSP data_sp) { 2455ce9c565SGreg Clayton DataExtractor data(data_sp, eByteOrderLittle, 4); 246c7bece56SGreg Clayton lldb::offset_t offset = 0; 247f754f88fSGreg Clayton uint16_t magic = data.GetU16(&offset); 248f754f88fSGreg Clayton return magic == IMAGE_DOS_SIGNATURE; 249f754f88fSGreg Clayton } 250f754f88fSGreg Clayton 251b9c1b51eSKate Stone lldb::SymbolType ObjectFilePECOFF::MapSymbolType(uint16_t coff_symbol_type) { 252c35b91ceSAdrian McCarthy // TODO: We need to complete this mapping of COFF symbol types to LLDB ones. 253c35b91ceSAdrian McCarthy // For now, here's a hack to make sure our function have types. 254b9c1b51eSKate Stone const auto complex_type = 255b9c1b51eSKate Stone coff_symbol_type >> llvm::COFF::SCT_COMPLEX_TYPE_SHIFT; 256b9c1b51eSKate Stone if (complex_type == llvm::COFF::IMAGE_SYM_DTYPE_FUNCTION) { 257c35b91ceSAdrian McCarthy return lldb::eSymbolTypeCode; 258c35b91ceSAdrian McCarthy } 259c35b91ceSAdrian McCarthy return lldb::eSymbolTypeInvalid; 260c35b91ceSAdrian McCarthy } 261f754f88fSGreg Clayton 262037ed1beSAaron Smith bool ObjectFilePECOFF::CreateBinary() { 263d372a8e8SPavel Labath if (m_binary) 264037ed1beSAaron Smith return true; 265037ed1beSAaron Smith 266a007a6d8SPavel Labath Log *log = GetLog(LLDBLog::Object); 267037ed1beSAaron Smith 268d372a8e8SPavel Labath auto binary = llvm::object::createBinary(llvm::MemoryBufferRef( 269d372a8e8SPavel Labath toStringRef(m_data.GetData()), m_file.GetFilename().GetStringRef())); 270037ed1beSAaron Smith if (!binary) { 2713db1d138SMartin Storsjö LLDB_LOG_ERROR(log, binary.takeError(), 2723db1d138SMartin Storsjö "Failed to create binary for file ({1}): {0}", m_file); 273037ed1beSAaron Smith return false; 274037ed1beSAaron Smith } 275037ed1beSAaron Smith 276037ed1beSAaron Smith // Make sure we only handle COFF format. 277d372a8e8SPavel Labath m_binary = 278d372a8e8SPavel Labath llvm::unique_dyn_cast<llvm::object::COFFObjectFile>(std::move(*binary)); 279d372a8e8SPavel Labath if (!m_binary) 280037ed1beSAaron Smith return false; 281037ed1beSAaron Smith 282d372a8e8SPavel Labath LLDB_LOG(log, "this = {0}, module = {1} ({2}), file = {3}, binary = {4}", 283d372a8e8SPavel Labath this, GetModule().get(), GetModule()->GetSpecificationDescription(), 284d372a8e8SPavel Labath m_file.GetPath(), m_binary.get()); 285037ed1beSAaron Smith return true; 286037ed1beSAaron Smith } 287037ed1beSAaron Smith 288e72dfb32SGreg Clayton ObjectFilePECOFF::ObjectFilePECOFF(const lldb::ModuleSP &module_sp, 289f2ea125eSJonas Devlieghere DataBufferSP data_sp, 2905ce9c565SGreg Clayton lldb::offset_t data_offset, 291f754f88fSGreg Clayton const FileSpec *file, 2925ce9c565SGreg Clayton lldb::offset_t file_offset, 293b9c1b51eSKate Stone lldb::offset_t length) 294b9c1b51eSKate Stone : ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset), 2955c43ffd6SPavel Labath m_dos_header(), m_coff_header(), m_sect_headers(), 296d372a8e8SPavel Labath m_entry_point_address(), m_deps_filespec() { 297f754f88fSGreg Clayton ::memset(&m_dos_header, 0, sizeof(m_dos_header)); 298f754f88fSGreg Clayton ::memset(&m_coff_header, 0, sizeof(m_coff_header)); 299f754f88fSGreg Clayton } 300f754f88fSGreg Clayton 301344546bdSWalter Erquinigo ObjectFilePECOFF::ObjectFilePECOFF(const lldb::ModuleSP &module_sp, 302f2ea125eSJonas Devlieghere WritableDataBufferSP header_data_sp, 303344546bdSWalter Erquinigo const lldb::ProcessSP &process_sp, 304344546bdSWalter Erquinigo addr_t header_addr) 305344546bdSWalter Erquinigo : ObjectFile(module_sp, process_sp, header_addr, header_data_sp), 3065c43ffd6SPavel Labath m_dos_header(), m_coff_header(), m_sect_headers(), 307d372a8e8SPavel Labath m_entry_point_address(), m_deps_filespec() { 308344546bdSWalter Erquinigo ::memset(&m_dos_header, 0, sizeof(m_dos_header)); 309344546bdSWalter Erquinigo ::memset(&m_coff_header, 0, sizeof(m_coff_header)); 310344546bdSWalter Erquinigo } 311344546bdSWalter Erquinigo 312fd2433e1SJonas Devlieghere ObjectFilePECOFF::~ObjectFilePECOFF() = default; 313f754f88fSGreg Clayton 314b9c1b51eSKate Stone bool ObjectFilePECOFF::ParseHeader() { 315a1743499SGreg Clayton ModuleSP module_sp(GetModule()); 316b9c1b51eSKate Stone if (module_sp) { 31716ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 318f754f88fSGreg Clayton m_sect_headers.clear(); 319f754f88fSGreg Clayton m_data.SetByteOrder(eByteOrderLittle); 320c7bece56SGreg Clayton lldb::offset_t offset = 0; 321f754f88fSGreg Clayton 322b9c1b51eSKate Stone if (ParseDOSHeader(m_data, m_dos_header)) { 323f754f88fSGreg Clayton offset = m_dos_header.e_lfanew; 324f754f88fSGreg Clayton uint32_t pe_signature = m_data.GetU32(&offset); 325f754f88fSGreg Clayton if (pe_signature != IMAGE_NT_SIGNATURE) 326f754f88fSGreg Clayton return false; 327b9c1b51eSKate Stone if (ParseCOFFHeader(m_data, &offset, m_coff_header)) { 328f754f88fSGreg Clayton if (m_coff_header.hdrsize > 0) 329f754f88fSGreg Clayton ParseCOFFOptionalHeader(&offset); 330f754f88fSGreg Clayton ParseSectionHeaders(offset); 33128469ca3SGreg Clayton } 332a0f72441SMartin Storsjö m_data.SetAddressByteSize(GetAddressByteSize()); 333f754f88fSGreg Clayton return true; 334f754f88fSGreg Clayton } 335a1743499SGreg Clayton } 336f754f88fSGreg Clayton return false; 337f754f88fSGreg Clayton } 338f754f88fSGreg Clayton 339b9c1b51eSKate Stone bool ObjectFilePECOFF::SetLoadAddress(Target &target, addr_t value, 340b9c1b51eSKate Stone bool value_is_offset) { 3412756adf3SVirgile Bello bool changed = false; 3422756adf3SVirgile Bello ModuleSP module_sp = GetModule(); 343b9c1b51eSKate Stone if (module_sp) { 3442756adf3SVirgile Bello size_t num_loaded_sections = 0; 3452756adf3SVirgile Bello SectionList *section_list = GetSectionList(); 346b9c1b51eSKate Stone if (section_list) { 347b9c1b51eSKate Stone if (!value_is_offset) { 3482756adf3SVirgile Bello value -= m_image_base; 3492756adf3SVirgile Bello } 3502756adf3SVirgile Bello 3512756adf3SVirgile Bello const size_t num_sections = section_list->GetSize(); 3522756adf3SVirgile Bello size_t sect_idx = 0; 3532756adf3SVirgile Bello 354b9c1b51eSKate Stone for (sect_idx = 0; sect_idx < num_sections; ++sect_idx) { 35505097246SAdrian Prantl // Iterate through the object file sections to find all of the sections 35605097246SAdrian Prantl // that have SHF_ALLOC in their flag bits. 3572756adf3SVirgile Bello SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx)); 358b9c1b51eSKate Stone if (section_sp && !section_sp->IsThreadSpecific()) { 359b9c1b51eSKate Stone if (target.GetSectionLoadList().SetSectionLoadAddress( 360b9c1b51eSKate Stone section_sp, section_sp->GetFileAddress() + value)) 3612756adf3SVirgile Bello ++num_loaded_sections; 3622756adf3SVirgile Bello } 3632756adf3SVirgile Bello } 3642756adf3SVirgile Bello changed = num_loaded_sections > 0; 3652756adf3SVirgile Bello } 3662756adf3SVirgile Bello } 3672756adf3SVirgile Bello return changed; 3682756adf3SVirgile Bello } 3692756adf3SVirgile Bello 370b9c1b51eSKate Stone ByteOrder ObjectFilePECOFF::GetByteOrder() const { return eByteOrderLittle; } 371f754f88fSGreg Clayton 372b9c1b51eSKate Stone bool ObjectFilePECOFF::IsExecutable() const { 373237ad974SCharles Davis return (m_coff_header.flags & llvm::COFF::IMAGE_FILE_DLL) == 0; 374f754f88fSGreg Clayton } 375f754f88fSGreg Clayton 376b9c1b51eSKate Stone uint32_t ObjectFilePECOFF::GetAddressByteSize() const { 377f754f88fSGreg Clayton if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32_PLUS) 378f754f88fSGreg Clayton return 8; 379f754f88fSGreg Clayton else if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32) 380f754f88fSGreg Clayton return 4; 381f754f88fSGreg Clayton return 4; 382f754f88fSGreg Clayton } 383f754f88fSGreg Clayton 384f754f88fSGreg Clayton // NeedsEndianSwap 385f754f88fSGreg Clayton // 38605097246SAdrian Prantl // Return true if an endian swap needs to occur when extracting data from this 38705097246SAdrian Prantl // file. 388b9c1b51eSKate Stone bool ObjectFilePECOFF::NeedsEndianSwap() const { 389f754f88fSGreg Clayton #if defined(__LITTLE_ENDIAN__) 390f754f88fSGreg Clayton return false; 391f754f88fSGreg Clayton #else 392f754f88fSGreg Clayton return true; 393f754f88fSGreg Clayton #endif 394f754f88fSGreg Clayton } 395f754f88fSGreg Clayton // ParseDOSHeader 396b9c1b51eSKate Stone bool ObjectFilePECOFF::ParseDOSHeader(DataExtractor &data, 397b9c1b51eSKate Stone dos_header_t &dos_header) { 398f754f88fSGreg Clayton bool success = false; 399c7bece56SGreg Clayton lldb::offset_t offset = 0; 40089eb1baeSVirgile Bello success = data.ValidOffsetForDataOfSize(0, sizeof(dos_header)); 401f754f88fSGreg Clayton 402b9c1b51eSKate Stone if (success) { 40389eb1baeSVirgile Bello dos_header.e_magic = data.GetU16(&offset); // Magic number 40489eb1baeSVirgile Bello success = dos_header.e_magic == IMAGE_DOS_SIGNATURE; 405f754f88fSGreg Clayton 406b9c1b51eSKate Stone if (success) { 40789eb1baeSVirgile Bello dos_header.e_cblp = data.GetU16(&offset); // Bytes on last page of file 40889eb1baeSVirgile Bello dos_header.e_cp = data.GetU16(&offset); // Pages in file 40989eb1baeSVirgile Bello dos_header.e_crlc = data.GetU16(&offset); // Relocations 410b9c1b51eSKate Stone dos_header.e_cparhdr = 411b9c1b51eSKate Stone data.GetU16(&offset); // Size of header in paragraphs 412b9c1b51eSKate Stone dos_header.e_minalloc = 413b9c1b51eSKate Stone data.GetU16(&offset); // Minimum extra paragraphs needed 414b9c1b51eSKate Stone dos_header.e_maxalloc = 415b9c1b51eSKate Stone data.GetU16(&offset); // Maximum extra paragraphs needed 41689eb1baeSVirgile Bello dos_header.e_ss = data.GetU16(&offset); // Initial (relative) SS value 41789eb1baeSVirgile Bello dos_header.e_sp = data.GetU16(&offset); // Initial SP value 41889eb1baeSVirgile Bello dos_header.e_csum = data.GetU16(&offset); // Checksum 41989eb1baeSVirgile Bello dos_header.e_ip = data.GetU16(&offset); // Initial IP value 42089eb1baeSVirgile Bello dos_header.e_cs = data.GetU16(&offset); // Initial (relative) CS value 421b9c1b51eSKate Stone dos_header.e_lfarlc = 422b9c1b51eSKate Stone data.GetU16(&offset); // File address of relocation table 42389eb1baeSVirgile Bello dos_header.e_ovno = data.GetU16(&offset); // Overlay number 424f754f88fSGreg Clayton 42589eb1baeSVirgile Bello dos_header.e_res[0] = data.GetU16(&offset); // Reserved words 42689eb1baeSVirgile Bello dos_header.e_res[1] = data.GetU16(&offset); // Reserved words 42789eb1baeSVirgile Bello dos_header.e_res[2] = data.GetU16(&offset); // Reserved words 42889eb1baeSVirgile Bello dos_header.e_res[3] = data.GetU16(&offset); // Reserved words 429f754f88fSGreg Clayton 430b9c1b51eSKate Stone dos_header.e_oemid = 431b9c1b51eSKate Stone data.GetU16(&offset); // OEM identifier (for e_oeminfo) 432b9c1b51eSKate Stone dos_header.e_oeminfo = 433b9c1b51eSKate Stone data.GetU16(&offset); // OEM information; e_oemid specific 43489eb1baeSVirgile Bello dos_header.e_res2[0] = data.GetU16(&offset); // Reserved words 43589eb1baeSVirgile Bello dos_header.e_res2[1] = data.GetU16(&offset); // Reserved words 43689eb1baeSVirgile Bello dos_header.e_res2[2] = data.GetU16(&offset); // Reserved words 43789eb1baeSVirgile Bello dos_header.e_res2[3] = data.GetU16(&offset); // Reserved words 43889eb1baeSVirgile Bello dos_header.e_res2[4] = data.GetU16(&offset); // Reserved words 43989eb1baeSVirgile Bello dos_header.e_res2[5] = data.GetU16(&offset); // Reserved words 44089eb1baeSVirgile Bello dos_header.e_res2[6] = data.GetU16(&offset); // Reserved words 44189eb1baeSVirgile Bello dos_header.e_res2[7] = data.GetU16(&offset); // Reserved words 44289eb1baeSVirgile Bello dos_header.e_res2[8] = data.GetU16(&offset); // Reserved words 44389eb1baeSVirgile Bello dos_header.e_res2[9] = data.GetU16(&offset); // Reserved words 444f754f88fSGreg Clayton 445b9c1b51eSKate Stone dos_header.e_lfanew = 446b9c1b51eSKate Stone data.GetU32(&offset); // File address of new exe header 447f754f88fSGreg Clayton } 448f754f88fSGreg Clayton } 449f754f88fSGreg Clayton if (!success) 45089eb1baeSVirgile Bello memset(&dos_header, 0, sizeof(dos_header)); 451f754f88fSGreg Clayton return success; 452f754f88fSGreg Clayton } 453f754f88fSGreg Clayton 454f754f88fSGreg Clayton // ParserCOFFHeader 455b9c1b51eSKate Stone bool ObjectFilePECOFF::ParseCOFFHeader(DataExtractor &data, 456b9c1b51eSKate Stone lldb::offset_t *offset_ptr, 457b9c1b51eSKate Stone coff_header_t &coff_header) { 458b9c1b51eSKate Stone bool success = 459b9c1b51eSKate Stone data.ValidOffsetForDataOfSize(*offset_ptr, sizeof(coff_header)); 460b9c1b51eSKate Stone if (success) { 46189eb1baeSVirgile Bello coff_header.machine = data.GetU16(offset_ptr); 46289eb1baeSVirgile Bello coff_header.nsects = data.GetU16(offset_ptr); 46389eb1baeSVirgile Bello coff_header.modtime = data.GetU32(offset_ptr); 46489eb1baeSVirgile Bello coff_header.symoff = data.GetU32(offset_ptr); 46589eb1baeSVirgile Bello coff_header.nsyms = data.GetU32(offset_ptr); 46689eb1baeSVirgile Bello coff_header.hdrsize = data.GetU16(offset_ptr); 46789eb1baeSVirgile Bello coff_header.flags = data.GetU16(offset_ptr); 468f754f88fSGreg Clayton } 469f754f88fSGreg Clayton if (!success) 47089eb1baeSVirgile Bello memset(&coff_header, 0, sizeof(coff_header)); 471f754f88fSGreg Clayton return success; 472f754f88fSGreg Clayton } 473f754f88fSGreg Clayton 474b9c1b51eSKate Stone bool ObjectFilePECOFF::ParseCOFFOptionalHeader(lldb::offset_t *offset_ptr) { 475f754f88fSGreg Clayton bool success = false; 476c7bece56SGreg Clayton const lldb::offset_t end_offset = *offset_ptr + m_coff_header.hdrsize; 477b9c1b51eSKate Stone if (*offset_ptr < end_offset) { 478f754f88fSGreg Clayton success = true; 479f754f88fSGreg Clayton m_coff_header_opt.magic = m_data.GetU16(offset_ptr); 480f754f88fSGreg Clayton m_coff_header_opt.major_linker_version = m_data.GetU8(offset_ptr); 481f754f88fSGreg Clayton m_coff_header_opt.minor_linker_version = m_data.GetU8(offset_ptr); 482f754f88fSGreg Clayton m_coff_header_opt.code_size = m_data.GetU32(offset_ptr); 483f754f88fSGreg Clayton m_coff_header_opt.data_size = m_data.GetU32(offset_ptr); 484f754f88fSGreg Clayton m_coff_header_opt.bss_size = m_data.GetU32(offset_ptr); 485f754f88fSGreg Clayton m_coff_header_opt.entry = m_data.GetU32(offset_ptr); 486f754f88fSGreg Clayton m_coff_header_opt.code_offset = m_data.GetU32(offset_ptr); 487f754f88fSGreg Clayton 488f754f88fSGreg Clayton const uint32_t addr_byte_size = GetAddressByteSize(); 489f754f88fSGreg Clayton 490b9c1b51eSKate Stone if (*offset_ptr < end_offset) { 491b9c1b51eSKate Stone if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32) { 492f754f88fSGreg Clayton // PE32 only 493f754f88fSGreg Clayton m_coff_header_opt.data_offset = m_data.GetU32(offset_ptr); 494b9c1b51eSKate Stone } else 495f754f88fSGreg Clayton m_coff_header_opt.data_offset = 0; 496f754f88fSGreg Clayton 497b9c1b51eSKate Stone if (*offset_ptr < end_offset) { 498b9c1b51eSKate Stone m_coff_header_opt.image_base = 499b9c1b51eSKate Stone m_data.GetMaxU64(offset_ptr, addr_byte_size); 500f754f88fSGreg Clayton m_coff_header_opt.sect_alignment = m_data.GetU32(offset_ptr); 501f754f88fSGreg Clayton m_coff_header_opt.file_alignment = m_data.GetU32(offset_ptr); 502f754f88fSGreg Clayton m_coff_header_opt.major_os_system_version = m_data.GetU16(offset_ptr); 503f754f88fSGreg Clayton m_coff_header_opt.minor_os_system_version = m_data.GetU16(offset_ptr); 504f754f88fSGreg Clayton m_coff_header_opt.major_image_version = m_data.GetU16(offset_ptr); 505f754f88fSGreg Clayton m_coff_header_opt.minor_image_version = m_data.GetU16(offset_ptr); 506f754f88fSGreg Clayton m_coff_header_opt.major_subsystem_version = m_data.GetU16(offset_ptr); 507f754f88fSGreg Clayton m_coff_header_opt.minor_subsystem_version = m_data.GetU16(offset_ptr); 508f754f88fSGreg Clayton m_coff_header_opt.reserved1 = m_data.GetU32(offset_ptr); 509f754f88fSGreg Clayton m_coff_header_opt.image_size = m_data.GetU32(offset_ptr); 510f754f88fSGreg Clayton m_coff_header_opt.header_size = m_data.GetU32(offset_ptr); 51128469ca3SGreg Clayton m_coff_header_opt.checksum = m_data.GetU32(offset_ptr); 512f754f88fSGreg Clayton m_coff_header_opt.subsystem = m_data.GetU16(offset_ptr); 513f754f88fSGreg Clayton m_coff_header_opt.dll_flags = m_data.GetU16(offset_ptr); 514b9c1b51eSKate Stone m_coff_header_opt.stack_reserve_size = 515b9c1b51eSKate Stone m_data.GetMaxU64(offset_ptr, addr_byte_size); 516b9c1b51eSKate Stone m_coff_header_opt.stack_commit_size = 517b9c1b51eSKate Stone m_data.GetMaxU64(offset_ptr, addr_byte_size); 518b9c1b51eSKate Stone m_coff_header_opt.heap_reserve_size = 519b9c1b51eSKate Stone m_data.GetMaxU64(offset_ptr, addr_byte_size); 520b9c1b51eSKate Stone m_coff_header_opt.heap_commit_size = 521b9c1b51eSKate Stone m_data.GetMaxU64(offset_ptr, addr_byte_size); 522f754f88fSGreg Clayton m_coff_header_opt.loader_flags = m_data.GetU32(offset_ptr); 523f754f88fSGreg Clayton uint32_t num_data_dir_entries = m_data.GetU32(offset_ptr); 524f754f88fSGreg Clayton m_coff_header_opt.data_dirs.clear(); 525f754f88fSGreg Clayton m_coff_header_opt.data_dirs.resize(num_data_dir_entries); 526f754f88fSGreg Clayton uint32_t i; 527b9c1b51eSKate Stone for (i = 0; i < num_data_dir_entries; i++) { 528f754f88fSGreg Clayton m_coff_header_opt.data_dirs[i].vmaddr = m_data.GetU32(offset_ptr); 529f754f88fSGreg Clayton m_coff_header_opt.data_dirs[i].vmsize = m_data.GetU32(offset_ptr); 530f754f88fSGreg Clayton } 5312756adf3SVirgile Bello 5322756adf3SVirgile Bello m_image_base = m_coff_header_opt.image_base; 533f754f88fSGreg Clayton } 534f754f88fSGreg Clayton } 535f754f88fSGreg Clayton } 536f754f88fSGreg Clayton // Make sure we are on track for section data which follows 537f754f88fSGreg Clayton *offset_ptr = end_offset; 538f754f88fSGreg Clayton return success; 539f754f88fSGreg Clayton } 540f754f88fSGreg Clayton 54130c2441aSAleksandr Urakov uint32_t ObjectFilePECOFF::GetRVA(const Address &addr) const { 54230c2441aSAleksandr Urakov return addr.GetFileAddress() - m_image_base; 54330c2441aSAleksandr Urakov } 54430c2441aSAleksandr Urakov 54530c2441aSAleksandr Urakov Address ObjectFilePECOFF::GetAddress(uint32_t rva) { 54630c2441aSAleksandr Urakov SectionList *sect_list = GetSectionList(); 54730c2441aSAleksandr Urakov if (!sect_list) 54830c2441aSAleksandr Urakov return Address(GetFileAddress(rva)); 54930c2441aSAleksandr Urakov 55030c2441aSAleksandr Urakov return Address(GetFileAddress(rva), sect_list); 55130c2441aSAleksandr Urakov } 55230c2441aSAleksandr Urakov 55330c2441aSAleksandr Urakov lldb::addr_t ObjectFilePECOFF::GetFileAddress(uint32_t rva) const { 55430c2441aSAleksandr Urakov return m_image_base + rva; 55530c2441aSAleksandr Urakov } 55630c2441aSAleksandr Urakov 557344546bdSWalter Erquinigo DataExtractor ObjectFilePECOFF::ReadImageData(uint32_t offset, size_t size) { 55830c2441aSAleksandr Urakov if (!size) 55930c2441aSAleksandr Urakov return {}; 56030c2441aSAleksandr Urakov 561a4a00cedSFred Riss if (m_data.ValidOffsetForDataOfSize(offset, size)) 562a4a00cedSFred Riss return DataExtractor(m_data, offset, size); 563a4a00cedSFred Riss 564344546bdSWalter Erquinigo ProcessSP process_sp(m_process_wp.lock()); 565344546bdSWalter Erquinigo DataExtractor data; 566344546bdSWalter Erquinigo if (process_sp) { 567a8f3ae7cSJonas Devlieghere auto data_up = std::make_unique<DataBufferHeap>(size, 0); 56897206d57SZachary Turner Status readmem_error; 569344546bdSWalter Erquinigo size_t bytes_read = 570d5b44036SJonas Devlieghere process_sp->ReadMemory(m_image_base + offset, data_up->GetBytes(), 571d5b44036SJonas Devlieghere data_up->GetByteSize(), readmem_error); 572344546bdSWalter Erquinigo if (bytes_read == size) { 573d5b44036SJonas Devlieghere DataBufferSP buffer_sp(data_up.release()); 574344546bdSWalter Erquinigo data.SetData(buffer_sp, 0, buffer_sp->GetByteSize()); 575344546bdSWalter Erquinigo } 576344546bdSWalter Erquinigo } 577344546bdSWalter Erquinigo return data; 578344546bdSWalter Erquinigo } 579344546bdSWalter Erquinigo 58030c2441aSAleksandr Urakov DataExtractor ObjectFilePECOFF::ReadImageDataByRVA(uint32_t rva, size_t size) { 58130c2441aSAleksandr Urakov Address addr = GetAddress(rva); 5827e1a3076SMartin Storsjö SectionSP sect = addr.GetSection(); 5837e1a3076SMartin Storsjö if (!sect) 5847e1a3076SMartin Storsjö return {}; 5857e1a3076SMartin Storsjö rva = sect->GetFileOffset() + addr.GetOffset(); 58630c2441aSAleksandr Urakov 58730c2441aSAleksandr Urakov return ReadImageData(rva, size); 58830c2441aSAleksandr Urakov } 58930c2441aSAleksandr Urakov 590f754f88fSGreg Clayton // ParseSectionHeaders 591b9c1b51eSKate Stone bool ObjectFilePECOFF::ParseSectionHeaders( 592b9c1b51eSKate Stone uint32_t section_header_data_offset) { 593f754f88fSGreg Clayton const uint32_t nsects = m_coff_header.nsects; 594f754f88fSGreg Clayton m_sect_headers.clear(); 595f754f88fSGreg Clayton 596b9c1b51eSKate Stone if (nsects > 0) { 597f754f88fSGreg Clayton const size_t section_header_byte_size = nsects * sizeof(section_header_t); 598344546bdSWalter Erquinigo DataExtractor section_header_data = 599344546bdSWalter Erquinigo ReadImageData(section_header_data_offset, section_header_byte_size); 600f754f88fSGreg Clayton 601c7bece56SGreg Clayton lldb::offset_t offset = 0; 602b9c1b51eSKate Stone if (section_header_data.ValidOffsetForDataOfSize( 603b9c1b51eSKate Stone offset, section_header_byte_size)) { 604f754f88fSGreg Clayton m_sect_headers.resize(nsects); 605f754f88fSGreg Clayton 606b9c1b51eSKate Stone for (uint32_t idx = 0; idx < nsects; ++idx) { 607f754f88fSGreg Clayton const void *name_data = section_header_data.GetData(&offset, 8); 608b9c1b51eSKate Stone if (name_data) { 609f754f88fSGreg Clayton memcpy(m_sect_headers[idx].name, name_data, 8); 610f754f88fSGreg Clayton m_sect_headers[idx].vmsize = section_header_data.GetU32(&offset); 611f754f88fSGreg Clayton m_sect_headers[idx].vmaddr = section_header_data.GetU32(&offset); 612f754f88fSGreg Clayton m_sect_headers[idx].size = section_header_data.GetU32(&offset); 613f754f88fSGreg Clayton m_sect_headers[idx].offset = section_header_data.GetU32(&offset); 614f754f88fSGreg Clayton m_sect_headers[idx].reloff = section_header_data.GetU32(&offset); 615f754f88fSGreg Clayton m_sect_headers[idx].lineoff = section_header_data.GetU32(&offset); 616f754f88fSGreg Clayton m_sect_headers[idx].nreloc = section_header_data.GetU16(&offset); 617f754f88fSGreg Clayton m_sect_headers[idx].nline = section_header_data.GetU16(&offset); 618f754f88fSGreg Clayton m_sect_headers[idx].flags = section_header_data.GetU32(&offset); 619f754f88fSGreg Clayton } 620f754f88fSGreg Clayton } 621f754f88fSGreg Clayton } 622f754f88fSGreg Clayton } 623f754f88fSGreg Clayton 624a6682a41SJonas Devlieghere return !m_sect_headers.empty(); 625f754f88fSGreg Clayton } 626f754f88fSGreg Clayton 6272886e4a0SPavel Labath llvm::StringRef ObjectFilePECOFF::GetSectionName(const section_header_t §) { 628f15014ffSBenjamin Kramer llvm::StringRef hdr_name(sect.name, llvm::array_lengthof(sect.name)); 6292886e4a0SPavel Labath hdr_name = hdr_name.split('\0').first; 6302886e4a0SPavel Labath if (hdr_name.consume_front("/")) { 6312886e4a0SPavel Labath lldb::offset_t stroff; 6322886e4a0SPavel Labath if (!to_integer(hdr_name, stroff, 10)) 6332886e4a0SPavel Labath return ""; 634b9c1b51eSKate Stone lldb::offset_t string_file_offset = 635b9c1b51eSKate Stone m_coff_header.symoff + (m_coff_header.nsyms * 18) + stroff; 6362886e4a0SPavel Labath if (const char *name = m_data.GetCStr(&string_file_offset)) 6372886e4a0SPavel Labath return name; 6382886e4a0SPavel Labath return ""; 639f754f88fSGreg Clayton } 6402886e4a0SPavel Labath return hdr_name; 641f754f88fSGreg Clayton } 642f754f88fSGreg Clayton 6437e6df41fSGreg Clayton void ObjectFilePECOFF::ParseSymtab(Symtab &symtab) { 644f754f88fSGreg Clayton SectionList *sect_list = GetSectionList(); 64528469ca3SGreg Clayton const uint32_t num_syms = m_coff_header.nsyms; 646344546bdSWalter Erquinigo if (m_file && num_syms > 0 && m_coff_header.symoff > 0) { 6470076e715SGreg Clayton const uint32_t symbol_size = 18; 64828469ca3SGreg Clayton const size_t symbol_data_size = num_syms * symbol_size; 649c35b91ceSAdrian McCarthy // Include the 4-byte string table size at the end of the symbols 650344546bdSWalter Erquinigo DataExtractor symtab_data = 651344546bdSWalter Erquinigo ReadImageData(m_coff_header.symoff, symbol_data_size + 4); 652c7bece56SGreg Clayton lldb::offset_t offset = symbol_data_size; 65328469ca3SGreg Clayton const uint32_t strtab_size = symtab_data.GetU32(&offset); 654344546bdSWalter Erquinigo if (strtab_size > 0) { 655344546bdSWalter Erquinigo DataExtractor strtab_data = ReadImageData( 656344546bdSWalter Erquinigo m_coff_header.symoff + symbol_data_size, strtab_size); 65728469ca3SGreg Clayton 65828469ca3SGreg Clayton offset = 0; 65928469ca3SGreg Clayton std::string symbol_name; 6607e6df41fSGreg Clayton Symbol *symbols = symtab.Resize(num_syms); 661b9c1b51eSKate Stone for (uint32_t i = 0; i < num_syms; ++i) { 662f754f88fSGreg Clayton coff_symbol_t symbol; 66328469ca3SGreg Clayton const uint32_t symbol_offset = offset; 664248a1305SKonrad Kleine const char *symbol_name_cstr = nullptr; 665c35b91ceSAdrian McCarthy // If the first 4 bytes of the symbol string are zero, then they 666c35b91ceSAdrian McCarthy // are followed by a 4-byte string table offset. Else these 66728469ca3SGreg Clayton // 8 bytes contain the symbol name 668b9c1b51eSKate Stone if (symtab_data.GetU32(&offset) == 0) { 66905097246SAdrian Prantl // Long string that doesn't fit into the symbol table name, so 67005097246SAdrian Prantl // now we must read the 4 byte string table offset 67128469ca3SGreg Clayton uint32_t strtab_offset = symtab_data.GetU32(&offset); 67228469ca3SGreg Clayton symbol_name_cstr = strtab_data.PeekCStr(strtab_offset); 67328469ca3SGreg Clayton symbol_name.assign(symbol_name_cstr); 674b9c1b51eSKate Stone } else { 675b9c1b51eSKate Stone // Short string that fits into the symbol table name which is 8 676b9c1b51eSKate Stone // bytes 67728469ca3SGreg Clayton offset += sizeof(symbol.name) - 4; // Skip remaining 67828469ca3SGreg Clayton symbol_name_cstr = symtab_data.PeekCStr(symbol_offset); 679248a1305SKonrad Kleine if (symbol_name_cstr == nullptr) 680f754f88fSGreg Clayton break; 68128469ca3SGreg Clayton symbol_name.assign(symbol_name_cstr, sizeof(symbol.name)); 68228469ca3SGreg Clayton } 68328469ca3SGreg Clayton symbol.value = symtab_data.GetU32(&offset); 68428469ca3SGreg Clayton symbol.sect = symtab_data.GetU16(&offset); 68528469ca3SGreg Clayton symbol.type = symtab_data.GetU16(&offset); 68628469ca3SGreg Clayton symbol.storage = symtab_data.GetU8(&offset); 68728469ca3SGreg Clayton symbol.naux = symtab_data.GetU8(&offset); 688037520e9SGreg Clayton symbols[i].GetMangled().SetValue(ConstString(symbol_name.c_str())); 689b9c1b51eSKate Stone if ((int16_t)symbol.sect >= 1) { 6904394b5beSMartin Storsjö Address symbol_addr(sect_list->FindSectionByID(symbol.sect), 691b9c1b51eSKate Stone symbol.value); 692358cf1eaSGreg Clayton symbols[i].GetAddressRef() = symbol_addr; 693c35b91ceSAdrian McCarthy symbols[i].SetType(MapSymbolType(symbol.type)); 6940076e715SGreg Clayton } 695f754f88fSGreg Clayton 696b9c1b51eSKate Stone if (symbol.naux > 0) { 697f754f88fSGreg Clayton i += symbol.naux; 698f07ddbc9SMartin Storsjö offset += symbol.naux * symbol_size; 6990076e715SGreg Clayton } 700f754f88fSGreg Clayton } 701f754f88fSGreg Clayton } 702344546bdSWalter Erquinigo } 703a4fe3a12SVirgile Bello 704a4fe3a12SVirgile Bello // Read export header 705b9c1b51eSKate Stone if (coff_data_dir_export_table < m_coff_header_opt.data_dirs.size() && 706b9c1b51eSKate Stone m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmsize > 0 && 707b9c1b51eSKate Stone m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmaddr > 0) { 708a4fe3a12SVirgile Bello export_directory_entry export_table; 709b9c1b51eSKate Stone uint32_t data_start = 710b9c1b51eSKate Stone m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmaddr; 711344546bdSWalter Erquinigo 71230c2441aSAleksandr Urakov DataExtractor symtab_data = ReadImageDataByRVA( 71330c2441aSAleksandr Urakov data_start, m_coff_header_opt.data_dirs[0].vmsize); 714a4fe3a12SVirgile Bello lldb::offset_t offset = 0; 715a4fe3a12SVirgile Bello 716a4fe3a12SVirgile Bello // Read export_table header 717a4fe3a12SVirgile Bello export_table.characteristics = symtab_data.GetU32(&offset); 718a4fe3a12SVirgile Bello export_table.time_date_stamp = symtab_data.GetU32(&offset); 719a4fe3a12SVirgile Bello export_table.major_version = symtab_data.GetU16(&offset); 720a4fe3a12SVirgile Bello export_table.minor_version = symtab_data.GetU16(&offset); 721a4fe3a12SVirgile Bello export_table.name = symtab_data.GetU32(&offset); 722a4fe3a12SVirgile Bello export_table.base = symtab_data.GetU32(&offset); 723a4fe3a12SVirgile Bello export_table.number_of_functions = symtab_data.GetU32(&offset); 724a4fe3a12SVirgile Bello export_table.number_of_names = symtab_data.GetU32(&offset); 725a4fe3a12SVirgile Bello export_table.address_of_functions = symtab_data.GetU32(&offset); 726a4fe3a12SVirgile Bello export_table.address_of_names = symtab_data.GetU32(&offset); 727a4fe3a12SVirgile Bello export_table.address_of_name_ordinals = symtab_data.GetU32(&offset); 728a4fe3a12SVirgile Bello 729a4fe3a12SVirgile Bello bool has_ordinal = export_table.address_of_name_ordinals != 0; 730a4fe3a12SVirgile Bello 731a4fe3a12SVirgile Bello lldb::offset_t name_offset = export_table.address_of_names - data_start; 732b9c1b51eSKate Stone lldb::offset_t name_ordinal_offset = 733b9c1b51eSKate Stone export_table.address_of_name_ordinals - data_start; 734a4fe3a12SVirgile Bello 7357e6df41fSGreg Clayton Symbol *symbols = symtab.Resize(export_table.number_of_names); 736a4fe3a12SVirgile Bello 737a4fe3a12SVirgile Bello std::string symbol_name; 738a4fe3a12SVirgile Bello 739a4fe3a12SVirgile Bello // Read each export table entry 740b9c1b51eSKate Stone for (size_t i = 0; i < export_table.number_of_names; ++i) { 741b9c1b51eSKate Stone uint32_t name_ordinal = 742b9c1b51eSKate Stone has_ordinal ? symtab_data.GetU16(&name_ordinal_offset) : i; 743a4fe3a12SVirgile Bello uint32_t name_address = symtab_data.GetU32(&name_offset); 744a4fe3a12SVirgile Bello 745b9c1b51eSKate Stone const char *symbol_name_cstr = 746b9c1b51eSKate Stone symtab_data.PeekCStr(name_address - data_start); 747a4fe3a12SVirgile Bello symbol_name.assign(symbol_name_cstr); 748a4fe3a12SVirgile Bello 749b9c1b51eSKate Stone lldb::offset_t function_offset = export_table.address_of_functions - 750b9c1b51eSKate Stone data_start + 751b9c1b51eSKate Stone sizeof(uint32_t) * name_ordinal; 752a4fe3a12SVirgile Bello uint32_t function_rva = symtab_data.GetU32(&function_offset); 753a4fe3a12SVirgile Bello 754b9c1b51eSKate Stone Address symbol_addr(m_coff_header_opt.image_base + function_rva, 755b9c1b51eSKate Stone sect_list); 756a4fe3a12SVirgile Bello symbols[i].GetMangled().SetValue(ConstString(symbol_name.c_str())); 757358cf1eaSGreg Clayton symbols[i].GetAddressRef() = symbol_addr; 758a4fe3a12SVirgile Bello symbols[i].SetType(lldb::eSymbolTypeCode); 759a4fe3a12SVirgile Bello symbols[i].SetDebug(true); 760a4fe3a12SVirgile Bello } 761a4fe3a12SVirgile Bello } 762f754f88fSGreg Clayton } 763f754f88fSGreg Clayton 76430c2441aSAleksandr Urakov std::unique_ptr<CallFrameInfo> ObjectFilePECOFF::CreateCallFrameInfo() { 76530c2441aSAleksandr Urakov if (coff_data_dir_exception_table >= m_coff_header_opt.data_dirs.size()) 76630c2441aSAleksandr Urakov return {}; 76730c2441aSAleksandr Urakov 76830c2441aSAleksandr Urakov data_directory data_dir_exception = 76930c2441aSAleksandr Urakov m_coff_header_opt.data_dirs[coff_data_dir_exception_table]; 77030c2441aSAleksandr Urakov if (!data_dir_exception.vmaddr) 77130c2441aSAleksandr Urakov return {}; 77230c2441aSAleksandr Urakov 773aa786b88SMartin Storsjö if (m_coff_header.machine != llvm::COFF::IMAGE_FILE_MACHINE_AMD64) 774aa786b88SMartin Storsjö return {}; 775aa786b88SMartin Storsjö 77630c2441aSAleksandr Urakov return std::make_unique<PECallFrameInfo>(*this, data_dir_exception.vmaddr, 77730c2441aSAleksandr Urakov data_dir_exception.vmsize); 77830c2441aSAleksandr Urakov } 77930c2441aSAleksandr Urakov 780b9c1b51eSKate Stone bool ObjectFilePECOFF::IsStripped() { 7813046e668SGreg Clayton // TODO: determine this for COFF 7823046e668SGreg Clayton return false; 7833046e668SGreg Clayton } 7843046e668SGreg Clayton 7852e5bb6d8SMartin Storsjö SectionType ObjectFilePECOFF::GetSectionType(llvm::StringRef sect_name, 7862e5bb6d8SMartin Storsjö const section_header_t §) { 7872e5bb6d8SMartin Storsjö ConstString const_sect_name(sect_name); 7882e5bb6d8SMartin Storsjö static ConstString g_code_sect_name(".code"); 7892e5bb6d8SMartin Storsjö static ConstString g_CODE_sect_name("CODE"); 7902e5bb6d8SMartin Storsjö static ConstString g_data_sect_name(".data"); 7912e5bb6d8SMartin Storsjö static ConstString g_DATA_sect_name("DATA"); 7922e5bb6d8SMartin Storsjö static ConstString g_bss_sect_name(".bss"); 7932e5bb6d8SMartin Storsjö static ConstString g_BSS_sect_name("BSS"); 7942e5bb6d8SMartin Storsjö 7952e5bb6d8SMartin Storsjö if (sect.flags & llvm::COFF::IMAGE_SCN_CNT_CODE && 7962e5bb6d8SMartin Storsjö ((const_sect_name == g_code_sect_name) || 7972e5bb6d8SMartin Storsjö (const_sect_name == g_CODE_sect_name))) { 7982e5bb6d8SMartin Storsjö return eSectionTypeCode; 7992e5bb6d8SMartin Storsjö } 8002e5bb6d8SMartin Storsjö if (sect.flags & llvm::COFF::IMAGE_SCN_CNT_INITIALIZED_DATA && 8012e5bb6d8SMartin Storsjö ((const_sect_name == g_data_sect_name) || 8022e5bb6d8SMartin Storsjö (const_sect_name == g_DATA_sect_name))) { 8032e5bb6d8SMartin Storsjö if (sect.size == 0 && sect.offset == 0) 8042e5bb6d8SMartin Storsjö return eSectionTypeZeroFill; 8052e5bb6d8SMartin Storsjö else 8062e5bb6d8SMartin Storsjö return eSectionTypeData; 8072e5bb6d8SMartin Storsjö } 8082e5bb6d8SMartin Storsjö if (sect.flags & llvm::COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA && 8092e5bb6d8SMartin Storsjö ((const_sect_name == g_bss_sect_name) || 8102e5bb6d8SMartin Storsjö (const_sect_name == g_BSS_sect_name))) { 8112e5bb6d8SMartin Storsjö if (sect.size == 0) 8122e5bb6d8SMartin Storsjö return eSectionTypeZeroFill; 8132e5bb6d8SMartin Storsjö else 8142e5bb6d8SMartin Storsjö return eSectionTypeData; 8152e5bb6d8SMartin Storsjö } 8162e5bb6d8SMartin Storsjö 8172e5bb6d8SMartin Storsjö SectionType section_type = 8182e5bb6d8SMartin Storsjö llvm::StringSwitch<SectionType>(sect_name) 8192e5bb6d8SMartin Storsjö .Case(".debug", eSectionTypeDebug) 8202e5bb6d8SMartin Storsjö .Case(".stabstr", eSectionTypeDataCString) 8212e5bb6d8SMartin Storsjö .Case(".reloc", eSectionTypeOther) 8222e5bb6d8SMartin Storsjö .Case(".debug_abbrev", eSectionTypeDWARFDebugAbbrev) 8232e5bb6d8SMartin Storsjö .Case(".debug_aranges", eSectionTypeDWARFDebugAranges) 8242e5bb6d8SMartin Storsjö .Case(".debug_frame", eSectionTypeDWARFDebugFrame) 8252e5bb6d8SMartin Storsjö .Case(".debug_info", eSectionTypeDWARFDebugInfo) 8262e5bb6d8SMartin Storsjö .Case(".debug_line", eSectionTypeDWARFDebugLine) 8272e5bb6d8SMartin Storsjö .Case(".debug_loc", eSectionTypeDWARFDebugLoc) 8282e5bb6d8SMartin Storsjö .Case(".debug_loclists", eSectionTypeDWARFDebugLocLists) 8292e5bb6d8SMartin Storsjö .Case(".debug_macinfo", eSectionTypeDWARFDebugMacInfo) 8302e5bb6d8SMartin Storsjö .Case(".debug_names", eSectionTypeDWARFDebugNames) 8312e5bb6d8SMartin Storsjö .Case(".debug_pubnames", eSectionTypeDWARFDebugPubNames) 8322e5bb6d8SMartin Storsjö .Case(".debug_pubtypes", eSectionTypeDWARFDebugPubTypes) 8332e5bb6d8SMartin Storsjö .Case(".debug_ranges", eSectionTypeDWARFDebugRanges) 8342e5bb6d8SMartin Storsjö .Case(".debug_str", eSectionTypeDWARFDebugStr) 8352e5bb6d8SMartin Storsjö .Case(".debug_types", eSectionTypeDWARFDebugTypes) 836934c025eSMartin Storsjö // .eh_frame can be truncated to 8 chars. 837934c025eSMartin Storsjö .Cases(".eh_frame", ".eh_fram", eSectionTypeEHFrame) 8382e5bb6d8SMartin Storsjö .Case(".gosymtab", eSectionTypeGoSymtab) 8392e5bb6d8SMartin Storsjö .Default(eSectionTypeInvalid); 8402e5bb6d8SMartin Storsjö if (section_type != eSectionTypeInvalid) 8412e5bb6d8SMartin Storsjö return section_type; 8422e5bb6d8SMartin Storsjö 8432e5bb6d8SMartin Storsjö if (sect.flags & llvm::COFF::IMAGE_SCN_CNT_CODE) 8442e5bb6d8SMartin Storsjö return eSectionTypeCode; 8452e5bb6d8SMartin Storsjö if (sect.flags & llvm::COFF::IMAGE_SCN_CNT_INITIALIZED_DATA) 8462e5bb6d8SMartin Storsjö return eSectionTypeData; 8472e5bb6d8SMartin Storsjö if (sect.flags & llvm::COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) { 8482e5bb6d8SMartin Storsjö if (sect.size == 0) 8492e5bb6d8SMartin Storsjö return eSectionTypeZeroFill; 8502e5bb6d8SMartin Storsjö else 8512e5bb6d8SMartin Storsjö return eSectionTypeData; 8522e5bb6d8SMartin Storsjö } 8532e5bb6d8SMartin Storsjö return eSectionTypeOther; 8542e5bb6d8SMartin Storsjö } 8552e5bb6d8SMartin Storsjö 856b9c1b51eSKate Stone void ObjectFilePECOFF::CreateSections(SectionList &unified_section_list) { 857d5b44036SJonas Devlieghere if (m_sections_up) 85888a2c2a4SPavel Labath return; 85906412daeSJonas Devlieghere m_sections_up = std::make_unique<SectionList>(); 860a1743499SGreg Clayton ModuleSP module_sp(GetModule()); 861b9c1b51eSKate Stone if (module_sp) { 86216ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 8637db8b5c4SPavel Labath 86473a7a55cSPavel Labath SectionSP header_sp = std::make_shared<Section>( 86573a7a55cSPavel Labath module_sp, this, ~user_id_t(0), ConstString("PECOFF header"), 86673a7a55cSPavel Labath eSectionTypeOther, m_coff_header_opt.image_base, 86773a7a55cSPavel Labath m_coff_header_opt.header_size, 86873a7a55cSPavel Labath /*file_offset*/ 0, m_coff_header_opt.header_size, 86973a7a55cSPavel Labath m_coff_header_opt.sect_alignment, 8707db8b5c4SPavel Labath /*flags*/ 0); 871f1e0ae34SPavel Labath header_sp->SetPermissions(ePermissionsReadable); 87273a7a55cSPavel Labath m_sections_up->AddSection(header_sp); 87373a7a55cSPavel Labath unified_section_list.AddSection(header_sp); 8747db8b5c4SPavel Labath 875f754f88fSGreg Clayton const uint32_t nsects = m_sect_headers.size(); 876e72dfb32SGreg Clayton ModuleSP module_sp(GetModule()); 877b9c1b51eSKate Stone for (uint32_t idx = 0; idx < nsects; ++idx) { 8782e5bb6d8SMartin Storsjö llvm::StringRef sect_name = GetSectionName(m_sect_headers[idx]); 8792e5bb6d8SMartin Storsjö ConstString const_sect_name(sect_name); 8802e5bb6d8SMartin Storsjö SectionType section_type = GetSectionType(sect_name, m_sect_headers[idx]); 881f754f88fSGreg Clayton 882b9c1b51eSKate Stone SectionSP section_sp(new Section( 883b9c1b51eSKate Stone module_sp, // Module to which this section belongs 884a7499c98SMichael Sartain this, // Object file to which this section belongs 8857db8b5c4SPavel Labath idx + 1, // Section ID is the 1 based section index. 886f754f88fSGreg Clayton const_sect_name, // Name of this section 8877db8b5c4SPavel Labath section_type, 88873a7a55cSPavel Labath m_coff_header_opt.image_base + 889b9c1b51eSKate Stone m_sect_headers[idx].vmaddr, // File VM address == addresses as 890b9c1b51eSKate Stone // they are found in the object file 891f754f88fSGreg Clayton m_sect_headers[idx].vmsize, // VM size in bytes of this section 892b9c1b51eSKate Stone m_sect_headers[idx] 893b9c1b51eSKate Stone .offset, // Offset to the data for this section in the file 894b9c1b51eSKate Stone m_sect_headers[idx] 895b9c1b51eSKate Stone .size, // Size in bytes of this section as found in the file 89648672afbSGreg Clayton m_coff_header_opt.sect_alignment, // Section alignment 897f754f88fSGreg Clayton m_sect_headers[idx].flags)); // Flags for this section 898f754f88fSGreg Clayton 899f1e0ae34SPavel Labath uint32_t permissions = 0; 900f1e0ae34SPavel Labath if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_MEM_EXECUTE) 901f1e0ae34SPavel Labath permissions |= ePermissionsExecutable; 902f1e0ae34SPavel Labath if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_MEM_READ) 903f1e0ae34SPavel Labath permissions |= ePermissionsReadable; 904f1e0ae34SPavel Labath if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_MEM_WRITE) 905f1e0ae34SPavel Labath permissions |= ePermissionsWritable; 906f1e0ae34SPavel Labath section_sp->SetPermissions(permissions); 907f1e0ae34SPavel Labath 90873a7a55cSPavel Labath m_sections_up->AddSection(section_sp); 90973a7a55cSPavel Labath unified_section_list.AddSection(section_sp); 910f754f88fSGreg Clayton } 911f754f88fSGreg Clayton } 912a1743499SGreg Clayton } 913f754f88fSGreg Clayton 914b8d03935SAaron Smith UUID ObjectFilePECOFF::GetUUID() { 915b8d03935SAaron Smith if (m_uuid.IsValid()) 916b8d03935SAaron Smith return m_uuid; 917b8d03935SAaron Smith 918b8d03935SAaron Smith if (!CreateBinary()) 919b8d03935SAaron Smith return UUID(); 920b8d03935SAaron Smith 921d372a8e8SPavel Labath m_uuid = GetCoffUUID(*m_binary); 922b8d03935SAaron Smith return m_uuid; 923b8d03935SAaron Smith } 924f754f88fSGreg Clayton 925*c8daf4a7SAlvin Wong llvm::Optional<FileSpec> ObjectFilePECOFF::GetDebugLink() { 926*c8daf4a7SAlvin Wong std::string gnu_debuglink_file; 927*c8daf4a7SAlvin Wong uint32_t gnu_debuglink_crc; 928*c8daf4a7SAlvin Wong if (GetDebugLinkContents(*m_binary, gnu_debuglink_file, gnu_debuglink_crc)) 929*c8daf4a7SAlvin Wong return FileSpec(gnu_debuglink_file); 930*c8daf4a7SAlvin Wong return llvm::None; 931*c8daf4a7SAlvin Wong } 932*c8daf4a7SAlvin Wong 933037ed1beSAaron Smith uint32_t ObjectFilePECOFF::ParseDependentModules() { 934037ed1beSAaron Smith ModuleSP module_sp(GetModule()); 935037ed1beSAaron Smith if (!module_sp) 936f754f88fSGreg Clayton return 0; 937037ed1beSAaron Smith 938037ed1beSAaron Smith std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 939037ed1beSAaron Smith if (m_deps_filespec) 940037ed1beSAaron Smith return m_deps_filespec->GetSize(); 941037ed1beSAaron Smith 942037ed1beSAaron Smith // Cache coff binary if it is not done yet. 943037ed1beSAaron Smith if (!CreateBinary()) 944037ed1beSAaron Smith return 0; 945037ed1beSAaron Smith 946a007a6d8SPavel Labath Log *log = GetLog(LLDBLog::Object); 947d372a8e8SPavel Labath LLDB_LOG(log, "this = {0}, module = {1} ({2}), file = {3}, binary = {4}", 948d372a8e8SPavel Labath this, GetModule().get(), GetModule()->GetSpecificationDescription(), 949d372a8e8SPavel Labath m_file.GetPath(), m_binary.get()); 950037ed1beSAaron Smith 951037ed1beSAaron Smith m_deps_filespec = FileSpecList(); 952037ed1beSAaron Smith 953d372a8e8SPavel Labath for (const auto &entry : m_binary->import_directories()) { 954037ed1beSAaron Smith llvm::StringRef dll_name; 955037ed1beSAaron Smith // Report a bogus entry. 9561c03389cSReid Kleckner if (llvm::Error e = entry.getName(dll_name)) { 95763e5fb76SJonas Devlieghere LLDB_LOGF(log, 95863e5fb76SJonas Devlieghere "ObjectFilePECOFF::ParseDependentModules() - failed to get " 959037ed1beSAaron Smith "import directory entry name: %s", 9601c03389cSReid Kleckner llvm::toString(std::move(e)).c_str()); 961037ed1beSAaron Smith continue; 962037ed1beSAaron Smith } 963037ed1beSAaron Smith 964037ed1beSAaron Smith // At this moment we only have the base name of the DLL. The full path can 965037ed1beSAaron Smith // only be seen after the dynamic loading. Our best guess is Try to get it 966037ed1beSAaron Smith // with the help of the object file's directory. 967b3f44ad9SStella Stamenova llvm::SmallString<128> dll_fullpath; 968037ed1beSAaron Smith FileSpec dll_specs(dll_name); 969037ed1beSAaron Smith dll_specs.GetDirectory().SetString(m_file.GetDirectory().GetCString()); 970037ed1beSAaron Smith 971037ed1beSAaron Smith if (!llvm::sys::fs::real_path(dll_specs.GetPath(), dll_fullpath)) 972f893d5bfSJonas Devlieghere m_deps_filespec->EmplaceBack(dll_fullpath); 973037ed1beSAaron Smith else { 974037ed1beSAaron Smith // Known DLLs or DLL not found in the object file directory. 975f893d5bfSJonas Devlieghere m_deps_filespec->EmplaceBack(dll_name); 976037ed1beSAaron Smith } 977037ed1beSAaron Smith } 978037ed1beSAaron Smith return m_deps_filespec->GetSize(); 979037ed1beSAaron Smith } 980037ed1beSAaron Smith 981037ed1beSAaron Smith uint32_t ObjectFilePECOFF::GetDependentModules(FileSpecList &files) { 982037ed1beSAaron Smith auto num_modules = ParseDependentModules(); 983037ed1beSAaron Smith auto original_size = files.GetSize(); 984037ed1beSAaron Smith 985037ed1beSAaron Smith for (unsigned i = 0; i < num_modules; ++i) 986037ed1beSAaron Smith files.AppendIfUnique(m_deps_filespec->GetFileSpecAtIndex(i)); 987037ed1beSAaron Smith 988037ed1beSAaron Smith return files.GetSize() - original_size; 989f754f88fSGreg Clayton } 990f754f88fSGreg Clayton 991b9c1b51eSKate Stone lldb_private::Address ObjectFilePECOFF::GetEntryPointAddress() { 9928e38c666SStephane Sezer if (m_entry_point_address.IsValid()) 9938e38c666SStephane Sezer return m_entry_point_address; 9948e38c666SStephane Sezer 9958e38c666SStephane Sezer if (!ParseHeader() || !IsExecutable()) 9968e38c666SStephane Sezer return m_entry_point_address; 9978e38c666SStephane Sezer 9988e38c666SStephane Sezer SectionList *section_list = GetSectionList(); 999a5235af9SAleksandr Urakov addr_t file_addr = m_coff_header_opt.entry + m_coff_header_opt.image_base; 10008e38c666SStephane Sezer 10018e38c666SStephane Sezer if (!section_list) 1002a5235af9SAleksandr Urakov m_entry_point_address.SetOffset(file_addr); 10038e38c666SStephane Sezer else 1004b8d03935SAaron Smith m_entry_point_address.ResolveAddressUsingFileSections(file_addr, 1005b8d03935SAaron Smith section_list); 10068e38c666SStephane Sezer return m_entry_point_address; 10078e38c666SStephane Sezer } 10088e38c666SStephane Sezer 1009d1304bbaSPavel Labath Address ObjectFilePECOFF::GetBaseAddress() { 1010d1304bbaSPavel Labath return Address(GetSectionList()->GetSectionAtIndex(0), 0); 1011d1304bbaSPavel Labath } 1012d1304bbaSPavel Labath 1013f754f88fSGreg Clayton // Dump 1014f754f88fSGreg Clayton // 1015f754f88fSGreg Clayton // Dump the specifics of the runtime file container (such as any headers 1016f754f88fSGreg Clayton // segments, sections, etc). 1017b9c1b51eSKate Stone void ObjectFilePECOFF::Dump(Stream *s) { 1018a1743499SGreg Clayton ModuleSP module_sp(GetModule()); 1019b9c1b51eSKate Stone if (module_sp) { 102016ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 1021324a1036SSaleem Abdulrasool s->Printf("%p: ", static_cast<void *>(this)); 1022f754f88fSGreg Clayton s->Indent(); 1023f754f88fSGreg Clayton s->PutCString("ObjectFilePECOFF"); 1024f754f88fSGreg Clayton 1025f760f5aeSPavel Labath ArchSpec header_arch = GetArchitecture(); 1026f754f88fSGreg Clayton 1027b9c1b51eSKate Stone *s << ", file = '" << m_file 1028b9c1b51eSKate Stone << "', arch = " << header_arch.GetArchitectureName() << "\n"; 1029f754f88fSGreg Clayton 10303046e668SGreg Clayton SectionList *sections = GetSectionList(); 10313046e668SGreg Clayton if (sections) 10323a168297SPavel Labath sections->Dump(s->AsRawOstream(), s->GetIndentLevel(), nullptr, true, 10333a168297SPavel Labath UINT32_MAX); 1034f754f88fSGreg Clayton 1035d5b44036SJonas Devlieghere if (m_symtab_up) 1036248a1305SKonrad Kleine m_symtab_up->Dump(s, nullptr, eSortOrderNone); 1037f754f88fSGreg Clayton 1038f754f88fSGreg Clayton if (m_dos_header.e_magic) 1039f754f88fSGreg Clayton DumpDOSHeader(s, m_dos_header); 1040b9c1b51eSKate Stone if (m_coff_header.machine) { 1041f754f88fSGreg Clayton DumpCOFFHeader(s, m_coff_header); 1042f754f88fSGreg Clayton if (m_coff_header.hdrsize) 1043f754f88fSGreg Clayton DumpOptCOFFHeader(s, m_coff_header_opt); 1044f754f88fSGreg Clayton } 1045f754f88fSGreg Clayton s->EOL(); 1046f754f88fSGreg Clayton DumpSectionHeaders(s); 1047f754f88fSGreg Clayton s->EOL(); 1048037ed1beSAaron Smith 1049037ed1beSAaron Smith DumpDependentModules(s); 1050037ed1beSAaron Smith s->EOL(); 1051f754f88fSGreg Clayton } 1052a1743499SGreg Clayton } 1053f754f88fSGreg Clayton 1054f754f88fSGreg Clayton // DumpDOSHeader 1055f754f88fSGreg Clayton // 1056f754f88fSGreg Clayton // Dump the MS-DOS header to the specified output stream 1057b9c1b51eSKate Stone void ObjectFilePECOFF::DumpDOSHeader(Stream *s, const dos_header_t &header) { 1058f754f88fSGreg Clayton s->PutCString("MSDOS Header\n"); 1059f754f88fSGreg Clayton s->Printf(" e_magic = 0x%4.4x\n", header.e_magic); 1060f754f88fSGreg Clayton s->Printf(" e_cblp = 0x%4.4x\n", header.e_cblp); 1061f754f88fSGreg Clayton s->Printf(" e_cp = 0x%4.4x\n", header.e_cp); 1062f754f88fSGreg Clayton s->Printf(" e_crlc = 0x%4.4x\n", header.e_crlc); 1063f754f88fSGreg Clayton s->Printf(" e_cparhdr = 0x%4.4x\n", header.e_cparhdr); 1064f754f88fSGreg Clayton s->Printf(" e_minalloc = 0x%4.4x\n", header.e_minalloc); 1065f754f88fSGreg Clayton s->Printf(" e_maxalloc = 0x%4.4x\n", header.e_maxalloc); 1066f754f88fSGreg Clayton s->Printf(" e_ss = 0x%4.4x\n", header.e_ss); 1067f754f88fSGreg Clayton s->Printf(" e_sp = 0x%4.4x\n", header.e_sp); 1068f754f88fSGreg Clayton s->Printf(" e_csum = 0x%4.4x\n", header.e_csum); 1069f754f88fSGreg Clayton s->Printf(" e_ip = 0x%4.4x\n", header.e_ip); 1070f754f88fSGreg Clayton s->Printf(" e_cs = 0x%4.4x\n", header.e_cs); 1071f754f88fSGreg Clayton s->Printf(" e_lfarlc = 0x%4.4x\n", header.e_lfarlc); 1072f754f88fSGreg Clayton s->Printf(" e_ovno = 0x%4.4x\n", header.e_ovno); 1073f754f88fSGreg Clayton s->Printf(" e_res[4] = { 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x }\n", 1074b9c1b51eSKate Stone header.e_res[0], header.e_res[1], header.e_res[2], header.e_res[3]); 1075f754f88fSGreg Clayton s->Printf(" e_oemid = 0x%4.4x\n", header.e_oemid); 1076f754f88fSGreg Clayton s->Printf(" e_oeminfo = 0x%4.4x\n", header.e_oeminfo); 1077b9c1b51eSKate Stone s->Printf(" e_res2[10] = { 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, " 1078b9c1b51eSKate Stone "0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x }\n", 1079b9c1b51eSKate Stone header.e_res2[0], header.e_res2[1], header.e_res2[2], 1080b9c1b51eSKate Stone header.e_res2[3], header.e_res2[4], header.e_res2[5], 1081b9c1b51eSKate Stone header.e_res2[6], header.e_res2[7], header.e_res2[8], 1082f754f88fSGreg Clayton header.e_res2[9]); 1083f754f88fSGreg Clayton s->Printf(" e_lfanew = 0x%8.8x\n", header.e_lfanew); 1084f754f88fSGreg Clayton } 1085f754f88fSGreg Clayton 1086f754f88fSGreg Clayton // DumpCOFFHeader 1087f754f88fSGreg Clayton // 1088f754f88fSGreg Clayton // Dump the COFF header to the specified output stream 1089b9c1b51eSKate Stone void ObjectFilePECOFF::DumpCOFFHeader(Stream *s, const coff_header_t &header) { 1090f754f88fSGreg Clayton s->PutCString("COFF Header\n"); 1091f754f88fSGreg Clayton s->Printf(" machine = 0x%4.4x\n", header.machine); 1092f754f88fSGreg Clayton s->Printf(" nsects = 0x%4.4x\n", header.nsects); 1093f754f88fSGreg Clayton s->Printf(" modtime = 0x%8.8x\n", header.modtime); 1094f754f88fSGreg Clayton s->Printf(" symoff = 0x%8.8x\n", header.symoff); 1095f754f88fSGreg Clayton s->Printf(" nsyms = 0x%8.8x\n", header.nsyms); 1096f754f88fSGreg Clayton s->Printf(" hdrsize = 0x%4.4x\n", header.hdrsize); 1097f754f88fSGreg Clayton } 1098f754f88fSGreg Clayton 1099f754f88fSGreg Clayton // DumpOptCOFFHeader 1100f754f88fSGreg Clayton // 1101f754f88fSGreg Clayton // Dump the optional COFF header to the specified output stream 1102b9c1b51eSKate Stone void ObjectFilePECOFF::DumpOptCOFFHeader(Stream *s, 1103b9c1b51eSKate Stone const coff_opt_header_t &header) { 1104f754f88fSGreg Clayton s->PutCString("Optional COFF Header\n"); 1105f754f88fSGreg Clayton s->Printf(" magic = 0x%4.4x\n", header.magic); 1106b9c1b51eSKate Stone s->Printf(" major_linker_version = 0x%2.2x\n", 1107b9c1b51eSKate Stone header.major_linker_version); 1108b9c1b51eSKate Stone s->Printf(" minor_linker_version = 0x%2.2x\n", 1109b9c1b51eSKate Stone header.minor_linker_version); 1110f754f88fSGreg Clayton s->Printf(" code_size = 0x%8.8x\n", header.code_size); 1111f754f88fSGreg Clayton s->Printf(" data_size = 0x%8.8x\n", header.data_size); 1112f754f88fSGreg Clayton s->Printf(" bss_size = 0x%8.8x\n", header.bss_size); 1113f754f88fSGreg Clayton s->Printf(" entry = 0x%8.8x\n", header.entry); 1114f754f88fSGreg Clayton s->Printf(" code_offset = 0x%8.8x\n", header.code_offset); 1115f754f88fSGreg Clayton s->Printf(" data_offset = 0x%8.8x\n", header.data_offset); 1116b9c1b51eSKate Stone s->Printf(" image_base = 0x%16.16" PRIx64 "\n", 1117b9c1b51eSKate Stone header.image_base); 1118f754f88fSGreg Clayton s->Printf(" sect_alignment = 0x%8.8x\n", header.sect_alignment); 1119f754f88fSGreg Clayton s->Printf(" file_alignment = 0x%8.8x\n", header.file_alignment); 1120b9c1b51eSKate Stone s->Printf(" major_os_system_version = 0x%4.4x\n", 1121b9c1b51eSKate Stone header.major_os_system_version); 1122b9c1b51eSKate Stone s->Printf(" minor_os_system_version = 0x%4.4x\n", 1123b9c1b51eSKate Stone header.minor_os_system_version); 1124b9c1b51eSKate Stone s->Printf(" major_image_version = 0x%4.4x\n", 1125b9c1b51eSKate Stone header.major_image_version); 1126b9c1b51eSKate Stone s->Printf(" minor_image_version = 0x%4.4x\n", 1127b9c1b51eSKate Stone header.minor_image_version); 1128b9c1b51eSKate Stone s->Printf(" major_subsystem_version = 0x%4.4x\n", 1129b9c1b51eSKate Stone header.major_subsystem_version); 1130b9c1b51eSKate Stone s->Printf(" minor_subsystem_version = 0x%4.4x\n", 1131b9c1b51eSKate Stone header.minor_subsystem_version); 1132f754f88fSGreg Clayton s->Printf(" reserved1 = 0x%8.8x\n", header.reserved1); 1133f754f88fSGreg Clayton s->Printf(" image_size = 0x%8.8x\n", header.image_size); 1134f754f88fSGreg Clayton s->Printf(" header_size = 0x%8.8x\n", header.header_size); 113528469ca3SGreg Clayton s->Printf(" checksum = 0x%8.8x\n", header.checksum); 1136f754f88fSGreg Clayton s->Printf(" subsystem = 0x%4.4x\n", header.subsystem); 1137f754f88fSGreg Clayton s->Printf(" dll_flags = 0x%4.4x\n", header.dll_flags); 1138b9c1b51eSKate Stone s->Printf(" stack_reserve_size = 0x%16.16" PRIx64 "\n", 1139b9c1b51eSKate Stone header.stack_reserve_size); 1140b9c1b51eSKate Stone s->Printf(" stack_commit_size = 0x%16.16" PRIx64 "\n", 1141b9c1b51eSKate Stone header.stack_commit_size); 1142b9c1b51eSKate Stone s->Printf(" heap_reserve_size = 0x%16.16" PRIx64 "\n", 1143b9c1b51eSKate Stone header.heap_reserve_size); 1144b9c1b51eSKate Stone s->Printf(" heap_commit_size = 0x%16.16" PRIx64 "\n", 1145b9c1b51eSKate Stone header.heap_commit_size); 1146f754f88fSGreg Clayton s->Printf(" loader_flags = 0x%8.8x\n", header.loader_flags); 1147b9c1b51eSKate Stone s->Printf(" num_data_dir_entries = 0x%8.8x\n", 1148b9c1b51eSKate Stone (uint32_t)header.data_dirs.size()); 1149f754f88fSGreg Clayton uint32_t i; 1150b9c1b51eSKate Stone for (i = 0; i < header.data_dirs.size(); i++) { 1151b9c1b51eSKate Stone s->Printf(" data_dirs[%2u] vmaddr = 0x%8.8x, vmsize = 0x%8.8x\n", i, 1152b9c1b51eSKate Stone header.data_dirs[i].vmaddr, header.data_dirs[i].vmsize); 1153f754f88fSGreg Clayton } 1154f754f88fSGreg Clayton } 1155f754f88fSGreg Clayton // DumpSectionHeader 1156f754f88fSGreg Clayton // 1157f754f88fSGreg Clayton // Dump a single ELF section header to the specified output stream 1158b9c1b51eSKate Stone void ObjectFilePECOFF::DumpSectionHeader(Stream *s, 1159b9c1b51eSKate Stone const section_header_t &sh) { 1160adcd0268SBenjamin Kramer std::string name = std::string(GetSectionName(sh)); 1161b9c1b51eSKate 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 " 1162b9c1b51eSKate Stone "0x%4.4x 0x%8.8x\n", 1163b9c1b51eSKate Stone name.c_str(), sh.vmaddr, sh.vmsize, sh.offset, sh.size, sh.reloff, 1164b9c1b51eSKate Stone sh.lineoff, sh.nreloc, sh.nline, sh.flags); 1165f754f88fSGreg Clayton } 1166f754f88fSGreg Clayton 1167f754f88fSGreg Clayton // DumpSectionHeaders 1168f754f88fSGreg Clayton // 1169f754f88fSGreg Clayton // Dump all of the ELF section header to the specified output stream 1170b9c1b51eSKate Stone void ObjectFilePECOFF::DumpSectionHeaders(Stream *s) { 1171f754f88fSGreg Clayton 1172f754f88fSGreg Clayton s->PutCString("Section Headers\n"); 1173b9c1b51eSKate Stone s->PutCString("IDX name vm addr vm size file off file " 1174b9c1b51eSKate Stone "size reloc off line off nreloc nline flags\n"); 1175b9c1b51eSKate Stone s->PutCString("==== ---------------- ---------- ---------- ---------- " 1176b9c1b51eSKate Stone "---------- ---------- ---------- ------ ------ ----------\n"); 1177f754f88fSGreg Clayton 1178f754f88fSGreg Clayton uint32_t idx = 0; 1179f754f88fSGreg Clayton SectionHeaderCollIter pos, end = m_sect_headers.end(); 1180f754f88fSGreg Clayton 1181b9c1b51eSKate Stone for (pos = m_sect_headers.begin(); pos != end; ++pos, ++idx) { 1182f754f88fSGreg Clayton s->Printf("[%2u] ", idx); 1183f754f88fSGreg Clayton ObjectFilePECOFF::DumpSectionHeader(s, *pos); 1184f754f88fSGreg Clayton } 1185f754f88fSGreg Clayton } 1186f754f88fSGreg Clayton 1187037ed1beSAaron Smith // DumpDependentModules 1188037ed1beSAaron Smith // 1189037ed1beSAaron Smith // Dump all of the dependent modules to the specified output stream 1190037ed1beSAaron Smith void ObjectFilePECOFF::DumpDependentModules(lldb_private::Stream *s) { 1191037ed1beSAaron Smith auto num_modules = ParseDependentModules(); 1192037ed1beSAaron Smith if (num_modules > 0) { 1193037ed1beSAaron Smith s->PutCString("Dependent Modules\n"); 1194037ed1beSAaron Smith for (unsigned i = 0; i < num_modules; ++i) { 1195037ed1beSAaron Smith auto spec = m_deps_filespec->GetFileSpecAtIndex(i); 1196037ed1beSAaron Smith s->Printf(" %s\n", spec.GetFilename().GetCString()); 1197037ed1beSAaron Smith } 1198037ed1beSAaron Smith } 1199037ed1beSAaron Smith } 1200037ed1beSAaron Smith 1201fb3b3bd1SZachary Turner bool ObjectFilePECOFF::IsWindowsSubsystem() { 1202fb3b3bd1SZachary Turner switch (m_coff_header_opt.subsystem) { 1203fb3b3bd1SZachary Turner case llvm::COFF::IMAGE_SUBSYSTEM_NATIVE: 1204fb3b3bd1SZachary Turner case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_GUI: 1205fb3b3bd1SZachary Turner case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_CUI: 1206fb3b3bd1SZachary Turner case llvm::COFF::IMAGE_SUBSYSTEM_NATIVE_WINDOWS: 1207fb3b3bd1SZachary Turner case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_CE_GUI: 1208fb3b3bd1SZachary Turner case llvm::COFF::IMAGE_SUBSYSTEM_XBOX: 1209fb3b3bd1SZachary Turner case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_BOOT_APPLICATION: 1210fb3b3bd1SZachary Turner return true; 1211fb3b3bd1SZachary Turner default: 1212fb3b3bd1SZachary Turner return false; 1213fb3b3bd1SZachary Turner } 1214fb3b3bd1SZachary Turner } 1215fb3b3bd1SZachary Turner 1216f760f5aeSPavel Labath ArchSpec ObjectFilePECOFF::GetArchitecture() { 1217237ad974SCharles Davis uint16_t machine = m_coff_header.machine; 1218b9c1b51eSKate Stone switch (machine) { 1219f760f5aeSPavel Labath default: 1220f760f5aeSPavel Labath break; 1221237ad974SCharles Davis case llvm::COFF::IMAGE_FILE_MACHINE_AMD64: 1222237ad974SCharles Davis case llvm::COFF::IMAGE_FILE_MACHINE_I386: 1223237ad974SCharles Davis case llvm::COFF::IMAGE_FILE_MACHINE_POWERPC: 1224237ad974SCharles Davis case llvm::COFF::IMAGE_FILE_MACHINE_POWERPCFP: 1225237ad974SCharles Davis case llvm::COFF::IMAGE_FILE_MACHINE_ARM: 12261108cb36SSaleem Abdulrasool case llvm::COFF::IMAGE_FILE_MACHINE_ARMNT: 1227237ad974SCharles Davis case llvm::COFF::IMAGE_FILE_MACHINE_THUMB: 1228638f072fSMartin Storsjo case llvm::COFF::IMAGE_FILE_MACHINE_ARM64: 1229f760f5aeSPavel Labath ArchSpec arch; 1230fb3b3bd1SZachary Turner arch.SetArchitecture(eArchTypeCOFF, machine, LLDB_INVALID_CPUTYPE, 1231fb3b3bd1SZachary Turner IsWindowsSubsystem() ? llvm::Triple::Win32 1232fb3b3bd1SZachary Turner : llvm::Triple::UnknownOS); 1233f760f5aeSPavel Labath return arch; 1234237ad974SCharles Davis } 1235f760f5aeSPavel Labath return ArchSpec(); 1236f754f88fSGreg Clayton } 1237f754f88fSGreg Clayton 1238b9c1b51eSKate Stone ObjectFile::Type ObjectFilePECOFF::CalculateType() { 1239b9c1b51eSKate Stone if (m_coff_header.machine != 0) { 1240237ad974SCharles Davis if ((m_coff_header.flags & llvm::COFF::IMAGE_FILE_DLL) == 0) 1241f754f88fSGreg Clayton return eTypeExecutable; 1242f754f88fSGreg Clayton else 1243f754f88fSGreg Clayton return eTypeSharedLibrary; 1244f754f88fSGreg Clayton } 1245f754f88fSGreg Clayton return eTypeExecutable; 1246f754f88fSGreg Clayton } 1247f754f88fSGreg Clayton 1248b9c1b51eSKate Stone ObjectFile::Strata ObjectFilePECOFF::CalculateStrata() { return eStrataUser; } 1249