OSDN Git Service

PR 48931 Make backtrace async-signal-safe, reformat output
[pf3gnuchains/gcc-fork.git] / libgfortran / runtime / main.c
1 /* Copyright (C) 2002-2003, 2005, 2007, 2009, 2011 
2    Free Software Foundation, Inc.
3    Contributed by Andy Vaught and Paul Brook <paul@nowt.org>
4
5 This file is part of the GNU Fortran runtime library (libgfortran).
6
7 Libgfortran is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 Libgfortran is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
20
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
24 <http://www.gnu.org/licenses/>.  */
25
26 #include "libgfortran.h"
27 #include <stdlib.h>
28 #include <string.h>
29 #include <limits.h>
30
31
32 #ifdef HAVE_UNISTD_H
33 #include <unistd.h>
34 #endif
35
36 /* Stupid function to be sure the constructor is always linked in, even
37    in the case of static linking.  See PR libfortran/22298 for details.  */
38 void
39 stupid_function_name_for_static_linking (void)
40 {
41   return;
42 }
43
44 /* This will be 0 for little-endian
45    machines and 1 for big-endian machines.  */
46 int big_endian = 0;
47
48
49 /* Figure out endianness for this machine.  */
50
51 static void
52 determine_endianness (void)
53 {
54   union
55   {
56     GFC_LOGICAL_8 l8;
57     GFC_LOGICAL_4 l4[2];
58   } u;
59
60   u.l8 = 1;
61   if (u.l4[0])
62     big_endian = 0;
63   else if (u.l4[1])
64     big_endian = 1;
65   else
66     runtime_error ("Unable to determine machine endianness");
67 }
68
69
70 static int argc_save;
71 static char **argv_save;
72
73 static const char *exe_path;
74 static int please_free_exe_path_when_done;
75
76 /* Save the path under which the program was called, for use in the
77    backtrace routines.  */
78 void
79 store_exe_path (const char * argv0)
80 {
81 #ifndef PATH_MAX
82 #define PATH_MAX 1024
83 #endif
84
85 #ifndef DIR_SEPARATOR   
86 #define DIR_SEPARATOR '/'
87 #endif
88
89   char buf[PATH_MAX], *cwd, *path;
90
91   /* This can only happen if store_exe_path is called multiple times.  */
92   if (please_free_exe_path_when_done)
93     free ((char *) exe_path);
94
95   /* Reading the /proc/self/exe symlink is Linux-specific(?), but if
96      it works it gives the correct answer.  */
97 #ifdef HAVE_READLINK
98   int len;
99   if ((len = readlink ("/proc/self/exe", buf, sizeof (buf) - 1)) != -1)
100     {
101       buf[len] = '\0';
102       exe_path = strdup (buf);
103       please_free_exe_path_when_done = 1;
104       return;
105     }
106 #endif
107
108   /* On the simulator argv is not set.  */
109   if (argv0 == NULL || argv0[0] == '/')
110     {
111       exe_path = argv0;
112       please_free_exe_path_when_done = 0;
113       return;
114     }
115
116   memset (buf, 0, sizeof (buf));
117 #ifdef HAVE_GETCWD
118   cwd = getcwd (buf, sizeof (buf));
119 #else
120   cwd = "";
121 #endif
122
123   /* exe_path will be cwd + "/" + argv[0] + "\0".  This will not work
124      if the executable is not in the cwd, but at this point we're out
125      of better ideas.  */
126   size_t pathlen = strlen (cwd) + 1 + strlen (argv0) + 1;
127   path = malloc (pathlen);
128   snprintf (path, pathlen, "%s%c%s", cwd, DIR_SEPARATOR, argv0);
129   exe_path = path;
130   please_free_exe_path_when_done = 1;
131 }
132
133
134 /* Return the full path of the executable.  */
135 char *
136 full_exe_path (void)
137 {
138   return (char *) exe_path;
139 }
140
141
142 /* Set the saved values of the command line arguments.  */
143
144 void
145 set_args (int argc, char **argv)
146 {
147   argc_save = argc;
148   argv_save = argv;
149   store_exe_path (argv[0]);
150 }
151 iexport(set_args);
152
153
154 /* Retrieve the saved values of the command line arguments.  */
155
156 void
157 get_args (int *argc, char ***argv)
158 {
159   *argc = argc_save;
160   *argv = argv_save;
161 }
162
163
164 /* Initialize the runtime library.  */
165
166 static void __attribute__((constructor))
167 init (void)
168 {
169   /* Figure out the machine endianness.  */
170   determine_endianness ();
171
172   /* Must be first */
173   init_variables ();
174
175   init_units ();
176   set_fpu ();
177   init_compile_options ();
178
179 #ifdef DEBUG
180   /* Check for special command lines.  */
181
182   if (argc > 1 && strcmp (argv[1], "--help") == 0)
183     show_variables ();
184
185   /* if (argc > 1 && strcmp(argv[1], "--resume") == 0) resume();  */
186 #endif
187
188   random_seed_i4 (NULL, NULL, NULL);
189 }
190
191
192 /* Cleanup the runtime library.  */
193
194 static void __attribute__((destructor))
195 cleanup (void)
196 {
197   close_units ();
198   
199   if (please_free_exe_path_when_done)
200     free ((char *) exe_path);
201 }