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 lldb::ByteOrder
77     GetByteOrder () const;
78 
79     virtual bool
80     IsExecutable () const;
81 
82     virtual uint32_t
83     GetAddressByteSize ()  const;
84 
85 //    virtual lldb_private::AddressClass
86 //    GetAddressClass (lldb::addr_t file_addr);
87 //
88     virtual lldb_private::Symtab *
89     GetSymtab ();
90 
91     virtual bool
92     IsStripped ();
93 
94     virtual void
95     CreateSections (lldb_private::SectionList &unified_section_list);
96 
97     virtual void
98     Dump (lldb_private::Stream *s);
99 
100     virtual bool
101     GetArchitecture (lldb_private::ArchSpec &arch);
102 
103     virtual bool
104     GetUUID (lldb_private::UUID* uuid);
105 
106     virtual uint32_t
107     GetDependentModules (lldb_private::FileSpecList& files);
108 
109     //------------------------------------------------------------------
110     // PluginInterface protocol
111     //------------------------------------------------------------------
112     virtual lldb_private::ConstString
113     GetPluginName();
114 
115     virtual uint32_t
116     GetPluginVersion();
117 //
118 //    virtual lldb_private::Address
119 //    GetEntryPointAddress ();
120 
121     virtual ObjectFile::Type
122     CalculateType();
123 
124     virtual ObjectFile::Strata
125     CalculateStrata();
126 
127 protected:
128 	bool NeedsEndianSwap() const;
129 
130 	typedef struct dos_header  {  // DOS .EXE header
131 		uint16_t e_magic;         // Magic number
132 		uint16_t e_cblp;          // Bytes on last page of file
133 		uint16_t e_cp;            // Pages in file
134 		uint16_t e_crlc;          // Relocations
135 		uint16_t e_cparhdr;       // Size of header in paragraphs
136 		uint16_t e_minalloc;      // Minimum extra paragraphs needed
137 		uint16_t e_maxalloc;      // Maximum extra paragraphs needed
138 		uint16_t e_ss;            // Initial (relative) SS value
139 		uint16_t e_sp;            // Initial SP value
140 		uint16_t e_csum;          // Checksum
141 		uint16_t e_ip;            // Initial IP value
142 		uint16_t e_cs;            // Initial (relative) CS value
143 		uint16_t e_lfarlc;        // File address of relocation table
144 		uint16_t e_ovno;          // Overlay number
145 		uint16_t e_res[4];        // Reserved words
146 		uint16_t e_oemid;         // OEM identifier (for e_oeminfo)
147 		uint16_t e_oeminfo;       // OEM information; e_oemid specific
148 		uint16_t e_res2[10];      // Reserved words
149 		uint32_t e_lfanew;        // File address of new exe header
150     } dos_header_t;
151 
152 	typedef struct coff_header {
153 		uint16_t machine;
154 		uint16_t nsects;
155 		uint32_t modtime;
156 		uint32_t symoff;
157 		uint32_t nsyms;
158 		uint16_t hdrsize;
159 		uint16_t flags;
160 	} coff_header_t;
161 
162 	typedef struct data_directory {
163 		uint32_t vmaddr;
164 		uint32_t vmsize;
165 	} data_directory_t;
166 
167 	typedef struct coff_opt_header
168 	{
169 		uint16_t	magic;
170 		uint8_t		major_linker_version;
171 		uint8_t		minor_linker_version;
172 		uint32_t	code_size;
173 		uint32_t	data_size;
174 		uint32_t	bss_size;
175 		uint32_t	entry;
176 		uint32_t	code_offset;
177 		uint32_t	data_offset;
178 
179 		uint64_t	image_base;
180 		uint32_t	sect_alignment;
181 		uint32_t	file_alignment;
182 		uint16_t	major_os_system_version;
183 		uint16_t	minor_os_system_version;
184 		uint16_t	major_image_version;
185 		uint16_t	minor_image_version;
186 		uint16_t	major_subsystem_version;
187 		uint16_t	minor_subsystem_version;
188 		uint32_t	reserved1;
189 		uint32_t	image_size;
190 		uint32_t	header_size;
191 		uint32_t	checksum;
192 		uint16_t	subsystem;
193 		uint16_t	dll_flags;
194 		uint64_t	stack_reserve_size;
195 		uint64_t	stack_commit_size;
196 		uint64_t	heap_reserve_size;
197 		uint64_t	heap_commit_size;
198 		uint32_t	loader_flags;
199         //    uint32_t	num_data_dir_entries;
200 		std::vector<data_directory> data_dirs;	// will contain num_data_dir_entries entries
201 	} coff_opt_header_t;
202 
203 	typedef struct section_header {
204 		char		name[8];
205 		uint32_t	vmsize;	// Virtual Size
206 		uint32_t	vmaddr;	// Virtual Addr
207 		uint32_t	size;	// File size
208 		uint32_t	offset;	// File offset
209 		uint32_t	reloff;	// Offset to relocations
210 		uint32_t	lineoff;// Offset to line table entries
211 		uint16_t	nreloc;	// Number of relocation entries
212 		uint16_t	nline;	// Number of line table entries
213 		uint32_t	flags;
214 	} section_header_t;
215 
216 	typedef struct coff_symbol {
217 		char		name[8];
218 		uint32_t	value;
219 		uint16_t	sect;
220 		uint16_t	type;
221 		uint8_t		storage;
222 		uint8_t		naux;
223 	} coff_symbol_t;
224 
225 	bool ParseDOSHeader ();
226 	bool ParseCOFFHeader (lldb::offset_t *offset_ptr);
227 	bool ParseCOFFOptionalHeader (lldb::offset_t *offset_ptr);
228 	bool ParseSectionHeaders (uint32_t offset);
229 
230 	static	void DumpDOSHeader(lldb_private::Stream *s, const dos_header_t& header);
231 	static	void DumpCOFFHeader(lldb_private::Stream *s, const coff_header_t& header);
232 	static	void DumpOptCOFFHeader(lldb_private::Stream *s, const coff_opt_header_t& header);
233     void DumpSectionHeaders(lldb_private::Stream *s);
234     void DumpSectionHeader(lldb_private::Stream *s, const section_header_t& sh);
235     bool GetSectionName(std::string& sect_name, const section_header_t& sect);
236 
237 	typedef std::vector<section_header_t>		SectionHeaderColl;
238 	typedef SectionHeaderColl::iterator			SectionHeaderCollIter;
239 	typedef SectionHeaderColl::const_iterator	SectionHeaderCollConstIter;
240 private:
241 	dos_header_t		m_dos_header;
242 	coff_header_t		m_coff_header;
243 	coff_opt_header_t	m_coff_header_opt;
244 	SectionHeaderColl	m_sect_headers;
245 };
246 
247 #endif  // #ifndef liblldb_ObjectFilePECOFF_h_
248