crt1.S 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. .text
  65. .globl _start
  66. .type _start,#function
  67. _start:
  68. /* Clear the frame pointer and link register since this is the outermost frame. */
  69. mov fp, #0
  70. mov lr, #0
  71. /* Pop argc off the stack and save a pointer to argv */
  72. ldr a2, [sp], #4
  73. mov a3, sp
  74. /* Push stack limit */
  75. str a3, [sp, #-4]!
  76. /* Push rtld_fini */
  77. str a1, [sp, #-4]!
  78. #ifdef __PIC__
  79. ldr sl, .L_GOT
  80. .L_GOT_OFF:
  81. add sl, pc, sl
  82. ldr ip, .L_GOT+4 /* _fini */
  83. ldr a1, [sl, ip]
  84. str a1, [sp, #-4]! /* Push _fini */
  85. ldr ip, .L_GOT+8 /* _init */
  86. ldr a4, [sl, ip]
  87. ldr ip, .L_GOT+12 /* main */
  88. ldr a1, [sl, ip]
  89. /* __uClibc_main (main, argc, argv, init, fini, rtld_fini, stack_end) */
  90. /* Let the libc call main and exit with its return code. */
  91. b __uClibc_main(PLT)
  92. #else
  93. /* Fetch address of fini */
  94. ldr ip, =_fini
  95. /* Push fini */
  96. str ip, [sp, #-4]!
  97. /* Set up the other arguments in registers */
  98. ldr a1, =main
  99. ldr a4, =_init
  100. /* __uClibc_main (main, argc, argv, init, fini, rtld_fini, stack_end) */
  101. /* Let the libc call main and exit with its return code. */
  102. b __uClibc_main
  103. #endif
  104. #ifdef __PIC__
  105. .L_GOT:
  106. .word _GLOBAL_OFFSET_TABLE_-(.L_GOT_OFF+8)
  107. .word _fini(GOT)
  108. .word _init(GOT)
  109. .word main(GOT)
  110. #endif
  111. /* Define a symbol for the first piece of initialized data. */
  112. .data
  113. .globl __data_start
  114. __data_start:
  115. .long 0
  116. .weak data_start
  117. data_start = __data_start