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));
201 public ELF(String file) throws IOException, ELFException { this(new SeekableFile(file,false)); }
202 public ELF(SeekableData data) throws IOException, ELFException {
204 ident = new ELFIdent();
205 header = new ELFHeader();
206 pheaders = new PHeader[header.phnum];
207 for(int i=0;i<header.phnum;i++) {
208 data.seek(header.phoff+i*header.phentsize);
209 pheaders[i] = new PHeader();
211 sheaders = new SHeader[header.shnum];
212 for(int i=0;i<header.shnum;i++) {
213 data.seek(header.shoff+i*header.shentsize);
214 sheaders[i] = new SHeader();
216 if(header.shstrndx < 0 || header.shstrndx >= header.shnum) throw new ELFException("Bad shstrndx");
217 data.seek(sheaders[header.shstrndx].offset);
218 stringTable = new byte[sheaders[header.shstrndx].size];
219 readFully(stringTable);
221 for(int i=0;i<header.shnum;i++) {
222 SHeader s = sheaders[i];
223 s.name = getString(s.nameidx);
227 private String getString(int off) { return getString(off,stringTable); }
228 private String getString(int off,byte[] strtab) {
229 StringBuffer sb = new StringBuffer();
230 if(off < 0 || off >= strtab.length) return "<invalid strtab entry>";
231 while(off >= 0 && off < strtab.length && strtab[off] != 0) sb.append((char)strtab[off++]);
232 return sb.toString();
235 public SHeader sectionWithName(String name) {
236 for(int i=0;i<sheaders.length;i++)
237 if(sheaders[i].name.equals(name))
242 public class ELFException extends IOException { ELFException(String s) { super(s); } }
244 private class SectionInputStream extends InputStream {
247 SectionInputStream(int start, int end) throws IOException {
248 if(sectionReaderActive)
249 throw new IOException("Section reader already active");
250 sectionReaderActive = true;
256 private int bytesLeft() { return maxpos - pos; }
257 public int read() throws IOException {
258 byte[] buf = new byte[1];
259 return read(buf,0,1) == -1 ? -1 : (buf[0]&0xff);
261 public int read(byte[] b, int off, int len) throws IOException {
262 int n = data.read(b,off,Math.min(len,bytesLeft())); if(n > 0) pos += n; return n;
264 public void close() { sectionReaderActive = false; }
267 private Symtab _symtab;
268 public Symtab getSymtab() throws IOException {
269 if(_symtab != null) return _symtab;
271 if(sectionReaderActive) throw new ELFException("Can't read the symtab while a section reader is active");
273 SHeader sh = sectionWithName(".symtab");
274 if(sh == null || sh.type != SHeader.SHT_SYMTAB) return null;
276 SHeader sth = sectionWithName(".strtab");
277 if(sth == null || sth.type != SHeader.SHT_STRTAB) return null;
279 byte[] strtab = new byte[sth.size];
280 DataInputStream dis = new DataInputStream(sth.getInputStream());
281 dis.readFully(strtab);
284 return _symtab = new Symtab(sh.offset, sh.size,strtab);
287 public class Symtab {
288 public Symbol[] symbols;
290 Symtab(int off, int size, byte[] strtab) throws IOException {
293 symbols = new Symbol[count];
294 for(int i=0;i<count;i++) symbols[i] = new Symbol(strtab);
297 public Symbol getSymbol(String name) {
299 for(int i=0;i<symbols.length;i++) {
300 if(symbols[i].name.equals(name)) {
304 System.err.println("WARNING: Multiple symbol matches for " + name);
310 public Symbol getGlobalSymbol(String name) {
311 for(int i=0;i<symbols.length;i++)
312 if(symbols[i].binding == Symbol.STB_GLOBAL && symbols[i].name.equals(name))
318 public class Symbol {
326 public SHeader sheader;
328 public final static int STT_FUNC = 2;
329 public final static int STB_GLOBAL = 1;
331 Symbol(byte[] strtab) throws IOException {
332 name = getString(readInt(),strtab);
336 type = (byte)(info&0xf);
337 binding = (byte)(info>>4);
339 // FEATURE: This should point to some other entry or something
344 private static String toHex(int n) { return "0x" + Long.toString(n & 0xffffffffL, 16); }
346 public static void main(String[] args) throws IOException {
347 ELF elf = new ELF(new SeekableInputStream(new FileInputStream(args[0])));
348 System.out.println("Type: " + toHex(elf.header.type));
349 System.out.println("Machine: " + toHex(elf.header.machine));
350 System.out.println("Entry: " + toHex(elf.header.entry));
351 for(int i=0;i<elf.pheaders.length;i++) {
352 ELF.PHeader ph = elf.pheaders[i];
353 System.out.println("PHeader " + toHex(i));
354 System.out.println("\tOffset: " + ph.offset);
355 System.out.println("\tVaddr: " + toHex(ph.vaddr));
356 System.out.println("\tFile Size: " + ph.filesz);
357 System.out.println("\tMem Size: " + ph.memsz);
359 for(int i=0;i<elf.sheaders.length;i++) {
360 ELF.SHeader sh = elf.sheaders[i];
361 System.out.println("SHeader " + toHex(i));
362 System.out.println("\tName: " + sh.name);
363 System.out.println("\tOffset: " + sh.offset);
364 System.out.println("\tAddr: " + toHex(sh.addr));
365 System.out.println("\tSize: " + sh.size);
366 System.out.println("\tType: " + toHex(sh.type));
368 Symtab symtab = elf.getSymtab();
370 System.out.println("Symbol table:");
371 for(int i=0;i<symtab.symbols.length;i++)
372 System.out.println("\t" + symtab.symbols[i].name + " -> " + toHex(symtab.symbols[i].addr));
374 System.out.println("Symbol table: None");