* lto-symtab.c (lto_varpool_replace_node): Merge TLS models.
[official-gcc.git] / gcc / lto-wrapper.c
blobe9507717d4447c25bd87527c3d588d06475c2ee2
1 /* Wrapper to call lto. Used by collect2 and the linker plugin.
2 Copyright (C) 2009-2015 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
11 version.
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
16 for more details.
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.
28 Example:
30 $ lto-wrapper gcc/xgcc -B gcc a.o b.o -o test -flto
32 The above will print something like
33 /tmp/ccwbQ8B2.lto.o
35 If WHOPR is used instead, more than one file might be produced
36 ./ccXj2DTk.lto.ltrans.o
37 ./ccCJuXGv.lto.ltrans.o
40 #include "config.h"
41 #include "system.h"
42 #include "coretypes.h"
43 #include "intl.h"
44 #include "diagnostic.h"
45 #include "obstack.h"
46 #include "opts.h"
47 #include "options.h"
48 #include "simple-object.h"
49 #include "lto-section-names.h"
50 #include "collect-utils.h"
52 /* Environment variable, used for passing the names of offload targets from GCC
53 driver to lto-wrapper. */
54 #define OFFLOAD_TARGET_NAMES_ENV "OFFLOAD_TARGET_NAMES"
56 enum lto_mode_d {
57 LTO_MODE_NONE, /* Not doing LTO. */
58 LTO_MODE_LTO, /* Normal LTO. */
59 LTO_MODE_WHOPR /* WHOPR. */
62 /* Current LTO mode. */
63 static enum lto_mode_d lto_mode = LTO_MODE_NONE;
65 static char *ltrans_output_file;
66 static char *flto_out;
67 static unsigned int nr;
68 static char **input_names;
69 static char **output_names;
70 static char **offload_names;
71 static const char *offloadbegin, *offloadend;
72 static char *makefile;
74 const char tool_name[] = "lto-wrapper";
76 /* Delete tempfiles. Called from utils_cleanup. */
78 void
79 tool_cleanup (bool)
81 unsigned int i;
83 if (ltrans_output_file)
84 maybe_unlink (ltrans_output_file);
85 if (flto_out)
86 maybe_unlink (flto_out);
87 if (makefile)
88 maybe_unlink (makefile);
89 for (i = 0; i < nr; ++i)
91 maybe_unlink (input_names[i]);
92 if (output_names[i])
93 maybe_unlink (output_names[i]);
97 static void
98 lto_wrapper_cleanup (void)
100 utils_cleanup (false);
103 /* Unlink a temporary LTRANS file unless requested otherwise. */
105 void
106 maybe_unlink (const char *file)
108 if (!save_temps)
110 if (unlink_if_ordinary (file)
111 && errno != ENOENT)
112 fatal_error ("deleting LTRANS file %s: %m", file);
114 else if (verbose)
115 fprintf (stderr, "[Leaving LTRANS %s]\n", file);
118 /* Template of LTRANS dumpbase suffix. */
119 #define DUMPBASE_SUFFIX ".ltrans18446744073709551615"
121 /* Create decoded options from the COLLECT_GCC and COLLECT_GCC_OPTIONS
122 environment according to LANG_MASK. */
124 static void
125 get_options_from_collect_gcc_options (const char *collect_gcc,
126 const char *collect_gcc_options,
127 unsigned int lang_mask,
128 struct cl_decoded_option **decoded_options,
129 unsigned int *decoded_options_count)
131 struct obstack argv_obstack;
132 char *argv_storage;
133 const char **argv;
134 int j, k, argc;
136 argv_storage = xstrdup (collect_gcc_options);
137 obstack_init (&argv_obstack);
138 obstack_ptr_grow (&argv_obstack, collect_gcc);
140 for (j = 0, k = 0; argv_storage[j] != '\0'; ++j)
142 if (argv_storage[j] == '\'')
144 obstack_ptr_grow (&argv_obstack, &argv_storage[k]);
145 ++j;
148 if (argv_storage[j] == '\0')
149 fatal_error ("malformed COLLECT_GCC_OPTIONS");
150 else if (strncmp (&argv_storage[j], "'\\''", 4) == 0)
152 argv_storage[k++] = '\'';
153 j += 4;
155 else if (argv_storage[j] == '\'')
156 break;
157 else
158 argv_storage[k++] = argv_storage[j++];
160 while (1);
161 argv_storage[k++] = '\0';
165 obstack_ptr_grow (&argv_obstack, NULL);
166 argc = obstack_object_size (&argv_obstack) / sizeof (void *) - 1;
167 argv = XOBFINISH (&argv_obstack, const char **);
169 decode_cmdline_options_to_array (argc, (const char **)argv,
170 lang_mask,
171 decoded_options, decoded_options_count);
172 obstack_free (&argv_obstack, NULL);
175 /* Append OPTION to the options array DECODED_OPTIONS with size
176 DECODED_OPTIONS_COUNT. */
178 static void
179 append_option (struct cl_decoded_option **decoded_options,
180 unsigned int *decoded_options_count,
181 struct cl_decoded_option *option)
183 ++*decoded_options_count;
184 *decoded_options
185 = (struct cl_decoded_option *)
186 xrealloc (*decoded_options,
187 (*decoded_options_count
188 * sizeof (struct cl_decoded_option)));
189 memcpy (&(*decoded_options)[*decoded_options_count - 1], option,
190 sizeof (struct cl_decoded_option));
193 /* Try to merge and complain about options FDECODED_OPTIONS when applied
194 ontop of DECODED_OPTIONS. */
196 static void
197 merge_and_complain (struct cl_decoded_option **decoded_options,
198 unsigned int *decoded_options_count,
199 struct cl_decoded_option *fdecoded_options,
200 unsigned int fdecoded_options_count)
202 unsigned int i, j;
204 /* ??? Merge options from files. Most cases can be
205 handled by either unioning or intersecting
206 (for example -fwrapv is a case for unioning,
207 -ffast-math is for intersection). Most complaints
208 about real conflicts between different options can
209 be deferred to the compiler proper. Options that
210 we can neither safely handle by intersection nor
211 unioning would need to be complained about here.
212 Ideally we'd have a flag in the opt files that
213 tells whether to union or intersect or reject.
214 In absence of that it's unclear what a good default is.
215 It's also difficult to get positional handling correct. */
217 /* The following does what the old LTO option code did,
218 union all target and a selected set of common options. */
219 for (i = 0; i < fdecoded_options_count; ++i)
221 struct cl_decoded_option *foption = &fdecoded_options[i];
222 switch (foption->opt_index)
224 case OPT_SPECIAL_unknown:
225 case OPT_SPECIAL_ignore:
226 case OPT_SPECIAL_program_name:
227 case OPT_SPECIAL_input_file:
228 break;
230 default:
231 if (!(cl_options[foption->opt_index].flags & CL_TARGET))
232 break;
234 /* Fallthru. */
235 case OPT_fPIC:
236 case OPT_fpic:
237 case OPT_fPIE:
238 case OPT_fpie:
239 case OPT_fcommon:
240 case OPT_fexceptions:
241 case OPT_fnon_call_exceptions:
242 case OPT_fgnu_tm:
243 /* Do what the old LTO code did - collect exactly one option
244 setting per OPT code, we pick the first we encounter.
245 ??? This doesn't make too much sense, but when it doesn't
246 then we should complain. */
247 for (j = 0; j < *decoded_options_count; ++j)
248 if ((*decoded_options)[j].opt_index == foption->opt_index)
249 break;
250 if (j == *decoded_options_count)
251 append_option (decoded_options, decoded_options_count, foption);
252 break;
254 case OPT_ftrapv:
255 case OPT_fstrict_overflow:
256 case OPT_ffp_contract_:
257 /* For selected options we can merge conservatively. */
258 for (j = 0; j < *decoded_options_count; ++j)
259 if ((*decoded_options)[j].opt_index == foption->opt_index)
260 break;
261 if (j == *decoded_options_count)
262 append_option (decoded_options, decoded_options_count, foption);
263 /* FP_CONTRACT_OFF < FP_CONTRACT_ON < FP_CONTRACT_FAST,
264 -fno-trapv < -ftrapv,
265 -fno-strict-overflow < -fstrict-overflow */
266 else if (foption->value < (*decoded_options)[j].value)
267 (*decoded_options)[j] = *foption;
268 break;
270 case OPT_fmath_errno:
271 case OPT_fsigned_zeros:
272 case OPT_ftrapping_math:
273 case OPT_fwrapv:
274 case OPT_fopenmp:
275 case OPT_fopenacc:
276 /* For selected options we can merge conservatively. */
277 for (j = 0; j < *decoded_options_count; ++j)
278 if ((*decoded_options)[j].opt_index == foption->opt_index)
279 break;
280 if (j == *decoded_options_count)
281 append_option (decoded_options, decoded_options_count, foption);
282 /* -fmath-errno > -fno-math-errno,
283 -fsigned-zeros > -fno-signed-zeros,
284 -ftrapping-math -> -fno-trapping-math,
285 -fwrapv > -fno-wrapv. */
286 else if (foption->value > (*decoded_options)[j].value)
287 (*decoded_options)[j] = *foption;
288 break;
290 case OPT_freg_struct_return:
291 case OPT_fpcc_struct_return:
292 case OPT_fshort_double:
293 for (j = 0; j < *decoded_options_count; ++j)
294 if ((*decoded_options)[j].opt_index == foption->opt_index)
295 break;
296 if (j == *decoded_options_count)
297 fatal_error ("Option %s not used consistently in all LTO input"
298 " files", foption->orig_option_with_args_text);
299 break;
301 case OPT_foffload_abi_:
302 for (j = 0; j < *decoded_options_count; ++j)
303 if ((*decoded_options)[j].opt_index == foption->opt_index)
304 break;
305 if (j == *decoded_options_count)
306 append_option (decoded_options, decoded_options_count, foption);
307 else if (foption->value != (*decoded_options)[j].value)
308 fatal_error ("Option %s not used consistently in all LTO input"
309 " files", foption->orig_option_with_args_text);
310 break;
312 case OPT_O:
313 case OPT_Ofast:
314 case OPT_Og:
315 case OPT_Os:
316 for (j = 0; j < *decoded_options_count; ++j)
317 if ((*decoded_options)[j].opt_index == OPT_O
318 || (*decoded_options)[j].opt_index == OPT_Ofast
319 || (*decoded_options)[j].opt_index == OPT_Og
320 || (*decoded_options)[j].opt_index == OPT_Os)
321 break;
322 if (j == *decoded_options_count)
323 append_option (decoded_options, decoded_options_count, foption);
324 else if ((*decoded_options)[j].opt_index == foption->opt_index
325 && foption->opt_index != OPT_O)
326 /* Exact same options get merged. */
328 else
330 /* For mismatched option kinds preserve the optimization
331 level only, thus merge it as -On. This also handles
332 merging of same optimization level -On. */
333 int level = 0;
334 switch (foption->opt_index)
336 case OPT_O:
337 if (foption->arg[0] == '\0')
338 level = MAX (level, 1);
339 else
340 level = MAX (level, atoi (foption->arg));
341 break;
342 case OPT_Ofast:
343 level = MAX (level, 3);
344 break;
345 case OPT_Og:
346 level = MAX (level, 1);
347 break;
348 case OPT_Os:
349 level = MAX (level, 2);
350 break;
351 default:
352 gcc_unreachable ();
354 switch ((*decoded_options)[j].opt_index)
356 case OPT_O:
357 if ((*decoded_options)[j].arg[0] == '\0')
358 level = MAX (level, 1);
359 else
360 level = MAX (level, atoi ((*decoded_options)[j].arg));
361 break;
362 case OPT_Ofast:
363 level = MAX (level, 3);
364 break;
365 case OPT_Og:
366 level = MAX (level, 1);
367 break;
368 case OPT_Os:
369 level = MAX (level, 2);
370 break;
371 default:
372 gcc_unreachable ();
374 (*decoded_options)[j].opt_index = OPT_O;
375 char *tem;
376 tem = xasprintf ("-O%d", level);
377 (*decoded_options)[j].arg = &tem[2];
378 (*decoded_options)[j].canonical_option[0] = tem;
379 (*decoded_options)[j].value = 1;
381 break;
383 case OPT_foffload_:
384 append_option (decoded_options, decoded_options_count, foption);
385 break;
390 /* Auxiliary function that frees elements of PTR and PTR itself.
391 N is number of elements to be freed. If PTR is NULL, nothing is freed.
392 If an element is NULL, subsequent elements are not freed. */
394 static void **
395 free_array_of_ptrs (void **ptr, unsigned n)
397 if (!ptr)
398 return NULL;
399 for (unsigned i = 0; i < n; i++)
401 if (!ptr[i])
402 break;
403 free (ptr[i]);
405 free (ptr);
406 return NULL;
409 /* Parse STR, saving found tokens into PVALUES and return their number.
410 Tokens are assumed to be delimited by ':'. If APPEND is non-null,
411 append it to every token we find. */
413 static unsigned
414 parse_env_var (const char *str, char ***pvalues, const char *append)
416 const char *curval, *nextval;
417 char **values;
418 unsigned num = 1, i;
420 curval = strchr (str, ':');
421 while (curval)
423 num++;
424 curval = strchr (curval + 1, ':');
427 values = (char**) xmalloc (num * sizeof (char*));
428 curval = str;
429 nextval = strchr (curval, ':');
430 if (nextval == NULL)
431 nextval = strchr (curval, '\0');
433 int append_len = append ? strlen (append) : 0;
434 for (i = 0; i < num; i++)
436 int l = nextval - curval;
437 values[i] = (char*) xmalloc (l + 1 + append_len);
438 memcpy (values[i], curval, l);
439 values[i][l] = 0;
440 if (append)
441 strcat (values[i], append);
442 curval = nextval + 1;
443 nextval = strchr (curval, ':');
444 if (nextval == NULL)
445 nextval = strchr (curval, '\0');
447 *pvalues = values;
448 return num;
451 /* Append options OPTS from lto or offload_lto sections to ARGV_OBSTACK. */
453 static void
454 append_compiler_options (obstack *argv_obstack, struct cl_decoded_option *opts,
455 unsigned int count)
457 /* Append compiler driver arguments as far as they were merged. */
458 for (unsigned int j = 1; j < count; ++j)
460 struct cl_decoded_option *option = &opts[j];
462 /* File options have been properly filtered by lto-opts.c. */
463 switch (option->opt_index)
465 /* Drop arguments that we want to take from the link line. */
466 case OPT_flto_:
467 case OPT_flto:
468 case OPT_flto_partition_:
469 continue;
471 default:
472 break;
475 /* For now do what the original LTO option code was doing - pass
476 on any CL_TARGET flag and a few selected others. */
477 switch (option->opt_index)
479 case OPT_fPIC:
480 case OPT_fpic:
481 case OPT_fPIE:
482 case OPT_fpie:
483 case OPT_fcommon:
484 case OPT_fexceptions:
485 case OPT_fnon_call_exceptions:
486 case OPT_fgnu_tm:
487 case OPT_freg_struct_return:
488 case OPT_fpcc_struct_return:
489 case OPT_fshort_double:
490 case OPT_ffp_contract_:
491 case OPT_fmath_errno:
492 case OPT_fsigned_zeros:
493 case OPT_ftrapping_math:
494 case OPT_fwrapv:
495 case OPT_fopenmp:
496 case OPT_fopenacc:
497 case OPT_ftrapv:
498 case OPT_fstrict_overflow:
499 case OPT_foffload_abi_:
500 case OPT_O:
501 case OPT_Ofast:
502 case OPT_Og:
503 case OPT_Os:
504 break;
506 default:
507 if (!(cl_options[option->opt_index].flags & CL_TARGET))
508 continue;
511 /* Pass the option on. */
512 for (unsigned int i = 0; i < option->canonical_option_num_elements; ++i)
513 obstack_ptr_grow (argv_obstack, option->canonical_option[i]);
517 /* Append linker options OPTS to ARGV_OBSTACK. */
519 static void
520 append_linker_options (obstack *argv_obstack, struct cl_decoded_option *opts,
521 unsigned int count)
523 /* Append linker driver arguments. Compiler options from the linker
524 driver arguments will override / merge with those from the compiler. */
525 for (unsigned int j = 1; j < count; ++j)
527 struct cl_decoded_option *option = &opts[j];
529 /* Do not pass on frontend specific flags not suitable for lto. */
530 if (!(cl_options[option->opt_index].flags
531 & (CL_COMMON|CL_TARGET|CL_DRIVER|CL_LTO)))
532 continue;
534 switch (option->opt_index)
536 case OPT_o:
537 case OPT_flto_:
538 case OPT_flto:
539 /* We've handled these LTO options, do not pass them on. */
540 continue;
542 case OPT_freg_struct_return:
543 case OPT_fpcc_struct_return:
544 case OPT_fshort_double:
545 /* Ignore these, they are determined by the input files.
546 ??? We fail to diagnose a possible mismatch here. */
547 continue;
549 default:
550 break;
553 /* Pass the option on. */
554 for (unsigned int i = 0; i < option->canonical_option_num_elements; ++i)
555 obstack_ptr_grow (argv_obstack, option->canonical_option[i]);
559 /* Extract options for TARGET offload compiler from OPTIONS and append
560 them to ARGV_OBSTACK. */
562 static void
563 append_offload_options (obstack *argv_obstack, const char *target,
564 struct cl_decoded_option *options,
565 unsigned int options_count)
567 for (unsigned i = 0; i < options_count; i++)
569 const char *cur, *next, *opts;
570 char **argv;
571 unsigned argc;
572 struct cl_decoded_option *option = &options[i];
574 if (option->opt_index != OPT_foffload_)
575 continue;
577 /* If option argument starts with '-' then no target is specified. That
578 means offload options are specified for all targets, so we need to
579 append them. */
580 if (option->arg[0] == '-')
581 opts = option->arg;
582 else
584 opts = strchr (option->arg, '=');
585 if (!opts)
586 continue;
588 cur = option->arg;
590 while (cur < opts)
592 next = strchr (cur, ',');
593 if (next == NULL)
594 next = opts;
595 next = (next > opts) ? opts : next;
597 if (strlen (target) == (size_t) (next - cur)
598 && strncmp (target, cur, next - cur) == 0)
599 break;
601 cur = next + 1;
604 if (cur >= opts)
605 continue;
607 opts++;
610 argv = buildargv (opts);
611 for (argc = 0; argv[argc]; argc++)
612 obstack_ptr_grow (argv_obstack, argv[argc]);
616 /* Check whether NAME can be accessed in MODE. This is like access,
617 except that it never considers directories to be executable. */
619 static int
620 access_check (const char *name, int mode)
622 if (mode == X_OK)
624 struct stat st;
626 if (stat (name, &st) < 0
627 || S_ISDIR (st.st_mode))
628 return -1;
631 return access (name, mode);
634 /* Prepare a target image for offload TARGET, using mkoffload tool from
635 COMPILER_PATH. Return the name of the resultant object file. */
637 static char *
638 compile_offload_image (const char *target, const char *compiler_path,
639 unsigned in_argc, char *in_argv[],
640 struct cl_decoded_option *compiler_opts,
641 unsigned int compiler_opt_count,
642 struct cl_decoded_option *linker_opts,
643 unsigned int linker_opt_count)
645 char *filename = NULL;
646 char **argv;
647 char *suffix
648 = XALLOCAVEC (char, sizeof ("/accel//mkoffload") + strlen (target));
649 strcpy (suffix, "/accel/");
650 strcat (suffix, target);
651 strcat (suffix, "/mkoffload");
653 char **paths = NULL;
654 unsigned n_paths = parse_env_var (compiler_path, &paths, suffix);
656 const char *compiler = NULL;
657 for (unsigned i = 0; i < n_paths; i++)
658 if (access_check (paths[i], X_OK) == 0)
660 compiler = paths[i];
661 break;
664 if (compiler)
666 /* Generate temporary output file name. */
667 filename = make_temp_file (".target.o");
669 struct obstack argv_obstack;
670 obstack_init (&argv_obstack);
671 obstack_ptr_grow (&argv_obstack, compiler);
672 obstack_ptr_grow (&argv_obstack, "-o");
673 obstack_ptr_grow (&argv_obstack, filename);
675 /* Append names of input object files. */
676 for (unsigned i = 0; i < in_argc; i++)
677 obstack_ptr_grow (&argv_obstack, in_argv[i]);
679 /* Append options from offload_lto sections. */
680 append_compiler_options (&argv_obstack, compiler_opts,
681 compiler_opt_count);
683 /* Append options specified by -foffload last. In case of conflicting
684 options we expect offload compiler to choose the latest. */
685 append_offload_options (&argv_obstack, target, compiler_opts,
686 compiler_opt_count);
687 append_offload_options (&argv_obstack, target, linker_opts,
688 linker_opt_count);
690 obstack_ptr_grow (&argv_obstack, NULL);
691 argv = XOBFINISH (&argv_obstack, char **);
692 fork_execute (argv[0], argv, true);
693 obstack_free (&argv_obstack, NULL);
696 free_array_of_ptrs ((void **) paths, n_paths);
697 return filename;
701 /* The main routine dealing with offloading.
702 The routine builds a target image for each offload target. IN_ARGC and
703 IN_ARGV specify options and input object files. As all of them could contain
704 target sections, we pass them all to target compilers. */
706 static void
707 compile_images_for_offload_targets (unsigned in_argc, char *in_argv[],
708 struct cl_decoded_option *compiler_opts,
709 unsigned int compiler_opt_count,
710 struct cl_decoded_option *linker_opts,
711 unsigned int linker_opt_count)
713 char **names = NULL;
714 const char *target_names = getenv (OFFLOAD_TARGET_NAMES_ENV);
715 if (!target_names)
716 return;
717 unsigned num_targets = parse_env_var (target_names, &names, NULL);
719 const char *compiler_path = getenv ("COMPILER_PATH");
720 if (!compiler_path)
721 goto out;
723 /* Prepare an image for each target and save the name of the resultant object
724 file to the OFFLOAD_NAMES array. It is terminated by a NULL entry. */
725 offload_names = XCNEWVEC (char *, num_targets + 1);
726 for (unsigned i = 0; i < num_targets; i++)
728 offload_names[i]
729 = compile_offload_image (names[i], compiler_path, in_argc, in_argv,
730 compiler_opts, compiler_opt_count,
731 linker_opts, linker_opt_count);
732 if (!offload_names[i])
733 fatal_error ("problem with building target image for %s\n", names[i]);
736 out:
737 free_array_of_ptrs ((void **) names, num_targets);
740 /* Copy a file from SRC to DEST. */
742 static void
743 copy_file (const char *dest, const char *src)
745 FILE *d = fopen (dest, "wb");
746 FILE *s = fopen (src, "rb");
747 char buffer[512];
748 while (!feof (s))
750 size_t len = fread (buffer, 1, 512, s);
751 if (ferror (s) != 0)
752 fatal_error ("reading input file");
753 if (len > 0)
755 fwrite (buffer, 1, len, d);
756 if (ferror (d) != 0)
757 fatal_error ("writing output file");
762 /* Find the crtoffloadbegin.o and crtoffloadend.o files in LIBRARY_PATH, make
763 copies and store the names of the copies in offloadbegin and offloadend. */
765 static void
766 find_offloadbeginend (void)
768 char **paths = NULL;
769 const char *library_path = getenv ("LIBRARY_PATH");
770 if (!library_path)
771 return;
772 unsigned n_paths = parse_env_var (library_path, &paths, "/crtoffloadbegin.o");
774 unsigned i;
775 for (i = 0; i < n_paths; i++)
776 if (access_check (paths[i], R_OK) == 0)
778 size_t len = strlen (paths[i]);
779 char *tmp = xstrdup (paths[i]);
780 strcpy (paths[i] + len - strlen ("begin.o"), "end.o");
781 if (access_check (paths[i], R_OK) != 0)
782 fatal_error ("installation error, can't find crtoffloadend.o");
783 /* The linker will delete the filenames we give it, so make
784 copies. */
785 offloadbegin = make_temp_file (".o");
786 offloadend = make_temp_file (".o");
787 copy_file (offloadbegin, tmp);
788 copy_file (offloadend, paths[i]);
789 free (tmp);
790 break;
792 if (i == n_paths)
793 fatal_error ("installation error, can't find crtoffloadbegin.o");
795 free_array_of_ptrs ((void **) paths, n_paths);
798 /* A subroutine of run_gcc. Examine the open file FD for lto sections with
799 name prefix PREFIX, at FILE_OFFSET, and store any options we find in OPTS
800 and OPT_COUNT. Return true if we found a matchingn section, false
801 otherwise. COLLECT_GCC holds the value of the environment variable with
802 the same name. */
804 static bool
805 find_and_merge_options (int fd, off_t file_offset, const char *prefix,
806 struct cl_decoded_option **opts,
807 unsigned int *opt_count, const char *collect_gcc)
809 off_t offset, length;
810 char *data;
811 char *fopts;
812 const char *errmsg;
813 int err;
814 struct cl_decoded_option *fdecoded_options = *opts;
815 unsigned int fdecoded_options_count = *opt_count;
817 simple_object_read *sobj;
818 sobj = simple_object_start_read (fd, file_offset, "__GNU_LTO",
819 &errmsg, &err);
820 if (!sobj)
821 return false;
823 char *secname = XALLOCAVEC (char, strlen (prefix) + sizeof (".opts"));
824 strcpy (secname, prefix);
825 strcat (secname, ".opts");
826 if (!simple_object_find_section (sobj, secname, &offset, &length,
827 &errmsg, &err))
829 simple_object_release_read (sobj);
830 return false;
833 lseek (fd, file_offset + offset, SEEK_SET);
834 data = (char *)xmalloc (length);
835 read (fd, data, length);
836 fopts = data;
839 struct cl_decoded_option *f2decoded_options;
840 unsigned int f2decoded_options_count;
841 get_options_from_collect_gcc_options (collect_gcc,
842 fopts, CL_LANG_ALL,
843 &f2decoded_options,
844 &f2decoded_options_count);
845 if (!fdecoded_options)
847 fdecoded_options = f2decoded_options;
848 fdecoded_options_count = f2decoded_options_count;
850 else
851 merge_and_complain (&fdecoded_options,
852 &fdecoded_options_count,
853 f2decoded_options, f2decoded_options_count);
855 fopts += strlen (fopts) + 1;
857 while (fopts - data < length);
859 free (data);
860 simple_object_release_read (sobj);
861 *opts = fdecoded_options;
862 *opt_count = fdecoded_options_count;
863 return true;
866 /* Execute gcc. ARGC is the number of arguments. ARGV contains the arguments. */
868 static void
869 run_gcc (unsigned argc, char *argv[])
871 unsigned i, j;
872 const char **new_argv;
873 const char **argv_ptr;
874 char *list_option_full = NULL;
875 const char *linker_output = NULL;
876 const char *collect_gcc, *collect_gcc_options;
877 int parallel = 0;
878 int jobserver = 0;
879 bool no_partition = false;
880 struct cl_decoded_option *fdecoded_options = NULL;
881 struct cl_decoded_option *offload_fdecoded_options = NULL;
882 unsigned int fdecoded_options_count = 0;
883 unsigned int offload_fdecoded_options_count = 0;
884 struct cl_decoded_option *decoded_options;
885 unsigned int decoded_options_count;
886 struct obstack argv_obstack;
887 int new_head_argc;
888 bool have_lto = false;
889 bool have_offload = false;
890 unsigned lto_argc = 0, offload_argc = 0;
891 char **lto_argv, **offload_argv;
893 /* Get the driver and options. */
894 collect_gcc = getenv ("COLLECT_GCC");
895 if (!collect_gcc)
896 fatal_error ("environment variable COLLECT_GCC must be set");
897 collect_gcc_options = getenv ("COLLECT_GCC_OPTIONS");
898 if (!collect_gcc_options)
899 fatal_error ("environment variable COLLECT_GCC_OPTIONS must be set");
900 get_options_from_collect_gcc_options (collect_gcc, collect_gcc_options,
901 CL_LANG_ALL,
902 &decoded_options,
903 &decoded_options_count);
905 /* Allocate arrays for input object files with LTO or offload IL,
906 and for possible preceding arguments. */
907 lto_argv = XNEWVEC (char *, argc);
908 offload_argv = XNEWVEC (char *, argc);
910 /* Look at saved options in the IL files. */
911 for (i = 1; i < argc; ++i)
913 char *p;
914 int fd;
915 off_t file_offset = 0;
916 long loffset;
917 int consumed;
918 char *filename = argv[i];
920 if ((p = strrchr (argv[i], '@'))
921 && p != argv[i]
922 && sscanf (p, "@%li%n", &loffset, &consumed) >= 1
923 && strlen (p) == (unsigned int) consumed)
925 filename = XNEWVEC (char, p - argv[i] + 1);
926 memcpy (filename, argv[i], p - argv[i]);
927 filename[p - argv[i]] = '\0';
928 file_offset = (off_t) loffset;
930 fd = open (argv[i], O_RDONLY);
931 if (fd == -1)
933 lto_argv[lto_argc++] = argv[i];
934 continue;
937 if (find_and_merge_options (fd, file_offset, LTO_SECTION_NAME_PREFIX,
938 &fdecoded_options, &fdecoded_options_count,
939 collect_gcc))
941 have_lto = true;
942 lto_argv[lto_argc++] = argv[i];
945 if (find_and_merge_options (fd, file_offset, OFFLOAD_SECTION_NAME_PREFIX,
946 &offload_fdecoded_options,
947 &offload_fdecoded_options_count, collect_gcc))
949 have_offload = true;
950 offload_argv[offload_argc++] = argv[i];
953 close (fd);
956 /* Initalize the common arguments for the driver. */
957 obstack_init (&argv_obstack);
958 obstack_ptr_grow (&argv_obstack, collect_gcc);
959 obstack_ptr_grow (&argv_obstack, "-xlto");
960 obstack_ptr_grow (&argv_obstack, "-c");
962 append_compiler_options (&argv_obstack, fdecoded_options,
963 fdecoded_options_count);
964 append_linker_options (&argv_obstack, decoded_options, decoded_options_count);
966 /* Scan linker driver arguments for things that are of relevance to us. */
967 for (j = 1; j < decoded_options_count; ++j)
969 struct cl_decoded_option *option = &decoded_options[j];
970 switch (option->opt_index)
972 case OPT_o:
973 linker_output = option->arg;
974 break;
976 case OPT_save_temps:
977 save_temps = 1;
978 break;
980 case OPT_v:
981 verbose = 1;
982 break;
984 case OPT_flto_partition_:
985 if (strcmp (option->arg, "none") == 0)
986 no_partition = true;
987 break;
989 case OPT_flto_:
990 if (strcmp (option->arg, "jobserver") == 0)
992 jobserver = 1;
993 parallel = 1;
995 else
997 parallel = atoi (option->arg);
998 if (parallel <= 1)
999 parallel = 0;
1001 /* Fallthru. */
1003 case OPT_flto:
1004 lto_mode = LTO_MODE_WHOPR;
1005 break;
1007 default:
1008 break;
1012 if (no_partition)
1014 lto_mode = LTO_MODE_LTO;
1015 jobserver = 0;
1016 parallel = 0;
1019 if (linker_output)
1021 char *output_dir, *base, *name;
1022 bool bit_bucket = strcmp (linker_output, HOST_BIT_BUCKET) == 0;
1024 output_dir = xstrdup (linker_output);
1025 base = output_dir;
1026 for (name = base; *name; name++)
1027 if (IS_DIR_SEPARATOR (*name))
1028 base = name + 1;
1029 *base = '\0';
1031 linker_output = &linker_output[base - output_dir];
1032 if (*output_dir == '\0')
1034 static char current_dir[] = { '.', DIR_SEPARATOR, '\0' };
1035 output_dir = current_dir;
1037 if (!bit_bucket)
1039 obstack_ptr_grow (&argv_obstack, "-dumpdir");
1040 obstack_ptr_grow (&argv_obstack, output_dir);
1043 obstack_ptr_grow (&argv_obstack, "-dumpbase");
1046 /* Remember at which point we can scrub args to re-use the commons. */
1047 new_head_argc = obstack_object_size (&argv_obstack) / sizeof (void *);
1049 if (have_offload)
1051 compile_images_for_offload_targets (offload_argc, offload_argv,
1052 offload_fdecoded_options,
1053 offload_fdecoded_options_count,
1054 decoded_options,
1055 decoded_options_count);
1056 if (offload_names)
1058 find_offloadbeginend ();
1059 for (i = 0; offload_names[i]; i++)
1060 printf ("%s\n", offload_names[i]);
1061 free_array_of_ptrs ((void **) offload_names, i);
1065 if (offloadbegin)
1066 printf ("%s\n", offloadbegin);
1068 /* If object files contain offload sections, but do not contain LTO sections,
1069 then there is no need to perform a link-time recompilation, i.e.
1070 lto-wrapper is used only for a compilation of offload images. */
1071 if (have_offload && !have_lto)
1073 for (i = 1; i < argc; ++i)
1074 if (strncmp (argv[i], "-fresolution=", sizeof ("-fresolution=") - 1))
1076 char *out_file;
1077 /* Can be ".o" or ".so". */
1078 char *ext = strrchr (argv[i], '.');
1079 if (ext == NULL)
1080 out_file = make_temp_file ("");
1081 else
1082 out_file = make_temp_file (ext);
1083 /* The linker will delete the files we give it, so make copies. */
1084 copy_file (out_file, argv[i]);
1085 printf ("%s\n", out_file);
1087 goto finish;
1090 if (lto_mode == LTO_MODE_LTO)
1092 flto_out = make_temp_file (".lto.o");
1093 if (linker_output)
1094 obstack_ptr_grow (&argv_obstack, linker_output);
1095 obstack_ptr_grow (&argv_obstack, "-o");
1096 obstack_ptr_grow (&argv_obstack, flto_out);
1098 else
1100 const char *list_option = "-fltrans-output-list=";
1101 size_t list_option_len = strlen (list_option);
1102 char *tmp;
1104 if (linker_output)
1106 char *dumpbase = (char *) xmalloc (strlen (linker_output)
1107 + sizeof (".wpa") + 1);
1108 strcpy (dumpbase, linker_output);
1109 strcat (dumpbase, ".wpa");
1110 obstack_ptr_grow (&argv_obstack, dumpbase);
1113 if (linker_output && save_temps)
1115 ltrans_output_file = (char *) xmalloc (strlen (linker_output)
1116 + sizeof (".ltrans.out") + 1);
1117 strcpy (ltrans_output_file, linker_output);
1118 strcat (ltrans_output_file, ".ltrans.out");
1120 else
1121 ltrans_output_file = make_temp_file (".ltrans.out");
1122 list_option_full = (char *) xmalloc (sizeof (char) *
1123 (strlen (ltrans_output_file) + list_option_len + 1));
1124 tmp = list_option_full;
1126 obstack_ptr_grow (&argv_obstack, tmp);
1127 strcpy (tmp, list_option);
1128 tmp += list_option_len;
1129 strcpy (tmp, ltrans_output_file);
1131 if (jobserver)
1132 obstack_ptr_grow (&argv_obstack, xstrdup ("-fwpa=jobserver"));
1133 else if (parallel > 1)
1135 char buf[256];
1136 sprintf (buf, "-fwpa=%i", parallel);
1137 obstack_ptr_grow (&argv_obstack, xstrdup (buf));
1139 else
1140 obstack_ptr_grow (&argv_obstack, "-fwpa");
1143 /* Append the input objects and possible preceding arguments. */
1144 for (i = 0; i < lto_argc; ++i)
1145 obstack_ptr_grow (&argv_obstack, lto_argv[i]);
1146 obstack_ptr_grow (&argv_obstack, NULL);
1148 new_argv = XOBFINISH (&argv_obstack, const char **);
1149 argv_ptr = &new_argv[new_head_argc];
1150 fork_execute (new_argv[0], CONST_CAST (char **, new_argv), true);
1152 if (lto_mode == LTO_MODE_LTO)
1154 printf ("%s\n", flto_out);
1155 free (flto_out);
1156 flto_out = NULL;
1158 else
1160 FILE *stream = fopen (ltrans_output_file, "r");
1161 FILE *mstream = NULL;
1162 struct obstack env_obstack;
1164 if (!stream)
1165 fatal_error ("fopen: %s: %m", ltrans_output_file);
1167 /* Parse the list of LTRANS inputs from the WPA stage. */
1168 obstack_init (&env_obstack);
1169 nr = 0;
1170 for (;;)
1172 const unsigned piece = 32;
1173 char *output_name = NULL;
1174 char *buf, *input_name = (char *)xmalloc (piece);
1175 size_t len;
1177 buf = input_name;
1178 cont:
1179 if (!fgets (buf, piece, stream))
1180 break;
1181 len = strlen (input_name);
1182 if (input_name[len - 1] != '\n')
1184 input_name = (char *)xrealloc (input_name, len + piece);
1185 buf = input_name + len;
1186 goto cont;
1188 input_name[len - 1] = '\0';
1190 if (input_name[0] == '*')
1191 output_name = &input_name[1];
1193 nr++;
1194 input_names = (char **)xrealloc (input_names, nr * sizeof (char *));
1195 output_names = (char **)xrealloc (output_names, nr * sizeof (char *));
1196 input_names[nr-1] = input_name;
1197 output_names[nr-1] = output_name;
1199 fclose (stream);
1200 maybe_unlink (ltrans_output_file);
1201 ltrans_output_file = NULL;
1203 if (parallel)
1205 makefile = make_temp_file (".mk");
1206 mstream = fopen (makefile, "w");
1209 /* Execute the LTRANS stage for each input file (or prepare a
1210 makefile to invoke this in parallel). */
1211 for (i = 0; i < nr; ++i)
1213 char *output_name;
1214 char *input_name = input_names[i];
1215 /* If it's a pass-through file do nothing. */
1216 if (output_names[i])
1217 continue;
1219 /* Replace the .o suffix with a .ltrans.o suffix and write
1220 the resulting name to the LTRANS output list. */
1221 obstack_grow (&env_obstack, input_name, strlen (input_name) - 2);
1222 obstack_grow (&env_obstack, ".ltrans.o", sizeof (".ltrans.o"));
1223 output_name = XOBFINISH (&env_obstack, char *);
1225 /* Adjust the dumpbase if the linker output file was seen. */
1226 if (linker_output)
1228 char *dumpbase
1229 = (char *) xmalloc (strlen (linker_output)
1230 + sizeof (DUMPBASE_SUFFIX) + 1);
1231 snprintf (dumpbase,
1232 strlen (linker_output) + sizeof (DUMPBASE_SUFFIX),
1233 "%s.ltrans%u", linker_output, i);
1234 argv_ptr[0] = dumpbase;
1237 argv_ptr[1] = "-fltrans";
1238 argv_ptr[2] = "-o";
1239 argv_ptr[3] = output_name;
1240 argv_ptr[4] = input_name;
1241 argv_ptr[5] = NULL;
1242 if (parallel)
1244 fprintf (mstream, "%s:\n\t@%s ", output_name, new_argv[0]);
1245 for (j = 1; new_argv[j] != NULL; ++j)
1246 fprintf (mstream, " '%s'", new_argv[j]);
1247 fprintf (mstream, "\n");
1248 /* If we are not preserving the ltrans input files then
1249 truncate them as soon as we have processed it. This
1250 reduces temporary disk-space usage. */
1251 if (! save_temps)
1252 fprintf (mstream, "\t@-touch -r %s %s.tem > /dev/null 2>&1 "
1253 "&& mv %s.tem %s\n",
1254 input_name, input_name, input_name, input_name);
1256 else
1258 fork_execute (new_argv[0], CONST_CAST (char **, new_argv),
1259 true);
1260 maybe_unlink (input_name);
1263 output_names[i] = output_name;
1265 if (parallel)
1267 struct pex_obj *pex;
1268 char jobs[32];
1270 fprintf (mstream, "all:");
1271 for (i = 0; i < nr; ++i)
1272 fprintf (mstream, " \\\n\t%s", output_names[i]);
1273 fprintf (mstream, "\n");
1274 fclose (mstream);
1275 if (!jobserver)
1277 /* Avoid passing --jobserver-fd= and similar flags
1278 unless jobserver mode is explicitly enabled. */
1279 putenv (xstrdup ("MAKEFLAGS="));
1280 putenv (xstrdup ("MFLAGS="));
1282 new_argv[0] = getenv ("MAKE");
1283 if (!new_argv[0])
1284 new_argv[0] = "make";
1285 new_argv[1] = "-f";
1286 new_argv[2] = makefile;
1287 i = 3;
1288 if (!jobserver)
1290 snprintf (jobs, 31, "-j%d", parallel);
1291 new_argv[i++] = jobs;
1293 new_argv[i++] = "all";
1294 new_argv[i++] = NULL;
1295 pex = collect_execute (new_argv[0], CONST_CAST (char **, new_argv),
1296 NULL, NULL, PEX_SEARCH, false);
1297 do_wait (new_argv[0], pex);
1298 maybe_unlink (makefile);
1299 makefile = NULL;
1300 for (i = 0; i < nr; ++i)
1301 maybe_unlink (input_names[i]);
1303 for (i = 0; i < nr; ++i)
1305 fputs (output_names[i], stdout);
1306 putc ('\n', stdout);
1307 free (input_names[i]);
1309 nr = 0;
1310 free (output_names);
1311 free (input_names);
1312 free (list_option_full);
1313 obstack_free (&env_obstack, NULL);
1316 finish:
1317 if (offloadend)
1318 printf ("%s\n", offloadend);
1320 XDELETE (lto_argv);
1321 XDELETE (offload_argv);
1322 obstack_free (&argv_obstack, NULL);
1326 /* Entry point. */
1329 main (int argc, char *argv[])
1331 const char *p;
1333 gcc_obstack_init (&opts_obstack);
1335 p = argv[0] + strlen (argv[0]);
1336 while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
1337 --p;
1338 progname = p;
1340 xmalloc_set_program_name (progname);
1342 gcc_init_libintl ();
1344 diagnostic_initialize (global_dc, 0);
1346 if (atexit (lto_wrapper_cleanup) != 0)
1347 fatal_error ("atexit failed");
1349 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
1350 signal (SIGINT, fatal_signal);
1351 #ifdef SIGHUP
1352 if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
1353 signal (SIGHUP, fatal_signal);
1354 #endif
1355 if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
1356 signal (SIGTERM, fatal_signal);
1357 #ifdef SIGPIPE
1358 if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
1359 signal (SIGPIPE, fatal_signal);
1360 #endif
1361 #ifdef SIGCHLD
1362 /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
1363 receive the signal. A different setting is inheritable */
1364 signal (SIGCHLD, SIG_DFL);
1365 #endif
1367 /* We may be called with all the arguments stored in some file and
1368 passed with @file. Expand them into argv before processing. */
1369 expandargv (&argc, &argv);
1371 run_gcc (argc, argv);
1373 return 0;