Usermode/ld-acess - Fixing elf64 support (and x86-64)
[tpg/acess2.git] / Usermode / Libraries / ld-acess.so_src / loadlib.c
index a5f0bc2..9a1bc10 100644 (file)
@@ -4,7 +4,7 @@
 */
 #include "common.h"
 
-#define DEBUG  0
+#define DEBUG  1
 
 #if DEBUG
 # define DEBUGS(v...)  SysDebug(v)
 #endif
 
 // === PROTOTYPES ===
-Uint   IsFileLoaded(char *file);
- int   GetSymbolFromBase(Uint base, char *name, Uint *ret);
+void   *IsFileLoaded(const char *file);
+ int   GetSymbolFromBase(void *base, const char *name, void **ret);
 
 // === IMPORTS ===
 extern const struct {
-       Uint    Value;
+       void    *Value;
        char    *Name;
 }      caLocalExports[];
 extern const int       ciNumLocalExports;
@@ -30,7 +30,7 @@ char  *gsNextAvailString = gsLoadedStrings;
 //tLoadLib     *gpLoadedLibraries = NULL;
 
 // === CODE ===
-char *FindLibrary(char *DestBuf, char *SoName, char *ExtraSearchDir)
+const char *FindLibrary(char *DestBuf, const char *SoName, const char *ExtraSearchDir)
 {      
        // -- #1: Executable Specified
        if(ExtraSearchDir)
@@ -54,51 +54,52 @@ char *FindLibrary(char *DestBuf, char *SoName, char *ExtraSearchDir)
 
 /**
  */
-Uint LoadLibrary(char *SoName, char *SearchDir, char **envp)
+void *LoadLibrary(const char *SoName, const char *SearchDir, char **envp)
 {
        char    sTmpName[1024];
-       char    *filename;
-       Uint    iArg;
-       void    (*fEntry)(int, int, char *[], char**);
+       const char      *filename;
+       void    *base;
+       void    (*fEntry)(void *, int, char *[], char**);
        
-       DEBUGS("LoadLibrary: (filename='%s', envp=0x%x)\n", filename, envp);
+       DEBUGS("LoadLibrary: (SoName='%s', SearchDir='%s', envp=0x%x)", SoName, SearchDir, envp);
        
        // Create Temp Name
        filename = FindLibrary(sTmpName, SoName, SearchDir);
        if(filename == NULL) {
-               DEBUGS("LoadLibrary: RETURN 0\n");
+               DEBUGS("LoadLibrary: RETURN 0");
                return 0;
        }
-       DEBUGS(" LoadLibrary: filename='%s'\n", filename);
-       
-       if( (iArg = IsFileLoaded(filename)) )
-               return iArg;
+       DEBUGS(" LoadLibrary: filename='%s'", filename);
        
+       if( (base = IsFileLoaded(filename)) )
+               return base;
+
+       DEBUGS(" LoadLibrary: SysLoadBin()");   
        // Load Library
-       iArg = SysLoadBin(filename, (Uint*)&fEntry);
-       if(iArg == 0) {
-               DEBUGS("LoadLibrary: RETURN 0\n");
+       base = SysLoadBin(filename, (void**)&fEntry);
+       if(!base) {
+               DEBUGS("LoadLibrary: RETURN 0");
                return 0;
        }
        
-       DEBUGS(" LoadLibrary: iArg=0x%x, iEntry=0x%x\n", iArg, fEntry);
+       DEBUGS(" LoadLibrary: iArg=%p, iEntry=0x%x", base, fEntry);
        
        // Load Symbols
-       fEntry = (void*)DoRelocate( iArg, envp, filename );
+       fEntry = DoRelocate( base, envp, filename );
        
        // Call Entrypoint
-       DEBUGS(" LoadLibrary: '%s' Entry 0x%x\n", SoName, fEntry);
-       fEntry(iArg, 0, NULL, envp);
+       DEBUGS(" LoadLibrary: '%s' Entry 0x%x", SoName, fEntry);
+       fEntry(base, 0, NULL, envp);
        
-       DEBUGS("LoadLibrary: RETURN 1\n");
-       return iArg;
+       DEBUGS("LoadLibrary: RETURN 1");
+       return base;
 }
 
 /**
  * \fn Uint IsFileLoaded(char *file)
  * \brief Determine if a file is already loaded
  */
-Uint IsFileLoaded(char *file)
+void *IsFileLoaded(const char *file)
 {
         int    i;
        DEBUGS("IsFileLoaded: (file='%s')", file);
@@ -119,7 +120,7 @@ Uint IsFileLoaded(char *file)
  * \fn void AddLoaded(char *File, Uint base)
  * \brief Add a file to the loaded list
  */
-void AddLoaded(char *File, Uint base)
+void AddLoaded(const char *File, void *base)
 {
         int    i, length;
        char    *name = gsNextAvailString;
@@ -148,14 +149,14 @@ void AddLoaded(char *File, Uint base)
        strcpy(name, File);
        gLoadedLibraries[i].Name = name;
        gsNextAvailString = &name[length+1];
-       DEBUGS("'%s' (0x%x) loaded as %i\n", name, base, i);
+       DEBUGS("'%s' (0x%x) loaded as %i", name, base, i);
        return;
 }
 
 /**
  * \fn void Unload(Uint Base)
  */
-void Unload(Uint Base)
+void Unload(void *Base)
 {      
         int    i, j;
         int    id;
@@ -192,13 +193,13 @@ void Unload(Uint Base)
 }
 
 /**
- \fn Uint GetSymbol(char *name)
+ \fn Uint GetSymbol(const char *name)
  \brief Gets a symbol value from a loaded library
 */
-Uint GetSymbol(char *name)
+void *GetSymbol(const char *name)
 {
         int    i;
-       Uint    ret;
+       void    *ret;
        
        //SysDebug("ciNumLocalExports = %i", ciNumLocalExports);
        for(i=0;i<ciNumLocalExports;i++)
@@ -207,7 +208,8 @@ Uint GetSymbol(char *name)
                        return caLocalExports[i].Value;
        }
        
-       for(i=0;i<sizeof(gLoadedLibraries)/sizeof(gLoadedLibraries[0]);i++)
+       // Entry 0 is ld-acess, ignore it
+       for(i = 1; i < MAX_LOADED_LIBRARIES; i ++)
        {
                if(gLoadedLibraries[i].Base == 0)       break;
                
@@ -223,7 +225,7 @@ Uint GetSymbol(char *name)
  \fn int GetSymbolFromBase(Uint base, char *name, Uint *ret)
  \breif Gets a symbol from a specified library
 */
-int GetSymbolFromBase(Uint base, char *name, Uint *ret)
+int GetSymbolFromBase(void *base, const char *name, void **ret)
 {
        if(*(Uint32*)base == (0x7F|('E'<<8)|('L'<<16)|('F'<<24)))
                return ElfGetSymbol(base, name, ret);

UCC git Repository :: git.ucc.asn.au