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 const char *FindLibrary(char *DestBuf, const char *SoName, const char *ExtraSearchDir)
38 // -- #1: Executable Specified
41 strcpy(DestBuf, ExtraSearchDir);
43 strcat(DestBuf, SoName);
44 if(file_exists(DestBuf)) return DestBuf;
48 strcpy(DestBuf, SYSTEM_LIB_DIR);
49 strcat(DestBuf, SoName);
50 if(file_exists(DestBuf)) return DestBuf;
52 // -- #3: Current Directory
53 if(file_exists(SoName)) return SoName;
60 void *LoadLibrary(const char *SoName, const char *SearchDir, char **envp)
65 void (*fEntry)(void *, int, char *[], char**);
67 DEBUGS("LoadLibrary: (SoName='%s', SearchDir='%s', envp=%p)", SoName, SearchDir, envp);
70 filename = FindLibrary(sTmpName, SoName, SearchDir);
71 if(filename == NULL) {
72 DEBUGS("LoadLibrary: RETURN 0");
75 DEBUGS(" LoadLibrary: filename='%s'", filename);
77 if( (base = IsFileLoaded(filename)) )
80 DEBUGS(" LoadLibrary: SysLoadBin()");
82 base = _SysLoadBin(filename, (void**)&fEntry);
84 DEBUGS("LoadLibrary: RETURN 0");
88 DEBUGS(" LoadLibrary: iArg=%p, fEntry=%p", base, fEntry);
91 fEntry = DoRelocate( base, envp, filename );
97 DEBUGS(" LoadLibrary: '%s' Entry %p", SoName, fEntry);
98 fEntry(base, 0, NULL, gEnvP);
100 DEBUGS("LoadLibrary: RETURN 1");
105 * \fn Uint IsFileLoaded(char *file)
106 * \brief Determine if a file is already loaded
108 void *IsFileLoaded(const char *file)
111 DEBUGS("IsFileLoaded: (file='%s')", file);
113 // Applications link against either libld-acess.so or ld-acess.so
114 if( strcmp(file, "/Acess/Libs/libld-acess.so") == 0
115 || strcmp(file, "/Acess/Libs/ld-acess.so") == 0 )
117 DEBUGS("IsFileLoaded: Found local (%p)", &gLinkedBase);
121 for( i = 0; i < MAX_LOADED_LIBRARIES; i++ )
123 if(gLoadedLibraries[i].Base == 0) break; // Last entry has Base set to NULL
124 DEBUGS(" strcmp('%s', '%s')", gLoadedLibraries[i].Name, file);
125 if(strcmp(gLoadedLibraries[i].Name, file) == 0) {
126 DEBUGS("IsFileLoaded: Found %i (%p)", i, gLoadedLibraries[i].Base);
127 return gLoadedLibraries[i].Base;
130 DEBUGS("IsFileLoaded: Not Found");
135 * \fn void AddLoaded(char *File, Uint base)
136 * \brief Add a file to the loaded list
138 void AddLoaded(const char *File, void *base)
141 char *name = gsNextAvailString;
143 DEBUGS("AddLoaded: (File='%s', base=%p)", File, base);
146 for( i = 0; i < MAX_LOADED_LIBRARIES; i ++ )
148 if(gLoadedLibraries[i].Base == 0) break;
150 if(i == MAX_LOADED_LIBRARIES) {
151 SysDebug("ERROR - ld-acess.so has run out of load slots!");
155 // Check space in string buffer
156 length = strlen(File);
157 if(&name[length+1] >= &gsLoadedStrings[MAX_STRINGS_BYTES]) {
158 SysDebug("ERROR - ld-acess.so has run out of string buffer memory!");
163 gLoadedLibraries[i].Base = base;
165 gLoadedLibraries[i].Name = name;
166 gsNextAvailString = &name[length+1];
167 DEBUGS("'%s' (%p) loaded as %i", name, base, i);
172 * \fn void Unload(Uint Base)
174 void Unload(void *Base)
179 for( id = 0; id < MAX_LOADED_LIBRARIES; id++ )
181 if(gLoadedLibraries[id].Base == Base) break;
183 if(id == MAX_LOADED_LIBRARIES) return;
186 _SysUnloadBin( Base );
187 // Save String Pointer
188 str = gLoadedLibraries[id].Name;
190 // Compact Loaded List
192 for( i = j + 1; i < MAX_LOADED_LIBRARIES; i++, j++ )
194 if(gLoadedLibraries[i].Base == 0) break;
196 strcpy(str, gLoadedLibraries[i].Name);
197 str += strlen(str)+1;
199 gLoadedLibraries[j].Base = gLoadedLibraries[i].Base;
200 gLoadedLibraries[j].Name = str;
204 gLoadedLibraries[j].Base = 0;
205 gLoadedLibraries[j].Name = NULL;
207 gsNextAvailString = str;
211 \fn Uint GetSymbol(const char *name)
212 \brief Gets a symbol value from a loaded library
214 int GetSymbol(const char *name, void **Value, size_t *Size)
218 //SysDebug("ciNumLocalExports = %i", ciNumLocalExports);
219 for(i=0;i<ciNumLocalExports;i++)
221 if( strcmp(caLocalExports[i].Name, name) == 0 ) {
222 *Value = caLocalExports[i].Value;
229 // Entry 0 is ld-acess, ignore it
230 for(i = 0; i < MAX_LOADED_LIBRARIES; i ++)
232 if(gLoadedLibraries[i].Base == 0)
235 //SysDebug(" GetSymbol: Trying 0x%x, '%s'",
236 // gLoadedLibraries[i].Base, gLoadedLibraries[i].Name);
237 if(GetSymbolFromBase(gLoadedLibraries[i].Base, name, Value, Size))
240 SysDebug("GetSymbol: === Symbol '%s' not found ===", name);
245 \fn int GetSymbolFromBase(Uint base, char *name, Uint *ret)
246 \breif Gets a symbol from a specified library
248 int GetSymbolFromBase(void *base, const char *name, void **ret, size_t *Size)
251 if(hdr[0] == 0x7F && hdr[1] == 'E' && hdr[2] == 'L' && hdr[3] == 'F')
252 return ElfGetSymbol(base, name, ret, Size);
253 if(hdr[0] == 'M' && hdr[1] == 'Z')
254 return PE_GetSymbol(base, name, ret, Size);
255 SysDebug("Unknown type at %p (%02x %02x %02x %02x)", base,
256 hdr[0], hdr[1], hdr[2], hdr[3]);