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