forked from red-prig/fpPS4
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ps4_elf.pas
3036 lines (2628 loc) · 70.8 KB
/
ps4_elf.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_elf;
{$mode objfpc}{$H+}
interface
uses
Windows,
sha1,
sys_types,
sys_kernel,
ps4libdoc,
ps4_program,
ps4_elf_tls,
Classes,
SysUtils;
type
t_user_malloc_init =function():Integer; SysV_ABI_CDecl;
t_user_malloc_finalize =function():Integer; SysV_ABI_CDecl;
t_user_malloc =function(size:qword):Pointer; SysV_ABI_CDecl;
t_user_free =procedure(ptr:Pointer); SysV_ABI_CDecl;
t_user_calloc =function(nelem,count:qword):Pointer; SysV_ABI_CDecl;
t_user_realloc =function(ptr:Pointer;size:qword):Pointer; SysV_ABI_CDecl;
t_user_memalign =function(align,size:qword):Pointer; SysV_ABI_CDecl;
t_user_reallocalign =function(ptr:Pointer;size,align:qword):Pointer; SysV_ABI_CDecl;
t_user_posix_memalign =function(ptr:PPointer;align,size:qword):Integer; SysV_ABI_CDecl;
t_user_malloc_stats =function(mmsize:Pointer):Integer; SysV_ABI_CDecl;
t_user_malloc_stats_fast =function(mmsize:Pointer):Integer; SysV_ABI_CDecl;
t_user_malloc_usable_size=function(ptr:Pointer):qword; SysV_ABI_CDecl;
PsceLibcMallocReplace=^TsceLibcMallocReplace;
TsceLibcMallocReplace=packed record
Size:QWORD;
Unknown1:QWORD; //00000001
user_malloc_init :t_user_malloc_init;
user_malloc_finalize :t_user_malloc_finalize;
user_malloc :t_user_malloc;
user_free :t_user_free;
user_calloc :t_user_calloc;
user_realloc :t_user_realloc;
user_memalign :t_user_memalign;
user_reallocalign :t_user_reallocalign;
user_posix_memalign :t_user_posix_memalign;
user_malloc_stats :t_user_malloc_stats;
user_malloc_stats_fast :t_user_malloc_stats_fast;
user_malloc_usable_size:t_user_malloc_usable_size;
end;
t_user_new_1 =function(size:qword):Pointer; SysV_ABI_CDecl;
t_user_new_2 =function(size:qword;throw:Pointer):Pointer; SysV_ABI_CDecl;
t_user_new_array_1 =function(size:qword):Pointer; SysV_ABI_CDecl;
t_user_new_array_2 =function(size:qword;throw:Pointer):Pointer; SysV_ABI_CDecl;
t_user_delete_1 =procedure(ptr:Pointer); SysV_ABI_CDecl;
t_user_delete_2 =procedure(ptr:Pointer;throw:Pointer); SysV_ABI_CDecl;
t_user_delete_array_1=procedure(ptr:Pointer); SysV_ABI_CDecl;
t_user_delete_array_2=procedure(ptr:Pointer;throw:Pointer); SysV_ABI_CDecl;
t_user_delete_3 =procedure(ptr:Pointer;size:qword); SysV_ABI_CDecl;
t_user_delete_4 =procedure(ptr:Pointer;size:qword;throw:Pointer); SysV_ABI_CDecl;
t_user_delete_array_3=procedure(ptr:Pointer;size:qword); SysV_ABI_CDecl;
t_user_delete_array_4=procedure(ptr:Pointer;size:qword;throw:Pointer); SysV_ABI_CDecl;
PsceLibcNewReplace=^TsceLibcNewReplace;
TsceLibcNewReplace=packed record
Size:QWORD;
Unknown1:QWORD; //00000002
user_new_1 :t_user_new_1;
user_new_2 :t_user_new_2;
user_new_array_1 :t_user_new_array_1;
user_new_array_2 :t_user_new_array_2;
user_delete_1 :t_user_delete_1;
user_delete_2 :t_user_delete_2;
user_delete_array_1:t_user_delete_array_1;
user_delete_array_2:t_user_delete_array_2;
user_delete_3 :t_user_delete_3;
user_delete_4 :t_user_delete_4;
user_delete_array_3:t_user_delete_array_3;
user_delete_array_4:t_user_delete_array_4;
end;
t_user_malloc_init_for_tls =function():Integer; SysV_ABI_CDecl;
t_user_malloc_fini_for_tls =function():Integer; SysV_ABI_CDecl;
t_user_malloc_for_tls =function(size:qword):Pointer; SysV_ABI_CDecl;
t_user_posix_memalign_for_tls=function(ptr:PPointer;align,size:qword):Integer; SysV_ABI_CDecl;
t_user_free_for_tls =procedure(ptr:Pointer); SysV_ABI_CDecl;
PsceLibcMallocReplaceForTls=^TsceLibcMallocReplaceForTls;
TsceLibcMallocReplaceForTls=packed record
Size:QWORD;
Unknown1:QWORD; //00000001
user_malloc_init_for_tls :t_user_malloc_init_for_tls;
user_malloc_fini_for_tls :t_user_malloc_fini_for_tls;
user_malloc_for_tls :t_user_malloc_for_tls;
user_posix_memalign_for_tls:t_user_posix_memalign_for_tls;
user_free_for_tls :t_user_free_for_tls;
end;
PSceLibcParam=^TSceLibcParam;
TSceLibcParam=packed record
Size:QWORD;
entry_count:DWORD;
SceLibcInternalHeap :DWORD; //1 //(entry_count > 1)
sceLibcHeapSize :PDWORD;
sceLibcHeapDelayedAlloc :PDWORD;
sceLibcHeapExtendedAlloc :PDWORD;
sceLibcHeapInitialSize :PDWORD;
_sceLibcMallocReplace :PsceLibcMallocReplace;
_sceLibcNewReplace :PsceLibcNewReplace;
sceLibcHeapHighAddressAlloc :PQWORD; //(entry_count > 2)
Need_sceLibc :PDWORD;
sceLibcHeapMemoryLock :PQWORD; //(entry_count > 4)
sceKernelInternalMemorySize :PQWORD;
_sceLibcMallocReplaceForTls :PsceLibcMallocReplaceForTls;
//The maximum amount of the memory areas for the mspace. min:0x1000000
sceLibcMaxSystemSize :PQWORD; //(entry_count > 7)
sceLibcHeapDebugFlags :PQWORD; //(entry_count > 8)
sceLibcStdThreadStackSize :PDWORD;
Unknown3:QWORD;
sceKernelInternalMemoryDebugFlags:PDWORD;
sceLibcWorkerThreadNum :PDWORD;
sceLibcWorkerThreadPriority :PDWORD;
sceLibcThreadUnnamedObjects :PDWORD;
end;
PSceKernelMemParam=^TSceKernelMemParam;
TSceKernelMemParam=packed record
Size:QWORD;
sceKernelExtendedPageTable :PQWORD;
sceKernelFlexibleMemorySize :PQWORD;
sceKernelExtendedMemory1 :PQWORD;
sceKernelExtendedGpuPageTable:PQWORD;
sceKernelExtendedMemory2 :PQWORD;
sceKernelExtendedCpuPageTable:PQWORD;
end;
PSceKernelFsParam=^TSceKernelFsParam;
TSceKernelFsParam=packed record
Size:QWORD;
sceKernelFsDupDent:PQWORD;
end;
PSceProcParam=^TSceProcParam;
TSceProcParam=packed record
Header:packed record
Size:QWORD; //0x50
Magic:DWORD; //"ORBI" //0x4942524F
Entry_count:DWORD; //>=1
SDK_version:QWORD; //0x4508101
end;
sceProcessName :PChar;
sceUserMainThreadName :PChar;
sceUserMainThreadPriority :PDWORD;
sceUserMainThreadStackSize:PDWORD;
_sceLibcParam :PSceLibcParam;
_sceKernelMemParam :PSceKernelMemParam;
_sceKernelFsParam :PSceKernelFsParam;
sceProcessPreloadEnabled :PDWORD;
end;
Telf_file=class;
PRelaInfo=^TRelaInfo;
TRelaInfo=packed record
pName:PChar;
value:QWORD;
Offset:QWORD;
Addend:QWORD;
rType:DWORD;
sBind:Byte;
sType:Byte;
shndx:Word;
end;
TOnRelaInfoCb=Procedure(elf:Telf_file;Info:PRelaInfo;data:Pointer);
PResolveImportInfo=^TResolveImportInfo;
TResolveImportInfo=packed record
pName:PChar;
_md:PMODULE;
lib:PLIBRARY;
nid:QWORD;
Offset:QWORD;
rType:DWORD;
sBind:Byte;
sType:Byte;
end;
TResolveImportCb=function(elf:Telf_file;Info:PResolveImportInfo;data:Pointer):Pointer;
Telf_file=class(TElf_node)
pSceFileName:RawByteString;
mElf:TMemChunk;
mMap:TMemChunk;
ModuleInfo:SceKernelModuleInfo;
ro_segments:array[0..3] of TMemChunk;
pEntry:QWORD;
dtInit:QWORD;
dtFini:QWORD;
pProcParam :QWORD;
pModuleParam :QWORD;
dtflags:QWORD;
pTls:packed record
tmpl_start:QWORD;
tmpl_size :QWORD;
full_size :QWORD;
align :QWORD;
index :QWORD;
offset :Int64;
stub:TMemChunk;
end;
pInit:packed record
dt_preinit_array,
dt_preinit_array_count:QWORD;
dt_init_array,
dt_init_array_count:QWORD;
end;
eh_frame_hdr:packed record
addr:QWORD;
size:QWORD;
end;
eh_frame:packed record
addr:QWORD;
size:QWORD;
end;
pSceDynLib:TMemChunk; //mElf
pDynEnt :PElf64_Dyn; //mElf
nDynEntCount:DWord; //mElf
pSymTab:PElf64_Sym; //pSceDynLib
nSymTabCount:Int64; //pSceDynLib
pStrTable:PAnsiChar; //pSceDynLib
nStrTabSize:Int64; //pSceDynLib
pRelaEntries:Pelf64_rela; //pSceDynLib
nRelaCount:Int64; //pSceDynLib
pPltRela:PElf64_Rela; //pSceDynLib
nPltRelaCount:Int64; //pSceDynLib
nPltRelType:Int64;
protected
function PreparePhdr:Boolean;
procedure PrepareTables(var entry:Elf64_Dyn);
procedure ParseSingleDynEntry(var entry:Elf64_Dyn);
function PrepareDynTables:Boolean;
procedure _calculateTotalLoadableSize(elf_phdr:Pelf64_phdr;s:SizeInt);
procedure _mapSegment(phdr:PElf64_Phdr;const name:PChar);
procedure _addSegment(phdr:PElf64_Phdr;const name:PChar);
function _ro_seg_len:Integer;
function _ro_seg_find_lo(adr:Pointer):Integer;
function _ro_seg_find_hi(adr:Pointer):Integer;
procedure _add_ro_seg(phdr:PElf64_Phdr);
procedure _print_rdol;
function _ro_seg_adr_in(adr:Pointer;size:QWORD):Boolean;
procedure _find_tls_stub(elf_phdr:Pelf64_phdr;s:SizeInt);
function MapImageIntoMemory:Boolean;
function RelocateRelaEnum(cbs:TOnRelaInfoCb;data:Pointer):Boolean;
function RelocatePltRelaEnum(cbs:TOnRelaInfoCb;data:Pointer):Boolean;
function ParseSymbolsEnum(cbs:TOnRelaInfoCb;data:Pointer):Boolean;
function LoadSymbolExport:Boolean;
procedure _PatchTls(Proc:Pointer;Addr:PByte;Size:QWORD);
procedure PatchTls(Proc:Pointer);
procedure mapProt;
procedure mapCodeInit;
Procedure ClearElfFile;
function _init_tls(is_static:QWORD):Ptls_tcb;
public
function _get_tls:Ptls_tcb;
Procedure Clean; override;
function SavePs4ElfToFile(Const name:RawByteString):Boolean;
function Prepare:Boolean; override;
Procedure LoadSymbolImport(cbs,data:Pointer); override;
Procedure ReLoadSymbolImport(cbs,data:Pointer); override;
function DympSymbol(F:THandle):Boolean;
procedure InitThread(is_static:QWORD); override;
Procedure InitProt; override;
Procedure InitCode; override;
function module_start(argc:size_t;argp,param:PPointer):Integer; override;
function GetCodeFrame:TMemChunk; override;
function GetEntry:Pointer; override;
function GetdInit:Pointer; override;
function GetdFini:Pointer; override;
Function GetModuleInfo:SceKernelModuleInfo; override;
Function GetModuleInfoEx:SceKernelModuleInfoEx; override;
procedure mapCodeEntry;
end;
type
TinitProc =function(argc:Integer;argv,environ:PPchar):Integer; SysV_ABI_CDecl; //preinit_array/init_array
TEntryPoint =procedure(pEnv:Pointer;pfnExitHandler:Pointer); SysV_ABI_CDecl; //EntryPoint
TmoduleStart=function(argc:size_t;argp,proc:Pointer):Integer; SysV_ABI_CDecl; //module_start/module_stop
function LoadPs4ElfFromFile(Const name:RawByteString):TElf_node;
function ps4_nid_hash(const name:RawByteString):QWORD;
function EncodeValue64(nVal:QWORD):RawByteString;
function DecodeValue64(strEnc:PAnsiChar;len:SizeUint;var nVal:QWORD):Boolean;
function DecodeEncName(strEncName:PAnsiChar;var nModuleId,nLibraryId:WORD;var nNid:QWORD):Boolean;
function _dynamic_tls_get_addr(ti:PTLS_index):Pointer; SysV_ABI_CDecl;
implementation
type
Ppatch_ld=^Tpatch_ld;
Tpatch_ld=packed record
//_movabs_rax:array[0..1] of Byte; // $48 $B8 //2
_addr:Pointer; //8
//_jmp_rax:array[0..1] of Byte; // $FF $E0 //2 = 14
end;
Ppatch_fs=^Tpatch_fs;
Tpatch_fs=packed record
_call_rip:array[0..1] of Byte; //$ff $15
//_push_rdx:Byte; // $52 //1
//_push_rcx:Byte; // $51 //1
//_call_32:Byte; // $E8 //1
_ofs:Integer; //4
//_pop_rcx:Byte; // $59 //1
//_pop_rdx:Byte; // $5a //1 = 9
_nop:array[0..2] of Byte; //$90 $90 $90
end;
//ff 15 [d3 ff ff ff]
Const
_patch_ld:Tpatch_ld=(
//_movabs_rax:($48,$B8);
_addr:nil;
//_jmp_rax:($FF,$E0);
);
_patch_fs:Tpatch_fs=(
_call_rip:($ff,$15);
//_push_rcx:$51;
//_push_rdx:$52;
//_call_32:$E8;
_ofs:0;
//_pop_rdx:$5a;
//_pop_rcx:$59;
_nop:($90,$90,$90);
);
Procedure Telf_file.ClearElfFile;
begin
if (Self=nil) then Exit;
FreeMem(mElf.pAddr);
mElf:=Default(TMemChunk);
pSceDynLib:=Default(TMemChunk);
pDynEnt :=nil;
nDynEntCount :=0;
pSymTab :=nil;
nSymTabCount :=0;
pStrTable :=nil;
nStrTabSize :=0;
pRelaEntries :=nil;
nRelaCount :=0;
pPltRela :=nil;
nPltRelaCount:=0;
end;
Procedure Telf_file.Clean;
begin
if (Self=nil) then Exit;
ClearElfFile;
FreeMem(mElf.pAddr);
if (mMap.pAddr<>nil) then
begin
VirtualFree(mMap.pAddr,0,MEM_RELEASE);
end;
pSceFileName:='';
inherited;
end;
function test_SF_flags(flags:QWORD):RawByteString;
begin
Result:='';
if (flags and SF_ORDR)<>0 then Result:=Result+' SF_ORDR';
if (flags and SF_ENCR)<>0 then Result:=Result+' SF_ENCR';
if (flags and SF_SIGN)<>0 then Result:=Result+' SF_SIGN';
if (flags and SF_DFLG)<>0 then Result:=Result+' SF_DFLG';
if (flags and SF_BFLG)<>0 then Result:=Result+' SF_BFLG';
end;
function LoadPs4ElfFromFile(Const name:RawByteString):TElf_node;
Var
elf:Telf_file;
F:THandle;
i:SizeInt;
self_header:Tself_header;
Segments:Pself_segment;
SELF_MEM:Pointer;
SELF_SIZE:Int64;
elf_hdr:Pelf64_hdr;
elf_phdr:Pelf64_phdr;
LowSeg,s:Int64;
Num_Segments:Word;
function _test_self(self_header:Pself_header):Boolean;
begin
Result:=False;
Writeln('SELF.Content_Type=',HexStr(self_header^.Content_Type,2));
Writeln('SELF.Program_Type=',HexStr(self_header^.Program_Type,2));
Writeln('SELF.Header_Size =',HexStr(self_header^.Header_Size,4));
Writeln('SELF.Sign_Size =',HexStr(self_header^.Sign_Size,4));
Writeln('SELF.Size_of =',HexStr(self_header^.Size_of,8));
Writeln('SELF.Num_Segments=',self_header^.Num_Segments);
if (self_header^.Num_Segments=0) then Exit;
Result:=True;
end;
function _test_elf(elf_hdr:Pelf64_hdr):Boolean;
begin
Result:=False;
if PDWORD(@elf_hdr^.e_ident)^<>ELFMAG then
begin
Writeln(StdErr,name,' ELF identifier mismatch:',HexStr(PDWORD(@elf_hdr^.e_ident)^,8));
Exit;
end;
Case elf_hdr^.e_type of
ET_SCE_EXEC :;
ET_SCE_REPLAY_EXEC:;
ET_SCE_RELEXEC :;
ET_SCE_DYNEXEC :;
ET_SCE_DYNAMIC :;
else
begin
Writeln(StdErr,name,' unspported TYPE:',HexStr(elf_hdr^.e_type,4));
Exit;
end;
end;
if (elf_hdr^.e_machine<>EM_X86_64) then
begin
Writeln(StdErr,name,' unspported ARCH:',elf_hdr^.e_machine);
Exit;
end;
Writeln('hdr.[EI_CLASS] :',elf_hdr^.e_ident[EI_CLASS]);
Writeln('hdr.[EI_DATA] :',elf_hdr^.e_ident[EI_DATA]);
Writeln('hdr.[EI_VERSION]:',elf_hdr^.e_ident[EI_VERSION]);
Writeln('hdr.[EI_OSABI] :',elf_hdr^.e_ident[EI_OSABI]);
Writeln('hdr.e_type :',HexStr(elf_hdr^.e_type,4));
Writeln('hdr.e_machine :',HexStr(elf_hdr^.e_machine,4));
Writeln('hdr.e_version :',HexStr(elf_hdr^.e_version,4));
Writeln('hdr.e_entry :',HexStr(elf_hdr^.e_entry,16));
Writeln('hdr.e_phoff :',HexStr(elf_hdr^.e_phoff,16));
Writeln('hdr.e_shoff :',HexStr(elf_hdr^.e_shoff,16));
Writeln('hdr.e_flags :',HexStr(elf_hdr^.e_flags,8));
Writeln('hdr.e_ehsize :',HexStr(elf_hdr^.e_ehsize,4));
Writeln('hdr.e_phentsize :',HexStr(elf_hdr^.e_phentsize,4));
Writeln('hdr.e_phnum :',elf_hdr^.e_phnum);
Writeln('hdr.e_shentsize :',HexStr(elf_hdr^.e_shentsize,4));
Writeln('hdr.e_shnum :',elf_hdr^.e_shnum );
Writeln('hdr.e_shstrndx :',HexStr(elf_hdr^.e_shstrndx,4));
if (elf_hdr^.e_phnum=0) then Exit;
Result:=True;
end;
begin
Result:=nil;
if (name='') then Exit;
F:=FileOpen(name,fmOpenRead);
if (F=feInvalidHandle) then Exit;
FileRead(F,self_header.Magic,SizeOf(DWORD));
case self_header.Magic of
ELFMAG:
begin
SELF_SIZE:=FileSeek(F,0,fsFromEnd);
if (SELF_SIZE<=0) then
begin
Writeln(StdErr,'Error read file:',name);
FileClose(F);
Exit;
end;
SELF_MEM:=GetMem(SELF_SIZE);
FileSeek(F,0,fsFromBeginning);
s:=FileRead(F,SELF_MEM^,SELF_SIZE);
FileClose(F);
if (s<>SELF_SIZE) then
begin
Writeln(StdErr,'Error read file:',name);
FreeMem(SELF_MEM);
Exit;
end;
if not _test_elf(SELF_MEM) then
begin
Writeln(StdErr,'Error test file:',name);
FreeMem(SELF_MEM);
Exit;
end;
elf:=Telf_file.Create;
elf.mElf.nSize:=SELF_SIZE;
elf.mElf.pAddr:=SELF_MEM;
elf._set_filename(name);
Result:=elf;
end;
self_magic:
begin
SELF_SIZE:=FileSeek(F,0,fsFromEnd);
if (SELF_SIZE<=0) then
begin
Writeln(StdErr,'Error read file:',name);
FileClose(F);
Exit;
end;
SELF_MEM:=GetMem(SELF_SIZE);
FileSeek(F,0,fsFromBeginning);
s:=FileRead(F,SELF_MEM^,SELF_SIZE);
FileClose(F);
if (s<>SELF_SIZE) then
begin
Writeln(StdErr,'Error read file:',name);
FreeMem(SELF_MEM);
Exit;
end;
if not _test_self(SELF_MEM) then
begin
FreeMem(SELF_MEM);
Exit;
end;
Num_Segments:=Pself_header(SELF_MEM)^.Num_Segments;
Segments:=SELF_MEM+SizeOf(Tself_header);
For i:=0 to Num_Segments-1 do
begin
if (Segments[i].flags and (SF_ENCR or SF_DFLG))<>0 then
begin
Writeln(StdErr,'[',i,']');
Writeln(StdErr,' Seg.flags =',test_SF_flags(Segments[i].flags));
Writeln(StdErr,' Seg.id =',Segments[i].flags shr 20);
Writeln(StdErr,' Seg.offset=',HexStr(Segments[i].offset,16));
Writeln(StdErr,' Seg.c_size=',HexStr(Segments[i].encrypted_compressed_size,16));
Writeln(StdErr,' Seg.d_size=',HexStr(Segments[i].decrypted_decompressed_size,16));
Writeln(StdErr,'encrypted or deflated SELF not support!');
FreeMem(SELF_MEM);
Exit;
end;
end;
elf_hdr:=Pointer(Segments)+(Num_Segments*SizeOf(Tself_segment));
if not _test_elf(elf_hdr) then
begin
Writeln(StdErr,'Error test file:',name);
FreeMem(SELF_MEM);
Exit;
end;
elf_phdr:=Pointer(elf_hdr)+SizeOf(elf64_hdr);
LowSeg:=High(Int64);
elf:=Telf_file.Create;
elf.mElf.nSize:=0;
For i:=0 to elf_hdr^.e_phnum-1 do
begin
s:=elf_phdr[i].p_offset;
if (s<>0) then
if (s<LowSeg) then LowSeg:=s;
s:=s+elf_phdr[i].p_filesz;
if s>elf.mElf.nSize then elf.mElf.nSize:=s;
end;
if (LowSeg=High(Int64)) then
begin
Writeln(StdErr,'Error LowSeg');
FreeMem(SELF_MEM);
elf.Free;
Exit;
end;
elf.mElf.pAddr:=AllocMem(elf.mElf.nSize);
Writeln('Elf with LowSeg:',LowSeg,' Size:',elf.mElf.nSize);
Move(elf_hdr^,elf.mElf.pAddr^,LowSeg);
For i:=0 to Num_Segments-1 do
if ((Segments[i].flags and SF_BFLG)<>0) then
begin
s:=(Segments[i].flags shr 20) and $FFF;
Move(Pointer(SELF_MEM+Segments[i].offset)^,
Pointer(elf.mElf.pAddr+elf_phdr[s].p_offset)^,
Segments[i].encrypted_compressed_size);
end;
FreeMem(SELF_MEM);
elf._set_filename(name);
Result:=elf;
end;
else
begin
FileClose(F);
Writeln(StdErr,name,' is unknow file type:',HexStr(self_header.Magic,8));
end;
end;
end;
function Telf_file.SavePs4ElfToFile(Const name:RawByteString):Boolean;
Var
F:THandle;
begin
Result:=False;
if (Self=nil) then Exit;
if (name='') then Exit;
if (mElf.pAddr=nil) or (mElf.nSize=0) then Exit;
F:=FileCreate(name);
if (F=feInvalidHandle) then Exit;
FileWrite(F,mElf.pAddr^,mElf.nSize);
FileClose(F);
end;
function GetStr(p:Pointer;L:SizeUint):RawByteString;
begin
SetString(Result,P,L);
end;
function test_PF_flags(p_flags:Elf64_Word):RawByteString;
begin
Result:='';
if (p_flags and PF_X)<>0 then Result:=Result+' PF_X';
if (p_flags and PF_W)<>0 then Result:=Result+' PF_W';
if (p_flags and PF_R)<>0 then Result:=Result+' PF_R';
end;
function read_encoded_value(hdr:p_eh_frame_hdr;hdr_size,hdr_vaddr:Int64;enc:Byte;var P:Pointer;var res:Int64):Boolean;
var
value:Int64;
endp:Pointer;
begin
Result:=False;
res:=0;
Case (enc and $70) of
DW_EH_PE_absptr:;
DW_EH_PE_pcrel:
begin
res:=(hdr_vaddr + (P - Pointer(hdr)));
end;
DW_EH_PE_datarel:
begin
res:=hdr_vaddr;
end;
else
Exit(True);
end;
value:=0;
endp:=Pointer(hdr) + hdr_size;
Case (enc and $0f) of
DW_EH_PE_udata2:
begin
if (p + 2 > endp) then Exit(True);
value:=PWORD(p)^;
p:=p + 2;
end;
DW_EH_PE_sdata2:
begin
if (p + 2 > endp) then Exit(True);
value:=PSmallint(p)^;
p:=p + 2;
end;
DW_EH_PE_udata4:
begin
if (p + 4 > endp) then Exit(True);
value:=PDWORD(p)^;
p:=p + 4;
end;
DW_EH_PE_sdata4:
begin
if (p + 4 > endp) then Exit(True);
value:=PInteger(p)^;
p:=p + 4;
end;
DW_EH_PE_udata8,
DW_EH_PE_sdata8:
begin
if (p + 8 > endp) then Exit(True);
value:=PInteger(p)^;
p:=p + 8;
end;
DW_EH_PE_absptr:
begin
value:=0;
end;
else
Exit(True);
end;
res:=res+value;
Result:=False;
end;
function parse_eh_frame_hdr(hdr:p_eh_frame_hdr;hdr_size,hdr_vaddr:Int64;var eh_frame_ptr:Int64):Boolean;
var
p:Pointer;
begin
if (hdr^.eh_frame_ptr_enc=DW_EH_PE_omit) then Exit(False);
p:=@hdr^.encoded;
if read_encoded_value(hdr,hdr_size,
hdr_vaddr,
hdr^.eh_frame_ptr_enc,
p,
eh_frame_ptr) then Exit(False);
Result:=True;
end;
function Telf_file.PreparePhdr:Boolean;
Var
i:SizeInt;
elf_hdr:Pelf64_hdr;
elf_phdr:Pelf64_phdr;
P:Pointer;
begin
Result:=False;
ModuleInfo:=Default(SceKernelModuleInfo);
ModuleInfo.size:=SizeOf(SceKernelModuleInfo);
elf_hdr:=mElf.pAddr;
if (elf_hdr=nil) then Exit;
elf_phdr:=Pointer(elf_hdr)+SizeOf(elf64_hdr);
if (elf_hdr^.e_phnum=0) then Exit;
For i:=0 to elf_hdr^.e_phnum-1 do
begin
{
Writeln('[',i,']');
case elf_phdr[i].p_type of
PT_NULL :Writeln(' phdr.PT_NULL');
PT_LOAD :Writeln(' phdr.PT_LOAD ');
PT_DYNAMIC:Writeln(' phdr.PT_DYNAMIC');
PT_INTERP :Writeln(' phdr.PT_INTERP');
PT_NOTE :Writeln(' phdr.PT_NOTE');
PT_SHLIB :Writeln(' phdr.PT_SHLIB');
PT_PHDR :Writeln(' phdr.PT_PHDR');
PT_TLS :Writeln(' phdr.PT_TLS');
PT_GNU_EH_FRAME :Writeln(' phdr.PT_GNU_EH_FRAME');
PT_GNU_STACK :Writeln(' phdr.PT_GNU_STACK');
PT_SCE_RELA :Writeln(' phdr.PT_SCE_RELA');
PT_SCE_DYNLIBDATA :Writeln(' phdr.PT_SCE_DYNLIBDATA');
PT_SCE_RELRO :Writeln(' phdr.PT_SCE_RELRO');
PT_SCE_COMMENT :Writeln(' phdr.PT_SCE_COMMENT');
PT_SCE_VERSION :Writeln(' phdr.PT_SCE_VERSION');
PT_SCE_PROCPARAM :Writeln(' phdr.PT_SCE_PROCPARAM');
PT_SCE_MODULE_PARAM:Writeln(' phdr.PT_SCE_MODULE_PARAM');
else
Writeln(' phdr.',HexStr(elf_phdr[i].p_type,16));
end;
Writeln(' phdr.p_flags =',test_PF_flags(elf_phdr[i].p_flags));
Writeln(' phdr.p_offset=',HexStr(elf_phdr[i].p_offset,16));
Writeln(' phdr.p_memsz =',HexStr(elf_phdr[i].p_memsz,16));
Writeln(' phdr.p_filesz=',HexStr(elf_phdr[i].p_filesz,16));
Writeln(' phdr.p_paddr =',HexStr(elf_phdr[i].p_paddr,16));
Writeln(' phdr.p_vaddr =',HexStr(elf_phdr[i].p_vaddr,16));
Writeln(' phdr.p_align =',HexStr(elf_phdr[i].p_align,16));
}
case elf_phdr[i].p_type of
PT_NOTE:; //skip
PT_LOAD:;//load
PT_SCE_RELRO:;//load
PT_SCE_PROCPARAM:
begin
pProcParam:=elf_phdr[i].p_vaddr;
end;
PT_SCE_MODULE_PARAM:
begin
pModuleParam:=elf_phdr[i].p_vaddr;
end;
PT_INTERP:
begin
P:=mElf.pAddr+elf_phdr[i].p_offset;
Writeln(GetStr(P,elf_phdr[i].p_filesz));
end;
PT_DYNAMIC:
begin
pDynEnt:=mElf.pAddr+elf_phdr[i].p_offset;
nDynEntCount:=elf_phdr[i].p_filesz div sizeof(Elf64_Dyn);
end;
PT_SCE_DYNLIBDATA:
begin
pSceDynLib.pAddr:=mElf.pAddr+elf_phdr[i].p_offset;
pSceDynLib.nSize:=elf_phdr[i].p_filesz;
end;
PT_TLS:
begin
pTls.tmpl_start:=elf_phdr[i].p_vaddr;
pTls.tmpl_size :=elf_phdr[i].p_filesz;
pTls.full_size :=elf_phdr[i].p_memsz;
pTls.align :=elf_phdr[i].p_align;
pTls.index :=PtrUint(Self);// module id
pTls.offset :=-pTls.full_size; //offset in static
//Assert(IsAlign(elf.pTls.tmpl_start,elf.pTls.align));
Writeln('tmpl_size:',pTls.tmpl_size,' full_size:',pTls.full_size);
end;
PT_SCE_COMMENT:
begin
end;
PT_SCE_VERSION:
begin
end;
PT_GNU_EH_FRAME:
begin
eh_frame_hdr.addr:=elf_phdr[i].p_vaddr;
eh_frame_hdr.size:=elf_phdr[i].p_filesz;
end;
else
Writeln('PHDR:',HexStr(elf_phdr[i].p_type,16));
end;
end;
Result:=True;
end;
function _lib_attr_text(id:DWord):RawByteString;
begin
Case id of
$01:Result:='AUTO_EXPORT';
$02:Result:='WEAK_EXPORT';
$08:Result:='LOOSE_IMPORT';
$09:Result:='AUTO_EXPORT|LOOSE_IMPORT';
$10:Result:='WEAK_EXPORT|LOOSE_IMPORT';
else
Result:=HexStr(id,8);
end;
end;
function _mod_attr_text(id:DWord):RawByteString;
begin
Case id of
$00:Result:='NONE';
$01:Result:='SCE_CANT_STOP';
$02:Result:='SCE_EXCLUSIVE_LOAD';
$04:Result:='SCE_EXCLUSIVE_START';
$08:Result:='SCE_CAN_RESTART';
$10:Result:='SCE_CAN_RELOCATE';
$20:Result:='SCE_CANT_SHARE';
else
Result:=HexStr(id,8);
end;
end;
procedure Telf_file.PrepareTables(var entry:Elf64_Dyn);
var
i:Byte;
begin
case entry.d_tag of
DT_PREINIT_ARRAY:
begin
pInit.dt_preinit_array:=entry.d_un.d_ptr;
Writeln('DT_PREINIT_ARRAY:',HexStr(entry.d_un.d_ptr,16));
end;
DT_PREINIT_ARRAYSZ:
begin
pInit.dt_preinit_array_count:=entry.d_un.d_ptr div SizeOf(Pointer);
Writeln('DT_PREINIT_ARRAY_count:',pInit.dt_preinit_array_count);
end;
DT_INIT:
begin
dtInit:=entry.d_un.d_ptr;
Writeln('INIT addr:',entry.d_un.d_ptr);
end;
DT_INIT_ARRAY:
begin
pInit.dt_init_array:=entry.d_un.d_ptr;
Writeln('DT_INIT_ARRAY:',HexStr(entry.d_un.d_ptr,16));
end;
DT_INIT_ARRAYSZ:
begin
pInit.dt_init_array_count:=entry.d_un.d_ptr div SizeOf(Pointer);
Writeln('DT_INIT_ARRAY_count:',pInit.dt_init_array_count);
end;
DT_FINI:
begin
dtFini:=entry.d_un.d_ptr;
Writeln('FINI addr:',HexStr(entry.d_un.d_ptr,16));
end;
DT_SCE_SYMTAB:
begin
pSymTab:=pSceDynLib.pAddr+entry.d_un.d_ptr;
Writeln('DT_SCE_SYMTAB:',entry.d_un.d_ptr);
end;
DT_SCE_SYMTABSZ: //symbol table
begin
nSymTabCount:=entry.d_un.d_val div SizeOf(Elf64_Sym);
Writeln('DT_SCE_SYMTABSZ:',entry.d_un.d_val);
end;
DT_SCE_STRTAB: //string table
begin
pStrTable:=pSceDynLib.pAddr+entry.d_un.d_ptr;
Writeln('DT_SCE_STRTAB:',entry.d_un.d_ptr);
end;
DT_SCE_STRSZ:
begin
nStrTabSize:=entry.d_un.d_ptr;
Writeln('DT_SCE_STRSZ:',entry.d_un.d_val);
end;
DT_SCE_RELA: //Relocation table
begin
pRelaEntries:=pSceDynLib.pAddr+entry.d_un.d_ptr;
Writeln('DT_SCE_RELA:',entry.d_un.d_ptr);
end;
DT_SCE_RELASZ:
begin
nRelaCount:=entry.d_un.d_val div sizeof(Elf64_Rela);
Writeln('DT_SCE_STRSZ:',nRelaCount);
end;
DT_SCE_JMPREL: //PLT relocation table
begin
pPltRela:=pSceDynLib.pAddr+entry.d_un.d_ptr;
Writeln('DT_SCE_JMPREL:',entry.d_un.d_ptr);
end;
DT_SCE_PLTREL:
begin
nPltRelType:=entry.d_un.d_val;
Writeln('DT_SCE_PLTREL:',entry.d_un.d_val);
end;
DT_SCE_PLTRELSZ:
begin
nPltRelaCount:=entry.d_un.d_val div sizeof(Elf64_Rela);
Writeln('DT_SCE_PLTRELSZ:',nPltRelaCount);
end;
DT_NEEDED:;
DT_SCE_MODULE_INFO:;
DT_SCE_NEEDED_MODULE:;
DT_SCE_EXPORT_LIB:;
DT_SCE_IMPORT_LIB:;
//Offset of the hash table. Similar to ELF hash tables, just has a custom tag.
DT_SCE_HASH :Writeln('HASH:',entry.d_un.d_val);
DT_SCE_HASHSZ:Writeln('HASH_SIZE:',entry.d_un.d_val);
//Offset of the global offset table.
DT_SCE_PLTGOT :Writeln('DT_SCE_PLTGOT :',HexStr(entry.d_un.d_val,16));
DT_NULL :Writeln('DT_NULL');
DT_DEBUG :Writeln('DT_DEBUG :',entry.d_un.d_val);
DT_TEXTREL :Writeln('DT_TEXTREL :',entry.d_un.d_val);
// Pointer to array of termination functions
DT_FINI_ARRAY :Writeln('DT_FINI_ARRAY :',entry.d_un.d_val);
DT_FINI_ARRAYSZ :Writeln('DT_FINI_ARRAYSZ :',entry.d_un.d_val);
//Should be set to `DF_TEXTREL`.
DT_FLAGS:
begin
dtflags:=entry.d_un.d_val;
Writeln('DT_FLAGS:',entry.d_un.d_val);