cprt0.as 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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., 51 Franklin Street, Fifth Floor, Boston, MA
  31. 02110-1301 USA. */
  32. /* This is the canonical entry point, usually the first thing in the text
  33. segment. The SVR4/Mips ABI (pages 3-31, 3-32) says that when the entry
  34. point runs, most registers' values are unspecified, except for:
  35. v0 ($2) Contains a function pointer to be registered with `atexit'.
  36. This is how the dynamic linker arranges to have DT_FINI
  37. functions called for shared libraries that have been loaded
  38. before this code runs.
  39. sp ($29) The stack contains the arguments and environment:
  40. 0(%esp) argc
  41. 4(%esp) argv[0]
  42. ...
  43. (4*argc)(%esp) NULL
  44. (4*(argc+1))(%esp) envp[0]
  45. ...
  46. NULL
  47. ra ($31) The return address register is set to zero so that programs
  48. that search backword through stack frames recognize the last
  49. stack frame.
  50. */
  51. /* We need to call:
  52. __libc_start_main (int (*main) (int, char **, char **), int argc,
  53. char **argv, void (*init) (void), void (*fini) (void),
  54. void (*rtld_fini) (void), void *stack_end)
  55. */
  56. .text
  57. .align 2
  58. .globl __libc_csu_init
  59. .type __libc_csu_init, @function
  60. __libc_csu_init:
  61. .set nomips16
  62. .weak __libc_csu_init
  63. .ent __libc_csu_init
  64. .frame $sp,0,$ra
  65. .mask 0x00000000,0
  66. .fmask 0x00000000,0
  67. .set noreorder
  68. .set nomacro
  69. jr $31
  70. nop
  71. .set macro
  72. .set reorder
  73. .end __libc_csu_init
  74. .size __libc_csu_init, .-__libc_csu_init
  75. .align 2
  76. .globl __libc_csu_fini
  77. .type __libc_csu_fini, @function
  78. __libc_csu_fini:
  79. .set nomips16
  80. .weak __libc_csu_fini
  81. .ent __libc_csu_fini
  82. .frame $sp,0,$ra
  83. .mask 0x00000000,0
  84. .fmask 0x00000000,0
  85. .set noreorder
  86. .set nomacro
  87. jr $31
  88. nop
  89. .set macro
  90. .set reorder
  91. .end __libc_csu_fini
  92. .size __libc_csu_fini, .-__libc_csu_fini
  93. .align 2
  94. .globl main
  95. .globl __start
  96. .type __start,@function
  97. __start:
  98. .globl _start
  99. .type _start,@function
  100. _start:
  101. .ent _start
  102. .set noreorder
  103. move $0, $31
  104. bal 10f
  105. nop
  106. 10:
  107. .cpload $31
  108. move $31, $0
  109. .set reorder
  110. /* Setup GP correctly if we're non-PIC. */
  111. la $28,_gp
  112. lui $4, %hi(main_stub) /* main */
  113. addiu $4,$4,%lo(main_stub)
  114. lw $5, 0($29) /* argc */
  115. addiu $6, $29, 4 /* argv */
  116. /* store argc */
  117. lw $t0,0($29)
  118. lui $t1,%hi(operatingsystem_parameter_argc)
  119. sw $t0,%lo(operatingsystem_parameter_argc)($t1)
  120. /* store argv */
  121. addiu $t1,$29,4
  122. lui $t2,%hi(operatingsystem_parameter_argv)
  123. sw $t1,%lo(operatingsystem_parameter_argv)($t2)
  124. /* store envp */
  125. addiu $t2,$t0,1
  126. sll $t2,$t2,0x2
  127. addu $t2,$t2,$t1
  128. lui $t3,%hi(operatingsystem_parameter_envp)
  129. sw $t2,%lo(operatingsystem_parameter_envp)($t3)
  130. /* Allocate space on the stack for seven arguments (o32 only)
  131. and make sure the stack is aligned to double words (8 bytes)
  132. on o32 and quad words (16 bytes) on n32 and n64. */
  133. and $29, -2 * 4
  134. subu $29, 32
  135. lw $7,%got(__libc_csu_init)($gp) /* init */
  136. lw $8,%got(__libc_csu_fini)($gp) /* fini */
  137. sw $8, 16($29) /* fini */
  138. sw $2, 20($29) /* rtld_fini */
  139. sw $29, 24($29) /* stack_end */
  140. lw $t9,%got(__libc_start_main)($gp)
  141. jalr $t9
  142. .end _start
  143. .size _start, . - _start
  144. .globl main_stub
  145. .type main_stub,@function
  146. main_stub:
  147. /* load fp */
  148. move $s8,$sp
  149. /* set __stkptr value to $s8 */
  150. lui $v0,%hi(__stkptr)
  151. sw $s8,%lo(__stkptr)($v0)
  152. lui $v0,%hi(__fpc_ret_sp)
  153. sw $sp,%lo(__fpc_ret_sp)($v0)
  154. lui $v0,%hi(__fpc_ret_ra)
  155. sw $ra,%lo(__fpc_ret_ra)($v0)
  156. lui $v0,%hi(main)
  157. addiu $t9,$v0,%lo(main)
  158. jr $t9
  159. nop
  160. .size main_stub,.-main_stub
  161. .globl _haltproc
  162. .ent _haltproc
  163. .type _haltproc,@function
  164. _haltproc:
  165. lui $v0,%hi(__fpc_ret_sp)
  166. lw $sp,%lo(__fpc_ret_sp)($v0)
  167. lui $v0,%hi(__fpc_ret_ra)
  168. lw $ra,%lo(__fpc_ret_ra)($v0)
  169. /* $a0 contains exitcode, we need to move it to $v0,
  170. as it would be at return of C main unction. */
  171. move $v0,$a0
  172. jr $ra
  173. nop
  174. hlt:
  175. b hlt
  176. .end _haltproc
  177. .size _haltproc,.-_haltproc
  178. /* Define a symbol for the first piece of initialized data. */
  179. .data
  180. .globl __data_start
  181. __data_start:
  182. .long 0
  183. .weak data_start
  184. data_start = __data_start
  185. .comm __stkptr,4
  186. .comm __dl_fini,4
  187. .comm __fpc_ret_sp,4
  188. .comm __fpc_ret_ra,4
  189. .comm operatingsystem_parameter_envp,4
  190. .comm operatingsystem_parameter_argc,4
  191. .comm operatingsystem_parameter_argv,4
  192. /* Also define _IO_stdin_used as it is used to */
  193. /* distinguish different libc version */
  194. .section .rodata
  195. .globl _IO_stdin_used
  196. _IO_stdin_used:
  197. .long 0x20001