crt1.S 4.5 KB

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