cleanup elf stuff
authorbrian <brian@brianweb.net>
Wed, 4 Aug 2004 11:51:58 +0000 (04:51 -0700)
committerbrian <brian@brianweb.net>
Wed, 4 Aug 2004 11:51:58 +0000 (04:51 -0700)
darcs-hash:20040804115158-24bed-0f05f0d6da5572f3b160883b143ea33fe72d0248.gz

src/org/ibex/nestedvm/Compiler.java
src/org/ibex/nestedvm/Interpreter.java
src/org/ibex/nestedvm/Runtime.java
src/org/ibex/nestedvm/util/ELF.java

index 23e25c2..03515ad 100644 (file)
@@ -194,9 +194,9 @@ public abstract class Compiler implements Registers {
         this.fullClassName = fullClassName;
         elf = new ELF(binary);
         
         this.fullClassName = fullClassName;
         elf = new ELF(binary);
         
-        if(elf.header.type != ELF.ELFHeader.ET_EXEC) throw new IOException("Binary is not an executable");
-        if(elf.header.machine != ELF.ELFHeader.EM_MIPS) throw new IOException("Binary is not for the MIPS I Architecture");
-        if(elf.ident.data != ELF.ELFIdent.ELFDATA2MSB) throw new IOException("Binary is not big endian");
+        if(elf.header.type != ELF.ET_EXEC) throw new IOException("Binary is not an executable");
+        if(elf.header.machine != ELF.EM_MIPS) throw new IOException("Binary is not for the MIPS I Architecture");
+        if(elf.ident.data != ELF.ELFDATA2MSB) throw new IOException("Binary is not big endian");
     }
 
     abstract void _go() throws Exn, IOException;
     }
 
     abstract void _go() throws Exn, IOException;
