crt0.S 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. /*
  17. When we enter this piece of code, the program stack looks like this:
  18. argc argument counter (integer)
  19. argv[0] program name (pointer)
  20. argv[1...N] program args (pointers)
  21. argv[argc-1] end of args (integer)
  22. NULL
  23. env[0...N] environment variables (pointers)
  24. NULL
  25. */
  26. #include <features.h>
  27. .text
  28. .align 4
  29. .globl _start
  30. .type _start,@function
  31. _start:
  32. /* locate the start of the environment variables */
  33. popl %ecx /* Store argc into %ecx */
  34. movl %esp,%ebx /* Store argv into ebx */
  35. movl %esp,%eax /* Store argv into eax as well*/
  36. movl %ecx,%edx /* Stick argc into %edx so we can do some math in a sec */
  37. leal 4(%eax,%edx,4),%eax
  38. /* [ register layout ]
  39. sizeof(char*) == 4
  40. %ecx = argc ; 0(esp)
  41. %ebx = argv ; 4(esp)
  42. %eax = env ; argv + (argc * 4) + 4
  43. */
  44. /* Set up an invalid (NULL return address, NULL frame pointer)
  45. callers stack frame so anybody unrolling the stack knows where
  46. to stop */
  47. xorl %ebp,%ebp /* NULL */
  48. pushl %ebp /* callers %cs */
  49. pushl %ebp /* callers %eip (return address) */
  50. pushl %ebp /* callers %ebp (frame pointer) */
  51. movl %esp,%ebp /* mark callers stack frame as invalid */
  52. #if defined L_crt1 && defined __UCLIBC_CTOR_DTOR__
  53. /* Push .init and .fini arguments to __uClibc_start_main() on the stack */
  54. pushl $_fini
  55. pushl $_init
  56. /* Push envp, argc, and argc arguments to __uClibc_start_main() on the stack */
  57. pushl %eax /* Environment pointer */
  58. pushl %ebx /* Argument pointer */
  59. pushl %ecx /* And the argument count */
  60. /* Ok, now run uClibc's main() -- shouldn't return */
  61. call __uClibc_start_main
  62. #else
  63. /* Push envp, argc, and argc arguments to __uClibc_start_main() on the stack */
  64. pushl %eax /* Environment pointer */
  65. pushl %ebx /* Argument pointer */
  66. pushl %ecx /* And the argument count */
  67. call __uClibc_main
  68. #endif
  69. /* Crash if somehow `exit' returns anyways. */
  70. hlt
  71. /* Stick in a dummy reference to main(), so that if an application
  72. * is linking when the main() function is in a static library (.a)
  73. * we can be sure that main() actually gets linked in */
  74. L_dummy_main_reference:
  75. .long main