Browse Source

Adding ELF Magic, second iteration. Error in sysproc line 400 :(

Elbing Miss 9 years ago
parent
commit
606449faa4
4 changed files with 45 additions and 17 deletions
  1. 0 1
      sys/include/a.out.h
  2. 20 7
      sys/src/9/port/kexec.c
  3. 23 7
      sys/src/9/port/sysproc.c
  4. 2 2
      sys/src/libmach/executable.c

+ 0 - 1
sys/include/a.out.h

@@ -40,7 +40,6 @@ struct	Exec
 #define	S_MAGIC		_MAGIC(HDR_MAGIC, 26)	/* amd64 */
 #define	T_MAGIC		_MAGIC(HDR_MAGIC, 27)	/* powerpc64 */
 #define	R_MAGIC		_MAGIC(HDR_MAGIC, 28)	/* arm64 */
-#define	ELF_MAG		((0x7f<<24) | ('E'<<16) | ('L'<<8) | 'F') /* amd64 ELF */
 
 #define	MIN_MAGIC	8
 #define	MAX_MAGIC	28			/* <= 90 */

+ 20 - 7
sys/src/9/port/kexec.c

@@ -17,6 +17,7 @@
 
 #include	"../port/edf.h"
 #include	<a.out.h>
+#include	"elf.h"
 #include "kexec.h"
 
 
@@ -45,10 +46,23 @@ l2be(int32_t l)
 	return (cp[0]<<24) | (cp[1]<<16) | (cp[2]<<8) | cp[3];
 }
 
+/*
 typedef struct {
 	Exec;
 	uint64_t hdr[1];
 } Khdr;
+*/
+
+typedef struct {
+        union{
+                struct {
+                        Exec;           /* a.out.h */
+                        uint64_t hdr[1];
+                };
+                E64hdr;			/* elf */
+        } e;
+        int32_t dummy;                  /* padding to ensure extra long */
+} Khdr;
 
 enum {
 	AsmNONE		= 0,
@@ -205,23 +219,23 @@ kforkexecac(Proc *p, int core, char *ufile, char **argv)
 	}
 
 //	p = (char*)&hdr;
-	magic = l2be(hdr.magic);
+	magic = l2be(hdr.e.magic);
 	DBG("badexec3\n");
 	
 	if(hdrsz != sizeof(Khdr) || magic != AOUT_MAGIC || magic != ELF_MAGIC)
 		error(Ebadexec);
 	if(magic & HDR_MAGIC){
-		entry = vl2be(hdr.hdr[0]);
+		entry = vl2be(hdr.e.hdr[0]);
 		hdrsz = sizeof(Khdr);
 	}
 	else{
-		entry = l2be(hdr.entry);
+		entry = l2be(hdr.e.entry);
 		hdrsz = sizeof(Exec);
 	}
 
-	textsz = l2be(hdr.text);
-	datasz = l2be(hdr.data);
-	bsssz = l2be(hdr.bss);
+	textsz = l2be(hdr.e.text);
+	datasz = l2be(hdr.e.data);
+	bsssz = l2be(hdr.e.bss);
 
 	tbase = p->seg[TSEG]->base;
 	tsize = tbase - p->seg[TSEG]->top;
@@ -255,7 +269,6 @@ kforkexecac(Proc *p, int core, char *ufile, char **argv)
 	DBG("kexec: testing if sizes overflow limits\n");	
 	if(textsz >= textlim || datasz > datalim || bsssz > bsslim)
 		error(Ebadexec);
-
 	DBG("kexec: do the top of the segments overflow limits?\n");	
 	if(textlim >= tbase+tsize || datalim >= dbase+dsize || bsslim >= bbase+bsize)
 		error(Ebadexec);

+ 23 - 7
sys/src/9/port/sysproc.c

@@ -18,6 +18,7 @@
 #include	"../port/edf.h"
 #include	<a.out.h>
 #include 	<trace.h>
+#include	"elf.h"
 
 
 void
@@ -274,10 +275,23 @@ l2be(int32_t l)
 	return (cp[0]<<24) | (cp[1]<<16) | (cp[2]<<8) | cp[3];
 }
 
+/*
 typedef struct {
 	Exec;
 	uint64_t hdr[1];
 } Hdr;
+*/
+
+typedef struct {
+        union{
+                struct {
+                        Exec;           /* a.out.h */
+                        uint64_t hdr[1];
+                };
+                E64hdr;                 /* elf */
+        } e;
+        int32_t dummy;                  /* padding to ensure extra long */
+} Hdr;
 
 /*
  * flags can ONLY specify that you want an AC for you, or
@@ -388,21 +402,23 @@ execac(Ar0* ar0, int flags, char *ufile, char **argv)
 	/*
 	 * #! has had its chance, now we need a real binary.
 	 */
-	magic = l2be(hdr.magic);
-	if(hdrsz != sizeof(Hdr) || magic != AOUT_MAGIC || magic != ELF_MAGIC)
+	magic = l2be(hdr.e.magic);
+	if(hdrsz != sizeof(Hdr) || magic != AOUT_MAGIC || magic != ELF_MAGIC) {
+		iprint("sysproc 414: bad exechdr\n");
 		error(Ebadexec);
+	}
 	if(magic & HDR_MAGIC){
-		entry = vl2be(hdr.hdr[0]);
+		entry = vl2be(hdr.e.hdr[0]);
 		hdrsz = sizeof(Hdr);
 	}
 	else{
-		entry = l2be(hdr.entry);
+		entry = l2be(hdr.e.entry);
 		hdrsz = sizeof(Exec);
 	}
 
-	textsz = l2be(hdr.text);
-	datasz = l2be(hdr.data);
-	bsssz = l2be(hdr.bss);
+	textsz = l2be(hdr.e.text);
+	datasz = l2be(hdr.e.data);
+	bsssz = l2be(hdr.e.bss);
 
 	textlim = UTROUND(UTZERO+hdrsz+textsz);
 	datalim = BIGPGROUND(textlim+datasz);

+ 2 - 2
sys/src/libmach/executable.c

@@ -23,7 +23,7 @@ typedef struct {
 	union{
 		struct {
 			Exec;		/* a.out.h */
-			unsigned long long hdr[1];
+			uint64_t hdr[1];
 		};
 #ifdef HARVEY32
 		Ehdr;			/* elf.h */
@@ -34,7 +34,7 @@ typedef struct {
 		struct sparcexec;	/* bootexec.h */
 		struct nextexec;	/* bootexec.h */
 	} e;
-	long dummy;			/* padding to ensure extra long */
+	int32_t dummy;			/* padding to ensure extra long */
 } ExecHdr;
 
 #ifdef HARVEYNEXT