crt1.S 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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, write to the Free
  30. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  31. 02111-1307 USA. */
  32. /* This is the canonical entry point, usually the first thing in the text
  33. segment. The SVR4/i386 ABI (pages 3-31, 3-32) says that when the entry
  34. point runs, most registers' values are unspecified, except for:
  35. %edx Contains a function pointer to be registered with `atexit'.
  36. This is how the dynamic linker arranges to have DT_FINI
  37. functions called for shared libraries that have been loaded
  38. before this code runs.
  39. %esp The stack contains the arguments and environment:
  40. 0(%esp) argc
  41. 4(%esp) argv[0]
  42. ...
  43. (4*argc)(%esp) NULL
  44. (4*(argc+1))(%esp) envp[0]
  45. ...
  46. NULL
  47. */
  48. #include <features.h>
  49. .text
  50. .global _start
  51. .type _start,%function
  52. #if defined(__UCLIBC_CTOR_DTOR__)
  53. .type _init,%function
  54. .type _fini,%function
  55. #else
  56. .weak _init
  57. .weak _fini
  58. #endif
  59. .type main,%function
  60. .type __uClibc_main,%function
  61. _start:
  62. /* Clear the frame pointer. The ABI suggests this be done, to mark
  63. the outermost frame obviously. */
  64. xorl %ebp, %ebp
  65. /* Extract the arguments as encoded on the stack and set up
  66. the arguments for `main': argc, argv. envp will be determined
  67. later in __libc_start_main. */
  68. popl %esi /* Pop the argument count. */
  69. movl %esp, %ecx /* argv starts just at the current stack top.*/
  70. /* Before pushing the arguments align the stack to a 16-byte
  71. (SSE needs 16-byte alignment) boundary to avoid penalties from
  72. misaligned accesses. Thanks to Edward Seidl <seidl@janed.com>
  73. for pointing this out. */
  74. andl $0xfffffff0, %esp
  75. pushl %eax /* Push garbage because we allocate
  76. 28 more bytes. */
  77. /* Provide the highest stack address to the user code (for stacks
  78. which grow downwards). */
  79. pushl %esp
  80. pushl %edx /* Push address of the shared library
  81. termination function. */
  82. #ifdef __PIC__
  83. /* Load PIC register. */
  84. call .L0
  85. .L0:
  86. pop %ebx
  87. addl $_GLOBAL_OFFSET_TABLE_+[.-.L0],%ebx
  88. /* Push address of our own entry points to .fini and .init. */
  89. pushl _fini@GOT(%ebx)
  90. pushl _init@GOT(%ebx)
  91. pushl %ecx /* Push second argument: argv. */
  92. pushl %esi /* Push first argument: argc. */
  93. pushl main@GOT(%ebx)
  94. /* Call the user's main function, and exit with its value.
  95. But let the libc call main. */
  96. call __uClibc_main@PLT
  97. #else
  98. /* Push address of our own entry points to .fini and .init. */
  99. pushl $_fini
  100. pushl $_init
  101. pushl %ecx /* Push second argument: argv. */
  102. pushl %esi /* Push first argument: argc. */
  103. pushl $main
  104. /* Call the user's main function, and exit with its value.
  105. But let the libc call main. */
  106. call __uClibc_main
  107. #endif
  108. hlt /* Crash if somehow `exit' does return. */
  109. .size _start,.-_start
  110. /* Define a symbol for the first piece of initialized data. */
  111. .data
  112. .global __data_start
  113. __data_start:
  114. .long 0
  115. .weak data_start
  116. data_start = __data_start