-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathggame.c
More file actions
465 lines (440 loc) · 14.1 KB
/
ggame.c
File metadata and controls
465 lines (440 loc) · 14.1 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
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
#include "ggame.h"
#include "geelog.h"
int ggame_add_label(GGame* prGame, const SceUInt32 address, const char* name) {
GlabelMap* prMap = NULL;
Glabel* prLabel = NULL;
if (prGame == NULL) {
return GGAME_NULLPTR;
}
prMap = ggame_get_labelmap(prGame);
prLabel = glabelmap_add(prMap);
if (prLabel == NULL) {
return GGAME_FAILURE;
}
if (glabel_set(prLabel, address, name) < 0) {
return GGAME_FAILURE;
}
return GGAME_SUCCESS;
}
Glabel* ggame_find_label(GGame* prGame, const SceUInt32 address) {
GlabelMap* prMap = NULL;
if (prGame == NULL) {
return NULL;
}
prMap = ggame_get_labelmap(prGame);
return glabelmap_find(prMap, address);
}
Gsegment* ggame_find_segment(GGame* prGame, const SceUInt32 address) {
GsegMap* prMap = NULL;
if (prGame == NULL) {
return NULL;
}
prMap = ggame_get_segmap(prGame);
return gsegmap_find(prMap, address);
}
int ggame_get_exportcount(GGame* prGame) {
int count = GGAME_FAILURE;
SceModule* prModule = NULL;
if (prGame != NULL) {
prModule = ggame_get_module(prGame);
if (prModule != NULL) {
count = prModule->ent_size / sizeof(SceLibraryEntryTable);
}
}
return count;
}
SceLibraryEntryTable* ggame_get_exporttable(GGame* prGame) {
SceModule* prModule = NULL;
if (prGame != NULL) {
prModule = ggame_get_module(prGame);
if (prModule != NULL) {
return (SceLibraryEntryTable*)prModule->ent_top;
}
}
return NULL;
}
SceLibraryStubTable* ggame_get_importtable(GGame* prGame) {
SceModule* prModule = NULL;
if (prGame != NULL) {
prModule = ggame_get_module(prGame);
if (prModule != NULL) {
return (SceLibraryStubTable*)prModule->stub_top;
}
}
return NULL;
}
GlabelMap* ggame_get_labelmap(GGame* prGame) {
if (prGame == NULL) {
return NULL;
}
return &prGame->labelMap;
}
SceModule* ggame_get_module(GGame* prGame) {
if (prGame != NULL) {
return prGame->gameModule;
}
return NULL;
}
SceUInt32 ggame_get_module_start(GGame* prGame) {
SceUInt32 address = 0;
GstubMap* prStubMap = NULL;
ModStub* prStub = NULL;
if (prGame != NULL) {
prStubMap = ggame_get_stubmap(prGame);
if (prStubMap != NULL) {
prStub = gstubmap_find(prStubMap, GGAME_NID_MODSTART);
if (prStub != NULL) {
address = prStub->pvStub;
}
}
}
return address;
}
GsegMap* ggame_get_segmap(GGame* prGame) {
if (prGame == NULL) {
return NULL;
}
return &prGame->segmentMap;
}
GstubMap* ggame_get_stubmap(GGame* prGame) {
if (prGame == NULL) {
return NULL;
}
return &prGame->stubMap;
}
int ggame_init(GGame* prGame) {
GsegMap* prMap = NULL;
GstubMap* prStubs = NULL;
GlabelMap* prLabels = NULL;
if (prGame == NULL) {
return GGAME_NULLPTR;
}
sprintf(prGame->sGameId, "");
sprintf(prGame->sDataPath, GGAME_DEF_DATAPATH);
prGame->gameModule = NULL;
prMap = ggame_get_segmap(prGame);
if (gsegmap_init(prMap) < 0) {
return GGAME_FAILURE;
}
prStubs = ggame_get_stubmap(prGame);
if (gstubmap_init(prStubs) < 0) {
return GGAME_FAILURE;
}
prLabels = ggame_get_labelmap(prGame);
if (glabelmap_init(prLabels) < 0) {
return GGAME_FAILURE;
}
return GGAME_SUCCESS;
}
int ggame_load_exports(GGame* prGame) {
SceModule* prModule = NULL;
SceLibraryEntryTable* prEnt = NULL;
SceUInt32* prFnid = NULL;
SceUInt32* prVnid = NULL;
SceUInt32* prFstub = NULL;
SceUInt32* prVstub = NULL;
GstubMap* prSmap = NULL;
ModStub* prStub = NULL;
SceUInt32 nId = 0;
void* pvStub = NULL;
int vars = 0;
int funcs = 0;
int offset = 0;
int i = 0;
char *sName = NULL;
if (prGame == NULL) {
return GGAME_NULLPTR;
}
prModule = ggame_get_module(prGame);
if (prModule == NULL) {
return GGAME_INVMODULE;
}
prEnt = ggame_get_exporttable(prGame);
if (prEnt == NULL) {
return GGAME_FAILURE;
}
vars = prEnt->vstubcount;
funcs = prEnt->stubcount;
prSmap = ggame_get_stubmap(prGame);
for (i = 0; i < funcs; i++) {
offset = i * sizeof(SceUInt32);
prFnid = (SceUInt32*)(prEnt->entrytable + offset);
nId = *prFnid;
offset = (i + funcs + vars) * sizeof(SceUInt32);
prFstub = (SceUInt32*)(prEnt->entrytable + offset);
pvStub = *prFstub;
prStub = gstubmap_add(prSmap);
if (prStub != NULL) {
sName = NULL;
if (nId == GGAME_NID_MODSTART) {
sName = "module_start";
ggame_add_label(prGame, pvStub, sName);
}
if (nId == GGAME_NID_MODSTOP) {
sName = "module_stop";
ggame_add_label(prGame, pvStub, sName);
}
if (modstub_set(prStub, MST_Function, nId, pvStub, sName,
prModule->modname) < 0) {
return GGAME_FAILURE;
}
}
}
for (i = 0; i < vars; i++) {
offset = (i + funcs) * sizeof(SceUInt32);
prVnid = (SceUInt32*)(prEnt->entrytable + offset);
nId = *prVnid;
offset = (i + funcs + vars + funcs) * sizeof(SceUInt32);
prVstub = (SceUInt32*)(prEnt->entrytable + offset);
pvStub = *prVstub;
prStub = gstubmap_add(prSmap);
if (prStub != NULL) {
sName = NULL;
if (nId == GGAME_NID_MODINFO) {
sName = "module_info";
ggame_add_label(prGame, pvStub, sName);
}
if (modstub_set(prStub, MST_Variable, nId, pvStub, sName,
prModule->modname) < 0) {
return GGAME_FAILURE;
}
}
}
return GGAME_SUCCESS;
}
int ggame_load_gameid(GGame* prGame) {
SceUID fd = -1;
int br = 0;
if (prGame == NULL) {
return GGAME_NULLPTR;
}
if (strlen(prGame->sDataPath) < 1) {
return GGAME_INVPATH;
}
fd = sceIoOpen(prGame->sDataPath, PSP_O_RDONLY, 0777);
if (fd < 0) {
return GGAME_FAILURE;
}
br = sceIoRead(fd, prGame->sGameId, GGAME_GAMEID_LEN);
if (br != GGAME_GAMEID_LEN) {
sceIoClose(fd);
return GGAME_IOERROR;
}
if (sceIoClose(fd) < 0) {
return GGAME_IOERROR;
}
return GGAME_SUCCESS;
}
int ggame_load_imports(GGame* prGame) {
SceModule* prModule = NULL;
SceLibraryStubTable* prTable = NULL;
GstubMap* prSmap = NULL;
GsegMap* prMap = NULL;
Gsegment* prSegment = NULL;
ModStub* prStub = NULL;
void* pvStubTop = NULL;
SceUInt32* prNid = NULL;
SceUInt32 nId = 0;
SceUInt32 textEnd = 0;
void* pvStub = NULL;
unsigned int uiStubSize = 0;
unsigned int uiStubRead = 0;
int i = 0;
char sSegName[64];
char sLabel[GLABEL_TEXT_LEN + 1];
if (prGame == NULL) {
return GGAME_NULLPTR;
}
prModule = ggame_get_module(prGame);
if (prModule == NULL) {
return GGAME_INVMODULE;
}
prSmap = ggame_get_stubmap(prGame);
prMap = ggame_get_segmap(prGame);
pvStubTop = prModule->stub_top;
uiStubSize = prModule->stub_size;
textEnd = pvStubTop;
while (uiStubRead < uiStubSize) {
prTable = (SceLibraryStubTable*)(pvStubTop + uiStubRead);
prNid = (SceUInt32*)prTable->nidtable;
for (i = 0; i < prTable->stubcount; i++) {
nId = prNid[i];
pvStub = prTable->stubtable + (i * 8);
if (pvStub < textEnd) {
textEnd = pvStub;
}
prStub = gstubmap_add(prSmap);
if (prStub != NULL) {
if (modstub_set(prStub, MST_Function, nId, pvStub, NULL,
prTable->libname) < 0) {
return GGAME_FAILURE;
}
switch (nId) {
case 0x05572A5F:
sprintf(sLabel, "sceKernelExitGame");
break;
case 0x090CCB3F:
sprintf(sLabel, "sceKernelPowerTick");
break;
case 0x13A5ABEF:
sprintf(sLabel, "sceKernelPrintf");
break;
case 0x172D316E:
sprintf(sLabel, "sceKernelStdin");
break;
case 0x20E911AA:
sprintf(sLabel, "sceKernelUnloadModule");
break;
case 0x27E22EC2:
sprintf(sLabel, "sceKernelResumeDispatchThread");
break;
case 0x27EB27B8:
sprintf(sLabel, "sceIoLseek");
break;
case 0x293B45B8:
sprintf(sLabel, "sceKernelGetThreadId");
break;
case 0x2E0911AA:
sprintf(sLabel, "sceKernelUnloadModule");
break;
case 0x3AEE7261:
sprintf(sLabel, "sceKernelPowerUnlock");
break;
case 0x446D8DE6:
sprintf(sLabel, "sceKernelCreateThread");
break;
case 0x4AC57943:
sprintf(sLabel, "sceKernelRegisterExitCallback");
break;
case 0x50F0C1EC:
sprintf(sLabel, "sceKernelStartModule");
break;
case 0x7591C7DB:
sprintf(sLabel, "sceKernelSetCompiledSdkVersion");
break;
case 0x977DE386:
sprintf(sLabel, "sceKernelLoadModule");
break;
case 0xA14F40B2:
sprintf(sLabel, "sceKernelVolatileMemTryLock");
break;
case 0xA569E425:
sprintf(sLabel, "sceKernelVolatileMemUnlock");
break;
case 0xA6BAB2E9:
sprintf(sLabel, "sceKernelStdout");
break;
case 0xAA73C935:
sprintf(sLabel, "sceKernelExitThread");
break;
case 0xBD2F1094:
sprintf(sLabel, "sceKernelLoadExec");
break;
case 0xD1FF982A:
sprintf(sLabel, "sceKernelStopModule");
break;
case 0xD8B73127:
sprintf(sLabel, "sceKernelGetModuleIdByAddress");
break;
case 0xEADB1BD7:
sprintf(sLabel, "sceKernelPowerLock");
break;
case 0xF0A26395:
sprintf(sLabel, "sceKernelGetModuleId");
break;
case 0xF475845D:
sprintf(sLabel, "sceKernelStartThread");
break;
case 0xF77D77CB:
sprintf(sLabel, "sceKernelSetCompilerVersion");
break;
case 0xF78BA90A:
sprintf(sLabel, "sceKernelStderr");
break;
default:
sprintf(sLabel, "%s_%08X", prTable->libname, nId);
}
ggame_add_label(prGame, pvStub, sLabel);
}
}
prSegment = gsegmap_add(prMap);
if (prSegment != NULL) {
sprintf(sSegName, ".%s.text", prTable->libname);
if (gsegment_set(prSegment, prTable->stubtable,
prTable->stubtable + (prTable->stubcount * 8) + 4,
sSegName, SET_Text) < 0) {
return GGAME_FAILURE;
}
}
uiStubRead += (prTable->len * 4);
}
prSegment = gsegmap_add(prMap);
if (prSegment != NULL) {
if (gsegment_set(prSegment, 0x08804000, textEnd, ".text", SET_Text) < 0)
{
return GGAME_FAILURE;
}
}
return GGAME_SUCCESS;
}
int ggame_load_module(GGame* prGame) {
const char* sFunc = "ggame_load_module";
SceUInt32 entTop = 0;
SceUInt32 entSz = 0;
SceUInt32 entEnd = 0;
SceUInt32 infTop = 0;
SceUInt32 infEnd = 0;
GsegMap* prMap = NULL;
Gsegment* prSeg = NULL;
GstubMap* prSmap = NULL;
ModStub* prStub = NULL;
char sMsg[GEELOG_LINELEN + 1];
if (prGame == NULL) {
return GGAME_NULLPTR;
}
prMap = ggame_get_segmap(prGame);
prGame->gameModule = sceKernelFindModuleByAddress(GGAME_MODULE_ADDR);
if (prGame->gameModule == NULL) {
return GGAME_FAILURE;
}
entTop = prGame->gameModule->ent_top;
entSz = prGame->gameModule->ent_size;
entEnd = entTop + entSz + 4;
prSeg = gsegmap_add(prMap);
if (prSeg == NULL) {
return GGAME_FAILURE;
}
if (gsegment_set(prSeg, entTop, entEnd, ".lib.ent.top", SET_RoData) < 0)
{
return GGAME_FAILURE;
}
entTop = prGame->gameModule->stub_top;
entSz = prGame->gameModule->stub_size;
entEnd = entTop + entSz + 4;
prSeg = gsegmap_add(prMap);
if (prSeg == NULL) {
return GGAME_FAILURE;
}
if (gsegment_set(prSeg, entTop, entEnd, ".lib.stub.top", SET_RoData) < 0)
{
return GGAME_FAILURE;
}
if (ggame_load_exports(prGame) < 0) {
return GGAME_FAILURE;
}
if (ggame_load_imports(prGame) < 0) {
return GGAME_FAILURE;
}
prSmap = ggame_get_stubmap(prGame);
prStub = gstubmap_find(prSmap, GGAME_NID_MODINFO);
if (prStub != NULL) {
infTop = prStub->pvStub;
infEnd = infTop + sizeof(SceModuleInfo) + 4;
prSeg = gsegmap_add(prMap);
if (prSeg != NULL) {
gsegment_set(prSeg, infTop, infEnd, ".rodata.sceModuleInfo",
SET_RoData);
}
}
return GGAME_SUCCESS;
}