crt0.S 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Library General Public License as
  5. published by the Free Software Foundation; either version 2 of the
  6. License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public
  12. License along with the GNU C Library; see the file COPYING.LIB. If
  13. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  14. Cambridge, MA 02139, USA. */
  15. /* Based on the code from GNU libc, but hacked up by John Beppu and Erik Andersen */
  16. /* adapted by PaX Team for ET_DYN/PIE binaries */
  17. /*
  18. When we enter this piece of code, the program stack looks like this:
  19. argc argument counter (integer)
  20. argv[0] program name (pointer)
  21. argv[1...N] program args (pointers)
  22. argv[argc-1] end of args (integer)
  23. NULL
  24. env[0...N] environment variables (pointers)
  25. NULL
  26. */
  27. #include <features.h>
  28. .text
  29. .align 4
  30. .global _start
  31. .type _start,%function
  32. #if defined L_crt0 || defined L_Scrt0 || ! defined __UCLIBC_CTOR_DTOR__
  33. .type __uClibc_main,%function
  34. #else
  35. .weak _init
  36. .weak _fini
  37. .type __uClibc_start_main,%function
  38. #endif
  39. /* Stick in a dummy reference to main(), so that if an application
  40. * is linking when the main() function is in a static library (.a)
  41. * we can be sure that main() actually gets linked in */
  42. .type main,%function
  43. _start:
  44. /* locate the start of the environment variables */
  45. popl %ecx /* Store argc into %ecx */
  46. movl %esp,%ebx /* Store argv into ebx */
  47. movl %esp,%eax /* Store argv into eax as well*/
  48. movl %ecx,%edx /* Stick argc into %edx so we can do some math in a sec */
  49. leal 4(%eax,%edx,4),%eax
  50. /* [ register layout ]
  51. sizeof(char*) == 4
  52. %ecx = argc ; 0(esp)
  53. %ebx = argv ; 4(esp)
  54. %eax = env ; argv + (argc * 4) + 4
  55. */
  56. /* Set up an invalid (NULL return address, NULL frame pointer)
  57. callers stack frame so anybody unrolling the stack knows where
  58. to stop */
  59. xorl %ebp,%ebp /* NULL */
  60. pushl %ebp /* callers %cs */
  61. pushl %ebp /* callers %eip (return address) */
  62. pushl %ebp /* callers %ebp (frame pointer) */
  63. movl %esp,%ebp /* mark callers stack frame as invalid */
  64. #if defined L_Scrt0 || defined L_Scrt1
  65. call .L0
  66. .L0:
  67. pop %edx
  68. addl $_GLOBAL_OFFSET_TABLE_+[.-.L0],%edx
  69. #endif
  70. #if (defined L_crt1 || defined L_Scrt1 || defined L_gcrt1 ) && defined __UCLIBC_CTOR_DTOR__
  71. /* Push .init and .fini arguments to __uClibc_start_main() on the stack */
  72. #ifdef L_Scrt1
  73. pushl _fini@GOT(%edx)
  74. pushl _init@GOT(%edx)
  75. #else
  76. pushl $_fini
  77. pushl $_init
  78. #endif
  79. /* Push envp, argc, and argc arguments to __uClibc_start_main() on the stack */
  80. pushl %eax /* Environment pointer */
  81. pushl %ebx /* Argument pointer */
  82. pushl %ecx /* And the argument count */
  83. /* Ok, now run uClibc's main() -- shouldn't return */
  84. #ifdef L_Scrt1
  85. call *__uClibc_start_main@GOT(%edx)
  86. #else
  87. call __uClibc_start_main
  88. #endif
  89. #else
  90. /* Push envp, argc, and argc arguments to __uClibc_start_main() on the stack */
  91. pushl %eax /* Environment pointer */
  92. pushl %ebx /* Argument pointer */
  93. pushl %ecx /* And the argument count */
  94. #ifdef L_Scrt0
  95. call *__uClibc_main@GOT(%edx)
  96. #else
  97. call __uClibc_main
  98. #endif
  99. #endif
  100. /* Crash if somehow `exit' returns anyways. */
  101. hlt
  102. .size _start,.-_start
  103. /* Define a symbol for the first piece of initialized data. */
  104. .data
  105. .globl __data_start
  106. __data_start:
  107. .long 0
  108. .weak data_start
  109. data_start = __data_start
  110. #if defined L_gcrt1 && defined __UCLIBC_PROFILING__
  111. # include "./gmon-start.S"
  112. #endif