minimal.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* Copyright (C) 2002-2015 Free Software Foundation, Inc.
  2. Contributed by Andy Vaught and Paul Brook <paul@nowt.org>
  3. This file is part of the GNU Fortran runtime library (libgfortran).
  4. Libgfortran is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. Libgfortran is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. Under Section 7 of GPL version 3, you are granted additional
  13. permissions described in the GCC Runtime Library Exception, version
  14. 3.1, as published by the Free Software Foundation.
  15. You should have received a copy of the GNU General Public License and
  16. a copy of the GCC Runtime Library Exception along with this program;
  17. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  18. <http://www.gnu.org/licenses/>. */
  19. #include "libgfortran.h"
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <limits.h>
  23. #include <errno.h>
  24. #ifdef HAVE_UNISTD_H
  25. #include <unistd.h>
  26. #endif
  27. /* Stupid function to be sure the constructor is always linked in, even
  28. in the case of static linking. See PR libfortran/22298 for details. */
  29. void
  30. stupid_function_name_for_static_linking (void)
  31. {
  32. return;
  33. }
  34. options_t options;
  35. /* This will be 0 for little-endian
  36. machines and 1 for big-endian machines.
  37. Currently minimal libgfortran only runs on little-endian devices
  38. which don't support constructors so this is just a constant. */
  39. int big_endian = 0;
  40. static int argc_save;
  41. static char **argv_save;
  42. static const char *exe_path;
  43. /* recursion_check()-- It's possible for additional errors to occur
  44. * during fatal error processing. We detect this condition here and
  45. * exit with code 4 immediately. */
  46. #define MAGIC 0x20DE8101
  47. static void
  48. recursion_check (void)
  49. {
  50. static int magic = 0;
  51. /* Don't even try to print something at this point */
  52. if (magic == MAGIC)
  53. sys_abort ();
  54. magic = MAGIC;
  55. }
  56. /* os_error()-- Operating system error. We get a message from the
  57. * operating system, show it and leave. Some operating system errors
  58. * are caught and processed by the library. If not, we come here. */
  59. void
  60. os_error (const char *message)
  61. {
  62. recursion_check ();
  63. printf ("Operating system error: ");
  64. printf ("%s\n", message);
  65. exit (1);
  66. }
  67. iexport(os_error);
  68. /* void runtime_error()-- These are errors associated with an
  69. * invalid fortran program. */
  70. void
  71. runtime_error (const char *message, ...)
  72. {
  73. va_list ap;
  74. recursion_check ();
  75. printf ("Fortran runtime error: ");
  76. va_start (ap, message);
  77. vprintf (message, ap);
  78. va_end (ap);
  79. printf ("\n");
  80. exit (2);
  81. }
  82. iexport(runtime_error);
  83. /* void runtime_error_at()-- These are errors associated with a
  84. * run time error generated by the front end compiler. */
  85. void
  86. runtime_error_at (const char *where, const char *message, ...)
  87. {
  88. va_list ap;
  89. recursion_check ();
  90. printf ("%s", where);
  91. printf ("\nFortran runtime error: ");
  92. va_start (ap, message);
  93. vprintf (message, ap);
  94. va_end (ap);
  95. printf ("\n");
  96. exit (2);
  97. }
  98. iexport(runtime_error_at);
  99. void
  100. runtime_warning_at (const char *where, const char *message, ...)
  101. {
  102. va_list ap;
  103. printf ("%s", where);
  104. printf ("\nFortran runtime warning: ");
  105. va_start (ap, message);
  106. vprintf (message, ap);
  107. va_end (ap);
  108. printf ("\n");
  109. }
  110. iexport(runtime_warning_at);
  111. /* void internal_error()-- These are this-can't-happen errors
  112. * that indicate something deeply wrong. */
  113. void
  114. internal_error (st_parameter_common *cmp, const char *message)
  115. {
  116. recursion_check ();
  117. printf ("Internal Error: ");
  118. printf ("%s", message);
  119. printf ("\n");
  120. /* This function call is here to get the main.o object file included
  121. when linking statically. This works because error.o is supposed to
  122. be always linked in (and the function call is in internal_error
  123. because hopefully it doesn't happen too often). */
  124. stupid_function_name_for_static_linking();
  125. exit (3);
  126. }
  127. /* Return the full path of the executable. */
  128. char *
  129. full_exe_path (void)
  130. {
  131. return (char *) exe_path;
  132. }
  133. /* Set the saved values of the command line arguments. */
  134. void
  135. set_args (int argc, char **argv)
  136. {
  137. argc_save = argc;
  138. argv_save = argv;
  139. exe_path = argv[0];
  140. }
  141. iexport(set_args);
  142. /* Retrieve the saved values of the command line arguments. */
  143. void
  144. get_args (int *argc, char ***argv)
  145. {
  146. *argc = argc_save;
  147. *argv = argv_save;
  148. }
  149. /* sys_abort()-- Terminate the program showing backtrace and dumping
  150. core. */
  151. void
  152. sys_abort (void)
  153. {
  154. /* If backtracing is enabled, print backtrace and disable signal
  155. handler for ABRT. */
  156. if (options.backtrace == 1
  157. || (options.backtrace == -1 && compile_options.backtrace == 1))
  158. {
  159. printf ("\nProgram aborted.\n");
  160. }
  161. abort();
  162. }