diff --git a/.cproject b/.cproject index 697a696..04b8d6f 100644 --- a/.cproject +++ b/.cproject @@ -24,13 +24,13 @@ - - diff --git a/.settings/language.settings.xml b/.settings/language.settings.xml index bfaac99..207fcc1 100644 --- a/.settings/language.settings.xml +++ b/.settings/language.settings.xml @@ -5,7 +5,7 @@ - + diff --git a/sys/i386/i386_exec.c b/sys/i386/i386_exec.c index 176e41e..2bc2613 100644 --- a/sys/i386/i386_exec.c +++ b/sys/i386/i386_exec.c @@ -91,7 +91,7 @@ Elf32_Addr pcpu_stop; /* Pre-relocation pcpu set stop. */ Elf32_Addr pcpu_base; /* Relocated pcpu set address. */ Elf32_Addr ld_addr; // Entry Point Of Linker (Load It Too) -}*elf_file_t; +} *elf_file_t; static int elf_parse_dynamic(elf_file_t ef); @@ -106,12 +106,14 @@ so do not use out side of kernel space *****************************************************************************************/ -uInt32 execThread(void (*tproc)(void), uint32_t stack, char *arg) { +uint32_t execThread(void (*tproc)(void), uint32_t stack, char *arg) { + kTask_t * newProcess = 0x0; + /* Find A New Thread */ newProcess = schedNewTask(); - assert(newProcess); + if (stack < 0x100000) kpanic("exec: stack not in valid area: [0x%X]\n", stack); @@ -131,7 +133,7 @@ newProcess->tss.esi = 0x0; newProcess->tss.edi = 0x0; - /* Set these up to be ring 3 tasks */ + /* Ring 3 Selectors */ /* newProcess->tss.es = 0x30+3; newProcess->tss.cs = 0x28+3; @@ -141,6 +143,7 @@ newProcess->tss.gs = 0x30+3; */ + /* Ring 0 Selectors */ newProcess->tss.es = 0x10; newProcess->tss.cs = 0x08; newProcess->tss.ss = 0x10; @@ -157,26 +160,26 @@ /* Set up default stack for thread here filled with arg list 3 times */ asm volatile( - "pusha \n" - "movl %%esp,%%ecx \n" - "movl %1,%%eax \n" - "movl %%eax,%%esp \n" - "pushl %%ebx \n" - "pushl %%ebx \n" - "pushl %%ebx \n" - "movl %%esp,%%eax \n" - "movl %%eax,%1 \n" - "movl %%ecx,%%esp \n" - "popa \n" - : - : "b" (arg),"m" (newProcess->tss.esp) + "pusha \n" + "movl %%esp,%%ecx \n" + "movl %1,%%eax \n" + "movl %%eax,%%esp \n" + "pushl %%ebx \n" + "pushl %%ebx \n" + "pushl %%ebx \n" + "movl %%esp,%%eax \n" + "movl %%eax,%1 \n" + "movl %%ecx,%%esp \n" + "popa \n" + : + : "b" (arg),"m" (newProcess->tss.esp) ); /* Put new thread into the READY state */ sched_setStatus(newProcess->id, READY); /* Return with the new process ID */ - return ((uInt32) newProcess); + return ((uint32_t) newProcess); } /***************************************************************************************** @@ -197,110 +200,118 @@ *****************************************************************************************/ void execFile(char *file, int argc, char **argv, int console) { + kTask_t newProcess = 0x0; + int i = 0x0; int x = 0x0; + uint32_t *tmp = 0x0; - fileDescriptor *tmpFd = 0x0; Elf32_Ehdr *binaryHeader = 0x0; - elfProgramHeader *programHeader = 0x0; + + Elf32_Phdr *programHeader = 0x0; /* Get A New Task For This Proccess */ - _current = schedNewTask(); - assert(_current); + newProcess = schedNewTask(); + assert(newProcess); - _current->gid = 0x0; - _current->uid = 0x0; - _current->term = tty_find(console); + newProcess->gid = 0x0; + newProcess->uid = 0x0; + newProcess->term = tty_find(console); - if (_current->term == 0x0) + if (newProcess->term == 0x0) kprintf("Error: invalid console\n"); /* Set tty ownership */ - _current->term->owner = _current->id; + newProcess->term->owner = newProcess->id; /* Now We Must Create A Virtual Space For This Proccess To Run In */ - _current->tss.cr3 = (uInt32) vmm_createVirtualSpace(_current->id); - kprintf("_current->tss.cr3: 0x%X", _current->tss.cr3); + newProcess->tss.cr3 = (uint32_t) vmm_createVirtualSpace(newProcess->id); + + /* To Better Load This Application We Will Switch Over To Its VM Space */ asm volatile( - "movl %0,%%eax \n" - "movl %%eax,%%cr3 \n" - : : "d" ((uInt32 *)(_current->tss.cr3)) + "movl %0,%%eax \n" + "movl %%eax,%%cr3 \n" + : : "d" ((uint32_t *)(newProcess->tss.cr3)) ); + + /* Lets Find The File */ - tmpFd = fopen(file, "r"); + newProcess->imageFd = fopen(file, "r"); /* If We Dont Find the File Return */ - if (tmpFd == 0x0) { + if (newProcess->imageFd == 0x0) { kprintf("Exec Format Error: Binary File Not Executable.\n"); - fclose(tmpFd); + fclose(newProcess->imageFd); return; } - if (tmpFd->perms == 0x0) { + + if (newProcess->imageFd->perms == 0x0) { kprintf("Exec Format Error: Binary File Not Executable.\n"); - fclose(tmpFd); + fclose(newProcess->imageFd); return; } /* Load ELF Header */ binaryHeader = (Elf32_Ehdr *) kmalloc(sizeof(Elf32_Ehdr)); - //kprintf(">a:%i:0x%X:0x%X<",sizeof(Elf32_Ehdr),binaryHeader,tmpFd); - fread(binaryHeader, sizeof(Elf32_Ehdr), 1, tmpFd); + fread(binaryHeader, sizeof(Elf32_Ehdr), 1, newProcess->imageFd); /* Check If App Is A Real Application */ if ((binaryHeader->e_ident[1] != 'E') && (binaryHeader->e_ident[2] != 'L') && (binaryHeader->e_ident[3] != 'F')) { kprintf("Exec Format Error: Binary File Not Executable.\n"); kfree(binaryHeader); - fclose(tmpFd); + fclose(newProcess->imageFd); return; - } else if (binaryHeader->e_type != 2) { + } + else if (binaryHeader->e_type != 2) { kprintf("Exec Format Error: Binary File Not Executable.\n"); kfree(binaryHeader); - fclose(tmpFd); + fclose(newProcess->imageFd); return; - } else if (binaryHeader->e_entry == 0x300000) { + } + else if (binaryHeader->e_entry == 0x300000) { kprintf("Exec Format Error: Binary File Not Executable.\n"); kfree(binaryHeader); - fclose(tmpFd); + fclose(newProcess->imageFd); return; } - _current->td.abi = binaryHeader->e_ident[EI_OSABI]; + newProcess->td.abi = binaryHeader->e_ident[EI_OSABI]; /* Load The Program Header(s) */ programHeader = (elfProgramHeader *) kmalloc(sizeof(elfProgramHeader) * binaryHeader->e_phnum); - fseek(tmpFd, binaryHeader->e_phoff, 0); + fseek(newProcess->imageFd, binaryHeader->e_phoff, 0); //kprintf(">c:%i:0x%X:0x%X<",sizeof(elfProgramHeader)*binaryHeader->e_phnum,programHeader,tmpFd); - fread(programHeader, (sizeof(elfProgramHeader) * binaryHeader->e_phnum), 1, tmpFd); + fread(programHeader, (sizeof(elfProgramHeader) * binaryHeader->e_phnum), 1, newProcess->imageFd); //kprintf(">d<"); /* Loop Through The Header And Load Sections Which Need To Be Loaded */ for (i = 0; i < binaryHeader->e_phnum; i++) { - if (programHeader[i].phType == 1) { + if (programHeader[i].p_type == 1) { /* Allocate Memory Im Going To Have To Make This Load Memory With Correct Settings so it helps us in the future */ - for (x = 0x0; x < (programHeader[i].phMemsz); x += 0x1000) { + for (x = 0x0; x < (programHeader[i].p_memsz); x += 0x1000) { /* Make readonly and read/write !!! */ - if (vmm_remapPage(vmm_findFreePage(_current->id), ((programHeader[i].phVaddr & 0xFFFFF000) + x), PAGE_DEFAULT) == 0x0) + if (vmm_remapPage(vmm_findFreePage(newProcess->id), ((programHeader[i].p_vaddr & 0xFFFFF000) + x), PAGE_DEFAULT) == 0x0) K_PANIC("Remap Page Failed"); - memset((void *) ((programHeader[i].phVaddr & 0xFFFFF000) + x), 0x0, 0x1000); + memset((void *) ((programHeader[i].p_vaddr & 0xFFFFF000) + x), 0x0, 0x1000); } - _current->oInfo.vmStart = 0x80000000; - _current->td.vm_daddr = (u_long) (programHeader[i].phVaddr & 0xFFFFF000); + /* Now Load Section To Memory */ - fseek(tmpFd, programHeader[i].phOffset, 0); - fread((void *) programHeader[i].phVaddr, programHeader[i].phFilesz, 1, tmpFd); - if ((programHeader[i].phFlags & 0x2) != 0x2) { - kprintf("pH: [0x%X]\n", programHeader[i].phMemsz); - for (x = 0x0; x < (programHeader[i].phMemsz); x += 0x1000) { - if ((vmm_setPageAttributes((programHeader[i].phVaddr & 0xFFFFF000) + x, PAGE_PRESENT | PAGE_USER)) != 0x0) + fseek(newProcess->imageFd, programHeader[i].p_offset, 0); + + fread((void *) programHeader[i].p_vaddr, programHeader[i].p_filesz, 1, newProcess->imageFd); + + if ((programHeader[i].p_flags & 0x2) != 0x2) { + for (x = 0x0; x < (programHeader[i].p_memsz); x += 0x1000) { + if ((vmm_setPageAttributes((programHeader[i].p_vaddr & 0xFFFFF000) + x, PAGE_PRESENT | PAGE_USER)) != 0x0) kpanic("Error: vmm_setPageAttributes failed, File: %s, Line: %i\n", __FILE__, __LINE__); } } @@ -308,57 +319,58 @@ } /* Set Virtual Memory Start */ - _current->oInfo.vmStart = 0x80000000; - _current->td.vm_daddr = (u_long) (programHeader[i].phVaddr & 0xFFFFF000); + newProcess->oInfo.vmStart = 0x80000000; + newProcess->td.vm_daddr = (u_long) (programHeader[i].p_vaddr & 0xFFFFF000); /* Set Up Stack Space */ //MrOlsen (2016-01-14) FIX: is the stack start supposed to be addressable xhcnage x= 1 to x=0 for (x = 0; x < 100; x++) { - vmm_remapPage(vmm_findFreePage(_current->id), STACK_ADDR - (x * 0x1000), PAGE_DEFAULT | PAGE_STACK); + vmm_remapPage(vmm_findFreePage(newProcess->id), STACK_ADDR - (x * 0x1000), PAGE_DEFAULT | PAGE_STACK); } /* Kernel Stack 0x2000 bytes long */ - vmm_remapPage(vmm_findFreePage(_current->id), 0x5BC000, KERNEL_PAGE_DEFAULT | PAGE_STACK); - vmm_remapPage(vmm_findFreePage(_current->id), 0x5BB000, KERNEL_PAGE_DEFAULT | PAGE_STACK); + vmm_remapPage(vmm_findFreePage(newProcess->id), 0x5BC000, KERNEL_PAGE_DEFAULT | PAGE_STACK); + vmm_remapPage(vmm_findFreePage(newProcess->id), 0x5BB000, KERNEL_PAGE_DEFAULT | PAGE_STACK); /* Set All The Proper Information For The Task */ - _current->tss.back_link = 0x0; - _current->tss.esp0 = 0x5BC000; - _current->tss.ss0 = 0x10; - _current->tss.esp1 = 0x0; - _current->tss.ss1 = 0x0; - _current->tss.esp2 = 0x0; - _current->tss.ss2 = 0x0; - _current->tss.eip = (long) binaryHeader->e_entry; - _current->tss.eflags = 0x206; - _current->tss.esp = STACK_ADDR - 16; - _current->tss.ebp = STACK_ADDR; - _current->tss.esi = 0x0; - _current->tss.edi = 0x0; + newProcess->tss.back_link = 0x0; + newProcess->tss.esp0 = 0x5BC000; + newProcess->tss.ss0 = 0x10; + newProcess->tss.esp1 = 0x0; + newProcess->tss.ss1 = 0x0; + newProcess->tss.esp2 = 0x0; + newProcess->tss.ss2 = 0x0; + newProcess->tss.eip = (long) binaryHeader->e_entry; + newProcess->tss.eflags = 0x206; + newProcess->tss.esp = STACK_ADDR - 16; + newProcess->tss.ebp = STACK_ADDR; + newProcess->tss.esi = 0x0; + newProcess->tss.edi = 0x0; /* Set these up to be ring 3 tasks */ - _current->tss.es = 0x30 + 3; - _current->tss.cs = 0x28 + 3; - _current->tss.ss = 0x30 + 3; - _current->tss.ds = 0x30 + 3; - _current->tss.fs = 0x30 + 3; - _current->tss.gs = 0x50 + 3; //0x30 + 3; + newProcess->tss.es = 0x30 + 3; + newProcess->tss.cs = 0x28 + 3; + newProcess->tss.ss = 0x30 + 3; + newProcess->tss.ds = 0x30 + 3; + newProcess->tss.fs = 0x30 + 3; + newProcess->tss.gs = 0x50 + 3; //0x30 + 3; - _current->tss.ldt = 0x18; - _current->tss.trace_bitmap = 0x0000; - _current->tss.io_map = 0x8000; + newProcess->tss.ldt = 0x18; + newProcess->tss.trace_bitmap = 0x0000; + newProcess->tss.io_map = 0x8000; - sched_setStatus(_current->id, READY); + sched_setStatus(newProcess->id, READY); kfree(binaryHeader); kfree(programHeader); - fclose(tmpFd); + fclose(newProcess->imageFd); - tmp = (uInt32 *) _current->tss.esp0 - 5; + tmp = (uInt32 *) newProcess->tss.esp0 - 5; + tmp[0] = binaryHeader->e_entry; tmp[3] = STACK_ADDR - 12; - tmp = (uint32_t *) _current->tss.esp; + tmp = (uint32_t *) newProcess->tss.esp; kprintf("argv: [0x%X]\n", argv); //*tmp++ = 0x0; // Stack EIP Return Addr @@ -376,33 +388,27 @@ /* Switch Back To The Kernels VM Space */ asm volatile( - "movl %0,%%eax \n" - "movl %%eax,%%cr3 \n" - : : "d" ((uInt32 *)(kernelPageDirectory)) + "movl %0,%%eax \n" + "movl %%eax,%%cr3 \n" + : : "d" ((uint32_t *)(kernelPageDirectory)) ); - kprintf("execFile Return: %i\n", _current->id); + kprintf("execFile Return: %i\n", newProcess->id); + + /* Put new thread into the READY state */ + sched_setStatus(newProcess->id, READY); + /* Finally Return */ return; } -/***************************************************************************************** - - Function: void sysExec(); - Description: This Is The System Call To Execute A New Task - - Notes: - - 2016-01-19 - Start Of Enhanced Exec - - 04-22-03 - It Now Loads Sections Not The Full File - - *****************************************************************************************/ int sys_exec(struct thread *td, char *file, char **argv, char **envp) { + int i = 0x0; int x = 0x0; int argc = 0x0; + uint32_t cr3 = 0x0; unsigned int *tmp = 0x0; @@ -428,13 +434,10 @@ struct i386_frame *iFrame = 0x0; - //Elf_Auxargs *auxargs = 0x0; - asm("movl %%cr3, %0;" : "=r" (cr3)); fd = fopen(file, "r"); - /* If the file doesn't exist fail */ if (fd == 0x0) return (-1); @@ -466,12 +469,14 @@ kfree(binaryHeader); fclose(fd); return (-1); - } else if (binaryHeader->e_type != ET_EXEC) { + } + else if (binaryHeader->e_type != ET_EXEC) { kprintf("Exec Format Error: Binary File Not Executable.\n"); kfree(binaryHeader); fclose(fd); return (-1); - } else if (binaryHeader->e_entry == 0x300000) { + } + else if (binaryHeader->e_entry == 0x300000) { kprintf("Exec Format Error: Binary File Not Executable.\n"); kfree(binaryHeader); fclose(fd); @@ -544,7 +549,8 @@ kprintf("setting text: 0x%X - 0x%X\n", seg_addr, seg_size); text_size = seg_size; text_addr = seg_addr; - } else { + } + else { kprintf("setting data: 0x%X - 0x%X\n", seg_addr, seg_size); data_size = seg_size; data_addr = seg_addr; @@ -560,14 +566,14 @@ * Thjis is for stack space */ _current->oInfo.vmStart = ((programHeader[i].p_vaddr & 0xFFFFF000) + 0xA900000); - break; + break; case PT_DYNAMIC: //newLoc = (char *)programHeader[i].phVaddr; //elfDynamicS = (elfDynamic *) programHeader[i].p_vaddr; ef->dynamic = (Elf32_Dyn *) programHeader[i].p_vaddr; //fseek( fd, programHeader[i].phOffset, 0 ); //fread( (void *) programHeader[i].phVaddr, programHeader[i].phFilesz, 1, fd ); - break; + break; case PT_INTERP: kprintf("Malloc: %i\n", programHeader[i].p_filesz); interp = (char *) kmalloc(programHeader[i].p_filesz); @@ -576,11 +582,11 @@ kprintf("Interp: [%s]\n", interp); //ldAddr = ldEnable(); ef->ld_addr = ldEnable(); - break; + break; case PT_GNU_STACK: - break; + break; default: - break; + break; } } @@ -618,7 +624,8 @@ //x++; } argv = argvNew; - } else { + } + else { argc = 1; } @@ -720,13 +727,13 @@ switch (dynp->d_tag) { case DT_NEEDED: asm("nop"); - break; + break; case DT_INIT: asm("nop"); - break; + break; case DT_FINI: asm("nop"); - break; + break; case DT_HASH: asm("nop"); /* From src/libexec/rtld-elf/rtld.c */ @@ -735,51 +742,51 @@ ef->nchains = hashtab[1]; ef->buckets = hashtab + 2; ef->chains = ef->buckets + ef->nbuckets; - break; + break; case DT_STRTAB: ef->strtab = (caddr_t) (ef->address + dynp->d_un.d_ptr); - break; + break; case DT_STRSZ: ef->strsz = dynp->d_un.d_val; - break; + break; case DT_SYMTAB: ef->symtab = (Elf32_Sym *) (ef->address + dynp->d_un.d_ptr); - break; + break; case DT_SYMENT: if (dynp->d_un.d_val != sizeof(Elf32_Sym)) return (ENOEXEC); - break; + break; case DT_REL: ef->rel = (const Elf32_Rel *) (ef->address + dynp->d_un.d_ptr); - break; + break; case DT_RELSZ: ef->relsize = dynp->d_un.d_val; - break; + break; case DT_RELENT: if (dynp->d_un.d_val != sizeof(Elf32_Rel)) return (ENOEXEC); - break; + break; case DT_JMPREL: ef->pltrel = (const Elf32_Rel *) (ef->address + dynp->d_un.d_ptr); - break; + break; case DT_PLTRELSZ: ef->pltrelsize = dynp->d_un.d_val; - break; + break; case DT_RELA: ef->rela = (const Elf32_Rela *) (ef->address + dynp->d_un.d_ptr); - break; + break; case DT_RELASZ: ef->relasize = dynp->d_un.d_val; - break; + break; case DT_RELAENT: if (dynp->d_un.d_val != sizeof(Elf32_Rela)) return (ENOEXEC); - break; + break; case DT_PLTREL: plttype = dynp->d_un.d_val; if (plttype != DT_REL && plttype != DT_RELA) return (ENOEXEC); - break; + break; case DT_PLTGOT: ef->got = (Elf32_Addr *) (ef->address + dynp->d_un.d_ptr); tmp = (void *) dynp->d_un.d_ptr; //elfDynamicS[i].dynPtr; @@ -789,10 +796,10 @@ kprintf("[0x%X]", tmp); tmp[2] = (uInt32) ef->ld_addr; tmp[1] = (uInt32) ef; - break; + break; default: kprintf("t_tag: 0x%X>", dynp->d_tag); - break; + break; } } ef->pltrela = (const Elf32_Rela *) ef->pltrel; diff --git a/sys/include/i386/elf.h b/sys/include/i386/elf.h index 0f92ee6..be6287c 100644 --- a/sys/include/i386/elf.h +++ b/sys/include/i386/elf.h @@ -1,72 +1,118 @@ #ifndef _I386_ELF_H_ -#define _i386_ELF_H_ 1 +#define _I386_ELF_H_ 1 -#include +#if defined(__i386__) || defined(_MACHINE_ELF_WANT_32BIT) -#define elfExecutable 0x002 -#define elfLibrary 0x003 +/* + * ELF definitions for the i386 architecture. + */ +#include /* Definitions common to all 32 bit architectures. */ +#if defined(__ELF_WORD_SIZE) && __ELF_WORD_SIZE == 64 +#include /* Definitions common to all 64 bit architectures. */ +#endif + +#ifndef __ELF_WORD_SIZE +#define __ELF_WORD_SIZE 32 /* Used by */ +#endif + +#include + +#define ELF_ARCH EM_386 + +#define ELF_MACHINE_OK(x) ((x) == EM_386 || (x) == EM_486) + +/* + * Auxiliary vector entries for passing information to the interpreter. + * + * The i386 supplement to the SVR4 ABI specification names this "auxv_t", + * but POSIX lays claim to all symbols ending with "_t". + */ + +typedef struct { /* Auxiliary vector entry on initial stack */ + int a_type; /* Entry type. */ + union { + long a_val; /* Integer value. */ + void *a_ptr; /* Address. */ + void (*a_fcn)(void); /* Function pointer (not used). */ + } a_un; +} Elf32_Auxinfo; + +#if __ELF_WORD_SIZE == 64 +/* Fake for amd64 loader support */ typedef struct { - uInt32 phType; /* Entry type. */ - uInt32 phOffset; /* File offset of contents. */ - uInt32 phVaddr; /* Virtual address in memory image. */ - uInt32 phPaddr; /* Physical address (not used). */ - uInt32 phFilesz; /* Size of contents in file. */ - uInt32 phMemsz; /* Size of contents in memory. */ - uInt32 phFlags; /* Access permission flags. */ - uInt32 phAlign; /* Alignment in memory and file. */ -} elfProgramHeader; + int fake; +} Elf64_Auxinfo; +#endif -typedef struct { - uInt32 shName; /* Section name (index into the section header string table). */ - uInt32 shType; /* Section type. */ - uInt32 shFlags; /* Section flags. */ - uInt32 shAddr; /* Address in memory image. */ - uInt32 shOffset; /* Offset in file. */ - uInt32 shSize; /* Size in bytes. */ - uInt32 shLink; /* Index of a related section. */ - uInt32 shInfo; /* Depends on section type. */ - uInt32 shAddralign; /* Alignment in bytes. */ - uInt32 shEntsize; /* Size of each entry in section. */ -} elfSectionHeader; +__ElfType(Auxinfo); -typedef struct { - uInt32 pltOffset; - uInt32 pltInfo; -} elfPltInfo; +/* Values for a_type. */ +#define AT_NULL 0 /* Terminates the vector. */ +#define AT_IGNORE 1 /* Ignored entry. */ +#define AT_EXECFD 2 /* File descriptor of program to load. */ +#define AT_PHDR 3 /* Program header of program already loaded. */ +#define AT_PHENT 4 /* Size of each program header entry. */ +#define AT_PHNUM 5 /* Number of program header entries. */ +#define AT_PAGESZ 6 /* Page size in bytes. */ +#define AT_BASE 7 /* Interpreter's base address. */ +#define AT_FLAGS 8 /* Flags (unused for i386). */ +#define AT_ENTRY 9 /* Where interpreter should transfer control. */ +#define AT_NOTELF 10 /* Program is not ELF ?? */ +#define AT_UID 11 /* Real uid. */ +#define AT_EUID 12 /* Effective uid. */ +#define AT_GID 13 /* Real gid. */ +#define AT_EGID 14 /* Effective gid. */ +#define AT_EXECPATH 15 /* Path to the executable. */ +#define AT_CANARY 16 /* Canary for SSP. */ +#define AT_CANARYLEN 17 /* Length of the canary. */ +#define AT_OSRELDATE 18 /* OSRELDATE. */ +#define AT_NCPUS 19 /* Number of CPUs. */ +#define AT_PAGESIZES 20 /* Pagesizes. */ +#define AT_PAGESIZESLEN 21 /* Number of pagesizes. */ +#define AT_TIMEKEEP 22 /* Pointer to timehands. */ +#define AT_STACKPROT 23 /* Initial stack protection. */ -typedef struct { - uInt32 dynName; - uInt32 dynValue; - uInt32 dynSize; - uInt32 dynInfo; -} elfDynSym; +#define AT_COUNT 24 /* Count of defined aux entry types. */ -typedef struct { - uInt32 dynVal; - uInt32 dynPtr; -} elfDynamic_Old; +/* + * Relocation types. + */ -typedef struct { - uint32_t d_tag; - union { - uint32_t d_val; - uint32_t d_ptr; - } d_un; -} elfDynamic; +#define R_386_COUNT 38 /* Count of defined relocation types. */ -typedef struct { - int32_t execfd; - uint32_t phdr; - uint32_t phent; - uint32_t phnum; - uint32_t pagesz; - uint32_t base; - uint32_t flags; - uint32_t entry; - uint32_t trace; -} Elf_Auxargs; +/* Define "machine" characteristics */ +#define ELF_TARG_CLASS ELFCLASS32 +#define ELF_TARG_DATA ELFDATA2LSB +#define ELF_TARG_MACH EM_386 +#define ELF_TARG_VER 1 +#define ET_DYN_LOAD_ADDR 0x01001000 + +#elif defined(__amd64__) + +/* + * ELF definitions for the AMD64 architecture. + */ + +#ifndef __ELF_WORD_SIZE +#define __ELF_WORD_SIZE 64 /* Used by */ +#endif +#include /* Definitions common to all 32 bit architectures. */ +#include /* Definitions common to all 64 bit architectures. */ +#include + +#define ELF_ARCH EM_X86_64 +#define ELF_ARCH32 EM_386 + +#define ELF_MACHINE_OK(x) ((x) == EM_X86_64) + +/* + * Auxiliary vector entries for passing information to the interpreter. + * + * The i386 supplement to the SVR4 ABI specification names this "auxv_t", + * but POSIX lays claim to all symbols ending with "_t". + */ typedef struct { /* Auxiliary vector entry on initial stack */ int a_type; /* Entry type. */ union { @@ -74,6 +120,75 @@ } a_un; } Elf32_Auxinfo; + +typedef struct { /* Auxiliary vector entry on initial stack */ + long a_type; /* Entry type. */ + union { + long a_val; /* Integer value. */ + void *a_ptr; /* Address. */ + void (*a_fcn)(void); /* Function pointer (not used). */ + } a_un; +} Elf64_Auxinfo; + +__ElfType(Auxinfo); + +/* Values for a_type. */ +#define AT_NULL 0 /* Terminates the vector. */ +#define AT_IGNORE 1 /* Ignored entry. */ +#define AT_EXECFD 2 /* File descriptor of program to load. */ +#define AT_PHDR 3 /* Program header of program already loaded. */ +#define AT_PHENT 4 /* Size of each program header entry. */ +#define AT_PHNUM 5 /* Number of program header entries. */ +#define AT_PAGESZ 6 /* Page size in bytes. */ +#define AT_BASE 7 /* Interpreter's base address. */ +#define AT_FLAGS 8 /* Flags (unused for i386). */ +#define AT_ENTRY 9 /* Where interpreter should transfer control. */ +#define AT_NOTELF 10 /* Program is not ELF ?? */ +#define AT_UID 11 /* Real uid. */ +#define AT_EUID 12 /* Effective uid. */ +#define AT_GID 13 /* Real gid. */ +#define AT_EGID 14 /* Effective gid. */ +#define AT_EXECPATH 15 /* Path to the executable. */ +#define AT_CANARY 16 /* Canary for SSP */ +#define AT_CANARYLEN 17 /* Length of the canary. */ +#define AT_OSRELDATE 18 /* OSRELDATE. */ +#define AT_NCPUS 19 /* Number of CPUs. */ +#define AT_PAGESIZES 20 /* Pagesizes. */ +#define AT_PAGESIZESLEN 21 /* Number of pagesizes. */ +#define AT_TIMEKEEP 22 /* Pointer to timehands. */ +#define AT_STACKPROT 23 /* Initial stack protection. */ + +#define AT_COUNT 24 /* Count of defined aux entry types. */ + +/* + * Relocation types. + */ + +#define R_X86_64_COUNT 24 /* Count of defined relocation types. */ + +/* Define "machine" characteristics */ +#if __ELF_WORD_SIZE == 32 +#define ELF_TARG_CLASS ELFCLASS32 +#else +#define ELF_TARG_CLASS ELFCLASS64 +#endif +#define ELF_TARG_DATA ELFDATA2LSB +#define ELF_TARG_MACH EM_X86_64 +#define ELF_TARG_VER 1 + +#if __ELF_WORD_SIZE == 32 +#define ET_DYN_LOAD_ADDR 0x01001000 +#else +#define ET_DYN_LOAD_ADDR 0x01021000 +#endif + +#endif /* __i386__, __amd64__ */ + +#include + +#define elfExecutable 0x002 +#define elfLibrary 0x003 + char *elfGetShType( int ); char *elfGetPhType( int ); char *elfGetRelType( int ); diff --git a/sys/include/sys/elf.h b/sys/include/sys/elf.h index 853cbf5..e0f465d 100644 --- a/sys/include/sys/elf.h +++ b/sys/include/sys/elf.h @@ -35,4 +35,43 @@ #include #include +typedef struct elf_file { + int preloaded; /* Was file pre-loaded */ + caddr_t address; /* Relocation address */ + Elf32_Dyn *dynamic; /* Symbol table etc. */ + Elf32_Hashelt nbuckets; /* DT_HASH info */ + Elf32_Hashelt nchains; + const Elf32_Hashelt *buckets; + const Elf32_Hashelt *chains; + caddr_t hash; + caddr_t strtab; /* DT_STRTAB */ + int strsz; /* DT_STRSZ */ + const Elf32_Sym *symtab; /* DT_SYMTAB */ + Elf32_Addr *got; /* DT_PLTGOT */ + const Elf32_Rel *pltrel; /* DT_JMPREL */ + int pltrelsize; /* DT_PLTRELSZ */ + const Elf32_Rela *pltrela; /* DT_JMPREL */ + int pltrelasize; /* DT_PLTRELSZ */ + const Elf32_Rel *rel; /* DT_REL */ + int relsize; /* DT_RELSZ */ + const Elf32_Rela *rela; /* DT_RELA */ + int relasize; /* DT_RELASZ */ + caddr_t modptr; + const Elf32_Sym *ddbsymtab; /* The symbol table we are using */ + long ddbsymcnt; /* Number of symbols */ + caddr_t ddbstrtab; /* String table */ + long ddbstrcnt; /* number of bytes in string table */ + caddr_t symbase; /* malloc'ed symbold base */ + caddr_t strbase; /* malloc'ed string base */ + caddr_t ctftab; /* CTF table */ + long ctfcnt; /* number of bytes in CTF table */ + caddr_t ctfoff; /* CTF offset table */ + caddr_t typoff; /* Type offset table */ + long typlen; /* Number of type entries. */ + Elf32_Addr pcpu_start; /* Pre-relocation pcpu set start. */ + Elf32_Addr pcpu_stop; /* Pre-relocation pcpu set stop. */ + Elf32_Addr pcpu_base; /* Relocated pcpu set address. */ + Elf32_Addr ld_addr; // Entry Point Of Linker (Load It Too) +} *elf_file_t; + #endif diff --git a/sys/include/sys/elf_generic.h b/sys/include/sys/elf_generic.h new file mode 100644 index 0000000..36df0db --- /dev/null +++ b/sys/include/sys/elf_generic.h @@ -0,0 +1,61 @@ +#ifndef _SYS_ELF_GENERIC_H_ +#define _SYS_ELF_GENERIC_H_ 1 + +#include + +/* + * Definitions of generic ELF names which relieve applications from + * needing to know the word size. + */ + +#if __ELF_WORD_SIZE != 32 && __ELF_WORD_SIZE != 64 +#error "__ELF_WORD_SIZE must be defined as 32 or 64" +#endif + +#define ELF_CLASS __CONCAT(ELFCLASS,__ELF_WORD_SIZE) + +#if BYTE_ORDER == LITTLE_ENDIAN +#define ELF_DATA ELFDATA2LSB +#elif BYTE_ORDER == BIG_ENDIAN +#define ELF_DATA ELFDATA2MSB +#else +#error "Unknown byte order" +#endif + +#define __elfN(x) __CONCAT(__CONCAT(__CONCAT(elf,__ELF_WORD_SIZE),_),x) +#define __ElfN(x) __CONCAT(__CONCAT(__CONCAT(Elf,__ELF_WORD_SIZE),_),x) +#define __ELFN(x) __CONCAT(__CONCAT(__CONCAT(ELF,__ELF_WORD_SIZE),_),x) +#define __ElfType(x) typedef __ElfN(x) __CONCAT(Elf_,x) + +__ElfType(Addr); +__ElfType(Half); +__ElfType(Off); +__ElfType(Sword); +__ElfType(Word); +__ElfType(Ehdr); +__ElfType(Shdr); +__ElfType(Phdr); +__ElfType(Dyn); +__ElfType(Rel); +__ElfType(Rela); +__ElfType(Sym); +__ElfType(Verdef); +__ElfType(Verdaux); +__ElfType(Verneed); +__ElfType(Vernaux); +__ElfType(Versym); + +/* Non-standard ELF types. */ +__ElfType(Hashelt); +__ElfType(Size); +__ElfType(Ssize); + +#define ELF_R_SYM __ELFN(R_SYM) +#define ELF_R_TYPE __ELFN(R_TYPE) +#define ELF_R_INFO __ELFN(R_INFO) +#define ELF_ST_BIND __ELFN(ST_BIND) +#define ELF_ST_TYPE __ELFN(ST_TYPE) +#define ELF_ST_INFO __ELFN(ST_INFO) +#define ELF_ST_VISIBILITY __ELFN(ST_VISIBILITY) + +#endif /* !_SYS_ELF_GENERIC_H_ */ diff --git a/sys/kernel/elf.c b/sys/kernel/elf.c index da3be43..c2e2096 100644 --- a/sys/kernel/elf.c +++ b/sys/kernel/elf.c @@ -1,31 +1,28 @@ -/***************************************************************************************** - Copyright (c) 2002-2004 The UbixOS Project - All rights reserved. - - Redistribution and use in source and binary forms, with or without modification, are - permitted provided that the following conditions are met: - - Redistributions of source code must retain the above copyright notice, this list of - conditions, the following disclaimer and the list of authors. Redistributions in binary - form must reproduce the above copyright notice, this list of conditions, the following - disclaimer and the list of authors in the documentation and/or other materials provided - with the distribution. Neither the name of the UbixOS Project nor the names of its - contributors may be used to endorse or promote products derived from this software - without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY - EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL - THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT - OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR - TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - $Id: elf.c 141 2016-01-17 02:05:18Z reddawg $ - - *****************************************************************************************/ +/*- + * Copyright (c) 2002-2004, 2018 The UbixOS Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this list of + * conditions, the following disclaimer and the list of authors. Redistributions in binary + * form must reproduce the above copyright notice, this list of conditions, the following + * disclaimer and the list of authors in the documentation and/or other materials provided + * with the distribution. Neither the name of the UbixOS Project nor the names of its + * contributors may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT + * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ #include #include @@ -37,15 +34,15 @@ typedef struct elf_file { -} *elf_file_type; +}*elf_file_type; int elf_load_file(kTask_t *p, const char *file, uint32_t *addr, uint32_t *entry) { + int ret = 0; int i = 0x0; int x = 0x0; int numsegs = 0x0; - //uint32_t base = 0x0; uint32_t base_addr = 0x0; uint32_t real_base_addr = 0x0; @@ -59,7 +56,6 @@ if (exec_fd == 0x0) return (-1); - /* Load the ELF header */ if ((binaryHeader = (Elf32_Ehdr *) kmalloc(sizeof(Elf32_Ehdr))) == 0x0) K_PANIC("malloc failed!"); @@ -68,17 +64,18 @@ /* Check If App Is A Real Application */ if ((binaryHeader->e_ident[1] != 'E') && (binaryHeader->e_ident[2] != 'L') && (binaryHeader->e_ident[3] != 'F')) { - kfree(binaryHeader); - fclose(exec_fd); - return (-1); + ret = -1; + goto failed; } if (binaryHeader->e_type == ET_DYN) real_base_addr = *addr; else if (binaryHeader->e_type == ET_EXEC) real_base_addr = 0x0; - else - return (-1); + else { + ret = -1; + goto failed; + } /* Load The Program Header(s) */ if ((programHeader = (Elf32_Phdr *) kmalloc(sizeof(Elf32_Phdr) * binaryHeader->e_phnum)) == 0x0) @@ -115,7 +112,7 @@ } } if (numsegs == 0x0) - base_addr = (programHeader[i].p_vaddr & 0xFFFFF000) + real_base_addr; + base_addr = programHeader[i].p_vaddr + real_base_addr; //(programHeader[i].p_vaddr & 0xFFFFF000) + real_base_addr; numsegs++; break; } @@ -125,9 +122,20 @@ *entry = binaryHeader->e_entry + real_base_addr; - kfree(binaryHeader); - kfree(programHeader); - return (0x0); + failed: + + /* Close The Open File */ + fclose(exec_fd); + + /* Free Binary Header Memory */ + if (binaryHeader != 0x0) + kfree(binaryHeader); + + /* Free Program Header Memory */ + if (programHeader != 0x0) + kfree(programHeader); + + return (ret); } const struct { @@ -162,8 +170,6 @@ return ((char *) elfRelType[relType].relTypeName); } - - /*** END ***/