blob: fd33cd3d32f8f10c6070c05d073fe38601b412d0 [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
21class ObjectFilePECOFF :
22 public lldb_private::ObjectFile
23{
24public:
Zachary Turnerad587ae42014-07-28 16:44:49 +000025 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 Clayton49a95e12014-07-29 23:23:58 +000049 } MachineType;
Greg Claytonf754f882011-09-09 20:33:05 +000050
Eugene Zelenko8157a882015-10-23 16:56:07 +000051 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 Claytonf754f882011-09-09 20:33:05 +000060 //------------------------------------------------------------------
61 // Static Functions
62 //------------------------------------------------------------------
63 static void
64 Initialize();
65
66 static void
67 Terminate();
68
Greg Clayton57abc5d2013-05-10 21:47:16 +000069 static lldb_private::ConstString
Greg Claytonf754f882011-09-09 20:33:05 +000070 GetPluginNameStatic();
71
72 static const char *
73 GetPluginDescriptionStatic();
74
75 static ObjectFile *
Greg Claytone72dfb32012-02-24 01:59:29 +000076 CreateInstance (const lldb::ModuleSP &module_sp,
Greg Clayton5ce9c562013-02-06 17:22:03 +000077 lldb::DataBufferSP& data_sp,
78 lldb::offset_t data_offset,
Greg Claytonf754f882011-09-09 20:33:05 +000079 const lldb_private::FileSpec* file,
Greg Clayton5ce9c562013-02-06 17:22:03 +000080 lldb::offset_t offset,
81 lldb::offset_t length);
Greg Claytonf754f882011-09-09 20:33:05 +000082
Greg Claytonc9660542012-02-05 02:38:54 +000083 static lldb_private::ObjectFile *
Greg Claytone72dfb32012-02-24 01:59:29 +000084 CreateMemoryInstance (const lldb::ModuleSP &module_sp,
Greg Claytonc9660542012-02-05 02:38:54 +000085 lldb::DataBufferSP& data_sp,
86 const lldb::ProcessSP &process_sp,
87 lldb::addr_t header_addr);
Greg Claytonf4d6de62013-04-24 22:29:28 +000088
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 Claytonf754f882011-09-09 20:33:05 +000097 static bool
Adrian McCarthyf7d18932015-11-20 23:09:11 +000098 SaveCore (const lldb::ProcessSP &process_sp,
99 const lldb_private::FileSpec &outfile,
100 lldb_private::Error &error);
101
102 static bool
Greg Clayton5ce9c562013-02-06 17:22:03 +0000103 MagicBytesMatch (lldb::DataBufferSP& data_sp);
Greg Claytonf754f882011-09-09 20:33:05 +0000104
Eugene Zelenko8157a882015-10-23 16:56:07 +0000105 bool
106 ParseHeader() override;
Greg Claytonf754f882011-09-09 20:33:05 +0000107
Eugene Zelenko8157a882015-10-23 16:56:07 +0000108 bool
109 SetLoadAddress(lldb_private::Target &target, lldb::addr_t value, bool value_is_offset) override;
Virgile Bello2756adf2014-03-08 17:17:20 +0000110
Eugene Zelenko8157a882015-10-23 16:56:07 +0000111 lldb::ByteOrder
112 GetByteOrder() const override;
Greg Claytonf754f882011-09-09 20:33:05 +0000113
Eugene Zelenko8157a882015-10-23 16:56:07 +0000114 bool
115 IsExecutable() const override;
Greg Claytonf754f882011-09-09 20:33:05 +0000116
Eugene Zelenko8157a882015-10-23 16:56:07 +0000117 uint32_t
118 GetAddressByteSize() const override;
Greg Claytonf754f882011-09-09 20:33:05 +0000119
120// virtual lldb_private::AddressClass
121// GetAddressClass (lldb::addr_t file_addr);
Greg Clayton3046e662013-07-10 01:23:25 +0000122
Eugene Zelenko8157a882015-10-23 16:56:07 +0000123 lldb_private::Symtab *
124 GetSymtab() override;
Greg Claytonf754f882011-09-09 20:33:05 +0000125
Eugene Zelenko8157a882015-10-23 16:56:07 +0000126 bool
127 IsStripped() override;
128
129 void
130 CreateSections(lldb_private::SectionList &unified_section_list) override;
Greg Claytonf754f882011-09-09 20:33:05 +0000131
Eugene Zelenko8157a882015-10-23 16:56:07 +0000132 void
133 Dump(lldb_private::Stream *s) override;
Greg Claytonf754f882011-09-09 20:33:05 +0000134
Eugene Zelenko8157a882015-10-23 16:56:07 +0000135 bool
136 GetArchitecture(lldb_private::ArchSpec &arch) override;
Greg Claytonf754f882011-09-09 20:33:05 +0000137
Eugene Zelenko8157a882015-10-23 16:56:07 +0000138 bool
139 GetUUID(lldb_private::UUID* uuid) override;
140
141 uint32_t
142 GetDependentModules(lldb_private::FileSpecList& files) override;
143
144// virtual lldb_private::Address
145// GetEntryPointAddress ();
146
147 ObjectFile::Type
148 CalculateType() override;
149
150 ObjectFile::Strata
151 CalculateStrata() override;
Greg Claytonf754f882011-09-09 20:33:05 +0000152
153 //------------------------------------------------------------------
154 // PluginInterface protocol
155 //------------------------------------------------------------------
Eugene Zelenko8157a882015-10-23 16:56:07 +0000156 lldb_private::ConstString
157 GetPluginName() override;
Greg Claytonf754f882011-09-09 20:33:05 +0000158
Eugene Zelenko8157a882015-10-23 16:56:07 +0000159 uint32_t
160 GetPluginVersion() override;
161
Greg Claytonf754f882011-09-09 20:33:05 +0000162protected:
163 bool NeedsEndianSwap() const;
164
165 typedef struct dos_header { // DOS .EXE header
166 uint16_t e_magic; // Magic number
167 uint16_t e_cblp; // Bytes on last page of file
168 uint16_t e_cp; // Pages in file
169 uint16_t e_crlc; // Relocations
170 uint16_t e_cparhdr; // Size of header in paragraphs
171 uint16_t e_minalloc; // Minimum extra paragraphs needed
172 uint16_t e_maxalloc; // Maximum extra paragraphs needed
173 uint16_t e_ss; // Initial (relative) SS value
174 uint16_t e_sp; // Initial SP value
175 uint16_t e_csum; // Checksum
176 uint16_t e_ip; // Initial IP value
177 uint16_t e_cs; // Initial (relative) CS value
178 uint16_t e_lfarlc; // File address of relocation table
179 uint16_t e_ovno; // Overlay number
180 uint16_t e_res[4]; // Reserved words
181 uint16_t e_oemid; // OEM identifier (for e_oeminfo)
182 uint16_t e_oeminfo; // OEM information; e_oemid specific
183 uint16_t e_res2[10]; // Reserved words
184 uint32_t e_lfanew; // File address of new exe header
185 } dos_header_t;
186
187 typedef struct coff_header {
188 uint16_t machine;
189 uint16_t nsects;
190 uint32_t modtime;
191 uint32_t symoff;
192 uint32_t nsyms;
193 uint16_t hdrsize;
194 uint16_t flags;
195 } coff_header_t;
196
197 typedef struct data_directory {
198 uint32_t vmaddr;
199 uint32_t vmsize;
200 } data_directory_t;
201
202 typedef struct coff_opt_header
203 {
204 uint16_t magic;
205 uint8_t major_linker_version;
206 uint8_t minor_linker_version;
207 uint32_t code_size;
208 uint32_t data_size;
209 uint32_t bss_size;
210 uint32_t entry;
211 uint32_t code_offset;
212 uint32_t data_offset;
213
214 uint64_t image_base;
215 uint32_t sect_alignment;
216 uint32_t file_alignment;
217 uint16_t major_os_system_version;
218 uint16_t minor_os_system_version;
219 uint16_t major_image_version;
220 uint16_t minor_image_version;
221 uint16_t major_subsystem_version;
222 uint16_t minor_subsystem_version;
223 uint32_t reserved1;
224 uint32_t image_size;
225 uint32_t header_size;
Greg Clayton28469ca2011-09-10 01:04:42 +0000226 uint32_t checksum;
Greg Claytonf754f882011-09-09 20:33:05 +0000227 uint16_t subsystem;
228 uint16_t dll_flags;
229 uint64_t stack_reserve_size;
230 uint64_t stack_commit_size;
231 uint64_t heap_reserve_size;
232 uint64_t heap_commit_size;
233 uint32_t loader_flags;
234 // uint32_t num_data_dir_entries;
235 std::vector<data_directory> data_dirs; // will contain num_data_dir_entries entries
236 } coff_opt_header_t;
Virgile Belloa4fe3a12013-08-25 13:27:20 +0000237
238 typedef enum coff_data_dir_type
239 {
240 coff_data_dir_export_table = 0,
241 coff_data_dir_import_table = 1,
242 } coff_data_dir_type;
Greg Claytonf754f882011-09-09 20:33:05 +0000243
244 typedef struct section_header {
245 char name[8];
246 uint32_t vmsize; // Virtual Size
247 uint32_t vmaddr; // Virtual Addr
248 uint32_t size; // File size
249 uint32_t offset; // File offset
250 uint32_t reloff; // Offset to relocations
251 uint32_t lineoff;// Offset to line table entries
252 uint16_t nreloc; // Number of relocation entries
253 uint16_t nline; // Number of line table entries
254 uint32_t flags;
255 } section_header_t;
256
257 typedef struct coff_symbol {
258 char name[8];
259 uint32_t value;
260 uint16_t sect;
261 uint16_t type;
262 uint8_t storage;
263 uint8_t naux;
264 } coff_symbol_t;
Virgile Belloa4fe3a12013-08-25 13:27:20 +0000265
266 typedef struct export_directory_entry {
267 uint32_t characteristics;
268 uint32_t time_date_stamp;
269 uint16_t major_version;
270 uint16_t minor_version;
271 uint32_t name;
272 uint32_t base;
273 uint32_t number_of_functions;
274 uint32_t number_of_names;
275 uint32_t address_of_functions;
276 uint32_t address_of_names;
277 uint32_t address_of_name_ordinals;
Michael Sartain0b5e5f42013-08-26 17:07:34 +0000278 } export_directory_entry;
Greg Claytonf754f882011-09-09 20:33:05 +0000279
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000280 static bool ParseDOSHeader (lldb_private::DataExtractor &data, dos_header_t &dos_header);
281 static bool ParseCOFFHeader (lldb_private::DataExtractor &data, lldb::offset_t *offset_ptr, coff_header_t &coff_header);
Greg Claytonc7bece562013-01-25 18:06:21 +0000282 bool ParseCOFFOptionalHeader (lldb::offset_t *offset_ptr);
Greg Claytonf754f882011-09-09 20:33:05 +0000283 bool ParseSectionHeaders (uint32_t offset);
284
285 static void DumpDOSHeader(lldb_private::Stream *s, const dos_header_t& header);
286 static void DumpCOFFHeader(lldb_private::Stream *s, const coff_header_t& header);
287 static void DumpOptCOFFHeader(lldb_private::Stream *s, const coff_opt_header_t& header);
288 void DumpSectionHeaders(lldb_private::Stream *s);
289 void DumpSectionHeader(lldb_private::Stream *s, const section_header_t& sh);
290 bool GetSectionName(std::string& sect_name, const section_header_t& sect);
291
292 typedef std::vector<section_header_t> SectionHeaderColl;
293 typedef SectionHeaderColl::iterator SectionHeaderCollIter;
294 typedef SectionHeaderColl::const_iterator SectionHeaderCollConstIter;
295private:
Greg Claytonf754f882011-09-09 20:33:05 +0000296 dos_header_t m_dos_header;
297 coff_header_t m_coff_header;
298 coff_opt_header_t m_coff_header_opt;
299 SectionHeaderColl m_sect_headers;
Virgile Bello2756adf2014-03-08 17:17:20 +0000300 lldb::addr_t m_image_base;
Greg Claytonf754f882011-09-09 20:33:05 +0000301};
302
Eugene Zelenko8157a882015-10-23 16:56:07 +0000303#endif // liblldb_ObjectFilePECOFF_h_