start.S 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* Startup code compliant to the ELF i386 ABI.
  2. Copyright (C) 1995-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. In addition to the permissions in the GNU Lesser General Public
  9. License, the Free Software Foundation gives you unlimited
  10. permission to link the compiled version of this file with other
  11. programs, and to distribute those programs without any restriction
  12. coming from the use of this file. (The GNU Lesser General Public
  13. License restrictions do apply in other respects; for example, they
  14. cover modification of the file, and distribution when not linked
  15. into another program.)
  16. Note that people who make modified versions of this file are not
  17. obligated to grant this special exception for their modified
  18. versions; it is their choice whether to do so. The GNU Lesser
  19. General Public License gives permission to release a modified
  20. version without this exception; this exception also makes it
  21. possible to release a modified version which carries forward this
  22. exception.
  23. The GNU C Library is distributed in the hope that it will be useful,
  24. but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  26. Lesser General Public License for more details.
  27. You should have received a copy of the GNU Lesser General Public
  28. License along with the GNU C Library; if not, see
  29. <https://www.gnu.org/licenses/>. */
  30. /* This is the canonical entry point, usually the first thing in the text
  31. segment. The SVR4/i386 ABI (pages 3-31, 3-32) says that when the entry
  32. point runs, most registers' values are unspecified, except for:
  33. %edx Contains a function pointer to be registered with `atexit'.
  34. This is how the dynamic linker arranges to have DT_FINI
  35. functions called for shared libraries that have been loaded
  36. before this code runs.
  37. %esp The stack contains the arguments and environment:
  38. 0(%esp) argc
  39. 4(%esp) argv[0]
  40. ...
  41. (4*argc)(%esp) NULL
  42. (4*(argc+1))(%esp) envp[0]
  43. ...
  44. NULL
  45. */
  46. #include <sysdep.h>
  47. ENTRY (_start)
  48. /* Clearing frame pointer is insufficient, use CFI. */
  49. cfi_undefined (eip)
  50. /* Clear the frame pointer. The ABI suggests this be done, to mark
  51. the outermost frame obviously. */
  52. xorl %ebp, %ebp
  53. /* Extract the arguments as encoded on the stack and set up
  54. the arguments for `main': argc, argv. envp will be determined
  55. later in __libc_start_main. */
  56. popl %esi /* Pop the argument count. */
  57. movl %esp, %ecx /* argv starts just at the current stack top.*/
  58. /* Before pushing the arguments align the stack to a 16-byte
  59. (SSE needs 16-byte alignment) boundary to avoid penalties from
  60. misaligned accesses. Thanks to Edward Seidl <seidl@janed.com>
  61. for pointing this out. */
  62. andl $0xfffffff0, %esp
  63. pushl %eax /* Push garbage because we allocate
  64. 28 more bytes. */
  65. /* Provide the highest stack address to the user code (for stacks
  66. which grow downwards). */
  67. pushl %esp
  68. pushl %edx /* Push address of the shared library
  69. termination function. */
  70. #ifdef PIC
  71. /* Load PIC register. */
  72. call 1f
  73. addl $_GLOBAL_OFFSET_TABLE_, %ebx
  74. /* This used to be the addresses of .fini and .init. */
  75. pushl $0
  76. pushl $0
  77. pushl %ecx /* Push second argument: argv. */
  78. pushl %esi /* Push first argument: argc. */
  79. # ifdef SHARED
  80. pushl main@GOT(%ebx)
  81. # else
  82. /* Avoid relocation in static PIE since _start is called before
  83. it is relocated. This also avoid rely on linker optimization to
  84. transform 'movl main@GOT(%ebx), %eax' to 'leal main@GOTOFF(%ebx)'
  85. if main is defined locally. */
  86. leal __wrap_main@GOTOFF(%ebx), %eax
  87. pushl %eax
  88. # endif
  89. /* Call the user's main function, and exit with its value.
  90. But let the libc call main. */
  91. call __libc_start_main@PLT
  92. #else
  93. /* This used to be the addresses of .fini and .init. */
  94. pushl $0
  95. pushl $0
  96. pushl %ecx /* Push second argument: argv. */
  97. pushl %esi /* Push first argument: argc. */
  98. pushl $main
  99. /* Call the user's main function, and exit with its value.
  100. But let the libc call main. */
  101. call __libc_start_main
  102. #endif
  103. hlt /* Crash if somehow `exit' does return. */
  104. #ifdef PIC
  105. 1: movl (%esp), %ebx
  106. ret
  107. #endif
  108. #if defined PIC && !defined SHARED
  109. __wrap_main:
  110. jmp main@PLT
  111. #endif
  112. END (_start)
  113. /* To fulfill the System V/i386 ABI we need this symbol. Yuck, it's so
  114. meaningless since we don't support machines < 80386. */
  115. .section .rodata
  116. .globl _fp_hw
  117. _fp_hw: .long 3
  118. .size _fp_hw, 4
  119. .type _fp_hw,@object
  120. /* Define a symbol for the first piece of initialized data. */
  121. .data
  122. .globl __data_start
  123. __data_start:
  124. .long 0
  125. .weak data_start
  126. data_start = __data_start