X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fxwt%2Fmips%2FELF.java;h=2a9b611ba582b750f562af97e74e1be4f1d72a0c;hb=ff3815a13aa6a57852f28c789f8a8d22f2a1f26e;hp=8f2c7719e9e42198e825d40254299438eca2eba5;hpb=38786988d12f2c48a314ee37c326965ff0bcadb6;p=org.ibex.core.git diff --git a/src/org/xwt/mips/ELF.java b/src/org/xwt/mips/ELF.java index 8f2c771..2a9b611 100644 --- a/src/org/xwt/mips/ELF.java +++ b/src/org/xwt/mips/ELF.java @@ -1,7 +1,7 @@ package org.xwt.mips; import java.io.*; -class ELF { +public class ELF { private DataInput fd; private Object image; @@ -19,12 +19,10 @@ class ELF { public PHeader[] pheaders; public SHeader[] sheaders; + private byte[] stringTable; + private boolean sectionReaderActive; - private static void skipFully(DataInput fd, int n) throws IOException { - while(n>0) n -= fd.skipBytes(n); - } - public class ELFHeader { byte klass; byte data; @@ -52,10 +50,10 @@ class ELF { if(fd.readInt() != ELF_MAGIC) throw new ELFException("Bad Magic (is: " ); klass = fd.readByte(); data = fd.readByte(); - skipFully(fd, 1); // version + fd.skipBytes(1); // version osabi = fd.readByte(); abiversion = fd.readByte(); - skipFully(fd, 7); + fd.skipBytes(7); type = fd.readShort(); machine = fd.readShort(); version = fd.readInt(); @@ -103,8 +101,8 @@ class ELF { public boolean writable() { return (flags & PF_W) != 0; } public InputStream getInputStream() throws IOException { - return new BufferedInputStream(new SectionInputStream( - offset,offset+filesz)); + return new BufferedInputStream(new SectionInputStream( + offset,offset+filesz)); } } @@ -121,7 +119,9 @@ class ELF { public int addralign; public int entsize; - public static final int T_NOBITS = 8; + public static final int SHT_SYMTAB = 2; + public static final int SHT_STRTAB = 3; + public static final int SHT_NOBITS = 8; SHeader() throws IOException { nameidx = fd.readInt(); @@ -137,13 +137,17 @@ class ELF { } public InputStream getInputStream() throws IOException { - return new BufferedInputStream(new SectionInputStream( - offset, type == T_NOBITS ? 0 : offset+size)); + return new BufferedInputStream(new SectionInputStream( + offset, type == SHT_NOBITS ? 0 : offset+size)); } } public ELF(Object img) throws IOException, ELFException { - image = img; + if (img instanceof String) { + image = fd = new MyRandomAccessFile((String)img, "r"); + } else { + image = img; + } seek(0); header = new ELFHeader(); pheaders = new PHeader[header.phnum]; @@ -158,16 +162,22 @@ class ELF { } if(header.shstrndx < 0 || header.shstrndx >= header.shnum) throw new ELFException("Bad shstrndx"); seek(sheaders[header.shstrndx].offset); - byte[] a = new byte[sheaders[header.shstrndx].size]; - fd.readFully(a); + stringTable = new byte[sheaders[header.shstrndx].size]; + fd.readFully(stringTable); + for(int i=0;i= 0) pos++; return b; } public int read(byte[] b, int off, int len) throws IOException { - fd.readFully(b, off, len); - return len; + fd.readFully(b,off,Math.min(len,bytesLeft())); return len; } public void close() { sectionReaderActive = false; } } + private Symtab _symtab; + public Symtab getSymtab() throws IOException { + if(_symtab != null) return _symtab; + + SHeader sh = sectionWithName(".symtab"); + if(sh == null || sh.type != SHeader.SHT_SYMTAB) return null; + + SHeader sth = sectionWithName(".strtab"); + if(sth == null || sth.type != SHeader.SHT_STRTAB) return null; + + byte[] strtab = new byte[sth.size]; + DataInputStream dis = new DataInputStream(sth.getInputStream()); + dis.readFully(strtab); + dis.close(); + + return _symtab = new Symtab(sh.getInputStream(),sh.size,strtab); + } + + public class Symtab { + public Symbol[] symbols; + + Symtab(InputStream is, int size, byte[] strtab) throws IOException { + DataInputStream dis = new DataInputStream(is); + int count = size/16; + symbols = new Symbol[count]; + for(int i=0;i