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