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>
5 This file is part of the GNU Fortran runtime library (libgfortran).
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)
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.
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.
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/>. */
26 #include "libgfortran.h"
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. */
39 stupid_function_name_for_static_linking (void)
44 /* This will be 0 for little-endian
45 machines and 1 for big-endian machines. */
49 /* Figure out endianness for this machine. */
52 determine_endianness (void)
66 runtime_error ("Unable to determine machine endianness");
71 static char **argv_save;
73 static const char *exe_path;
74 static int please_free_exe_path_when_done;
76 /* Save the path under which the program was called, for use in the
77 backtrace routines. */
79 store_exe_path (const char * argv0)
86 #define DIR_SEPARATOR '/'
89 char buf[PATH_MAX], *path;
92 /* This can only happen if store_exe_path is called multiple times. */
93 if (please_free_exe_path_when_done)
94 free ((char *) exe_path);
96 /* Reading the /proc/self/exe symlink is Linux-specific(?), but if
97 it works it gives the correct answer. */
100 if ((len = readlink ("/proc/self/exe", buf, sizeof (buf) - 1)) != -1)
103 exe_path = strdup (buf);
104 please_free_exe_path_when_done = 1;
109 /* If the path is absolute or on a simulator where argv is not set. */
112 || ('A' <= argv0[0] && argv0[0] <= 'Z' && argv0[1] == ':')
113 || ('a' <= argv0[0] && argv0[0] <= 'z' && argv0[1] == ':')
114 || (argv0[0] == '/' && argv0[1] == '/')
115 || (argv0[0] == '\\' && argv0[1] == '\\'))
117 if (argv0 == NULL || argv0[0] == DIR_SEPARATOR)
121 please_free_exe_path_when_done = 0;
126 cwd = getcwd (buf, sizeof (buf));
133 /* exe_path will be cwd + "/" + argv[0] + "\0". This will not work
134 if the executable is not in the cwd, but at this point we're out
136 size_t pathlen = strlen (cwd) + 1 + strlen (argv0) + 1;
137 path = malloc (pathlen);
138 snprintf (path, pathlen, "%s%c%s", cwd, DIR_SEPARATOR, argv0);
140 please_free_exe_path_when_done = 1;
144 /* Return the full path of the executable. */
148 return (char *) exe_path;
152 char *addr2line_path;
154 /* Find addr2line and store the path. */
157 find_addr2line (void)
161 char *path = getenv ("PATH");
164 size_t n = strlen (path);
165 char ap[n + 1 + A2L_LEN];
167 for (size_t i = 0; i < n; i++)
174 memcpy (ap + ai, "addr2line", A2L_LEN);
175 if (access (ap, R_OK|X_OK) == 0)
177 addr2line_path = strdup (ap);
188 /* Set the saved values of the command line arguments. */
191 set_args (int argc, char **argv)
195 store_exe_path (argv[0]);
200 /* Retrieve the saved values of the command line arguments. */
203 get_args (int *argc, char ***argv)
210 /* Initialize the runtime library. */
212 static void __attribute__((constructor))
215 /* Figure out the machine endianness. */
216 determine_endianness ();
223 init_compile_options ();
226 /* Check for special command lines. */
228 if (argc > 1 && strcmp (argv[1], "--help") == 0)
231 /* if (argc > 1 && strcmp(argv[1], "--resume") == 0) resume(); */
234 if (options.backtrace == 1)
237 random_seed_i4 (NULL, NULL, NULL);
241 /* Cleanup the runtime library. */
243 static void __attribute__((destructor))
248 if (please_free_exe_path_when_done)
249 free ((char *) exe_path);
251 free (addr2line_path);