2 AcessOS 1 - Dynamic Loader
10 # define DEBUGS(v...) SysDebug(v)
16 Uint IsFileLoaded(char *file);
17 int GetSymbolFromBase(Uint base, char *name, Uint *ret);
23 } caLocalExports[] = {
24 {(Uint)gLoadedLibraries, "gLoadedLibraries"}
28 tLoadedLib gLoadedLibraries[MAX_LOADED_LIBRARIES];
29 char gsLoadedStrings[MAX_STRINGS_BYTES];
30 char *gsNextAvailString = gsLoadedStrings;
31 //tLoadLib *gpLoadedLibraries = NULL;
34 char *FindLibrary(char *DestBuf, char *SoName, 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 Uint LoadLibrary(char *SoName, char *SearchDir, char **envp)
63 void (*fEntry)(int, int, char *[], char**);
65 DEBUGS("LoadLibrary: (filename='%s', envp=0x%x)\n", filename, envp);
68 filename = FindLibrary(sTmpName, SoName, SearchDir);
69 if(filename == NULL) {
70 DEBUGS("LoadLibrary: RETURN 0\n");
73 DEBUGS(" LoadLibrary: filename='%s'\n", filename);
75 if( (iArg = IsFileLoaded(filename)) )
79 iArg = SysLoadBin(filename, (Uint*)&fEntry);
81 DEBUGS("LoadLibrary: RETURN 0\n");
85 DEBUGS(" LoadLibrary: iArg=0x%x, iEntry=0x%x\n", iArg, fEntry);
88 fEntry = (void*)DoRelocate( iArg, envp, filename );
91 DEBUGS(" LoadLibrary: '%s' Entry 0x%x\n", SoName, fEntry);
92 fEntry(iArg, 0, NULL, envp);
94 DEBUGS("LoadLibrary: RETURN 1\n");
99 * \fn Uint IsFileLoaded(char *file)
100 * \brief Determine if a file is already loaded
102 Uint IsFileLoaded(char *file)
105 DEBUGS("IsFileLoaded: (file='%s')", file);
106 for( i = 0; i < MAX_LOADED_LIBRARIES; i++ )
108 if(gLoadedLibraries[i].Base == 0) break; // Last entry has Base set to NULL
109 DEBUGS(" strcmp('%s', '%s')", gLoadedLibraries[i].Name, file);
110 if(strcmp(gLoadedLibraries[i].Name, file) == 0) {
111 DEBUGS("IsFileLoaded: Found %i (0x%x)", i, gLoadedLibraries[i].Base);
112 return gLoadedLibraries[i].Base;
115 DEBUGS("IsFileLoaded: Not Found");
120 * \fn void AddLoaded(char *File, Uint base)
121 * \brief Add a file to the loaded list
123 void AddLoaded(char *File, Uint base)
126 char *name = gsNextAvailString;
128 DEBUGS("AddLoaded: (File='%s', base=0x%x)", File, base);
131 for( i = 0; i < MAX_LOADED_LIBRARIES; i ++ )
133 if(gLoadedLibraries[i].Base == 0) break;
135 if(i == MAX_LOADED_LIBRARIES) {
136 SysDebug("ERROR - ld-acess.so has run out of load slots!");
140 // Check space in string buffer
141 length = strlen(File);
142 if(&name[length+1] >= &gsLoadedStrings[MAX_STRINGS_BYTES]) {
143 SysDebug("ERROR - ld-acess.so has run out of string buffer memory!");
148 gLoadedLibraries[i].Base = base;
150 gLoadedLibraries[i].Name = name;
151 gsNextAvailString = &name[length+1];
152 DEBUGS("'%s' (0x%x) loaded as %i\n", name, base, i);
157 * \fn void Unload(Uint Base)
159 void Unload(Uint Base)
164 for( id = 0; id < MAX_LOADED_LIBRARIES; id++ )
166 if(gLoadedLibraries[id].Base == Base) break;
168 if(id == MAX_LOADED_LIBRARIES) return;
171 SysUnloadBin( Base );
172 // Save String Pointer
173 str = gLoadedLibraries[id].Name;
175 // Compact Loaded List
177 for( i = j + 1; i < MAX_LOADED_LIBRARIES; i++, j++ )
179 if(gLoadedLibraries[i].Base == 0) break;
181 strcpy(str, gLoadedLibraries[i].Name);
182 str += strlen(str)+1;
184 gLoadedLibraries[j].Base = gLoadedLibraries[i].Base;
185 gLoadedLibraries[j].Name = str;
189 gLoadedLibraries[j].Base = 0;
190 gLoadedLibraries[j].Name = NULL;
192 gsNextAvailString = str;
196 \fn Uint GetSymbol(char *name)
197 \brief Gets a symbol value from a loaded library
199 Uint GetSymbol(char *name)
203 for(i=0;i<sizeof(caLocalExports)/sizeof(caLocalExports[0]);i++)
205 if( strcmp(caLocalExports[i].Name, name) == 0 )
206 return caLocalExports[i].Value;
209 for(i=0;i<sizeof(gLoadedLibraries)/sizeof(gLoadedLibraries[0]);i++)
211 if(gLoadedLibraries[i].Base == 0) break;
213 //SysDebug(" GetSymbol: Trying 0x%x, '%s'\n",
214 // gLoadedLibraries[i].Base, gLoadedLibraries[i].Name);
215 if(GetSymbolFromBase(gLoadedLibraries[i].Base, name, &ret)) return ret;
217 SysDebug("GetSymbol: === Symbol '%s' not found ===\n", name);
222 \fn int GetSymbolFromBase(Uint base, char *name, Uint *ret)
223 \breif Gets a symbol from a specified library
225 int GetSymbolFromBase(Uint base, char *name, Uint *ret)
227 if(*(Uint32*)base == (0x7F|('E'<<8)|('L'<<16)|('F'<<24)))
228 return ElfGetSymbol(base, name, ret);
229 if(*(Uint16*)base == ('M'|('Z'<<8)))
230 return PE_GetSymbol(base, name, ret);