User/Kernel - ARMv7 fixes (relocation of .text)
[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
19 // === IMPORTS ===
20 extern const struct {
21         void    *Value;
22         char    *Name;
23 }       caLocalExports[];
24 extern const int        ciNumLocalExports;
25 extern char     **gEnvP;
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         if( !fEntry ) {
91                 return 0;
92         }
93         
94         // Call Entrypoint
95         DEBUGS(" LoadLibrary: '%s' Entry %p", SoName, fEntry);
96         fEntry(base, 0, NULL, gEnvP);
97         
98         DEBUGS("LoadLibrary: RETURN 1");
99         return base;
100 }
101
102 /**
103  * \fn Uint IsFileLoaded(char *file)
104  * \brief Determine if a file is already loaded
105  */
106 void *IsFileLoaded(const char *file)
107 {
108          int    i;
109         DEBUGS("IsFileLoaded: (file='%s')", file);
110         for( i = 0; i < MAX_LOADED_LIBRARIES; i++ )
111         {
112                 if(gLoadedLibraries[i].Base == 0)       break;  // Last entry has Base set to NULL
113                 DEBUGS(" strcmp('%s', '%s')", gLoadedLibraries[i].Name, file);
114                 if(strcmp(gLoadedLibraries[i].Name, file) == 0) {
115                         DEBUGS("IsFileLoaded: Found %i (%p)", i, gLoadedLibraries[i].Base);
116                         return gLoadedLibraries[i].Base;
117                 }
118         }
119         DEBUGS("IsFileLoaded: Not Found");
120         return 0;
121 }
122
123 /**
124  * \fn void AddLoaded(char *File, Uint base)
125  * \brief Add a file to the loaded list
126  */
127 void AddLoaded(const char *File, void *base)
128 {
129          int    i, length;
130         char    *name = gsNextAvailString;
131         
132         DEBUGS("AddLoaded: (File='%s', base=%p)", File, base);
133         
134         // Find a free slot
135         for( i = 0; i < MAX_LOADED_LIBRARIES; i ++ )
136         {
137                 if(gLoadedLibraries[i].Base == 0)       break;
138         }
139         if(i == MAX_LOADED_LIBRARIES) {
140                 SysDebug("ERROR - ld-acess.so has run out of load slots!");
141                 return;
142         }
143         
144         // Check space in string buffer
145         length = strlen(File);
146         if(&name[length+1] >= &gsLoadedStrings[MAX_STRINGS_BYTES]) {
147                 SysDebug("ERROR - ld-acess.so has run out of string buffer memory!");
148                 return;
149         }
150         
151         // Set information
152         gLoadedLibraries[i].Base = base;
153         strcpy(name, File);
154         gLoadedLibraries[i].Name = name;
155         gsNextAvailString = &name[length+1];
156         DEBUGS("'%s' (%p) loaded as %i", name, base, i);
157         return;
158 }
159
160 /**
161  * \fn void Unload(Uint Base)
162  */
163 void Unload(void *Base)
164 {       
165          int    i, j;
166          int    id;
167         char    *str;
168         for( id = 0; id < MAX_LOADED_LIBRARIES; id++ )
169         {
170                 if(gLoadedLibraries[id].Base == Base)   break;
171         }
172         if(id == MAX_LOADED_LIBRARIES)  return;
173         
174         // Unload Binary
175         SysUnloadBin( Base );
176         // Save String Pointer
177         str = gLoadedLibraries[id].Name;
178         
179         // Compact Loaded List
180         j = id;
181         for( i = j + 1; i < MAX_LOADED_LIBRARIES; i++, j++ )
182         {
183                 if(gLoadedLibraries[i].Base == 0)       break;
184                 // Compact String
185                 strcpy(str, gLoadedLibraries[i].Name);
186                 str += strlen(str)+1;
187                 // Compact Entry
188                 gLoadedLibraries[j].Base = gLoadedLibraries[i].Base;
189                 gLoadedLibraries[j].Name = str;
190         }
191         
192         // NULL Last Entry
193         gLoadedLibraries[j].Base = 0;
194         gLoadedLibraries[j].Name = NULL;
195         // Save next string
196         gsNextAvailString = str;
197 }
198
199 /**
200  \fn Uint GetSymbol(const char *name)
201  \brief Gets a symbol value from a loaded library
202 */
203 int GetSymbol(const char *name, void **Value, size_t *Size)
204 {
205          int    i;
206         
207         //SysDebug("ciNumLocalExports = %i", ciNumLocalExports);
208         for(i=0;i<ciNumLocalExports;i++)
209         {
210                 if( strcmp(caLocalExports[i].Name, name) == 0 ) {
211                         *Value = caLocalExports[i].Value;
212                         if(Size)
213                                 *Size = 0;
214                         return 1;
215                 }
216         }
217         
218         // Entry 0 is ld-acess, ignore it
219         for(i = 1; i < MAX_LOADED_LIBRARIES; i ++)
220         {
221                 if(gLoadedLibraries[i].Base == 0)
222                         break;
223                 
224                 //SysDebug(" GetSymbol: Trying 0x%x, '%s'",
225                 //      gLoadedLibraries[i].Base, gLoadedLibraries[i].Name);
226                 if(GetSymbolFromBase(gLoadedLibraries[i].Base, name, Value, Size))
227                         return 1;
228         }
229         SysDebug("GetSymbol: === Symbol '%s' not found ===", name);
230         return 0;
231 }
232
233 /**
234  \fn int GetSymbolFromBase(Uint base, char *name, Uint *ret)
235  \breif Gets a symbol from a specified library
236 */
237 int GetSymbolFromBase(void *base, const char *name, void **ret, size_t *Size)
238 {
239         uint8_t *hdr = base;
240         if(hdr[0] == 0x7F && hdr[1] == 'E' && hdr[2] == 'L' && hdr[3] == 'F')
241                 return ElfGetSymbol(base, name, ret, Size);
242         if(hdr[0] == 'M' && hdr[1] == 'Z')
243                 return PE_GetSymbol(base, name, ret, Size);
244         SysDebug("Unknown type at %p (%02x %02x %02x %02x)", base,
245                 hdr[0], hdr[1], hdr[2], hdr[3]);
246         return 0;
247 }
248

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