crt1.S 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. #if defined(__FDPIC__)
  32. /* This is the canonical entry point, usually the first thing in the text
  33. segment. When the entry point runs, most register values are unspecified,
  34. except for:
  35. a6 Address of .dynamic section
  36. a5 Interpreter map
  37. a4 Executable map
  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. */
  51. .text
  52. .align 4
  53. .literal_position
  54. .global _start
  55. .type _start, @function
  56. _start:
  57. #if defined(__XTENSA_CALL0_ABI__)
  58. .begin no-transform
  59. call0 1f
  60. 2:
  61. .end no-transform
  62. .align 4
  63. .literal_position
  64. 1:
  65. movi a15, 2b
  66. sub a15, a0, a15
  67. mov a12, a4
  68. mov a13, a5
  69. mov a14, a6
  70. mov a2, a4
  71. movi a3, __ROFIXUP_LIST__
  72. add a3, a3, a15
  73. movi a4, __ROFIXUP_END__
  74. add a4, a4, a15
  75. movi a0, __self_reloc
  76. add a0, a0, a15
  77. callx0 a0
  78. mov a11, a2
  79. movi a2, main@GOTOFFFUNCDESC
  80. add a2, a2, a11
  81. l32i a3, sp, 0 /* argc */
  82. addi a4, sp, 4 /* argv */
  83. /* a5 is either 0 when static or set by the RTLD to the rtld_fini */
  84. mov a7, a13
  85. /* unused stack_end argument is what used to be argc */
  86. movi a5, _init@GOTOFFFUNCDESC
  87. add a5, a5, a11
  88. movi a6, _fini@GOTOFFFUNCDESC
  89. add a6, a6, a11
  90. movi a0, __uClibc_main@GOTOFFFUNCDESC
  91. add a0, a0, a11
  92. l32i a11, a0, 4
  93. l32i a0, a0, 0
  94. callx0 a0
  95. ill
  96. #else
  97. #error Unsupported Xtensa ABI
  98. #endif
  99. #else /* defined(__FDPIC__) */
  100. #ifndef __UCLIBC_CTOR_DTOR__
  101. .weak _init
  102. .weak _fini
  103. #endif
  104. /* This is the canonical entry point, usually the first thing in the text
  105. segment. When the entry point runs, most register values are unspecified,
  106. except for:
  107. a2 Contains a function pointer to be registered with `atexit'.
  108. This is how the dynamic linker arranges to have DT_FINI
  109. functions called for shared libraries that have been loaded
  110. before this code runs.
  111. a1 The stack (i.e., a1+16) contains the arguments and environment:
  112. a1+0 argc
  113. a1+4 argv[0]
  114. ...
  115. a1+(4*argc) NULL
  116. a1+(4*(argc+1)) envp[0]
  117. ...
  118. NULL
  119. Setup parameters accordingly (for a call4). See function prototype
  120. from sysdeps/generic/libc-start.c
  121. a6 = *main
  122. a7 = argc
  123. a8 = ubp_av
  124. a9 = *init
  125. a10 = *fini
  126. a11 = *rtld_fini
  127. [sp+0] = stack_end
  128. */
  129. .text
  130. .align 4
  131. .literal_position
  132. .global _start
  133. .type _start, @function
  134. _start:
  135. #ifdef L_rcrt1
  136. .begin no-transform
  137. call0 1f
  138. .Lret_addr:
  139. .end no-transform
  140. .align 4
  141. 1:
  142. #endif
  143. #if defined(__XTENSA_WINDOWED_ABI__)
  144. #ifdef L_rcrt1
  145. movi a6, .Lret_addr
  146. sub a6, a0, a6
  147. movi a0, 0
  148. movi a4, reloc_static_pie
  149. add a4, a4, a6
  150. callx4 a4
  151. #else
  152. /* Clear a0 to obviously mark the outermost frame. */
  153. movi a0, 0
  154. #endif
  155. /* Load up the user's main function. */
  156. movi a6, main
  157. /* Extract the arguments as encoded on the stack and set up
  158. the arguments for `main': argc, argv. envp will be determined
  159. later in __uClibc_main. */
  160. l32i a7, a1, 0 /* Load the argument count. */
  161. addi a8, a1, 4 /* Compute the argv pointer. */
  162. /* Push address of our own entry points to .fini and .init. */
  163. movi a9, _init
  164. movi a10, _fini
  165. /* Setup the shared library termination function. */
  166. mov a11, a2
  167. /* Provide the highest stack address to the user code (for stacks
  168. which grow downwards). Note that we destroy the stack version
  169. of argc here. */
  170. s32i a1, a1, 0
  171. /* Call the user's main function, and exit with its value.
  172. But let the libc call main. */
  173. movi a4, __uClibc_main
  174. callx4 a4
  175. #elif defined(__XTENSA_CALL0_ABI__)
  176. #ifdef L_rcrt1
  177. mov a12, a2
  178. movi a2, .Lret_addr
  179. sub a2, a0, a2
  180. movi a0, reloc_static_pie
  181. add a0, a0, a2
  182. callx0 a0
  183. mov a7, a12
  184. #else
  185. /* Setup the shared library termination function. */
  186. mov a7, a2
  187. #endif
  188. /* Load up the user's main function. */
  189. movi a2, main
  190. /* Extract the arguments as encoded on the stack and set up
  191. the arguments for `main': argc, argv. envp will be determined
  192. later in __uClibc_main. */
  193. l32i a3, a1, 0 /* Load the argument count. */
  194. addi a4, a1, 4 /* Compute the argv pointer. */
  195. /* Push address of our own entry points to .fini and .init. */
  196. movi a5, _init
  197. movi a6, _fini
  198. /* Provide the highest stack address to the user code (for stacks
  199. which grow downwards). Note that we destroy the stack version
  200. of argc here. */
  201. s32i a1, a1, 0
  202. /* Call the user's main function, and exit with its value.
  203. But let the libc call main. */
  204. movi a0, __uClibc_main
  205. callx0 a0
  206. #else
  207. #error Unsupported Xtensa ABI
  208. #endif
  209. /* Crash if somehow `exit' does return. */
  210. ill
  211. /* Define a symbol for the first piece of initialized data. */
  212. .data
  213. .align 4
  214. .global __data_start
  215. __data_start:
  216. .long 0
  217. .weak data_start
  218. data_start = __data_start
  219. #endif /* defined(__FDPIC__) */