1 /* Wrapper to call lto. Used by collect2 and the linker plugin.
2 Copyright (C) 2009-2017 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 WHOPR is used instead, more than one file might be produced
36 ./ccXj2DTk.lto.ltrans.o
37 ./ccCJuXGv.lto.ltrans.o
42 #include "coretypes.h"
44 #include "diagnostic.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"
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 char *offload_objects_file_name;
72 static char *makefile;
74 const char tool_name[] = "lto-wrapper";
76 /* Delete tempfiles. Called from utils_cleanup. */
83 if (ltrans_output_file)
84 maybe_unlink (ltrans_output_file);
86 maybe_unlink (flto_out);
87 if (offload_objects_file_name)
88 maybe_unlink (offload_objects_file_name);
90 maybe_unlink (makefile);
91 for (i = 0; i < nr; ++i)
93 maybe_unlink (input_names[i]);
95 maybe_unlink (output_names[i]);
100 lto_wrapper_cleanup (void)
102 utils_cleanup (false);
105 /* Unlink a temporary LTRANS file unless requested otherwise. */
108 maybe_unlink (const char *file)
112 if (unlink_if_ordinary (file)
114 fatal_error (input_location, "deleting LTRANS file %s: %m", file);
117 fprintf (stderr, "[Leaving LTRANS %s]\n", file);
120 /* Template of LTRANS dumpbase suffix. */
121 #define DUMPBASE_SUFFIX ".ltrans18446744073709551615"
123 /* Create decoded options from the COLLECT_GCC and COLLECT_GCC_OPTIONS
124 environment according to LANG_MASK. */
127 get_options_from_collect_gcc_options (const char *collect_gcc,
128 const char *collect_gcc_options,
129 unsigned int lang_mask,
130 struct cl_decoded_option **decoded_options,
131 unsigned int *decoded_options_count)
133 struct obstack argv_obstack;
138 argv_storage = xstrdup (collect_gcc_options);
139 obstack_init (&argv_obstack);
140 obstack_ptr_grow (&argv_obstack, collect_gcc);
142 for (j = 0, k = 0; argv_storage[j] != '\0'; ++j)
144 if (argv_storage[j] == '\'')
146 obstack_ptr_grow (&argv_obstack, &argv_storage[k]);
150 if (argv_storage[j] == '\0')
151 fatal_error (input_location, "malformed COLLECT_GCC_OPTIONS");
152 else if (strncmp (&argv_storage[j], "'\\''", 4) == 0)
154 argv_storage[k++] = '\'';
157 else if (argv_storage[j] == '\'')
160 argv_storage[k++] = argv_storage[j++];
163 argv_storage[k++] = '\0';
167 obstack_ptr_grow (&argv_obstack, NULL);
168 argc = obstack_object_size (&argv_obstack) / sizeof (void *) - 1;
169 argv = XOBFINISH (&argv_obstack, const char **);
171 decode_cmdline_options_to_array (argc, (const char **)argv,
173 decoded_options, decoded_options_count);
174 obstack_free (&argv_obstack, NULL);
177 /* Append OPTION to the options array DECODED_OPTIONS with size
178 DECODED_OPTIONS_COUNT. */
181 append_option (struct cl_decoded_option **decoded_options,
182 unsigned int *decoded_options_count,
183 struct cl_decoded_option *option)
185 ++*decoded_options_count;
187 = (struct cl_decoded_option *)
188 xrealloc (*decoded_options,
189 (*decoded_options_count
190 * sizeof (struct cl_decoded_option)));
191 memcpy (&(*decoded_options)[*decoded_options_count - 1], option,
192 sizeof (struct cl_decoded_option));
195 /* Remove option number INDEX from DECODED_OPTIONS, update
196 DECODED_OPTIONS_COUNT. */
199 remove_option (struct cl_decoded_option **decoded_options,
200 int index, unsigned int *decoded_options_count)
202 --*decoded_options_count;
203 memmove (&(*decoded_options)[index + 1],
204 &(*decoded_options)[index],
205 sizeof (struct cl_decoded_option)
206 * (*decoded_options_count - index));
209 /* Try to merge and complain about options FDECODED_OPTIONS when applied
210 ontop of DECODED_OPTIONS. */
213 merge_and_complain (struct cl_decoded_option **decoded_options,
214 unsigned int *decoded_options_count,
215 struct cl_decoded_option *fdecoded_options,
216 unsigned int fdecoded_options_count)
219 struct cl_decoded_option *pic_option = NULL;
220 struct cl_decoded_option *pie_option = NULL;
222 /* ??? Merge options from files. Most cases can be
223 handled by either unioning or intersecting
224 (for example -fwrapv is a case for unioning,
225 -ffast-math is for intersection). Most complaints
226 about real conflicts between different options can
227 be deferred to the compiler proper. Options that
228 we can neither safely handle by intersection nor
229 unioning would need to be complained about here.
230 Ideally we'd have a flag in the opt files that
231 tells whether to union or intersect or reject.
232 In absence of that it's unclear what a good default is.
233 It's also difficult to get positional handling correct. */
235 /* The following does what the old LTO option code did,
236 union all target and a selected set of common options. */
237 for (i = 0; i < fdecoded_options_count; ++i)
239 struct cl_decoded_option *foption = &fdecoded_options[i];
240 switch (foption->opt_index)
242 case OPT_SPECIAL_unknown:
243 case OPT_SPECIAL_ignore:
244 case OPT_SPECIAL_program_name:
245 case OPT_SPECIAL_input_file:
249 if (!(cl_options[foption->opt_index].flags & CL_TARGET))
253 case OPT_fdiagnostics_show_caret:
254 case OPT_fdiagnostics_show_option:
255 case OPT_fdiagnostics_show_location_:
256 case OPT_fshow_column:
259 /* Do what the old LTO code did - collect exactly one option
260 setting per OPT code, we pick the first we encounter.
261 ??? This doesn't make too much sense, but when it doesn't
262 then we should complain. */
263 for (j = 0; j < *decoded_options_count; ++j)
264 if ((*decoded_options)[j].opt_index == foption->opt_index)
266 if (j == *decoded_options_count)
267 append_option (decoded_options, decoded_options_count, foption);
270 /* Figure out what PIC/PIE level wins and merge the results. */
273 pic_option = foption;
277 pie_option = foption;
283 case OPT_fcheck_pointer_bounds:
284 /* For selected options we can merge conservatively. */
285 for (j = 0; j < *decoded_options_count; ++j)
286 if ((*decoded_options)[j].opt_index == foption->opt_index)
288 if (j == *decoded_options_count)
289 append_option (decoded_options, decoded_options_count, foption);
290 /* -fopenmp > -fno-openmp,
291 -fopenacc > -fno-openacc,
292 -fcilkplus > -fno-cilkplus,
293 -fcheck_pointer_bounds > -fcheck_pointer_bounds */
294 else if (foption->value > (*decoded_options)[j].value)
295 (*decoded_options)[j] = *foption;
298 case OPT_fopenacc_dim_:
299 /* Append or check identical. */
300 for (j = 0; j < *decoded_options_count; ++j)
301 if ((*decoded_options)[j].opt_index == foption->opt_index)
303 if (j == *decoded_options_count)
304 append_option (decoded_options, decoded_options_count, foption);
305 else if (strcmp ((*decoded_options)[j].arg, foption->arg))
306 fatal_error (input_location,
307 "Option %s with different values",
308 foption->orig_option_with_args_text);
315 for (j = 0; j < *decoded_options_count; ++j)
316 if ((*decoded_options)[j].opt_index == OPT_O
317 || (*decoded_options)[j].opt_index == OPT_Ofast
318 || (*decoded_options)[j].opt_index == OPT_Og
319 || (*decoded_options)[j].opt_index == OPT_Os)
321 if (j == *decoded_options_count)
322 append_option (decoded_options, decoded_options_count, foption);
323 else if ((*decoded_options)[j].opt_index == foption->opt_index
324 && foption->opt_index != OPT_O)
325 /* Exact same options get merged. */
329 /* For mismatched option kinds preserve the optimization
330 level only, thus merge it as -On. This also handles
331 merging of same optimization level -On. */
333 switch (foption->opt_index)
336 if (foption->arg[0] == '\0')
337 level = MAX (level, 1);
339 level = MAX (level, atoi (foption->arg));
342 level = MAX (level, 3);
345 level = MAX (level, 1);
348 level = MAX (level, 2);
353 switch ((*decoded_options)[j].opt_index)
356 if ((*decoded_options)[j].arg[0] == '\0')
357 level = MAX (level, 1);
359 level = MAX (level, atoi ((*decoded_options)[j].arg));
362 level = MAX (level, 3);
365 level = MAX (level, 1);
368 level = MAX (level, 2);
373 (*decoded_options)[j].opt_index = OPT_O;
375 tem = xasprintf ("-O%d", level);
376 (*decoded_options)[j].arg = &tem[2];
377 (*decoded_options)[j].canonical_option[0] = tem;
378 (*decoded_options)[j].value = 1;
383 case OPT_foffload_abi_:
384 for (j = 0; j < *decoded_options_count; ++j)
385 if ((*decoded_options)[j].opt_index == foption->opt_index)
387 if (j == *decoded_options_count)
388 append_option (decoded_options, decoded_options_count, foption);
389 else if (foption->value != (*decoded_options)[j].value)
390 fatal_error (input_location,
391 "Option %s not used consistently in all LTO input"
392 " files", foption->orig_option_with_args_text);
397 append_option (decoded_options, decoded_options_count, foption);
402 /* Merge PIC options:
403 -fPIC + -fpic = -fpic
404 -fPIC + -fno-pic = -fno-pic
405 -fpic/-fPIC + nothin = nothing.
406 It is a common mistake to mix few -fPIC compiled objects into otherwise
407 non-PIC code. We do not want to build everything with PIC then.
409 It would be good to warn on mismatches, but it is bit hard to do as
410 we do not know what nothing translates to. */
412 for (unsigned int j = 0; j < *decoded_options_count;)
413 if ((*decoded_options)[j].opt_index == OPT_fPIC
414 || (*decoded_options)[j].opt_index == OPT_fpic)
417 || (pic_option->value > 0) != ((*decoded_options)[j].value > 0))
418 remove_option (decoded_options, j, decoded_options_count);
419 else if (pic_option->opt_index == OPT_fPIC
420 && (*decoded_options)[j].opt_index == OPT_fpic)
422 (*decoded_options)[j] = *pic_option;
428 else if ((*decoded_options)[j].opt_index == OPT_fPIE
429 || (*decoded_options)[j].opt_index == OPT_fpie)
432 || pie_option->value != (*decoded_options)[j].value)
433 remove_option (decoded_options, j, decoded_options_count);
434 else if (pie_option->opt_index == OPT_fPIE
435 && (*decoded_options)[j].opt_index == OPT_fpie)
437 (*decoded_options)[j] = *pie_option;
447 /* Auxiliary function that frees elements of PTR and PTR itself.
448 N is number of elements to be freed. If PTR is NULL, nothing is freed.
449 If an element is NULL, subsequent elements are not freed. */
452 free_array_of_ptrs (void **ptr, unsigned n)
456 for (unsigned i = 0; i < n; i++)
466 /* Parse STR, saving found tokens into PVALUES and return their number.
467 Tokens are assumed to be delimited by ':'. If APPEND is non-null,
468 append it to every token we find. */
471 parse_env_var (const char *str, char ***pvalues, const char *append)
473 const char *curval, *nextval;
477 curval = strchr (str, ':');
481 curval = strchr (curval + 1, ':');
484 values = (char**) xmalloc (num * sizeof (char*));
486 nextval = strchr (curval, ':');
488 nextval = strchr (curval, '\0');
490 int append_len = append ? strlen (append) : 0;
491 for (i = 0; i < num; i++)
493 int l = nextval - curval;
494 values[i] = (char*) xmalloc (l + 1 + append_len);
495 memcpy (values[i], curval, l);
498 strcat (values[i], append);
499 curval = nextval + 1;
500 nextval = strchr (curval, ':');
502 nextval = strchr (curval, '\0');
508 /* Append options OPTS from lto or offload_lto sections to ARGV_OBSTACK. */
511 append_compiler_options (obstack *argv_obstack, struct cl_decoded_option *opts,
514 /* Append compiler driver arguments as far as they were merged. */
515 for (unsigned int j = 1; j < count; ++j)
517 struct cl_decoded_option *option = &opts[j];
519 /* File options have been properly filtered by lto-opts.c. */
520 switch (option->opt_index)
522 /* Drop arguments that we want to take from the link line. */
525 case OPT_flto_partition_:
532 /* For now do what the original LTO option code was doing - pass
533 on any CL_TARGET flag and a few selected others. */
534 switch (option->opt_index)
536 case OPT_fdiagnostics_show_caret:
537 case OPT_fdiagnostics_show_option:
538 case OPT_fdiagnostics_show_location_:
539 case OPT_fshow_column:
548 case OPT_fopenacc_dim_:
550 case OPT_foffload_abi_:
555 case OPT_fcheck_pointer_bounds:
559 if (!(cl_options[option->opt_index].flags & CL_TARGET))
563 /* Pass the option on. */
564 for (unsigned int i = 0; i < option->canonical_option_num_elements; ++i)
565 obstack_ptr_grow (argv_obstack, option->canonical_option[i]);
569 /* Append diag options in OPTS with length COUNT to ARGV_OBSTACK. */
572 append_diag_options (obstack *argv_obstack, struct cl_decoded_option *opts,
575 /* Append compiler driver arguments as far as they were merged. */
576 for (unsigned int j = 1; j < count; ++j)
578 struct cl_decoded_option *option = &opts[j];
580 switch (option->opt_index)
582 case OPT_fdiagnostics_color_:
583 case OPT_fdiagnostics_show_caret:
584 case OPT_fdiagnostics_show_option:
585 case OPT_fdiagnostics_show_location_:
586 case OPT_fshow_column:
592 /* Pass the option on. */
593 for (unsigned int i = 0; i < option->canonical_option_num_elements; ++i)
594 obstack_ptr_grow (argv_obstack, option->canonical_option[i]);
599 /* Append linker options OPTS to ARGV_OBSTACK. */
602 append_linker_options (obstack *argv_obstack, struct cl_decoded_option *opts,
605 /* Append linker driver arguments. Compiler options from the linker
606 driver arguments will override / merge with those from the compiler. */
607 for (unsigned int j = 1; j < count; ++j)
609 struct cl_decoded_option *option = &opts[j];
611 /* Do not pass on frontend specific flags not suitable for lto. */
612 if (!(cl_options[option->opt_index].flags
613 & (CL_COMMON|CL_TARGET|CL_DRIVER|CL_LTO)))
616 switch (option->opt_index)
621 /* We've handled these LTO options, do not pass them on. */
627 /* Ignore -fno-XXX form of these options, as otherwise
628 corresponding builtins will not be enabled. */
629 if (option->value == 0)
637 /* Pass the option on. */
638 for (unsigned int i = 0; i < option->canonical_option_num_elements; ++i)
639 obstack_ptr_grow (argv_obstack, option->canonical_option[i]);
643 /* Extract options for TARGET offload compiler from OPTIONS and append
644 them to ARGV_OBSTACK. */
647 append_offload_options (obstack *argv_obstack, const char *target,
648 struct cl_decoded_option *options,
649 unsigned int options_count)
651 for (unsigned i = 0; i < options_count; i++)
653 const char *cur, *next, *opts;
656 struct cl_decoded_option *option = &options[i];
658 if (option->opt_index != OPT_foffload_)
661 /* If option argument starts with '-' then no target is specified. That
662 means offload options are specified for all targets, so we need to
664 if (option->arg[0] == '-')
668 opts = strchr (option->arg, '=');
669 /* If there are offload targets specified, but no actual options,
670 there is nothing to do here. */
678 next = strchr (cur, ',');
681 next = (next > opts) ? opts : next;
683 /* Are we looking for this offload target? */
684 if (strlen (target) == (size_t) (next - cur)
685 && strncmp (target, cur, next - cur) == 0)
688 /* Skip the comma or equal sign. */
698 argv = buildargv (opts);
699 for (argc = 0; argv[argc]; argc++)
700 obstack_ptr_grow (argv_obstack, argv[argc]);
704 /* Check whether NAME can be accessed in MODE. This is like access,
705 except that it never considers directories to be executable. */
708 access_check (const char *name, int mode)
714 if (stat (name, &st) < 0
715 || S_ISDIR (st.st_mode))
719 return access (name, mode);
722 /* Prepare a target image for offload TARGET, using mkoffload tool from
723 COMPILER_PATH. Return the name of the resultant object file. */
726 compile_offload_image (const char *target, const char *compiler_path,
727 unsigned in_argc, char *in_argv[],
728 struct cl_decoded_option *compiler_opts,
729 unsigned int compiler_opt_count,
730 struct cl_decoded_option *linker_opts,
731 unsigned int linker_opt_count)
733 char *filename = NULL;
736 = XALLOCAVEC (char, sizeof ("/accel//mkoffload") + strlen (target));
737 strcpy (suffix, "/accel/");
738 strcat (suffix, target);
739 strcat (suffix, "/mkoffload");
742 unsigned n_paths = parse_env_var (compiler_path, &paths, suffix);
744 const char *compiler = NULL;
745 for (unsigned i = 0; i < n_paths; i++)
746 if (access_check (paths[i], X_OK) == 0)
754 /* Generate temporary output file name. */
755 filename = make_temp_file (".target.o");
757 struct obstack argv_obstack;
758 obstack_init (&argv_obstack);
759 obstack_ptr_grow (&argv_obstack, compiler);
761 obstack_ptr_grow (&argv_obstack, "-save-temps");
763 obstack_ptr_grow (&argv_obstack, "-v");
764 obstack_ptr_grow (&argv_obstack, "-o");
765 obstack_ptr_grow (&argv_obstack, filename);
767 /* Append names of input object files. */
768 for (unsigned i = 0; i < in_argc; i++)
769 obstack_ptr_grow (&argv_obstack, in_argv[i]);
771 /* Append options from offload_lto sections. */
772 append_compiler_options (&argv_obstack, compiler_opts,
774 append_diag_options (&argv_obstack, linker_opts, linker_opt_count);
776 /* Append options specified by -foffload last. In case of conflicting
777 options we expect offload compiler to choose the latest. */
778 append_offload_options (&argv_obstack, target, compiler_opts,
780 append_offload_options (&argv_obstack, target, linker_opts,
783 obstack_ptr_grow (&argv_obstack, NULL);
784 argv = XOBFINISH (&argv_obstack, char **);
785 fork_execute (argv[0], argv, true);
786 obstack_free (&argv_obstack, NULL);
789 free_array_of_ptrs ((void **) paths, n_paths);
794 /* The main routine dealing with offloading.
795 The routine builds a target image for each offload target. IN_ARGC and
796 IN_ARGV specify options and input object files. As all of them could contain
797 target sections, we pass them all to target compilers. */
800 compile_images_for_offload_targets (unsigned in_argc, char *in_argv[],
801 struct cl_decoded_option *compiler_opts,
802 unsigned int compiler_opt_count,
803 struct cl_decoded_option *linker_opts,
804 unsigned int linker_opt_count)
807 const char *target_names = getenv (OFFLOAD_TARGET_NAMES_ENV);
810 unsigned num_targets = parse_env_var (target_names, &names, NULL);
812 int next_name_entry = 0;
813 const char *compiler_path = getenv ("COMPILER_PATH");
817 /* Prepare an image for each target and save the name of the resultant object
818 file to the OFFLOAD_NAMES array. It is terminated by a NULL entry. */
819 offload_names = XCNEWVEC (char *, num_targets + 1);
820 for (unsigned i = 0; i < num_targets; i++)
822 /* HSA does not use LTO-like streaming and a different compiler, skip
824 if (strcmp (names[i], "hsa") == 0)
827 offload_names[next_name_entry]
828 = compile_offload_image (names[i], compiler_path, in_argc, in_argv,
829 compiler_opts, compiler_opt_count,
830 linker_opts, linker_opt_count);
831 if (!offload_names[next_name_entry])
832 fatal_error (input_location,
833 "problem with building target image for %s\n", names[i]);
838 free_array_of_ptrs ((void **) names, num_targets);
841 /* Copy a file from SRC to DEST. */
844 copy_file (const char *dest, const char *src)
846 FILE *d = fopen (dest, "wb");
847 FILE *s = fopen (src, "rb");
851 size_t len = fread (buffer, 1, 512, s);
853 fatal_error (input_location, "reading input file");
856 fwrite (buffer, 1, len, d);
858 fatal_error (input_location, "writing output file");
865 /* Find the crtoffloadtable.o file in LIBRARY_PATH, make copy and pass name of
866 the copy to the linker. */
869 find_crtoffloadtable (void)
872 const char *library_path = getenv ("LIBRARY_PATH");
875 unsigned n_paths = parse_env_var (library_path, &paths, "/crtoffloadtable.o");
878 for (i = 0; i < n_paths; i++)
879 if (access_check (paths[i], R_OK) == 0)
881 /* The linker will delete the filename we give it, so make a copy. */
882 char *crtoffloadtable = make_temp_file (".crtoffloadtable.o");
883 copy_file (crtoffloadtable, paths[i]);
884 printf ("%s\n", crtoffloadtable);
885 XDELETEVEC (crtoffloadtable);
889 fatal_error (input_location,
890 "installation error, can't find crtoffloadtable.o");
892 free_array_of_ptrs ((void **) paths, n_paths);
895 /* A subroutine of run_gcc. Examine the open file FD for lto sections with
896 name prefix PREFIX, at FILE_OFFSET, and store any options we find in OPTS
897 and OPT_COUNT. Return true if we found a matchingn section, false
898 otherwise. COLLECT_GCC holds the value of the environment variable with
902 find_and_merge_options (int fd, off_t file_offset, const char *prefix,
903 struct cl_decoded_option **opts,
904 unsigned int *opt_count, const char *collect_gcc)
906 off_t offset, length;
911 struct cl_decoded_option *fdecoded_options = *opts;
912 unsigned int fdecoded_options_count = *opt_count;
914 simple_object_read *sobj;
915 sobj = simple_object_start_read (fd, file_offset, "__GNU_LTO",
920 char *secname = XALLOCAVEC (char, strlen (prefix) + sizeof (".opts"));
921 strcpy (secname, prefix);
922 strcat (secname, ".opts");
923 if (!simple_object_find_section (sobj, secname, &offset, &length,
926 simple_object_release_read (sobj);
930 lseek (fd, file_offset + offset, SEEK_SET);
931 data = (char *)xmalloc (length);
932 read (fd, data, length);
936 struct cl_decoded_option *f2decoded_options;
937 unsigned int f2decoded_options_count;
938 get_options_from_collect_gcc_options (collect_gcc,
941 &f2decoded_options_count);
942 if (!fdecoded_options)
944 fdecoded_options = f2decoded_options;
945 fdecoded_options_count = f2decoded_options_count;
948 merge_and_complain (&fdecoded_options,
949 &fdecoded_options_count,
950 f2decoded_options, f2decoded_options_count);
952 fopts += strlen (fopts) + 1;
954 while (fopts - data < length);
957 simple_object_release_read (sobj);
958 *opts = fdecoded_options;
959 *opt_count = fdecoded_options_count;
963 /* Execute gcc. ARGC is the number of arguments. ARGV contains the arguments. */
966 run_gcc (unsigned argc, char *argv[])
969 const char **new_argv;
970 const char **argv_ptr;
971 char *list_option_full = NULL;
972 const char *linker_output = NULL;
973 const char *collect_gcc, *collect_gcc_options;
976 bool no_partition = false;
977 struct cl_decoded_option *fdecoded_options = NULL;
978 struct cl_decoded_option *offload_fdecoded_options = NULL;
979 unsigned int fdecoded_options_count = 0;
980 unsigned int offload_fdecoded_options_count = 0;
981 struct cl_decoded_option *decoded_options;
982 unsigned int decoded_options_count;
983 struct obstack argv_obstack;
985 bool have_lto = false;
986 bool have_offload = false;
987 unsigned lto_argc = 0;
990 /* Get the driver and options. */
991 collect_gcc = getenv ("COLLECT_GCC");
993 fatal_error (input_location,
994 "environment variable COLLECT_GCC must be set");
995 collect_gcc_options = getenv ("COLLECT_GCC_OPTIONS");
996 if (!collect_gcc_options)
997 fatal_error (input_location,
998 "environment variable COLLECT_GCC_OPTIONS must be set");
999 get_options_from_collect_gcc_options (collect_gcc, collect_gcc_options,
1002 &decoded_options_count);
1004 /* Allocate array for input object files with LTO IL,
1005 and for possible preceding arguments. */
1006 lto_argv = XNEWVEC (char *, argc);
1008 /* Look at saved options in the IL files. */
1009 for (i = 1; i < argc; ++i)
1013 off_t file_offset = 0;
1016 char *filename = argv[i];
1018 if (strncmp (argv[i], "-foffload-objects=",
1019 sizeof ("-foffload-objects=") - 1) == 0)
1021 have_offload = true;
1022 offload_objects_file_name
1023 = argv[i] + sizeof ("-foffload-objects=") - 1;
1027 if ((p = strrchr (argv[i], '@'))
1029 && sscanf (p, "@%li%n", &loffset, &consumed) >= 1
1030 && strlen (p) == (unsigned int) consumed)
1032 filename = XNEWVEC (char, p - argv[i] + 1);
1033 memcpy (filename, argv[i], p - argv[i]);
1034 filename[p - argv[i]] = '\0';
1035 file_offset = (off_t) loffset;
1037 fd = open (filename, O_RDONLY | O_BINARY);
1040 lto_argv[lto_argc++] = argv[i];
1044 if (find_and_merge_options (fd, file_offset, LTO_SECTION_NAME_PREFIX,
1045 &fdecoded_options, &fdecoded_options_count,
1049 lto_argv[lto_argc++] = argv[i];
1054 /* Initalize the common arguments for the driver. */
1055 obstack_init (&argv_obstack);
1056 obstack_ptr_grow (&argv_obstack, collect_gcc);
1057 obstack_ptr_grow (&argv_obstack, "-xlto");
1058 obstack_ptr_grow (&argv_obstack, "-c");
1060 append_compiler_options (&argv_obstack, fdecoded_options,
1061 fdecoded_options_count);
1062 append_linker_options (&argv_obstack, decoded_options, decoded_options_count);
1064 /* Scan linker driver arguments for things that are of relevance to us. */
1065 for (j = 1; j < decoded_options_count; ++j)
1067 struct cl_decoded_option *option = &decoded_options[j];
1068 switch (option->opt_index)
1071 linker_output = option->arg;
1074 case OPT_save_temps:
1082 case OPT_flto_partition_:
1083 if (strcmp (option->arg, "none") == 0)
1084 no_partition = true;
1088 if (strcmp (option->arg, "jobserver") == 0)
1095 parallel = atoi (option->arg);
1102 lto_mode = LTO_MODE_WHOPR;
1112 lto_mode = LTO_MODE_LTO;
1119 char *output_dir, *base, *name;
1120 bool bit_bucket = strcmp (linker_output, HOST_BIT_BUCKET) == 0;
1122 output_dir = xstrdup (linker_output);
1124 for (name = base; *name; name++)
1125 if (IS_DIR_SEPARATOR (*name))
1129 linker_output = &linker_output[base - output_dir];
1130 if (*output_dir == '\0')
1132 static char current_dir[] = { '.', DIR_SEPARATOR, '\0' };
1133 output_dir = current_dir;
1137 obstack_ptr_grow (&argv_obstack, "-dumpdir");
1138 obstack_ptr_grow (&argv_obstack, output_dir);
1141 obstack_ptr_grow (&argv_obstack, "-dumpbase");
1144 /* Remember at which point we can scrub args to re-use the commons. */
1145 new_head_argc = obstack_object_size (&argv_obstack) / sizeof (void *);
1149 unsigned i, num_offload_files;
1150 char **offload_argv;
1153 f = fopen (offload_objects_file_name, "r");
1155 fatal_error (input_location, "cannot open %s: %m",
1156 offload_objects_file_name);
1157 if (fscanf (f, "%u ", &num_offload_files) != 1)
1158 fatal_error (input_location, "cannot read %s: %m",
1159 offload_objects_file_name);
1160 offload_argv = XCNEWVEC (char *, num_offload_files);
1162 /* Read names of object files with offload. */
1163 for (i = 0; i < num_offload_files; i++)
1165 const unsigned piece = 32;
1166 char *buf, *filename = XNEWVEC (char, piece);
1171 if (!fgets (buf, piece, f))
1173 len = strlen (filename);
1174 if (filename[len - 1] != '\n')
1176 filename = XRESIZEVEC (char, filename, len + piece);
1177 buf = filename + len;
1180 filename[len - 1] = '\0';
1181 offload_argv[i] = filename;
1184 if (offload_argv[num_offload_files - 1] == NULL)
1185 fatal_error (input_location, "invalid format of %s",
1186 offload_objects_file_name);
1187 maybe_unlink (offload_objects_file_name);
1188 offload_objects_file_name = NULL;
1190 /* Look at saved offload options in files. */
1191 for (i = 0; i < num_offload_files; i++)
1196 off_t file_offset = 0;
1197 char *filename = offload_argv[i];
1199 if ((p = strrchr (offload_argv[i], '@'))
1200 && p != offload_argv[i]
1201 && sscanf (p, "@%li%n", &loffset, &consumed) >= 1
1202 && strlen (p) == (unsigned int) consumed)
1204 filename = XNEWVEC (char, p - offload_argv[i] + 1);
1205 memcpy (filename, offload_argv[i], p - offload_argv[i]);
1206 filename[p - offload_argv[i]] = '\0';
1207 file_offset = (off_t) loffset;
1209 fd = open (filename, O_RDONLY | O_BINARY);
1211 fatal_error (input_location, "cannot open %s: %m", filename);
1212 if (!find_and_merge_options (fd, file_offset,
1213 OFFLOAD_SECTION_NAME_PREFIX,
1214 &offload_fdecoded_options,
1215 &offload_fdecoded_options_count,
1217 fatal_error (input_location, "cannot read %s: %m", filename);
1219 if (filename != offload_argv[i])
1220 XDELETEVEC (filename);
1223 compile_images_for_offload_targets (num_offload_files, offload_argv,
1224 offload_fdecoded_options,
1225 offload_fdecoded_options_count,
1227 decoded_options_count);
1229 free_array_of_ptrs ((void **) offload_argv, num_offload_files);
1233 find_crtoffloadtable ();
1234 for (i = 0; offload_names[i]; i++)
1235 printf ("%s\n", offload_names[i]);
1236 free_array_of_ptrs ((void **) offload_names, i);
1240 /* If object files contain offload sections, but do not contain LTO sections,
1241 then there is no need to perform a link-time recompilation, i.e.
1242 lto-wrapper is used only for a compilation of offload images. */
1243 if (have_offload && !have_lto)
1246 if (lto_mode == LTO_MODE_LTO)
1248 flto_out = make_temp_file (".lto.o");
1250 obstack_ptr_grow (&argv_obstack, linker_output);
1251 obstack_ptr_grow (&argv_obstack, "-o");
1252 obstack_ptr_grow (&argv_obstack, flto_out);
1256 const char *list_option = "-fltrans-output-list=";
1257 size_t list_option_len = strlen (list_option);
1262 char *dumpbase = (char *) xmalloc (strlen (linker_output)
1263 + sizeof (".wpa") + 1);
1264 strcpy (dumpbase, linker_output);
1265 strcat (dumpbase, ".wpa");
1266 obstack_ptr_grow (&argv_obstack, dumpbase);
1269 if (linker_output && save_temps)
1271 ltrans_output_file = (char *) xmalloc (strlen (linker_output)
1272 + sizeof (".ltrans.out") + 1);
1273 strcpy (ltrans_output_file, linker_output);
1274 strcat (ltrans_output_file, ".ltrans.out");
1277 ltrans_output_file = make_temp_file (".ltrans.out");
1278 list_option_full = (char *) xmalloc (sizeof (char) *
1279 (strlen (ltrans_output_file) + list_option_len + 1));
1280 tmp = list_option_full;
1282 obstack_ptr_grow (&argv_obstack, tmp);
1283 strcpy (tmp, list_option);
1284 tmp += list_option_len;
1285 strcpy (tmp, ltrans_output_file);
1288 obstack_ptr_grow (&argv_obstack, xstrdup ("-fwpa=jobserver"));
1289 else if (parallel > 1)
1292 sprintf (buf, "-fwpa=%i", parallel);
1293 obstack_ptr_grow (&argv_obstack, xstrdup (buf));
1296 obstack_ptr_grow (&argv_obstack, "-fwpa");
1299 /* Append the input objects and possible preceding arguments. */
1300 for (i = 0; i < lto_argc; ++i)
1301 obstack_ptr_grow (&argv_obstack, lto_argv[i]);
1302 obstack_ptr_grow (&argv_obstack, NULL);
1304 new_argv = XOBFINISH (&argv_obstack, const char **);
1305 argv_ptr = &new_argv[new_head_argc];
1306 fork_execute (new_argv[0], CONST_CAST (char **, new_argv), true);
1308 if (lto_mode == LTO_MODE_LTO)
1310 printf ("%s\n", flto_out);
1316 FILE *stream = fopen (ltrans_output_file, "r");
1317 FILE *mstream = NULL;
1318 struct obstack env_obstack;
1321 fatal_error (input_location, "fopen: %s: %m", ltrans_output_file);
1323 /* Parse the list of LTRANS inputs from the WPA stage. */
1324 obstack_init (&env_obstack);
1328 const unsigned piece = 32;
1329 char *output_name = NULL;
1330 char *buf, *input_name = (char *)xmalloc (piece);
1335 if (!fgets (buf, piece, stream))
1337 len = strlen (input_name);
1338 if (input_name[len - 1] != '\n')
1340 input_name = (char *)xrealloc (input_name, len + piece);
1341 buf = input_name + len;
1344 input_name[len - 1] = '\0';
1346 if (input_name[0] == '*')
1347 output_name = &input_name[1];
1350 input_names = (char **)xrealloc (input_names, nr * sizeof (char *));
1351 output_names = (char **)xrealloc (output_names, nr * sizeof (char *));
1352 input_names[nr-1] = input_name;
1353 output_names[nr-1] = output_name;
1356 maybe_unlink (ltrans_output_file);
1357 ltrans_output_file = NULL;
1361 makefile = make_temp_file (".mk");
1362 mstream = fopen (makefile, "w");
1365 /* Execute the LTRANS stage for each input file (or prepare a
1366 makefile to invoke this in parallel). */
1367 for (i = 0; i < nr; ++i)
1370 char *input_name = input_names[i];
1371 /* If it's a pass-through file do nothing. */
1372 if (output_names[i])
1375 /* Replace the .o suffix with a .ltrans.o suffix and write
1376 the resulting name to the LTRANS output list. */
1377 obstack_grow (&env_obstack, input_name, strlen (input_name) - 2);
1378 obstack_grow (&env_obstack, ".ltrans.o", sizeof (".ltrans.o"));
1379 output_name = XOBFINISH (&env_obstack, char *);
1381 /* Adjust the dumpbase if the linker output file was seen. */
1385 = (char *) xmalloc (strlen (linker_output)
1386 + sizeof (DUMPBASE_SUFFIX) + 1);
1388 strlen (linker_output) + sizeof (DUMPBASE_SUFFIX),
1389 "%s.ltrans%u", linker_output, i);
1390 argv_ptr[0] = dumpbase;
1393 argv_ptr[1] = "-fltrans";
1395 argv_ptr[3] = output_name;
1396 argv_ptr[4] = input_name;
1400 fprintf (mstream, "%s:\n\t@%s ", output_name, new_argv[0]);
1401 for (j = 1; new_argv[j] != NULL; ++j)
1402 fprintf (mstream, " '%s'", new_argv[j]);
1403 fprintf (mstream, "\n");
1404 /* If we are not preserving the ltrans input files then
1405 truncate them as soon as we have processed it. This
1406 reduces temporary disk-space usage. */
1408 fprintf (mstream, "\t@-touch -r %s %s.tem > /dev/null 2>&1 "
1409 "&& mv %s.tem %s\n",
1410 input_name, input_name, input_name, input_name);
1414 fork_execute (new_argv[0], CONST_CAST (char **, new_argv),
1416 maybe_unlink (input_name);
1419 output_names[i] = output_name;
1423 struct pex_obj *pex;
1426 fprintf (mstream, "all:");
1427 for (i = 0; i < nr; ++i)
1428 fprintf (mstream, " \\\n\t%s", output_names[i]);
1429 fprintf (mstream, "\n");
1433 /* Avoid passing --jobserver-fd= and similar flags
1434 unless jobserver mode is explicitly enabled. */
1435 putenv (xstrdup ("MAKEFLAGS="));
1436 putenv (xstrdup ("MFLAGS="));
1438 new_argv[0] = getenv ("MAKE");
1440 new_argv[0] = "make";
1442 new_argv[2] = makefile;
1446 snprintf (jobs, 31, "-j%d", parallel);
1447 new_argv[i++] = jobs;
1449 new_argv[i++] = "all";
1450 new_argv[i++] = NULL;
1451 pex = collect_execute (new_argv[0], CONST_CAST (char **, new_argv),
1452 NULL, NULL, PEX_SEARCH, false);
1453 do_wait (new_argv[0], pex);
1454 maybe_unlink (makefile);
1456 for (i = 0; i < nr; ++i)
1457 maybe_unlink (input_names[i]);
1459 for (i = 0; i < nr; ++i)
1461 fputs (output_names[i], stdout);
1462 putc ('\n', stdout);
1463 free (input_names[i]);
1466 free (output_names);
1468 free (list_option_full);
1469 obstack_free (&env_obstack, NULL);
1474 obstack_free (&argv_obstack, NULL);
1481 main (int argc, char *argv[])
1485 init_opts_obstack ();
1487 p = argv[0] + strlen (argv[0]);
1488 while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
1492 xmalloc_set_program_name (progname);
1494 gcc_init_libintl ();
1496 diagnostic_initialize (global_dc, 0);
1498 if (atexit (lto_wrapper_cleanup) != 0)
1499 fatal_error (input_location, "atexit failed");
1501 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
1502 signal (SIGINT, fatal_signal);
1504 if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
1505 signal (SIGHUP, fatal_signal);
1507 if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
1508 signal (SIGTERM, fatal_signal);
1510 if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
1511 signal (SIGPIPE, fatal_signal);
1514 /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
1515 receive the signal. A different setting is inheritable */
1516 signal (SIGCHLD, SIG_DFL);
1519 /* We may be called with all the arguments stored in some file and
1520 passed with @file. Expand them into argv before processing. */
1521 expandargv (&argc, &argv);
1523 run_gcc (argc, argv);