si_crt.inc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. FreeBSD/x86 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. libc_fpc_ret, libc_fpc_ret_ebx, libc_fpc_ret_ebp: ptrint; { return address to libc }
  30. __progname : pchar; cvar;
  31. procedure get_rtld_cleanup; external name 'get_rtld_cleanup';
  32. procedure atexit; external name 'atexit';
  33. procedure init_tls; external name '_init_tls';
  34. procedure libc_start_main; external name '__libc_start_main';
  35. procedure PASCALMAIN; external name 'PASCALMAIN';
  36. var progname : pchar cvar; // = '';
  37. __fpucw : word; cvar; // = 0x1332;
  38. {******************************************************************************
  39. libc 2.1 library start/halt
  40. ******************************************************************************}
  41. {$asmmode ATT}
  42. procedure _FPC_libc_start; assembler; nostackframe; public name '_start';
  43. asm
  44. pushl %ebp
  45. movl %esp,%ebp
  46. subl $40,%esp
  47. call get_rtld_cleanup
  48. movl %eax,%edx
  49. pushl %edi
  50. pushl %esi
  51. pushl %ebx
  52. #APP
  53. movl %edx,%edx
  54. #NO_APP
  55. leal 8(%ebp),%edi
  56. movl %edi,operatingsystem_parameter_argv
  57. mov -4(%edi),%eax
  58. movl %eax,operatingsystem_parameter_argc
  59. movl 4(%ebp),%ebx
  60. leal 12(%ebp,%ebx,4),%esi
  61. movl %esi,operatingsystem_parameter_envp
  62. movl %esi,environ
  63. testl %ebx,%ebx
  64. jle .L2
  65. movl 8(%ebp),%eax
  66. testl %eax,%eax
  67. je .L2
  68. movl %eax,__progname
  69. cmpb $0,(%eax)
  70. je .L2
  71. .p2align 2,0x90
  72. .L6:
  73. cmpb $47,(%eax)
  74. jne .L5
  75. leal 1(%eax),%ecx
  76. movl %ecx,__progname
  77. .L5:
  78. incl %eax
  79. cmpb $0,(%eax)
  80. jne .L6
  81. .L2:
  82. movl $_DYNAMIC,%eax
  83. testl %eax,%eax
  84. je .LTLS
  85. pushl %edx
  86. call atexit
  87. addl $4,%esp
  88. .L9:
  89. pushl $_fini
  90. call atexit
  91. call _init
  92. # pushl %esi
  93. # pushl %edi
  94. # pushl %ebx
  95. # call main
  96. # pushl %eax
  97. # call exit
  98. finit /* initialize fpu */
  99. fwait
  100. fldcw ___fpucw
  101. xorl %ebp,%ebp
  102. call main
  103. pushl %eax
  104. jmp _haltproc
  105. .LTLS:
  106. call _init_tls
  107. jmp .L9
  108. get_rtld_cleanup:
  109. pushl %ebp
  110. movl %esp, %ebp
  111. subl $4, %esp
  112. #APP
  113. movl %edx,-4(%ebp)
  114. #NO_APP
  115. movl -4(%ebp), %eax
  116. leave
  117. ret
  118. end;
  119. procedure _FPC_libc_haltproc; assembler; nostackframe; public name '_haltproc';
  120. asm
  121. mov $1,%eax
  122. movzwl operatingsystem_result,%ebx
  123. pushl %ebx
  124. call .Lactualsyscall
  125. addl $4,%esp
  126. jmp _haltproc
  127. .Lactualsyscall:
  128. int $0x80
  129. jb .LErrorcode
  130. xor %ebx,%ebx
  131. ret
  132. .section .note.ABI-tag,"a",@progbits
  133. .p2align 2
  134. .type abitag, @object
  135. .size abitag, 24
  136. abitag:
  137. .long 8
  138. .long 4
  139. .long 1
  140. .string "FreeBSD"
  141. .long 900044
  142. .LErrorcode:
  143. mov %eax,%ebx
  144. mov $-1,%eax
  145. end;
  146. .file "cprt0.as"
  147. .ident "FreePascal 2.2.x series dynlinked"
  148. .section .rodata.str1.1,"aMS",@progbits,1
  149. .LC0:
  150. .string ""
  151. .data
  152. .p2align 2
  153. .globl __progname
  154. .type __progname,@object
  155. .size __progname,4
  156. __progname:
  157. .long .LC0
  158. .text
  159. .p2align 2,,3
  160. .type __fpucw,@object
  161. .size __fpucw,4
  162. .global __fpucw
  163. ___fpucw:
  164. .long 0x1332
  165. .globl ___fpc_brk_addr /* heap management */
  166. .type ___fpc_brk_addr,@object
  167. .size ___fpc_brk_addr,4
  168. ___fpc_brk_addr:
  169. .long 0
  170. .text
  171. .p2align 4,,15
  172. .globl _start
  173. .type _start,@function
  174. _start:
  175. .p2align 2,0x90
  176. .Lfe1:
  177. .size _start,.Lfe1-_start
  178. .comm environ,4,4
  179. .p2align 4,,15
  180. .type get_rtld_cleanup, @function
  181. .weak _DYNAMIC
  182. .ident "GCC: (GNU) 3.4.2 - FPC: 2.2.0"
  183. .bss
  184. .type __stkptr,@object
  185. .size __stkptr,4
  186. .global __stkptr
  187. __stkptr:
  188. .skip 4
  189. .type operatingsystem_parameters,@object
  190. .size operatingsystem_parameters,12
  191. operatingsystem_parameters:
  192. .skip 3*4
  193. .global operatingsystem_parameter_envp
  194. .global operatingsystem_parameter_argc
  195. .global operatingsystem_parameter_argv
  196. .set operatingsystem_parameter_envp,operatingsystem_parameters+0
  197. .set operatingsystem_parameter_argc,operatingsystem_parameters+4
  198. .set operatingsystem_parameter_argv,operatingsystem_parameters+8
  199. //.section .threadvar,"aw",@nobits
  200. .comm ___fpc_threadvar_offset,4