Daily bump.
[official-gcc.git] / libgfortran / runtime / main.c
blob32033ba3e900969f0d1f2941887533e8fdf3ef3e
1 /* Copyright (C) 2002-2013 Free Software Foundation, Inc.
2 Contributed by Andy Vaught and Paul Brook <paul@nowt.org>
4 This file is part of the GNU Fortran runtime library (libgfortran).
6 Libgfortran is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
11 Libgfortran is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 Under Section 7 of GPL version 3, you are granted additional
17 permissions described in the GCC Runtime Library Exception, version
18 3.1, as published by the Free Software Foundation.
20 You should have received a copy of the GNU General Public License and
21 a copy of the GCC Runtime Library Exception along with this program;
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 <http://www.gnu.org/licenses/>. */
25 #include "libgfortran.h"
26 #include <stdlib.h>
27 #include <string.h>
28 #include <limits.h>
31 #ifdef HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif
35 /* Stupid function to be sure the constructor is always linked in, even
36 in the case of static linking. See PR libfortran/22298 for details. */
37 void
38 stupid_function_name_for_static_linking (void)
40 return;
43 /* This will be 0 for little-endian
44 machines and 1 for big-endian machines. */
45 int big_endian = 0;
48 /* Figure out endianness for this machine. */
50 static void
51 determine_endianness (void)
53 union
55 GFC_LOGICAL_8 l8;
56 GFC_LOGICAL_4 l4[2];
57 } u;
59 u.l8 = 1;
60 if (u.l4[0])
61 big_endian = 0;
62 else if (u.l4[1])
63 big_endian = 1;
64 else
65 runtime_error ("Unable to determine machine endianness");
69 static int argc_save;
70 static char **argv_save;
72 static const char *exe_path;
73 static int please_free_exe_path_when_done;
75 /* Save the path under which the program was called, for use in the
76 backtrace routines. */
77 void
78 store_exe_path (const char * argv0)
80 #ifndef PATH_MAX
81 #define PATH_MAX 1024
82 #endif
84 #ifndef DIR_SEPARATOR
85 #define DIR_SEPARATOR '/'
86 #endif
88 char buf[PATH_MAX], *path;
89 const char *cwd;
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);
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)
101 buf[len] = '\0';
102 exe_path = strdup (buf);
103 please_free_exe_path_when_done = 1;
104 return;
106 #endif
108 /* If the path is absolute or on a simulator where argv is not set. */
109 #ifdef __MINGW32__
110 if (argv0 == NULL
111 || ('A' <= argv0[0] && argv0[0] <= 'Z' && argv0[1] == ':')
112 || ('a' <= argv0[0] && argv0[0] <= 'z' && argv0[1] == ':')
113 || (argv0[0] == '/' && argv0[1] == '/')
114 || (argv0[0] == '\\' && argv0[1] == '\\'))
115 #else
116 if (argv0 == NULL || argv0[0] == DIR_SEPARATOR)
117 #endif
119 exe_path = argv0;
120 please_free_exe_path_when_done = 0;
121 return;
124 #ifdef HAVE_GETCWD
125 cwd = getcwd (buf, sizeof (buf));
126 #else
127 cwd = NULL;
128 #endif
130 if (!cwd)
132 exe_path = argv0;
133 please_free_exe_path_when_done = 0;
134 return;
137 /* exe_path will be cwd + "/" + argv[0] + "\0". This will not work
138 if the executable is not in the cwd, but at this point we're out
139 of better ideas. */
140 size_t pathlen = strlen (cwd) + 1 + strlen (argv0) + 1;
141 path = malloc (pathlen);
142 snprintf (path, pathlen, "%s%c%s", cwd, DIR_SEPARATOR, argv0);
143 exe_path = path;
144 please_free_exe_path_when_done = 1;
148 /* Return the full path of the executable. */
149 char *
150 full_exe_path (void)
152 return (char *) exe_path;
156 #ifndef HAVE_STRTOK_R
157 static char*
158 gfstrtok_r (char *str, const char *delim,
159 char **saveptr __attribute__ ((unused)))
161 return strtok (str, delim);
163 #define strtok_r gfstrtok_r
164 #endif
166 char *addr2line_path;
168 /* Find addr2line and store the path. */
170 void
171 find_addr2line (void)
173 #ifdef HAVE_ACCESS
174 #define A2L_LEN 11
175 char *path = secure_getenv ("PATH");
176 if (!path)
177 return;
178 char *tp = strdup (path);
179 if (!tp)
180 return;
181 size_t n = strlen (path);
182 char *ap = xmalloc (n + A2L_LEN);
183 char *saveptr;
184 for (char *str = tp;; str = NULL)
186 char *token = strtok_r (str, ":", &saveptr);
187 if (!token)
188 break;
189 size_t toklen = strlen (token);
190 memcpy (ap, token, toklen);
191 memcpy (ap + toklen, "/addr2line", A2L_LEN);
192 if (access (ap, R_OK|X_OK) == 0)
194 addr2line_path = strdup (ap);
195 break;
198 free (tp);
199 free (ap);
200 #endif
204 /* Set the saved values of the command line arguments. */
206 void
207 set_args (int argc, char **argv)
209 argc_save = argc;
210 argv_save = argv;
211 store_exe_path (argv[0]);
213 iexport(set_args);
216 /* Retrieve the saved values of the command line arguments. */
218 void
219 get_args (int *argc, char ***argv)
221 *argc = argc_save;
222 *argv = argv_save;
226 /* Initialize the runtime library. */
228 static void __attribute__((constructor))
229 init (void)
231 /* Figure out the machine endianness. */
232 determine_endianness ();
234 /* Must be first */
235 init_variables ();
237 init_units ();
238 set_fpu ();
239 init_compile_options ();
241 #ifdef DEBUG
242 /* Check for special command lines. */
244 if (argc > 1 && strcmp (argv[1], "--help") == 0)
245 show_variables ();
247 /* if (argc > 1 && strcmp(argv[1], "--resume") == 0) resume(); */
248 #endif
250 if (options.backtrace == 1)
251 find_addr2line ();
253 random_seed_i4 (NULL, NULL, NULL);
257 /* Cleanup the runtime library. */
259 static void __attribute__((destructor))
260 cleanup (void)
262 close_units ();
264 if (please_free_exe_path_when_done)
265 free ((char *) exe_path);
267 free (addr2line_path);