crt1.S 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* Startup code compliant to the ELF Xtensa ABI.
  2. Copyright (C) 2001, 2007 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. In addition to the permissions in the GNU Lesser General Public
  9. License, the Free Software Foundation gives you unlimited
  10. permission to link the compiled version of this file with other
  11. programs, and to distribute those programs without any restriction
  12. coming from the use of this file. (The GNU Lesser General Public
  13. License restrictions do apply in other respects; for example, they
  14. cover modification of the file, and distribution when not linked
  15. into another program.)
  16. Note that people who make modified versions of this file are not
  17. obligated to grant this special exception for their modified
  18. versions; it is their choice whether to do so. The GNU Lesser
  19. General Public License gives permission to release a modified
  20. version without this exception; this exception also makes it
  21. possible to release a modified version which carries forward this
  22. exception.
  23. The GNU C Library is distributed in the hope that it will be useful,
  24. but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  26. Lesser General Public License for more details.
  27. You should have received a copy of the GNU Lesser General Public
  28. License along with the GNU C Library; if not, see
  29. <http://www.gnu.org/licenses/>. */
  30. #include <features.h>
  31. #ifndef __UCLIBC_CTOR_DTOR__
  32. .weak _init
  33. .weak _fini
  34. #endif
  35. /* This is the canonical entry point, usually the first thing in the text
  36. segment. When the entry point runs, most register values are unspecified,
  37. except for:
  38. a2 Contains a function pointer to be registered with `atexit'.
  39. This is how the dynamic linker arranges to have DT_FINI
  40. functions called for shared libraries that have been loaded
  41. before this code runs.
  42. a1 The stack (i.e., a1+16) contains the arguments and environment:
  43. a1+0 argc
  44. a1+4 argv[0]
  45. ...
  46. a1+(4*argc) NULL
  47. a1+(4*(argc+1)) envp[0]
  48. ...
  49. NULL
  50. Setup parameters accordingly (for a call4). See function prototype
  51. from sysdeps/generic/libc-start.c
  52. a6 = *main
  53. a7 = argc
  54. a8 = ubp_av
  55. a9 = *init
  56. a10 = *fini
  57. a11 = *rtld_fini
  58. [sp+0] = stack_end
  59. */
  60. .text
  61. .align 4
  62. .literal_position
  63. .global _start
  64. .type _start, @function
  65. _start:
  66. #ifdef L_rcrt1
  67. .begin no-transform
  68. call0 1f
  69. .Lret_addr:
  70. .end no-transform
  71. .align 4
  72. 1:
  73. #endif
  74. #if defined(__XTENSA_WINDOWED_ABI__)
  75. #ifdef L_rcrt1
  76. movi a6, .Lret_addr
  77. sub a6, a0, a6
  78. movi a0, 0
  79. movi a4, reloc_static_pie
  80. add a4, a4, a6
  81. callx4 a4
  82. #else
  83. /* Clear a0 to obviously mark the outermost frame. */
  84. movi a0, 0
  85. #endif
  86. /* Load up the user's main function. */
  87. movi a6, main
  88. /* Extract the arguments as encoded on the stack and set up
  89. the arguments for `main': argc, argv. envp will be determined
  90. later in __uClibc_main. */
  91. l32i a7, a1, 0 /* Load the argument count. */
  92. addi a8, a1, 4 /* Compute the argv pointer. */
  93. /* Push address of our own entry points to .fini and .init. */
  94. movi a9, _init
  95. movi a10, _fini
  96. /* Setup the shared library termination function. */
  97. mov a11, a2
  98. /* Provide the highest stack address to the user code (for stacks
  99. which grow downwards). Note that we destroy the stack version
  100. of argc here. */
  101. s32i a1, a1, 0
  102. /* Call the user's main function, and exit with its value.
  103. But let the libc call main. */
  104. movi a4, __uClibc_main
  105. callx4 a4
  106. #elif defined(__XTENSA_CALL0_ABI__)
  107. #ifdef L_rcrt1
  108. mov a12, a2
  109. movi a2, .Lret_addr
  110. sub a2, a0, a2
  111. movi a0, reloc_static_pie
  112. add a0, a0, a2
  113. callx0 a0
  114. mov a7, a12
  115. #else
  116. /* Setup the shared library termination function. */
  117. mov a7, a2
  118. #endif
  119. /* Load up the user's main function. */
  120. movi a2, main
  121. /* Extract the arguments as encoded on the stack and set up
  122. the arguments for `main': argc, argv. envp will be determined
  123. later in __uClibc_main. */
  124. l32i a3, a1, 0 /* Load the argument count. */
  125. addi a4, a1, 4 /* Compute the argv pointer. */
  126. /* Push address of our own entry points to .fini and .init. */
  127. movi a5, _init
  128. movi a6, _fini
  129. /* Provide the highest stack address to the user code (for stacks
  130. which grow downwards). Note that we destroy the stack version
  131. of argc here. */
  132. s32i a1, a1, 0
  133. /* Call the user's main function, and exit with its value.
  134. But let the libc call main. */
  135. movi a0, __uClibc_main
  136. callx0 a0
  137. #else
  138. #error Unsupported Xtensa ABI
  139. #endif
  140. /* Crash if somehow `exit' does return. */
  141. ill
  142. /* Define a symbol for the first piece of initialized data. */
  143. .data
  144. .align 4
  145. .global __data_start
  146. __data_start:
  147. .long 0
  148. .weak data_start
  149. data_start = __data_start