si_uc.inc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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/uClibc startup code for Free Pascal
  13. taken from uClibc/sysdeps/linux/i386/crt1.S
  14. %edx Contains a function pointer to be registered with `atexit'.
  15. This is how the dynamic linker arranges to have DT_FINI
  16. functions called for shared libraries that have been loaded
  17. before this code runs.
  18. Stack layout at program start:
  19. nil
  20. envn
  21. ....
  22. .... ENVIRONMENT VARIABLES
  23. env1
  24. env0
  25. nil
  26. argn
  27. ....
  28. .... COMMAND LINE OPTIONS
  29. arg1
  30. arg0
  31. argc <--- esp
  32. }
  33. var
  34. dlexitproc: pointer; { atexit from loader }
  35. procedure uclibc_init; external name '__uClibc_init';
  36. procedure uclibc_fini; external name '__uClibc_fini';
  37. procedure uclibc_exit; external name 'exit';
  38. procedure uclibc_main; external name '__uClibc_main';
  39. procedure PASCALMAIN; external name 'PASCALMAIN';
  40. { Some helpers }
  41. {$ifdef FPC_PIC}
  42. function get3eipasebx : pointer; compilerproc; nostackframe; assembler;
  43. asm
  44. movl (%esp),%ebx
  45. ret
  46. end;
  47. {$endif FPC_PIC}
  48. procedure _init_fini_dummy; compilerproc; nostackframe; assembler;
  49. asm
  50. ret
  51. end;
  52. {******************************************************************************
  53. glibc 2.1 lib + profiling start/halt
  54. ******************************************************************************}
  55. {$asmmode ATT}
  56. procedure _FPC_libc21_start; assembler; nostackframe; public name '_start';
  57. asm
  58. xorl %ebp,%ebp
  59. { First locate the start of the environment variables }
  60. popl %ecx { Get argc in ecx }
  61. movl %esp,%ebx { Esp now points to the arguments }
  62. leal 4(%esp,%ecx,4),%eax { The start of the environment is: esp+4*eax+4 }
  63. andl $0xfffffff8,%esp { Align stack }
  64. {$ifdef FPC_PIC}
  65. pushl %edx
  66. pushl %ebx
  67. pushl %ecx
  68. call get3eipasebx
  69. addl $_GLOBAL_OFFSET_TABLE_,%ebx
  70. movl dlexitproc@GOT(%ebx),%ecx
  71. movl %edx,(%ecx)
  72. movl operatingsystem_parameter_envp@GOT(%ebx),%ecx
  73. movl %eax,(%ecx)
  74. movl operatingsystem_parameter_argc@GOT(%ebx),%edx
  75. popl %ecx
  76. movl %ecx,(%edx)
  77. movl operatingsystem_parameter_argv@GOT(%ebx),%edx
  78. popl %ebx
  79. movl %ebx,(%edx)
  80. popl %edx
  81. {$else FPC_PIC}
  82. movl %edx, dlexitproc
  83. movl %eax,operatingsystem_parameter_envp
  84. movl %ecx,operatingsystem_parameter_argc
  85. movl %ebx,operatingsystem_parameter_argv
  86. {$endif FPC_PIC}
  87. { Save initial stackpointer }
  88. {$ifdef FPC_PIC}
  89. pushl %ebx
  90. call get3eipasebx
  91. addl $_GLOBAL_OFFSET_TABLE_,%ebx
  92. movl initialstkptr@GOT(%ebx),%ebx
  93. movl %esp,(%ebx)
  94. popl %ebx
  95. {$else FPC_PIC}
  96. movl %esp,initialstkptr
  97. {$endif FPC_PIC}
  98. { int __libc_start_main(
  99. int *(main) (int, char * *, char * *),
  100. int argc,
  101. char * * ubp_av,
  102. void (*init) (void),
  103. void (*fini) (void),
  104. void (*rtld_fini) (void),
  105. void (* stack_end)); }
  106. pushl %ebp { padding }
  107. pushl %esp { stack_end }
  108. pushl %edx { function to be registered with
  109. atexit(), passed by loader }
  110. pushl $_init_fini_dummy
  111. pushl $_init_fini_dummy
  112. pushl %ebx { Push second argument: argv. }
  113. pushl %ecx { Push first argument: argc. }
  114. pushl $PASCALMAIN
  115. call uclibc_main
  116. hlt
  117. end;
  118. procedure _FPC_libc21_haltproc; assembler; nostackframe; public name '_haltproc';
  119. asm
  120. .Lhaltproc:
  121. {$ifdef FPC_PIC}
  122. call get3eipasebx
  123. addl $_GLOBAL_OFFSET_TABLE_,%ebx
  124. movl ExitCode@GOT(%ebx),%ebx
  125. {$if sizeof(ExitCode)=2}
  126. movzwl (%ebx),%ebx
  127. {$else}
  128. mov (%ebx),%ebx
  129. {$endif}
  130. {$else FPC_PIC}
  131. {$if sizeof(ExitCode)=2}
  132. movzwl ExitCode,%ebx
  133. {$else}
  134. mov ExitCode,%ebx
  135. {$endif}
  136. {$endif FPC_PIC}
  137. pushl %ebx
  138. call uclibc_exit
  139. xorl %eax,%eax
  140. incl %eax { eax=1, exit call }
  141. popl %ebx
  142. int $0x80
  143. jmp .Lhaltproc
  144. end;