annotate agent/src/share/classes/sun/jvm/hotspot/debugger/posix/elf/ELFFileParser.java @ 1913:3b2dea75431e

6984311: JSR 292 needs optional bootstrap method parameters Summary: Allow CONSTANT_InvokeDynamic nodes to have any number of extra operands. Reviewed-by: twisti
author jrose
date Sat, 30 Oct 2010 13:08:23 -0700
parents c18cbe5936b8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
1552
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 0
diff changeset
2 * Copyright (c) 2001, 2004, Oracle and/or its affiliates. All rights reserved.
0
a61af66fc99e Initial load
duke
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a61af66fc99e Initial load
duke
parents:
diff changeset
4 *
a61af66fc99e Initial load
duke
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
a61af66fc99e Initial load
duke
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
a61af66fc99e Initial load
duke
parents:
diff changeset
7 * published by the Free Software Foundation.
a61af66fc99e Initial load
duke
parents:
diff changeset
8 *
a61af66fc99e Initial load
duke
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
a61af66fc99e Initial load
duke
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a61af66fc99e Initial load
duke
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a61af66fc99e Initial load
duke
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
a61af66fc99e Initial load
duke
parents:
diff changeset
13 * accompanied this code).
a61af66fc99e Initial load
duke
parents:
diff changeset
14 *
a61af66fc99e Initial load
duke
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
a61af66fc99e Initial load
duke
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
a61af66fc99e Initial load
duke
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a61af66fc99e Initial load
duke
parents:
diff changeset
18 *
1552
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 0
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 0
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 0
diff changeset
21 * questions.
0
a61af66fc99e Initial load
duke
parents:
diff changeset
22 *
a61af66fc99e Initial load
duke
parents:
diff changeset
23 */
a61af66fc99e Initial load
duke
parents:
diff changeset
24
a61af66fc99e Initial load
duke
parents:
diff changeset
25 package sun.jvm.hotspot.debugger.posix.elf;
a61af66fc99e Initial load
duke
parents:
diff changeset
26
a61af66fc99e Initial load
duke
parents:
diff changeset
27 import java.io.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
28 import java.util.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
29 import sun.jvm.hotspot.utilities.memo.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
30 import sun.jvm.hotspot.debugger.DataSource;
a61af66fc99e Initial load
duke
parents:
diff changeset
31 import sun.jvm.hotspot.debugger.RandomAccessFileDataSource;
a61af66fc99e Initial load
duke
parents:
diff changeset
32
a61af66fc99e Initial load
duke
parents:
diff changeset
33 public class ELFFileParser {
a61af66fc99e Initial load
duke
parents:
diff changeset
34 private static ELFFileParser elfParser;
a61af66fc99e Initial load
duke
parents:
diff changeset
35 private static final String US_ASCII = "US-ASCII";
a61af66fc99e Initial load
duke
parents:
diff changeset
36
a61af66fc99e Initial load
duke
parents:
diff changeset
37 public static ELFFileParser getParser() {
a61af66fc99e Initial load
duke
parents:
diff changeset
38 if (elfParser == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
39 elfParser = new ELFFileParser();
a61af66fc99e Initial load
duke
parents:
diff changeset
40 }
a61af66fc99e Initial load
duke
parents:
diff changeset
41 return elfParser;
a61af66fc99e Initial load
duke
parents:
diff changeset
42 }
a61af66fc99e Initial load
duke
parents:
diff changeset
43
a61af66fc99e Initial load
duke
parents:
diff changeset
44 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
45 * Parses the data in filename and returns the ELFFile representation.
a61af66fc99e Initial load
duke
parents:
diff changeset
46 */
a61af66fc99e Initial load
duke
parents:
diff changeset
47 public ELFFile parse(String filename) throws ELFException {
a61af66fc99e Initial load
duke
parents:
diff changeset
48 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
49 RandomAccessFile file = new RandomAccessFile(filename, "r");
a61af66fc99e Initial load
duke
parents:
diff changeset
50 return parse(new RandomAccessFileDataSource(file));
a61af66fc99e Initial load
duke
parents:
diff changeset
51 } catch (FileNotFoundException e) {
a61af66fc99e Initial load
duke
parents:
diff changeset
52 throw new ELFException(e);
a61af66fc99e Initial load
duke
parents:
diff changeset
53 }
a61af66fc99e Initial load
duke
parents:
diff changeset
54 }
a61af66fc99e Initial load
duke
parents:
diff changeset
55
a61af66fc99e Initial load
duke
parents:
diff changeset
56 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
57 * Parses the data source and returns the ELFFile representation.
a61af66fc99e Initial load
duke
parents:
diff changeset
58 */
a61af66fc99e Initial load
duke
parents:
diff changeset
59 public ELFFile parse(DataSource source) throws ELFException {
a61af66fc99e Initial load
duke
parents:
diff changeset
60 return new ELFFileImpl(source);
a61af66fc99e Initial load
duke
parents:
diff changeset
61 }
a61af66fc99e Initial load
duke
parents:
diff changeset
62
a61af66fc99e Initial load
duke
parents:
diff changeset
63 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
64 * Implementation of the ELFFile interface.
a61af66fc99e Initial load
duke
parents:
diff changeset
65 */
a61af66fc99e Initial load
duke
parents:
diff changeset
66 class ELFFileImpl implements ELFFile {
a61af66fc99e Initial load
duke
parents:
diff changeset
67 private DataSource file;
a61af66fc99e Initial load
duke
parents:
diff changeset
68 private ELFHeader header;
a61af66fc99e Initial load
duke
parents:
diff changeset
69 private byte ident[] = new byte[16];
a61af66fc99e Initial load
duke
parents:
diff changeset
70
a61af66fc99e Initial load
duke
parents:
diff changeset
71 ELFFileImpl(DataSource file) throws ELFException {
a61af66fc99e Initial load
duke
parents:
diff changeset
72 this.file = file;
a61af66fc99e Initial load
duke
parents:
diff changeset
73 int bytesRead = readBytes(ident);
a61af66fc99e Initial load
duke
parents:
diff changeset
74 if (bytesRead != ident.length) {
a61af66fc99e Initial load
duke
parents:
diff changeset
75 throw new ELFException("Error reading elf header (read " +
a61af66fc99e Initial load
duke
parents:
diff changeset
76 bytesRead + "bytes, expected to " +
a61af66fc99e Initial load
duke
parents:
diff changeset
77 "read " + ident.length + "bytes).");
a61af66fc99e Initial load
duke
parents:
diff changeset
78 }
a61af66fc99e Initial load
duke
parents:
diff changeset
79
a61af66fc99e Initial load
duke
parents:
diff changeset
80 // Check the magic number before we continue reading the file.
a61af66fc99e Initial load
duke
parents:
diff changeset
81 if (!Arrays.equals(getMagicNumber(), ELF_MAGIC_NUMBER)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
82 throw new ELFException("Bad magic number for file.");
a61af66fc99e Initial load
duke
parents:
diff changeset
83 }
a61af66fc99e Initial load
duke
parents:
diff changeset
84
a61af66fc99e Initial load
duke
parents:
diff changeset
85 header = new ELFHeaderImpl();
a61af66fc99e Initial load
duke
parents:
diff changeset
86 }
a61af66fc99e Initial load
duke
parents:
diff changeset
87
a61af66fc99e Initial load
duke
parents:
diff changeset
88 public ELFHeader getHeader() { return header; }
a61af66fc99e Initial load
duke
parents:
diff changeset
89
a61af66fc99e Initial load
duke
parents:
diff changeset
90 public byte[] getMagicNumber() {
a61af66fc99e Initial load
duke
parents:
diff changeset
91 byte magicNumber[] = new byte[4];
a61af66fc99e Initial load
duke
parents:
diff changeset
92 magicNumber[0] = ident[NDX_MAGIC_0];
a61af66fc99e Initial load
duke
parents:
diff changeset
93 magicNumber[1] = ident[NDX_MAGIC_1];
a61af66fc99e Initial load
duke
parents:
diff changeset
94 magicNumber[2] = ident[NDX_MAGIC_2];
a61af66fc99e Initial load
duke
parents:
diff changeset
95 magicNumber[3] = ident[NDX_MAGIC_3];
a61af66fc99e Initial load
duke
parents:
diff changeset
96 return magicNumber;
a61af66fc99e Initial load
duke
parents:
diff changeset
97 }
a61af66fc99e Initial load
duke
parents:
diff changeset
98
a61af66fc99e Initial load
duke
parents:
diff changeset
99 public byte getObjectSize() { return ident[NDX_OBJECT_SIZE]; }
a61af66fc99e Initial load
duke
parents:
diff changeset
100 public byte getEncoding() { return ident[NDX_ENCODING]; }
a61af66fc99e Initial load
duke
parents:
diff changeset
101 public byte getVersion() { return ident[NDX_VERSION]; }
a61af66fc99e Initial load
duke
parents:
diff changeset
102
a61af66fc99e Initial load
duke
parents:
diff changeset
103
a61af66fc99e Initial load
duke
parents:
diff changeset
104 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
105 * Implementation of the ELFHeader interface.
a61af66fc99e Initial load
duke
parents:
diff changeset
106 */
a61af66fc99e Initial load
duke
parents:
diff changeset
107 class ELFHeaderImpl implements ELFHeader {
a61af66fc99e Initial load
duke
parents:
diff changeset
108 /** Marks the file as an object file and provide machine-independent
a61af66fc99e Initial load
duke
parents:
diff changeset
109 * data so the contents may be decoded and interpreted. */
a61af66fc99e Initial load
duke
parents:
diff changeset
110 private byte ident[] = new byte[16]; // unsigned char
a61af66fc99e Initial load
duke
parents:
diff changeset
111 /** Identifies the object file type. */
a61af66fc99e Initial load
duke
parents:
diff changeset
112 private short file_type; // Elf32_Half
a61af66fc99e Initial load
duke
parents:
diff changeset
113 /** The required architecture. */
a61af66fc99e Initial load
duke
parents:
diff changeset
114 private short arch; // Elf32_Half
a61af66fc99e Initial load
duke
parents:
diff changeset
115 /** Version */
a61af66fc99e Initial load
duke
parents:
diff changeset
116 private int version; // Elf32_Word
a61af66fc99e Initial load
duke
parents:
diff changeset
117 /** Virtual address to which the system first transfers control.
a61af66fc99e Initial load
duke
parents:
diff changeset
118 * If there is no entry point for the file the value is 0. */
a61af66fc99e Initial load
duke
parents:
diff changeset
119 private int entry_point; // Elf32_Addr
a61af66fc99e Initial load
duke
parents:
diff changeset
120 /** Program header table offset in bytes. If there is no program
a61af66fc99e Initial load
duke
parents:
diff changeset
121 * header table the value is 0. */
a61af66fc99e Initial load
duke
parents:
diff changeset
122 private int ph_offset; // Elf32_Off
a61af66fc99e Initial load
duke
parents:
diff changeset
123 /** Section header table offset in bytes. If there is no section
a61af66fc99e Initial load
duke
parents:
diff changeset
124 * header table the value is 0. */
a61af66fc99e Initial load
duke
parents:
diff changeset
125 private int sh_offset; // Elf32_Off
a61af66fc99e Initial load
duke
parents:
diff changeset
126 /** Processor specific flags. */
a61af66fc99e Initial load
duke
parents:
diff changeset
127 private int flags; // Elf32_Word
a61af66fc99e Initial load
duke
parents:
diff changeset
128 /** ELF header size in bytes. */
a61af66fc99e Initial load
duke
parents:
diff changeset
129 private short eh_size; // Elf32_Half
a61af66fc99e Initial load
duke
parents:
diff changeset
130 /** Size of one entry in the file's program header table in bytes.
a61af66fc99e Initial load
duke
parents:
diff changeset
131 * All entries are the same size. */
a61af66fc99e Initial load
duke
parents:
diff changeset
132 private short ph_entry_size; // Elf32_Half
a61af66fc99e Initial load
duke
parents:
diff changeset
133 /** Number of entries in the program header table, 0 if no
a61af66fc99e Initial load
duke
parents:
diff changeset
134 * entries. */
a61af66fc99e Initial load
duke
parents:
diff changeset
135 private short num_ph; // Elf32_Half
a61af66fc99e Initial load
duke
parents:
diff changeset
136 /** Section header entry size in bytes. */
a61af66fc99e Initial load
duke
parents:
diff changeset
137 private short sh_entry_size; // Elf32_Half
a61af66fc99e Initial load
duke
parents:
diff changeset
138 /** Number of entries in the section header table, 0 if no
a61af66fc99e Initial load
duke
parents:
diff changeset
139 * entries. */
a61af66fc99e Initial load
duke
parents:
diff changeset
140 private short num_sh; // Elf32_Half
a61af66fc99e Initial load
duke
parents:
diff changeset
141 /** Index into the section header table associated with the section
a61af66fc99e Initial load
duke
parents:
diff changeset
142 * name string table. SH_UNDEF if there is no section name string
a61af66fc99e Initial load
duke
parents:
diff changeset
143 * table. */
a61af66fc99e Initial load
duke
parents:
diff changeset
144 private short sh_string_ndx; // Elf32_Half
a61af66fc99e Initial load
duke
parents:
diff changeset
145
a61af66fc99e Initial load
duke
parents:
diff changeset
146 /** MemoizedObject array of section headers associated with this
a61af66fc99e Initial load
duke
parents:
diff changeset
147 * ELF file. */
a61af66fc99e Initial load
duke
parents:
diff changeset
148 private MemoizedObject[] sectionHeaders;
a61af66fc99e Initial load
duke
parents:
diff changeset
149 /** MemoizedObject array of program headers associated with this
a61af66fc99e Initial load
duke
parents:
diff changeset
150 * ELF file. */
a61af66fc99e Initial load
duke
parents:
diff changeset
151 private MemoizedObject[] programHeaders;
a61af66fc99e Initial load
duke
parents:
diff changeset
152
a61af66fc99e Initial load
duke
parents:
diff changeset
153 /** Used to cache symbol table lookup. */
a61af66fc99e Initial load
duke
parents:
diff changeset
154 private ELFSectionHeader symbolTableSection;
a61af66fc99e Initial load
duke
parents:
diff changeset
155 /** Used to cache dynamic symbol table lookup. */
a61af66fc99e Initial load
duke
parents:
diff changeset
156 private ELFSectionHeader dynamicSymbolTableSection;
a61af66fc99e Initial load
duke
parents:
diff changeset
157 /** Used to cache hash table lookup. */
a61af66fc99e Initial load
duke
parents:
diff changeset
158 private ELFHashTable hashTable;
a61af66fc99e Initial load
duke
parents:
diff changeset
159
a61af66fc99e Initial load
duke
parents:
diff changeset
160 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
161 * Reads the ELF header and sets up the section and program headers
a61af66fc99e Initial load
duke
parents:
diff changeset
162 * in memoized arrays.
a61af66fc99e Initial load
duke
parents:
diff changeset
163 */
a61af66fc99e Initial load
duke
parents:
diff changeset
164 ELFHeaderImpl() throws ELFException {
a61af66fc99e Initial load
duke
parents:
diff changeset
165 file_type = readShort();
a61af66fc99e Initial load
duke
parents:
diff changeset
166 arch = readShort();
a61af66fc99e Initial load
duke
parents:
diff changeset
167 version = readInt();
a61af66fc99e Initial load
duke
parents:
diff changeset
168 entry_point = readInt();
a61af66fc99e Initial load
duke
parents:
diff changeset
169 ph_offset = readInt();
a61af66fc99e Initial load
duke
parents:
diff changeset
170 sh_offset = readInt();
a61af66fc99e Initial load
duke
parents:
diff changeset
171 flags = readInt();
a61af66fc99e Initial load
duke
parents:
diff changeset
172 eh_size = readShort();
a61af66fc99e Initial load
duke
parents:
diff changeset
173 ph_entry_size = readShort();
a61af66fc99e Initial load
duke
parents:
diff changeset
174 num_ph = readShort();
a61af66fc99e Initial load
duke
parents:
diff changeset
175 sh_entry_size = readShort();
a61af66fc99e Initial load
duke
parents:
diff changeset
176 num_sh = readShort();
a61af66fc99e Initial load
duke
parents:
diff changeset
177 sh_string_ndx = readShort();
a61af66fc99e Initial load
duke
parents:
diff changeset
178
a61af66fc99e Initial load
duke
parents:
diff changeset
179 // Set up the section headers
a61af66fc99e Initial load
duke
parents:
diff changeset
180 sectionHeaders = new MemoizedObject[num_sh];
a61af66fc99e Initial load
duke
parents:
diff changeset
181 for (int i = 0; i < num_sh; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
182 final long sectionHeaderOffset =
a61af66fc99e Initial load
duke
parents:
diff changeset
183 (long)(sh_offset + (i * sh_entry_size));
a61af66fc99e Initial load
duke
parents:
diff changeset
184 sectionHeaders[i] = new MemoizedObject() {
a61af66fc99e Initial load
duke
parents:
diff changeset
185 public Object computeValue() {
a61af66fc99e Initial load
duke
parents:
diff changeset
186 return new ELFSectionHeaderImpl(sectionHeaderOffset);
a61af66fc99e Initial load
duke
parents:
diff changeset
187 }
a61af66fc99e Initial load
duke
parents:
diff changeset
188 };
a61af66fc99e Initial load
duke
parents:
diff changeset
189 }
a61af66fc99e Initial load
duke
parents:
diff changeset
190
a61af66fc99e Initial load
duke
parents:
diff changeset
191 // // Set up the program headers
a61af66fc99e Initial load
duke
parents:
diff changeset
192 // programHeaders = new MemoizedObject[num_sh];
a61af66fc99e Initial load
duke
parents:
diff changeset
193 // for (int i = 0; i < num_sh; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
194 // final long programHeaderOffset =
a61af66fc99e Initial load
duke
parents:
diff changeset
195 // (long)(ph_offset + (i * ph_entry_size));
a61af66fc99e Initial load
duke
parents:
diff changeset
196 // programHeaders[i] = new MemoizedObject() {
a61af66fc99e Initial load
duke
parents:
diff changeset
197 // public Object computeValue() {
a61af66fc99e Initial load
duke
parents:
diff changeset
198 // return new ProgramHeaderImpl(programHeaderOffset);
a61af66fc99e Initial load
duke
parents:
diff changeset
199 // }
a61af66fc99e Initial load
duke
parents:
diff changeset
200 // };
a61af66fc99e Initial load
duke
parents:
diff changeset
201 // }
a61af66fc99e Initial load
duke
parents:
diff changeset
202 }
a61af66fc99e Initial load
duke
parents:
diff changeset
203
a61af66fc99e Initial load
duke
parents:
diff changeset
204 public short getFileType() { return file_type; }
a61af66fc99e Initial load
duke
parents:
diff changeset
205 public short getArch() { return arch; }
a61af66fc99e Initial load
duke
parents:
diff changeset
206 public short getSectionHeaderSize() { return sh_entry_size; }
a61af66fc99e Initial load
duke
parents:
diff changeset
207 public short getNumberOfSectionHeaders() { return num_sh; }
a61af66fc99e Initial load
duke
parents:
diff changeset
208
a61af66fc99e Initial load
duke
parents:
diff changeset
209 // public short getProgramHeaderSize() { return ph_entry_size; }
a61af66fc99e Initial load
duke
parents:
diff changeset
210 // public short getNumberOfProgramHeaders() { return num_ph; }
a61af66fc99e Initial load
duke
parents:
diff changeset
211
a61af66fc99e Initial load
duke
parents:
diff changeset
212
a61af66fc99e Initial load
duke
parents:
diff changeset
213 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
214 * Returns the section header at the specified index. The section
a61af66fc99e Initial load
duke
parents:
diff changeset
215 * header at index 0 is defined as being a undefined section. */
a61af66fc99e Initial load
duke
parents:
diff changeset
216 public ELFSectionHeader getSectionHeader(int index) {
a61af66fc99e Initial load
duke
parents:
diff changeset
217 return (ELFSectionHeader)sectionHeaders[index].getValue();
a61af66fc99e Initial load
duke
parents:
diff changeset
218 }
a61af66fc99e Initial load
duke
parents:
diff changeset
219
a61af66fc99e Initial load
duke
parents:
diff changeset
220 public ELFStringTable getSectionHeaderStringTable() {
a61af66fc99e Initial load
duke
parents:
diff changeset
221 return getSectionHeader(sh_string_ndx).getStringTable();
a61af66fc99e Initial load
duke
parents:
diff changeset
222 }
a61af66fc99e Initial load
duke
parents:
diff changeset
223
a61af66fc99e Initial load
duke
parents:
diff changeset
224 public ELFStringTable getStringTable() {
a61af66fc99e Initial load
duke
parents:
diff changeset
225 return findStringTableWithName(ELFSectionHeader.STRING_TABLE_NAME);
a61af66fc99e Initial load
duke
parents:
diff changeset
226 }
a61af66fc99e Initial load
duke
parents:
diff changeset
227
a61af66fc99e Initial load
duke
parents:
diff changeset
228 public ELFStringTable getDynamicStringTable() {
a61af66fc99e Initial load
duke
parents:
diff changeset
229 return findStringTableWithName(
a61af66fc99e Initial load
duke
parents:
diff changeset
230 ELFSectionHeader.DYNAMIC_STRING_TABLE_NAME);
a61af66fc99e Initial load
duke
parents:
diff changeset
231 }
a61af66fc99e Initial load
duke
parents:
diff changeset
232
a61af66fc99e Initial load
duke
parents:
diff changeset
233 private ELFStringTable findStringTableWithName(String tableName) {
a61af66fc99e Initial load
duke
parents:
diff changeset
234 // Loop through the section header and look for a section
a61af66fc99e Initial load
duke
parents:
diff changeset
235 // header with the name "tableName". We can ignore entry 0
a61af66fc99e Initial load
duke
parents:
diff changeset
236 // since it is defined as being undefined.
a61af66fc99e Initial load
duke
parents:
diff changeset
237 ELFSectionHeader sh = null;
a61af66fc99e Initial load
duke
parents:
diff changeset
238 for (int i = 1; i < getNumberOfSectionHeaders(); i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
239 sh = getSectionHeader(i);
a61af66fc99e Initial load
duke
parents:
diff changeset
240 if (tableName.equals(sh.getName())) {
a61af66fc99e Initial load
duke
parents:
diff changeset
241 return sh.getStringTable();
a61af66fc99e Initial load
duke
parents:
diff changeset
242 }
a61af66fc99e Initial load
duke
parents:
diff changeset
243 }
a61af66fc99e Initial load
duke
parents:
diff changeset
244 return null;
a61af66fc99e Initial load
duke
parents:
diff changeset
245 }
a61af66fc99e Initial load
duke
parents:
diff changeset
246
a61af66fc99e Initial load
duke
parents:
diff changeset
247 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
248 * The ELFHashTable does not currently work. This method will
a61af66fc99e Initial load
duke
parents:
diff changeset
249 * always return null. */
a61af66fc99e Initial load
duke
parents:
diff changeset
250 public ELFHashTable getHashTable() {
a61af66fc99e Initial load
duke
parents:
diff changeset
251 // if (hashTable != null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
252 // return hashTable;
a61af66fc99e Initial load
duke
parents:
diff changeset
253 // }
a61af66fc99e Initial load
duke
parents:
diff changeset
254 //
a61af66fc99e Initial load
duke
parents:
diff changeset
255 // ELFHashTable ht = null;
a61af66fc99e Initial load
duke
parents:
diff changeset
256 // for (int i = 1; i < getNumberOfSectionHeaders(); i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
257 // if ((ht = getSectionHeader(i).getHashTable()) != null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
258 // hashTable = ht;
a61af66fc99e Initial load
duke
parents:
diff changeset
259 // return hashTable;
a61af66fc99e Initial load
duke
parents:
diff changeset
260 // }
a61af66fc99e Initial load
duke
parents:
diff changeset
261 // }
a61af66fc99e Initial load
duke
parents:
diff changeset
262 return null;
a61af66fc99e Initial load
duke
parents:
diff changeset
263 }
a61af66fc99e Initial load
duke
parents:
diff changeset
264
a61af66fc99e Initial load
duke
parents:
diff changeset
265 public ELFSectionHeader getSymbolTableSection() {
a61af66fc99e Initial load
duke
parents:
diff changeset
266 if (symbolTableSection != null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
267 return symbolTableSection;
a61af66fc99e Initial load
duke
parents:
diff changeset
268 }
a61af66fc99e Initial load
duke
parents:
diff changeset
269
a61af66fc99e Initial load
duke
parents:
diff changeset
270 symbolTableSection =
a61af66fc99e Initial load
duke
parents:
diff changeset
271 getSymbolTableSection(ELFSectionHeader.TYPE_SYMTBL);
a61af66fc99e Initial load
duke
parents:
diff changeset
272 return symbolTableSection;
a61af66fc99e Initial load
duke
parents:
diff changeset
273 }
a61af66fc99e Initial load
duke
parents:
diff changeset
274
a61af66fc99e Initial load
duke
parents:
diff changeset
275 public ELFSectionHeader getDynamicSymbolTableSection() {
a61af66fc99e Initial load
duke
parents:
diff changeset
276 if (dynamicSymbolTableSection != null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
277 return dynamicSymbolTableSection;
a61af66fc99e Initial load
duke
parents:
diff changeset
278 }
a61af66fc99e Initial load
duke
parents:
diff changeset
279
a61af66fc99e Initial load
duke
parents:
diff changeset
280 dynamicSymbolTableSection =
a61af66fc99e Initial load
duke
parents:
diff changeset
281 getSymbolTableSection(ELFSectionHeader.TYPE_DYNSYM);
a61af66fc99e Initial load
duke
parents:
diff changeset
282 return dynamicSymbolTableSection;
a61af66fc99e Initial load
duke
parents:
diff changeset
283 }
a61af66fc99e Initial load
duke
parents:
diff changeset
284
a61af66fc99e Initial load
duke
parents:
diff changeset
285 private ELFSectionHeader getSymbolTableSection(int type) {
a61af66fc99e Initial load
duke
parents:
diff changeset
286 ELFSectionHeader sh = null;
a61af66fc99e Initial load
duke
parents:
diff changeset
287 for (int i = 1; i < getNumberOfSectionHeaders(); i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
288 sh = getSectionHeader(i);
a61af66fc99e Initial load
duke
parents:
diff changeset
289 if (sh.getType() == type) {
a61af66fc99e Initial load
duke
parents:
diff changeset
290 dynamicSymbolTableSection = sh;
a61af66fc99e Initial load
duke
parents:
diff changeset
291 return sh;
a61af66fc99e Initial load
duke
parents:
diff changeset
292 }
a61af66fc99e Initial load
duke
parents:
diff changeset
293 }
a61af66fc99e Initial load
duke
parents:
diff changeset
294 return null;
a61af66fc99e Initial load
duke
parents:
diff changeset
295 }
a61af66fc99e Initial load
duke
parents:
diff changeset
296
a61af66fc99e Initial load
duke
parents:
diff changeset
297 public ELFSymbol getELFSymbol(String symbolName) {
a61af66fc99e Initial load
duke
parents:
diff changeset
298 if (symbolName == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
299 return null;
a61af66fc99e Initial load
duke
parents:
diff changeset
300 }
a61af66fc99e Initial load
duke
parents:
diff changeset
301
a61af66fc99e Initial load
duke
parents:
diff changeset
302 // Check dynamic symbol table for symbol name.
a61af66fc99e Initial load
duke
parents:
diff changeset
303 ELFSymbol symbol = null;
a61af66fc99e Initial load
duke
parents:
diff changeset
304 int numSymbols = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
305 ELFSectionHeader sh = getDynamicSymbolTableSection();
a61af66fc99e Initial load
duke
parents:
diff changeset
306 if (sh != null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
307 numSymbols = sh.getNumberOfSymbols();
a61af66fc99e Initial load
duke
parents:
diff changeset
308 for (int i = 0; i < Math.ceil(numSymbols / 2); i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
309 if (symbolName.equals(
a61af66fc99e Initial load
duke
parents:
diff changeset
310 (symbol = sh.getELFSymbol(i)).getName())) {
a61af66fc99e Initial load
duke
parents:
diff changeset
311 return symbol;
a61af66fc99e Initial load
duke
parents:
diff changeset
312 } else if (symbolName.equals(
a61af66fc99e Initial load
duke
parents:
diff changeset
313 (symbol = sh.getELFSymbol(
a61af66fc99e Initial load
duke
parents:
diff changeset
314 numSymbols - 1 - i)).getName())) {
a61af66fc99e Initial load
duke
parents:
diff changeset
315 return symbol;
a61af66fc99e Initial load
duke
parents:
diff changeset
316 }
a61af66fc99e Initial load
duke
parents:
diff changeset
317 }
a61af66fc99e Initial load
duke
parents:
diff changeset
318 }
a61af66fc99e Initial load
duke
parents:
diff changeset
319
a61af66fc99e Initial load
duke
parents:
diff changeset
320 // Check symbol table for symbol name.
a61af66fc99e Initial load
duke
parents:
diff changeset
321 sh = getSymbolTableSection();
a61af66fc99e Initial load
duke
parents:
diff changeset
322 if (sh != null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
323 numSymbols = sh.getNumberOfSymbols();
a61af66fc99e Initial load
duke
parents:
diff changeset
324 for (int i = 0; i < Math.ceil(numSymbols / 2); i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
325 if (symbolName.equals(
a61af66fc99e Initial load
duke
parents:
diff changeset
326 (symbol = sh.getELFSymbol(i)).getName())) {
a61af66fc99e Initial load
duke
parents:
diff changeset
327 return symbol;
a61af66fc99e Initial load
duke
parents:
diff changeset
328 } else if (symbolName.equals(
a61af66fc99e Initial load
duke
parents:
diff changeset
329 (symbol = sh.getELFSymbol(
a61af66fc99e Initial load
duke
parents:
diff changeset
330 numSymbols - 1 - i)).getName())) {
a61af66fc99e Initial load
duke
parents:
diff changeset
331 return symbol;
a61af66fc99e Initial load
duke
parents:
diff changeset
332 }
a61af66fc99e Initial load
duke
parents:
diff changeset
333 }
a61af66fc99e Initial load
duke
parents:
diff changeset
334 }
a61af66fc99e Initial load
duke
parents:
diff changeset
335 return null;
a61af66fc99e Initial load
duke
parents:
diff changeset
336 }
a61af66fc99e Initial load
duke
parents:
diff changeset
337
a61af66fc99e Initial load
duke
parents:
diff changeset
338 public ELFSymbol getELFSymbol(long address) {
a61af66fc99e Initial load
duke
parents:
diff changeset
339 // Check dynamic symbol table for address.
a61af66fc99e Initial load
duke
parents:
diff changeset
340 ELFSymbol symbol = null;
a61af66fc99e Initial load
duke
parents:
diff changeset
341 int numSymbols = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
342 long value = 0L;
a61af66fc99e Initial load
duke
parents:
diff changeset
343
a61af66fc99e Initial load
duke
parents:
diff changeset
344 ELFSectionHeader sh = getDynamicSymbolTableSection();
a61af66fc99e Initial load
duke
parents:
diff changeset
345 if (sh != null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
346 numSymbols = sh.getNumberOfSymbols();
a61af66fc99e Initial load
duke
parents:
diff changeset
347 for (int i = 0; i < numSymbols; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
348 symbol = sh.getELFSymbol(i);
a61af66fc99e Initial load
duke
parents:
diff changeset
349 value = symbol.getValue();
a61af66fc99e Initial load
duke
parents:
diff changeset
350 if (address >= value && address < value + symbol.getSize()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
351 return symbol;
a61af66fc99e Initial load
duke
parents:
diff changeset
352 }
a61af66fc99e Initial load
duke
parents:
diff changeset
353 }
a61af66fc99e Initial load
duke
parents:
diff changeset
354 }
a61af66fc99e Initial load
duke
parents:
diff changeset
355
a61af66fc99e Initial load
duke
parents:
diff changeset
356 // Check symbol table for symbol name.
a61af66fc99e Initial load
duke
parents:
diff changeset
357 sh = getSymbolTableSection();
a61af66fc99e Initial load
duke
parents:
diff changeset
358 if (sh != null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
359 numSymbols = sh.getNumberOfSymbols();
a61af66fc99e Initial load
duke
parents:
diff changeset
360 for (int i = 0; i < numSymbols; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
361 symbol = sh.getELFSymbol(i);
a61af66fc99e Initial load
duke
parents:
diff changeset
362 value = symbol.getValue();
a61af66fc99e Initial load
duke
parents:
diff changeset
363 if (address >= value && address < value + symbol.getSize()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
364 return symbol;
a61af66fc99e Initial load
duke
parents:
diff changeset
365 }
a61af66fc99e Initial load
duke
parents:
diff changeset
366 }
a61af66fc99e Initial load
duke
parents:
diff changeset
367 }
a61af66fc99e Initial load
duke
parents:
diff changeset
368 return null;
a61af66fc99e Initial load
duke
parents:
diff changeset
369 }
a61af66fc99e Initial load
duke
parents:
diff changeset
370
a61af66fc99e Initial load
duke
parents:
diff changeset
371 // public ProgramHeader getProgramHeader(int index) {
a61af66fc99e Initial load
duke
parents:
diff changeset
372 // return (ProgramHeader)programHeaders[index].getValue();
a61af66fc99e Initial load
duke
parents:
diff changeset
373 // }
a61af66fc99e Initial load
duke
parents:
diff changeset
374 }
a61af66fc99e Initial load
duke
parents:
diff changeset
375
a61af66fc99e Initial load
duke
parents:
diff changeset
376
a61af66fc99e Initial load
duke
parents:
diff changeset
377 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
378 * Implementation of the ELFSectionHeader interface.
a61af66fc99e Initial load
duke
parents:
diff changeset
379 */
a61af66fc99e Initial load
duke
parents:
diff changeset
380 class ELFSectionHeaderImpl implements ELFSectionHeader {
a61af66fc99e Initial load
duke
parents:
diff changeset
381 /** Index into the section header string table which gives the
a61af66fc99e Initial load
duke
parents:
diff changeset
382 * name of the section. */
a61af66fc99e Initial load
duke
parents:
diff changeset
383 private int name_ndx; // Elf32_Word
a61af66fc99e Initial load
duke
parents:
diff changeset
384 /** Section content and semantics. */
a61af66fc99e Initial load
duke
parents:
diff changeset
385 private int type; // Elf32_Word
a61af66fc99e Initial load
duke
parents:
diff changeset
386 /** Flags. */
a61af66fc99e Initial load
duke
parents:
diff changeset
387 private int flags; // Elf32_Word
a61af66fc99e Initial load
duke
parents:
diff changeset
388 /** If the section will be in the memory image of a process this
a61af66fc99e Initial load
duke
parents:
diff changeset
389 * will be the address at which the first byte of section will be
a61af66fc99e Initial load
duke
parents:
diff changeset
390 * loaded. Otherwise, this value is 0. */
a61af66fc99e Initial load
duke
parents:
diff changeset
391 private int address; // Elf32_Addr
a61af66fc99e Initial load
duke
parents:
diff changeset
392 /** Offset from beginning of file to first byte of the section. */
a61af66fc99e Initial load
duke
parents:
diff changeset
393 private int section_offset; // Elf32_Off
a61af66fc99e Initial load
duke
parents:
diff changeset
394 /** Size in bytes of the section. TYPE_NOBITS is a special case. */
a61af66fc99e Initial load
duke
parents:
diff changeset
395 private int size; // Elf32_Word
a61af66fc99e Initial load
duke
parents:
diff changeset
396 /** Section header table index link. */
a61af66fc99e Initial load
duke
parents:
diff changeset
397 private int link; // Elf32_Word
a61af66fc99e Initial load
duke
parents:
diff changeset
398 /** Extra information determined by the section type. */
a61af66fc99e Initial load
duke
parents:
diff changeset
399 private int info; // Elf32_Word
a61af66fc99e Initial load
duke
parents:
diff changeset
400 /** Address alignment constraints for the section. */
a61af66fc99e Initial load
duke
parents:
diff changeset
401 private int address_alignment; // Elf32_Word
a61af66fc99e Initial load
duke
parents:
diff changeset
402 /** Size of a fixed-size entry, 0 if none. */
a61af66fc99e Initial load
duke
parents:
diff changeset
403 private int entry_size; // Elf32_Word
a61af66fc99e Initial load
duke
parents:
diff changeset
404
a61af66fc99e Initial load
duke
parents:
diff changeset
405 /** Memoized symbol table. */
a61af66fc99e Initial load
duke
parents:
diff changeset
406 private MemoizedObject[] symbols;
a61af66fc99e Initial load
duke
parents:
diff changeset
407 /** Memoized string table. */
a61af66fc99e Initial load
duke
parents:
diff changeset
408 private MemoizedObject stringTable;
a61af66fc99e Initial load
duke
parents:
diff changeset
409 /** Memoized hash table. */
a61af66fc99e Initial load
duke
parents:
diff changeset
410 private MemoizedObject hashTable;
a61af66fc99e Initial load
duke
parents:
diff changeset
411
a61af66fc99e Initial load
duke
parents:
diff changeset
412 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
413 * Reads the section header information located at offset.
a61af66fc99e Initial load
duke
parents:
diff changeset
414 */
a61af66fc99e Initial load
duke
parents:
diff changeset
415 ELFSectionHeaderImpl(long offset) throws ELFException {
a61af66fc99e Initial load
duke
parents:
diff changeset
416 seek(offset);
a61af66fc99e Initial load
duke
parents:
diff changeset
417 name_ndx = readInt();
a61af66fc99e Initial load
duke
parents:
diff changeset
418 type = readInt();
a61af66fc99e Initial load
duke
parents:
diff changeset
419 flags = readInt();
a61af66fc99e Initial load
duke
parents:
diff changeset
420 address = readInt();
a61af66fc99e Initial load
duke
parents:
diff changeset
421 section_offset = readInt();
a61af66fc99e Initial load
duke
parents:
diff changeset
422 size = readInt();
a61af66fc99e Initial load
duke
parents:
diff changeset
423 link = readInt();
a61af66fc99e Initial load
duke
parents:
diff changeset
424 info = readInt();
a61af66fc99e Initial load
duke
parents:
diff changeset
425 address_alignment = readInt();
a61af66fc99e Initial load
duke
parents:
diff changeset
426 entry_size = readInt();
a61af66fc99e Initial load
duke
parents:
diff changeset
427
a61af66fc99e Initial load
duke
parents:
diff changeset
428 switch (type) {
a61af66fc99e Initial load
duke
parents:
diff changeset
429 case ELFSectionHeader.TYPE_NULL:
a61af66fc99e Initial load
duke
parents:
diff changeset
430 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
431 case ELFSectionHeader.TYPE_PROGBITS:
a61af66fc99e Initial load
duke
parents:
diff changeset
432 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
433 case ELFSectionHeader.TYPE_SYMTBL:
a61af66fc99e Initial load
duke
parents:
diff changeset
434 case ELFSectionHeader.TYPE_DYNSYM:
a61af66fc99e Initial load
duke
parents:
diff changeset
435 // Setup the symbol table.
a61af66fc99e Initial load
duke
parents:
diff changeset
436 int num_entries = size / entry_size;
a61af66fc99e Initial load
duke
parents:
diff changeset
437 symbols = new MemoizedObject[num_entries];
a61af66fc99e Initial load
duke
parents:
diff changeset
438 for (int i = 0; i < num_entries; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
439 final int symbolOffset = section_offset +
a61af66fc99e Initial load
duke
parents:
diff changeset
440 (i * entry_size);
a61af66fc99e Initial load
duke
parents:
diff changeset
441 symbols[i] = new MemoizedObject() {
a61af66fc99e Initial load
duke
parents:
diff changeset
442 public Object computeValue() {
a61af66fc99e Initial load
duke
parents:
diff changeset
443 return new ELFSymbolImpl(symbolOffset,type);
a61af66fc99e Initial load
duke
parents:
diff changeset
444 }
a61af66fc99e Initial load
duke
parents:
diff changeset
445 };
a61af66fc99e Initial load
duke
parents:
diff changeset
446 }
a61af66fc99e Initial load
duke
parents:
diff changeset
447 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
448 case ELFSectionHeader.TYPE_STRTBL:
a61af66fc99e Initial load
duke
parents:
diff changeset
449 // Setup the string table.
a61af66fc99e Initial load
duke
parents:
diff changeset
450 final int strTableOffset = section_offset;
a61af66fc99e Initial load
duke
parents:
diff changeset
451 final int strTableSize = size;
a61af66fc99e Initial load
duke
parents:
diff changeset
452 stringTable = new MemoizedObject() {
a61af66fc99e Initial load
duke
parents:
diff changeset
453 public Object computeValue() {
a61af66fc99e Initial load
duke
parents:
diff changeset
454 return new ELFStringTableImpl(strTableOffset,
a61af66fc99e Initial load
duke
parents:
diff changeset
455 strTableSize);
a61af66fc99e Initial load
duke
parents:
diff changeset
456 }
a61af66fc99e Initial load
duke
parents:
diff changeset
457 };
a61af66fc99e Initial load
duke
parents:
diff changeset
458 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
459 case ELFSectionHeader.TYPE_RELO_EXPLICIT:
a61af66fc99e Initial load
duke
parents:
diff changeset
460 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
461 case ELFSectionHeader.TYPE_HASH:
a61af66fc99e Initial load
duke
parents:
diff changeset
462 final int hashTableOffset = section_offset;
a61af66fc99e Initial load
duke
parents:
diff changeset
463 final int hashTableSize = size;
a61af66fc99e Initial load
duke
parents:
diff changeset
464 hashTable = new MemoizedObject() {
a61af66fc99e Initial load
duke
parents:
diff changeset
465 public Object computeValue() {
a61af66fc99e Initial load
duke
parents:
diff changeset
466 return new ELFHashTableImpl(hashTableOffset,
a61af66fc99e Initial load
duke
parents:
diff changeset
467 hashTableSize);
a61af66fc99e Initial load
duke
parents:
diff changeset
468 }
a61af66fc99e Initial load
duke
parents:
diff changeset
469 };
a61af66fc99e Initial load
duke
parents:
diff changeset
470 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
471 case ELFSectionHeader.TYPE_DYNAMIC:
a61af66fc99e Initial load
duke
parents:
diff changeset
472 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
473 case ELFSectionHeader.TYPE_NOTE:
a61af66fc99e Initial load
duke
parents:
diff changeset
474 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
475 case ELFSectionHeader.TYPE_NOBITS:
a61af66fc99e Initial load
duke
parents:
diff changeset
476 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
477 case ELFSectionHeader.TYPE_RELO:
a61af66fc99e Initial load
duke
parents:
diff changeset
478 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
479 case ELFSectionHeader.TYPE_SHLIB:
a61af66fc99e Initial load
duke
parents:
diff changeset
480 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
481 default:
a61af66fc99e Initial load
duke
parents:
diff changeset
482 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
483 }
a61af66fc99e Initial load
duke
parents:
diff changeset
484 }
a61af66fc99e Initial load
duke
parents:
diff changeset
485
a61af66fc99e Initial load
duke
parents:
diff changeset
486 public int getType() {
a61af66fc99e Initial load
duke
parents:
diff changeset
487 return type;
a61af66fc99e Initial load
duke
parents:
diff changeset
488 }
a61af66fc99e Initial load
duke
parents:
diff changeset
489
a61af66fc99e Initial load
duke
parents:
diff changeset
490 public int getNumberOfSymbols() {
a61af66fc99e Initial load
duke
parents:
diff changeset
491 if (symbols != null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
492 return symbols.length;
a61af66fc99e Initial load
duke
parents:
diff changeset
493 }
a61af66fc99e Initial load
duke
parents:
diff changeset
494 return 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
495 }
a61af66fc99e Initial load
duke
parents:
diff changeset
496
a61af66fc99e Initial load
duke
parents:
diff changeset
497 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
498 * Returns the ELFSymbol at the specified index. Index 0 is
a61af66fc99e Initial load
duke
parents:
diff changeset
499 * reserved for the undefined ELF symbol. */
a61af66fc99e Initial load
duke
parents:
diff changeset
500 public ELFSymbol getELFSymbol(int index) {
a61af66fc99e Initial load
duke
parents:
diff changeset
501 return (ELFSymbol)symbols[index].getValue();
a61af66fc99e Initial load
duke
parents:
diff changeset
502 }
a61af66fc99e Initial load
duke
parents:
diff changeset
503
a61af66fc99e Initial load
duke
parents:
diff changeset
504 public ELFStringTable getStringTable() {
a61af66fc99e Initial load
duke
parents:
diff changeset
505 if (stringTable != null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
506 return (ELFStringTable)stringTable.getValue();
a61af66fc99e Initial load
duke
parents:
diff changeset
507 }
a61af66fc99e Initial load
duke
parents:
diff changeset
508 return null;
a61af66fc99e Initial load
duke
parents:
diff changeset
509 }
a61af66fc99e Initial load
duke
parents:
diff changeset
510
a61af66fc99e Initial load
duke
parents:
diff changeset
511 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
512 * The ELFHashTable does not currently work. This method will
a61af66fc99e Initial load
duke
parents:
diff changeset
513 * always return null. */
a61af66fc99e Initial load
duke
parents:
diff changeset
514 public ELFHashTable getHashTable() {
a61af66fc99e Initial load
duke
parents:
diff changeset
515 if (hashTable != null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
516 return (ELFHashTable)hashTable.getValue();
a61af66fc99e Initial load
duke
parents:
diff changeset
517 }
a61af66fc99e Initial load
duke
parents:
diff changeset
518 return null;
a61af66fc99e Initial load
duke
parents:
diff changeset
519 }
a61af66fc99e Initial load
duke
parents:
diff changeset
520
a61af66fc99e Initial load
duke
parents:
diff changeset
521 public String getName() {
a61af66fc99e Initial load
duke
parents:
diff changeset
522 if (name_ndx == 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
523 return null;
a61af66fc99e Initial load
duke
parents:
diff changeset
524 }
a61af66fc99e Initial load
duke
parents:
diff changeset
525
a61af66fc99e Initial load
duke
parents:
diff changeset
526 ELFStringTable tbl = getHeader().getSectionHeaderStringTable();
a61af66fc99e Initial load
duke
parents:
diff changeset
527 return tbl.get(name_ndx);
a61af66fc99e Initial load
duke
parents:
diff changeset
528 }
a61af66fc99e Initial load
duke
parents:
diff changeset
529
a61af66fc99e Initial load
duke
parents:
diff changeset
530 public int getLink() {
a61af66fc99e Initial load
duke
parents:
diff changeset
531 return link;
a61af66fc99e Initial load
duke
parents:
diff changeset
532 }
a61af66fc99e Initial load
duke
parents:
diff changeset
533
a61af66fc99e Initial load
duke
parents:
diff changeset
534 public int getOffset() {
a61af66fc99e Initial load
duke
parents:
diff changeset
535 return section_offset;
a61af66fc99e Initial load
duke
parents:
diff changeset
536 }
a61af66fc99e Initial load
duke
parents:
diff changeset
537 }
a61af66fc99e Initial load
duke
parents:
diff changeset
538
a61af66fc99e Initial load
duke
parents:
diff changeset
539
a61af66fc99e Initial load
duke
parents:
diff changeset
540 // class ProgramHeaderImpl implements ProgramHeader {
a61af66fc99e Initial load
duke
parents:
diff changeset
541 // /** Defines the kind of segment this element describes. */
a61af66fc99e Initial load
duke
parents:
diff changeset
542 // private int type; // Elf32_Word
a61af66fc99e Initial load
duke
parents:
diff changeset
543 // /** Offset from the beginning of the file. */
a61af66fc99e Initial load
duke
parents:
diff changeset
544 // private int offset; // Elf32_Off
a61af66fc99e Initial load
duke
parents:
diff changeset
545 // /** Virtual address at which the first byte of the segment
a61af66fc99e Initial load
duke
parents:
diff changeset
546 // * resides in memory. */
a61af66fc99e Initial load
duke
parents:
diff changeset
547 // private int virtual_address; // Elf32_Addr
a61af66fc99e Initial load
duke
parents:
diff changeset
548 // /** Reserved for the physical address of the segment on systems
a61af66fc99e Initial load
duke
parents:
diff changeset
549 // * where physical addressinf is relevant. */
a61af66fc99e Initial load
duke
parents:
diff changeset
550 // private int physical_address; // Elf32_addr
a61af66fc99e Initial load
duke
parents:
diff changeset
551 // /** File image size of segment in bytes, may be 0. */
a61af66fc99e Initial load
duke
parents:
diff changeset
552 // private int file_size; // Elf32_Word
a61af66fc99e Initial load
duke
parents:
diff changeset
553 // /** Memory image size of segment in bytes, may be 0. */
a61af66fc99e Initial load
duke
parents:
diff changeset
554 // private int mem_size; // Elf32_Word
a61af66fc99e Initial load
duke
parents:
diff changeset
555 // /** Flags relevant to this segment. Values for flags are defined
a61af66fc99e Initial load
duke
parents:
diff changeset
556 // * in ELFSectionHeader. */
a61af66fc99e Initial load
duke
parents:
diff changeset
557 // private int flags; // Elf32_Word
a61af66fc99e Initial load
duke
parents:
diff changeset
558 // private int alignment; // Elf32_Word
a61af66fc99e Initial load
duke
parents:
diff changeset
559 //
a61af66fc99e Initial load
duke
parents:
diff changeset
560 // private MemoizedObject[] symbols;
a61af66fc99e Initial load
duke
parents:
diff changeset
561 //
a61af66fc99e Initial load
duke
parents:
diff changeset
562 // ProgramHeaderImpl(long offset) throws ELFException {
a61af66fc99e Initial load
duke
parents:
diff changeset
563 // seek(offset);
a61af66fc99e Initial load
duke
parents:
diff changeset
564 // type = readInt();
a61af66fc99e Initial load
duke
parents:
diff changeset
565 // this.offset = readInt();
a61af66fc99e Initial load
duke
parents:
diff changeset
566 // virtual_address = readInt();
a61af66fc99e Initial load
duke
parents:
diff changeset
567 // physical_address = readInt();
a61af66fc99e Initial load
duke
parents:
diff changeset
568 // file_size = readInt();
a61af66fc99e Initial load
duke
parents:
diff changeset
569 // mem_size = readInt();
a61af66fc99e Initial load
duke
parents:
diff changeset
570 // flags = readInt();
a61af66fc99e Initial load
duke
parents:
diff changeset
571 // alignment = readInt();
a61af66fc99e Initial load
duke
parents:
diff changeset
572 //
a61af66fc99e Initial load
duke
parents:
diff changeset
573 // switch (type) {
a61af66fc99e Initial load
duke
parents:
diff changeset
574 // case ELFSectionHeader.TYPE_NULL:
a61af66fc99e Initial load
duke
parents:
diff changeset
575 // break;
a61af66fc99e Initial load
duke
parents:
diff changeset
576 // case ELFSectionHeader.TYPE_PROGBITS:
a61af66fc99e Initial load
duke
parents:
diff changeset
577 // break;
a61af66fc99e Initial load
duke
parents:
diff changeset
578 // case ELFSectionHeader.TYPE_SYMTBL:
a61af66fc99e Initial load
duke
parents:
diff changeset
579 // case ELFSectionHeader.TYPE_DYNSYM:
a61af66fc99e Initial load
duke
parents:
diff changeset
580 // break;
a61af66fc99e Initial load
duke
parents:
diff changeset
581 // case ELFSectionHeader.TYPE_STRTBL:
a61af66fc99e Initial load
duke
parents:
diff changeset
582 // // Setup the string table.
a61af66fc99e Initial load
duke
parents:
diff changeset
583 // final int strTableOffset = section_offset;
a61af66fc99e Initial load
duke
parents:
diff changeset
584 // final int strTableSize = size;
a61af66fc99e Initial load
duke
parents:
diff changeset
585 // stringTable = new MemoizedObject() {
a61af66fc99e Initial load
duke
parents:
diff changeset
586 // public Object computeValue() {
a61af66fc99e Initial load
duke
parents:
diff changeset
587 // return new ELFStringTableImpl(strTableOffset,
a61af66fc99e Initial load
duke
parents:
diff changeset
588 // strTableSize);
a61af66fc99e Initial load
duke
parents:
diff changeset
589 // }
a61af66fc99e Initial load
duke
parents:
diff changeset
590 // };
a61af66fc99e Initial load
duke
parents:
diff changeset
591 // new ELFStringTableImpl(offset, file_size);
a61af66fc99e Initial load
duke
parents:
diff changeset
592 // break;
a61af66fc99e Initial load
duke
parents:
diff changeset
593 // case ELFSectionHeader.TYPE_RELO_EXPLICIT:
a61af66fc99e Initial load
duke
parents:
diff changeset
594 // break;
a61af66fc99e Initial load
duke
parents:
diff changeset
595 // case ELFSectionHeader.TYPE_HASH:
a61af66fc99e Initial load
duke
parents:
diff changeset
596 // break;
a61af66fc99e Initial load
duke
parents:
diff changeset
597 // case ELFSectionHeader.TYPE_DYNAMIC:
a61af66fc99e Initial load
duke
parents:
diff changeset
598 // break;
a61af66fc99e Initial load
duke
parents:
diff changeset
599 // case ELFSectionHeader.TYPE_NOTE:
a61af66fc99e Initial load
duke
parents:
diff changeset
600 // break;
a61af66fc99e Initial load
duke
parents:
diff changeset
601 // case ELFSectionHeader.TYPE_NOBITS:
a61af66fc99e Initial load
duke
parents:
diff changeset
602 // break;
a61af66fc99e Initial load
duke
parents:
diff changeset
603 // case ELFSectionHeader.TYPE_RELO:
a61af66fc99e Initial load
duke
parents:
diff changeset
604 // break;
a61af66fc99e Initial load
duke
parents:
diff changeset
605 // case ELFSectionHeader.TYPE_SHLIB:
a61af66fc99e Initial load
duke
parents:
diff changeset
606 // break;
a61af66fc99e Initial load
duke
parents:
diff changeset
607 // default:
a61af66fc99e Initial load
duke
parents:
diff changeset
608 // break;
a61af66fc99e Initial load
duke
parents:
diff changeset
609 // }
a61af66fc99e Initial load
duke
parents:
diff changeset
610 // }
a61af66fc99e Initial load
duke
parents:
diff changeset
611 //
a61af66fc99e Initial load
duke
parents:
diff changeset
612 // public int getType() {
a61af66fc99e Initial load
duke
parents:
diff changeset
613 // return type;
a61af66fc99e Initial load
duke
parents:
diff changeset
614 // }
a61af66fc99e Initial load
duke
parents:
diff changeset
615 // }
a61af66fc99e Initial load
duke
parents:
diff changeset
616
a61af66fc99e Initial load
duke
parents:
diff changeset
617
a61af66fc99e Initial load
duke
parents:
diff changeset
618 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
619 * Implementation of the ELFSymbol interface.
a61af66fc99e Initial load
duke
parents:
diff changeset
620 */
a61af66fc99e Initial load
duke
parents:
diff changeset
621 class ELFSymbolImpl implements ELFSymbol {
a61af66fc99e Initial load
duke
parents:
diff changeset
622 /** Index into the symbol string table that holds the character
a61af66fc99e Initial load
duke
parents:
diff changeset
623 * representation of the symbols. 0 means the symbol has no
a61af66fc99e Initial load
duke
parents:
diff changeset
624 * character name. */
a61af66fc99e Initial load
duke
parents:
diff changeset
625 private int name_ndx; // Elf32_Word
a61af66fc99e Initial load
duke
parents:
diff changeset
626 /** Value of the associated symbol. This may be an address or
a61af66fc99e Initial load
duke
parents:
diff changeset
627 * an absolute value. */
a61af66fc99e Initial load
duke
parents:
diff changeset
628 private int value; // Elf32_Addr
a61af66fc99e Initial load
duke
parents:
diff changeset
629 /** Size of the symbol. 0 if the symbol has no size or the size
a61af66fc99e Initial load
duke
parents:
diff changeset
630 * is unknown. */
a61af66fc99e Initial load
duke
parents:
diff changeset
631 private int size; // Elf32_Word
a61af66fc99e Initial load
duke
parents:
diff changeset
632 /** Specifies the symbol type and beinding attributes. */
a61af66fc99e Initial load
duke
parents:
diff changeset
633 private byte info; // unsigned char
a61af66fc99e Initial load
duke
parents:
diff changeset
634 /** Currently holds the value of 0 and has no meaning. */
a61af66fc99e Initial load
duke
parents:
diff changeset
635 private byte other; // unsigned char
a61af66fc99e Initial load
duke
parents:
diff changeset
636 /** Index to the associated section header. This value will need
a61af66fc99e Initial load
duke
parents:
diff changeset
637 * to be read as an unsigned short if we compare it to
a61af66fc99e Initial load
duke
parents:
diff changeset
638 * ELFSectionHeader.NDX_LORESERVE and ELFSectionHeader.NDX_HIRESERVE. */
a61af66fc99e Initial load
duke
parents:
diff changeset
639 private short section_header_ndx; // Elf32_Half
a61af66fc99e Initial load
duke
parents:
diff changeset
640
a61af66fc99e Initial load
duke
parents:
diff changeset
641 private int section_type;
a61af66fc99e Initial load
duke
parents:
diff changeset
642
a61af66fc99e Initial load
duke
parents:
diff changeset
643 /** Offset from the beginning of the file to this symbol. */
a61af66fc99e Initial load
duke
parents:
diff changeset
644 private long offset;
a61af66fc99e Initial load
duke
parents:
diff changeset
645
a61af66fc99e Initial load
duke
parents:
diff changeset
646 ELFSymbolImpl(long offset, int section_type) throws ELFException {
a61af66fc99e Initial load
duke
parents:
diff changeset
647 seek(offset);
a61af66fc99e Initial load
duke
parents:
diff changeset
648 this.offset = offset;
a61af66fc99e Initial load
duke
parents:
diff changeset
649 name_ndx = readInt();
a61af66fc99e Initial load
duke
parents:
diff changeset
650 value = readInt();
a61af66fc99e Initial load
duke
parents:
diff changeset
651 size = readInt();
a61af66fc99e Initial load
duke
parents:
diff changeset
652 info = readByte();
a61af66fc99e Initial load
duke
parents:
diff changeset
653 other = readByte();
a61af66fc99e Initial load
duke
parents:
diff changeset
654 section_header_ndx = readShort();
a61af66fc99e Initial load
duke
parents:
diff changeset
655
a61af66fc99e Initial load
duke
parents:
diff changeset
656 this.section_type = section_type;
a61af66fc99e Initial load
duke
parents:
diff changeset
657
a61af66fc99e Initial load
duke
parents:
diff changeset
658 switch (getType()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
659 case TYPE_NOOBJECT:
a61af66fc99e Initial load
duke
parents:
diff changeset
660 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
661 case TYPE_OBJECT:
a61af66fc99e Initial load
duke
parents:
diff changeset
662 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
663 case TYPE_FUNCTION:
a61af66fc99e Initial load
duke
parents:
diff changeset
664 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
665 case TYPE_SECTION:
a61af66fc99e Initial load
duke
parents:
diff changeset
666 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
667 case TYPE_FILE:
a61af66fc99e Initial load
duke
parents:
diff changeset
668 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
669 case TYPE_LOPROC:
a61af66fc99e Initial load
duke
parents:
diff changeset
670 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
671 case TYPE_HIPROC:
a61af66fc99e Initial load
duke
parents:
diff changeset
672 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
673 default:
a61af66fc99e Initial load
duke
parents:
diff changeset
674 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
675 }
a61af66fc99e Initial load
duke
parents:
diff changeset
676 }
a61af66fc99e Initial load
duke
parents:
diff changeset
677
a61af66fc99e Initial load
duke
parents:
diff changeset
678 public int getBinding() { return info >> 4; }
a61af66fc99e Initial load
duke
parents:
diff changeset
679 public int getType() { return info & 0x0F; }
a61af66fc99e Initial load
duke
parents:
diff changeset
680 public long getOffset() { return offset; }
a61af66fc99e Initial load
duke
parents:
diff changeset
681
a61af66fc99e Initial load
duke
parents:
diff changeset
682 public String getName() {
a61af66fc99e Initial load
duke
parents:
diff changeset
683 // Check to make sure this symbol has a name.
a61af66fc99e Initial load
duke
parents:
diff changeset
684 if (name_ndx == 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
685 return null;
a61af66fc99e Initial load
duke
parents:
diff changeset
686 }
a61af66fc99e Initial load
duke
parents:
diff changeset
687
a61af66fc99e Initial load
duke
parents:
diff changeset
688 // Retrieve the name of the symbol from the correct string
a61af66fc99e Initial load
duke
parents:
diff changeset
689 // table.
a61af66fc99e Initial load
duke
parents:
diff changeset
690 String symbol_name = null;
a61af66fc99e Initial load
duke
parents:
diff changeset
691 if (section_type == ELFSectionHeader.TYPE_SYMTBL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
692 symbol_name = getHeader().getStringTable().get(name_ndx);
a61af66fc99e Initial load
duke
parents:
diff changeset
693 } else if (section_type == ELFSectionHeader.TYPE_DYNSYM) {
a61af66fc99e Initial load
duke
parents:
diff changeset
694 symbol_name =
a61af66fc99e Initial load
duke
parents:
diff changeset
695 getHeader().getDynamicStringTable().get(name_ndx);
a61af66fc99e Initial load
duke
parents:
diff changeset
696 }
a61af66fc99e Initial load
duke
parents:
diff changeset
697 return symbol_name;
a61af66fc99e Initial load
duke
parents:
diff changeset
698 }
a61af66fc99e Initial load
duke
parents:
diff changeset
699
a61af66fc99e Initial load
duke
parents:
diff changeset
700 public long getValue() {
a61af66fc99e Initial load
duke
parents:
diff changeset
701 return value;
a61af66fc99e Initial load
duke
parents:
diff changeset
702 }
a61af66fc99e Initial load
duke
parents:
diff changeset
703
a61af66fc99e Initial load
duke
parents:
diff changeset
704 public int getSize() {
a61af66fc99e Initial load
duke
parents:
diff changeset
705 return size;
a61af66fc99e Initial load
duke
parents:
diff changeset
706 }
a61af66fc99e Initial load
duke
parents:
diff changeset
707 }
a61af66fc99e Initial load
duke
parents:
diff changeset
708
a61af66fc99e Initial load
duke
parents:
diff changeset
709 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
710 * Implementation of the ELFStringTable interface.
a61af66fc99e Initial load
duke
parents:
diff changeset
711 */
a61af66fc99e Initial load
duke
parents:
diff changeset
712 class ELFStringTableImpl implements ELFStringTable {
a61af66fc99e Initial load
duke
parents:
diff changeset
713 /** The string table data. */
a61af66fc99e Initial load
duke
parents:
diff changeset
714 private byte data[];
a61af66fc99e Initial load
duke
parents:
diff changeset
715 private int numStrings;
a61af66fc99e Initial load
duke
parents:
diff changeset
716
a61af66fc99e Initial load
duke
parents:
diff changeset
717 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
718 * Reads all the strings from [offset, length].
a61af66fc99e Initial load
duke
parents:
diff changeset
719 */
a61af66fc99e Initial load
duke
parents:
diff changeset
720 ELFStringTableImpl(long offset, int length) throws ELFException {
a61af66fc99e Initial load
duke
parents:
diff changeset
721 seek(offset);
a61af66fc99e Initial load
duke
parents:
diff changeset
722 data = new byte[length];
a61af66fc99e Initial load
duke
parents:
diff changeset
723 int bytesRead = readBytes(data);
a61af66fc99e Initial load
duke
parents:
diff changeset
724 if (bytesRead != length) {
a61af66fc99e Initial load
duke
parents:
diff changeset
725 throw new ELFException("Error reading string table (read " +
a61af66fc99e Initial load
duke
parents:
diff changeset
726 bytesRead + "bytes, expected to " +
a61af66fc99e Initial load
duke
parents:
diff changeset
727 "read " + data.length + "bytes).");
a61af66fc99e Initial load
duke
parents:
diff changeset
728 }
a61af66fc99e Initial load
duke
parents:
diff changeset
729
a61af66fc99e Initial load
duke
parents:
diff changeset
730 // Count the strings.
a61af66fc99e Initial load
duke
parents:
diff changeset
731 numStrings = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
732 for (int ptr = 0; ptr < data.length; ptr++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
733 if (data[ptr] == '\0') {
a61af66fc99e Initial load
duke
parents:
diff changeset
734 numStrings++;
a61af66fc99e Initial load
duke
parents:
diff changeset
735 }
a61af66fc99e Initial load
duke
parents:
diff changeset
736 }
a61af66fc99e Initial load
duke
parents:
diff changeset
737 }
a61af66fc99e Initial load
duke
parents:
diff changeset
738
a61af66fc99e Initial load
duke
parents:
diff changeset
739 public String get(int index) {
a61af66fc99e Initial load
duke
parents:
diff changeset
740 int startPtr = index;
a61af66fc99e Initial load
duke
parents:
diff changeset
741 int endPtr = index;
a61af66fc99e Initial load
duke
parents:
diff changeset
742 while (data[endPtr] != '\0') {
a61af66fc99e Initial load
duke
parents:
diff changeset
743 endPtr++;
a61af66fc99e Initial load
duke
parents:
diff changeset
744 }
a61af66fc99e Initial load
duke
parents:
diff changeset
745 return new String(data, startPtr, endPtr - startPtr);
a61af66fc99e Initial load
duke
parents:
diff changeset
746 }
a61af66fc99e Initial load
duke
parents:
diff changeset
747
a61af66fc99e Initial load
duke
parents:
diff changeset
748 public int getNumStrings() {
a61af66fc99e Initial load
duke
parents:
diff changeset
749 return numStrings;
a61af66fc99e Initial load
duke
parents:
diff changeset
750 }
a61af66fc99e Initial load
duke
parents:
diff changeset
751 }
a61af66fc99e Initial load
duke
parents:
diff changeset
752
a61af66fc99e Initial load
duke
parents:
diff changeset
753
a61af66fc99e Initial load
duke
parents:
diff changeset
754 /** Implementation of the ELFHashTable. */
a61af66fc99e Initial load
duke
parents:
diff changeset
755 class ELFHashTableImpl implements ELFHashTable {
a61af66fc99e Initial load
duke
parents:
diff changeset
756 private int num_buckets;
a61af66fc99e Initial load
duke
parents:
diff changeset
757 private int num_chains;
a61af66fc99e Initial load
duke
parents:
diff changeset
758
a61af66fc99e Initial load
duke
parents:
diff changeset
759 // These could probably be memoized.
a61af66fc99e Initial load
duke
parents:
diff changeset
760 private int buckets[];
a61af66fc99e Initial load
duke
parents:
diff changeset
761 private int chains[];
a61af66fc99e Initial load
duke
parents:
diff changeset
762
a61af66fc99e Initial load
duke
parents:
diff changeset
763 ELFHashTableImpl(long offset, int length) throws ELFException {
a61af66fc99e Initial load
duke
parents:
diff changeset
764 seek(offset);
a61af66fc99e Initial load
duke
parents:
diff changeset
765 num_buckets = readInt();
a61af66fc99e Initial load
duke
parents:
diff changeset
766 num_chains = readInt();
a61af66fc99e Initial load
duke
parents:
diff changeset
767
a61af66fc99e Initial load
duke
parents:
diff changeset
768 buckets = new int[num_buckets];
a61af66fc99e Initial load
duke
parents:
diff changeset
769 chains = new int[num_chains];
a61af66fc99e Initial load
duke
parents:
diff changeset
770 // Read the bucket data.
a61af66fc99e Initial load
duke
parents:
diff changeset
771 for (int i = 0; i < num_buckets; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
772 buckets[i] = readInt();
a61af66fc99e Initial load
duke
parents:
diff changeset
773 }
a61af66fc99e Initial load
duke
parents:
diff changeset
774
a61af66fc99e Initial load
duke
parents:
diff changeset
775 // Read the chain data.
a61af66fc99e Initial load
duke
parents:
diff changeset
776 for (int i = 0; i < num_chains; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
777 chains[i] = readInt();
a61af66fc99e Initial load
duke
parents:
diff changeset
778 }
a61af66fc99e Initial load
duke
parents:
diff changeset
779
a61af66fc99e Initial load
duke
parents:
diff changeset
780 // Make sure that the amount of bytes we were supposed to read
a61af66fc99e Initial load
duke
parents:
diff changeset
781 // was what we actually read.
a61af66fc99e Initial load
duke
parents:
diff changeset
782 int actual = num_buckets * 4 + num_chains * 4 + 8;
a61af66fc99e Initial load
duke
parents:
diff changeset
783 if (length != actual) {
a61af66fc99e Initial load
duke
parents:
diff changeset
784 throw new ELFException("Error reading string table (read " +
a61af66fc99e Initial load
duke
parents:
diff changeset
785 actual + "bytes, expected to " +
a61af66fc99e Initial load
duke
parents:
diff changeset
786 "read " + length + "bytes).");
a61af66fc99e Initial load
duke
parents:
diff changeset
787 }
a61af66fc99e Initial load
duke
parents:
diff changeset
788 }
a61af66fc99e Initial load
duke
parents:
diff changeset
789
a61af66fc99e Initial load
duke
parents:
diff changeset
790 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
791 * This method doesn't work every time and is unreliable. Use
a61af66fc99e Initial load
duke
parents:
diff changeset
792 * ELFSection.getELFSymbol(String) to retrieve symbols by name.
a61af66fc99e Initial load
duke
parents:
diff changeset
793 * NOTE: since this method is currently broken it will always
a61af66fc99e Initial load
duke
parents:
diff changeset
794 * return null. */
a61af66fc99e Initial load
duke
parents:
diff changeset
795 public ELFSymbol getSymbol(String symbolName) {
a61af66fc99e Initial load
duke
parents:
diff changeset
796 // if (symbolName == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
797 // return null;
a61af66fc99e Initial load
duke
parents:
diff changeset
798 // }
a61af66fc99e Initial load
duke
parents:
diff changeset
799 //
a61af66fc99e Initial load
duke
parents:
diff changeset
800 // long hash = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
801 // long g = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
802 //
a61af66fc99e Initial load
duke
parents:
diff changeset
803 // for (int i = 0; i < symbolName.length(); i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
804 // hash = (hash << 4) + symbolName.charAt(i);
a61af66fc99e Initial load
duke
parents:
diff changeset
805 // if ((g = hash & 0xf0000000) != 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
806 // hash ^= g >>> 24;
a61af66fc99e Initial load
duke
parents:
diff changeset
807 // }
a61af66fc99e Initial load
duke
parents:
diff changeset
808 // hash &= ~g;
a61af66fc99e Initial load
duke
parents:
diff changeset
809 // }
a61af66fc99e Initial load
duke
parents:
diff changeset
810 //
a61af66fc99e Initial load
duke
parents:
diff changeset
811 // ELFSymbol symbol = null;
a61af66fc99e Initial load
duke
parents:
diff changeset
812 // ELFSectionHeader dyn_sh =
a61af66fc99e Initial load
duke
parents:
diff changeset
813 // getHeader().getDynamicSymbolTableSection();
a61af66fc99e Initial load
duke
parents:
diff changeset
814 // int index = (int)hash % num_buckets;
a61af66fc99e Initial load
duke
parents:
diff changeset
815 // while(index != 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
816 // symbol = dyn_sh.getELFSymbol(index);
a61af66fc99e Initial load
duke
parents:
diff changeset
817 // if (symbolName.equals(symbol.getName())) {
a61af66fc99e Initial load
duke
parents:
diff changeset
818 // break;
a61af66fc99e Initial load
duke
parents:
diff changeset
819 // }
a61af66fc99e Initial load
duke
parents:
diff changeset
820 // symbol = null;
a61af66fc99e Initial load
duke
parents:
diff changeset
821 // index = chains[index];
a61af66fc99e Initial load
duke
parents:
diff changeset
822 // }
a61af66fc99e Initial load
duke
parents:
diff changeset
823 // return symbol;
a61af66fc99e Initial load
duke
parents:
diff changeset
824 return null;
a61af66fc99e Initial load
duke
parents:
diff changeset
825 }
a61af66fc99e Initial load
duke
parents:
diff changeset
826 }
a61af66fc99e Initial load
duke
parents:
diff changeset
827
a61af66fc99e Initial load
duke
parents:
diff changeset
828
a61af66fc99e Initial load
duke
parents:
diff changeset
829 public void close() throws ELFException {
a61af66fc99e Initial load
duke
parents:
diff changeset
830 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
831 file.close();
a61af66fc99e Initial load
duke
parents:
diff changeset
832 } catch (IOException e) {
a61af66fc99e Initial load
duke
parents:
diff changeset
833 throw new ELFException(e);
a61af66fc99e Initial load
duke
parents:
diff changeset
834 }
a61af66fc99e Initial load
duke
parents:
diff changeset
835 }
a61af66fc99e Initial load
duke
parents:
diff changeset
836
a61af66fc99e Initial load
duke
parents:
diff changeset
837 void seek(long offset) throws ELFException {
a61af66fc99e Initial load
duke
parents:
diff changeset
838 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
839 file.seek(offset);
a61af66fc99e Initial load
duke
parents:
diff changeset
840 } catch (IOException e) {
a61af66fc99e Initial load
duke
parents:
diff changeset
841 throw new ELFException(e);
a61af66fc99e Initial load
duke
parents:
diff changeset
842 }
a61af66fc99e Initial load
duke
parents:
diff changeset
843 }
a61af66fc99e Initial load
duke
parents:
diff changeset
844
a61af66fc99e Initial load
duke
parents:
diff changeset
845 long getFilePointer() throws ELFException {
a61af66fc99e Initial load
duke
parents:
diff changeset
846 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
847 return file.getFilePointer();
a61af66fc99e Initial load
duke
parents:
diff changeset
848 } catch (IOException e) {
a61af66fc99e Initial load
duke
parents:
diff changeset
849 throw new ELFException(e);
a61af66fc99e Initial load
duke
parents:
diff changeset
850 }
a61af66fc99e Initial load
duke
parents:
diff changeset
851 }
a61af66fc99e Initial load
duke
parents:
diff changeset
852
a61af66fc99e Initial load
duke
parents:
diff changeset
853 byte readByte() throws ELFException {
a61af66fc99e Initial load
duke
parents:
diff changeset
854 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
855 return file.readByte();
a61af66fc99e Initial load
duke
parents:
diff changeset
856 } catch (IOException e) {
a61af66fc99e Initial load
duke
parents:
diff changeset
857 throw new ELFException(e);
a61af66fc99e Initial load
duke
parents:
diff changeset
858 }
a61af66fc99e Initial load
duke
parents:
diff changeset
859 }
a61af66fc99e Initial load
duke
parents:
diff changeset
860
a61af66fc99e Initial load
duke
parents:
diff changeset
861 int readBytes(byte[] b) throws ELFException {
a61af66fc99e Initial load
duke
parents:
diff changeset
862 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
863 return file.read(b);
a61af66fc99e Initial load
duke
parents:
diff changeset
864 } catch (IOException e) {
a61af66fc99e Initial load
duke
parents:
diff changeset
865 throw new ELFException(e);
a61af66fc99e Initial load
duke
parents:
diff changeset
866 }
a61af66fc99e Initial load
duke
parents:
diff changeset
867 }
a61af66fc99e Initial load
duke
parents:
diff changeset
868
a61af66fc99e Initial load
duke
parents:
diff changeset
869 short readShort() throws ELFException {
a61af66fc99e Initial load
duke
parents:
diff changeset
870 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
871 short val;
a61af66fc99e Initial load
duke
parents:
diff changeset
872 switch (ident[NDX_ENCODING]) {
a61af66fc99e Initial load
duke
parents:
diff changeset
873 case DATA_LSB:
a61af66fc99e Initial load
duke
parents:
diff changeset
874 val = byteSwap(file.readShort());
a61af66fc99e Initial load
duke
parents:
diff changeset
875 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
876 case DATA_MSB:
a61af66fc99e Initial load
duke
parents:
diff changeset
877 val = file.readShort();
a61af66fc99e Initial load
duke
parents:
diff changeset
878 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
879 default:
a61af66fc99e Initial load
duke
parents:
diff changeset
880 throw new ELFException("Invalid encoding.");
a61af66fc99e Initial load
duke
parents:
diff changeset
881 }
a61af66fc99e Initial load
duke
parents:
diff changeset
882 return val;
a61af66fc99e Initial load
duke
parents:
diff changeset
883 } catch (IOException e) {
a61af66fc99e Initial load
duke
parents:
diff changeset
884 throw new ELFException(e);
a61af66fc99e Initial load
duke
parents:
diff changeset
885 }
a61af66fc99e Initial load
duke
parents:
diff changeset
886 }
a61af66fc99e Initial load
duke
parents:
diff changeset
887
a61af66fc99e Initial load
duke
parents:
diff changeset
888 int readInt() throws ELFException {
a61af66fc99e Initial load
duke
parents:
diff changeset
889 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
890 int val;
a61af66fc99e Initial load
duke
parents:
diff changeset
891 switch (ident[NDX_ENCODING]) {
a61af66fc99e Initial load
duke
parents:
diff changeset
892 case DATA_LSB:
a61af66fc99e Initial load
duke
parents:
diff changeset
893 val = byteSwap(file.readInt());
a61af66fc99e Initial load
duke
parents:
diff changeset
894 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
895 case DATA_MSB:
a61af66fc99e Initial load
duke
parents:
diff changeset
896 val = file.readInt();
a61af66fc99e Initial load
duke
parents:
diff changeset
897 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
898 default:
a61af66fc99e Initial load
duke
parents:
diff changeset
899 throw new ELFException("Invalid encoding.");
a61af66fc99e Initial load
duke
parents:
diff changeset
900 }
a61af66fc99e Initial load
duke
parents:
diff changeset
901 return val;
a61af66fc99e Initial load
duke
parents:
diff changeset
902 } catch (IOException e) {
a61af66fc99e Initial load
duke
parents:
diff changeset
903 throw new ELFException(e);
a61af66fc99e Initial load
duke
parents:
diff changeset
904 }
a61af66fc99e Initial load
duke
parents:
diff changeset
905 }
a61af66fc99e Initial load
duke
parents:
diff changeset
906
a61af66fc99e Initial load
duke
parents:
diff changeset
907 long readLong() throws ELFException {
a61af66fc99e Initial load
duke
parents:
diff changeset
908 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
909 long val;
a61af66fc99e Initial load
duke
parents:
diff changeset
910 switch (ident[NDX_ENCODING]) {
a61af66fc99e Initial load
duke
parents:
diff changeset
911 case DATA_LSB:
a61af66fc99e Initial load
duke
parents:
diff changeset
912 val = byteSwap(file.readLong());
a61af66fc99e Initial load
duke
parents:
diff changeset
913 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
914 case DATA_MSB:
a61af66fc99e Initial load
duke
parents:
diff changeset
915 val = file.readLong();
a61af66fc99e Initial load
duke
parents:
diff changeset
916 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
917 default:
a61af66fc99e Initial load
duke
parents:
diff changeset
918 throw new ELFException("Invalid encoding.");
a61af66fc99e Initial load
duke
parents:
diff changeset
919 }
a61af66fc99e Initial load
duke
parents:
diff changeset
920 return val;
a61af66fc99e Initial load
duke
parents:
diff changeset
921 } catch (IOException e) {
a61af66fc99e Initial load
duke
parents:
diff changeset
922 throw new ELFException(e);
a61af66fc99e Initial load
duke
parents:
diff changeset
923 }
a61af66fc99e Initial load
duke
parents:
diff changeset
924 }
a61af66fc99e Initial load
duke
parents:
diff changeset
925
a61af66fc99e Initial load
duke
parents:
diff changeset
926 /** Signed byte utility functions used for converting from big-endian
a61af66fc99e Initial load
duke
parents:
diff changeset
927 * (MSB) to little-endian (LSB). */
a61af66fc99e Initial load
duke
parents:
diff changeset
928 short byteSwap(short arg) {
a61af66fc99e Initial load
duke
parents:
diff changeset
929 return (short) ((arg << 8) | ((arg >>> 8) & 0xFF));
a61af66fc99e Initial load
duke
parents:
diff changeset
930 }
a61af66fc99e Initial load
duke
parents:
diff changeset
931
a61af66fc99e Initial load
duke
parents:
diff changeset
932 int byteSwap(int arg) {
a61af66fc99e Initial load
duke
parents:
diff changeset
933 return (((int) byteSwap((short) arg)) << 16) |
a61af66fc99e Initial load
duke
parents:
diff changeset
934 (((int) (byteSwap((short) (arg >>> 16)))) & 0xFFFF);
a61af66fc99e Initial load
duke
parents:
diff changeset
935 }
a61af66fc99e Initial load
duke
parents:
diff changeset
936
a61af66fc99e Initial load
duke
parents:
diff changeset
937 long byteSwap(long arg) {
a61af66fc99e Initial load
duke
parents:
diff changeset
938 return ((((long) byteSwap((int) arg)) << 32) |
a61af66fc99e Initial load
duke
parents:
diff changeset
939 (((long) byteSwap((int) (arg >>> 32))) & 0xFFFFFFFF));
a61af66fc99e Initial load
duke
parents:
diff changeset
940 }
a61af66fc99e Initial load
duke
parents:
diff changeset
941
a61af66fc99e Initial load
duke
parents:
diff changeset
942
a61af66fc99e Initial load
duke
parents:
diff changeset
943 /* Unsigned byte utility functions. Since java does not have unsigned
a61af66fc99e Initial load
duke
parents:
diff changeset
944 * data types we must convert values manually and we must return
a61af66fc99e Initial load
duke
parents:
diff changeset
945 * unsigned values in a larger data type. Therefore we can only have
a61af66fc99e Initial load
duke
parents:
diff changeset
946 * unsigned values for byte, short, and int. */
a61af66fc99e Initial load
duke
parents:
diff changeset
947 short readUnsignedByte() throws ELFException {
a61af66fc99e Initial load
duke
parents:
diff changeset
948 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
949 return unsignedByte(file.readByte());
a61af66fc99e Initial load
duke
parents:
diff changeset
950 } catch (IOException e) {
a61af66fc99e Initial load
duke
parents:
diff changeset
951 throw new ELFException(e);
a61af66fc99e Initial load
duke
parents:
diff changeset
952 }
a61af66fc99e Initial load
duke
parents:
diff changeset
953 }
a61af66fc99e Initial load
duke
parents:
diff changeset
954
a61af66fc99e Initial load
duke
parents:
diff changeset
955 int readUnsignedShort() throws ELFException {
a61af66fc99e Initial load
duke
parents:
diff changeset
956 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
957 int val;
a61af66fc99e Initial load
duke
parents:
diff changeset
958 switch (ident[NDX_ENCODING]) {
a61af66fc99e Initial load
duke
parents:
diff changeset
959 case DATA_LSB:
a61af66fc99e Initial load
duke
parents:
diff changeset
960 val = unsignedByteSwap(file.readShort());
a61af66fc99e Initial load
duke
parents:
diff changeset
961 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
962 case DATA_MSB:
a61af66fc99e Initial load
duke
parents:
diff changeset
963 val = unsignedByte(file.readShort());
a61af66fc99e Initial load
duke
parents:
diff changeset
964 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
965 default:
a61af66fc99e Initial load
duke
parents:
diff changeset
966 throw new ELFException("Invalid encoding.");
a61af66fc99e Initial load
duke
parents:
diff changeset
967 }
a61af66fc99e Initial load
duke
parents:
diff changeset
968 return val;
a61af66fc99e Initial load
duke
parents:
diff changeset
969 } catch (IOException e) {
a61af66fc99e Initial load
duke
parents:
diff changeset
970 throw new ELFException(e);
a61af66fc99e Initial load
duke
parents:
diff changeset
971 }
a61af66fc99e Initial load
duke
parents:
diff changeset
972 }
a61af66fc99e Initial load
duke
parents:
diff changeset
973
a61af66fc99e Initial load
duke
parents:
diff changeset
974 long readUnsignedInt() throws ELFException {
a61af66fc99e Initial load
duke
parents:
diff changeset
975 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
976 long val;
a61af66fc99e Initial load
duke
parents:
diff changeset
977 switch (ident[NDX_ENCODING]) {
a61af66fc99e Initial load
duke
parents:
diff changeset
978 case DATA_LSB:
a61af66fc99e Initial load
duke
parents:
diff changeset
979 val = unsignedByteSwap(file.readInt());
a61af66fc99e Initial load
duke
parents:
diff changeset
980 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
981 case DATA_MSB:
a61af66fc99e Initial load
duke
parents:
diff changeset
982 val = unsignedByte(file.readInt());
a61af66fc99e Initial load
duke
parents:
diff changeset
983 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
984 default:
a61af66fc99e Initial load
duke
parents:
diff changeset
985 throw new ELFException("Invalid encoding.");
a61af66fc99e Initial load
duke
parents:
diff changeset
986 }
a61af66fc99e Initial load
duke
parents:
diff changeset
987 return val;
a61af66fc99e Initial load
duke
parents:
diff changeset
988 } catch (IOException e) {
a61af66fc99e Initial load
duke
parents:
diff changeset
989 throw new ELFException(e);
a61af66fc99e Initial load
duke
parents:
diff changeset
990 }
a61af66fc99e Initial load
duke
parents:
diff changeset
991 }
a61af66fc99e Initial load
duke
parents:
diff changeset
992
a61af66fc99e Initial load
duke
parents:
diff changeset
993 /** Returns the unsigned value of the byte. */
a61af66fc99e Initial load
duke
parents:
diff changeset
994 short unsignedByte(byte arg) {
a61af66fc99e Initial load
duke
parents:
diff changeset
995 return (short)(arg & 0x00FF);
a61af66fc99e Initial load
duke
parents:
diff changeset
996 }
a61af66fc99e Initial load
duke
parents:
diff changeset
997
a61af66fc99e Initial load
duke
parents:
diff changeset
998 /** Returns a big-endian unsigned representation of the short. */
a61af66fc99e Initial load
duke
parents:
diff changeset
999 int unsignedByte(short arg) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1000 int val;
a61af66fc99e Initial load
duke
parents:
diff changeset
1001 if (arg >= 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1002 val = arg;
a61af66fc99e Initial load
duke
parents:
diff changeset
1003 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
1004 val = (int)(((int)unsignedByte((byte)(arg >>> 8)) << 8) |
a61af66fc99e Initial load
duke
parents:
diff changeset
1005 ((byte)arg));
a61af66fc99e Initial load
duke
parents:
diff changeset
1006 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1007 return val;
a61af66fc99e Initial load
duke
parents:
diff changeset
1008 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1009
a61af66fc99e Initial load
duke
parents:
diff changeset
1010 /** Returns a big-endian unsigned representation of the int. */
a61af66fc99e Initial load
duke
parents:
diff changeset
1011 long unsignedByte(int arg) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1012 long val;
a61af66fc99e Initial load
duke
parents:
diff changeset
1013 if (arg >= 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1014 val = arg;
a61af66fc99e Initial load
duke
parents:
diff changeset
1015 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
1016 val = (long)(((long)unsignedByte((short)(arg >>> 16)) << 16) |
a61af66fc99e Initial load
duke
parents:
diff changeset
1017 ((short)arg));
a61af66fc99e Initial load
duke
parents:
diff changeset
1018 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1019 return val;
a61af66fc99e Initial load
duke
parents:
diff changeset
1020 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1021
a61af66fc99e Initial load
duke
parents:
diff changeset
1022 /** Unsigned byte utility functions used for converting from big-endian
a61af66fc99e Initial load
duke
parents:
diff changeset
1023 * (MSB) to little-endian (LSB). */
a61af66fc99e Initial load
duke
parents:
diff changeset
1024 int unsignedByteSwap(short arg) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1025 return (int)(((int)unsignedByte((byte)arg)) << 8) |
a61af66fc99e Initial load
duke
parents:
diff changeset
1026 ((int)unsignedByte((byte)(arg >>> 8)));
a61af66fc99e Initial load
duke
parents:
diff changeset
1027 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1028
a61af66fc99e Initial load
duke
parents:
diff changeset
1029 long unsignedByteSwap(int arg) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1030 return (long)(((long)unsignedByteSwap((short)arg)) << 16) |
a61af66fc99e Initial load
duke
parents:
diff changeset
1031 ((long)unsignedByteSwap((short)(arg >>> 16)));
a61af66fc99e Initial load
duke
parents:
diff changeset
1032 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1033 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1034
a61af66fc99e Initial load
duke
parents:
diff changeset
1035 public static void main(String args[]) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1036 if (args.length != 1) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1037 System.out.println("Usage: java ELFFileParser <elf file>");
a61af66fc99e Initial load
duke
parents:
diff changeset
1038 System.exit(0);
a61af66fc99e Initial load
duke
parents:
diff changeset
1039 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1040
a61af66fc99e Initial load
duke
parents:
diff changeset
1041 // Parse the file.
a61af66fc99e Initial load
duke
parents:
diff changeset
1042 ELFFile elfFile = ELFFileParser.getParser().parse(args[0]);
a61af66fc99e Initial load
duke
parents:
diff changeset
1043
a61af66fc99e Initial load
duke
parents:
diff changeset
1044 ELFHeader elfHeader = elfFile.getHeader();
a61af66fc99e Initial load
duke
parents:
diff changeset
1045 System.out.println("ELF File: " + args[0]);
a61af66fc99e Initial load
duke
parents:
diff changeset
1046
a61af66fc99e Initial load
duke
parents:
diff changeset
1047 System.out.println("ELF object size: " +
a61af66fc99e Initial load
duke
parents:
diff changeset
1048 ((elfFile.getObjectSize() == 0) ? "Invalid Object Size" :
a61af66fc99e Initial load
duke
parents:
diff changeset
1049 (elfFile.getObjectSize() == 1) ? "32-bit" : "64-bit"));
a61af66fc99e Initial load
duke
parents:
diff changeset
1050 System.out.println("ELF data encoding: " +
a61af66fc99e Initial load
duke
parents:
diff changeset
1051 ((elfFile.getEncoding() == 0) ? "Invalid Data Encoding" :
a61af66fc99e Initial load
duke
parents:
diff changeset
1052 (elfFile.getEncoding() == 1) ? "LSB" : "MSB"));
a61af66fc99e Initial load
duke
parents:
diff changeset
1053
a61af66fc99e Initial load
duke
parents:
diff changeset
1054 int h = elfHeader.getNumberOfSectionHeaders();
a61af66fc99e Initial load
duke
parents:
diff changeset
1055 System.out.println("--> Start: reading " + h + " section headers.");
a61af66fc99e Initial load
duke
parents:
diff changeset
1056 for (int i = 0; i < elfHeader.getNumberOfSectionHeaders(); i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1057 ELFSectionHeader sh = elfHeader.getSectionHeader(i);
a61af66fc99e Initial load
duke
parents:
diff changeset
1058 String str = sh.getName();
a61af66fc99e Initial load
duke
parents:
diff changeset
1059 System.out.println("----> Start: Section (" + i + ") " + str);
a61af66fc99e Initial load
duke
parents:
diff changeset
1060
a61af66fc99e Initial load
duke
parents:
diff changeset
1061 int num = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
1062 if ((num = sh.getNumberOfSymbols()) != 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1063 System.out.println("------> Start: reading " + num + " symbols.");
a61af66fc99e Initial load
duke
parents:
diff changeset
1064 for (int j = 0; j < num ; j++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1065 ELFSymbol sym = sh.getELFSymbol(j);
a61af66fc99e Initial load
duke
parents:
diff changeset
1066 //String name = sym.getName();
a61af66fc99e Initial load
duke
parents:
diff changeset
1067 //if (name != null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1068 // System.out.println(name);
a61af66fc99e Initial load
duke
parents:
diff changeset
1069 //}
a61af66fc99e Initial load
duke
parents:
diff changeset
1070 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1071 System.out.println("<------ End: reading " + num + " symbols.");
a61af66fc99e Initial load
duke
parents:
diff changeset
1072 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1073 ELFStringTable st;
a61af66fc99e Initial load
duke
parents:
diff changeset
1074 if (sh.getType() == ELFSectionHeader.TYPE_STRTBL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1075 System.out.println("------> Start: reading string table.");
a61af66fc99e Initial load
duke
parents:
diff changeset
1076 st = sh.getStringTable();
a61af66fc99e Initial load
duke
parents:
diff changeset
1077 System.out.println("<------ End: reading string table.");
a61af66fc99e Initial load
duke
parents:
diff changeset
1078 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1079 if (sh.getType() == ELFSectionHeader.TYPE_HASH) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1080 System.out.println("------> Start: reading hash table.");
a61af66fc99e Initial load
duke
parents:
diff changeset
1081 sh.getHashTable();
a61af66fc99e Initial load
duke
parents:
diff changeset
1082 System.out.println("<------ End: reading hash table.");
a61af66fc99e Initial load
duke
parents:
diff changeset
1083 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1084 System.out.println("<---- End: Section (" + i + ") " + str);
a61af66fc99e Initial load
duke
parents:
diff changeset
1085 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1086 System.out.println("<-- End: reading " + h + " section headers.");
a61af66fc99e Initial load
duke
parents:
diff changeset
1087 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
1088 h = elfHeader.getNumberOfProgramHeaders();
a61af66fc99e Initial load
duke
parents:
diff changeset
1089 System.out.println("--> Start: reading " + h + " program headers.");
a61af66fc99e Initial load
duke
parents:
diff changeset
1090 for (int i = 0; i < elfHeader.getNumberOfProgramHeaders(); i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1091 elfHeader.getProgramHeader(i);
a61af66fc99e Initial load
duke
parents:
diff changeset
1092 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1093 System.out.println("<-- End: reading " + h + " program headers.");
a61af66fc99e Initial load
duke
parents:
diff changeset
1094 */
a61af66fc99e Initial load
duke
parents:
diff changeset
1095 elfFile.close();
a61af66fc99e Initial load
duke
parents:
diff changeset
1096 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1097 }