si_c.inc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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: pchar; 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 PASCALMAIN; external name 'PASCALMAIN';
  42. procedure main_stub; assembler; nostackframe;
  43. asm
  44. { save return address }
  45. popq %rax
  46. // stack alignment
  47. pushq %rax
  48. movq %rax,fpc_ret(%rip)
  49. movq %rbp,fpc_ret_rbp(%rip)
  50. pushq %rax
  51. { Save initial stackpointer }
  52. movq initialstkptr@GOTPCREL(%rip),%rax
  53. movq %rsp,(%rax)
  54. { start the program }
  55. xorq %rbp,%rbp
  56. call PASCALMAIN
  57. hlt
  58. end;
  59. procedure ini_dummy;
  60. begin
  61. end;
  62. {******************************************************************************
  63. C library start/halt
  64. ******************************************************************************}
  65. procedure _FPC_libc_start; assembler; nostackframe; public name '_start';
  66. asm
  67. { Clear the frame pointer. The ABI suggests this be done, to mark
  68. the outermost frame obviously. }
  69. xorq %rbp, %rbp
  70. { Extract the arguments as encoded on the stack and set up
  71. the arguments for __libc_start_main (int (*main) (int, char **, char **),
  72. int argc, char *argv,
  73. void (*init) (void), void (*fini) (void),
  74. void (*rtld_fini) (void), void *stack_end).
  75. The arguments are passed via registers and on the stack:
  76. main: %rdi
  77. argc: %rsi
  78. argv: %rdx
  79. init: %rcx
  80. fini: %r8
  81. rtld_fini: %r9
  82. stack_end: stack. }
  83. movq %rdx, %r9 { Address of the shared library termination
  84. function. }
  85. popq %rsi { Pop the argument count. }
  86. movq %rsp, %rdx { argv starts just at the current stack top. }
  87. movq operatingsystem_parameter_argc@GOTPCREL(%rip),%rax
  88. movq %rsi,(%rax)
  89. movq operatingsystem_parameter_argv@GOTPCREL(%rip),%rax
  90. movq %rsp,(%rax)
  91. leaq 8(,%rsi,8),%rax
  92. addq %rsp,%rax
  93. movq operatingsystem_parameter_envp@GOTPCREL(%rip),%rcx
  94. movq %rax,(%rcx)
  95. { Align the stack to a 16 byte boundary to follow the ABI. }
  96. andq $0xfffffffffffffff0, %rsp
  97. pushq %rax { Push garbage because we push 8 more bytes. }
  98. { Provide the highest stack address to the user code (for stacks
  99. which grow downwards). }
  100. pushq %rsp
  101. { Pass address of our own entry points to .fini and .init. }
  102. movq $ini_dummy, %r8
  103. movq $ini_dummy, %rcx
  104. movq $main_stub, %rdi
  105. { Call the user's main function, and exit with its value.
  106. But let the libc call main. }
  107. call libc_start_main
  108. hlt { Crash if somehow `exit' does return. }
  109. end;
  110. procedure _FPC_libc_haltproc(e:longint); assembler; nostackframe; public name '_haltproc';
  111. asm
  112. movl %edi,%eax
  113. movq fpc_ret(%rip),%rdx { return to libc }
  114. movq fpc_ret_rbp(%rip),%rbp
  115. pushq %rdx
  116. end;