3 import org.xwt.mips.util.*;
7 private SeekableData data;
10 public ELFHeader header;
11 public PHeader[] pheaders;
12 public SHeader[] sheaders;
14 private byte[] stringTable;
16 private boolean sectionReaderActive;
19 private void readFully(byte[] buf) throws IOException {
23 int n = data.read(buf,pos,len);
24 if(n == -1) throw new IOException("EOF");
30 private int readIntBE() throws IOException {
31 byte[] buf = new byte[4];
33 return ((buf[0]&0xff)<<24)|((buf[1]&0xff)<<16)|((buf[2]&0xff)<<8)|((buf[3]&0xff)<<0);
35 private int readInt() throws IOException {
37 if(ident!=null && ident.data == ELFIdent.ELFDATA2LSB)
38 x = ((x<<24)&0xff000000) | ((x<<8)&0xff0000) | ((x>>>8)&0xff00) | ((x>>24)&0xff);
42 private short readShortBE() throws IOException {
43 byte[] buf = new byte[2];
45 return (short)(((buf[0]&0xff)<<8)|((buf[1]&0xff)<<0));
47 private short readShort() throws IOException {
48 short x = readShortBE();
49 if(ident!=null && ident.data == ELFIdent.ELFDATA2LSB)
50 x = (short)((((x<<8)&0xff00) | ((x>>8)&0xff))&0xffff);
54 private byte readByte() throws IOException {
55 byte[] buf = new byte[1];
60 public class ELFIdent {
61 private static final int ELF_MAGIC = 0x7f454c46; // '\177', 'E', 'L', 'F'
63 public static final int ELFCLASSNONE = 0;
64 public static final int ELFCLASS32 = 1;
65 public static final int ELFCLASS64 = 2;
69 public static final int ELFDATANONE = 0;
70 public static final int ELFDATA2LSB = 1;
71 public static final int ELFDATA2MSB = 2;
74 public byte abiversion;
76 ELFIdent() throws IOException {
77 if(readIntBE() != ELF_MAGIC) throw new ELFException("Bad Magic (is: " );
80 if(klass != ELFCLASS32) throw new ELFException("org.xwt.mips.ELF does not suport 64-bit binaries");
83 if(data != ELFDATA2LSB && data != ELFDATA2MSB) throw new ELFException("Unknown byte order");
85 readByte(); // version
87 abiversion = readByte();
88 for(int i=0;i<7;i++) readByte(); // padding
92 public class ELFHeader {
93 public static final short ET_EXEC = 2;
96 public static final short EM_MIPS = 8;
105 public short phentsize;
107 public short shentsize;
109 public short shstrndx;
111 ELFHeader() throws IOException {
113 machine = readShort();
115 if(version != 1) throw new ELFException("version != 1");
120 ehsize = readShort();
121 phentsize = readShort();
123 shentsize = readShort();
125 shstrndx = readShort();
129 public class PHeader {
139 public static final int PF_X = 0x1;
140 public static final int PF_W = 0x2;
141 public static final int PF_R = 0x4;
143 public static final int PT_LOAD = 1;
145 PHeader() throws IOException {
154 if(filesz > memsz) throw new ELFException("ELF inconsistency: filesz > memsz (" + toHex(filesz) + " > " + toHex(memsz) + ")");
157 public boolean writable() { return (flags & PF_W) != 0; }
159 public InputStream getInputStream() throws IOException {
160 return new BufferedInputStream(new SectionInputStream(
161 offset,offset+filesz));
165 public class SHeader {
175 public int addralign;
178 public static final int SHT_SYMTAB = 2;
179 public static final int SHT_STRTAB = 3;
180 public static final int SHT_NOBITS = 8;
182 SHeader() throws IOException {
191 addralign = readInt();
195 public InputStream getInputStream() throws IOException {
196 return new BufferedInputStream(new SectionInputStream(
197 offset, type == SHT_NOBITS ? 0 : offset+size));
200 public boolean isText() { return name.equals(".text"); }
201 public boolean isData() { return name.equals(".data") || name.equals(".sdata") || name.equals(".rodata") || name.equals(".ctors") || name.equals(".dtors"); }
202 public boolean isBSS() { return name.equals(".bss") || name.equals(".sbss"); }
205 public ELF(String file) throws IOException, ELFException { this(new SeekableFile(file,false)); }
206 public ELF(SeekableData data) throws IOException, ELFException {
208 ident = new ELFIdent();
209 header = new ELFHeader();
210 pheaders = new PHeader[header.phnum];
211 for(int i=0;i<header.phnum;i++) {
212 data.seek(header.phoff+i*header.phentsize);
213 pheaders[i] = new PHeader();
215 sheaders = new SHeader[header.shnum];
216 for(int i=0;i<header.shnum;i++) {
217 data.seek(header.shoff+i*header.shentsize);
218 sheaders[i] = new SHeader();
220 if(header.shstrndx < 0 || header.shstrndx >= header.shnum) throw new ELFException("Bad shstrndx");
221 data.seek(sheaders[header.shstrndx].offset);
222 stringTable = new byte[sheaders[header.shstrndx].size];
223 readFully(stringTable);
225 for(int i=0;i<header.shnum;i++) {
226 SHeader s = sheaders[i];
227 s.name = getString(s.nameidx);
231 private String getString(int off) { return getString(off,stringTable); }
232 private String getString(int off,byte[] strtab) {
233 StringBuffer sb = new StringBuffer();
234 if(off < 0 || off >= strtab.length) return "<invalid strtab entry>";
235 while(off >= 0 && off < strtab.length && strtab[off] != 0) sb.append((char)strtab[off++]);
236 return sb.toString();
239 public SHeader sectionWithName(String name) {
240 for(int i=0;i<sheaders.length;i++)
241 if(sheaders[i].name.equals(name))
246 public class ELFException extends IOException { ELFException(String s) { super(s); } }
248 private class SectionInputStream extends InputStream {
251 SectionInputStream(int start, int end) throws IOException {
252 if(sectionReaderActive)
253 throw new IOException("Section reader already active");
254 sectionReaderActive = true;
260 private int bytesLeft() { return maxpos - pos; }
261 public int read() throws IOException {
262 byte[] buf = new byte[1];
263 return read(buf,0,1) == -1 ? -1 : (buf[0]&0xff);
265 public int read(byte[] b, int off, int len) throws IOException {
266 int n = data.read(b,off,Math.min(len,bytesLeft())); if(n > 0) pos += n; return n;
268 public void close() { sectionReaderActive = false; }
271 private Symtab _symtab;
272 public Symtab getSymtab() throws IOException {
273 if(_symtab != null) return _symtab;
275 if(sectionReaderActive) throw new ELFException("Can't read the symtab while a section reader is active");
277 SHeader sh = sectionWithName(".symtab");
278 if(sh == null || sh.type != SHeader.SHT_SYMTAB) return null;
280 SHeader sth = sectionWithName(".strtab");
281 if(sth == null || sth.type != SHeader.SHT_STRTAB) return null;
283 byte[] strtab = new byte[sth.size];
284 DataInputStream dis = new DataInputStream(sth.getInputStream());
285 dis.readFully(strtab);
288 return _symtab = new Symtab(sh.offset, sh.size,strtab);
291 public class Symtab {
292 public Symbol[] symbols;
294 Symtab(int off, int size, byte[] strtab) throws IOException {
297 symbols = new Symbol[count];
298 for(int i=0;i<count;i++) symbols[i] = new Symbol(strtab);
301 public Symbol getSymbol(String name) {
303 for(int i=0;i<symbols.length;i++) {
304 if(symbols[i].name.equals(name)) {
308 System.err.println("WARNING: Multiple symbol matches for " + name);
314 public Symbol getGlobalSymbol(String name) {
315 for(int i=0;i<symbols.length;i++)
316 if(symbols[i].binding == Symbol.STB_GLOBAL && symbols[i].name.equals(name))
322 public class Symbol {
330 public SHeader sheader;
332 public final static int STT_FUNC = 2;
333 public final static int STB_GLOBAL = 1;
335 Symbol(byte[] strtab) throws IOException {
336 name = getString(readInt(),strtab);
340 type = (byte)(info&0xf);
341 binding = (byte)(info>>4);
343 // FEATURE: This should point to some other entry or something
348 private static String toHex(int n) { return "0x" + Long.toString(n & 0xffffffffL, 16); }
350 public static void main(String[] args) throws IOException {
351 ELF elf = new ELF(new SeekableInputStream(new FileInputStream(args[0])));
352 System.out.println("Type: " + toHex(elf.header.type));
353 System.out.println("Machine: " + toHex(elf.header.machine));
354 System.out.println("Entry: " + toHex(elf.header.entry));
355 for(int i=0;i<elf.pheaders.length;i++) {
356 ELF.PHeader ph = elf.pheaders[i];
357 System.out.println("PHeader " + toHex(i));
358 System.out.println("\tOffset: " + ph.offset);
359 System.out.println("\tVaddr: " + toHex(ph.vaddr));
360 System.out.println("\tFile Size: " + ph.filesz);
361 System.out.println("\tMem Size: " + ph.memsz);
363 for(int i=0;i<elf.sheaders.length;i++) {
364 ELF.SHeader sh = elf.sheaders[i];
365 System.out.println("SHeader " + toHex(i));
366 System.out.println("\tName: " + sh.name);
367 System.out.println("\tOffset: " + sh.offset);
368 System.out.println("\tAddr: " + toHex(sh.addr));
369 System.out.println("\tSize: " + sh.size);
370 System.out.println("\tType: " + toHex(sh.type));
372 Symtab symtab = elf.getSymtab();
374 System.out.println("Symbol table:");
375 for(int i=0;i<symtab.symbols.length;i++)
376 System.out.println("\t" + symtab.symbols[i].name + " -> " + toHex(symtab.symbols[i].addr));
378 System.out.println("Symbol table: None");