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