crt0.S 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* When we enter this piece of code, the program stack looks like this:
  2. argc argument counter (integer)
  3. argv[0] program name (pointer)
  4. argv[1...N] program args (pointers)
  5. argv[argc-1] end of args (integer)
  6. NULL
  7. env[0...N] environment variables (pointers)
  8. NULL
  9. For uClinux it looks like this:
  10. argc argument counter (integer)
  11. argv char *argv[]
  12. envp char *envp[]
  13. argv[0] program name (pointer)
  14. argv[1...N] program args (pointers)
  15. argv[argc-1] end of args (integer)
  16. NULL
  17. env[0...N] environment variables (pointers)
  18. NULL
  19. When we are done here, we want
  20. a1=argc
  21. a2=argv[0]
  22. a3=argv[argc+1]
  23. ARM register quick reference:
  24. Name Number ARM Procedure Calling Standard Role
  25. a1 r0 argument 1 / integer result / scratch register / argc
  26. a2 r1 argument 2 / scratch register / argv
  27. a3 r2 argument 3 / scratch register / envp
  28. a4 r3 argument 4 / scratch register
  29. v1 r4 register variable
  30. v2 r5 register variable
  31. v3 r6 register variable
  32. v4 r7 register variable
  33. v5 r8 register variable
  34. sb/v6 r9 static base / register variable
  35. sl/v7 r10 stack limit / stack chunk handle / reg. variable
  36. fp r11 frame pointer
  37. ip r12 scratch register / new-sb in inter-link-unit calls
  38. sp r13 lower end of current stack frame
  39. lr r14 link address / scratch register
  40. pc r15 program counter
  41. */
  42. #include <features.h>
  43. .text
  44. .global _start
  45. .type _start,%function
  46. #if defined L_crt0 || ! defined __UCLIBC_CTOR_DTOR__
  47. .type __uClibc_main,%function
  48. #else
  49. .weak _init
  50. .weak _fini
  51. .type __uClibc_start_main,%function
  52. #endif
  53. /* Stick in a dummy reference to main(), so that if an application
  54. * is linking when the main() function is in a static library (.a)
  55. * we can be sure that main() actually gets linked in */
  56. .type main,%function
  57. .text
  58. _start:
  59. /* clear the frame pointer */
  60. mov fp, #0
  61. #ifdef __ARCH_HAS_MMU__
  62. /* Load register r0 (argc) from the stack to its final resting place */
  63. ldr r0, [sp], #4
  64. /* Copy argv pointer into r1 -- which its final resting place */
  65. mov r1, sp
  66. /* Skip to the end of argv and put a pointer to whatever
  67. we find there (hopefully the environment) in r2 */
  68. add r2, r1, r0, lsl #2
  69. add r2, r2, #4
  70. #else
  71. /*
  72. * uClinux stacks look a little different from normal
  73. * MMU-full Linux stacks (for no good reason)
  74. */
  75. /* pull argc, argv and envp off the stack */
  76. ldr r0,[sp, #0]
  77. ldr r1,[sp, #4]
  78. ldr r2,[sp, #8]
  79. #endif
  80. #if (defined L_crt1 ) && defined __UCLIBC_CTOR_DTOR__
  81. #ifdef __PIC__
  82. /* Store the address of _init in r3 as an argument to main() */
  83. adr r5, .L_init
  84. ldr r3, .L_init
  85. add r3, r3, r5
  86. /* Push _fini onto the stack as the final argument to main() */
  87. ldr r4, .L_init + 4
  88. add r4, r4, r5
  89. #else
  90. /* Store the address of _init in r3 as an argument to main() */
  91. ldr r3, =_init
  92. /* Push _fini onto the stack as the final argument to main() */
  93. ldr r4, =_fini
  94. #endif
  95. stmfd sp!, {r4}
  96. /* Ok, now run uClibc's main() -- shouldn't return */
  97. bl __uClibc_start_main
  98. #else
  99. bl __uClibc_main
  100. #endif
  101. /* Crash if somehow `exit' returns anyways. */
  102. bl abort
  103. #if (defined L_crt1 ) && defined __UCLIBC_CTOR_DTOR__ && defined __PIC__
  104. .L_init:
  105. .word _init - .L_init
  106. .word _fini - .L_init
  107. #endif
  108. /* We need this stuff to make gdb behave itself, otherwise
  109. gdb will chokes with SIGILL when trying to debug apps.
  110. */
  111. .section ".note.ABI-tag", "a"
  112. .align 4
  113. .long 1f - 0f
  114. .long 3f - 2f
  115. .long 1
  116. 0: .asciz "GNU"
  117. 1: .align 4
  118. 2: .long 0
  119. .long 2,0,0
  120. 3: .align 4
  121. /* Define a symbol for the first piece of initialized data. */
  122. .data
  123. .globl __data_start
  124. __data_start:
  125. .long 0
  126. .weak data_start
  127. data_start = __data_start