+static const char *exe_path;
+static int please_free_exe_path_when_done;
+
+/* Save the path under which the program was called, for use in the
+ backtrace routines. */
+void
+store_exe_path (const char * argv0)
+{
+#ifndef PATH_MAX
+#define PATH_MAX 1024
+#endif
+
+#ifndef DIR_SEPARATOR
+#define DIR_SEPARATOR '/'
+#endif
+
+ char buf[PATH_MAX], *cwd, *path;
+
+ /* This can only happen if store_exe_path is called multiple times. */
+ if (please_free_exe_path_when_done)
+ free ((char *) exe_path);
+
+ /* Reading the /proc/self/exe symlink is Linux-specific(?), but if
+ it works it gives the correct answer. */
+#ifdef HAVE_READLINK
+ int len;
+ if ((len = readlink ("/proc/self/exe", buf, sizeof (buf) - 1)) != -1)
+ {
+ buf[len] = '\0';
+ exe_path = strdup (buf);
+ please_free_exe_path_when_done = 1;
+ return;
+ }
+#endif
+
+ /* On the simulator argv is not set. */
+ if (argv0 == NULL || argv0[0] == '/')
+ {
+ exe_path = argv0;
+ please_free_exe_path_when_done = 0;
+ return;
+ }
+
+ memset (buf, 0, sizeof (buf));
+#ifdef HAVE_GETCWD
+ cwd = getcwd (buf, sizeof (buf));
+#else
+ cwd = "";
+#endif
+
+ /* exe_path will be cwd + "/" + argv[0] + "\0". This will not work
+ if the executable is not in the cwd, but at this point we're out
+ of better ideas. */
+ size_t pathlen = strlen (cwd) + 1 + strlen (argv0) + 1;
+ path = malloc (pathlen);
+ snprintf (path, pathlen, "%s%c%s", cwd, DIR_SEPARATOR, argv0);
+ exe_path = path;
+ please_free_exe_path_when_done = 1;
+}
+
+
+/* Return the full path of the executable. */
+char *
+full_exe_path (void)
+{
+ return (char *) exe_path;
+}
+
+