Fix previous commit
[official-gcc.git] / gcc / lto-wrapper.c
blob9a7bbd0c02226eba74275d048ac84a921bf12c5b
1 /* Wrapper to call lto. Used by collect2 and the linker plugin.
2 Copyright (C) 2009-2019 Free Software Foundation, Inc.
4 Factored out of collect2 by Rafael Espindola <espindola@google.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 /* This program is passed a gcc, a list of gcc arguments and a list of
24 object files containing IL. It scans the argument list to check if
25 we are in whopr mode or not modifies the arguments and needed and
26 prints a list of output files on stdout.
28 Example:
30 $ lto-wrapper gcc/xgcc -B gcc a.o b.o -o test -flto
32 The above will print something like
33 /tmp/ccwbQ8B2.lto.o
35 If WHOPR is used instead, more than one file might be produced
36 ./ccXj2DTk.lto.ltrans.o
37 ./ccCJuXGv.lto.ltrans.o
40 #include "config.h"
41 #include "system.h"
42 #include "coretypes.h"
43 #include "intl.h"
44 #include "diagnostic.h"
45 #include "obstack.h"
46 #include "opts.h"
47 #include "options.h"
48 #include "simple-object.h"
49 #include "lto-section-names.h"
50 #include "collect-utils.h"
52 /* Environment variable, used for passing the names of offload targets from GCC
53 driver to lto-wrapper. */
54 #define OFFLOAD_TARGET_NAMES_ENV "OFFLOAD_TARGET_NAMES"
56 enum lto_mode_d {
57 LTO_MODE_NONE, /* Not doing LTO. */
58 LTO_MODE_LTO, /* Normal LTO. */
59 LTO_MODE_WHOPR /* WHOPR. */
62 /* Current LTO mode. */
63 static enum lto_mode_d lto_mode = LTO_MODE_NONE;
65 static char *ltrans_output_file;
66 static char *flto_out;
67 static unsigned int nr;
68 static int *ltrans_priorities;
69 static char **input_names;
70 static char **output_names;
71 static char **offload_names;
72 static char *offload_objects_file_name;
73 static char *makefile;
74 static unsigned int num_deb_objs;
75 static const char **early_debug_object_names;
77 const char tool_name[] = "lto-wrapper";
79 /* Delete tempfiles. Called from utils_cleanup. */
81 void
82 tool_cleanup (bool)
84 unsigned int i;
86 if (ltrans_output_file)
87 maybe_unlink (ltrans_output_file);
88 if (flto_out)
89 maybe_unlink (flto_out);
90 if (offload_objects_file_name)
91 maybe_unlink (offload_objects_file_name);
92 if (makefile)
93 maybe_unlink (makefile);
94 if (early_debug_object_names)
95 for (i = 0; i < num_deb_objs; ++i)
96 if (early_debug_object_names[i])
97 maybe_unlink (early_debug_object_names[i]);
98 for (i = 0; i < nr; ++i)
100 maybe_unlink (input_names[i]);
101 if (output_names[i])
102 maybe_unlink (output_names[i]);
106 static void
107 lto_wrapper_cleanup (void)
109 utils_cleanup (false);
112 /* Unlink a temporary LTRANS file unless requested otherwise. */
114 void
115 maybe_unlink (const char *file)
117 if (!save_temps)
119 if (unlink_if_ordinary (file)
120 && errno != ENOENT)
121 fatal_error (input_location, "deleting LTRANS file %s: %m", file);
123 else if (verbose)
124 fprintf (stderr, "[Leaving LTRANS %s]\n", file);
127 /* Template of LTRANS dumpbase suffix. */
128 #define DUMPBASE_SUFFIX ".ltrans18446744073709551615"
130 /* Create decoded options from the COLLECT_GCC and COLLECT_GCC_OPTIONS
131 environment. */
133 static void
134 get_options_from_collect_gcc_options (const char *collect_gcc,
135 const char *collect_gcc_options,
136 struct cl_decoded_option **decoded_options,
137 unsigned int *decoded_options_count)
139 struct obstack argv_obstack;
140 char *argv_storage;
141 const char **argv;
142 int j, k, argc;
144 argv_storage = xstrdup (collect_gcc_options);
145 obstack_init (&argv_obstack);
146 obstack_ptr_grow (&argv_obstack, collect_gcc);
148 for (j = 0, k = 0; argv_storage[j] != '\0'; ++j)
150 if (argv_storage[j] == '\'')
152 obstack_ptr_grow (&argv_obstack, &argv_storage[k]);
153 ++j;
156 if (argv_storage[j] == '\0')
157 fatal_error (input_location,
158 "malformed %<COLLECT_GCC_OPTIONS%>");
159 else if (strncmp (&argv_storage[j], "'\\''", 4) == 0)
161 argv_storage[k++] = '\'';
162 j += 4;
164 else if (argv_storage[j] == '\'')
165 break;
166 else
167 argv_storage[k++] = argv_storage[j++];
169 while (1);
170 argv_storage[k++] = '\0';
174 obstack_ptr_grow (&argv_obstack, NULL);
175 argc = obstack_object_size (&argv_obstack) / sizeof (void *) - 1;
176 argv = XOBFINISH (&argv_obstack, const char **);
178 decode_cmdline_options_to_array (argc, (const char **)argv, CL_DRIVER,
179 decoded_options, decoded_options_count);
180 obstack_free (&argv_obstack, NULL);
183 /* Append OPTION to the options array DECODED_OPTIONS with size
184 DECODED_OPTIONS_COUNT. */
186 static void
187 append_option (struct cl_decoded_option **decoded_options,
188 unsigned int *decoded_options_count,
189 struct cl_decoded_option *option)
191 ++*decoded_options_count;
192 *decoded_options
193 = (struct cl_decoded_option *)
194 xrealloc (*decoded_options,
195 (*decoded_options_count
196 * sizeof (struct cl_decoded_option)));
197 memcpy (&(*decoded_options)[*decoded_options_count - 1], option,
198 sizeof (struct cl_decoded_option));
201 /* Remove option number INDEX from DECODED_OPTIONS, update
202 DECODED_OPTIONS_COUNT. */
204 static void
205 remove_option (struct cl_decoded_option **decoded_options,
206 int index, unsigned int *decoded_options_count)
208 --*decoded_options_count;
209 memmove (&(*decoded_options)[index + 1],
210 &(*decoded_options)[index],
211 sizeof (struct cl_decoded_option)
212 * (*decoded_options_count - index));
215 /* Try to merge and complain about options FDECODED_OPTIONS when applied
216 ontop of DECODED_OPTIONS. */
218 static void
219 merge_and_complain (struct cl_decoded_option **decoded_options,
220 unsigned int *decoded_options_count,
221 struct cl_decoded_option *fdecoded_options,
222 unsigned int fdecoded_options_count)
224 unsigned int i, j;
225 struct cl_decoded_option *pic_option = NULL;
226 struct cl_decoded_option *pie_option = NULL;
228 /* ??? Merge options from files. Most cases can be
229 handled by either unioning or intersecting
230 (for example -fwrapv is a case for unioning,
231 -ffast-math is for intersection). Most complaints
232 about real conflicts between different options can
233 be deferred to the compiler proper. Options that
234 we can neither safely handle by intersection nor
235 unioning would need to be complained about here.
236 Ideally we'd have a flag in the opt files that
237 tells whether to union or intersect or reject.
238 In absence of that it's unclear what a good default is.
239 It's also difficult to get positional handling correct. */
241 /* The following does what the old LTO option code did,
242 union all target and a selected set of common options. */
243 for (i = 0; i < fdecoded_options_count; ++i)
245 struct cl_decoded_option *foption = &fdecoded_options[i];
246 switch (foption->opt_index)
248 case OPT_SPECIAL_unknown:
249 case OPT_SPECIAL_ignore:
250 case OPT_SPECIAL_warn_removed:
251 case OPT_SPECIAL_program_name:
252 case OPT_SPECIAL_input_file:
253 break;
255 default:
256 if (!(cl_options[foption->opt_index].flags & CL_TARGET))
257 break;
259 /* Fallthru. */
260 case OPT_fdiagnostics_show_caret:
261 case OPT_fdiagnostics_show_labels:
262 case OPT_fdiagnostics_show_line_numbers:
263 case OPT_fdiagnostics_show_option:
264 case OPT_fdiagnostics_show_location_:
265 case OPT_fshow_column:
266 case OPT_fcommon:
267 case OPT_fgnu_tm:
268 case OPT_g:
269 /* Do what the old LTO code did - collect exactly one option
270 setting per OPT code, we pick the first we encounter.
271 ??? This doesn't make too much sense, but when it doesn't
272 then we should complain. */
273 for (j = 0; j < *decoded_options_count; ++j)
274 if ((*decoded_options)[j].opt_index == foption->opt_index)
275 break;
276 if (j == *decoded_options_count)
277 append_option (decoded_options, decoded_options_count, foption);
278 break;
280 /* Figure out what PIC/PIE level wins and merge the results. */
281 case OPT_fPIC:
282 case OPT_fpic:
283 pic_option = foption;
284 break;
285 case OPT_fPIE:
286 case OPT_fpie:
287 pie_option = foption;
288 break;
290 case OPT_fopenmp:
291 case OPT_fopenacc:
292 /* For selected options we can merge conservatively. */
293 for (j = 0; j < *decoded_options_count; ++j)
294 if ((*decoded_options)[j].opt_index == foption->opt_index)
295 break;
296 if (j == *decoded_options_count)
297 append_option (decoded_options, decoded_options_count, foption);
298 /* -fopenmp > -fno-openmp,
299 -fopenacc > -fno-openacc */
300 else if (foption->value > (*decoded_options)[j].value)
301 (*decoded_options)[j] = *foption;
302 break;
304 case OPT_fopenacc_dim_:
305 /* Append or check identical. */
306 for (j = 0; j < *decoded_options_count; ++j)
307 if ((*decoded_options)[j].opt_index == foption->opt_index)
308 break;
309 if (j == *decoded_options_count)
310 append_option (decoded_options, decoded_options_count, foption);
311 else if (strcmp ((*decoded_options)[j].arg, foption->arg))
312 fatal_error (input_location,
313 "option %s with different values",
314 foption->orig_option_with_args_text);
315 break;
317 case OPT_O:
318 case OPT_Ofast:
319 case OPT_Og:
320 case OPT_Os:
321 for (j = 0; j < *decoded_options_count; ++j)
322 if ((*decoded_options)[j].opt_index == OPT_O
323 || (*decoded_options)[j].opt_index == OPT_Ofast
324 || (*decoded_options)[j].opt_index == OPT_Og
325 || (*decoded_options)[j].opt_index == OPT_Os)
326 break;
327 if (j == *decoded_options_count)
328 append_option (decoded_options, decoded_options_count, foption);
329 else if ((*decoded_options)[j].opt_index == foption->opt_index
330 && foption->opt_index != OPT_O)
331 /* Exact same options get merged. */
333 else
335 /* For mismatched option kinds preserve the optimization
336 level only, thus merge it as -On. This also handles
337 merging of same optimization level -On. */
338 int level = 0;
339 switch (foption->opt_index)
341 case OPT_O:
342 if (foption->arg[0] == '\0')
343 level = MAX (level, 1);
344 else
345 level = MAX (level, atoi (foption->arg));
346 break;
347 case OPT_Ofast:
348 level = MAX (level, 3);
349 break;
350 case OPT_Og:
351 level = MAX (level, 1);
352 break;
353 case OPT_Os:
354 level = MAX (level, 2);
355 break;
356 default:
357 gcc_unreachable ();
359 switch ((*decoded_options)[j].opt_index)
361 case OPT_O:
362 if ((*decoded_options)[j].arg[0] == '\0')
363 level = MAX (level, 1);
364 else
365 level = MAX (level, atoi ((*decoded_options)[j].arg));
366 break;
367 case OPT_Ofast:
368 level = MAX (level, 3);
369 break;
370 case OPT_Og:
371 level = MAX (level, 1);
372 break;
373 case OPT_Os:
374 level = MAX (level, 2);
375 break;
376 default:
377 gcc_unreachable ();
379 (*decoded_options)[j].opt_index = OPT_O;
380 char *tem;
381 tem = xasprintf ("-O%d", level);
382 (*decoded_options)[j].arg = &tem[2];
383 (*decoded_options)[j].canonical_option[0] = tem;
384 (*decoded_options)[j].value = 1;
386 break;
389 case OPT_foffload_abi_:
390 for (j = 0; j < *decoded_options_count; ++j)
391 if ((*decoded_options)[j].opt_index == foption->opt_index)
392 break;
393 if (j == *decoded_options_count)
394 append_option (decoded_options, decoded_options_count, foption);
395 else if (foption->value != (*decoded_options)[j].value)
396 fatal_error (input_location,
397 "option %s not used consistently in all LTO input"
398 " files", foption->orig_option_with_args_text);
399 break;
402 case OPT_foffload_:
403 append_option (decoded_options, decoded_options_count, foption);
404 break;
408 /* Merge PIC options:
409 -fPIC + -fpic = -fpic
410 -fPIC + -fno-pic = -fno-pic
411 -fpic/-fPIC + nothin = nothing.
412 It is a common mistake to mix few -fPIC compiled objects into otherwise
413 non-PIC code. We do not want to build everything with PIC then.
415 Similarly we merge PIE options, however in addition we keep
416 -fPIC + -fPIE = -fPIE
417 -fpic + -fPIE = -fpie
418 -fPIC/-fpic + -fpie = -fpie
420 It would be good to warn on mismatches, but it is bit hard to do as
421 we do not know what nothing translates to. */
423 for (unsigned int j = 0; j < *decoded_options_count;)
424 if ((*decoded_options)[j].opt_index == OPT_fPIC
425 || (*decoded_options)[j].opt_index == OPT_fpic)
427 /* -fno-pic in one unit implies -fno-pic everywhere. */
428 if ((*decoded_options)[j].value == 0)
429 j++;
430 /* If we have no pic option or merge in -fno-pic, we still may turn
431 existing pic/PIC mode into pie/PIE if -fpie/-fPIE is present. */
432 else if ((pic_option && pic_option->value == 0)
433 || !pic_option)
435 if (pie_option)
437 bool big = (*decoded_options)[j].opt_index == OPT_fPIC
438 && pie_option->opt_index == OPT_fPIE;
439 (*decoded_options)[j].opt_index = big ? OPT_fPIE : OPT_fpie;
440 if (pie_option->value)
441 (*decoded_options)[j].canonical_option[0] = big ? "-fPIE" : "-fpie";
442 else
443 (*decoded_options)[j].canonical_option[0] = big ? "-fno-pie" : "-fno-pie";
444 (*decoded_options)[j].value = pie_option->value;
445 j++;
447 else if (pic_option)
449 (*decoded_options)[j] = *pic_option;
450 j++;
452 /* We do not know if target defaults to pic or not, so just remove
453 option if it is missing in one unit but enabled in other. */
454 else
455 remove_option (decoded_options, j, decoded_options_count);
457 else if (pic_option->opt_index == OPT_fpic
458 && (*decoded_options)[j].opt_index == OPT_fPIC)
460 (*decoded_options)[j] = *pic_option;
461 j++;
463 else
464 j++;
466 else if ((*decoded_options)[j].opt_index == OPT_fPIE
467 || (*decoded_options)[j].opt_index == OPT_fpie)
469 /* -fno-pie in one unit implies -fno-pie everywhere. */
470 if ((*decoded_options)[j].value == 0)
471 j++;
472 /* If we have no pie option or merge in -fno-pie, we still preserve
473 PIE/pie if pic/PIC is present. */
474 else if ((pie_option && pie_option->value == 0)
475 || !pie_option)
477 /* If -fPIC/-fpic is given, merge it with -fPIE/-fpie. */
478 if (pic_option)
480 if (pic_option->opt_index == OPT_fpic
481 && (*decoded_options)[j].opt_index == OPT_fPIE)
483 (*decoded_options)[j].opt_index = OPT_fpie;
484 (*decoded_options)[j].canonical_option[0]
485 = pic_option->value ? "-fpie" : "-fno-pie";
487 else if (!pic_option->value)
488 (*decoded_options)[j].canonical_option[0] = "-fno-pie";
489 (*decoded_options)[j].value = pic_option->value;
490 j++;
492 else if (pie_option)
494 (*decoded_options)[j] = *pie_option;
495 j++;
497 /* Because we always append pic/PIE options this code path should
498 not happen unless the LTO object was built by old lto1 which
499 did not contain that logic yet. */
500 else
501 remove_option (decoded_options, j, decoded_options_count);
503 else if (pie_option->opt_index == OPT_fpie
504 && (*decoded_options)[j].opt_index == OPT_fPIE)
506 (*decoded_options)[j] = *pie_option;
507 j++;
509 else
510 j++;
512 else
513 j++;
516 /* Auxiliary function that frees elements of PTR and PTR itself.
517 N is number of elements to be freed. If PTR is NULL, nothing is freed.
518 If an element is NULL, subsequent elements are not freed. */
520 static void **
521 free_array_of_ptrs (void **ptr, unsigned n)
523 if (!ptr)
524 return NULL;
525 for (unsigned i = 0; i < n; i++)
527 if (!ptr[i])
528 break;
529 free (ptr[i]);
531 free (ptr);
532 return NULL;
535 /* Parse STR, saving found tokens into PVALUES and return their number.
536 Tokens are assumed to be delimited by ':'. If APPEND is non-null,
537 append it to every token we find. */
539 static unsigned
540 parse_env_var (const char *str, char ***pvalues, const char *append)
542 const char *curval, *nextval;
543 char **values;
544 unsigned num = 1, i;
546 curval = strchr (str, ':');
547 while (curval)
549 num++;
550 curval = strchr (curval + 1, ':');
553 values = (char**) xmalloc (num * sizeof (char*));
554 curval = str;
555 nextval = strchr (curval, ':');
556 if (nextval == NULL)
557 nextval = strchr (curval, '\0');
559 int append_len = append ? strlen (append) : 0;
560 for (i = 0; i < num; i++)
562 int l = nextval - curval;
563 values[i] = (char*) xmalloc (l + 1 + append_len);
564 memcpy (values[i], curval, l);
565 values[i][l] = 0;
566 if (append)
567 strcat (values[i], append);
568 curval = nextval + 1;
569 nextval = strchr (curval, ':');
570 if (nextval == NULL)
571 nextval = strchr (curval, '\0');
573 *pvalues = values;
574 return num;
577 /* Append options OPTS from lto or offload_lto sections to ARGV_OBSTACK. */
579 static void
580 append_compiler_options (obstack *argv_obstack, struct cl_decoded_option *opts,
581 unsigned int count)
583 /* Append compiler driver arguments as far as they were merged. */
584 for (unsigned int j = 1; j < count; ++j)
586 struct cl_decoded_option *option = &opts[j];
588 /* File options have been properly filtered by lto-opts.c. */
589 switch (option->opt_index)
591 /* Drop arguments that we want to take from the link line. */
592 case OPT_flto_:
593 case OPT_flto:
594 case OPT_flto_partition_:
595 continue;
597 default:
598 break;
601 /* For now do what the original LTO option code was doing - pass
602 on any CL_TARGET flag and a few selected others. */
603 switch (option->opt_index)
605 case OPT_fdiagnostics_show_caret:
606 case OPT_fdiagnostics_show_labels:
607 case OPT_fdiagnostics_show_line_numbers:
608 case OPT_fdiagnostics_show_option:
609 case OPT_fdiagnostics_show_location_:
610 case OPT_fshow_column:
611 case OPT_fPIC:
612 case OPT_fpic:
613 case OPT_fPIE:
614 case OPT_fpie:
615 case OPT_fcommon:
616 case OPT_fgnu_tm:
617 case OPT_fopenmp:
618 case OPT_fopenacc:
619 case OPT_fopenacc_dim_:
620 case OPT_foffload_abi_:
621 case OPT_g:
622 case OPT_O:
623 case OPT_Ofast:
624 case OPT_Og:
625 case OPT_Os:
626 break;
628 default:
629 if (!(cl_options[option->opt_index].flags & CL_TARGET))
630 continue;
633 /* Pass the option on. */
634 for (unsigned int i = 0; i < option->canonical_option_num_elements; ++i)
635 obstack_ptr_grow (argv_obstack, option->canonical_option[i]);
639 /* Append diag options in OPTS with length COUNT to ARGV_OBSTACK. */
641 static void
642 append_diag_options (obstack *argv_obstack, struct cl_decoded_option *opts,
643 unsigned int count)
645 /* Append compiler driver arguments as far as they were merged. */
646 for (unsigned int j = 1; j < count; ++j)
648 struct cl_decoded_option *option = &opts[j];
650 switch (option->opt_index)
652 case OPT_fdiagnostics_color_:
653 case OPT_fdiagnostics_format_:
654 case OPT_fdiagnostics_show_caret:
655 case OPT_fdiagnostics_show_labels:
656 case OPT_fdiagnostics_show_line_numbers:
657 case OPT_fdiagnostics_show_option:
658 case OPT_fdiagnostics_show_location_:
659 case OPT_fshow_column:
660 break;
661 default:
662 continue;
665 /* Pass the option on. */
666 for (unsigned int i = 0; i < option->canonical_option_num_elements; ++i)
667 obstack_ptr_grow (argv_obstack, option->canonical_option[i]);
672 /* Append linker options OPTS to ARGV_OBSTACK. */
674 static void
675 append_linker_options (obstack *argv_obstack, struct cl_decoded_option *opts,
676 unsigned int count)
678 /* Append linker driver arguments. Compiler options from the linker
679 driver arguments will override / merge with those from the compiler. */
680 for (unsigned int j = 1; j < count; ++j)
682 struct cl_decoded_option *option = &opts[j];
684 /* Do not pass on frontend specific flags not suitable for lto. */
685 if (!(cl_options[option->opt_index].flags
686 & (CL_COMMON|CL_TARGET|CL_DRIVER|CL_LTO)))
687 continue;
689 switch (option->opt_index)
691 case OPT_o:
692 case OPT_flto_:
693 case OPT_flto:
694 /* We've handled these LTO options, do not pass them on. */
695 continue;
697 case OPT_fopenmp:
698 case OPT_fopenacc:
699 /* Ignore -fno-XXX form of these options, as otherwise
700 corresponding builtins will not be enabled. */
701 if (option->value == 0)
702 continue;
703 break;
705 default:
706 break;
709 /* Pass the option on. */
710 for (unsigned int i = 0; i < option->canonical_option_num_elements; ++i)
711 obstack_ptr_grow (argv_obstack, option->canonical_option[i]);
715 /* Extract options for TARGET offload compiler from OPTIONS and append
716 them to ARGV_OBSTACK. */
718 static void
719 append_offload_options (obstack *argv_obstack, const char *target,
720 struct cl_decoded_option *options,
721 unsigned int options_count)
723 for (unsigned i = 0; i < options_count; i++)
725 const char *cur, *next, *opts;
726 char **argv;
727 unsigned argc;
728 struct cl_decoded_option *option = &options[i];
730 if (option->opt_index != OPT_foffload_)
731 continue;
733 /* If option argument starts with '-' then no target is specified. That
734 means offload options are specified for all targets, so we need to
735 append them. */
736 if (option->arg[0] == '-')
737 opts = option->arg;
738 else
740 opts = strchr (option->arg, '=');
741 /* If there are offload targets specified, but no actual options,
742 there is nothing to do here. */
743 if (!opts)
744 continue;
746 cur = option->arg;
748 while (cur < opts)
750 next = strchr (cur, ',');
751 if (next == NULL)
752 next = opts;
753 next = (next > opts) ? opts : next;
755 /* Are we looking for this offload target? */
756 if (strlen (target) == (size_t) (next - cur)
757 && strncmp (target, cur, next - cur) == 0)
758 break;
760 /* Skip the comma or equal sign. */
761 cur = next + 1;
764 if (cur >= opts)
765 continue;
767 opts++;
770 argv = buildargv (opts);
771 for (argc = 0; argv[argc]; argc++)
772 obstack_ptr_grow (argv_obstack, argv[argc]);
776 /* Check whether NAME can be accessed in MODE. This is like access,
777 except that it never considers directories to be executable. */
779 static int
780 access_check (const char *name, int mode)
782 if (mode == X_OK)
784 struct stat st;
786 if (stat (name, &st) < 0
787 || S_ISDIR (st.st_mode))
788 return -1;
791 return access (name, mode);
794 /* Prepare a target image for offload TARGET, using mkoffload tool from
795 COMPILER_PATH. Return the name of the resultant object file. */
797 static char *
798 compile_offload_image (const char *target, const char *compiler_path,
799 unsigned in_argc, char *in_argv[],
800 struct cl_decoded_option *compiler_opts,
801 unsigned int compiler_opt_count,
802 struct cl_decoded_option *linker_opts,
803 unsigned int linker_opt_count)
805 char *filename = NULL;
806 char **argv;
807 char *suffix
808 = XALLOCAVEC (char, sizeof ("/accel//mkoffload") + strlen (target));
809 strcpy (suffix, "/accel/");
810 strcat (suffix, target);
811 strcat (suffix, "/mkoffload");
813 char **paths = NULL;
814 unsigned n_paths = parse_env_var (compiler_path, &paths, suffix);
816 const char *compiler = NULL;
817 for (unsigned i = 0; i < n_paths; i++)
818 if (access_check (paths[i], X_OK) == 0)
820 compiler = paths[i];
821 break;
824 if (!compiler)
825 fatal_error (input_location,
826 "could not find %s in %s (consider using %<-B%>)",
827 suffix + 1, compiler_path);
829 /* Generate temporary output file name. */
830 filename = make_temp_file (".target.o");
832 struct obstack argv_obstack;
833 obstack_init (&argv_obstack);
834 obstack_ptr_grow (&argv_obstack, compiler);
835 if (save_temps)
836 obstack_ptr_grow (&argv_obstack, "-save-temps");
837 if (verbose)
838 obstack_ptr_grow (&argv_obstack, "-v");
839 obstack_ptr_grow (&argv_obstack, "-o");
840 obstack_ptr_grow (&argv_obstack, filename);
842 /* Append names of input object files. */
843 for (unsigned i = 0; i < in_argc; i++)
844 obstack_ptr_grow (&argv_obstack, in_argv[i]);
846 /* Append options from offload_lto sections. */
847 append_compiler_options (&argv_obstack, compiler_opts,
848 compiler_opt_count);
849 append_diag_options (&argv_obstack, linker_opts, linker_opt_count);
851 /* Append options specified by -foffload last. In case of conflicting
852 options we expect offload compiler to choose the latest. */
853 append_offload_options (&argv_obstack, target, compiler_opts,
854 compiler_opt_count);
855 append_offload_options (&argv_obstack, target, linker_opts,
856 linker_opt_count);
858 obstack_ptr_grow (&argv_obstack, NULL);
859 argv = XOBFINISH (&argv_obstack, char **);
860 fork_execute (argv[0], argv, true);
861 obstack_free (&argv_obstack, NULL);
863 free_array_of_ptrs ((void **) paths, n_paths);
864 return filename;
868 /* The main routine dealing with offloading.
869 The routine builds a target image for each offload target. IN_ARGC and
870 IN_ARGV specify options and input object files. As all of them could contain
871 target sections, we pass them all to target compilers. */
873 static void
874 compile_images_for_offload_targets (unsigned in_argc, char *in_argv[],
875 struct cl_decoded_option *compiler_opts,
876 unsigned int compiler_opt_count,
877 struct cl_decoded_option *linker_opts,
878 unsigned int linker_opt_count)
880 char **names = NULL;
881 const char *target_names = getenv (OFFLOAD_TARGET_NAMES_ENV);
882 if (!target_names)
883 return;
884 unsigned num_targets = parse_env_var (target_names, &names, NULL);
886 int next_name_entry = 0;
887 const char *compiler_path = getenv ("COMPILER_PATH");
888 if (!compiler_path)
889 goto out;
891 /* Prepare an image for each target and save the name of the resultant object
892 file to the OFFLOAD_NAMES array. It is terminated by a NULL entry. */
893 offload_names = XCNEWVEC (char *, num_targets + 1);
894 for (unsigned i = 0; i < num_targets; i++)
896 /* HSA does not use LTO-like streaming and a different compiler, skip
897 it. */
898 if (strcmp (names[i], "hsa") == 0)
899 continue;
901 offload_names[next_name_entry]
902 = compile_offload_image (names[i], compiler_path, in_argc, in_argv,
903 compiler_opts, compiler_opt_count,
904 linker_opts, linker_opt_count);
905 if (!offload_names[next_name_entry])
906 fatal_error (input_location,
907 "problem with building target image for %s", names[i]);
908 next_name_entry++;
911 out:
912 free_array_of_ptrs ((void **) names, num_targets);
915 /* Copy a file from SRC to DEST. */
917 static void
918 copy_file (const char *dest, const char *src)
920 FILE *d = fopen (dest, "wb");
921 FILE *s = fopen (src, "rb");
922 char buffer[512];
923 while (!feof (s))
925 size_t len = fread (buffer, 1, 512, s);
926 if (ferror (s) != 0)
927 fatal_error (input_location, "reading input file");
928 if (len > 0)
930 fwrite (buffer, 1, len, d);
931 if (ferror (d) != 0)
932 fatal_error (input_location, "writing output file");
935 fclose (d);
936 fclose (s);
939 /* Find the crtoffloadtable.o file in LIBRARY_PATH, make copy and pass name of
940 the copy to the linker. */
942 static void
943 find_crtoffloadtable (void)
945 char **paths = NULL;
946 const char *library_path = getenv ("LIBRARY_PATH");
947 if (!library_path)
948 return;
949 unsigned n_paths = parse_env_var (library_path, &paths, "/crtoffloadtable.o");
951 unsigned i;
952 for (i = 0; i < n_paths; i++)
953 if (access_check (paths[i], R_OK) == 0)
955 /* The linker will delete the filename we give it, so make a copy. */
956 char *crtoffloadtable = make_temp_file (".crtoffloadtable.o");
957 copy_file (crtoffloadtable, paths[i]);
958 printf ("%s\n", crtoffloadtable);
959 XDELETEVEC (crtoffloadtable);
960 break;
962 if (i == n_paths)
963 fatal_error (input_location,
964 "installation error, cannot find %<crtoffloadtable.o%>");
966 free_array_of_ptrs ((void **) paths, n_paths);
969 /* A subroutine of run_gcc. Examine the open file FD for lto sections with
970 name prefix PREFIX, at FILE_OFFSET, and store any options we find in OPTS
971 and OPT_COUNT. Return true if we found a matchingn section, false
972 otherwise. COLLECT_GCC holds the value of the environment variable with
973 the same name. */
975 static bool
976 find_and_merge_options (int fd, off_t file_offset, const char *prefix,
977 struct cl_decoded_option **opts,
978 unsigned int *opt_count, const char *collect_gcc)
980 off_t offset, length;
981 char *data;
982 char *fopts;
983 const char *errmsg;
984 int err;
985 struct cl_decoded_option *fdecoded_options = *opts;
986 unsigned int fdecoded_options_count = *opt_count;
988 simple_object_read *sobj;
989 sobj = simple_object_start_read (fd, file_offset, "__GNU_LTO",
990 &errmsg, &err);
991 if (!sobj)
992 return false;
994 char *secname = XALLOCAVEC (char, strlen (prefix) + sizeof (".opts"));
995 strcpy (secname, prefix);
996 strcat (secname, ".opts");
997 if (!simple_object_find_section (sobj, secname, &offset, &length,
998 &errmsg, &err))
1000 simple_object_release_read (sobj);
1001 return false;
1004 lseek (fd, file_offset + offset, SEEK_SET);
1005 data = (char *)xmalloc (length);
1006 read (fd, data, length);
1007 fopts = data;
1010 struct cl_decoded_option *f2decoded_options;
1011 unsigned int f2decoded_options_count;
1012 get_options_from_collect_gcc_options (collect_gcc, fopts,
1013 &f2decoded_options,
1014 &f2decoded_options_count);
1015 if (!fdecoded_options)
1017 fdecoded_options = f2decoded_options;
1018 fdecoded_options_count = f2decoded_options_count;
1020 else
1021 merge_and_complain (&fdecoded_options,
1022 &fdecoded_options_count,
1023 f2decoded_options, f2decoded_options_count);
1025 fopts += strlen (fopts) + 1;
1027 while (fopts - data < length);
1029 free (data);
1030 simple_object_release_read (sobj);
1031 *opts = fdecoded_options;
1032 *opt_count = fdecoded_options_count;
1033 return true;
1036 /* Copy early debug info sections from INFILE to a new file whose name
1037 is returned. Return NULL on error. */
1039 const char *
1040 debug_objcopy (const char *infile, bool rename)
1042 char *outfile;
1043 const char *errmsg;
1044 int err;
1046 const char *p;
1047 const char *orig_infile = infile;
1048 off_t inoff = 0;
1049 long loffset;
1050 int consumed;
1051 if ((p = strrchr (infile, '@'))
1052 && p != infile
1053 && sscanf (p, "@%li%n", &loffset, &consumed) >= 1
1054 && strlen (p) == (unsigned int) consumed)
1056 char *fname = xstrdup (infile);
1057 fname[p - infile] = '\0';
1058 infile = fname;
1059 inoff = (off_t) loffset;
1061 int infd = open (infile, O_RDONLY | O_BINARY);
1062 if (infd == -1)
1063 return NULL;
1064 simple_object_read *inobj = simple_object_start_read (infd, inoff,
1065 "__GNU_LTO",
1066 &errmsg, &err);
1067 if (!inobj)
1068 return NULL;
1070 off_t off, len;
1071 if (simple_object_find_section (inobj, ".gnu.debuglto_.debug_info",
1072 &off, &len, &errmsg, &err) != 1)
1074 if (errmsg)
1075 fatal_error (0, "%s: %s", errmsg, xstrerror (err));
1077 simple_object_release_read (inobj);
1078 close (infd);
1079 return NULL;
1082 if (save_temps)
1084 outfile = (char *) xmalloc (strlen (orig_infile)
1085 + sizeof (".debug.temp.o") + 1);
1086 strcpy (outfile, orig_infile);
1087 strcat (outfile, ".debug.temp.o");
1089 else
1090 outfile = make_temp_file (".debug.temp.o");
1091 errmsg = simple_object_copy_lto_debug_sections (inobj, outfile, &err, rename);
1092 if (errmsg)
1094 unlink_if_ordinary (outfile);
1095 fatal_error (0, "%s: %s", errmsg, xstrerror (err));
1098 simple_object_release_read (inobj);
1099 close (infd);
1101 return outfile;
1104 /* Helper for qsort: compare priorities for parallel compilation. */
1107 cmp_priority (const void *a, const void *b)
1109 return *((const int *)b)-*((const int *)a);
1112 /* Number of CPUs that can be used for parallel LTRANS phase. */
1114 static unsigned long nthreads_var = 0;
1116 #ifdef HAVE_PTHREAD_AFFINITY_NP
1117 unsigned long cpuset_size;
1118 static unsigned long get_cpuset_size;
1119 cpu_set_t *cpusetp;
1121 unsigned long
1122 static cpuset_popcount (unsigned long cpusetsize, cpu_set_t *cpusetp)
1124 #ifdef CPU_COUNT_S
1125 /* glibc 2.7 and above provide a macro for this. */
1126 return CPU_COUNT_S (cpusetsize, cpusetp);
1127 #else
1128 #ifdef CPU_COUNT
1129 if (cpusetsize == sizeof (cpu_set_t))
1130 /* glibc 2.6 and above provide a macro for this. */
1131 return CPU_COUNT (cpusetp);
1132 #endif
1133 size_t i;
1134 unsigned long ret = 0;
1135 STATIC_ASSERT (sizeof (cpusetp->__bits[0]) == sizeof (unsigned long int));
1136 for (i = 0; i < cpusetsize / sizeof (cpusetp->__bits[0]); i++)
1138 unsigned long int mask = cpusetp->__bits[i];
1139 if (mask == 0)
1140 continue;
1141 ret += __builtin_popcountl (mask);
1143 return ret;
1144 #endif
1146 #endif
1148 /* At startup, determine the default number of threads. It would seem
1149 this should be related to the number of cpus online. */
1151 static void
1152 init_num_threads (void)
1154 #ifdef HAVE_PTHREAD_AFFINITY_NP
1155 #if defined (_SC_NPROCESSORS_CONF) && defined (CPU_ALLOC_SIZE)
1156 cpuset_size = sysconf (_SC_NPROCESSORS_CONF);
1157 cpuset_size = CPU_ALLOC_SIZE (cpuset_size);
1158 #else
1159 cpuset_size = sizeof (cpu_set_t);
1160 #endif
1162 cpusetp = (cpu_set_t *) xmalloc (gomp_cpuset_size);
1165 int ret = pthread_getaffinity_np (pthread_self (), gomp_cpuset_size,
1166 cpusetp);
1167 if (ret == 0)
1169 /* Count only the CPUs this process can use. */
1170 nthreads_var = cpuset_popcount (cpuset_size, cpusetp);
1171 if (nthreads_var == 0)
1172 break;
1173 get_cpuset_size = cpuset_size;
1174 #ifdef CPU_ALLOC_SIZE
1175 unsigned long i;
1176 for (i = cpuset_size * 8; i; i--)
1177 if (CPU_ISSET_S (i - 1, cpuset_size, cpusetp))
1178 break;
1179 cpuset_size = CPU_ALLOC_SIZE (i);
1180 #endif
1181 return;
1183 if (ret != EINVAL)
1184 break;
1185 #ifdef CPU_ALLOC_SIZE
1186 if (cpuset_size < sizeof (cpu_set_t))
1187 cpuset_size = sizeof (cpu_set_t);
1188 else
1189 cpuset_size = cpuset_size * 2;
1190 if (cpuset_size < 8 * sizeof (cpu_set_t))
1191 cpusetp
1192 = (cpu_set_t *) realloc (cpusetp, cpuset_size);
1193 else
1195 /* Avoid fatal if too large memory allocation would be
1196 requested, e.g. kernel returning EINVAL all the time. */
1197 void *p = realloc (cpusetp, cpuset_size);
1198 if (p == NULL)
1199 break;
1200 cpusetp = (cpu_set_t *) p;
1202 #else
1203 break;
1204 #endif
1206 while (1);
1207 cpuset_size = 0;
1208 nthreads_var = 1;
1209 free (cpusetp);
1210 cpusetp = NULL;
1211 #endif
1212 #ifdef _SC_NPROCESSORS_ONLN
1213 nthreads_var = sysconf (_SC_NPROCESSORS_ONLN);
1214 #endif
1217 /* FIXME: once using -std=c11, we can use std::thread::hardware_concurrency. */
1219 /* Return true when a jobserver is running and can accept a job. */
1221 static bool
1222 jobserver_active_p (void)
1224 const char *makeflags = getenv ("MAKEFLAGS");
1225 if (makeflags == NULL)
1226 return false;
1228 const char *needle = "--jobserver-auth=";
1229 const char *n = strstr (makeflags, needle);
1230 if (n == NULL)
1231 return false;
1233 int rfd = -1;
1234 int wfd = -1;
1236 return (sscanf (n + strlen (needle), "%d,%d", &rfd, &wfd) == 2
1237 && rfd > 0
1238 && wfd > 0
1239 && is_valid_fd (rfd)
1240 && is_valid_fd (wfd));
1243 /* Execute gcc. ARGC is the number of arguments. ARGV contains the arguments. */
1245 static void
1246 run_gcc (unsigned argc, char *argv[])
1248 unsigned i, j;
1249 const char **new_argv;
1250 const char **argv_ptr;
1251 char *list_option_full = NULL;
1252 const char *linker_output = NULL;
1253 const char *collect_gcc, *collect_gcc_options;
1254 int parallel = 0;
1255 int jobserver = 0;
1256 int auto_parallel = 0;
1257 bool no_partition = false;
1258 struct cl_decoded_option *fdecoded_options = NULL;
1259 struct cl_decoded_option *offload_fdecoded_options = NULL;
1260 unsigned int fdecoded_options_count = 0;
1261 unsigned int offload_fdecoded_options_count = 0;
1262 struct cl_decoded_option *decoded_options;
1263 unsigned int decoded_options_count;
1264 struct obstack argv_obstack;
1265 int new_head_argc;
1266 bool have_lto = false;
1267 bool have_offload = false;
1268 unsigned lto_argc = 0, ltoobj_argc = 0;
1269 char **lto_argv, **ltoobj_argv;
1270 bool linker_output_rel = false;
1271 bool skip_debug = false;
1272 unsigned n_debugobj;
1274 /* Get the driver and options. */
1275 collect_gcc = getenv ("COLLECT_GCC");
1276 if (!collect_gcc)
1277 fatal_error (input_location,
1278 "environment variable %<COLLECT_GCC%> must be set");
1279 collect_gcc_options = getenv ("COLLECT_GCC_OPTIONS");
1280 if (!collect_gcc_options)
1281 fatal_error (input_location,
1282 "environment variable %<COLLECT_GCC_OPTIONS%> must be set");
1283 get_options_from_collect_gcc_options (collect_gcc, collect_gcc_options,
1284 &decoded_options,
1285 &decoded_options_count);
1287 /* Allocate array for input object files with LTO IL,
1288 and for possible preceding arguments. */
1289 lto_argv = XNEWVEC (char *, argc);
1290 ltoobj_argv = XNEWVEC (char *, argc);
1292 /* Look at saved options in the IL files. */
1293 for (i = 1; i < argc; ++i)
1295 char *p;
1296 int fd;
1297 off_t file_offset = 0;
1298 long loffset;
1299 int consumed;
1300 char *filename = argv[i];
1302 if (strncmp (argv[i], "-foffload-objects=",
1303 sizeof ("-foffload-objects=") - 1) == 0)
1305 have_offload = true;
1306 offload_objects_file_name
1307 = argv[i] + sizeof ("-foffload-objects=") - 1;
1308 continue;
1311 if ((p = strrchr (argv[i], '@'))
1312 && p != argv[i]
1313 && sscanf (p, "@%li%n", &loffset, &consumed) >= 1
1314 && strlen (p) == (unsigned int) consumed)
1316 filename = XNEWVEC (char, p - argv[i] + 1);
1317 memcpy (filename, argv[i], p - argv[i]);
1318 filename[p - argv[i]] = '\0';
1319 file_offset = (off_t) loffset;
1321 fd = open (filename, O_RDONLY | O_BINARY);
1322 /* Linker plugin passes -fresolution and -flinker-output options.
1323 -flinker-output is passed only when user did not specify one and thus
1324 we do not need to worry about duplicities with the option handling
1325 below. */
1326 if (fd == -1)
1328 lto_argv[lto_argc++] = argv[i];
1329 if (strcmp (argv[i], "-flinker-output=rel") == 0)
1330 linker_output_rel = true;
1331 continue;
1334 if (find_and_merge_options (fd, file_offset, LTO_SECTION_NAME_PREFIX,
1335 &fdecoded_options, &fdecoded_options_count,
1336 collect_gcc))
1338 have_lto = true;
1339 ltoobj_argv[ltoobj_argc++] = argv[i];
1341 close (fd);
1344 /* Initalize the common arguments for the driver. */
1345 obstack_init (&argv_obstack);
1346 obstack_ptr_grow (&argv_obstack, collect_gcc);
1347 obstack_ptr_grow (&argv_obstack, "-xlto");
1348 obstack_ptr_grow (&argv_obstack, "-c");
1350 append_compiler_options (&argv_obstack, fdecoded_options,
1351 fdecoded_options_count);
1352 append_linker_options (&argv_obstack, decoded_options, decoded_options_count);
1354 /* Scan linker driver arguments for things that are of relevance to us. */
1355 for (j = 1; j < decoded_options_count; ++j)
1357 struct cl_decoded_option *option = &decoded_options[j];
1358 switch (option->opt_index)
1360 case OPT_o:
1361 linker_output = option->arg;
1362 break;
1364 case OPT_save_temps:
1365 save_temps = 1;
1366 break;
1368 case OPT_v:
1369 verbose = 1;
1370 break;
1372 case OPT_flto_partition_:
1373 if (strcmp (option->arg, "none") == 0)
1374 no_partition = true;
1375 break;
1377 case OPT_flto_:
1378 if (strcmp (option->arg, "jobserver") == 0)
1380 parallel = 1;
1381 jobserver = 1;
1383 else if (strcmp (option->arg, "auto") == 0)
1385 parallel = 1;
1386 auto_parallel = 1;
1388 else
1390 parallel = atoi (option->arg);
1391 if (parallel <= 1)
1392 parallel = 0;
1394 /* Fallthru. */
1396 case OPT_flto:
1397 lto_mode = LTO_MODE_WHOPR;
1398 break;
1400 case OPT_flinker_output_:
1401 linker_output_rel = !strcmp (option->arg, "rel");
1402 break;
1404 case OPT_g:
1405 /* Recognize -g0. */
1406 skip_debug = option->arg && !strcmp (option->arg, "0");
1407 break;
1409 default:
1410 break;
1414 /* Output lto-wrapper invocation command. */
1415 if (verbose)
1417 for (i = 0; i < argc; ++i)
1419 fputs (argv[i], stderr);
1420 fputc (' ', stderr);
1422 fputc ('\n', stderr);
1425 if (linker_output_rel)
1426 no_partition = true;
1428 if (no_partition)
1430 lto_mode = LTO_MODE_LTO;
1431 jobserver = 0;
1432 auto_parallel = 0;
1433 parallel = 0;
1435 else if (!jobserver && jobserver_active_p ())
1437 parallel = 1;
1438 jobserver = 1;
1441 if (linker_output)
1443 char *output_dir, *base, *name;
1444 bool bit_bucket = strcmp (linker_output, HOST_BIT_BUCKET) == 0;
1446 output_dir = xstrdup (linker_output);
1447 base = output_dir;
1448 for (name = base; *name; name++)
1449 if (IS_DIR_SEPARATOR (*name))
1450 base = name + 1;
1451 *base = '\0';
1453 linker_output = &linker_output[base - output_dir];
1454 if (*output_dir == '\0')
1456 static char current_dir[] = { '.', DIR_SEPARATOR, '\0' };
1457 output_dir = current_dir;
1459 if (!bit_bucket)
1461 obstack_ptr_grow (&argv_obstack, "-dumpdir");
1462 obstack_ptr_grow (&argv_obstack, output_dir);
1465 obstack_ptr_grow (&argv_obstack, "-dumpbase");
1468 /* Remember at which point we can scrub args to re-use the commons. */
1469 new_head_argc = obstack_object_size (&argv_obstack) / sizeof (void *);
1471 if (have_offload)
1473 unsigned i, num_offload_files;
1474 char **offload_argv;
1475 FILE *f;
1477 f = fopen (offload_objects_file_name, "r");
1478 if (f == NULL)
1479 fatal_error (input_location, "cannot open %s: %m",
1480 offload_objects_file_name);
1481 if (fscanf (f, "%u ", &num_offload_files) != 1)
1482 fatal_error (input_location, "cannot read %s: %m",
1483 offload_objects_file_name);
1484 offload_argv = XCNEWVEC (char *, num_offload_files);
1486 /* Read names of object files with offload. */
1487 for (i = 0; i < num_offload_files; i++)
1489 const unsigned piece = 32;
1490 char *buf, *filename = XNEWVEC (char, piece);
1491 size_t len;
1493 buf = filename;
1494 cont1:
1495 if (!fgets (buf, piece, f))
1496 break;
1497 len = strlen (filename);
1498 if (filename[len - 1] != '\n')
1500 filename = XRESIZEVEC (char, filename, len + piece);
1501 buf = filename + len;
1502 goto cont1;
1504 filename[len - 1] = '\0';
1505 offload_argv[i] = filename;
1507 fclose (f);
1508 if (offload_argv[num_offload_files - 1] == NULL)
1509 fatal_error (input_location, "invalid format of %s",
1510 offload_objects_file_name);
1511 maybe_unlink (offload_objects_file_name);
1512 offload_objects_file_name = NULL;
1514 /* Look at saved offload options in files. */
1515 for (i = 0; i < num_offload_files; i++)
1517 char *p;
1518 long loffset;
1519 int fd, consumed;
1520 off_t file_offset = 0;
1521 char *filename = offload_argv[i];
1523 if ((p = strrchr (offload_argv[i], '@'))
1524 && p != offload_argv[i]
1525 && sscanf (p, "@%li%n", &loffset, &consumed) >= 1
1526 && strlen (p) == (unsigned int) consumed)
1528 filename = XNEWVEC (char, p - offload_argv[i] + 1);
1529 memcpy (filename, offload_argv[i], p - offload_argv[i]);
1530 filename[p - offload_argv[i]] = '\0';
1531 file_offset = (off_t) loffset;
1533 fd = open (filename, O_RDONLY | O_BINARY);
1534 if (fd == -1)
1535 fatal_error (input_location, "cannot open %s: %m", filename);
1536 if (!find_and_merge_options (fd, file_offset,
1537 OFFLOAD_SECTION_NAME_PREFIX,
1538 &offload_fdecoded_options,
1539 &offload_fdecoded_options_count,
1540 collect_gcc))
1541 fatal_error (input_location, "cannot read %s: %m", filename);
1542 close (fd);
1543 if (filename != offload_argv[i])
1544 XDELETEVEC (filename);
1547 compile_images_for_offload_targets (num_offload_files, offload_argv,
1548 offload_fdecoded_options,
1549 offload_fdecoded_options_count,
1550 decoded_options,
1551 decoded_options_count);
1553 free_array_of_ptrs ((void **) offload_argv, num_offload_files);
1555 if (offload_names)
1557 find_crtoffloadtable ();
1558 for (i = 0; offload_names[i]; i++)
1559 printf ("%s\n", offload_names[i]);
1560 free_array_of_ptrs ((void **) offload_names, i);
1564 /* If object files contain offload sections, but do not contain LTO sections,
1565 then there is no need to perform a link-time recompilation, i.e.
1566 lto-wrapper is used only for a compilation of offload images. */
1567 if (have_offload && !have_lto)
1568 goto finish;
1570 if (lto_mode == LTO_MODE_LTO)
1572 if (linker_output)
1574 obstack_ptr_grow (&argv_obstack, linker_output);
1575 flto_out = (char *) xmalloc (strlen (linker_output)
1576 + sizeof (".lto.o") + 1);
1577 strcpy (flto_out, linker_output);
1578 strcat (flto_out, ".lto.o");
1580 else
1581 flto_out = make_temp_file (".lto.o");
1582 obstack_ptr_grow (&argv_obstack, "-o");
1583 obstack_ptr_grow (&argv_obstack, flto_out);
1585 else
1587 const char *list_option = "-fltrans-output-list=";
1588 size_t list_option_len = strlen (list_option);
1589 char *tmp;
1591 if (linker_output)
1593 char *dumpbase = (char *) xmalloc (strlen (linker_output)
1594 + sizeof (".wpa") + 1);
1595 strcpy (dumpbase, linker_output);
1596 strcat (dumpbase, ".wpa");
1597 obstack_ptr_grow (&argv_obstack, dumpbase);
1600 if (linker_output && save_temps)
1602 ltrans_output_file = (char *) xmalloc (strlen (linker_output)
1603 + sizeof (".ltrans.out") + 1);
1604 strcpy (ltrans_output_file, linker_output);
1605 strcat (ltrans_output_file, ".ltrans.out");
1607 else
1609 char *prefix = NULL;
1610 if (linker_output)
1612 prefix = (char *) xmalloc (strlen (linker_output) + 2);
1613 strcpy (prefix, linker_output);
1614 strcat (prefix, ".");
1617 ltrans_output_file = make_temp_file_with_prefix (prefix,
1618 ".ltrans.out");
1619 free (prefix);
1621 list_option_full = (char *) xmalloc (sizeof (char) *
1622 (strlen (ltrans_output_file) + list_option_len + 1));
1623 tmp = list_option_full;
1625 obstack_ptr_grow (&argv_obstack, tmp);
1626 strcpy (tmp, list_option);
1627 tmp += list_option_len;
1628 strcpy (tmp, ltrans_output_file);
1630 if (jobserver)
1632 if (verbose)
1633 fprintf (stderr, "Using make jobserver\n");
1634 obstack_ptr_grow (&argv_obstack, xstrdup ("-fwpa=jobserver"));
1636 else if (auto_parallel)
1638 char buf[256];
1639 init_num_threads ();
1640 if (verbose)
1641 fprintf (stderr, "LTO parallelism level set to %ld\n",
1642 nthreads_var);
1643 sprintf (buf, "-fwpa=%ld", nthreads_var);
1644 obstack_ptr_grow (&argv_obstack, xstrdup (buf));
1646 else if (parallel > 1)
1648 char buf[256];
1649 sprintf (buf, "-fwpa=%i", parallel);
1650 obstack_ptr_grow (&argv_obstack, xstrdup (buf));
1652 else
1653 obstack_ptr_grow (&argv_obstack, "-fwpa");
1656 /* Append input arguments. */
1657 for (i = 0; i < lto_argc; ++i)
1658 obstack_ptr_grow (&argv_obstack, lto_argv[i]);
1659 /* Append the input objects. */
1660 for (i = 0; i < ltoobj_argc; ++i)
1661 obstack_ptr_grow (&argv_obstack, ltoobj_argv[i]);
1662 obstack_ptr_grow (&argv_obstack, NULL);
1664 new_argv = XOBFINISH (&argv_obstack, const char **);
1665 argv_ptr = &new_argv[new_head_argc];
1666 fork_execute (new_argv[0], CONST_CAST (char **, new_argv), true);
1668 /* Copy the early generated debug info from the objects to temporary
1669 files and append those to the partial link commandline. */
1670 n_debugobj = 0;
1671 early_debug_object_names = NULL;
1672 if (! skip_debug)
1674 early_debug_object_names = XCNEWVEC (const char *, ltoobj_argc+ 1);
1675 num_deb_objs = ltoobj_argc;
1676 for (i = 0; i < ltoobj_argc; ++i)
1678 const char *tem;
1679 if ((tem = debug_objcopy (ltoobj_argv[i], !linker_output_rel)))
1681 early_debug_object_names[i] = tem;
1682 n_debugobj++;
1687 if (lto_mode == LTO_MODE_LTO)
1689 printf ("%s\n", flto_out);
1690 if (!skip_debug)
1692 for (i = 0; i < ltoobj_argc; ++i)
1693 if (early_debug_object_names[i] != NULL)
1694 printf ("%s\n", early_debug_object_names[i]);
1696 /* These now belong to collect2. */
1697 free (flto_out);
1698 flto_out = NULL;
1699 free (early_debug_object_names);
1700 early_debug_object_names = NULL;
1702 else
1704 FILE *stream = fopen (ltrans_output_file, "r");
1705 FILE *mstream = NULL;
1706 struct obstack env_obstack;
1707 int priority;
1709 if (!stream)
1710 fatal_error (input_location, "%<fopen%>: %s: %m", ltrans_output_file);
1712 /* Parse the list of LTRANS inputs from the WPA stage. */
1713 obstack_init (&env_obstack);
1714 nr = 0;
1715 for (;;)
1717 const unsigned piece = 32;
1718 char *output_name = NULL;
1719 char *buf, *input_name = (char *)xmalloc (piece);
1720 size_t len;
1722 buf = input_name;
1723 if (fscanf (stream, "%i\n", &priority) != 1)
1725 if (!feof (stream))
1726 fatal_error (input_location,
1727 "corrupted ltrans output file %s",
1728 ltrans_output_file);
1729 break;
1731 cont:
1732 if (!fgets (buf, piece, stream))
1733 break;
1734 len = strlen (input_name);
1735 if (input_name[len - 1] != '\n')
1737 input_name = (char *)xrealloc (input_name, len + piece);
1738 buf = input_name + len;
1739 goto cont;
1741 input_name[len - 1] = '\0';
1743 if (input_name[0] == '*')
1744 output_name = &input_name[1];
1746 nr++;
1747 ltrans_priorities
1748 = (int *)xrealloc (ltrans_priorities, nr * sizeof (int) * 2);
1749 input_names = (char **)xrealloc (input_names, nr * sizeof (char *));
1750 output_names = (char **)xrealloc (output_names, nr * sizeof (char *));
1751 ltrans_priorities[(nr-1)*2] = priority;
1752 ltrans_priorities[(nr-1)*2+1] = nr-1;
1753 input_names[nr-1] = input_name;
1754 output_names[nr-1] = output_name;
1756 fclose (stream);
1757 maybe_unlink (ltrans_output_file);
1758 ltrans_output_file = NULL;
1760 if (parallel)
1762 makefile = make_temp_file (".mk");
1763 mstream = fopen (makefile, "w");
1764 qsort (ltrans_priorities, nr, sizeof (int) * 2, cmp_priority);
1767 /* Execute the LTRANS stage for each input file (or prepare a
1768 makefile to invoke this in parallel). */
1769 for (i = 0; i < nr; ++i)
1771 char *output_name;
1772 char *input_name = input_names[i];
1773 /* If it's a pass-through file do nothing. */
1774 if (output_names[i])
1775 continue;
1777 /* Replace the .o suffix with a .ltrans.o suffix and write
1778 the resulting name to the LTRANS output list. */
1779 obstack_grow (&env_obstack, input_name, strlen (input_name) - 2);
1780 obstack_grow (&env_obstack, ".ltrans.o", sizeof (".ltrans.o"));
1781 output_name = XOBFINISH (&env_obstack, char *);
1783 /* Adjust the dumpbase if the linker output file was seen. */
1784 if (linker_output)
1786 char *dumpbase
1787 = (char *) xmalloc (strlen (linker_output)
1788 + sizeof (DUMPBASE_SUFFIX) + 1);
1789 snprintf (dumpbase,
1790 strlen (linker_output) + sizeof (DUMPBASE_SUFFIX),
1791 "%s.ltrans%u", linker_output, i);
1792 argv_ptr[0] = dumpbase;
1795 argv_ptr[1] = "-fltrans";
1796 argv_ptr[2] = "-o";
1797 argv_ptr[3] = output_name;
1798 argv_ptr[4] = input_name;
1799 argv_ptr[5] = NULL;
1800 if (parallel)
1802 fprintf (mstream, "%s:\n\t@%s ", output_name, new_argv[0]);
1803 for (j = 1; new_argv[j] != NULL; ++j)
1804 fprintf (mstream, " '%s'", new_argv[j]);
1805 fprintf (mstream, "\n");
1806 /* If we are not preserving the ltrans input files then
1807 truncate them as soon as we have processed it. This
1808 reduces temporary disk-space usage. */
1809 if (! save_temps)
1810 fprintf (mstream, "\t@-touch -r %s %s.tem > /dev/null 2>&1 "
1811 "&& mv %s.tem %s\n",
1812 input_name, input_name, input_name, input_name);
1814 else
1816 fork_execute (new_argv[0], CONST_CAST (char **, new_argv),
1817 true);
1818 maybe_unlink (input_name);
1821 output_names[i] = output_name;
1823 if (parallel)
1825 struct pex_obj *pex;
1826 char jobs[32];
1828 fprintf (mstream,
1829 ".PHONY: all\n"
1830 "all:");
1831 for (i = 0; i < nr; ++i)
1833 int j = ltrans_priorities[i*2 + 1];
1834 fprintf (mstream, " \\\n\t%s", output_names[j]);
1836 fprintf (mstream, "\n");
1837 fclose (mstream);
1838 if (!jobserver)
1840 /* Avoid passing --jobserver-fd= and similar flags
1841 unless jobserver mode is explicitly enabled. */
1842 putenv (xstrdup ("MAKEFLAGS="));
1843 putenv (xstrdup ("MFLAGS="));
1845 new_argv[0] = getenv ("MAKE");
1846 if (!new_argv[0])
1847 new_argv[0] = "make";
1848 new_argv[1] = "-f";
1849 new_argv[2] = makefile;
1850 i = 3;
1851 if (!jobserver)
1853 snprintf (jobs, 31, "-j%ld",
1854 auto_parallel ? nthreads_var : parallel);
1855 new_argv[i++] = jobs;
1857 new_argv[i++] = "all";
1858 new_argv[i++] = NULL;
1859 pex = collect_execute (new_argv[0], CONST_CAST (char **, new_argv),
1860 NULL, NULL, PEX_SEARCH, false);
1861 do_wait (new_argv[0], pex);
1862 maybe_unlink (makefile);
1863 makefile = NULL;
1864 for (i = 0; i < nr; ++i)
1865 maybe_unlink (input_names[i]);
1867 for (i = 0; i < nr; ++i)
1869 fputs (output_names[i], stdout);
1870 putc ('\n', stdout);
1871 free (input_names[i]);
1873 if (!skip_debug)
1875 for (i = 0; i < ltoobj_argc; ++i)
1876 if (early_debug_object_names[i] != NULL)
1877 printf ("%s\n", early_debug_object_names[i]);
1879 nr = 0;
1880 free (ltrans_priorities);
1881 free (output_names);
1882 output_names = NULL;
1883 free (early_debug_object_names);
1884 early_debug_object_names = NULL;
1885 free (input_names);
1886 free (list_option_full);
1887 obstack_free (&env_obstack, NULL);
1890 finish:
1891 XDELETE (lto_argv);
1892 obstack_free (&argv_obstack, NULL);
1896 /* Entry point. */
1899 main (int argc, char *argv[])
1901 const char *p;
1903 init_opts_obstack ();
1905 p = argv[0] + strlen (argv[0]);
1906 while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
1907 --p;
1908 progname = p;
1910 xmalloc_set_program_name (progname);
1912 gcc_init_libintl ();
1914 diagnostic_initialize (global_dc, 0);
1916 if (atexit (lto_wrapper_cleanup) != 0)
1917 fatal_error (input_location, "%<atexit%> failed");
1919 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
1920 signal (SIGINT, fatal_signal);
1921 #ifdef SIGHUP
1922 if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
1923 signal (SIGHUP, fatal_signal);
1924 #endif
1925 if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
1926 signal (SIGTERM, fatal_signal);
1927 #ifdef SIGPIPE
1928 if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
1929 signal (SIGPIPE, fatal_signal);
1930 #endif
1931 #ifdef SIGCHLD
1932 /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
1933 receive the signal. A different setting is inheritable */
1934 signal (SIGCHLD, SIG_DFL);
1935 #endif
1937 /* We may be called with all the arguments stored in some file and
1938 passed with @file. Expand them into argv before processing. */
1939 expandargv (&argc, &argv);
1941 run_gcc (argc, argv);
1943 return 0;