Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 1 | //===-- ObjectFilePECOFF.h --------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef liblldb_ObjectFilePECOFF_h_ |
| 11 | #define liblldb_ObjectFilePECOFF_h_ |
| 12 | |
Eugene Zelenko | 8157a88 | 2015-10-23 16:56:07 +0000 | [diff] [blame^] | 13 | // C Includes |
| 14 | // C++ Includes |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 15 | #include <vector> |
| 16 | |
Eugene Zelenko | 8157a88 | 2015-10-23 16:56:07 +0000 | [diff] [blame^] | 17 | // Other libraries and framework includes |
| 18 | // Project includes |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 19 | #include "lldb/Symbol/ObjectFile.h" |
| 20 | |
| 21 | class ObjectFilePECOFF : |
| 22 | public lldb_private::ObjectFile |
| 23 | { |
| 24 | public: |
Zachary Turner | ad587ae4 | 2014-07-28 16:44:49 +0000 | [diff] [blame] | 25 | typedef enum MachineType |
| 26 | { |
| 27 | MachineUnknown = 0x0, |
| 28 | MachineAm33 = 0x1d3, |
| 29 | MachineAmd64 = 0x8664, |
| 30 | MachineArm = 0x1c0, |
| 31 | MachineArmNt = 0x1c4, |
| 32 | MachineArm64 = 0xaa64, |
| 33 | MachineEbc = 0xebc, |
| 34 | MachineX86 = 0x14c, |
| 35 | MachineIA64 = 0x200, |
| 36 | MachineM32R = 0x9041, |
| 37 | MachineMips16 = 0x266, |
| 38 | MachineMipsFpu = 0x366, |
| 39 | MachineMipsFpu16 = 0x466, |
| 40 | MachinePowerPc = 0x1f0, |
| 41 | MachinePowerPcfp = 0x1f1, |
| 42 | MachineR4000 = 0x166, |
| 43 | MachineSh3 = 0x1a2, |
| 44 | MachineSh3dsp = 0x1a3, |
| 45 | MachineSh4 = 0x1a6, |
| 46 | MachineSh5 = 0x1a8, |
| 47 | MachineThumb = 0x1c2, |
| 48 | MachineWcemIpsv2 = 0x169 |
Greg Clayton | 49a95e1 | 2014-07-29 23:23:58 +0000 | [diff] [blame] | 49 | } MachineType; |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 50 | |
Eugene Zelenko | 8157a88 | 2015-10-23 16:56:07 +0000 | [diff] [blame^] | 51 | ObjectFilePECOFF(const lldb::ModuleSP &module_sp, |
| 52 | lldb::DataBufferSP& data_sp, |
| 53 | lldb::offset_t data_offset, |
| 54 | const lldb_private::FileSpec* file, |
| 55 | lldb::offset_t file_offset, |
| 56 | lldb::offset_t length); |
| 57 | |
| 58 | ~ObjectFilePECOFF() override; |
| 59 | |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 60 | //------------------------------------------------------------------ |
| 61 | // Static Functions |
| 62 | //------------------------------------------------------------------ |
| 63 | static void |
| 64 | Initialize(); |
| 65 | |
| 66 | static void |
| 67 | Terminate(); |
| 68 | |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 69 | static lldb_private::ConstString |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 70 | GetPluginNameStatic(); |
| 71 | |
| 72 | static const char * |
| 73 | GetPluginDescriptionStatic(); |
| 74 | |
| 75 | static ObjectFile * |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 76 | CreateInstance (const lldb::ModuleSP &module_sp, |
Greg Clayton | 5ce9c56 | 2013-02-06 17:22:03 +0000 | [diff] [blame] | 77 | lldb::DataBufferSP& data_sp, |
| 78 | lldb::offset_t data_offset, |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 79 | const lldb_private::FileSpec* file, |
Greg Clayton | 5ce9c56 | 2013-02-06 17:22:03 +0000 | [diff] [blame] | 80 | lldb::offset_t offset, |
| 81 | lldb::offset_t length); |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 82 | |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 83 | static lldb_private::ObjectFile * |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 84 | CreateMemoryInstance (const lldb::ModuleSP &module_sp, |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 85 | lldb::DataBufferSP& data_sp, |
| 86 | const lldb::ProcessSP &process_sp, |
| 87 | lldb::addr_t header_addr); |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 88 | |
| 89 | static size_t |
| 90 | GetModuleSpecifications (const lldb_private::FileSpec& file, |
| 91 | lldb::DataBufferSP& data_sp, |
| 92 | lldb::offset_t data_offset, |
| 93 | lldb::offset_t file_offset, |
| 94 | lldb::offset_t length, |
| 95 | lldb_private::ModuleSpecList &specs); |
| 96 | |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 97 | static bool |
Greg Clayton | 5ce9c56 | 2013-02-06 17:22:03 +0000 | [diff] [blame] | 98 | MagicBytesMatch (lldb::DataBufferSP& data_sp); |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 99 | |
Eugene Zelenko | 8157a88 | 2015-10-23 16:56:07 +0000 | [diff] [blame^] | 100 | bool |
| 101 | ParseHeader() override; |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 102 | |
Eugene Zelenko | 8157a88 | 2015-10-23 16:56:07 +0000 | [diff] [blame^] | 103 | bool |
| 104 | SetLoadAddress(lldb_private::Target &target, lldb::addr_t value, bool value_is_offset) override; |
Virgile Bello | 2756adf | 2014-03-08 17:17:20 +0000 | [diff] [blame] | 105 | |
Eugene Zelenko | 8157a88 | 2015-10-23 16:56:07 +0000 | [diff] [blame^] | 106 | lldb::ByteOrder |
| 107 | GetByteOrder() const override; |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 108 | |
Eugene Zelenko | 8157a88 | 2015-10-23 16:56:07 +0000 | [diff] [blame^] | 109 | bool |
| 110 | IsExecutable() const override; |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 111 | |
Eugene Zelenko | 8157a88 | 2015-10-23 16:56:07 +0000 | [diff] [blame^] | 112 | uint32_t |
| 113 | GetAddressByteSize() const override; |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 114 | |
| 115 | // virtual lldb_private::AddressClass |
| 116 | // GetAddressClass (lldb::addr_t file_addr); |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 117 | |
Eugene Zelenko | 8157a88 | 2015-10-23 16:56:07 +0000 | [diff] [blame^] | 118 | lldb_private::Symtab * |
| 119 | GetSymtab() override; |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 120 | |
Eugene Zelenko | 8157a88 | 2015-10-23 16:56:07 +0000 | [diff] [blame^] | 121 | bool |
| 122 | IsStripped() override; |
| 123 | |
| 124 | void |
| 125 | CreateSections(lldb_private::SectionList &unified_section_list) override; |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 126 | |
Eugene Zelenko | 8157a88 | 2015-10-23 16:56:07 +0000 | [diff] [blame^] | 127 | void |
| 128 | Dump(lldb_private::Stream *s) override; |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 129 | |
Eugene Zelenko | 8157a88 | 2015-10-23 16:56:07 +0000 | [diff] [blame^] | 130 | bool |
| 131 | GetArchitecture(lldb_private::ArchSpec &arch) override; |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 132 | |
Eugene Zelenko | 8157a88 | 2015-10-23 16:56:07 +0000 | [diff] [blame^] | 133 | bool |
| 134 | GetUUID(lldb_private::UUID* uuid) override; |
| 135 | |
| 136 | uint32_t |
| 137 | GetDependentModules(lldb_private::FileSpecList& files) override; |
| 138 | |
| 139 | // virtual lldb_private::Address |
| 140 | // GetEntryPointAddress (); |
| 141 | |
| 142 | ObjectFile::Type |
| 143 | CalculateType() override; |
| 144 | |
| 145 | ObjectFile::Strata |
| 146 | CalculateStrata() override; |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 147 | |
| 148 | //------------------------------------------------------------------ |
| 149 | // PluginInterface protocol |
| 150 | //------------------------------------------------------------------ |
Eugene Zelenko | 8157a88 | 2015-10-23 16:56:07 +0000 | [diff] [blame^] | 151 | lldb_private::ConstString |
| 152 | GetPluginName() override; |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 153 | |
Eugene Zelenko | 8157a88 | 2015-10-23 16:56:07 +0000 | [diff] [blame^] | 154 | uint32_t |
| 155 | GetPluginVersion() override; |
| 156 | |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 157 | protected: |
| 158 | bool NeedsEndianSwap() const; |
| 159 | |
| 160 | typedef struct dos_header { // DOS .EXE header |
| 161 | uint16_t e_magic; // Magic number |
| 162 | uint16_t e_cblp; // Bytes on last page of file |
| 163 | uint16_t e_cp; // Pages in file |
| 164 | uint16_t e_crlc; // Relocations |
| 165 | uint16_t e_cparhdr; // Size of header in paragraphs |
| 166 | uint16_t e_minalloc; // Minimum extra paragraphs needed |
| 167 | uint16_t e_maxalloc; // Maximum extra paragraphs needed |
| 168 | uint16_t e_ss; // Initial (relative) SS value |
| 169 | uint16_t e_sp; // Initial SP value |
| 170 | uint16_t e_csum; // Checksum |
| 171 | uint16_t e_ip; // Initial IP value |
| 172 | uint16_t e_cs; // Initial (relative) CS value |
| 173 | uint16_t e_lfarlc; // File address of relocation table |
| 174 | uint16_t e_ovno; // Overlay number |
| 175 | uint16_t e_res[4]; // Reserved words |
| 176 | uint16_t e_oemid; // OEM identifier (for e_oeminfo) |
| 177 | uint16_t e_oeminfo; // OEM information; e_oemid specific |
| 178 | uint16_t e_res2[10]; // Reserved words |
| 179 | uint32_t e_lfanew; // File address of new exe header |
| 180 | } dos_header_t; |
| 181 | |
| 182 | typedef struct coff_header { |
| 183 | uint16_t machine; |
| 184 | uint16_t nsects; |
| 185 | uint32_t modtime; |
| 186 | uint32_t symoff; |
| 187 | uint32_t nsyms; |
| 188 | uint16_t hdrsize; |
| 189 | uint16_t flags; |
| 190 | } coff_header_t; |
| 191 | |
| 192 | typedef struct data_directory { |
| 193 | uint32_t vmaddr; |
| 194 | uint32_t vmsize; |
| 195 | } data_directory_t; |
| 196 | |
| 197 | typedef struct coff_opt_header |
| 198 | { |
| 199 | uint16_t magic; |
| 200 | uint8_t major_linker_version; |
| 201 | uint8_t minor_linker_version; |
| 202 | uint32_t code_size; |
| 203 | uint32_t data_size; |
| 204 | uint32_t bss_size; |
| 205 | uint32_t entry; |
| 206 | uint32_t code_offset; |
| 207 | uint32_t data_offset; |
| 208 | |
| 209 | uint64_t image_base; |
| 210 | uint32_t sect_alignment; |
| 211 | uint32_t file_alignment; |
| 212 | uint16_t major_os_system_version; |
| 213 | uint16_t minor_os_system_version; |
| 214 | uint16_t major_image_version; |
| 215 | uint16_t minor_image_version; |
| 216 | uint16_t major_subsystem_version; |
| 217 | uint16_t minor_subsystem_version; |
| 218 | uint32_t reserved1; |
| 219 | uint32_t image_size; |
| 220 | uint32_t header_size; |
Greg Clayton | 28469ca | 2011-09-10 01:04:42 +0000 | [diff] [blame] | 221 | uint32_t checksum; |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 222 | uint16_t subsystem; |
| 223 | uint16_t dll_flags; |
| 224 | uint64_t stack_reserve_size; |
| 225 | uint64_t stack_commit_size; |
| 226 | uint64_t heap_reserve_size; |
| 227 | uint64_t heap_commit_size; |
| 228 | uint32_t loader_flags; |
| 229 | // uint32_t num_data_dir_entries; |
| 230 | std::vector<data_directory> data_dirs; // will contain num_data_dir_entries entries |
| 231 | } coff_opt_header_t; |
Virgile Bello | a4fe3a1 | 2013-08-25 13:27:20 +0000 | [diff] [blame] | 232 | |
| 233 | typedef enum coff_data_dir_type |
| 234 | { |
| 235 | coff_data_dir_export_table = 0, |
| 236 | coff_data_dir_import_table = 1, |
| 237 | } coff_data_dir_type; |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 238 | |
| 239 | typedef struct section_header { |
| 240 | char name[8]; |
| 241 | uint32_t vmsize; // Virtual Size |
| 242 | uint32_t vmaddr; // Virtual Addr |
| 243 | uint32_t size; // File size |
| 244 | uint32_t offset; // File offset |
| 245 | uint32_t reloff; // Offset to relocations |
| 246 | uint32_t lineoff;// Offset to line table entries |
| 247 | uint16_t nreloc; // Number of relocation entries |
| 248 | uint16_t nline; // Number of line table entries |
| 249 | uint32_t flags; |
| 250 | } section_header_t; |
| 251 | |
| 252 | typedef struct coff_symbol { |
| 253 | char name[8]; |
| 254 | uint32_t value; |
| 255 | uint16_t sect; |
| 256 | uint16_t type; |
| 257 | uint8_t storage; |
| 258 | uint8_t naux; |
| 259 | } coff_symbol_t; |
Virgile Bello | a4fe3a1 | 2013-08-25 13:27:20 +0000 | [diff] [blame] | 260 | |
| 261 | typedef struct export_directory_entry { |
| 262 | uint32_t characteristics; |
| 263 | uint32_t time_date_stamp; |
| 264 | uint16_t major_version; |
| 265 | uint16_t minor_version; |
| 266 | uint32_t name; |
| 267 | uint32_t base; |
| 268 | uint32_t number_of_functions; |
| 269 | uint32_t number_of_names; |
| 270 | uint32_t address_of_functions; |
| 271 | uint32_t address_of_names; |
| 272 | uint32_t address_of_name_ordinals; |
Michael Sartain | 0b5e5f4 | 2013-08-26 17:07:34 +0000 | [diff] [blame] | 273 | } export_directory_entry; |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 274 | |
Virgile Bello | 89eb1ba | 2014-03-09 09:59:36 +0000 | [diff] [blame] | 275 | static bool ParseDOSHeader (lldb_private::DataExtractor &data, dos_header_t &dos_header); |
| 276 | static bool ParseCOFFHeader (lldb_private::DataExtractor &data, lldb::offset_t *offset_ptr, coff_header_t &coff_header); |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 277 | bool ParseCOFFOptionalHeader (lldb::offset_t *offset_ptr); |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 278 | bool ParseSectionHeaders (uint32_t offset); |
| 279 | |
| 280 | static void DumpDOSHeader(lldb_private::Stream *s, const dos_header_t& header); |
| 281 | static void DumpCOFFHeader(lldb_private::Stream *s, const coff_header_t& header); |
| 282 | static void DumpOptCOFFHeader(lldb_private::Stream *s, const coff_opt_header_t& header); |
| 283 | void DumpSectionHeaders(lldb_private::Stream *s); |
| 284 | void DumpSectionHeader(lldb_private::Stream *s, const section_header_t& sh); |
| 285 | bool GetSectionName(std::string& sect_name, const section_header_t& sect); |
| 286 | |
| 287 | typedef std::vector<section_header_t> SectionHeaderColl; |
| 288 | typedef SectionHeaderColl::iterator SectionHeaderCollIter; |
| 289 | typedef SectionHeaderColl::const_iterator SectionHeaderCollConstIter; |
| 290 | private: |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 291 | dos_header_t m_dos_header; |
| 292 | coff_header_t m_coff_header; |
| 293 | coff_opt_header_t m_coff_header_opt; |
| 294 | SectionHeaderColl m_sect_headers; |
Virgile Bello | 2756adf | 2014-03-08 17:17:20 +0000 | [diff] [blame] | 295 | lldb::addr_t m_image_base; |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 296 | }; |
| 297 | |
Eugene Zelenko | 8157a88 | 2015-10-23 16:56:07 +0000 | [diff] [blame^] | 298 | #endif // liblldb_ObjectFilePECOFF_h_ |