si_prc.inc 4.1 KB

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