si_c21g.inc 3.8 KB

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