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)
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"
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. */
38 stupid_function_name_for_static_linking (void)
43 /* This will be 0 for little-endian
44 machines and 1 for big-endian machines. */
48 /* Figure out endianness for this machine. */
51 determine_endianness (void)
65 runtime_error ("Unable to determine machine endianness");
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. */
78 store_exe_path (const char * argv0
)
85 #define DIR_SEPARATOR '/'
88 char buf
[PATH_MAX
], *path
;
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. */
99 if ((len
= readlink ("/proc/self/exe", buf
, sizeof (buf
) - 1)) != -1)
102 exe_path
= strdup (buf
);
103 please_free_exe_path_when_done
= 1;
108 /* If the path is absolute or on a simulator where argv is not set. */
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] == '\\'))
116 if (argv0
== NULL
|| argv0
[0] == DIR_SEPARATOR
)
120 please_free_exe_path_when_done
= 0;
125 cwd
= getcwd (buf
, sizeof (buf
));
133 please_free_exe_path_when_done
= 0;
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
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
);
144 please_free_exe_path_when_done
= 1;
148 /* Return the full path of the executable. */
152 return (char *) exe_path
;
156 char *addr2line_path
;
158 /* Find addr2line and store the path. */
161 find_addr2line (void)
165 char *path
= secure_getenv ("PATH");
168 size_t n
= strlen (path
);
169 char ap
[n
+ 1 + A2L_LEN
];
171 for (size_t i
= 0; i
< n
; i
++)
178 memcpy (ap
+ ai
, "addr2line", A2L_LEN
);
179 if (access (ap
, R_OK
|X_OK
) == 0)
181 addr2line_path
= strdup (ap
);
192 /* Set the saved values of the command line arguments. */
195 set_args (int argc
, char **argv
)
199 store_exe_path (argv
[0]);
204 /* Retrieve the saved values of the command line arguments. */
207 get_args (int *argc
, char ***argv
)
214 /* Initialize the runtime library. */
216 static void __attribute__((constructor
))
219 /* Figure out the machine endianness. */
220 determine_endianness ();
227 init_compile_options ();
230 /* Check for special command lines. */
232 if (argc
> 1 && strcmp (argv
[1], "--help") == 0)
235 /* if (argc > 1 && strcmp(argv[1], "--resume") == 0) resume(); */
238 if (options
.backtrace
== 1)
241 random_seed_i4 (NULL
, NULL
, NULL
);
245 /* Cleanup the runtime library. */
247 static void __attribute__((destructor
))
252 if (please_free_exe_path_when_done
)
253 free ((char *) exe_path
);
255 free (addr2line_path
);