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

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