1 /* Wrapper to call lto. Used by collect2 and the linker plugin.
2 Copyright (C) 2009-2021 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"
55 #define OFFLOAD_TARGET_DEFAULT_ENV "OFFLOAD_TARGET_DEFAULT"
57 /* By default there is no special suffix for target executables. */
58 #ifdef TARGET_EXECUTABLE_SUFFIX
59 #define HAVE_TARGET_EXECUTABLE_SUFFIX
61 #define TARGET_EXECUTABLE_SUFFIX ""
65 LTO_MODE_NONE
, /* Not doing LTO. */
66 LTO_MODE_LTO
, /* Normal LTO. */
67 LTO_MODE_WHOPR
/* WHOPR. */
70 /* Current LTO mode. */
71 static enum lto_mode_d lto_mode
= LTO_MODE_NONE
;
73 static char *ltrans_output_file
;
74 static char *flto_out
;
75 static unsigned int nr
;
76 static int *ltrans_priorities
;
77 static char **input_names
;
78 static char **output_names
;
79 static char **offload_names
;
80 static char *offload_objects_file_name
;
81 static char *makefile
;
82 static unsigned int num_deb_objs
;
83 static const char **early_debug_object_names
;
84 static bool xassembler_options_error
= false;
86 const char tool_name
[] = "lto-wrapper";
88 /* Delete tempfiles. Called from utils_cleanup. */
95 if (ltrans_output_file
)
96 maybe_unlink (ltrans_output_file
);
98 maybe_unlink (flto_out
);
99 if (offload_objects_file_name
)
100 maybe_unlink (offload_objects_file_name
);
102 maybe_unlink (makefile
);
103 if (early_debug_object_names
)
104 for (i
= 0; i
< num_deb_objs
; ++i
)
105 if (early_debug_object_names
[i
])
106 maybe_unlink (early_debug_object_names
[i
]);
107 for (i
= 0; i
< nr
; ++i
)
109 maybe_unlink (input_names
[i
]);
111 maybe_unlink (output_names
[i
]);
116 lto_wrapper_cleanup (void)
118 utils_cleanup (false);
121 /* Unlink a temporary LTRANS file unless requested otherwise. */
124 maybe_unlink (const char *file
)
128 if (unlink_if_ordinary (file
)
130 fatal_error (input_location
, "deleting LTRANS file %s: %m", file
);
133 fprintf (stderr
, "[Leaving LTRANS %s]\n", file
);
136 /* Template of LTRANS dumpbase suffix. */
137 #define DUMPBASE_SUFFIX "ltrans18446744073709551615"
139 /* Create decoded options from the COLLECT_GCC and COLLECT_GCC_OPTIONS
143 get_options_from_collect_gcc_options (const char *collect_gcc
,
144 const char *collect_gcc_options
,
145 struct cl_decoded_option
**decoded_options
,
146 unsigned int *decoded_options_count
)
148 struct obstack argv_obstack
;
152 obstack_init (&argv_obstack
);
153 obstack_ptr_grow (&argv_obstack
, collect_gcc
);
155 parse_options_from_collect_gcc_options (collect_gcc_options
,
156 &argv_obstack
, &argc
);
157 argv
= XOBFINISH (&argv_obstack
, const char **);
159 decode_cmdline_options_to_array (argc
, (const char **)argv
, CL_DRIVER
,
160 decoded_options
, decoded_options_count
);
161 obstack_free (&argv_obstack
, NULL
);
164 /* Append OPTION to the options array DECODED_OPTIONS with size
165 DECODED_OPTIONS_COUNT. */
168 append_option (struct cl_decoded_option
**decoded_options
,
169 unsigned int *decoded_options_count
,
170 struct cl_decoded_option
*option
)
172 ++*decoded_options_count
;
174 = (struct cl_decoded_option
*)
175 xrealloc (*decoded_options
,
176 (*decoded_options_count
177 * sizeof (struct cl_decoded_option
)));
178 memcpy (&(*decoded_options
)[*decoded_options_count
- 1], option
,
179 sizeof (struct cl_decoded_option
));
182 /* Remove option number INDEX from DECODED_OPTIONS, update
183 DECODED_OPTIONS_COUNT. */
186 remove_option (struct cl_decoded_option
**decoded_options
,
187 int index
, unsigned int *decoded_options_count
)
189 --*decoded_options_count
;
190 memmove (&(*decoded_options
)[index
+ 1],
191 &(*decoded_options
)[index
],
192 sizeof (struct cl_decoded_option
)
193 * (*decoded_options_count
- index
));
196 /* Try to merge and complain about options FDECODED_OPTIONS when applied
197 ontop of DECODED_OPTIONS. */
200 merge_and_complain (struct cl_decoded_option
**decoded_options
,
201 unsigned int *decoded_options_count
,
202 struct cl_decoded_option
*fdecoded_options
,
203 unsigned int fdecoded_options_count
,
204 struct cl_decoded_option
*decoded_cl_options
,
205 unsigned int decoded_cl_options_count
)
208 struct cl_decoded_option
*pic_option
= NULL
;
209 struct cl_decoded_option
*pie_option
= NULL
;
210 struct cl_decoded_option
*cf_protection_option
= NULL
;
212 /* ??? Merge options from files. Most cases can be
213 handled by either unioning or intersecting
214 (for example -fwrapv is a case for unioning,
215 -ffast-math is for intersection). Most complaints
216 about real conflicts between different options can
217 be deferred to the compiler proper. Options that
218 we can neither safely handle by intersection nor
219 unioning would need to be complained about here.
220 Ideally we'd have a flag in the opt files that
221 tells whether to union or intersect or reject.
222 In absence of that it's unclear what a good default is.
223 It's also difficult to get positional handling correct. */
225 /* Look for a -fcf-protection option in the link-time options
226 which overrides any -fcf-protection from the lto sections. */
227 for (i
= 0; i
< decoded_cl_options_count
; ++i
)
229 struct cl_decoded_option
*foption
= &decoded_cl_options
[i
];
230 if (foption
->opt_index
== OPT_fcf_protection_
)
232 cf_protection_option
= foption
;
236 /* The following does what the old LTO option code did,
237 union all target and a selected set of common options. */
238 for (i
= 0; i
< fdecoded_options_count
; ++i
)
240 struct cl_decoded_option
*foption
= &fdecoded_options
[i
];
241 switch (foption
->opt_index
)
243 case OPT_SPECIAL_unknown
:
244 case OPT_SPECIAL_ignore
:
245 case OPT_SPECIAL_warn_removed
:
246 case OPT_SPECIAL_program_name
:
247 case OPT_SPECIAL_input_file
:
251 if (!(cl_options
[foption
->opt_index
].flags
& CL_TARGET
))
255 case OPT_fdiagnostics_show_caret
:
256 case OPT_fdiagnostics_show_labels
:
257 case OPT_fdiagnostics_show_line_numbers
:
258 case OPT_fdiagnostics_show_option
:
259 case OPT_fdiagnostics_show_location_
:
260 case OPT_fshow_column
:
264 /* Do what the old LTO code did - collect exactly one option
265 setting per OPT code, we pick the first we encounter.
266 ??? This doesn't make too much sense, but when it doesn't
267 then we should complain. */
268 for (j
= 0; j
< *decoded_options_count
; ++j
)
269 if ((*decoded_options
)[j
].opt_index
== foption
->opt_index
)
271 if (j
== *decoded_options_count
)
272 append_option (decoded_options
, decoded_options_count
, foption
);
275 /* Figure out what PIC/PIE level wins and merge the results. */
278 pic_option
= foption
;
282 pie_option
= foption
;
287 /* For selected options we can merge conservatively. */
288 for (j
= 0; j
< *decoded_options_count
; ++j
)
289 if ((*decoded_options
)[j
].opt_index
== foption
->opt_index
)
291 if (j
== *decoded_options_count
)
292 append_option (decoded_options
, decoded_options_count
, foption
);
293 /* -fopenmp > -fno-openmp,
294 -fopenacc > -fno-openacc */
295 else if (foption
->value
> (*decoded_options
)[j
].value
)
296 (*decoded_options
)[j
] = *foption
;
299 case OPT_fopenacc_dim_
:
300 /* Append or check identical. */
301 for (j
= 0; j
< *decoded_options_count
; ++j
)
302 if ((*decoded_options
)[j
].opt_index
== foption
->opt_index
)
304 if (j
== *decoded_options_count
)
305 append_option (decoded_options
, decoded_options_count
, foption
);
306 else if (strcmp ((*decoded_options
)[j
].arg
, foption
->arg
))
307 fatal_error (input_location
,
308 "option %s with different values",
309 foption
->orig_option_with_args_text
);
312 case OPT_fcf_protection_
:
313 /* Default to link-time option, else append or check identical. */
314 if (!cf_protection_option
315 || cf_protection_option
->value
== CF_CHECK
)
317 for (j
= 0; j
< *decoded_options_count
; ++j
)
318 if ((*decoded_options
)[j
].opt_index
== foption
->opt_index
)
320 if (j
== *decoded_options_count
)
321 append_option (decoded_options
, decoded_options_count
, foption
);
322 else if ((*decoded_options
)[j
].value
!= foption
->value
)
324 if (cf_protection_option
325 && cf_protection_option
->value
== CF_CHECK
)
326 fatal_error (input_location
,
327 "option %qs with mismatching values"
330 (*decoded_options
)[j
].arg
, foption
->arg
);
333 /* Merge and update the -fcf-protection option. */
334 (*decoded_options
)[j
].value
&= (foption
->value
336 switch ((*decoded_options
)[j
].value
)
339 (*decoded_options
)[j
].arg
= "none";
342 (*decoded_options
)[j
].arg
= "branch";
345 (*decoded_options
)[j
].arg
= "return";
359 for (j
= 0; j
< *decoded_options_count
; ++j
)
360 if ((*decoded_options
)[j
].opt_index
== OPT_O
361 || (*decoded_options
)[j
].opt_index
== OPT_Ofast
362 || (*decoded_options
)[j
].opt_index
== OPT_Og
363 || (*decoded_options
)[j
].opt_index
== OPT_Os
)
365 if (j
== *decoded_options_count
)
366 append_option (decoded_options
, decoded_options_count
, foption
);
367 else if ((*decoded_options
)[j
].opt_index
== foption
->opt_index
368 && foption
->opt_index
!= OPT_O
)
369 /* Exact same options get merged. */
373 /* For mismatched option kinds preserve the optimization
374 level only, thus merge it as -On. This also handles
375 merging of same optimization level -On. */
377 switch (foption
->opt_index
)
380 if (foption
->arg
[0] == '\0')
381 level
= MAX (level
, 1);
383 level
= MAX (level
, atoi (foption
->arg
));
386 level
= MAX (level
, 3);
389 level
= MAX (level
, 1);
392 level
= MAX (level
, 2);
397 switch ((*decoded_options
)[j
].opt_index
)
400 if ((*decoded_options
)[j
].arg
[0] == '\0')
401 level
= MAX (level
, 1);
403 level
= MAX (level
, atoi ((*decoded_options
)[j
].arg
));
406 level
= MAX (level
, 3);
409 level
= MAX (level
, 1);
412 level
= MAX (level
, 2);
417 (*decoded_options
)[j
].opt_index
= OPT_O
;
419 tem
= xasprintf ("-O%d", level
);
420 (*decoded_options
)[j
].arg
= &tem
[2];
421 (*decoded_options
)[j
].canonical_option
[0] = tem
;
422 (*decoded_options
)[j
].value
= 1;
427 case OPT_foffload_abi_
:
428 for (j
= 0; j
< *decoded_options_count
; ++j
)
429 if ((*decoded_options
)[j
].opt_index
== foption
->opt_index
)
431 if (j
== *decoded_options_count
)
432 append_option (decoded_options
, decoded_options_count
, foption
);
433 else if (foption
->value
!= (*decoded_options
)[j
].value
)
434 fatal_error (input_location
,
435 "option %s not used consistently in all LTO input"
436 " files", foption
->orig_option_with_args_text
);
441 append_option (decoded_options
, decoded_options_count
, foption
);
446 /* Merge PIC options:
447 -fPIC + -fpic = -fpic
448 -fPIC + -fno-pic = -fno-pic
449 -fpic/-fPIC + nothing = nothing.
450 It is a common mistake to mix few -fPIC compiled objects into otherwise
451 non-PIC code. We do not want to build everything with PIC then.
453 Similarly we merge PIE options, however in addition we keep
454 -fPIC + -fPIE = -fPIE
455 -fpic + -fPIE = -fpie
456 -fPIC/-fpic + -fpie = -fpie
458 It would be good to warn on mismatches, but it is bit hard to do as
459 we do not know what nothing translates to. */
461 for (unsigned int j
= 0; j
< *decoded_options_count
;)
462 if ((*decoded_options
)[j
].opt_index
== OPT_fPIC
463 || (*decoded_options
)[j
].opt_index
== OPT_fpic
)
465 /* -fno-pic in one unit implies -fno-pic everywhere. */
466 if ((*decoded_options
)[j
].value
== 0)
468 /* If we have no pic option or merge in -fno-pic, we still may turn
469 existing pic/PIC mode into pie/PIE if -fpie/-fPIE is present. */
470 else if ((pic_option
&& pic_option
->value
== 0)
475 bool big
= (*decoded_options
)[j
].opt_index
== OPT_fPIC
476 && pie_option
->opt_index
== OPT_fPIE
;
477 (*decoded_options
)[j
].opt_index
= big
? OPT_fPIE
: OPT_fpie
;
478 if (pie_option
->value
)
479 (*decoded_options
)[j
].canonical_option
[0]
480 = big
? "-fPIE" : "-fpie";
482 (*decoded_options
)[j
].canonical_option
[0] = "-fno-pie";
483 (*decoded_options
)[j
].value
= pie_option
->value
;
488 (*decoded_options
)[j
] = *pic_option
;
491 /* We do not know if target defaults to pic or not, so just remove
492 option if it is missing in one unit but enabled in other. */
494 remove_option (decoded_options
, j
, decoded_options_count
);
496 else if (pic_option
->opt_index
== OPT_fpic
497 && (*decoded_options
)[j
].opt_index
== OPT_fPIC
)
499 (*decoded_options
)[j
] = *pic_option
;
505 else if ((*decoded_options
)[j
].opt_index
== OPT_fPIE
506 || (*decoded_options
)[j
].opt_index
== OPT_fpie
)
508 /* -fno-pie in one unit implies -fno-pie everywhere. */
509 if ((*decoded_options
)[j
].value
== 0)
511 /* If we have no pie option or merge in -fno-pie, we still preserve
512 PIE/pie if pic/PIC is present. */
513 else if ((pie_option
&& pie_option
->value
== 0)
516 /* If -fPIC/-fpic is given, merge it with -fPIE/-fpie. */
519 if (pic_option
->opt_index
== OPT_fpic
520 && (*decoded_options
)[j
].opt_index
== OPT_fPIE
)
522 (*decoded_options
)[j
].opt_index
= OPT_fpie
;
523 (*decoded_options
)[j
].canonical_option
[0]
524 = pic_option
->value
? "-fpie" : "-fno-pie";
526 else if (!pic_option
->value
)
527 (*decoded_options
)[j
].canonical_option
[0] = "-fno-pie";
528 (*decoded_options
)[j
].value
= pic_option
->value
;
533 (*decoded_options
)[j
] = *pie_option
;
536 /* Because we always append pic/PIE options this code path should
537 not happen unless the LTO object was built by old lto1 which
538 did not contain that logic yet. */
540 remove_option (decoded_options
, j
, decoded_options_count
);
542 else if (pie_option
->opt_index
== OPT_fpie
543 && (*decoded_options
)[j
].opt_index
== OPT_fPIE
)
545 (*decoded_options
)[j
] = *pie_option
;
554 if (!xassembler_options_error
)
555 for (i
= j
= 0; ; i
++, j
++)
557 for (; i
< *decoded_options_count
; i
++)
558 if ((*decoded_options
)[i
].opt_index
== OPT_Xassembler
)
561 for (; j
< fdecoded_options_count
; j
++)
562 if (fdecoded_options
[j
].opt_index
== OPT_Xassembler
)
565 if (i
== *decoded_options_count
&& j
== fdecoded_options_count
)
567 else if (i
< *decoded_options_count
&& j
== fdecoded_options_count
)
569 warning (0, "Extra option to %<-Xassembler%>: %s,"
570 " dropping all %<-Xassembler%> and %<-Wa%> options.",
571 (*decoded_options
)[i
].arg
);
572 xassembler_options_error
= true;
575 else if (i
== *decoded_options_count
&& j
< fdecoded_options_count
)
577 warning (0, "Extra option to %<-Xassembler%>: %s,"
578 " dropping all %<-Xassembler%> and %<-Wa%> options.",
579 fdecoded_options
[j
].arg
);
580 xassembler_options_error
= true;
583 else if (strcmp ((*decoded_options
)[i
].arg
, fdecoded_options
[j
].arg
))
585 warning (0, "Options to %<-Xassembler%> do not match: %s, %s,"
586 " dropping all %<-Xassembler%> and %<-Wa%> options.",
587 (*decoded_options
)[i
].arg
, fdecoded_options
[j
].arg
);
588 xassembler_options_error
= true;
594 /* Auxiliary function that frees elements of PTR and PTR itself.
595 N is number of elements to be freed. If PTR is NULL, nothing is freed.
596 If an element is NULL, subsequent elements are not freed. */
599 free_array_of_ptrs (void **ptr
, unsigned n
)
603 for (unsigned i
= 0; i
< n
; i
++)
613 /* Parse STR, saving found tokens into PVALUES and return their number.
614 Tokens are assumed to be delimited by ':'. If APPEND is non-null,
615 append it to every token we find. */
618 parse_env_var (const char *str
, char ***pvalues
, const char *append
)
620 const char *curval
, *nextval
;
624 curval
= strchr (str
, ':');
628 curval
= strchr (curval
+ 1, ':');
631 values
= (char**) xmalloc (num
* sizeof (char*));
633 nextval
= strchr (curval
, ':');
635 nextval
= strchr (curval
, '\0');
637 int append_len
= append
? strlen (append
) : 0;
638 for (i
= 0; i
< num
; i
++)
640 int l
= nextval
- curval
;
641 values
[i
] = (char*) xmalloc (l
+ 1 + append_len
);
642 memcpy (values
[i
], curval
, l
);
645 strcat (values
[i
], append
);
646 curval
= nextval
+ 1;
647 nextval
= strchr (curval
, ':');
649 nextval
= strchr (curval
, '\0');
655 /* Append options OPTS from lto or offload_lto sections to ARGV_OBSTACK. */
658 append_compiler_options (obstack
*argv_obstack
, struct cl_decoded_option
*opts
,
661 /* Append compiler driver arguments as far as they were merged. */
662 for (unsigned int j
= 1; j
< count
; ++j
)
664 struct cl_decoded_option
*option
= &opts
[j
];
666 /* File options have been properly filtered by lto-opts.c. */
667 switch (option
->opt_index
)
669 /* Drop arguments that we want to take from the link line. */
672 case OPT_flto_partition_
:
679 /* For now do what the original LTO option code was doing - pass
680 on any CL_TARGET flag and a few selected others. */
681 switch (option
->opt_index
)
683 case OPT_fdiagnostics_show_caret
:
684 case OPT_fdiagnostics_show_labels
:
685 case OPT_fdiagnostics_show_line_numbers
:
686 case OPT_fdiagnostics_show_option
:
687 case OPT_fdiagnostics_show_location_
:
688 case OPT_fshow_column
:
697 case OPT_fopenacc_dim_
:
698 case OPT_foffload_abi_
:
699 case OPT_fcf_protection_
:
708 /* When we detected a mismatch in assembler options between
709 the input TU's fall back to previous behavior of ignoring them. */
710 if (xassembler_options_error
)
715 if (!(cl_options
[option
->opt_index
].flags
& CL_TARGET
))
719 /* Pass the option on. */
720 for (unsigned int i
= 0; i
< option
->canonical_option_num_elements
; ++i
)
721 obstack_ptr_grow (argv_obstack
, option
->canonical_option
[i
]);
725 /* Append diag options in OPTS with length COUNT to ARGV_OBSTACK. */
728 append_diag_options (obstack
*argv_obstack
, struct cl_decoded_option
*opts
,
731 /* Append compiler driver arguments as far as they were merged. */
732 for (unsigned int j
= 1; j
< count
; ++j
)
734 struct cl_decoded_option
*option
= &opts
[j
];
736 switch (option
->opt_index
)
738 case OPT_fdiagnostics_color_
:
739 case OPT_fdiagnostics_format_
:
740 case OPT_fdiagnostics_show_caret
:
741 case OPT_fdiagnostics_show_labels
:
742 case OPT_fdiagnostics_show_line_numbers
:
743 case OPT_fdiagnostics_show_option
:
744 case OPT_fdiagnostics_show_location_
:
745 case OPT_fshow_column
:
751 /* Pass the option on. */
752 for (unsigned int i
= 0; i
< option
->canonical_option_num_elements
; ++i
)
753 obstack_ptr_grow (argv_obstack
, option
->canonical_option
[i
]);
758 /* Append linker options OPTS to ARGV_OBSTACK. */
761 append_linker_options (obstack
*argv_obstack
, struct cl_decoded_option
*opts
,
764 /* Append linker driver arguments. Compiler options from the linker
765 driver arguments will override / merge with those from the compiler. */
766 for (unsigned int j
= 1; j
< count
; ++j
)
768 struct cl_decoded_option
*option
= &opts
[j
];
770 /* Do not pass on frontend specific flags not suitable for lto. */
771 if (!(cl_options
[option
->opt_index
].flags
772 & (CL_COMMON
|CL_TARGET
|CL_DRIVER
|CL_LTO
)))
775 switch (option
->opt_index
)
780 /* We've handled these LTO options, do not pass them on. */
785 /* Ignore -fno-XXX form of these options, as otherwise
786 corresponding builtins will not be enabled. */
787 if (option
->value
== 0)
795 /* Pass the option on. */
796 for (unsigned int i
= 0; i
< option
->canonical_option_num_elements
; ++i
)
797 obstack_ptr_grow (argv_obstack
, option
->canonical_option
[i
]);
801 /* Extract options for TARGET offload compiler from OPTIONS and append
802 them to ARGV_OBSTACK. */
805 append_offload_options (obstack
*argv_obstack
, const char *target
,
806 struct cl_decoded_option
*options
,
807 unsigned int options_count
)
809 for (unsigned i
= 0; i
< options_count
; i
++)
811 const char *cur
, *next
, *opts
;
814 struct cl_decoded_option
*option
= &options
[i
];
816 if (option
->opt_index
!= OPT_foffload_
)
819 /* If option argument starts with '-' then no target is specified. That
820 means offload options are specified for all targets, so we need to
822 if (option
->arg
[0] == '-')
826 opts
= strchr (option
->arg
, '=');
827 /* If there are offload targets specified, but no actual options,
828 there is nothing to do here. */
836 next
= strchr (cur
, ',');
839 next
= (next
> opts
) ? opts
: next
;
841 /* Are we looking for this offload target? */
842 if (strlen (target
) == (size_t) (next
- cur
)
843 && strncmp (target
, cur
, next
- cur
) == 0)
846 /* Skip the comma or equal sign. */
856 argv
= buildargv (opts
);
857 for (argc
= 0; argv
[argc
]; argc
++)
858 obstack_ptr_grow (argv_obstack
, argv
[argc
]);
862 /* Check whether NAME can be accessed in MODE. This is like access,
863 except that it never considers directories to be executable. */
866 access_check (const char *name
, int mode
)
872 if (stat (name
, &st
) < 0
873 || S_ISDIR (st
.st_mode
))
877 return access (name
, mode
);
880 /* Prepare a target image for offload TARGET, using mkoffload tool from
881 COMPILER_PATH. Return the name of the resultant object file. */
884 compile_offload_image (const char *target
, const char *compiler_path
,
885 unsigned in_argc
, char *in_argv
[],
886 struct cl_decoded_option
*compiler_opts
,
887 unsigned int compiler_opt_count
,
888 struct cl_decoded_option
*linker_opts
,
889 unsigned int linker_opt_count
)
891 char *filename
= NULL
;
895 = XALLOCAVEC (char, sizeof ("/accel//mkoffload") + strlen (target
));
896 strcpy (suffix
, "/accel/");
897 strcat (suffix
, target
);
898 strcat (suffix
, "/mkoffload");
901 unsigned n_paths
= parse_env_var (compiler_path
, &paths
, suffix
);
903 const char *compiler
= NULL
;
904 for (unsigned i
= 0; i
< n_paths
; i
++)
905 if (access_check (paths
[i
], X_OK
) == 0)
910 #if OFFLOAD_DEFAULTED
911 if (!compiler
&& getenv (OFFLOAD_TARGET_DEFAULT_ENV
))
913 free_array_of_ptrs ((void **) paths
, n_paths
);
919 fatal_error (input_location
,
920 "could not find %s in %s (consider using %<-B%>)",
921 suffix
+ 1, compiler_path
);
923 dumpbase
= concat (dumppfx
, "x", target
, NULL
);
925 /* Generate temporary output file name. */
927 filename
= concat (dumpbase
, ".o", NULL
);
929 filename
= make_temp_file (".target.o");
931 struct obstack argv_obstack
;
932 obstack_init (&argv_obstack
);
933 obstack_ptr_grow (&argv_obstack
, compiler
);
935 obstack_ptr_grow (&argv_obstack
, "-save-temps");
937 obstack_ptr_grow (&argv_obstack
, "-v");
938 obstack_ptr_grow (&argv_obstack
, "-o");
939 obstack_ptr_grow (&argv_obstack
, filename
);
941 /* Append names of input object files. */
942 for (unsigned i
= 0; i
< in_argc
; i
++)
943 obstack_ptr_grow (&argv_obstack
, in_argv
[i
]);
945 /* Append options from offload_lto sections. */
946 append_compiler_options (&argv_obstack
, compiler_opts
,
948 append_diag_options (&argv_obstack
, linker_opts
, linker_opt_count
);
950 obstack_ptr_grow (&argv_obstack
, "-dumpbase");
951 obstack_ptr_grow (&argv_obstack
, dumpbase
);
953 /* Append options specified by -foffload last. In case of conflicting
954 options we expect offload compiler to choose the latest. */
955 append_offload_options (&argv_obstack
, target
, compiler_opts
,
957 append_offload_options (&argv_obstack
, target
, linker_opts
,
960 obstack_ptr_grow (&argv_obstack
, NULL
);
961 argv
= XOBFINISH (&argv_obstack
, char **);
962 fork_execute (argv
[0], argv
, true, "offload_args");
963 obstack_free (&argv_obstack
, NULL
);
965 free_array_of_ptrs ((void **) paths
, n_paths
);
970 /* The main routine dealing with offloading.
971 The routine builds a target image for each offload target. IN_ARGC and
972 IN_ARGV specify options and input object files. As all of them could contain
973 target sections, we pass them all to target compilers. */
976 compile_images_for_offload_targets (unsigned in_argc
, char *in_argv
[],
977 struct cl_decoded_option
*compiler_opts
,
978 unsigned int compiler_opt_count
,
979 struct cl_decoded_option
*linker_opts
,
980 unsigned int linker_opt_count
)
983 const char *target_names
= getenv (OFFLOAD_TARGET_NAMES_ENV
);
986 unsigned num_targets
= parse_env_var (target_names
, &names
, NULL
);
987 int next_name_entry
= 0;
989 const char *compiler_path
= getenv ("COMPILER_PATH");
993 /* Prepare an image for each target and save the name of the resultant object
994 file to the OFFLOAD_NAMES array. It is terminated by a NULL entry. */
995 offload_names
= XCNEWVEC (char *, num_targets
+ 1);
996 for (unsigned i
= 0; i
< num_targets
; i
++)
998 offload_names
[next_name_entry
]
999 = compile_offload_image (names
[i
], compiler_path
, in_argc
, in_argv
,
1000 compiler_opts
, compiler_opt_count
,
1001 linker_opts
, linker_opt_count
);
1002 if (!offload_names
[next_name_entry
])
1003 #if OFFLOAD_DEFAULTED
1006 fatal_error (input_location
,
1007 "problem with building target image for %s", names
[i
]);
1012 #if OFFLOAD_DEFAULTED
1013 if (next_name_entry
== 0)
1015 free (offload_names
);
1016 offload_names
= NULL
;
1021 free_array_of_ptrs ((void **) names
, num_targets
);
1024 /* Copy a file from SRC to DEST. */
1027 copy_file (const char *dest
, const char *src
)
1029 FILE *d
= fopen (dest
, "wb");
1030 FILE *s
= fopen (src
, "rb");
1034 size_t len
= fread (buffer
, 1, 512, s
);
1035 if (ferror (s
) != 0)
1036 fatal_error (input_location
, "reading input file");
1039 fwrite (buffer
, 1, len
, d
);
1040 if (ferror (d
) != 0)
1041 fatal_error (input_location
, "writing output file");
1048 /* Find the crtoffloadtable.o file in LIBRARY_PATH, make copy and pass name of
1049 the copy to the linker. */
1052 find_crtoffloadtable (int save_temps
, const char *dumppfx
)
1054 char **paths
= NULL
;
1055 const char *library_path
= getenv ("LIBRARY_PATH");
1058 unsigned n_paths
= parse_env_var (library_path
, &paths
, "/crtoffloadtable.o");
1061 for (i
= 0; i
< n_paths
; i
++)
1062 if (access_check (paths
[i
], R_OK
) == 0)
1064 /* The linker will delete the filename we give it, so make a copy. */
1065 char *crtoffloadtable
;
1067 crtoffloadtable
= make_temp_file (".crtoffloadtable.o");
1069 crtoffloadtable
= concat (dumppfx
, "crtoffloadtable.o", NULL
);
1070 copy_file (crtoffloadtable
, paths
[i
]);
1071 printf ("%s\n", crtoffloadtable
);
1072 XDELETEVEC (crtoffloadtable
);
1076 fatal_error (input_location
,
1077 "installation error, cannot find %<crtoffloadtable.o%>");
1079 free_array_of_ptrs ((void **) paths
, n_paths
);
1082 /* A subroutine of run_gcc. Examine the open file FD for lto sections with
1083 name prefix PREFIX, at FILE_OFFSET, and store any options we find in OPTS
1084 and OPT_COUNT. Return true if we found a matching section, false
1085 otherwise. COLLECT_GCC holds the value of the environment variable with
1089 find_and_merge_options (int fd
, off_t file_offset
, const char *prefix
,
1090 struct cl_decoded_option
*decoded_cl_options
,
1091 unsigned int decoded_cl_options_count
,
1092 struct cl_decoded_option
**opts
,
1093 unsigned int *opt_count
, const char *collect_gcc
)
1095 off_t offset
, length
;
1100 struct cl_decoded_option
*fdecoded_options
= *opts
;
1101 unsigned int fdecoded_options_count
= *opt_count
;
1103 simple_object_read
*sobj
;
1104 sobj
= simple_object_start_read (fd
, file_offset
, "__GNU_LTO",
1109 char *secname
= XALLOCAVEC (char, strlen (prefix
) + sizeof (".opts"));
1110 strcpy (secname
, prefix
);
1111 strcat (secname
, ".opts");
1112 if (!simple_object_find_section (sobj
, secname
, &offset
, &length
,
1115 simple_object_release_read (sobj
);
1119 lseek (fd
, file_offset
+ offset
, SEEK_SET
);
1120 data
= (char *)xmalloc (length
);
1121 read (fd
, data
, length
);
1125 struct cl_decoded_option
*f2decoded_options
;
1126 unsigned int f2decoded_options_count
;
1127 get_options_from_collect_gcc_options (collect_gcc
, fopts
,
1129 &f2decoded_options_count
);
1130 if (!fdecoded_options
)
1132 fdecoded_options
= f2decoded_options
;
1133 fdecoded_options_count
= f2decoded_options_count
;
1136 merge_and_complain (&fdecoded_options
,
1137 &fdecoded_options_count
,
1138 f2decoded_options
, f2decoded_options_count
,
1140 decoded_cl_options_count
);
1142 fopts
+= strlen (fopts
) + 1;
1144 while (fopts
- data
< length
);
1147 simple_object_release_read (sobj
);
1148 *opts
= fdecoded_options
;
1149 *opt_count
= fdecoded_options_count
;
1153 /* Copy early debug info sections from INFILE to a new file whose name
1154 is returned. Return NULL on error. */
1157 debug_objcopy (const char *infile
, bool rename
)
1164 const char *orig_infile
= infile
;
1168 if ((p
= strrchr (infile
, '@'))
1170 && sscanf (p
, "@%li%n", &loffset
, &consumed
) >= 1
1171 && strlen (p
) == (unsigned int) consumed
)
1173 char *fname
= xstrdup (infile
);
1174 fname
[p
- infile
] = '\0';
1176 inoff
= (off_t
) loffset
;
1178 int infd
= open (infile
, O_RDONLY
| O_BINARY
);
1181 simple_object_read
*inobj
= simple_object_start_read (infd
, inoff
,
1188 if (simple_object_find_section (inobj
, ".gnu.debuglto_.debug_info",
1189 &off
, &len
, &errmsg
, &err
) != 1)
1192 fatal_error (0, "%s: %s", errmsg
, xstrerror (err
));
1194 simple_object_release_read (inobj
);
1200 outfile
= concat (orig_infile
, ".debug.temp.o", NULL
);
1202 outfile
= make_temp_file (".debug.temp.o");
1203 errmsg
= simple_object_copy_lto_debug_sections (inobj
, outfile
, &err
, rename
);
1206 unlink_if_ordinary (outfile
);
1207 fatal_error (0, "%s: %s", errmsg
, xstrerror (err
));
1210 simple_object_release_read (inobj
);
1216 /* Helper for qsort: compare priorities for parallel compilation. */
1219 cmp_priority (const void *a
, const void *b
)
1221 return *((const int *)b
)-*((const int *)a
);
1224 /* Number of CPUs that can be used for parallel LTRANS phase. */
1226 static unsigned long nthreads_var
= 0;
1228 #ifdef HAVE_PTHREAD_AFFINITY_NP
1229 unsigned long cpuset_size
;
1230 static unsigned long get_cpuset_size
;
1234 static cpuset_popcount (unsigned long cpusetsize
, cpu_set_t
*cpusetp
)
1237 /* glibc 2.7 and above provide a macro for this. */
1238 return CPU_COUNT_S (cpusetsize
, cpusetp
);
1241 if (cpusetsize
== sizeof (cpu_set_t
))
1242 /* glibc 2.6 and above provide a macro for this. */
1243 return CPU_COUNT (cpusetp
);
1246 unsigned long ret
= 0;
1247 STATIC_ASSERT (sizeof (cpusetp
->__bits
[0]) == sizeof (unsigned long int));
1248 for (i
= 0; i
< cpusetsize
/ sizeof (cpusetp
->__bits
[0]); i
++)
1250 unsigned long int mask
= cpusetp
->__bits
[i
];
1253 ret
+= __builtin_popcountl (mask
);
1260 /* At startup, determine the default number of threads. It would seem
1261 this should be related to the number of cpus online. */
1264 init_num_threads (void)
1266 #ifdef HAVE_PTHREAD_AFFINITY_NP
1267 #if defined (_SC_NPROCESSORS_CONF) && defined (CPU_ALLOC_SIZE)
1268 cpuset_size
= sysconf (_SC_NPROCESSORS_CONF
);
1269 cpuset_size
= CPU_ALLOC_SIZE (cpuset_size
);
1271 cpuset_size
= sizeof (cpu_set_t
);
1274 cpusetp
= (cpu_set_t
*) xmalloc (gomp_cpuset_size
);
1277 int ret
= pthread_getaffinity_np (pthread_self (), gomp_cpuset_size
,
1281 /* Count only the CPUs this process can use. */
1282 nthreads_var
= cpuset_popcount (cpuset_size
, cpusetp
);
1283 if (nthreads_var
== 0)
1285 get_cpuset_size
= cpuset_size
;
1286 #ifdef CPU_ALLOC_SIZE
1288 for (i
= cpuset_size
* 8; i
; i
--)
1289 if (CPU_ISSET_S (i
- 1, cpuset_size
, cpusetp
))
1291 cpuset_size
= CPU_ALLOC_SIZE (i
);
1297 #ifdef CPU_ALLOC_SIZE
1298 if (cpuset_size
< sizeof (cpu_set_t
))
1299 cpuset_size
= sizeof (cpu_set_t
);
1301 cpuset_size
= cpuset_size
* 2;
1302 if (cpuset_size
< 8 * sizeof (cpu_set_t
))
1304 = (cpu_set_t
*) realloc (cpusetp
, cpuset_size
);
1307 /* Avoid fatal if too large memory allocation would be
1308 requested, e.g. kernel returning EINVAL all the time. */
1309 void *p
= realloc (cpusetp
, cpuset_size
);
1312 cpusetp
= (cpu_set_t
*) p
;
1324 #ifdef _SC_NPROCESSORS_ONLN
1325 nthreads_var
= sysconf (_SC_NPROCESSORS_ONLN
);
1329 /* Test and return reason why a jobserver cannot be detected. */
1332 jobserver_active_p (void)
1334 #define JS_PREFIX "jobserver is not available: "
1335 #define JS_NEEDLE "--jobserver-auth="
1337 const char *makeflags
= getenv ("MAKEFLAGS");
1338 if (makeflags
== NULL
)
1339 return JS_PREFIX
"%<MAKEFLAGS%> environment variable is unset";
1341 const char *n
= strstr (makeflags
, JS_NEEDLE
);
1343 return JS_PREFIX
"%<" JS_NEEDLE
"%> is not present in %<MAKEFLAGS%>";
1348 if (sscanf (n
+ strlen (JS_NEEDLE
), "%d,%d", &rfd
, &wfd
) == 2
1351 && is_valid_fd (rfd
)
1352 && is_valid_fd (wfd
))
1355 return JS_PREFIX
"cannot access %<" JS_NEEDLE
"%> file descriptors";
1358 /* Test that a make command is present and working, return true if so. */
1363 const char *make
= "make";
1364 char **make_argv
= buildargv (getenv ("MAKE"));
1366 make
= make_argv
[0];
1367 const char *make_args
[] = {make
, "--version", NULL
};
1369 int exit_status
= 0;
1372 = pex_one (PEX_SEARCH
, make_args
[0], CONST_CAST (char **, make_args
),
1373 "make", NULL
, NULL
, &exit_status
, &err
);
1374 freeargv (make_argv
);
1375 return errmsg
== NULL
&& exit_status
== 0 && err
== 0;
1378 /* Execute gcc. ARGC is the number of arguments. ARGV contains the arguments. */
1381 run_gcc (unsigned argc
, char *argv
[])
1384 const char **new_argv
;
1385 const char **argv_ptr
;
1386 char *list_option_full
= NULL
;
1387 const char *linker_output
= NULL
;
1388 const char *collect_gcc
;
1389 char *collect_gcc_options
;
1392 int auto_parallel
= 0;
1393 bool no_partition
= false;
1394 struct cl_decoded_option
*fdecoded_options
= NULL
;
1395 struct cl_decoded_option
*offload_fdecoded_options
= NULL
;
1396 unsigned int fdecoded_options_count
= 0;
1397 unsigned int offload_fdecoded_options_count
= 0;
1398 struct cl_decoded_option
*decoded_options
;
1399 unsigned int decoded_options_count
;
1400 struct obstack argv_obstack
;
1402 bool have_lto
= false;
1403 bool have_offload
= false;
1404 unsigned lto_argc
= 0, ltoobj_argc
= 0;
1405 char **lto_argv
, **ltoobj_argv
;
1406 bool linker_output_rel
= false;
1407 bool skip_debug
= false;
1408 unsigned n_debugobj
;
1409 const char *incoming_dumppfx
= dumppfx
= NULL
;
1410 static char current_dir
[] = { '.', DIR_SEPARATOR
, '\0' };
1412 /* Get the driver and options. */
1413 collect_gcc
= getenv ("COLLECT_GCC");
1415 fatal_error (input_location
,
1416 "environment variable %<COLLECT_GCC%> must be set");
1417 collect_gcc_options
= getenv ("COLLECT_GCC_OPTIONS");
1418 if (!collect_gcc_options
)
1419 fatal_error (input_location
,
1420 "environment variable %<COLLECT_GCC_OPTIONS%> must be set");
1422 char *collect_as_options
= getenv ("COLLECT_AS_OPTIONS");
1424 /* Prepend -Xassembler to each option, and append the string
1425 to collect_gcc_options. */
1426 if (collect_as_options
)
1428 obstack temporary_obstack
;
1429 obstack_init (&temporary_obstack
);
1431 prepend_xassembler_to_collect_as_options (collect_as_options
,
1432 &temporary_obstack
);
1433 obstack_1grow (&temporary_obstack
, '\0');
1435 char *xassembler_opts_string
1436 = XOBFINISH (&temporary_obstack
, char *);
1437 collect_gcc_options
= concat (collect_gcc_options
, xassembler_opts_string
,
1441 get_options_from_collect_gcc_options (collect_gcc
, collect_gcc_options
,
1443 &decoded_options_count
);
1445 /* Allocate array for input object files with LTO IL,
1446 and for possible preceding arguments. */
1447 lto_argv
= XNEWVEC (char *, argc
);
1448 ltoobj_argv
= XNEWVEC (char *, argc
);
1450 /* Look at saved options in the IL files. */
1451 for (i
= 1; i
< argc
; ++i
)
1455 off_t file_offset
= 0;
1458 char *filename
= argv
[i
];
1460 if (strncmp (argv
[i
], "-foffload-objects=",
1461 sizeof ("-foffload-objects=") - 1) == 0)
1463 have_offload
= true;
1464 offload_objects_file_name
1465 = argv
[i
] + sizeof ("-foffload-objects=") - 1;
1469 if ((p
= strrchr (argv
[i
], '@'))
1471 && sscanf (p
, "@%li%n", &loffset
, &consumed
) >= 1
1472 && strlen (p
) == (unsigned int) consumed
)
1474 filename
= XNEWVEC (char, p
- argv
[i
] + 1);
1475 memcpy (filename
, argv
[i
], p
- argv
[i
]);
1476 filename
[p
- argv
[i
]] = '\0';
1477 file_offset
= (off_t
) loffset
;
1479 fd
= open (filename
, O_RDONLY
| O_BINARY
);
1480 /* Linker plugin passes -fresolution and -flinker-output options.
1481 -flinker-output is passed only when user did not specify one and thus
1482 we do not need to worry about duplicities with the option handling
1486 lto_argv
[lto_argc
++] = argv
[i
];
1487 if (strcmp (argv
[i
], "-flinker-output=rel") == 0)
1488 linker_output_rel
= true;
1492 if (find_and_merge_options (fd
, file_offset
, LTO_SECTION_NAME_PREFIX
,
1493 decoded_options
, decoded_options_count
,
1494 &fdecoded_options
, &fdecoded_options_count
,
1498 ltoobj_argv
[ltoobj_argc
++] = argv
[i
];
1503 /* Initalize the common arguments for the driver. */
1504 obstack_init (&argv_obstack
);
1505 obstack_ptr_grow (&argv_obstack
, collect_gcc
);
1506 obstack_ptr_grow (&argv_obstack
, "-xlto");
1507 obstack_ptr_grow (&argv_obstack
, "-c");
1509 append_compiler_options (&argv_obstack
, fdecoded_options
,
1510 fdecoded_options_count
);
1511 append_linker_options (&argv_obstack
, decoded_options
, decoded_options_count
);
1513 /* Scan linker driver arguments for things that are of relevance to us. */
1514 for (j
= 1; j
< decoded_options_count
; ++j
)
1516 struct cl_decoded_option
*option
= &decoded_options
[j
];
1517 switch (option
->opt_index
)
1520 linker_output
= option
->arg
;
1523 /* We don't have to distinguish between -save-temps=* and
1524 -save-temps, -dumpdir already carries that
1526 case OPT_save_temps_
:
1527 case OPT_save_temps
:
1535 case OPT_flto_partition_
:
1536 if (strcmp (option
->arg
, "none") == 0)
1537 no_partition
= true;
1541 if (strcmp (option
->arg
, "jobserver") == 0)
1546 else if (strcmp (option
->arg
, "auto") == 0)
1553 parallel
= atoi (option
->arg
);
1560 lto_mode
= LTO_MODE_WHOPR
;
1563 case OPT_flinker_output_
:
1564 linker_output_rel
= !strcmp (option
->arg
, "rel");
1568 /* Recognize -g0. */
1569 skip_debug
= option
->arg
&& !strcmp (option
->arg
, "0");
1573 incoming_dumppfx
= dumppfx
= option
->arg
;
1581 /* Output lto-wrapper invocation command. */
1584 for (i
= 0; i
< argc
; ++i
)
1586 fputs (argv
[i
], stderr
);
1587 fputc (' ', stderr
);
1589 fputc ('\n', stderr
);
1592 if (linker_output_rel
)
1593 no_partition
= true;
1597 lto_mode
= LTO_MODE_LTO
;
1604 const char *jobserver_error
= jobserver_active_p ();
1605 if (jobserver
&& jobserver_error
!= NULL
)
1607 warning (0, jobserver_error
);
1608 /* Fall back to auto parallelism. */
1612 else if (!jobserver
&& jobserver_error
== NULL
)
1619 /* We need make working for a parallel execution. */
1620 if (parallel
&& !make_exists ())
1626 || strcmp (linker_output
, HOST_BIT_BUCKET
) == 0)
1630 const char *obase
= lbasename (linker_output
), *temp
;
1632 /* Strip the executable extension. */
1633 size_t blen
= strlen (obase
), xlen
;
1634 if ((temp
= strrchr (obase
+ 1, '.'))
1635 && (xlen
= strlen (temp
))
1636 && (strcmp (temp
, ".exe") == 0
1637 #if defined(HAVE_TARGET_EXECUTABLE_SUFFIX)
1638 || strcmp (temp
, TARGET_EXECUTABLE_SUFFIX
) == 0
1640 || strcmp (obase
, "a.out") == 0))
1641 dumppfx
= xstrndup (linker_output
,
1642 obase
- linker_output
+ blen
- xlen
+ 1);
1644 dumppfx
= concat (linker_output
, ".", NULL
);
1648 /* If there's no directory component in the dumppfx, add one, so
1649 that, when it is used as -dumpbase, it overrides any occurrence
1650 of -dumpdir that might have been passed in. */
1651 if (!dumppfx
|| lbasename (dumppfx
) == dumppfx
)
1652 dumppfx
= concat (current_dir
, dumppfx
, NULL
);
1654 /* Make sure some -dumpdir is passed, so as to get predictable
1655 -dumpbase overriding semantics. If we got an incoming -dumpdir
1656 argument, we'll pass it on, so don't bother with another one
1658 if (!incoming_dumppfx
)
1660 obstack_ptr_grow (&argv_obstack
, "-dumpdir");
1661 obstack_ptr_grow (&argv_obstack
, "");
1663 obstack_ptr_grow (&argv_obstack
, "-dumpbase");
1665 /* Remember at which point we can scrub args to re-use the commons. */
1666 new_head_argc
= obstack_object_size (&argv_obstack
) / sizeof (void *);
1670 unsigned i
, num_offload_files
;
1671 char **offload_argv
;
1674 f
= fopen (offload_objects_file_name
, "r");
1676 fatal_error (input_location
, "cannot open %s: %m",
1677 offload_objects_file_name
);
1678 if (fscanf (f
, "%u ", &num_offload_files
) != 1)
1679 fatal_error (input_location
, "cannot read %s: %m",
1680 offload_objects_file_name
);
1681 offload_argv
= XCNEWVEC (char *, num_offload_files
);
1683 /* Read names of object files with offload. */
1684 for (i
= 0; i
< num_offload_files
; i
++)
1686 const unsigned piece
= 32;
1687 char *buf
, *filename
= XNEWVEC (char, piece
);
1692 if (!fgets (buf
, piece
, f
))
1694 len
= strlen (filename
);
1695 if (filename
[len
- 1] != '\n')
1697 filename
= XRESIZEVEC (char, filename
, len
+ piece
);
1698 buf
= filename
+ len
;
1701 filename
[len
- 1] = '\0';
1702 offload_argv
[i
] = filename
;
1705 if (offload_argv
[num_offload_files
- 1] == NULL
)
1706 fatal_error (input_location
, "invalid format of %s",
1707 offload_objects_file_name
);
1708 maybe_unlink (offload_objects_file_name
);
1709 offload_objects_file_name
= NULL
;
1711 /* Look at saved offload options in files. */
1712 for (i
= 0; i
< num_offload_files
; i
++)
1717 off_t file_offset
= 0;
1718 char *filename
= offload_argv
[i
];
1720 if ((p
= strrchr (offload_argv
[i
], '@'))
1721 && p
!= offload_argv
[i
]
1722 && sscanf (p
, "@%li%n", &loffset
, &consumed
) >= 1
1723 && strlen (p
) == (unsigned int) consumed
)
1725 filename
= XNEWVEC (char, p
- offload_argv
[i
] + 1);
1726 memcpy (filename
, offload_argv
[i
], p
- offload_argv
[i
]);
1727 filename
[p
- offload_argv
[i
]] = '\0';
1728 file_offset
= (off_t
) loffset
;
1730 fd
= open (filename
, O_RDONLY
| O_BINARY
);
1732 fatal_error (input_location
, "cannot open %s: %m", filename
);
1733 if (!find_and_merge_options (fd
, file_offset
,
1734 OFFLOAD_SECTION_NAME_PREFIX
,
1735 decoded_options
, decoded_options_count
,
1736 &offload_fdecoded_options
,
1737 &offload_fdecoded_options_count
,
1739 fatal_error (input_location
, "cannot read %s: %m", filename
);
1741 if (filename
!= offload_argv
[i
])
1742 XDELETEVEC (filename
);
1745 compile_images_for_offload_targets (num_offload_files
, offload_argv
,
1746 offload_fdecoded_options
,
1747 offload_fdecoded_options_count
,
1749 decoded_options_count
);
1751 free_array_of_ptrs ((void **) offload_argv
, num_offload_files
);
1755 find_crtoffloadtable (save_temps
, dumppfx
);
1756 for (i
= 0; offload_names
[i
]; i
++)
1757 printf ("%s\n", offload_names
[i
]);
1758 free_array_of_ptrs ((void **) offload_names
, i
);
1762 /* If object files contain offload sections, but do not contain LTO sections,
1763 then there is no need to perform a link-time recompilation, i.e.
1764 lto-wrapper is used only for a compilation of offload images. */
1765 if (have_offload
&& !have_lto
)
1768 if (lto_mode
== LTO_MODE_LTO
)
1770 /* -dumpbase argument for LTO. */
1771 flto_out
= concat (dumppfx
, "lto.o", NULL
);
1772 obstack_ptr_grow (&argv_obstack
, flto_out
);
1775 flto_out
= make_temp_file (".lto.o");
1776 obstack_ptr_grow (&argv_obstack
, "-o");
1777 obstack_ptr_grow (&argv_obstack
, flto_out
);
1781 const char *list_option
= "-fltrans-output-list=";
1783 /* -dumpbase argument for WPA. */
1784 char *dumpbase
= concat (dumppfx
, "wpa", NULL
);
1785 obstack_ptr_grow (&argv_obstack
, dumpbase
);
1788 ltrans_output_file
= concat (dumppfx
, "ltrans.out", NULL
);
1790 ltrans_output_file
= make_temp_file (".ltrans.out");
1791 list_option_full
= concat (list_option
, ltrans_output_file
, NULL
);
1792 obstack_ptr_grow (&argv_obstack
, list_option_full
);
1797 fprintf (stderr
, "Using make jobserver\n");
1798 obstack_ptr_grow (&argv_obstack
, xstrdup ("-fwpa=jobserver"));
1800 else if (auto_parallel
)
1803 init_num_threads ();
1804 if (nthreads_var
== 0)
1807 fprintf (stderr
, "LTO parallelism level set to %ld\n",
1809 sprintf (buf
, "-fwpa=%ld", nthreads_var
);
1810 obstack_ptr_grow (&argv_obstack
, xstrdup (buf
));
1812 else if (parallel
> 1)
1815 sprintf (buf
, "-fwpa=%i", parallel
);
1816 obstack_ptr_grow (&argv_obstack
, xstrdup (buf
));
1819 obstack_ptr_grow (&argv_obstack
, "-fwpa");
1822 /* Append input arguments. */
1823 for (i
= 0; i
< lto_argc
; ++i
)
1824 obstack_ptr_grow (&argv_obstack
, lto_argv
[i
]);
1825 /* Append the input objects. */
1826 for (i
= 0; i
< ltoobj_argc
; ++i
)
1827 obstack_ptr_grow (&argv_obstack
, ltoobj_argv
[i
]);
1828 obstack_ptr_grow (&argv_obstack
, NULL
);
1830 new_argv
= XOBFINISH (&argv_obstack
, const char **);
1831 argv_ptr
= &new_argv
[new_head_argc
];
1832 fork_execute (new_argv
[0], CONST_CAST (char **, new_argv
), true,
1835 /* Copy the early generated debug info from the objects to temporary
1836 files and append those to the partial link commandline. */
1838 early_debug_object_names
= NULL
;
1841 early_debug_object_names
= XCNEWVEC (const char *, ltoobj_argc
+ 1);
1842 num_deb_objs
= ltoobj_argc
;
1843 for (i
= 0; i
< ltoobj_argc
; ++i
)
1846 if ((tem
= debug_objcopy (ltoobj_argv
[i
], !linker_output_rel
)))
1848 early_debug_object_names
[i
] = tem
;
1854 if (lto_mode
== LTO_MODE_LTO
)
1856 printf ("%s\n", flto_out
);
1859 for (i
= 0; i
< ltoobj_argc
; ++i
)
1860 if (early_debug_object_names
[i
] != NULL
)
1861 printf ("%s\n", early_debug_object_names
[i
]);
1863 /* These now belong to collect2. */
1866 free (early_debug_object_names
);
1867 early_debug_object_names
= NULL
;
1871 FILE *stream
= fopen (ltrans_output_file
, "r");
1872 FILE *mstream
= NULL
;
1873 struct obstack env_obstack
;
1877 fatal_error (input_location
, "%<fopen%>: %s: %m", ltrans_output_file
);
1879 /* Parse the list of LTRANS inputs from the WPA stage. */
1880 obstack_init (&env_obstack
);
1884 const unsigned piece
= 32;
1885 char *output_name
= NULL
;
1886 char *buf
, *input_name
= (char *)xmalloc (piece
);
1890 if (fscanf (stream
, "%i\n", &priority
) != 1)
1893 fatal_error (input_location
,
1894 "corrupted ltrans output file %s",
1895 ltrans_output_file
);
1899 if (!fgets (buf
, piece
, stream
))
1901 len
= strlen (input_name
);
1902 if (input_name
[len
- 1] != '\n')
1904 input_name
= (char *)xrealloc (input_name
, len
+ piece
);
1905 buf
= input_name
+ len
;
1908 input_name
[len
- 1] = '\0';
1910 if (input_name
[0] == '*')
1911 output_name
= &input_name
[1];
1915 = (int *)xrealloc (ltrans_priorities
, nr
* sizeof (int) * 2);
1916 input_names
= (char **)xrealloc (input_names
, nr
* sizeof (char *));
1917 output_names
= (char **)xrealloc (output_names
, nr
* sizeof (char *));
1918 ltrans_priorities
[(nr
-1)*2] = priority
;
1919 ltrans_priorities
[(nr
-1)*2+1] = nr
-1;
1920 input_names
[nr
-1] = input_name
;
1921 output_names
[nr
-1] = output_name
;
1924 maybe_unlink (ltrans_output_file
);
1925 ltrans_output_file
= NULL
;
1929 makefile
= make_temp_file (".mk");
1930 mstream
= fopen (makefile
, "w");
1931 qsort (ltrans_priorities
, nr
, sizeof (int) * 2, cmp_priority
);
1934 /* Execute the LTRANS stage for each input file (or prepare a
1935 makefile to invoke this in parallel). */
1936 for (i
= 0; i
< nr
; ++i
)
1939 char *input_name
= input_names
[i
];
1940 /* If it's a pass-through file do nothing. */
1941 if (output_names
[i
])
1944 /* Replace the .o suffix with a .ltrans.o suffix and write
1945 the resulting name to the LTRANS output list. */
1946 obstack_grow (&env_obstack
, input_name
, strlen (input_name
) - 2);
1947 obstack_grow (&env_obstack
, ".ltrans.o", sizeof (".ltrans.o"));
1948 output_name
= XOBFINISH (&env_obstack
, char *);
1950 /* Adjust the dumpbase if the linker output file was seen. */
1951 int dumpbase_len
= (strlen (dumppfx
) + sizeof (DUMPBASE_SUFFIX
));
1952 char *dumpbase
= (char *) xmalloc (dumpbase_len
+ 1);
1953 snprintf (dumpbase
, dumpbase_len
, "%sltrans%u.ltrans", dumppfx
, i
);
1954 argv_ptr
[0] = dumpbase
;
1956 argv_ptr
[1] = "-fltrans";
1958 argv_ptr
[3] = output_name
;
1959 argv_ptr
[4] = input_name
;
1963 fprintf (mstream
, "%s:\n\t@%s ", output_name
, new_argv
[0]);
1964 for (j
= 1; new_argv
[j
] != NULL
; ++j
)
1965 fprintf (mstream
, " '%s'", new_argv
[j
]);
1966 fprintf (mstream
, "\n");
1967 /* If we are not preserving the ltrans input files then
1968 truncate them as soon as we have processed it. This
1969 reduces temporary disk-space usage. */
1971 fprintf (mstream
, "\t@-touch -r %s %s.tem > /dev/null 2>&1 "
1972 "&& mv %s.tem %s\n",
1973 input_name
, input_name
, input_name
, input_name
);
1977 char argsuffix
[sizeof (DUMPBASE_SUFFIX
) + 1];
1979 snprintf (argsuffix
, sizeof (DUMPBASE_SUFFIX
),
1980 "ltrans%u.ltrans_args", i
);
1981 fork_execute (new_argv
[0], CONST_CAST (char **, new_argv
),
1982 true, save_temps
? argsuffix
: NULL
);
1983 maybe_unlink (input_name
);
1986 output_names
[i
] = output_name
;
1990 struct pex_obj
*pex
;
1996 for (i
= 0; i
< nr
; ++i
)
1998 int j
= ltrans_priorities
[i
*2 + 1];
1999 fprintf (mstream
, " \\\n\t%s", output_names
[j
]);
2001 fprintf (mstream
, "\n");
2005 /* Avoid passing --jobserver-fd= and similar flags
2006 unless jobserver mode is explicitly enabled. */
2007 putenv (xstrdup ("MAKEFLAGS="));
2008 putenv (xstrdup ("MFLAGS="));
2011 char **make_argv
= buildargv (getenv ("MAKE"));
2014 for (unsigned argc
= 0; make_argv
[argc
]; argc
++)
2015 obstack_ptr_grow (&argv_obstack
, make_argv
[argc
]);
2018 obstack_ptr_grow (&argv_obstack
, "make");
2020 obstack_ptr_grow (&argv_obstack
, "-f");
2021 obstack_ptr_grow (&argv_obstack
, makefile
);
2024 snprintf (jobs
, 31, "-j%ld",
2025 auto_parallel
? nthreads_var
: parallel
);
2026 obstack_ptr_grow (&argv_obstack
, jobs
);
2028 obstack_ptr_grow (&argv_obstack
, "all");
2029 obstack_ptr_grow (&argv_obstack
, NULL
);
2030 new_argv
= XOBFINISH (&argv_obstack
, const char **);
2032 pex
= collect_execute (new_argv
[0], CONST_CAST (char **, new_argv
),
2033 NULL
, NULL
, PEX_SEARCH
, false, NULL
);
2034 do_wait (new_argv
[0], pex
);
2035 freeargv (make_argv
);
2036 maybe_unlink (makefile
);
2038 for (i
= 0; i
< nr
; ++i
)
2039 maybe_unlink (input_names
[i
]);
2041 for (i
= 0; i
< nr
; ++i
)
2043 fputs (output_names
[i
], stdout
);
2044 putc ('\n', stdout
);
2045 free (input_names
[i
]);
2049 for (i
= 0; i
< ltoobj_argc
; ++i
)
2050 if (early_debug_object_names
[i
] != NULL
)
2051 printf ("%s\n", early_debug_object_names
[i
]);
2054 free (ltrans_priorities
);
2055 free (output_names
);
2056 output_names
= NULL
;
2057 free (early_debug_object_names
);
2058 early_debug_object_names
= NULL
;
2060 free (list_option_full
);
2061 obstack_free (&env_obstack
, NULL
);
2066 obstack_free (&argv_obstack
, NULL
);
2073 main (int argc
, char *argv
[])
2077 init_opts_obstack ();
2079 p
= argv
[0] + strlen (argv
[0]);
2080 while (p
!= argv
[0] && !IS_DIR_SEPARATOR (p
[-1]))
2084 xmalloc_set_program_name (progname
);
2086 gcc_init_libintl ();
2088 diagnostic_initialize (global_dc
, 0);
2090 if (atexit (lto_wrapper_cleanup
) != 0)
2091 fatal_error (input_location
, "%<atexit%> failed");
2093 if (signal (SIGINT
, SIG_IGN
) != SIG_IGN
)
2094 signal (SIGINT
, fatal_signal
);
2096 if (signal (SIGHUP
, SIG_IGN
) != SIG_IGN
)
2097 signal (SIGHUP
, fatal_signal
);
2099 if (signal (SIGTERM
, SIG_IGN
) != SIG_IGN
)
2100 signal (SIGTERM
, fatal_signal
);
2102 if (signal (SIGPIPE
, SIG_IGN
) != SIG_IGN
)
2103 signal (SIGPIPE
, fatal_signal
);
2106 /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
2107 receive the signal. A different setting is inheritable */
2108 signal (SIGCHLD
, SIG_DFL
);
2111 /* We may be called with all the arguments stored in some file and
2112 passed with @file. Expand them into argv before processing. */
2113 expandargv (&argc
, &argv
);
2115 run_gcc (argc
, argv
);