crt1.S 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* Copyright (C) 2011-2018 Free Software Foundation, Inc.
  2. The GNU C Library is free software; you can redistribute it and/or
  3. modify it under the terms of the GNU Lesser General Public
  4. License as published by the Free Software Foundation; either
  5. version 2.1 of the License, or (at your option) any later version.
  6. In addition to the permissions in the GNU Lesser General Public
  7. License, the Free Software Foundation gives you unlimited
  8. permission to link the compiled version of this file with other
  9. programs, and to distribute those programs without any restriction
  10. coming from the use of this file. (The GNU Lesser General Public
  11. License restrictions do apply in other respects; for example, they
  12. cover modification of the file, and distribution when not linked
  13. into another program.)
  14. Note that people who make modified versions of this file are not
  15. obligated to grant this special exception for their modified
  16. versions; it is their choice whether to do so. The GNU Lesser
  17. General Public License gives permission to release a modified
  18. version without this exception; this exception also makes it
  19. possible to release a modified version which carries forward this
  20. exception.
  21. The GNU C Library is distributed in the hope that it will be useful,
  22. but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  24. Lesser General Public License for more details.
  25. You should have received a copy of the GNU Lesser General Public
  26. License along with the GNU C Library. If not, see
  27. <http://www.gnu.org/licenses/>. */
  28. /* This is the canonical entry point, usually the first thing in the text
  29. segment. The ELF standard tells us that the stack is set up like this on
  30. entry (the left side is the offset from "sp"), in units of
  31. __SIZEOF_POINTER__ entries:
  32. +0 argc
  33. +1 argv[0]
  34. ...
  35. +(argc+1) NULL
  36. +(argc+2) envp[0]
  37. ...
  38. NULL
  39. ... ElfInfo
  40. The ElfInfo is pairs of key/value long words following the envp
  41. pointers and terminated by a zero-valued key.
  42. Although not mandated by the standard, it happens to be the case
  43. that we store the actual argv and envp strings immediately after
  44. the ElfInfo data on the stack.
  45. On entry r0 points to the shared library termination function, or 0
  46. if there isn't one.
  47. */
  48. #include <features.h>
  49. #include <sysdep.h>
  50. #include <arch/abi.h>
  51. /* Just create no-ops if we don't support PC-relative PLT relocations. */
  52. #ifdef NO_PLT_PCREL
  53. # define hw2_last_plt(x) 0
  54. # define hw1_plt(x) 0
  55. # define hw0_plt(x) 0
  56. #endif
  57. .text
  58. .global _start
  59. .type _start,@function
  60. .align 8
  61. _start:
  62. /* Linux starts us with sp pointing at the conventional Elf layout,
  63. but we need to allow two "caller" words for our ABI convention. */
  64. {
  65. /* Load argc (stored as a "long", equivalent to a pointer type). */
  66. LD_PTR r1, sp
  67. /* Save incoming 'sp', which points to the Elf argument block. */
  68. move r52, sp
  69. }
  70. {
  71. /* Allocate stack frame callee space for __libc_start_main. */
  72. ADDI_PTR r12, sp, -(2 * REGSIZE)
  73. }
  74. {
  75. /* Get our PC. */
  76. lnk r13
  77. /* sp is not necessarily properly aligned on startup because
  78. of the way ld.so pops off leading argv elements. So align it. */
  79. andi sp, r12, -8
  80. }
  81. .Lmy_pc:
  82. {
  83. /* Pass the address of the shared library termination function. */
  84. move r5, r0
  85. /* Compute location where __libc_start_main's caller is supposed to
  86. store its frame pointer. */
  87. ADDI_PTR r12, sp, REGSIZE
  88. /* Zero out callee space for return address. Unnecessary but free.
  89. This is just paranoia to help backtracing not go awry. */
  90. st sp, zero
  91. }
  92. {
  93. /* Zero out our frame pointer for __libc_start_main. */
  94. st r12, zero
  95. /* Zero out lr to make __libc_start_main the end of backtrace. */
  96. move lr, zero
  97. /* Compute a pointer to argv. envp will be determined
  98. later in __libc_start_main. We set up the first argument
  99. (the address of main) below. */
  100. ADDI_PTR r2, r52, __SIZEOF_POINTER__
  101. }
  102. {
  103. /* Pass the highest stack address to user code. */
  104. ADDI_PTR r6, sp, (2 * REGSIZE)
  105. /* Pass address of main() in r0, and of our own entry
  106. points to .fini and .init in r3 and r4. */
  107. moveli r0, hw2_last(main - .Lmy_pc)
  108. }
  109. {
  110. shl16insli r0, r0, hw1(main - .Lmy_pc)
  111. moveli r3, hw2_last(_init - .Lmy_pc)
  112. }
  113. {
  114. shl16insli r0, r0, hw0(main - .Lmy_pc)
  115. shl16insli r3, r3, hw1(_init - .Lmy_pc)
  116. }
  117. {
  118. ADD_PTR r0, r0, r13
  119. shl16insli r3, r3, hw0(_init - .Lmy_pc)
  120. }
  121. {
  122. moveli r12, hw2_last_plt(__uClibc_main - .Lmy_pc)
  123. ADD_PTR r3, r3, r13
  124. }
  125. {
  126. shl16insli r12, r12, hw1_plt(__uClibc_main - .Lmy_pc)
  127. moveli r4, hw2_last(_fini - .Lmy_pc)
  128. }
  129. {
  130. shl16insli r12, r12, hw0_plt(__uClibc_main - .Lmy_pc)
  131. shl16insli r4, r4, hw1(_fini - .Lmy_pc)
  132. }
  133. {
  134. ADD_PTR r12, r12, r13
  135. shl16insli r4, r4, hw0(_fini - .Lmy_pc)
  136. }
  137. {
  138. ADD_PTR r4, r4, r13
  139. #ifdef NO_PLT_PCREL
  140. j plt(__uClibc_main)
  141. #else
  142. jr r12
  143. #endif
  144. }
  145. {
  146. /* Tell backtracer to give up (_start has no caller). */
  147. info INFO_OP_CANNOT_BACKTRACE
  148. }
  149. .size _start, .-_start
  150. /* Define a symbol for the first piece of initialized data. */
  151. .data
  152. .global __data_start
  153. .align 8
  154. __data_start:
  155. .long 0
  156. .weak data_start
  157. data_start = __data_start