si_c.inc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 gas}
  29. {$L abitag.o}
  30. var
  31. libc_environ: PAnsiChar; external name '__environ';
  32. libc_fpu_control: word; external name '__fpu_control';
  33. libc_init_proc: procedure; external name '_init';
  34. libc_fini_proc: procedure; external name '_fini';
  35. fpc_ret,fpc_ret_rbp : pointer;
  36. procedure libc_atexit; external name '__libc_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 _FPC_libc_haltproc(e:longint); forward;
  42. procedure ___libc_csu_init; external name '__libc_csu_init';
  43. {$if FPC_FULLVERSION>30202}
  44. procedure __libc_csu_init; public name '__libc_csu_init'; assembler;
  45. asm
  46. .weak __libc_csu_init
  47. end;
  48. {$else FPC_FULLVERSION>30202}
  49. procedure __libc_csu_init; public name '__libc_csu_init'; assembler;
  50. asm
  51. end;
  52. {$endif FPC_FULLVERSION>30202}
  53. procedure ___libc_csu_fini; external name '__libc_csu_fini';
  54. {$if FPC_FULLVERSION>30202}
  55. procedure __libc_csu_fini; public name '__libc_csu_fini'; assembler;
  56. asm
  57. .weak __libc_csu_fini
  58. end;
  59. {$else FPC_FULLVERSION>30202}
  60. procedure __libc_csu_fini; public name '__libc_csu_fini'; assembler;
  61. asm
  62. end;
  63. {$endif FPC_FULLVERSION>30202}
  64. procedure main_stub; assembler; nostackframe;
  65. asm
  66. { save return address }
  67. popq %rax
  68. // stack alignment
  69. pushq %rax
  70. movq %rax,fpc_ret(%rip)
  71. movq %rbp,fpc_ret_rbp(%rip)
  72. pushq %rax
  73. {$ifdef FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  74. { Save initial stackpointer }
  75. movq SysInitEntryInformation@GOTPCREL(%rip),%rdi
  76. movq %rsp,TEntryInformation.OS.stkptr(%rdi)
  77. { store stack length }
  78. movq StackLength@GOTPCREL(%rip),%rax
  79. movq (%rax),%rax
  80. movq %rax,TEntryInformation.OS.stklen(%rdi)
  81. { store pointer to haltproc }
  82. movq _FPC_libc_haltproc@GOTPCREL(%rip),%rax
  83. movq %rax,TEntryInformation.OS.haltproc(%rdi)
  84. xorq %rbp,%rbp
  85. call SysEntry
  86. {$else FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  87. { Save initial stackpointer }
  88. movq initialstkptr@GOTPCREL(%rip),%rax
  89. movq %rsp,(%rax)
  90. { start the program }
  91. xorq %rbp,%rbp
  92. call PASCALMAIN@PLT
  93. {$endif FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  94. hlt
  95. end;
  96. procedure ini_dummy;
  97. begin
  98. end;
  99. {******************************************************************************
  100. C library start/halt
  101. ******************************************************************************}
  102. procedure _FPC_libc_start; assembler; nostackframe; public name '_start';
  103. asm
  104. { Clear the frame pointer. The ABI suggests this be done, to mark
  105. the outermost frame obviously. }
  106. xorq %rbp, %rbp
  107. { Extract the arguments as encoded on the stack and set up
  108. the arguments for __libc_start_main (int (*main) (int, char **, char **),
  109. int argc, char *argv,
  110. void (*init) (void), void (*fini) (void),
  111. void (*rtld_fini) (void), void *stack_end).
  112. The arguments are passed via registers and on the stack:
  113. main: %rdi
  114. argc: %rsi
  115. argv: %rdx
  116. init: %rcx
  117. fini: %r8
  118. rtld_fini: %r9
  119. stack_end: stack. }
  120. movq %rdx, %r9 { Address of the shared library termination
  121. function. }
  122. popq %rsi { Pop the argument count. }
  123. movq %rsp, %rdx { argv starts just at the current stack top. }
  124. {$ifdef FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  125. movq SysInitEntryInformation@GOTPCREL(%rip),%rcx
  126. { argc is longint in EntryInformation, thus use %edi register }
  127. movl %esi,TEntryInformation.OS.argc(%rcx)
  128. movq %rsp,TEntryInformation.OS.argv(%rcx)
  129. leaq 8(,%rsi,8),%rax
  130. addq %rsp,%rax
  131. movq %rax,TEntryInformation.OS.envp(%rcx)
  132. {$else FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  133. movq operatingsystem_parameter_argc@GOTPCREL(%rip),%rax
  134. movq %rsi,(%rax)
  135. movq operatingsystem_parameter_argv@GOTPCREL(%rip),%rax
  136. movq %rsp,(%rax)
  137. leaq 8(,%rsi,8),%rax
  138. addq %rsp,%rax
  139. movq operatingsystem_parameter_envp@GOTPCREL(%rip),%rcx
  140. movq %rax,(%rcx)
  141. {$endif FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  142. { Align the stack to a 16 byte boundary to follow the ABI. }
  143. andq $0xfffffffffffffff0, %rsp
  144. pushq %rax { Push garbage because we push 8 more bytes. }
  145. { Provide the highest stack address to the user code (for stacks
  146. which grow downwards). }
  147. pushq %rsp
  148. { Pass address of our own entry points to .fini and .init. }
  149. movq ___libc_csu_fini@GOTPCREL(%rip), %r8
  150. movq ___libc_csu_init@GOTPCREL(%rip), %rcx
  151. movq main_stub@GOTPCREL(%rip), %rdi
  152. { Call the user's main function, and exit with its value.
  153. But let the libc call main. }
  154. call libc_start_main@PLT
  155. hlt { Crash if somehow `exit' does return. }
  156. end;
  157. procedure _FPC_libc_haltproc(e:longint); assembler; nostackframe; public name '_haltproc';
  158. asm
  159. movl %edi,%eax
  160. popq %rdx { keep stack aligned }
  161. movq fpc_ret(%rip),%rdx { return to libc }
  162. movq fpc_ret_rbp(%rip),%rbp
  163. pushq %rdx
  164. end;