blob: 87d8e6a5de28e534c8d50a07a787880246676e39 [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:
21
22 //------------------------------------------------------------------
23 // Static Functions
24 //------------------------------------------------------------------
25 static void
26 Initialize();
27
28 static void
29 Terminate();
30
Greg Clayton57abc5d2013-05-10 21:47:16 +000031 static lldb_private::ConstString
Greg Claytonf754f882011-09-09 20:33:05 +000032 GetPluginNameStatic();
33
34 static const char *
35 GetPluginDescriptionStatic();
36
37 static ObjectFile *
Greg Claytone72dfb32012-02-24 01:59:29 +000038 CreateInstance (const lldb::ModuleSP &module_sp,
Greg Clayton5ce9c562013-02-06 17:22:03 +000039 lldb::DataBufferSP& data_sp,
40 lldb::offset_t data_offset,
Greg Claytonf754f882011-09-09 20:33:05 +000041 const lldb_private::FileSpec* file,
Greg Clayton5ce9c562013-02-06 17:22:03 +000042 lldb::offset_t offset,
43 lldb::offset_t length);
Greg Claytonf754f882011-09-09 20:33:05 +000044
Greg Claytonc9660542012-02-05 02:38:54 +000045 static lldb_private::ObjectFile *
Greg Claytone72dfb32012-02-24 01:59:29 +000046 CreateMemoryInstance (const lldb::ModuleSP &module_sp,
Greg Claytonc9660542012-02-05 02:38:54 +000047 lldb::DataBufferSP& data_sp,
48 const lldb::ProcessSP &process_sp,
49 lldb::addr_t header_addr);
Greg Claytonf4d6de62013-04-24 22:29:28 +000050
51 static size_t
52 GetModuleSpecifications (const lldb_private::FileSpec& file,
53 lldb::DataBufferSP& data_sp,
54 lldb::offset_t data_offset,
55 lldb::offset_t file_offset,
56 lldb::offset_t length,
57 lldb_private::ModuleSpecList &specs);
58
Greg Claytonf754f882011-09-09 20:33:05 +000059 static bool
Greg Clayton5ce9c562013-02-06 17:22:03 +000060 MagicBytesMatch (lldb::DataBufferSP& data_sp);
Greg Claytonf754f882011-09-09 20:33:05 +000061
62
Greg Claytone72dfb32012-02-24 01:59:29 +000063 ObjectFilePECOFF (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 file_offset,
68 lldb::offset_t length);
Greg Claytonf754f882011-09-09 20:33:05 +000069
70 virtual
71 ~ObjectFilePECOFF();
72
73 virtual bool
74 ParseHeader ();
75
Virgile Bello2756adf2014-03-08 17:17:20 +000076 virtual bool
77 SetLoadAddress(lldb_private::Target &target, lldb::addr_t value, bool value_is_offset);
78
Greg Claytonf754f882011-09-09 20:33:05 +000079 virtual lldb::ByteOrder
80 GetByteOrder () const;
81
82 virtual bool
83 IsExecutable () const;
84
Greg Claytonc7bece562013-01-25 18:06:21 +000085 virtual uint32_t
Greg Claytonf754f882011-09-09 20:33:05 +000086 GetAddressByteSize () const;
87
88// virtual lldb_private::AddressClass
89// GetAddressClass (lldb::addr_t file_addr);
90//
91 virtual lldb_private::Symtab *
Greg Clayton3046e662013-07-10 01:23:25 +000092 GetSymtab ();
Greg Claytonf754f882011-09-09 20:33:05 +000093
Greg Clayton3046e662013-07-10 01:23:25 +000094 virtual bool
95 IsStripped ();
96
97 virtual void
98 CreateSections (lldb_private::SectionList &unified_section_list);
Greg Claytonf754f882011-09-09 20:33:05 +000099
100 virtual void
101 Dump (lldb_private::Stream *s);
102
103 virtual bool
104 GetArchitecture (lldb_private::ArchSpec &arch);
105
106 virtual bool
107 GetUUID (lldb_private::UUID* uuid);
108
109 virtual uint32_t
110 GetDependentModules (lldb_private::FileSpecList& files);
111
112 //------------------------------------------------------------------
113 // PluginInterface protocol
114 //------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +0000115 virtual lldb_private::ConstString
Greg Claytonf754f882011-09-09 20:33:05 +0000116 GetPluginName();
117
Greg Claytonf754f882011-09-09 20:33:05 +0000118 virtual uint32_t
119 GetPluginVersion();
120//
121// virtual lldb_private::Address
122// GetEntryPointAddress ();
123
124 virtual ObjectFile::Type
125 CalculateType();
126
127 virtual ObjectFile::Strata
128 CalculateStrata();
129
130protected:
131 bool NeedsEndianSwap() const;
132
133 typedef struct dos_header { // DOS .EXE header
134 uint16_t e_magic; // Magic number
135 uint16_t e_cblp; // Bytes on last page of file
136 uint16_t e_cp; // Pages in file
137 uint16_t e_crlc; // Relocations
138 uint16_t e_cparhdr; // Size of header in paragraphs
139 uint16_t e_minalloc; // Minimum extra paragraphs needed
140 uint16_t e_maxalloc; // Maximum extra paragraphs needed
141 uint16_t e_ss; // Initial (relative) SS value
142 uint16_t e_sp; // Initial SP value
143 uint16_t e_csum; // Checksum
144 uint16_t e_ip; // Initial IP value
145 uint16_t e_cs; // Initial (relative) CS value
146 uint16_t e_lfarlc; // File address of relocation table
147 uint16_t e_ovno; // Overlay number
148 uint16_t e_res[4]; // Reserved words
149 uint16_t e_oemid; // OEM identifier (for e_oeminfo)
150 uint16_t e_oeminfo; // OEM information; e_oemid specific
151 uint16_t e_res2[10]; // Reserved words
152 uint32_t e_lfanew; // File address of new exe header
153 } dos_header_t;
154
155 typedef struct coff_header {
156 uint16_t machine;
157 uint16_t nsects;
158 uint32_t modtime;
159 uint32_t symoff;
160 uint32_t nsyms;
161 uint16_t hdrsize;
162 uint16_t flags;
163 } coff_header_t;
164
165 typedef struct data_directory {
166 uint32_t vmaddr;
167 uint32_t vmsize;
168 } data_directory_t;
169
170 typedef struct coff_opt_header
171 {
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;
Greg Clayton28469ca2011-09-10 01:04:42 +0000194 uint32_t checksum;
Greg Claytonf754f882011-09-09 20:33:05 +0000195 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> data_dirs; // will contain num_data_dir_entries entries
204 } coff_opt_header_t;
Virgile Belloa4fe3a12013-08-25 13:27:20 +0000205
206 typedef enum coff_data_dir_type
207 {
208 coff_data_dir_export_table = 0,
209 coff_data_dir_import_table = 1,
210 } coff_data_dir_type;
Greg Claytonf754f882011-09-09 20:33:05 +0000211
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;
Virgile Belloa4fe3a12013-08-25 13:27:20 +0000233
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;
Michael Sartain0b5e5f42013-08-26 17:07:34 +0000246 } export_directory_entry;
Greg Claytonf754f882011-09-09 20:33:05 +0000247
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000248 static bool ParseDOSHeader (lldb_private::DataExtractor &data, dos_header_t &dos_header);
249 static bool ParseCOFFHeader (lldb_private::DataExtractor &data, lldb::offset_t *offset_ptr, coff_header_t &coff_header);
Greg Claytonc7bece562013-01-25 18:06:21 +0000250 bool ParseCOFFOptionalHeader (lldb::offset_t *offset_ptr);
Greg Claytonf754f882011-09-09 20:33:05 +0000251 bool ParseSectionHeaders (uint32_t offset);
252
253 static void DumpDOSHeader(lldb_private::Stream *s, const dos_header_t& header);
254 static void DumpCOFFHeader(lldb_private::Stream *s, const coff_header_t& header);
255 static void DumpOptCOFFHeader(lldb_private::Stream *s, const coff_opt_header_t& header);
256 void DumpSectionHeaders(lldb_private::Stream *s);
257 void DumpSectionHeader(lldb_private::Stream *s, const section_header_t& sh);
258 bool GetSectionName(std::string& sect_name, const section_header_t& sect);
259
260 typedef std::vector<section_header_t> SectionHeaderColl;
261 typedef SectionHeaderColl::iterator SectionHeaderCollIter;
262 typedef SectionHeaderColl::const_iterator SectionHeaderCollConstIter;
263private:
Greg Claytonf754f882011-09-09 20:33:05 +0000264 dos_header_t m_dos_header;
265 coff_header_t m_coff_header;
266 coff_opt_header_t m_coff_header_opt;
267 SectionHeaderColl m_sect_headers;
Virgile Bello2756adf2014-03-08 17:17:20 +0000268 lldb::addr_t m_image_base;
Greg Claytonf754f882011-09-09 20:33:05 +0000269};
270
271#endif // #ifndef liblldb_ObjectFilePECOFF_h_