cprt0.as 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. .globl main
  58. .globl __start
  59. .type __start,@function
  60. __start:
  61. .globl _start
  62. .type _start,@function
  63. _start:
  64. .ent _start
  65. .set noreorder
  66. move $0, $31
  67. bal 10f
  68. nop
  69. 10:
  70. .cpload $31
  71. move $31, $0
  72. .set reorder
  73. /* Setup GP correctly if we're non-PIC. */
  74. la $28,_gp
  75. lui $4, %hi(main_stub) /* main */
  76. addiu $4,$4,%lo(main_stub)
  77. lw $5, 0($29) /* argc */
  78. addiu $6, $29, 4 /* argv */
  79. /* store argc */
  80. lw $t0,0($29)
  81. lui $t1,%hi(operatingsystem_parameter_argc)
  82. sw $t0,%lo(operatingsystem_parameter_argc)($t1)
  83. /* store argv */
  84. addiu $t1,$29,4
  85. lui $t2,%hi(operatingsystem_parameter_argv)
  86. sw $t1,%lo(operatingsystem_parameter_argv)($t2)
  87. /* store envp */
  88. addiu $t2,$t0,1
  89. sll $t2,$t2,0x2
  90. addu $t2,$t2,$t1
  91. lui $t3,%hi(operatingsystem_parameter_envp)
  92. sw $t2,%lo(operatingsystem_parameter_envp)($t3)
  93. /* Allocate space on the stack for seven arguments (o32 only)
  94. and make sure the stack is aligned to double words (8 bytes)
  95. on o32 and quad words (16 bytes) on n32 and n64. */
  96. and $29, -2 * 4
  97. subu $29, 32
  98. lw $7,%got(__libc_csu_init)($gp) /* init */
  99. lw $8,%got(__libc_csu_fini)($gp) /* fini */
  100. sw $8, 16($29) /* fini */
  101. sw $2, 20($29) /* rtld_fini */
  102. sw $29, 24($29) /* stack_end */
  103. lw $t9,%got(__libc_start_main)($gp)
  104. jalr $t9
  105. .end _start
  106. .size _start, . - _start
  107. .globl main_stub
  108. .type main_stub,@function
  109. main_stub:
  110. lui $v0,%hi(__fpc_ret_sp)
  111. sw $sp,%lo(__fpc_ret_sp)($v0)
  112. lui $v0,%hi(__fpc_ret_ra)
  113. sw $ra,%lo(__fpc_ret_ra)($v0)
  114. lui $v0,%hi(main)
  115. addiu $t9,$v0,%lo(main)
  116. jr $t9
  117. nop
  118. .size main_stub,.-main_stub
  119. .globl _haltproc
  120. .ent _haltproc
  121. .type _haltproc,@function
  122. _haltproc:
  123. lui $v0,%hi(__fpc_ret_sp)
  124. lw $sp,%lo(__fpc_ret_sp)($v0)
  125. lui $v0,%hi(__fpc_ret_ra)
  126. lw $ra,%lo(__fpc_ret_ra)($v0)
  127. /* $a0 contains exitcode, we need to move it to $v0,
  128. as it would be at return of C main unction. */
  129. move $v0,$a0
  130. jr $ra
  131. nop
  132. hlt:
  133. b hlt
  134. .end _haltproc
  135. .size _haltproc,.-_haltproc
  136. /* Define a symbol for the first piece of initialized data. */
  137. .data
  138. .globl __data_start
  139. __data_start:
  140. .long 0
  141. .weak data_start
  142. data_start = __data_start
  143. .comm __stkptr,4
  144. .comm __dl_fini,4
  145. .comm __fpc_ret_sp,4
  146. .comm __fpc_ret_ra,4
  147. .comm operatingsystem_parameter_envp,4
  148. .comm operatingsystem_parameter_argc,4
  149. .comm operatingsystem_parameter_argv,4