blob: eb8d9e75189ed61a809e1a7b3827229d72b81d10 [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
13#include <vector>
14
Greg Claytonf754f882011-09-09 20:33:05 +000015#include "lldb/Symbol/ObjectFile.h"
16
17class ObjectFilePECOFF :
18 public lldb_private::ObjectFile
19{
20public:
Zachary Turnerad587ae42014-07-28 16:44:49 +000021 typedef enum MachineType
22 {
23 MachineUnknown = 0x0,
24 MachineAm33 = 0x1d3,
25 MachineAmd64 = 0x8664,
26 MachineArm = 0x1c0,
27 MachineArmNt = 0x1c4,
28 MachineArm64 = 0xaa64,
29 MachineEbc = 0xebc,
30 MachineX86 = 0x14c,
31 MachineIA64 = 0x200,
32 MachineM32R = 0x9041,
33 MachineMips16 = 0x266,
34 MachineMipsFpu = 0x366,
35 MachineMipsFpu16 = 0x466,
36 MachinePowerPc = 0x1f0,
37 MachinePowerPcfp = 0x1f1,
38 MachineR4000 = 0x166,
39 MachineSh3 = 0x1a2,
40 MachineSh3dsp = 0x1a3,
41 MachineSh4 = 0x1a6,
42 MachineSh5 = 0x1a8,
43 MachineThumb = 0x1c2,
44 MachineWcemIpsv2 = 0x169
Greg Clayton49a95e12014-07-29 23:23:58 +000045 } MachineType;
Greg Claytonf754f882011-09-09 20:33:05 +000046
47 //------------------------------------------------------------------
48 // Static Functions
49 //------------------------------------------------------------------
50 static void
51 Initialize();
52
53 static void
54 Terminate();
55
Greg Clayton57abc5d2013-05-10 21:47:16 +000056 static lldb_private::ConstString
Greg Claytonf754f882011-09-09 20:33:05 +000057 GetPluginNameStatic();
58
59 static const char *
60 GetPluginDescriptionStatic();
61
62 static ObjectFile *
Greg Claytone72dfb32012-02-24 01:59:29 +000063 CreateInstance (const lldb::ModuleSP &module_sp,
Greg Clayton5ce9c562013-02-06 17:22:03 +000064 lldb::DataBufferSP& data_sp,
65 lldb::offset_t data_offset,
Greg Claytonf754f882011-09-09 20:33:05 +000066 const lldb_private::FileSpec* file,
Greg Clayton5ce9c562013-02-06 17:22:03 +000067 lldb::offset_t offset,
68 lldb::offset_t length);
Greg Claytonf754f882011-09-09 20:33:05 +000069
Greg Claytonc9660542012-02-05 02:38:54 +000070 static lldb_private::ObjectFile *
Greg Claytone72dfb32012-02-24 01:59:29 +000071 CreateMemoryInstance (const lldb::ModuleSP &module_sp,
Greg Claytonc9660542012-02-05 02:38:54 +000072 lldb::DataBufferSP& data_sp,
73 const lldb::ProcessSP &process_sp,
74 lldb::addr_t header_addr);
Greg Claytonf4d6de62013-04-24 22:29:28 +000075
76 static size_t
77 GetModuleSpecifications (const lldb_private::FileSpec& file,
78 lldb::DataBufferSP& data_sp,
79 lldb::offset_t data_offset,
80 lldb::offset_t file_offset,
81 lldb::offset_t length,
82 lldb_private::ModuleSpecList &specs);
83
Greg Claytonf754f882011-09-09 20:33:05 +000084 static bool
Greg Clayton5ce9c562013-02-06 17:22:03 +000085 MagicBytesMatch (lldb::DataBufferSP& data_sp);
Greg Claytonf754f882011-09-09 20:33:05 +000086
87
Greg Claytone72dfb32012-02-24 01:59:29 +000088 ObjectFilePECOFF (const lldb::ModuleSP &module_sp,
Greg Clayton5ce9c562013-02-06 17:22:03 +000089 lldb::DataBufferSP& data_sp,
90 lldb::offset_t data_offset,
Greg Claytonf754f882011-09-09 20:33:05 +000091 const lldb_private::FileSpec* file,
Greg Clayton5ce9c562013-02-06 17:22:03 +000092 lldb::offset_t file_offset,
93 lldb::offset_t length);
Greg Claytonf754f882011-09-09 20:33:05 +000094
95 virtual
96 ~ObjectFilePECOFF();
97
98 virtual bool
99 ParseHeader ();
100
Virgile Bello2756adf2014-03-08 17:17:20 +0000101 virtual bool
102 SetLoadAddress(lldb_private::Target &target, lldb::addr_t value, bool value_is_offset);
103
Greg Claytonf754f882011-09-09 20:33:05 +0000104 virtual lldb::ByteOrder
105 GetByteOrder () const;
106
107 virtual bool
108 IsExecutable () const;
109
Greg Claytonc7bece562013-01-25 18:06:21 +0000110 virtual uint32_t
Greg Claytonf754f882011-09-09 20:33:05 +0000111 GetAddressByteSize () const;
112
113// virtual lldb_private::AddressClass
114// GetAddressClass (lldb::addr_t file_addr);
115//
116 virtual lldb_private::Symtab *
Greg Clayton3046e662013-07-10 01:23:25 +0000117 GetSymtab ();
Greg Claytonf754f882011-09-09 20:33:05 +0000118
Greg Clayton3046e662013-07-10 01:23:25 +0000119 virtual bool
120 IsStripped ();
121
122 virtual void
123 CreateSections (lldb_private::SectionList &unified_section_list);
Greg Claytonf754f882011-09-09 20:33:05 +0000124
125 virtual void
126 Dump (lldb_private::Stream *s);
127
128 virtual bool
129 GetArchitecture (lldb_private::ArchSpec &arch);
130
131 virtual bool
132 GetUUID (lldb_private::UUID* uuid);
133
134 virtual uint32_t
135 GetDependentModules (lldb_private::FileSpecList& files);
136
137 //------------------------------------------------------------------
138 // PluginInterface protocol
139 //------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +0000140 virtual lldb_private::ConstString
Greg Claytonf754f882011-09-09 20:33:05 +0000141 GetPluginName();
142
Greg Claytonf754f882011-09-09 20:33:05 +0000143 virtual uint32_t
144 GetPluginVersion();
145//
146// virtual lldb_private::Address
147// GetEntryPointAddress ();
148
149 virtual ObjectFile::Type
150 CalculateType();
151
152 virtual ObjectFile::Strata
153 CalculateStrata();
154
155protected:
156 bool NeedsEndianSwap() const;
157
158 typedef struct dos_header { // DOS .EXE header
159 uint16_t e_magic; // Magic number
160 uint16_t e_cblp; // Bytes on last page of file
161 uint16_t e_cp; // Pages in file
162 uint16_t e_crlc; // Relocations
163 uint16_t e_cparhdr; // Size of header in paragraphs
164 uint16_t e_minalloc; // Minimum extra paragraphs needed
165 uint16_t e_maxalloc; // Maximum extra paragraphs needed
166 uint16_t e_ss; // Initial (relative) SS value
167 uint16_t e_sp; // Initial SP value
168 uint16_t e_csum; // Checksum
169 uint16_t e_ip; // Initial IP value
170 uint16_t e_cs; // Initial (relative) CS value
171 uint16_t e_lfarlc; // File address of relocation table
172 uint16_t e_ovno; // Overlay number
173 uint16_t e_res[4]; // Reserved words
174 uint16_t e_oemid; // OEM identifier (for e_oeminfo)
175 uint16_t e_oeminfo; // OEM information; e_oemid specific
176 uint16_t e_res2[10]; // Reserved words
177 uint32_t e_lfanew; // File address of new exe header
178 } dos_header_t;
179
180 typedef struct coff_header {
181 uint16_t machine;
182 uint16_t nsects;
183 uint32_t modtime;
184 uint32_t symoff;
185 uint32_t nsyms;
186 uint16_t hdrsize;
187 uint16_t flags;
188 } coff_header_t;
189
190 typedef struct data_directory {
191 uint32_t vmaddr;
192 uint32_t vmsize;
193 } data_directory_t;
194
195 typedef struct coff_opt_header
196 {
197 uint16_t magic;
198 uint8_t major_linker_version;
199 uint8_t minor_linker_version;
200 uint32_t code_size;
201 uint32_t data_size;
202 uint32_t bss_size;
203 uint32_t entry;
204 uint32_t code_offset;
205 uint32_t data_offset;
206
207 uint64_t image_base;
208 uint32_t sect_alignment;
209 uint32_t file_alignment;
210 uint16_t major_os_system_version;
211 uint16_t minor_os_system_version;
212 uint16_t major_image_version;
213 uint16_t minor_image_version;
214 uint16_t major_subsystem_version;
215 uint16_t minor_subsystem_version;
216 uint32_t reserved1;
217 uint32_t image_size;
218 uint32_t header_size;
Greg Clayton28469ca2011-09-10 01:04:42 +0000219 uint32_t checksum;
Greg Claytonf754f882011-09-09 20:33:05 +0000220 uint16_t subsystem;
221 uint16_t dll_flags;
222 uint64_t stack_reserve_size;
223 uint64_t stack_commit_size;
224 uint64_t heap_reserve_size;
225 uint64_t heap_commit_size;
226 uint32_t loader_flags;
227 // uint32_t num_data_dir_entries;
228 std::vector<data_directory> data_dirs; // will contain num_data_dir_entries entries
229 } coff_opt_header_t;
Virgile Belloa4fe3a12013-08-25 13:27:20 +0000230
231 typedef enum coff_data_dir_type
232 {
233 coff_data_dir_export_table = 0,
234 coff_data_dir_import_table = 1,
235 } coff_data_dir_type;
Greg Claytonf754f882011-09-09 20:33:05 +0000236
237 typedef struct section_header {
238 char name[8];
239 uint32_t vmsize; // Virtual Size
240 uint32_t vmaddr; // Virtual Addr
241 uint32_t size; // File size
242 uint32_t offset; // File offset
243 uint32_t reloff; // Offset to relocations
244 uint32_t lineoff;// Offset to line table entries
245 uint16_t nreloc; // Number of relocation entries
246 uint16_t nline; // Number of line table entries
247 uint32_t flags;
248 } section_header_t;
249
250 typedef struct coff_symbol {
251 char name[8];
252 uint32_t value;
253 uint16_t sect;
254 uint16_t type;
255 uint8_t storage;
256 uint8_t naux;
257 } coff_symbol_t;
Virgile Belloa4fe3a12013-08-25 13:27:20 +0000258
259 typedef struct export_directory_entry {
260 uint32_t characteristics;
261 uint32_t time_date_stamp;
262 uint16_t major_version;
263 uint16_t minor_version;
264 uint32_t name;
265 uint32_t base;
266 uint32_t number_of_functions;
267 uint32_t number_of_names;
268 uint32_t address_of_functions;
269 uint32_t address_of_names;
270 uint32_t address_of_name_ordinals;
Michael Sartain0b5e5f42013-08-26 17:07:34 +0000271 } export_directory_entry;
Greg Claytonf754f882011-09-09 20:33:05 +0000272
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000273 static bool ParseDOSHeader (lldb_private::DataExtractor &data, dos_header_t &dos_header);
274 static bool ParseCOFFHeader (lldb_private::DataExtractor &data, lldb::offset_t *offset_ptr, coff_header_t &coff_header);
Greg Claytonc7bece562013-01-25 18:06:21 +0000275 bool ParseCOFFOptionalHeader (lldb::offset_t *offset_ptr);
Greg Claytonf754f882011-09-09 20:33:05 +0000276 bool ParseSectionHeaders (uint32_t offset);
277
278 static void DumpDOSHeader(lldb_private::Stream *s, const dos_header_t& header);
279 static void DumpCOFFHeader(lldb_private::Stream *s, const coff_header_t& header);
280 static void DumpOptCOFFHeader(lldb_private::Stream *s, const coff_opt_header_t& header);
281 void DumpSectionHeaders(lldb_private::Stream *s);
282 void DumpSectionHeader(lldb_private::Stream *s, const section_header_t& sh);
283 bool GetSectionName(std::string& sect_name, const section_header_t& sect);
284
285 typedef std::vector<section_header_t> SectionHeaderColl;
286 typedef SectionHeaderColl::iterator SectionHeaderCollIter;
287 typedef SectionHeaderColl::const_iterator SectionHeaderCollConstIter;
288private:
Greg Claytonf754f882011-09-09 20:33:05 +0000289 dos_header_t m_dos_header;
290 coff_header_t m_coff_header;
291 coff_opt_header_t m_coff_header_opt;
292 SectionHeaderColl m_sect_headers;
Virgile Bello2756adf2014-03-08 17:17:20 +0000293 lldb::addr_t m_image_base;
Greg Claytonf754f882011-09-09 20:33:05 +0000294};
295
296#endif // #ifndef liblldb_ObjectFilePECOFF_h_