crt1.S 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. _start:
  61. /* Clear the frame pointer. The ABI suggests this be done, to mark
  62. the outermost frame obviously. */
  63. xorl %ebp, %ebp
  64. /* Extract the arguments as encoded on the stack and set up
  65. the arguments for `main': argc, argv. envp will be determined
  66. later in __libc_start_main. */
  67. popl %esi /* Pop the argument count. */
  68. movl %esp, %ecx /* argv starts just at the current stack top.*/
  69. /* Before pushing the arguments align the stack to a 16-byte
  70. (SSE needs 16-byte alignment) boundary to avoid penalties from
  71. misaligned accesses. Thanks to Edward Seidl <seidl@janed.com>
  72. for pointing this out. */
  73. andl $0xfffffff0, %esp
  74. pushl %eax /* Push garbage because we allocate
  75. 28 more bytes. */
  76. /* Provide the highest stack address to the user code (for stacks
  77. which grow downwards). */
  78. pushl %esp
  79. pushl %edx /* Push address of the shared library
  80. termination function. */
  81. #ifdef __PIC__
  82. /* Load PIC register. */
  83. call .L0
  84. .L0:
  85. pop %ebx
  86. addl $_GLOBAL_OFFSET_TABLE_+[.-.L0],%ebx
  87. /* Push address of our own entry points to .fini and .init. */
  88. pushl _fini@GOT(%ebx)
  89. pushl _init@GOT(%ebx)
  90. pushl %ecx /* Push second argument: argv. */
  91. pushl %esi /* Push first argument: argc. */
  92. pushl main@GOT(%ebx)
  93. /* Call the user's main function, and exit with its value.
  94. But let the libc call main. */
  95. call __uClibc_main@PLT
  96. #else
  97. /* Push address of our own entry points to .fini and .init. */
  98. pushl $_fini
  99. pushl $_init
  100. pushl %ecx /* Push second argument: argv. */
  101. pushl %esi /* Push first argument: argc. */
  102. pushl $main
  103. /* Call the user's main function, and exit with its value.
  104. But let the libc call main. */
  105. call __uClibc_main
  106. #endif
  107. hlt /* Crash if somehow `exit' does return. */
  108. .size _start,.-_start
  109. /* Define a symbol for the first piece of initialized data. */
  110. .data
  111. .global __data_start
  112. __data_start:
  113. .long 0
  114. .weak data_start
  115. data_start = __data_start