si_c.inc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. var
  29. libc_environ: pchar; external name '__environ';
  30. libc_fpu_control: word; external name '__fpu_control';
  31. libc_init_proc: procedure; external name '_init';
  32. libc_fini_proc: procedure; external name '_fini';
  33. procedure libc_atexit; external name '__libc_atexit';
  34. procedure libc_exit; external name '__libc_exit';
  35. procedure libc_init; external name '__libc_init';
  36. procedure libc_setfpucw; external name '__setfpucw';
  37. procedure libc_start_main; external name '__libc_start_main';
  38. procedure PASCALMAIN; external name 'PASCALMAIN';
  39. Procedure fpc_geteipasebx;[external name 'fpc_geteipasebx'];
  40. {******************************************************************************
  41. C library start/halt
  42. ******************************************************************************}
  43. {$asmmode ATT}
  44. procedure _FPC_libc_start; assembler; nostackframe; public name '_start';
  45. var
  46. _ebx: LongInt;
  47. _ecx: LongInt;
  48. _libc_init_proc: LongInt;
  49. asm
  50. { First locate the start of the environment variables }
  51. popl %ecx { Get argc in ecx }
  52. {$ifdef FPC_PIC}
  53. movl %esp,_ebx { Points to the arguments }
  54. movl %ecx,_ecx
  55. {$else FPC_PIC}
  56. movl %esp,%ebx { Points to the arguments }
  57. {$endif FPC_PIC}
  58. leal 4(%esp,%ecx,4),%eax { The start of the environment is: esp+4*eax+8 }
  59. andl $0xfffffff8,%esp { Align stack }
  60. {$ifdef FPC_PIC}
  61. pushl %ecx
  62. call fpc_geteipasebx
  63. addl $_GLOBAL_OFFSET_TABLE_,%ebx
  64. movl operatingsystem_parameter_envp@GOT(%ebx),%ecx
  65. movl %eax,(%ecx)
  66. movl libc_environ@GOT(%ebx),%ecx
  67. movl %eax,(%ecx)
  68. pushl %eax
  69. movl operatingsystem_parameter_argc@GOT(%ebx),%ecx
  70. movl _ecx,%eax
  71. movl %eax,(%ecx)
  72. movl operatingsystem_parameter_argv@GOT(%ebx),%ecx
  73. movl _ebx,%eax
  74. movl %eax,(%ecx)
  75. popl %eax
  76. popl %ecx
  77. movl _ebx,%ebx
  78. {$else FPC_PIC}
  79. movl %eax,operatingsystem_parameter_envp { Move the environment pointer }
  80. movl %ecx,operatingsystem_parameter_argc { Move the argument counter }
  81. movl %ebx,operatingsystem_parameter_argv { Move the argument pointer }
  82. movl %eax,libc_environ { libc environ }
  83. {$endif FPC_PIC}
  84. pushl %eax
  85. pushl %ebx
  86. pushl %ecx
  87. call libc_init { init libc }
  88. {$ifdef FPC_PIC}
  89. pushl %ecx
  90. pushl %ebx
  91. call fpc_geteipasebx
  92. addl $_GLOBAL_OFFSET_TABLE_,%ebx
  93. movl libc_init_proc@GOT(%ebx),%ecx
  94. movl (%ecx),%ecx
  95. movl %ecx,_libc_init_proc
  96. popl %ebx
  97. popl %ecx
  98. {$else FPC_PIC}
  99. movzwl libc_fpu_control,%eax
  100. {$endif FPC_PIC}
  101. pushl %eax
  102. call libc_setfpucw
  103. popl %eax
  104. pushl $libc_fini_proc
  105. call libc_atexit
  106. popl %eax
  107. {$ifdef FPC_PIC}
  108. call _libc_init_proc
  109. {$else FPC_PIC}
  110. call libc_init_proc
  111. {$endif FPC_PIC}
  112. popl %eax
  113. popl %eax
  114. { Save initial stackpointer }
  115. {$ifdef FPC_PIC}
  116. pushl %ecx
  117. pushl %ebx
  118. call fpc_geteipasebx
  119. addl $_GLOBAL_OFFSET_TABLE_,%ebx
  120. movl initialstkptr@GOT(%ebx),%ecx
  121. movl %esp,(%ecx)
  122. popl %ebx
  123. popl %ecx
  124. {$else FPC_PIC}
  125. movl %esp,initialstkptr
  126. {$endif FPC_PIC}
  127. xorl %ebp,%ebp
  128. call PASCALMAIN { start the program }
  129. end;
  130. procedure _FPC_libc_haltproc; assembler; nostackframe; public name '_haltproc';
  131. asm
  132. .Lhaltproc:
  133. {$ifdef FPC_PIC}
  134. call fpc_geteipasebx
  135. addl $_GLOBAL_OFFSET_TABLE_,%ebx
  136. movl ExitCode@GOT(%ebx),%ebx
  137. {$if sizeof(ExitCode)=2}
  138. movzwl (%ebx),%ebx
  139. {$else}
  140. mov (%ebx),%ebx
  141. {$endif}
  142. {$else FPC_PIC}
  143. {$if sizeof(ExitCode)=2}
  144. movzwl ExitCode,%ebx
  145. {$else}
  146. mov ExitCode,%ebx
  147. {$endif}
  148. {$endif FPC_PIC}
  149. pushl %ebx
  150. call libc_exit
  151. xorl %eax,%eax
  152. incl %eax { eax=1, exit call }
  153. popl %ebx
  154. int $0x80
  155. jmp .Lhaltproc
  156. end;