crt1.S 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* Startup code compliant to the ELF i386 ABI.
  2. Copyright (C) 1995, 1996, 1997, 1998, 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. /* This is the canonical entry point, usually the first thing in the text
  32. segment. The SVR4/i386 ABI (pages 3-31, 3-32) says that when the entry
  33. point runs, most registers' values are unspecified, except for:
  34. %edx Contains a function pointer to be registered with `atexit'.
  35. This is how the dynamic linker arranges to have DT_FINI
  36. functions called for shared libraries that have been loaded
  37. before this code runs.
  38. %esp The stack contains the arguments and environment:
  39. 0(%esp) argc
  40. 4(%esp) argv[0]
  41. ...
  42. (4*argc)(%esp) NULL
  43. (4*(argc+1))(%esp) envp[0]
  44. ...
  45. NULL
  46. */
  47. #include <features.h>
  48. .text
  49. .global _start
  50. .type _start,%function
  51. #if defined(__UCLIBC_CTOR_DTOR__)
  52. .type _init,%function
  53. .type _fini,%function
  54. #else
  55. .weak _init
  56. .weak _fini
  57. #endif
  58. .type main,%function
  59. .type __uClibc_main,%function
  60. #ifdef L_rcrt1
  61. .type reloc_static_pie,%function
  62. #endif
  63. _start:
  64. /* Clear the frame pointer. The ABI suggests this be done, to mark
  65. the outermost frame obviously. */
  66. xorl %ebp, %ebp
  67. /* Extract the arguments as encoded on the stack and set up
  68. the arguments for `main': argc, argv. envp will be determined
  69. later in __libc_start_main. */
  70. popl %esi /* Pop the argument count. */
  71. movl %esp, %ecx /* argv starts just at the current stack top.*/
  72. /* Before pushing the arguments align the stack to a 16-byte
  73. (SSE needs 16-byte alignment) boundary to avoid penalties from
  74. misaligned accesses. Thanks to Edward Seidl <seidl@janed.com>
  75. for pointing this out. */
  76. andl $0xfffffff0, %esp
  77. pushl %eax /* Push garbage because we allocate
  78. 28 more bytes. */
  79. /* Provide the highest stack address to the user code (for stacks
  80. which grow downwards). */
  81. pushl %esp
  82. pushl %edx /* Push address of the shared library
  83. termination function. */
  84. #ifdef __PIC__
  85. /* Load PIC register. */
  86. call .L0
  87. .L0:
  88. pop %ebx
  89. addl $_GLOBAL_OFFSET_TABLE_+[.-.L0],%ebx
  90. #ifdef L_rcrt1
  91. /* We cannot rely on _DYNAMIC being usable here due to RELRO.
  92. Instead we calculate the load address based off a symbol
  93. that we know will exist, _start. */
  94. pushl %ecx /* Save ecx so it won't get clobbered */
  95. pushl %ebx /* Save ebx so it won't get clobbered */
  96. xorl %ecx, %ecx /* Clear ecx */
  97. addl _start@GOT(%ebx), %ecx /* Get the offset of _start */
  98. movl _start@GOT(%ebx), %eax /* Get the run time address of _start */
  99. subl %ecx, %eax /* Subtract to find the load address */
  100. pushl %eax /* Pass the load address */
  101. call reloc_static_pie@PLT
  102. popl %eax /* Clean up from function call */
  103. popl %ebx /* Restore the GOT address */
  104. popl %ecx /* restore ecx */
  105. #endif
  106. /* Push address of our own entry points to .fini and .init. */
  107. pushl _fini@GOT(%ebx)
  108. pushl _init@GOT(%ebx)
  109. pushl %ecx /* Push second argument: argv. */
  110. pushl %esi /* Push first argument: argc. */
  111. pushl main@GOT(%ebx)
  112. /* Call the user's main function, and exit with its value.
  113. But let the libc call main. */
  114. call __uClibc_main@PLT
  115. #else
  116. /* Push address of our own entry points to .fini and .init. */
  117. pushl $_fini
  118. pushl $_init
  119. pushl %ecx /* Push second argument: argv. */
  120. pushl %esi /* Push first argument: argc. */
  121. pushl $main
  122. /* Call the user's main function, and exit with its value.
  123. But let the libc call main. */
  124. call __uClibc_main
  125. #endif
  126. hlt /* Crash if somehow `exit' does return. */
  127. .size _start,.-_start
  128. /* Define a symbol for the first piece of initialized data. */
  129. .data
  130. .global __data_start
  131. __data_start:
  132. .long 0
  133. .weak data_start
  134. data_start = __data_start