diff --git a/sys/init/main.c b/sys/init/main.c index c1011c0..3bdce03 100644 --- a/sys/init/main.c +++ b/sys/init/main.c @@ -1,5 +1,5 @@ /*- - * Copyright (c) 2002-2018, 2020 The UbixOS Project. + * Copyright (c) 2002-2018, 2020, 2022 The UbixOS Project. * All rights reserved. * * This was developed by Christopher W. Olsen for the UbixOS Project. diff --git a/sys/init/start.S b/sys/init/start.S index d5362c9..244b3f0 100644 --- a/sys/init/start.S +++ b/sys/init/start.S @@ -1,5 +1,5 @@ /*- - * Copyright (c) 2002-2018 The UbixOS Project. + * Copyright (c) 2002-2018, 2022 The UbixOS Project. * All rights reserved. * * This was developed by Christopher W. Olsen for the UbixOS Project. diff --git a/sys/kernel/elf.c b/sys/kernel/elf.c index 211f3d9..53800a5 100644 --- a/sys/kernel/elf.c +++ b/sys/kernel/elf.c @@ -1,5 +1,5 @@ /*- - * Copyright (c) 2002-2018 The UbixOS Project. + * Copyright (c) 2002-2018, 2022 The UbixOS Project. * All rights reserved. * * This was developed by Christopher W. Olsen for the UbixOS Project. @@ -34,138 +34,228 @@ #include #include -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; +/* + * Must document elf_load_file function + * + * return -1 if file not found + */ +int elf_load_file( kTask_t *p, const char *file, uint32_t *addr, uint32_t *entry ) { - uint32_t base_addr = 0x0; - uint32_t real_base_addr = 0x0; + int ret = 0; - Elf32_Ehdr *binaryHeader = 0x0; - Elf32_Phdr *programHeader = 0x0; + int i = 0x0; + int x = 0x0; + int numsegs = 0x0; - fileDescriptor_t *exec_fd = 0x0; + uint32_t base_addr = 0x0; + uint32_t real_base_addr = 0x0; - exec_fd = fopen(file, "r"); + Elf32_Ehdr *binaryHeader = 0x0; + Elf32_Phdr *programHeader = 0x0; - if (exec_fd == 0x0) - return (-1); + fileDescriptor_t *exec_fd = 0x0; - /* Load the ELF header */ - if ((binaryHeader = (Elf32_Ehdr *) kmalloc(sizeof(Elf32_Ehdr))) == 0x0) - K_PANIC("malloc failed!"); + exec_fd = fopen(file, "r"); - fread(binaryHeader, sizeof(Elf32_Ehdr), 1, exec_fd); + if (exec_fd == 0x0) { - /* Check If App Is A Real Application */ - if ((binaryHeader->e_ident[1] != 'E') && (binaryHeader->e_ident[2] != 'L') && (binaryHeader->e_ident[3] != 'F')) { - ret = -1; - goto failed; - } + return ( -1 ); - if (binaryHeader->e_type == ET_DYN) - real_base_addr = *addr; - else if (binaryHeader->e_type == ET_EXEC) - real_base_addr = 0x0; - else { - ret = -1; - goto failed; - } + } - /* Load The Program Header(s) */ - if ((programHeader = (Elf32_Phdr *) kmalloc(sizeof(Elf32_Phdr) * binaryHeader->e_phnum)) == 0x0) - K_PANIC("malloc failed!"); + /* Load the ELF header */ + if( ( binaryHeader = ( Elf32_Ehdr * ) kmalloc( sizeof( Elf32_Ehdr ) ) ) == 0x0 ) { - kern_fseek(exec_fd, binaryHeader->e_phoff, 0); + K_PANIC( "malloc failed!" ); - fread(programHeader, (sizeof(Elf32_Phdr) * binaryHeader->e_phnum), 1, exec_fd); + } - for (numsegs = 0x0, i = 0x0; i < binaryHeader->e_phnum; i++) { - switch (programHeader[i].p_type) { - case PT_LOAD: - /* - 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].p_memsz + 0xFFF); x += 0x1000) { + if( fread( binaryHeader, sizeof( Elf32_Ehdr ), 1, exec_fd ) != sizeof( Elf32_Ehdr ) ) { - /* Make readonly and read/write */ - if (vmm_remapPage(vmm_findFreePage(_current->id), ((programHeader[i].p_vaddr & 0xFFFFF000) + x + real_base_addr), PAGE_DEFAULT, _current->id, 0x0) == 0x0) - K_PANIC("Error: Remap Page Failed"); + K_PANIC( "Error loading EHDR" ); - memset((void *) ((programHeader[i].p_vaddr & 0xFFFFF000) + x + real_base_addr), 0x0, 0x1000); - } + } - /* Now Load Section To Memory */ - kern_fseek(exec_fd, programHeader[i].p_offset, 0); - fread((void *) programHeader[i].p_vaddr + real_base_addr, programHeader[i].p_filesz, 1, exec_fd); + /* Check if the loaded binary is an ELF binary */ + if( ( binaryHeader->e_ident[1] != 'E' ) && ( binaryHeader->e_ident[2] != 'L' ) && ( binaryHeader->e_ident[3] != 'F' ) ) { - 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 + real_base_addr, PAGE_PRESENT | PAGE_USER)) != 0x0) - K_PANIC("vmm_setPageAttributes failed"); - } - } - if (numsegs == 0x0) - base_addr = programHeader[i].p_vaddr + real_base_addr; //(programHeader[i].p_vaddr & 0xFFFFF000) + real_base_addr; - numsegs++; - break; + 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 { + + ret = -1; + + goto failed; + + } + + /* Load The Program Header(s) */ + if( ( programHeader = ( Elf32_Phdr * ) kmalloc( sizeof( Elf32_Phdr ) * binaryHeader->e_phnum ) ) == 0x0 ) { + + K_PANIC( "malloc failed!" ); + + } + + if( kern_fseek( exec_fd, binaryHeader->e_phoff, 0 ) != binaryHeader->e_phoff ) { + + K_PANIC( "Failed to seek" ); + } - } - *addr = base_addr; + /* Load program header */ + if( fread( programHeader, ( sizeof( Elf32_Phdr ) * binaryHeader->e_phnum ), 1, exec_fd ) != ( sizeof( Elf32_Phdr ) * binaryHeader->e_phnum ) ) { - *entry = binaryHeader->e_entry + real_base_addr; + K_PANIC( "Failed to load program header" ); - failed: + } - /* Close The Open File */ - fclose(exec_fd); + for( numsegs = 0x0, i = 0x0; i < binaryHeader->e_phnum; i++ ) { - /* Free Binary Header Memory */ - if (binaryHeader != 0x0) - kfree(binaryHeader); + switch ( programHeader[i].p_type ) { - /* Free Program Header Memory */ - if (programHeader != 0x0) - kfree(programHeader); + case PT_LOAD: + /* + Allocate Memory Im Going To Have To Make This Load Memory With Correct + Settings so it helps us in the future + */ - return (ret); + for( x = 0x0; x < ( programHeader[i].p_memsz + 0xFFF ); x += 0x1000 ) { + + /* Make readonly and read/write */ + if( vmm_remapPage( vmm_findFreePage( _current->id ), ( ( programHeader[i].p_vaddr & 0xFFFFF000 ) + x + real_base_addr ), PAGE_DEFAULT, _current->id, 0x0 ) == 0x0 ) { + + K_PANIC("Error: Remap Page Failed"); + + } + + memset( ( void * ) ( ( programHeader[i].p_vaddr & 0xFFFFF000 ) + x + real_base_addr ), 0x0, 0x1000 ); + + } + + /* Now Load Section To Memory */ + if( kern_fseek( exec_fd, programHeader[i].p_offset, 0 ) != programHeader[i].p_offset ) { + + K_PANIC( "fseek failed" ); + + } + + fread( ( void * ) programHeader[i].p_vaddr + real_base_addr, programHeader[i].p_filesz, 1, exec_fd ); + + 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 + real_base_addr, PAGE_PRESENT | PAGE_USER ) ) != 0x0 ) { + + K_PANIC( "vmm_setPageAttributes failed" ); + + } + } + } + + + if (numsegs == 0x0) { + + base_addr = programHeader[i].p_vaddr + real_base_addr; //(programHeader[i].p_vaddr & 0xFFFFF000) + real_base_addr; + + } + + numsegs++; + + break; + + } + + } + + *addr = base_addr; + + *entry = binaryHeader->e_entry + real_base_addr; + + 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 { - char *elfTypeName; - uInt32 id; + + char *elfTypeName; + + uInt32 id; + } elfType[] = { { "ET_NONE", 0 }, { "ET_REL", 1 }, { "ET_EXEC", 2 }, { "ET_DYN", 3 }, { "ET_CORE", 4 }, { "ET_LOPROC", 0xff00 }, { "ET_HIPROC", 0xffff }, }; const struct { - char *phTypeName; - uInt32 id; + + char *phTypeName; + + uInt32 id; + } elfPhType[] = { { "PT_NULL", 0 }, { "PT_LOAD", 1 }, { "PT_DYNAMIC", 2 }, { "PT_INTERP", 3 }, { "PT_NOTE", 4 }, { "PT_SHLIB", 5 }, { "PT_PHDR", 6 }, { "PT_LOPROC", 0x70000000 }, { "PT_HIPROC", 0x7fffffff }, }; const struct { - char *shTypeName; - uInt32 id; + + char *shTypeName; + + uInt32 id; + } elfShType[] = { { "SHT_NULL", 0 }, { "SHT_PROGBITS", 1 }, { "SHT_SYMTAB", 2 }, { "SHT_STRTAB", 3 }, { "SHT_RELA", 4 }, { "SHT_HASH", 5 }, { "SHT_DYNAMIC", 6 }, { "SHT_NOTE", 7 }, { "SHT_NOBITS", 8 }, { "SHT_REL", 9 }, { "SHT_SHLIB", 10 }, { "SHT_DYNSYM", 11 }, }; const struct { - char *relTypeName; - uInt32 id; + + char *relTypeName; + + uInt32 id; + } elfRelType[] = { { "R_386_NONE", 0 }, { "R_386_32", 1 }, { "R_386_PC32", 2 }, { "R_386_GOT32", 3 }, { "R_386_PLT32", 4 }, { "R_386_COPY", 5 }, { "R_386_GLOB_DAT", 6 }, { "R_386_JMP_SLOT", 7 }, { "R_386_RELATIVE", 8 }, { "R_386_GOTOFF", 9 }, { "R_386_GOTPC", 10 }, }; -char *elfGetShType(int shType) { - return ((char *) elfShType[shType].shTypeName); +char *elfGetShType( int shType ) { + + return( ( char * ) elfShType[shType].shTypeName ); + } -char *elfGetPhType(int phType) { - return ((char *) elfPhType[phType].phTypeName); +char *elfGetPhType( int phType ) { + + return( ( char * ) elfPhType[phType].phTypeName ); + } -char *elfGetRelType(int relType) { - return ((char *) elfRelType[relType].relTypeName); +char *elfGetRelType( int relType ) { + + return( ( char * ) elfRelType[relType].relTypeName ); + } /***