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" 26037ed1beSAaron Smith #include "lldb/Utility/Log.h" 27bf9a7730SZachary Turner #include "lldb/Utility/StreamString.h" 2838d0632eSPavel Labath #include "lldb/Utility/Timer.h" 29666cc0b2SZachary Turner #include "lldb/Utility/UUID.h" 305f19b907SPavel Labath #include "llvm/BinaryFormat/COFF.h" 31f754f88fSGreg Clayton 32037ed1beSAaron Smith #include "llvm/Object/COFFImportFile.h" 33037ed1beSAaron Smith #include "llvm/Support/Error.h" 343f4a4b36SZachary Turner #include "llvm/Support/MemoryBuffer.h" 353f4a4b36SZachary Turner 36f754f88fSGreg Clayton #define IMAGE_DOS_SIGNATURE 0x5A4D // MZ 37f754f88fSGreg Clayton #define IMAGE_NT_SIGNATURE 0x00004550 // PE00 38f754f88fSGreg Clayton #define OPT_HEADER_MAGIC_PE32 0x010b 39f754f88fSGreg Clayton #define OPT_HEADER_MAGIC_PE32_PLUS 0x020b 40f754f88fSGreg Clayton 41f754f88fSGreg Clayton using namespace lldb; 42f754f88fSGreg Clayton using namespace lldb_private; 43f754f88fSGreg Clayton 44bba9ba8dSJonas Devlieghere LLDB_PLUGIN_DEFINE(ObjectFilePECOFF) 45fbb4d1e4SJonas Devlieghere 46d372a8e8SPavel Labath static UUID GetCoffUUID(llvm::object::COFFObjectFile &coff_obj) { 47b8d03935SAaron Smith const llvm::codeview::DebugInfo *pdb_info = nullptr; 48b8d03935SAaron Smith llvm::StringRef pdb_file; 49b8d03935SAaron Smith 50d372a8e8SPavel Labath if (!coff_obj.getDebugPDBInfo(pdb_info, pdb_file) && pdb_info) { 51b8d03935SAaron Smith if (pdb_info->PDB70.CVSignature == llvm::OMF::Signature::PDB70) { 524348e0eeSZequan Wu UUID::CvRecordPdb70 info; 534348e0eeSZequan Wu memcpy(&info.Uuid, pdb_info->PDB70.Signature, sizeof(info.Uuid)); 544348e0eeSZequan Wu info.Age = pdb_info->PDB70.Age; 554348e0eeSZequan Wu return UUID::fromCvRecord(info); 56b8d03935SAaron Smith } 57b8d03935SAaron Smith } 58b8d03935SAaron Smith 59b8d03935SAaron Smith return UUID(); 60b8d03935SAaron Smith } 61b8d03935SAaron Smith 62e84f7841SPavel Labath char ObjectFilePECOFF::ID; 63e84f7841SPavel Labath 64b9c1b51eSKate Stone void ObjectFilePECOFF::Initialize() { 65b9c1b51eSKate Stone PluginManager::RegisterPlugin( 66b9c1b51eSKate Stone GetPluginNameStatic(), GetPluginDescriptionStatic(), CreateInstance, 67b9c1b51eSKate Stone CreateMemoryInstance, GetModuleSpecifications, SaveCore); 68f754f88fSGreg Clayton } 69f754f88fSGreg Clayton 70b9c1b51eSKate Stone void ObjectFilePECOFF::Terminate() { 71f754f88fSGreg Clayton PluginManager::UnregisterPlugin(CreateInstance); 72f754f88fSGreg Clayton } 73f754f88fSGreg Clayton 742ace1e57SPavel Labath llvm::StringRef ObjectFilePECOFF::GetPluginDescriptionStatic() { 75b9c1b51eSKate Stone return "Portable Executable and Common Object File Format object file reader " 76b9c1b51eSKate Stone "(32 and 64 bit)"; 77f754f88fSGreg Clayton } 78f754f88fSGreg Clayton 79b9c1b51eSKate Stone ObjectFile *ObjectFilePECOFF::CreateInstance(const lldb::ModuleSP &module_sp, 805ce9c565SGreg Clayton DataBufferSP &data_sp, 815ce9c565SGreg Clayton lldb::offset_t data_offset, 8228e4942bSPavel Labath const lldb_private::FileSpec *file_p, 835ce9c565SGreg Clayton lldb::offset_t file_offset, 84b9c1b51eSKate Stone lldb::offset_t length) { 8528e4942bSPavel Labath FileSpec file = file_p ? *file_p : FileSpec(); 86b9c1b51eSKate Stone if (!data_sp) { 8750251fc7SPavel Labath data_sp = MapFileData(file, length, file_offset); 883f4a4b36SZachary Turner if (!data_sp) 893f4a4b36SZachary Turner return nullptr; 905ce9c565SGreg Clayton data_offset = 0; 915ce9c565SGreg Clayton } 925ce9c565SGreg Clayton 933f4a4b36SZachary Turner if (!ObjectFilePECOFF::MagicBytesMatch(data_sp)) 943f4a4b36SZachary Turner return nullptr; 953f4a4b36SZachary Turner 965ce9c565SGreg Clayton // Update the data to contain the entire file if it doesn't already 973f4a4b36SZachary Turner if (data_sp->GetByteSize() < length) { 9850251fc7SPavel Labath data_sp = MapFileData(file, length, file_offset); 993f4a4b36SZachary Turner if (!data_sp) 1003f4a4b36SZachary Turner return nullptr; 101f754f88fSGreg Clayton } 1023f4a4b36SZachary Turner 103a8f3ae7cSJonas Devlieghere auto objfile_up = std::make_unique<ObjectFilePECOFF>( 10428e4942bSPavel Labath module_sp, data_sp, data_offset, file_p, file_offset, length); 105d5b44036SJonas Devlieghere if (!objfile_up || !objfile_up->ParseHeader()) 1063f4a4b36SZachary Turner return nullptr; 1073f4a4b36SZachary Turner 108037ed1beSAaron Smith // Cache coff binary. 109d5b44036SJonas Devlieghere if (!objfile_up->CreateBinary()) 110037ed1beSAaron Smith return nullptr; 111d5b44036SJonas Devlieghere return objfile_up.release(); 112f754f88fSGreg Clayton } 113f754f88fSGreg Clayton 114b9c1b51eSKate Stone ObjectFile *ObjectFilePECOFF::CreateMemoryInstance( 115b9c1b51eSKate Stone const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp, 116b9c1b51eSKate Stone const lldb::ProcessSP &process_sp, lldb::addr_t header_addr) { 117344546bdSWalter Erquinigo if (!data_sp || !ObjectFilePECOFF::MagicBytesMatch(data_sp)) 118344546bdSWalter Erquinigo return nullptr; 119a8f3ae7cSJonas Devlieghere auto objfile_up = std::make_unique<ObjectFilePECOFF>( 120344546bdSWalter Erquinigo module_sp, data_sp, process_sp, header_addr); 121d5b44036SJonas Devlieghere if (objfile_up.get() && objfile_up->ParseHeader()) { 122d5b44036SJonas Devlieghere return objfile_up.release(); 123344546bdSWalter Erquinigo } 124344546bdSWalter Erquinigo return nullptr; 125c9660546SGreg Clayton } 126c9660546SGreg Clayton 127b9c1b51eSKate Stone size_t ObjectFilePECOFF::GetModuleSpecifications( 128b9c1b51eSKate Stone const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp, 129b9c1b51eSKate Stone lldb::offset_t data_offset, lldb::offset_t file_offset, 130b9c1b51eSKate Stone lldb::offset_t length, lldb_private::ModuleSpecList &specs) { 13189eb1baeSVirgile Bello const size_t initial_count = specs.GetSize(); 132b8d03935SAaron Smith if (!data_sp || !ObjectFilePECOFF::MagicBytesMatch(data_sp)) 133b8d03935SAaron Smith return initial_count; 13489eb1baeSVirgile Bello 1353db1d138SMartin Storsjö Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT)); 1363db1d138SMartin Storsjö 137a4a00cedSFred Riss if (data_sp->GetByteSize() < length) 138d372a8e8SPavel Labath if (DataBufferSP full_sp = MapFileData(file, -1, file_offset)) 139d372a8e8SPavel Labath data_sp = std::move(full_sp); 140d372a8e8SPavel Labath auto binary = llvm::object::createBinary(llvm::MemoryBufferRef( 141d372a8e8SPavel Labath toStringRef(data_sp->GetData()), file.GetFilename().GetStringRef())); 1423db1d138SMartin Storsjö 1433db1d138SMartin Storsjö if (!binary) { 1443db1d138SMartin Storsjö LLDB_LOG_ERROR(log, binary.takeError(), 1453db1d138SMartin Storsjö "Failed to create binary for file ({1}): {0}", file); 146b8d03935SAaron Smith return initial_count; 1473db1d138SMartin Storsjö } 14889eb1baeSVirgile Bello 149d372a8e8SPavel Labath auto *COFFObj = llvm::dyn_cast<llvm::object::COFFObjectFile>(binary->get()); 150d372a8e8SPavel Labath if (!COFFObj) 151b8d03935SAaron Smith return initial_count; 15289eb1baeSVirgile Bello 153b8d03935SAaron Smith ModuleSpec module_spec(file); 154b8d03935SAaron Smith ArchSpec &spec = module_spec.GetArchitecture(); 155b8d03935SAaron Smith lldb_private::UUID &uuid = module_spec.GetUUID(); 156b8d03935SAaron Smith if (!uuid.IsValid()) 157d372a8e8SPavel Labath uuid = GetCoffUUID(*COFFObj); 158b8d03935SAaron Smith 159b8d03935SAaron Smith switch (COFFObj->getMachine()) { 160b8d03935SAaron Smith case MachineAmd64: 161ad587ae4SZachary Turner spec.SetTriple("x86_64-pc-windows"); 162b8d03935SAaron Smith specs.Append(module_spec); 163b8d03935SAaron Smith break; 164b8d03935SAaron Smith case MachineX86: 165ad587ae4SZachary Turner spec.SetTriple("i386-pc-windows"); 166b8d03935SAaron Smith specs.Append(module_spec); 1675e6f4520SZachary Turner spec.SetTriple("i686-pc-windows"); 168b8d03935SAaron Smith specs.Append(module_spec); 169b8d03935SAaron Smith break; 170b8d03935SAaron Smith case MachineArmNt: 171544c8f48SMartin Storsjo spec.SetTriple("armv7-pc-windows"); 172b8d03935SAaron Smith specs.Append(module_spec); 173b8d03935SAaron Smith break; 174638f072fSMartin Storsjo case MachineArm64: 175674d5543SMartin Storsjo spec.SetTriple("aarch64-pc-windows"); 176638f072fSMartin Storsjo specs.Append(module_spec); 177638f072fSMartin Storsjo break; 178b8d03935SAaron Smith default: 179b8d03935SAaron Smith break; 18089eb1baeSVirgile Bello } 18189eb1baeSVirgile Bello 18289eb1baeSVirgile Bello return specs.GetSize() - initial_count; 183f4d6de6aSGreg Clayton } 184f4d6de6aSGreg Clayton 185b9c1b51eSKate Stone bool ObjectFilePECOFF::SaveCore(const lldb::ProcessSP &process_sp, 186f7d1893fSAdrian McCarthy const lldb_private::FileSpec &outfile, 1879ea6dd5cSJason Molenda lldb::SaveCoreStyle &core_style, 18897206d57SZachary Turner lldb_private::Status &error) { 1899ea6dd5cSJason Molenda core_style = eSaveCoreFull; 190f7d1893fSAdrian McCarthy return SaveMiniDump(process_sp, outfile, error); 191f7d1893fSAdrian McCarthy } 192f7d1893fSAdrian McCarthy 193b9c1b51eSKate Stone bool ObjectFilePECOFF::MagicBytesMatch(DataBufferSP &data_sp) { 1945ce9c565SGreg Clayton DataExtractor data(data_sp, eByteOrderLittle, 4); 195c7bece56SGreg Clayton lldb::offset_t offset = 0; 196f754f88fSGreg Clayton uint16_t magic = data.GetU16(&offset); 197f754f88fSGreg Clayton return magic == IMAGE_DOS_SIGNATURE; 198f754f88fSGreg Clayton } 199f754f88fSGreg Clayton 200b9c1b51eSKate Stone lldb::SymbolType ObjectFilePECOFF::MapSymbolType(uint16_t coff_symbol_type) { 201c35b91ceSAdrian McCarthy // TODO: We need to complete this mapping of COFF symbol types to LLDB ones. 202c35b91ceSAdrian McCarthy // For now, here's a hack to make sure our function have types. 203b9c1b51eSKate Stone const auto complex_type = 204b9c1b51eSKate Stone coff_symbol_type >> llvm::COFF::SCT_COMPLEX_TYPE_SHIFT; 205b9c1b51eSKate Stone if (complex_type == llvm::COFF::IMAGE_SYM_DTYPE_FUNCTION) { 206c35b91ceSAdrian McCarthy return lldb::eSymbolTypeCode; 207c35b91ceSAdrian McCarthy } 208c35b91ceSAdrian McCarthy return lldb::eSymbolTypeInvalid; 209c35b91ceSAdrian McCarthy } 210f754f88fSGreg Clayton 211037ed1beSAaron Smith bool ObjectFilePECOFF::CreateBinary() { 212d372a8e8SPavel Labath if (m_binary) 213037ed1beSAaron Smith return true; 214037ed1beSAaron Smith 215037ed1beSAaron Smith Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT)); 216037ed1beSAaron Smith 217d372a8e8SPavel Labath auto binary = llvm::object::createBinary(llvm::MemoryBufferRef( 218d372a8e8SPavel Labath toStringRef(m_data.GetData()), m_file.GetFilename().GetStringRef())); 219037ed1beSAaron Smith if (!binary) { 2203db1d138SMartin Storsjö LLDB_LOG_ERROR(log, binary.takeError(), 2213db1d138SMartin Storsjö "Failed to create binary for file ({1}): {0}", m_file); 222037ed1beSAaron Smith return false; 223037ed1beSAaron Smith } 224037ed1beSAaron Smith 225037ed1beSAaron Smith // Make sure we only handle COFF format. 226d372a8e8SPavel Labath m_binary = 227d372a8e8SPavel Labath llvm::unique_dyn_cast<llvm::object::COFFObjectFile>(std::move(*binary)); 228d372a8e8SPavel Labath if (!m_binary) 229037ed1beSAaron Smith return false; 230037ed1beSAaron Smith 231d372a8e8SPavel Labath LLDB_LOG(log, "this = {0}, module = {1} ({2}), file = {3}, binary = {4}", 232d372a8e8SPavel Labath this, GetModule().get(), GetModule()->GetSpecificationDescription(), 233d372a8e8SPavel Labath m_file.GetPath(), m_binary.get()); 234037ed1beSAaron Smith return true; 235037ed1beSAaron Smith } 236037ed1beSAaron Smith 237e72dfb32SGreg Clayton ObjectFilePECOFF::ObjectFilePECOFF(const lldb::ModuleSP &module_sp, 2385ce9c565SGreg Clayton DataBufferSP &data_sp, 2395ce9c565SGreg Clayton lldb::offset_t data_offset, 240f754f88fSGreg Clayton const FileSpec *file, 2415ce9c565SGreg Clayton lldb::offset_t file_offset, 242b9c1b51eSKate Stone lldb::offset_t length) 243b9c1b51eSKate Stone : ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset), 2445c43ffd6SPavel Labath m_dos_header(), m_coff_header(), m_sect_headers(), 245d372a8e8SPavel Labath m_entry_point_address(), m_deps_filespec() { 246f754f88fSGreg Clayton ::memset(&m_dos_header, 0, sizeof(m_dos_header)); 247f754f88fSGreg Clayton ::memset(&m_coff_header, 0, sizeof(m_coff_header)); 248f754f88fSGreg Clayton } 249f754f88fSGreg Clayton 250344546bdSWalter Erquinigo ObjectFilePECOFF::ObjectFilePECOFF(const lldb::ModuleSP &module_sp, 251344546bdSWalter Erquinigo DataBufferSP &header_data_sp, 252344546bdSWalter Erquinigo const lldb::ProcessSP &process_sp, 253344546bdSWalter Erquinigo addr_t header_addr) 254344546bdSWalter Erquinigo : ObjectFile(module_sp, process_sp, header_addr, header_data_sp), 2555c43ffd6SPavel Labath m_dos_header(), m_coff_header(), m_sect_headers(), 256d372a8e8SPavel Labath m_entry_point_address(), m_deps_filespec() { 257344546bdSWalter Erquinigo ::memset(&m_dos_header, 0, sizeof(m_dos_header)); 258344546bdSWalter Erquinigo ::memset(&m_coff_header, 0, sizeof(m_coff_header)); 259344546bdSWalter Erquinigo } 260344546bdSWalter Erquinigo 261fd2433e1SJonas Devlieghere ObjectFilePECOFF::~ObjectFilePECOFF() = default; 262f754f88fSGreg Clayton 263b9c1b51eSKate Stone bool ObjectFilePECOFF::ParseHeader() { 264a1743499SGreg Clayton ModuleSP module_sp(GetModule()); 265b9c1b51eSKate Stone if (module_sp) { 26616ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 267f754f88fSGreg Clayton m_sect_headers.clear(); 268f754f88fSGreg Clayton m_data.SetByteOrder(eByteOrderLittle); 269c7bece56SGreg Clayton lldb::offset_t offset = 0; 270f754f88fSGreg Clayton 271b9c1b51eSKate Stone if (ParseDOSHeader(m_data, m_dos_header)) { 272f754f88fSGreg Clayton offset = m_dos_header.e_lfanew; 273f754f88fSGreg Clayton uint32_t pe_signature = m_data.GetU32(&offset); 274f754f88fSGreg Clayton if (pe_signature != IMAGE_NT_SIGNATURE) 275f754f88fSGreg Clayton return false; 276b9c1b51eSKate Stone if (ParseCOFFHeader(m_data, &offset, m_coff_header)) { 277f754f88fSGreg Clayton if (m_coff_header.hdrsize > 0) 278f754f88fSGreg Clayton ParseCOFFOptionalHeader(&offset); 279f754f88fSGreg Clayton ParseSectionHeaders(offset); 28028469ca3SGreg Clayton } 281a0f72441SMartin Storsjö m_data.SetAddressByteSize(GetAddressByteSize()); 282f754f88fSGreg Clayton return true; 283f754f88fSGreg Clayton } 284a1743499SGreg Clayton } 285f754f88fSGreg Clayton return false; 286f754f88fSGreg Clayton } 287f754f88fSGreg Clayton 288b9c1b51eSKate Stone bool ObjectFilePECOFF::SetLoadAddress(Target &target, addr_t value, 289b9c1b51eSKate Stone bool value_is_offset) { 2902756adf3SVirgile Bello bool changed = false; 2912756adf3SVirgile Bello ModuleSP module_sp = GetModule(); 292b9c1b51eSKate Stone if (module_sp) { 2932756adf3SVirgile Bello size_t num_loaded_sections = 0; 2942756adf3SVirgile Bello SectionList *section_list = GetSectionList(); 295b9c1b51eSKate Stone if (section_list) { 296b9c1b51eSKate Stone if (!value_is_offset) { 2972756adf3SVirgile Bello value -= m_image_base; 2982756adf3SVirgile Bello } 2992756adf3SVirgile Bello 3002756adf3SVirgile Bello const size_t num_sections = section_list->GetSize(); 3012756adf3SVirgile Bello size_t sect_idx = 0; 3022756adf3SVirgile Bello 303b9c1b51eSKate Stone for (sect_idx = 0; sect_idx < num_sections; ++sect_idx) { 30405097246SAdrian Prantl // Iterate through the object file sections to find all of the sections 30505097246SAdrian Prantl // that have SHF_ALLOC in their flag bits. 3062756adf3SVirgile Bello SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx)); 307b9c1b51eSKate Stone if (section_sp && !section_sp->IsThreadSpecific()) { 308b9c1b51eSKate Stone if (target.GetSectionLoadList().SetSectionLoadAddress( 309b9c1b51eSKate Stone section_sp, section_sp->GetFileAddress() + value)) 3102756adf3SVirgile Bello ++num_loaded_sections; 3112756adf3SVirgile Bello } 3122756adf3SVirgile Bello } 3132756adf3SVirgile Bello changed = num_loaded_sections > 0; 3142756adf3SVirgile Bello } 3152756adf3SVirgile Bello } 3162756adf3SVirgile Bello return changed; 3172756adf3SVirgile Bello } 3182756adf3SVirgile Bello 319b9c1b51eSKate Stone ByteOrder ObjectFilePECOFF::GetByteOrder() const { return eByteOrderLittle; } 320f754f88fSGreg Clayton 321b9c1b51eSKate Stone bool ObjectFilePECOFF::IsExecutable() const { 322237ad974SCharles Davis return (m_coff_header.flags & llvm::COFF::IMAGE_FILE_DLL) == 0; 323f754f88fSGreg Clayton } 324f754f88fSGreg Clayton 325b9c1b51eSKate Stone uint32_t ObjectFilePECOFF::GetAddressByteSize() const { 326f754f88fSGreg Clayton if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32_PLUS) 327f754f88fSGreg Clayton return 8; 328f754f88fSGreg Clayton else if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32) 329f754f88fSGreg Clayton return 4; 330f754f88fSGreg Clayton return 4; 331f754f88fSGreg Clayton } 332f754f88fSGreg Clayton 333f754f88fSGreg Clayton // NeedsEndianSwap 334f754f88fSGreg Clayton // 33505097246SAdrian Prantl // Return true if an endian swap needs to occur when extracting data from this 33605097246SAdrian Prantl // file. 337b9c1b51eSKate Stone bool ObjectFilePECOFF::NeedsEndianSwap() const { 338f754f88fSGreg Clayton #if defined(__LITTLE_ENDIAN__) 339f754f88fSGreg Clayton return false; 340f754f88fSGreg Clayton #else 341f754f88fSGreg Clayton return true; 342f754f88fSGreg Clayton #endif 343f754f88fSGreg Clayton } 344f754f88fSGreg Clayton // ParseDOSHeader 345b9c1b51eSKate Stone bool ObjectFilePECOFF::ParseDOSHeader(DataExtractor &data, 346b9c1b51eSKate Stone dos_header_t &dos_header) { 347f754f88fSGreg Clayton bool success = false; 348c7bece56SGreg Clayton lldb::offset_t offset = 0; 34989eb1baeSVirgile Bello success = data.ValidOffsetForDataOfSize(0, sizeof(dos_header)); 350f754f88fSGreg Clayton 351b9c1b51eSKate Stone if (success) { 35289eb1baeSVirgile Bello dos_header.e_magic = data.GetU16(&offset); // Magic number 35389eb1baeSVirgile Bello success = dos_header.e_magic == IMAGE_DOS_SIGNATURE; 354f754f88fSGreg Clayton 355b9c1b51eSKate Stone if (success) { 35689eb1baeSVirgile Bello dos_header.e_cblp = data.GetU16(&offset); // Bytes on last page of file 35789eb1baeSVirgile Bello dos_header.e_cp = data.GetU16(&offset); // Pages in file 35889eb1baeSVirgile Bello dos_header.e_crlc = data.GetU16(&offset); // Relocations 359b9c1b51eSKate Stone dos_header.e_cparhdr = 360b9c1b51eSKate Stone data.GetU16(&offset); // Size of header in paragraphs 361b9c1b51eSKate Stone dos_header.e_minalloc = 362b9c1b51eSKate Stone data.GetU16(&offset); // Minimum extra paragraphs needed 363b9c1b51eSKate Stone dos_header.e_maxalloc = 364b9c1b51eSKate Stone data.GetU16(&offset); // Maximum extra paragraphs needed 36589eb1baeSVirgile Bello dos_header.e_ss = data.GetU16(&offset); // Initial (relative) SS value 36689eb1baeSVirgile Bello dos_header.e_sp = data.GetU16(&offset); // Initial SP value 36789eb1baeSVirgile Bello dos_header.e_csum = data.GetU16(&offset); // Checksum 36889eb1baeSVirgile Bello dos_header.e_ip = data.GetU16(&offset); // Initial IP value 36989eb1baeSVirgile Bello dos_header.e_cs = data.GetU16(&offset); // Initial (relative) CS value 370b9c1b51eSKate Stone dos_header.e_lfarlc = 371b9c1b51eSKate Stone data.GetU16(&offset); // File address of relocation table 37289eb1baeSVirgile Bello dos_header.e_ovno = data.GetU16(&offset); // Overlay number 373f754f88fSGreg Clayton 37489eb1baeSVirgile Bello dos_header.e_res[0] = data.GetU16(&offset); // Reserved words 37589eb1baeSVirgile Bello dos_header.e_res[1] = data.GetU16(&offset); // Reserved words 37689eb1baeSVirgile Bello dos_header.e_res[2] = data.GetU16(&offset); // Reserved words 37789eb1baeSVirgile Bello dos_header.e_res[3] = data.GetU16(&offset); // Reserved words 378f754f88fSGreg Clayton 379b9c1b51eSKate Stone dos_header.e_oemid = 380b9c1b51eSKate Stone data.GetU16(&offset); // OEM identifier (for e_oeminfo) 381b9c1b51eSKate Stone dos_header.e_oeminfo = 382b9c1b51eSKate Stone data.GetU16(&offset); // OEM information; e_oemid specific 38389eb1baeSVirgile Bello dos_header.e_res2[0] = data.GetU16(&offset); // Reserved words 38489eb1baeSVirgile Bello dos_header.e_res2[1] = data.GetU16(&offset); // Reserved words 38589eb1baeSVirgile Bello dos_header.e_res2[2] = data.GetU16(&offset); // Reserved words 38689eb1baeSVirgile Bello dos_header.e_res2[3] = data.GetU16(&offset); // Reserved words 38789eb1baeSVirgile Bello dos_header.e_res2[4] = data.GetU16(&offset); // Reserved words 38889eb1baeSVirgile Bello dos_header.e_res2[5] = data.GetU16(&offset); // Reserved words 38989eb1baeSVirgile Bello dos_header.e_res2[6] = data.GetU16(&offset); // Reserved words 39089eb1baeSVirgile Bello dos_header.e_res2[7] = data.GetU16(&offset); // Reserved words 39189eb1baeSVirgile Bello dos_header.e_res2[8] = data.GetU16(&offset); // Reserved words 39289eb1baeSVirgile Bello dos_header.e_res2[9] = data.GetU16(&offset); // Reserved words 393f754f88fSGreg Clayton 394b9c1b51eSKate Stone dos_header.e_lfanew = 395b9c1b51eSKate Stone data.GetU32(&offset); // File address of new exe header 396f754f88fSGreg Clayton } 397f754f88fSGreg Clayton } 398f754f88fSGreg Clayton if (!success) 39989eb1baeSVirgile Bello memset(&dos_header, 0, sizeof(dos_header)); 400f754f88fSGreg Clayton return success; 401f754f88fSGreg Clayton } 402f754f88fSGreg Clayton 403f754f88fSGreg Clayton // ParserCOFFHeader 404b9c1b51eSKate Stone bool ObjectFilePECOFF::ParseCOFFHeader(DataExtractor &data, 405b9c1b51eSKate Stone lldb::offset_t *offset_ptr, 406b9c1b51eSKate Stone coff_header_t &coff_header) { 407b9c1b51eSKate Stone bool success = 408b9c1b51eSKate Stone data.ValidOffsetForDataOfSize(*offset_ptr, sizeof(coff_header)); 409b9c1b51eSKate Stone if (success) { 41089eb1baeSVirgile Bello coff_header.machine = data.GetU16(offset_ptr); 41189eb1baeSVirgile Bello coff_header.nsects = data.GetU16(offset_ptr); 41289eb1baeSVirgile Bello coff_header.modtime = data.GetU32(offset_ptr); 41389eb1baeSVirgile Bello coff_header.symoff = data.GetU32(offset_ptr); 41489eb1baeSVirgile Bello coff_header.nsyms = data.GetU32(offset_ptr); 41589eb1baeSVirgile Bello coff_header.hdrsize = data.GetU16(offset_ptr); 41689eb1baeSVirgile Bello coff_header.flags = data.GetU16(offset_ptr); 417f754f88fSGreg Clayton } 418f754f88fSGreg Clayton if (!success) 41989eb1baeSVirgile Bello memset(&coff_header, 0, sizeof(coff_header)); 420f754f88fSGreg Clayton return success; 421f754f88fSGreg Clayton } 422f754f88fSGreg Clayton 423b9c1b51eSKate Stone bool ObjectFilePECOFF::ParseCOFFOptionalHeader(lldb::offset_t *offset_ptr) { 424f754f88fSGreg Clayton bool success = false; 425c7bece56SGreg Clayton const lldb::offset_t end_offset = *offset_ptr + m_coff_header.hdrsize; 426b9c1b51eSKate Stone if (*offset_ptr < end_offset) { 427f754f88fSGreg Clayton success = true; 428f754f88fSGreg Clayton m_coff_header_opt.magic = m_data.GetU16(offset_ptr); 429f754f88fSGreg Clayton m_coff_header_opt.major_linker_version = m_data.GetU8(offset_ptr); 430f754f88fSGreg Clayton m_coff_header_opt.minor_linker_version = m_data.GetU8(offset_ptr); 431f754f88fSGreg Clayton m_coff_header_opt.code_size = m_data.GetU32(offset_ptr); 432f754f88fSGreg Clayton m_coff_header_opt.data_size = m_data.GetU32(offset_ptr); 433f754f88fSGreg Clayton m_coff_header_opt.bss_size = m_data.GetU32(offset_ptr); 434f754f88fSGreg Clayton m_coff_header_opt.entry = m_data.GetU32(offset_ptr); 435f754f88fSGreg Clayton m_coff_header_opt.code_offset = m_data.GetU32(offset_ptr); 436f754f88fSGreg Clayton 437f754f88fSGreg Clayton const uint32_t addr_byte_size = GetAddressByteSize(); 438f754f88fSGreg Clayton 439b9c1b51eSKate Stone if (*offset_ptr < end_offset) { 440b9c1b51eSKate Stone if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32) { 441f754f88fSGreg Clayton // PE32 only 442f754f88fSGreg Clayton m_coff_header_opt.data_offset = m_data.GetU32(offset_ptr); 443b9c1b51eSKate Stone } else 444f754f88fSGreg Clayton m_coff_header_opt.data_offset = 0; 445f754f88fSGreg Clayton 446b9c1b51eSKate Stone if (*offset_ptr < end_offset) { 447b9c1b51eSKate Stone m_coff_header_opt.image_base = 448b9c1b51eSKate Stone m_data.GetMaxU64(offset_ptr, addr_byte_size); 449f754f88fSGreg Clayton m_coff_header_opt.sect_alignment = m_data.GetU32(offset_ptr); 450f754f88fSGreg Clayton m_coff_header_opt.file_alignment = m_data.GetU32(offset_ptr); 451f754f88fSGreg Clayton m_coff_header_opt.major_os_system_version = m_data.GetU16(offset_ptr); 452f754f88fSGreg Clayton m_coff_header_opt.minor_os_system_version = m_data.GetU16(offset_ptr); 453f754f88fSGreg Clayton m_coff_header_opt.major_image_version = m_data.GetU16(offset_ptr); 454f754f88fSGreg Clayton m_coff_header_opt.minor_image_version = m_data.GetU16(offset_ptr); 455f754f88fSGreg Clayton m_coff_header_opt.major_subsystem_version = m_data.GetU16(offset_ptr); 456f754f88fSGreg Clayton m_coff_header_opt.minor_subsystem_version = m_data.GetU16(offset_ptr); 457f754f88fSGreg Clayton m_coff_header_opt.reserved1 = m_data.GetU32(offset_ptr); 458f754f88fSGreg Clayton m_coff_header_opt.image_size = m_data.GetU32(offset_ptr); 459f754f88fSGreg Clayton m_coff_header_opt.header_size = m_data.GetU32(offset_ptr); 46028469ca3SGreg Clayton m_coff_header_opt.checksum = m_data.GetU32(offset_ptr); 461f754f88fSGreg Clayton m_coff_header_opt.subsystem = m_data.GetU16(offset_ptr); 462f754f88fSGreg Clayton m_coff_header_opt.dll_flags = m_data.GetU16(offset_ptr); 463b9c1b51eSKate Stone m_coff_header_opt.stack_reserve_size = 464b9c1b51eSKate Stone m_data.GetMaxU64(offset_ptr, addr_byte_size); 465b9c1b51eSKate Stone m_coff_header_opt.stack_commit_size = 466b9c1b51eSKate Stone m_data.GetMaxU64(offset_ptr, addr_byte_size); 467b9c1b51eSKate Stone m_coff_header_opt.heap_reserve_size = 468b9c1b51eSKate Stone m_data.GetMaxU64(offset_ptr, addr_byte_size); 469b9c1b51eSKate Stone m_coff_header_opt.heap_commit_size = 470b9c1b51eSKate Stone m_data.GetMaxU64(offset_ptr, addr_byte_size); 471f754f88fSGreg Clayton m_coff_header_opt.loader_flags = m_data.GetU32(offset_ptr); 472f754f88fSGreg Clayton uint32_t num_data_dir_entries = m_data.GetU32(offset_ptr); 473f754f88fSGreg Clayton m_coff_header_opt.data_dirs.clear(); 474f754f88fSGreg Clayton m_coff_header_opt.data_dirs.resize(num_data_dir_entries); 475f754f88fSGreg Clayton uint32_t i; 476b9c1b51eSKate Stone for (i = 0; i < num_data_dir_entries; i++) { 477f754f88fSGreg Clayton m_coff_header_opt.data_dirs[i].vmaddr = m_data.GetU32(offset_ptr); 478f754f88fSGreg Clayton m_coff_header_opt.data_dirs[i].vmsize = m_data.GetU32(offset_ptr); 479f754f88fSGreg Clayton } 4802756adf3SVirgile Bello 4812756adf3SVirgile Bello m_image_base = m_coff_header_opt.image_base; 482f754f88fSGreg Clayton } 483f754f88fSGreg Clayton } 484f754f88fSGreg Clayton } 485f754f88fSGreg Clayton // Make sure we are on track for section data which follows 486f754f88fSGreg Clayton *offset_ptr = end_offset; 487f754f88fSGreg Clayton return success; 488f754f88fSGreg Clayton } 489f754f88fSGreg Clayton 49030c2441aSAleksandr Urakov uint32_t ObjectFilePECOFF::GetRVA(const Address &addr) const { 49130c2441aSAleksandr Urakov return addr.GetFileAddress() - m_image_base; 49230c2441aSAleksandr Urakov } 49330c2441aSAleksandr Urakov 49430c2441aSAleksandr Urakov Address ObjectFilePECOFF::GetAddress(uint32_t rva) { 49530c2441aSAleksandr Urakov SectionList *sect_list = GetSectionList(); 49630c2441aSAleksandr Urakov if (!sect_list) 49730c2441aSAleksandr Urakov return Address(GetFileAddress(rva)); 49830c2441aSAleksandr Urakov 49930c2441aSAleksandr Urakov return Address(GetFileAddress(rva), sect_list); 50030c2441aSAleksandr Urakov } 50130c2441aSAleksandr Urakov 50230c2441aSAleksandr Urakov lldb::addr_t ObjectFilePECOFF::GetFileAddress(uint32_t rva) const { 50330c2441aSAleksandr Urakov return m_image_base + rva; 50430c2441aSAleksandr Urakov } 50530c2441aSAleksandr Urakov 506344546bdSWalter Erquinigo DataExtractor ObjectFilePECOFF::ReadImageData(uint32_t offset, size_t size) { 50730c2441aSAleksandr Urakov if (!size) 50830c2441aSAleksandr Urakov return {}; 50930c2441aSAleksandr Urakov 510a4a00cedSFred Riss if (m_data.ValidOffsetForDataOfSize(offset, size)) 511a4a00cedSFred Riss return DataExtractor(m_data, offset, size); 512a4a00cedSFred Riss 513344546bdSWalter Erquinigo ProcessSP process_sp(m_process_wp.lock()); 514344546bdSWalter Erquinigo DataExtractor data; 515344546bdSWalter Erquinigo if (process_sp) { 516a8f3ae7cSJonas Devlieghere auto data_up = std::make_unique<DataBufferHeap>(size, 0); 51797206d57SZachary Turner Status readmem_error; 518344546bdSWalter Erquinigo size_t bytes_read = 519d5b44036SJonas Devlieghere process_sp->ReadMemory(m_image_base + offset, data_up->GetBytes(), 520d5b44036SJonas Devlieghere data_up->GetByteSize(), readmem_error); 521344546bdSWalter Erquinigo if (bytes_read == size) { 522d5b44036SJonas Devlieghere DataBufferSP buffer_sp(data_up.release()); 523344546bdSWalter Erquinigo data.SetData(buffer_sp, 0, buffer_sp->GetByteSize()); 524344546bdSWalter Erquinigo } 525344546bdSWalter Erquinigo } 526344546bdSWalter Erquinigo return data; 527344546bdSWalter Erquinigo } 528344546bdSWalter Erquinigo 52930c2441aSAleksandr Urakov DataExtractor ObjectFilePECOFF::ReadImageDataByRVA(uint32_t rva, size_t size) { 53030c2441aSAleksandr Urakov Address addr = GetAddress(rva); 5317e1a3076SMartin Storsjö SectionSP sect = addr.GetSection(); 5327e1a3076SMartin Storsjö if (!sect) 5337e1a3076SMartin Storsjö return {}; 5347e1a3076SMartin Storsjö rva = sect->GetFileOffset() + addr.GetOffset(); 53530c2441aSAleksandr Urakov 53630c2441aSAleksandr Urakov return ReadImageData(rva, size); 53730c2441aSAleksandr Urakov } 53830c2441aSAleksandr Urakov 539f754f88fSGreg Clayton // ParseSectionHeaders 540b9c1b51eSKate Stone bool ObjectFilePECOFF::ParseSectionHeaders( 541b9c1b51eSKate Stone uint32_t section_header_data_offset) { 542f754f88fSGreg Clayton const uint32_t nsects = m_coff_header.nsects; 543f754f88fSGreg Clayton m_sect_headers.clear(); 544f754f88fSGreg Clayton 545b9c1b51eSKate Stone if (nsects > 0) { 546f754f88fSGreg Clayton const size_t section_header_byte_size = nsects * sizeof(section_header_t); 547344546bdSWalter Erquinigo DataExtractor section_header_data = 548344546bdSWalter Erquinigo ReadImageData(section_header_data_offset, section_header_byte_size); 549f754f88fSGreg Clayton 550c7bece56SGreg Clayton lldb::offset_t offset = 0; 551b9c1b51eSKate Stone if (section_header_data.ValidOffsetForDataOfSize( 552b9c1b51eSKate Stone offset, section_header_byte_size)) { 553f754f88fSGreg Clayton m_sect_headers.resize(nsects); 554f754f88fSGreg Clayton 555b9c1b51eSKate Stone for (uint32_t idx = 0; idx < nsects; ++idx) { 556f754f88fSGreg Clayton const void *name_data = section_header_data.GetData(&offset, 8); 557b9c1b51eSKate Stone if (name_data) { 558f754f88fSGreg Clayton memcpy(m_sect_headers[idx].name, name_data, 8); 559f754f88fSGreg Clayton m_sect_headers[idx].vmsize = section_header_data.GetU32(&offset); 560f754f88fSGreg Clayton m_sect_headers[idx].vmaddr = section_header_data.GetU32(&offset); 561f754f88fSGreg Clayton m_sect_headers[idx].size = section_header_data.GetU32(&offset); 562f754f88fSGreg Clayton m_sect_headers[idx].offset = section_header_data.GetU32(&offset); 563f754f88fSGreg Clayton m_sect_headers[idx].reloff = section_header_data.GetU32(&offset); 564f754f88fSGreg Clayton m_sect_headers[idx].lineoff = section_header_data.GetU32(&offset); 565f754f88fSGreg Clayton m_sect_headers[idx].nreloc = section_header_data.GetU16(&offset); 566f754f88fSGreg Clayton m_sect_headers[idx].nline = section_header_data.GetU16(&offset); 567f754f88fSGreg Clayton m_sect_headers[idx].flags = section_header_data.GetU32(&offset); 568f754f88fSGreg Clayton } 569f754f88fSGreg Clayton } 570f754f88fSGreg Clayton } 571f754f88fSGreg Clayton } 572f754f88fSGreg Clayton 573a6682a41SJonas Devlieghere return !m_sect_headers.empty(); 574f754f88fSGreg Clayton } 575f754f88fSGreg Clayton 5762886e4a0SPavel Labath llvm::StringRef ObjectFilePECOFF::GetSectionName(const section_header_t §) { 5772886e4a0SPavel Labath llvm::StringRef hdr_name(sect.name, llvm::array_lengthof(sect.name)); 5782886e4a0SPavel Labath hdr_name = hdr_name.split('\0').first; 5792886e4a0SPavel Labath if (hdr_name.consume_front("/")) { 5802886e4a0SPavel Labath lldb::offset_t stroff; 5812886e4a0SPavel Labath if (!to_integer(hdr_name, stroff, 10)) 5822886e4a0SPavel Labath return ""; 583b9c1b51eSKate Stone lldb::offset_t string_file_offset = 584b9c1b51eSKate Stone m_coff_header.symoff + (m_coff_header.nsyms * 18) + stroff; 5852886e4a0SPavel Labath if (const char *name = m_data.GetCStr(&string_file_offset)) 5862886e4a0SPavel Labath return name; 5872886e4a0SPavel Labath return ""; 588f754f88fSGreg Clayton } 5892886e4a0SPavel Labath return hdr_name; 590f754f88fSGreg Clayton } 591f754f88fSGreg Clayton 592*7e6df41fSGreg Clayton void ObjectFilePECOFF::ParseSymtab(Symtab &symtab) { 593f754f88fSGreg Clayton SectionList *sect_list = GetSectionList(); 59428469ca3SGreg Clayton const uint32_t num_syms = m_coff_header.nsyms; 595344546bdSWalter Erquinigo if (m_file && num_syms > 0 && m_coff_header.symoff > 0) { 5960076e715SGreg Clayton const uint32_t symbol_size = 18; 59728469ca3SGreg Clayton const size_t symbol_data_size = num_syms * symbol_size; 598c35b91ceSAdrian McCarthy // Include the 4-byte string table size at the end of the symbols 599344546bdSWalter Erquinigo DataExtractor symtab_data = 600344546bdSWalter Erquinigo ReadImageData(m_coff_header.symoff, symbol_data_size + 4); 601c7bece56SGreg Clayton lldb::offset_t offset = symbol_data_size; 60228469ca3SGreg Clayton const uint32_t strtab_size = symtab_data.GetU32(&offset); 603344546bdSWalter Erquinigo if (strtab_size > 0) { 604344546bdSWalter Erquinigo DataExtractor strtab_data = ReadImageData( 605344546bdSWalter Erquinigo m_coff_header.symoff + symbol_data_size, strtab_size); 60628469ca3SGreg Clayton 60728469ca3SGreg Clayton offset = 0; 60828469ca3SGreg Clayton std::string symbol_name; 609*7e6df41fSGreg Clayton Symbol *symbols = symtab.Resize(num_syms); 610b9c1b51eSKate Stone for (uint32_t i = 0; i < num_syms; ++i) { 611f754f88fSGreg Clayton coff_symbol_t symbol; 61228469ca3SGreg Clayton const uint32_t symbol_offset = offset; 613248a1305SKonrad Kleine const char *symbol_name_cstr = nullptr; 614c35b91ceSAdrian McCarthy // If the first 4 bytes of the symbol string are zero, then they 615c35b91ceSAdrian McCarthy // are followed by a 4-byte string table offset. Else these 61628469ca3SGreg Clayton // 8 bytes contain the symbol name 617b9c1b51eSKate Stone if (symtab_data.GetU32(&offset) == 0) { 61805097246SAdrian Prantl // Long string that doesn't fit into the symbol table name, so 61905097246SAdrian Prantl // now we must read the 4 byte string table offset 62028469ca3SGreg Clayton uint32_t strtab_offset = symtab_data.GetU32(&offset); 62128469ca3SGreg Clayton symbol_name_cstr = strtab_data.PeekCStr(strtab_offset); 62228469ca3SGreg Clayton symbol_name.assign(symbol_name_cstr); 623b9c1b51eSKate Stone } else { 624b9c1b51eSKate Stone // Short string that fits into the symbol table name which is 8 625b9c1b51eSKate Stone // bytes 62628469ca3SGreg Clayton offset += sizeof(symbol.name) - 4; // Skip remaining 62728469ca3SGreg Clayton symbol_name_cstr = symtab_data.PeekCStr(symbol_offset); 628248a1305SKonrad Kleine if (symbol_name_cstr == nullptr) 629f754f88fSGreg Clayton break; 63028469ca3SGreg Clayton symbol_name.assign(symbol_name_cstr, sizeof(symbol.name)); 63128469ca3SGreg Clayton } 63228469ca3SGreg Clayton symbol.value = symtab_data.GetU32(&offset); 63328469ca3SGreg Clayton symbol.sect = symtab_data.GetU16(&offset); 63428469ca3SGreg Clayton symbol.type = symtab_data.GetU16(&offset); 63528469ca3SGreg Clayton symbol.storage = symtab_data.GetU8(&offset); 63628469ca3SGreg Clayton symbol.naux = symtab_data.GetU8(&offset); 637037520e9SGreg Clayton symbols[i].GetMangled().SetValue(ConstString(symbol_name.c_str())); 638b9c1b51eSKate Stone if ((int16_t)symbol.sect >= 1) { 6394394b5beSMartin Storsjö Address symbol_addr(sect_list->FindSectionByID(symbol.sect), 640b9c1b51eSKate Stone symbol.value); 641358cf1eaSGreg Clayton symbols[i].GetAddressRef() = symbol_addr; 642c35b91ceSAdrian McCarthy symbols[i].SetType(MapSymbolType(symbol.type)); 6430076e715SGreg Clayton } 644f754f88fSGreg Clayton 645b9c1b51eSKate Stone if (symbol.naux > 0) { 646f754f88fSGreg Clayton i += symbol.naux; 647f07ddbc9SMartin Storsjö offset += symbol.naux * symbol_size; 6480076e715SGreg Clayton } 649f754f88fSGreg Clayton } 650f754f88fSGreg Clayton } 651344546bdSWalter Erquinigo } 652a4fe3a12SVirgile Bello 653a4fe3a12SVirgile Bello // Read export header 654b9c1b51eSKate Stone if (coff_data_dir_export_table < m_coff_header_opt.data_dirs.size() && 655b9c1b51eSKate Stone m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmsize > 0 && 656b9c1b51eSKate Stone m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmaddr > 0) { 657a4fe3a12SVirgile Bello export_directory_entry export_table; 658b9c1b51eSKate Stone uint32_t data_start = 659b9c1b51eSKate Stone m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmaddr; 660344546bdSWalter Erquinigo 66130c2441aSAleksandr Urakov DataExtractor symtab_data = ReadImageDataByRVA( 66230c2441aSAleksandr Urakov data_start, m_coff_header_opt.data_dirs[0].vmsize); 663a4fe3a12SVirgile Bello lldb::offset_t offset = 0; 664a4fe3a12SVirgile Bello 665a4fe3a12SVirgile Bello // Read export_table header 666a4fe3a12SVirgile Bello export_table.characteristics = symtab_data.GetU32(&offset); 667a4fe3a12SVirgile Bello export_table.time_date_stamp = symtab_data.GetU32(&offset); 668a4fe3a12SVirgile Bello export_table.major_version = symtab_data.GetU16(&offset); 669a4fe3a12SVirgile Bello export_table.minor_version = symtab_data.GetU16(&offset); 670a4fe3a12SVirgile Bello export_table.name = symtab_data.GetU32(&offset); 671a4fe3a12SVirgile Bello export_table.base = symtab_data.GetU32(&offset); 672a4fe3a12SVirgile Bello export_table.number_of_functions = symtab_data.GetU32(&offset); 673a4fe3a12SVirgile Bello export_table.number_of_names = symtab_data.GetU32(&offset); 674a4fe3a12SVirgile Bello export_table.address_of_functions = symtab_data.GetU32(&offset); 675a4fe3a12SVirgile Bello export_table.address_of_names = symtab_data.GetU32(&offset); 676a4fe3a12SVirgile Bello export_table.address_of_name_ordinals = symtab_data.GetU32(&offset); 677a4fe3a12SVirgile Bello 678a4fe3a12SVirgile Bello bool has_ordinal = export_table.address_of_name_ordinals != 0; 679a4fe3a12SVirgile Bello 680a4fe3a12SVirgile Bello lldb::offset_t name_offset = export_table.address_of_names - data_start; 681b9c1b51eSKate Stone lldb::offset_t name_ordinal_offset = 682b9c1b51eSKate Stone export_table.address_of_name_ordinals - data_start; 683a4fe3a12SVirgile Bello 684*7e6df41fSGreg Clayton Symbol *symbols = symtab.Resize(export_table.number_of_names); 685a4fe3a12SVirgile Bello 686a4fe3a12SVirgile Bello std::string symbol_name; 687a4fe3a12SVirgile Bello 688a4fe3a12SVirgile Bello // Read each export table entry 689b9c1b51eSKate Stone for (size_t i = 0; i < export_table.number_of_names; ++i) { 690b9c1b51eSKate Stone uint32_t name_ordinal = 691b9c1b51eSKate Stone has_ordinal ? symtab_data.GetU16(&name_ordinal_offset) : i; 692a4fe3a12SVirgile Bello uint32_t name_address = symtab_data.GetU32(&name_offset); 693a4fe3a12SVirgile Bello 694b9c1b51eSKate Stone const char *symbol_name_cstr = 695b9c1b51eSKate Stone symtab_data.PeekCStr(name_address - data_start); 696a4fe3a12SVirgile Bello symbol_name.assign(symbol_name_cstr); 697a4fe3a12SVirgile Bello 698b9c1b51eSKate Stone lldb::offset_t function_offset = export_table.address_of_functions - 699b9c1b51eSKate Stone data_start + 700b9c1b51eSKate Stone sizeof(uint32_t) * name_ordinal; 701a4fe3a12SVirgile Bello uint32_t function_rva = symtab_data.GetU32(&function_offset); 702a4fe3a12SVirgile Bello 703b9c1b51eSKate Stone Address symbol_addr(m_coff_header_opt.image_base + function_rva, 704b9c1b51eSKate Stone sect_list); 705a4fe3a12SVirgile Bello symbols[i].GetMangled().SetValue(ConstString(symbol_name.c_str())); 706358cf1eaSGreg Clayton symbols[i].GetAddressRef() = symbol_addr; 707a4fe3a12SVirgile Bello symbols[i].SetType(lldb::eSymbolTypeCode); 708a4fe3a12SVirgile Bello symbols[i].SetDebug(true); 709a4fe3a12SVirgile Bello } 710a4fe3a12SVirgile Bello } 711f754f88fSGreg Clayton } 712f754f88fSGreg Clayton 71330c2441aSAleksandr Urakov std::unique_ptr<CallFrameInfo> ObjectFilePECOFF::CreateCallFrameInfo() { 71430c2441aSAleksandr Urakov if (coff_data_dir_exception_table >= m_coff_header_opt.data_dirs.size()) 71530c2441aSAleksandr Urakov return {}; 71630c2441aSAleksandr Urakov 71730c2441aSAleksandr Urakov data_directory data_dir_exception = 71830c2441aSAleksandr Urakov m_coff_header_opt.data_dirs[coff_data_dir_exception_table]; 71930c2441aSAleksandr Urakov if (!data_dir_exception.vmaddr) 72030c2441aSAleksandr Urakov return {}; 72130c2441aSAleksandr Urakov 722aa786b88SMartin Storsjö if (m_coff_header.machine != llvm::COFF::IMAGE_FILE_MACHINE_AMD64) 723aa786b88SMartin Storsjö return {}; 724aa786b88SMartin Storsjö 72530c2441aSAleksandr Urakov return std::make_unique<PECallFrameInfo>(*this, data_dir_exception.vmaddr, 72630c2441aSAleksandr Urakov data_dir_exception.vmsize); 72730c2441aSAleksandr Urakov } 72830c2441aSAleksandr Urakov 729b9c1b51eSKate Stone bool ObjectFilePECOFF::IsStripped() { 7303046e668SGreg Clayton // TODO: determine this for COFF 7313046e668SGreg Clayton return false; 7323046e668SGreg Clayton } 7333046e668SGreg Clayton 7342e5bb6d8SMartin Storsjö SectionType ObjectFilePECOFF::GetSectionType(llvm::StringRef sect_name, 7352e5bb6d8SMartin Storsjö const section_header_t §) { 7362e5bb6d8SMartin Storsjö ConstString const_sect_name(sect_name); 7372e5bb6d8SMartin Storsjö static ConstString g_code_sect_name(".code"); 7382e5bb6d8SMartin Storsjö static ConstString g_CODE_sect_name("CODE"); 7392e5bb6d8SMartin Storsjö static ConstString g_data_sect_name(".data"); 7402e5bb6d8SMartin Storsjö static ConstString g_DATA_sect_name("DATA"); 7412e5bb6d8SMartin Storsjö static ConstString g_bss_sect_name(".bss"); 7422e5bb6d8SMartin Storsjö static ConstString g_BSS_sect_name("BSS"); 7432e5bb6d8SMartin Storsjö 7442e5bb6d8SMartin Storsjö if (sect.flags & llvm::COFF::IMAGE_SCN_CNT_CODE && 7452e5bb6d8SMartin Storsjö ((const_sect_name == g_code_sect_name) || 7462e5bb6d8SMartin Storsjö (const_sect_name == g_CODE_sect_name))) { 7472e5bb6d8SMartin Storsjö return eSectionTypeCode; 7482e5bb6d8SMartin Storsjö } 7492e5bb6d8SMartin Storsjö if (sect.flags & llvm::COFF::IMAGE_SCN_CNT_INITIALIZED_DATA && 7502e5bb6d8SMartin Storsjö ((const_sect_name == g_data_sect_name) || 7512e5bb6d8SMartin Storsjö (const_sect_name == g_DATA_sect_name))) { 7522e5bb6d8SMartin Storsjö if (sect.size == 0 && sect.offset == 0) 7532e5bb6d8SMartin Storsjö return eSectionTypeZeroFill; 7542e5bb6d8SMartin Storsjö else 7552e5bb6d8SMartin Storsjö return eSectionTypeData; 7562e5bb6d8SMartin Storsjö } 7572e5bb6d8SMartin Storsjö if (sect.flags & llvm::COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA && 7582e5bb6d8SMartin Storsjö ((const_sect_name == g_bss_sect_name) || 7592e5bb6d8SMartin Storsjö (const_sect_name == g_BSS_sect_name))) { 7602e5bb6d8SMartin Storsjö if (sect.size == 0) 7612e5bb6d8SMartin Storsjö return eSectionTypeZeroFill; 7622e5bb6d8SMartin Storsjö else 7632e5bb6d8SMartin Storsjö return eSectionTypeData; 7642e5bb6d8SMartin Storsjö } 7652e5bb6d8SMartin Storsjö 7662e5bb6d8SMartin Storsjö SectionType section_type = 7672e5bb6d8SMartin Storsjö llvm::StringSwitch<SectionType>(sect_name) 7682e5bb6d8SMartin Storsjö .Case(".debug", eSectionTypeDebug) 7692e5bb6d8SMartin Storsjö .Case(".stabstr", eSectionTypeDataCString) 7702e5bb6d8SMartin Storsjö .Case(".reloc", eSectionTypeOther) 7712e5bb6d8SMartin Storsjö .Case(".debug_abbrev", eSectionTypeDWARFDebugAbbrev) 7722e5bb6d8SMartin Storsjö .Case(".debug_aranges", eSectionTypeDWARFDebugAranges) 7732e5bb6d8SMartin Storsjö .Case(".debug_frame", eSectionTypeDWARFDebugFrame) 7742e5bb6d8SMartin Storsjö .Case(".debug_info", eSectionTypeDWARFDebugInfo) 7752e5bb6d8SMartin Storsjö .Case(".debug_line", eSectionTypeDWARFDebugLine) 7762e5bb6d8SMartin Storsjö .Case(".debug_loc", eSectionTypeDWARFDebugLoc) 7772e5bb6d8SMartin Storsjö .Case(".debug_loclists", eSectionTypeDWARFDebugLocLists) 7782e5bb6d8SMartin Storsjö .Case(".debug_macinfo", eSectionTypeDWARFDebugMacInfo) 7792e5bb6d8SMartin Storsjö .Case(".debug_names", eSectionTypeDWARFDebugNames) 7802e5bb6d8SMartin Storsjö .Case(".debug_pubnames", eSectionTypeDWARFDebugPubNames) 7812e5bb6d8SMartin Storsjö .Case(".debug_pubtypes", eSectionTypeDWARFDebugPubTypes) 7822e5bb6d8SMartin Storsjö .Case(".debug_ranges", eSectionTypeDWARFDebugRanges) 7832e5bb6d8SMartin Storsjö .Case(".debug_str", eSectionTypeDWARFDebugStr) 7842e5bb6d8SMartin Storsjö .Case(".debug_types", eSectionTypeDWARFDebugTypes) 785934c025eSMartin Storsjö // .eh_frame can be truncated to 8 chars. 786934c025eSMartin Storsjö .Cases(".eh_frame", ".eh_fram", eSectionTypeEHFrame) 7872e5bb6d8SMartin Storsjö .Case(".gosymtab", eSectionTypeGoSymtab) 7882e5bb6d8SMartin Storsjö .Default(eSectionTypeInvalid); 7892e5bb6d8SMartin Storsjö if (section_type != eSectionTypeInvalid) 7902e5bb6d8SMartin Storsjö return section_type; 7912e5bb6d8SMartin Storsjö 7922e5bb6d8SMartin Storsjö if (sect.flags & llvm::COFF::IMAGE_SCN_CNT_CODE) 7932e5bb6d8SMartin Storsjö return eSectionTypeCode; 7942e5bb6d8SMartin Storsjö if (sect.flags & llvm::COFF::IMAGE_SCN_CNT_INITIALIZED_DATA) 7952e5bb6d8SMartin Storsjö return eSectionTypeData; 7962e5bb6d8SMartin Storsjö if (sect.flags & llvm::COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) { 7972e5bb6d8SMartin Storsjö if (sect.size == 0) 7982e5bb6d8SMartin Storsjö return eSectionTypeZeroFill; 7992e5bb6d8SMartin Storsjö else 8002e5bb6d8SMartin Storsjö return eSectionTypeData; 8012e5bb6d8SMartin Storsjö } 8022e5bb6d8SMartin Storsjö return eSectionTypeOther; 8032e5bb6d8SMartin Storsjö } 8042e5bb6d8SMartin Storsjö 805b9c1b51eSKate Stone void ObjectFilePECOFF::CreateSections(SectionList &unified_section_list) { 806d5b44036SJonas Devlieghere if (m_sections_up) 80788a2c2a4SPavel Labath return; 80806412daeSJonas Devlieghere m_sections_up = std::make_unique<SectionList>(); 809a1743499SGreg Clayton ModuleSP module_sp(GetModule()); 810b9c1b51eSKate Stone if (module_sp) { 81116ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 8127db8b5c4SPavel Labath 81373a7a55cSPavel Labath SectionSP header_sp = std::make_shared<Section>( 81473a7a55cSPavel Labath module_sp, this, ~user_id_t(0), ConstString("PECOFF header"), 81573a7a55cSPavel Labath eSectionTypeOther, m_coff_header_opt.image_base, 81673a7a55cSPavel Labath m_coff_header_opt.header_size, 81773a7a55cSPavel Labath /*file_offset*/ 0, m_coff_header_opt.header_size, 81873a7a55cSPavel Labath m_coff_header_opt.sect_alignment, 8197db8b5c4SPavel Labath /*flags*/ 0); 820f1e0ae34SPavel Labath header_sp->SetPermissions(ePermissionsReadable); 82173a7a55cSPavel Labath m_sections_up->AddSection(header_sp); 82273a7a55cSPavel Labath unified_section_list.AddSection(header_sp); 8237db8b5c4SPavel Labath 824f754f88fSGreg Clayton const uint32_t nsects = m_sect_headers.size(); 825e72dfb32SGreg Clayton ModuleSP module_sp(GetModule()); 826b9c1b51eSKate Stone for (uint32_t idx = 0; idx < nsects; ++idx) { 8272e5bb6d8SMartin Storsjö llvm::StringRef sect_name = GetSectionName(m_sect_headers[idx]); 8282e5bb6d8SMartin Storsjö ConstString const_sect_name(sect_name); 8292e5bb6d8SMartin Storsjö SectionType section_type = GetSectionType(sect_name, m_sect_headers[idx]); 830f754f88fSGreg Clayton 831b9c1b51eSKate Stone SectionSP section_sp(new Section( 832b9c1b51eSKate Stone module_sp, // Module to which this section belongs 833a7499c98SMichael Sartain this, // Object file to which this section belongs 8347db8b5c4SPavel Labath idx + 1, // Section ID is the 1 based section index. 835f754f88fSGreg Clayton const_sect_name, // Name of this section 8367db8b5c4SPavel Labath section_type, 83773a7a55cSPavel Labath m_coff_header_opt.image_base + 838b9c1b51eSKate Stone m_sect_headers[idx].vmaddr, // File VM address == addresses as 839b9c1b51eSKate Stone // they are found in the object file 840f754f88fSGreg Clayton m_sect_headers[idx].vmsize, // VM size in bytes of this section 841b9c1b51eSKate Stone m_sect_headers[idx] 842b9c1b51eSKate Stone .offset, // Offset to the data for this section in the file 843b9c1b51eSKate Stone m_sect_headers[idx] 844b9c1b51eSKate Stone .size, // Size in bytes of this section as found in the file 84548672afbSGreg Clayton m_coff_header_opt.sect_alignment, // Section alignment 846f754f88fSGreg Clayton m_sect_headers[idx].flags)); // Flags for this section 847f754f88fSGreg Clayton 848f1e0ae34SPavel Labath uint32_t permissions = 0; 849f1e0ae34SPavel Labath if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_MEM_EXECUTE) 850f1e0ae34SPavel Labath permissions |= ePermissionsExecutable; 851f1e0ae34SPavel Labath if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_MEM_READ) 852f1e0ae34SPavel Labath permissions |= ePermissionsReadable; 853f1e0ae34SPavel Labath if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_MEM_WRITE) 854f1e0ae34SPavel Labath permissions |= ePermissionsWritable; 855f1e0ae34SPavel Labath section_sp->SetPermissions(permissions); 856f1e0ae34SPavel Labath 85773a7a55cSPavel Labath m_sections_up->AddSection(section_sp); 85873a7a55cSPavel Labath unified_section_list.AddSection(section_sp); 859f754f88fSGreg Clayton } 860f754f88fSGreg Clayton } 861a1743499SGreg Clayton } 862f754f88fSGreg Clayton 863b8d03935SAaron Smith UUID ObjectFilePECOFF::GetUUID() { 864b8d03935SAaron Smith if (m_uuid.IsValid()) 865b8d03935SAaron Smith return m_uuid; 866b8d03935SAaron Smith 867b8d03935SAaron Smith if (!CreateBinary()) 868b8d03935SAaron Smith return UUID(); 869b8d03935SAaron Smith 870d372a8e8SPavel Labath m_uuid = GetCoffUUID(*m_binary); 871b8d03935SAaron Smith return m_uuid; 872b8d03935SAaron Smith } 873f754f88fSGreg Clayton 874037ed1beSAaron Smith uint32_t ObjectFilePECOFF::ParseDependentModules() { 875037ed1beSAaron Smith ModuleSP module_sp(GetModule()); 876037ed1beSAaron Smith if (!module_sp) 877f754f88fSGreg Clayton return 0; 878037ed1beSAaron Smith 879037ed1beSAaron Smith std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 880037ed1beSAaron Smith if (m_deps_filespec) 881037ed1beSAaron Smith return m_deps_filespec->GetSize(); 882037ed1beSAaron Smith 883037ed1beSAaron Smith // Cache coff binary if it is not done yet. 884037ed1beSAaron Smith if (!CreateBinary()) 885037ed1beSAaron Smith return 0; 886037ed1beSAaron Smith 887037ed1beSAaron Smith Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT)); 888d372a8e8SPavel Labath LLDB_LOG(log, "this = {0}, module = {1} ({2}), file = {3}, binary = {4}", 889d372a8e8SPavel Labath this, GetModule().get(), GetModule()->GetSpecificationDescription(), 890d372a8e8SPavel Labath m_file.GetPath(), m_binary.get()); 891037ed1beSAaron Smith 892037ed1beSAaron Smith m_deps_filespec = FileSpecList(); 893037ed1beSAaron Smith 894d372a8e8SPavel Labath for (const auto &entry : m_binary->import_directories()) { 895037ed1beSAaron Smith llvm::StringRef dll_name; 896037ed1beSAaron Smith // Report a bogus entry. 8971c03389cSReid Kleckner if (llvm::Error e = entry.getName(dll_name)) { 89863e5fb76SJonas Devlieghere LLDB_LOGF(log, 89963e5fb76SJonas Devlieghere "ObjectFilePECOFF::ParseDependentModules() - failed to get " 900037ed1beSAaron Smith "import directory entry name: %s", 9011c03389cSReid Kleckner llvm::toString(std::move(e)).c_str()); 902037ed1beSAaron Smith continue; 903037ed1beSAaron Smith } 904037ed1beSAaron Smith 905037ed1beSAaron Smith // At this moment we only have the base name of the DLL. The full path can 906037ed1beSAaron Smith // only be seen after the dynamic loading. Our best guess is Try to get it 907037ed1beSAaron Smith // with the help of the object file's directory. 908b3f44ad9SStella Stamenova llvm::SmallString<128> dll_fullpath; 909037ed1beSAaron Smith FileSpec dll_specs(dll_name); 910037ed1beSAaron Smith dll_specs.GetDirectory().SetString(m_file.GetDirectory().GetCString()); 911037ed1beSAaron Smith 912037ed1beSAaron Smith if (!llvm::sys::fs::real_path(dll_specs.GetPath(), dll_fullpath)) 913f893d5bfSJonas Devlieghere m_deps_filespec->EmplaceBack(dll_fullpath); 914037ed1beSAaron Smith else { 915037ed1beSAaron Smith // Known DLLs or DLL not found in the object file directory. 916f893d5bfSJonas Devlieghere m_deps_filespec->EmplaceBack(dll_name); 917037ed1beSAaron Smith } 918037ed1beSAaron Smith } 919037ed1beSAaron Smith return m_deps_filespec->GetSize(); 920037ed1beSAaron Smith } 921037ed1beSAaron Smith 922037ed1beSAaron Smith uint32_t ObjectFilePECOFF::GetDependentModules(FileSpecList &files) { 923037ed1beSAaron Smith auto num_modules = ParseDependentModules(); 924037ed1beSAaron Smith auto original_size = files.GetSize(); 925037ed1beSAaron Smith 926037ed1beSAaron Smith for (unsigned i = 0; i < num_modules; ++i) 927037ed1beSAaron Smith files.AppendIfUnique(m_deps_filespec->GetFileSpecAtIndex(i)); 928037ed1beSAaron Smith 929037ed1beSAaron Smith return files.GetSize() - original_size; 930f754f88fSGreg Clayton } 931f754f88fSGreg Clayton 932b9c1b51eSKate Stone lldb_private::Address ObjectFilePECOFF::GetEntryPointAddress() { 9338e38c666SStephane Sezer if (m_entry_point_address.IsValid()) 9348e38c666SStephane Sezer return m_entry_point_address; 9358e38c666SStephane Sezer 9368e38c666SStephane Sezer if (!ParseHeader() || !IsExecutable()) 9378e38c666SStephane Sezer return m_entry_point_address; 9388e38c666SStephane Sezer 9398e38c666SStephane Sezer SectionList *section_list = GetSectionList(); 940a5235af9SAleksandr Urakov addr_t file_addr = m_coff_header_opt.entry + m_coff_header_opt.image_base; 9418e38c666SStephane Sezer 9428e38c666SStephane Sezer if (!section_list) 943a5235af9SAleksandr Urakov m_entry_point_address.SetOffset(file_addr); 9448e38c666SStephane Sezer else 945b8d03935SAaron Smith m_entry_point_address.ResolveAddressUsingFileSections(file_addr, 946b8d03935SAaron Smith section_list); 9478e38c666SStephane Sezer return m_entry_point_address; 9488e38c666SStephane Sezer } 9498e38c666SStephane Sezer 950d1304bbaSPavel Labath Address ObjectFilePECOFF::GetBaseAddress() { 951d1304bbaSPavel Labath return Address(GetSectionList()->GetSectionAtIndex(0), 0); 952d1304bbaSPavel Labath } 953d1304bbaSPavel Labath 954f754f88fSGreg Clayton // Dump 955f754f88fSGreg Clayton // 956f754f88fSGreg Clayton // Dump the specifics of the runtime file container (such as any headers 957f754f88fSGreg Clayton // segments, sections, etc). 958b9c1b51eSKate Stone void ObjectFilePECOFF::Dump(Stream *s) { 959a1743499SGreg Clayton ModuleSP module_sp(GetModule()); 960b9c1b51eSKate Stone if (module_sp) { 96116ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 962324a1036SSaleem Abdulrasool s->Printf("%p: ", static_cast<void *>(this)); 963f754f88fSGreg Clayton s->Indent(); 964f754f88fSGreg Clayton s->PutCString("ObjectFilePECOFF"); 965f754f88fSGreg Clayton 966f760f5aeSPavel Labath ArchSpec header_arch = GetArchitecture(); 967f754f88fSGreg Clayton 968b9c1b51eSKate Stone *s << ", file = '" << m_file 969b9c1b51eSKate Stone << "', arch = " << header_arch.GetArchitectureName() << "\n"; 970f754f88fSGreg Clayton 9713046e668SGreg Clayton SectionList *sections = GetSectionList(); 9723046e668SGreg Clayton if (sections) 9733a168297SPavel Labath sections->Dump(s->AsRawOstream(), s->GetIndentLevel(), nullptr, true, 9743a168297SPavel Labath UINT32_MAX); 975f754f88fSGreg Clayton 976d5b44036SJonas Devlieghere if (m_symtab_up) 977248a1305SKonrad Kleine m_symtab_up->Dump(s, nullptr, eSortOrderNone); 978f754f88fSGreg Clayton 979f754f88fSGreg Clayton if (m_dos_header.e_magic) 980f754f88fSGreg Clayton DumpDOSHeader(s, m_dos_header); 981b9c1b51eSKate Stone if (m_coff_header.machine) { 982f754f88fSGreg Clayton DumpCOFFHeader(s, m_coff_header); 983f754f88fSGreg Clayton if (m_coff_header.hdrsize) 984f754f88fSGreg Clayton DumpOptCOFFHeader(s, m_coff_header_opt); 985f754f88fSGreg Clayton } 986f754f88fSGreg Clayton s->EOL(); 987f754f88fSGreg Clayton DumpSectionHeaders(s); 988f754f88fSGreg Clayton s->EOL(); 989037ed1beSAaron Smith 990037ed1beSAaron Smith DumpDependentModules(s); 991037ed1beSAaron Smith s->EOL(); 992f754f88fSGreg Clayton } 993a1743499SGreg Clayton } 994f754f88fSGreg Clayton 995f754f88fSGreg Clayton // DumpDOSHeader 996f754f88fSGreg Clayton // 997f754f88fSGreg Clayton // Dump the MS-DOS header to the specified output stream 998b9c1b51eSKate Stone void ObjectFilePECOFF::DumpDOSHeader(Stream *s, const dos_header_t &header) { 999f754f88fSGreg Clayton s->PutCString("MSDOS Header\n"); 1000f754f88fSGreg Clayton s->Printf(" e_magic = 0x%4.4x\n", header.e_magic); 1001f754f88fSGreg Clayton s->Printf(" e_cblp = 0x%4.4x\n", header.e_cblp); 1002f754f88fSGreg Clayton s->Printf(" e_cp = 0x%4.4x\n", header.e_cp); 1003f754f88fSGreg Clayton s->Printf(" e_crlc = 0x%4.4x\n", header.e_crlc); 1004f754f88fSGreg Clayton s->Printf(" e_cparhdr = 0x%4.4x\n", header.e_cparhdr); 1005f754f88fSGreg Clayton s->Printf(" e_minalloc = 0x%4.4x\n", header.e_minalloc); 1006f754f88fSGreg Clayton s->Printf(" e_maxalloc = 0x%4.4x\n", header.e_maxalloc); 1007f754f88fSGreg Clayton s->Printf(" e_ss = 0x%4.4x\n", header.e_ss); 1008f754f88fSGreg Clayton s->Printf(" e_sp = 0x%4.4x\n", header.e_sp); 1009f754f88fSGreg Clayton s->Printf(" e_csum = 0x%4.4x\n", header.e_csum); 1010f754f88fSGreg Clayton s->Printf(" e_ip = 0x%4.4x\n", header.e_ip); 1011f754f88fSGreg Clayton s->Printf(" e_cs = 0x%4.4x\n", header.e_cs); 1012f754f88fSGreg Clayton s->Printf(" e_lfarlc = 0x%4.4x\n", header.e_lfarlc); 1013f754f88fSGreg Clayton s->Printf(" e_ovno = 0x%4.4x\n", header.e_ovno); 1014f754f88fSGreg Clayton s->Printf(" e_res[4] = { 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x }\n", 1015b9c1b51eSKate Stone header.e_res[0], header.e_res[1], header.e_res[2], header.e_res[3]); 1016f754f88fSGreg Clayton s->Printf(" e_oemid = 0x%4.4x\n", header.e_oemid); 1017f754f88fSGreg Clayton s->Printf(" e_oeminfo = 0x%4.4x\n", header.e_oeminfo); 1018b9c1b51eSKate Stone s->Printf(" e_res2[10] = { 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, " 1019b9c1b51eSKate Stone "0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x }\n", 1020b9c1b51eSKate Stone header.e_res2[0], header.e_res2[1], header.e_res2[2], 1021b9c1b51eSKate Stone header.e_res2[3], header.e_res2[4], header.e_res2[5], 1022b9c1b51eSKate Stone header.e_res2[6], header.e_res2[7], header.e_res2[8], 1023f754f88fSGreg Clayton header.e_res2[9]); 1024f754f88fSGreg Clayton s->Printf(" e_lfanew = 0x%8.8x\n", header.e_lfanew); 1025f754f88fSGreg Clayton } 1026f754f88fSGreg Clayton 1027f754f88fSGreg Clayton // DumpCOFFHeader 1028f754f88fSGreg Clayton // 1029f754f88fSGreg Clayton // Dump the COFF header to the specified output stream 1030b9c1b51eSKate Stone void ObjectFilePECOFF::DumpCOFFHeader(Stream *s, const coff_header_t &header) { 1031f754f88fSGreg Clayton s->PutCString("COFF Header\n"); 1032f754f88fSGreg Clayton s->Printf(" machine = 0x%4.4x\n", header.machine); 1033f754f88fSGreg Clayton s->Printf(" nsects = 0x%4.4x\n", header.nsects); 1034f754f88fSGreg Clayton s->Printf(" modtime = 0x%8.8x\n", header.modtime); 1035f754f88fSGreg Clayton s->Printf(" symoff = 0x%8.8x\n", header.symoff); 1036f754f88fSGreg Clayton s->Printf(" nsyms = 0x%8.8x\n", header.nsyms); 1037f754f88fSGreg Clayton s->Printf(" hdrsize = 0x%4.4x\n", header.hdrsize); 1038f754f88fSGreg Clayton } 1039f754f88fSGreg Clayton 1040f754f88fSGreg Clayton // DumpOptCOFFHeader 1041f754f88fSGreg Clayton // 1042f754f88fSGreg Clayton // Dump the optional COFF header to the specified output stream 1043b9c1b51eSKate Stone void ObjectFilePECOFF::DumpOptCOFFHeader(Stream *s, 1044b9c1b51eSKate Stone const coff_opt_header_t &header) { 1045f754f88fSGreg Clayton s->PutCString("Optional COFF Header\n"); 1046f754f88fSGreg Clayton s->Printf(" magic = 0x%4.4x\n", header.magic); 1047b9c1b51eSKate Stone s->Printf(" major_linker_version = 0x%2.2x\n", 1048b9c1b51eSKate Stone header.major_linker_version); 1049b9c1b51eSKate Stone s->Printf(" minor_linker_version = 0x%2.2x\n", 1050b9c1b51eSKate Stone header.minor_linker_version); 1051f754f88fSGreg Clayton s->Printf(" code_size = 0x%8.8x\n", header.code_size); 1052f754f88fSGreg Clayton s->Printf(" data_size = 0x%8.8x\n", header.data_size); 1053f754f88fSGreg Clayton s->Printf(" bss_size = 0x%8.8x\n", header.bss_size); 1054f754f88fSGreg Clayton s->Printf(" entry = 0x%8.8x\n", header.entry); 1055f754f88fSGreg Clayton s->Printf(" code_offset = 0x%8.8x\n", header.code_offset); 1056f754f88fSGreg Clayton s->Printf(" data_offset = 0x%8.8x\n", header.data_offset); 1057b9c1b51eSKate Stone s->Printf(" image_base = 0x%16.16" PRIx64 "\n", 1058b9c1b51eSKate Stone header.image_base); 1059f754f88fSGreg Clayton s->Printf(" sect_alignment = 0x%8.8x\n", header.sect_alignment); 1060f754f88fSGreg Clayton s->Printf(" file_alignment = 0x%8.8x\n", header.file_alignment); 1061b9c1b51eSKate Stone s->Printf(" major_os_system_version = 0x%4.4x\n", 1062b9c1b51eSKate Stone header.major_os_system_version); 1063b9c1b51eSKate Stone s->Printf(" minor_os_system_version = 0x%4.4x\n", 1064b9c1b51eSKate Stone header.minor_os_system_version); 1065b9c1b51eSKate Stone s->Printf(" major_image_version = 0x%4.4x\n", 1066b9c1b51eSKate Stone header.major_image_version); 1067b9c1b51eSKate Stone s->Printf(" minor_image_version = 0x%4.4x\n", 1068b9c1b51eSKate Stone header.minor_image_version); 1069b9c1b51eSKate Stone s->Printf(" major_subsystem_version = 0x%4.4x\n", 1070b9c1b51eSKate Stone header.major_subsystem_version); 1071b9c1b51eSKate Stone s->Printf(" minor_subsystem_version = 0x%4.4x\n", 1072b9c1b51eSKate Stone header.minor_subsystem_version); 1073f754f88fSGreg Clayton s->Printf(" reserved1 = 0x%8.8x\n", header.reserved1); 1074f754f88fSGreg Clayton s->Printf(" image_size = 0x%8.8x\n", header.image_size); 1075f754f88fSGreg Clayton s->Printf(" header_size = 0x%8.8x\n", header.header_size); 107628469ca3SGreg Clayton s->Printf(" checksum = 0x%8.8x\n", header.checksum); 1077f754f88fSGreg Clayton s->Printf(" subsystem = 0x%4.4x\n", header.subsystem); 1078f754f88fSGreg Clayton s->Printf(" dll_flags = 0x%4.4x\n", header.dll_flags); 1079b9c1b51eSKate Stone s->Printf(" stack_reserve_size = 0x%16.16" PRIx64 "\n", 1080b9c1b51eSKate Stone header.stack_reserve_size); 1081b9c1b51eSKate Stone s->Printf(" stack_commit_size = 0x%16.16" PRIx64 "\n", 1082b9c1b51eSKate Stone header.stack_commit_size); 1083b9c1b51eSKate Stone s->Printf(" heap_reserve_size = 0x%16.16" PRIx64 "\n", 1084b9c1b51eSKate Stone header.heap_reserve_size); 1085b9c1b51eSKate Stone s->Printf(" heap_commit_size = 0x%16.16" PRIx64 "\n", 1086b9c1b51eSKate Stone header.heap_commit_size); 1087f754f88fSGreg Clayton s->Printf(" loader_flags = 0x%8.8x\n", header.loader_flags); 1088b9c1b51eSKate Stone s->Printf(" num_data_dir_entries = 0x%8.8x\n", 1089b9c1b51eSKate Stone (uint32_t)header.data_dirs.size()); 1090f754f88fSGreg Clayton uint32_t i; 1091b9c1b51eSKate Stone for (i = 0; i < header.data_dirs.size(); i++) { 1092b9c1b51eSKate Stone s->Printf(" data_dirs[%2u] vmaddr = 0x%8.8x, vmsize = 0x%8.8x\n", i, 1093b9c1b51eSKate Stone header.data_dirs[i].vmaddr, header.data_dirs[i].vmsize); 1094f754f88fSGreg Clayton } 1095f754f88fSGreg Clayton } 1096f754f88fSGreg Clayton // DumpSectionHeader 1097f754f88fSGreg Clayton // 1098f754f88fSGreg Clayton // Dump a single ELF section header to the specified output stream 1099b9c1b51eSKate Stone void ObjectFilePECOFF::DumpSectionHeader(Stream *s, 1100b9c1b51eSKate Stone const section_header_t &sh) { 1101adcd0268SBenjamin Kramer std::string name = std::string(GetSectionName(sh)); 1102b9c1b51eSKate 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 " 1103b9c1b51eSKate Stone "0x%4.4x 0x%8.8x\n", 1104b9c1b51eSKate Stone name.c_str(), sh.vmaddr, sh.vmsize, sh.offset, sh.size, sh.reloff, 1105b9c1b51eSKate Stone sh.lineoff, sh.nreloc, sh.nline, sh.flags); 1106f754f88fSGreg Clayton } 1107f754f88fSGreg Clayton 1108f754f88fSGreg Clayton // DumpSectionHeaders 1109f754f88fSGreg Clayton // 1110f754f88fSGreg Clayton // Dump all of the ELF section header to the specified output stream 1111b9c1b51eSKate Stone void ObjectFilePECOFF::DumpSectionHeaders(Stream *s) { 1112f754f88fSGreg Clayton 1113f754f88fSGreg Clayton s->PutCString("Section Headers\n"); 1114b9c1b51eSKate Stone s->PutCString("IDX name vm addr vm size file off file " 1115b9c1b51eSKate Stone "size reloc off line off nreloc nline flags\n"); 1116b9c1b51eSKate Stone s->PutCString("==== ---------------- ---------- ---------- ---------- " 1117b9c1b51eSKate Stone "---------- ---------- ---------- ------ ------ ----------\n"); 1118f754f88fSGreg Clayton 1119f754f88fSGreg Clayton uint32_t idx = 0; 1120f754f88fSGreg Clayton SectionHeaderCollIter pos, end = m_sect_headers.end(); 1121f754f88fSGreg Clayton 1122b9c1b51eSKate Stone for (pos = m_sect_headers.begin(); pos != end; ++pos, ++idx) { 1123f754f88fSGreg Clayton s->Printf("[%2u] ", idx); 1124f754f88fSGreg Clayton ObjectFilePECOFF::DumpSectionHeader(s, *pos); 1125f754f88fSGreg Clayton } 1126f754f88fSGreg Clayton } 1127f754f88fSGreg Clayton 1128037ed1beSAaron Smith // DumpDependentModules 1129037ed1beSAaron Smith // 1130037ed1beSAaron Smith // Dump all of the dependent modules to the specified output stream 1131037ed1beSAaron Smith void ObjectFilePECOFF::DumpDependentModules(lldb_private::Stream *s) { 1132037ed1beSAaron Smith auto num_modules = ParseDependentModules(); 1133037ed1beSAaron Smith if (num_modules > 0) { 1134037ed1beSAaron Smith s->PutCString("Dependent Modules\n"); 1135037ed1beSAaron Smith for (unsigned i = 0; i < num_modules; ++i) { 1136037ed1beSAaron Smith auto spec = m_deps_filespec->GetFileSpecAtIndex(i); 1137037ed1beSAaron Smith s->Printf(" %s\n", spec.GetFilename().GetCString()); 1138037ed1beSAaron Smith } 1139037ed1beSAaron Smith } 1140037ed1beSAaron Smith } 1141037ed1beSAaron Smith 1142fb3b3bd1SZachary Turner bool ObjectFilePECOFF::IsWindowsSubsystem() { 1143fb3b3bd1SZachary Turner switch (m_coff_header_opt.subsystem) { 1144fb3b3bd1SZachary Turner case llvm::COFF::IMAGE_SUBSYSTEM_NATIVE: 1145fb3b3bd1SZachary Turner case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_GUI: 1146fb3b3bd1SZachary Turner case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_CUI: 1147fb3b3bd1SZachary Turner case llvm::COFF::IMAGE_SUBSYSTEM_NATIVE_WINDOWS: 1148fb3b3bd1SZachary Turner case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_CE_GUI: 1149fb3b3bd1SZachary Turner case llvm::COFF::IMAGE_SUBSYSTEM_XBOX: 1150fb3b3bd1SZachary Turner case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_BOOT_APPLICATION: 1151fb3b3bd1SZachary Turner return true; 1152fb3b3bd1SZachary Turner default: 1153fb3b3bd1SZachary Turner return false; 1154fb3b3bd1SZachary Turner } 1155fb3b3bd1SZachary Turner } 1156fb3b3bd1SZachary Turner 1157f760f5aeSPavel Labath ArchSpec ObjectFilePECOFF::GetArchitecture() { 1158237ad974SCharles Davis uint16_t machine = m_coff_header.machine; 1159b9c1b51eSKate Stone switch (machine) { 1160f760f5aeSPavel Labath default: 1161f760f5aeSPavel Labath break; 1162237ad974SCharles Davis case llvm::COFF::IMAGE_FILE_MACHINE_AMD64: 1163237ad974SCharles Davis case llvm::COFF::IMAGE_FILE_MACHINE_I386: 1164237ad974SCharles Davis case llvm::COFF::IMAGE_FILE_MACHINE_POWERPC: 1165237ad974SCharles Davis case llvm::COFF::IMAGE_FILE_MACHINE_POWERPCFP: 1166237ad974SCharles Davis case llvm::COFF::IMAGE_FILE_MACHINE_ARM: 11671108cb36SSaleem Abdulrasool case llvm::COFF::IMAGE_FILE_MACHINE_ARMNT: 1168237ad974SCharles Davis case llvm::COFF::IMAGE_FILE_MACHINE_THUMB: 1169638f072fSMartin Storsjo case llvm::COFF::IMAGE_FILE_MACHINE_ARM64: 1170f760f5aeSPavel Labath ArchSpec arch; 1171fb3b3bd1SZachary Turner arch.SetArchitecture(eArchTypeCOFF, machine, LLDB_INVALID_CPUTYPE, 1172fb3b3bd1SZachary Turner IsWindowsSubsystem() ? llvm::Triple::Win32 1173fb3b3bd1SZachary Turner : llvm::Triple::UnknownOS); 1174f760f5aeSPavel Labath return arch; 1175237ad974SCharles Davis } 1176f760f5aeSPavel Labath return ArchSpec(); 1177f754f88fSGreg Clayton } 1178f754f88fSGreg Clayton 1179b9c1b51eSKate Stone ObjectFile::Type ObjectFilePECOFF::CalculateType() { 1180b9c1b51eSKate Stone if (m_coff_header.machine != 0) { 1181237ad974SCharles Davis if ((m_coff_header.flags & llvm::COFF::IMAGE_FILE_DLL) == 0) 1182f754f88fSGreg Clayton return eTypeExecutable; 1183f754f88fSGreg Clayton else 1184f754f88fSGreg Clayton return eTypeSharedLibrary; 1185f754f88fSGreg Clayton } 1186f754f88fSGreg Clayton return eTypeExecutable; 1187f754f88fSGreg Clayton } 1188f754f88fSGreg Clayton 1189b9c1b51eSKate Stone ObjectFile::Strata ObjectFilePECOFF::CalculateStrata() { return eStrataUser; } 1190