crt1.S 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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, see
  30. <http://www.gnu.org/licenses/>. */
  31. /* This is the canonical entry point, usually the first thing in the text
  32. segment.
  33. Note that the code in the .init section has already been run.
  34. This includes _init and _libc_init
  35. At this entry point, most registers' values are unspecified, except:
  36. a1 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. sp The stack contains the arguments and environment:
  41. 0(sp) argc
  42. 4(sp) argv[0]
  43. ...
  44. (4*argc)(sp) NULL
  45. (4*(argc+1))(sp) envp[0]
  46. ...
  47. NULL
  48. */
  49. /*
  50. For uClinux it looks like this:
  51. argc argument counter (integer)
  52. argv char *argv[]
  53. envp char *envp[]
  54. argv[0] program name (pointer)
  55. argv[1...N] program args (pointers)
  56. argv[argc-1] end of args (integer)
  57. NULL
  58. env[0...N] environment variables (pointers)
  59. NULL
  60. ARM register quick reference:
  61. Name Number ARM Procedure Calling Standard Role
  62. a1 r0 argument 1 / integer result / scratch register / argc
  63. a2 r1 argument 2 / scratch register / argv
  64. a3 r2 argument 3 / scratch register / envp
  65. a4 r3 argument 4 / scratch register
  66. v1 r4 register variable
  67. v2 r5 register variable
  68. v3 r6 register variable
  69. v4 r7 register variable
  70. v5 r8 register variable
  71. sb/v6 r9 static base / register variable
  72. sl/v7 r10 stack limit / stack chunk handle / reg. variable
  73. fp r11 frame pointer
  74. ip r12 scratch register / new-sb in inter-link-unit calls
  75. sp r13 lower end of current stack frame
  76. lr r14 link address / scratch register
  77. pc r15 program counter
  78. */
  79. #include <features.h>
  80. #include <bits/arm_asm.h>
  81. #if defined(__FDPIC__)
  82. .text
  83. .globl _start
  84. .type _start,%function
  85. .align 2
  86. _start:
  87. /* Start by self relocation. */
  88. #if defined(__thumb2__)
  89. sub r4, pc, #4
  90. #else
  91. sub r4, pc, #8
  92. #endif
  93. ldr r1, .L__ROFIXUP_LIST__
  94. add r1, r1, r4
  95. ldr r2, .L__ROFIXUP_END__
  96. add r2, r2, r4
  97. mov r0, r7
  98. push {r9, r10}
  99. bl __self_reloc
  100. pop {r9, r10}
  101. mov r9, r0
  102. /* Clear the frame pointer since this is the outermost frame. */
  103. mov r3, #0
  104. mov fp, r3
  105. /* Now fill __uClibc_main arguments. */
  106. sub sp, sp, #16 /* Reserve space for 3 args (keep aligment) */
  107. ldr r0, .L_main_got_funcdesc /* r0 GOT FUNCDESC of main */
  108. ldr r0, [r9, r0]
  109. ldr r1, [sp, #16] /* argc */
  110. add r2, sp, #20 /* argv */
  111. ldr r3, .L_init_got_funcdesc /* r3 GOT init */
  112. ldr r3, [r9, r3]
  113. ldr r4, .L_fini_got_funcdesc
  114. ldr r4, [r9, r4]
  115. str r4, [sp, #0] /* sp + 0 GOT fini */
  116. movs r4, r8 /* Test if static binary (r8 is 0 as there is no interpreter). */
  117. ite ne
  118. movne r4, r10
  119. moveq r4, #0
  120. str r4, [sp, #4] /* sp + 4 got rtld_fini */
  121. mov r4, r2 /* stack_end is unused.... */
  122. str r4, [sp, #8] /* sp + 8 got stack_end */
  123. bl __uClibc_main
  124. /* We should never get here.... */
  125. bl abort
  126. .L__ROFIXUP_LIST__:
  127. .word __ROFIXUP_LIST__ - _start
  128. .L__ROFIXUP_END__:
  129. .word __ROFIXUP_END__ - _start
  130. .L_main_got_funcdesc:
  131. .word main(GOTFUNCDESC)
  132. .L_init_got_funcdesc:
  133. .word _init(GOTFUNCDESC)
  134. .L_fini_got_funcdesc:
  135. .word _fini(GOTFUNCDESC)
  136. /* Define a symbol for the first piece of initialized data. */
  137. .data
  138. .globl __data_start
  139. __data_start:
  140. .long 0
  141. .weak data_start
  142. data_start = __data_start
  143. #else /* defined(__FDPIC__) */
  144. .text
  145. .globl _start
  146. .type _start,%function
  147. .type _init,%function
  148. .type _fini,%function
  149. #ifndef __UCLIBC_CTOR_DTOR__
  150. .weak _init
  151. .weak _fini
  152. #endif
  153. #if defined(THUMB1_ONLY)
  154. .thumb_func
  155. _start:
  156. /* Clear the frame pointer since this is the outermost frame. */
  157. mov r3, #0
  158. mov fp, r3
  159. #ifdef __ARCH_USE_MMU__
  160. /* Pop argc off the stack and save a pointer to argv */
  161. pop {a2}
  162. mov a3, sp
  163. #else
  164. /*
  165. * uClinux/arm stacks look a little different from normal
  166. * MMU-full Linux/arm stacks (for no good reason)
  167. */
  168. /* pull argc and argv off the stack. We are going to push 3
  169. * arguments, so pop one here to maintain doubleword alignment. */
  170. pop {a2}
  171. ldr a3, [sp]
  172. #endif
  173. /* Push stack limit and rtld_fini */
  174. push {a1, a3}
  175. #ifdef __PIC__
  176. ldr r4, .L_GOT
  177. adr r5, .L_GOT
  178. add r4, r5, r4
  179. ldr r5, .L_GOT+4 /* _fini */
  180. ldr a1, [r4, r5]
  181. push {a1} /* Push _fini */
  182. ldr r5, .L_GOT+8 /* _init */
  183. ldr a4, [r4, r5]
  184. ldr r5, .L_GOT+12 /* main */
  185. ldr a1, [r4, r5]
  186. #else
  187. /* Fetch address of fini */
  188. ldr r4, =_fini
  189. /* Push fini */
  190. push {r4}
  191. /* Set up the other arguments in registers */
  192. ldr a1, =main
  193. ldr a4, =_init
  194. #endif
  195. /* __uClibc_main (main, argc, argv, init, fini, rtld_fini, stack_end) */
  196. /* Let the libc call main and exit with its return code. */
  197. bl __uClibc_main
  198. /* should never get here....*/
  199. bl abort
  200. .pool
  201. #ifdef __PIC__
  202. .L_GOT:
  203. .word _GLOBAL_OFFSET_TABLE_-.L_GOT
  204. .word _fini(GOT)
  205. .word _init(GOT)
  206. .word main(GOT)
  207. #endif
  208. #else /* !THUMB1_ONLY */
  209. _start:
  210. /* Clear the frame pointer and link register since this is the outermost frame. */
  211. mov fp, #0
  212. mov lr, #0
  213. #ifdef __ARCH_USE_MMU__
  214. #ifdef L_rcrt1
  215. /* We don't need to save a1 since no dynamic linker should have run */
  216. ldr a1, .L_GOT /* Get value at .L_GOT + 0 (offset to GOT)*/
  217. adr a2, .L_GOT /* Get address of .L_GOT */
  218. ldr a3, .L_GOT+16 /* Get value of _start(GOT) stored in .L_GOT */
  219. adr a4, _start /* Get address of _start after relocation (changes to pc - ~30 or so) */
  220. add a1, a1, a2 /* Calculate where the GOT is */
  221. ldr a2, [a1, a3] /* GOT + _start(GOT) = offset of _start from begin of file */
  222. sub a1, a4, a2 /* Current addr of _start - offset from beginning of file = load addr */
  223. bl reloc_static_pie
  224. mov a1, #0 /* Clean up a1 so that a random address won't get called at the end of program */
  225. /* Clear the frame pointer and link register again since it might be modified by previous call */
  226. mov fp, #0
  227. mov lr, #0
  228. #endif
  229. /* Pop argc off the stack and save a pointer to argv */
  230. ldr a2, [sp], #4
  231. mov a3, sp
  232. #else
  233. /*
  234. * uClinux/arm stacks look a little different from normal
  235. * MMU-full Linux/arm stacks (for no good reason)
  236. */
  237. /* pull argc and argv off the stack. We are going to push 3
  238. * arguments, so pop one here to maintain doubleword alignment. */
  239. ldr a2, [sp], #4
  240. ldr a3, [sp]
  241. #endif
  242. /* Push stack limit */
  243. str a3, [sp, #-4]!
  244. /* Push rtld_fini */
  245. str a1, [sp, #-4]!
  246. #ifdef __PIC__
  247. ldr sl, .L_GOT
  248. adr a4, .L_GOT
  249. add sl, sl, a4
  250. ldr ip, .L_GOT+4 /* _fini */
  251. ldr a1, [sl, ip]
  252. str a1, [sp, #-4]! /* Push _fini */
  253. ldr ip, .L_GOT+8 /* _init */
  254. ldr a4, [sl, ip]
  255. ldr ip, .L_GOT+12 /* main */
  256. ldr a1, [sl, ip]
  257. /* __uClibc_main (main, argc, argv, init, fini, rtld_fini, stack_end) */
  258. /* Let the libc call main and exit with its return code. */
  259. b __uClibc_main(PLT)
  260. #else
  261. /* Fetch address of fini */
  262. ldr ip, =_fini
  263. /* Push fini */
  264. str ip, [sp, #-4]!
  265. /* Set up the other arguments in registers */
  266. ldr a1, =main
  267. ldr a4, =_init
  268. /* __uClibc_main (main, argc, argv, init, fini, rtld_fini, stack_end) */
  269. /* Let the libc call main and exit with its return code. */
  270. b __uClibc_main
  271. #endif
  272. /* should never get here....*/
  273. bl abort
  274. #ifdef __PIC__
  275. .L_GOT:
  276. .word _GLOBAL_OFFSET_TABLE_ - .L_GOT
  277. .word _fini(GOT)
  278. .word _init(GOT)
  279. .word main(GOT)
  280. #ifdef L_rcrt1
  281. .word _start(GOT)
  282. #endif
  283. #endif
  284. #endif
  285. /* Define a symbol for the first piece of initialized data. */
  286. .data
  287. .globl __data_start
  288. __data_start:
  289. .long 0
  290. .weak data_start
  291. data_start = __data_start
  292. #endif /* defined(__FDPIC__) */