2 AcessOS 1 - Dynamic Loader
13 # define DEBUGS(v...) SysDebug(v)
18 #define MAX_QUEUED_ENTRYPOINTS 8
21 extern const tLocalExport caLocalExports[];
22 extern const int ciNumLocalExports;
24 extern char gLinkedBase[];
27 typedef void tLibEntry(void *, int, char *[], char**);
30 void *IsFileLoaded(const char *file);
33 tLoadedLib gLoadedLibraries[MAX_LOADED_LIBRARIES];
34 char gsLoadedStrings[MAX_STRINGS_BYTES];
35 char *gsNextAvailString = gsLoadedStrings;
39 } gaQueuedEntrypoints[MAX_QUEUED_ENTRYPOINTS];
40 int giNumQueuedEntrypoints;
41 //tLoadLib *gpLoadedLibraries = NULL;
44 void ldacess_DumpLoadedLibraries(void)
46 for( int i = 0; i < MAX_LOADED_LIBRARIES; i ++ )
48 const tLoadedLib* ll = &gLoadedLibraries[i];
49 if(ll->Base == 0) break; // Last entry has Base set to NULL
50 _SysDebug("%p: %s", ll->Base, ll->Name);
55 * \brief Call queued up entry points (after relocations completed)
57 void CallQueuedEntrypoints(char **EnvP)
59 while( giNumQueuedEntrypoints )
61 giNumQueuedEntrypoints --;
62 const struct sQueuedEntry *qe = &gaQueuedEntrypoints[giNumQueuedEntrypoints];
63 DEBUGS("Calling EP %p for %p", qe->Entry, qe->Base);
64 qe->Entry(qe->Base, 0, NULL, EnvP);
68 const char *FindLibrary(char *DestBuf, const char *SoName, const char *ExtraSearchDir)
70 // -- #1: Executable Specified
73 strcpy(DestBuf, ExtraSearchDir);
75 strcat(DestBuf, SoName);
76 if(file_exists(DestBuf)) return DestBuf;
80 strcpy(DestBuf, SYSTEM_LIB_DIR);
81 strcat(DestBuf, SoName);
82 if(file_exists(DestBuf)) return DestBuf;
84 // -- #3: Current Directory
85 if(file_exists(SoName)) return SoName;
92 void *LoadLibrary(const char *SoName, const char *SearchDir, char **envp)
97 DEBUGS("LoadLibrary: (SoName='%s', SearchDir='%s', envp=%p)", SoName, SearchDir, envp);
100 const char *filename = FindLibrary(sTmpName, SoName, SearchDir);
101 if(filename == NULL) {
102 DEBUGS("LoadLibrary: RETURN 0");
105 DEBUGS(" LoadLibrary: filename='%s'", filename);
107 if( (base = IsFileLoaded(filename)) )
110 DEBUGS(" LoadLibrary: SysLoadBin()");
113 base = _SysLoadBin(filename, (void**)&fEntry);
115 DEBUGS("LoadLibrary: RETURN 0");
119 DEBUGS(" LoadLibrary: iArg=%p, fEntry=%p", base, fEntry);
122 fEntry = DoRelocate( base, envp, filename );
128 // - TODO: Queue entrypoint calls
129 if( giNumQueuedEntrypoints >= MAX_QUEUED_ENTRYPOINTS ) {
130 SysDebug("ERROR - Maximum number of queued entrypoints exceeded on %p '%s'",
134 gaQueuedEntrypoints[giNumQueuedEntrypoints].Base = base;
135 gaQueuedEntrypoints[giNumQueuedEntrypoints].Entry = fEntry;
136 giNumQueuedEntrypoints ++;
138 DEBUGS("LoadLibrary: RETURN success");
143 * \fn Uint IsFileLoaded(char *file)
144 * \brief Determine if a file is already loaded
146 void *IsFileLoaded(const char *file)
148 DEBUGS("IsFileLoaded: (file='%s')", file);
150 // Applications link against either libld-acess.so or ld-acess.so
151 if( strcmp(file, "/Acess/Libs/libld-acess.so") == 0
152 || strcmp(file, "/Acess/Libs/ld-acess.so") == 0 )
154 DEBUGS("IsFileLoaded: Found local (%p)", &gLinkedBase);
158 for( int i = 0; i < MAX_LOADED_LIBRARIES; i++ )
160 if(gLoadedLibraries[i].Base == 0) break; // Last entry has Base set to NULL
161 DEBUGS(" strcmp('%s', '%s')", gLoadedLibraries[i].Name, file);
162 if(strcmp(gLoadedLibraries[i].Name, file) == 0) {
163 DEBUGS("IsFileLoaded: Found %i (%p)", i, gLoadedLibraries[i].Base);
164 return gLoadedLibraries[i].Base;
167 DEBUGS("IsFileLoaded: Not Found");
172 * \fn void AddLoaded(char *File, Uint base)
173 * \brief Add a file to the loaded list
175 void AddLoaded(const char *File, void *base)
178 char *name = gsNextAvailString;
180 DEBUGS("AddLoaded: (File='%s', base=%p)", File, base);
183 for( i = 0; i < MAX_LOADED_LIBRARIES; i ++ )
185 if(gLoadedLibraries[i].Base == 0) break;
187 if(i == MAX_LOADED_LIBRARIES) {
188 SysDebug("ERROR - ld-acess.so has run out of load slots!");
192 // Check space in string buffer
193 length = strlen(File);
194 if(&name[length+1] >= &gsLoadedStrings[MAX_STRINGS_BYTES]) {
195 SysDebug("ERROR - ld-acess.so has run out of string buffer memory!");
200 gLoadedLibraries[i].Base = base;
202 gLoadedLibraries[i].Name = name;
203 gsNextAvailString = &name[length+1];
204 DEBUGS("'%s' (%p) loaded as %i", name, base, i);
209 * \fn void Unload(Uint Base)
211 void Unload(void *Base)
215 for( id = 0; id < MAX_LOADED_LIBRARIES; id++ )
217 if(gLoadedLibraries[id].Base == Base) break;
219 if(id == MAX_LOADED_LIBRARIES) return;
222 _SysUnloadBin( Base );
223 // Save String Pointer
224 str = gLoadedLibraries[id].Name;
226 // Compact Loaded List
228 for( int i = j + 1; i < MAX_LOADED_LIBRARIES; i++, j++ )
230 if(gLoadedLibraries[i].Base == 0) break;
232 strcpy(str, gLoadedLibraries[i].Name);
233 str += strlen(str)+1;
235 gLoadedLibraries[j].Base = gLoadedLibraries[i].Base;
236 gLoadedLibraries[j].Name = str;
240 gLoadedLibraries[j].Base = 0;
241 gLoadedLibraries[j].Name = NULL;
243 gsNextAvailString = str;
247 \fn Uint GetSymbol(const char *name)
248 \brief Gets a symbol value from a loaded library
250 int GetSymbol(const char *name, void **Value, size_t *Size, void *IgnoreBase)
255 //SysDebug("GetSymbol: (%s)");
256 for( int i = 0; i < ciNumLocalExports; i ++ )
258 const tLocalExport* le = &caLocalExports[i];
259 if( strcmp(le->Name, name) == 0 ) {
263 DEBUGS("'%s' = Local %p+%#x", name, le->Value, 0);
268 bool have_weak = false;
269 for(int i = 0; i < MAX_LOADED_LIBRARIES && gLoadedLibraries[i].Base != 0; i ++)
271 const tLoadedLib* ll = &gLoadedLibraries[i];
272 // Allow ignoring the current module
273 if( ll->Base == IgnoreBase ) {
274 //SysDebug("GetSymbol: Ignore %p", gLoadedLibraries[i].Base);
278 //SysDebug(" GetSymbol: Trying 0x%x, '%s'", ll->Base, ll->Name);
281 int rv = GetSymbolFromBase(ll->Base, name, &tmpval, &tmpsize);
287 DEBUGS("'%s' = %p '%s' Strong %p+%#x", name, ll->Base, ll->Name, *Value, *Size);
294 DEBUGS("'%s' = Weak %p+%#x", name, *Value, *Size);
298 DEBUGS("'%s' = ?", name);
304 \fn int GetSymbolFromBase(Uint base, char *name, Uint *ret)
305 \breif Gets a symbol from a specified library
307 int GetSymbolFromBase(void *base, const char *name, void **ret, size_t *Size)
310 if(hdr[0] == 0x7F && hdr[1] == 'E' && hdr[2] == 'L' && hdr[3] == 'F')
311 return ElfGetSymbol(base, name, ret, Size);
312 if(hdr[0] == 'M' && hdr[1] == 'Z')
313 return PE_GetSymbol(base, name, ret, Size);
314 SysDebug("Unknown type at %p (%02x %02x %02x %02x)", base,
315 hdr[0], hdr[1], hdr[2], hdr[3]);