start.S 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /* Startup code compliant to the ELF Mips ABI.
  2. Copyright (C) 1995-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. In addition to the permissions in the GNU Lesser General Public
  9. License, the Free Software Foundation gives you unlimited
  10. permission to link the compiled version of this file with other
  11. programs, and to distribute those programs without any restriction
  12. coming from the use of this file. (The GNU Lesser General Public
  13. License restrictions do apply in other respects; for example, they
  14. cover modification of the file, and distribution when not linked
  15. into another program.)
  16. Note that people who make modified versions of this file are not
  17. obligated to grant this special exception for their modified
  18. versions; it is their choice whether to do so. The GNU Lesser
  19. General Public License gives permission to release a modified
  20. version without this exception; this exception also makes it
  21. possible to release a modified version which carries forward this
  22. exception.
  23. The GNU C Library is distributed in the hope that it will be useful,
  24. but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  26. Lesser General Public License for more details.
  27. You should have received a copy of the GNU Lesser General Public
  28. License along with the GNU C Library. If not, see
  29. <http://www.gnu.org/licenses/>. */
  30. #define __ASSEMBLY__ 1
  31. #include <entry.h>
  32. #include <sgidefs.h>
  33. #include <sys/asm.h>
  34. #ifndef ENTRY_POINT
  35. #error ENTRY_POINT needs to be defined for start.S on MIPS/ELF.
  36. #endif
  37. /* This is the canonical entry point, usually the first thing in the text
  38. segment. The SVR4/Mips ABI (pages 3-31, 3-32) says that when the entry
  39. point runs, most registers' values are unspecified, except for:
  40. v0 ($2) Contains a function pointer to be registered with `atexit'.
  41. This is how the dynamic linker arranges to have DT_FINI
  42. functions called for shared libraries that have been loaded
  43. before this code runs.
  44. sp ($29) The stack contains the arguments and environment:
  45. 0(%esp) argc
  46. 4(%esp) argv[0]
  47. ...
  48. (4*argc)(%esp) NULL
  49. (4*(argc+1))(%esp) envp[0]
  50. ...
  51. NULL
  52. ra ($31) The return address register is set to zero so that programs
  53. that search backword through stack frames recognize the last
  54. stack frame.
  55. */
  56. /* We need to call:
  57. __libc_start_main (int (*main) (int, char **, char **), int argc,
  58. char **argv, void (*init) (void), void (*fini) (void),
  59. void (*rtld_fini) (void), void *stack_end)
  60. */
  61. .text
  62. .globl ENTRY_POINT
  63. .type ENTRY_POINT,@function
  64. #ifndef __mips16
  65. ENTRY_POINT:
  66. # ifdef __PIC__
  67. SETUP_GPX($0)
  68. SETUP_GPX64($25,$0)
  69. # else
  70. PTR_LA $28, _gp /* Setup GP correctly if we're non-PIC. */
  71. move $31, $0
  72. # endif
  73. PTR_LA $4, main /* main */
  74. PTR_L $5, 0($29) /* argc */
  75. PTR_ADDIU $6, $29, PTRSIZE /* argv */
  76. /* Allocate space on the stack for seven arguments (o32 only)
  77. and make sure the stack is aligned to double words (8 bytes)
  78. on o32 and quad words (16 bytes) on n32 and n64. */
  79. and $29, -2 * SZREG
  80. # if _MIPS_SIM == _ABIO32
  81. PTR_SUBIU $29, 32
  82. # endif
  83. PTR_LA $7, __libc_csu_init /* init */
  84. PTR_LA $8, __libc_csu_fini
  85. # if _MIPS_SIM == _ABIO32
  86. PTR_S $8, 16($29) /* fini */
  87. PTR_S $2, 20($29) /* rtld_fini */
  88. PTR_S $29, 24($29) /* stack_end */
  89. # else
  90. move $9, $2 /* rtld_fini */
  91. move $10, $29 /* stack_end */
  92. # endif
  93. PTR_LA $25, __libc_start_main
  94. jalr $25
  95. hlt: b hlt /* Crash if somehow it does return. */
  96. #elif _MIPS_SIM == _ABIO32 /* __mips16 */
  97. /* MIPS16 entry point. */
  98. .set mips16
  99. ENTRY_POINT:
  100. # ifdef __PIC__
  101. li $3, %hi(_gp_disp)
  102. addiu $4, $pc, %lo(_gp_disp)
  103. sll $3, 16
  104. addu $3, $4
  105. move $gp, $3
  106. # else
  107. li $3, %hi(_gp)
  108. sll $3, 16
  109. addiu $3, %lo(_gp)
  110. move $gp, $3
  111. # endif
  112. /* Tie end of stack frames. */
  113. li $4, 0
  114. move $31, $4
  115. /* Create new SP value in $7, including alignment. */
  116. li $4, 2 * SZREG
  117. neg $4, $4
  118. move $7, $sp
  119. and $7, $4
  120. addiu $7, -32
  121. /* Load arguments with original SP. */
  122. lw $5, 0($sp)
  123. addiu $6, $sp, PTRSIZE
  124. /* Update SP. */
  125. move $sp, $7
  126. /* Lay out last arguments, and call __libc_start_main(). */
  127. # ifdef __PIC__
  128. sw $7, 24($sp) /* stack_end */
  129. lw $4, %got(__libc_csu_fini)($3)
  130. lw $7, %got(__libc_csu_init)($3) /* init */
  131. sw $4, 16($sp) /* fini */
  132. lw $4, %got(main)($3) /* main */
  133. lw $3, %call16(__libc_start_main)($3)
  134. sw $2, 20($sp) /* rtld_fini */
  135. move $25, $3
  136. jalr $3
  137. # else
  138. lw $4, 1f
  139. sw $7, 24($sp) /* stack_end */
  140. lw $7, 2f /* init */
  141. sw $4, 16($sp) /* fini */
  142. lw $4, 3f /* main */
  143. sw $2, 20($sp) /* rtld_fini */
  144. /* Load and call __libc_start_main(). */
  145. lw $3, 4f
  146. jalr $3
  147. # endif
  148. hlt: b hlt /* Crash if somehow it does return. */
  149. # ifndef __PIC__
  150. .align 2
  151. 1: .word __libc_csu_fini
  152. 2: .word __libc_csu_init
  153. 3: .word main
  154. 4: .word __libc_start_main
  155. # endif
  156. #else /* __mips16 && _MIPS_SIM != _ABIO32 */
  157. # error "MIPS16 support for N32/N64 not implemented"
  158. #endif /* __mips16 */
  159. /* Define a symbol for the first piece of initialized data. */
  160. .data
  161. .globl __data_start
  162. __data_start:
  163. .long 0
  164. .weak data_start
  165. data_start = __data_start