author | l12wang |
Thu, 08 Oct 2009 17:43:28 -0500 | |
changeset 99 | 83ae0d82da3b |
parent 37 | c2bce6dd59e7 |
permissions | -rw-r--r-- |
37 | 1 |
/******************************************************************************* |
2 |
* Copyright (c) 2000, 2007 QNX Software Systems and others. |
|
3 |
* All rights reserved. This program and the accompanying materials |
|
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
|
5 |
* which accompanies this distribution, and is available at |
|
6 |
* http://www.eclipse.org/legal/epl-v10.html |
|
7 |
* |
|
8 |
* Contributors: |
|
9 |
* QNX Software Systems - Initial API and implementation |
|
10 |
* Markus Schorn (Wind River Systems) |
|
11 |
*******************************************************************************/ |
|
12 |
||
13 |
package org.eclipse.cdt.utils.coff; |
|
14 |
||
15 |
import java.io.IOException; |
|
16 |
import java.io.RandomAccessFile; |
|
17 |
||
18 |
import org.eclipse.cdt.core.CCorePlugin; |
|
19 |
import org.eclipse.cdt.core.IAddressFactory; |
|
20 |
import org.eclipse.cdt.core.ISymbolReader; |
|
21 |
import org.eclipse.cdt.utils.Addr32Factory; |
|
22 |
import org.eclipse.cdt.utils.coff.Coff.FileHeader; |
|
23 |
import org.eclipse.cdt.utils.coff.Coff.OptionalHeader; |
|
24 |
import org.eclipse.cdt.utils.coff.Coff.SectionHeader; |
|
25 |
import org.eclipse.cdt.utils.coff.Coff.Symbol; |
|
26 |
import org.eclipse.cdt.utils.coff.Exe.ExeHeader; |
|
27 |
import org.eclipse.cdt.utils.debug.dwarf.DwarfReader; |
|
28 |
import org.eclipse.cdt.utils.debug.stabs.StabsReader; |
|
29 |
||
30 |
/** |
|
31 |
* The PE file header consists of an MS-DOS stub, the PE signalture, the COFF file Header |
|
32 |
* and an Optional Header. |
|
33 |
* <pre> |
|
34 |
* +-------------------+ |
|
35 |
* | DOS-stub | |
|
36 |
* +-------------------+ |
|
37 |
* | file-header | |
|
38 |
* +-------------------+ |
|
39 |
* | optional header | |
|
40 |
* |- - - - - - - - - -| |
|
41 |
* | | |
|
42 |
* | data directories | |
|
43 |
* | | |
|
44 |
* +-------------------+ |
|
45 |
* | | |
|
46 |
* | section headers | |
|
47 |
* | | |
|
48 |
* +-------------------+ |
|
49 |
* | | |
|
50 |
* | section 1 | |
|
51 |
* | | |
|
52 |
* +-------------------+ |
|
53 |
* | | |
|
54 |
* | section 2 | |
|
55 |
* | | |
|
56 |
* +-------------------+ |
|
57 |
* | | |
|
58 |
* | ... | |
|
59 |
* | | |
|
60 |
* +-------------------+ |
|
61 |
* | | |
|
62 |
* | section n | |
|
63 |
* | | |
|
64 |
* +-------------------+ |
|
65 |
* </pre> |
|
66 |
*/ |
|
67 |
public class PE { |
|
68 |
||
69 |
||
70 |
public static final String NL = System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$ |
|
71 |
RandomAccessFile rfile; |
|
72 |
String filename; |
|
73 |
ExeHeader exeHeader; |
|
74 |
DOSHeader dosHeader; |
|
75 |
FileHeader fileHeader; |
|
76 |
OptionalHeader optionalHeader; |
|
77 |
NTOptionalHeader ntHeader; |
|
78 |
ImageDataDirectory[] dataDirectories; |
|
79 |
SectionHeader[] scnhdrs; |
|
80 |
Symbol[] symbolTable; |
|
81 |
byte[] stringTable; |
|
82 |
||
83 |
public static class Attribute { |
|
84 |
public static final int PE_TYPE_EXE = 1; |
|
85 |
public static final int PE_TYPE_SHLIB = 2; |
|
86 |
public static final int PE_TYPE_OBJ = 3; |
|
87 |
public static final int PE_TYPE_CORE = 4; |
|
88 |
||
89 |
String cpu; |
|
90 |
int type; |
|
91 |
int word; |
|
92 |
boolean bDebug; |
|
93 |
boolean isle; |
|
94 |
IAddressFactory addrFactory; |
|
95 |
||
96 |
public String getCPU() { |
|
97 |
return cpu; |
|
98 |
} |
|
99 |
||
100 |
public int getType() { |
|
101 |
return type; |
|
102 |
} |
|
103 |
||
104 |
public boolean hasDebug() { |
|
105 |
return bDebug; |
|
106 |
} |
|
107 |
||
108 |
public boolean isLittleEndian() { |
|
109 |
return isle; |
|
110 |
} |
|
111 |
||
112 |
public int getWord() { |
|
113 |
return word; |
|
114 |
} |
|
115 |
} |
|
116 |
||
117 |
/** |
|
118 |
*/ |
|
119 |
public static class DOSHeader { |
|
120 |
final static int DOSHDRSZ = 100; |
|
121 |
byte[] e_res = new byte[8]; /* Reserved words, all 0x0. */ |
|
122 |
byte[] e_oemid = new byte[2]; /* OEM identifier (for e_oeminfo), 0x0. */ |
|
123 |
byte[] e_oeminfo = new byte[2]; /* OEM information; e_oemid specific, 0x0. */ |
|
124 |
byte[] e_res2 = new byte[20]; /* Reserved words, all 0x0. */ |
|
125 |
int e_lfanew; /* 4 byte File address of new exe header, offset 60(0x3c), 0x80. */ |
|
126 |
byte[] dos_message = new byte[64]; /* Other stuff, always follow DOS header. */ |
|
127 |
||
128 |
public DOSHeader(RandomAccessFile file) throws IOException { |
|
129 |
this(file, file.getFilePointer()); |
|
130 |
} |
|
131 |
||
132 |
public DOSHeader(RandomAccessFile file, long offset) throws IOException { |
|
133 |
file.seek(offset); |
|
134 |
byte[] hdr = new byte[DOSHDRSZ]; |
|
135 |
file.readFully(hdr); |
|
136 |
ReadMemoryAccess memory = new ReadMemoryAccess(hdr, true); |
|
137 |
commonSetup(memory); |
|
138 |
} |
|
139 |
||
140 |
public DOSHeader(byte[] hdr, boolean little) throws IOException { |
|
141 |
ReadMemoryAccess memory = new ReadMemoryAccess(hdr, little); |
|
142 |
commonSetup(memory); |
|
143 |
} |
|
144 |
||
145 |
public DOSHeader(ReadMemoryAccess memory) throws IOException { |
|
146 |
commonSetup(memory); |
|
147 |
} |
|
148 |
||
149 |
public void commonSetup(ReadMemoryAccess memory) throws IOException { |
|
150 |
if (memory.getSize() < DOSHDRSZ) { |
|
151 |
throw new IOException("Not a Dos Header"); //$NON-NLS-1$ |
|
152 |
} |
|
153 |
memory.getBytes(e_res); |
|
154 |
memory.getBytes(e_oemid); |
|
155 |
memory.getBytes(e_oeminfo); |
|
156 |
memory.getBytes(e_res2); |
|
157 |
e_lfanew = memory.getInt(); |
|
158 |
memory.getBytes(dos_message); |
|
159 |
} |
|
160 |
||
161 |
@Override |
|
162 |
public String toString() { |
|
163 |
StringBuffer buffer = new StringBuffer(); |
|
164 |
buffer.append("DOS STUB VALUES").append(NL); //$NON-NLS-1$ |
|
165 |
buffer.append("e_lfanew = ").append(e_lfanew).append(NL); //$NON-NLS-1$ |
|
166 |
buffer.append(new String(dos_message)).append(NL); |
|
167 |
return buffer.toString(); |
|
168 |
} |
|
169 |
} |
|
170 |
||
171 |
public static class IMAGE_DEBUG_DIRECTORY { |
|
172 |
final int DEBUGDIRSZ = 28; |
|
173 |
public int Characteristics; |
|
174 |
public int TimeDateStamp; |
|
175 |
public short MajorVersion; |
|
176 |
public short MinorVersion; |
|
177 |
public int Type; |
|
178 |
public int SizeOfData; |
|
179 |
public int AddressOfRawData; |
|
180 |
public int PointerToRawData; |
|
181 |
||
182 |
public IMAGE_DEBUG_DIRECTORY(RandomAccessFile file, long offset) throws IOException { |
|
183 |
file.seek(offset); |
|
184 |
byte[] dir = new byte[DEBUGDIRSZ]; |
|
185 |
file.readFully(dir); |
|
186 |
ReadMemoryAccess memory = new ReadMemoryAccess(dir, true); |
|
187 |
Characteristics = memory.getInt(); |
|
188 |
TimeDateStamp = memory.getInt(); |
|
189 |
MajorVersion = memory.getShort(); |
|
190 |
MinorVersion = memory.getShort(); |
|
191 |
Type = memory.getInt(); |
|
192 |
SizeOfData = memory.getInt(); |
|
193 |
AddressOfRawData = memory.getInt(); |
|
194 |
PointerToRawData = memory.getInt(); |
|
195 |
} |
|
196 |
} |
|
197 |
||
198 |
public static class IMAGE_DATA_DIRECTORY { |
|
199 |
||
200 |
public int VirtualAddress; |
|
201 |
public int Size; |
|
202 |
} |
|
203 |
||
204 |
public static class NTOptionalHeader { |
|
205 |
||
206 |
public final static int NTHDRSZ = 196; |
|
207 |
public int ImageBase; // 4 bytes. |
|
208 |
public int SectionAlignment; // 4 bytes. |
|
209 |
public int FileAlignment; // 4 bytes. |
|
210 |
public short MajorOperatingSystemVersion; // 2 bytes. |
|
211 |
public short MinorOperatingSystemVersion; // 2 bytes. |
|
212 |
public short MajorImageVersion; // 2 bytes. |
|
213 |
public short MinorImageVersion; // 2 bytes. |
|
214 |
public short MajorSubsystemVersion; // 2 bytes. |
|
215 |
public short MinorSubsystemVersion; // 2 bytes. |
|
216 |
public byte[] Reserved = new byte[4]; // 4 bytes. |
|
217 |
public int SizeOfImage; // 4 bytes. |
|
218 |
public int SizeOfHeaders; // 4 bytes. |
|
219 |
public int CheckSum; // 4 bytes. |
|
220 |
public short Subsystem; // 2 bytes. |
|
221 |
public short DLLCharacteristics; // 2 bytes. |
|
222 |
public int SizeOfStackReserve; // 4 bytes. |
|
223 |
public int SizeOfStackCommit; // 4 bytes. |
|
224 |
public int SizeOfHeapReserve; // 4 bytes. |
|
225 |
public int SizeOfHeapCommit; // 4 bytes. |
|
226 |
public int LoaderFlags; // 4 bytes. |
|
227 |
public int NumberOfRvaAndSizes; // 4 bytes. |
|
228 |
public IMAGE_DATA_DIRECTORY DataDirectory[]; |
|
229 |
||
230 |
public NTOptionalHeader(RandomAccessFile file) throws IOException { |
|
231 |
this(file, file.getFilePointer()); |
|
232 |
} |
|
233 |
||
234 |
public NTOptionalHeader(RandomAccessFile file, long offset) throws IOException { |
|
235 |
file.seek(offset); |
|
236 |
byte[] hdr = new byte[NTHDRSZ]; |
|
237 |
file.readFully(hdr); |
|
238 |
ReadMemoryAccess memory = new ReadMemoryAccess(hdr, true); |
|
239 |
ImageBase = memory.getInt(); |
|
240 |
SectionAlignment = memory.getInt(); |
|
241 |
FileAlignment = memory.getInt(); |
|
242 |
MajorOperatingSystemVersion = memory.getShort(); |
|
243 |
MinorOperatingSystemVersion = memory.getShort(); |
|
244 |
MajorImageVersion = memory.getShort(); |
|
245 |
MinorImageVersion = memory.getShort(); |
|
246 |
MajorSubsystemVersion = memory.getShort(); |
|
247 |
MinorSubsystemVersion = memory.getShort(); |
|
248 |
memory.getBytes(Reserved); |
|
249 |
SizeOfImage = memory.getInt(); |
|
250 |
SizeOfHeaders = memory.getInt(); |
|
251 |
CheckSum = memory.getInt(); |
|
252 |
Subsystem = memory.getShort(); |
|
253 |
DLLCharacteristics = memory.getShort(); |
|
254 |
SizeOfStackReserve = memory.getInt(); |
|
255 |
SizeOfStackCommit = memory.getInt(); |
|
256 |
SizeOfHeapReserve = memory.getInt(); |
|
257 |
SizeOfHeapCommit = memory.getInt(); |
|
258 |
LoaderFlags = memory.getInt(); |
|
259 |
NumberOfRvaAndSizes = memory.getInt(); |
|
260 |
||
261 |
DataDirectory = new IMAGE_DATA_DIRECTORY[NumberOfRvaAndSizes]; // 8*16=128 bytes |
|
262 |
for (int i = 0; i < NumberOfRvaAndSizes; i++) { |
|
263 |
DataDirectory[i] = new IMAGE_DATA_DIRECTORY(); |
|
264 |
DataDirectory[i].VirtualAddress = memory.getInt(); |
|
265 |
DataDirectory[i].Size = memory.getInt(); |
|
266 |
} |
|
267 |
} |
|
268 |
||
269 |
@Override |
|
270 |
public String toString() { |
|
271 |
StringBuffer buffer = new StringBuffer(); |
|
272 |
buffer.append("NT OPTIONAL HEADER VALUES").append(NL); //$NON-NLS-1$ |
|
273 |
buffer.append("ImageBase = ").append(ImageBase).append(NL); //$NON-NLS-1$ |
|
274 |
buffer.append("SexctionAlignement = ").append(SectionAlignment).append(NL); //$NON-NLS-1$ |
|
275 |
buffer.append("FileAlignment = ").append(FileAlignment).append(NL); //$NON-NLS-1$ |
|
276 |
buffer.append("MajorOSVersion = ").append(MajorOperatingSystemVersion).append(NL); //$NON-NLS-1$ |
|
277 |
buffer.append("MinorOSVersion = ").append(MinorOperatingSystemVersion).append(NL); //$NON-NLS-1$ |
|
278 |
buffer.append("MajorImageVersion = ").append(MajorImageVersion).append(NL); //$NON-NLS-1$ |
|
279 |
buffer.append("MinorImageVersion = ").append(MinorImageVersion).append(NL); //$NON-NLS-1$ |
|
280 |
buffer.append("MajorSubVersion = ").append(MajorSubsystemVersion).append(NL); //$NON-NLS-1$ |
|
281 |
buffer.append("MinorSubVersion = ").append(MinorSubsystemVersion).append(NL); //$NON-NLS-1$ |
|
282 |
buffer.append("Reserved = ").append(Reserved).append(NL); //$NON-NLS-1$ |
|
283 |
buffer.append("SizeOfImage = ").append(SizeOfImage).append(NL); //$NON-NLS-1$ |
|
284 |
buffer.append("SizeOfHeaders = ").append(SizeOfHeaders).append(NL); //$NON-NLS-1$ |
|
285 |
buffer.append("CheckSum = ").append(CheckSum).append(NL); //$NON-NLS-1$ |
|
286 |
buffer.append("Subsystem = ").append(Subsystem).append(NL); //$NON-NLS-1$ |
|
287 |
buffer.append("DLL = ").append(DLLCharacteristics).append(NL); //$NON-NLS-1$ |
|
288 |
buffer.append("StackReserve = ").append(SizeOfStackReserve).append(NL); //$NON-NLS-1$ |
|
289 |
buffer.append("StackCommit = ").append(SizeOfStackCommit).append(NL); //$NON-NLS-1$ |
|
290 |
buffer.append("HeapReserve = ").append(SizeOfHeapReserve).append(NL); //$NON-NLS-1$ |
|
291 |
buffer.append("HeapCommit = ").append(SizeOfHeapCommit).append(NL); //$NON-NLS-1$ |
|
292 |
buffer.append("LoaderFlags = ").append(LoaderFlags).append(NL); //$NON-NLS-1$ |
|
293 |
buffer.append("#Rva size = ").append(NumberOfRvaAndSizes).append(NL); //$NON-NLS-1$ |
|
294 |
return buffer.toString(); |
|
295 |
} |
|
296 |
} |
|
297 |
||
298 |
public class ImageDataDirectory { |
|
299 |
public int rva; |
|
300 |
public int size; |
|
301 |
||
302 |
public ImageDataDirectory(int r, int s) { |
|
303 |
rva = r; |
|
304 |
size = s; |
|
305 |
} |
|
306 |
||
307 |
@Override |
|
308 |
public String toString() { |
|
309 |
StringBuffer buffer = new StringBuffer(); |
|
310 |
buffer.append("rva = ").append(rva).append(" "); //$NON-NLS-1$ //$NON-NLS-2$ |
|
311 |
buffer.append("size = ").append(size).append(NL); //$NON-NLS-1$ |
|
312 |
return buffer.toString(); |
|
313 |
} |
|
314 |
} |
|
315 |
||
316 |
public class ImportDirectoryEntry { |
|
317 |
public final static int ENTRYSZ = 20; |
|
318 |
public int rva; |
|
319 |
public int timestamp; |
|
320 |
public int forwarder; |
|
321 |
public int name; |
|
322 |
public int thunk; |
|
323 |
||
324 |
public ImportDirectoryEntry(RandomAccessFile file) throws IOException { |
|
325 |
this(file, file.getFilePointer()); |
|
326 |
} |
|
327 |
||
328 |
public ImportDirectoryEntry(RandomAccessFile file, long offset) throws IOException { |
|
329 |
file.seek(offset); |
|
330 |
byte[] bytes = new byte[ENTRYSZ]; |
|
331 |
file.readFully(bytes); |
|
332 |
ReadMemoryAccess memory = new ReadMemoryAccess(bytes, true); |
|
333 |
rva = memory.getInt(); |
|
334 |
timestamp = memory.getInt(); |
|
335 |
forwarder = memory.getInt(); |
|
336 |
name = memory.getInt(); |
|
337 |
thunk = memory.getInt(); |
|
338 |
} |
|
339 |
||
340 |
@Override |
|
341 |
public String toString() { |
|
342 |
StringBuffer buffer = new StringBuffer(); |
|
343 |
buffer.append("rva = ").append(rva); //$NON-NLS-1$ |
|
344 |
buffer.append(" timestamp = ").append(timestamp); //$NON-NLS-1$ |
|
345 |
buffer.append(" forwarder = ").append(forwarder); //$NON-NLS-1$ |
|
346 |
buffer.append(" name = ").append(name); //$NON-NLS-1$ |
|
347 |
buffer.append(" thunk = ").append(thunk).append(NL); //$NON-NLS-1$ |
|
348 |
return buffer.toString(); |
|
349 |
} |
|
350 |
} |
|
351 |
||
352 |
public PE (String filename) throws IOException { |
|
353 |
this(filename, 0); |
|
354 |
} |
|
355 |
||
356 |
public PE(String filename, long pos) throws IOException { |
|
357 |
this(filename, pos, true); |
|
358 |
} |
|
359 |
||
360 |
public PE (String filename, long pos, boolean filter) throws IOException { |
|
361 |
try { |
|
362 |
rfile = new RandomAccessFile(filename, "r"); //$NON-NLS-1$ |
|
363 |
this.filename = filename; |
|
364 |
rfile.seek(pos); |
|
365 |
||
366 |
// Object files do not have exe/dos header. |
|
367 |
try { |
|
368 |
exeHeader = new ExeHeader(rfile); |
|
369 |
dosHeader = new DOSHeader(rfile); |
|
370 |
// Jump the Coff header, and Check the sig. |
|
371 |
rfile.seek(dosHeader.e_lfanew); |
|
372 |
byte[] sig = new byte[4]; |
|
373 |
rfile.readFully(sig); |
|
374 |
if (!((sig[0] == 'P') && (sig[1] == 'E') |
|
375 |
&& (sig[2] == '\0') && (sig[3] == '\0'))) { |
|
376 |
throw new IOException(CCorePlugin.getResourceString("Util.exception.notPE")); //$NON-NLS-1$ |
|
377 |
} |
|
378 |
} catch (IOException e) { |
|
379 |
rfile.seek(pos); |
|
380 |
} |
|
381 |
||
382 |
fileHeader = new Coff.FileHeader(rfile, rfile.getFilePointer()); |
|
383 |
||
384 |
// Check if this a valid machine. |
|
385 |
if (!isValidMachine(fileHeader.f_magic)) { |
|
386 |
throw new IOException(CCorePlugin.getResourceString("Util.exception.unknownFormat")); //$NON-NLS-1$ |
|
387 |
} |
|
388 |
||
389 |
if (fileHeader.f_opthdr > 0) { |
|
390 |
optionalHeader = new Coff.OptionalHeader(rfile, rfile.getFilePointer()); |
|
391 |
ntHeader = new NTOptionalHeader(rfile, rfile.getFilePointer()); |
|
392 |
} |
|
393 |
} finally { |
|
394 |
if (rfile != null) { |
|
395 |
rfile.close(); |
|
396 |
rfile = null; |
|
397 |
} |
|
398 |
} |
|
399 |
} |
|
400 |
||
401 |
public static boolean isValidMachine(int magic) { |
|
402 |
// Check if this a valid machine. |
|
403 |
switch (magic) { |
|
404 |
case PEConstants.IMAGE_FILE_MACHINE_ALPHA: |
|
405 |
case PEConstants.IMAGE_FILE_MACHINE_ARM: |
|
406 |
case PEConstants.IMAGE_FILE_MACHINE_ARM2: |
|
407 |
case PEConstants.IMAGE_FILE_MACHINE_ALPHA64: |
|
408 |
case PEConstants.IMAGE_FILE_MACHINE_I386: |
|
409 |
case PEConstants.IMAGE_FILE_MACHINE_IA64: |
|
410 |
case PEConstants.IMAGE_FILE_MACHINE_M68K: |
|
411 |
case PEConstants.IMAGE_FILE_MACHINE_MIPS16: |
|
412 |
case PEConstants.IMAGE_FILE_MACHINE_MIPSFPU: |
|
413 |
case PEConstants.IMAGE_FILE_MACHINE_MIPSFPU16: |
|
414 |
case PEConstants.IMAGE_FILE_MACHINE_POWERPC: |
|
415 |
case PEConstants.IMAGE_FILE_MACHINE_R3000: |
|
416 |
case PEConstants.IMAGE_FILE_MACHINE_R4000: |
|
417 |
case PEConstants.IMAGE_FILE_MACHINE_R10000: |
|
418 |
case PEConstants.IMAGE_FILE_MACHINE_SH3: |
|
419 |
case PEConstants.IMAGE_FILE_MACHINE_SH4: |
|
420 |
case PEConstants.IMAGE_FILE_MACHINE_THUMB: |
|
421 |
// Ok; |
|
422 |
return true; |
|
423 |
//throw new IOException("Unknow machine/format"); |
|
424 |
} |
|
425 |
return false; |
|
426 |
} |
|
427 |
||
428 |
public static Attribute getAttributes(FileHeader filhdr) { |
|
429 |
Attribute attrib = new Attribute(); |
|
430 |
// Machine type. |
|
431 |
switch (filhdr.f_magic) { |
|
432 |
case PEConstants.IMAGE_FILE_MACHINE_UNKNOWN: |
|
433 |
attrib.cpu = "none"; //$NON-NLS-1$ |
|
434 |
break; |
|
435 |
case PEConstants.IMAGE_FILE_MACHINE_ALPHA: |
|
436 |
attrib.cpu = "alpha"; //$NON-NLS-1$ |
|
437 |
break; |
|
438 |
case PEConstants.IMAGE_FILE_MACHINE_ARM: |
|
439 |
case PEConstants.IMAGE_FILE_MACHINE_ARM2: |
|
440 |
attrib.cpu = "arm"; //$NON-NLS-1$ |
|
441 |
break; |
|
442 |
case PEConstants.IMAGE_FILE_MACHINE_ALPHA64: |
|
443 |
attrib.cpu = "arm64"; //$NON-NLS-1$ |
|
444 |
break; |
|
445 |
case PEConstants.IMAGE_FILE_MACHINE_I386: |
|
446 |
attrib.cpu = "x86"; //$NON-NLS-1$ |
|
447 |
break; |
|
448 |
case PEConstants.IMAGE_FILE_MACHINE_IA64: |
|
449 |
attrib.cpu = "ia64"; //$NON-NLS-1$ |
|
450 |
break; |
|
451 |
case PEConstants.IMAGE_FILE_MACHINE_M68K: |
|
452 |
attrib.cpu = "m68k"; //$NON-NLS-1$ |
|
453 |
break; |
|
454 |
case PEConstants.IMAGE_FILE_MACHINE_MIPS16: |
|
455 |
attrib.cpu = "mips16"; //$NON-NLS-1$ |
|
456 |
break; |
|
457 |
case PEConstants.IMAGE_FILE_MACHINE_MIPSFPU: |
|
458 |
attrib.cpu = "mipsfpu"; //$NON-NLS-1$ |
|
459 |
break; |
|
460 |
case PEConstants.IMAGE_FILE_MACHINE_MIPSFPU16: |
|
461 |
attrib.cpu = "mipsfpu16"; //$NON-NLS-1$ |
|
462 |
break; |
|
463 |
case PEConstants.IMAGE_FILE_MACHINE_POWERPC: |
|
464 |
attrib.cpu = "powerpc"; //$NON-NLS-1$ |
|
465 |
break; |
|
466 |
case PEConstants.IMAGE_FILE_MACHINE_R3000: |
|
467 |
attrib.cpu = "r3000"; //$NON-NLS-1$ |
|
468 |
break; |
|
469 |
case PEConstants.IMAGE_FILE_MACHINE_R4000: |
|
470 |
attrib.cpu = "r4000"; //$NON-NLS-1$ |
|
471 |
break; |
|
472 |
case PEConstants.IMAGE_FILE_MACHINE_R10000: |
|
473 |
attrib.cpu = "r10000"; //$NON-NLS-1$ |
|
474 |
break; |
|
475 |
case PEConstants.IMAGE_FILE_MACHINE_SH3: |
|
476 |
attrib.cpu = "sh3"; //$NON-NLS-1$ |
|
477 |
break; |
|
478 |
case PEConstants.IMAGE_FILE_MACHINE_SH4: |
|
479 |
attrib.cpu = "sh4"; //$NON-NLS-1$ |
|
480 |
break; |
|
481 |
case PEConstants.IMAGE_FILE_MACHINE_THUMB: |
|
482 |
attrib.cpu = "thumb"; //$NON-NLS-1$ |
|
483 |
break; |
|
484 |
} |
|
485 |
||
486 |
/* PE characteristics, FileHeader.f_flags. */ |
|
487 |
if ((filhdr.f_flags & PEConstants.IMAGE_FILE_DLL) != 0) { |
|
488 |
attrib.type = Attribute.PE_TYPE_SHLIB; |
|
489 |
} else if ((filhdr.f_flags & PEConstants.IMAGE_FILE_EXECUTABLE_IMAGE) != 0) { |
|
490 |
attrib.type = Attribute.PE_TYPE_EXE; |
|
491 |
} else { |
|
492 |
attrib.type = Attribute.PE_TYPE_OBJ; |
|
493 |
} |
|
494 |
||
495 |
// For PE always assume little endian unless otherwise. |
|
496 |
attrib.isle = true; |
|
497 |
// Little Endian. |
|
498 |
if ((filhdr.f_flags & PEConstants.IMAGE_FILE_BYTES_REVERSED_LO) != 0) { |
|
499 |
attrib.isle = true; |
|
500 |
} |
|
501 |
// Big Endian. |
|
502 |
if ((filhdr.f_flags & PEConstants.IMAGE_FILE_BYTES_REVERSED_HI) != 0) { |
|
503 |
attrib.isle = false; |
|
504 |
} |
|
505 |
||
506 |
// No debug information. |
|
507 |
if ((filhdr.f_flags & PEConstants.IMAGE_FILE_DEBUG_STRIPPED) != 0) { |
|
508 |
attrib.bDebug = false; |
|
509 |
} else { |
|
510 |
attrib.bDebug = true; |
|
511 |
} |
|
512 |
||
513 |
// sizeof word. |
|
514 |
if ((filhdr.f_flags & PEConstants.IMAGE_FILE_16BIT_MACHINE) != 0) { |
|
515 |
attrib.word = 16; |
|
516 |
} |
|
517 |
if ((filhdr.f_flags & PEConstants.IMAGE_FILE_32BIT_MACHINE) != 0) { |
|
518 |
attrib.word = 32; |
|
519 |
} |
|
520 |
||
521 |
attrib.addrFactory = new Addr32Factory(); |
|
522 |
return attrib; |
|
523 |
} |
|
524 |
||
525 |
public static boolean isExeHeader(byte[] e_signature) { |
|
526 |
if (e_signature == null || e_signature.length < 2 || e_signature[0] != 'M' || e_signature[1] != 'Z') |
|
527 |
return false; |
|
528 |
return true; |
|
529 |
} |
|
530 |
||
531 |
public Attribute getAttribute() throws IOException { |
|
532 |
return getAttributes(getFileHeader()); |
|
533 |
} |
|
534 |
||
535 |
public static Attribute getAttribute(byte[] data) throws IOException { |
|
536 |
ReadMemoryAccess memory = new ReadMemoryAccess(data, true); |
|
537 |
int idx = 0; |
|
538 |
try { |
|
539 |
//Exe.ExeHeader exeHdr = new Exe.ExeHeader(memory); |
|
540 |
new Exe.ExeHeader(memory); |
|
541 |
DOSHeader dosHdr = new DOSHeader(memory); |
|
542 |
// Jump the Coff header, and Check the sig. |
|
543 |
idx = dosHdr.e_lfanew; |
|
544 |
if (idx + 4 < data.length) { |
|
545 |
if (!((data[idx + 0] == 'P') && (data[idx + 1] == 'E') |
|
546 |
&& (data[idx + 2] == '\0') && (data[idx + 3] == '\0'))) { |
|
547 |
throw new IOException(CCorePlugin.getResourceString("Util.exception.notPE")); //$NON-NLS-1$ |
|
548 |
} |
|
549 |
idx += 4; |
|
550 |
} |
|
551 |
} catch (IOException e) { |
|
552 |
} |
|
553 |
if (idx < data.length) { |
|
554 |
byte[] bytes = new byte[data.length - idx]; |
|
555 |
System.arraycopy(data, idx, bytes, 0, data.length - idx); |
|
556 |
Coff.FileHeader filehdr = new Coff.FileHeader(bytes, true); |
|
557 |
if (isValidMachine(filehdr.f_magic)) { |
|
558 |
return getAttributes(filehdr); |
|
559 |
} |
|
560 |
} |
|
561 |
throw new IOException(CCorePlugin.getResourceString("Util.exception.notPE")); //$NON-NLS-1$ |
|
562 |
} |
|
563 |
||
564 |
public static Attribute getAttribute(String file) throws IOException { |
|
565 |
PE pe = new PE(file); |
|
566 |
Attribute attrib = pe.getAttribute(); |
|
567 |
pe.dispose(); |
|
568 |
return attrib; |
|
569 |
} |
|
570 |
||
571 |
||
572 |
public void dispose() throws IOException { |
|
573 |
if (rfile != null) { |
|
574 |
rfile.close(); |
|
575 |
rfile = null; |
|
576 |
} |
|
577 |
} |
|
578 |
||
579 |
@Override |
|
580 |
protected void finalize() throws Throwable { |
|
581 |
try { |
|
582 |
dispose(); |
|
583 |
} finally { |
|
584 |
super.finalize(); |
|
585 |
} |
|
586 |
} |
|
587 |
||
588 |
public ExeHeader getExeHeader() { |
|
589 |
return exeHeader; |
|
590 |
} |
|
591 |
||
592 |
public DOSHeader getDOSHeader() { |
|
593 |
return dosHeader; |
|
594 |
} |
|
595 |
||
596 |
public FileHeader getFileHeader() { |
|
597 |
return fileHeader; |
|
598 |
} |
|
599 |
||
600 |
public OptionalHeader getOptionalHeader() { |
|
601 |
return optionalHeader; |
|
602 |
} |
|
603 |
||
604 |
public NTOptionalHeader getNTOptionalHeader() { |
|
605 |
return ntHeader; |
|
606 |
} |
|
607 |
||
608 |
public ImageDataDirectory[] getImageDataDirectories() throws IOException { |
|
609 |
if (dataDirectories == null) { |
|
610 |
RandomAccessFile accessFile = getRandomAccessFile(); |
|
611 |
long offset = 0; |
|
612 |
if (dosHeader != null) { |
|
613 |
offset = dosHeader.e_lfanew + 4/*NT SIG*/; |
|
614 |
} |
|
615 |
offset += FileHeader.FILHSZ + OptionalHeader.AOUTHDRSZ + NTOptionalHeader.NTHDRSZ; |
|
616 |
accessFile.seek(offset); |
|
617 |
dataDirectories = new ImageDataDirectory[PEConstants.IMAGE_NUMBEROF_DIRECTORY_ENTRIES]; |
|
618 |
byte[] data = new byte[dataDirectories.length * (4 + 4)]; |
|
619 |
accessFile.readFully(data); |
|
620 |
ReadMemoryAccess memory = new ReadMemoryAccess(data, true); |
|
621 |
for (int i = 0; i < dataDirectories.length; i++) { |
|
622 |
int rva = memory.getInt(); |
|
623 |
int size = memory.getInt(); |
|
624 |
dataDirectories[i] = new ImageDataDirectory(rva, size); |
|
625 |
} |
|
626 |
} |
|
627 |
return dataDirectories; |
|
628 |
} |
|
629 |
public SectionHeader[] getSectionHeaders() throws IOException { |
|
630 |
if (scnhdrs == null) { |
|
631 |
RandomAccessFile accessFile = getRandomAccessFile(); |
|
632 |
scnhdrs = new SectionHeader[fileHeader.f_nscns]; |
|
633 |
long offset = 0; |
|
634 |
if (dosHeader != null) { |
|
635 |
offset = dosHeader.e_lfanew + 4 /* NT SIG */; |
|
636 |
} |
|
637 |
offset += FileHeader.FILHSZ + fileHeader.f_opthdr; |
|
638 |
for (int i = 0; i < scnhdrs.length; i++, offset += SectionHeader.SCNHSZ) { |
|
639 |
scnhdrs[i] = new SectionHeader(accessFile, offset); |
|
640 |
} |
|
641 |
} |
|
642 |
return scnhdrs; |
|
643 |
} |
|
644 |
||
645 |
public Symbol[] getSymbols() throws IOException { |
|
646 |
if (symbolTable == null) { |
|
99
83ae0d82da3b
Fixed the bug that symbol address is wrongly fixed up. With this we can correctly set breakpoint at a symbol, e.g. for any symbol specified in stop-at-main preference UI.
l12wang
parents:
37
diff
changeset
|
647 |
SectionHeader[] secHeaders = getSectionHeaders(); |
83ae0d82da3b
Fixed the bug that symbol address is wrongly fixed up. With this we can correctly set breakpoint at a symbol, e.g. for any symbol specified in stop-at-main preference UI.
l12wang
parents:
37
diff
changeset
|
648 |
NTOptionalHeader ntHeader = getNTOptionalHeader(); |
83ae0d82da3b
Fixed the bug that symbol address is wrongly fixed up. With this we can correctly set breakpoint at a symbol, e.g. for any symbol specified in stop-at-main preference UI.
l12wang
parents:
37
diff
changeset
|
649 |
|
37 | 650 |
RandomAccessFile accessFile = getRandomAccessFile(); |
651 |
long offset = fileHeader.f_symptr; |
|
652 |
symbolTable = new Symbol[fileHeader.f_nsyms]; |
|
653 |
for (int i = 0; i < symbolTable.length; i++, offset += Symbol.SYMSZ) { |
|
99
83ae0d82da3b
Fixed the bug that symbol address is wrongly fixed up. With this we can correctly set breakpoint at a symbol, e.g. for any symbol specified in stop-at-main preference UI.
l12wang
parents:
37
diff
changeset
|
654 |
Symbol newSym = new Symbol(accessFile, offset); |
83ae0d82da3b
Fixed the bug that symbol address is wrongly fixed up. With this we can correctly set breakpoint at a symbol, e.g. for any symbol specified in stop-at-main preference UI.
l12wang
parents:
37
diff
changeset
|
655 |
|
83ae0d82da3b
Fixed the bug that symbol address is wrongly fixed up. With this we can correctly set breakpoint at a symbol, e.g. for any symbol specified in stop-at-main preference UI.
l12wang
parents:
37
diff
changeset
|
656 |
// Now convert section offset of the symbol to image offset. |
83ae0d82da3b
Fixed the bug that symbol address is wrongly fixed up. With this we can correctly set breakpoint at a symbol, e.g. for any symbol specified in stop-at-main preference UI.
l12wang
parents:
37
diff
changeset
|
657 |
if (newSym.n_scnum >= 1 && newSym.n_scnum <= secHeaders.length) // valid section # |
83ae0d82da3b
Fixed the bug that symbol address is wrongly fixed up. With this we can correctly set breakpoint at a symbol, e.g. for any symbol specified in stop-at-main preference UI.
l12wang
parents:
37
diff
changeset
|
658 |
newSym.n_value += secHeaders[newSym.n_scnum-1].s_vaddr; |
83ae0d82da3b
Fixed the bug that symbol address is wrongly fixed up. With this we can correctly set breakpoint at a symbol, e.g. for any symbol specified in stop-at-main preference UI.
l12wang
parents:
37
diff
changeset
|
659 |
|
83ae0d82da3b
Fixed the bug that symbol address is wrongly fixed up. With this we can correctly set breakpoint at a symbol, e.g. for any symbol specified in stop-at-main preference UI.
l12wang
parents:
37
diff
changeset
|
660 |
// convert to absolute address. |
37 | 661 |
if (ntHeader != null) |
99
83ae0d82da3b
Fixed the bug that symbol address is wrongly fixed up. With this we can correctly set breakpoint at a symbol, e.g. for any symbol specified in stop-at-main preference UI.
l12wang
parents:
37
diff
changeset
|
662 |
newSym.n_value += ntHeader.ImageBase; |
83ae0d82da3b
Fixed the bug that symbol address is wrongly fixed up. With this we can correctly set breakpoint at a symbol, e.g. for any symbol specified in stop-at-main preference UI.
l12wang
parents:
37
diff
changeset
|
663 |
|
83ae0d82da3b
Fixed the bug that symbol address is wrongly fixed up. With this we can correctly set breakpoint at a symbol, e.g. for any symbol specified in stop-at-main preference UI.
l12wang
parents:
37
diff
changeset
|
664 |
symbolTable[i] = newSym; |
37 | 665 |
} |
666 |
} |
|
667 |
return symbolTable; |
|
668 |
} |
|
669 |
||
670 |
||
671 |
public byte[] getStringTable() throws IOException { |
|
672 |
if (stringTable == null) { |
|
673 |
if (fileHeader.f_nsyms > 0) { |
|
674 |
RandomAccessFile accessFile = getRandomAccessFile(); |
|
675 |
long symbolsize = Symbol.SYMSZ * fileHeader.f_nsyms; |
|
676 |
long offset = fileHeader.f_symptr + symbolsize; |
|
677 |
accessFile.seek(offset); |
|
678 |
byte[] bytes = new byte[4]; |
|
679 |
accessFile.readFully(bytes); |
|
680 |
int str_len = ReadMemoryAccess.getIntLE(bytes); |
|
681 |
if (str_len > 4 && str_len < accessFile.length()) { |
|
682 |
str_len -= 4; |
|
683 |
stringTable = new byte[str_len]; |
|
684 |
accessFile.seek(offset + 4); |
|
685 |
accessFile.readFully(stringTable); |
|
686 |
} else { |
|
687 |
stringTable = new byte[0]; |
|
688 |
} |
|
689 |
} else { |
|
690 |
stringTable = new byte[0]; |
|
691 |
} |
|
692 |
} |
|
693 |
return stringTable; |
|
694 |
} |
|
695 |
||
696 |
@Override |
|
697 |
public String toString() { |
|
698 |
StringBuffer buffer = new StringBuffer(); |
|
699 |
if (exeHeader != null) { |
|
700 |
buffer.append(exeHeader); |
|
701 |
} |
|
702 |
if (dosHeader != null) { |
|
703 |
buffer.append(dosHeader); |
|
704 |
} |
|
705 |
buffer.append(fileHeader); |
|
706 |
if (optionalHeader != null) { |
|
707 |
buffer.append(optionalHeader); |
|
708 |
} |
|
709 |
if (ntHeader != null) { |
|
710 |
buffer.append(ntHeader); |
|
711 |
} |
|
712 |
try { |
|
713 |
ImageDataDirectory[] dirs = getImageDataDirectories(); |
|
714 |
for (int i = 0; i < dirs.length; i++) { |
|
715 |
buffer.append("Entry ").append(i); //$NON-NLS-1$ |
|
716 |
buffer.append(" ").append(dirs[i]); //$NON-NLS-1$ |
|
717 |
} |
|
718 |
} catch (IOException e) { |
|
719 |
e.printStackTrace(); |
|
720 |
} |
|
721 |
||
722 |
try { |
|
723 |
SectionHeader[] sections = getSectionHeaders(); |
|
724 |
for (int i = 0; i < sections.length; i++) { |
|
725 |
buffer.append(sections[i]); |
|
726 |
} |
|
727 |
} catch (IOException e) { |
|
728 |
e.printStackTrace(); |
|
729 |
} |
|
730 |
||
731 |
try { |
|
732 |
Symbol[] symbols = getSymbols(); |
|
733 |
for (int i = 0; i < symbols.length; i++) { |
|
734 |
buffer.append(symbols[i]); |
|
735 |
} |
|
736 |
} catch (IOException e) { |
|
737 |
e.printStackTrace(); |
|
738 |
} |
|
739 |
||
740 |
try { |
|
741 |
byte[] bytes = getStringTable(); |
|
742 |
String[] strings = Coff.getStringTable(bytes); |
|
743 |
for (int i = 0; i < strings.length; i++) { |
|
744 |
buffer.append(strings[i]); |
|
745 |
} |
|
746 |
} catch (IOException e) { |
|
747 |
e.printStackTrace(); |
|
748 |
} |
|
749 |
return buffer.toString(); |
|
750 |
} |
|
751 |
||
752 |
RandomAccessFile getRandomAccessFile () throws IOException { |
|
753 |
if (rfile == null) { |
|
754 |
rfile = new RandomAccessFile(filename, "r"); //$NON-NLS-1$ |
|
755 |
} |
|
756 |
return rfile; |
|
757 |
} |
|
758 |
||
759 |
private ISymbolReader createCodeViewReader() { |
|
760 |
ISymbolReader symReader = null; |
|
761 |
final int IMAGE_DIRECTORY_ENTRY_DEBUG = 6; |
|
762 |
||
763 |
try { |
|
764 |
// the debug directory is the 6th entry |
|
765 |
NTOptionalHeader ntHeader = getNTOptionalHeader(); |
|
766 |
if (ntHeader==null || ntHeader.NumberOfRvaAndSizes < IMAGE_DIRECTORY_ENTRY_DEBUG) |
|
767 |
return null; |
|
768 |
||
769 |
int debugDir = ntHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress; |
|
770 |
if (debugDir == 0) |
|
771 |
return null; |
|
772 |
||
773 |
int debugFormats = ntHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG].Size / 28; |
|
774 |
if (debugFormats == 0) |
|
775 |
return null; |
|
776 |
||
777 |
SectionHeader[] sections = getSectionHeaders(); |
|
778 |
||
779 |
// loop through the section headers to find the .rdata section |
|
780 |
for (int i = 0; i < sections.length; i++) { |
|
781 |
String name = new String(sections[i].s_name).trim(); |
|
782 |
if (name.equals(".rdata")) { //$NON-NLS-1$ |
|
783 |
// figure out the file offset of the debug ddirectory entries |
|
784 |
int offsetInto_rdata = debugDir - sections[i].s_vaddr; |
|
785 |
int fileOffset = sections[i].s_scnptr + offsetInto_rdata; |
|
786 |
RandomAccessFile accessFile = getRandomAccessFile(); |
|
787 |
||
788 |
// loop through the debug directories looking for CodeView (type 2) |
|
789 |
for (int j = 0; j < debugFormats; j++) { |
|
790 |
PE.IMAGE_DEBUG_DIRECTORY dir = new PE.IMAGE_DEBUG_DIRECTORY( |
|
791 |
accessFile, fileOffset); |
|
792 |
||
793 |
if ((2 == dir.Type) && (dir.SizeOfData > 0)) { |
|
794 |
// CodeView found, seek to actual data |
|
795 |
int debugBase = dir.PointerToRawData; |
|
796 |
accessFile.seek(debugBase); |
|
797 |
||
798 |
// sanity check. the first four bytes of the CodeView |
|
799 |
// data should be "NB11" |
|
800 |
String s2 = accessFile.readLine(); |
|
801 |
if (s2.startsWith("NB11")) { //$NON-NLS-1$ |
|
802 |
Attribute att = getAttribute(); |
|
803 |
symReader = new CodeViewReader(accessFile, |
|
804 |
debugBase, att.isLittleEndian()); |
|
805 |
return symReader; |
|
806 |
} |
|
807 |
} |
|
808 |
fileOffset += dir.DEBUGDIRSZ; |
|
809 |
} |
|
810 |
} |
|
811 |
} |
|
812 |
} catch (IOException e) { |
|
813 |
e.printStackTrace(); |
|
814 |
} |
|
815 |
||
816 |
return symReader; |
|
817 |
} |
|
818 |
||
819 |
private ISymbolReader createStabsReader() { |
|
820 |
ISymbolReader symReader = null; |
|
821 |
try { |
|
822 |
SectionHeader[] sections = getSectionHeaders(); |
|
823 |
byte[] stab = null; |
|
824 |
byte[] stabstr = null; |
|
825 |
||
826 |
// loop through the section headers looking for stabs info |
|
827 |
for (int i = 0; i < sections.length; i++) { |
|
828 |
String name = new String(sections[i].s_name).trim(); |
|
829 |
if (name.equals(".stab")) { //$NON-NLS-1$ |
|
830 |
stab = sections[i].getRawData(); |
|
831 |
} |
|
832 |
if (name.equals(".stabstr")) { //$NON-NLS-1$ |
|
833 |
stabstr = sections[i].getRawData(); |
|
834 |
} |
|
835 |
} |
|
836 |
||
837 |
// if we found both sections then proceed |
|
838 |
if (stab != null && stabstr != null) { |
|
839 |
Attribute att = getAttribute(); |
|
840 |
symReader = new StabsReader(stab, stabstr, att.isLittleEndian()); |
|
841 |
} |
|
842 |
||
843 |
} catch (IOException e) { |
|
844 |
} |
|
845 |
return symReader; |
|
846 |
} |
|
847 |
||
848 |
public ISymbolReader getSymbolReader() { |
|
849 |
ISymbolReader reader = null; |
|
850 |
reader = createStabsReader(); |
|
851 |
if (reader == null) { |
|
852 |
reader = createCodeViewReader(); |
|
853 |
} |
|
854 |
if (reader == null) { |
|
855 |
reader = createDwarfReader(); |
|
856 |
} |
|
857 |
return reader; |
|
858 |
} |
|
859 |
||
860 |
private ISymbolReader createDwarfReader() { |
|
861 |
DwarfReader reader = null; |
|
862 |
// Check if Dwarf data exists |
|
863 |
try { |
|
864 |
reader = new DwarfReader(this); |
|
865 |
} catch (IOException e) { |
|
866 |
// No Dwarf data in the Elf. |
|
867 |
} |
|
868 |
return reader; |
|
869 |
} |
|
870 |
||
871 |
/** |
|
872 |
* @since 5.1 |
|
873 |
*/ |
|
874 |
public String getStringTableEntry(int offset) throws IOException |
|
875 |
{ |
|
876 |
byte[] bytes = getStringTable(); |
|
877 |
offset = offset - 4; |
|
878 |
for (int i = offset; i < bytes.length; i++) { |
|
879 |
if (bytes[i] == 0) { |
|
880 |
return new String(bytes, offset, i - offset); |
|
881 |
} |
|
882 |
} |
|
883 |
||
884 |
return new String(); |
|
885 |
} |
|
886 |
||
887 |
/** |
|
888 |
* @since 5.1 |
|
889 |
*/ |
|
890 |
public String getFilename() { |
|
891 |
return filename; |
|
892 |
} |
|
893 |
} |