d3dee99a7797097720aa8f4dab0c891c963abe58
[tpg/acess2.git] / AcessNative / ld-acess_src / binary.c
1 /*
2  * AcessNative
3  */
4 #include "common.h"
5 #include <stdint.h>
6 #include <stdio.h>
7 #include <string.h>
8
9 #define LIBRARY_PATH    "$$$$../Usermode/Output/i386/Libs"
10
11 // === TYPES ===
12 typedef struct sBinary {
13         struct sBinary  *Next;
14         void    *Base;
15          int    Ready;
16         tBinFmt *Format;
17         char    Path[];
18 }       tBinary;
19
20 // === IMPORTS ===
21 extern void     *Elf_Load(int fd);
22 extern uintptr_t        Elf_Relocate(void *Base);
23 extern int      Elf_GetSymbol(void *Base, char *Name, uintptr_t *ret);
24 extern int      ciNumBuiltinSymbols;
25 extern tSym     caBuiltinSymbols[];
26
27 // === PROTOTYPES ===
28 void    Binary_AddToList(const char *Filename, void *Base, tBinFmt *Format);
29
30 // === GLOBALS ===
31 tBinFmt gElf_FormatDef = {
32 //      .Mask = 0xFFFFFFFF,
33 //      .Magic = "\x7F""ELF",
34         .Name = "ELF32",
35         .Load = Elf_Load,
36         .Relocate = Elf_Relocate,
37         .GetSymbol = Elf_GetSymbol
38         };
39 tBinary *gLoadedBinaries;
40
41 // === CODE ===
42 char *Binary_LocateLibrary(const char *Name)
43 {
44         char    *envPath = getenv("ACESS_LIBRARY_PATH");
45          int    nameLen = strlen(Name);
46          int    fd;
47         
48         if( strcmp(Name, "libld-acess.so") == 0 ) {
49                 return strdup("libld-acess.so");
50         }
51         
52         // Try the environment ACESS_LIBRARY_PATH
53         if( envPath && envPath[0] != '\0' )
54         {
55                  int    len = strlen(envPath)+1+nameLen+1;
56                 char    tmp[len];
57
58                 strcpy(tmp, envPath);
59                 strcat(tmp, "/");
60                 strcat(tmp, Name);
61                 
62                 fd = acess_open(tmp, 4);        // OPENFLAG_EXEC
63                 if(fd != -1) {
64                         acess_close(fd);
65                         return strdup(tmp);
66                 }
67         }               
68
69         {
70                  int    len = strlen(LIBRARY_PATH)+1+nameLen+1;
71                 char    tmp[len];
72
73                 strcpy(tmp, LIBRARY_PATH);
74                 strcat(tmp, "/");
75                 strcat(tmp, Name);
76                 
77                 #if DEBUG
78                 printf("Binary_LocateLibrary: tmp = '%s'\n", tmp);
79                 #endif
80
81                 fd = acess_open(tmp, 4);        // OPENFLAG_EXEC
82                 if(fd != -1) {
83                         acess_close(fd);
84                         return strdup(tmp);
85                 }
86         }               
87
88         #if DEBUG
89         fprintf(stderr, "Unable to locate library '%s'\n", Name);
90         #endif
91
92         return NULL;
93 }
94
95 void *Binary_LoadLibrary(const char *Name)
96 {
97         char    *path;
98         void    *ret;
99          int    (*entry)(int,char*[],char**) = NULL;
100
101         // Find File
102         path = Binary_LocateLibrary(Name);
103         #if DEBUG
104         printf("Binary_LoadLibrary: path = '%s'\n", path);
105         #endif
106         if( !path ) {
107                 return NULL;
108         }
109
110         ret = Binary_Load(path, (uintptr_t*)&entry);
111         free(path);
112         
113         #if DEBUG
114         printf("Binary_LoadLibrary: ret = %p, entry = %p\n", ret, entry);
115         #endif
116         if( entry ) {
117                 char    *argv[] = {NULL};
118                 #if DEBUG
119                 printf("Calling '%s' entry point %p\n", Name, entry);
120                 #endif
121                 entry(0, argv, NULL);
122         }
123
124         return ret;
125 }
126
127 void *Binary_Load(const char *Filename, uintptr_t *EntryPoint)
128 {
129          int    fd;
130         uint32_t        dword = 0xFA17FA17;
131         void    *ret;
132         uintptr_t       entry = 0;
133         tBinFmt *fmt;
134
135         // Ignore loading ld-acess
136         if( strcmp(Filename, "libld-acess.so") == 0 ) {
137                 *EntryPoint = 0;
138                 return (void*)-1;
139         }
140
141         {
142                 tBinary *bin;
143                 for(bin = gLoadedBinaries; bin; bin = bin->Next)
144                 {
145                         if( strcmp(Filename, bin->Path) == 0 ) {
146                                 return bin->Base;
147                         }
148                 }
149         }
150
151         fd = acess_open(Filename, 2|1); // Execute and Read
152         if( fd == -1 ) {
153                 // TODO: Handle libary directories
154                 perror("Opening binary");
155                 return NULL;
156         }
157
158         acess_read(fd, 4, &dword);
159         acess_seek(fd, 0, ACESS_SEEK_SET);
160         
161         if( memcmp(&dword, "\x7F""ELF", 4) == 0 ) {
162                 fmt = &gElf_FormatDef;
163         }
164         else {
165                 fprintf(stderr, "Unknown executable format (0x%08x)\n", dword);
166                 acess_close(fd);
167                 return NULL;
168         }
169         
170         #if DEBUG
171         printf("fmt->Load(%i)...\n", fd);
172         #endif
173         ret = fmt->Load(fd);
174         acess_close(fd);
175         #if DEBUG
176         printf("fmt->Load(%p): %p\n", fd, ret);
177         #endif
178         if( !ret ) {
179                 return NULL;
180         }
181         
182         Binary_AddToList(Filename, ret, fmt);
183
184         entry = fmt->Relocate(ret);
185         #if DEBUG
186         printf("fmt->Relocate(%p): %p\n", ret, (void*)entry);
187         #endif
188         if( !entry ) {
189                 // TODO: Clean up
190                 return NULL;
191         }
192         
193         if( EntryPoint )
194                 *EntryPoint = entry;
195
196         Binary_SetReadyToUse(ret);
197
198         return ret;
199 }
200
201 void Binary_AddToList(const char *Filename, void *Base, tBinFmt *Format)
202 {
203         tBinary *bin = malloc(sizeof(tBinary) + strlen(Filename) + 1);
204         bin->Base = Base;
205         bin->Format = Format;
206         strcpy(bin->Path, Filename);
207         bin->Ready = 0;
208         
209         bin->Next = gLoadedBinaries;
210         gLoadedBinaries = bin;
211 }
212
213 void Binary_SetReadyToUse(void *Base)
214 {
215         tBinary *bin;
216         for(bin = gLoadedBinaries; bin; bin = bin->Next)
217         {
218                 if( bin->Base != Base ) continue ;
219                 bin->Ready = 1;
220         }
221 }
222
223 int Binary_GetSymbol(const char *SymbolName, uintptr_t *Value)
224 {
225          int    i;
226         tBinary *bin;
227         
228         //printf("Binary_GetSymbol: (SymbolName='%s', Value=%p)\n",
229         //      SymbolName, Value);
230
231         // Search builtins
232         // - Placed first to override smartarses that define their own versions
233         //   of system calls
234         for( i = 0; i < ciNumBuiltinSymbols; i ++ )
235         {
236                 if( strcmp(caBuiltinSymbols[i].Name, SymbolName) == 0 ) {
237                         *Value = (uintptr_t)caBuiltinSymbols[i].Value;
238                         return 1;
239                 }
240         }
241         
242         // TODO: Search list of loaded binaries
243         for(bin = gLoadedBinaries; bin; bin = bin->Next)
244         {
245                 if( !bin->Ready )       continue;
246                 //printf(" Binary_GetSymbol: bin = %p{%p, %s}\n", bin, bin->Base, bin->Path);
247                 if( bin->Format->GetSymbol(bin->Base, (char*)SymbolName, Value) )
248                         return 1;
249         }
250
251         //printf("Binary_GetSymbol: RETURN 0, not found\n");
252         
253         return 0;
254 }

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