2 AcessOS 1 - Dynamic Loader
12 # define DEBUGS(v...) SysDebug(v)
18 void *IsFileLoaded(const char *file);
25 extern const int ciNumLocalExports;
27 extern char gLinkedBase[];
30 tLoadedLib gLoadedLibraries[MAX_LOADED_LIBRARIES];
31 char gsLoadedStrings[MAX_STRINGS_BYTES];
32 char *gsNextAvailString = gsLoadedStrings;
33 //tLoadLib *gpLoadedLibraries = NULL;
36 void ldacess_DumpLoadedLibraries(void)
38 for( int i = 0; i < MAX_LOADED_LIBRARIES; i ++ )
40 if(gLoadedLibraries[i].Base == 0) break; // Last entry has Base set to NULL
42 gLoadedLibraries[i].Base,
43 gLoadedLibraries[i].Name
48 const char *FindLibrary(char *DestBuf, const char *SoName, const char *ExtraSearchDir)
50 // -- #1: Executable Specified
53 strcpy(DestBuf, ExtraSearchDir);
55 strcat(DestBuf, SoName);
56 if(file_exists(DestBuf)) return DestBuf;
60 strcpy(DestBuf, SYSTEM_LIB_DIR);
61 strcat(DestBuf, SoName);
62 if(file_exists(DestBuf)) return DestBuf;
64 // -- #3: Current Directory
65 if(file_exists(SoName)) return SoName;
72 void *LoadLibrary(const char *SoName, const char *SearchDir, char **envp)
77 void (*fEntry)(void *, int, char *[], char**);
79 DEBUGS("LoadLibrary: (SoName='%s', SearchDir='%s', envp=%p)", SoName, SearchDir, envp);
82 filename = FindLibrary(sTmpName, SoName, SearchDir);
83 if(filename == NULL) {
84 DEBUGS("LoadLibrary: RETURN 0");
87 DEBUGS(" LoadLibrary: filename='%s'", filename);
89 if( (base = IsFileLoaded(filename)) )
92 DEBUGS(" LoadLibrary: SysLoadBin()");
94 base = _SysLoadBin(filename, (void**)&fEntry);
96 DEBUGS("LoadLibrary: RETURN 0");
100 DEBUGS(" LoadLibrary: iArg=%p, fEntry=%p", base, fEntry);
103 fEntry = DoRelocate( base, envp, filename );
109 DEBUGS(" LoadLibrary: '%s' Entry %p", SoName, fEntry);
110 fEntry(base, 0, NULL, gEnvP);
112 DEBUGS("LoadLibrary: RETURN 1");
117 * \fn Uint IsFileLoaded(char *file)
118 * \brief Determine if a file is already loaded
120 void *IsFileLoaded(const char *file)
123 DEBUGS("IsFileLoaded: (file='%s')", file);
125 // Applications link against either libld-acess.so or ld-acess.so
126 if( strcmp(file, "/Acess/Libs/libld-acess.so") == 0
127 || strcmp(file, "/Acess/Libs/ld-acess.so") == 0 )
129 DEBUGS("IsFileLoaded: Found local (%p)", &gLinkedBase);
133 for( i = 0; i < MAX_LOADED_LIBRARIES; i++ )
135 if(gLoadedLibraries[i].Base == 0) break; // Last entry has Base set to NULL
136 DEBUGS(" strcmp('%s', '%s')", gLoadedLibraries[i].Name, file);
137 if(strcmp(gLoadedLibraries[i].Name, file) == 0) {
138 DEBUGS("IsFileLoaded: Found %i (%p)", i, gLoadedLibraries[i].Base);
139 return gLoadedLibraries[i].Base;
142 DEBUGS("IsFileLoaded: Not Found");
147 * \fn void AddLoaded(char *File, Uint base)
148 * \brief Add a file to the loaded list
150 void AddLoaded(const char *File, void *base)
153 char *name = gsNextAvailString;
155 DEBUGS("AddLoaded: (File='%s', base=%p)", File, base);
158 for( i = 0; i < MAX_LOADED_LIBRARIES; i ++ )
160 if(gLoadedLibraries[i].Base == 0) break;
162 if(i == MAX_LOADED_LIBRARIES) {
163 SysDebug("ERROR - ld-acess.so has run out of load slots!");
167 // Check space in string buffer
168 length = strlen(File);
169 if(&name[length+1] >= &gsLoadedStrings[MAX_STRINGS_BYTES]) {
170 SysDebug("ERROR - ld-acess.so has run out of string buffer memory!");
175 gLoadedLibraries[i].Base = base;
177 gLoadedLibraries[i].Name = name;
178 gsNextAvailString = &name[length+1];
179 DEBUGS("'%s' (%p) loaded as %i", name, base, i);
184 * \fn void Unload(Uint Base)
186 void Unload(void *Base)
191 for( id = 0; id < MAX_LOADED_LIBRARIES; id++ )
193 if(gLoadedLibraries[id].Base == Base) break;
195 if(id == MAX_LOADED_LIBRARIES) return;
198 _SysUnloadBin( Base );
199 // Save String Pointer
200 str = gLoadedLibraries[id].Name;
202 // Compact Loaded List
204 for( i = j + 1; i < MAX_LOADED_LIBRARIES; i++, j++ )
206 if(gLoadedLibraries[i].Base == 0) break;
208 strcpy(str, gLoadedLibraries[i].Name);
209 str += strlen(str)+1;
211 gLoadedLibraries[j].Base = gLoadedLibraries[i].Base;
212 gLoadedLibraries[j].Name = str;
216 gLoadedLibraries[j].Base = 0;
217 gLoadedLibraries[j].Name = NULL;
219 gsNextAvailString = str;
223 \fn Uint GetSymbol(const char *name)
224 \brief Gets a symbol value from a loaded library
226 int GetSymbol(const char *name, void **Value, size_t *Size)
230 //SysDebug("ciNumLocalExports = %i", ciNumLocalExports);
231 for(i=0;i<ciNumLocalExports;i++)
233 if( strcmp(caLocalExports[i].Name, name) == 0 ) {
234 *Value = caLocalExports[i].Value;
241 // Entry 0 is ld-acess, ignore it
242 for(i = 0; i < MAX_LOADED_LIBRARIES; i ++)
244 if(gLoadedLibraries[i].Base == 0)
247 //SysDebug(" GetSymbol: Trying 0x%x, '%s'",
248 // gLoadedLibraries[i].Base, gLoadedLibraries[i].Name);
249 if(GetSymbolFromBase(gLoadedLibraries[i].Base, name, Value, Size))
252 SysDebug("GetSymbol: === Symbol '%s' not found ===", name);
257 \fn int GetSymbolFromBase(Uint base, char *name, Uint *ret)
258 \breif Gets a symbol from a specified library
260 int GetSymbolFromBase(void *base, const char *name, void **ret, size_t *Size)
263 if(hdr[0] == 0x7F && hdr[1] == 'E' && hdr[2] == 'L' && hdr[3] == 'F')
264 return ElfGetSymbol(base, name, ret, Size);
265 if(hdr[0] == 'M' && hdr[1] == 'Z')
266 return PE_GetSymbol(base, name, ret, Size);
267 SysDebug("Unknown type at %p (%02x %02x %02x %02x)", base,
268 hdr[0], hdr[1], hdr[2], hdr[3]);