crt0.S 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. .text
  47. _start:
  48. /* clear the frame pointer */
  49. mov fp, #0
  50. #ifdef __UCLIBC_HAS_MMU__
  51. /* Load register r0 (argc) from the stack to its final resting place */
  52. ldr r0, [sp], #4
  53. /* Copy argv pointer into r1 -- which its final resting place */
  54. mov r1, sp
  55. /* Skip to the end of argv and put a pointer to whatever
  56. we find there (hopefully the environment) in r2 */
  57. add r2, r1, r0, lsl #2
  58. add r2, r2, #4
  59. #else
  60. /*
  61. * uClinux stacks look a little different from normal
  62. * MMU-full Linux stacks (for no good reason)
  63. */
  64. /* pull argc, argv and envp off the stack */
  65. ldr r0,[sp, #0]
  66. ldr r1,[sp, #4]
  67. ldr r2,[sp, #8]
  68. #endif
  69. /* Store the address of _init in r3 as an argument to main() */
  70. ldr r3, =_init
  71. /* Push _fini onto the stack as the final argument to main() */
  72. ldr r4, =_fini
  73. stmfd sp!, {r4}
  74. /* Ok, now run uClibc's main() -- shouldn't return */
  75. bl __uClibc_start_main
  76. /* Crash if somehow `exit' returns anyways. */
  77. bl abort
  78. /* Stick in a dummy reference to main(), so that if an application
  79. * is linking when the main() function is in a static library (.a)
  80. * we can be sure that main() actually gets linked in */
  81. L_dummy_main_reference:
  82. .long main