crt0.S 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 __UCLIBC_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 L_gcrt1 ) && defined __UCLIBC_CTOR_DTOR__
  81. /* Store the address of _init in r3 as an argument to main() */
  82. ldr r3, =_init
  83. /* Push _fini onto the stack as the final argument to main() */
  84. ldr r4, =_fini
  85. stmfd sp!, {r4}
  86. /* Ok, now run uClibc's main() -- shouldn't return */
  87. bl __uClibc_start_main
  88. #else
  89. bl __uClibc_main
  90. #endif
  91. /* Crash if somehow `exit' returns anyways. */
  92. bl abort
  93. /* We need this stuff to make gdb behave itself, otherwise
  94. gdb will chokes with SIGILL when trying to debug apps.
  95. */
  96. .section ".note.ABI-tag", "a"
  97. .align 4
  98. .long 1f - 0f
  99. .long 3f - 2f
  100. .long 1
  101. 0: .asciz "GNU"
  102. 1: .align 4
  103. 2: .long 0
  104. .long 2,0,0
  105. 3: .align 4
  106. #if defined L_gcrt1 && defined __UCLIBC_PROFILING__
  107. # include "./gmon-start.S"
  108. #endif