crt0.S 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. When we are done here, we want
  10. a1=argc
  11. a2=argv[0]
  12. a3=argv[argc+1]
  13. This file now uses the register naming from the ARM Procedure Calling Standard
  14. Name Number APCS Role
  15. a1 0 argument 1 / integer result / scratch register / argc
  16. a2 1 argument 2 / scratch register / argv
  17. a3 2 argument 3 / scratch register / envp
  18. a4 3 argument 4 / scratch register
  19. v1 4 register variable
  20. v2 5 register variable
  21. v3 6 register variable
  22. v4 7 register variable
  23. v5 8 register variable
  24. sb/v6 9 static base / register variable
  25. sl/v7 10 stack limit / stack chunk handle / reg. variable
  26. fp 11 frame pointer
  27. ip 12 scratch register / new-sb in inter-link-unit calls
  28. sp 13 lower end of current stack frame
  29. lr 14 link address / scratch register
  30. pc 15 program counter
  31. */
  32. .text
  33. .align 2
  34. .global __environ
  35. .global _start
  36. .global exit
  37. .global main
  38. .type _start,%function
  39. .type exit,%function
  40. .type main,%function
  41. .text
  42. _start:
  43. /* clear the frame pointer */
  44. mov fp, #0
  45. /* Load register a1 (argc) from the stack to its final resting place */
  46. ldr a1, [sp], #4
  47. /* Copy argv pointer into a2 -- which its final resting place */
  48. mov a2, sp
  49. /* Set up environ, skip to the end of argv, and put
  50. * a pointer to whatever we find there (hopefully the
  51. environment) in a3 */
  52. ldr a4, .L3
  53. add a3, a2, a1, lsl #2
  54. add a3, a3, #4
  55. str a3, [a4, #0]
  56. bl main
  57. bl exit
  58. .align 2
  59. .L3:
  60. .word __environ
  61. .data
  62. .align 2
  63. .global __environ
  64. __environ:
  65. .long 0
  66. .weak environ
  67. environ = __environ