prt0.as 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #
  2. # This file is part of the Free Pascal run time library.
  3. # Copyright (c) 2002 by Florian Klaempfl
  4. # members of the Free Pascal development team.
  5. #
  6. # See the file COPYING.FPC, included in this distribution,
  7. # for details about the copyright.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY;without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. #
  13. #**********************************************************************}
  14. #
  15. # Linux ELF startup code for Free Pascal
  16. #
  17. /* This is the canonical entry point, usually the first thing in the text
  18. segment. The document "System V Application Binary Interface AMD64
  19. Architecture Processor Supplement Version 0.99.5" pg. 30 defines that
  20. the entry point runs, most registers' values are unspecified, except
  21. for:
  22. %rdx Contains a function pointer to be registered with `atexit'.
  23. This is how the dynamic linker arranges to have DT_FINI
  24. functions called for shared libraries that have been loaded
  25. before this code runs.
  26. %rsp The stack contains the arguments and environment:
  27. 0(%rsp) argc
  28. 8(%rsp) argv[0]
  29. ...
  30. (8*argc)(%rsp) NULL
  31. (8*(argc+1))(%rsp) envp[0]
  32. ...
  33. NULL
  34. */
  35. .text
  36. .globl _dynamic_start
  37. .type _dynamic_start,@function
  38. _dynamic_start:
  39. movq __dl_fini@GOTPCREL(%rip),%rax
  40. movq %rdx,(%rax)
  41. jmp _start
  42. .text
  43. .globl _start
  44. .type _start,@function
  45. _start:
  46. movq entryinfo@GOTPCREL(%rip),%rdi /* load address of entryinfo variable into argument for SysEntry */
  47. popq %rsi /* Pop the argument count. */
  48. movq %rsi,56(%rdi)
  49. movq %rsp,64(%rdi) /* argv starts just at the current stack top */
  50. leaq 8(,%rsi,8),%rax
  51. addq %rsp,%rax
  52. movq %rax,72(%rdi)
  53. andq $~15,%rsp /* Align the stack to a 16 byte boundary to follow the ABI */
  54. /* Save initial stackpointer */
  55. movq %rsp,80(%rdi)
  56. /* store stack length */
  57. movq __stklen@GOTPCREL(%rip),%rax
  58. movq %rax,88(%rdi)
  59. /* store pointer to _haltproc */
  60. movq _haltproc@GOTPCREL(%rip),%rax
  61. movq %rax,96(%rdi)
  62. /* populate the table pointers */
  63. movq INITFINAL@GOTPCREL(%rip),%rax
  64. movq %rax,(%rdi)
  65. movq FPC_THREADVARTABLES@GOTPCREL(%rip),%rax
  66. movq %rax,8(%rdi)
  67. movq FPC_RESOURCESTRINGTABLES@GOTPCREL(%rip),%rax
  68. movq %rax,16(%rdi)
  69. movq FPC_RESSTRINITTABLES@GOTPCREL(%rip),%rax
  70. movq %rax,24(%rdi)
  71. movq FPC_RESLOCATION@GOTPCREL(%rip),%rax
  72. movq %rax,32(%rdi)
  73. movq PASCALMAIN@GOTPCREL(%rip),%rax
  74. movq %rax,40(%rdi)
  75. /* valgrind_used can stay 0 */
  76. xorq %rbp, %rbp
  77. call FPC_SysEntry@PLT
  78. jmp _haltproc
  79. .globl _haltproc
  80. .type _haltproc,@function
  81. _haltproc:
  82. movq __dl_fini@GOTPCREL(%rip),%rax
  83. movq (%rax),%rax
  84. testq %rax,%rax
  85. jz .LNoDlFiniCall
  86. call *%rax
  87. .LNoDlFiniCall:
  88. movq operatingsystem_result@GOTPCREL(%rip),%rax
  89. movzwl (%rax),%edi
  90. movl $231,%eax /* exit_group call */
  91. syscall
  92. jmp _haltproc
  93. /* Define a symbol for the first piece of initialized data. */
  94. .data
  95. .globl __data_start
  96. __data_start:
  97. .long 0
  98. .weak data_start
  99. data_start = __data_start
  100. .bss
  101. .comm __dl_fini,8
  102. /* the entry information looks like this:
  103. TEntryInformation = record
  104. InitFinalTable : Pointer; // offset 0
  105. ThreadvarTablesTable : Pointer; // offset 8
  106. ResourceStringTables : Pointer; // offset 16
  107. ResStrInitTables : Pointer; // offset 24
  108. ResLocation : Pointer; // offset 32
  109. PascalMain : Procedure; // offset 40
  110. valgrind_used : boolean; // offset 48
  111. OS : TEntryInformationOS; // offset 56
  112. end;
  113. with TEntryInformationOS being
  114. TEntryInformationOS = record
  115. argc: longint; // offset 56
  116. argv: ppchar; // offset 64
  117. envp: ppchar; // offset 72
  118. stkptr: pointer; // offset 80
  119. stklen: sizeuint; // offset 88
  120. haltproc: procedure(e:longint);cdecl; // offset 96
  121. end;
  122. The size of TEntryInformationOS including padding is 5 * sizeof(Pointer) = 40
  123. The size of TEntryInformation including padding without OS is 8 * sizeof(Pointer) = 64
  124. Thus the total size of TEntryInformation including padding is 104
  125. */
  126. .comm entryinfo,104
  127. /* We need this stuff to make gdb behave itself, otherwise
  128. gdb will chokes with SIGILL when trying to debug apps.
  129. */
  130. .section ".note.ABI-tag", "a"
  131. .align 4
  132. .long 1f - 0f
  133. .long 3f - 2f
  134. .long 1
  135. 0: .asciz "GNU"
  136. 1: .align 4
  137. 2: .long 0
  138. .long 2,4,0
  139. 3: .align 4
  140. .section .note.GNU-stack,"",@progbits