1 /* Wrapper to call lto. Used by collect2 and the linker plugin.
2 Copyright (C) 2009, 2010 Free Software Foundation, Inc.
4 Factored out of collect2 by Rafael Espindola <espindola@google.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 /* This program is passed a gcc, a list of gcc arguments and a list of
24 object files containing IL. It scans the argument list to check if
25 we are in whopr mode or not modifies the arguments and needed and
26 prints a list of output files on stdout.
30 $ lto-wrapper gcc/xgcc -B gcc a.o b.o -o test -flto
32 The above will print something like
35 If -fwhopr is used instead, more than one file might be produced
36 ./ccXj2DTk.lto.ltrans.o
37 ./ccCJuXGv.lto.ltrans.o
42 #include "coretypes.h"
45 #include "libiberty.h"
48 int debug; /* true if -debug */
51 LTO_MODE_NONE, /* Not doing LTO. */
52 LTO_MODE_LTO, /* Normal LTO. */
53 LTO_MODE_WHOPR /* WHOPR. */
56 /* Current LTO mode. */
57 static enum lto_mode_d lto_mode = LTO_MODE_NONE;
59 static char *ltrans_output_file;
60 static char *flto_out;
61 static char *args_name;
63 static void maybe_unlink_file (const char *);
65 /* Delete tempfiles and exit function. */
68 lto_wrapper_exit (int status)
70 static bool cleanup_done = false;
73 /* Setting cleanup_done prevents an infinite loop if one of the
74 calls to maybe_unlink_file fails. */
77 if (ltrans_output_file)
78 maybe_unlink_file (ltrans_output_file);
80 maybe_unlink_file (flto_out);
82 maybe_unlink_file (args_name);
87 /* Just die. CMSGID is the error message. */
89 static void __attribute__ ((format (printf, 1, 2)))
90 fatal (const char * cmsgid, ...)
94 va_start (ap, cmsgid);
95 fprintf (stderr, "lto-wrapper: ");
96 vfprintf (stderr, _(cmsgid), ap);
97 fprintf (stderr, "\n");
100 lto_wrapper_exit (FATAL_EXIT_CODE);
104 /* Die when sys call fails. CMSGID is the error message. */
106 static void __attribute__ ((format (printf, 1, 2)))
107 fatal_perror (const char *cmsgid, ...)
112 va_start (ap, cmsgid);
113 fprintf (stderr, "lto-wrapper: ");
114 vfprintf (stderr, _(cmsgid), ap);
115 fprintf (stderr, ": %s\n", xstrerror (e));
118 lto_wrapper_exit (FATAL_EXIT_CODE);
122 /* Execute a program, and wait for the reply. ARGV are the arguments. The
123 last one must be NULL. */
125 static struct pex_obj *
126 collect_execute (char **argv)
137 for (p_argv = argv; (str = *p_argv) != (char *) 0; p_argv++)
138 fprintf (stderr, " %s", str);
140 fprintf (stderr, "\n");
146 pex = pex_init (0, "lto-wrapper", NULL);
148 fatal_perror ("pex_init failed");
150 errmsg = pex_run (pex, PEX_LAST | PEX_SEARCH, argv[0], argv, NULL,
157 fatal_perror (errmsg);
167 /* Wait for a process to finish, and exit if a nonzero status is found.
168 PROG is the program name. PEX is the process we should wait for. */
171 collect_wait (const char *prog, struct pex_obj *pex)
175 if (!pex_get_status (pex, 1, &status))
176 fatal_perror ("can't get program status");
181 if (WIFSIGNALED (status))
183 int sig = WTERMSIG (status);
184 if (WCOREDUMP (status))
185 fatal ("%s terminated with signal %d [%s], core dumped",
186 prog, sig, strsignal (sig));
188 fatal ("%s terminated with signal %d [%s]",
189 prog, sig, strsignal (sig));
192 if (WIFEXITED (status))
193 fatal ("%s returned %d exit status", prog, WEXITSTATUS (status));
200 /* Unlink a temporary LTRANS file unless requested otherwise. */
203 maybe_unlink_file (const char *file)
207 if (unlink_if_ordinary (file))
208 fatal_perror ("deleting LTRANS file %s", file);
211 fprintf (stderr, "[Leaving LTRANS %s]\n", file);
215 /* Execute program ARGV[0] with arguments ARGV. Wait for it to finish. */
218 fork_execute (char **argv)
226 args_name = make_temp_file (".args");
227 at_args = concat ("@", args_name, NULL);
228 args = fopen (args_name, "w");
230 fatal ("failed to open %s", args_name);
232 status = writeargv (&argv[1], args);
235 fatal ("could not write to temporary file %s", args_name);
239 new_argv[0] = argv[0];
240 new_argv[1] = at_args;
243 pex = collect_execute (new_argv);
244 collect_wait (new_argv[0], pex);
246 maybe_unlink_file (args_name);
250 /* Template of LTRANS dumpbase suffix. */
251 #define DUMPBASE_SUFFIX ".ltrans18446744073709551615"
253 /* Execute gcc. ARGC is the number of arguments. ARGV contains the arguments. */
256 run_gcc (unsigned argc, char *argv[])
259 unsigned new_argc = argc;
260 const char **new_argv;
261 const char **argv_ptr;
262 char *list_option_full = NULL;
265 new_argv = (const char **) xcalloc (sizeof (char *), new_argc);
269 *argv_ptr++ = argv[0];
270 *argv_ptr++ = "-combine";
274 if (lto_mode == LTO_MODE_LTO)
276 flto_out = make_temp_file (".lto.o");
278 *argv_ptr++ = flto_out;
280 else if (lto_mode == LTO_MODE_WHOPR)
282 const char *list_option = "-fltrans-output-list=";
283 size_t list_option_len = strlen (list_option);
286 ltrans_output_file = make_temp_file (".ltrans.out");
287 list_option_full = (char *) xmalloc (sizeof (char) *
288 (strlen (ltrans_output_file) + list_option_len + 1));
289 tmp = list_option_full;
292 strcpy (tmp, list_option);
293 tmp += list_option_len;
294 strcpy (tmp, ltrans_output_file);
296 *argv_ptr++ = "-fwpa";
299 fatal ("invalid LTO mode");
301 /* Add inherited GCC options to the LTO back end command line.
302 Filter out some obviously inappropriate options that will
303 conflict with the options that we force above. We pass
304 all of the remaining options on to LTO, and let it complain
305 about any it doesn't like. Note that we invoke LTO via the
306 `gcc' driver, so the usual option processing takes place.
307 Except for `-flto' and `-fwhopr', we should only filter options that
308 are meaningful to `ld', lest an option go silently unclaimed. */
309 for (i = 1; i < argc; i++)
311 const char *s = argv[i];
313 if (strcmp (s, "-flto") == 0 || strcmp (s, "-fwhopr") == 0)
314 /* We've handled this LTO option, don't pass it on. */
316 else if (*s == '-' && s[1] == 'o')
318 /* Drop `-o' and its filename argument. We will use a
319 temporary file for the LTO output. The `-o' option
320 will be interpreted by the linker. */
323 char *output_dir, *base, *name;
326 output_dir = xstrdup (argv[i]);
328 for (name = base; *name; name++)
329 if (IS_DIR_SEPARATOR (*name))
333 *argv_ptr++ = "-dumpbase";
334 if (*output_dir == '\0')
336 static char current_dir[] =
337 { '.', DIR_SEPARATOR, '\0' };
338 output_dir = current_dir;
339 *argv_ptr++ = argv[i];
342 *argv_ptr++ = &argv[i][base - output_dir];
344 *argv_ptr++ = "-dumpdir";
345 *argv_ptr++ = output_dir;
349 /* Pass the option or argument to LTO. */
355 fork_execute (CONST_CAST (char **, new_argv));
360 if (lto_mode == LTO_MODE_LTO)
362 printf("%s\n", flto_out);
366 else if (lto_mode == LTO_MODE_WHOPR)
368 FILE *stream = fopen (ltrans_output_file, "r");
369 const char *collect_gcc_options, *collect_gcc;
370 struct obstack env_obstack;
371 bool seen_dumpbase = false;
372 char *dumpbase_suffix = NULL;
376 fatal_perror ("fopen: %s", ltrans_output_file);
378 /* Get the driver and options. */
379 collect_gcc = getenv ("COLLECT_GCC");
381 fatal ("environment variable COLLECT_GCC must be set");
383 /* Set the CFLAGS environment variable. */
384 collect_gcc_options = getenv ("COLLECT_GCC_OPTIONS");
385 if (!collect_gcc_options)
386 fatal ("environment variable COLLECT_GCC_OPTIONS must be set");
388 /* Count arguments. */
390 for (j = 0; collect_gcc_options[j] != '\0'; ++j)
391 if (collect_gcc_options[j] == '\'')
395 fatal ("malformed COLLECT_GCC_OPTIONS");
397 /* Initalize the arguments for the LTRANS driver. */
398 new_argv = (const char **) xmalloc ((8 + i / 2) * sizeof (char *));
400 *argv_ptr++ = collect_gcc;
401 *argv_ptr++ = "-xlto";
403 for (j = 0; collect_gcc_options[j] != '\0'; ++j)
404 if (collect_gcc_options[j] == '\'')
410 while (collect_gcc_options[j] != '\'')
412 obstack_init (&env_obstack);
413 obstack_grow (&env_obstack, &collect_gcc_options[i], j - i);
415 obstack_grow (&env_obstack, DUMPBASE_SUFFIX,
416 sizeof (DUMPBASE_SUFFIX));
418 obstack_1grow (&env_obstack, 0);
419 option = XOBFINISH (&env_obstack, char *);
422 dumpbase_suffix = option + 7 + j - i;
423 seen_dumpbase = false;
426 /* LTRANS does not need -fwhopr. */
427 if (strncmp (option, "-fwhopr", 7) != 0)
429 if (strncmp (option, "-dumpbase", 9) == 0)
430 seen_dumpbase = true;
431 *argv_ptr++ = option;
434 *argv_ptr++ = "-fltrans";
438 const unsigned piece = 32;
440 char *buf, *input_name = (char *)xmalloc (piece);
445 if (!fgets (buf, piece, stream))
447 len = strlen (input_name);
448 if (input_name[len - 1] != '\n')
450 input_name = (char *)xrealloc (input_name, len + piece);
451 buf = input_name + len;
454 input_name[len - 1] = '\0';
456 if (input_name[0] == '*')
459 output_name = &input_name[1];
468 /* Otherwise, add FILES[I] to lto_execute_ltrans command line
469 and add the resulting file to LTRANS output list. */
471 /* Replace the .o suffix with a .ltrans.o suffix and write
472 the resulting name to the LTRANS output list. */
473 obstack_init (&env_obstack);
474 obstack_grow (&env_obstack, input_name, strlen (input_name) - 2);
475 obstack_grow (&env_obstack, ".ltrans.o", sizeof (".ltrans.o"));
476 output_name = XOBFINISH (&env_obstack, char *);
479 argv_ptr[1] = output_name;
480 argv_ptr[2] = input_name;
483 /* Append a sequence number to -dumpbase for LTRANS. */
485 snprintf (dumpbase_suffix, sizeof (DUMPBASE_SUFFIX) - 7,
486 "%lu", (unsigned long) i);
488 /* Execute the driver. */
489 pex = pex_init (0, "lto1", NULL);
491 fatal ("pex_init failed: %s", xstrerror (errno));
493 errmsg = pex_run (pex, PEX_LAST | PEX_SEARCH, new_argv[0],
494 CONST_CAST (char **, new_argv),
497 fatal ("%s: %s", errmsg, xstrerror (err));
499 if (!pex_get_status (pex, 1, &status))
500 fatal ("can't get program status: %s", xstrerror (errno));
504 if (WIFSIGNALED (status))
506 int sig = WTERMSIG (status);
507 fatal ("%s terminated with signal %d [%s]%s",
508 new_argv[0], sig, strsignal (sig),
509 WCOREDUMP (status) ? ", core dumped" : "");
512 fatal ("%s terminated with status %d", new_argv[0], status);
517 maybe_unlink_file (input_name);
520 fputs (output_name, stdout);
524 maybe_unlink_file (ltrans_output_file);
525 free (list_option_full);
526 obstack_free (&env_obstack, NULL);
529 fatal ("invalid LTO mode");
533 /* Parse the command line. Copy any unused argument to GCC_ARGV. ARGC is the
534 number of arguments. ARGV contains the arguments. */
537 process_args (int argc, char *argv[], char *gcc_argv[])
542 for (i = 1; i < argc; i ++)
544 if (! strcmp (argv[i], "-debug"))
546 else if (! strcmp (argv[i], "-flto"))
547 lto_mode = LTO_MODE_LTO;
548 else if (! strcmp (argv[i], "-fwhopr"))
549 lto_mode = LTO_MODE_WHOPR;
552 gcc_argv[j] = argv[i];
564 main (int argc, char *argv[])
571 /* We may be called with all the arguments stored in some file and
572 passed with @file. Expand them into argv before processing. */
573 expandargv (&argc, &argv);
574 gcc_argv = (char **) xcalloc (sizeof (char *), argc);
575 gcc_argc = process_args (argc, argv, gcc_argv);
576 run_gcc (gcc_argc, gcc_argv);