2 AcessOS 1 - Dynamic Loader
\r
10 # define DEBUGS(v...) SysDebug(v)
\r
12 # define DEBUGS(v...)
\r
15 // === CONSTANTS ===
\r
16 #define MAX_LOADED_LIBRARIES 64
\r
17 #define MAX_STRINGS_BYTES 4096
\r
18 #define SYSTEM_LIB_DIR "/Acess/Libs/"
\r
20 // === PROTOTYPES ===
\r
21 Uint IsFileLoaded(char *file);
22 int GetSymbolFromBase(Uint base, char *name, Uint *ret);
\r
28 } gLoadedLibraries[MAX_LOADED_LIBRARIES];
\r
29 char gsLoadedStrings[MAX_STRINGS_BYTES];
\r
30 char *gsNextAvailString = gsLoadedStrings;
\r
31 //tLoadLib *gpLoadedLibraries = NULL;
\r
34 char *FindLibrary(char *DestBuf, char *SoName, char *ExtraSearchDir)
\r
36 // -- #1: Executable Specified
\r
39 strcpy(DestBuf, ExtraSearchDir);
\r
40 strcat(DestBuf, "/");
\r
41 strcat(DestBuf, SoName);
\r
42 if(file_exists(DestBuf)) return DestBuf;
\r
46 strcpy(DestBuf, SYSTEM_LIB_DIR);
\r
47 strcat(DestBuf, SoName);
\r
48 if(file_exists(DestBuf)) return DestBuf;
\r
50 // -- #3: Current Directory
\r
51 if(file_exists(SoName)) return SoName;
\r
58 Uint LoadLibrary(char *SoName, char *SearchDir, char **envp)
\r
60 char sTmpName[1024];
\r
63 void (*fEntry)(int, int, char *[], char**);
\r
65 DEBUGS("LoadLibrary: (filename='%s', envp=0x%x)\n", filename, envp);
\r
68 filename = FindLibrary(sTmpName, SoName, SearchDir);
\r
69 DEBUGS(" LoadLibrary: filename='%s'\n", filename);
\r
71 if( (iArg = IsFileLoaded(filename)) )
\r
75 iArg = SysLoadBin(filename, (Uint*)&fEntry);
\r
77 DEBUGS("LoadLibrary: RETURN 0\n");
\r
81 DEBUGS(" LoadLibrary: iArg=0x%x, iEntry=0x%x\n", iArg, fEntry);
\r
84 fEntry = (void*)DoRelocate( iArg, envp, filename );
\r
87 DEBUGS(" LoadLibrary: '%s' Entry 0x%x\n", SoName, fEntry);
\r
88 fEntry(iArg, 0, NULL, envp);
\r
90 DEBUGS("LoadLibrary: RETURN 1\n");
\r
95 * \fn Uint IsFileLoaded(char *file)
\r
96 * \brief Determine if a file is already loaded
\r
98 Uint IsFileLoaded(char *file)
\r
101 DEBUGS("IsFileLoaded: (file='%s')", file);
\r
102 for( i = 0; i < MAX_LOADED_LIBRARIES; i++ )
\r
104 if(gLoadedLibraries[i].Base == 0) break; // Last entry has Base set to NULL
\r
105 DEBUGS(" strcmp('%s', '%s')", gLoadedLibraries[i].Name, file);
\r
106 if(strcmp(gLoadedLibraries[i].Name, file) == 0) {
\r
107 DEBUGS("IsFileLoaded: Found %i (0x%x)", i, gLoadedLibraries[i].Base);
\r
108 return gLoadedLibraries[i].Base;
\r
111 DEBUGS("IsFileLoaded: Not Found");
\r
116 * \fn void AddLoaded(char *File, Uint base)
\r
117 * \brief Add a file to the loaded list
\r
119 void AddLoaded(char *File, Uint base)
122 char *name = gsNextAvailString;
\r
124 DEBUGS("AddLoaded: (File='%s', base=0x%x)", File, base);
\r
126 // Find a free slot
\r
127 for( i = 0; i < MAX_LOADED_LIBRARIES; i ++ )
\r
129 if(gLoadedLibraries[i].Base == 0) break;
\r
131 if(i == MAX_LOADED_LIBRARIES) {
\r
132 SysDebug("ERROR - ld-acess.so has run out of load slots!");
\r
136 // Check space in string buffer
\r
137 length = strlen(File);
\r
138 if(&name[length+1] >= &gsLoadedStrings[MAX_STRINGS_BYTES]) {
\r
139 SysDebug("ERROR - ld-acess.so has run out of string buffer memory!");
\r
144 gLoadedLibraries[i].Base = base;
\r
145 strcpy(name, File);
\r
146 gLoadedLibraries[i].Name = name;
\r
147 gsNextAvailString = &name[length+1];
\r
148 DEBUGS("'%s' (0x%x) loaded as %i\n", name, base, i);
\r
153 * \fn void Unload(Uint Base)
\r
155 void Unload(Uint Base)
\r
160 for( id = 0; id < MAX_LOADED_LIBRARIES; id++ )
\r
162 if(gLoadedLibraries[id].Base == Base) break;
\r
164 if(id == MAX_LOADED_LIBRARIES) return;
\r
167 SysUnloadBin( Base );
\r
168 // Save String Pointer
\r
169 str = gLoadedLibraries[id].Name;
\r
171 // Compact Loaded List
\r
173 for( i = j + 1; i < MAX_LOADED_LIBRARIES; i++, j++ )
\r
175 if(gLoadedLibraries[i].Base == 0) break;
\r
177 strcpy(str, gLoadedLibraries[i].Name);
\r
178 str += strlen(str)+1;
\r
180 gLoadedLibraries[j].Base = gLoadedLibraries[i].Base;
\r
181 gLoadedLibraries[j].Name = str;
\r
185 gLoadedLibraries[j].Base = 0;
\r
186 gLoadedLibraries[j].Name = NULL;
\r
187 // Save next string
\r
188 gsNextAvailString = str;
\r
192 \fn Uint GetSymbol(char *name)
193 \brief Gets a symbol value from a loaded library
195 Uint GetSymbol(char *name)
199 for(i=0;i<sizeof(gLoadedLibraries)/sizeof(gLoadedLibraries[0]);i++)
\r
201 if(gLoadedLibraries[i].Base == 0) break;
\r
203 //SysDebug(" GetSymbol: Trying 0x%x, '%s'\n",
\r
204 // gLoadedLibraries[i].Base, gLoadedLibraries[i].Name);
205 if(GetSymbolFromBase(gLoadedLibraries[i].Base, name, &ret)) return ret;
\r
207 SysDebug("GetSymbol: === Symbol '%s' not found ===\n", name);
\r
212 \fn int GetSymbolFromBase(Uint base, char *name, Uint *ret)
213 \breif Gets a symbol from a specified library
215 int GetSymbolFromBase(Uint base, char *name, Uint *ret)
217 if(*(Uint32*)base == (0x7F|('E'<<8)|('L'<<16)|('F'<<24)))
218 return ElfGetSymbol(base, name, ret);
219 if(*(Uint16*)base == ('M'|('Z'<<8)))
220 return PE_GetSymbol(base, name, ret);