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, size_t *size);
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 );
93 DEBUGS(" LoadLibrary: '%s' Entry %p", SoName, fEntry);
94 fEntry(base, 0, NULL, gEnvP);
96 DEBUGS("LoadLibrary: RETURN 1");
101 * \fn Uint IsFileLoaded(char *file)
102 * \brief Determine if a file is already loaded
104 void *IsFileLoaded(const char *file)
107 DEBUGS("IsFileLoaded: (file='%s')", file);
108 for( i = 0; i < MAX_LOADED_LIBRARIES; i++ )
110 if(gLoadedLibraries[i].Base == 0) break; // Last entry has Base set to NULL
111 DEBUGS(" strcmp('%s', '%s')", gLoadedLibraries[i].Name, file);
112 if(strcmp(gLoadedLibraries[i].Name, file) == 0) {
113 DEBUGS("IsFileLoaded: Found %i (%p)", i, gLoadedLibraries[i].Base);
114 return gLoadedLibraries[i].Base;
117 DEBUGS("IsFileLoaded: Not Found");
122 * \fn void AddLoaded(char *File, Uint base)
123 * \brief Add a file to the loaded list
125 void AddLoaded(const char *File, void *base)
128 char *name = gsNextAvailString;
130 DEBUGS("AddLoaded: (File='%s', base=%p)", File, base);
133 for( i = 0; i < MAX_LOADED_LIBRARIES; i ++ )
135 if(gLoadedLibraries[i].Base == 0) break;
137 if(i == MAX_LOADED_LIBRARIES) {
138 SysDebug("ERROR - ld-acess.so has run out of load slots!");
142 // Check space in string buffer
143 length = strlen(File);
144 if(&name[length+1] >= &gsLoadedStrings[MAX_STRINGS_BYTES]) {
145 SysDebug("ERROR - ld-acess.so has run out of string buffer memory!");
150 gLoadedLibraries[i].Base = base;
152 gLoadedLibraries[i].Name = name;
153 gsNextAvailString = &name[length+1];
154 DEBUGS("'%s' (%p) loaded as %i", name, base, i);
159 * \fn void Unload(Uint Base)
161 void Unload(void *Base)
166 for( id = 0; id < MAX_LOADED_LIBRARIES; id++ )
168 if(gLoadedLibraries[id].Base == Base) break;
170 if(id == MAX_LOADED_LIBRARIES) return;
173 SysUnloadBin( Base );
174 // Save String Pointer
175 str = gLoadedLibraries[id].Name;
177 // Compact Loaded List
179 for( i = j + 1; i < MAX_LOADED_LIBRARIES; i++, j++ )
181 if(gLoadedLibraries[i].Base == 0) break;
183 strcpy(str, gLoadedLibraries[i].Name);
184 str += strlen(str)+1;
186 gLoadedLibraries[j].Base = gLoadedLibraries[i].Base;
187 gLoadedLibraries[j].Name = str;
191 gLoadedLibraries[j].Base = 0;
192 gLoadedLibraries[j].Name = NULL;
194 gsNextAvailString = str;
198 \fn Uint GetSymbol(const char *name)
199 \brief Gets a symbol value from a loaded library
201 void *GetSymbol(const char *name, size_t *Size)
206 //SysDebug("ciNumLocalExports = %i", ciNumLocalExports);
207 for(i=0;i<ciNumLocalExports;i++)
209 if( strcmp(caLocalExports[i].Name, name) == 0 )
210 return caLocalExports[i].Value;
213 // Entry 0 is ld-acess, ignore it
214 for(i = 1; i < MAX_LOADED_LIBRARIES; i ++)
216 if(gLoadedLibraries[i].Base == 0) break;
218 //SysDebug(" GetSymbol: Trying 0x%x, '%s'",
219 // gLoadedLibraries[i].Base, gLoadedLibraries[i].Name);
220 if(GetSymbolFromBase(gLoadedLibraries[i].Base, name, &ret, Size)) return ret;
222 SysDebug("GetSymbol: === Symbol '%s' not found ===", name);
227 \fn int GetSymbolFromBase(Uint base, char *name, Uint *ret)
228 \breif Gets a symbol from a specified library
230 int GetSymbolFromBase(void *base, const char *name, void **ret, size_t *Size)
233 if(hdr[0] == 0x7F && hdr[1] == 'E' && hdr[2] == 'L' && hdr[3] == 'F')
234 return ElfGetSymbol(base, name, ret, Size);
235 if(hdr[0] == 'M' && hdr[1] == 'Z')
236 return PE_GetSymbol(base, name, ret, Size);
237 SysDebug("Unknown type at %p (%02x %02x %02x %02x)", base,
238 hdr[0], hdr[1], hdr[2], hdr[3]);