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