-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathmodhook.c
More file actions
82 lines (66 loc) · 1.82 KB
/
modhook.c
File metadata and controls
82 lines (66 loc) · 1.82 KB
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
#include "modhook.h"
#include <intrman.h>
#include <stdint.h>
iop_library_t *modhook_getModule(const char *name)
{
iop_library_t *libptr;
int i;
// Get first loaded library
libptr = GetLoadcoreInternalData()->let_next;
// Loop through all loaded libraries
while (libptr != NULL) {
// Compare library name only
for (i = 0; i < 8; i++) {
if (libptr->name[i] != name[i])
break;
}
// Return if match
if (i == 8)
return libptr;
// Next library
libptr = libptr->prev;
}
return NULL;
}
unsigned int modhook_getTableSize(iop_library_t *lib)
{
void **exp;
unsigned int size;
exp = NULL;
if (lib != NULL) {
exp = lib->exports;
}
size = 0;
if (exp != NULL)
while (*exp++ != NULL)
size++;
return size;
}
void *modhook_hookExportEntry(iop_library_t *lib, unsigned int entry, void *func)
{
if (entry < modhook_getTableSize(lib)) {
int oldstate;
void **exp, *temp;
exp = &lib->exports[entry];
CpuSuspendIntr(&oldstate);
temp = *exp;
*exp = func;
func = temp;
CpuResumeIntr(oldstate);
return func;
}
return NULL;
}
void modhook_relinkExports(iop_library_t *lib)
{
struct irx_import_table *table;
struct irx_import_stub *stub;
// go through each table that imports the library
for (table = lib->caller; table != NULL; table = table->next) {
// go through each import in the table
for (stub = (struct irx_import_stub *)table->stubs; stub->jump != 0; stub++) {
// patch the stub to jump to the address specified in the library export table for "fno"
stub->jump = 0x08000000 | (((uint32_t)lib->exports[stub->fno] << 4) >> 6);
}
}
}