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