#include "types.h" #include "stat.h" #include "param.h" #include "mmu.h" #include "proc.h" #include "defs.h" #include "x86.h" #include "traps.h" #include "syscall.h" #include "spinlock.h" #include "buf.h" #include "fs.h" #include "fsvar.h" #include "elf.h" #include "file.h" #include "fcntl.h" int exec(char *path, char **argv) { uint sz, sp, p1, p2; int i, nargs, argbytes, len, off; struct inode *ip; struct elfhdr elf; struct proghdr ph; char *mem; char *s, *last; sz = 0; mem = 0; if((ip = ilock(namei(path))) == 0) return -1; if(readi(ip, (char*)&elf, 0, sizeof(elf)) < sizeof(elf)) goto bad; if(elf.magic != ELF_MAGIC) goto bad; for(i=0, off=elf.phoff; iname, last, sizeof(cp->name)); // commit to the new image. kfree(cp->mem, cp->sz); cp->sz = sz; cp->mem = mem; mem = 0; for(i=0, off=elf.phoff; i sz) goto bad2; if(readi(ip, cp->mem + ph.va, ph.offset, ph.filesz) != ph.filesz) goto bad2; memset(cp->mem + ph.va + ph.filesz, 0, ph.memsz - ph.filesz); } iput(iunlock(ip)); cp->tf->eip = elf.entry; cp->tf->esp = sp; setupsegs(cp); return 0; bad: if(mem) kfree(mem, sz); iput(iunlock(ip)); return -1; bad2: iput(iunlock(ip)); proc_exit(); return 0; }