crt0.S 3.0 KB

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