-
Notifications
You must be signed in to change notification settings - Fork 27
/
ProcessPrx.C
2618 lines (2341 loc) · 59.8 KB
/
ProcessPrx.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
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************
* PRXTool : Utility for PSP executables.
* (c) TyRaNiD 2k5
*
* ProcessPrx.C - Implementation of a class to manipulate a PRX
***************************************************************/
#include <stdio.h>
#include <string.h>
#include <cassert>
#include "ProcessPrx.h"
#include "VirtualMem.h"
#include "output.h"
#include "disasm.h"
static const char* g_szRelTypes[16] =
{
"R_NONE",
"R_16",
"R_32",
"R_REL32",
"R_26",
"R_HI16",
"R_LO16",
"R_GPREL16",
"R_LITERAL",
"R_GOT16",
"R_PC16",
"R_CALL16",
"R_GPREL32",
"X_HI16",
"X_J26",
"X_JAL26"
};
/* Flag indicates the reloc offset field is relative to the text section base */
#define RELOC_OFS_TEXT 0
/* Flag indicates the reloc offset field is relative to the data section base */
#define RELOC_OFS_DATA 1
/* Flag indicates the reloc'ed field should be fixed up relative to the data section base */
#define RELOC_REL_DATA 256
/* Minimum string size */
#define MINIMUM_STRING 4
CProcessPrx::CProcessPrx(u32 dwBase)
: CProcessElf()
, m_defNidMgr()
, m_pCurrNidMgr(&m_defNidMgr)
, m_pElfRelocs(NULL)
, m_iRelocCount(0)
, m_dwBase(dwBase)
, m_blXmlDump(false)
{
memset(&m_modInfo, 0, sizeof(PspModule));
m_blPrxLoaded = false;
}
CProcessPrx::~CProcessPrx()
{
FreeMemory();
}
void CProcessPrx::FreeMemory()
{
/* Lets delete the export list */
PspLibExport *pExport;
PspLibImport *pImport;
pExport = m_modInfo.exp_head;
while(pExport != NULL)
{
PspLibExport *pNext;
pNext = pExport->next;
delete pExport;
pExport = pNext;
}
pImport = m_modInfo.imp_head;
while(pImport != NULL)
{
PspLibImport *pNext;
pNext = pImport->next;
delete pImport;
pImport = pNext;
}
if(m_pElfRelocs != NULL)
{
delete m_pElfRelocs;
m_pElfRelocs = NULL;
}
m_iRelocCount = 0;
/* Check the import and export lists and free */
memset(&m_modInfo, 0, sizeof(PspModule));
FreeSymbols(m_syms);
FreeImms(m_imms);
}
int CProcessPrx::LoadSingleImport(PspModuleImport *pImport, u32 addr)
{
bool blError = true;
int count = 0;
int iLoop;
u32 nidAddr;
u32 funcAddr;
u32 varAddr;
PspLibImport *pLib = NULL;
SAFE_ALLOC(pLib, PspLibImport);
if(pLib != NULL)
{
do
{
memset(pLib, 0, sizeof(PspModuleImport));
pLib->addr = addr;
pLib->stub.name = LW(pImport->name);
pLib->stub.flags = LW(pImport->flags);
pLib->stub.counts = LW(pImport->counts);
pLib->stub.nids = LW(pImport->nids);
pLib->stub.funcs = LW(pImport->funcs);
pLib->stub.vars = LW(pImport->vars);
if(pLib->stub.name == 0)
{
/* Shouldn't be zero, although technically it could be */
COutput::Puts(LEVEL_ERROR, "Import libraries must have a name");
break;
}
else
{
char *pName = (char*) m_vMem.GetPtr(pLib->stub.name);
const char *dep;
if(pName == NULL)
{
COutput::Printf(LEVEL_ERROR, "Invalid memory address for import name (0x%08X)\n", pLib->stub.name);
break;
}
/* Should use strncpy I guess */
strcpy(pLib->name, pName);
dep = m_pCurrNidMgr->FindDependancy(pName);
if(dep)
{
const char *slash;
/* Remove any path element */
slash = strrchr(dep, '/');
if(slash)
{
dep = slash + 1;
}
strcpy(pLib->file, dep);
}
}
COutput::Printf(LEVEL_DEBUG, "Found import library '%s'\n", pLib->name);
COutput::Printf(LEVEL_DEBUG, "Flags %08X, counts %08X, nids %08X, funcs %08X\n",
pLib->stub.flags, pLib->stub.counts, pLib->stub.nids, pLib->stub.funcs);
pLib->v_count = (pLib->stub.counts >> 8) & 0xFF;
pLib->f_count = (pLib->stub.counts >> 16) & 0xFFFF;
count = pLib->stub.counts & 0xFF;
nidAddr = pLib->stub.nids;
funcAddr = pLib->stub.funcs;
varAddr = pLib->stub.vars;
if(m_vMem.GetSize(nidAddr) < (sizeof(u32) * pLib->f_count))
{
COutput::Puts(LEVEL_ERROR, "Not enough space for library import nids");
break;
}
if(m_vMem.GetSize(funcAddr) < (u32) (8 * pLib->f_count))
{
COutput::Puts(LEVEL_ERROR, "Not enough space for library functions");
break;
}
for(iLoop = 0; iLoop < pLib->f_count; iLoop++)
{
pLib->funcs[iLoop].nid = m_vMem.GetU32(nidAddr);
strcpy(pLib->funcs[iLoop].name, m_pCurrNidMgr->FindLibName(pLib->name, pLib->funcs[iLoop].nid));
pLib->funcs[iLoop].type = PSP_ENTRY_FUNC;
pLib->funcs[iLoop].addr = funcAddr;
pLib->funcs[iLoop].nid_addr = nidAddr;
COutput::Printf(LEVEL_DEBUG, "Found import nid:0x%08X func:0x%08X name:%s\n",
pLib->funcs[iLoop].nid, pLib->funcs[iLoop].addr, pLib->funcs[iLoop].name);
nidAddr += 4;
funcAddr += 8;
}
for(iLoop = 0; iLoop < pLib->v_count; iLoop++)
{
u32 varFixup;
u32 varData;
pLib->vars[iLoop].addr = m_vMem.GetU32(varAddr);
pLib->vars[iLoop].nid = m_vMem.GetU32(varAddr+4);
pLib->vars[iLoop].type = PSP_ENTRY_VAR;
pLib->vars[iLoop].nid_addr = varAddr+4;
strcpy(pLib->vars[iLoop].name, m_pCurrNidMgr->FindLibName(pLib->name, pLib->vars[iLoop].nid));
COutput::Printf(LEVEL_DEBUG, "Found variable nid:0x%08X addr:0x%08X name:%s\n",
pLib->vars[iLoop].nid, pLib->vars[iLoop].addr, pLib->vars[iLoop].name);
varFixup = pLib->vars[iLoop].addr;
while((varData = m_vMem.GetU32(varFixup)))
{
COutput::Printf(LEVEL_DEBUG, "Variable Fixup: addr:%08X type:%08X\n",
(varData & 0x3FFFFFF) << 2, varData >> 26);
varFixup += 4;
}
varAddr += 8;
}
if(m_modInfo.imp_head == NULL)
{
pLib->next = NULL;
pLib->prev = NULL;
m_modInfo.imp_head = pLib;
}
else
{
// Search for the end of the list
PspLibImport* pImport;
pImport = m_modInfo.imp_head;
while(pImport->next != NULL)
{
pImport = pImport->next;
}
pImport->next = pLib;
pLib->prev = pImport;
pLib->next = NULL;
}
blError = false;
}
while(false);
}
else
{
COutput::Puts(LEVEL_ERROR, "Could not allocate memory for import library");
}
if(blError == true)
{
count = 0;
if(pLib != NULL)
{
delete pLib;
pLib = NULL;
}
}
return count;
}
bool CProcessPrx::LoadImports()
{
bool blRet = true;
u32 imp_base;
u32 imp_end;
assert(m_modInfo.imp_head == NULL);
imp_base = m_modInfo.info.imports;
imp_end = m_modInfo.info.imp_end;
if(imp_base != 0)
{
while((imp_end - imp_base) >= PSP_IMPORT_BASE_SIZE)
{
u32 count;
PspModuleImport *pImport;
pImport = (PspModuleImport*) m_vMem.GetPtr(imp_base);
if(pImport != NULL)
{
count = LoadSingleImport(pImport, imp_base);
if(count > 0)
{
imp_base += (count * sizeof(u32));
}
else
{
blRet = false;
break;
}
}
else
{
blRet = false;
break;
}
}
}
return blRet;
}
int CProcessPrx::LoadSingleExport(PspModuleExport *pExport, u32 addr)
{
bool blError = true;
int count = 0;
int iLoop;
PspLibExport* pLib = NULL;
u32 expAddr;
assert(pExport != NULL);
SAFE_ALLOC(pLib, PspLibExport);
if(pLib != NULL)
{
do
{
memset(pLib, 0, sizeof(PspLibExport));
pLib->addr = addr;
pLib->stub.name = LW(pExport->name);
pLib->stub.flags = LW(pExport->flags);
pLib->stub.counts = LW(pExport->counts);
pLib->stub.exports = LW(pExport->exports);
if(pLib->stub.name == 0)
{
/* If 0 then this is the system, this should be the only one */
strcpy(pLib->name, PSP_SYSTEM_EXPORT);
}
else
{
char *pName = (char*) m_vMem.GetPtr(pLib->stub.name);
if(pName == NULL)
{
COutput::Printf(LEVEL_ERROR, "Invalid memory address for export name (0x%08X)\n", pLib->stub.name);
break;
}
strcpy(pLib->name, pName);
}
COutput::Printf(LEVEL_DEBUG, "Found export library '%s'\n", pLib->name);
COutput::Printf(LEVEL_DEBUG, "Flags %08X, counts %08X, exports %08X\n",
pLib->stub.flags, pLib->stub.counts, pLib->stub.exports);
pLib->v_count = (pLib->stub.counts >> 8) & 0xFF;
pLib->f_count = (pLib->stub.counts >> 16) & 0xFFFF;
count = pLib->stub.counts & 0xFF;
expAddr = pLib->stub.exports;
if(m_vMem.GetSize(expAddr) < (sizeof(u32) * (pLib->v_count + pLib->f_count)))
{
COutput::Printf(LEVEL_ERROR, "Invalid memory address for exports (0x%08X)\n", pLib->stub.exports);
break;
}
for(iLoop = 0; iLoop < pLib->f_count; iLoop++)
{
/* We will fix up the names later */
pLib->funcs[iLoop].nid = m_vMem.GetU32(expAddr);
strcpy(pLib->funcs[iLoop].name, m_pCurrNidMgr->FindLibName(pLib->name, pLib->funcs[iLoop].nid));
pLib->funcs[iLoop].type = PSP_ENTRY_FUNC;
pLib->funcs[iLoop].addr = m_vMem.GetU32(expAddr + (sizeof(u32) * (pLib->v_count + pLib->f_count)));
pLib->funcs[iLoop].nid_addr = expAddr;
COutput::Printf(LEVEL_DEBUG, "Found export nid:0x%08X func:0x%08X name:%s\n",
pLib->funcs[iLoop].nid, pLib->funcs[iLoop].addr, pLib->funcs[iLoop].name);
expAddr += 4;
}
for(iLoop = 0; iLoop < pLib->v_count; iLoop++)
{
/* We will fix up the names later */
pLib->vars[iLoop].nid = m_vMem.GetU32(expAddr);
strcpy(pLib->vars[iLoop].name, m_pCurrNidMgr->FindLibName(pLib->name, pLib->vars[iLoop].nid));
pLib->vars[iLoop].type = PSP_ENTRY_FUNC;
pLib->vars[iLoop].addr = m_vMem.GetU32(expAddr + (sizeof(u32) * (pLib->v_count + pLib->f_count)));
pLib->vars[iLoop].nid_addr = expAddr;
COutput::Printf(LEVEL_DEBUG, "Found export nid:0x%08X var:0x%08X name:%s\n",
pLib->vars[iLoop].nid, pLib->vars[iLoop].addr, pLib->vars[iLoop].name);
expAddr += 4;
}
if(m_modInfo.exp_head == NULL)
{
pLib->next = NULL;
pLib->prev = NULL;
m_modInfo.exp_head = pLib;
}
else
{
// Search for the end of the list
PspLibExport* pExport;
pExport = m_modInfo.exp_head;
while(pExport->next != NULL)
{
pExport = pExport->next;
}
pExport->next = pLib;
pLib->prev = pExport;
pLib->next = NULL;
}
blError = false;
}
while(false);
}
else
{
COutput::Printf(LEVEL_ERROR, "Couldn't allocate memory for export\n");
}
if(blError)
{
count = 0;
if(pLib != NULL)
{
delete pLib;
pLib = NULL;
}
}
return count;
}
bool CProcessPrx::LoadExports()
{
bool blRet = true;
u32 exp_base;
u32 exp_end;
assert(m_modInfo.exp_head == NULL);
exp_base = m_modInfo.info.exports;
exp_end = m_modInfo.info.exp_end;
if(exp_base != 0)
{
while((exp_end - exp_base) >= sizeof(PspModuleExport))
{
u32 count;
PspModuleExport *pExport;
pExport = (PspModuleExport*) m_vMem.GetPtr(exp_base);
if(pExport != NULL)
{
count = LoadSingleExport(pExport, exp_base);
if(count > 0)
{
exp_base += (count * sizeof(u32));
}
else
{
blRet = false;
break;
}
}
else
{
blRet = false;
break;
}
}
}
return blRet;
}
bool CProcessPrx::FillModule(u8 *pData, u32 iAddr)
{
bool blRet = false;
if(pData != NULL)
{
PspModuleInfo *pModInfo;
pModInfo = (PspModuleInfo*) pData;
memcpy(m_modInfo.name, pModInfo->name, PSP_MODULE_MAX_NAME);
m_modInfo.name[PSP_MODULE_MAX_NAME] = 0;
m_modInfo.addr = iAddr;
memcpy(&m_modInfo.info, pModInfo, sizeof(PspModuleInfo));
m_modInfo.info.flags = LW(m_modInfo.info.flags);
m_modInfo.info.gp = LW(m_modInfo.info.gp);
m_modInfo.info.exports = LW(m_modInfo.info.exports);
m_modInfo.info.exp_end = LW(m_modInfo.info.exp_end);
m_modInfo.info.imports = LW(m_modInfo.info.imports);
m_modInfo.info.imp_end = LW(m_modInfo.info.imp_end);
m_stubBottom = m_modInfo.info.exports - 4; // ".lib.ent.top"
COutput::Printf(LEVEL_DEBUG, "Stub bottom 0x%08X\n", m_stubBottom);
blRet = true;
if(COutput::GetDebug())
{
COutput::Puts(LEVEL_DEBUG, "Module Info:");
COutput::Printf(LEVEL_DEBUG, "Name: %s\n", m_modInfo.name);
COutput::Printf(LEVEL_DEBUG, "Addr: 0x%08X\n", m_modInfo.addr);
COutput::Printf(LEVEL_DEBUG, "Flags: 0x%08X\n", m_modInfo.info.flags);
COutput::Printf(LEVEL_DEBUG, "GP: 0x%08X\n", m_modInfo.info.gp);
COutput::Printf(LEVEL_DEBUG, "Exports: 0x%08X, Exp_end 0x%08X\n", m_modInfo.info.exports, m_modInfo.info.exp_end);
COutput::Printf(LEVEL_DEBUG, "Imports: 0x%08X, Imp_end 0x%08X\n", m_modInfo.info.imports, m_modInfo.info.imp_end);
}
}
return blRet;
}
bool CProcessPrx::CreateFakeSections()
{
/* If we have no section headers let's build some fake sections */
if(m_iSHCount == 0)
{
if(m_iPHCount < 3)
{
COutput::Printf(LEVEL_ERROR, "Invalid number of program headers for newstyle PRX (%d)\n",
m_iPHCount);
return false;
}
if (m_pElfPrograms[2].iType == PT_PRXRELOC) {
m_iSHCount = 6;
} else {
m_iSHCount = 5;
}
SAFE_ALLOC(m_pElfSections, ElfSection[m_iSHCount]);
if(m_pElfSections == NULL)
{
return false;
}
memset(m_pElfSections, 0, sizeof(ElfSection) * m_iSHCount);
m_pElfSections[1].iType = SHT_PROGBITS;
m_pElfSections[1].iFlags = SHF_ALLOC | SHF_EXECINSTR;
m_pElfSections[1].iAddr = m_pElfPrograms[0].iVaddr;
m_pElfSections[1].pData = m_pElf + m_pElfPrograms[0].iOffset;
m_pElfSections[1].iSize = m_stubBottom;
strcpy(m_pElfSections[1].szName, ".text");
m_pElfSections[2].iType = SHT_PROGBITS;
m_pElfSections[2].iFlags = SHF_ALLOC;
m_pElfSections[2].iAddr = m_stubBottom;
m_pElfSections[2].pData = m_pElf + m_pElfPrograms[0].iOffset + m_stubBottom;
m_pElfSections[2].iSize = m_pElfPrograms[0].iMemsz - m_stubBottom;
strcpy(m_pElfSections[2].szName, ".rodata");
m_pElfSections[3].iType = SHT_PROGBITS;
m_pElfSections[3].iFlags = SHF_ALLOC | SHF_WRITE;
m_pElfSections[3].iAddr = m_pElfPrograms[1].iVaddr;
m_pElfSections[3].pData = m_pElf + m_pElfPrograms[1].iOffset;
m_pElfSections[3].iSize = m_pElfPrograms[1].iFilesz;
strcpy(m_pElfSections[3].szName, ".data");
m_pElfSections[4].iType = SHT_NOBITS;
m_pElfSections[4].iFlags = SHF_ALLOC | SHF_WRITE;
m_pElfSections[4].iAddr = m_pElfPrograms[1].iVaddr + m_pElfPrograms[1].iFilesz;
m_pElfSections[4].pData = m_pElf + m_pElfPrograms[1].iOffset + m_pElfPrograms[1].iFilesz;
m_pElfSections[4].iSize = m_pElfPrograms[1].iMemsz - m_pElfPrograms[1].iFilesz;
strcpy(m_pElfSections[4].szName, ".bss");
if (m_pElfPrograms[2].iType == PT_PRXRELOC) {
m_pElfSections[5].iType = SHT_PRXRELOC;
m_pElfSections[5].iFlags = 0;
m_pElfSections[5].iAddr = 0;
m_pElfSections[5].pData = m_pElf + m_pElfPrograms[2].iOffset;
m_pElfSections[5].iSize = m_pElfPrograms[2].iFilesz;
/* Bind to section 1, not that is matters */
m_pElfSections[5].iInfo = 1;
strcpy(m_pElfSections[5].szName, ".reloc");
}
if(COutput::GetDebug())
{
ElfDumpSections();
}
}
return true;
}
int CProcessPrx::CountRelocs()
{
int iLoop;
int iRelocCount = 0;
for(iLoop = 0; iLoop < m_iSHCount; iLoop++)
{
if((m_pElfSections[iLoop].iType == SHT_PRXRELOC) || (m_pElfSections[iLoop].iType == SHT_REL))
{
if(m_pElfSections[iLoop].iSize % sizeof(Elf32_Rel))
{
COutput::Printf(LEVEL_DEBUG, "Relocation section invalid\n");
}
iRelocCount += m_pElfSections[iLoop].iSize / sizeof(Elf32_Rel);
}
}
for(iLoop = 0; iLoop < m_iPHCount; iLoop++)
{
if(m_pElfPrograms[iLoop].iType == PT_PRXRELOC2) {
u8 *block1, block1s, part1s;
u8 *block2, block2s, part2s;
u8 *pos, *end;
if (m_pElfPrograms[iLoop].pData[0] != 0 ||
m_pElfPrograms[iLoop].pData[1] != 0) {
COutput::Printf(LEVEL_DEBUG, "Should start with 0x00 0x00\n");
return 0;
}
part1s = m_pElfPrograms[iLoop].pData[2];
part2s = m_pElfPrograms[iLoop].pData[3];
block1s = m_pElfPrograms[iLoop].pData[4];
block1 = &m_pElfPrograms[iLoop].pData[4];
block2 = block1 + block1s;
block2s = block2[0];
pos = block2 + block2s;
end = &m_pElfPrograms[iLoop].pData[m_pElfPrograms[iLoop].iFilesz];
while (pos < end) {
u32 cmd, part1, temp;
cmd = pos[0] | (pos[1] << 16);
pos += 2;
temp = (cmd << (16 - part1s)) & 0xFFFF;
temp = (temp >> (16 - part1s)) & 0xFFFF;
if (temp >= block1s) {
COutput::Printf(LEVEL_DEBUG, "Invalid cmd1 index\n");
return 0;
}
part1 = block1[temp];
if ( (part1 & 0x01) == 0 ) {
if ( ( part1 & 0x06 ) == 4 ) {
pos += 4;
}
}
else {
switch (part1 & 0x06) {
case 2:
pos += 2;
break;
case 4:
pos += 4;
break;
}
switch (part1 & 0x38) {
case 0x10:
pos += 2;
break;
case 0x18:
pos += 4;
break;
}
}
iRelocCount++;
}
}
}
COutput::Printf(LEVEL_DEBUG, "Relocation entries %d\n", iRelocCount);
return iRelocCount;
}
int CProcessPrx::LoadRelocsTypeA(struct ElfReloc *pRelocs)
{
int i, count;
const Elf32_Rel *reloc;
int iLoop, iCurrRel = 0;
for(iLoop = 0; iLoop < m_iSHCount; iLoop++)
{
if((m_pElfSections[iLoop].iType == SHT_PRXRELOC) || (m_pElfSections[iLoop].iType == SHT_REL))
{
count = m_pElfSections[iLoop].iSize / sizeof(Elf32_Rel);
reloc = (const Elf32_Rel *) m_pElfSections[iLoop].pData;
for(i = 0; i < count; i++) {
pRelocs[iCurrRel].secname = m_pElfSections[iLoop].szName;
pRelocs[iCurrRel].base = 0;
pRelocs[iCurrRel].type = ELF32_R_TYPE(LW(reloc->r_info));
pRelocs[iCurrRel].symbol = ELF32_R_SYM(LW(reloc->r_info));
pRelocs[iCurrRel].info = LW(reloc->r_info);
pRelocs[iCurrRel].offset = reloc->r_offset;
reloc++;
iCurrRel++;
}
}
}
return iCurrRel;
}
int CProcessPrx::LoadRelocsTypeB(struct ElfReloc *pRelocs)
{
u8 *block1, *block2, *pos, *end;
u32 block1s, block2s, part1s, part2s;
u32 cmd, part1, part2, lastpart2;
u32 addend = 0, offset = 0;
u32 ofsbase = 0xFFFFFFFF, addrbase;
u32 temp1, temp2;
u32 nbits;
int iLoop, iCurrRel = 0;
for(iLoop = 0; iLoop < m_iPHCount; iLoop++)
{
if(m_pElfPrograms[iLoop].iType == PT_PRXRELOC2)
{
part1s = m_pElfPrograms[iLoop].pData[2];
part2s = m_pElfPrograms[iLoop].pData[3];
block1s =m_pElfPrograms[iLoop].pData[4];
block1 = &m_pElfPrograms[iLoop].pData[4];
block2 = block1 + block1s;
block2s = block2[0];
pos = block2 + block2s;
end = &m_pElfPrograms[iLoop].pData[m_pElfPrograms[iLoop].iFilesz];
for (nbits = 1; (1 << nbits) < iLoop; nbits++) {
if (nbits >= 33) {
COutput::Printf(LEVEL_DEBUG, "Invalid nbits\n");
return 0;
}
}
lastpart2 = block2s;
while (pos < end) {
cmd = pos[0] | (pos[1] << 8);
pos += 2;
temp1 = (cmd << (16 - part1s)) & 0xFFFF;
temp1 = (temp1 >> (16 - part1s)) & 0xFFFF;
if (temp1 >= block1s) {
COutput::Printf(LEVEL_DEBUG, "Invalid part1 index\n");
return 0;
}
part1 = block1[temp1];
if ((part1 & 0x01) == 0) {
ofsbase = (cmd << (16 - part1s - nbits)) & 0xFFFF;
ofsbase = (ofsbase >> (16 - nbits)) & 0xFFFF;
if (!(ofsbase < iLoop)) {
COutput::Printf(LEVEL_DEBUG, "Invalid offset base\n");
return 0;
}
if ((part1 & 0x06) == 0) {
offset = cmd >> (part1s + nbits);
} else if ((part1 & 0x06) == 4) {
offset = pos[0] | (pos[1] << 8) | (pos[2] << 16) | (pos[3] << 24);
pos += 4;
} else {
COutput::Printf(LEVEL_DEBUG, "Invalid size\n");
return 0;
}
} else {
temp2 = (cmd << (16 - (part1s + nbits + part2s))) & 0xFFFF;
temp2 = (temp2 >> (16 - part2s)) & 0xFFFF;
if (temp2 >= block2s) {
COutput::Printf(LEVEL_DEBUG, "Invalid part2 index\n");
return 0;
}
addrbase = (cmd << (16 - part1s - nbits)) & 0xFFFF;
addrbase = (addrbase >> (16 - nbits)) & 0xFFFF;
if (!(addrbase < iLoop)) {
COutput::Printf(LEVEL_DEBUG, "Invalid address base\n");
return 0;
}
part2 = block2[temp2];
switch (part1 & 0x06) {
case 0:
temp1 = cmd;
if (temp1 & 0x8000) {
temp1 |= ~0xFFFF;
temp1 >>= part1s + part2s + nbits;
temp1 |= ~0xFFFF;
} else {
temp1 >>= part1s + part2s + nbits;
}
offset += temp1;
break;
case 2:
temp1 = cmd;
if (temp1 & 0x8000) temp1 |= ~0xFFFF;
temp1 = (temp1 >> (part1s + part2s + nbits)) << 16;
temp1 |= pos[0] | (pos[1] << 8);
offset += temp1;
pos += 2;
break;
case 4:
offset = pos[0] | (pos[1] << 8) | (pos[2] << 16) | (pos[3] << 24);
pos += 4;
break;
default:
COutput::Printf(LEVEL_DEBUG, "invalid part1 size\n");
return 0;
}
if (!(offset < m_pElfPrograms[ofsbase].iFilesz)) {
COutput::Printf(LEVEL_DEBUG, "invalid relocation offset\n");
return 0;
}
switch (part1 & 0x38) {
case 0x00:
addend = 0;
break;
case 0x08:
if ((lastpart2 ^ 0x04) != 0) {
addend = 0;
}
break;
case 0x10:
addend = pos[0] | (pos[1] << 8);
pos += 2;
break;
case 0x18:
addend = pos[0] | (pos[1] << 8) | (pos[2] << 16) | (pos[3] << 24);
pos += 4;
COutput::Printf(LEVEL_DEBUG, "invalid addendum size\n");
return 0;
default:
COutput::Printf(LEVEL_DEBUG, "invalid addendum size\n");
return 0;
}
lastpart2 = part2;
pRelocs[iCurrRel].secname = NULL;
pRelocs[iCurrRel].base = 0;
pRelocs[iCurrRel].symbol = ofsbase | (addrbase << 8);
pRelocs[iCurrRel].info = (ofsbase << 8) | (addrbase << 8);
pRelocs[iCurrRel].offset = offset;
switch (part2) {
case 2:
pRelocs[iCurrRel].type = R_MIPS_32;
break;
case 0:
continue;
case 3:
pRelocs[iCurrRel].type = R_MIPS_26;
break;
case 6:
pRelocs[iCurrRel].type = R_MIPS_X_J26;
break;
case 7:
pRelocs[iCurrRel].type = R_MIPS_X_JAL26;
break;
case 4:
pRelocs[iCurrRel].type = R_MIPS_X_HI16;
pRelocs[iCurrRel].base = (s16) addend;
break;
case 1:
case 5:
pRelocs[iCurrRel].type = R_MIPS_LO16;
break;
default:
COutput::Printf(LEVEL_DEBUG, "invalid relocation type\n");
return 0;
}
temp1 = (cmd << (16 - part1s)) & 0xFFFF;
temp1 = (temp1 >> (16 - part1s)) & 0xFFFF;
temp2 = (cmd << (16 - (part1s + nbits + part2s))) & 0xFFFF;
temp2 = (temp2 >> (16 - part2s)) & 0xFFFF;
COutput::Printf(LEVEL_DEBUG, "CMD=0x%04X I1=0x%02X I2=0x%02X PART1=0x%02X PART2=0x%02X\n", cmd, temp1, temp2, part1, part2);
pRelocs[iCurrRel].info |= pRelocs[iCurrRel].type;
iCurrRel++;
}
}
}
}
return iCurrRel;
}
bool CProcessPrx::LoadRelocs()
{
bool blRet = false;
int iRelocCount = 0;
int iCurrRel = 0;
int count;
int iLoop;
iRelocCount = this->CountRelocs();
if(iRelocCount > 0)
{
SAFE_ALLOC(m_pElfRelocs, ElfReloc[iRelocCount]);
if(m_pElfRelocs != NULL)
{
memset(m_pElfRelocs, 0, sizeof(ElfReloc) * iRelocCount);
COutput::Printf(LEVEL_DEBUG, "Loading Type A relocs\n");
count = this->LoadRelocsTypeA (&m_pElfRelocs[iCurrRel]);
if (count) {
iCurrRel += count;
} else {
}
COutput::Printf(LEVEL_DEBUG, "Loading Type B relocs\n");
count = this->LoadRelocsTypeB (&m_pElfRelocs[iCurrRel]);
if (count) {
iCurrRel += count;
} else {
}
m_iRelocCount = iCurrRel;
if(COutput::GetDebug())
{
COutput::Printf(LEVEL_DEBUG, "Dumping relocs %d\n", m_iRelocCount);
for(iLoop = 0; iLoop < m_iRelocCount; iLoop++)
{
if(m_pElfRelocs[iLoop].type < 16)
{
COutput::Printf(LEVEL_DEBUG, "Reloc %s:%d Type:%s Symbol:%d Offset %08X Info:%08X\n",
m_pElfRelocs[iLoop].secname, iLoop, g_szRelTypes[m_pElfRelocs[iLoop].type],
m_pElfRelocs[iLoop].symbol, m_pElfRelocs[iLoop].offset, m_pElfRelocs[iLoop].info);
}
else
{
COutput::Printf(LEVEL_DEBUG, "Reloc %s:%d Type:%d Symbol:%d Offset %08X\n",
m_pElfRelocs[iLoop].secname, iLoop, m_pElfRelocs[iLoop].type,
m_pElfRelocs[iLoop].symbol, m_pElfRelocs[iLoop].offset);
}
}
}
}
}
blRet = true;
return blRet;
}
bool CProcessPrx::LoadFromFile(const char *szFilename)
{
bool blRet = false;
if(CProcessElf::LoadFromFile(szFilename))
{
/* Do PRX specific stuff */
ElfSection *pInfoSect;
u8 *pData = NULL;
u32 iAddr = 0;
FreeMemory();
m_blPrxLoaded = false;
m_vMem = CVirtualMem(m_pElfBin, m_iBinSize, m_iBaseAddr, MEM_LITTLE_ENDIAN);
pInfoSect = ElfFindSection(PSP_MODULE_INFO_NAME);
if(pInfoSect == NULL)
{
/* Get from program headers */
if(m_iPHCount > 0)
{
iAddr = (m_pElfPrograms[0].iPaddr & 0x7FFFFFFF) - m_pElfPrograms[0].iOffset;
pData = m_pElfBin + iAddr;
}
}
else
{
pData = pInfoSect->pData;
iAddr = pInfoSect->iAddr;
}
if(pData != NULL)
{
if((FillModule(pData, iAddr)) && (LoadRelocs()))
{
m_blPrxLoaded = true;
if(m_pElfRelocs)
{
FixupRelocs(m_dwBase, m_imms);
}
if ((LoadExports()) && (LoadImports()) && (CreateFakeSections()))
{
COutput::Printf(LEVEL_INFO, "Loaded PRX %s successfully\n", szFilename);
BuildMaps();
blRet = true;
}
}
}
else
{
COutput::Printf(LEVEL_ERROR, "Could not find module section\n");
}
}
return blRet;
}
bool CProcessPrx::LoadFromBinFile(const char *szFilename, unsigned int dwDataBase)
{