crt1.S 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* Startup code compliant to the ELF Mips ABI.
  2. Copyright (C) 1995, 1997, 2000, 2001, 2002, 2003, 2004
  3. Free Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. In addition to the permissions in the GNU Lesser General Public
  10. License, the Free Software Foundation gives you unlimited
  11. permission to link the compiled version of this file with other
  12. programs, and to distribute those programs without any restriction
  13. coming from the use of this file. (The GNU Lesser General Public
  14. License restrictions do apply in other respects; for example, they
  15. cover modification of the file, and distribution when not linked
  16. into another program.)
  17. Note that people who make modified versions of this file are not
  18. obligated to grant this special exception for their modified
  19. versions; it is their choice whether to do so. The GNU Lesser
  20. General Public License gives permission to release a modified
  21. version without this exception; this exception also makes it
  22. possible to release a modified version which carries forward this
  23. exception.
  24. The GNU C Library is distributed in the hope that it will be useful,
  25. but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  27. Lesser General Public License for more details.
  28. You should have received a copy of the GNU Lesser General Public
  29. License along with the GNU C Library; if not, write to the Free
  30. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  31. 02111-1307 USA. */
  32. #include <sys/regdef.h>
  33. #include <sys/asm.h>
  34. #include <features.h>
  35. /* This is the canonical entry point, usually the first thing in the text
  36. segment. The SVR4/Mips ABI (pages 3-31, 3-32) says that when the entry
  37. point runs, most registers' values are unspecified, except for:
  38. v0 ($2) Contains a function pointer to be registered with `atexit'.
  39. This is how the dynamic linker arranges to have DT_FINI
  40. functions called for shared libraries that have been loaded
  41. before this code runs.
  42. sp ($29) The stack contains the arguments and environment:
  43. 0(%esp) argc
  44. 4(%esp) argv[0]
  45. ...
  46. (4*argc)(%esp) NULL
  47. (4*(argc+1))(%esp) envp[0]
  48. ...
  49. NULL
  50. ra ($31) The return address register is set to zero so that programs
  51. that search backword through stack frames recognize the last
  52. stack frame.
  53. */
  54. /* We need to call:
  55. __uClibc_main (int (*main) (int, char **, char **), int argc,
  56. char **argv, void (*init) (void), void (*fini) (void),
  57. void (*rtld_fini) (void), void *stack_end)
  58. */
  59. .text
  60. .globl __start
  61. .type __start,@function
  62. .type _init,@function
  63. .type _fini,@function
  64. #ifndef __UCLIBC_CTOR_DTOR__
  65. .weak _init
  66. .weak _fini
  67. #endif
  68. .type main,@function
  69. .type __uClibc_main,@function
  70. .ent __start
  71. __start:
  72. #ifdef __PIC__
  73. #if _MIPS_SIM == _MIPS_SIM_ABI32
  74. .frame sp, 24, sp
  75. .set noreorder
  76. move $0, $31 /* Save old ra. */
  77. bal 10f /* Find addr of cpload. */
  78. nop
  79. 10:
  80. .cpload $31
  81. move $31, $0
  82. .set reorder
  83. .cprestore 16
  84. #else
  85. move $0, $31; /* Save old ra. */
  86. .set noreorder
  87. bal 10f /* Find addr of .cpsetup. */
  88. nop
  89. 10:
  90. .set reorder
  91. .cpsetup $31, $25, 10b
  92. move $31, $0
  93. #endif
  94. #else
  95. la $28, _gp /* Setup GP correctly if we're non-PIC. */
  96. move $31, $0
  97. #endif
  98. PTR_LA $4, main /* main */
  99. PTR_L $5, 0($29) /* argc */
  100. PTR_ADDIU $6, $29, PTRSIZE /* argv */
  101. /* Allocate space on the stack for seven arguments and
  102. * make sure the stack is aligned to double words (8 bytes) */
  103. #if _MIPS_SIM == _MIPS_SIM_ABI32
  104. and $29, -2 * 4
  105. subu $29, 32
  106. la $7, _init /* init */
  107. la $8, _fini
  108. sw $8, 16($29) /* fini */
  109. sw $2, 20($29) /* rtld_fini */
  110. sw $29, 24($29) /* stack_end */
  111. #else
  112. and $29, -2 * PTRSIZE
  113. PTR_LA $7, _init /* init */
  114. PTR_LA $8, _fini /* fini */
  115. move $9, $2 /* rtld_fini */
  116. move $10, $29 /* stack_end */
  117. #endif
  118. jal __uClibc_main
  119. hlt:
  120. /* Crash if somehow `__uClibc_main' returns anyway. */
  121. b hlt
  122. .size __start,.-__start
  123. .end __start
  124. /* Define a symbol for the first piece of initialized data. */
  125. .data
  126. .globl __data_start
  127. __data_start:
  128. .long 0
  129. .weak data_start
  130. data_start = __data_start