crt0.S 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. */
  10. #include <features.h>
  11. #include <sys/regdef.h>
  12. .text
  13. .global __start
  14. .type __start,%function
  15. .type __uClibc_start_main,%function
  16. .weak _init
  17. .weak _fini
  18. .type __uClibc_start_main,%function
  19. /* Stick in a dummy reference to main(), so that if an application
  20. * is linking when the main() function is in a static library (.a)
  21. * we can be sure that main() actually gets linked in */
  22. .type main,%function
  23. __start:
  24. #ifdef __PIC__
  25. .set noreorder
  26. bltzal zero,0f
  27. nop
  28. 0: .cpload $31
  29. .set reorder
  30. #endif
  31. move $31, zero /* no return, but since the jal kills $31, skip this */
  32. lw a0, 0($29) /* argc */
  33. addu a1, $29, 4 /* get to start of argv */
  34. addu a2, a0, 1 /* argv[0] program name (ordinal->cardinal) */
  35. sll a2, a2, 2 /* multiple by 4 */
  36. add a2, a2, a1 /* a2 now points to start of envp */
  37. la a3, _init /* a3 is address of _init */
  38. addiu sp, sp, -24 /* 16 + 4 rounded up to multiple of 8 */
  39. /* multiple of 8 for longlong/double support */
  40. la v0, _fini
  41. sw v0, 16(sp) /* stack has 5th argument, address of _fini */
  42. /* Ok, now run uClibc's main() -- shouldn't return */
  43. jal __uClibc_start_main
  44. addiu sp, sp, 24 /* undo stack argument */
  45. /* Crash if somehow `exit' returns anyways. */
  46. hlt:
  47. b hlt