Skip to content

Commit 6c8a1f8

Browse files
committed
make ioplib an independent library
1 parent 85aab1c commit 6c8a1f8

5 files changed

Lines changed: 147 additions & 1 deletion

File tree

iop/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ SUBDIRS = \
2424
startup \
2525
system \
2626
tcpip \
27-
usb
27+
usb \
28+
utilities
2829

2930
include $(PS2SDKSRC)/Defs.make
3031
include $(PS2SDKSRC)/Rules.make

iop/utilities/Makefile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# _____ ___ ____ ___ ____
2+
# ____| | ____| | | |____|
3+
# | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
4+
#-----------------------------------------------------------------------
5+
# Copyright 2001-2004, ps2dev - http://www.ps2dev.org
6+
# Licenced under Academic Free License version 2.0
7+
# Review ps2sdk README & LICENSE files for further details.
8+
9+
SUBDIRS = modhook
10+
11+
include $(PS2SDKSRC)/Defs.make
12+
include $(PS2SDKSRC)/Rules.make

iop/utilities/modhook/Makefile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# _____ ___ ____ ___ ____
2+
# ____| | ____| | | |____|
3+
# | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
4+
#-----------------------------------------------------------------------
5+
# Copyright 2001-2024, ps2dev - http://www.ps2dev.org
6+
# Licenced under Academic Free License version 2.0
7+
# Review ps2sdk README & LICENSE files for further details.
8+
9+
IOP_OBJS = ioplib.o
10+
IOP_LIB = libmodhook.a
11+
12+
include $(PS2SDKSRC)/Defs.make
13+
include $(PS2SDKSRC)/iop/Rules.lib.make
14+
include $(PS2SDKSRC)/iop/Rules.make
15+
include $(PS2SDKSRC)/iop/Rules.release
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#ifndef IOPLIB_H
2+
#define IOPLIB_H
3+
4+
#include <loadcore.h>
5+
6+
/**
7+
* @file ioplib.c
8+
* @brief IOP module manipulation library for hooking exports.
9+
* @note depends on: `CpuSuspendIntr` `CpuResumeIntr` `GetLoadcoreInternalData`
10+
*/
11+
12+
/**
13+
* @brief returns an iop library pointer for the specified IRX module
14+
* @returns NULL on error, else, a pointer to the struct
15+
*/
16+
iop_library_t *ioplib_getByName(const char *name);
17+
18+
/**
19+
* @brief returns the size of the export table for the specified module
20+
* @param lib the library to obtain the export table size
21+
* @returns the ammount of exports registered for that module
22+
*/
23+
unsigned int ioplib_getTableSize(iop_library_t *lib);
24+
25+
/**
26+
* @brief replaces the function called as a module export
27+
* @param lib The iop_library_t struct of the module to modify
28+
* @param entry the export number to be modified
29+
* @param func the function to replace the export with
30+
* @returns a pointer to the original function
31+
*/
32+
void *ioplib_hookExportEntry(iop_library_t *lib, unsigned int entry, void *func);
33+
void ioplib_relinkExports(iop_library_t *lib);
34+
35+
36+
#endif

iop/utilities/modhook/src/ioplib.c

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include "ioplib.h"
2+
#include <intrman.h>
3+
#include <stdint.h>
4+
5+
iop_library_t *ioplib_getByName(const char *name)
6+
{
7+
iop_library_t *libptr;
8+
int i;
9+
10+
// Get first loaded library
11+
libptr = GetLoadcoreInternalData()->let_next;
12+
// Loop through all loaded libraries
13+
while (libptr != NULL) {
14+
// Compare library name only
15+
for (i = 0; i < 8; i++) {
16+
if (libptr->name[i] != name[i])
17+
break;
18+
}
19+
20+
// Return if match
21+
if (i == 8)
22+
return libptr;
23+
24+
// Next library
25+
libptr = libptr->prev;
26+
}
27+
28+
return NULL;
29+
}
30+
31+
unsigned int ioplib_getTableSize(iop_library_t *lib)
32+
{
33+
void **exp;
34+
unsigned int size;
35+
36+
exp = NULL;
37+
if (lib != NULL) {
38+
exp = lib->exports;
39+
}
40+
size = 0;
41+
42+
if (exp != NULL)
43+
while (*exp++ != NULL)
44+
size++;
45+
46+
return size;
47+
}
48+
49+
void *ioplib_hookExportEntry(iop_library_t *lib, unsigned int entry, void *func)
50+
{
51+
if (entry < ioplib_getTableSize(lib)) {
52+
int oldstate;
53+
void **exp, *temp;
54+
55+
exp = &lib->exports[entry];
56+
57+
CpuSuspendIntr(&oldstate);
58+
temp = *exp;
59+
*exp = func;
60+
func = temp;
61+
CpuResumeIntr(oldstate);
62+
63+
return func;
64+
}
65+
66+
return NULL;
67+
}
68+
69+
void ioplib_relinkExports(iop_library_t *lib)
70+
{
71+
struct irx_import_table *table;
72+
struct irx_import_stub *stub;
73+
74+
// go through each table that imports the library
75+
for (table = lib->caller; table != NULL; table = table->next) {
76+
// go through each import in the table
77+
for (stub = (struct irx_import_stub *)table->stubs; stub->jump != 0; stub++) {
78+
// patch the stub to jump to the address specified in the library export table for "fno"
79+
stub->jump = 0x08000000 | (((uint32_t)lib->exports[stub->fno] << 4) >> 6);
80+
}
81+
}
82+
}

0 commit comments

Comments
 (0)