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