si_prc.pp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2017 by the Free Pascal development team
  4. System Entry point for PalmOS, Pascal only programs
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {$SMARTLINK OFF}
  12. unit si_prc;
  13. interface
  14. implementation
  15. {$i palmos.inc}
  16. var
  17. appInfo: SysAppInfoPtr; public name '__appInfo';
  18. StkLen: LongInt; external name '__stklen';
  19. sysinit_jmpbuf: jmp_buf;
  20. ExitCode: LongInt;
  21. var
  22. { this is declared by the PalmOS linker script }
  23. data_start: pbyte; external name 'data_start';
  24. procedure PascalMain; external name 'PASCALMAIN';
  25. procedure FPCRelocateData; forward;
  26. { this function must be the first in this unit which contains code }
  27. function _FPC_proc_start: longint; cdecl; public name '_start';
  28. var
  29. locAppInfo: SysAppInfoPtr;
  30. prevGlobals: Pointer;
  31. globalsPtr: Pointer;
  32. begin
  33. _FPC_proc_start:=0;
  34. if SysAppStartup(locAppInfo, prevGlobals, globalsPtr) <> 0 then
  35. begin
  36. SndPlaySystemSound(sndError);
  37. exit(-1);
  38. end;
  39. if (locAppInfo^.launchFlags and sysAppLaunchFlagNewGlobals) > 0 then
  40. FPCRelocateData;
  41. { we don't support anything but normal startup now }
  42. { FIXME: figure it out how various startup commands can }
  43. { coexist with the normal system unit infrastructure (KB) }
  44. if locAppInfo^.cmd = sysAppLaunchCmdNormalLaunch then
  45. begin
  46. if setjmp(sysinit_jmpbuf) = 0 then
  47. begin
  48. appInfo:=locAppInfo;
  49. PascalMain;
  50. end;
  51. _FPC_proc_start:=ExitCode;
  52. end;
  53. SysAppExit(locAppInfo, prevGlobals, globalsPtr);
  54. end;
  55. procedure _FPC_proc_halt(_ExitCode: longint); cdecl; public name '_haltproc';
  56. begin
  57. ExitCode:=_ExitCode;
  58. longjmp(sysinit_jmpbuf,1);
  59. end;
  60. { data segment relocation magic, ported to Pascal from prc-tools C version }
  61. type
  62. PReloc = ^TReloc;
  63. TReloc = record
  64. case boolean of
  65. true: ( next: smallint; addend: word );
  66. false: ( value: dword );
  67. end;
  68. procedure RelocateChain(offset: smallint; base: pointer);
  69. var
  70. data_res: pbyte;
  71. site: PReloc;
  72. begin
  73. data_res:=@data_start;
  74. while offset >= 0 do
  75. begin
  76. site:=PReloc(data_res + offset);
  77. offset:=site^.next;
  78. site^.next:=0;
  79. site^.value:=site^.value + PtrUInt(base);
  80. end;
  81. end;
  82. procedure FPCRelocateData;
  83. var
  84. relocH: MemHandle;
  85. chain: psmallint;
  86. const
  87. rloc_id = $726c6f63; // 'rloc'
  88. begin
  89. relocH:=DmGet1Resource(rloc_id, 0);
  90. if relocH <> nil then
  91. begin
  92. chain:=MemHandleLock(relocH);
  93. RelocateChain(chain^, @data_start);
  94. Inc(chain);
  95. RelocateChain(chain^, @_FPC_proc_start);
  96. Inc(chain);
  97. MemHandleUnlock(relocH);
  98. DmReleaseResource(relocH);
  99. end;
  100. end;
  101. end.