si_prc.inc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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),%rax
  53. movq %rax,TEntryInformation.OS.stklen(%r10)
  54. { store pointer to haltproc }
  55. movq _FPC_proc_haltproc@GOTPCREL(%rip),%rax
  56. movq %rax,TEntryInformation.OS.haltproc(%r10)
  57. movq %r10,%rdi
  58. xorq %rbp, %rbp
  59. {$ifdef FPC_USE_LIBC}
  60. call SysEntry
  61. {$else}
  62. call SysEntry_InitTLS
  63. {$endif}
  64. {$else FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  65. popq %rsi { Pop the argument count. }
  66. movq operatingsystem_parameter_argc@GOTPCREL(%rip),%rax
  67. movq %rsi,(%rax)
  68. movq operatingsystem_parameter_argv@GOTPCREL(%rip),%rax
  69. movq %rsp,(%rax) { argv starts just at the current stack top. }
  70. leaq 8(,%rsi,8),%rax
  71. addq %rsp,%rax
  72. movq operatingsystem_parameter_envp@GOTPCREL(%rip),%rsi
  73. movq %rax,(%rsi)
  74. andq $0xfffffffffffffff0,%rsp { Align the stack to a 16 byte boundary to follow the ABI. }
  75. { Save initial stackpointer }
  76. movq initialstkptr@GOTPCREL(%rip),%rax
  77. movq %rsp,(%rax)
  78. {$if (FPC_FULLVERSION>30200) and not defined(FPC_USE_LIBC)}
  79. call InitTLS
  80. {$endif FPC_FULLVERSION>30200 and not FPC_USE_LIBC}
  81. xorq %rbp, %rbp
  82. call PASCALMAIN
  83. {$endif FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  84. end;
  85. procedure _FPC_dynamic_proc_start; assembler; nostackframe; public name '_dynamic_start';
  86. asm
  87. movq dlexitproc@GOTPCREL(%rip),%rax
  88. movq %rdx,(%rax)
  89. jmp _FPC_proc_start
  90. end;
  91. procedure _FPC_proc_haltproc(e: longint); assembler; public name '_haltproc';
  92. var
  93. code: longint;
  94. asm
  95. movl %edi,code
  96. movq dlexitproc@GOTPCREL(%rip),%rax
  97. movq (%rax),%rdx
  98. testq %rdx,%rdx
  99. jz .Lhaltproc
  100. call *%rdx
  101. .Lhaltproc:
  102. movl $231,%eax { exit_group call }
  103. movl code,%edi
  104. syscall
  105. jmp .Lhaltproc
  106. end;