Working on separating architecture dependent and independent stuff,
[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   0
8
9 #if DEBUG
10 # define DEBUGS(v...)   SysDebug(v)
11 #else
12 # define DEBUGS(v...)   
13 #endif
14
15 // === PROTOTYPES ===
16 Uint    IsFileLoaded(char *file);
17  int    GetSymbolFromBase(Uint base, char *name, Uint *ret);
18
19 // === CONSTANTS ===
20 const struct {
21         Uint    Value;
22         char    *Name;
23 }       caLocalExports[] = {
24         {(Uint)gLoadedLibraries, "gLoadedLibraries"}
25 };
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 char *FindLibrary(char *DestBuf, char *SoName, 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 Uint LoadLibrary(char *SoName, char *SearchDir, char **envp)
59 {
60         char    sTmpName[1024];
61         char    *filename;
62         Uint    iArg;
63         void    (*fEntry)(int, int, char *[], char**);
64         
65         DEBUGS("LoadLibrary: (filename='%s', envp=0x%x)\n", filename, envp);
66         
67         // Create Temp Name
68         filename = FindLibrary(sTmpName, SoName, SearchDir);
69         if(filename == NULL) {
70                 DEBUGS("LoadLibrary: RETURN 0\n");
71                 return 0;
72         }
73         DEBUGS(" LoadLibrary: filename='%s'\n", filename);
74         
75         if( (iArg = IsFileLoaded(filename)) )
76                 return iArg;
77         
78         // Load Library
79         iArg = SysLoadBin(filename, (Uint*)&fEntry);
80         if(iArg == 0) {
81                 DEBUGS("LoadLibrary: RETURN 0\n");
82                 return 0;
83         }
84         
85         DEBUGS(" LoadLibrary: iArg=0x%x, iEntry=0x%x\n", iArg, fEntry);
86         
87         // Load Symbols
88         fEntry = (void*)DoRelocate( iArg, envp, filename );
89         
90         // Call Entrypoint
91         DEBUGS(" LoadLibrary: '%s' Entry 0x%x\n", SoName, fEntry);
92         fEntry(iArg, 0, NULL, envp);
93         
94         DEBUGS("LoadLibrary: RETURN 1\n");
95         return iArg;
96 }
97
98 /**
99  * \fn Uint IsFileLoaded(char *file)
100  * \brief Determine if a file is already loaded
101  */
102 Uint IsFileLoaded(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(char *File, Uint 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\n", name, base, i);
153         return;
154 }
155
156 /**
157  * \fn void Unload(Uint Base)
158  */
159 void Unload(Uint 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(char *name)
197  \brief Gets a symbol value from a loaded library
198 */
199 Uint GetSymbol(char *name)
200 {
201          int    i;
202         Uint    ret;
203         for(i=0;i<sizeof(caLocalExports)/sizeof(caLocalExports[0]);i++)
204         {
205                 if( strcmp(caLocalExports[i].Name, name) == 0 )
206                         return caLocalExports[i].Value;
207         }
208         
209         for(i=0;i<sizeof(gLoadedLibraries)/sizeof(gLoadedLibraries[0]);i++)
210         {
211                 if(gLoadedLibraries[i].Base == 0)       break;
212                 
213                 //SysDebug(" GetSymbol: Trying 0x%x, '%s'\n",
214                 //      gLoadedLibraries[i].Base, gLoadedLibraries[i].Name);
215                 if(GetSymbolFromBase(gLoadedLibraries[i].Base, name, &ret))     return ret;
216         }
217         SysDebug("GetSymbol: === Symbol '%s' not found ===\n", name);
218         return 0;
219 }
220
221 /**
222  \fn int GetSymbolFromBase(Uint base, char *name, Uint *ret)
223  \breif Gets a symbol from a specified library
224 */
225 int GetSymbolFromBase(Uint base, char *name, Uint *ret)
226 {
227         if(*(Uint32*)base == (0x7F|('E'<<8)|('L'<<16)|('F'<<24)))
228                 return ElfGetSymbol(base, name, ret);
229         if(*(Uint16*)base == ('M'|('Z'<<8)))
230                 return PE_GetSymbol(base, name, ret);
231         return 0;
232 }
233

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