crt1.S 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright (C) 2003 by Erik Andersen
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU Library General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or (at your
  7. * option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
  12. * for more details.
  13. *
  14. * You should have received a copy of the GNU Library General Public License
  15. * along with this program; see the file COPYING.LIB. If not, see
  16. * <http://www.gnu.org/licenses/>.
  17. */
  18. #include <features.h>
  19. /* Integer registers. */
  20. #define r0 0
  21. #define r1 1
  22. #define r2 2
  23. #define r3 3
  24. #define r4 4
  25. #define r5 5
  26. #define r6 6
  27. #define r7 7
  28. #define r8 8
  29. #define r9 9
  30. #define r10 10
  31. #define r13 13
  32. #define r31 31
  33. .text
  34. .globl _start
  35. .type _start,%function
  36. .type _init,%function
  37. .type _fini,%function
  38. #ifndef __UCLIBC_CTOR_DTOR__
  39. .weak _init
  40. .weak _fini
  41. #endif
  42. #ifdef L_rcrt1
  43. .type reloc_static_pie,%function
  44. #endif
  45. .type main,%function
  46. .type __uClibc_main,%function
  47. _start:
  48. mr r9,r1 /* Save the stack pointer and pass it to __uClibc_main */
  49. clrrwi r1,r1,4 /* Align stack ptr to 16 bytes */
  50. #ifdef __PIC__
  51. # ifdef HAVE_ASM_PPC_REL16
  52. bcl 20,31,1f
  53. 1: mflr r31
  54. addis r31,r31,_GLOBAL_OFFSET_TABLE_-1b@ha
  55. addi r31,r31,_GLOBAL_OFFSET_TABLE_-1b@l
  56. # else
  57. bl _GLOBAL_OFFSET_TABLE_-4@local
  58. mflr r31
  59. # endif
  60. /* in PIC/PIE, plt stubs need r30 to point to the GOT if using secure-plt */
  61. # ifdef PPC_HAS_SECUREPLT
  62. mr 30,31
  63. # endif
  64. #ifdef L_rcrt1
  65. stwu r3, -4(r1) /* Save r3 */
  66. stwu r9, -16(r1) /* Save r9 */
  67. bcl 20,31,2f /* Jump to label 2 */
  68. 2: mflr r3 /* Load lr into r3 */
  69. addis r3, r3, _DYNAMIC-2b@ha /* Add high half of _DYNAMIC to r3 */
  70. addi r3,r3,_DYNAMIC-2b@l /* Add low half of _DYNAMIC */
  71. lwz r4, 0(r31) /* load _DYNAMIC from the GOT */
  72. subf r3, r4, r3 /* sub _DYNAMIC@got and it's actual address */
  73. bl reloc_static_pie /* Call reloc_static_pie */
  74. lwzu r9, 0(r1) /* restore r9 */
  75. addi r1, r1, 16 /* update stack pointer */
  76. lwzu r3, 0(r1) /* restore r3 */
  77. addi r1, r1, 4 /* update stack pointer */
  78. li r5, 0 /* zero r5 */
  79. #endif
  80. #endif
  81. /* Set up the small data pointer in r13. */
  82. #ifdef __PIC__
  83. lwz r13,_SDA_BASE_@got(r31)
  84. #else
  85. lis r13,_SDA_BASE_@ha
  86. addi r13,r13,_SDA_BASE_@l
  87. #endif
  88. /* Set up an initial stack frame, and clear the LR. */
  89. li r0,0
  90. stwu r1,-16(r1)
  91. mtlr r0
  92. stw r0,0(r1)
  93. /* find argc from the stack pointer */
  94. lwz r4,0(r9)
  95. /* find argv one word offset from the stack pointer */
  96. addi r5,r9,4
  97. mr r8,r3 /* Pass _dl_fini from ldso or NULL if statically linked */
  98. /* Note: PPC depends on the kernel to zero r3 before */
  99. /* handing over to user space, otherwise static apps */
  100. /* will SEGV during exit() */
  101. /* Ok, now run uClibc's main() -- shouldn't return */
  102. #ifdef __PIC__
  103. lwz r6,_init@got(r31)
  104. lwz r7,_fini@got(r31)
  105. lwz r3,main@got(r31)
  106. b __uClibc_main@plt
  107. #else
  108. lis r6,_init@ha # load top 16 bits
  109. addi r6,r6,_init@l # load bottom 16 bits
  110. lis r7,_fini@ha # load top 16 bits
  111. addi r7,r7,_fini@l # load bottom 16 bits
  112. lis r3,main@ha # load top 16 bits
  113. addi r3,r3,main@l # load bottom 16 bits
  114. b __uClibc_main
  115. #endif
  116. .size _start,.-_start
  117. /* Define a symbol for the first piece of initialized data. */
  118. .data
  119. .globl __data_start
  120. __data_start:
  121. .long 0
  122. .weak data_start
  123. data_start = __data_start