crt1.S 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. .type _init,%function
  33. .type _fini,%function
  34. .type main,%function
  35. .type __uClibc_start_main,%function
  36. _start:
  37. /* locate the start of the environment variables */
  38. popl %ecx /* Store argc into %ecx */
  39. movl %esp,%ebx /* Store argv into ebx */
  40. movl %esp,%eax /* Store argv into eax as well*/
  41. movl %edx,%esi /* Store edx(FINI ptr) in %esi */
  42. movl %ecx,%edx /* Stick argc into %edx so we can do some math in a sec */
  43. leal 4(%eax,%edx,4),%eax
  44. /* [ register layout ]
  45. sizeof(char*) == 4
  46. %ecx = argc ; 0(esp)
  47. %ebx = argv ; 4(esp)
  48. %eax = env ; argv + (argc * 4) + 4
  49. */
  50. xorl %ebp,%ebp /* NULL */
  51. /*
  52. Before pushing the arguments align the stack to a 16-byte
  53. (SSE needs 16-byte alignment) boundary to avoid penalties from
  54. misaligned accesses. Thanks to Edward Seidl <seidl@janed.com>
  55. for pointing this out.
  56. */
  57. andl $0xfffffff0, %esp
  58. /* Push NULL to make sure stack ptr
  59. is 16 byte aligned when calling __uClibc_start_main */
  60. pushl %ebp
  61. /* Set up an invalid (NULL return address, NULL frame pointer)
  62. callers stack frame so anybody unrolling the stack knows where
  63. to stop */
  64. pushl %ebp /* callers %cs */
  65. pushl %ebp /* callers %eip (return address) */
  66. pushl %ebp /* callers %ebp (frame pointer) */
  67. movl %esp,%ebp /* mark callers stack frame as invalid */
  68. #if defined L_Scrt1
  69. call .L0
  70. .L0:
  71. pop %edx
  72. addl $_GLOBAL_OFFSET_TABLE_+[.-.L0],%edx
  73. #endif
  74. pushl %esp /* push stack ptr */
  75. pushl %esi /* Push FINI pointer */
  76. /* Push apps .init, .fini and main arguments to __uClibc_start_main() on the stack */
  77. #ifdef L_Scrt1
  78. pushl _fini@GOT(%edx)
  79. pushl _init@GOT(%edx)
  80. #else
  81. pushl $_fini
  82. pushl $_init
  83. #endif
  84. /* Push envp, argc, and argc arguments to __uClibc_start_main() on the stack */
  85. pushl %eax /* Environment pointer */
  86. pushl %ebx /* Argument pointer */
  87. pushl %ecx /* And the argument count */
  88. /* Ok, now run uClibc's main() -- shouldn't return */
  89. #ifdef L_Scrt1
  90. pushl main@GOT(%edx)
  91. call *__uClibc_start_main@GOT(%edx)
  92. #else
  93. pushl $main
  94. call __uClibc_start_main
  95. #endif
  96. /* Crash if somehow `exit' returns anyways. */
  97. hlt
  98. .size _start,.-_start
  99. /* Define a symbol for the first piece of initialized data. */
  100. .data
  101. .globl __data_start
  102. __data_start:
  103. .long 0
  104. .weak data_start
  105. data_start = __data_start