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 
22     //------------------------------------------------------------------
23     // Static Functions
24     //------------------------------------------------------------------
25     static void
26     Initialize();
27 
28     static void
29     Terminate();
30 
31     static lldb_private::ConstString
32     GetPluginNameStatic();
33 
34     static const char *
35     GetPluginDescriptionStatic();
36 
37     static ObjectFile *
38     CreateInstance (const lldb::ModuleSP &module_sp,
39                     lldb::DataBufferSP& data_sp,
40                     lldb::offset_t data_offset,
41                     const lldb_private::FileSpec* file,
42                     lldb::offset_t offset,
43                     lldb::offset_t length);
44 
45     static lldb_private::ObjectFile *
46     CreateMemoryInstance (const lldb::ModuleSP &module_sp,
47                           lldb::DataBufferSP& data_sp,
48                           const lldb::ProcessSP &process_sp,
49                           lldb::addr_t header_addr);
50 
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 
59     static bool
60     MagicBytesMatch (lldb::DataBufferSP& data_sp);
61 
62 
63     ObjectFilePECOFF (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 file_offset,
68                       lldb::offset_t length);
69 
70 	virtual
71     ~ObjectFilePECOFF();
72 
73     virtual bool
74     ParseHeader ();
75 
76     virtual bool
77     SetLoadAddress(lldb_private::Target &target, lldb::addr_t value, bool value_is_offset);
78 
79     virtual lldb::ByteOrder
80     GetByteOrder () const;
81 
82     virtual bool
83     IsExecutable () const;
84 
85     virtual uint32_t
86     GetAddressByteSize ()  const;
87 
88 //    virtual lldb_private::AddressClass
89 //    GetAddressClass (lldb::addr_t file_addr);
90 //
91     virtual lldb_private::Symtab *
92     GetSymtab ();
93 
94     virtual bool
95     IsStripped ();
96 
97     virtual void
98     CreateSections (lldb_private::SectionList &unified_section_list);
99 
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     //------------------------------------------------------------------
115     virtual lldb_private::ConstString
116     GetPluginName();
117 
118     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 
130 protected:
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;
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> data_dirs;	// will contain num_data_dir_entries entries
204 	} coff_opt_header_t;
205 
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;
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, dos_header_t &dos_header);
249 	static bool ParseCOFFHeader (lldb_private::DataExtractor &data, lldb::offset_t *offset_ptr, coff_header_t &coff_header);
250 	bool ParseCOFFOptionalHeader (lldb::offset_t *offset_ptr);
251 	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;
263 private:
264 	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;
268     lldb::addr_t		m_image_base;
269 };
270 
271 #endif  // #ifndef liblldb_ObjectFilePECOFF_h_
272