si_c.inc 4.3 KB

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