si_c21g.inc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. gmon_etext: longint; external name '_etext';
  30. gmon_start: longint; external name '_start';
  31. gmon_mcleanup: procedure; external name '_mcleanup';
  32. libc21_fpc_ret, libc21_fpc_ret_ebx: ptrint; { return address to libc }
  33. libc21_fpc_ret_esi, libc21_fpc_ret_edi: ptrint;
  34. gmon_monstarted: longint = 0;
  35. procedure gmon_monstartup; external name 'monstartup';
  36. procedure libc_atexit; external name 'atexit';
  37. procedure libc_exit; external name '__libc_exit';
  38. procedure libc_init; external name '__libc_init';
  39. procedure libc_setfpucw; external name '__setfpucw';
  40. procedure libc_start_main; external name '__libc_start_main';
  41. procedure PASCALMAIN; external name 'PASCALMAIN';
  42. {******************************************************************************
  43. glibc 2.1 lib + profiling start/halt
  44. ******************************************************************************}
  45. {$asmmode ATT}
  46. procedure _FPC_libc21_gprof_gmon_start; assembler; nostackframe;
  47. asm
  48. pushl %ebp
  49. movl gmon_monstarted,%eax
  50. leal 0x1(%eax),%edx
  51. movl %esp,%ebp
  52. movl %edx,gmon_monstarted
  53. testl %eax,%eax
  54. jnz .Lnomonstart
  55. pushl $gmon_etext { Initialize gmon }
  56. pushl $gmon_start
  57. call gmon_monstartup
  58. addl $8,%esp
  59. pushl $gmon_mcleanup
  60. call libc_atexit
  61. addl $4,%esp
  62. .Lnomonstart:
  63. movl %ebp,%esp
  64. popl %ebp
  65. ret
  66. end;
  67. procedure _FPC_libc21_gprof_start; assembler; nostackframe; public name '_start';
  68. asm
  69. { First locate the start of the environment variables }
  70. popl %esi
  71. movl %eax,%edi
  72. movl %esp,%ebx { Points to the arguments }
  73. movl %esi,%eax
  74. incl %eax
  75. shll $2,%eax
  76. addl %esp,%eax
  77. andl $0xfffffff8,%esp { Align stack }
  78. movl %eax,operatingsystem_parameter_envp { Move the environment pointer }
  79. movl %esi,operatingsystem_parameter_argc { Move the argument counter }
  80. movl %ebx,operatingsystem_parameter_argv { Move the argument pointer }
  81. movl %edi,%eax
  82. xorl %ebp,%ebp
  83. pushl %eax
  84. pushl %esp
  85. pushl %edx
  86. pushl $.Lfini_dummy
  87. pushl $.Linit_dummy
  88. pushl %ebx
  89. pushl %esi
  90. pushl $.Lcmain
  91. call libc_start_main
  92. .Linit_dummy:
  93. .Lfini_dummy:
  94. ret
  95. { fake main routine which will be run from libc }
  96. .Lcmain:
  97. { save return address }
  98. popl %eax
  99. movl %eax,libc21_fpc_ret
  100. movl %ebx,libc21_fpc_ret_ebx
  101. movl %esi,libc21_fpc_ret_esi
  102. movl %edi,libc21_fpc_ret_edi
  103. pushl %eax
  104. call _FPC_libc21_gprof_gmon_start
  105. { Save initial stackpointer }
  106. movl %esp,initialstkptr
  107. { start the program }
  108. call PASCALMAIN
  109. hlt
  110. end;
  111. procedure _FPC_libc21_gprof_haltproc; assembler; nostackframe; public name '_haltproc';
  112. asm
  113. {$if sizeof(ExitCode)=2}
  114. movzwl ExitCode,%eax
  115. {$else}
  116. mov ExitCode,%eax
  117. {$endif}
  118. movl libc21_fpc_ret,%edx { return to libc }
  119. movl libc21_fpc_ret_ebx,%ebx
  120. movl libc21_fpc_ret_esi,%esi
  121. movl libc21_fpc_ret_edi,%edi
  122. push %edx
  123. ret
  124. end;