crt1.S 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /* Startup code for ARM & ELF
  2. Copyright (C) 1995, 1996, 1997, 1998, 2001, 2002, 2005
  3. Free Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  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. /* This is the canonical entry point, usually the first thing in the text
  33. segment.
  34. Note that the code in the .init section has already been run.
  35. This includes _init and _libc_init
  36. At this entry point, most registers' values are unspecified, except:
  37. a1 Contains a function pointer to be registered with `atexit'.
  38. This is how the dynamic linker arranges to have DT_FINI
  39. functions called for shared libraries that have been loaded
  40. before this code runs.
  41. sp The stack contains the arguments and environment:
  42. 0(sp) argc
  43. 4(sp) argv[0]
  44. ...
  45. (4*argc)(sp) NULL
  46. (4*(argc+1))(sp) envp[0]
  47. ...
  48. NULL
  49. */
  50. /*
  51. For uClinux it looks like this:
  52. argc argument counter (integer)
  53. argv char *argv[]
  54. envp char *envp[]
  55. argv[0] program name (pointer)
  56. argv[1...N] program args (pointers)
  57. argv[argc-1] end of args (integer)
  58. NULL
  59. env[0...N] environment variables (pointers)
  60. NULL
  61. ARM register quick reference:
  62. Name Number ARM Procedure Calling Standard Role
  63. a1 r0 argument 1 / integer result / scratch register / argc
  64. a2 r1 argument 2 / scratch register / argv
  65. a3 r2 argument 3 / scratch register / envp
  66. a4 r3 argument 4 / scratch register
  67. v1 r4 register variable
  68. v2 r5 register variable
  69. v3 r6 register variable
  70. v4 r7 register variable
  71. v5 r8 register variable
  72. sb/v6 r9 static base / register variable
  73. sl/v7 r10 stack limit / stack chunk handle / reg. variable
  74. fp r11 frame pointer
  75. ip r12 scratch register / new-sb in inter-link-unit calls
  76. sp r13 lower end of current stack frame
  77. lr r14 link address / scratch register
  78. pc r15 program counter
  79. */
  80. #include <features.h>
  81. #include <bits/arm_asm.h>
  82. .text
  83. .globl _start
  84. .type _start,%function
  85. .type _init,%function
  86. .type _fini,%function
  87. #ifndef __UCLIBC_CTOR_DTOR__
  88. .weak _init
  89. .weak _fini
  90. #endif
  91. #if defined(THUMB1_ONLY)
  92. .thumb_func
  93. _start:
  94. /* Clear the frame pointer since this is the outermost frame. */
  95. mov r3, #0
  96. mov fp, r3
  97. #ifdef __ARCH_USE_MMU__
  98. /* Pop argc off the stack and save a pointer to argv */
  99. pop {a2}
  100. mov a3, sp
  101. #else
  102. /*
  103. * uClinux/arm stacks look a little different from normal
  104. * MMU-full Linux/arm stacks (for no good reason)
  105. */
  106. /* pull argc and argv off the stack. We are going to push 3
  107. * arguments, so pop one here to maintain doubleword alignment. */
  108. pop {a2}
  109. ldr a3, [sp]
  110. #endif
  111. /* Push stack limit and rtld_fini */
  112. push {a1, a3}
  113. #ifdef __PIC__
  114. ldr r4, .L_GOT
  115. .L_GOT_OFF:
  116. adr r5, .L_GOT
  117. add r4, r5, r4
  118. ldr r5, .L_GOT+4 /* _fini */
  119. ldr a1, [r4, r5]
  120. push {a1} /* Push _fini */
  121. ldr r5, .L_GOT+8 /* _init */
  122. ldr a4, [r4, r5]
  123. ldr r5, .L_GOT+12 /* main */
  124. ldr a1, [r4, r5]
  125. #else
  126. /* Fetch address of fini */
  127. ldr r4, =_fini
  128. /* Push fini */
  129. push {r4}
  130. /* Set up the other arguments in registers */
  131. ldr a1, =main
  132. ldr a4, =_init
  133. #endif
  134. /* __uClibc_main (main, argc, argv, init, fini, rtld_fini, stack_end) */
  135. /* Let the libc call main and exit with its return code. */
  136. bl __uClibc_main
  137. /* should never get here....*/
  138. bl abort
  139. .pool
  140. #ifdef __PIC__
  141. .L_GOT:
  142. .word _GLOBAL_OFFSET_TABLE_-.L_GOT
  143. .word _fini(GOT)
  144. .word _init(GOT)
  145. .word main(GOT)
  146. #endif
  147. #else /* !THUMB1_ONLY */
  148. _start:
  149. /* Clear the frame pointer and link register since this is the outermost frame. */
  150. mov fp, #0
  151. mov lr, #0
  152. #ifdef __ARCH_USE_MMU__
  153. /* Pop argc off the stack and save a pointer to argv */
  154. ldr a2, [sp], #4
  155. mov a3, sp
  156. #else
  157. /*
  158. * uClinux/arm stacks look a little different from normal
  159. * MMU-full Linux/arm stacks (for no good reason)
  160. */
  161. /* pull argc and argv off the stack. We are going to push 3
  162. * arguments, so pop one here to maintain doubleword alignment. */
  163. ldr a2, [sp], #4
  164. ldr a3, [sp]
  165. #endif
  166. /* Push stack limit */
  167. str a3, [sp, #-4]!
  168. /* Push rtld_fini */
  169. str a1, [sp, #-4]!
  170. #ifdef __PIC__
  171. ldr sl, .L_GOT
  172. .L_GOT_OFF:
  173. add sl, pc, sl
  174. ldr ip, .L_GOT+4 /* _fini */
  175. ldr a1, [sl, ip]
  176. str a1, [sp, #-4]! /* Push _fini */
  177. ldr ip, .L_GOT+8 /* _init */
  178. ldr a4, [sl, ip]
  179. ldr ip, .L_GOT+12 /* main */
  180. ldr a1, [sl, ip]
  181. /* __uClibc_main (main, argc, argv, init, fini, rtld_fini, stack_end) */
  182. /* Let the libc call main and exit with its return code. */
  183. b __uClibc_main(PLT)
  184. #else
  185. /* Fetch address of fini */
  186. ldr ip, =_fini
  187. /* Push fini */
  188. str ip, [sp, #-4]!
  189. /* Set up the other arguments in registers */
  190. ldr a1, =main
  191. ldr a4, =_init
  192. /* __uClibc_main (main, argc, argv, init, fini, rtld_fini, stack_end) */
  193. /* Let the libc call main and exit with its return code. */
  194. b __uClibc_main
  195. #endif
  196. /* should never get here....*/
  197. bl abort
  198. #ifdef __PIC__
  199. .L_GOT:
  200. #ifdef __thumb__
  201. .word _GLOBAL_OFFSET_TABLE_-(.L_GOT_OFF+4)
  202. #else /* __thumb2__ */
  203. .word _GLOBAL_OFFSET_TABLE_-(.L_GOT_OFF+8)
  204. #endif
  205. .word _fini(GOT)
  206. .word _init(GOT)
  207. .word main(GOT)
  208. #endif
  209. #endif
  210. /* Define a symbol for the first piece of initialized data. */
  211. .data
  212. .globl __data_start
  213. __data_start:
  214. .long 0
  215. .weak data_start
  216. data_start = __data_start