1 //===-- 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 // C Includes
14 // C++ Includes
15 #include <vector>
16 
17 // Other libraries and framework includes
18 // Project includes
19 #include "lldb/Symbol/ObjectFile.h"
20 
21 class ObjectFilePECOFF :
22     public lldb_private::ObjectFile
23 {
24 public:
25     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
49     } MachineType;
50 
51     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 
60     //------------------------------------------------------------------
61     // Static Functions
62     //------------------------------------------------------------------
63     static void
64     Initialize();
65 
66     static void
67     Terminate();
68 
69     static lldb_private::ConstString
70     GetPluginNameStatic();
71 
72     static const char *
73     GetPluginDescriptionStatic();
74 
75     static ObjectFile *
76     CreateInstance (const lldb::ModuleSP &module_sp,
77                     lldb::DataBufferSP& data_sp,
78                     lldb::offset_t data_offset,
79                     const lldb_private::FileSpec* file,
80                     lldb::offset_t offset,
81                     lldb::offset_t length);
82 
83     static lldb_private::ObjectFile *
84     CreateMemoryInstance (const lldb::ModuleSP &module_sp,
85                           lldb::DataBufferSP& data_sp,
86                           const lldb::ProcessSP &process_sp,
87                           lldb::addr_t header_addr);
88 
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 
97     static bool
98     SaveCore (const lldb::ProcessSP &process_sp,
99               const lldb_private::FileSpec &outfile,
100               lldb_private::Error &error);
101 
102     static bool
103     MagicBytesMatch (lldb::DataBufferSP& data_sp);
104 
105     static lldb::SymbolType
106     MapSymbolType(uint16_t coff_symbol_type);
107 
108     bool
109     ParseHeader() override;
110 
111     bool
112     SetLoadAddress(lldb_private::Target &target, lldb::addr_t value, bool value_is_offset) override;
113 
114     lldb::ByteOrder
115     GetByteOrder() const override;
116 
117     bool
118     IsExecutable() const override;
119 
120     uint32_t
121     GetAddressByteSize() const override;
122 
123 //    virtual lldb_private::AddressClass
124 //    GetAddressClass (lldb::addr_t file_addr);
125 
126     lldb_private::Symtab *
127     GetSymtab() override;
128 
129     bool
130     IsStripped() override;
131 
132     void
133     CreateSections(lldb_private::SectionList &unified_section_list) override;
134 
135     void
136     Dump(lldb_private::Stream *s) override;
137 
138     bool
139     GetArchitecture(lldb_private::ArchSpec &arch) override;
140 
141     bool
142     GetUUID(lldb_private::UUID* uuid) override;
143 
144     uint32_t
145     GetDependentModules(lldb_private::FileSpecList& files) override;
146 
147     virtual lldb_private::Address
148     GetEntryPointAddress () override;
149 
150     ObjectFile::Type
151     CalculateType() override;
152 
153     ObjectFile::Strata
154     CalculateStrata() override;
155 
156     //------------------------------------------------------------------
157     // PluginInterface protocol
158     //------------------------------------------------------------------
159     lldb_private::ConstString
160     GetPluginName() override;
161 
162     uint32_t
163     GetPluginVersion() override;
164 
165 protected:
166 	bool NeedsEndianSwap() const;
167 
168 	typedef struct dos_header  {  // DOS .EXE header
169 		uint16_t e_magic;         // Magic number
170 		uint16_t e_cblp;          // Bytes on last page of file
171 		uint16_t e_cp;            // Pages in file
172 		uint16_t e_crlc;          // Relocations
173 		uint16_t e_cparhdr;       // Size of header in paragraphs
174 		uint16_t e_minalloc;      // Minimum extra paragraphs needed
175 		uint16_t e_maxalloc;      // Maximum extra paragraphs needed
176 		uint16_t e_ss;            // Initial (relative) SS value
177 		uint16_t e_sp;            // Initial SP value
178 		uint16_t e_csum;          // Checksum
179 		uint16_t e_ip;            // Initial IP value
180 		uint16_t e_cs;            // Initial (relative) CS value
181 		uint16_t e_lfarlc;        // File address of relocation table
182 		uint16_t e_ovno;          // Overlay number
183 		uint16_t e_res[4];        // Reserved words
184 		uint16_t e_oemid;         // OEM identifier (for e_oeminfo)
185 		uint16_t e_oeminfo;       // OEM information; e_oemid specific
186 		uint16_t e_res2[10];      // Reserved words
187 		uint32_t e_lfanew;        // File address of new exe header
188     } dos_header_t;
189 
190 	typedef struct coff_header {
191 		uint16_t machine;
192 		uint16_t nsects;
193 		uint32_t modtime;
194 		uint32_t symoff;
195 		uint32_t nsyms;
196 		uint16_t hdrsize;
197 		uint16_t flags;
198 	} coff_header_t;
199 
200 	typedef struct data_directory {
201 		uint32_t vmaddr;
202 		uint32_t vmsize;
203 	} data_directory_t;
204 
205 	typedef struct coff_opt_header
206 	{
207 		uint16_t	magic;
208 		uint8_t		major_linker_version;
209 		uint8_t		minor_linker_version;
210 		uint32_t	code_size;
211 		uint32_t	data_size;
212 		uint32_t	bss_size;
213 		uint32_t	entry;
214 		uint32_t	code_offset;
215 		uint32_t	data_offset;
216 
217 		uint64_t	image_base;
218 		uint32_t	sect_alignment;
219 		uint32_t	file_alignment;
220 		uint16_t	major_os_system_version;
221 		uint16_t	minor_os_system_version;
222 		uint16_t	major_image_version;
223 		uint16_t	minor_image_version;
224 		uint16_t	major_subsystem_version;
225 		uint16_t	minor_subsystem_version;
226 		uint32_t	reserved1;
227 		uint32_t	image_size;
228 		uint32_t	header_size;
229 		uint32_t	checksum;
230 		uint16_t	subsystem;
231 		uint16_t	dll_flags;
232 		uint64_t	stack_reserve_size;
233 		uint64_t	stack_commit_size;
234 		uint64_t	heap_reserve_size;
235 		uint64_t	heap_commit_size;
236 		uint32_t	loader_flags;
237         //    uint32_t	num_data_dir_entries;
238 		std::vector<data_directory> data_dirs;	// will contain num_data_dir_entries entries
239 	} coff_opt_header_t;
240 
241     typedef enum coff_data_dir_type
242     {
243         coff_data_dir_export_table = 0,
244         coff_data_dir_import_table = 1,
245     } coff_data_dir_type;
246 
247 	typedef struct section_header {
248 		char		name[8];
249 		uint32_t	vmsize;	// Virtual Size
250 		uint32_t	vmaddr;	// Virtual Addr
251 		uint32_t	size;	// File size
252 		uint32_t	offset;	// File offset
253 		uint32_t	reloff;	// Offset to relocations
254 		uint32_t	lineoff;// Offset to line table entries
255 		uint16_t	nreloc;	// Number of relocation entries
256 		uint16_t	nline;	// Number of line table entries
257 		uint32_t	flags;
258 	} section_header_t;
259 
260 	typedef struct coff_symbol {
261 		char		name[8];
262 		uint32_t	value;
263 		uint16_t	sect;
264 		uint16_t	type;
265 		uint8_t		storage;
266 		uint8_t		naux;
267 	} coff_symbol_t;
268 
269     typedef struct export_directory_entry {
270         uint32_t   characteristics;
271         uint32_t   time_date_stamp;
272         uint16_t   major_version;
273         uint16_t   minor_version;
274         uint32_t   name;
275         uint32_t   base;
276         uint32_t   number_of_functions;
277         uint32_t   number_of_names;
278         uint32_t   address_of_functions;
279         uint32_t   address_of_names;
280         uint32_t   address_of_name_ordinals;
281     } export_directory_entry;
282 
283 	static bool ParseDOSHeader (lldb_private::DataExtractor &data, dos_header_t &dos_header);
284 	static bool ParseCOFFHeader (lldb_private::DataExtractor &data, lldb::offset_t *offset_ptr, coff_header_t &coff_header);
285 	bool ParseCOFFOptionalHeader (lldb::offset_t *offset_ptr);
286 	bool ParseSectionHeaders (uint32_t offset);
287 
288 	static	void DumpDOSHeader(lldb_private::Stream *s, const dos_header_t& header);
289 	static	void DumpCOFFHeader(lldb_private::Stream *s, const coff_header_t& header);
290 	static	void DumpOptCOFFHeader(lldb_private::Stream *s, const coff_opt_header_t& header);
291     void DumpSectionHeaders(lldb_private::Stream *s);
292     void DumpSectionHeader(lldb_private::Stream *s, const section_header_t& sh);
293     bool GetSectionName(std::string& sect_name, const section_header_t& sect);
294 
295 	typedef std::vector<section_header_t>		SectionHeaderColl;
296 	typedef SectionHeaderColl::iterator			SectionHeaderCollIter;
297 	typedef SectionHeaderColl::const_iterator	SectionHeaderCollConstIter;
298 private:
299 	dos_header_t		m_dos_header;
300 	coff_header_t		m_coff_header;
301 	coff_opt_header_t	m_coff_header_opt;
302 	SectionHeaderColl	m_sect_headers;
303     lldb::addr_t		m_image_base;
304     lldb_private::Address	m_entry_point_address;
305 };
306 
307 #endif // liblldb_ObjectFilePECOFF_h_
308