crt0.S 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. /* If you don't know what is going on here, check out
  16. http://linuxassembly.org/startup.html
  17. For a description of the stack layout.
  18. -John Beppu, and Erik Andersen
  19. */
  20. .global __environ
  21. .global __errno
  22. .global _start
  23. .global exit
  24. .global main
  25. .global __stdio_close_all
  26. .global _void_void_null_func
  27. .global _start_exit
  28. .text
  29. _start:
  30. /* First locate the start of the environment variables */
  31. popl %ecx /* Store argc into %ecx */
  32. movl %esp,%ebx /* Store argv into ebx */
  33. movl %esp,%eax /* Store argv into eax as well*/
  34. movl %ecx,%edx /* Stick argc into %edx so we can do some math in a sec */
  35. leal 4(%eax,%edx,4),%eax
  36. /* [ register layout ]
  37. sizeof(char*) == 4
  38. %ecx = argc ; 0(esp)
  39. %ebx = argv ; 4(esp)
  40. %eax = env ; argv + (argc * 4) + 4
  41. */
  42. /* Set up an invalid (NULL return address, NULL frame pointer)
  43. callers stack frame so anybody unrolling the stack knows where
  44. to stop */
  45. xorl %ebp,%ebp /* NULL */
  46. pushl %ebp /* callers %cs */
  47. pushl %ebp /* callers %eip (return address) */
  48. pushl %ebp /* callers %ebp (frame pointer) */
  49. movl %esp,%ebp /* mark callers stack frame as invalid */
  50. /* Now set the environment, argc, and argv where the app can get to them */
  51. pushl %eax /* Environment pointer */
  52. pushl %ebx /* Argument pointer */
  53. pushl %ecx /* And the argument count */
  54. /* Make sure we are not using iBCS2 personality. (i.e. force linux). */
  55. movl $136,%eax
  56. sub %ebx,%ebx
  57. int $0x80
  58. /* set up __environ */
  59. movl 8(%esp),%eax
  60. movl %eax,__environ
  61. /* Tell libc to initialize anything it needs to do */
  62. call __libc_init
  63. /* call __malloc_init */
  64. call __init_stdio
  65. /* Ok, now run main() */
  66. call main
  67. pushl %eax
  68. call exit
  69. /* Just in case _exit fails... We use int $0x80 for __exit(). */
  70. popl %ebx
  71. .align 4,0x90
  72. _start_exit:
  73. movl $1,%eax
  74. int $0x80
  75. jmp _start_exit
  76. .align 4,0x90
  77. _void_void_null_func:
  78. ret
  79. .weak __libc_init
  80. __libc_init = _void_void_null_func
  81. /*
  82. .weak __malloc_init
  83. __malloc_init = _void_void_null_func
  84. */
  85. .weak __init_stdio
  86. __init_stdio = _void_void_null_func
  87. .weak __stdio_close_all
  88. __stdio_close_all = _void_void_null_func
  89. .data
  90. __environ:
  91. .long 0
  92. .weak environ
  93. .align 4
  94. environ = __environ
  95. .data
  96. __errno:
  97. .long 0
  98. .weak errno
  99. .align 4
  100. errno = __errno