dllprt0.as 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #
  2. # This file is part of the Free Pascal run time library.
  3. # Copyright (c) 2006 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 SVR4/i386 ABI (pages 3-31, 3-32) says that when the entry
  19. point runs, most registers' values are unspecified, except for:
  20. %rdx Contains a function pointer to be registered with `atexit'.
  21. This is how the dynamic linker arranges to have DT_FINI
  22. functions called for shared libraries that have been loaded
  23. before this code runs.
  24. %rsp The stack contains the arguments and environment:
  25. 0(%rsp) argc
  26. 8(%rsp) argv[0]
  27. ...
  28. (8*argc)(%rsp) NULL
  29. (8*(argc+1))(%rsp) envp[0]
  30. ...
  31. NULL
  32. */
  33. .section .init
  34. .align 16
  35. .globl FPC_SHARED_LIB_START
  36. .type FPC_SHARED_LIB_START,@function
  37. FPC_SHARED_LIB_START:
  38. jmp _startlib@PLT
  39. .text
  40. .globl _startlib
  41. .type _startlib,@function
  42. _startlib:
  43. movq entryinfo@GOTPCREL(%rip),%r10 /* load address of entryinfo variable */
  44. movq %rdi,56(%r10)
  45. movq %rsi,64(%r10) /* argv starts just at the current stack top */
  46. movq %rdx,72(%r10)
  47. /* Save initial stackpointer */
  48. movq %rsp,80(%r10)
  49. /* store stack length */
  50. movq __stklen@GOTPCREL(%rip),%rax
  51. movq %rax,88(%r10)
  52. /* store pointer to _haltproc */
  53. movq _haltproc@GOTPCREL(%rip),%rax
  54. movq %rax,96(%r10)
  55. /* populate the table pointers */
  56. movq INITFINAL@GOTPCREL(%rip),%rax
  57. movq %rax,(%r10)
  58. movq FPC_THREADVARTABLES@GOTPCREL(%rip),%rax
  59. movq %rax,8(%r10)
  60. movq FPC_RESOURCESTRINGTABLES@GOTPCREL(%rip),%rax
  61. movq %rax,16(%r10)
  62. movq FPC_RESSTRINITTABLES@GOTPCREL(%rip),%rax
  63. movq %rax,24(%r10)
  64. movq FPC_RESLOCATION@GOTPCREL(%rip),%rax
  65. movq %rax,32(%r10)
  66. movq PASCALMAIN@GOTPCREL(%rip),%rax
  67. movq %rax,40(%r10)
  68. /* valgrind_used can stay 0 */
  69. /* setup argument for FPC_SysEntry */
  70. movq %r10,%rdi
  71. movq operatingsystem_islibrary@GOTPCREL(%rip),%r10
  72. movb $1,(%r10)
  73. call FPC_SysEntry@PLT
  74. ret
  75. /* this routine is only called when the halt() routine of the RTL embedded in
  76. the shared library is called */
  77. .globl _haltproc
  78. .type _haltproc,@function
  79. _haltproc:
  80. movl $231,%eax /* exit_group call */
  81. movq operatingsystem_result@GOTPCREL(%rip),%rbx
  82. movzwl (%rbx),%edi
  83. syscall
  84. jmp _haltproc@PLT
  85. /* Define a symbol for the first piece of initialized data. */
  86. .data
  87. .globl __data_start
  88. __data_start:
  89. .long 0
  90. .weak data_start
  91. data_start = __data_start
  92. .bss
  93. /* the entry information looks like this:
  94. TEntryInformation = record
  95. InitFinalTable : Pointer; // offset 0
  96. ThreadvarTablesTable : Pointer; // offset 8
  97. ResourceStringTables : Pointer; // offset 16
  98. ResStrInitTables : Pointer; // offset 24
  99. ResLocation : Pointer; // offset 32
  100. PascalMain : Procedure; // offset 40
  101. valgrind_used : boolean; // offset 48
  102. OS : TEntryInformationOS; // offset 56
  103. end;
  104. with TEntryInformationOS being
  105. TEntryInformationOS = record
  106. argc: longint; // offset 56
  107. argv: ppchar; // offset 64
  108. envp: ppchar; // offset 72
  109. stkptr: pointer; // offset 80
  110. stklen: sizeuint; // offset 88
  111. haltproc: procedure(e:longint);cdecl; // offset 96
  112. end;
  113. The size of TEntryInformationOS including padding is 5 * sizeof(Pointer) = 40
  114. The size of TEntryInformation including padding without OS is 8 * sizeof(Pointer) = 64
  115. Thus the total size of TEntryInformation including padding is 104
  116. */
  117. .comm entryinfo,104
  118. /* We need this stuff to make gdb behave itself, otherwise
  119. gdb will chokes with SIGILL when trying to debug apps.
  120. Makes ld choke:
  121. .section ".note.ABI-tag", "a"
  122. .align 4
  123. .long 1f - 0f
  124. .long 3f - 2f
  125. .long 1
  126. 0: .asciz "GNU"
  127. 1: .align 4
  128. 2: .long 0
  129. .long 2,4,0
  130. 3: .align 4
  131. */
  132. .section .note.GNU-stack,"",@progbits