si_prc.inc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 dlexitproc@GOT(%ebx),%ecx
  51. movl %edx,(%ecx)
  52. movl operatingsystem_parameter_envp@GOT(%ebx),%ecx
  53. movl %eax,(%ecx)
  54. movl operatingsystem_parameter_argc@GOT(%ebx),%edx
  55. popl %ecx
  56. movl %ecx,(%edx)
  57. movl operatingsystem_parameter_argv@GOT(%ebx),%edx
  58. popl %ebx
  59. movl %ebx,(%edx)
  60. {$else FPC_PIC}
  61. movl %edx, dlexitproc
  62. movl %eax,operatingsystem_parameter_envp
  63. movl %ecx,operatingsystem_parameter_argc
  64. movl %ebx,operatingsystem_parameter_argv
  65. {$endif FPC_PIC}
  66. { Initialize FPU }
  67. call SysResetFPU
  68. { Save initial stackpointer }
  69. {$ifdef FPC_PIC}
  70. pushl %ebx
  71. call fpc_geteipasebxlocal
  72. addl $_GLOBAL_OFFSET_TABLE_,%ebx
  73. movl initialstkptr@GOT(%ebx),%ebx
  74. movl %esp,(%ebx)
  75. popl %ebx
  76. {$else FPC_PIC}
  77. movl %esp,initialstkptr
  78. {$endif FPC_PIC}
  79. xorl %ebp,%ebp
  80. call PASCALMAIN
  81. end;
  82. procedure _FPC_proc_haltproc; assembler; nostackframe; public name '_haltproc';
  83. asm
  84. .Lhaltproc:
  85. {$ifdef FPC_PIC}
  86. call fpc_geteipasebxlocal
  87. addl $_GLOBAL_OFFSET_TABLE_,%ebx
  88. movl dlexitproc@GOT(%ebx),%eax
  89. movl (%eax),%eax
  90. {$else FPC_PIC}
  91. movl dlexitproc,%eax
  92. {$endif FPC_PIC}
  93. testl %eax,%eax
  94. je .Lnodlexitproc
  95. call *%eax
  96. .Lnodlexitproc:
  97. movl syscall_nr_exit_group,%eax
  98. {$ifdef FPC_PIC}
  99. call fpc_geteipasebxlocal
  100. addl $_GLOBAL_OFFSET_TABLE_,%ebx
  101. movl ExitCode@GOT(%ebx),%ebx
  102. {$if sizeof(ExitCode)=2}
  103. movzwl (%ebx),%ebx
  104. {$else}
  105. mov (%ebx),%ebx
  106. {$endif}
  107. {$else FPC_PIC}
  108. {$if sizeof(ExitCode)=2}
  109. movzwl ExitCode,%ebx
  110. {$else}
  111. mov ExitCode,%ebx
  112. {$endif}
  113. {$endif FPC_PIC}
  114. int $0x80
  115. movl syscall_nr_exit,%eax
  116. {$ifdef FPC_PIC}
  117. call fpc_geteipasebxlocal
  118. addl $_GLOBAL_OFFSET_TABLE_,%ebx
  119. movl ExitCode@GOT(%ebx),%ebx
  120. {$if sizeof(ExitCode)=2}
  121. movzwl (%ebx),%ebx
  122. {$else}
  123. mov (%ebx),%ebx
  124. {$endif}
  125. {$else FPC_PIC}
  126. {$if sizeof(ExitCode)=2}
  127. movzwl ExitCode,%ebx
  128. {$else}
  129. mov ExitCode,%ebx
  130. {$endif}
  131. {$endif FPC_PIC}
  132. int $0x80
  133. jmp .Lhaltproc
  134. end;