1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
5 package org.ibex.nestedvm.util;
10 private Seekable data;
12 public ELFIdent ident;
13 public ELFHeader header;
14 public PHeader[] pheaders;
15 public SHeader[] sheaders;
17 private byte[] stringTable;
19 private boolean sectionReaderActive;
22 private void readFully(byte[] buf) throws IOException {
26 int n = data.read(buf,pos,len);
27 if(n == -1) throw new IOException("EOF");
33 private int readIntBE() throws IOException {
34 byte[] buf = new byte[4];
36 return ((buf[0]&0xff)<<24)|((buf[1]&0xff)<<16)|((buf[2]&0xff)<<8)|((buf[3]&0xff)<<0);
38 private int readInt() throws IOException {
40 if(ident!=null && ident.data == ELFIdent.ELFDATA2LSB)
41 x = ((x<<24)&0xff000000) | ((x<<8)&0xff0000) | ((x>>>8)&0xff00) | ((x>>24)&0xff);
45 private short readShortBE() throws IOException {
46 byte[] buf = new byte[2];
48 return (short)(((buf[0]&0xff)<<8)|((buf[1]&0xff)<<0));
50 private short readShort() throws IOException {
51 short x = readShortBE();
52 if(ident!=null && ident.data == ELFIdent.ELFDATA2LSB)
53 x = (short)((((x<<8)&0xff00) | ((x>>8)&0xff))&0xffff);
57 private byte readByte() throws IOException {
58 byte[] buf = new byte[1];
63 public class ELFIdent {
64 private static final int ELF_MAGIC = 0x7f454c46; // '\177', 'E', 'L', 'F'
66 public static final int ELFCLASSNONE = 0;
67 public static final int ELFCLASS32 = 1;
68 public static final int ELFCLASS64 = 2;
72 public static final int ELFDATANONE = 0;
73 public static final int ELFDATA2LSB = 1;
74 public static final int ELFDATA2MSB = 2;
77 public byte abiversion;
79 ELFIdent() throws IOException {
80 if(readIntBE() != ELF_MAGIC) throw new ELFException("Bad Magic");
83 if(klass != ELFCLASS32) throw new ELFException("org.ibex.nestedvm.util.ELF does not suport 64-bit binaries");
86 if(data != ELFDATA2LSB && data != ELFDATA2MSB) throw new ELFException("Unknown byte order");
88 readByte(); // version
90 abiversion = readByte();
91 for(int i=0;i<7;i++) readByte(); // padding
95 public class ELFHeader {
96 public static final short ET_EXEC = 2;
99 public static final short EM_MIPS = 8;
100 public short machine;
108 public short phentsize;
110 public short shentsize;
112 public short shstrndx;
114 ELFHeader() throws IOException {
116 machine = readShort();
118 if(version != 1) throw new ELFException("version != 1");
123 ehsize = readShort();
124 phentsize = readShort();
126 shentsize = readShort();
128 shstrndx = readShort();
132 public class PHeader {
142 public static final int PF_X = 0x1;
143 public static final int PF_W = 0x2;
144 public static final int PF_R = 0x4;
146 public static final int PT_LOAD = 1;
148 PHeader() throws IOException {
157 if(filesz > memsz) throw new ELFException("ELF inconsistency: filesz > memsz (" + toHex(filesz) + " > " + toHex(memsz) + ")");
160 public boolean writable() { return (flags & PF_W) != 0; }
162 public InputStream getInputStream() throws IOException {
163 return new BufferedInputStream(new SectionInputStream(
164 offset,offset+filesz));
168 public class SHeader {
178 public int addralign;
181 public static final int SHT_SYMTAB = 2;
182 public static final int SHT_STRTAB = 3;
183 public static final int SHT_NOBITS = 8;
185 SHeader() throws IOException {
194 addralign = readInt();
198 public InputStream getInputStream() throws IOException {
199 return new BufferedInputStream(new SectionInputStream(
200 offset, type == SHT_NOBITS ? 0 : offset+size));
203 public boolean isText() { return name.equals(".text"); }
204 public boolean isData() { return name.equals(".data") || name.equals(".sdata") || name.equals(".rodata") || name.equals(".ctors") || name.equals(".dtors"); }
205 public boolean isBSS() { return name.equals(".bss") || name.equals(".sbss"); }
208 public ELF(String file) throws IOException, ELFException { this(new Seekable.File(file,false)); }
209 public ELF(Seekable data) throws IOException, ELFException {
211 ident = new ELFIdent();
212 header = new ELFHeader();
213 pheaders = new PHeader[header.phnum];
214 for(int i=0;i<header.phnum;i++) {
215 data.seek(header.phoff+i*header.phentsize);
216 pheaders[i] = new PHeader();
218 sheaders = new SHeader[header.shnum];
219 for(int i=0;i<header.shnum;i++) {
220 data.seek(header.shoff+i*header.shentsize);
221 sheaders[i] = new SHeader();
223 if(header.shstrndx < 0 || header.shstrndx >= header.shnum) throw new ELFException("Bad shstrndx");
224 data.seek(sheaders[header.shstrndx].offset);
225 stringTable = new byte[sheaders[header.shstrndx].size];
226 readFully(stringTable);
228 for(int i=0;i<header.shnum;i++) {
229 SHeader s = sheaders[i];
230 s.name = getString(s.nameidx);
234 private String getString(int off) { return getString(off,stringTable); }
235 private String getString(int off,byte[] strtab) {
236 StringBuffer sb = new StringBuffer();
237 if(off < 0 || off >= strtab.length) return "<invalid strtab entry>";
238 while(off >= 0 && off < strtab.length && strtab[off] != 0) sb.append((char)strtab[off++]);
239 return sb.toString();
242 public SHeader sectionWithName(String name) {
243 for(int i=0;i<sheaders.length;i++)
244 if(sheaders[i].name.equals(name))
249 public class ELFException extends IOException { ELFException(String s) { super(s); } }
251 private class SectionInputStream extends InputStream {
254 SectionInputStream(int start, int end) throws IOException {
255 if(sectionReaderActive)
256 throw new IOException("Section reader already active");
257 sectionReaderActive = true;
263 private int bytesLeft() { return maxpos - pos; }
264 public int read() throws IOException {
265 byte[] buf = new byte[1];
266 return read(buf,0,1) == -1 ? -1 : (buf[0]&0xff);
268 public int read(byte[] b, int off, int len) throws IOException {
269 int n = data.read(b,off,Math.min(len,bytesLeft())); if(n > 0) pos += n; return n;
271 public void close() { sectionReaderActive = false; }
274 private Symtab _symtab;
275 public Symtab getSymtab() throws IOException {
276 if(_symtab != null) return _symtab;
278 if(sectionReaderActive) throw new ELFException("Can't read the symtab while a section reader is active");
280 SHeader sh = sectionWithName(".symtab");
281 if(sh == null || sh.type != SHeader.SHT_SYMTAB) return null;
283 SHeader sth = sectionWithName(".strtab");
284 if(sth == null || sth.type != SHeader.SHT_STRTAB) return null;
286 byte[] strtab = new byte[sth.size];
287 DataInputStream dis = new DataInputStream(sth.getInputStream());
288 dis.readFully(strtab);
291 return _symtab = new Symtab(sh.offset, sh.size,strtab);
294 public class Symtab {
295 public Symbol[] symbols;
297 Symtab(int off, int size, byte[] strtab) throws IOException {
300 symbols = new Symbol[count];
301 for(int i=0;i<count;i++) symbols[i] = new Symbol(strtab);
304 public Symbol getSymbol(String name) {
306 for(int i=0;i<symbols.length;i++) {
307 if(symbols[i].name.equals(name)) {
311 System.err.println("WARNING: Multiple symbol matches for " + name);
317 public Symbol getGlobalSymbol(String name) {
318 for(int i=0;i<symbols.length;i++)
319 if(symbols[i].binding == Symbol.STB_GLOBAL && symbols[i].name.equals(name))
325 public class Symbol {
334 public SHeader sheader;
336 public final static int STT_FUNC = 2;
337 public final static int STB_GLOBAL = 1;
339 Symbol(byte[] strtab) throws IOException {
340 name = getString(readInt(),strtab);
344 type = (byte)(info&0xf);
345 binding = (byte)(info>>4);
351 private static String toHex(int n) { return "0x" + Long.toString(n & 0xffffffffL, 16); }
353 /*public static void main(String[] args) throws IOException {
354 ELF elf = new ELF(new Seekable.InputStream(new FileInputStream(args[0])));
355 System.out.println("Type: " + toHex(elf.header.type));
356 System.out.println("Machine: " + toHex(elf.header.machine));
357 System.out.println("Entry: " + toHex(elf.header.entry));
358 for(int i=0;i<elf.pheaders.length;i++) {
359 ELF.PHeader ph = elf.pheaders[i];
360 System.out.println("PHeader " + toHex(i));
361 System.out.println("\tOffset: " + ph.offset);
362 System.out.println("\tVaddr: " + toHex(ph.vaddr));
363 System.out.println("\tFile Size: " + ph.filesz);
364 System.out.println("\tMem Size: " + ph.memsz);
366 for(int i=0;i<elf.sheaders.length;i++) {
367 ELF.SHeader sh = elf.sheaders[i];
368 System.out.println("SHeader " + toHex(i));
369 System.out.println("\tName: " + sh.name);
370 System.out.println("\tOffset: " + sh.offset);
371 System.out.println("\tAddr: " + toHex(sh.addr));
372 System.out.println("\tSize: " + sh.size);
373 System.out.println("\tType: " + toHex(sh.type));
375 Symtab symtab = elf.getSymtab();
377 System.out.println("Symbol table:");
378 for(int i=0;i<symtab.symbols.length;i++)
379 System.out.println("\t" + symtab.symbols[i].name + " -> " + toHex(symtab.symbols[i].addr));
381 System.out.println("Symbol table: None");