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