2 AcessOS 1 - Dynamic Loader
12 # define DEBUGS(v...) SysDebug(v)
17 #define MAX_QUEUED_ENTRYPOINTS 8
24 extern const int ciNumLocalExports;
26 extern char gLinkedBase[];
29 typedef void tLibEntry(void *, int, char *[], char**);
32 void *IsFileLoaded(const char *file);
35 tLoadedLib gLoadedLibraries[MAX_LOADED_LIBRARIES];
36 char gsLoadedStrings[MAX_STRINGS_BYTES];
37 char *gsNextAvailString = gsLoadedStrings;
41 } gaQueuedEntrypoints[MAX_QUEUED_ENTRYPOINTS];
42 int giNumQueuedEntrypoints;
43 //tLoadLib *gpLoadedLibraries = NULL;
46 void ldacess_DumpLoadedLibraries(void)
48 for( int i = 0; i < MAX_LOADED_LIBRARIES; i ++ )
50 if(gLoadedLibraries[i].Base == 0) break; // Last entry has Base set to NULL
52 gLoadedLibraries[i].Base,
53 gLoadedLibraries[i].Name
59 * \brief Call queued up entry points (after relocations completed)
61 void CallQueuedEntrypoints(char **EnvP)
63 while( giNumQueuedEntrypoints )
65 giNumQueuedEntrypoints --;
66 const struct sQueuedEntry *qe = &gaQueuedEntrypoints[giNumQueuedEntrypoints];
67 //_SysDebug("Calling EP for %p", qe->Base);
68 qe->Entry(qe->Base, 0, NULL, EnvP);
72 const char *FindLibrary(char *DestBuf, const char *SoName, const char *ExtraSearchDir)
74 // -- #1: Executable Specified
77 strcpy(DestBuf, ExtraSearchDir);
79 strcat(DestBuf, SoName);
80 if(file_exists(DestBuf)) return DestBuf;
84 strcpy(DestBuf, SYSTEM_LIB_DIR);
85 strcat(DestBuf, SoName);
86 if(file_exists(DestBuf)) return DestBuf;
88 // -- #3: Current Directory
89 if(file_exists(SoName)) return SoName;
96 void *LoadLibrary(const char *SoName, const char *SearchDir, char **envp)
101 DEBUGS("LoadLibrary: (SoName='%s', SearchDir='%s', envp=%p)", SoName, SearchDir, envp);
104 const char *filename = FindLibrary(sTmpName, SoName, SearchDir);
105 if(filename == NULL) {
106 DEBUGS("LoadLibrary: RETURN 0");
109 DEBUGS(" LoadLibrary: filename='%s'", filename);
111 if( (base = IsFileLoaded(filename)) )
114 DEBUGS(" LoadLibrary: SysLoadBin()");
117 base = _SysLoadBin(filename, (void**)&fEntry);
119 DEBUGS("LoadLibrary: RETURN 0");
123 DEBUGS(" LoadLibrary: iArg=%p, fEntry=%p", base, fEntry);
126 fEntry = DoRelocate( base, envp, filename );
132 // - TODO: Queue entrypoint calls
133 if( giNumQueuedEntrypoints >= MAX_QUEUED_ENTRYPOINTS ) {
134 SysDebug("ERROR - Maximum number of queued entrypoints exceeded on %p '%s'",
138 gaQueuedEntrypoints[giNumQueuedEntrypoints].Base = base;
139 gaQueuedEntrypoints[giNumQueuedEntrypoints].Entry = fEntry;
140 giNumQueuedEntrypoints ++;
142 DEBUGS("LoadLibrary: RETURN success");
147 * \fn Uint IsFileLoaded(char *file)
148 * \brief Determine if a file is already loaded
150 void *IsFileLoaded(const char *file)
153 DEBUGS("IsFileLoaded: (file='%s')", file);
155 // Applications link against either libld-acess.so or ld-acess.so
156 if( strcmp(file, "/Acess/Libs/libld-acess.so") == 0
157 || strcmp(file, "/Acess/Libs/ld-acess.so") == 0 )
159 DEBUGS("IsFileLoaded: Found local (%p)", &gLinkedBase);
163 for( i = 0; i < MAX_LOADED_LIBRARIES; i++ )
165 if(gLoadedLibraries[i].Base == 0) break; // Last entry has Base set to NULL
166 DEBUGS(" strcmp('%s', '%s')", gLoadedLibraries[i].Name, file);
167 if(strcmp(gLoadedLibraries[i].Name, file) == 0) {
168 DEBUGS("IsFileLoaded: Found %i (%p)", i, gLoadedLibraries[i].Base);
169 return gLoadedLibraries[i].Base;
172 DEBUGS("IsFileLoaded: Not Found");
177 * \fn void AddLoaded(char *File, Uint base)
178 * \brief Add a file to the loaded list
180 void AddLoaded(const char *File, void *base)
183 char *name = gsNextAvailString;
185 DEBUGS("AddLoaded: (File='%s', base=%p)", File, base);
188 for( i = 0; i < MAX_LOADED_LIBRARIES; i ++ )
190 if(gLoadedLibraries[i].Base == 0) break;
192 if(i == MAX_LOADED_LIBRARIES) {
193 SysDebug("ERROR - ld-acess.so has run out of load slots!");
197 // Check space in string buffer
198 length = strlen(File);
199 if(&name[length+1] >= &gsLoadedStrings[MAX_STRINGS_BYTES]) {
200 SysDebug("ERROR - ld-acess.so has run out of string buffer memory!");
205 gLoadedLibraries[i].Base = base;
207 gLoadedLibraries[i].Name = name;
208 gsNextAvailString = &name[length+1];
209 DEBUGS("'%s' (%p) loaded as %i", name, base, i);
214 * \fn void Unload(Uint Base)
216 void Unload(void *Base)
221 for( id = 0; id < MAX_LOADED_LIBRARIES; id++ )
223 if(gLoadedLibraries[id].Base == Base) break;
225 if(id == MAX_LOADED_LIBRARIES) return;
228 _SysUnloadBin( Base );
229 // Save String Pointer
230 str = gLoadedLibraries[id].Name;
232 // Compact Loaded List
234 for( i = j + 1; i < MAX_LOADED_LIBRARIES; i++, j++ )
236 if(gLoadedLibraries[i].Base == 0) break;
238 strcpy(str, gLoadedLibraries[i].Name);
239 str += strlen(str)+1;
241 gLoadedLibraries[j].Base = gLoadedLibraries[i].Base;
242 gLoadedLibraries[j].Name = str;
246 gLoadedLibraries[j].Base = 0;
247 gLoadedLibraries[j].Name = NULL;
249 gsNextAvailString = str;
253 \fn Uint GetSymbol(const char *name)
254 \brief Gets a symbol value from a loaded library
256 int GetSymbol(const char *name, void **Value, size_t *Size, void *IgnoreBase)
258 //SysDebug("GetSymbol: (%s)");
259 for( int i = 0; i < ciNumLocalExports; i ++ )
261 if( strcmp(caLocalExports[i].Name, name) == 0 ) {
262 *Value = caLocalExports[i].Value;
265 //SysDebug("GetSymbol: Local %p+0x%x", *Value, 0);
270 for(int i = 0; i < MAX_LOADED_LIBRARIES && gLoadedLibraries[i].Base != 0; i ++)
272 // Allow ignoring the current module
273 if( gLoadedLibraries[i].Base == IgnoreBase ) {
274 //SysDebug("GetSymbol: Ignore %p", gLoadedLibraries[i].Base);
278 //SysDebug(" GetSymbol: Trying 0x%x, '%s'",
279 // gLoadedLibraries[i].Base, gLoadedLibraries[i].Name);
280 if(GetSymbolFromBase(gLoadedLibraries[i].Base, name, Value, Size)) {
281 //SysDebug("GetSymbol: Remote %p - %p", gLoadedLibraries[i].Base, *Value);
285 //SysDebug("GetSymbol: None");
290 \fn int GetSymbolFromBase(Uint base, char *name, Uint *ret)
291 \breif Gets a symbol from a specified library
293 int GetSymbolFromBase(void *base, const char *name, void **ret, size_t *Size)
296 if(hdr[0] == 0x7F && hdr[1] == 'E' && hdr[2] == 'L' && hdr[3] == 'F')
297 return ElfGetSymbol(base, name, ret, Size);
298 if(hdr[0] == 'M' && hdr[1] == 'Z')
299 return PE_GetSymbol(base, name, ret, Size);
300 SysDebug("Unknown type at %p (%02x %02x %02x %02x)", base,
301 hdr[0], hdr[1], hdr[2], hdr[3]);