si_c.inc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. var
  29. libc_environ: pchar; external name '__environ';
  30. libc_fpu_control: word; external name '__fpu_control';
  31. libc_init_proc: procedure; external name '_init';
  32. libc_fini_proc: procedure; external name '_fini';
  33. fpc_ret,fpc_ret_rbp : pointer;
  34. procedure libc_atexit; external name '__libc_atexit';
  35. procedure libc_exit; external name '__libc_exit';
  36. procedure libc_init; external name '__libc_init';
  37. procedure libc_setfpucw; external name '__setfpucw';
  38. procedure libc_start_main; external name '__libc_start_main';
  39. procedure PASCALMAIN; external name 'PASCALMAIN';
  40. procedure main_stub; assembler; nostackframe;
  41. asm
  42. { save return address }
  43. popq %rax
  44. // stack alignment
  45. pushq %rax
  46. movq %rax,fpc_ret(%rip)
  47. movq %rbp,fpc_ret_rbp(%rip)
  48. pushq %rax
  49. { Save initial stackpointer }
  50. movq initialstkptr@GOTPCREL(%rip),%rax
  51. movq %rsp,(%rax)
  52. { start the program }
  53. xorq %rbp,%rbp
  54. call PASCALMAIN
  55. hlt
  56. end;
  57. procedure ini_dummy;
  58. begin
  59. end;
  60. {******************************************************************************
  61. C library start/halt
  62. ******************************************************************************}
  63. procedure _FPC_libc_start; assembler; nostackframe; public name '_start';
  64. asm
  65. { Clear the frame pointer. The ABI suggests this be done, to mark
  66. the outermost frame obviously. }
  67. xorq %rbp, %rbp
  68. { Extract the arguments as encoded on the stack and set up
  69. the arguments for __libc_start_main (int (*main) (int, char **, char **),
  70. int argc, char *argv,
  71. void (*init) (void), void (*fini) (void),
  72. void (*rtld_fini) (void), void *stack_end).
  73. The arguments are passed via registers and on the stack:
  74. main: %rdi
  75. argc: %rsi
  76. argv: %rdx
  77. init: %rcx
  78. fini: %r8
  79. rtld_fini: %r9
  80. stack_end: stack. }
  81. movq %rdx, %r9 { Address of the shared library termination
  82. function. }
  83. popq %rsi { Pop the argument count. }
  84. movq %rsp, %rdx { argv starts just at the current stack top. }
  85. movq operatingsystem_parameter_argc@GOTPCREL(%rip),%rax
  86. movq %rsi,(%rax)
  87. movq operatingsystem_parameter_argv@GOTPCREL(%rip),%rax
  88. movq %rsp,(%rax)
  89. leaq 8(,%rsi,8),%rax
  90. addq %rsp,%rax
  91. movq operatingsystem_parameter_envp@GOTPCREL(%rip),%rcx
  92. movq %rax,(%rcx)
  93. { Align the stack to a 16 byte boundary to follow the ABI. }
  94. andq $0xfffffffffffffff0, %rsp
  95. pushq %rax { Push garbage because we push 8 more bytes. }
  96. { Provide the highest stack address to the user code (for stacks
  97. which grow downwards). }
  98. pushq %rsp
  99. { Pass address of our own entry points to .fini and .init. }
  100. movq $ini_dummy, %r8
  101. movq $ini_dummy, %rcx
  102. movq $main_stub, %rdi
  103. { Call the user's main function, and exit with its value.
  104. But let the libc call main. }
  105. call libc_start_main
  106. hlt { Crash if somehow `exit' does return. }
  107. { We need this stuff to make gdb behave itself, otherwise
  108. gdb will chokes with SIGILL when trying to debug apps.
  109. }
  110. (*
  111. .section ".note.ABI-tag", "a"
  112. .align 4
  113. .long 1f - 0f
  114. .long 3f - 2f
  115. .long 1
  116. 0: .asciz "GNU"
  117. 1: .align 4
  118. 2: .long 0
  119. .long 2,4,0
  120. 3: .align 4
  121. *)
  122. end;
  123. procedure _FPC_libc_haltproc; assembler; nostackframe; public name '_haltproc';
  124. asm
  125. movq ExitCode@GOTPCREL(%rip),%rax
  126. movzwq (%rax),%rax { load and save exitcode }
  127. movq fpc_ret(%rip),%rdx { return to libc }
  128. movq fpc_ret_rbp(%rip),%rbp
  129. pushq %rdx
  130. end;