si_prc.inc 3.8 KB

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