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

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