2 AcessOS 1 - Dynamic Loader
12 # define DEBUGS(v...) SysDebug(v)
18 void *IsFileLoaded(const char *file);
25 extern const int ciNumLocalExports;
29 tLoadedLib gLoadedLibraries[MAX_LOADED_LIBRARIES];
30 char gsLoadedStrings[MAX_STRINGS_BYTES];
31 char *gsNextAvailString = gsLoadedStrings;
32 //tLoadLib *gpLoadedLibraries = NULL;
35 const char *FindLibrary(char *DestBuf, const char *SoName, const char *ExtraSearchDir)
37 // -- #1: Executable Specified
40 strcpy(DestBuf, ExtraSearchDir);
42 strcat(DestBuf, SoName);
43 if(file_exists(DestBuf)) return DestBuf;
47 strcpy(DestBuf, SYSTEM_LIB_DIR);
48 strcat(DestBuf, SoName);
49 if(file_exists(DestBuf)) return DestBuf;
51 // -- #3: Current Directory
52 if(file_exists(SoName)) return SoName;
59 void *LoadLibrary(const char *SoName, const char *SearchDir, char **envp)
64 void (*fEntry)(void *, int, char *[], char**);
66 DEBUGS("LoadLibrary: (SoName='%s', SearchDir='%s', envp=%p)", SoName, SearchDir, envp);
69 filename = FindLibrary(sTmpName, SoName, SearchDir);
70 if(filename == NULL) {
71 DEBUGS("LoadLibrary: RETURN 0");
74 DEBUGS(" LoadLibrary: filename='%s'", filename);
76 if( (base = IsFileLoaded(filename)) )
79 DEBUGS(" LoadLibrary: SysLoadBin()");
81 base = _SysLoadBin(filename, (void**)&fEntry);
83 DEBUGS("LoadLibrary: RETURN 0");
87 DEBUGS(" LoadLibrary: iArg=%p, fEntry=%p", base, fEntry);
90 fEntry = DoRelocate( base, envp, filename );
96 DEBUGS(" LoadLibrary: '%s' Entry %p", SoName, fEntry);
97 fEntry(base, 0, NULL, gEnvP);
99 DEBUGS("LoadLibrary: RETURN 1");
104 * \fn Uint IsFileLoaded(char *file)
105 * \brief Determine if a file is already loaded
107 void *IsFileLoaded(const char *file)
110 DEBUGS("IsFileLoaded: (file='%s')", file);
111 for( i = 0; i < MAX_LOADED_LIBRARIES; i++ )
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;
120 DEBUGS("IsFileLoaded: Not Found");
125 * \fn void AddLoaded(char *File, Uint base)
126 * \brief Add a file to the loaded list
128 void AddLoaded(const char *File, void *base)
131 char *name = gsNextAvailString;
133 DEBUGS("AddLoaded: (File='%s', base=%p)", File, base);
136 for( i = 0; i < MAX_LOADED_LIBRARIES; i ++ )
138 if(gLoadedLibraries[i].Base == 0) break;
140 if(i == MAX_LOADED_LIBRARIES) {
141 SysDebug("ERROR - ld-acess.so has run out of load slots!");
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!");
153 gLoadedLibraries[i].Base = base;
155 gLoadedLibraries[i].Name = name;
156 gsNextAvailString = &name[length+1];
157 DEBUGS("'%s' (%p) loaded as %i", name, base, i);
162 * \fn void Unload(Uint Base)
164 void Unload(void *Base)
169 for( id = 0; id < MAX_LOADED_LIBRARIES; id++ )
171 if(gLoadedLibraries[id].Base == Base) break;
173 if(id == MAX_LOADED_LIBRARIES) return;
176 _SysUnloadBin( Base );
177 // Save String Pointer
178 str = gLoadedLibraries[id].Name;
180 // Compact Loaded List
182 for( i = j + 1; i < MAX_LOADED_LIBRARIES; i++, j++ )
184 if(gLoadedLibraries[i].Base == 0) break;
186 strcpy(str, gLoadedLibraries[i].Name);
187 str += strlen(str)+1;
189 gLoadedLibraries[j].Base = gLoadedLibraries[i].Base;
190 gLoadedLibraries[j].Name = str;
194 gLoadedLibraries[j].Base = 0;
195 gLoadedLibraries[j].Name = NULL;
197 gsNextAvailString = str;
201 \fn Uint GetSymbol(const char *name)
202 \brief Gets a symbol value from a loaded library
204 int GetSymbol(const char *name, void **Value, size_t *Size)
208 //SysDebug("ciNumLocalExports = %i", ciNumLocalExports);
209 for(i=0;i<ciNumLocalExports;i++)
211 if( strcmp(caLocalExports[i].Name, name) == 0 ) {
212 *Value = caLocalExports[i].Value;
219 // Entry 0 is ld-acess, ignore it
220 for(i = 1; i < MAX_LOADED_LIBRARIES; i ++)
222 if(gLoadedLibraries[i].Base == 0)
225 //SysDebug(" GetSymbol: Trying 0x%x, '%s'",
226 // gLoadedLibraries[i].Base, gLoadedLibraries[i].Name);
227 if(GetSymbolFromBase(gLoadedLibraries[i].Base, name, Value, Size))
230 SysDebug("GetSymbol: === Symbol '%s' not found ===", name);
235 \fn int GetSymbolFromBase(Uint base, char *name, Uint *ret)
236 \breif Gets a symbol from a specified library
238 int GetSymbolFromBase(void *base, const char *name, void **ret, size_t *Size)
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]);