@@ -251,7 +251,7 @@ public abstract class Compiler implements Registers {
         
         for(int i=0;i<elf.sheaders.length;i++) {
             String name = elf.sheaders[i].name;
         
         for(int i=0;i<elf.sheaders.length;i++) {
             String name = elf.sheaders[i].name;
-            if(elf.sheaders[i].addr != 0 && !(
+            if((elf.sheaders[i].flags & ELF.SHF_ALLOC) !=0 && !(
                 name.equals(".text")|| name.equals(".data") || name.equals(".sdata") || name.equals(".rodata") ||
                 name.equals(".ctors") || name.equals(".dtors") || name.equals(".bss") || name.equals(".sbss")))
                     throw new Exn("Unknown section: " + name);
                 name.equals(".text")|| name.equals(".data") || name.equals(".sdata") || name.equals(".rodata") ||
                 name.equals(".ctors") || name.equals(".dtors") || name.equals(".bss") || name.equals(".sbss")))
                     throw new Exn("Unknown section: " + name);
index d0e7379..b6b3424 100644 (file)
@@ -669,9 +669,9 @@ public class Interpreter extends UnixRuntime implements Cloneable {
         ELF elf = new ELF(data);
         symtab = elf.getSymtab();
         
         ELF elf = new ELF(data);
         symtab = elf.getSymtab();
         
-        if(elf.header.type != ELF.ELFHeader.ET_EXEC) throw new IOException("Binary is not an executable");
-        if(elf.header.machine != ELF.ELFHeader.EM_MIPS) throw new IOException("Binary is not for the MIPS I Architecture");
-        if(elf.ident.data != ELF.ELFIdent.ELFDATA2MSB) throw new IOException("Binary is not big endian");
+        if(elf.header.type != ELF.ET_EXEC) throw new IOException("Binary is not an executable");
+        if(elf.header.machine != ELF.EM_MIPS) throw new IOException("Binary is not for the MIPS I Architecture");
+        if(elf.ident.data != ELF.ELFDATA2MSB) throw new IOException("Binary is not big endian");
         
         entryPoint = elf.header.entry;
         
         
         entryPoint = elf.header.entry;
         
@@ -691,7 +691,7 @@ public class Interpreter extends UnixRuntime implements Cloneable {
         int pageWords = (1<<pageShift) >> 2;
         for(int i=0;i<pheaders.length;i++) {
             ELF.PHeader ph = pheaders[i];
         int pageWords = (1<<pageShift) >> 2;
         for(int i=0;i<pheaders.length;i++) {
             ELF.PHeader ph = pheaders[i];
-            if(ph.type != ELF.PHeader.PT_LOAD) continue;
+            if(ph.type != ELF.PT_LOAD) continue;
             int memsize = ph.memsz;
             int filesize = ph.filesz;
             if(memsize == 0) continue;
             int memsize = ph.memsz;
             int filesize = ph.filesz;
             if(memsize == 0) continue;
index af84619..e909b81 100644 (file)
@@ -731,7 +731,6 @@ public abstract class Runtime implements UsermodeConstants,Registers,Cloneable {
         if((flags & ~(3|O_CREAT|O_EXCL|O_APPEND|O_TRUNC)) != 0) {
             if(STDERR_DIAG)
                 System.err.println("WARNING: Unsupported flags passed to open(\"" + f + "\"): " + toHex(flags & ~(3|O_CREAT|O_EXCL|O_APPEND|O_TRUNC)));
         if((flags & ~(3|O_CREAT|O_EXCL|O_APPEND|O_TRUNC)) != 0) {
             if(STDERR_DIAG)
                 System.err.println("WARNING: Unsupported flags passed to open(\"" + f + "\"): " + toHex(flags & ~(3|O_CREAT|O_EXCL|O_APPEND|O_TRUNC)));
-           
             throw new ErrnoException(ENOTSUP);
         }
         boolean write = (flags&3) != RD_ONLY;
             throw new ErrnoException(ENOTSUP);
         }
         boolean write = (flags&3) != RD_ONLY;
@@ -1023,7 +1022,7 @@ public abstract class Runtime implements UsermodeConstants,Registers,Cloneable {
                 return -ENOSYS;
         }
     }
                 return -ENOSYS;
         }
     }
-            
+          
     /** The syscall dispatcher.
         The should be called by subclasses when the syscall instruction is invoked.
         <i>syscall</i> should be the contents of V0 and <i>a</i>, <i>b</i>, <i>c</i>, and <i>d</i> should be 
     /** The syscall dispatcher.
         The should be called by subclasses when the syscall instruction is invoked.
         <i>syscall</i> should be the contents of V0 and <i>a</i>, <i>b</i>, <i>c</i>, and <i>d</i> should be 
index db990bb..2f97071 100644 (file)
@@ -7,6 +7,34 @@ package org.ibex.nestedvm.util;
 import java.io.*;
 
 public class ELF {
 import java.io.*;
 
 public class ELF {
+    private static final int ELF_MAGIC = 0x7f454c46; // '\177', 'E', 'L', 'F'
+
+    public static final int ELFCLASSNONE = 0;
+    public static final int ELFCLASS32 = 1;
+    public static final int ELFCLASS64 = 2;
+
+    public static final int ELFDATANONE = 0;
+    public static final int ELFDATA2LSB = 1;
+    public static final int ELFDATA2MSB = 2;     
+    
+    public static final int SHT_SYMTAB = 2;
+    public static final int SHT_STRTAB = 3;
+    public static final int SHT_NOBITS = 8;
+    
+    public static final int SHF_WRITE = 1;
+    public static final int SHF_ALLOC = 2;
+    public static final int SHF_EXECINSTR = 4;
+    
+    public static final int PF_X = 0x1;
+    public static final int PF_W = 0x2;
+    public static final int PF_R = 0x4;
+    
+    public static final int PT_LOAD = 1;
+
+    public static final short ET_EXEC = 2;
+    public static final short EM_MIPS = 8;
+
+    
     private Seekable data;
     
     public ELFIdent ident;
     private Seekable data;
     
     public ELFIdent ident;
@@ -37,7 +65,7 @@ public class ELF {
     }
     private int readInt() throws IOException {
         int x = readIntBE();
     }
     private int readInt() throws IOException {
         int x = readIntBE();
-        if(ident!=null && ident.data == ELFIdent.ELFDATA2LSB) 
+        if(ident!=null && ident.data == ELFDATA2LSB) 
             x = ((x<<24)&0xff000000) | ((x<<8)&0xff0000) | ((x>>>8)&0xff00) | ((x>>24)&0xff);
         return x;
     }
             x = ((x<<24)&0xff000000) | ((x<<8)&0xff0000) | ((x>>>8)&0xff00) | ((x>>24)&0xff);
         return x;
     }
@@ -49,7 +77,7 @@ public class ELF {
     }
     private short readShort() throws IOException {
         short x = readShortBE();
     }
     private short readShort() throws IOException {
         short x = readShortBE();
-        if(ident!=null && ident.data == ELFIdent.ELFDATA2LSB) 
+        if(ident!=null && ident.data == ELFDATA2LSB) 
             x = (short)((((x<<8)&0xff00) | ((x>>8)&0xff))&0xffff);
         return x;
     }
             x = (short)((((x<<8)&0xff00) | ((x>>8)&0xff))&0xffff);
         return x;
     }
@@ -61,17 +89,7 @@ public class ELF {
     }
         
     public class ELFIdent {
     }
         
     public class ELFIdent {
-        private static final int ELF_MAGIC = 0x7f454c46; // '\177', 'E', 'L', 'F'
-
-        public static final int ELFCLASSNONE = 0;
-        public static final int ELFCLASS32 = 1;
-        public static final int ELFCLASS64 = 2;
-        public byte klass;
-
-
-        public static final int ELFDATANONE = 0;
-        public static final int ELFDATA2LSB = 1;
-        public static final int ELFDATA2MSB = 2;        
+        public byte klass;   
         public byte data;
         public byte osabi;
         public byte abiversion;
         public byte data;
         public byte osabi;
         public byte abiversion;
@@ -93,12 +111,8 @@ public class ELF {
     }
     
     public class ELFHeader {
     }
     
     public class ELFHeader {
-        public static final short ET_EXEC = 2;
-        public short type;
-        
-        public static final short EM_MIPS = 8;
+        public short type;        
         public short machine;
         public short machine;
-        
         public int version;
         public int entry;
         public int phoff;
         public int version;
         public int entry;
         public int phoff;
@@ -139,12 +153,6 @@ public class ELF {
         public int flags;
         public int align;
         
         public int flags;
         public int align;
         
-        public static final int PF_X = 0x1;
-        public static final int PF_W = 0x2;
-        public static final int PF_R = 0x4;
-        
-        public static final int PT_LOAD = 1;
-        
         PHeader() throws IOException {
             type = readInt();
             offset = readInt();
         PHeader() throws IOException {
             type = readInt();
             offset = readInt();
@@ -178,10 +186,6 @@ public class ELF {
         public int addralign;
         public int entsize;
         
         public int addralign;
         public int entsize;
         
-        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 = readInt();
             type = readInt();
         SHeader() throws IOException {
             nameidx = readInt();
             type = readInt();
@@ -278,10 +282,10 @@ public class ELF {
         if(sectionReaderActive) throw new ELFException("Can't read the symtab while a section reader is active");
         
         SHeader sh = sectionWithName(".symtab");
         if(sectionReaderActive) throw new ELFException("Can't read the symtab while a section reader is active");
         
         SHeader sh = sectionWithName(".symtab");
-        if(sh == null || sh.type != SHeader.SHT_SYMTAB) return null;
+        if(sh == null || sh.type != SHT_SYMTAB) return null;
         
         SHeader sth = sectionWithName(".strtab");
         
         SHeader sth = sectionWithName(".strtab");
-        if(sth == null || sth.type != SHeader.SHT_STRTAB) return null;
+        if(sth == null || sth.type != SHT_STRTAB) return null;
         
         byte[] strtab = new byte[sth.size];
         DataInputStream dis = new DataInputStream(sth.getInputStream());
         
         byte[] strtab = new byte[sth.size];
         DataInputStream dis = new DataInputStream(sth.getInputStream());