crt1.S 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* Startup code compliant to the ELF x86-64 ABI.
  2. Copyright (C) 2001, 2002, 2003, 2005 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Andreas Jaeger <aj@suse.de>, 2001.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. In addition to the permissions in the GNU Lesser General Public
  10. License, the Free Software Foundation gives you unlimited
  11. permission to link the compiled version of this file with other
  12. programs, and to distribute those programs without any restriction
  13. coming from the use of this file. (The GNU Lesser General Public
  14. License restrictions do apply in other respects; for example, they
  15. cover modification of the file, and distribution when not linked
  16. into another program.)
  17. Note that people who make modified versions of this file are not
  18. obligated to grant this special exception for their modified
  19. versions; it is their choice whether to do so. The GNU Lesser
  20. General Public License gives permission to release a modified
  21. version without this exception; this exception also makes it
  22. possible to release a modified version which carries forward this
  23. exception.
  24. The GNU C Library is distributed in the hope that it will be useful,
  25. but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  27. Lesser General Public License for more details.
  28. You should have received a copy of the GNU Lesser General Public
  29. License along with the GNU C Library; if not, see
  30. <http://www.gnu.org/licenses/>. */
  31. /* Originally based on glibc's sysdeps/x86_64/elf/start.S */
  32. /* This is the canonical entry point, usually the first thing in the text
  33. segment. The SVR4/i386 ABI (pages 3-31, 3-32) says that when the entry
  34. point runs, most registers' values are unspecified, except for:
  35. %rdx Contains a function pointer to be registered with `atexit'.
  36. This is how the dynamic linker arranges to have DT_FINI
  37. functions called for shared libraries that have been loaded
  38. before this code runs.
  39. %rsp The stack contains the arguments and environment:
  40. 0(%rsp) argc
  41. 8(%rsp) argv[0]
  42. ...
  43. (8*argc)(%rsp) NULL
  44. (8*(argc+1))(%rsp) envp[0]
  45. ...
  46. NULL
  47. */
  48. #if defined NOT_IN_libc
  49. # error error in build framework
  50. #endif
  51. #include <features.h>
  52. .text
  53. .global _start
  54. .type _start,%function
  55. #if defined(__UCLIBC_CTOR_DTOR__)
  56. .type _init,%function
  57. .type _fini,%function
  58. #else
  59. .weak _init
  60. .weak _fini
  61. #endif
  62. .type __uClibc_main,%function
  63. /* Stick in a dummy reference to main(), so that if an application
  64. * is linking when the main() function is in a static library (.a)
  65. * we can be sure that main() actually gets linked in */
  66. .type main,%function
  67. _start:
  68. /* Clear the frame pointer. The ABI suggests this be done, to mark
  69. the outermost frame obviously. */
  70. xorl %ebp, %ebp
  71. #ifdef L_rcrt1
  72. pushq %rdi /* save rdi (but should be 0...) */
  73. pushq %rdx /* store rdx (rtld_fini) */
  74. xorq %rcx, %rcx /* ensure rcx is 0 */
  75. addq _start@GOTPCREL(%rip), %rcx /* get offset of _start from beginning of file */
  76. movq _start@GOTPCREL(%rip), %rax /* get run time address of _start */
  77. subq %rcx, %rax /* calculate run time load offset */
  78. movq %rax, %rdi /* load offset -> param 1 */
  79. call reloc_static_pie /* relocate dynamic addrs */
  80. xorq %rax, %rax /* cleanup */
  81. popq %rdx
  82. popq %rdi
  83. #endif
  84. /* Extract the arguments as encoded on the stack and set up
  85. the arguments for __libc_start_main (int (*main) (int, char **, char **),
  86. int argc, char *argv,
  87. void (*init) (void), void (*fini) (void),
  88. void (*rtld_fini) (void), void *stack_end).
  89. The arguments are passed via registers and on the stack:
  90. main: %rdi
  91. argc: %rsi
  92. argv: %rdx
  93. init: %rcx
  94. fini: %r8
  95. rtld_fini: %r9
  96. stack_end: stack. */
  97. movq %rdx, %r9 /* Address of the shared library termination function. */
  98. popq %rsi /* Pop the argument count. */
  99. movq %rsp, %rdx /* argv starts just at the current stack top. */
  100. /* Align the stack to a 16 byte boundary to follow the ABI. */
  101. andq $~15, %rsp
  102. pushq %rax /* Push garbage because we push 8 more bytes. */
  103. /* Provide the highest stack address to the user code (for stacks
  104. which grow downwards). */
  105. pushq %rsp
  106. #if defined(L_Scrt1) || defined(L_rcrt1)
  107. /* Give address for main() */
  108. movq main@GOTPCREL(%rip), %rdi
  109. /* setup init/fini address */
  110. movq _init@GOTPCREL(%rip), %rcx
  111. movq _fini@GOTPCREL(%rip), %r8
  112. /* start the fun */
  113. call __uClibc_main@PLT
  114. #else
  115. /* Give address for main() */
  116. movq $main, %rdi
  117. /* setup init/fini address */
  118. movq $_init, %rcx
  119. movq $_fini, %r8
  120. /* start the fun */
  121. call __uClibc_main
  122. #endif
  123. hlt /* Crash if somehow `exit' does return. */
  124. .size _start,.-_start
  125. /* Define a symbol for the first piece of initialized data. */
  126. .data
  127. .global __data_start
  128. __data_start:
  129. .long 0
  130. .weak data_start
  131. data_start = __data_start