blob: d8a94e19d34f4acd11cc1b7a6f5b86e1ada3314a [file] [log] [blame]
Greg Claytonf754f882011-09-09 20:33:05 +00001//===-- 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 Zelenko8157a882015-10-23 16:56:07 +000013// C Includes
14// C++ Includes
Greg Claytonf754f882011-09-09 20:33:05 +000015#include <vector>
16
Eugene Zelenko8157a882015-10-23 16:56:07 +000017// Other libraries and framework includes
18// Project includes
Greg Claytonf754f882011-09-09 20:33:05 +000019#include "lldb/Symbol/ObjectFile.h"
20
Kate Stoneb9c1b512016-09-06 20:57:50 +000021class ObjectFilePECOFF : public lldb_private::ObjectFile {
Greg Claytonf754f882011-09-09 20:33:05 +000022public:
Kate Stoneb9c1b512016-09-06 20:57:50 +000023 typedef enum MachineType {
24 MachineUnknown = 0x0,
25 MachineAm33 = 0x1d3,
26 MachineAmd64 = 0x8664,
27 MachineArm = 0x1c0,
28 MachineArmNt = 0x1c4,
29 MachineArm64 = 0xaa64,
30 MachineEbc = 0xebc,
31 MachineX86 = 0x14c,
32 MachineIA64 = 0x200,
33 MachineM32R = 0x9041,
34 MachineMips16 = 0x266,
35 MachineMipsFpu = 0x366,
36 MachineMipsFpu16 = 0x466,
37 MachinePowerPc = 0x1f0,
38 MachinePowerPcfp = 0x1f1,
39 MachineR4000 = 0x166,
40 MachineSh3 = 0x1a2,
41 MachineSh3dsp = 0x1a3,
42 MachineSh4 = 0x1a6,
43 MachineSh5 = 0x1a8,
44 MachineThumb = 0x1c2,
45 MachineWcemIpsv2 = 0x169
46 } MachineType;
Eugene Zelenko8157a882015-10-23 16:56:07 +000047
Kate Stoneb9c1b512016-09-06 20:57:50 +000048 ObjectFilePECOFF(const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp,
49 lldb::offset_t data_offset,
50 const lldb_private::FileSpec *file,
51 lldb::offset_t file_offset, lldb::offset_t length);
Eugene Zelenko8157a882015-10-23 16:56:07 +000052
Walter Erquinigo344546b2016-10-17 20:28:19 +000053 ObjectFilePECOFF(const lldb::ModuleSP &module_sp,
54 lldb::DataBufferSP &header_data_sp,
55 const lldb::ProcessSP &process_sp, lldb::addr_t header_addr);
56
Kate Stoneb9c1b512016-09-06 20:57:50 +000057 ~ObjectFilePECOFF() override;
Greg Claytonf4d6de62013-04-24 22:29:28 +000058
Kate Stoneb9c1b512016-09-06 20:57:50 +000059 //------------------------------------------------------------------
60 // Static Functions
61 //------------------------------------------------------------------
62 static void Initialize();
Adrian McCarthyf7d18932015-11-20 23:09:11 +000063
Kate Stoneb9c1b512016-09-06 20:57:50 +000064 static void Terminate();
Adrian McCarthyc35b91c2016-01-26 00:58:09 +000065
Kate Stoneb9c1b512016-09-06 20:57:50 +000066 static lldb_private::ConstString GetPluginNameStatic();
Adrian McCarthyc35b91c2016-01-26 00:58:09 +000067
Kate Stoneb9c1b512016-09-06 20:57:50 +000068 static const char *GetPluginDescriptionStatic();
Virgile Bello2756adf2014-03-08 17:17:20 +000069
Kate Stoneb9c1b512016-09-06 20:57:50 +000070 static ObjectFile *
71 CreateInstance(const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp,
72 lldb::offset_t data_offset, const lldb_private::FileSpec *file,
73 lldb::offset_t offset, lldb::offset_t length);
Adrian McCarthyc35b91c2016-01-26 00:58:09 +000074
Kate Stoneb9c1b512016-09-06 20:57:50 +000075 static lldb_private::ObjectFile *CreateMemoryInstance(
76 const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp,
77 const lldb::ProcessSP &process_sp, lldb::addr_t header_addr);
Greg Clayton3046e662013-07-10 01:23:25 +000078
Kate Stoneb9c1b512016-09-06 20:57:50 +000079 static size_t GetModuleSpecifications(const lldb_private::FileSpec &file,
80 lldb::DataBufferSP &data_sp,
81 lldb::offset_t data_offset,
82 lldb::offset_t file_offset,
83 lldb::offset_t length,
84 lldb_private::ModuleSpecList &specs);
Eugene Zelenko8157a882015-10-23 16:56:07 +000085
Kate Stoneb9c1b512016-09-06 20:57:50 +000086 static bool SaveCore(const lldb::ProcessSP &process_sp,
87 const lldb_private::FileSpec &outfile,
Zachary Turner97206d52017-05-12 04:51:55 +000088 lldb_private::Status &error);
Kate Stoneb9c1b512016-09-06 20:57:50 +000089
90 static bool MagicBytesMatch(lldb::DataBufferSP &data_sp);
91
92 static lldb::SymbolType MapSymbolType(uint16_t coff_symbol_type);
93
94 bool ParseHeader() override;
95
96 bool SetLoadAddress(lldb_private::Target &target, lldb::addr_t value,
97 bool value_is_offset) override;
98
99 lldb::ByteOrder GetByteOrder() const override;
100
101 bool IsExecutable() const override;
102
103 uint32_t GetAddressByteSize() const override;
104
105 // virtual lldb_private::AddressClass
106 // GetAddressClass (lldb::addr_t file_addr);
107
108 lldb_private::Symtab *GetSymtab() override;
109
110 bool IsStripped() override;
111
112 void CreateSections(lldb_private::SectionList &unified_section_list) override;
113
114 void Dump(lldb_private::Stream *s) override;
115
116 bool GetArchitecture(lldb_private::ArchSpec &arch) override;
117
118 bool GetUUID(lldb_private::UUID *uuid) override;
119
120 uint32_t GetDependentModules(lldb_private::FileSpecList &files) override;
121
122 virtual lldb_private::Address GetEntryPointAddress() override;
123
124 ObjectFile::Type CalculateType() override;
125
126 ObjectFile::Strata CalculateStrata() override;
127
128 //------------------------------------------------------------------
129 // PluginInterface protocol
130 //------------------------------------------------------------------
131 lldb_private::ConstString GetPluginName() override;
132
133 uint32_t GetPluginVersion() override;
Eugene Zelenko8157a882015-10-23 16:56:07 +0000134
Zachary Turnerfb3b3bd2016-09-20 20:44:50 +0000135 bool IsWindowsSubsystem();
136
Walter Erquinigo344546b2016-10-17 20:28:19 +0000137 lldb_private::DataExtractor ReadImageData(uint32_t offset, size_t size);
138
Greg Claytonf754f882011-09-09 20:33:05 +0000139protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000140 bool NeedsEndianSwap() const;
Virgile Belloa4fe3a12013-08-25 13:27:20 +0000141
Kate Stoneb9c1b512016-09-06 20:57:50 +0000142 typedef struct dos_header { // DOS .EXE header
143 uint16_t e_magic; // Magic number
144 uint16_t e_cblp; // Bytes on last page of file
145 uint16_t e_cp; // Pages in file
146 uint16_t e_crlc; // Relocations
147 uint16_t e_cparhdr; // Size of header in paragraphs
148 uint16_t e_minalloc; // Minimum extra paragraphs needed
149 uint16_t e_maxalloc; // Maximum extra paragraphs needed
150 uint16_t e_ss; // Initial (relative) SS value
151 uint16_t e_sp; // Initial SP value
152 uint16_t e_csum; // Checksum
153 uint16_t e_ip; // Initial IP value
154 uint16_t e_cs; // Initial (relative) CS value
155 uint16_t e_lfarlc; // File address of relocation table
156 uint16_t e_ovno; // Overlay number
157 uint16_t e_res[4]; // Reserved words
158 uint16_t e_oemid; // OEM identifier (for e_oeminfo)
159 uint16_t e_oeminfo; // OEM information; e_oemid specific
160 uint16_t e_res2[10]; // Reserved words
161 uint32_t e_lfanew; // File address of new exe header
162 } dos_header_t;
Virgile Belloa4fe3a12013-08-25 13:27:20 +0000163
Kate Stoneb9c1b512016-09-06 20:57:50 +0000164 typedef struct coff_header {
165 uint16_t machine;
166 uint16_t nsects;
167 uint32_t modtime;
168 uint32_t symoff;
169 uint32_t nsyms;
170 uint16_t hdrsize;
171 uint16_t flags;
172 } coff_header_t;
173
174 typedef struct data_directory {
175 uint32_t vmaddr;
176 uint32_t vmsize;
177 } data_directory_t;
178
179 typedef struct coff_opt_header {
180 uint16_t magic;
181 uint8_t major_linker_version;
182 uint8_t minor_linker_version;
183 uint32_t code_size;
184 uint32_t data_size;
185 uint32_t bss_size;
186 uint32_t entry;
187 uint32_t code_offset;
188 uint32_t data_offset;
189
190 uint64_t image_base;
191 uint32_t sect_alignment;
192 uint32_t file_alignment;
193 uint16_t major_os_system_version;
194 uint16_t minor_os_system_version;
195 uint16_t major_image_version;
196 uint16_t minor_image_version;
197 uint16_t major_subsystem_version;
198 uint16_t minor_subsystem_version;
199 uint32_t reserved1;
200 uint32_t image_size;
201 uint32_t header_size;
202 uint32_t checksum;
203 uint16_t subsystem;
204 uint16_t dll_flags;
205 uint64_t stack_reserve_size;
206 uint64_t stack_commit_size;
207 uint64_t heap_reserve_size;
208 uint64_t heap_commit_size;
209 uint32_t loader_flags;
210 // uint32_t num_data_dir_entries;
211 std::vector<data_directory>
212 data_dirs; // will contain num_data_dir_entries entries
213 } coff_opt_header_t;
214
215 typedef enum coff_data_dir_type {
216 coff_data_dir_export_table = 0,
217 coff_data_dir_import_table = 1,
218 } coff_data_dir_type;
219
220 typedef struct section_header {
221 char name[8];
222 uint32_t vmsize; // Virtual Size
223 uint32_t vmaddr; // Virtual Addr
224 uint32_t size; // File size
225 uint32_t offset; // File offset
226 uint32_t reloff; // Offset to relocations
227 uint32_t lineoff; // Offset to line table entries
228 uint16_t nreloc; // Number of relocation entries
229 uint16_t nline; // Number of line table entries
230 uint32_t flags;
231 } section_header_t;
232
233 typedef struct coff_symbol {
234 char name[8];
235 uint32_t value;
236 uint16_t sect;
237 uint16_t type;
238 uint8_t storage;
239 uint8_t naux;
240 } coff_symbol_t;
241
242 typedef struct export_directory_entry {
243 uint32_t characteristics;
244 uint32_t time_date_stamp;
245 uint16_t major_version;
246 uint16_t minor_version;
247 uint32_t name;
248 uint32_t base;
249 uint32_t number_of_functions;
250 uint32_t number_of_names;
251 uint32_t address_of_functions;
252 uint32_t address_of_names;
253 uint32_t address_of_name_ordinals;
254 } export_directory_entry;
255
256 static bool ParseDOSHeader(lldb_private::DataExtractor &data,
257 dos_header_t &dos_header);
258 static bool ParseCOFFHeader(lldb_private::DataExtractor &data,
259 lldb::offset_t *offset_ptr,
260 coff_header_t &coff_header);
261 bool ParseCOFFOptionalHeader(lldb::offset_t *offset_ptr);
262 bool ParseSectionHeaders(uint32_t offset);
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 bool GetSectionName(std::string &sect_name, const section_header_t &sect);
273
274 typedef std::vector<section_header_t> SectionHeaderColl;
275 typedef SectionHeaderColl::iterator SectionHeaderCollIter;
276 typedef SectionHeaderColl::const_iterator SectionHeaderCollConstIter;
277
Greg Claytonf754f882011-09-09 20:33:05 +0000278private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000279 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;
Greg Claytonf754f882011-09-09 20:33:05 +0000285};
286
Eugene Zelenko8157a882015-10-23 16:56:07 +0000287#endif // liblldb_ObjectFilePECOFF_h_