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
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 const char *offloadbegin
, *offloadend
;
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
);
88 maybe_unlink (makefile
);
89 for (i
= 0; i
< nr
; ++i
)
91 maybe_unlink (input_names
[i
]);
93 maybe_unlink (output_names
[i
]);
98 lto_wrapper_cleanup (void)
100 utils_cleanup (false);
103 /* Unlink a temporary LTRANS file unless requested otherwise. */
106 maybe_unlink (const char *file
)
110 if (unlink_if_ordinary (file
)
112 fatal_error (input_location
, "deleting LTRANS file %s: %m", file
);
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. */
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
;
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
]);
148 if (argv_storage
[j
] == '\0')
149 fatal_error (input_location
, "malformed COLLECT_GCC_OPTIONS");
150 else if (strncmp (&argv_storage
[j
], "'\\''", 4) == 0)
152 argv_storage
[k
++] = '\'';
155 else if (argv_storage
[j
] == '\'')
158 argv_storage
[k
++] = argv_storage
[j
++];
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
,
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. */
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
;
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. */
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
)
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
:
231 if (!(cl_options
[foption
->opt_index
].flags
& CL_TARGET
))
235 case OPT_fdiagnostics_show_caret
:
236 case OPT_fdiagnostics_show_option
:
237 case OPT_fdiagnostics_show_location_
:
238 case OPT_fshow_column
:
244 case OPT_fexceptions
:
245 case OPT_fnon_call_exceptions
:
247 /* Do what the old LTO code did - collect exactly one option
248 setting per OPT code, we pick the first we encounter.
249 ??? This doesn't make too much sense, but when it doesn't
250 then we should complain. */
251 for (j
= 0; j
< *decoded_options_count
; ++j
)
252 if ((*decoded_options
)[j
].opt_index
== foption
->opt_index
)
254 if (j
== *decoded_options_count
)
255 append_option (decoded_options
, decoded_options_count
, foption
);
259 case OPT_fstrict_overflow
:
260 case OPT_ffp_contract_
:
261 /* For selected options we can merge conservatively. */
262 for (j
= 0; j
< *decoded_options_count
; ++j
)
263 if ((*decoded_options
)[j
].opt_index
== foption
->opt_index
)
265 if (j
== *decoded_options_count
)
266 append_option (decoded_options
, decoded_options_count
, foption
);
267 /* FP_CONTRACT_OFF < FP_CONTRACT_ON < FP_CONTRACT_FAST,
268 -fno-trapv < -ftrapv,
269 -fno-strict-overflow < -fstrict-overflow */
270 else if (foption
->value
< (*decoded_options
)[j
].value
)
271 (*decoded_options
)[j
] = *foption
;
274 case OPT_fmath_errno
:
275 case OPT_fsigned_zeros
:
276 case OPT_ftrapping_math
:
280 case OPT_fcheck_pointer_bounds
:
281 /* For selected options we can merge conservatively. */
282 for (j
= 0; j
< *decoded_options_count
; ++j
)
283 if ((*decoded_options
)[j
].opt_index
== foption
->opt_index
)
285 if (j
== *decoded_options_count
)
286 append_option (decoded_options
, decoded_options_count
, foption
);
287 /* -fmath-errno > -fno-math-errno,
288 -fsigned-zeros > -fno-signed-zeros,
289 -ftrapping-math -> -fno-trapping-math,
290 -fwrapv > -fno-wrapv. */
291 else if (foption
->value
> (*decoded_options
)[j
].value
)
292 (*decoded_options
)[j
] = *foption
;
295 case OPT_freg_struct_return
:
296 case OPT_fpcc_struct_return
:
297 case OPT_fshort_double
:
298 for (j
= 0; j
< *decoded_options_count
; ++j
)
299 if ((*decoded_options
)[j
].opt_index
== foption
->opt_index
)
301 if (j
== *decoded_options_count
)
302 fatal_error (input_location
,
303 "Option %s not used consistently in all LTO input"
304 " files", foption
->orig_option_with_args_text
);
307 case OPT_foffload_abi_
:
308 for (j
= 0; j
< *decoded_options_count
; ++j
)
309 if ((*decoded_options
)[j
].opt_index
== foption
->opt_index
)
311 if (j
== *decoded_options_count
)
312 append_option (decoded_options
, decoded_options_count
, foption
);
313 else if (foption
->value
!= (*decoded_options
)[j
].value
)
314 fatal_error (input_location
,
315 "Option %s not used consistently in all LTO input"
316 " files", foption
->orig_option_with_args_text
);
323 for (j
= 0; j
< *decoded_options_count
; ++j
)
324 if ((*decoded_options
)[j
].opt_index
== OPT_O
325 || (*decoded_options
)[j
].opt_index
== OPT_Ofast
326 || (*decoded_options
)[j
].opt_index
== OPT_Og
327 || (*decoded_options
)[j
].opt_index
== OPT_Os
)
329 if (j
== *decoded_options_count
)
330 append_option (decoded_options
, decoded_options_count
, foption
);
331 else if ((*decoded_options
)[j
].opt_index
== foption
->opt_index
332 && foption
->opt_index
!= OPT_O
)
333 /* Exact same options get merged. */
337 /* For mismatched option kinds preserve the optimization
338 level only, thus merge it as -On. This also handles
339 merging of same optimization level -On. */
341 switch (foption
->opt_index
)
344 if (foption
->arg
[0] == '\0')
345 level
= MAX (level
, 1);
347 level
= MAX (level
, atoi (foption
->arg
));
350 level
= MAX (level
, 3);
353 level
= MAX (level
, 1);
356 level
= MAX (level
, 2);
361 switch ((*decoded_options
)[j
].opt_index
)
364 if ((*decoded_options
)[j
].arg
[0] == '\0')
365 level
= MAX (level
, 1);
367 level
= MAX (level
, atoi ((*decoded_options
)[j
].arg
));
370 level
= MAX (level
, 3);
373 level
= MAX (level
, 1);
376 level
= MAX (level
, 2);
381 (*decoded_options
)[j
].opt_index
= OPT_O
;
383 tem
= xasprintf ("-O%d", level
);
384 (*decoded_options
)[j
].arg
= &tem
[2];
385 (*decoded_options
)[j
].canonical_option
[0] = tem
;
386 (*decoded_options
)[j
].value
= 1;
391 append_option (decoded_options
, decoded_options_count
, foption
);
397 /* Auxiliary function that frees elements of PTR and PTR itself.
398 N is number of elements to be freed. If PTR is NULL, nothing is freed.
399 If an element is NULL, subsequent elements are not freed. */
402 free_array_of_ptrs (void **ptr
, unsigned n
)
406 for (unsigned i
= 0; i
< n
; i
++)
416 /* Parse STR, saving found tokens into PVALUES and return their number.
417 Tokens are assumed to be delimited by ':'. If APPEND is non-null,
418 append it to every token we find. */
421 parse_env_var (const char *str
, char ***pvalues
, const char *append
)
423 const char *curval
, *nextval
;
427 curval
= strchr (str
, ':');
431 curval
= strchr (curval
+ 1, ':');
434 values
= (char**) xmalloc (num
* sizeof (char*));
436 nextval
= strchr (curval
, ':');
438 nextval
= strchr (curval
, '\0');
440 int append_len
= append
? strlen (append
) : 0;
441 for (i
= 0; i
< num
; i
++)
443 int l
= nextval
- curval
;
444 values
[i
] = (char*) xmalloc (l
+ 1 + append_len
);
445 memcpy (values
[i
], curval
, l
);
448 strcat (values
[i
], append
);
449 curval
= nextval
+ 1;
450 nextval
= strchr (curval
, ':');
452 nextval
= strchr (curval
, '\0');
458 /* Append options OPTS from lto or offload_lto sections to ARGV_OBSTACK. */
461 append_compiler_options (obstack
*argv_obstack
, struct cl_decoded_option
*opts
,
464 /* Append compiler driver arguments as far as they were merged. */
465 for (unsigned int j
= 1; j
< count
; ++j
)
467 struct cl_decoded_option
*option
= &opts
[j
];
469 /* File options have been properly filtered by lto-opts.c. */
470 switch (option
->opt_index
)
472 /* Drop arguments that we want to take from the link line. */
475 case OPT_flto_partition_
:
482 /* For now do what the original LTO option code was doing - pass
483 on any CL_TARGET flag and a few selected others. */
484 switch (option
->opt_index
)
486 case OPT_fdiagnostics_show_caret
:
487 case OPT_fdiagnostics_show_option
:
488 case OPT_fdiagnostics_show_location_
:
489 case OPT_fshow_column
:
495 case OPT_fexceptions
:
496 case OPT_fnon_call_exceptions
:
498 case OPT_freg_struct_return
:
499 case OPT_fpcc_struct_return
:
500 case OPT_fshort_double
:
501 case OPT_ffp_contract_
:
502 case OPT_fmath_errno
:
503 case OPT_fsigned_zeros
:
504 case OPT_ftrapping_math
:
509 case OPT_fstrict_overflow
:
510 case OPT_foffload_abi_
:
515 case OPT_fcheck_pointer_bounds
:
519 if (!(cl_options
[option
->opt_index
].flags
& CL_TARGET
))
523 /* Pass the option on. */
524 for (unsigned int i
= 0; i
< option
->canonical_option_num_elements
; ++i
)
525 obstack_ptr_grow (argv_obstack
, option
->canonical_option
[i
]);
529 /* Append linker options OPTS to ARGV_OBSTACK. */
532 append_linker_options (obstack
*argv_obstack
, struct cl_decoded_option
*opts
,
535 /* Append linker driver arguments. Compiler options from the linker
536 driver arguments will override / merge with those from the compiler. */
537 for (unsigned int j
= 1; j
< count
; ++j
)
539 struct cl_decoded_option
*option
= &opts
[j
];
541 /* Do not pass on frontend specific flags not suitable for lto. */
542 if (!(cl_options
[option
->opt_index
].flags
543 & (CL_COMMON
|CL_TARGET
|CL_DRIVER
|CL_LTO
)))
546 switch (option
->opt_index
)
551 /* We've handled these LTO options, do not pass them on. */
554 case OPT_freg_struct_return
:
555 case OPT_fpcc_struct_return
:
556 case OPT_fshort_double
:
557 /* Ignore these, they are determined by the input files.
558 ??? We fail to diagnose a possible mismatch here. */
565 /* Pass the option on. */
566 for (unsigned int i
= 0; i
< option
->canonical_option_num_elements
; ++i
)
567 obstack_ptr_grow (argv_obstack
, option
->canonical_option
[i
]);
571 /* Extract options for TARGET offload compiler from OPTIONS and append
572 them to ARGV_OBSTACK. */
575 append_offload_options (obstack
*argv_obstack
, const char *target
,
576 struct cl_decoded_option
*options
,
577 unsigned int options_count
)
579 for (unsigned i
= 0; i
< options_count
; i
++)
581 const char *cur
, *next
, *opts
;
584 struct cl_decoded_option
*option
= &options
[i
];
586 if (option
->opt_index
!= OPT_foffload_
)
589 /* If option argument starts with '-' then no target is specified. That
590 means offload options are specified for all targets, so we need to
592 if (option
->arg
[0] == '-')
596 opts
= strchr (option
->arg
, '=');
597 /* If there are offload targets specified, but no actual options,
598 there is nothing to do here. */
606 next
= strchr (cur
, ',');
609 next
= (next
> opts
) ? opts
: next
;
611 /* Are we looking for this offload target? */
612 if (strlen (target
) == (size_t) (next
- cur
)
613 && strncmp (target
, cur
, next
- cur
) == 0)
616 /* Skip the comma or equal sign. */
626 argv
= buildargv (opts
);
627 for (argc
= 0; argv
[argc
]; argc
++)
628 obstack_ptr_grow (argv_obstack
, argv
[argc
]);
632 /* Check whether NAME can be accessed in MODE. This is like access,
633 except that it never considers directories to be executable. */
636 access_check (const char *name
, int mode
)
642 if (stat (name
, &st
) < 0
643 || S_ISDIR (st
.st_mode
))
647 return access (name
, mode
);
650 /* Prepare a target image for offload TARGET, using mkoffload tool from
651 COMPILER_PATH. Return the name of the resultant object file. */
654 compile_offload_image (const char *target
, const char *compiler_path
,
655 unsigned in_argc
, char *in_argv
[],
656 struct cl_decoded_option
*compiler_opts
,
657 unsigned int compiler_opt_count
,
658 struct cl_decoded_option
*linker_opts
,
659 unsigned int linker_opt_count
)
661 char *filename
= NULL
;
664 = XALLOCAVEC (char, sizeof ("/accel//mkoffload") + strlen (target
));
665 strcpy (suffix
, "/accel/");
666 strcat (suffix
, target
);
667 strcat (suffix
, "/mkoffload");
670 unsigned n_paths
= parse_env_var (compiler_path
, &paths
, suffix
);
672 const char *compiler
= NULL
;
673 for (unsigned i
= 0; i
< n_paths
; i
++)
674 if (access_check (paths
[i
], X_OK
) == 0)
682 /* Generate temporary output file name. */
683 filename
= make_temp_file (".target.o");
685 struct obstack argv_obstack
;
686 obstack_init (&argv_obstack
);
687 obstack_ptr_grow (&argv_obstack
, compiler
);
689 obstack_ptr_grow (&argv_obstack
, "-save-temps");
691 obstack_ptr_grow (&argv_obstack
, "-v");
692 obstack_ptr_grow (&argv_obstack
, "-o");
693 obstack_ptr_grow (&argv_obstack
, filename
);
695 /* Append names of input object files. */
696 for (unsigned i
= 0; i
< in_argc
; i
++)
697 obstack_ptr_grow (&argv_obstack
, in_argv
[i
]);
699 /* Append options from offload_lto sections. */
700 append_compiler_options (&argv_obstack
, compiler_opts
,
703 /* Append options specified by -foffload last. In case of conflicting
704 options we expect offload compiler to choose the latest. */
705 append_offload_options (&argv_obstack
, target
, compiler_opts
,
707 append_offload_options (&argv_obstack
, target
, linker_opts
,
710 obstack_ptr_grow (&argv_obstack
, NULL
);
711 argv
= XOBFINISH (&argv_obstack
, char **);
712 fork_execute (argv
[0], argv
, true);
713 obstack_free (&argv_obstack
, NULL
);
716 free_array_of_ptrs ((void **) paths
, n_paths
);
721 /* The main routine dealing with offloading.
722 The routine builds a target image for each offload target. IN_ARGC and
723 IN_ARGV specify options and input object files. As all of them could contain
724 target sections, we pass them all to target compilers. */
727 compile_images_for_offload_targets (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
)
734 const char *target_names
= getenv (OFFLOAD_TARGET_NAMES_ENV
);
737 unsigned num_targets
= parse_env_var (target_names
, &names
, NULL
);
739 const char *compiler_path
= getenv ("COMPILER_PATH");
743 /* Prepare an image for each target and save the name of the resultant object
744 file to the OFFLOAD_NAMES array. It is terminated by a NULL entry. */
745 offload_names
= XCNEWVEC (char *, num_targets
+ 1);
746 for (unsigned i
= 0; i
< num_targets
; i
++)
749 = compile_offload_image (names
[i
], compiler_path
, in_argc
, in_argv
,
750 compiler_opts
, compiler_opt_count
,
751 linker_opts
, linker_opt_count
);
752 if (!offload_names
[i
])
753 fatal_error (input_location
,
754 "problem with building target image for %s\n", names
[i
]);
758 free_array_of_ptrs ((void **) names
, num_targets
);
761 /* Copy a file from SRC to DEST. */
764 copy_file (const char *dest
, const char *src
)
766 FILE *d
= fopen (dest
, "wb");
767 FILE *s
= fopen (src
, "rb");
771 size_t len
= fread (buffer
, 1, 512, s
);
773 fatal_error (input_location
, "reading input file");
776 fwrite (buffer
, 1, len
, d
);
778 fatal_error (input_location
, "writing output file");
783 /* Find the crtoffloadbegin.o and crtoffloadend.o files in LIBRARY_PATH, make
784 copies and store the names of the copies in offloadbegin and offloadend. */
787 find_offloadbeginend (void)
790 const char *library_path
= getenv ("LIBRARY_PATH");
793 unsigned n_paths
= parse_env_var (library_path
, &paths
, "/crtoffloadbegin.o");
796 for (i
= 0; i
< n_paths
; i
++)
797 if (access_check (paths
[i
], R_OK
) == 0)
799 size_t len
= strlen (paths
[i
]);
800 char *tmp
= xstrdup (paths
[i
]);
801 strcpy (paths
[i
] + len
- strlen ("begin.o"), "end.o");
802 if (access_check (paths
[i
], R_OK
) != 0)
803 fatal_error (input_location
,
804 "installation error, can't find crtoffloadend.o");
805 /* The linker will delete the filenames we give it, so make
807 offloadbegin
= make_temp_file (".o");
808 offloadend
= make_temp_file (".o");
809 copy_file (offloadbegin
, tmp
);
810 copy_file (offloadend
, paths
[i
]);
815 fatal_error (input_location
,
816 "installation error, can't find crtoffloadbegin.o");
818 free_array_of_ptrs ((void **) paths
, n_paths
);
821 /* A subroutine of run_gcc. Examine the open file FD for lto sections with
822 name prefix PREFIX, at FILE_OFFSET, and store any options we find in OPTS
823 and OPT_COUNT. Return true if we found a matchingn section, false
824 otherwise. COLLECT_GCC holds the value of the environment variable with
828 find_and_merge_options (int fd
, off_t file_offset
, const char *prefix
,
829 struct cl_decoded_option
**opts
,
830 unsigned int *opt_count
, const char *collect_gcc
)
832 off_t offset
, length
;
837 struct cl_decoded_option
*fdecoded_options
= *opts
;
838 unsigned int fdecoded_options_count
= *opt_count
;
840 simple_object_read
*sobj
;
841 sobj
= simple_object_start_read (fd
, file_offset
, "__GNU_LTO",
846 char *secname
= XALLOCAVEC (char, strlen (prefix
) + sizeof (".opts"));
847 strcpy (secname
, prefix
);
848 strcat (secname
, ".opts");
849 if (!simple_object_find_section (sobj
, secname
, &offset
, &length
,
852 simple_object_release_read (sobj
);
856 lseek (fd
, file_offset
+ offset
, SEEK_SET
);
857 data
= (char *)xmalloc (length
);
858 read (fd
, data
, length
);
862 struct cl_decoded_option
*f2decoded_options
;
863 unsigned int f2decoded_options_count
;
864 get_options_from_collect_gcc_options (collect_gcc
,
867 &f2decoded_options_count
);
868 if (!fdecoded_options
)
870 fdecoded_options
= f2decoded_options
;
871 fdecoded_options_count
= f2decoded_options_count
;
874 merge_and_complain (&fdecoded_options
,
875 &fdecoded_options_count
,
876 f2decoded_options
, f2decoded_options_count
);
878 fopts
+= strlen (fopts
) + 1;
880 while (fopts
- data
< length
);
883 simple_object_release_read (sobj
);
884 *opts
= fdecoded_options
;
885 *opt_count
= fdecoded_options_count
;
889 /* Execute gcc. ARGC is the number of arguments. ARGV contains the arguments. */
892 run_gcc (unsigned argc
, char *argv
[])
895 const char **new_argv
;
896 const char **argv_ptr
;
897 char *list_option_full
= NULL
;
898 const char *linker_output
= NULL
;
899 const char *collect_gcc
, *collect_gcc_options
;
902 bool no_partition
= false;
903 struct cl_decoded_option
*fdecoded_options
= NULL
;
904 struct cl_decoded_option
*offload_fdecoded_options
= NULL
;
905 unsigned int fdecoded_options_count
= 0;
906 unsigned int offload_fdecoded_options_count
= 0;
907 struct cl_decoded_option
*decoded_options
;
908 unsigned int decoded_options_count
;
909 struct obstack argv_obstack
;
911 bool have_lto
= false;
912 bool have_offload
= false;
913 unsigned lto_argc
= 0, offload_argc
= 0;
914 char **lto_argv
, **offload_argv
;
916 /* Get the driver and options. */
917 collect_gcc
= getenv ("COLLECT_GCC");
919 fatal_error (input_location
,
920 "environment variable COLLECT_GCC must be set");
921 collect_gcc_options
= getenv ("COLLECT_GCC_OPTIONS");
922 if (!collect_gcc_options
)
923 fatal_error (input_location
,
924 "environment variable COLLECT_GCC_OPTIONS must be set");
925 get_options_from_collect_gcc_options (collect_gcc
, collect_gcc_options
,
928 &decoded_options_count
);
930 /* Allocate arrays for input object files with LTO or offload IL,
931 and for possible preceding arguments. */
932 lto_argv
= XNEWVEC (char *, argc
);
933 offload_argv
= XNEWVEC (char *, argc
);
935 /* Look at saved options in the IL files. */
936 for (i
= 1; i
< argc
; ++i
)
940 off_t file_offset
= 0;
943 char *filename
= argv
[i
];
945 if ((p
= strrchr (argv
[i
], '@'))
947 && sscanf (p
, "@%li%n", &loffset
, &consumed
) >= 1
948 && strlen (p
) == (unsigned int) consumed
)
950 filename
= XNEWVEC (char, p
- argv
[i
] + 1);
951 memcpy (filename
, argv
[i
], p
- argv
[i
]);
952 filename
[p
- argv
[i
]] = '\0';
953 file_offset
= (off_t
) loffset
;
955 fd
= open (filename
, O_RDONLY
| O_BINARY
);
958 lto_argv
[lto_argc
++] = argv
[i
];
962 if (find_and_merge_options (fd
, file_offset
, LTO_SECTION_NAME_PREFIX
,
963 &fdecoded_options
, &fdecoded_options_count
,
967 lto_argv
[lto_argc
++] = argv
[i
];
970 if (find_and_merge_options (fd
, file_offset
, OFFLOAD_SECTION_NAME_PREFIX
,
971 &offload_fdecoded_options
,
972 &offload_fdecoded_options_count
, collect_gcc
))
975 offload_argv
[offload_argc
++] = argv
[i
];
981 /* Initalize the common arguments for the driver. */
982 obstack_init (&argv_obstack
);
983 obstack_ptr_grow (&argv_obstack
, collect_gcc
);
984 obstack_ptr_grow (&argv_obstack
, "-xlto");
985 obstack_ptr_grow (&argv_obstack
, "-c");
987 append_compiler_options (&argv_obstack
, fdecoded_options
,
988 fdecoded_options_count
);
989 append_linker_options (&argv_obstack
, decoded_options
, decoded_options_count
);
991 /* Scan linker driver arguments for things that are of relevance to us. */
992 for (j
= 1; j
< decoded_options_count
; ++j
)
994 struct cl_decoded_option
*option
= &decoded_options
[j
];
995 switch (option
->opt_index
)
998 linker_output
= option
->arg
;
1001 case OPT_save_temps
:
1009 case OPT_flto_partition_
:
1010 if (strcmp (option
->arg
, "none") == 0)
1011 no_partition
= true;
1015 if (strcmp (option
->arg
, "jobserver") == 0)
1022 parallel
= atoi (option
->arg
);
1029 lto_mode
= LTO_MODE_WHOPR
;
1039 lto_mode
= LTO_MODE_LTO
;
1046 char *output_dir
, *base
, *name
;
1047 bool bit_bucket
= strcmp (linker_output
, HOST_BIT_BUCKET
) == 0;
1049 output_dir
= xstrdup (linker_output
);
1051 for (name
= base
; *name
; name
++)
1052 if (IS_DIR_SEPARATOR (*name
))
1056 linker_output
= &linker_output
[base
- output_dir
];
1057 if (*output_dir
== '\0')
1059 static char current_dir
[] = { '.', DIR_SEPARATOR
, '\0' };
1060 output_dir
= current_dir
;
1064 obstack_ptr_grow (&argv_obstack
, "-dumpdir");
1065 obstack_ptr_grow (&argv_obstack
, output_dir
);
1068 obstack_ptr_grow (&argv_obstack
, "-dumpbase");
1071 /* Remember at which point we can scrub args to re-use the commons. */
1072 new_head_argc
= obstack_object_size (&argv_obstack
) / sizeof (void *);
1076 compile_images_for_offload_targets (offload_argc
, offload_argv
,
1077 offload_fdecoded_options
,
1078 offload_fdecoded_options_count
,
1080 decoded_options_count
);
1083 find_offloadbeginend ();
1084 for (i
= 0; offload_names
[i
]; i
++)
1085 printf ("%s\n", offload_names
[i
]);
1086 free_array_of_ptrs ((void **) offload_names
, i
);
1091 printf ("%s\n", offloadbegin
);
1093 /* If object files contain offload sections, but do not contain LTO sections,
1094 then there is no need to perform a link-time recompilation, i.e.
1095 lto-wrapper is used only for a compilation of offload images. */
1096 if (have_offload
&& !have_lto
)
1098 for (i
= 1; i
< argc
; ++i
)
1099 if (strncmp (argv
[i
], "-fresolution=", sizeof ("-fresolution=") - 1))
1102 /* Can be ".o" or ".so". */
1103 char *ext
= strrchr (argv
[i
], '.');
1105 out_file
= make_temp_file ("");
1107 out_file
= make_temp_file (ext
);
1108 /* The linker will delete the files we give it, so make copies. */
1109 copy_file (out_file
, argv
[i
]);
1110 printf ("%s\n", out_file
);
1115 if (lto_mode
== LTO_MODE_LTO
)
1117 flto_out
= make_temp_file (".lto.o");
1119 obstack_ptr_grow (&argv_obstack
, linker_output
);
1120 obstack_ptr_grow (&argv_obstack
, "-o");
1121 obstack_ptr_grow (&argv_obstack
, flto_out
);
1125 const char *list_option
= "-fltrans-output-list=";
1126 size_t list_option_len
= strlen (list_option
);
1131 char *dumpbase
= (char *) xmalloc (strlen (linker_output
)
1132 + sizeof (".wpa") + 1);
1133 strcpy (dumpbase
, linker_output
);
1134 strcat (dumpbase
, ".wpa");
1135 obstack_ptr_grow (&argv_obstack
, dumpbase
);
1138 if (linker_output
&& save_temps
)
1140 ltrans_output_file
= (char *) xmalloc (strlen (linker_output
)
1141 + sizeof (".ltrans.out") + 1);
1142 strcpy (ltrans_output_file
, linker_output
);
1143 strcat (ltrans_output_file
, ".ltrans.out");
1146 ltrans_output_file
= make_temp_file (".ltrans.out");
1147 list_option_full
= (char *) xmalloc (sizeof (char) *
1148 (strlen (ltrans_output_file
) + list_option_len
+ 1));
1149 tmp
= list_option_full
;
1151 obstack_ptr_grow (&argv_obstack
, tmp
);
1152 strcpy (tmp
, list_option
);
1153 tmp
+= list_option_len
;
1154 strcpy (tmp
, ltrans_output_file
);
1157 obstack_ptr_grow (&argv_obstack
, xstrdup ("-fwpa=jobserver"));
1158 else if (parallel
> 1)
1161 sprintf (buf
, "-fwpa=%i", parallel
);
1162 obstack_ptr_grow (&argv_obstack
, xstrdup (buf
));
1165 obstack_ptr_grow (&argv_obstack
, "-fwpa");
1168 /* Append the input objects and possible preceding arguments. */
1169 for (i
= 0; i
< lto_argc
; ++i
)
1170 obstack_ptr_grow (&argv_obstack
, lto_argv
[i
]);
1171 obstack_ptr_grow (&argv_obstack
, NULL
);
1173 new_argv
= XOBFINISH (&argv_obstack
, const char **);
1174 argv_ptr
= &new_argv
[new_head_argc
];
1175 fork_execute (new_argv
[0], CONST_CAST (char **, new_argv
), true);
1177 if (lto_mode
== LTO_MODE_LTO
)
1179 printf ("%s\n", flto_out
);
1185 FILE *stream
= fopen (ltrans_output_file
, "r");
1186 FILE *mstream
= NULL
;
1187 struct obstack env_obstack
;
1190 fatal_error (input_location
, "fopen: %s: %m", ltrans_output_file
);
1192 /* Parse the list of LTRANS inputs from the WPA stage. */
1193 obstack_init (&env_obstack
);
1197 const unsigned piece
= 32;
1198 char *output_name
= NULL
;
1199 char *buf
, *input_name
= (char *)xmalloc (piece
);
1204 if (!fgets (buf
, piece
, stream
))
1206 len
= strlen (input_name
);
1207 if (input_name
[len
- 1] != '\n')
1209 input_name
= (char *)xrealloc (input_name
, len
+ piece
);
1210 buf
= input_name
+ len
;
1213 input_name
[len
- 1] = '\0';
1215 if (input_name
[0] == '*')
1216 output_name
= &input_name
[1];
1219 input_names
= (char **)xrealloc (input_names
, nr
* sizeof (char *));
1220 output_names
= (char **)xrealloc (output_names
, nr
* sizeof (char *));
1221 input_names
[nr
-1] = input_name
;
1222 output_names
[nr
-1] = output_name
;
1225 maybe_unlink (ltrans_output_file
);
1226 ltrans_output_file
= NULL
;
1230 makefile
= make_temp_file (".mk");
1231 mstream
= fopen (makefile
, "w");
1234 /* Execute the LTRANS stage for each input file (or prepare a
1235 makefile to invoke this in parallel). */
1236 for (i
= 0; i
< nr
; ++i
)
1239 char *input_name
= input_names
[i
];
1240 /* If it's a pass-through file do nothing. */
1241 if (output_names
[i
])
1244 /* Replace the .o suffix with a .ltrans.o suffix and write
1245 the resulting name to the LTRANS output list. */
1246 obstack_grow (&env_obstack
, input_name
, strlen (input_name
) - 2);
1247 obstack_grow (&env_obstack
, ".ltrans.o", sizeof (".ltrans.o"));
1248 output_name
= XOBFINISH (&env_obstack
, char *);
1250 /* Adjust the dumpbase if the linker output file was seen. */
1254 = (char *) xmalloc (strlen (linker_output
)
1255 + sizeof (DUMPBASE_SUFFIX
) + 1);
1257 strlen (linker_output
) + sizeof (DUMPBASE_SUFFIX
),
1258 "%s.ltrans%u", linker_output
, i
);
1259 argv_ptr
[0] = dumpbase
;
1262 argv_ptr
[1] = "-fltrans";
1264 argv_ptr
[3] = output_name
;
1265 argv_ptr
[4] = input_name
;
1269 fprintf (mstream
, "%s:\n\t@%s ", output_name
, new_argv
[0]);
1270 for (j
= 1; new_argv
[j
] != NULL
; ++j
)
1271 fprintf (mstream
, " '%s'", new_argv
[j
]);
1272 fprintf (mstream
, "\n");
1273 /* If we are not preserving the ltrans input files then
1274 truncate them as soon as we have processed it. This
1275 reduces temporary disk-space usage. */
1277 fprintf (mstream
, "\t@-touch -r %s %s.tem > /dev/null 2>&1 "
1278 "&& mv %s.tem %s\n",
1279 input_name
, input_name
, input_name
, input_name
);
1283 fork_execute (new_argv
[0], CONST_CAST (char **, new_argv
),
1285 maybe_unlink (input_name
);
1288 output_names
[i
] = output_name
;
1292 struct pex_obj
*pex
;
1295 fprintf (mstream
, "all:");
1296 for (i
= 0; i
< nr
; ++i
)
1297 fprintf (mstream
, " \\\n\t%s", output_names
[i
]);
1298 fprintf (mstream
, "\n");
1302 /* Avoid passing --jobserver-fd= and similar flags
1303 unless jobserver mode is explicitly enabled. */
1304 putenv (xstrdup ("MAKEFLAGS="));
1305 putenv (xstrdup ("MFLAGS="));
1307 new_argv
[0] = getenv ("MAKE");
1309 new_argv
[0] = "make";
1311 new_argv
[2] = makefile
;
1315 snprintf (jobs
, 31, "-j%d", parallel
);
1316 new_argv
[i
++] = jobs
;
1318 new_argv
[i
++] = "all";
1319 new_argv
[i
++] = NULL
;
1320 pex
= collect_execute (new_argv
[0], CONST_CAST (char **, new_argv
),
1321 NULL
, NULL
, PEX_SEARCH
, false);
1322 do_wait (new_argv
[0], pex
);
1323 maybe_unlink (makefile
);
1325 for (i
= 0; i
< nr
; ++i
)
1326 maybe_unlink (input_names
[i
]);
1328 for (i
= 0; i
< nr
; ++i
)
1330 fputs (output_names
[i
], stdout
);
1331 putc ('\n', stdout
);
1332 free (input_names
[i
]);
1335 free (output_names
);
1337 free (list_option_full
);
1338 obstack_free (&env_obstack
, NULL
);
1343 printf ("%s\n", offloadend
);
1346 XDELETE (offload_argv
);
1347 obstack_free (&argv_obstack
, NULL
);
1354 main (int argc
, char *argv
[])
1358 init_opts_obstack ();
1360 p
= argv
[0] + strlen (argv
[0]);
1361 while (p
!= argv
[0] && !IS_DIR_SEPARATOR (p
[-1]))
1365 xmalloc_set_program_name (progname
);
1367 gcc_init_libintl ();
1369 diagnostic_initialize (global_dc
, 0);
1371 if (atexit (lto_wrapper_cleanup
) != 0)
1372 fatal_error (input_location
, "atexit failed");
1374 if (signal (SIGINT
, SIG_IGN
) != SIG_IGN
)
1375 signal (SIGINT
, fatal_signal
);
1377 if (signal (SIGHUP
, SIG_IGN
) != SIG_IGN
)
1378 signal (SIGHUP
, fatal_signal
);
1380 if (signal (SIGTERM
, SIG_IGN
) != SIG_IGN
)
1381 signal (SIGTERM
, fatal_signal
);
1383 if (signal (SIGPIPE
, SIG_IGN
) != SIG_IGN
)
1384 signal (SIGPIPE
, fatal_signal
);
1387 /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
1388 receive the signal. A different setting is inheritable */
1389 signal (SIGCHLD
, SIG_DFL
);
1392 /* We may be called with all the arguments stored in some file and
1393 passed with @file. Expand them into argv before processing. */
1394 expandargv (&argc
, &argv
);
1396 run_gcc (argc
, argv
);