/* Wrapper to call lto. Used by collect2 and the linker plugin.
- Copyright (C) 2009 Free Software Foundation, Inc.
+ Copyright (C) 2009, 2010 Free Software Foundation, Inc.
Factored out of collect2 by Rafael Espindola <espindola@google.com>
/* Current LTO mode. */
static enum lto_mode_d lto_mode = LTO_MODE_NONE;
+static char *ltrans_output_file;
+static char *flto_out;
+static char *args_name;
+
+static void maybe_unlink_file (const char *);
+
+/* Delete tempfiles and exit function. */
+
+static void
+lto_wrapper_exit (int status)
+{
+ static bool cleanup_done = false;
+ if (!cleanup_done)
+ {
+ /* Setting cleanup_done prevents an infinite loop if one of the
+ calls to maybe_unlink_file fails. */
+ cleanup_done = true;
+
+ if (ltrans_output_file)
+ maybe_unlink_file (ltrans_output_file);
+ if (flto_out)
+ maybe_unlink_file (flto_out);
+ if (args_name)
+ maybe_unlink_file (args_name);
+ }
+ exit (status);
+}
+
/* Just die. CMSGID is the error message. */
static void __attribute__ ((format (printf, 1, 2)))
fprintf (stderr, "\n");
va_end (ap);
- exit (FATAL_EXIT_CODE);
+ lto_wrapper_exit (FATAL_EXIT_CODE);
}
fprintf (stderr, ": %s\n", xstrerror (e));
va_end (ap);
- exit (FATAL_EXIT_CODE);
+ lto_wrapper_exit (FATAL_EXIT_CODE);
}
{
struct pex_obj *pex;
char *new_argv[3];
- char *args_name = make_temp_file (".args");
- char *at_args = concat ("@", args_name, NULL);
- FILE *args = fopen (args_name, "w");
+ char *at_args;
+ FILE *args;
int status;
+ args_name = make_temp_file (".args");
+ at_args = concat ("@", args_name, NULL);
+ args = fopen (args_name, "w");
if (args == NULL)
fatal ("failed to open %s", args_name);
collect_wait (new_argv[0], pex);
maybe_unlink_file (args_name);
- free (args_name);
free (at_args);
}
unsigned new_argc = argc;
const char **new_argv;
const char **argv_ptr;
- char *ltrans_output_file = NULL;
- char *flto_out = NULL;
char *list_option_full = NULL;
- new_argc += 8;
+ new_argc += 12;
new_argv = (const char **) xcalloc (sizeof (char *), new_argc);
argv_ptr = new_argv;
temporary file for the LTO output. The `-o' option
will be interpreted by the linker. */
if (s[2] == '\0')
- i++;
+ {
+ char *output_dir, *base, *name;
+
+ i++;
+ output_dir = xstrdup (argv[i]);
+ base = output_dir;
+ for (name = base; *name; name++)
+ if (IS_DIR_SEPARATOR (*name))
+ base = name + 1;
+ *base = '\0';
+
+ *argv_ptr++ = "-dumpbase";
+ if (*output_dir == '\0')
+ {
+ static char current_dir[] =
+ { '.', DIR_SEPARATOR, '\0' };
+ output_dir = current_dir;
+ *argv_ptr++ = argv[i];
+ }
+ else
+ *argv_ptr++ = &argv[i][base - output_dir];
+
+ *argv_ptr++ = "-dumpdir";
+ *argv_ptr++ = output_dir;
+ }
}
else
/* Pass the option or argument to LTO. */
putc (c, stdout);
fclose (stream);
maybe_unlink_file (ltrans_output_file);
- free (ltrans_output_file);
free (list_option_full);
}
else