X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Usermode%2FLibraries%2Fld-acess.so_src%2Fmain.c;h=976ceec445a3c6483d3020ca50129d93d14290a5;hb=d8d31a4ec9a28eb8de493146ce75e8238e8e13b1;hp=4f2ef88b41803fa229849d750d2151f767dab52f;hpb=901bf4bada7bbba17bc8f1dcf36334a5b615e62f;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 4f2ef88b..976ceec4 100644 --- a/Usermode/Libraries/ld-acess.so_src/main.c +++ b/Usermode/Libraries/ld-acess.so_src/main.c @@ -2,17 +2,20 @@ AcessOS 1 - Dynamic Loader By thePowersGang */ +#include +#include #include "common.h" +#undef SoMain // === 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 tLoadedLib gLoadedLibraries[]; +extern char gLinkedBase[]; +char **gEnvP; +extern int memcmp(const void *m1, const void *m2, size_t size); +extern void CallQueuedEntrypoints(char **EnvP); // === CODE === /** @@ -20,56 +23,58 @@ extern tLoadedLib gLoadedLibraries[]; \brief Library entry point \note This is the entrypoint for the library */ -int SoMain(Uint base, int arg1) +void *SoMain(void *base, int argc, char **argv, char **envp) { - int ret; + void *ret; + gEnvP = envp; + // - 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); _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"); _exit(1); for(;;); } - gLoadedLibraries[0].Base = (Uint)&gLinkedBase; - gLoadedLibraries[0].Name = "ld-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); _exit(-1); for(;;); } - - // And call user - //SysDebug("Calling User at 0x%x\n", ret); - CallUser( ret, (Uint)&arg1 ); - - return 0; + + // Call queued entry points (from libraries) + CallQueuedEntrypoints(envp); + + SysDebug("ld-acess - SoMain: Program entry %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); _exit(-1); for(;;); @@ -78,13 +83,30 @@ int DoRelocate( Uint base, char **envp, char *Filename ) /** \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(;;); } + +void exit(int val) +{ + _exit(val); +} + +void abort(void) +{ + _exit(-4); +}