From f590b72d9c78546623007db36ba8f81fd5525fa7 Mon Sep 17 00:00:00 2001 From: red-prig Date: Sun, 23 Oct 2022 00:09:28 +0300 Subject: [PATCH] + --- kernel/ps4_kernel_file.pas | 2 +- kernel/ps4_libkernel.pas | 2 +- ps4_elf.pas | 249 +++++++++++++++------------- ps4_program.pas | 32 ++-- sys/sys_crt.pas | 91 ++++++++-- sys/sys_types.pas | 9 +- tools/nid_gui_test/main.lfm | 3 - tools/nid_gui_test/main.pas | 6 +- tools/nid_gui_test/nid_gui_test.lpi | 1 + 9 files changed, 243 insertions(+), 152 deletions(-) diff --git a/kernel/ps4_kernel_file.pas b/kernel/ps4_kernel_file.pas index 5745870f..fa895d92 100644 --- a/kernel/ps4_kernel_file.pas +++ b/kernel/ps4_kernel_file.pas @@ -290,7 +290,7 @@ begin Result:=0; if (path=nil) then Exit(EINVAL); - Writeln('open:',path,' ',flags,' ',mode); + Writeln('open:',path,' ',flags,' (',OctStr(mode,3),')'); Assert((flags and O_DIRECTORY)=0,'folder open TODO'); diff --git a/kernel/ps4_libkernel.pas b/kernel/ps4_libkernel.pas index 04e5e8e1..955b8e66 100644 --- a/kernel/ps4_libkernel.pas +++ b/kernel/ps4_libkernel.pas @@ -263,7 +263,7 @@ begin //sceKernelGetLibkernelTextLocation(pOut^.address,pOut^.size) end; -function ps4_sceKernelGetProcParam:Pointer; SysV_ABI_CDecl; +function ps4_sceKernelGetProcParam:PSceProcParam; SysV_ABI_CDecl; begin Writeln('KernelGetProcParam'); Result:=GetSceProcParam; diff --git a/ps4_elf.pas b/ps4_elf.pas index 799591d3..fce58d18 100644 --- a/ps4_elf.pas +++ b/ps4_elf.pas @@ -398,18 +398,32 @@ Var 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'); + Writeln(StdErr,name,' ELF identifier mismatch:',HexStr(PDWORD(@elf_hdr^.e_ident)^,8)); Exit; end; - if ((elf_hdr^.e_type <> ET_SCE_DYNEXEC) and - (elf_hdr^.e_type <> ET_SCE_DYNAMIC)) or - (elf_hdr^.e_machine <> EM_X86_64) then + + 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 TYPE/ARCH.'); + 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]); @@ -563,7 +577,7 @@ begin else begin FileClose(F); - Writeln(StdErr,name,' is unknow file type!'); + Writeln(StdErr,name,' is unknow file type:',HexStr(self_header.Magic,8)); end; end; end; @@ -994,7 +1008,6 @@ end; procedure Telf_file.ParseSingleDynEntry(var entry:Elf64_Dyn); var - i:SizeInt; mu:TModuleValue; lu:TLibraryValue; _md:TMODULE; @@ -1647,29 +1660,98 @@ begin Result:=True; end; -function _on_module_start_stop(pName:PChar):Integer; +var + mod_space:TMODULE=( + attr:0; + Import:True; + strName:''; + ); + +var + lib_space:TLIBRARY=( + parent:nil; + MapSymbol:nil; + attr:0; + Import:True; + strName:''; + Fset_proc_cb:nil; + Fget_proc_cb:nil; + ); + +function _convert_info_name(elf:Telf_file; + Info:PRelaInfo; + IInfo:PResolveImportInfo + ):Boolean; +var + nModId,nLibId:WORD; begin - Result:=-1; - if (PQWORD(pName)^=$735F656C75646F6D) then //module_s - begin - Case PDWORD(@pName[8])^ of - $74726174: //tart - if (pName[$C]=#0) then - begin //module_start - Result:=0; + Result:=True; + + nModId:=$FFFF; //no mod + nLibId:=$FFFF; //no lib + + case Info^.sType of + STT_NOTYPE: + begin + IInfo^.nid:=ps4_nid_hash(Info^.pName); + + if (Info^.shndx=SHN_UNDEF) then //import + begin + // + end else + begin + nModId:=elf._find_mod_export; + nLibId:=elf._find_lib_export; end; - $00706F74: //top0 - begin //module_stop - Result:=1; + + end; + STT_SCE: + begin + if not DecodeValue64(Info^.pName,StrLen(Info^.pName),IInfo^.nid) then + begin + Exit(False); end; - else; - end; + + if (Info^.shndx=SHN_UNDEF) then //import + begin + // + end else + begin + nModId:=elf._find_mod_export; + nLibId:=elf._find_lib_export; + end; + + end; + else + begin + if not DecodeEncName(Info^.pName,nModId,nLibId,IInfo^.nid) then + begin + Exit(False); + end; + end; end; + + if (nModId=$FFFF) then + begin + IInfo^._md:=@mod_space; + end else + begin + IInfo^._md:=elf._get_mod(nModId); + end; + + if (nLibId=$FFFF) then + begin + IInfo^.lib:=@lib_space; + end else + begin + IInfo^.lib:=elf._get_lib(nLibId); + end; + end; Procedure OnLoadRelaExport(elf:Telf_file;Info:PRelaInfo;data:Pointer); - procedure _do_set(nSymVal:Pointer); inline; + procedure _do_set(nSymVal:Pointer);// inline; begin if (Info^.Offset<>0) then begin @@ -1681,62 +1763,27 @@ Procedure OnLoadRelaExport(elf:Telf_file;Info:PRelaInfo;data:Pointer); var IInfo:TResolveImportInfo; - nModuleId,nLibraryId:Word; - val:Pointer; Import:Boolean; begin - - case Info^.sType of - STT_NOTYPE :Import:=False; - STT_SCE :Import:=False; - else - Import:=(Info^.shndx=SHN_UNDEF); - end; + Import:=(Info^.shndx=SHN_UNDEF); if Import then Exit; IInfo:=Default(TResolveImportInfo); - nModuleId:=0; - nLibraryId:=0; - - case Info^.sType of - STT_NOTYPE: - begin - IInfo.nid:=ps4_nid_hash(Info^.pName); - nModuleId:=elf._find_mod_export; - nLibraryId:=elf._find_lib_export; - end; - STT_SCE: - begin - if not DecodeValue64(Info^.pName,StrLen(Info^.pName),IInfo.nid) then - begin - Writeln(StdErr,'Error decode:',Info^.pName); - end; - nModuleId:=elf._find_mod_export; - nLibraryId:=elf._find_lib_export; - end; - else - begin - if not DecodeEncName(Info^.pName,nModuleId,nLibraryId,IInfo.nid) then - begin - Writeln(StdErr,'Error decode:',Info^.pName); - end; - end; + if not _convert_info_name(elf,Info,@IInfo) then + begin + Writeln(StdErr,'Error decode:',Info^.pName); + Exit; end; - IInfo._md:=elf._get_mod(nModuleId); - IInfo.lib:=elf._get_lib(nLibraryId); - - if (IInfo._md=nil) then + if (IInfo._md=nil)then begin Writeln(StdErr,'Unknow module from ',Info^.pName); - end; - - if (IInfo._md<>nil) then + end else if (IInfo._md^.Import<>Import) then begin Writeln(StdErr,'Wrong module ref:',IInfo._md^.strName,':',IInfo._md^.Import,'<>',Import); @@ -1746,8 +1793,7 @@ Procedure OnLoadRelaExport(elf:Telf_file;Info:PRelaInfo;data:Pointer); begin Writeln(StdErr,'Unknow library from ',Info^.pName); Exit; - end; - + end else if (IInfo.lib^.Import<>Import) then begin Writeln(StdErr,'Wrong library ref:',IInfo.lib^.strName,':',IInfo.lib^.Import,'<>',Import); @@ -1834,8 +1880,6 @@ Procedure OnLoadRelaImport(elf:Telf_file;Info:PRelaInfo;data:Pointer); var IInfo:TResolveImportInfo; - nModuleId,nLibraryId:Word; - Import:Boolean; begin @@ -1846,28 +1890,22 @@ Procedure OnLoadRelaImport(elf:Telf_file;Info:PRelaInfo;data:Pointer); else Import:=(Info^.shndx=SHN_UNDEF); end; + Import:=(Info^.shndx=SHN_UNDEF); if not Import then Exit; IInfo:=Default(TResolveImportInfo); - nModuleId:=0; - nLibraryId:=0; - - if not DecodeEncName(Info^.pName,nModuleId,nLibraryId,IInfo.nid) then + if not _convert_info_name(elf,Info,@IInfo) then begin Writeln(StdErr,'Error decode:',Info^.pName); + Exit; end; - IInfo._md:=elf._get_mod(nModuleId); - IInfo.lib:=elf._get_lib(nLibraryId); - if (IInfo._md=nil) then begin Writeln(StdErr,'Unknow module from ',Info^.pName); - end; - - if (IInfo._md<>nil) then + end else if (IInfo._md^.Import<>Import) then begin Writeln(StdErr,'Wrong module ref:',IInfo._md^.strName,':',IInfo._md^.Import,'<>',Import); @@ -1877,8 +1915,7 @@ Procedure OnLoadRelaImport(elf:Telf_file;Info:PRelaInfo;data:Pointer); begin Writeln(StdErr,'Unknow library from ',Info^.pName); Exit; - end; - + end else if (IInfo.lib^.Import<>Import) then begin Writeln(StdErr,'Wrong library ref:',IInfo.lib^.strName,':',IInfo.lib^.Import,'<>',Import); @@ -1961,8 +1998,6 @@ const IInfo:TResolveImportInfo; - nModuleId,nLibraryId:Word; - Import:Boolean; begin @@ -1972,46 +2007,19 @@ const else Import:=(Info^.shndx=SHN_UNDEF); end; + Import:=(Info^.shndx=SHN_UNDEF); IInfo:=Default(TResolveImportInfo); - nModuleId:=0; - nLibraryId:=0; - - case Info^.sType of - STT_NOTYPE: - begin - IInfo.nid:=ps4_nid_hash(Info^.pName); - nModuleId:=elf._find_mod_export; - nLibraryId:=elf._find_lib_export; - end; - STT_SCE: - begin - if not DecodeValue64(Info^.pName,StrLen(Info^.pName),IInfo.nid) then - begin - Writeln(StdErr,'Error decode:',Info^.pName); - end; - nModuleId:=elf._find_mod_export; - nLibraryId:=elf._find_lib_export; - end; - else - begin - if not DecodeEncName(Info^.pName,nModuleId,nLibraryId,IInfo.nid) then - begin - Writeln(StdErr,'Error decode:',Info^.pName); - end; - end; + if not _convert_info_name(elf,Info,@IInfo) then + begin + FWriteln('Error decode:'+Info^.pName); end; - IInfo._md:=elf._get_mod(nModuleId); - IInfo.lib:=elf._get_lib(nLibraryId); - if (IInfo._md=nil) then begin FWriteln('Unknow module from '+Info^.pName); - end; - - if (IInfo._md<>nil) then + end else if (IInfo._md^.Import<>Import) then begin FWriteln('Wrong module ref:'+IInfo._md^.strName+':'+BoolToStr(IInfo._md^.Import)+'<>'+BoolToStr(Import)); @@ -2020,9 +2028,7 @@ const if (IInfo.lib=nil) then begin FWriteln('Unknow library from '+Info^.pName); - end; - - if (IInfo.lib<>nil) then + end else if (IInfo.lib^.Import<>Import) then begin FWriteln('Wrong library ref:'+IInfo.lib^.strName+':'+BoolToStr(IInfo.lib^.Import)+'<>'+BoolToStr(Import)); @@ -2030,7 +2036,14 @@ const functName:=ps4libdoc.GetFunctName(IInfo.nid); - FWrite(__nBind(Info^.sBind)+':'+__sType(Info^.sType)+':'+IInfo._md^.strName +':'); + FWrite(__nBind(Info^.sBind)+':'+__sType(Info^.sType)+':'); + + if (IInfo._md<>nil) then + begin + FWrite(IInfo._md^.strName); + end; + + FWriteln(':'); if (IInfo.lib<>nil) then begin diff --git a/ps4_program.pas b/ps4_program.pas index dee8ad4c..a6a1eb9c 100644 --- a/ps4_program.pas +++ b/ps4_program.pas @@ -58,13 +58,13 @@ type aMods:array of TMODULE; aLibs:array of PLIBRARY; procedure _set_filename(const name:RawByteString); - procedure _add_need(const name:RawByteString); inline; - procedure _set_mod(id:Word;_md:TMODULE); inline; - procedure _set_mod_attr(u:TModuleValue); inline; - function _get_mod(id:Word):PMODULE; inline; - procedure _set_lib(id:Word;lib:TLIBRARY); inline; - procedure _set_lib_attr(u:TLibraryValue); inline; - function _get_lib(id:Word):PLIBRARY; inline; + 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 @@ -351,7 +351,7 @@ begin pFileName:=name; end; -procedure TElf_node._add_need(const name:RawByteString); inline; +procedure TElf_node._add_need(const name:RawByteString); var i:SizeInt; begin @@ -360,7 +360,7 @@ begin aNeed[i]:=name; end; -procedure TElf_node._set_mod(id:Word;_md:TMODULE); inline; +procedure TElf_node._set_mod(id:Word;_md:TMODULE); var i:SizeInt; begin @@ -373,7 +373,7 @@ begin aMods[id]:=_md; end; -procedure TElf_node._set_mod_attr(u:TModuleValue); inline; +procedure TElf_node._set_mod_attr(u:TModuleValue); var i:SizeInt; begin @@ -386,7 +386,7 @@ begin aMods[u.id].attr:=u.name_offset; end; -function TElf_node._get_mod(id:Word):PMODULE; inline; +function TElf_node._get_mod(id:Word):PMODULE; begin Result:=nil; if (Length(aMods)>id) then @@ -408,7 +408,7 @@ begin end; end; -procedure TElf_node._set_lib(id:Word;lib:TLIBRARY); inline; +procedure TElf_node._set_lib(id:Word;lib:TLIBRARY); var i:SizeInt; plib:PLIBRARY; @@ -426,7 +426,7 @@ begin aLibs[id]:=plib; end; -procedure TElf_node._set_lib_attr(u:TLibraryValue); inline; +procedure TElf_node._set_lib_attr(u:TLibraryValue); var i:SizeInt; plib:PLIBRARY; @@ -444,7 +444,7 @@ begin aLibs[u.id]:=plib; end; -function TElf_node._get_lib(id:Word):PLIBRARY; inline; +function TElf_node._get_lib(id:Word):PLIBRARY; begin Result:=nil; if (Length(aLibs)>id) then @@ -637,6 +637,7 @@ var data:PPointer; PP:PPointer; begin + if (@Self=nil) then Exit(False); if (MapSymbol=nil) then MapSymbol:=HAMT_create64; data:=nil; @@ -668,6 +669,7 @@ var PP:PPointer; begin Result:=nil; + if (@Self=nil) then Exit; data:=nil; PP:=HAMT_search64(MapSymbol,nid); if (PP<>nil) then data:=PP^; @@ -676,6 +678,7 @@ 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 @@ -684,6 +687,7 @@ 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 diff --git a/sys/sys_crt.pas b/sys/sys_crt.pas index 5da4067e..2f240c6c 100644 --- a/sys/sys_crt.pas +++ b/sys/sys_crt.pas @@ -14,6 +14,7 @@ Procedure sys_crt_init; implementation uses + sys_kernel, sys_signal; var @@ -34,6 +35,17 @@ begin end; end; +function GetConsoleCursorPosition(hConsoleOutput:HANDLE;var dwCursorPosition:COORD):WINBOOL; +var + info:CONSOLE_SCREEN_BUFFER_INFO; +begin + Result:=GetConsoleScreenBufferInfo(hConsoleOutput,@info); + if Result then + begin + dwCursorPosition:=info.dwCursorPosition; + end; +end; + Procedure CrtOutWrite(var t:TextRec); var n:DWORD; @@ -44,7 +56,11 @@ Begin _sig_lock(SL_NOINTRRUP); spin_lock(StdOutLock); - WriteConsole(t.Handle,t.Bufptr,t.BufPos,@n,nil); + WriteConsole(t.Handle, + t.Bufptr, + t.BufPos, + @n, + nil); spin_unlock(StdOutLock); _sig_unlock(SL_NOINTRRUP); @@ -56,6 +72,7 @@ end; Procedure CrtErrWrite(var t:TextRec); var n:DWORD; + dwCursorPosition:COORD; Begin if (t.BufPos=0) then exit; n:=0; @@ -63,9 +80,45 @@ Begin _sig_lock(SL_NOINTRRUP); spin_lock(StdOutLock); - SetConsoleTextAttribute(t.Handle,StdErrColor); - WriteConsole(t.Handle,t.Bufptr,t.BufPos,@n,nil); - SetConsoleTextAttribute(t.Handle,StdOutColor); + dwCursorPosition:=Default(COORD); + + GetConsoleCursorPosition(t.Handle, + dwCursorPosition); + + WriteConsole(t.Handle, + t.Bufptr, + t.BufPos, + @n, + nil); + + FillConsoleOutputAttribute(t.Handle, + StdErrColor, + t.BufPos, + dwCursorPosition, + n); + + spin_unlock(StdOutLock); + _sig_unlock(SL_NOINTRRUP); + + if (n<>t.BufPos) then InOutRes:=101; + t.BufPos:=0; +end; + +Procedure CrtFileWrite(var t:TextRec); +var + n:DWORD; +Begin + if (t.BufPos=0) then exit; + n:=0; + + _sig_lock(SL_NOINTRRUP); + spin_lock(StdOutLock); + + WriteFile(t.Handle, + t.Bufptr^, + t.BufPos, + n, + nil); spin_unlock(StdOutLock); _sig_unlock(SL_NOINTRRUP); @@ -82,17 +135,35 @@ end; Procedure CrtOpenOut(Var F:TextRec); Begin TextRec(F).Handle:=GetStdHandle(STD_OUTPUT_HANDLE); - TextRec(F).InOutFunc:=@CrtOutWrite; - TextRec(F).FlushFunc:=@CrtOutWrite; - TextRec(F).CloseFunc:=@CrtClose; + + if (SwGetFileType(TextRec(F).Handle)=FILE_TYPE_CHAR) then + begin + TextRec(F).InOutFunc:=@CrtOutWrite; + TextRec(F).FlushFunc:=@CrtOutWrite; + TextRec(F).CloseFunc:=@CrtClose; + end else + begin + TextRec(F).InOutFunc:=@CrtFileWrite; + TextRec(F).FlushFunc:=@CrtFileWrite; + TextRec(F).CloseFunc:=@CrtClose; + end; end; Procedure CrtOpenErr(Var F:TextRec); Begin TextRec(F).Handle:=GetStdHandle(STD_ERROR_HANDLE); - TextRec(F).InOutFunc:=@CrtErrWrite; - TextRec(F).FlushFunc:=@CrtErrWrite; - TextRec(F).CloseFunc:=@CrtClose; + + if (SwGetFileType(TextRec(F).Handle)=FILE_TYPE_CHAR) then + begin + TextRec(F).InOutFunc:=@CrtErrWrite; + TextRec(F).FlushFunc:=@CrtErrWrite; + TextRec(F).CloseFunc:=@CrtClose; + end else + begin + TextRec(F).InOutFunc:=@CrtFileWrite; + TextRec(F).FlushFunc:=@CrtFileWrite; + TextRec(F).CloseFunc:=@CrtClose; + end; end; procedure AssignCrt(var F:Text;cb:codepointer); diff --git a/sys/sys_types.pas b/sys/sys_types.pas index a91b9a07..c3747e8e 100644 --- a/sys/sys_types.pas +++ b/sys/sys_types.pas @@ -28,8 +28,13 @@ Const ELFMAG =$464C457F; SELFMAG=4; - ET_SCE_DYNEXEC=$FE10; - ET_SCE_DYNAMIC=$FE18; + ET_SCE_EXEC =$FE00; + ET_SCE_REPLAY_EXEC=$FE01; + ET_SCE_RELEXEC =$FE04; + ET_SCE_STUBLIB =$FE0C; + ET_SCE_DYNEXEC =$FE10; + ET_SCE_DYNAMIC =$FE18; + EM_X86_64 =62; // AMD x86-64 EI_NIDENT=16; diff --git a/tools/nid_gui_test/main.lfm b/tools/nid_gui_test/main.lfm index b27c6507..550ad648 100644 --- a/tools/nid_gui_test/main.lfm +++ b/tools/nid_gui_test/main.lfm @@ -19,7 +19,6 @@ object frmMain: TfrmMain Font.Height = -19 ParentFont = False TabOrder = 0 - OnClick = NidBase64Click OnExit = NidBase64Click OnKeyDown = TextKeyDown end @@ -35,7 +34,6 @@ object frmMain: TfrmMain Font.Height = -19 ParentFont = False TabOrder = 1 - OnClick = NidHexClick OnExit = NidHexClick OnKeyDown = TextKeyDown end @@ -51,7 +49,6 @@ object frmMain: TfrmMain Font.Height = -19 ParentFont = False TabOrder = 2 - OnClick = NidNameClick OnExit = NidNameClick OnKeyDown = TextKeyDown end diff --git a/tools/nid_gui_test/main.pas b/tools/nid_gui_test/main.pas index 515a02ff..67002ec5 100644 --- a/tools/nid_gui_test/main.pas +++ b/tools/nid_gui_test/main.pas @@ -120,11 +120,11 @@ end; procedure TfrmMain.TextKeyDown(Sender:TObject;var Key:Word;Shift:TShiftState); begin - if Sender.InheritsFrom(TLabeledEdit) then - if (TLabeledEdit(Sender).OnClick<>nil) then if (Key=13) then + if Sender.InheritsFrom(TLabeledEdit) then + if (TLabeledEdit(Sender).OnExit<>nil) then begin - TLabeledEdit(Sender).OnClick(Sender); + TLabeledEdit(Sender).OnExit(Sender); end; end; diff --git a/tools/nid_gui_test/nid_gui_test.lpi b/tools/nid_gui_test/nid_gui_test.lpi index 63d07ebf..20baea79 100644 --- a/tools/nid_gui_test/nid_gui_test.lpi +++ b/tools/nid_gui_test/nid_gui_test.lpi @@ -38,6 +38,7 @@ +