si_prc.inc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. procedure fpc_geteipasebxlocal; [external name 'fpc_geteipasebx'];
  36. {$endif}
  37. {$ifndef FPC_USE_LIBC}
  38. procedure InitTLS; [external name 'FPC_INITTLS'];
  39. {$endif}
  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 $0xfffffff0,%esp { Align stack to 16 bytes }
  47. {$ifdef FPC_PIC}
  48. pushl %ebx
  49. pushl %ecx
  50. call fpc_geteipasebxlocal
  51. addl $_GLOBAL_OFFSET_TABLE_,%ebx
  52. movl operatingsystem_parameter_envp@GOT(%ebx),%ecx
  53. movl %eax,(%ecx)
  54. movl operatingsystem_parameter_argc@GOT(%ebx),%edx
  55. popl %ecx
  56. movl %ecx,(%edx)
  57. movl operatingsystem_parameter_argv@GOT(%ebx),%edx
  58. popl %ebx
  59. movl %ebx,(%edx)
  60. {$else FPC_PIC}
  61. movl %eax,operatingsystem_parameter_envp
  62. movl %ecx,operatingsystem_parameter_argc
  63. movl %ebx,operatingsystem_parameter_argv
  64. {$endif FPC_PIC}
  65. { Initialize FPU }
  66. call SysResetFPU
  67. { Save initial stackpointer }
  68. {$ifdef FPC_PIC}
  69. pushl %ebx
  70. call fpc_geteipasebxlocal
  71. addl $_GLOBAL_OFFSET_TABLE_,%ebx
  72. movl initialstkptr@GOT(%ebx),%ebx
  73. movl %esp,(%ebx)
  74. popl %ebx
  75. {$else FPC_PIC}
  76. movl %esp,initialstkptr
  77. {$endif FPC_PIC}
  78. {$if (FPC_FULLVERSION>30200) and not defined(FPC_USE_LIBC)}
  79. call InitTLS
  80. {$endif FPC_FULLVERSION>30200 and not FPC_USE_LIBC}
  81. xorl %ebp,%ebp
  82. call PASCALMAIN
  83. end;
  84. procedure _FPC_dynamic_proc_start; assembler; nostackframe; public name '_dynamic_start';
  85. asm
  86. {$ifdef FPC_PIC}
  87. pushl %ebx
  88. call fpc_geteipasebxlocal
  89. addl $_GLOBAL_OFFSET_TABLE_,%ebx
  90. movl dlexitproc@GOT(%ebx),%ebx
  91. movl %edx,(%ebx)
  92. popl %ebx
  93. {$else}
  94. movl %edx, dlexitproc
  95. {$endif}
  96. jmp _FPC_proc_start
  97. end;
  98. procedure _FPC_proc_haltproc(e:longint); cdecl; assembler; public name '_haltproc';
  99. asm
  100. //addl $12, %esp { align stack back to 16 bytes }
  101. {$ifdef FPC_PIC}
  102. call fpc_geteipasebxlocal
  103. addl $_GLOBAL_OFFSET_TABLE_,%ebx
  104. movl dlexitproc@GOT(%ebx),%eax
  105. movl (%eax),%eax
  106. {$else FPC_PIC}
  107. movl dlexitproc,%eax
  108. {$endif FPC_PIC}
  109. testl %eax,%eax
  110. je .Lnodlexitproc
  111. call *%eax
  112. .Lnodlexitproc:
  113. movl syscall_nr_exit_group,%eax
  114. movl e,%ebx
  115. int $0x80
  116. movl syscall_nr_exit,%eax
  117. movl e,%ebx
  118. int $0x80
  119. jmp .Lnodlexitproc
  120. end;