Merge branch 'master' of git://cadel.mutabah.net/acess2
[tpg/acess2.git] / Usermode / Libraries / ld-acess.so_src / loadlib.c
1 /*
2  AcessOS 1 - Dynamic Loader
3  By thePowersGang
4 */
5 #include "common.h"
6 #include <stdint.h>
7 #include <acess/sys.h>
8
9 #define DEBUG   0
10
11 #if DEBUG
12 # define DEBUGS(v...)   SysDebug(v)
13 #else
14 # define DEBUGS(v...)   
15 #endif
16
17 // === PROTOTYPES ===
18 void    *IsFileLoaded(const char *file);
19
20 // === IMPORTS ===
21 extern const struct {
22         void    *Value;
23         char    *Name;
24 }       caLocalExports[];
25 extern const int        ciNumLocalExports;
26 extern char     **gEnvP;
27
28 // === GLOABLS ===
29 tLoadedLib      gLoadedLibraries[MAX_LOADED_LIBRARIES];
30 char    gsLoadedStrings[MAX_STRINGS_BYTES];
31 char    *gsNextAvailString = gsLoadedStrings;
32 //tLoadLib      *gpLoadedLibraries = NULL;
33
34 // === CODE ===
35 const char *FindLibrary(char *DestBuf, const char *SoName, const char *ExtraSearchDir)
36 {       
37         // -- #1: Executable Specified
38         if(ExtraSearchDir)
39         {
40                 strcpy(DestBuf, ExtraSearchDir);
41                 strcat(DestBuf, "/");
42                 strcat(DestBuf, SoName);
43                 if(file_exists(DestBuf))        return DestBuf;
44         }
45         
46         // -- #2: System
47         strcpy(DestBuf, SYSTEM_LIB_DIR);
48         strcat(DestBuf, SoName);
49         if(file_exists(DestBuf))        return DestBuf;
50         
51         // -- #3: Current Directory
52         if(file_exists(SoName)) return SoName;
53         
54         return NULL;
55 }
56
57 /**
58  */
59 void *LoadLibrary(const char *SoName, const char *SearchDir, char **envp)
60 {
61         char    sTmpName[1024];
62         const char      *filename;
63         void    *base;
64         void    (*fEntry)(void *, int, char *[], char**);
65         
66         DEBUGS("LoadLibrary: (SoName='%s', SearchDir='%s', envp=%p)", SoName, SearchDir, envp);
67         
68         // Create Temp Name
69         filename = FindLibrary(sTmpName, SoName, SearchDir);
70         if(filename == NULL) {
71                 DEBUGS("LoadLibrary: RETURN 0");
72                 return 0;
73         }
74         DEBUGS(" LoadLibrary: filename='%s'", filename);
75         
76         if( (base = IsFileLoaded(filename)) )
77                 return base;
78
79         DEBUGS(" LoadLibrary: SysLoadBin()");   
80         // Load Library
81         base = _SysLoadBin(filename, (void**)&fEntry);
82         if(!base) {
83                 DEBUGS("LoadLibrary: RETURN 0");
84                 return 0;
85         }
86         
87         DEBUGS(" LoadLibrary: iArg=%p, fEntry=%p", base, fEntry);
88         
89         // Load Symbols
90         fEntry = DoRelocate( base, envp, filename );
91         if( !fEntry ) {
92                 return 0;
93         }
94         
95         // Call Entrypoint
96         DEBUGS(" LoadLibrary: '%s' Entry %p", SoName, fEntry);
97         fEntry(base, 0, NULL, gEnvP);
98         
99         DEBUGS("LoadLibrary: RETURN 1");
100         return base;
101 }
102
103 /**
104  * \fn Uint IsFileLoaded(char *file)
105  * \brief Determine if a file is already loaded
106  */
107 void *IsFileLoaded(const char *file)
108 {
109          int    i;
110         DEBUGS("IsFileLoaded: (file='%s')", file);
111         for( i = 0; i < MAX_LOADED_LIBRARIES; i++ )
112         {
113                 if(gLoadedLibraries[i].Base == 0)       break;  // Last entry has Base set to NULL
114                 DEBUGS(" strcmp('%s', '%s')", gLoadedLibraries[i].Name, file);
115                 if(strcmp(gLoadedLibraries[i].Name, file) == 0) {
116                         DEBUGS("IsFileLoaded: Found %i (%p)", i, gLoadedLibraries[i].Base);
117                         return gLoadedLibraries[i].Base;
118                 }
119         }
120         DEBUGS("IsFileLoaded: Not Found");
121         return 0;
122 }
123
124 /**
125  * \fn void AddLoaded(char *File, Uint base)
126  * \brief Add a file to the loaded list
127  */
128 void AddLoaded(const char *File, void *base)
129 {
130          int    i, length;
131         char    *name = gsNextAvailString;
132         
133         DEBUGS("AddLoaded: (File='%s', base=%p)", File, base);
134         
135         // Find a free slot
136         for( i = 0; i < MAX_LOADED_LIBRARIES; i ++ )
137         {
138                 if(gLoadedLibraries[i].Base == 0)       break;
139         }
140         if(i == MAX_LOADED_LIBRARIES) {
141                 SysDebug("ERROR - ld-acess.so has run out of load slots!");
142                 return;
143         }
144         
145         // Check space in string buffer
146         length = strlen(File);
147         if(&name[length+1] >= &gsLoadedStrings[MAX_STRINGS_BYTES]) {
148                 SysDebug("ERROR - ld-acess.so has run out of string buffer memory!");
149                 return;
150         }
151         
152         // Set information
153         gLoadedLibraries[i].Base = base;
154         strcpy(name, File);
155         gLoadedLibraries[i].Name = name;
156         gsNextAvailString = &name[length+1];
157         DEBUGS("'%s' (%p) loaded as %i", name, base, i);
158         return;
159 }
160
161 /**
162  * \fn void Unload(Uint Base)
163  */
164 void Unload(void *Base)
165 {       
166          int    i, j;
167          int    id;
168         char    *str;
169         for( id = 0; id < MAX_LOADED_LIBRARIES; id++ )
170         {
171                 if(gLoadedLibraries[id].Base == Base)   break;
172         }
173         if(id == MAX_LOADED_LIBRARIES)  return;
174         
175         // Unload Binary
176         _SysUnloadBin( Base );
177         // Save String Pointer
178         str = gLoadedLibraries[id].Name;
179         
180         // Compact Loaded List
181         j = id;
182         for( i = j + 1; i < MAX_LOADED_LIBRARIES; i++, j++ )
183         {
184                 if(gLoadedLibraries[i].Base == 0)       break;
185                 // Compact String
186                 strcpy(str, gLoadedLibraries[i].Name);
187                 str += strlen(str)+1;
188                 // Compact Entry
189                 gLoadedLibraries[j].Base = gLoadedLibraries[i].Base;
190                 gLoadedLibraries[j].Name = str;
191         }
192         
193         // NULL Last Entry
194         gLoadedLibraries[j].Base = 0;
195         gLoadedLibraries[j].Name = NULL;
196         // Save next string
197         gsNextAvailString = str;
198 }
199
200 /**
201  \fn Uint GetSymbol(const char *name)
202  \brief Gets a symbol value from a loaded library
203 */
204 int GetSymbol(const char *name, void **Value, size_t *Size)
205 {
206          int    i;
207         
208         //SysDebug("ciNumLocalExports = %i", ciNumLocalExports);
209         for(i=0;i<ciNumLocalExports;i++)
210         {
211                 if( strcmp(caLocalExports[i].Name, name) == 0 ) {
212                         *Value = caLocalExports[i].Value;
213                         if(Size)
214                                 *Size = 0;
215                         return 1;
216                 }
217         }
218         
219         // Entry 0 is ld-acess, ignore it
220         for(i = 1; i < MAX_LOADED_LIBRARIES; i ++)
221         {
222                 if(gLoadedLibraries[i].Base == 0)
223                         break;
224                 
225                 //SysDebug(" GetSymbol: Trying 0x%x, '%s'",
226                 //      gLoadedLibraries[i].Base, gLoadedLibraries[i].Name);
227                 if(GetSymbolFromBase(gLoadedLibraries[i].Base, name, Value, Size))
228                         return 1;
229         }
230         SysDebug("GetSymbol: === Symbol '%s' not found ===", name);
231         return 0;
232 }
233
234 /**
235  \fn int GetSymbolFromBase(Uint base, char *name, Uint *ret)
236  \breif Gets a symbol from a specified library
237 */
238 int GetSymbolFromBase(void *base, const char *name, void **ret, size_t *Size)
239 {
240         uint8_t *hdr = base;
241         if(hdr[0] == 0x7F && hdr[1] == 'E' && hdr[2] == 'L' && hdr[3] == 'F')
242                 return ElfGetSymbol(base, name, ret, Size);
243         if(hdr[0] == 'M' && hdr[1] == 'Z')
244                 return PE_GetSymbol(base, name, ret, Size);
245         SysDebug("Unknown type at %p (%02x %02x %02x %02x)", base,
246                 hdr[0], hdr[1], hdr[2], hdr[3]);
247         return 0;
248 }
249

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