crt0.S 1.6 KB

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