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);
24 extern const int ciNumLocalExports;
27 tLoadedLib gLoadedLibraries[MAX_LOADED_LIBRARIES];
28 char gsLoadedStrings[MAX_STRINGS_BYTES];
29 char *gsNextAvailString = gsLoadedStrings;
30 //tLoadLib *gpLoadedLibraries = NULL;
33 char *FindLibrary(char *DestBuf, char *SoName, char *ExtraSearchDir)
35 // -- #1: Executable Specified
38 strcpy(DestBuf, ExtraSearchDir);
40 strcat(DestBuf, SoName);
41 if(file_exists(DestBuf)) return DestBuf;
45 strcpy(DestBuf, SYSTEM_LIB_DIR);
46 strcat(DestBuf, SoName);
47 if(file_exists(DestBuf)) return DestBuf;
49 // -- #3: Current Directory
50 if(file_exists(SoName)) return SoName;
57 Uint LoadLibrary(char *SoName, char *SearchDir, char **envp)
62 void (*fEntry)(int, int, char *[], char**);
64 DEBUGS("LoadLibrary: (filename='%s', envp=0x%x)\n", filename, envp);
67 filename = FindLibrary(sTmpName, SoName, SearchDir);
68 if(filename == NULL) {
69 DEBUGS("LoadLibrary: RETURN 0\n");
72 DEBUGS(" LoadLibrary: filename='%s'\n", filename);
74 if( (iArg = IsFileLoaded(filename)) )
78 iArg = SysLoadBin(filename, (Uint*)&fEntry);
80 DEBUGS("LoadLibrary: RETURN 0\n");
84 DEBUGS(" LoadLibrary: iArg=0x%x, iEntry=0x%x\n", iArg, fEntry);
87 fEntry = (void*)DoRelocate( iArg, envp, filename );
90 DEBUGS(" LoadLibrary: '%s' Entry 0x%x\n", SoName, fEntry);
91 fEntry(iArg, 0, NULL, envp);
93 DEBUGS("LoadLibrary: RETURN 1\n");
98 * \fn Uint IsFileLoaded(char *file)
99 * \brief Determine if a file is already loaded
101 Uint IsFileLoaded(char *file)
104 DEBUGS("IsFileLoaded: (file='%s')", file);
105 for( i = 0; i < MAX_LOADED_LIBRARIES; i++ )
107 if(gLoadedLibraries[i].Base == 0) break; // Last entry has Base set to NULL
108 DEBUGS(" strcmp('%s', '%s')", gLoadedLibraries[i].Name, file);
109 if(strcmp(gLoadedLibraries[i].Name, file) == 0) {
110 DEBUGS("IsFileLoaded: Found %i (0x%x)", i, gLoadedLibraries[i].Base);
111 return gLoadedLibraries[i].Base;
114 DEBUGS("IsFileLoaded: Not Found");
119 * \fn void AddLoaded(char *File, Uint base)
120 * \brief Add a file to the loaded list
122 void AddLoaded(char *File, Uint base)
125 char *name = gsNextAvailString;
127 DEBUGS("AddLoaded: (File='%s', base=0x%x)", File, base);
130 for( i = 0; i < MAX_LOADED_LIBRARIES; i ++ )
132 if(gLoadedLibraries[i].Base == 0) break;
134 if(i == MAX_LOADED_LIBRARIES) {
135 SysDebug("ERROR - ld-acess.so has run out of load slots!");
139 // Check space in string buffer
140 length = strlen(File);
141 if(&name[length+1] >= &gsLoadedStrings[MAX_STRINGS_BYTES]) {
142 SysDebug("ERROR - ld-acess.so has run out of string buffer memory!");
147 gLoadedLibraries[i].Base = base;
149 gLoadedLibraries[i].Name = name;
150 gsNextAvailString = &name[length+1];
151 DEBUGS("'%s' (0x%x) loaded as %i\n", name, base, i);
156 * \fn void Unload(Uint Base)
158 void Unload(Uint Base)
163 for( id = 0; id < MAX_LOADED_LIBRARIES; id++ )
165 if(gLoadedLibraries[id].Base == Base) break;
167 if(id == MAX_LOADED_LIBRARIES) return;
170 SysUnloadBin( Base );
171 // Save String Pointer
172 str = gLoadedLibraries[id].Name;
174 // Compact Loaded List
176 for( i = j + 1; i < MAX_LOADED_LIBRARIES; i++, j++ )
178 if(gLoadedLibraries[i].Base == 0) break;
180 strcpy(str, gLoadedLibraries[i].Name);
181 str += strlen(str)+1;
183 gLoadedLibraries[j].Base = gLoadedLibraries[i].Base;
184 gLoadedLibraries[j].Name = str;
188 gLoadedLibraries[j].Base = 0;
189 gLoadedLibraries[j].Name = NULL;
191 gsNextAvailString = str;
195 \fn Uint GetSymbol(char *name)
196 \brief Gets a symbol value from a loaded library
198 Uint GetSymbol(char *name)
203 //SysDebug("ciNumLocalExports = %i", ciNumLocalExports);
204 for(i=0;i<ciNumLocalExports;i++)
206 if( strcmp(caLocalExports[i].Name, name) == 0 )
207 return caLocalExports[i].Value;
210 for(i=0;i<sizeof(gLoadedLibraries)/sizeof(gLoadedLibraries[0]);i++)
212 if(gLoadedLibraries[i].Base == 0) break;
214 //SysDebug(" GetSymbol: Trying 0x%x, '%s'",
215 // gLoadedLibraries[i].Base, gLoadedLibraries[i].Name);
216 if(GetSymbolFromBase(gLoadedLibraries[i].Base, name, &ret)) return ret;
218 SysDebug("GetSymbol: === Symbol '%s' not found ===", name);
223 \fn int GetSymbolFromBase(Uint base, char *name, Uint *ret)
224 \breif Gets a symbol from a specified library
226 int GetSymbolFromBase(Uint base, char *name, Uint *ret)
228 if(*(Uint32*)base == (0x7F|('E'<<8)|('L'<<16)|('F'<<24)))
229 return ElfGetSymbol(base, name, ret);
230 if(*(Uint16*)base == ('M'|('Z'<<8)))
231 return PE_GetSymbol(base, name, ret);
232 SysDebug("Unknown type at %p", base);