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

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