c, c++: attribute format on a ctor with a vbase [PR101833, PR47634]
[official-gcc.git] / gcc / lto-wrapper.cc
blob26e06e77be4e0afb2bc3e913062a9c51cab5d205
1 /* Wrapper to call lto. Used by collect2 and the linker plugin.
2 Copyright (C) 2009-2022 Free Software Foundation, Inc.
4 Factored out of collect2 by Rafael Espindola <espindola@google.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 /* This program is passed a gcc, a list of gcc arguments and a list of
24 object files containing IL. It scans the argument list to check if
25 we are in whopr mode or not modifies the arguments and needed and
26 prints a list of output files on stdout.
28 Example:
30 $ lto-wrapper gcc/xgcc -B gcc a.o b.o -o test -flto
32 The above will print something like
33 /tmp/ccwbQ8B2.lto.o
35 If WHOPR is used instead, more than one file might be produced
36 ./ccXj2DTk.lto.ltrans.o
37 ./ccCJuXGv.lto.ltrans.o
40 #include "config.h"
41 #include "system.h"
42 #include "coretypes.h"
43 #include "intl.h"
44 #include "diagnostic.h"
45 #include "obstack.h"
46 #include "opts.h"
47 #include "options.h"
48 #include "simple-object.h"
49 #include "lto-section-names.h"
50 #include "collect-utils.h"
51 #include "opts-diagnostic.h"
53 /* Environment variable, used for passing the names of offload targets from GCC
54 driver to lto-wrapper. */
55 #define OFFLOAD_TARGET_NAMES_ENV "OFFLOAD_TARGET_NAMES"
56 #define OFFLOAD_TARGET_DEFAULT_ENV "OFFLOAD_TARGET_DEFAULT"
58 /* By default there is no special suffix for target executables. */
59 #ifdef TARGET_EXECUTABLE_SUFFIX
60 #define HAVE_TARGET_EXECUTABLE_SUFFIX
61 #else
62 #define TARGET_EXECUTABLE_SUFFIX ""
63 #endif
65 enum lto_mode_d {
66 LTO_MODE_NONE, /* Not doing LTO. */
67 LTO_MODE_LTO, /* Normal LTO. */
68 LTO_MODE_WHOPR /* WHOPR. */
71 /* Current LTO mode. */
72 static enum lto_mode_d lto_mode = LTO_MODE_NONE;
74 static char *ltrans_output_file;
75 static char *flto_out;
76 static unsigned int nr;
77 static int *ltrans_priorities;
78 static char **input_names;
79 static char **output_names;
80 static char **offload_names;
81 static char *offload_objects_file_name;
82 static char *makefile;
83 static unsigned int num_deb_objs;
84 static const char **early_debug_object_names;
85 static bool xassembler_options_error = false;
87 const char tool_name[] = "lto-wrapper";
89 /* Delete tempfiles. Called from utils_cleanup. */
91 void
92 tool_cleanup (bool)
94 unsigned int i;
96 if (ltrans_output_file)
97 maybe_unlink (ltrans_output_file);
98 if (flto_out)
99 maybe_unlink (flto_out);
100 if (offload_objects_file_name)
101 maybe_unlink (offload_objects_file_name);
102 if (makefile)
103 maybe_unlink (makefile);
104 if (early_debug_object_names)
105 for (i = 0; i < num_deb_objs; ++i)
106 if (early_debug_object_names[i])
107 maybe_unlink (early_debug_object_names[i]);
108 for (i = 0; i < nr; ++i)
110 maybe_unlink (input_names[i]);
111 if (output_names[i])
112 maybe_unlink (output_names[i]);
116 static void
117 lto_wrapper_cleanup (void)
119 utils_cleanup (false);
122 /* Unlink a temporary LTRANS file unless requested otherwise. */
124 void
125 maybe_unlink (const char *file)
127 if (!save_temps)
129 if (unlink_if_ordinary (file)
130 && errno != ENOENT)
131 fatal_error (input_location, "deleting LTRANS file %s: %m", file);
133 else if (verbose)
134 fprintf (stderr, "[Leaving LTRANS %s]\n", file);
137 /* Template of LTRANS dumpbase suffix. */
138 #define DUMPBASE_SUFFIX "ltrans18446744073709551615"
140 /* Create decoded options from the COLLECT_GCC and COLLECT_GCC_OPTIONS
141 environment. */
143 static vec<cl_decoded_option>
144 get_options_from_collect_gcc_options (const char *collect_gcc,
145 const char *collect_gcc_options)
147 cl_decoded_option *decoded_options;
148 unsigned int decoded_options_count;
149 struct obstack argv_obstack;
150 const char **argv;
151 int argc;
153 obstack_init (&argv_obstack);
154 obstack_ptr_grow (&argv_obstack, collect_gcc);
156 parse_options_from_collect_gcc_options (collect_gcc_options,
157 &argv_obstack, &argc);
158 argv = XOBFINISH (&argv_obstack, const char **);
160 decode_cmdline_options_to_array (argc, (const char **)argv, CL_DRIVER,
161 &decoded_options, &decoded_options_count);
162 vec<cl_decoded_option> decoded;
163 decoded.create (decoded_options_count);
164 for (unsigned i = 0; i < decoded_options_count; ++i)
165 decoded.quick_push (decoded_options[i]);
166 free (decoded_options);
168 obstack_free (&argv_obstack, NULL);
170 return decoded;
173 /* Find option in OPTIONS based on OPT_INDEX. -1 value is returned
174 if the option is not present. */
176 static int
177 find_option (vec<cl_decoded_option> &options, size_t opt_index)
179 for (unsigned i = 0; i < options.length (); ++i)
180 if (options[i].opt_index == opt_index)
181 return i;
183 return -1;
186 static int
187 find_option (vec<cl_decoded_option> &options, cl_decoded_option *option)
189 return find_option (options, option->opt_index);
192 /* Merge -flto FOPTION into vector of DECODED_OPTIONS. */
194 static void
195 merge_flto_options (vec<cl_decoded_option> &decoded_options,
196 cl_decoded_option *foption)
198 int existing_opt = find_option (decoded_options, foption);
199 if (existing_opt == -1)
200 decoded_options.safe_push (*foption);
201 else
203 if (strcmp (foption->arg, decoded_options[existing_opt].arg) != 0)
205 /* -flto=auto is preferred. */
206 if (strcmp (decoded_options[existing_opt].arg, "auto") == 0)
208 else if (strcmp (foption->arg, "auto") == 0
209 || strcmp (foption->arg, "jobserver") == 0)
210 decoded_options[existing_opt].arg = foption->arg;
211 else if (strcmp (decoded_options[existing_opt].arg,
212 "jobserver") != 0)
214 int n = atoi (foption->arg);
215 int original_n = atoi (decoded_options[existing_opt].arg);
216 if (n > original_n)
217 decoded_options[existing_opt].arg = foption->arg;
223 /* Try to merge and complain about options FDECODED_OPTIONS when applied
224 ontop of DECODED_OPTIONS. */
226 static void
227 merge_and_complain (vec<cl_decoded_option> &decoded_options,
228 vec<cl_decoded_option> fdecoded_options,
229 vec<cl_decoded_option> decoded_cl_options)
231 unsigned int i, j;
232 cl_decoded_option *pic_option = NULL;
233 cl_decoded_option *pie_option = NULL;
234 cl_decoded_option *cf_protection_option = NULL;
236 /* ??? Merge options from files. Most cases can be
237 handled by either unioning or intersecting
238 (for example -fwrapv is a case for unioning,
239 -ffast-math is for intersection). Most complaints
240 about real conflicts between different options can
241 be deferred to the compiler proper. Options that
242 we can neither safely handle by intersection nor
243 unioning would need to be complained about here.
244 Ideally we'd have a flag in the opt files that
245 tells whether to union or intersect or reject.
246 In absence of that it's unclear what a good default is.
247 It's also difficult to get positional handling correct. */
249 /* Look for a -fcf-protection option in the link-time options
250 which overrides any -fcf-protection from the lto sections. */
251 for (i = 0; i < decoded_cl_options.length (); ++i)
253 cl_decoded_option *foption = &decoded_cl_options[i];
254 if (foption->opt_index == OPT_fcf_protection_)
256 cf_protection_option = foption;
260 /* The following does what the old LTO option code did,
261 union all target and a selected set of common options. */
262 for (i = 0; i < fdecoded_options.length (); ++i)
264 cl_decoded_option *foption = &fdecoded_options[i];
265 int existing_opt = find_option (decoded_options, foption);
266 switch (foption->opt_index)
268 case OPT_SPECIAL_unknown:
269 case OPT_SPECIAL_ignore:
270 case OPT_SPECIAL_warn_removed:
271 case OPT_SPECIAL_program_name:
272 case OPT_SPECIAL_input_file:
273 break;
275 default:
276 if (!(cl_options[foption->opt_index].flags & CL_TARGET))
277 break;
279 /* Fallthru. */
280 case OPT_fdiagnostics_show_caret:
281 case OPT_fdiagnostics_show_labels:
282 case OPT_fdiagnostics_show_line_numbers:
283 case OPT_fdiagnostics_show_option:
284 case OPT_fdiagnostics_show_location_:
285 case OPT_fshow_column:
286 case OPT_fcommon:
287 case OPT_fgnu_tm:
288 case OPT_g:
289 /* Do what the old LTO code did - collect exactly one option
290 setting per OPT code, we pick the first we encounter.
291 ??? This doesn't make too much sense, but when it doesn't
292 then we should complain. */
293 if (existing_opt == -1)
294 decoded_options.safe_push (*foption);
295 break;
297 /* Figure out what PIC/PIE level wins and merge the results. */
298 case OPT_fPIC:
299 case OPT_fpic:
300 pic_option = foption;
301 break;
302 case OPT_fPIE:
303 case OPT_fpie:
304 pie_option = foption;
305 break;
307 case OPT_fopenmp:
308 case OPT_fopenacc:
309 /* For selected options we can merge conservatively. */
310 if (existing_opt == -1)
311 decoded_options.safe_push (*foption);
312 /* -fopenmp > -fno-openmp,
313 -fopenacc > -fno-openacc */
314 else if (foption->value > decoded_options[existing_opt].value)
315 decoded_options[existing_opt] = *foption;
316 break;
318 case OPT_fopenacc_dim_:
319 /* Append or check identical. */
320 if (existing_opt == -1)
321 decoded_options.safe_push (*foption);
322 else if (strcmp (decoded_options[existing_opt].arg, foption->arg))
323 fatal_error (input_location,
324 "option %s with different values",
325 foption->orig_option_with_args_text);
326 break;
328 case OPT_fcf_protection_:
329 /* Default to link-time option, else append or check identical. */
330 if (!cf_protection_option
331 || cf_protection_option->value == CF_CHECK)
333 if (existing_opt == -1)
334 decoded_options.safe_push (*foption);
335 else if (decoded_options[existing_opt].value != foption->value)
337 if (cf_protection_option
338 && cf_protection_option->value == CF_CHECK)
339 fatal_error (input_location,
340 "option %qs with mismatching values"
341 " (%s, %s)",
342 "-fcf-protection",
343 decoded_options[existing_opt].arg,
344 foption->arg);
345 else
347 /* Merge and update the -fcf-protection option. */
348 decoded_options[existing_opt].value
349 &= (foption->value & CF_FULL);
350 switch (decoded_options[existing_opt].value)
352 case CF_NONE:
353 decoded_options[existing_opt].arg = "none";
354 break;
355 case CF_BRANCH:
356 decoded_options[existing_opt].arg = "branch";
357 break;
358 case CF_RETURN:
359 decoded_options[existing_opt].arg = "return";
360 break;
361 default:
362 gcc_unreachable ();
367 break;
369 case OPT_O:
370 case OPT_Ofast:
371 case OPT_Og:
372 case OPT_Os:
373 case OPT_Oz:
374 existing_opt = -1;
375 for (j = 0; j < decoded_options.length (); ++j)
376 if (decoded_options[j].opt_index == OPT_O
377 || decoded_options[j].opt_index == OPT_Ofast
378 || decoded_options[j].opt_index == OPT_Og
379 || decoded_options[j].opt_index == OPT_Os
380 || decoded_options[j].opt_index == OPT_Oz)
382 existing_opt = j;
383 break;
385 if (existing_opt == -1)
386 decoded_options.safe_push (*foption);
387 else if (decoded_options[existing_opt].opt_index == foption->opt_index
388 && foption->opt_index != OPT_O)
389 /* Exact same options get merged. */
391 else
393 /* For mismatched option kinds preserve the optimization
394 level only, thus merge it as -On. This also handles
395 merging of same optimization level -On. */
396 int level = 0;
397 switch (foption->opt_index)
399 case OPT_O:
400 if (foption->arg[0] == '\0')
401 level = MAX (level, 1);
402 else
403 level = MAX (level, atoi (foption->arg));
404 break;
405 case OPT_Ofast:
406 level = MAX (level, 3);
407 break;
408 case OPT_Og:
409 level = MAX (level, 1);
410 break;
411 case OPT_Os:
412 case OPT_Oz:
413 level = MAX (level, 2);
414 break;
415 default:
416 gcc_unreachable ();
418 switch (decoded_options[existing_opt].opt_index)
420 case OPT_O:
421 if (decoded_options[existing_opt].arg[0] == '\0')
422 level = MAX (level, 1);
423 else
424 level = MAX (level,
425 atoi (decoded_options[existing_opt].arg));
426 break;
427 case OPT_Ofast:
428 level = MAX (level, 3);
429 break;
430 case OPT_Og:
431 level = MAX (level, 1);
432 break;
433 case OPT_Os:
434 case OPT_Oz:
435 level = MAX (level, 2);
436 break;
437 default:
438 gcc_unreachable ();
440 decoded_options[existing_opt].opt_index = OPT_O;
441 char *tem;
442 tem = xasprintf ("-O%d", level);
443 decoded_options[existing_opt].arg = &tem[2];
444 decoded_options[existing_opt].canonical_option[0] = tem;
445 decoded_options[existing_opt].value = 1;
447 break;
450 case OPT_foffload_abi_:
451 if (existing_opt == -1)
452 decoded_options.safe_push (*foption);
453 else if (foption->value != decoded_options[existing_opt].value)
454 fatal_error (input_location,
455 "option %s not used consistently in all LTO input"
456 " files", foption->orig_option_with_args_text);
457 break;
460 case OPT_foffload_options_:
461 decoded_options.safe_push (*foption);
462 break;
464 case OPT_flto_:
465 merge_flto_options (decoded_options, foption);
466 break;
470 /* Merge PIC options:
471 -fPIC + -fpic = -fpic
472 -fPIC + -fno-pic = -fno-pic
473 -fpic/-fPIC + nothing = nothing.
474 It is a common mistake to mix few -fPIC compiled objects into otherwise
475 non-PIC code. We do not want to build everything with PIC then.
477 Similarly we merge PIE options, however in addition we keep
478 -fPIC + -fPIE = -fPIE
479 -fpic + -fPIE = -fpie
480 -fPIC/-fpic + -fpie = -fpie
482 It would be good to warn on mismatches, but it is bit hard to do as
483 we do not know what nothing translates to. */
485 for (unsigned int j = 0; j < decoded_options.length ();)
486 if (decoded_options[j].opt_index == OPT_fPIC
487 || decoded_options[j].opt_index == OPT_fpic)
489 /* -fno-pic in one unit implies -fno-pic everywhere. */
490 if (decoded_options[j].value == 0)
491 j++;
492 /* If we have no pic option or merge in -fno-pic, we still may turn
493 existing pic/PIC mode into pie/PIE if -fpie/-fPIE is present. */
494 else if ((pic_option && pic_option->value == 0)
495 || !pic_option)
497 if (pie_option)
499 bool big = decoded_options[j].opt_index == OPT_fPIC
500 && pie_option->opt_index == OPT_fPIE;
501 decoded_options[j].opt_index = big ? OPT_fPIE : OPT_fpie;
502 if (pie_option->value)
503 decoded_options[j].canonical_option[0]
504 = big ? "-fPIE" : "-fpie";
505 else
506 decoded_options[j].canonical_option[0] = "-fno-pie";
507 decoded_options[j].value = pie_option->value;
508 j++;
510 else if (pic_option)
512 decoded_options[j] = *pic_option;
513 j++;
515 /* We do not know if target defaults to pic or not, so just remove
516 option if it is missing in one unit but enabled in other. */
517 else
518 decoded_options.ordered_remove (j);
520 else if (pic_option->opt_index == OPT_fpic
521 && decoded_options[j].opt_index == OPT_fPIC)
523 decoded_options[j] = *pic_option;
524 j++;
526 else
527 j++;
529 else if (decoded_options[j].opt_index == OPT_fPIE
530 || decoded_options[j].opt_index == OPT_fpie)
532 /* -fno-pie in one unit implies -fno-pie everywhere. */
533 if (decoded_options[j].value == 0)
534 j++;
535 /* If we have no pie option or merge in -fno-pie, we still preserve
536 PIE/pie if pic/PIC is present. */
537 else if ((pie_option && pie_option->value == 0)
538 || !pie_option)
540 /* If -fPIC/-fpic is given, merge it with -fPIE/-fpie. */
541 if (pic_option)
543 if (pic_option->opt_index == OPT_fpic
544 && decoded_options[j].opt_index == OPT_fPIE)
546 decoded_options[j].opt_index = OPT_fpie;
547 decoded_options[j].canonical_option[0]
548 = pic_option->value ? "-fpie" : "-fno-pie";
550 else if (!pic_option->value)
551 decoded_options[j].canonical_option[0] = "-fno-pie";
552 decoded_options[j].value = pic_option->value;
553 j++;
555 else if (pie_option)
557 decoded_options[j] = *pie_option;
558 j++;
560 /* Because we always append pic/PIE options this code path should
561 not happen unless the LTO object was built by old lto1 which
562 did not contain that logic yet. */
563 else
564 decoded_options.ordered_remove (j);
566 else if (pie_option->opt_index == OPT_fpie
567 && decoded_options[j].opt_index == OPT_fPIE)
569 decoded_options[j] = *pie_option;
570 j++;
572 else
573 j++;
575 else
576 j++;
578 if (!xassembler_options_error)
579 for (i = j = 0; ; i++, j++)
581 int existing_opt_index
582 = find_option (decoded_options, OPT_Xassembler);
583 int existing_opt2_index
584 = find_option (fdecoded_options, OPT_Xassembler);
586 cl_decoded_option *existing_opt = NULL;
587 cl_decoded_option *existing_opt2 = NULL;
588 if (existing_opt_index != -1)
589 existing_opt = &decoded_options[existing_opt_index];
590 if (existing_opt2_index != -1)
591 existing_opt2 = &fdecoded_options[existing_opt2_index];
593 if (existing_opt == NULL && existing_opt2 == NULL)
594 break;
595 else if (existing_opt != NULL && existing_opt2 == NULL)
597 warning (0, "Extra option to %<-Xassembler%>: %s,"
598 " dropping all %<-Xassembler%> and %<-Wa%> options.",
599 existing_opt->arg);
600 xassembler_options_error = true;
601 break;
603 else if (existing_opt == NULL && existing_opt2 != NULL)
605 warning (0, "Extra option to %<-Xassembler%>: %s,"
606 " dropping all %<-Xassembler%> and %<-Wa%> options.",
607 existing_opt2->arg);
608 xassembler_options_error = true;
609 break;
611 else if (strcmp (existing_opt->arg, existing_opt2->arg) != 0)
613 warning (0, "Options to %<-Xassembler%> do not match: %s, %s,"
614 " dropping all %<-Xassembler%> and %<-Wa%> options.",
615 existing_opt->arg, existing_opt2->arg);
616 xassembler_options_error = true;
617 break;
622 /* Auxiliary function that frees elements of PTR and PTR itself.
623 N is number of elements to be freed. If PTR is NULL, nothing is freed.
624 If an element is NULL, subsequent elements are not freed. */
626 static void **
627 free_array_of_ptrs (void **ptr, unsigned n)
629 if (!ptr)
630 return NULL;
631 for (unsigned i = 0; i < n; i++)
633 if (!ptr[i])
634 break;
635 free (ptr[i]);
637 free (ptr);
638 return NULL;
641 /* Parse STR, saving found tokens into PVALUES and return their number.
642 Tokens are assumed to be delimited by ':'. If APPEND is non-null,
643 append it to every token we find. */
645 static unsigned
646 parse_env_var (const char *str, char ***pvalues, const char *append)
648 const char *curval, *nextval;
649 char **values;
650 unsigned num = 1, i;
652 curval = strchr (str, ':');
653 while (curval)
655 num++;
656 curval = strchr (curval + 1, ':');
659 values = (char**) xmalloc (num * sizeof (char*));
660 curval = str;
661 nextval = strchr (curval, ':');
662 if (nextval == NULL)
663 nextval = strchr (curval, '\0');
665 int append_len = append ? strlen (append) : 0;
666 for (i = 0; i < num; i++)
668 int l = nextval - curval;
669 values[i] = (char*) xmalloc (l + 1 + append_len);
670 memcpy (values[i], curval, l);
671 values[i][l] = 0;
672 if (append)
673 strcat (values[i], append);
674 curval = nextval + 1;
675 nextval = strchr (curval, ':');
676 if (nextval == NULL)
677 nextval = strchr (curval, '\0');
679 *pvalues = values;
680 return num;
683 /* Append options OPTS from lto or offload_lto sections to ARGV_OBSTACK. */
685 static void
686 append_compiler_options (obstack *argv_obstack, vec<cl_decoded_option> opts)
688 /* Append compiler driver arguments as far as they were merged. */
689 for (unsigned int j = 1; j < opts.length (); ++j)
691 cl_decoded_option *option = &opts[j];
693 /* File options have been properly filtered by lto-opts.cc. */
694 switch (option->opt_index)
696 /* Drop arguments that we want to take from the link line. */
697 case OPT_flto_:
698 case OPT_flto:
699 case OPT_flto_partition_:
700 continue;
702 default:
703 break;
706 /* For now do what the original LTO option code was doing - pass
707 on any CL_TARGET flag and a few selected others. */
708 switch (option->opt_index)
710 case OPT_fdiagnostics_show_caret:
711 case OPT_fdiagnostics_show_labels:
712 case OPT_fdiagnostics_show_line_numbers:
713 case OPT_fdiagnostics_show_option:
714 case OPT_fdiagnostics_show_location_:
715 case OPT_fshow_column:
716 case OPT_fPIC:
717 case OPT_fpic:
718 case OPT_fPIE:
719 case OPT_fpie:
720 case OPT_fcommon:
721 case OPT_fgnu_tm:
722 case OPT_fopenmp:
723 case OPT_fopenacc:
724 case OPT_fopenacc_dim_:
725 case OPT_foffload_abi_:
726 case OPT_fcf_protection_:
727 case OPT_g:
728 case OPT_O:
729 case OPT_Ofast:
730 case OPT_Og:
731 case OPT_Os:
732 case OPT_Oz:
733 break;
735 case OPT_Xassembler:
736 /* When we detected a mismatch in assembler options between
737 the input TU's fall back to previous behavior of ignoring them. */
738 if (xassembler_options_error)
739 continue;
740 break;
742 default:
743 if (!(cl_options[option->opt_index].flags & CL_TARGET))
744 continue;
747 /* Pass the option on. */
748 for (unsigned int i = 0; i < option->canonical_option_num_elements; ++i)
749 obstack_ptr_grow (argv_obstack, option->canonical_option[i]);
753 /* Append diag options in OPTS to ARGV_OBSTACK. */
755 static void
756 append_diag_options (obstack *argv_obstack, vec<cl_decoded_option> opts)
758 /* Append compiler driver arguments as far as they were merged. */
759 for (unsigned int j = 1; j < opts.length (); ++j)
761 cl_decoded_option *option = &opts[j];
763 switch (option->opt_index)
765 case OPT_fdiagnostics_color_:
766 case OPT_fdiagnostics_format_:
767 case OPT_fdiagnostics_show_caret:
768 case OPT_fdiagnostics_show_labels:
769 case OPT_fdiagnostics_show_line_numbers:
770 case OPT_fdiagnostics_show_option:
771 case OPT_fdiagnostics_show_location_:
772 case OPT_fshow_column:
773 break;
774 default:
775 continue;
778 /* Pass the option on. */
779 for (unsigned int i = 0; i < option->canonical_option_num_elements; ++i)
780 obstack_ptr_grow (argv_obstack, option->canonical_option[i]);
785 /* Append linker options OPTS to ARGV_OBSTACK. */
787 static void
788 append_linker_options (obstack *argv_obstack, vec<cl_decoded_option> opts)
790 /* Append linker driver arguments. Compiler options from the linker
791 driver arguments will override / merge with those from the compiler. */
792 for (unsigned int j = 1; j < opts.length (); ++j)
794 cl_decoded_option *option = &opts[j];
796 /* Do not pass on frontend specific flags not suitable for lto. */
797 if (!(cl_options[option->opt_index].flags
798 & (CL_COMMON|CL_TARGET|CL_DRIVER|CL_LTO)))
799 continue;
801 switch (option->opt_index)
803 case OPT_o:
804 case OPT_flto_:
805 case OPT_flto:
806 /* We've handled these LTO options, do not pass them on. */
807 continue;
809 case OPT_fopenmp:
810 case OPT_fopenacc:
811 /* Ignore -fno-XXX form of these options, as otherwise
812 corresponding builtins will not be enabled. */
813 if (option->value == 0)
814 continue;
815 break;
817 default:
818 break;
821 /* Pass the option on. */
822 for (unsigned int i = 0; i < option->canonical_option_num_elements; ++i)
823 obstack_ptr_grow (argv_obstack, option->canonical_option[i]);
827 /* Extract options for TARGET offload compiler from OPTIONS and append
828 them to ARGV_OBSTACK. */
830 static void
831 append_offload_options (obstack *argv_obstack, const char *target,
832 vec<cl_decoded_option> options)
834 for (unsigned i = 0; i < options.length (); i++)
836 const char *cur, *next, *opts;
837 char **argv;
838 unsigned argc;
839 cl_decoded_option *option = &options[i];
841 if (option->opt_index != OPT_foffload_options_)
842 continue;
844 /* If option argument starts with '-' then no target is specified. That
845 means offload options are specified for all targets, so we need to
846 append them. */
847 if (option->arg[0] == '-')
848 opts = option->arg;
849 else
851 opts = strchr (option->arg, '=');
852 gcc_assert (opts);
853 cur = option->arg;
855 while (cur < opts)
857 next = strchr (cur, ',');
858 if (next == NULL)
859 next = opts;
860 next = (next > opts) ? opts : next;
862 /* Are we looking for this offload target? */
863 if (strlen (target) == (size_t) (next - cur)
864 && strncmp (target, cur, next - cur) == 0)
865 break;
867 /* Skip the comma or equal sign. */
868 cur = next + 1;
871 if (cur >= opts)
872 continue;
874 opts++;
877 argv = buildargv (opts);
878 for (argc = 0; argv[argc]; argc++)
879 obstack_ptr_grow (argv_obstack, argv[argc]);
883 /* Check whether NAME can be accessed in MODE. This is like access,
884 except that it never considers directories to be executable. */
886 static int
887 access_check (const char *name, int mode)
889 if (mode == X_OK)
891 struct stat st;
893 if (stat (name, &st) < 0
894 || S_ISDIR (st.st_mode))
895 return -1;
898 return access (name, mode);
901 /* Prepare a target image for offload TARGET, using mkoffload tool from
902 COMPILER_PATH. Return the name of the resultant object file. */
904 static char *
905 compile_offload_image (const char *target, const char *compiler_path,
906 unsigned in_argc, char *in_argv[],
907 vec<cl_decoded_option> compiler_opts,
908 vec<cl_decoded_option> linker_opts)
910 char *filename = NULL;
911 char *dumpbase;
912 char **argv;
913 char *suffix
914 = XALLOCAVEC (char, sizeof ("/accel//mkoffload") + strlen (target));
915 strcpy (suffix, "/accel/");
916 strcat (suffix, target);
917 strcat (suffix, "/mkoffload");
919 char **paths = NULL;
920 unsigned n_paths = parse_env_var (compiler_path, &paths, suffix);
922 const char *compiler = NULL;
923 for (unsigned i = 0; i < n_paths; i++)
924 if (access_check (paths[i], X_OK) == 0)
926 compiler = paths[i];
927 break;
929 #if OFFLOAD_DEFAULTED
930 if (!compiler && getenv (OFFLOAD_TARGET_DEFAULT_ENV))
932 free_array_of_ptrs ((void **) paths, n_paths);
933 return NULL;
935 #endif
937 if (!compiler)
938 fatal_error (input_location,
939 "could not find %s in %s (consider using %<-B%>)",
940 suffix + 1, compiler_path);
942 dumpbase = concat (dumppfx, "x", target, NULL);
944 /* Generate temporary output file name. */
945 if (save_temps)
946 filename = concat (dumpbase, ".o", NULL);
947 else
948 filename = make_temp_file (".target.o");
950 struct obstack argv_obstack;
951 obstack_init (&argv_obstack);
952 obstack_ptr_grow (&argv_obstack, compiler);
953 if (save_temps)
954 obstack_ptr_grow (&argv_obstack, "-save-temps");
955 if (verbose)
956 obstack_ptr_grow (&argv_obstack, "-v");
957 obstack_ptr_grow (&argv_obstack, "-o");
958 obstack_ptr_grow (&argv_obstack, filename);
960 /* Append names of input object files. */
961 for (unsigned i = 0; i < in_argc; i++)
962 obstack_ptr_grow (&argv_obstack, in_argv[i]);
964 /* Append options from offload_lto sections. */
965 append_compiler_options (&argv_obstack, compiler_opts);
966 append_diag_options (&argv_obstack, linker_opts);
968 obstack_ptr_grow (&argv_obstack, "-dumpbase");
969 obstack_ptr_grow (&argv_obstack, dumpbase);
971 /* Append options specified by -foffload last. In case of conflicting
972 options we expect offload compiler to choose the latest. */
973 append_offload_options (&argv_obstack, target, compiler_opts);
974 append_offload_options (&argv_obstack, target, linker_opts);
976 obstack_ptr_grow (&argv_obstack, NULL);
977 argv = XOBFINISH (&argv_obstack, char **);
978 fork_execute (argv[0], argv, true, "offload_args");
979 obstack_free (&argv_obstack, NULL);
981 free_array_of_ptrs ((void **) paths, n_paths);
982 return filename;
986 /* The main routine dealing with offloading.
987 The routine builds a target image for each offload target. IN_ARGC and
988 IN_ARGV specify options and input object files. As all of them could contain
989 target sections, we pass them all to target compilers. */
991 static void
992 compile_images_for_offload_targets (unsigned in_argc, char *in_argv[],
993 vec<cl_decoded_option> compiler_opts,
994 vec<cl_decoded_option> linker_opts)
996 char **names = NULL;
997 const char *target_names = getenv (OFFLOAD_TARGET_NAMES_ENV);
998 if (!target_names)
999 return;
1000 unsigned num_targets = parse_env_var (target_names, &names, NULL);
1001 int next_name_entry = 0;
1003 const char *compiler_path = getenv ("COMPILER_PATH");
1004 if (!compiler_path)
1005 goto out;
1007 /* Prepare an image for each target and save the name of the resultant object
1008 file to the OFFLOAD_NAMES array. It is terminated by a NULL entry. */
1009 offload_names = XCNEWVEC (char *, num_targets + 1);
1010 for (unsigned i = 0; i < num_targets; i++)
1012 offload_names[next_name_entry]
1013 = compile_offload_image (names[i], compiler_path, in_argc, in_argv,
1014 compiler_opts, linker_opts);
1015 if (!offload_names[next_name_entry])
1016 #if OFFLOAD_DEFAULTED
1017 continue;
1018 #else
1019 fatal_error (input_location,
1020 "problem with building target image for %s", names[i]);
1021 #endif
1022 next_name_entry++;
1025 #if OFFLOAD_DEFAULTED
1026 if (next_name_entry == 0)
1028 free (offload_names);
1029 offload_names = NULL;
1031 #endif
1033 out:
1034 free_array_of_ptrs ((void **) names, num_targets);
1037 /* Copy a file from SRC to DEST. */
1039 static void
1040 copy_file (const char *dest, const char *src)
1042 FILE *d = fopen (dest, "wb");
1043 FILE *s = fopen (src, "rb");
1044 char buffer[512];
1045 while (!feof (s))
1047 size_t len = fread (buffer, 1, 512, s);
1048 if (ferror (s) != 0)
1049 fatal_error (input_location, "reading input file");
1050 if (len > 0)
1052 fwrite (buffer, 1, len, d);
1053 if (ferror (d) != 0)
1054 fatal_error (input_location, "writing output file");
1057 fclose (d);
1058 fclose (s);
1061 /* Find the crtoffloadtable.o file in LIBRARY_PATH, make copy and pass name of
1062 the copy to the linker. */
1064 static void
1065 find_crtoffloadtable (int save_temps, const char *dumppfx)
1067 char **paths = NULL;
1068 const char *library_path = getenv ("LIBRARY_PATH");
1069 if (!library_path)
1070 return;
1071 unsigned n_paths = parse_env_var (library_path, &paths, "/crtoffloadtable.o");
1073 unsigned i;
1074 for (i = 0; i < n_paths; i++)
1075 if (access_check (paths[i], R_OK) == 0)
1077 /* The linker will delete the filename we give it, so make a copy. */
1078 char *crtoffloadtable;
1079 if (!save_temps)
1080 crtoffloadtable = make_temp_file (".crtoffloadtable.o");
1081 else
1082 crtoffloadtable = concat (dumppfx, "crtoffloadtable.o", NULL);
1083 copy_file (crtoffloadtable, paths[i]);
1084 printf ("%s\n", crtoffloadtable);
1085 XDELETEVEC (crtoffloadtable);
1086 break;
1088 if (i == n_paths)
1089 fatal_error (input_location,
1090 "installation error, cannot find %<crtoffloadtable.o%>");
1092 free_array_of_ptrs ((void **) paths, n_paths);
1095 /* A subroutine of run_gcc. Examine the open file FD for lto sections with
1096 name prefix PREFIX, at FILE_OFFSET, and store any options we find in OPTS.
1097 Return true if we found a matching section, false
1098 otherwise. COLLECT_GCC holds the value of the environment variable with
1099 the same name. */
1101 static bool
1102 find_and_merge_options (int fd, off_t file_offset, const char *prefix,
1103 vec<cl_decoded_option> decoded_cl_options,
1104 vec<cl_decoded_option> *opts, const char *collect_gcc)
1106 off_t offset, length;
1107 char *data;
1108 char *fopts;
1109 const char *errmsg;
1110 int err;
1111 vec<cl_decoded_option> fdecoded_options;
1113 simple_object_read *sobj;
1114 sobj = simple_object_start_read (fd, file_offset, "__GNU_LTO",
1115 &errmsg, &err);
1116 if (!sobj)
1117 return false;
1119 char *secname = XALLOCAVEC (char, strlen (prefix) + sizeof (".opts"));
1120 strcpy (secname, prefix);
1121 strcat (secname, ".opts");
1122 if (!simple_object_find_section (sobj, secname, &offset, &length,
1123 &errmsg, &err))
1125 simple_object_release_read (sobj);
1126 return false;
1129 lseek (fd, file_offset + offset, SEEK_SET);
1130 data = (char *)xmalloc (length);
1131 read (fd, data, length);
1132 fopts = data;
1133 bool first = true;
1136 vec<cl_decoded_option> f2decoded_options
1137 = get_options_from_collect_gcc_options (collect_gcc, fopts);
1138 if (first)
1140 fdecoded_options = f2decoded_options;
1141 first = false;
1143 else
1144 merge_and_complain (fdecoded_options, f2decoded_options,
1145 decoded_cl_options);
1147 fopts += strlen (fopts) + 1;
1149 while (fopts - data < length);
1151 free (data);
1152 simple_object_release_read (sobj);
1153 *opts = fdecoded_options;
1154 return true;
1157 /* Copy early debug info sections from INFILE to a new file whose name
1158 is returned. Return NULL on error. */
1160 const char *
1161 debug_objcopy (const char *infile, bool rename)
1163 char *outfile;
1164 const char *errmsg;
1165 int err;
1167 const char *p;
1168 const char *orig_infile = infile;
1169 off_t inoff = 0;
1170 long loffset;
1171 int consumed;
1172 if ((p = strrchr (infile, '@'))
1173 && p != infile
1174 && sscanf (p, "@%li%n", &loffset, &consumed) >= 1
1175 && strlen (p) == (unsigned int) consumed)
1177 char *fname = xstrdup (infile);
1178 fname[p - infile] = '\0';
1179 infile = fname;
1180 inoff = (off_t) loffset;
1182 int infd = open (infile, O_RDONLY | O_BINARY);
1183 if (infd == -1)
1184 return NULL;
1185 simple_object_read *inobj = simple_object_start_read (infd, inoff,
1186 "__GNU_LTO",
1187 &errmsg, &err);
1188 if (!inobj)
1189 return NULL;
1191 off_t off, len;
1192 if (simple_object_find_section (inobj, ".gnu.debuglto_.debug_info",
1193 &off, &len, &errmsg, &err) != 1)
1195 if (errmsg)
1196 fatal_error (0, "%s: %s", errmsg, xstrerror (err));
1198 simple_object_release_read (inobj);
1199 close (infd);
1200 return NULL;
1203 if (save_temps)
1204 outfile = concat (orig_infile, ".debug.temp.o", NULL);
1205 else
1206 outfile = make_temp_file (".debug.temp.o");
1207 errmsg = simple_object_copy_lto_debug_sections (inobj, outfile, &err, rename);
1208 if (errmsg)
1210 unlink_if_ordinary (outfile);
1211 fatal_error (0, "%s: %s", errmsg, xstrerror (err));
1214 simple_object_release_read (inobj);
1215 close (infd);
1217 return outfile;
1220 /* Helper for qsort: compare priorities for parallel compilation. */
1223 cmp_priority (const void *a, const void *b)
1225 return *((const int *)b)-*((const int *)a);
1228 /* Number of CPUs that can be used for parallel LTRANS phase. */
1230 static unsigned long nthreads_var = 0;
1232 #ifdef HAVE_PTHREAD_AFFINITY_NP
1233 unsigned long cpuset_size;
1234 static unsigned long get_cpuset_size;
1235 cpu_set_t *cpusetp;
1237 unsigned long
1238 static cpuset_popcount (unsigned long cpusetsize, cpu_set_t *cpusetp)
1240 #ifdef CPU_COUNT_S
1241 /* glibc 2.7 and above provide a macro for this. */
1242 return CPU_COUNT_S (cpusetsize, cpusetp);
1243 #else
1244 #ifdef CPU_COUNT
1245 if (cpusetsize == sizeof (cpu_set_t))
1246 /* glibc 2.6 and above provide a macro for this. */
1247 return CPU_COUNT (cpusetp);
1248 #endif
1249 size_t i;
1250 unsigned long ret = 0;
1251 STATIC_ASSERT (sizeof (cpusetp->__bits[0]) == sizeof (unsigned long int));
1252 for (i = 0; i < cpusetsize / sizeof (cpusetp->__bits[0]); i++)
1254 unsigned long int mask = cpusetp->__bits[i];
1255 if (mask == 0)
1256 continue;
1257 ret += __builtin_popcountl (mask);
1259 return ret;
1260 #endif
1262 #endif
1264 /* At startup, determine the default number of threads. It would seem
1265 this should be related to the number of cpus online. */
1267 static void
1268 init_num_threads (void)
1270 #ifdef HAVE_PTHREAD_AFFINITY_NP
1271 #if defined (_SC_NPROCESSORS_CONF) && defined (CPU_ALLOC_SIZE)
1272 cpuset_size = sysconf (_SC_NPROCESSORS_CONF);
1273 cpuset_size = CPU_ALLOC_SIZE (cpuset_size);
1274 #else
1275 cpuset_size = sizeof (cpu_set_t);
1276 #endif
1278 cpusetp = (cpu_set_t *) xmalloc (gomp_cpuset_size);
1281 int ret = pthread_getaffinity_np (pthread_self (), gomp_cpuset_size,
1282 cpusetp);
1283 if (ret == 0)
1285 /* Count only the CPUs this process can use. */
1286 nthreads_var = cpuset_popcount (cpuset_size, cpusetp);
1287 if (nthreads_var == 0)
1288 break;
1289 get_cpuset_size = cpuset_size;
1290 #ifdef CPU_ALLOC_SIZE
1291 unsigned long i;
1292 for (i = cpuset_size * 8; i; i--)
1293 if (CPU_ISSET_S (i - 1, cpuset_size, cpusetp))
1294 break;
1295 cpuset_size = CPU_ALLOC_SIZE (i);
1296 #endif
1297 return;
1299 if (ret != EINVAL)
1300 break;
1301 #ifdef CPU_ALLOC_SIZE
1302 if (cpuset_size < sizeof (cpu_set_t))
1303 cpuset_size = sizeof (cpu_set_t);
1304 else
1305 cpuset_size = cpuset_size * 2;
1306 if (cpuset_size < 8 * sizeof (cpu_set_t))
1307 cpusetp
1308 = (cpu_set_t *) realloc (cpusetp, cpuset_size);
1309 else
1311 /* Avoid fatal if too large memory allocation would be
1312 requested, e.g. kernel returning EINVAL all the time. */
1313 void *p = realloc (cpusetp, cpuset_size);
1314 if (p == NULL)
1315 break;
1316 cpusetp = (cpu_set_t *) p;
1318 #else
1319 break;
1320 #endif
1322 while (1);
1323 cpuset_size = 0;
1324 nthreads_var = 1;
1325 free (cpusetp);
1326 cpusetp = NULL;
1327 #endif
1328 #ifdef _SC_NPROCESSORS_ONLN
1329 nthreads_var = sysconf (_SC_NPROCESSORS_ONLN);
1330 #endif
1333 /* Test and return reason why a jobserver cannot be detected. */
1335 static const char *
1336 jobserver_active_p (void)
1338 #define JS_PREFIX "jobserver is not available: "
1339 #define JS_NEEDLE "--jobserver-auth="
1341 const char *makeflags = getenv ("MAKEFLAGS");
1342 if (makeflags == NULL)
1343 return JS_PREFIX "%<MAKEFLAGS%> environment variable is unset";
1345 const char *n = strstr (makeflags, JS_NEEDLE);
1346 if (n == NULL)
1347 return JS_PREFIX "%<" JS_NEEDLE "%> is not present in %<MAKEFLAGS%>";
1349 int rfd = -1;
1350 int wfd = -1;
1352 if (sscanf (n + strlen (JS_NEEDLE), "%d,%d", &rfd, &wfd) == 2
1353 && rfd > 0
1354 && wfd > 0
1355 && is_valid_fd (rfd)
1356 && is_valid_fd (wfd))
1357 return NULL;
1358 else
1359 return JS_PREFIX "cannot access %<" JS_NEEDLE "%> file descriptors";
1362 /* Print link to -flto documentation with a hint message. */
1364 void
1365 print_lto_docs_link ()
1367 bool print_url = global_dc->printer->url_format != URL_FORMAT_NONE;
1368 const char *url = global_dc->get_option_url (global_dc, OPT_flto);
1370 pretty_printer pp;
1371 pp.url_format = URL_FORMAT_DEFAULT;
1372 pp_string (&pp, "see the ");
1373 if (print_url)
1374 pp_begin_url (&pp, url);
1375 pp_string (&pp, "%<-flto%> option documentation");
1376 if (print_url)
1377 pp_end_url (&pp);
1378 pp_string (&pp, " for more information");
1379 inform (UNKNOWN_LOCATION, pp_formatted_text (&pp));
1382 /* Test that a make command is present and working, return true if so. */
1384 static bool
1385 make_exists (void)
1387 const char *make = "make";
1388 char **make_argv = buildargv (getenv ("MAKE"));
1389 if (make_argv)
1390 make = make_argv[0];
1391 const char *make_args[] = {make, "--version", NULL};
1393 int exit_status = 0;
1394 int err = 0;
1395 const char *errmsg
1396 = pex_one (PEX_SEARCH, make_args[0], CONST_CAST (char **, make_args),
1397 "make", NULL, NULL, &exit_status, &err);
1398 freeargv (make_argv);
1399 return errmsg == NULL && exit_status == 0 && err == 0;
1402 /* Execute gcc. ARGC is the number of arguments. ARGV contains the arguments. */
1404 static void
1405 run_gcc (unsigned argc, char *argv[])
1407 unsigned i, j;
1408 const char **new_argv;
1409 const char **argv_ptr;
1410 char *list_option_full = NULL;
1411 const char *linker_output = NULL;
1412 const char *collect_gcc;
1413 char *collect_gcc_options;
1414 int parallel = 0;
1415 int jobserver = 0;
1416 bool jobserver_requested = false;
1417 int auto_parallel = 0;
1418 bool no_partition = false;
1419 const char *jobserver_error = NULL;
1420 vec<cl_decoded_option> fdecoded_options;
1421 fdecoded_options.create (16);
1422 vec<cl_decoded_option> offload_fdecoded_options = vNULL;
1423 struct obstack argv_obstack;
1424 int new_head_argc;
1425 bool have_lto = false;
1426 bool have_offload = false;
1427 unsigned lto_argc = 0, ltoobj_argc = 0;
1428 char **lto_argv, **ltoobj_argv;
1429 bool linker_output_rel = false;
1430 bool skip_debug = false;
1431 const char *incoming_dumppfx = dumppfx = NULL;
1432 static char current_dir[] = { '.', DIR_SEPARATOR, '\0' };
1434 /* Get the driver and options. */
1435 collect_gcc = getenv ("COLLECT_GCC");
1436 if (!collect_gcc)
1437 fatal_error (input_location,
1438 "environment variable %<COLLECT_GCC%> must be set");
1439 collect_gcc_options = getenv ("COLLECT_GCC_OPTIONS");
1440 if (!collect_gcc_options)
1441 fatal_error (input_location,
1442 "environment variable %<COLLECT_GCC_OPTIONS%> must be set");
1444 char *collect_as_options = getenv ("COLLECT_AS_OPTIONS");
1446 /* Prepend -Xassembler to each option, and append the string
1447 to collect_gcc_options. */
1448 if (collect_as_options)
1450 obstack temporary_obstack;
1451 obstack_init (&temporary_obstack);
1453 prepend_xassembler_to_collect_as_options (collect_as_options,
1454 &temporary_obstack);
1455 obstack_1grow (&temporary_obstack, '\0');
1457 char *xassembler_opts_string
1458 = XOBFINISH (&temporary_obstack, char *);
1459 collect_gcc_options = concat (collect_gcc_options, xassembler_opts_string,
1460 NULL);
1463 vec<cl_decoded_option> decoded_options
1464 = get_options_from_collect_gcc_options (collect_gcc, collect_gcc_options);
1466 /* Allocate array for input object files with LTO IL,
1467 and for possible preceding arguments. */
1468 lto_argv = XNEWVEC (char *, argc);
1469 ltoobj_argv = XNEWVEC (char *, argc);
1471 /* Look at saved options in the IL files. */
1472 for (i = 1; i < argc; ++i)
1474 char *p;
1475 int fd;
1476 off_t file_offset = 0;
1477 long loffset;
1478 int consumed;
1479 char *filename = argv[i];
1481 if (startswith (argv[i], "-foffload-objects="))
1483 have_offload = true;
1484 offload_objects_file_name
1485 = argv[i] + sizeof ("-foffload-objects=") - 1;
1486 continue;
1489 if ((p = strrchr (argv[i], '@'))
1490 && p != argv[i]
1491 && sscanf (p, "@%li%n", &loffset, &consumed) >= 1
1492 && strlen (p) == (unsigned int) consumed)
1494 filename = XNEWVEC (char, p - argv[i] + 1);
1495 memcpy (filename, argv[i], p - argv[i]);
1496 filename[p - argv[i]] = '\0';
1497 file_offset = (off_t) loffset;
1499 fd = open (filename, O_RDONLY | O_BINARY);
1500 /* Linker plugin passes -fresolution and -flinker-output options.
1501 -flinker-output is passed only when user did not specify one and thus
1502 we do not need to worry about duplicities with the option handling
1503 below. */
1504 if (fd == -1)
1506 lto_argv[lto_argc++] = argv[i];
1507 if (strcmp (argv[i], "-flinker-output=rel") == 0)
1508 linker_output_rel = true;
1509 continue;
1512 if (find_and_merge_options (fd, file_offset, LTO_SECTION_NAME_PREFIX,
1513 decoded_options, &fdecoded_options,
1514 collect_gcc))
1516 have_lto = true;
1517 ltoobj_argv[ltoobj_argc++] = argv[i];
1519 close (fd);
1522 /* Initalize the common arguments for the driver. */
1523 obstack_init (&argv_obstack);
1524 obstack_ptr_grow (&argv_obstack, collect_gcc);
1525 obstack_ptr_grow (&argv_obstack, "-xlto");
1526 obstack_ptr_grow (&argv_obstack, "-c");
1528 append_compiler_options (&argv_obstack, fdecoded_options);
1529 append_linker_options (&argv_obstack, decoded_options);
1531 /* Scan linker driver arguments for things that are of relevance to us. */
1532 for (j = 1; j < decoded_options.length (); ++j)
1534 cl_decoded_option *option = &decoded_options[j];
1535 switch (option->opt_index)
1537 case OPT_o:
1538 linker_output = option->arg;
1539 break;
1541 /* We don't have to distinguish between -save-temps=* and
1542 -save-temps, -dumpdir already carries that
1543 information. */
1544 case OPT_save_temps_:
1545 case OPT_save_temps:
1546 save_temps = 1;
1547 break;
1549 case OPT_v:
1550 verbose = 1;
1551 break;
1553 case OPT_flto_partition_:
1554 if (strcmp (option->arg, "none") == 0)
1555 no_partition = true;
1556 break;
1558 case OPT_flto_:
1559 /* Merge linker -flto= option with what we have in IL files. */
1560 merge_flto_options (fdecoded_options, option);
1561 if (strcmp (option->arg, "jobserver") == 0)
1562 jobserver_requested = true;
1563 break;
1565 case OPT_flinker_output_:
1566 linker_output_rel = !strcmp (option->arg, "rel");
1567 break;
1569 case OPT_g:
1570 /* Recognize -g0. */
1571 skip_debug = option->arg && !strcmp (option->arg, "0");
1572 break;
1574 case OPT_dumpdir:
1575 incoming_dumppfx = dumppfx = option->arg;
1576 break;
1578 case OPT_fdiagnostics_urls_:
1579 diagnostic_urls_init (global_dc, option->value);
1580 break;
1582 case OPT_fdiagnostics_color_:
1583 diagnostic_color_init (global_dc, option->value);
1584 break;
1586 default:
1587 break;
1591 /* Process LTO-related options on merged options. */
1592 for (j = 1; j < fdecoded_options.length (); ++j)
1594 cl_decoded_option *option = &fdecoded_options[j];
1595 switch (option->opt_index)
1597 case OPT_flto_:
1598 if (strcmp (option->arg, "jobserver") == 0)
1600 parallel = 1;
1601 jobserver = 1;
1603 else if (strcmp (option->arg, "auto") == 0)
1605 parallel = 1;
1606 auto_parallel = 1;
1608 else
1610 parallel = atoi (option->arg);
1611 if (parallel <= 1)
1612 parallel = 0;
1614 /* Fallthru. */
1616 case OPT_flto:
1617 lto_mode = LTO_MODE_WHOPR;
1618 break;
1622 /* Output lto-wrapper invocation command. */
1623 if (verbose)
1625 for (i = 0; i < argc; ++i)
1627 fputs (argv[i], stderr);
1628 fputc (' ', stderr);
1630 fputc ('\n', stderr);
1633 if (linker_output_rel)
1634 no_partition = true;
1636 if (no_partition)
1638 lto_mode = LTO_MODE_LTO;
1639 jobserver = 0;
1640 jobserver_requested = false;
1641 auto_parallel = 0;
1642 parallel = 0;
1644 else
1646 jobserver_error = jobserver_active_p ();
1647 if (jobserver && jobserver_error != NULL)
1649 /* Fall back to auto parallelism. */
1650 jobserver = 0;
1651 auto_parallel = 1;
1653 else if (!jobserver && jobserver_error == NULL)
1655 parallel = 1;
1656 jobserver = 1;
1660 /* We need make working for a parallel execution. */
1661 if (parallel && !make_exists ())
1662 parallel = 0;
1664 if (!dumppfx)
1666 if (!linker_output
1667 || strcmp (linker_output, HOST_BIT_BUCKET) == 0)
1668 dumppfx = "a.";
1669 else
1671 const char *obase = lbasename (linker_output), *temp;
1673 /* Strip the executable extension. */
1674 size_t blen = strlen (obase), xlen;
1675 if ((temp = strrchr (obase + 1, '.'))
1676 && (xlen = strlen (temp))
1677 && (strcmp (temp, ".exe") == 0
1678 #if defined(HAVE_TARGET_EXECUTABLE_SUFFIX)
1679 || strcmp (temp, TARGET_EXECUTABLE_SUFFIX) == 0
1680 #endif
1681 || strcmp (obase, "a.out") == 0))
1682 dumppfx = xstrndup (linker_output,
1683 obase - linker_output + blen - xlen + 1);
1684 else
1685 dumppfx = concat (linker_output, ".", NULL);
1689 /* If there's no directory component in the dumppfx, add one, so
1690 that, when it is used as -dumpbase, it overrides any occurrence
1691 of -dumpdir that might have been passed in. */
1692 if (!dumppfx || lbasename (dumppfx) == dumppfx)
1693 dumppfx = concat (current_dir, dumppfx, NULL);
1695 /* Make sure some -dumpdir is passed, so as to get predictable
1696 -dumpbase overriding semantics. If we got an incoming -dumpdir
1697 argument, we'll pass it on, so don't bother with another one
1698 then. */
1699 if (!incoming_dumppfx)
1701 obstack_ptr_grow (&argv_obstack, "-dumpdir");
1702 obstack_ptr_grow (&argv_obstack, "");
1704 obstack_ptr_grow (&argv_obstack, "-dumpbase");
1706 /* Remember at which point we can scrub args to re-use the commons. */
1707 new_head_argc = obstack_object_size (&argv_obstack) / sizeof (void *);
1709 if (have_offload)
1711 unsigned i, num_offload_files;
1712 char **offload_argv;
1713 FILE *f;
1715 f = fopen (offload_objects_file_name, "r");
1716 if (f == NULL)
1717 fatal_error (input_location, "cannot open %s: %m",
1718 offload_objects_file_name);
1719 if (fscanf (f, "%u ", &num_offload_files) != 1)
1720 fatal_error (input_location, "cannot read %s: %m",
1721 offload_objects_file_name);
1722 offload_argv = XCNEWVEC (char *, num_offload_files);
1724 /* Read names of object files with offload. */
1725 for (i = 0; i < num_offload_files; i++)
1727 const unsigned piece = 32;
1728 char *buf, *filename = XNEWVEC (char, piece);
1729 size_t len;
1731 buf = filename;
1732 cont1:
1733 if (!fgets (buf, piece, f))
1734 break;
1735 len = strlen (filename);
1736 if (filename[len - 1] != '\n')
1738 filename = XRESIZEVEC (char, filename, len + piece);
1739 buf = filename + len;
1740 goto cont1;
1742 filename[len - 1] = '\0';
1743 offload_argv[i] = filename;
1745 fclose (f);
1746 if (offload_argv[num_offload_files - 1] == NULL)
1747 fatal_error (input_location, "invalid format of %s",
1748 offload_objects_file_name);
1749 maybe_unlink (offload_objects_file_name);
1750 offload_objects_file_name = NULL;
1752 /* Look at saved offload options in files. */
1753 for (i = 0; i < num_offload_files; i++)
1755 char *p;
1756 long loffset;
1757 int fd, consumed;
1758 off_t file_offset = 0;
1759 char *filename = offload_argv[i];
1761 if ((p = strrchr (offload_argv[i], '@'))
1762 && p != offload_argv[i]
1763 && sscanf (p, "@%li%n", &loffset, &consumed) >= 1
1764 && strlen (p) == (unsigned int) consumed)
1766 filename = XNEWVEC (char, p - offload_argv[i] + 1);
1767 memcpy (filename, offload_argv[i], p - offload_argv[i]);
1768 filename[p - offload_argv[i]] = '\0';
1769 file_offset = (off_t) loffset;
1771 fd = open (filename, O_RDONLY | O_BINARY);
1772 if (fd == -1)
1773 fatal_error (input_location, "cannot open %s: %m", filename);
1774 if (!find_and_merge_options (fd, file_offset,
1775 OFFLOAD_SECTION_NAME_PREFIX,
1776 decoded_options, &offload_fdecoded_options,
1777 collect_gcc))
1778 fatal_error (input_location, "cannot read %s: %m", filename);
1779 close (fd);
1780 if (filename != offload_argv[i])
1781 XDELETEVEC (filename);
1784 compile_images_for_offload_targets (num_offload_files, offload_argv,
1785 offload_fdecoded_options, decoded_options);
1787 free_array_of_ptrs ((void **) offload_argv, num_offload_files);
1789 if (offload_names)
1791 find_crtoffloadtable (save_temps, dumppfx);
1792 for (i = 0; offload_names[i]; i++)
1793 printf ("%s\n", offload_names[i]);
1794 free_array_of_ptrs ((void **) offload_names, i);
1798 /* If object files contain offload sections, but do not contain LTO sections,
1799 then there is no need to perform a link-time recompilation, i.e.
1800 lto-wrapper is used only for a compilation of offload images. */
1801 if (have_offload && !have_lto)
1802 goto finish;
1804 if (lto_mode == LTO_MODE_LTO)
1806 /* -dumpbase argument for LTO. */
1807 flto_out = concat (dumppfx, "lto.o", NULL);
1808 obstack_ptr_grow (&argv_obstack, flto_out);
1810 if (!save_temps)
1811 flto_out = make_temp_file (".lto.o");
1812 obstack_ptr_grow (&argv_obstack, "-o");
1813 obstack_ptr_grow (&argv_obstack, flto_out);
1815 else
1817 const char *list_option = "-fltrans-output-list=";
1819 /* -dumpbase argument for WPA. */
1820 char *dumpbase = concat (dumppfx, "wpa", NULL);
1821 obstack_ptr_grow (&argv_obstack, dumpbase);
1823 if (save_temps)
1824 ltrans_output_file = concat (dumppfx, "ltrans.out", NULL);
1825 else
1826 ltrans_output_file = make_temp_file (".ltrans.out");
1827 list_option_full = concat (list_option, ltrans_output_file, NULL);
1828 obstack_ptr_grow (&argv_obstack, list_option_full);
1830 if (jobserver)
1832 if (verbose)
1833 fprintf (stderr, "Using make jobserver\n");
1834 obstack_ptr_grow (&argv_obstack, xstrdup ("-fwpa=jobserver"));
1836 else if (auto_parallel)
1838 char buf[256];
1839 init_num_threads ();
1840 if (nthreads_var == 0)
1841 nthreads_var = 1;
1842 if (verbose)
1843 fprintf (stderr, "LTO parallelism level set to %ld\n",
1844 nthreads_var);
1845 sprintf (buf, "-fwpa=%ld", nthreads_var);
1846 obstack_ptr_grow (&argv_obstack, xstrdup (buf));
1848 else if (parallel > 1)
1850 char buf[256];
1851 sprintf (buf, "-fwpa=%i", parallel);
1852 obstack_ptr_grow (&argv_obstack, xstrdup (buf));
1854 else
1855 obstack_ptr_grow (&argv_obstack, "-fwpa");
1858 /* Append input arguments. */
1859 for (i = 0; i < lto_argc; ++i)
1860 obstack_ptr_grow (&argv_obstack, lto_argv[i]);
1861 /* Append the input objects. */
1862 for (i = 0; i < ltoobj_argc; ++i)
1863 obstack_ptr_grow (&argv_obstack, ltoobj_argv[i]);
1864 obstack_ptr_grow (&argv_obstack, NULL);
1866 new_argv = XOBFINISH (&argv_obstack, const char **);
1867 argv_ptr = &new_argv[new_head_argc];
1868 fork_execute (new_argv[0], CONST_CAST (char **, new_argv), true,
1869 "ltrans_args");
1871 /* Copy the early generated debug info from the objects to temporary
1872 files and append those to the partial link commandline. */
1873 early_debug_object_names = NULL;
1874 if (! skip_debug)
1876 early_debug_object_names = XCNEWVEC (const char *, ltoobj_argc+ 1);
1877 num_deb_objs = ltoobj_argc;
1878 for (i = 0; i < ltoobj_argc; ++i)
1880 const char *tem;
1881 if ((tem = debug_objcopy (ltoobj_argv[i], !linker_output_rel)))
1882 early_debug_object_names[i] = tem;
1886 if (lto_mode == LTO_MODE_LTO)
1888 printf ("%s\n", flto_out);
1889 if (!skip_debug)
1891 for (i = 0; i < ltoobj_argc; ++i)
1892 if (early_debug_object_names[i] != NULL)
1893 printf ("%s\n", early_debug_object_names[i]);
1895 /* These now belong to collect2. */
1896 free (flto_out);
1897 flto_out = NULL;
1898 free (early_debug_object_names);
1899 early_debug_object_names = NULL;
1901 else
1903 FILE *stream = fopen (ltrans_output_file, "r");
1904 FILE *mstream = NULL;
1905 struct obstack env_obstack;
1906 int priority;
1908 if (!stream)
1909 fatal_error (input_location, "%<fopen%>: %s: %m", ltrans_output_file);
1911 /* Parse the list of LTRANS inputs from the WPA stage. */
1912 obstack_init (&env_obstack);
1913 nr = 0;
1914 for (;;)
1916 const unsigned piece = 32;
1917 char *output_name = NULL;
1918 char *buf, *input_name = (char *)xmalloc (piece);
1919 size_t len;
1921 buf = input_name;
1922 if (fscanf (stream, "%i\n", &priority) != 1)
1924 if (!feof (stream))
1925 fatal_error (input_location,
1926 "corrupted ltrans output file %s",
1927 ltrans_output_file);
1928 break;
1930 cont:
1931 if (!fgets (buf, piece, stream))
1932 break;
1933 len = strlen (input_name);
1934 if (input_name[len - 1] != '\n')
1936 input_name = (char *)xrealloc (input_name, len + piece);
1937 buf = input_name + len;
1938 goto cont;
1940 input_name[len - 1] = '\0';
1942 if (input_name[0] == '*')
1943 output_name = &input_name[1];
1945 nr++;
1946 ltrans_priorities
1947 = (int *)xrealloc (ltrans_priorities, nr * sizeof (int) * 2);
1948 input_names = (char **)xrealloc (input_names, nr * sizeof (char *));
1949 output_names = (char **)xrealloc (output_names, nr * sizeof (char *));
1950 ltrans_priorities[(nr-1)*2] = priority;
1951 ltrans_priorities[(nr-1)*2+1] = nr-1;
1952 input_names[nr-1] = input_name;
1953 output_names[nr-1] = output_name;
1955 fclose (stream);
1956 maybe_unlink (ltrans_output_file);
1957 ltrans_output_file = NULL;
1959 if (nr > 1)
1961 if (jobserver_requested && jobserver_error != NULL)
1963 warning (0, jobserver_error);
1964 print_lto_docs_link ();
1966 else if (parallel == 0)
1968 warning (0, "using serial compilation of %d LTRANS jobs", nr);
1969 print_lto_docs_link ();
1973 if (parallel)
1975 makefile = make_temp_file (".mk");
1976 mstream = fopen (makefile, "w");
1977 qsort (ltrans_priorities, nr, sizeof (int) * 2, cmp_priority);
1980 /* Execute the LTRANS stage for each input file (or prepare a
1981 makefile to invoke this in parallel). */
1982 for (i = 0; i < nr; ++i)
1984 char *output_name;
1985 char *input_name = input_names[i];
1986 /* If it's a pass-through file do nothing. */
1987 if (output_names[i])
1988 continue;
1990 /* Replace the .o suffix with a .ltrans.o suffix and write
1991 the resulting name to the LTRANS output list. */
1992 obstack_grow (&env_obstack, input_name, strlen (input_name) - 2);
1993 obstack_grow (&env_obstack, ".ltrans.o", sizeof (".ltrans.o"));
1994 output_name = XOBFINISH (&env_obstack, char *);
1996 /* Adjust the dumpbase if the linker output file was seen. */
1997 int dumpbase_len = (strlen (dumppfx)
1998 + sizeof (DUMPBASE_SUFFIX)
1999 + sizeof (".ltrans"));
2000 char *dumpbase = (char *) xmalloc (dumpbase_len + 1);
2001 snprintf (dumpbase, dumpbase_len, "%sltrans%u.ltrans", dumppfx, i);
2002 argv_ptr[0] = dumpbase;
2004 argv_ptr[1] = "-fltrans";
2005 argv_ptr[2] = "-o";
2006 argv_ptr[3] = output_name;
2007 argv_ptr[4] = input_name;
2008 argv_ptr[5] = NULL;
2009 if (parallel)
2011 fprintf (mstream, "%s:\n\t@%s ", output_name, new_argv[0]);
2012 for (j = 1; new_argv[j] != NULL; ++j)
2013 fprintf (mstream, " '%s'", new_argv[j]);
2014 fprintf (mstream, "\n");
2015 /* If we are not preserving the ltrans input files then
2016 truncate them as soon as we have processed it. This
2017 reduces temporary disk-space usage. */
2018 if (! save_temps)
2019 fprintf (mstream, "\t@-touch -r %s %s.tem > /dev/null 2>&1 "
2020 "&& mv %s.tem %s\n",
2021 input_name, input_name, input_name, input_name);
2023 else
2025 char argsuffix[sizeof (DUMPBASE_SUFFIX)
2026 + sizeof (".ltrans_args") + 1];
2027 if (save_temps)
2028 snprintf (argsuffix,
2029 sizeof (DUMPBASE_SUFFIX) + sizeof (".ltrans_args"),
2030 "ltrans%u.ltrans_args", i);
2031 fork_execute (new_argv[0], CONST_CAST (char **, new_argv),
2032 true, save_temps ? argsuffix : NULL);
2033 maybe_unlink (input_name);
2036 output_names[i] = output_name;
2038 if (parallel)
2040 struct pex_obj *pex;
2041 char jobs[32];
2043 fprintf (mstream,
2044 ".PHONY: all\n"
2045 "all:");
2046 for (i = 0; i < nr; ++i)
2048 int j = ltrans_priorities[i*2 + 1];
2049 fprintf (mstream, " \\\n\t%s", output_names[j]);
2051 fprintf (mstream, "\n");
2052 fclose (mstream);
2053 if (!jobserver)
2055 /* Avoid passing --jobserver-fd= and similar flags
2056 unless jobserver mode is explicitly enabled. */
2057 putenv (xstrdup ("MAKEFLAGS="));
2058 putenv (xstrdup ("MFLAGS="));
2061 char **make_argv = buildargv (getenv ("MAKE"));
2062 if (make_argv)
2064 for (unsigned argc = 0; make_argv[argc]; argc++)
2065 obstack_ptr_grow (&argv_obstack, make_argv[argc]);
2067 else
2068 obstack_ptr_grow (&argv_obstack, "make");
2070 obstack_ptr_grow (&argv_obstack, "-f");
2071 obstack_ptr_grow (&argv_obstack, makefile);
2072 if (!jobserver)
2074 snprintf (jobs, 31, "-j%ld",
2075 auto_parallel ? nthreads_var : parallel);
2076 obstack_ptr_grow (&argv_obstack, jobs);
2078 obstack_ptr_grow (&argv_obstack, "all");
2079 obstack_ptr_grow (&argv_obstack, NULL);
2080 new_argv = XOBFINISH (&argv_obstack, const char **);
2082 pex = collect_execute (new_argv[0], CONST_CAST (char **, new_argv),
2083 NULL, NULL, PEX_SEARCH, false, NULL);
2084 do_wait (new_argv[0], pex);
2085 freeargv (make_argv);
2086 maybe_unlink (makefile);
2087 makefile = NULL;
2088 for (i = 0; i < nr; ++i)
2089 maybe_unlink (input_names[i]);
2091 for (i = 0; i < nr; ++i)
2093 fputs (output_names[i], stdout);
2094 putc ('\n', stdout);
2095 free (input_names[i]);
2097 if (!skip_debug)
2099 for (i = 0; i < ltoobj_argc; ++i)
2100 if (early_debug_object_names[i] != NULL)
2101 printf ("%s\n", early_debug_object_names[i]);
2103 nr = 0;
2104 free (ltrans_priorities);
2105 free (output_names);
2106 output_names = NULL;
2107 free (early_debug_object_names);
2108 early_debug_object_names = NULL;
2109 free (input_names);
2110 free (list_option_full);
2111 obstack_free (&env_obstack, NULL);
2114 finish:
2115 XDELETE (lto_argv);
2116 obstack_free (&argv_obstack, NULL);
2120 /* Entry point. */
2123 main (int argc, char *argv[])
2125 const char *p;
2127 init_opts_obstack ();
2129 p = argv[0] + strlen (argv[0]);
2130 while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
2131 --p;
2132 progname = p;
2134 xmalloc_set_program_name (progname);
2136 gcc_init_libintl ();
2138 diagnostic_initialize (global_dc, 0);
2139 diagnostic_color_init (global_dc);
2140 diagnostic_urls_init (global_dc);
2141 global_dc->get_option_url = get_option_url;
2143 if (atexit (lto_wrapper_cleanup) != 0)
2144 fatal_error (input_location, "%<atexit%> failed");
2146 setup_signals ();
2148 /* We may be called with all the arguments stored in some file and
2149 passed with @file. Expand them into argv before processing. */
2150 expandargv (&argc, &argv);
2152 run_gcc (argc, argv);
2154 return 0;