si_prc.inc 3.8 KB

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