2 AcessOS 1 - Dynamic Loader
11 # define DEBUGS(v...) SysDebug(v)
17 void *IsFileLoaded(const char *file);
24 extern const int ciNumLocalExports;
28 tLoadedLib gLoadedLibraries[MAX_LOADED_LIBRARIES];
29 char gsLoadedStrings[MAX_STRINGS_BYTES];
30 char *gsNextAvailString = gsLoadedStrings;
31 //tLoadLib *gpLoadedLibraries = NULL;
34 const char *FindLibrary(char *DestBuf, const char *SoName, const char *ExtraSearchDir)
36 // -- #1: Executable Specified
39 strcpy(DestBuf, ExtraSearchDir);
41 strcat(DestBuf, SoName);
42 if(file_exists(DestBuf)) return DestBuf;
46 strcpy(DestBuf, SYSTEM_LIB_DIR);
47 strcat(DestBuf, SoName);
48 if(file_exists(DestBuf)) return DestBuf;
50 // -- #3: Current Directory
51 if(file_exists(SoName)) return SoName;
58 void *LoadLibrary(const char *SoName, const char *SearchDir, char **envp)
63 void (*fEntry)(void *, int, char *[], char**);
65 DEBUGS("LoadLibrary: (SoName='%s', SearchDir='%s', envp=%p)", SoName, SearchDir, envp);
68 filename = FindLibrary(sTmpName, SoName, SearchDir);
69 if(filename == NULL) {
70 DEBUGS("LoadLibrary: RETURN 0");
73 DEBUGS(" LoadLibrary: filename='%s'", filename);
75 if( (base = IsFileLoaded(filename)) )
78 DEBUGS(" LoadLibrary: SysLoadBin()");
80 base = SysLoadBin(filename, (void**)&fEntry);
82 DEBUGS("LoadLibrary: RETURN 0");
86 DEBUGS(" LoadLibrary: iArg=%p, fEntry=%p", base, fEntry);
89 fEntry = DoRelocate( base, envp, filename );
95 DEBUGS(" LoadLibrary: '%s' Entry %p", SoName, fEntry);
96 fEntry(base, 0, NULL, gEnvP);
98 DEBUGS("LoadLibrary: RETURN 1");
103 * \fn Uint IsFileLoaded(char *file)
104 * \brief Determine if a file is already loaded
106 void *IsFileLoaded(const char *file)
109 DEBUGS("IsFileLoaded: (file='%s')", file);
110 for( i = 0; i < MAX_LOADED_LIBRARIES; i++ )
112 if(gLoadedLibraries[i].Base == 0) break; // Last entry has Base set to NULL
113 DEBUGS(" strcmp('%s', '%s')", gLoadedLibraries[i].Name, file);
114 if(strcmp(gLoadedLibraries[i].Name, file) == 0) {
115 DEBUGS("IsFileLoaded: Found %i (%p)", i, gLoadedLibraries[i].Base);
116 return gLoadedLibraries[i].Base;
119 DEBUGS("IsFileLoaded: Not Found");
124 * \fn void AddLoaded(char *File, Uint base)
125 * \brief Add a file to the loaded list
127 void AddLoaded(const char *File, void *base)
130 char *name = gsNextAvailString;
132 DEBUGS("AddLoaded: (File='%s', base=%p)", File, base);
135 for( i = 0; i < MAX_LOADED_LIBRARIES; i ++ )
137 if(gLoadedLibraries[i].Base == 0) break;
139 if(i == MAX_LOADED_LIBRARIES) {
140 SysDebug("ERROR - ld-acess.so has run out of load slots!");
144 // Check space in string buffer
145 length = strlen(File);
146 if(&name[length+1] >= &gsLoadedStrings[MAX_STRINGS_BYTES]) {
147 SysDebug("ERROR - ld-acess.so has run out of string buffer memory!");
152 gLoadedLibraries[i].Base = base;
154 gLoadedLibraries[i].Name = name;
155 gsNextAvailString = &name[length+1];
156 DEBUGS("'%s' (%p) loaded as %i", name, base, i);
161 * \fn void Unload(Uint Base)
163 void Unload(void *Base)
168 for( id = 0; id < MAX_LOADED_LIBRARIES; id++ )
170 if(gLoadedLibraries[id].Base == Base) break;
172 if(id == MAX_LOADED_LIBRARIES) return;
175 SysUnloadBin( Base );
176 // Save String Pointer
177 str = gLoadedLibraries[id].Name;
179 // Compact Loaded List
181 for( i = j + 1; i < MAX_LOADED_LIBRARIES; i++, j++ )
183 if(gLoadedLibraries[i].Base == 0) break;
185 strcpy(str, gLoadedLibraries[i].Name);
186 str += strlen(str)+1;
188 gLoadedLibraries[j].Base = gLoadedLibraries[i].Base;
189 gLoadedLibraries[j].Name = str;
193 gLoadedLibraries[j].Base = 0;
194 gLoadedLibraries[j].Name = NULL;
196 gsNextAvailString = str;
200 \fn Uint GetSymbol(const char *name)
201 \brief Gets a symbol value from a loaded library
203 int GetSymbol(const char *name, void **Value, size_t *Size)
207 //SysDebug("ciNumLocalExports = %i", ciNumLocalExports);
208 for(i=0;i<ciNumLocalExports;i++)
210 if( strcmp(caLocalExports[i].Name, name) == 0 ) {
211 *Value = caLocalExports[i].Value;
218 // Entry 0 is ld-acess, ignore it
219 for(i = 1; i < MAX_LOADED_LIBRARIES; i ++)
221 if(gLoadedLibraries[i].Base == 0)
224 //SysDebug(" GetSymbol: Trying 0x%x, '%s'",
225 // gLoadedLibraries[i].Base, gLoadedLibraries[i].Name);
226 if(GetSymbolFromBase(gLoadedLibraries[i].Base, name, Value, Size))
229 SysDebug("GetSymbol: === Symbol '%s' not found ===", name);
234 \fn int GetSymbolFromBase(Uint base, char *name, Uint *ret)
235 \breif Gets a symbol from a specified library
237 int GetSymbolFromBase(void *base, const char *name, void **ret, size_t *Size)
240 if(hdr[0] == 0x7F && hdr[1] == 'E' && hdr[2] == 'L' && hdr[3] == 'F')
241 return ElfGetSymbol(base, name, ret, Size);
242 if(hdr[0] == 'M' && hdr[1] == 'Z')
243 return PE_GetSymbol(base, name, ret, Size);
244 SysDebug("Unknown type at %p (%02x %02x %02x %02x)", base,
245 hdr[0], hdr[1], hdr[2], hdr[3]);