si_prc.inc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. {******************************************************************************
  30. Process start/halt
  31. ******************************************************************************}
  32. var
  33. dlexitproc: pointer;
  34. procedure _FPC_proc_haltproc(e: longint); forward;
  35. procedure _FPC_proc_start; assembler; nostackframe; public name '_start';
  36. asm
  37. {$ifdef FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  38. movq SysInitEntryInformation@GOTPCREL(%rip),%r10 { load address of SysInitEntryInformation variable }
  39. popq %rsi { Pop the argument count. }
  40. movq %rsi,TEntryInformation.OS.argc(%r10)
  41. movq %rsp,TEntryInformation.OS.argv(%r10) { argv starts just at the current stack top. }
  42. leaq 8(,%rsi,8),%rax
  43. addq %rsp,%rax
  44. movq %rax,TEntryInformation.OS.envp(%r10)
  45. andq $0xfffffffffffffff0,%rsp { Align the stack to a 16 byte boundary to follow the ABI. }
  46. { Save initial stackpointer }
  47. movq %rsp,TEntryInformation.OS.stkptr(%r10)
  48. { store stack length }
  49. movq StackLength@GOTPCREL(%rip),%rax
  50. movq %rax,TEntryInformation.OS.stklen(%r10)
  51. { store pointer to haltproc }
  52. movq _FPC_proc_haltproc@GOTPCREL(%rip),%rax
  53. movq %rax,TEntryInformation.OS.haltproc(%r10)
  54. movq %r10,%rdi
  55. xorq %rbp, %rbp
  56. call SysEntry_InitTLS
  57. {$else FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  58. popq %rsi { Pop the argument count. }
  59. movq operatingsystem_parameter_argc@GOTPCREL(%rip),%rax
  60. movq %rsi,(%rax)
  61. movq operatingsystem_parameter_argv@GOTPCREL(%rip),%rax
  62. movq %rsp,(%rax) { argv starts just at the current stack top. }
  63. leaq 8(,%rsi,8),%rax
  64. addq %rsp,%rax
  65. movq operatingsystem_parameter_envp@GOTPCREL(%rip),%rsi
  66. movq %rax,(%rsi)
  67. andq $0xfffffffffffffff0,%rsp { Align the stack to a 16 byte boundary to follow the ABI. }
  68. { Save initial stackpointer }
  69. movq initialstkptr@GOTPCREL(%rip),%rax
  70. movq %rsp,(%rax)
  71. {$if FPC_FULLVERSION>30200}
  72. call InitTLS
  73. {$endif FPC_FULLVERSION>30200}
  74. xorq %rbp, %rbp
  75. call PASCALMAIN
  76. {$endif FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  77. end;
  78. procedure _FPC_dynamic_proc_start; assembler; nostackframe; public name '_dynamic_start';
  79. asm
  80. movq dlexitproc@GOTPCREL(%rip),%rax
  81. movq %rdx,(%rax)
  82. jmp _FPC_proc_start
  83. end;
  84. procedure _FPC_proc_haltproc(e: longint); assembler; public name '_haltproc';
  85. var
  86. code: longint;
  87. asm
  88. movl %edi,code
  89. movq dlexitproc@GOTPCREL(%rip),%rax
  90. movq (%rax),%rdx
  91. testq %rdx,%rdx
  92. jz .Lhaltproc
  93. call *%rdx
  94. .Lhaltproc:
  95. movl $231,%eax { exit_group call }
  96. movl code,%edi
  97. syscall
  98. jmp .Lhaltproc
  99. end;