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