crt1.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*-
  2. * Copyright 1996-1998 John D. Polstra.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. *
  25. * $FreeBSD: src/lib/csu/i386-elf/crt1.c,v 1.3.2.1 1999/08/29 14:55:14 peter Exp $
  26. */
  27. #ifndef __GNUC__
  28. #error "GCC is needed to compile this file"
  29. #endif
  30. #include <stddef.h>
  31. #include <stdlib.h>
  32. typedef void (*fptr)(void);
  33. extern void _fini(void);
  34. extern void _init(void);
  35. extern int main(int, char **, char **);
  36. #ifdef GCRT
  37. extern void _mcleanup(void);
  38. extern void monstartup(void *, void *);
  39. extern int eprol;
  40. extern int etext;
  41. #endif
  42. extern int _DYNAMIC;
  43. #pragma weak _DYNAMIC
  44. #ifdef __i386__
  45. #define get_rtld_cleanup() \
  46. ({ fptr __value; \
  47. __asm__("movl %%edx,%0" : "=rm"(__value)); \
  48. __value; })
  49. #else
  50. #error "This file only supports the i386 architecture"
  51. #endif
  52. char **environ;
  53. char *__progname = "";
  54. void
  55. _start(char *arguments, ...)
  56. {
  57. fptr rtld_cleanup;
  58. int argc;
  59. char **argv;
  60. char **env;
  61. rtld_cleanup = get_rtld_cleanup();
  62. argv = &arguments;
  63. argc = * (int *) (argv - 1);
  64. env = argv + argc + 1;
  65. environ = env;
  66. if(argc > 0 && argv[0] != NULL) {
  67. char *s;
  68. __progname = argv[0];
  69. for (s = __progname; *s != '\0'; s++)
  70. if (*s == '/')
  71. __progname = s + 1;
  72. }
  73. if(&_DYNAMIC != NULL)
  74. atexit(rtld_cleanup);
  75. #ifdef GCRT
  76. atexit(_mcleanup);
  77. #endif
  78. atexit(_fini);
  79. #ifdef GCRT
  80. monstartup(&eprol, &etext);
  81. #endif
  82. _init();
  83. exit( main(argc, argv, env) );
  84. }
  85. #ifdef GCRT
  86. __asm__(".text");
  87. __asm__("eprol:");
  88. __asm__(".previous");
  89. #endif