2 AcessOS 1 - Dynamic Loader
\r
9 // === PROTOTYPES ===
\r
10 void *DoRelocate(void *base, char **envp, const char *Filename);
\r
11 int CallUser(void *Entry, void *SP);
\r
14 extern char gLinkedBase[];
\r
16 extern int memcmp(const void *m1, const void *m2, size_t size);
\r
20 \fn int SoMain(Uint base, int argc, char *argv[], char **envp)
\r
21 \brief Library entry point
\r
22 \note This is the entrypoint for the library
\r
24 void *SoMain(void *base, int argc, char **argv, char **envp)
\r
30 // - Assume that the file pointer will be less than 4096
\r
31 if((intptr_t)base < 0x1000) {
\r
32 SysDebug("ld-acess - SoMain: Passed file pointer %i\n", base);
\r
36 // Check if we are being called directly
\r
37 if(base == &gLinkedBase) {
\r
38 SysDebug("ld-acess should not be directly called\n");
\r
43 // Otherwise do relocations
\r
44 //ret = DoRelocate( base, envp, "Executable" );
\r
45 ret = DoRelocate( base, NULL, "Executable" );
\r
47 SysDebug("ld-acess - SoMain: Relocate failed, base=0x%x\n", base);
\r
52 SysDebug("ld-acess - SoMain: ret = %p", ret);
\r
57 \fn int DoRelocate(void *base, char **envp)
\r
58 \brief Relocates an in-memory image
\r
60 void *DoRelocate(void *base, char **envp, const char *Filename)
\r
62 uint8_t *hdr = base;
\r
64 if(memcmp(base, "\x7F""ELF", 4) == 0)
\r
65 return ElfRelocate(base, envp, Filename);
\r
66 if(hdr[0] == 0x7F && hdr[1] == 'E' && hdr[2] == 'L' && hdr[3] == 'F')
\r
67 return ElfRelocate(base, envp, Filename);
\r
69 if(hdr[0] == 'M' && hdr[1] == 'Z')
\r
70 return PE_Relocate(base, envp, Filename);
\r
72 SysDebug("ld-acess - DoRelocate: Unkown file format '0x%x 0x%x 0x%x 0x%x'\n",
\r
73 hdr[0], hdr[1], hdr[2], hdr[3] );
\r
74 SysDebug("ld-acess - DoRelocate: File '%s'\n", Filename);
\r
80 \fn int CallUser(Uint entry, Uint sp)
\r
82 int CallUser(void *entry, void *sp)
\r
84 #if ARCHDIR_IS_x86_64
\r
85 ((void **)sp)[-1] = 0; // Clear return address
\r
86 __asm__ __volatile__ (
\r
87 "mov %%rax, %%rsp;\n\t"
\r
89 : : "a"(sp), "c"(entry));
\r
90 #elif ARCHDIR_IS_x86
\r
91 ((void **)sp)[-1] = 0; // Clear return address
\r
92 __asm__ __volatile__ (
\r
93 "mov %%eax, %%esp;\n\t"
\r
95 : : "a"(sp), "c"(entry));
\r