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 liblldb_ObjectFilePECOFF_h_ 10 #define liblldb_ObjectFilePECOFF_h_ 11 12 #include <vector> 13 14 #include "lldb/Symbol/ObjectFile.h" 15 #include "llvm/Object/Binary.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::DataBufferSP &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 lldb_private::ConstString GetPluginNameStatic(); 61 62 static const char *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::DataBufferSP &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_private::Status &error); 83 84 static bool MagicBytesMatch(lldb::DataBufferSP &data_sp); 85 86 static lldb::SymbolType MapSymbolType(uint16_t coff_symbol_type); 87 88 // LLVM RTTI support 89 static char ID; 90 bool isA(const void *ClassID) const override { 91 return ClassID == &ID || ObjectFile::isA(ClassID); 92 } 93 static bool classof(const ObjectFile *obj) { return obj->isA(&ID); } 94 95 bool ParseHeader() override; 96 97 bool SetLoadAddress(lldb_private::Target &target, lldb::addr_t value, 98 bool value_is_offset) override; 99 100 lldb::ByteOrder GetByteOrder() const override; 101 102 bool IsExecutable() const override; 103 104 uint32_t GetAddressByteSize() const override; 105 106 // virtual lldb_private::AddressClass 107 // GetAddressClass (lldb::addr_t file_addr); 108 109 lldb_private::Symtab *GetSymtab() override; 110 111 bool IsStripped() override; 112 113 void CreateSections(lldb_private::SectionList &unified_section_list) override; 114 115 void Dump(lldb_private::Stream *s) override; 116 117 lldb_private::ArchSpec GetArchitecture() override; 118 119 lldb_private::UUID GetUUID() override; 120 121 uint32_t GetDependentModules(lldb_private::FileSpecList &files) override; 122 123 lldb_private::Address GetEntryPointAddress() override; 124 125 lldb_private::Address GetBaseAddress() override; 126 127 ObjectFile::Type CalculateType() override; 128 129 ObjectFile::Strata CalculateStrata() override; 130 131 // PluginInterface protocol 132 lldb_private::ConstString GetPluginName() override; 133 134 uint32_t GetPluginVersion() override; 135 136 bool IsWindowsSubsystem(); 137 138 lldb_private::DataExtractor ReadImageData(uint32_t offset, size_t size); 139 140 protected: 141 bool NeedsEndianSwap() const; 142 143 typedef struct dos_header { // DOS .EXE header 144 uint16_t e_magic; // Magic number 145 uint16_t e_cblp; // Bytes on last page of file 146 uint16_t e_cp; // Pages in file 147 uint16_t e_crlc; // Relocations 148 uint16_t e_cparhdr; // Size of header in paragraphs 149 uint16_t e_minalloc; // Minimum extra paragraphs needed 150 uint16_t e_maxalloc; // Maximum extra paragraphs needed 151 uint16_t e_ss; // Initial (relative) SS value 152 uint16_t e_sp; // Initial SP value 153 uint16_t e_csum; // Checksum 154 uint16_t e_ip; // Initial IP value 155 uint16_t e_cs; // Initial (relative) CS value 156 uint16_t e_lfarlc; // File address of relocation table 157 uint16_t e_ovno; // Overlay number 158 uint16_t e_res[4]; // Reserved words 159 uint16_t e_oemid; // OEM identifier (for e_oeminfo) 160 uint16_t e_oeminfo; // OEM information; e_oemid specific 161 uint16_t e_res2[10]; // Reserved words 162 uint32_t e_lfanew; // File address of new exe header 163 } dos_header_t; 164 165 typedef struct coff_header { 166 uint16_t machine; 167 uint16_t nsects; 168 uint32_t modtime; 169 uint32_t symoff; 170 uint32_t nsyms; 171 uint16_t hdrsize; 172 uint16_t flags; 173 } coff_header_t; 174 175 typedef struct data_directory { 176 uint32_t vmaddr; 177 uint32_t vmsize; 178 } data_directory_t; 179 180 typedef struct coff_opt_header { 181 uint16_t magic = 0; 182 uint8_t major_linker_version = 0; 183 uint8_t minor_linker_version = 0; 184 uint32_t code_size = 0; 185 uint32_t data_size = 0; 186 uint32_t bss_size = 0; 187 uint32_t entry = 0; 188 uint32_t code_offset = 0; 189 uint32_t data_offset = 0; 190 191 uint64_t image_base = 0; 192 uint32_t sect_alignment = 0; 193 uint32_t file_alignment = 0; 194 uint16_t major_os_system_version = 0; 195 uint16_t minor_os_system_version = 0; 196 uint16_t major_image_version = 0; 197 uint16_t minor_image_version = 0; 198 uint16_t major_subsystem_version = 0; 199 uint16_t minor_subsystem_version = 0; 200 uint32_t reserved1 = 0; 201 uint32_t image_size = 0; 202 uint32_t header_size = 0; 203 uint32_t checksum = 0; 204 uint16_t subsystem = 0; 205 uint16_t dll_flags = 0; 206 uint64_t stack_reserve_size = 0; 207 uint64_t stack_commit_size = 0; 208 uint64_t heap_reserve_size = 0; 209 uint64_t heap_commit_size = 0; 210 uint32_t loader_flags = 0; 211 // uint32_t num_data_dir_entries; 212 std::vector<data_directory> 213 data_dirs; // will contain num_data_dir_entries entries 214 } coff_opt_header_t; 215 216 enum coff_data_dir_type { 217 coff_data_dir_export_table = 0, 218 coff_data_dir_import_table = 1, 219 }; 220 221 typedef struct section_header { 222 char name[8]; 223 uint32_t vmsize; // Virtual Size 224 uint32_t vmaddr; // Virtual Addr 225 uint32_t size; // File size 226 uint32_t offset; // File offset 227 uint32_t reloff; // Offset to relocations 228 uint32_t lineoff; // Offset to line table entries 229 uint16_t nreloc; // Number of relocation entries 230 uint16_t nline; // Number of line table entries 231 uint32_t flags; 232 } section_header_t; 233 234 typedef struct coff_symbol { 235 char name[8]; 236 uint32_t value; 237 uint16_t sect; 238 uint16_t type; 239 uint8_t storage; 240 uint8_t naux; 241 } coff_symbol_t; 242 243 typedef struct export_directory_entry { 244 uint32_t characteristics; 245 uint32_t time_date_stamp; 246 uint16_t major_version; 247 uint16_t minor_version; 248 uint32_t name; 249 uint32_t base; 250 uint32_t number_of_functions; 251 uint32_t number_of_names; 252 uint32_t address_of_functions; 253 uint32_t address_of_names; 254 uint32_t address_of_name_ordinals; 255 } export_directory_entry; 256 257 static bool ParseDOSHeader(lldb_private::DataExtractor &data, 258 dos_header_t &dos_header); 259 static bool ParseCOFFHeader(lldb_private::DataExtractor &data, 260 lldb::offset_t *offset_ptr, 261 coff_header_t &coff_header); 262 bool ParseCOFFOptionalHeader(lldb::offset_t *offset_ptr); 263 bool ParseSectionHeaders(uint32_t offset); 264 265 uint32_t ParseDependentModules(); 266 267 static void DumpDOSHeader(lldb_private::Stream *s, 268 const dos_header_t &header); 269 static void DumpCOFFHeader(lldb_private::Stream *s, 270 const coff_header_t &header); 271 static void DumpOptCOFFHeader(lldb_private::Stream *s, 272 const coff_opt_header_t &header); 273 void DumpSectionHeaders(lldb_private::Stream *s); 274 void DumpSectionHeader(lldb_private::Stream *s, const section_header_t &sh); 275 void DumpDependentModules(lldb_private::Stream *s); 276 277 llvm::StringRef GetSectionName(const section_header_t §); 278 279 typedef std::vector<section_header_t> SectionHeaderColl; 280 typedef SectionHeaderColl::iterator SectionHeaderCollIter; 281 typedef SectionHeaderColl::const_iterator SectionHeaderCollConstIter; 282 283 private: 284 bool CreateBinary(); 285 286 private: 287 dos_header_t m_dos_header; 288 coff_header_t m_coff_header; 289 coff_opt_header_t m_coff_header_opt; 290 SectionHeaderColl m_sect_headers; 291 lldb::addr_t m_image_base; 292 lldb_private::Address m_entry_point_address; 293 llvm::Optional<lldb_private::FileSpecList> m_deps_filespec; 294 typedef llvm::object::OwningBinary<llvm::object::Binary> OWNBINType; 295 llvm::Optional<OWNBINType> m_owningbin; 296 lldb_private::UUID m_uuid; 297 }; 298 299 #endif // liblldb_ObjectFilePECOFF_h_ 300