blob: 2031382a1e1a6cbddd7a44a24058397549c8b6f6 [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
Kate Stoneb9c1b512016-09-06 20:57:50 +000053 ~ObjectFilePECOFF() override;
Greg Claytonf4d6de62013-04-24 22:29:28 +000054
Kate Stoneb9c1b512016-09-06 20:57:50 +000055 //------------------------------------------------------------------
56 // Static Functions
57 //------------------------------------------------------------------
58 static void Initialize();
Adrian McCarthyf7d18932015-11-20 23:09:11 +000059
Kate Stoneb9c1b512016-09-06 20:57:50 +000060 static void Terminate();
Adrian McCarthyc35b91c2016-01-26 00:58:09 +000061
Kate Stoneb9c1b512016-09-06 20:57:50 +000062 static lldb_private::ConstString GetPluginNameStatic();
Adrian McCarthyc35b91c2016-01-26 00:58:09 +000063
Kate Stoneb9c1b512016-09-06 20:57:50 +000064 static const char *GetPluginDescriptionStatic();
Virgile Bello2756adf2014-03-08 17:17:20 +000065
Kate Stoneb9c1b512016-09-06 20:57:50 +000066 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);
Adrian McCarthyc35b91c2016-01-26 00:58:09 +000070
Kate Stoneb9c1b512016-09-06 20:57:50 +000071 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);
Greg Clayton3046e662013-07-10 01:23:25 +000074
Kate Stoneb9c1b512016-09-06 20:57:50 +000075 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);
Eugene Zelenko8157a882015-10-23 16:56:07 +000081
Kate Stoneb9c1b512016-09-06 20:57:50 +000082 static bool SaveCore(const lldb::ProcessSP &process_sp,
83 const lldb_private::FileSpec &outfile,
84 lldb_private::Error &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 bool GetArchitecture(lldb_private::ArchSpec &arch) override;
113
114 bool GetUUID(lldb_private::UUID *uuid) override;
115
116 uint32_t GetDependentModules(lldb_private::FileSpecList &files) override;
117
118 virtual lldb_private::Address GetEntryPointAddress() override;
119
120 ObjectFile::Type CalculateType() override;
121
122 ObjectFile::Strata CalculateStrata() override;
123
124 //------------------------------------------------------------------
125 // PluginInterface protocol
126 //------------------------------------------------------------------
127 lldb_private::ConstString GetPluginName() override;
128
129 uint32_t GetPluginVersion() override;
Eugene Zelenko8157a882015-10-23 16:56:07 +0000130
Greg Claytonf754f882011-09-09 20:33:05 +0000131protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000132 bool NeedsEndianSwap() const;
Virgile Belloa4fe3a12013-08-25 13:27:20 +0000133
Kate Stoneb9c1b512016-09-06 20:57:50 +0000134 typedef struct dos_header { // DOS .EXE header
135 uint16_t e_magic; // Magic number
136 uint16_t e_cblp; // Bytes on last page of file
137 uint16_t e_cp; // Pages in file
138 uint16_t e_crlc; // Relocations
139 uint16_t e_cparhdr; // Size of header in paragraphs
140 uint16_t e_minalloc; // Minimum extra paragraphs needed
141 uint16_t e_maxalloc; // Maximum extra paragraphs needed
142 uint16_t e_ss; // Initial (relative) SS value
143 uint16_t e_sp; // Initial SP value
144 uint16_t e_csum; // Checksum
145 uint16_t e_ip; // Initial IP value
146 uint16_t e_cs; // Initial (relative) CS value
147 uint16_t e_lfarlc; // File address of relocation table
148 uint16_t e_ovno; // Overlay number
149 uint16_t e_res[4]; // Reserved words
150 uint16_t e_oemid; // OEM identifier (for e_oeminfo)
151 uint16_t e_oeminfo; // OEM information; e_oemid specific
152 uint16_t e_res2[10]; // Reserved words
153 uint32_t e_lfanew; // File address of new exe header
154 } dos_header_t;
Virgile Belloa4fe3a12013-08-25 13:27:20 +0000155
Kate Stoneb9c1b512016-09-06 20:57:50 +0000156 typedef struct coff_header {
157 uint16_t machine;
158 uint16_t nsects;
159 uint32_t modtime;
160 uint32_t symoff;
161 uint32_t nsyms;
162 uint16_t hdrsize;
163 uint16_t flags;
164 } coff_header_t;
165
166 typedef struct data_directory {
167 uint32_t vmaddr;
168 uint32_t vmsize;
169 } data_directory_t;
170
171 typedef struct coff_opt_header {
172 uint16_t magic;
173 uint8_t major_linker_version;
174 uint8_t minor_linker_version;
175 uint32_t code_size;
176 uint32_t data_size;
177 uint32_t bss_size;
178 uint32_t entry;
179 uint32_t code_offset;
180 uint32_t data_offset;
181
182 uint64_t image_base;
183 uint32_t sect_alignment;
184 uint32_t file_alignment;
185 uint16_t major_os_system_version;
186 uint16_t minor_os_system_version;
187 uint16_t major_image_version;
188 uint16_t minor_image_version;
189 uint16_t major_subsystem_version;
190 uint16_t minor_subsystem_version;
191 uint32_t reserved1;
192 uint32_t image_size;
193 uint32_t header_size;
194 uint32_t checksum;
195 uint16_t subsystem;
196 uint16_t dll_flags;
197 uint64_t stack_reserve_size;
198 uint64_t stack_commit_size;
199 uint64_t heap_reserve_size;
200 uint64_t heap_commit_size;
201 uint32_t loader_flags;
202 // uint32_t num_data_dir_entries;
203 std::vector<data_directory>
204 data_dirs; // will contain num_data_dir_entries entries
205 } coff_opt_header_t;
206
207 typedef enum coff_data_dir_type {
208 coff_data_dir_export_table = 0,
209 coff_data_dir_import_table = 1,
210 } coff_data_dir_type;
211
212 typedef struct section_header {
213 char name[8];
214 uint32_t vmsize; // Virtual Size
215 uint32_t vmaddr; // Virtual Addr
216 uint32_t size; // File size
217 uint32_t offset; // File offset
218 uint32_t reloff; // Offset to relocations
219 uint32_t lineoff; // Offset to line table entries
220 uint16_t nreloc; // Number of relocation entries
221 uint16_t nline; // Number of line table entries
222 uint32_t flags;
223 } section_header_t;
224
225 typedef struct coff_symbol {
226 char name[8];
227 uint32_t value;
228 uint16_t sect;
229 uint16_t type;
230 uint8_t storage;
231 uint8_t naux;
232 } coff_symbol_t;
233
234 typedef struct export_directory_entry {
235 uint32_t characteristics;
236 uint32_t time_date_stamp;
237 uint16_t major_version;
238 uint16_t minor_version;
239 uint32_t name;
240 uint32_t base;
241 uint32_t number_of_functions;
242 uint32_t number_of_names;
243 uint32_t address_of_functions;
244 uint32_t address_of_names;
245 uint32_t address_of_name_ordinals;
246 } export_directory_entry;
247
248 static bool ParseDOSHeader(lldb_private::DataExtractor &data,
249 dos_header_t &dos_header);
250 static bool ParseCOFFHeader(lldb_private::DataExtractor &data,
251 lldb::offset_t *offset_ptr,
252 coff_header_t &coff_header);
253 bool ParseCOFFOptionalHeader(lldb::offset_t *offset_ptr);
254 bool ParseSectionHeaders(uint32_t offset);
255
256 static void DumpDOSHeader(lldb_private::Stream *s,
257 const dos_header_t &header);
258 static void DumpCOFFHeader(lldb_private::Stream *s,
259 const coff_header_t &header);
260 static void DumpOptCOFFHeader(lldb_private::Stream *s,
261 const coff_opt_header_t &header);
262 void DumpSectionHeaders(lldb_private::Stream *s);
263 void DumpSectionHeader(lldb_private::Stream *s, const section_header_t &sh);
264 bool GetSectionName(std::string &sect_name, const section_header_t &sect);
265
266 typedef std::vector<section_header_t> SectionHeaderColl;
267 typedef SectionHeaderColl::iterator SectionHeaderCollIter;
268 typedef SectionHeaderColl::const_iterator SectionHeaderCollConstIter;
269
Greg Claytonf754f882011-09-09 20:33:05 +0000270private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000271 dos_header_t m_dos_header;
272 coff_header_t m_coff_header;
273 coff_opt_header_t m_coff_header_opt;
274 SectionHeaderColl m_sect_headers;
275 lldb::addr_t m_image_base;
276 lldb_private::Address m_entry_point_address;
Greg Claytonf754f882011-09-09 20:33:05 +0000277};
278
Eugene Zelenko8157a882015-10-23 16:56:07 +0000279#endif // liblldb_ObjectFilePECOFF_h_