si_g.inc 5.9 KB

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