crt1.S 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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, see
  30. <http://www.gnu.org/licenses/>. */
  31. #include <sys/regdef.h>
  32. #include <sys/asm.h>
  33. #include <features.h>
  34. /* This is the canonical entry point, usually the first thing in the text
  35. segment. The SVR4/Mips ABI (pages 3-31, 3-32) says that when the entry
  36. point runs, most registers' values are unspecified, except for:
  37. v0 ($2) Contains a function pointer to be registered with `atexit'.
  38. This is how the dynamic linker arranges to have DT_FINI
  39. functions called for shared libraries that have been loaded
  40. before this code runs.
  41. sp ($29) The stack contains the arguments and environment:
  42. 0(%esp) argc
  43. 4(%esp) argv[0]
  44. ...
  45. (4*argc)(%esp) NULL
  46. (4*(argc+1))(%esp) envp[0]
  47. ...
  48. NULL
  49. ra ($31) The return address register is set to zero so that programs
  50. that search backword through stack frames recognize the last
  51. stack frame.
  52. */
  53. /* We need to call:
  54. __uClibc_main (int (*main) (int, char **, char **), int argc,
  55. char **argv, void (*init) (void), void (*fini) (void),
  56. void (*rtld_fini) (void), void *stack_end)
  57. */
  58. .text
  59. .globl __start
  60. .type __start,@function
  61. .type _init,@function
  62. .type _fini,@function
  63. #ifndef __UCLIBC_CTOR_DTOR__
  64. .weak _init
  65. .weak _fini
  66. #endif
  67. #ifdef L_rcrt1
  68. .type reloc_static_pie,@function
  69. .hidden .L0
  70. #endif
  71. .type main,@function
  72. .type __uClibc_main,@function
  73. .ent __start
  74. __start:
  75. #ifdef __PIC__
  76. SETUP_GPX($0)
  77. SETUP_GPX64($25,$0)
  78. #else
  79. PTR_LA $28, _gp /* Setup GP correctly if we're non-PIC. */
  80. move $31, $0
  81. #endif
  82. #ifdef L_rcrt1
  83. PTR_LA $4, _DYNAMIC /* Place _DYNAMIC into the GOT */
  84. REG_S $4, -0x7ff0($28) /* offset to GOT stolen from dl-startup */
  85. jal .L0 /* Get the current $pc address */
  86. .L0:
  87. PTR_SUBU $4, $31, $25 /* Calculate load addr */
  88. move $31, $0 /* Clear ra */
  89. and $29, -2 * SZREG /* Ensure stack is aligned */
  90. PTR_ADDIU $29, (-2 * SZREG) /* Allocate 2 register spaces on stack */
  91. REG_S $2, SZREG($29) /* Store atexit in case it exists */
  92. PTR_LA $5, reloc_static_pie /* function calls before relocation
  93. don't work unless we set $t9 manually */
  94. PTR_ADDU $25, $4, $5 /* store reloc_static_pie in $t9 */
  95. jalr $25 /* call reloc_static_pie */
  96. nop /* delay slot, just in case */
  97. REG_L $2, SZREG($29) /* cleanup stack */
  98. PTR_ADDIU $29, $29, (2 * SZREG)
  99. #endif
  100. PTR_LA $4, main /* main */
  101. PTR_L $5, 0($29) /* argc */
  102. PTR_ADDIU $6, $29, PTRSIZE /* argv */
  103. /* Allocate space on the stack for seven arguments and
  104. * make sure the stack is aligned to double words (8 bytes) */
  105. and $29, -2 * SZREG
  106. #if _MIPS_SIM == _MIPS_SIM_ABI32
  107. PTR_SUBIU $29, 32
  108. #endif
  109. PTR_LA $7, _init /* init */
  110. PTR_LA $8, _fini
  111. #if _MIPS_SIM == _MIPS_SIM_ABI32
  112. PTR_S $8, 16($29) /* fini */
  113. PTR_S $2, 20($29) /* rtld_fini */
  114. PTR_S $29, 24($29) /* stack_end */
  115. #else
  116. move $9, $2 /* rtld_fini */
  117. move $10, $29 /* stack_end */
  118. #endif
  119. jal __uClibc_main
  120. hlt:
  121. /* Crash if somehow `__uClibc_main' returns anyway. */
  122. b hlt
  123. .size __start,.-__start
  124. .end __start
  125. /* Define a symbol for the first piece of initialized data. */
  126. .data
  127. .globl __data_start
  128. __data_start:
  129. .long 0
  130. .weak data_start
  131. data_start = __data_start