PR c++/60994
[official-gcc.git] / libgfortran / runtime / main.c
blob3c66a30fff2e783da1e3f58d0379c929bccba7c1
1 /* Copyright (C) 2002-2015 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>
29 #include <errno.h>
32 #ifdef HAVE_UNISTD_H
33 #include <unistd.h>
34 #endif
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)
41 return;
44 /* This will be 0 for little-endian
45 machines and 1 for big-endian machines. */
46 int big_endian = 0;
49 /* Figure out endianness for this machine. */
51 static void
52 determine_endianness (void)
54 union
56 GFC_LOGICAL_8 l8;
57 GFC_LOGICAL_4 l4[2];
58 } u;
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");
70 static int argc_save;
71 static char **argv_save;
73 static const char *exe_path;
74 static bool please_free_exe_path_when_done;
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)
81 #ifndef DIR_SEPARATOR
82 #define DIR_SEPARATOR '/'
83 #endif
85 char *cwd, *path;
87 /* This can only happen if store_exe_path is called multiple times. */
88 if (please_free_exe_path_when_done)
89 free ((char *) exe_path);
91 /* Reading the /proc/self/exe symlink is Linux-specific(?), but if
92 it works it gives the correct answer. */
93 #ifdef HAVE_READLINK
94 ssize_t len, psize = 256;
95 while (1)
97 path = xmalloc (psize);
98 len = readlink ("/proc/self/exe", path, psize);
99 if (len < 0)
101 free (path);
102 break;
104 else if (len < psize)
106 path[len] = '\0';
107 exe_path = strdup (path);
108 free (path);
109 please_free_exe_path_when_done = true;
110 return;
112 /* The remaining option is len == psize. */
113 free (path);
114 psize *= 4;
116 #endif
118 /* If the path is absolute or on a simulator where argv is not set. */
119 #ifdef __MINGW32__
120 if (argv0 == NULL
121 || ('A' <= argv0[0] && argv0[0] <= 'Z' && argv0[1] == ':')
122 || ('a' <= argv0[0] && argv0[0] <= 'z' && argv0[1] == ':')
123 || (argv0[0] == '/' && argv0[1] == '/')
124 || (argv0[0] == '\\' && argv0[1] == '\\'))
125 #else
126 if (argv0 == NULL || argv0[0] == DIR_SEPARATOR)
127 #endif
129 exe_path = argv0;
130 please_free_exe_path_when_done = false;
131 return;
134 #ifdef HAVE_GETCWD
135 size_t cwdsize = 256;
136 while (1)
138 cwd = xmalloc (cwdsize);
139 if (getcwd (cwd, cwdsize))
140 break;
141 else if (errno == ERANGE)
143 free (cwd);
144 cwdsize *= 4;
146 else
148 free (cwd);
149 cwd = NULL;
150 break;
153 #else
154 cwd = NULL;
155 #endif
157 if (!cwd)
159 exe_path = argv0;
160 please_free_exe_path_when_done = false;
161 return;
164 /* exe_path will be cwd + "/" + argv[0] + "\0". This will not work
165 if the executable is not in the cwd, but at this point we're out
166 of better ideas. */
167 size_t pathlen = strlen (cwd) + 1 + strlen (argv0) + 1;
168 path = xmalloc (pathlen);
169 snprintf (path, pathlen, "%s%c%s", cwd, DIR_SEPARATOR, argv0);
170 free (cwd);
171 exe_path = path;
172 please_free_exe_path_when_done = true;
176 /* Return the full path of the executable. */
177 char *
178 full_exe_path (void)
180 return (char *) exe_path;
184 #ifndef HAVE_STRTOK_R
185 static char*
186 gfstrtok_r (char *str, const char *delim,
187 char **saveptr __attribute__ ((unused)))
189 return strtok (str, delim);
191 #define strtok_r gfstrtok_r
192 #endif
194 char *addr2line_path;
196 /* Find addr2line and store the path. */
198 void
199 find_addr2line (void)
201 #ifdef HAVE_ACCESS
202 #define A2L_LEN 11
203 char *path = secure_getenv ("PATH");
204 if (!path)
205 return;
206 char *tp = strdup (path);
207 if (!tp)
208 return;
209 size_t n = strlen (path);
210 char *ap = xmalloc (n + A2L_LEN);
211 char *saveptr;
212 for (char *str = tp;; str = NULL)
214 char *token = strtok_r (str, ":", &saveptr);
215 if (!token)
216 break;
217 size_t toklen = strlen (token);
218 memcpy (ap, token, toklen);
219 memcpy (ap + toklen, "/addr2line", A2L_LEN);
220 if (access (ap, R_OK|X_OK) == 0)
222 addr2line_path = strdup (ap);
223 break;
226 free (tp);
227 free (ap);
228 #endif
232 /* Set the saved values of the command line arguments. */
234 void
235 set_args (int argc, char **argv)
237 argc_save = argc;
238 argv_save = argv;
239 store_exe_path (argv[0]);
241 iexport(set_args);
244 /* Retrieve the saved values of the command line arguments. */
246 void
247 get_args (int *argc, char ***argv)
249 *argc = argc_save;
250 *argv = argv_save;
254 /* Initialize the runtime library. */
256 static void __attribute__((constructor))
257 init (void)
259 /* Figure out the machine endianness. */
260 determine_endianness ();
262 /* Must be first */
263 init_variables ();
265 init_units ();
266 set_fpu ();
267 init_compile_options ();
269 #ifdef DEBUG
270 /* Check for special command lines. */
272 if (argc > 1 && strcmp (argv[1], "--help") == 0)
273 show_variables ();
275 /* if (argc > 1 && strcmp(argv[1], "--resume") == 0) resume(); */
276 #endif
278 if (options.backtrace == 1)
279 find_addr2line ();
281 random_seed_i4 (NULL, NULL, NULL);
285 /* Cleanup the runtime library. */
287 static void __attribute__((destructor))
288 cleanup (void)
290 close_units ();
292 if (please_free_exe_path_when_done)
293 free ((char *) exe_path);
295 free (addr2line_path);