nwpre.pp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. (*
  2. # $Id: nwpre.as,v 1.3 2003/03/25 18:17:54 armin Exp $
  3. # This file is part of the Free Pascal run time library.
  4. # Copyright (c) 1999-2011 by the Free Pascal development team
  5. # Copyright (c) 2002-2011 Armin Diehl
  6. #
  7. # This is the (nwpre-like) startup code for netware (clib)
  8. #
  9. # See the file COPYING.FPC, included in this distribution,
  10. # for details about the copyright.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. #
  16. #**********************************************************************
  17. # This version initializes BSS
  18. #
  19. # Imported functions will not be known by the linker because only the
  20. # generated object file will be included into the link process. The
  21. # ppu will not be read. Therefore the file nwpre.imp containing the
  22. # names of all imported functions needs to be created. This file
  23. # will be used by the internal linker to import the needed functions.
  24. #**********************************************************************
  25. *)
  26. unit nwpre;
  27. interface
  28. implementation
  29. procedure _SetupArgV_411 (startProc:pointer); cdecl; external 'clib' name '_SetupArgV_411';
  30. procedure _nlm_main; external name '_nlm_main';
  31. procedure FPC_NW_CHECKFUNCTION; external name 'FPC_NW_CHECKFUNCTION';
  32. function _StartNLM (NLMHandle : longint;
  33. initErrorScreenID : longint;
  34. cmdLineP : pchar;
  35. loadDirectoryPath : pchar;
  36. uninitializedDataLength : longint;
  37. NLMFileHandle : longint;
  38. readRoutineP : pointer;
  39. customDataOffset : longint;
  40. customDataSize : longint;
  41. NLMInformation : pointer;
  42. userStartFunc : pointer) : longint; cdecl; external '!clib' name '_StartNLM';
  43. function _TerminateNLM (NLMInformation : pointer;
  44. threadID, status : longint) : longint; cdecl; external '!clib' name '_TerminateNLM';
  45. procedure _Stop; cdecl; forward;
  46. // This is the main program (not loader) Entry-Point that will be called by netware
  47. // it sets up the argc and argv and calls _nlm_main (in system.pp)
  48. procedure _pasStart; assembler; export; [alias:'_pasStart_'];
  49. asm
  50. pushl $_nlm_main
  51. call _SetupArgV_411
  52. addl $4,%esp
  53. ret
  54. // this is a hack to avoid that FPC_NW_CHECKFUNCTION will be
  55. // eleminated by the linker (with smartlinking)
  56. // TODO: change the internal linker to allow check and stop
  57. call FPC_NW_CHECKFUNCTION
  58. call _Stop
  59. end;
  60. // structure needed by clib
  61. type kNLMInfoT =
  62. packed record
  63. Signature : array [0..3] of char; // LONG 'NLMI'
  64. Flavor : longint; // TRADINIONAL_FLAVOR = 0
  65. Version : longint; // TRADINIONAL_VERSION = 0, LIBERTY_VERSION = 1
  66. LongDoubleSize : longint; // gcc nwpre defines 12, watcom 8
  67. wchar_tSize : longint;
  68. end;
  69. var
  70. _kNLMInfo:kNLMInfoT = (Signature:'NLMI';Flavor:0;Version:1;LongDoubleSize:8;wChar_tSize:2);
  71. // symbol is generated by the internal linker, when using ld in the future again,
  72. // the link script for ld needs to be modified to include this symbol
  73. bss : ptruint; external name '__bss_start__';
  74. // fillchar
  75. // netware kernel function
  76. procedure CSetB(value:byte; var addr; count:longint); cdecl; external '!' name 'CSetB';
  77. // this will be called by the loader, we pass the address of _pasStart_ and
  78. // _kNLMInfo (needed by clib) and clib will call _pasStart within a newly
  79. // created thread
  80. function _Prelude (NLMHandle : longint;
  81. initErrorScreenID : longint;
  82. cmdLineP : pchar;
  83. loadDirectoryPath : pchar;
  84. uninitializedDataLength : longint;
  85. NLMFileHandle : longint;
  86. readRoutineP : pointer;
  87. customDataOffset : longint;
  88. customDataSize : longint) : longint; cdecl; export; [alias:'_Prelude'];
  89. begin
  90. // initialize BSS
  91. CSetB(0,bss,uninitializedDataLength);
  92. // let clib setup a thread and call pasStart in this new thread
  93. _Prelude := _StartNLM (NLMHandle,
  94. initErrorScreenID,
  95. cmdLineP,
  96. loadDirectoryPath,
  97. uninitializedDataLength,
  98. NLMFileHandle,
  99. readRoutineP,
  100. customDataOffset,
  101. customDataSize,
  102. @_kNLMInfo,
  103. @_pasStart);
  104. end;
  105. (*
  106. procedure _Prelude; assembler; export; [alias:'_Prelude'];
  107. asm
  108. pushl %ebp
  109. movl %esp,%ebp
  110. pushl %edi
  111. pushl %esi
  112. pushl %ebx
  113. movl 0x14(%ebp),%edi
  114. movl 0x18(%ebp),%esi
  115. movl %esi, __uninitializedDataSize
  116. movl 0x1c(%ebp),%ebx
  117. movl 0x20(%ebp),%ecx
  118. movl 0x28(%ebp),%eax
  119. pushl $_pasStart
  120. pushl $_kNLMInfo
  121. pushl %eax
  122. movl 0x24(%ebp),%edx // 1b7f6
  123. pushl %edx
  124. pushl %ecx
  125. pushl %ebx
  126. pushl %esi // uninitialized data size
  127. pushl %edi
  128. movl 0x10(%ebp),%edx
  129. pushl %edx
  130. movl 0xc(%ebp),%edx
  131. pushl %edx
  132. movl 0x8(%ebp),%edx
  133. pushl %edx
  134. call _StartNLM
  135. test %eax,%eax
  136. jne .Lx1
  137. xorl %eax,%eax // dont know why this is needed ?
  138. .Lx1:
  139. lea 0xfffffff4(%ebp),%esp
  140. popl %ebx
  141. popl %esi
  142. popl %edi
  143. movl %ebp,%esp
  144. popl %ebp
  145. ret
  146. end;
  147. *)
  148. //# the global stop-function
  149. // fpc will generate an (unneeded) stack frame here, gcc does not
  150. (*
  151. procedure _Stop; cdecl; export; [alias:'_Stop'];
  152. begin
  153. _TerminateNLM (@_kNLMInfo,0,5);
  154. end;
  155. *)
  156. procedure _Stop; cdecl; assembler; [alias:'_Stop'];
  157. asm
  158. pushl $0x5
  159. pushl $0x0
  160. movl _kNLMInfo,%edx
  161. pushl %edx
  162. call _TerminateNLM
  163. addl $0x0c,%esp
  164. ret
  165. end;
  166. end.