si_prc.inc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. {******************************************************************************
  30. Process start/halt
  31. ******************************************************************************}
  32. var
  33. dlexitproc : pointer;
  34. {$ifdef FPC_PIC}
  35. function fpc_geteipasebxlocal : pointer; [external name 'fpc_geteipasebx'];
  36. {$endif}
  37. procedure InitTLS; [external name 'FPC_INITTLS'];
  38. { so far, I found no case where this is actually called, so it is a dummy so far (FK) }
  39. function ___tls_get_addr(p : pointer) : pointer; public name '___tls_get_addr';
  40. begin
  41. end;
  42. procedure _FPC_proc_start; assembler; nostackframe; public name '_start';
  43. asm
  44. { First locate the start of the environment variables }
  45. popl %ecx { Get argc in ecx }
  46. movl %esp,%ebx { Esp now points to the arguments }
  47. leal 4(%esp,%ecx,4),%eax { The start of the environment is: esp+4*eax+4 }
  48. andl $0xfffffff0,%esp { Align stack to 16 bytes }
  49. {$ifdef FPC_PIC}
  50. pushl %ebx
  51. pushl %ecx
  52. call fpc_geteipasebxlocal
  53. addl $_GLOBAL_OFFSET_TABLE_,%ebx
  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 %eax,operatingsystem_parameter_envp
  64. movl %ecx,operatingsystem_parameter_argc
  65. movl %ebx,operatingsystem_parameter_argv
  66. {$endif FPC_PIC}
  67. { Initialize FPU }
  68. call SysResetFPU
  69. { Save initial stackpointer }
  70. {$ifdef FPC_PIC}
  71. pushl %ebx
  72. call fpc_geteipasebxlocal
  73. addl $_GLOBAL_OFFSET_TABLE_,%ebx
  74. movl initialstkptr@GOT(%ebx),%ebx
  75. movl %esp,(%ebx)
  76. popl %ebx
  77. {$else FPC_PIC}
  78. movl %esp,initialstkptr
  79. {$endif FPC_PIC}
  80. {$if FPC_FULLVERSION>30200}
  81. call InitTLS
  82. {$endif FPC_FULLVERSION>30200}
  83. xorl %ebp,%ebp
  84. call PASCALMAIN
  85. end;
  86. procedure _FPC_dynamic_proc_start; assembler; nostackframe; public name '_dynamic_start';
  87. asm
  88. {$ifdef FPC_PIC}
  89. pushl %ebx
  90. call fpc_geteipasebxlocal
  91. addl $_GLOBAL_OFFSET_TABLE_,%ebx
  92. movl dlexitproc@GOT(%ebx),%ebx
  93. movl %edx,(%ebx)
  94. popl %ebx
  95. {$else}
  96. movl %edx, dlexitproc
  97. {$endif}
  98. jmp _FPC_proc_start
  99. end;
  100. procedure _FPC_proc_haltproc(e:longint); cdecl; assembler; public name '_haltproc';
  101. asm
  102. //addl $12, %esp { align stack back to 16 bytes }
  103. {$ifdef FPC_PIC}
  104. call fpc_geteipasebxlocal
  105. addl $_GLOBAL_OFFSET_TABLE_,%ebx
  106. movl dlexitproc@GOT(%ebx),%eax
  107. movl (%eax),%eax
  108. {$else FPC_PIC}
  109. movl dlexitproc,%eax
  110. {$endif FPC_PIC}
  111. testl %eax,%eax
  112. je .Lnodlexitproc
  113. call *%eax
  114. .Lnodlexitproc:
  115. movl syscall_nr_exit_group,%eax
  116. movl e,%ebx
  117. int $0x80
  118. movl syscall_nr_exit,%eax
  119. movl e,%ebx
  120. int $0x80
  121. jmp .Lnodlexitproc
  122. end;