X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Usermode%2FLibraries%2Fld-acess.so_src%2Fmain.c;h=ff715bb33502853568f04b76e7d694745098b011;hb=032ec5adf8d4faeaa9293da50ad2e32c83dd1704;hp=dc5d4d34ff57eaab626da63a113b32b77e8cd3e0;hpb=e8a85694d2dfc768dcd68826b31f8711d257fb51;p=tpg%2Facess2.git diff --git a/Usermode/Libraries/ld-acess.so_src/main.c b/Usermode/Libraries/ld-acess.so_src/main.c index dc5d4d34..ff715bb3 100644 --- a/Usermode/Libraries/ld-acess.so_src/main.c +++ b/Usermode/Libraries/ld-acess.so_src/main.c @@ -2,16 +2,17 @@ AcessOS 1 - Dynamic Loader By thePowersGang */ +#include +#include #include "common.h" // === PROTOTYPES === - int DoRelocate( Uint base, char **envp, char *Filename ); - int CallUser(Uint entry, Uint SP); - int ElfRelocate(void *Base, char *envp[], char *Filename); - int PE_Relocate(void *Base, char *envp[], char *Filename); +void *DoRelocate(void *base, char **envp, const char *Filename); + int CallUser(void *Entry, void *SP); // === Imports === -extern void gLinkedBase; +extern char gLinkedBase[]; +extern tLoadedLib gLoadedLibraries[]; // === CODE === /** @@ -19,71 +20,80 @@ extern void gLinkedBase; \brief Library entry point \note This is the entrypoint for the library */ -int SoMain(Uint base, int arg1) +void *SoMain(void *base) { - int ret; - - //SysDebug("SoMain: base = 0x%x", base); - //SysDebug("SoMain: arg1 = 0x%x", arg1); + void *ret; // - Assume that the file pointer will be less than 4096 - if(base < 0x1000) { + if((intptr_t)base < 0x1000) { SysDebug("ld-acess - SoMain: Passed file pointer %i\n", base); - SysExit(); + _exit(-1); for(;;); } // Check if we are being called directly - if(base == (Uint)&gLinkedBase) { + if(base == &gLinkedBase) { SysDebug("ld-acess should not be directly called\n"); - SysExit(); + _exit(1); for(;;); } + + gLoadedLibraries[0].Base = &gLinkedBase; + // 'libld-acess.so' because that is what applications link against + gLoadedLibraries[0].Name = "/Acess/Libs/libld-acess.so"; // Otherwise do relocations //ret = DoRelocate( base, envp, "Executable" ); ret = DoRelocate( base, NULL, "Executable" ); if( ret == 0 ) { SysDebug("ld-acess - SoMain: Relocate failed, base=0x%x\n", base); - SysExit(); + _exit(-1); for(;;); } - - // And call user - //SysDebug("Calling User at 0x%x\n", ret); - CallUser( ret, (Uint)&arg1 ); - - return 0; + + SysDebug("ld-acess - SoMain: ret = %p", ret); + return ret; } /** - \fn int DoRelocate(Uint base, char **envp) + \fn int DoRelocate(void *base, char **envp) \brief Relocates an in-memory image */ -int DoRelocate( Uint base, char **envp, char *Filename ) +void *DoRelocate(void *base, char **envp, const char *Filename) { + uint8_t *hdr = base; // Load Executable - if(*(Uint*)base == (0x7F|('E'<<8)|('L'<<16)|('F'<<24))) - return ElfRelocate((void*)base, envp, Filename); - if(*(Uint16*)base == ('M'|('Z'<<8))) - return PE_Relocate((void*)base, envp, Filename); + if(memcmp(base, "\x7F""ELF", 4) == 0) + return ElfRelocate(base, envp, Filename); + if(hdr[0] == 0x7F && hdr[1] == 'E' && hdr[2] == 'L' && hdr[3] == 'F') + return ElfRelocate(base, envp, Filename); + + if(hdr[0] == 'M' && hdr[1] == 'Z') + return PE_Relocate(base, envp, Filename); SysDebug("ld-acess - DoRelocate: Unkown file format '0x%x 0x%x 0x%x 0x%x'\n", - *(Uint8*)(base), *(Uint8*)(base+1), *(Uint8*)(base+2), *(Uint8*)(base+3) ); + hdr[0], hdr[1], hdr[2], hdr[3] ); SysDebug("ld-acess - DoRelocate: File '%s'\n", Filename); - SysExit(); + _exit(-1); for(;;); } /** \fn int CallUser(Uint entry, Uint sp) */ -int CallUser(Uint entry, Uint sp) +int CallUser(void *entry, void *sp) { - //SysDebug("CallUser: (entry=0x%x, sp=0x%x)", entry, sp); - *(Uint*)(sp-4) = 0; // Clear return address + #if ARCHDIR_IS_x86_64 + ((void **)sp)[-1] = 0; // Clear return address + __asm__ __volatile__ ( + "mov %%rax, %%rsp;\n\t" + "jmp *%%rcx" + : : "a"(sp), "c"(entry)); + #elif ARCHDIR_IS_x86 + ((void **)sp)[-1] = 0; // Clear return address __asm__ __volatile__ ( "mov %%eax, %%esp;\n\t" "jmp *%%ecx" : : "a"(sp), "c"(entry)); + #endif for(;;); }