This repository has been archived by the owner on Jun 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
dl.c
396 lines (374 loc) · 10.7 KB
/
dl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <taihen.h>
#include <psp2/sysmodule.h>
#include <psp2/kernel/modulemgr.h>
#include <psp2/kernel/processmgr.h>
#include "dlfcn.h"
#include "uthash.h"
#define SYMBOL_LENGTH_MAX 59
#define MODNAME_LENGTH_MAX 59
#define DLERRMSG_LENGTH_MAX 127
#define DL_MODULE_TYPE_SYS 's'
#define DL_MODULE_TYPE_FILE 'f'
#define DL_MODULE_TYPE_PRELOADED 'p'
#define DLERR(...) \
do { \
snprintf(_dl_errmsg, DLERRMSG_LENGTH_MAX+1, __VA_ARGS__); \
_dl_err = 1; \
} while (0)
typedef struct {
char name[SYMBOL_LENGTH_MAX+1];
int nid;
UT_hash_handle hh;
} dl_symbol_info;
typedef struct {
int type;
int sid;
int uid;
int refcount;
char name[MODNAME_LENGTH_MAX+1];
dl_symbol_info *dl_symbols;
UT_hash_handle hh;
} dl_module_info;
typedef struct dl_handle {
char name[MODNAME_LENGTH_MAX+1];
SceUID reserved;
} dl_handle;
SceUID sceKernelCreateRWLock(const char *, SceUInt32, void *);
SceUID sceKernelDeleteRWLock(SceUInt32);
SceInt32 sceKernelLockWriteRWLock(SceUID, SceUInt32 *);
SceInt32 sceKernelUnlockWriteRWLock(SceUID);
SceInt32 sceKernelLockReadRWLock(SceUID, SceUInt32 *);
SceInt32 sceKernelUnlockReadRWLock(SceUID);
static const char *_dl_default_module[] = {"SceLibKernel"};
static int _dl_err = 0;
static char *_dl_errmsg = NULL;
static dl_module_info *_dl_module_db = NULL;
static SceUID _dl_dbhash_rwlock;
static char *basename(const char *filename)
{
char *p = strrchr(filename, '/');
return p ? p + 1 : (char *)filename;
}
static int free_module(dl_module_info *module)
{
switch(module->type)
{
case DL_MODULE_TYPE_FILE:
{
return sceKernelStopUnloadModule(module->uid, 0, NULL, 0, NULL, NULL);
}
case DL_MODULE_TYPE_SYS:
{
return sceSysmoduleUnloadModule(module->sid);
}
default:
{
return -1;
}
}
}
static int load_module(dl_module_info *module)
{
switch(module->type)
{
case DL_MODULE_TYPE_FILE:
{
int ret = sceKernelLoadStartModule(module->name, 0, NULL, 0, NULL, NULL);
if (ret >=0 ) module->uid = ret;
return ret;
}
case DL_MODULE_TYPE_SYS:
{
return sceSysmoduleLoadModule(module->sid);
}
default:
{
return -1;
}
}
}
static void *symbol_lookup(const char * __restrict module, const char * __restrict symbol)
{
if (sceKernelLockReadRWLock(_dl_dbhash_rwlock, NULL) < 0)
{
DLERR("Error: failed to acquire read lock\n");
return NULL;
}
void *func;
dl_module_info *module_info = NULL;
dl_symbol_info *symbol_info = NULL;
HASH_FIND_STR(_dl_module_db, module, module_info);
if (!module_info)
{
DLERR("Error: failed to find module %s in database\n", module);
func = NULL;
goto end;
}
HASH_FIND_STR(module_info->dl_symbols, symbol, symbol_info);
if (!symbol_info)
{
func = NULL;
goto end;
}
int ret;
if (module_info->type == DL_MODULE_TYPE_FILE)
{
char *name = strdup(basename(module));
*strrchr(name, '.') = 0;
ret = taiGetModuleExportFunc(name, TAI_ANY_LIBRARY, symbol_info->nid, (uintptr_t *)&func);
free(name);
}
else
{
ret = taiGetModuleExportFunc(module, TAI_ANY_LIBRARY, symbol_info->nid, (uintptr_t *)&func);
}
func = !ret ? func : NULL;
end:
sceKernelUnlockReadRWLock(_dl_dbhash_rwlock);
return func;
}
int dlinit()
{
_dl_dbhash_rwlock = sceKernelCreateRWLock("dldbhashrwlock", 0x2, NULL);
if (_dl_dbhash_rwlock < 0)
{
fprintf(stderr, "Error: failed to create dbhash rwlock\n");
return -1;
}
_dl_errmsg = calloc(1, DLERRMSG_LENGTH_MAX+1);
if (!_dl_errmsg)
{
fprintf(stderr, "Error: no memory\n");
dlfree();
return -1;
}
return 0;
}
void dlfree()
{
dldbfreeall();
memset(_dl_errmsg, 0, DLERRMSG_LENGTH_MAX+1);
free(_dl_errmsg);
sceKernelDeleteRWLock(_dl_dbhash_rwlock);
}
int dldbadd(const char *path)
{
if (sceKernelLockWriteRWLock(_dl_dbhash_rwlock, NULL) < 0)
{
DLERR("Error: failed to acquire write lock\n");
return -1;
}
FILE *fp = fopen(path, "r");
if (fp == NULL)
{
DLERR("Error: could not open %s\n", path);
sceKernelUnlockWriteRWLock(_dl_dbhash_rwlock);
return -1;
}
int failed = 0;
char *linebuf = NULL;
size_t bufsize = 0;
int linenum = 1;
dl_module_info *current_module = NULL;
while(__getline(&linebuf, &bufsize, fp) > 0)
{
linebuf[strcspn(linebuf, "\r\n")] = 0; /* Removing trailing newline. */
switch(linebuf[0])
{
/* Module SID. */
case '#':
{
if(!current_module)
{
DLERR("Error: no modules found in %s\n", path);
failed = 1;
goto fail;
}
if (sscanf(linebuf, "#0x%X", ¤t_module->sid) != 1)
{
DLERR("Error: could not parse %s at line %d\n", path, linenum);
failed = 1;
goto fail;
}
break;
}
/* Exports NID entry. */
case '*':
{
if(!current_module)
{
DLERR("Error: no modules found in %s\n", path);
failed = 1;
goto fail;
}
dl_symbol_info *symbol = calloc(1, sizeof(dl_symbol_info));
if (!symbol)
{
DLERR("Error: no memory\n");
failed = 1;
goto fail;
}
if (sscanf(linebuf, "*%s 0x%X", symbol->name, &symbol->nid) != 2)
{
DLERR("Error: could not parse %s at line %d\n", path, linenum);
memset(symbol, 0, sizeof(dl_symbol_info));
free(symbol);
failed = 1;
goto fail;
}
dl_symbol_info *duplicated_symbol;
HASH_REPLACE_STR(current_module->dl_symbols, name, symbol, duplicated_symbol);
if (duplicated_symbol)
{
fprintf(stderr, "Warning: duplicated symbol %s at line %d\n", duplicated_symbol->name, linenum);
memset(duplicated_symbol, 0, sizeof(dl_symbol_info));
free(duplicated_symbol);
}
break;
}
/* Module type [name | path]. */
case '$':
{
HASH_FIND_STR(_dl_module_db, linebuf+3, current_module);
if (!current_module)
{
current_module = calloc(1, sizeof(dl_module_info));
if (!current_module)
{
DLERR("Error: no memory\n");
failed = 1;
goto fail;
}
char module_type;
if (sscanf(linebuf, "$%c %s", &module_type, current_module->name) != 2)
{
DLERR("Error: could not parse %s at line %d\n", path, linenum);
memset(current_module, 0, sizeof(dl_module_info));
free(current_module);
failed = 1;
goto fail;
}
current_module->type = module_type;
HASH_ADD_STR(_dl_module_db, name, current_module);
}
break;
}
}
linenum++;
}
fail:
if (failed) dldbfreeall();
fclose(fp);
memset(linebuf, 0, bufsize);
free(linebuf);
sceKernelUnlockWriteRWLock(_dl_dbhash_rwlock);
return 0;
}
int dldbfreeall()
{
if (sceKernelLockWriteRWLock(_dl_dbhash_rwlock, NULL) < 0)
{
DLERR("Error: failed to acquire write lock\n");
return -1;
}
dl_module_info *module, *tmp;
HASH_ITER(hh, _dl_module_db, module, tmp)
{
free_module(module);
HASH_DEL(_dl_module_db, module);
memset(module, 0, sizeof(dl_module_info));
free(module);
}
sceKernelUnlockWriteRWLock(_dl_dbhash_rwlock);
return 0;
}
void *dlopen(const char *filename, int flag)
{
dl_module_info *module = NULL;
dl_handle *new_handle = NULL;
HASH_FIND_STR(_dl_module_db, filename, module);
if (!module)
{
DLERR("Error: failed to find module %s in database\n", filename);
return NULL;
}
if (sceKernelLockWriteRWLock(_dl_dbhash_rwlock, NULL) < 0)
{
DLERR("Error: failed to acquire write lock\n");
return NULL;
}
if (!module->refcount)
{
if (load_module(module) < 0)
{
DLERR("Error: failed to load module %s\n", module->name);
goto end;
}
}
new_handle = calloc(1, sizeof(dl_handle));
if (!new_handle)
{
DLERR("Error: no memory\n");
if (!module->refcount)
{
free_module(module);
}
goto end;
}
module->refcount++;
strcpy(new_handle->name, module->name);
end:
sceKernelUnlockWriteRWLock(_dl_dbhash_rwlock);
return new_handle;
}
char *dlerror()
{
if (_dl_err)
{
_dl_err = 0;
return _dl_errmsg;
}
return NULL;
}
int dlclose(void *_handle)
{
dl_handle *handle = _handle;
dl_module_info *module = NULL;
HASH_FIND_STR(_dl_module_db, handle->name, module);
if (sceKernelLockWriteRWLock(_dl_dbhash_rwlock, NULL) < 0)
{
DLERR("Error: failed to acquire write lock\n");
return -1;
}
if (1 == module->refcount)
{
free_module(module);
}
module->refcount--;
sceKernelUnlockWriteRWLock(_dl_dbhash_rwlock);
memset(handle, 0, sizeof(dl_handle));
free(handle);
return 0;
}
void *dlsym(void * __restrict _handle, const char * __restrict symbol)
{
dl_handle *handle = _handle;
void *ret = NULL;
if (!handle)
{
int i;
for(i = 0; i < sizeof(_dl_default_module) / sizeof(_dl_default_module[0]); i++)
{
ret = symbol_lookup(_dl_default_module[i], symbol);
if (ret) return ret;
}
if (!_dl_err) DLERR("Error: failed to find symbol %s in default modules\n", symbol);
return NULL;
}
ret = symbol_lookup(handle->name, symbol);
if (!ret && !_dl_err) DLERR("Error: failed to find symbol %s in module %s\n", symbol, handle->name);
return ret;
}