forked from red-prig/fpPS4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ps4_program.pas
1348 lines (1195 loc) · 25.3 KB
/
ps4_program.pas
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
unit ps4_program;
{$mode objfpc}{$H+}
interface
uses
Windows,
Classes,
SysUtils,
RWLock,
hamt,
sys_types,
sys_kernel,
ps4_handles;
type
PMODULE=^TMODULE;
TMODULE=packed record
attr:DWORD;
Import:Boolean;
strName:string[80];
end;
PLIBRARY=^TLIBRARY;
T_set_proc_cb=function(lib:PLIBRARY;nid:QWORD;value:Pointer):Boolean;
T_get_proc_cb=function(lib:PLIBRARY;nid:QWORD):Pointer;
TElf_node=class;
TLIBRARY=packed object
parent:TElf_node;
MapSymbol:THAMT;
attr:DWORD;
Import:Boolean;
strName:string[80];
Fset_proc_cb:T_set_proc_cb;
Fget_proc_cb:T_get_proc_cb;
function _set_proc(nid:QWORD;value:Pointer):Boolean;
function _get_proc(nid:QWORD):Pointer;
function set_proc(nid:QWORD;value:Pointer):Boolean;
function get_proc(nid:QWORD):Pointer;
end;
TElf_node=class(TClassHandle)
private
pPrev,pNext:TElf_node;
protected
FHandle:Integer;
FStatic:Boolean;
FPrepared:Boolean;
FLoadImport:Boolean;
FInitProt:Boolean;
FInitThread:Boolean;
FInitCode:Boolean;
aNeed:array of RawByteString;
aMods:array of TMODULE;
aLibs:array of PLIBRARY;
procedure _set_filename(const name:RawByteString);
procedure _add_need(const name:RawByteString);
procedure _set_mod(id:Word;_md:TMODULE);
procedure _set_mod_attr(u:TModuleValue);
function _get_mod(id:Word):PMODULE;
procedure _set_lib(id:Word;lib:TLIBRARY);
procedure _set_lib_attr(u:TLibraryValue);
function _get_lib(id:Word):PLIBRARY;
function _find_mod_export:Word;
function _find_lib_export:Word;
public
pFileName:RawByteString;
property IsStatic:Boolean read FStatic write FStatic;
property IsInit:Boolean read FInitCode write FInitCode;
property Handle:Integer read FHandle;
property Next:TElf_node read pNext;
function _add_lib(const strName:RawByteString):PLIBRARY;
function ModuleNameFromId(id:WORD):RawByteString;
function LibraryNameFromId(id:WORD):RawByteString;
Constructor Create;
destructor Destroy; override;
Procedure Clean; virtual;
function Prepare:Boolean; virtual;
Procedure LoadSymbolImport(cbs,data:Pointer); virtual;
Procedure ReLoadSymbolImport(cbs,data:Pointer); virtual;
Procedure InitThread(is_static:QWORD); virtual;
Procedure FreeThread; virtual;
Procedure InitProt; virtual;
Procedure InitCode; virtual;
function module_start(argc:size_t;argp,param:PPointer):Integer; virtual;
function GetCodeFrame:TMemChunk; virtual;
function GetEntry:Pointer; virtual;
function GetdInit:Pointer; virtual;
function GetdFini:Pointer; virtual;
Function GetModuleInfo:SceKernelModuleInfo; virtual;
Function GetModuleInfoEx:SceKernelModuleInfoEx; virtual;
Function get_proc(nid:QWORD):Pointer;
Function get_proc_by_name(const name:RawByteString):Pointer;
end;
TOnElfLoadCb=function(Const name:RawByteString):TElf_node;
Phamt64locked=^Thamt64locked;
Thamt64locked=object
lock:TRWLock;
hamt:TSTUB_HAMT64;
Procedure Init;
Procedure LockRd;
Procedure LockWr;
Procedure Unlock;
end;
Thamt64locked_proc=object(Thamt64locked)
function _set_proc(nid:QWORD;value:Pointer):Boolean;
function _get_proc(nid:QWORD):Pointer;
end;
TElfNodeList=object(Thamt64locked)
pHead,pTail:TElf_node;
procedure Push_head(Node:TElf_node);
procedure Push_tail(Node:TElf_node);
function Pop_head:TElf_node;
function Pop_tail:TElf_node;
procedure InsertAfter(node,new:TElf_node);
procedure InsertBefore(node,new:TElf_node);
procedure Remove(node:TElf_node);
end;
Tps4_program=object
public
resolve_cb:Pointer;
reload_cb:Pointer;
prog:TElf_node;
app0_file:RawByteString;
app0_path:RawByteString;
app1_path:RawByteString;
save_path:RawByteString;
private
pre_load:Thamt64locked_proc;
fin_load:Thamt64locked_proc;
files:TElfNodeList;
mods:Thamt64locked;
libs:Thamt64locked;
elfs:TIntegerHandles;
function RegistredFile(node:TElf_node):Boolean;
Procedure RegistredMod(node:TElf_node;const strName:RawByteString);
public
Procedure LockRd;
Procedure LockWr;
Procedure Unlock;
function FirstFile:TElf_node;
function AcqureFileByName(const strName:RawByteString):TElf_node;
procedure PopupFile(node:TElf_node);
Procedure SetLib(lib:PLIBRARY);
function GetLib(const strName:RawByteString):PLIBRARY;
function RegistredElf(node:TElf_node):Boolean;
Procedure RegistredPreLoad(const strName:RawByteString;cb:TOnElfLoadCb);
Procedure RegistredFinLoad(const strName:RawByteString;cb:TOnElfLoadCb);
function Loader(Const name:RawByteString):TElf_node;
Procedure ResolveDepended(node:TElf_node);
Procedure LoadSymbolImport(data:Pointer);
Procedure ReLoadSymbolImport(data:Pointer);
Procedure InitProt;
Procedure InitCode;
Procedure InitThread(is_static:QWORD);
Procedure FreeThread;
function AcqureFileByCodeAdr(Adr:Pointer):TElf_node;
function AcqureFileByHandle(handle:Integer):TElf_node;
end;
var
ps4_app:Tps4_program;
Procedure DumpExceptionCallStack(E: Exception);
function GetSceProcParam:Pointer;
function GetSceUserMainThreadName:PChar;
function GetSceUserMainThreadPriority:PDWORD;
function GetSceUserMainThreadStackSize:PDWORD;
function GetSceLibcParam:Pointer;
function GetSceKernelMemParam:Pointer;
function GetSceKernelFlexibleMemorySize:PQWORD;
Function get_dev_progname:RawByteString;
implementation
uses
ps4_elf,ps4_elf_tls;
Procedure DumpExceptionCallStack(E: Exception);
var
I: Integer;
Frames: PPointer;
Report: string;
begin
Report := '';
if E <> nil then
begin
Report := Report + 'Exception class: ' + E.ClassName + LineEnding +
'Message: ' + E.Message + LineEnding;
end;
Report := Report + BackTraceStrFunc(ExceptAddr);
Frames := ExceptFrames;
for I := 0 to ExceptFrameCount - 1 do
Report := Report + LineEnding + BackTraceStrFunc(Frames[I]);
Writeln(StdErr,Report);
end;
//--
procedure TElfNodeList.Push_head(Node:TElf_node);
begin
if (pHead=nil) then
begin
pTail:=node;
node.pNext:=nil;
end else
begin
pHead.pPrev:=node;
node.pNext:=pHead;
end;
node.pPrev:=nil;
pHead:=node;
end;
procedure TElfNodeList.Push_tail(Node:TElf_node);
begin
if (pTail=nil) then
begin
pHead:=node;
node.pPrev:=nil;
end else
begin
pTail.pNext:=node;
node.pPrev:=pTail;
end;
node.pNext:=nil;
pTail:=node;
end;
function TElfNodeList.Pop_head:TElf_node;
begin
if (pHead=nil) then
begin
Result:=nil;
end else
begin
Result:=pHead;
pHead:=pHead.pNext;
if (pHead=nil) then
begin
pTail:=nil;
end else
begin
pHead.pPrev:=nil;
end;
Result.pPrev:=nil;
Result.pNext:=nil;
end;
end;
function TElfNodeList.Pop_tail:TElf_node;
begin
if (pTail=nil) then
begin
Result:=nil;
end else
begin
Result:=pTail;
pTail:=pTail.pPrev;
if (pTail=nil) then
begin
pHead:=nil;
end else
begin
pTail.pNext:=nil;
end;
Result.pPrev:=nil;
Result.pNext:=nil;
end;
end;
procedure TElfNodeList.InsertAfter(node,new:TElf_node);
begin
new.pPrev:=node;
if (node.pNext=nil) then
begin
new.pNext:=nil;
pTail:=new;
end else
begin
new.pNext:=node.pNext;
node.pNext.pPrev:=new;
end;
node.pNext:=new;
end;
procedure TElfNodeList.InsertBefore(node,new:TElf_node);
begin
new.pNext:=node;
if (node.pPrev=nil) then
begin
new.pPrev:=nil;
pHead:=new;
end else
begin
new.pPrev:=node.pPrev;
node.pPrev.pNext:=new;
end;
node.pPrev:=new;
end;
procedure TElfNodeList.Remove(node:TElf_node);
begin
if (node.pPrev=nil) then
begin
if (pHead=node) then
begin
pHead:=node.pNext;
end;
end else
begin
node.pPrev.pNext:=node.pNext;
end;
if (node.pNext=nil) then
begin
if (pTail=node) then
begin
pTail:=node.pPrev;
end;
end else
begin
node.pNext.pPrev:=node.pPrev;
end;
end;
//
procedure TElf_node._set_filename(const name:RawByteString);
var
i,L:SizeInt;
begin
i:=Length(name);
While (i>1) do
begin
if (name[i] in AllowDirectorySeparators) then
begin
Inc(i);
L:=Length(name)-i+1;
pFileName:=Copy(name,i,L);
Exit;
end;
Dec(i);
end;
pFileName:=name;
end;
procedure TElf_node._add_need(const name:RawByteString);
var
i:SizeInt;
begin
i:=Length(aNeed);
SetLength(aNeed,i+1);
aNeed[i]:=name;
end;
procedure TElf_node._set_mod(id:Word;_md:TMODULE);
var
i:SizeInt;
begin
i:=Length(aMods);
if (i<=id) then
begin
i:=id+1;
SetLength(aMods,i);
end;
aMods[id]:=_md;
end;
procedure TElf_node._set_mod_attr(u:TModuleValue);
var
i:SizeInt;
begin
i:=Length(aMods);
if (i<=u.id) then
begin
i:=u.id+1;
SetLength(aMods,i);
end;
aMods[u.id].attr:=u.name_offset;
end;
function TElf_node._get_mod(id:Word):PMODULE;
begin
Result:=nil;
if (Length(aMods)>id) then
begin
Result:=@aMods[id];
end;
end;
function TElf_node._find_mod_export:Word;
var
i:Word;
begin
Result:=0;
if Length(aMods)>0 then
For i:=0 to High(aMods) do
if not aMods[i].Import then
begin
Exit(i);
end;
end;
procedure TElf_node._set_lib(id:Word;lib:TLIBRARY);
var
i:SizeInt;
plib:PLIBRARY;
begin
i:=Length(aLibs);
if (i<=id) then
begin
i:=id+1;
SetLength(aLibs,i);
end;
plib:=aLibs[id];
if (plib=nil) then plib:=AllocMem(SizeOf(TLIBRARY));
plib^:=lib;
plib^.parent:=Self;
aLibs[id]:=plib;
end;
procedure TElf_node._set_lib_attr(u:TLibraryValue);
var
i:SizeInt;
plib:PLIBRARY;
begin
i:=Length(aLibs);
if (i<=u.id) then
begin
i:=u.id+1;
SetLength(aLibs,i);
end;
plib:=aLibs[u.id];
if (plib=nil) then plib:=AllocMem(SizeOf(TLIBRARY));
plib^.attr:=u.name_offset;
plib^.parent:=Self;
aLibs[u.id]:=plib;
end;
function TElf_node._get_lib(id:Word):PLIBRARY;
begin
Result:=nil;
if (Length(aLibs)>id) then
begin
Result:=aLibs[id];
end;
end;
function TElf_node._add_lib(const strName:RawByteString):PLIBRARY;
var
i:SizeInt;
begin
i:=Length(aLibs);
SetLength(aLibs,i+1);
Result:=AllocMem(SizeOf(TLIBRARY));
Result^.parent:=Self;
Result^.strName:=strName;
aLibs[i]:=Result;
end;
function TElf_node._find_lib_export:Word;
var
i:Word;
begin
Result:=0;
if Length(aLibs)>0 then
For i:=0 to High(aLibs) do
if (aLibs[i]<>nil) then
if not aLibs[i]^.Import then
begin
Exit(i);
end;
end;
function TElf_node.ModuleNameFromId(id:WORD):RawByteString;
var
_md:PMODULE;
begin
Result:='';
if (Self=nil) then Exit;
_md:=_get_mod(id);
if (_md<>nil) then Result:=_md^.strName;
end;
function TElf_node.LibraryNameFromId(id:WORD):RawByteString;
var
lib:PLIBRARY;
begin
Result:='';
if (Self=nil) then Exit;
lib:=_get_lib(id);
if (lib<>nil) then Result:=lib^.strName;
end;
procedure _free_map_cb(data,userdata:Pointer);
begin
FreeMem(data);
end;
Procedure TElf_node.Clean;
var
i:SizeInt;
lib:PLIBRARY;
begin
if (Self=nil) then Exit;
if Length(aLibs)<>0 then
begin
For i:=0 to Length(aLibs)-1 do
begin
lib:=aLibs[i];
if (lib<>nil) then
begin
HAMT_destroy64(lib^.MapSymbol,@_free_map_cb,nil);
lib^.strName:='';
FreeMem(lib);
end;
end;
end;
pFileName:='';
SetLength(aNeed,0);
SetLength(aMods,0);
SetLength(aLibs,0);
end;
Constructor TElf_node.Create;
begin
FStatic:=True;
end;
destructor TElf_node.Destroy;
begin
Clean;
inherited;
end;
function TElf_node.Prepare:Boolean;
begin
Result:=True;
FPrepared:=True;;
end;
Procedure TElf_node.LoadSymbolImport(cbs,data:Pointer);
begin
FLoadImport:=True;
end;
Procedure TElf_node.ReLoadSymbolImport(cbs,data:Pointer);
begin
end;
Procedure TElf_node.InitThread(is_static:QWORD);
begin
end;
Procedure TElf_node.FreeThread;
begin
end;
Procedure TElf_node.InitProt;
begin
FInitProt:=True;
end;
Procedure TElf_node.InitCode;
begin
FInitCode:=True;
end;
function TElf_node.module_start(argc:size_t;argp,param:PPointer):Integer;
begin
Result:=0;
end;
function TElf_node.GetCodeFrame:TMemChunk;
begin
Result:=Default(TMemChunk);
end;
function TElf_node.GetEntry:Pointer;
begin
Result:=nil;
end;
function TElf_node.GetdInit:Pointer;
begin
Result:=nil;
end;
function TElf_node.GetdFini:Pointer;
begin
Result:=nil;
end;
Function TElf_node.GetModuleInfo:SceKernelModuleInfo;
begin
Result:=Default(SceKernelModuleInfo);
Result.size:=SizeOf(SceKernelModuleInfo);
MoveChar0(PChar(pFileName)^,Result.name,SCE_DBG_MAX_NAME_LENGTH);
end;
Function TElf_node.GetModuleInfoEx:SceKernelModuleInfoEx;
begin
Result:=Default(SceKernelModuleInfoEx);
Result.st_size:=SizeOf(SceKernelModuleInfoEx);
MoveChar0(PChar(pFileName)^,Result.name,SCE_DBG_MAX_NAME_LENGTH);
Result.id:=FHandle;
Result.ref_count:=1;
end;
Function TElf_node.get_proc(nid:QWORD):Pointer;
var
i:Integer;
begin
Result:=nil;
if Length(aLibs)<>0 then
begin
For i:=0 to Length(aLibs)-1 do
if (aLibs[i]<>nil) then
if (not aLibs[i]^.Import) then
begin
Result:=aLibs[i]^.get_proc(nid);
if (Result<>nil) then Exit;
end;
end;
end;
Function TElf_node.get_proc_by_name(const name:RawByteString):Pointer;
begin
Result:=get_proc(ps4_nid_hash(name));
end;
function TLIBRARY._set_proc(nid:QWORD;value:Pointer):Boolean;
var
data:PPointer;
PP:PPointer;
begin
if (@Self=nil) then Exit(False);
if (MapSymbol=nil) then MapSymbol:=HAMT_create64;
data:=nil;
PP:=HAMT_search64(MapSymbol,nid);
if (PP<>nil) then data:=PP^;
if (data=nil) then
begin
data:=GetMem(SizeOf(Pointer)*2);
data[0]:=value;
data[1]:=Pointer(nid);
PP:=HAMT_insert64(MapSymbol,nid,data);
Assert(PP<>nil);
Result:=(PP^=data);
if not Result then
begin
FreeMem(data);
end;
end else
begin
data[0]:=value;
end;
end;
function TLIBRARY._get_proc(nid:QWORD):Pointer;
var
data:PPointer;
PP:PPointer;
begin
Result:=nil;
if (@Self=nil) then Exit;
data:=nil;
PP:=HAMT_search64(MapSymbol,nid);
if (PP<>nil) then data:=PP^;
if (data<>nil) then Result:=data^;
end;
function TLIBRARY.set_proc(nid:QWORD;value:Pointer):Boolean;
begin
if (@Self=nil) then Exit(False);
if (Fset_proc_cb<>nil) then
Result:=Fset_proc_cb(@self,nid,value)
else
Result:=_set_proc(nid,value);
end;
function TLIBRARY.get_proc(nid:QWORD):Pointer;
begin
if (@Self=nil) then Exit(nil);
if (Fget_proc_cb<>nil) then
Result:=Fget_proc_cb(@self,nid)
else
Result:=_get_proc(nid);
end;
function Tps4_program.RegistredFile(node:TElf_node):Boolean;
var
nid:QWORD;
PP:PPointer;
begin
Result:=True;
files.LockWr;
files.Remove(node);
files.Push_tail(node);
nid:=ps4_nid_hash(node.pFileName);
PP:=HAMT_insert64(@files.hamt,nid,Pointer(node));
Assert(PP<>nil);
if (PP^<>Pointer(node)) then
begin
Writeln(StdErr,'Warn, ',node.pFileName,' file is registred');
Result:=False;
end;
files.Unlock;
end;
Procedure Tps4_program.LockRd;
begin
files.LockRd;
end;
Procedure Tps4_program.LockWr;
begin
files.LockWr;
end;
Procedure Tps4_program.Unlock;
begin
files.Unlock;
end;
function Tps4_program.FirstFile:TElf_node;
begin
Result:=files.pHead;
end;
function Tps4_program.AcqureFileByName(const strName:RawByteString):TElf_node;
var
nid:QWORD;
PP:PPointer;
begin
Result:=nil;
nid:=ps4_nid_hash(strName);
files.LockRd;
PP:=HAMT_search64(@files.hamt,nid);
if (PP<>nil) then Result:=TElf_node(PP^);
if (Result<>nil) then Result.Acqure;
files.Unlock;
end;
procedure Tps4_program.PopupFile(node:TElf_node);
begin
if (node=nil) then Exit;
files.LockRd;
files.Remove(node);
files.Push_head(node);
files.Unlock;
end;
Procedure Tps4_program.RegistredMod(node:TElf_node;const strName:RawByteString);
var
nid:QWORD;
PP:PPointer;
begin
nid:=ps4_nid_hash(strName);
PP:=HAMT_insert64(@mods.hamt,nid,Pointer(node));
Assert(PP<>nil);
if (PP^<>Pointer(node)) then Writeln(StdErr,'Warn, ',strName,' module is registred');
end;
Procedure Tps4_program.SetLib(lib:PLIBRARY);
var
nid:QWORD;
PP:PPointer;
begin
if (lib=nil) then Exit;
if lib^.Import then Exit;
//Writeln('Regs, ',lib^.strName);
nid:=ps4_nid_hash(lib^.strName);
PP:=HAMT_insert64(@libs.hamt,nid,Pointer(lib));
Assert(PP<>nil);
if (PP^<>Pointer(lib)) then Writeln(StdErr,'Warn, ',lib^.strName,' lib is registred');
end;
function Tps4_program.GetLib(const strName:RawByteString):PLIBRARY;
var
nid:QWORD;
PP:PPointer;
begin
Result:=nil;
nid:=ps4_nid_hash(strName);
libs.LockRd;
PP:=HAMT_search64(@libs.hamt,nid);
if (PP<>nil) then Result:=PP^;
libs.Unlock;
end;
function Tps4_program.RegistredElf(node:TElf_node):Boolean;
var
FHandle:Integer;
i:SizeInt;
begin
if (node=nil) then Exit(False);
FHandle:=0;
if not elfs.New(node,FHandle) then
raise Exception.Create('Error alloc handle');
node.FHandle:=FHandle;
Result:=RegistredFile(node);
if not Result then Exit;
if Length(node.aMods)<>0 then
begin
mods.LockWr;
For i:=0 to Length(node.aMods)-1 do
with node.aMods[i] do
if not Import then
RegistredMod(node,strName);
mods.Unlock;
end;
if Length(node.aLibs)<>0 then
begin
libs.LockWr;
For i:=0 to Length(node.aLibs)-1 do
SetLib(node.aLibs[i]);
libs.Unlock;
end;
node.Release;
end;
function Thamt64locked_proc._set_proc(nid:QWORD;value:Pointer):Boolean;
var
data:PPointer;
PP:PPointer;
begin
data:=GetMem(SizeOf(Pointer));
data^:=value;
PP:=HAMT_insert64(@hamt,nid,data);
Assert(PP<>nil);
Result:=(PP^=data);
if not Result then
begin
FreeMem(data);
end;
end;
function Thamt64locked_proc._get_proc(nid:QWORD):Pointer;
var
data:PPointer;
PP:PPointer;
begin
Result:=nil;
data:=nil;
PP:=HAMT_search64(@hamt,nid);
if (PP<>nil) then data:=PP^;
if (data<>nil) then Result:=data^;
end;
Procedure Tps4_program.RegistredPreLoad(const strName:RawByteString;cb:TOnElfLoadCb);
var
nid:QWORD;
begin
if (cb=nil) then Exit;
nid:=ps4_nid_hash(strName);
pre_load.LockWr;
if not pre_load._set_proc(nid,Pointer(cb)) then
begin
Writeln(StdErr,'Warn, ',strName,' is registred')
end;
pre_load.Unlock;
end;
Procedure Tps4_program.RegistredFinLoad(const strName:RawByteString;cb:TOnElfLoadCb);
var
nid:QWORD;
begin
if (cb=nil) then Exit;
nid:=ps4_nid_hash(strName);
fin_load.LockWr;
if not fin_load._set_proc(nid,Pointer(cb)) then
begin
Writeln(StdErr,'Warn, ',strName,' is registred')
end;
fin_load.Unlock;
end;
type
TNodeStack=object
type
PNode=^TNode;
TNode=record
pNext:PNode;
S:RawByteString;
end;
var
pHead:PNode;
procedure Push(Const S:RawByteString);
function Pop:RawByteString;
end;
procedure TNodeStack.Push(Const S:RawByteString);
var
Node:PNode;
begin
Node:=AllocMem(SizeOf(TNode));
Node^.S:=S;
if (pHead=nil) then
begin
node^.pNext:=nil;
end else
begin
node^.pNext:=pHead;
end;
pHead:=node;
end;
function TNodeStack.Pop:RawByteString;
var
Node:PNode;
begin
Result:='';
Node:=pHead;
if (pHead<>nil) then
begin
pHead:=pHead^.pNext;
end;
if (Node<>nil) then
begin
Node^.pNext:=nil;
end;
if (Node<>nil) then
begin
Result:=Node^.S;
FreeMem(Node);
end;
end;
Function sce_load_filter(Const name:RawByteString):Boolean;
const
c_libc='libc';
c_libSce='libSce';
begin
Result:=(Copy(name,1,Length(c_libc))=c_libc) or
(Copy(name,1,Length(c_libSce))=c_libSce);
end;
function TryLoadElf(Const path,name:RawByteString):TElf_node;
var
s:RawByteString;
begin
//bulid path
s:=IncludeTrailingPathDelimiter(path)+'sce_module'+DirectorySeparator+name;
Result:=LoadPs4ElfFromFile(s); //try defaut .prx
if (Result=nil) then
begin
s:=ChangeFileExt(s,'.sprx');
Result:=LoadPs4ElfFromFile(s); //try .sprx
end;
end;
function Tps4_program.Loader(Const name:RawByteString):TElf_node;
var
nid:QWORD;
PP:PPointer;
cb:TOnElfLoadCb;
begin
Result:=nil;
nid:=ps4_nid_hash(name);
files.LockRd;
PP:=HAMT_search64(@files.hamt,nid);
if (PP<>nil) then Result:=TElf_node(PP^);
files.Unlock;
if (Result<>nil) then Exit; //is loaded