crt0.S 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* Startup code compliant to the ELF x86-64 ABI.
  2. Copyright (C) 2001, 2002, 2003 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, write to the Free
  30. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  31. 02111-1307 USA. */
  32. /* Originally based on glibc's sysdeps/x86_64/elf/start.S */
  33. /* This is the canonical entry point, usually the first thing in the text
  34. segment. The SVR4/i386 ABI (pages 3-31, 3-32) says that when the entry
  35. point runs, most registers' values are unspecified, except for:
  36. %rdx Contains a function pointer to be registered with `atexit'.
  37. This is how the dynamic linker arranges to have DT_FINI
  38. functions called for shared libraries that have been loaded
  39. before this code runs.
  40. %rsp The stack contains the arguments and environment:
  41. 0(%rsp) argc
  42. 8(%rsp) argv[0]
  43. ...
  44. (8*argc)(%rsp) NULL
  45. (8*(argc+1))(%rsp) envp[0]
  46. ...
  47. NULL
  48. */
  49. #include <features.h>
  50. .text
  51. .global _start
  52. .type _start,%function
  53. #if defined L_crt0 || defined L_Scrt0 || ! defined __UCLIBC_CTOR_DTOR__
  54. .type __uClibc_main,%function
  55. #else
  56. .weak _init
  57. .weak _fini
  58. .type __uClibc_start_main,%function
  59. #endif
  60. /* Stick in a dummy reference to main(), so that if an application
  61. * is linking when the main() function is in a static library (.a)
  62. * we can be sure that main() actually gets linked in */
  63. .type main,%function
  64. _start:
  65. /* Clear the frame pointer. The ABI suggests this be done, to mark
  66. the outermost frame obviously. */
  67. xorq %rbp, %rbp
  68. /* Extract the arguments as encoded on the stack and set up
  69. the arguments for __libc_start_main (int (*main) (int, char **, char **),
  70. int argc, char *argv,
  71. void (*init) (void), void (*fini) (void),
  72. void (*rtld_fini) (void), void *stack_end).
  73. The arguments are passed via registers and on the stack:
  74. main: %rdi
  75. argc: %rsi
  76. argv: %rdx
  77. init: %rcx
  78. fini: %r8
  79. rtld_fini: %r9
  80. stack_end: stack. */
  81. #if 0
  82. /* glibc */
  83. movq %rdx, %r9 /* Address of the shared library termination function. */
  84. popq %rsi /* Pop the argument count. */
  85. movq %rsp, %rdx /* argv starts just at the current stack top. */
  86. movq %rsi, %rbx /* now we calc envp ... envp = argc */
  87. addq $8, %rbx
  88. shl $0x3, %rbx /* envp *= 8 */
  89. addq %rdx, %rbx /* envp += argv */
  90. #else
  91. /* uclibc */
  92. movq %rdx, %r9 /* Address of the shared library termination function. */
  93. popq %rdi /* Pop the argument count. */
  94. movq %rsp, %rsi /* argv starts just at the current stack top. */
  95. movq %rdi, %rdx /* now we calc envp ... envp = argc */
  96. addq $8, %rdx
  97. shl $0x3, %rdx /* envp *= 8 */
  98. addq %rsi, %rdx /* envp += argv */
  99. #endif
  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. /**************
  107. * START TODO */
  108. #if defined L_Scrt0 || defined L_Scrt1
  109. call .L0
  110. .L0:
  111. popq %rbx
  112. addq $_GLOBAL_OFFSET_TABLE_+[.-.L0],%rbx
  113. #endif
  114. #if (defined L_crt1 || defined L_Scrt1) && defined __UCLIBC_CTOR_DTOR__
  115. /* Push .init and .fini arguments to __uClibc_start_main() on the stack */
  116. #ifdef L_Scrt1
  117. pushl _fini@GOT(%rip)
  118. pushl _init@GOT(%rip)
  119. #else
  120. pushl $_fini
  121. pushl $_init
  122. #endif
  123. /* Push envp, argc, and argc arguments to __uClibc_start_main() on the stack */
  124. #if 0
  125. pushq %rdx /* Environment pointer */
  126. pushq %rsi /* Argument pointer */
  127. pushq %rdi /* And the argument count */
  128. #endif
  129. /* Ok, now run uClibc's main() -- shouldn't return */
  130. #ifdef L_Scrt1
  131. call *__uClibc_start_main@GOT(%rip)
  132. #else
  133. call __uClibc_start_main
  134. #endif
  135. #else
  136. /* Push envp, argc, and argc arguments to __uClibc_start_main() on the stack */
  137. #if 0
  138. pushq %rdx /* Environment pointer */
  139. pushq %rsi /* Argument pointer */
  140. pushq %rdi /* And the argument count */
  141. #endif
  142. #ifdef L_Scrt0
  143. call *__uClibc_main@GOT(%rip)
  144. #else
  145. call __uClibc_main
  146. #endif
  147. #endif
  148. /* END TODO *
  149. ************/
  150. hlt /* Crash if somehow `exit' does return. */
  151. .size _start,.-_start
  152. /* Define a symbol for the first piece of initialized data. */
  153. .data
  154. .globl __data_start
  155. __data_start:
  156. .long 0
  157. .weak data_start
  158. data_start = __data_start