Usermode/ld-acess - Reworked build structure and syscall files
[tpg/acess2.git] / Usermode / Libraries / ld-acess.so_src / main.c
1 /*\r
2  AcessOS 1 - Dynamic Loader\r
3  By thePowersGang\r
4 */\r
5 #include <stdint.h>\r
6 #include "common.h"\r
7 \r
8 // === PROTOTYPES ===\r
9 void    *DoRelocate(void *base, char **envp, char *Filename);\r
10  int    CallUser(void *Entry, void *SP);\r
11 void    *ElfRelocate(void *Base, char *envp[], char *Filename);\r
12 void    *PE_Relocate(void *Base, char *envp[], char *Filename);\r
13 \r
14 // === Imports ===\r
15 extern void     gLinkedBase;\r
16 extern tLoadedLib       gLoadedLibraries[];\r
17  \r
18 // === CODE ===\r
19 /**\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
23 */\r
24 int SoMain(void *base, void *arg1)\r
25 {\r
26         void    *ret;\r
27          \r
28         // - Assume that the file pointer will be less than 4096\r
29         if((intptr_t)base < 0x1000) {\r
30                 SysDebug("ld-acess - SoMain: Passed file pointer %i\n", base);\r
31                 _exit(-1);\r
32                 for(;;);\r
33         }\r
34         // Check if we are being called directly\r
35         if(base == &gLinkedBase) {\r
36                 SysDebug("ld-acess should not be directly called\n");\r
37                 _exit(1);\r
38                 for(;;);\r
39         }\r
40 \r
41         gLoadedLibraries[0].Base = &gLinkedBase;\r
42         // 'libld-acess.so' because that is what applications link against\r
43         gLoadedLibraries[0].Name = "/Acess/Libs/libld-acess.so";\r
44         \r
45         // Otherwise do relocations\r
46         //ret = DoRelocate( base, envp, "Executable" );\r
47         ret = DoRelocate( base, NULL, "Executable" );\r
48         if( ret == 0 ) {\r
49                 SysDebug("ld-acess - SoMain: Relocate failed, base=0x%x\n", base);\r
50                 _exit(-1);\r
51                 for(;;);\r
52         }\r
53         \r
54         // And call user\r
55         //SysDebug("Calling User at 0x%x\n", ret);\r
56         CallUser( ret, &arg1 );\r
57         \r
58         return 0;\r
59 }\r
60 \r
61 /**\r
62  \fn int DoRelocate(void *base, char **envp)\r
63  \brief Relocates an in-memory image\r
64 */\r
65 void *DoRelocate(void *base, char **envp, char *Filename)\r
66 {\r
67         // Load Executable\r
68         if(*(Uint32*)base == (0x7F|('E'<<8)|('L'<<16)|('F'<<24)))\r
69                 return ElfRelocate(base, envp, Filename);\r
70         if(*(Uint16*)base == ('M'|('Z'<<8)))\r
71                 return PE_Relocate(base, envp, Filename);\r
72         \r
73         SysDebug("ld-acess - DoRelocate: Unkown file format '0x%x 0x%x 0x%x 0x%x'\n",\r
74                 *(Uint8*)(base), *(Uint8*)(base+1), *(Uint8*)(base+2), *(Uint8*)(base+3) );\r
75         SysDebug("ld-acess - DoRelocate: File '%s'\n", Filename);\r
76         _exit(-1);\r
77         for(;;);\r
78 }\r
79 \r
80 /**\r
81  \fn int CallUser(Uint entry, Uint sp)\r
82 */\r
83 int CallUser(void *entry, void *sp)\r
84 {\r
85         #if ARCHDIR_IS_x86_64\r
86         ((void **)sp)[-1] = 0;  // Clear return address\r
87         __asm__ __volatile__ (\r
88         "mov %%rax, %%rsp;\n\t"\r
89         "jmp *%%rcx"\r
90         : : "a"(sp), "c"(entry));\r
91         #elif ARCHDIR_IS_x86\r
92         ((void **)sp)[-1] = 0;  // Clear return address\r
93         __asm__ __volatile__ (\r
94         "mov %%eax, %%esp;\n\t"\r
95         "jmp *%%ecx"\r
96         : : "a"(sp), "c"(entry));\r
97         #endif\r
98         for(;;);\r
99 }\r

UCC git Repository :: git.ucc.asn.au