si_prc.inc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2005 by Michael Van Canneyt, Peter Vreman,
  4. & Daniel Mantione, members of the Free Pascal development team.
  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. {$asmmode att}
  12. {
  13. Linux ELF startup code for Free Pascal
  14. Stack layout at program start:
  15. nil
  16. envn
  17. ....
  18. .... ENVIRONMENT VARIABLES
  19. env1
  20. env0
  21. nil
  22. argn
  23. ....
  24. .... COMMAND LINE OPTIONS
  25. arg1
  26. arg0
  27. argc <--- esp
  28. }
  29. procedure PASCALMAIN; external name 'PASCALMAIN';
  30. {******************************************************************************
  31. Process start/halt
  32. ******************************************************************************}
  33. var
  34. dlexitproc : pointer;
  35. {$ifdef FPC_PIC}
  36. function fpc_geteipasebxlocal : pointer; [external name 'fpc_geteipasebx'];
  37. {$endif}
  38. procedure _FPC_proc_start; assembler; nostackframe; public name '_start';
  39. asm
  40. { First locate the start of the environment variables }
  41. popl %ecx { Get argc in ecx }
  42. movl %esp,%ebx { Esp now points to the arguments }
  43. leal 4(%esp,%ecx,4),%eax { The start of the environment is: esp+4*eax+4 }
  44. andl $0xfffffff8,%esp { Align stack }
  45. {$ifdef FPC_PIC}
  46. pushl %ebx
  47. pushl %ecx
  48. call fpc_geteipasebxlocal
  49. addl $_GLOBAL_OFFSET_TABLE_,%ebx
  50. movl operatingsystem_parameter_envp@GOT(%ebx),%ecx
  51. movl %eax,(%ecx)
  52. movl operatingsystem_parameter_argc@GOT(%ebx),%edx
  53. popl %ecx
  54. movl %ecx,(%edx)
  55. movl operatingsystem_parameter_argv@GOT(%ebx),%edx
  56. popl %ebx
  57. movl %ebx,(%edx)
  58. {$else FPC_PIC}
  59. movl %eax,operatingsystem_parameter_envp
  60. movl %ecx,operatingsystem_parameter_argc
  61. movl %ebx,operatingsystem_parameter_argv
  62. {$endif FPC_PIC}
  63. { Initialize FPU }
  64. call SysResetFPU
  65. { Save initial stackpointer }
  66. {$ifdef FPC_PIC}
  67. pushl %ebx
  68. call fpc_geteipasebxlocal
  69. addl $_GLOBAL_OFFSET_TABLE_,%ebx
  70. movl initialstkptr@GOT(%ebx),%ebx
  71. movl %esp,(%ebx)
  72. popl %ebx
  73. {$else FPC_PIC}
  74. movl %esp,initialstkptr
  75. {$endif FPC_PIC}
  76. xorl %ebp,%ebp
  77. call PASCALMAIN
  78. end;
  79. procedure _FPC_dynamic_proc_start; assembler; nostackframe; public name '_dynamic_start';
  80. asm
  81. {$ifdef FPC_PIC}
  82. pushl %ebx
  83. call fpc_geteipasebxlocal
  84. addl $_GLOBAL_OFFSET_TABLE_,%ebx
  85. movl dlexitproc@GOT(%ebx),%ebx
  86. movl %edx,(%ebx)
  87. popl %ebx
  88. {$else}
  89. movl %edx, dlexitproc
  90. {$endif}
  91. jmp _FPC_proc_start
  92. end;
  93. procedure _FPC_proc_haltproc; assembler; nostackframe; public name '_haltproc';
  94. asm
  95. .Lhaltproc:
  96. {$ifdef FPC_PIC}
  97. call fpc_geteipasebxlocal
  98. addl $_GLOBAL_OFFSET_TABLE_,%ebx
  99. movl dlexitproc@GOT(%ebx),%eax
  100. movl (%eax),%eax
  101. {$else FPC_PIC}
  102. movl dlexitproc,%eax
  103. {$endif FPC_PIC}
  104. testl %eax,%eax
  105. je .Lnodlexitproc
  106. call *%eax
  107. .Lnodlexitproc:
  108. movl syscall_nr_exit_group,%eax
  109. {$ifdef FPC_PIC}
  110. call fpc_geteipasebxlocal
  111. addl $_GLOBAL_OFFSET_TABLE_,%ebx
  112. movl ExitCode@GOT(%ebx),%ebx
  113. {$if sizeof(ExitCode)=2}
  114. movzwl (%ebx),%ebx
  115. {$else}
  116. mov (%ebx),%ebx
  117. {$endif}
  118. {$else FPC_PIC}
  119. {$if sizeof(ExitCode)=2}
  120. movzwl ExitCode,%ebx
  121. {$else}
  122. mov ExitCode,%ebx
  123. {$endif}
  124. {$endif FPC_PIC}
  125. int $0x80
  126. movl syscall_nr_exit,%eax
  127. {$ifdef FPC_PIC}
  128. call fpc_geteipasebxlocal
  129. addl $_GLOBAL_OFFSET_TABLE_,%ebx
  130. movl ExitCode@GOT(%ebx),%ebx
  131. {$if sizeof(ExitCode)=2}
  132. movzwl (%ebx),%ebx
  133. {$else}
  134. mov (%ebx),%ebx
  135. {$endif}
  136. {$else FPC_PIC}
  137. {$if sizeof(ExitCode)=2}
  138. movzwl ExitCode,%ebx
  139. {$else}
  140. mov ExitCode,%ebx
  141. {$endif}
  142. {$endif FPC_PIC}
  143. int $0x80
  144. jmp .Lhaltproc
  145. end;