1 //===-- ObjectFilePECOFF.h --------------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef LLDB_SOURCE_PLUGINS_OBJECTFILE_PECOFF_OBJECTFILEPECOFF_H 10 #define LLDB_SOURCE_PLUGINS_OBJECTFILE_PECOFF_OBJECTFILEPECOFF_H 11 12 #include <optional> 13 #include <vector> 14 15 #include "lldb/Symbol/ObjectFile.h" 16 #include "llvm/Object/COFF.h" 17 18 class ObjectFilePECOFF : public lldb_private::ObjectFile { 19 public: 20 enum MachineType { 21 MachineUnknown = 0x0, 22 MachineAm33 = 0x1d3, 23 MachineAmd64 = 0x8664, 24 MachineArm = 0x1c0, 25 MachineArmNt = 0x1c4, 26 MachineArm64 = 0xaa64, 27 MachineEbc = 0xebc, 28 MachineX86 = 0x14c, 29 MachineIA64 = 0x200, 30 MachineM32R = 0x9041, 31 MachineMips16 = 0x266, 32 MachineMipsFpu = 0x366, 33 MachineMipsFpu16 = 0x466, 34 MachinePowerPc = 0x1f0, 35 MachinePowerPcfp = 0x1f1, 36 MachineR4000 = 0x166, 37 MachineSh3 = 0x1a2, 38 MachineSh3dsp = 0x1a3, 39 MachineSh4 = 0x1a6, 40 MachineSh5 = 0x1a8, 41 MachineThumb = 0x1c2, 42 MachineWcemIpsv2 = 0x169 43 }; 44 45 ObjectFilePECOFF(const lldb::ModuleSP &module_sp, lldb::DataBufferSP data_sp, 46 lldb::offset_t data_offset, 47 const lldb_private::FileSpec *file, 48 lldb::offset_t file_offset, lldb::offset_t length); 49 50 ObjectFilePECOFF(const lldb::ModuleSP &module_sp, 51 lldb::WritableDataBufferSP header_data_sp, 52 const lldb::ProcessSP &process_sp, lldb::addr_t header_addr); 53 54 ~ObjectFilePECOFF() override; 55 56 // Static Functions 57 static void Initialize(); 58 59 static void DebuggerInitialize(lldb_private::Debugger &debugger); 60 61 static void Terminate(); 62 GetPluginNameStatic()63 static llvm::StringRef GetPluginNameStatic() { return "pe-coff"; } 64 65 static llvm::StringRef GetPluginDescriptionStatic(); 66 67 static ObjectFile * 68 CreateInstance(const lldb::ModuleSP &module_sp, lldb::DataBufferSP data_sp, 69 lldb::offset_t data_offset, const lldb_private::FileSpec *file, 70 lldb::offset_t offset, lldb::offset_t length); 71 72 static lldb_private::ObjectFile *CreateMemoryInstance( 73 const lldb::ModuleSP &module_sp, lldb::WritableDataBufferSP data_sp, 74 const lldb::ProcessSP &process_sp, lldb::addr_t header_addr); 75 76 static size_t GetModuleSpecifications(const lldb_private::FileSpec &file, 77 lldb::DataBufferSP &data_sp, 78 lldb::offset_t data_offset, 79 lldb::offset_t file_offset, 80 lldb::offset_t length, 81 lldb_private::ModuleSpecList &specs); 82 83 static bool SaveCore(const lldb::ProcessSP &process_sp, 84 const lldb_private::FileSpec &outfile, 85 lldb::SaveCoreStyle &core_style, 86 lldb_private::Status &error); 87 88 static bool MagicBytesMatch(lldb::DataBufferSP data_sp); 89 90 static lldb::SymbolType MapSymbolType(uint16_t coff_symbol_type); 91 92 // LLVM RTTI support 93 static char ID; isA(const void * ClassID)94 bool isA(const void *ClassID) const override { 95 return ClassID == &ID || ObjectFile::isA(ClassID); 96 } classof(const ObjectFile * obj)97 static bool classof(const ObjectFile *obj) { return obj->isA(&ID); } 98 99 bool ParseHeader() override; 100 101 bool SetLoadAddress(lldb_private::Target &target, lldb::addr_t value, 102 bool value_is_offset) override; 103 104 lldb::ByteOrder GetByteOrder() const override; 105 106 bool IsExecutable() const override; 107 108 uint32_t GetAddressByteSize() const override; 109 110 // virtual lldb_private::AddressClass 111 // GetAddressClass (lldb::addr_t file_addr); 112 113 void ParseSymtab(lldb_private::Symtab &symtab) override; 114 115 bool IsStripped() override; 116 117 void CreateSections(lldb_private::SectionList &unified_section_list) override; 118 119 void Dump(lldb_private::Stream *s) override; 120 121 lldb_private::ArchSpec GetArchitecture() override; 122 123 lldb_private::UUID GetUUID() override; 124 125 /// Return the contents of the .gnu_debuglink section, if the object file 126 /// contains it. 127 std::optional<lldb_private::FileSpec> GetDebugLink(); 128 129 uint32_t GetDependentModules(lldb_private::FileSpecList &files) override; 130 131 lldb_private::Address GetEntryPointAddress() override; 132 133 lldb_private::Address GetBaseAddress() override; 134 135 ObjectFile::Type CalculateType() override; 136 137 ObjectFile::Strata CalculateStrata() override; 138 139 // PluginInterface protocol GetPluginName()140 llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); } 141 142 bool IsWindowsSubsystem(); 143 144 uint32_t GetRVA(const lldb_private::Address &addr) const; 145 lldb_private::Address GetAddress(uint32_t rva); 146 lldb::addr_t GetFileAddress(uint32_t rva) const; 147 148 lldb_private::DataExtractor ReadImageData(uint32_t offset, size_t size); 149 lldb_private::DataExtractor ReadImageDataByRVA(uint32_t rva, size_t size); 150 151 std::unique_ptr<lldb_private::CallFrameInfo> CreateCallFrameInfo() override; 152 153 protected: 154 bool NeedsEndianSwap() const; 155 156 typedef struct dos_header { // DOS .EXE header 157 uint16_t e_magic = 0; // Magic number 158 uint16_t e_cblp = 0; // Bytes on last page of file 159 uint16_t e_cp = 0; // Pages in file 160 uint16_t e_crlc = 0; // Relocations 161 uint16_t e_cparhdr = 0; // Size of header in paragraphs 162 uint16_t e_minalloc = 0; // Minimum extra paragraphs needed 163 uint16_t e_maxalloc = 0; // Maximum extra paragraphs needed 164 uint16_t e_ss = 0; // Initial (relative) SS value 165 uint16_t e_sp = 0; // Initial SP value 166 uint16_t e_csum = 0; // Checksum 167 uint16_t e_ip = 0; // Initial IP value 168 uint16_t e_cs = 0; // Initial (relative) CS value 169 uint16_t e_lfarlc = 0; // File address of relocation table 170 uint16_t e_ovno = 0; // Overlay number 171 uint16_t e_res[4]; // Reserved words 172 uint16_t e_oemid = 0; // OEM identifier (for e_oeminfo) 173 uint16_t e_oeminfo = 0; // OEM information; e_oemid specific 174 uint16_t e_res2[10] = {}; // Reserved words 175 uint32_t e_lfanew = 0; // File address of new exe header 176 } dos_header_t; 177 178 typedef struct coff_header { 179 uint16_t machine = 0; 180 uint16_t nsects = 0; 181 uint32_t modtime = 0; 182 uint32_t symoff = 0; 183 uint32_t nsyms = 0; 184 uint16_t hdrsize = 0; 185 uint16_t flags = 0; 186 } coff_header_t; 187 188 typedef struct data_directory { 189 uint32_t vmaddr = 0; 190 uint32_t vmsize = 0; 191 } data_directory_t; 192 193 typedef struct coff_opt_header { 194 uint16_t magic = 0; 195 uint8_t major_linker_version = 0; 196 uint8_t minor_linker_version = 0; 197 uint32_t code_size = 0; 198 uint32_t data_size = 0; 199 uint32_t bss_size = 0; 200 uint32_t entry = 0; 201 uint32_t code_offset = 0; 202 uint32_t data_offset = 0; 203 204 uint64_t image_base = 0; 205 uint32_t sect_alignment = 0; 206 uint32_t file_alignment = 0; 207 uint16_t major_os_system_version = 0; 208 uint16_t minor_os_system_version = 0; 209 uint16_t major_image_version = 0; 210 uint16_t minor_image_version = 0; 211 uint16_t major_subsystem_version = 0; 212 uint16_t minor_subsystem_version = 0; 213 uint32_t reserved1 = 0; 214 uint32_t image_size = 0; 215 uint32_t header_size = 0; 216 uint32_t checksum = 0; 217 uint16_t subsystem = 0; 218 uint16_t dll_flags = 0; 219 uint64_t stack_reserve_size = 0; 220 uint64_t stack_commit_size = 0; 221 uint64_t heap_reserve_size = 0; 222 uint64_t heap_commit_size = 0; 223 uint32_t loader_flags = 0; 224 // uint32_t num_data_dir_entries; 225 std::vector<data_directory> 226 data_dirs; // will contain num_data_dir_entries entries 227 } coff_opt_header_t; 228 229 typedef struct section_header { 230 char name[8] = {}; 231 uint32_t vmsize = 0; // Virtual Size 232 uint32_t vmaddr = 0; // Virtual Addr 233 uint32_t size = 0; // File size 234 uint32_t offset = 0; // File offset 235 uint32_t reloff = 0; // Offset to relocations 236 uint32_t lineoff = 0; // Offset to line table entries 237 uint16_t nreloc = 0; // Number of relocation entries 238 uint16_t nline = 0; // Number of line table entries 239 uint32_t flags = 0; 240 } section_header_t; 241 242 static bool ParseDOSHeader(lldb_private::DataExtractor &data, 243 dos_header_t &dos_header); 244 static bool ParseCOFFHeader(lldb_private::DataExtractor &data, 245 lldb::offset_t *offset_ptr, 246 coff_header_t &coff_header); 247 bool ParseCOFFOptionalHeader(lldb::offset_t *offset_ptr); 248 bool ParseSectionHeaders(uint32_t offset); 249 250 uint32_t ParseDependentModules(); 251 252 static void DumpDOSHeader(lldb_private::Stream *s, 253 const dos_header_t &header); 254 static void DumpCOFFHeader(lldb_private::Stream *s, 255 const coff_header_t &header); 256 static void DumpOptCOFFHeader(lldb_private::Stream *s, 257 const coff_opt_header_t &header); 258 void DumpSectionHeaders(lldb_private::Stream *s); 259 void DumpSectionHeader(lldb_private::Stream *s, const section_header_t &sh); 260 void DumpDependentModules(lldb_private::Stream *s); 261 262 llvm::StringRef GetSectionName(const section_header_t §); 263 static lldb::SectionType GetSectionType(llvm::StringRef sect_name, 264 const section_header_t §); 265 266 typedef std::vector<section_header_t> SectionHeaderColl; 267 typedef SectionHeaderColl::iterator SectionHeaderCollIter; 268 typedef SectionHeaderColl::const_iterator SectionHeaderCollConstIter; 269 270 private: 271 bool CreateBinary(); 272 typedef std::vector<std::pair<uint32_t, uint32_t>> rva_symbol_list_t; 273 void AppendFromCOFFSymbolTable(lldb_private::SectionList *sect_list, 274 lldb_private::Symtab &symtab, 275 const rva_symbol_list_t &sorted_exports); 276 rva_symbol_list_t AppendFromExportTable(lldb_private::SectionList *sect_list, 277 lldb_private::Symtab &symtab); 278 279 dos_header_t m_dos_header; 280 coff_header_t m_coff_header; 281 coff_opt_header_t m_coff_header_opt; 282 SectionHeaderColl m_sect_headers; 283 lldb::addr_t m_image_base; 284 lldb_private::Address m_entry_point_address; 285 std::optional<lldb_private::FileSpecList> m_deps_filespec; 286 std::unique_ptr<llvm::object::COFFObjectFile> m_binary; 287 lldb_private::UUID m_uuid; 288 }; 289 290 #endif // LLDB_SOURCE_PLUGINS_OBJECTFILE_PECOFF_OBJECTFILEPECOFF_H 291