2 AcessOS 1 - Dynamic Loader
11 # define DEBUGS(v...) SysDebug(v)
17 void *IsFileLoaded(const char *file);
18 int GetSymbolFromBase(void *base, const char *name, void **ret);
25 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 );
92 DEBUGS(" LoadLibrary: '%s' Entry %p", SoName, fEntry);
93 fEntry(base, 0, NULL, envp);
95 DEBUGS("LoadLibrary: RETURN 1");
100 * \fn Uint IsFileLoaded(char *file)
101 * \brief Determine if a file is already loaded
103 void *IsFileLoaded(const char *file)
106 DEBUGS("IsFileLoaded: (file='%s')", file);
107 for( i = 0; i < MAX_LOADED_LIBRARIES; i++ )
109 if(gLoadedLibraries[i].Base == 0) break; // Last entry has Base set to NULL
110 DEBUGS(" strcmp('%s', '%s')", gLoadedLibraries[i].Name, file);
111 if(strcmp(gLoadedLibraries[i].Name, file) == 0) {
112 DEBUGS("IsFileLoaded: Found %i (%p)", i, gLoadedLibraries[i].Base);
113 return gLoadedLibraries[i].Base;
116 DEBUGS("IsFileLoaded: Not Found");
121 * \fn void AddLoaded(char *File, Uint base)
122 * \brief Add a file to the loaded list
124 void AddLoaded(const char *File, void *base)
127 char *name = gsNextAvailString;
129 DEBUGS("AddLoaded: (File='%s', base=%p)", File, base);
132 for( i = 0; i < MAX_LOADED_LIBRARIES; i ++ )
134 if(gLoadedLibraries[i].Base == 0) break;
136 if(i == MAX_LOADED_LIBRARIES) {
137 SysDebug("ERROR - ld-acess.so has run out of load slots!");
141 // Check space in string buffer
142 length = strlen(File);
143 if(&name[length+1] >= &gsLoadedStrings[MAX_STRINGS_BYTES]) {
144 SysDebug("ERROR - ld-acess.so has run out of string buffer memory!");
149 gLoadedLibraries[i].Base = base;
151 gLoadedLibraries[i].Name = name;
152 gsNextAvailString = &name[length+1];
153 DEBUGS("'%s' (%p) loaded as %i", name, base, i);
158 * \fn void Unload(Uint Base)
160 void Unload(void *Base)
165 for( id = 0; id < MAX_LOADED_LIBRARIES; id++ )
167 if(gLoadedLibraries[id].Base == Base) break;
169 if(id == MAX_LOADED_LIBRARIES) return;
172 SysUnloadBin( Base );
173 // Save String Pointer
174 str = gLoadedLibraries[id].Name;
176 // Compact Loaded List
178 for( i = j + 1; i < MAX_LOADED_LIBRARIES; i++, j++ )
180 if(gLoadedLibraries[i].Base == 0) break;
182 strcpy(str, gLoadedLibraries[i].Name);
183 str += strlen(str)+1;
185 gLoadedLibraries[j].Base = gLoadedLibraries[i].Base;
186 gLoadedLibraries[j].Name = str;
190 gLoadedLibraries[j].Base = 0;
191 gLoadedLibraries[j].Name = NULL;
193 gsNextAvailString = str;
197 \fn Uint GetSymbol(const char *name)
198 \brief Gets a symbol value from a loaded library
200 void *GetSymbol(const char *name)
205 //SysDebug("ciNumLocalExports = %i", ciNumLocalExports);
206 for(i=0;i<ciNumLocalExports;i++)
208 if( strcmp(caLocalExports[i].Name, name) == 0 )
209 return caLocalExports[i].Value;
212 // Entry 0 is ld-acess, ignore it
213 for(i = 1; i < MAX_LOADED_LIBRARIES; i ++)
215 if(gLoadedLibraries[i].Base == 0) break;
217 //SysDebug(" GetSymbol: Trying 0x%x, '%s'",
218 // gLoadedLibraries[i].Base, gLoadedLibraries[i].Name);
219 if(GetSymbolFromBase(gLoadedLibraries[i].Base, name, &ret)) return ret;
221 SysDebug("GetSymbol: === Symbol '%s' not found ===", name);
226 \fn int GetSymbolFromBase(Uint base, char *name, Uint *ret)
227 \breif Gets a symbol from a specified library
229 int GetSymbolFromBase(void *base, const char *name, void **ret)
232 if(hdr[0] == 0x7F && hdr[1] == 'E' && hdr[2] == 'L' && hdr[3] == 'F')
233 return ElfGetSymbol(base, name, ret);
234 if(hdr[0] == 'M' && hdr[1] == 'Z')
235 return PE_GetSymbol(base, name, ret);
236 SysDebug("Unknown type at %p (%02x %02x %02x %02x)", base,
237 hdr[0], hdr[1], hdr[2], hdr[3]);