si_c21g.inc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. {$asmmode att}
  29. { Some helpers }
  30. {$ifdef FPC_PIC}
  31. function fpc0geteipasebx : pointer; compilerproc; nostackframe; assembler;
  32. asm
  33. movl (%esp),%ebx
  34. ret
  35. end;
  36. {$endif FPC_PIC}
  37. procedure _init_fini_dummy; compilerproc; nostackframe; assembler;
  38. asm
  39. ret
  40. end;
  41. procedure gmon_mcleanup; cdecl; external name '_mcleanup';
  42. procedure gmon_monstartup (main,etext : pointer);cdecl;external name 'monstartup';
  43. procedure libc_start_main; external name '__libc_start_main';
  44. procedure PASCALMAIN; external name 'PASCALMAIN';
  45. var
  46. dlexitproc: pointer; { atexit from loader }
  47. gmon_start : record end;external name 'PASCALMAIN';
  48. gmon_etext : record end;external name '_etext';
  49. gmon_monstarted: longint = 0;
  50. {******************************************************************************
  51. Process start/halt
  52. ******************************************************************************}
  53. procedure _FPC_libc21_gprof_gmon_start; public name '_FPC_libc21_gprof_gmon_start';
  54. begin
  55. if gmon_monstarted=0 then
  56. begin
  57. inc(gmon_monstarted);
  58. gmon_monstartup(@gmon_start,@gmon_etext);
  59. end;
  60. PASCALMAIN;
  61. end;
  62. procedure _FPC_libc21_gprof_start; assembler; nostackframe; public name '_start';
  63. asm
  64. xorl %ebp,%ebp
  65. { First locate the start of the environment variables }
  66. popl %ecx { Get argc in ecx }
  67. movl %esp,%ebx { Esp now points to the arguments }
  68. leal 4(%esp,%ecx,4),%eax { The start of the environment is: esp+4*eax+4 }
  69. andl $0xfffffff0,%esp { Align stack to 16 bytes }
  70. {$ifdef FPC_PIC}
  71. pushl %edx
  72. pushl %ebx
  73. pushl %ecx
  74. call fpc0geteipasebx
  75. addl $_GLOBAL_OFFSET_TABLE_,%ebx
  76. movl dlexitproc@GOT(%ebx),%ecx
  77. movl %edx,(%ecx)
  78. movl operatingsystem_parameter_envp@GOT(%ebx),%ecx
  79. movl %eax,(%ecx)
  80. movl operatingsystem_parameter_argc@GOT(%ebx),%edx
  81. popl %ecx
  82. movl %ecx,(%edx)
  83. movl operatingsystem_parameter_argv@GOT(%ebx),%edx
  84. popl %ebx
  85. movl %ebx,(%edx)
  86. popl %edx
  87. {$else FPC_PIC}
  88. movl %edx, dlexitproc
  89. movl %eax,operatingsystem_parameter_envp
  90. movl %ecx,operatingsystem_parameter_argc
  91. movl %ebx,operatingsystem_parameter_argv
  92. {$endif FPC_PIC}
  93. { Save initial stackpointer }
  94. {$ifdef FPC_PIC}
  95. pushl %ebx
  96. call fpc0geteipasebx
  97. addl $_GLOBAL_OFFSET_TABLE_,%ebx
  98. movl initialstkptr@GOT(%ebx),%ebx
  99. movl %esp,(%ebx)
  100. popl %ebx
  101. {$else FPC_PIC}
  102. movl %esp,initialstkptr
  103. {$endif FPC_PIC}
  104. { int __libc_start_main(
  105. int *(main) (int, char * *, char * *),
  106. int argc,
  107. char * * ubp_av,
  108. void (*init) (void),
  109. void (*fini) (void),
  110. void (*rtld_fini) (void),
  111. void (* stack_end)); }
  112. pushl %ebp { padding }
  113. pushl %esp { stack_end }
  114. pushl %edx { function to be registered with
  115. atexit(), passed by loader }
  116. pushl $_init_fini_dummy
  117. pushl $_init_fini_dummy
  118. pushl %ebx { Push second argument: argv. }
  119. pushl %ecx { Push first argument: argc. }
  120. pushl $_FPC_libc21_gprof_gmon_start
  121. call libc_start_main
  122. hlt
  123. end;
  124. procedure _FPC_libc21_gprof_haltproc(e:longint);cdecl;public name '_haltproc';
  125. begin
  126. if gmon_monstarted=1 then
  127. begin
  128. dec(gmon_monstarted);
  129. gmon_mcleanup;
  130. end;
  131. asm
  132. .Lhaltproc:
  133. {$ifdef FPC_PIC}
  134. call fpc0geteipasebx
  135. addl $_GLOBAL_OFFSET_TABLE_,%ebx
  136. movl dlexitproc@GOT(%ebx),%eax
  137. movl (%eax),%eax
  138. {$else FPC_PIC}
  139. movl dlexitproc,%eax
  140. {$endif FPC_PIC}
  141. testl %eax,%eax
  142. je .Lnodlexitproc
  143. call *%eax
  144. .Lnodlexitproc:
  145. movl syscall_nr_exit_group,%eax
  146. {$ifdef FPC_PIC}
  147. call fpc0geteipasebx
  148. addl $_GLOBAL_OFFSET_TABLE_,%ebx
  149. movl ExitCode@GOT(%ebx),%ebx
  150. {$if sizeof(ExitCode)=2}
  151. movzwl (%ebx),%ebx
  152. {$else}
  153. mov (%ebx),%ebx
  154. {$endif}
  155. {$else FPC_PIC}
  156. {$if sizeof(ExitCode)=2}
  157. movzwl ExitCode,%ebx
  158. {$else}
  159. mov ExitCode,%ebx
  160. {$endif}
  161. {$endif FPC_PIC}
  162. int $0x80
  163. movl syscall_nr_exit,%eax
  164. {$ifdef FPC_PIC}
  165. call fpc0geteipasebx
  166. addl $_GLOBAL_OFFSET_TABLE_,%ebx
  167. movl ExitCode@GOT(%ebx),%ebx
  168. {$if sizeof(ExitCode)=2}
  169. movzwl (%ebx),%ebx
  170. {$else}
  171. mov (%ebx),%ebx
  172. {$endif}
  173. {$else FPC_PIC}
  174. {$if sizeof(ExitCode)=2}
  175. movzwl ExitCode,%ebx
  176. {$else}
  177. mov ExitCode,%ebx
  178. {$endif}
  179. {$endif FPC_PIC}
  180. int $0x80
  181. jmp .Lhaltproc
  182. end;
  183. end;