si_prc.inc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. %rdx Contains a function pointer to be registered with `atexit'.
  14. This is how the dynamic linker arranges to have DT_FINI
  15. functions called for shared libraries that have been loaded
  16. before this code runs.
  17. %rsp The stack contains the arguments and environment:
  18. 0(%rsp) argc
  19. 8(%rsp) argv[0]
  20. ...
  21. (8*argc)(%rsp) NULL
  22. (8*(argc+1))(%rsp) envp[0]
  23. ...
  24. NULL
  25. }
  26. {$asmmode gas}
  27. {$L abitag.o}
  28. procedure InitTLS; [external name 'FPC_INITTLS'];
  29. { so far, I found no case where this is actually called, so it is a dummy so far (FK) }
  30. function __tls_get_addr(p : pointer) : pointer; public name '__tls_get_addr';
  31. begin
  32. end;
  33. {******************************************************************************
  34. Process start/halt
  35. ******************************************************************************}
  36. var
  37. dlexitproc: pointer;
  38. procedure _FPC_proc_haltproc(e: longint); forward;
  39. procedure _FPC_proc_start; assembler; nostackframe; public name '_start';
  40. asm
  41. {$ifdef FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  42. movq SysInitEntryInformation@GOTPCREL(%rip),%r10 { load address of SysInitEntryInformation variable }
  43. popq %rsi { Pop the argument count. }
  44. movq %rsi,TEntryInformation.OS.argc(%r10)
  45. movq %rsp,TEntryInformation.OS.argv(%r10) { argv starts just at the current stack top. }
  46. leaq 8(,%rsi,8),%rax
  47. addq %rsp,%rax
  48. movq %rax,TEntryInformation.OS.envp(%r10)
  49. andq $0xfffffffffffffff0,%rsp { Align the stack to a 16 byte boundary to follow the ABI. }
  50. { Save initial stackpointer }
  51. movq %rsp,TEntryInformation.OS.stkptr(%r10)
  52. { store stack length }
  53. movq StackLength@GOTPCREL(%rip),%rax
  54. movq %rax,TEntryInformation.OS.stklen(%r10)
  55. { store pointer to haltproc }
  56. movq _FPC_proc_haltproc@GOTPCREL(%rip),%rax
  57. movq %rax,TEntryInformation.OS.haltproc(%r10)
  58. movq %r10,%rdi
  59. xorq %rbp, %rbp
  60. call SysEntry_InitTLS
  61. {$else FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  62. popq %rsi { Pop the argument count. }
  63. movq operatingsystem_parameter_argc@GOTPCREL(%rip),%rax
  64. movq %rsi,(%rax)
  65. movq operatingsystem_parameter_argv@GOTPCREL(%rip),%rax
  66. movq %rsp,(%rax) { argv starts just at the current stack top. }
  67. leaq 8(,%rsi,8),%rax
  68. addq %rsp,%rax
  69. movq operatingsystem_parameter_envp@GOTPCREL(%rip),%rsi
  70. movq %rax,(%rsi)
  71. andq $0xfffffffffffffff0,%rsp { Align the stack to a 16 byte boundary to follow the ABI. }
  72. { Save initial stackpointer }
  73. movq initialstkptr@GOTPCREL(%rip),%rax
  74. movq %rsp,(%rax)
  75. {$if FPC_FULLVERSION>30200}
  76. call InitTLS
  77. {$endif FPC_FULLVERSION>30200}
  78. xorq %rbp, %rbp
  79. call PASCALMAIN
  80. {$endif FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  81. end;
  82. procedure _FPC_dynamic_proc_start; assembler; nostackframe; public name '_dynamic_start';
  83. asm
  84. movq dlexitproc@GOTPCREL(%rip),%rax
  85. movq %rdx,(%rax)
  86. jmp _FPC_proc_start
  87. end;
  88. procedure _FPC_proc_haltproc(e: longint); assembler; public name '_haltproc';
  89. var
  90. code: longint;
  91. asm
  92. movl %edi,code
  93. movq dlexitproc@GOTPCREL(%rip),%rax
  94. movq (%rax),%rdx
  95. testq %rdx,%rdx
  96. jz .Lhaltproc
  97. call *%rdx
  98. .Lhaltproc:
  99. movl $231,%eax { exit_group call }
  100. movl code,%edi
  101. syscall
  102. jmp .Lhaltproc
  103. end;