crt1.S 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* Startup code for ARM & ELF
  2. Copyright (C) 1995, 1996, 1997, 1998, 2001, 2002 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. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. /* This is the canonical entry point, usually the first thing in the text
  17. segment.
  18. Note that the code in the .init section has already been run.
  19. This includes _init and _libc_init
  20. At this entry point, most registers' values are unspecified, except:
  21. a1 Contains a function pointer to be registered with `atexit'.
  22. This is how the dynamic linker arranges to have DT_FINI
  23. functions called for shared libraries that have been loaded
  24. before this code runs.
  25. sp The stack contains the arguments and environment:
  26. 0(sp) argc
  27. 4(sp) argv[0]
  28. ...
  29. (4*argc)(sp) NULL
  30. (4*(argc+1))(sp) envp[0]
  31. ...
  32. NULL
  33. */
  34. /*
  35. For uClinux it looks like this:
  36. argc argument counter (integer)
  37. argv char *argv[]
  38. envp char *envp[]
  39. argv[0] program name (pointer)
  40. argv[1...N] program args (pointers)
  41. argv[argc-1] end of args (integer)
  42. NULL
  43. env[0...N] environment variables (pointers)
  44. NULL
  45. ARM register quick reference:
  46. Name Number ARM Procedure Calling Standard Role
  47. a1 r0 argument 1 / integer result / scratch register / argc
  48. a2 r1 argument 2 / scratch register / argv
  49. a3 r2 argument 3 / scratch register / envp
  50. a4 r3 argument 4 / scratch register
  51. v1 r4 register variable
  52. v2 r5 register variable
  53. v3 r6 register variable
  54. v4 r7 register variable
  55. v5 r8 register variable
  56. sb/v6 r9 static base / register variable
  57. sl/v7 r10 stack limit / stack chunk handle / reg. variable
  58. fp r11 frame pointer
  59. ip r12 scratch register / new-sb in inter-link-unit calls
  60. sp r13 lower end of current stack frame
  61. lr r14 link address / scratch register
  62. pc r15 program counter
  63. */
  64. #include <features.h>
  65. .text
  66. .globl _start
  67. .type _start,%function
  68. .type _init,%function
  69. .type _fini,%function
  70. #ifndef __UCLIBC_CTOR_DTOR__
  71. .weak _init
  72. .weak _fini
  73. #endif
  74. _start:
  75. /* Clear the frame pointer and link register since this is the outermost frame. */
  76. mov fp, #0
  77. mov lr, #0
  78. /* Pop argc off the stack and save a pointer to argv */
  79. ldr a2, [sp], #4
  80. mov a3, sp
  81. /* Push stack limit */
  82. str a3, [sp, #-4]!
  83. /* Push rtld_fini */
  84. str a1, [sp, #-4]!
  85. #ifdef __PIC__
  86. ldr sl, .L_GOT
  87. .L_GOT_OFF:
  88. add sl, pc, sl
  89. ldr ip, .L_GOT+4 /* _fini */
  90. ldr a1, [sl, ip]
  91. str a1, [sp, #-4]! /* Push _fini */
  92. ldr ip, .L_GOT+8 /* _init */
  93. ldr a4, [sl, ip]
  94. ldr ip, .L_GOT+12 /* main */
  95. ldr a1, [sl, ip]
  96. /* __uClibc_main (main, argc, argv, init, fini, rtld_fini, stack_end) */
  97. /* Let the libc call main and exit with its return code. */
  98. b __uClibc_main(PLT)
  99. #else
  100. /* Fetch address of fini */
  101. ldr ip, =_fini
  102. /* Push fini */
  103. str ip, [sp, #-4]!
  104. /* Set up the other arguments in registers */
  105. ldr a1, =main
  106. ldr a4, =_init
  107. /* __uClibc_main (main, argc, argv, init, fini, rtld_fini, stack_end) */
  108. /* Let the libc call main and exit with its return code. */
  109. b __uClibc_main
  110. #endif
  111. #ifdef __PIC__
  112. .L_GOT:
  113. .word _GLOBAL_OFFSET_TABLE_-(.L_GOT_OFF+8)
  114. .word _fini(GOT)
  115. .word _init(GOT)
  116. .word main(GOT)
  117. #endif
  118. /* Define a symbol for the first piece of initialized data. */
  119. .data
  120. .globl __data_start
  121. __data_start:
  122. .long 0
  123. .weak data_start
  124. data_start = __data_start