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 #include <vector>
14 
15 #include "lldb/Symbol/ObjectFile.h"
16 
17 class ObjectFilePECOFF :
18     public lldb_private::ObjectFile
19 {
20 public:
21     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
45     } MachineType;
46 
47     //------------------------------------------------------------------
48     // Static Functions
49     //------------------------------------------------------------------
50     static void
51     Initialize();
52 
53     static void
54     Terminate();
55 
56     static lldb_private::ConstString
57     GetPluginNameStatic();
58 
59     static const char *
60     GetPluginDescriptionStatic();
61 
62     static ObjectFile *
63     CreateInstance (const lldb::ModuleSP &module_sp,
64                     lldb::DataBufferSP& data_sp,
65                     lldb::offset_t data_offset,
66                     const lldb_private::FileSpec* file,
67                     lldb::offset_t offset,
68                     lldb::offset_t length);
69 
70     static lldb_private::ObjectFile *
71     CreateMemoryInstance (const lldb::ModuleSP &module_sp,
72                           lldb::DataBufferSP& data_sp,
73                           const lldb::ProcessSP &process_sp,
74                           lldb::addr_t header_addr);
75 
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 
84     static bool
85     MagicBytesMatch (lldb::DataBufferSP& data_sp);
86 
87 
88     ObjectFilePECOFF (const lldb::ModuleSP &module_sp,
89                       lldb::DataBufferSP& data_sp,
90                       lldb::offset_t data_offset,
91                       const lldb_private::FileSpec* file,
92                       lldb::offset_t file_offset,
93                       lldb::offset_t length);
94 
95 	virtual
96     ~ObjectFilePECOFF();
97 
98     virtual bool
99     ParseHeader ();
100 
101     virtual bool
102     SetLoadAddress(lldb_private::Target &target, lldb::addr_t value, bool value_is_offset);
103 
104     virtual lldb::ByteOrder
105     GetByteOrder () const;
106 
107     virtual bool
108     IsExecutable () const;
109 
110     virtual uint32_t
111     GetAddressByteSize ()  const;
112 
113 //    virtual lldb_private::AddressClass
114 //    GetAddressClass (lldb::addr_t file_addr);
115 //
116     virtual lldb_private::Symtab *
117     GetSymtab ();
118 
119     virtual bool
120     IsStripped ();
121 
122     virtual void
123     CreateSections (lldb_private::SectionList &unified_section_list);
124 
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     //------------------------------------------------------------------
140     virtual lldb_private::ConstString
141     GetPluginName();
142 
143     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 
155 protected:
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;
219 		uint32_t	checksum;
220 		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;
230 
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;
236 
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;
258 
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;
271     } export_directory_entry;
272 
273 	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);
275 	bool ParseCOFFOptionalHeader (lldb::offset_t *offset_ptr);
276 	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;
288 private:
289 	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;
293     lldb::addr_t		m_image_base;
294 };
295 
296 #endif  // #ifndef liblldb_ObjectFilePECOFF_h_
297