Usermode/ld-acess - Fixing elf64 support (and x86-64)
[tpg/acess2.git] / Usermode / Libraries / ld-acess.so_src / loadlib.c
1 /*
2  AcessOS 1 - Dynamic Loader
3  By thePowersGang
4 */
5 #include "common.h"
6
7 #define DEBUG   1
8
9 #if DEBUG
10 # define DEBUGS(v...)   SysDebug(v)
11 #else
12 # define DEBUGS(v...)   
13 #endif
14
15 // === PROTOTYPES ===
16 void    *IsFileLoaded(const char *file);
17  int    GetSymbolFromBase(void *base, const char *name, void **ret);
18
19 // === IMPORTS ===
20 extern const struct {
21         void    *Value;
22         char    *Name;
23 }       caLocalExports[];
24 extern const int        ciNumLocalExports;
25
26 // === GLOABLS ===
27 tLoadedLib      gLoadedLibraries[MAX_LOADED_LIBRARIES];
28 char    gsLoadedStrings[MAX_STRINGS_BYTES];
29 char    *gsNextAvailString = gsLoadedStrings;
30 //tLoadLib      *gpLoadedLibraries = NULL;
31
32 // === CODE ===
33 const char *FindLibrary(char *DestBuf, const char *SoName, const char *ExtraSearchDir)
34 {       
35         // -- #1: Executable Specified
36         if(ExtraSearchDir)
37         {
38                 strcpy(DestBuf, ExtraSearchDir);
39                 strcat(DestBuf, "/");
40                 strcat(DestBuf, SoName);
41                 if(file_exists(DestBuf))        return DestBuf;
42         }
43         
44         // -- #2: System
45         strcpy(DestBuf, SYSTEM_LIB_DIR);
46         strcat(DestBuf, SoName);
47         if(file_exists(DestBuf))        return DestBuf;
48         
49         // -- #3: Current Directory
50         if(file_exists(SoName)) return SoName;
51         
52         return NULL;
53 }
54
55 /**
56  */
57 void *LoadLibrary(const char *SoName, const char *SearchDir, char **envp)
58 {
59         char    sTmpName[1024];
60         const char      *filename;
61         void    *base;
62         void    (*fEntry)(void *, int, char *[], char**);
63         
64         DEBUGS("LoadLibrary: (SoName='%s', SearchDir='%s', envp=0x%x)", SoName, SearchDir, envp);
65         
66         // Create Temp Name
67         filename = FindLibrary(sTmpName, SoName, SearchDir);
68         if(filename == NULL) {
69                 DEBUGS("LoadLibrary: RETURN 0");
70                 return 0;
71         }
72         DEBUGS(" LoadLibrary: filename='%s'", filename);
73         
74         if( (base = IsFileLoaded(filename)) )
75                 return base;
76
77         DEBUGS(" LoadLibrary: SysLoadBin()");   
78         // Load Library
79         base = SysLoadBin(filename, (void**)&fEntry);
80         if(!base) {
81                 DEBUGS("LoadLibrary: RETURN 0");
82                 return 0;
83         }
84         
85         DEBUGS(" LoadLibrary: iArg=%p, iEntry=0x%x", base, fEntry);
86         
87         // Load Symbols
88         fEntry = DoRelocate( base, envp, filename );
89         
90         // Call Entrypoint
91         DEBUGS(" LoadLibrary: '%s' Entry 0x%x", SoName, fEntry);
92         fEntry(base, 0, NULL, envp);
93         
94         DEBUGS("LoadLibrary: RETURN 1");
95         return base;
96 }
97
98 /**
99  * \fn Uint IsFileLoaded(char *file)
100  * \brief Determine if a file is already loaded
101  */
102 void *IsFileLoaded(const char *file)
103 {
104          int    i;
105         DEBUGS("IsFileLoaded: (file='%s')", file);
106         for( i = 0; i < MAX_LOADED_LIBRARIES; i++ )
107         {
108                 if(gLoadedLibraries[i].Base == 0)       break;  // Last entry has Base set to NULL
109                 DEBUGS(" strcmp('%s', '%s')", gLoadedLibraries[i].Name, file);
110                 if(strcmp(gLoadedLibraries[i].Name, file) == 0) {
111                         DEBUGS("IsFileLoaded: Found %i (0x%x)", i, gLoadedLibraries[i].Base);
112                         return gLoadedLibraries[i].Base;
113                 }
114         }
115         DEBUGS("IsFileLoaded: Not Found");
116         return 0;
117 }
118
119 /**
120  * \fn void AddLoaded(char *File, Uint base)
121  * \brief Add a file to the loaded list
122  */
123 void AddLoaded(const char *File, void *base)
124 {
125          int    i, length;
126         char    *name = gsNextAvailString;
127         
128         DEBUGS("AddLoaded: (File='%s', base=0x%x)", File, base);
129         
130         // Find a free slot
131         for( i = 0; i < MAX_LOADED_LIBRARIES; i ++ )
132         {
133                 if(gLoadedLibraries[i].Base == 0)       break;
134         }
135         if(i == MAX_LOADED_LIBRARIES) {
136                 SysDebug("ERROR - ld-acess.so has run out of load slots!");
137                 return;
138         }
139         
140         // Check space in string buffer
141         length = strlen(File);
142         if(&name[length+1] >= &gsLoadedStrings[MAX_STRINGS_BYTES]) {
143                 SysDebug("ERROR - ld-acess.so has run out of string buffer memory!");
144                 return;
145         }
146         
147         // Set information
148         gLoadedLibraries[i].Base = base;
149         strcpy(name, File);
150         gLoadedLibraries[i].Name = name;
151         gsNextAvailString = &name[length+1];
152         DEBUGS("'%s' (0x%x) loaded as %i", name, base, i);
153         return;
154 }
155
156 /**
157  * \fn void Unload(Uint Base)
158  */
159 void Unload(void *Base)
160 {       
161          int    i, j;
162          int    id;
163         char    *str;
164         for( id = 0; id < MAX_LOADED_LIBRARIES; id++ )
165         {
166                 if(gLoadedLibraries[id].Base == Base)   break;
167         }
168         if(id == MAX_LOADED_LIBRARIES)  return;
169         
170         // Unload Binary
171         SysUnloadBin( Base );
172         // Save String Pointer
173         str = gLoadedLibraries[id].Name;
174         
175         // Compact Loaded List
176         j = id;
177         for( i = j + 1; i < MAX_LOADED_LIBRARIES; i++, j++ )
178         {
179                 if(gLoadedLibraries[i].Base == 0)       break;
180                 // Compact String
181                 strcpy(str, gLoadedLibraries[i].Name);
182                 str += strlen(str)+1;
183                 // Compact Entry
184                 gLoadedLibraries[j].Base = gLoadedLibraries[i].Base;
185                 gLoadedLibraries[j].Name = str;
186         }
187         
188         // NULL Last Entry
189         gLoadedLibraries[j].Base = 0;
190         gLoadedLibraries[j].Name = NULL;
191         // Save next string
192         gsNextAvailString = str;
193 }
194
195 /**
196  \fn Uint GetSymbol(const char *name)
197  \brief Gets a symbol value from a loaded library
198 */
199 void *GetSymbol(const char *name)
200 {
201          int    i;
202         void    *ret;
203         
204         //SysDebug("ciNumLocalExports = %i", ciNumLocalExports);
205         for(i=0;i<ciNumLocalExports;i++)
206         {
207                 if( strcmp(caLocalExports[i].Name, name) == 0 )
208                         return caLocalExports[i].Value;
209         }
210         
211         // Entry 0 is ld-acess, ignore it
212         for(i = 1; i < MAX_LOADED_LIBRARIES; i ++)
213         {
214                 if(gLoadedLibraries[i].Base == 0)       break;
215                 
216                 //SysDebug(" GetSymbol: Trying 0x%x, '%s'",
217                 //      gLoadedLibraries[i].Base, gLoadedLibraries[i].Name);
218                 if(GetSymbolFromBase(gLoadedLibraries[i].Base, name, &ret))     return ret;
219         }
220         SysDebug("GetSymbol: === Symbol '%s' not found ===", name);
221         return 0;
222 }
223
224 /**
225  \fn int GetSymbolFromBase(Uint base, char *name, Uint *ret)
226  \breif Gets a symbol from a specified library
227 */
228 int GetSymbolFromBase(void *base, const char *name, void **ret)
229 {
230         if(*(Uint32*)base == (0x7F|('E'<<8)|('L'<<16)|('F'<<24)))
231                 return ElfGetSymbol(base, name, ret);
232         if(*(Uint16*)base == ('M'|('Z'<<8)))
233                 return PE_GetSymbol(base, name, ret);
234         SysDebug("Unknown type at %p", base);
235         return 0;
236 }
237

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