si_c.inc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. asm
  46. { First locate the start of the environment variables }
  47. popl %ecx { Get argc in ecx }
  48. {$ifdef FPC_PIC}
  49. movl %esp,%ebp { Points to the arguments }
  50. movl %ecx,%esi
  51. {$else FPC_PIC}
  52. movl %esp,%ebx { Points to the arguments }
  53. {$endif FPC_PIC}
  54. leal 4(%esp,%ecx,4),%eax { The start of the environment is: esp+4*eax+8 }
  55. andl $0xfffffff8,%esp { Align stack }
  56. {$ifdef FPC_PIC}
  57. pushl %ecx
  58. call fpc_geteipasebx
  59. addl $_GLOBAL_OFFSET_TABLE_,%ebx
  60. movl operatingsystem_parameter_envp@GOT(%ebx),%ecx
  61. movl %eax,(%ecx)
  62. movl libc_environ@GOT(%ebx),%ecx
  63. movl %eax,(%ecx)
  64. pushl %eax
  65. movl operatingsystem_parameter_argc@GOT(%ebx),%ecx
  66. movl %esi,%eax
  67. movl %eax,(%ecx)
  68. movl operatingsystem_parameter_argv@GOT(%ebx),%ecx
  69. movl %ebp,%eax
  70. movl %eax,(%ecx)
  71. popl %eax
  72. popl %ecx
  73. movl %ebp,%ebx
  74. {$else FPC_PIC}
  75. movl %eax,operatingsystem_parameter_envp { Move the environment pointer }
  76. movl %ecx,operatingsystem_parameter_argc { Move the argument counter }
  77. movl %ebx,operatingsystem_parameter_argv { Move the argument pointer }
  78. movl %eax,libc_environ { libc environ }
  79. {$endif FPC_PIC}
  80. pushl %eax
  81. pushl %ebx
  82. pushl %ecx
  83. call libc_init { init libc }
  84. {$ifdef FPC_PIC}
  85. pushl %ecx
  86. pushl %ebx
  87. call fpc_geteipasebx
  88. addl $_GLOBAL_OFFSET_TABLE_,%ebx
  89. movl libc_init_proc@GOT(%ebx),%ecx
  90. movl (%ecx),%edi
  91. popl %ebx
  92. popl %ecx
  93. {$else FPC_PIC}
  94. movzwl libc_fpu_control,%eax
  95. {$endif FPC_PIC}
  96. pushl %eax
  97. call libc_setfpucw
  98. popl %eax
  99. pushl $libc_fini_proc
  100. call libc_atexit
  101. popl %eax
  102. {$ifdef FPC_PIC}
  103. call *%edi
  104. {$else FPC_PIC}
  105. call libc_init_proc
  106. {$endif FPC_PIC}
  107. popl %eax
  108. popl %eax
  109. { Save initial stackpointer }
  110. {$ifdef FPC_PIC}
  111. pushl %ecx
  112. pushl %ebx
  113. call fpc_geteipasebx
  114. addl $_GLOBAL_OFFSET_TABLE_,%ebx
  115. movl initialstkptr@GOT(%ebx),%ecx
  116. movl %esp,(%ecx)
  117. popl %ebx
  118. popl %ecx
  119. {$else FPC_PIC}
  120. movl %esp,initialstkptr
  121. {$endif FPC_PIC}
  122. xorl %ebp,%ebp
  123. call PASCALMAIN { start the program }
  124. end;
  125. procedure _FPC_libc_haltproc; assembler; nostackframe; public name '_haltproc';
  126. asm
  127. .Lhaltproc:
  128. {$ifdef FPC_PIC}
  129. call fpc_geteipasebx
  130. addl $_GLOBAL_OFFSET_TABLE_,%ebx
  131. movl ExitCode@GOT(%ebx),%ebx
  132. {$if sizeof(ExitCode)=2}
  133. movzwl (%ebx),%ebx
  134. {$else}
  135. mov (%ebx),%ebx
  136. {$endif}
  137. {$else FPC_PIC}
  138. {$if sizeof(ExitCode)=2}
  139. movzwl ExitCode,%ebx
  140. {$else}
  141. mov ExitCode,%ebx
  142. {$endif}
  143. {$endif FPC_PIC}
  144. pushl %ebx
  145. call libc_exit
  146. xorl %eax,%eax
  147. incl %eax { eax=1, exit call }
  148. popl %ebx
  149. int $0x80
  150. jmp .Lhaltproc
  151. end;