X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Usermode%2FLibraries%2Fld-acess.so_src%2Floadlib.c;h=a5f0bc260c9f8e65d92f326cd6505e7f61df7b36;hb=ea5bf1c03b31db64014d0aa770fa6c326d729efa;hp=267555d1bc7e8a2d26c2d10dce235f2a1bc7a55c;hpb=17e16b3110b4c5124b0707435e0427993d696545;p=tpg%2Facess2.git diff --git a/Usermode/Libraries/ld-acess.so_src/loadlib.c b/Usermode/Libraries/ld-acess.so_src/loadlib.c index 267555d1..a5f0bc26 100644 --- a/Usermode/Libraries/ld-acess.so_src/loadlib.c +++ b/Usermode/Libraries/ld-acess.so_src/loadlib.c @@ -1,179 +1,221 @@ -/* - AcessOS 1 - Dynamic Loader - By thePowersGang -*/ -#include "common.h" - -#define DEBUG 0 - -#if DEBUG -# define DEBUGS(v...) SysDebug(v) -#else -# define DEBUGS(v...) -#endif - -// === PROTOTYPES === +/* + AcessOS 1 - Dynamic Loader + By thePowersGang +*/ +#include "common.h" + +#define DEBUG 0 + +#if DEBUG +# define DEBUGS(v...) SysDebug(v) +#else +# define DEBUGS(v...) +#endif + +// === PROTOTYPES === Uint IsFileLoaded(char *file); - int GetSymbolFromBase(Uint base, char *name, Uint *ret); - -// === GLOABLS === -#define MAX_LOADED_LIBRARIES 64 -#define MAX_STRINGS_BYTES 4096 -struct { - Uint Base; - char *Name; -} gLoadedLibraries[MAX_LOADED_LIBRARIES]; -char gsLoadedStrings[MAX_STRINGS_BYTES]; -char *gsNextAvailString = gsLoadedStrings; -//tLoadLib *gpLoadedLibraries = NULL; - -// === CODE === -Uint LoadLibrary(char *filename, char *SearchDir, char **envp) -{ - char sTmpName[1024] = "/Acess/Libs/"; - Uint iArg; - void (*fEntry)(int, int, char *[], char**); - - DEBUGS("LoadLibrary: (filename='%s', envp=0x%x)\n", filename, envp); - - // Create Temp Name - strcpy(&sTmpName[12], filename); - DEBUGS(" LoadLibrary: sTmpName='%s'\n", sTmpName); - - if( (iArg = IsFileLoaded(sTmpName)) ) - return iArg; - - // Load Library - iArg = SysLoadBin(sTmpName, (Uint*)&fEntry); - if(iArg == 0) { - DEBUGS("LoadLibrary: RETURN 0\n"); - return 0; - } - - DEBUGS(" LoadLibrary: iArg=0x%x, iEntry=0x%x\n", iArg, fEntry); - - // Load Symbols - fEntry = (void*)DoRelocate( iArg, envp, sTmpName ); - - // Call Entrypoint - DEBUGS(" LoadLibrary: '%s' Entry 0x%x\n", filename, fEntry); - fEntry(iArg, 0, NULL, envp); - - DEBUGS("LoadLibrary: RETURN 1\n"); - return iArg; + int GetSymbolFromBase(Uint base, char *name, Uint *ret); + +// === IMPORTS === +extern const struct { + Uint Value; + char *Name; +} caLocalExports[]; +extern const int ciNumLocalExports; + +// === GLOABLS === +tLoadedLib gLoadedLibraries[MAX_LOADED_LIBRARIES]; +char gsLoadedStrings[MAX_STRINGS_BYTES]; +char *gsNextAvailString = gsLoadedStrings; +//tLoadLib *gpLoadedLibraries = NULL; + +// === CODE === +char *FindLibrary(char *DestBuf, char *SoName, char *ExtraSearchDir) +{ + // -- #1: Executable Specified + if(ExtraSearchDir) + { + strcpy(DestBuf, ExtraSearchDir); + strcat(DestBuf, "/"); + strcat(DestBuf, SoName); + if(file_exists(DestBuf)) return DestBuf; + } + + // -- #2: System + strcpy(DestBuf, SYSTEM_LIB_DIR); + strcat(DestBuf, SoName); + if(file_exists(DestBuf)) return DestBuf; + + // -- #3: Current Directory + if(file_exists(SoName)) return SoName; + + return NULL; +} + +/** + */ +Uint LoadLibrary(char *SoName, char *SearchDir, char **envp) +{ + char sTmpName[1024]; + char *filename; + Uint iArg; + void (*fEntry)(int, int, char *[], char**); + + DEBUGS("LoadLibrary: (filename='%s', envp=0x%x)\n", filename, envp); + + // Create Temp Name + filename = FindLibrary(sTmpName, SoName, SearchDir); + if(filename == NULL) { + DEBUGS("LoadLibrary: RETURN 0\n"); + return 0; + } + DEBUGS(" LoadLibrary: filename='%s'\n", filename); + + if( (iArg = IsFileLoaded(filename)) ) + return iArg; + + // Load Library + iArg = SysLoadBin(filename, (Uint*)&fEntry); + if(iArg == 0) { + DEBUGS("LoadLibrary: RETURN 0\n"); + return 0; + } + + DEBUGS(" LoadLibrary: iArg=0x%x, iEntry=0x%x\n", iArg, fEntry); + + // Load Symbols + fEntry = (void*)DoRelocate( iArg, envp, filename ); + + // Call Entrypoint + DEBUGS(" LoadLibrary: '%s' Entry 0x%x\n", SoName, fEntry); + fEntry(iArg, 0, NULL, envp); + + DEBUGS("LoadLibrary: RETURN 1\n"); + return iArg; } - -/** - * \fn Uint IsFileLoaded(char *file) - * \brief Determine if a file is already loaded - */ -Uint IsFileLoaded(char *file) -{ - int i; - DEBUGS("IsFileLoaded: (file='%s')\n", file); - for( i = 0; i < MAX_LOADED_LIBRARIES; i++ ) - { - if(gLoadedLibraries[i].Base == 0) break; // Last entry has Base set to NULL - if(strcmp(gLoadedLibraries[i].Name, file) == 0) { - DEBUGS("IsFileLoaded: Found %i (0x%x)\n", i, gLoadedLibraries[i].Base); - return gLoadedLibraries[i].Base; - } - } - DEBUGS("IsFileLoaded: Not Found\n"); - return 0; -} - -/** - * \fn void AddLoaded(char *File, Uint base) - * \brief Add a file to the loaded list + +/** + * \fn Uint IsFileLoaded(char *file) + * \brief Determine if a file is already loaded + */ +Uint IsFileLoaded(char *file) +{ + int i; + DEBUGS("IsFileLoaded: (file='%s')", file); + for( i = 0; i < MAX_LOADED_LIBRARIES; i++ ) + { + if(gLoadedLibraries[i].Base == 0) break; // Last entry has Base set to NULL + DEBUGS(" strcmp('%s', '%s')", gLoadedLibraries[i].Name, file); + if(strcmp(gLoadedLibraries[i].Name, file) == 0) { + DEBUGS("IsFileLoaded: Found %i (0x%x)", i, gLoadedLibraries[i].Base); + return gLoadedLibraries[i].Base; + } + } + DEBUGS("IsFileLoaded: Not Found"); + return 0; +} + +/** + * \fn void AddLoaded(char *File, Uint base) + * \brief Add a file to the loaded list */ void AddLoaded(char *File, Uint base) -{ - int i, length; - char *name = gsNextAvailString; - - DEBUGS("AddLoaded: (File='%s', base=0x%x)", File, base); - - // Find a free slot - for( i = 0; i < MAX_LOADED_LIBRARIES; i ++ ) - { - if(gLoadedLibraries[i].Base == 0) break; - } - if(i == MAX_LOADED_LIBRARIES) { - SysDebug("ERROR - ld-acess.so has run out of load slots!"); - return; - } - - // Check space in string buffer - length = strlen(File); - if(&name[length+1] >= &gsLoadedStrings[MAX_STRINGS_BYTES]) { - SysDebug("ERROR - ld-acess.so has run out of string buffer memory!"); - return; - } - - // Set information - gLoadedLibraries[i].Base = base; - strcpy(name, File); - gLoadedLibraries[i].Name = name; - gsNextAvailString = &name[length+1]; - DEBUGS("'%s' (0x%x) loaded as %i\n", name, base, i); +{ + int i, length; + char *name = gsNextAvailString; + + DEBUGS("AddLoaded: (File='%s', base=0x%x)", File, base); + + // Find a free slot + for( i = 0; i < MAX_LOADED_LIBRARIES; i ++ ) + { + if(gLoadedLibraries[i].Base == 0) break; + } + if(i == MAX_LOADED_LIBRARIES) { + SysDebug("ERROR - ld-acess.so has run out of load slots!"); + return; + } + + // Check space in string buffer + length = strlen(File); + if(&name[length+1] >= &gsLoadedStrings[MAX_STRINGS_BYTES]) { + SysDebug("ERROR - ld-acess.so has run out of string buffer memory!"); + return; + } + + // Set information + gLoadedLibraries[i].Base = base; + strcpy(name, File); + gLoadedLibraries[i].Name = name; + gsNextAvailString = &name[length+1]; + DEBUGS("'%s' (0x%x) loaded as %i\n", name, base, i); return; -} - -/** - * \fn void Unload(Uint Base) - */ -void Unload(Uint Base) -{ - int i, j; - int id; - char *str; - for( id = 0; id < MAX_LOADED_LIBRARIES; id++ ) - { - if(gLoadedLibraries[id].Base == Base) break; - } - if(id == MAX_LOADED_LIBRARIES) return; - - // Unload Binary - SysUnloadBin( Base ); - // Save String Pointer - str = gLoadedLibraries[id].Name; - - // Compact Loaded List - j = id; - for( i = j + 1; i < MAX_LOADED_LIBRARIES; i++, j++ ) - { - if(gLoadedLibraries[i].Base == 0) break; - // Compact String - strcpy(str, gLoadedLibraries[i].Name); - str += strlen(str)+1; - // Compact Entry - gLoadedLibraries[j].Base = gLoadedLibraries[i].Base; - gLoadedLibraries[j].Name = str; - } - - // NULL Last Entry - gLoadedLibraries[j].Base = 0; - gLoadedLibraries[j].Name = NULL; - // Save next string - gsNextAvailString = str; -} - +} + +/** + * \fn void Unload(Uint Base) + */ +void Unload(Uint Base) +{ + int i, j; + int id; + char *str; + for( id = 0; id < MAX_LOADED_LIBRARIES; id++ ) + { + if(gLoadedLibraries[id].Base == Base) break; + } + if(id == MAX_LOADED_LIBRARIES) return; + + // Unload Binary + SysUnloadBin( Base ); + // Save String Pointer + str = gLoadedLibraries[id].Name; + + // Compact Loaded List + j = id; + for( i = j + 1; i < MAX_LOADED_LIBRARIES; i++, j++ ) + { + if(gLoadedLibraries[i].Base == 0) break; + // Compact String + strcpy(str, gLoadedLibraries[i].Name); + str += strlen(str)+1; + // Compact Entry + gLoadedLibraries[j].Base = gLoadedLibraries[i].Base; + gLoadedLibraries[j].Name = str; + } + + // NULL Last Entry + gLoadedLibraries[j].Base = 0; + gLoadedLibraries[j].Name = NULL; + // Save next string + gsNextAvailString = str; +} + /** \fn Uint GetSymbol(char *name) \brief Gets a symbol value from a loaded library */ Uint GetSymbol(char *name) -{ +{ int i; - Uint ret; - for(i=0;i