re PR fortran/88376 (ICE in is_illegal_recursion, at fortran/resolve.c:1689)
[official-gcc.git] / gcc / lto-wrapper.c
blobc079c7b7161525109b8d0fa30b771dd0d5e6e7ac
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 according to LANG_MASK. */
133 static void
134 get_options_from_collect_gcc_options (const char *collect_gcc,
135 const char *collect_gcc_options,
136 unsigned int lang_mask,
137 struct cl_decoded_option **decoded_options,
138 unsigned int *decoded_options_count)
140 struct obstack argv_obstack;
141 char *argv_storage;
142 const char **argv;
143 int j, k, argc;
145 argv_storage = xstrdup (collect_gcc_options);
146 obstack_init (&argv_obstack);
147 obstack_ptr_grow (&argv_obstack, collect_gcc);
149 for (j = 0, k = 0; argv_storage[j] != '\0'; ++j)
151 if (argv_storage[j] == '\'')
153 obstack_ptr_grow (&argv_obstack, &argv_storage[k]);
154 ++j;
157 if (argv_storage[j] == '\0')
158 fatal_error (input_location, "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,
179 lang_mask,
180 decoded_options, decoded_options_count);
181 obstack_free (&argv_obstack, NULL);
184 /* Append OPTION to the options array DECODED_OPTIONS with size
185 DECODED_OPTIONS_COUNT. */
187 static void
188 append_option (struct cl_decoded_option **decoded_options,
189 unsigned int *decoded_options_count,
190 struct cl_decoded_option *option)
192 ++*decoded_options_count;
193 *decoded_options
194 = (struct cl_decoded_option *)
195 xrealloc (*decoded_options,
196 (*decoded_options_count
197 * sizeof (struct cl_decoded_option)));
198 memcpy (&(*decoded_options)[*decoded_options_count - 1], option,
199 sizeof (struct cl_decoded_option));
202 /* Remove option number INDEX from DECODED_OPTIONS, update
203 DECODED_OPTIONS_COUNT. */
205 static void
206 remove_option (struct cl_decoded_option **decoded_options,
207 int index, unsigned int *decoded_options_count)
209 --*decoded_options_count;
210 memmove (&(*decoded_options)[index + 1],
211 &(*decoded_options)[index],
212 sizeof (struct cl_decoded_option)
213 * (*decoded_options_count - index));
216 /* Try to merge and complain about options FDECODED_OPTIONS when applied
217 ontop of DECODED_OPTIONS. */
219 static void
220 merge_and_complain (struct cl_decoded_option **decoded_options,
221 unsigned int *decoded_options_count,
222 struct cl_decoded_option *fdecoded_options,
223 unsigned int fdecoded_options_count)
225 unsigned int i, j;
226 struct cl_decoded_option *pic_option = NULL;
227 struct cl_decoded_option *pie_option = NULL;
229 /* ??? Merge options from files. Most cases can be
230 handled by either unioning or intersecting
231 (for example -fwrapv is a case for unioning,
232 -ffast-math is for intersection). Most complaints
233 about real conflicts between different options can
234 be deferred to the compiler proper. Options that
235 we can neither safely handle by intersection nor
236 unioning would need to be complained about here.
237 Ideally we'd have a flag in the opt files that
238 tells whether to union or intersect or reject.
239 In absence of that it's unclear what a good default is.
240 It's also difficult to get positional handling correct. */
242 /* The following does what the old LTO option code did,
243 union all target and a selected set of common options. */
244 for (i = 0; i < fdecoded_options_count; ++i)
246 struct cl_decoded_option *foption = &fdecoded_options[i];
247 switch (foption->opt_index)
249 case OPT_SPECIAL_unknown:
250 case OPT_SPECIAL_ignore:
251 case OPT_SPECIAL_deprecated:
252 case OPT_SPECIAL_program_name:
253 case OPT_SPECIAL_input_file:
254 break;
256 default:
257 if (!(cl_options[foption->opt_index].flags & CL_TARGET))
258 break;
260 /* Fallthru. */
261 case OPT_fdiagnostics_show_caret:
262 case OPT_fdiagnostics_show_labels:
263 case OPT_fdiagnostics_show_line_numbers:
264 case OPT_fdiagnostics_show_option:
265 case OPT_fdiagnostics_show_location_:
266 case OPT_fshow_column:
267 case OPT_fcommon:
268 case OPT_fgnu_tm:
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_O:
622 case OPT_Ofast:
623 case OPT_Og:
624 case OPT_Os:
625 break;
627 default:
628 if (!(cl_options[option->opt_index].flags & CL_TARGET))
629 continue;
632 /* Pass the option on. */
633 for (unsigned int i = 0; i < option->canonical_option_num_elements; ++i)
634 obstack_ptr_grow (argv_obstack, option->canonical_option[i]);
638 /* Append diag options in OPTS with length COUNT to ARGV_OBSTACK. */
640 static void
641 append_diag_options (obstack *argv_obstack, struct cl_decoded_option *opts,
642 unsigned int count)
644 /* Append compiler driver arguments as far as they were merged. */
645 for (unsigned int j = 1; j < count; ++j)
647 struct cl_decoded_option *option = &opts[j];
649 switch (option->opt_index)
651 case OPT_fdiagnostics_color_:
652 case OPT_fdiagnostics_format_:
653 case OPT_fdiagnostics_show_caret:
654 case OPT_fdiagnostics_show_labels:
655 case OPT_fdiagnostics_show_line_numbers:
656 case OPT_fdiagnostics_show_option:
657 case OPT_fdiagnostics_show_location_:
658 case OPT_fshow_column:
659 break;
660 default:
661 continue;
664 /* Pass the option on. */
665 for (unsigned int i = 0; i < option->canonical_option_num_elements; ++i)
666 obstack_ptr_grow (argv_obstack, option->canonical_option[i]);
671 /* Append linker options OPTS to ARGV_OBSTACK. */
673 static void
674 append_linker_options (obstack *argv_obstack, struct cl_decoded_option *opts,
675 unsigned int count)
677 /* Append linker driver arguments. Compiler options from the linker
678 driver arguments will override / merge with those from the compiler. */
679 for (unsigned int j = 1; j < count; ++j)
681 struct cl_decoded_option *option = &opts[j];
683 /* Do not pass on frontend specific flags not suitable for lto. */
684 if (!(cl_options[option->opt_index].flags
685 & (CL_COMMON|CL_TARGET|CL_DRIVER|CL_LTO)))
686 continue;
688 switch (option->opt_index)
690 case OPT_o:
691 case OPT_flto_:
692 case OPT_flto:
693 /* We've handled these LTO options, do not pass them on. */
694 continue;
696 case OPT_fopenmp:
697 case OPT_fopenacc:
698 /* Ignore -fno-XXX form of these options, as otherwise
699 corresponding builtins will not be enabled. */
700 if (option->value == 0)
701 continue;
702 break;
704 default:
705 break;
708 /* Pass the option on. */
709 for (unsigned int i = 0; i < option->canonical_option_num_elements; ++i)
710 obstack_ptr_grow (argv_obstack, option->canonical_option[i]);
714 /* Extract options for TARGET offload compiler from OPTIONS and append
715 them to ARGV_OBSTACK. */
717 static void
718 append_offload_options (obstack *argv_obstack, const char *target,
719 struct cl_decoded_option *options,
720 unsigned int options_count)
722 for (unsigned i = 0; i < options_count; i++)
724 const char *cur, *next, *opts;
725 char **argv;
726 unsigned argc;
727 struct cl_decoded_option *option = &options[i];
729 if (option->opt_index != OPT_foffload_)
730 continue;
732 /* If option argument starts with '-' then no target is specified. That
733 means offload options are specified for all targets, so we need to
734 append them. */
735 if (option->arg[0] == '-')
736 opts = option->arg;
737 else
739 opts = strchr (option->arg, '=');
740 /* If there are offload targets specified, but no actual options,
741 there is nothing to do here. */
742 if (!opts)
743 continue;
745 cur = option->arg;
747 while (cur < opts)
749 next = strchr (cur, ',');
750 if (next == NULL)
751 next = opts;
752 next = (next > opts) ? opts : next;
754 /* Are we looking for this offload target? */
755 if (strlen (target) == (size_t) (next - cur)
756 && strncmp (target, cur, next - cur) == 0)
757 break;
759 /* Skip the comma or equal sign. */
760 cur = next + 1;
763 if (cur >= opts)
764 continue;
766 opts++;
769 argv = buildargv (opts);
770 for (argc = 0; argv[argc]; argc++)
771 obstack_ptr_grow (argv_obstack, argv[argc]);
775 /* Check whether NAME can be accessed in MODE. This is like access,
776 except that it never considers directories to be executable. */
778 static int
779 access_check (const char *name, int mode)
781 if (mode == X_OK)
783 struct stat st;
785 if (stat (name, &st) < 0
786 || S_ISDIR (st.st_mode))
787 return -1;
790 return access (name, mode);
793 /* Prepare a target image for offload TARGET, using mkoffload tool from
794 COMPILER_PATH. Return the name of the resultant object file. */
796 static char *
797 compile_offload_image (const char *target, const char *compiler_path,
798 unsigned in_argc, char *in_argv[],
799 struct cl_decoded_option *compiler_opts,
800 unsigned int compiler_opt_count,
801 struct cl_decoded_option *linker_opts,
802 unsigned int linker_opt_count)
804 char *filename = NULL;
805 char **argv;
806 char *suffix
807 = XALLOCAVEC (char, sizeof ("/accel//mkoffload") + strlen (target));
808 strcpy (suffix, "/accel/");
809 strcat (suffix, target);
810 strcat (suffix, "/mkoffload");
812 char **paths = NULL;
813 unsigned n_paths = parse_env_var (compiler_path, &paths, suffix);
815 const char *compiler = NULL;
816 for (unsigned i = 0; i < n_paths; i++)
817 if (access_check (paths[i], X_OK) == 0)
819 compiler = paths[i];
820 break;
823 if (!compiler)
824 fatal_error (input_location,
825 "could not find %s in %s (consider using '-B')\n", suffix + 1,
826 compiler_path);
828 /* Generate temporary output file name. */
829 filename = make_temp_file (".target.o");
831 struct obstack argv_obstack;
832 obstack_init (&argv_obstack);
833 obstack_ptr_grow (&argv_obstack, compiler);
834 if (save_temps)
835 obstack_ptr_grow (&argv_obstack, "-save-temps");
836 if (verbose)
837 obstack_ptr_grow (&argv_obstack, "-v");
838 obstack_ptr_grow (&argv_obstack, "-o");
839 obstack_ptr_grow (&argv_obstack, filename);
841 /* Append names of input object files. */
842 for (unsigned i = 0; i < in_argc; i++)
843 obstack_ptr_grow (&argv_obstack, in_argv[i]);
845 /* Append options from offload_lto sections. */
846 append_compiler_options (&argv_obstack, compiler_opts,
847 compiler_opt_count);
848 append_diag_options (&argv_obstack, linker_opts, linker_opt_count);
850 /* Append options specified by -foffload last. In case of conflicting
851 options we expect offload compiler to choose the latest. */
852 append_offload_options (&argv_obstack, target, compiler_opts,
853 compiler_opt_count);
854 append_offload_options (&argv_obstack, target, linker_opts,
855 linker_opt_count);
857 obstack_ptr_grow (&argv_obstack, NULL);
858 argv = XOBFINISH (&argv_obstack, char **);
859 fork_execute (argv[0], argv, true);
860 obstack_free (&argv_obstack, NULL);
862 free_array_of_ptrs ((void **) paths, n_paths);
863 return filename;
867 /* The main routine dealing with offloading.
868 The routine builds a target image for each offload target. IN_ARGC and
869 IN_ARGV specify options and input object files. As all of them could contain
870 target sections, we pass them all to target compilers. */
872 static void
873 compile_images_for_offload_targets (unsigned in_argc, char *in_argv[],
874 struct cl_decoded_option *compiler_opts,
875 unsigned int compiler_opt_count,
876 struct cl_decoded_option *linker_opts,
877 unsigned int linker_opt_count)
879 char **names = NULL;
880 const char *target_names = getenv (OFFLOAD_TARGET_NAMES_ENV);
881 if (!target_names)
882 return;
883 unsigned num_targets = parse_env_var (target_names, &names, NULL);
885 int next_name_entry = 0;
886 const char *compiler_path = getenv ("COMPILER_PATH");
887 if (!compiler_path)
888 goto out;
890 /* Prepare an image for each target and save the name of the resultant object
891 file to the OFFLOAD_NAMES array. It is terminated by a NULL entry. */
892 offload_names = XCNEWVEC (char *, num_targets + 1);
893 for (unsigned i = 0; i < num_targets; i++)
895 /* HSA does not use LTO-like streaming and a different compiler, skip
896 it. */
897 if (strcmp (names[i], "hsa") == 0)
898 continue;
900 offload_names[next_name_entry]
901 = compile_offload_image (names[i], compiler_path, in_argc, in_argv,
902 compiler_opts, compiler_opt_count,
903 linker_opts, linker_opt_count);
904 if (!offload_names[next_name_entry])
905 fatal_error (input_location,
906 "problem with building target image for %s\n", names[i]);
907 next_name_entry++;
910 out:
911 free_array_of_ptrs ((void **) names, num_targets);
914 /* Copy a file from SRC to DEST. */
916 static void
917 copy_file (const char *dest, const char *src)
919 FILE *d = fopen (dest, "wb");
920 FILE *s = fopen (src, "rb");
921 char buffer[512];
922 while (!feof (s))
924 size_t len = fread (buffer, 1, 512, s);
925 if (ferror (s) != 0)
926 fatal_error (input_location, "reading input file");
927 if (len > 0)
929 fwrite (buffer, 1, len, d);
930 if (ferror (d) != 0)
931 fatal_error (input_location, "writing output file");
934 fclose (d);
935 fclose (s);
938 /* Find the crtoffloadtable.o file in LIBRARY_PATH, make copy and pass name of
939 the copy to the linker. */
941 static void
942 find_crtoffloadtable (void)
944 char **paths = NULL;
945 const char *library_path = getenv ("LIBRARY_PATH");
946 if (!library_path)
947 return;
948 unsigned n_paths = parse_env_var (library_path, &paths, "/crtoffloadtable.o");
950 unsigned i;
951 for (i = 0; i < n_paths; i++)
952 if (access_check (paths[i], R_OK) == 0)
954 /* The linker will delete the filename we give it, so make a copy. */
955 char *crtoffloadtable = make_temp_file (".crtoffloadtable.o");
956 copy_file (crtoffloadtable, paths[i]);
957 printf ("%s\n", crtoffloadtable);
958 XDELETEVEC (crtoffloadtable);
959 break;
961 if (i == n_paths)
962 fatal_error (input_location,
963 "installation error, can't find crtoffloadtable.o");
965 free_array_of_ptrs ((void **) paths, n_paths);
968 /* A subroutine of run_gcc. Examine the open file FD for lto sections with
969 name prefix PREFIX, at FILE_OFFSET, and store any options we find in OPTS
970 and OPT_COUNT. Return true if we found a matchingn section, false
971 otherwise. COLLECT_GCC holds the value of the environment variable with
972 the same name. */
974 static bool
975 find_and_merge_options (int fd, off_t file_offset, const char *prefix,
976 struct cl_decoded_option **opts,
977 unsigned int *opt_count, const char *collect_gcc)
979 off_t offset, length;
980 char *data;
981 char *fopts;
982 const char *errmsg;
983 int err;
984 struct cl_decoded_option *fdecoded_options = *opts;
985 unsigned int fdecoded_options_count = *opt_count;
987 simple_object_read *sobj;
988 sobj = simple_object_start_read (fd, file_offset, "__GNU_LTO",
989 &errmsg, &err);
990 if (!sobj)
991 return false;
993 char *secname = XALLOCAVEC (char, strlen (prefix) + sizeof (".opts"));
994 strcpy (secname, prefix);
995 strcat (secname, ".opts");
996 if (!simple_object_find_section (sobj, secname, &offset, &length,
997 &errmsg, &err))
999 simple_object_release_read (sobj);
1000 return false;
1003 lseek (fd, file_offset + offset, SEEK_SET);
1004 data = (char *)xmalloc (length);
1005 read (fd, data, length);
1006 fopts = data;
1009 struct cl_decoded_option *f2decoded_options;
1010 unsigned int f2decoded_options_count;
1011 get_options_from_collect_gcc_options (collect_gcc,
1012 fopts, CL_LANG_ALL,
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 off_t inoff = 0;
1048 long loffset;
1049 int consumed;
1050 if ((p = strrchr (infile, '@'))
1051 && p != infile
1052 && sscanf (p, "@%li%n", &loffset, &consumed) >= 1
1053 && strlen (p) == (unsigned int) consumed)
1055 char *fname = xstrdup (infile);
1056 fname[p - infile] = '\0';
1057 infile = fname;
1058 inoff = (off_t) loffset;
1060 int infd = open (infile, O_RDONLY | O_BINARY);
1061 if (infd == -1)
1062 return NULL;
1063 simple_object_read *inobj = simple_object_start_read (infd, inoff,
1064 "__GNU_LTO",
1065 &errmsg, &err);
1066 if (!inobj)
1067 return NULL;
1069 off_t off, len;
1070 if (simple_object_find_section (inobj, ".gnu.debuglto_.debug_info",
1071 &off, &len, &errmsg, &err) != 1)
1073 if (errmsg)
1074 fatal_error (0, "%s: %s\n", errmsg, xstrerror (err));
1076 simple_object_release_read (inobj);
1077 close (infd);
1078 return NULL;
1081 if (save_temps)
1083 outfile = (char *) xmalloc (strlen (infile)
1084 + sizeof (".debug.temp.o") + 1);
1085 strcpy (outfile, infile);
1086 strcat (outfile, ".debug.temp.o");
1088 else
1089 outfile = make_temp_file (".debug.temp.o");
1090 errmsg = simple_object_copy_lto_debug_sections (inobj, outfile, &err, rename);
1091 if (errmsg)
1093 unlink_if_ordinary (outfile);
1094 fatal_error (0, "%s: %s\n", errmsg, xstrerror (err));
1097 simple_object_release_read (inobj);
1098 close (infd);
1100 return outfile;
1103 /* Helper for qsort: compare priorities for parallel compilation. */
1106 cmp_priority (const void *a, const void *b)
1108 return *((const int *)b)-*((const int *)a);
1112 /* Execute gcc. ARGC is the number of arguments. ARGV contains the arguments. */
1114 static void
1115 run_gcc (unsigned argc, char *argv[])
1117 unsigned i, j;
1118 const char **new_argv;
1119 const char **argv_ptr;
1120 char *list_option_full = NULL;
1121 const char *linker_output = NULL;
1122 const char *collect_gcc, *collect_gcc_options;
1123 int parallel = 0;
1124 int jobserver = 0;
1125 bool no_partition = false;
1126 struct cl_decoded_option *fdecoded_options = NULL;
1127 struct cl_decoded_option *offload_fdecoded_options = NULL;
1128 unsigned int fdecoded_options_count = 0;
1129 unsigned int offload_fdecoded_options_count = 0;
1130 struct cl_decoded_option *decoded_options;
1131 unsigned int decoded_options_count;
1132 struct obstack argv_obstack;
1133 int new_head_argc;
1134 bool have_lto = false;
1135 bool have_offload = false;
1136 unsigned lto_argc = 0, ltoobj_argc = 0;
1137 char **lto_argv, **ltoobj_argv;
1138 bool linker_output_rel = false;
1139 bool skip_debug = false;
1140 unsigned n_debugobj;
1142 /* Get the driver and options. */
1143 collect_gcc = getenv ("COLLECT_GCC");
1144 if (!collect_gcc)
1145 fatal_error (input_location,
1146 "environment variable COLLECT_GCC must be set");
1147 collect_gcc_options = getenv ("COLLECT_GCC_OPTIONS");
1148 if (!collect_gcc_options)
1149 fatal_error (input_location,
1150 "environment variable COLLECT_GCC_OPTIONS must be set");
1151 get_options_from_collect_gcc_options (collect_gcc, collect_gcc_options,
1152 CL_LANG_ALL,
1153 &decoded_options,
1154 &decoded_options_count);
1156 /* Allocate array for input object files with LTO IL,
1157 and for possible preceding arguments. */
1158 lto_argv = XNEWVEC (char *, argc);
1159 ltoobj_argv = XNEWVEC (char *, argc);
1161 /* Look at saved options in the IL files. */
1162 for (i = 1; i < argc; ++i)
1164 char *p;
1165 int fd;
1166 off_t file_offset = 0;
1167 long loffset;
1168 int consumed;
1169 char *filename = argv[i];
1171 if (strncmp (argv[i], "-foffload-objects=",
1172 sizeof ("-foffload-objects=") - 1) == 0)
1174 have_offload = true;
1175 offload_objects_file_name
1176 = argv[i] + sizeof ("-foffload-objects=") - 1;
1177 continue;
1180 if ((p = strrchr (argv[i], '@'))
1181 && p != argv[i]
1182 && sscanf (p, "@%li%n", &loffset, &consumed) >= 1
1183 && strlen (p) == (unsigned int) consumed)
1185 filename = XNEWVEC (char, p - argv[i] + 1);
1186 memcpy (filename, argv[i], p - argv[i]);
1187 filename[p - argv[i]] = '\0';
1188 file_offset = (off_t) loffset;
1190 fd = open (filename, O_RDONLY | O_BINARY);
1191 /* Linker plugin passes -fresolution and -flinker-output options.
1192 -flinker-output is passed only when user did not specify one and thus
1193 we do not need to worry about duplicities with the option handling
1194 below. */
1195 if (fd == -1)
1197 lto_argv[lto_argc++] = argv[i];
1198 if (strcmp (argv[i], "-flinker-output=rel") == 0)
1199 linker_output_rel = true;
1200 continue;
1203 if (find_and_merge_options (fd, file_offset, LTO_SECTION_NAME_PREFIX,
1204 &fdecoded_options, &fdecoded_options_count,
1205 collect_gcc))
1207 have_lto = true;
1208 ltoobj_argv[ltoobj_argc++] = argv[i];
1210 close (fd);
1213 /* Initalize the common arguments for the driver. */
1214 obstack_init (&argv_obstack);
1215 obstack_ptr_grow (&argv_obstack, collect_gcc);
1216 obstack_ptr_grow (&argv_obstack, "-xlto");
1217 obstack_ptr_grow (&argv_obstack, "-c");
1219 append_compiler_options (&argv_obstack, fdecoded_options,
1220 fdecoded_options_count);
1221 append_linker_options (&argv_obstack, decoded_options, decoded_options_count);
1223 /* Scan linker driver arguments for things that are of relevance to us. */
1224 for (j = 1; j < decoded_options_count; ++j)
1226 struct cl_decoded_option *option = &decoded_options[j];
1227 switch (option->opt_index)
1229 case OPT_o:
1230 linker_output = option->arg;
1231 break;
1233 case OPT_save_temps:
1234 save_temps = 1;
1235 break;
1237 case OPT_v:
1238 verbose = 1;
1239 break;
1241 case OPT_flto_partition_:
1242 if (strcmp (option->arg, "none") == 0)
1243 no_partition = true;
1244 break;
1246 case OPT_flto_:
1247 if (strcmp (option->arg, "jobserver") == 0)
1249 jobserver = 1;
1250 parallel = 1;
1252 else
1254 parallel = atoi (option->arg);
1255 if (parallel <= 1)
1256 parallel = 0;
1258 /* Fallthru. */
1260 case OPT_flto:
1261 lto_mode = LTO_MODE_WHOPR;
1262 break;
1264 case OPT_flinker_output_:
1265 linker_output_rel = !strcmp (option->arg, "rel");
1266 break;
1269 default:
1270 break;
1274 /* Output lto-wrapper invocation command. */
1275 if (verbose)
1277 for (i = 0; i < argc; ++i)
1279 fputs (argv[i], stderr);
1280 fputc (' ', stderr);
1282 fputc ('\n', stderr);
1285 if (linker_output_rel)
1286 no_partition = true;
1288 if (no_partition)
1290 lto_mode = LTO_MODE_LTO;
1291 jobserver = 0;
1292 parallel = 0;
1295 if (linker_output)
1297 char *output_dir, *base, *name;
1298 bool bit_bucket = strcmp (linker_output, HOST_BIT_BUCKET) == 0;
1300 output_dir = xstrdup (linker_output);
1301 base = output_dir;
1302 for (name = base; *name; name++)
1303 if (IS_DIR_SEPARATOR (*name))
1304 base = name + 1;
1305 *base = '\0';
1307 linker_output = &linker_output[base - output_dir];
1308 if (*output_dir == '\0')
1310 static char current_dir[] = { '.', DIR_SEPARATOR, '\0' };
1311 output_dir = current_dir;
1313 if (!bit_bucket)
1315 obstack_ptr_grow (&argv_obstack, "-dumpdir");
1316 obstack_ptr_grow (&argv_obstack, output_dir);
1319 obstack_ptr_grow (&argv_obstack, "-dumpbase");
1322 /* Remember at which point we can scrub args to re-use the commons. */
1323 new_head_argc = obstack_object_size (&argv_obstack) / sizeof (void *);
1325 if (have_offload)
1327 unsigned i, num_offload_files;
1328 char **offload_argv;
1329 FILE *f;
1331 f = fopen (offload_objects_file_name, "r");
1332 if (f == NULL)
1333 fatal_error (input_location, "cannot open %s: %m",
1334 offload_objects_file_name);
1335 if (fscanf (f, "%u ", &num_offload_files) != 1)
1336 fatal_error (input_location, "cannot read %s: %m",
1337 offload_objects_file_name);
1338 offload_argv = XCNEWVEC (char *, num_offload_files);
1340 /* Read names of object files with offload. */
1341 for (i = 0; i < num_offload_files; i++)
1343 const unsigned piece = 32;
1344 char *buf, *filename = XNEWVEC (char, piece);
1345 size_t len;
1347 buf = filename;
1348 cont1:
1349 if (!fgets (buf, piece, f))
1350 break;
1351 len = strlen (filename);
1352 if (filename[len - 1] != '\n')
1354 filename = XRESIZEVEC (char, filename, len + piece);
1355 buf = filename + len;
1356 goto cont1;
1358 filename[len - 1] = '\0';
1359 offload_argv[i] = filename;
1361 fclose (f);
1362 if (offload_argv[num_offload_files - 1] == NULL)
1363 fatal_error (input_location, "invalid format of %s",
1364 offload_objects_file_name);
1365 maybe_unlink (offload_objects_file_name);
1366 offload_objects_file_name = NULL;
1368 /* Look at saved offload options in files. */
1369 for (i = 0; i < num_offload_files; i++)
1371 char *p;
1372 long loffset;
1373 int fd, consumed;
1374 off_t file_offset = 0;
1375 char *filename = offload_argv[i];
1377 if ((p = strrchr (offload_argv[i], '@'))
1378 && p != offload_argv[i]
1379 && sscanf (p, "@%li%n", &loffset, &consumed) >= 1
1380 && strlen (p) == (unsigned int) consumed)
1382 filename = XNEWVEC (char, p - offload_argv[i] + 1);
1383 memcpy (filename, offload_argv[i], p - offload_argv[i]);
1384 filename[p - offload_argv[i]] = '\0';
1385 file_offset = (off_t) loffset;
1387 fd = open (filename, O_RDONLY | O_BINARY);
1388 if (fd == -1)
1389 fatal_error (input_location, "cannot open %s: %m", filename);
1390 if (!find_and_merge_options (fd, file_offset,
1391 OFFLOAD_SECTION_NAME_PREFIX,
1392 &offload_fdecoded_options,
1393 &offload_fdecoded_options_count,
1394 collect_gcc))
1395 fatal_error (input_location, "cannot read %s: %m", filename);
1396 close (fd);
1397 if (filename != offload_argv[i])
1398 XDELETEVEC (filename);
1401 compile_images_for_offload_targets (num_offload_files, offload_argv,
1402 offload_fdecoded_options,
1403 offload_fdecoded_options_count,
1404 decoded_options,
1405 decoded_options_count);
1407 free_array_of_ptrs ((void **) offload_argv, num_offload_files);
1409 if (offload_names)
1411 find_crtoffloadtable ();
1412 for (i = 0; offload_names[i]; i++)
1413 printf ("%s\n", offload_names[i]);
1414 free_array_of_ptrs ((void **) offload_names, i);
1418 /* If object files contain offload sections, but do not contain LTO sections,
1419 then there is no need to perform a link-time recompilation, i.e.
1420 lto-wrapper is used only for a compilation of offload images. */
1421 if (have_offload && !have_lto)
1422 goto finish;
1424 if (lto_mode == LTO_MODE_LTO)
1426 if (linker_output)
1428 obstack_ptr_grow (&argv_obstack, linker_output);
1429 flto_out = (char *) xmalloc (strlen (linker_output)
1430 + sizeof (".lto.o") + 1);
1431 strcpy (flto_out, linker_output);
1432 strcat (flto_out, ".lto.o");
1434 else
1435 flto_out = make_temp_file (".lto.o");
1436 obstack_ptr_grow (&argv_obstack, "-o");
1437 obstack_ptr_grow (&argv_obstack, flto_out);
1439 else
1441 const char *list_option = "-fltrans-output-list=";
1442 size_t list_option_len = strlen (list_option);
1443 char *tmp;
1445 if (linker_output)
1447 char *dumpbase = (char *) xmalloc (strlen (linker_output)
1448 + sizeof (".wpa") + 1);
1449 strcpy (dumpbase, linker_output);
1450 strcat (dumpbase, ".wpa");
1451 obstack_ptr_grow (&argv_obstack, dumpbase);
1454 if (linker_output && save_temps)
1456 ltrans_output_file = (char *) xmalloc (strlen (linker_output)
1457 + sizeof (".ltrans.out") + 1);
1458 strcpy (ltrans_output_file, linker_output);
1459 strcat (ltrans_output_file, ".ltrans.out");
1461 else
1463 char *prefix = NULL;
1464 if (linker_output)
1466 prefix = (char *) xmalloc (strlen (linker_output) + 2);
1467 strcpy (prefix, linker_output);
1468 strcat (prefix, ".");
1471 ltrans_output_file = make_temp_file_with_prefix (prefix,
1472 ".ltrans.out");
1473 free (prefix);
1475 list_option_full = (char *) xmalloc (sizeof (char) *
1476 (strlen (ltrans_output_file) + list_option_len + 1));
1477 tmp = list_option_full;
1479 obstack_ptr_grow (&argv_obstack, tmp);
1480 strcpy (tmp, list_option);
1481 tmp += list_option_len;
1482 strcpy (tmp, ltrans_output_file);
1484 if (jobserver)
1485 obstack_ptr_grow (&argv_obstack, xstrdup ("-fwpa=jobserver"));
1486 else if (parallel > 1)
1488 char buf[256];
1489 sprintf (buf, "-fwpa=%i", parallel);
1490 obstack_ptr_grow (&argv_obstack, xstrdup (buf));
1492 else
1493 obstack_ptr_grow (&argv_obstack, "-fwpa");
1496 /* Append input arguments. */
1497 for (i = 0; i < lto_argc; ++i)
1498 obstack_ptr_grow (&argv_obstack, lto_argv[i]);
1499 /* Append the input objects. */
1500 for (i = 0; i < ltoobj_argc; ++i)
1501 obstack_ptr_grow (&argv_obstack, ltoobj_argv[i]);
1502 obstack_ptr_grow (&argv_obstack, NULL);
1504 new_argv = XOBFINISH (&argv_obstack, const char **);
1505 argv_ptr = &new_argv[new_head_argc];
1506 fork_execute (new_argv[0], CONST_CAST (char **, new_argv), true);
1508 /* Copy the early generated debug info from the objects to temporary
1509 files and append those to the partial link commandline. */
1510 n_debugobj = 0;
1511 early_debug_object_names = NULL;
1512 if (! skip_debug)
1514 early_debug_object_names = XCNEWVEC (const char *, ltoobj_argc+ 1);
1515 num_deb_objs = ltoobj_argc;
1516 for (i = 0; i < ltoobj_argc; ++i)
1518 const char *tem;
1519 if ((tem = debug_objcopy (ltoobj_argv[i], !linker_output_rel)))
1521 early_debug_object_names[i] = tem;
1522 n_debugobj++;
1527 if (lto_mode == LTO_MODE_LTO)
1529 printf ("%s\n", flto_out);
1530 if (!skip_debug)
1532 for (i = 0; i < ltoobj_argc; ++i)
1533 if (early_debug_object_names[i] != NULL)
1534 printf ("%s\n", early_debug_object_names[i]);
1536 /* These now belong to collect2. */
1537 free (flto_out);
1538 flto_out = NULL;
1539 free (early_debug_object_names);
1540 early_debug_object_names = NULL;
1542 else
1544 FILE *stream = fopen (ltrans_output_file, "r");
1545 FILE *mstream = NULL;
1546 struct obstack env_obstack;
1547 int priority;
1549 if (!stream)
1550 fatal_error (input_location, "fopen: %s: %m", ltrans_output_file);
1552 /* Parse the list of LTRANS inputs from the WPA stage. */
1553 obstack_init (&env_obstack);
1554 nr = 0;
1555 for (;;)
1557 const unsigned piece = 32;
1558 char *output_name = NULL;
1559 char *buf, *input_name = (char *)xmalloc (piece);
1560 size_t len;
1562 buf = input_name;
1563 if (fscanf (stream, "%i\n", &priority) != 1)
1565 if (!feof (stream))
1566 fatal_error (input_location,
1567 "Corrupted ltrans output file %s",
1568 ltrans_output_file);
1569 break;
1571 cont:
1572 if (!fgets (buf, piece, stream))
1573 break;
1574 len = strlen (input_name);
1575 if (input_name[len - 1] != '\n')
1577 input_name = (char *)xrealloc (input_name, len + piece);
1578 buf = input_name + len;
1579 goto cont;
1581 input_name[len - 1] = '\0';
1583 if (input_name[0] == '*')
1584 output_name = &input_name[1];
1586 nr++;
1587 ltrans_priorities
1588 = (int *)xrealloc (ltrans_priorities, nr * sizeof (int) * 2);
1589 input_names = (char **)xrealloc (input_names, nr * sizeof (char *));
1590 output_names = (char **)xrealloc (output_names, nr * sizeof (char *));
1591 ltrans_priorities[(nr-1)*2] = priority;
1592 ltrans_priorities[(nr-1)*2+1] = nr-1;
1593 input_names[nr-1] = input_name;
1594 output_names[nr-1] = output_name;
1596 fclose (stream);
1597 maybe_unlink (ltrans_output_file);
1598 ltrans_output_file = NULL;
1600 if (parallel)
1602 makefile = make_temp_file (".mk");
1603 mstream = fopen (makefile, "w");
1604 qsort (ltrans_priorities, nr, sizeof (int) * 2, cmp_priority);
1607 /* Execute the LTRANS stage for each input file (or prepare a
1608 makefile to invoke this in parallel). */
1609 for (i = 0; i < nr; ++i)
1611 char *output_name;
1612 char *input_name = input_names[i];
1613 /* If it's a pass-through file do nothing. */
1614 if (output_names[i])
1615 continue;
1617 /* Replace the .o suffix with a .ltrans.o suffix and write
1618 the resulting name to the LTRANS output list. */
1619 obstack_grow (&env_obstack, input_name, strlen (input_name) - 2);
1620 obstack_grow (&env_obstack, ".ltrans.o", sizeof (".ltrans.o"));
1621 output_name = XOBFINISH (&env_obstack, char *);
1623 /* Adjust the dumpbase if the linker output file was seen. */
1624 if (linker_output)
1626 char *dumpbase
1627 = (char *) xmalloc (strlen (linker_output)
1628 + sizeof (DUMPBASE_SUFFIX) + 1);
1629 snprintf (dumpbase,
1630 strlen (linker_output) + sizeof (DUMPBASE_SUFFIX),
1631 "%s.ltrans%u", linker_output, i);
1632 argv_ptr[0] = dumpbase;
1635 argv_ptr[1] = "-fltrans";
1636 argv_ptr[2] = "-o";
1637 argv_ptr[3] = output_name;
1638 argv_ptr[4] = input_name;
1639 argv_ptr[5] = NULL;
1640 if (parallel)
1642 fprintf (mstream, "%s:\n\t@%s ", output_name, new_argv[0]);
1643 for (j = 1; new_argv[j] != NULL; ++j)
1644 fprintf (mstream, " '%s'", new_argv[j]);
1645 fprintf (mstream, "\n");
1646 /* If we are not preserving the ltrans input files then
1647 truncate them as soon as we have processed it. This
1648 reduces temporary disk-space usage. */
1649 if (! save_temps)
1650 fprintf (mstream, "\t@-touch -r %s %s.tem > /dev/null 2>&1 "
1651 "&& mv %s.tem %s\n",
1652 input_name, input_name, input_name, input_name);
1654 else
1656 fork_execute (new_argv[0], CONST_CAST (char **, new_argv),
1657 true);
1658 maybe_unlink (input_name);
1661 output_names[i] = output_name;
1663 if (parallel)
1665 struct pex_obj *pex;
1666 char jobs[32];
1668 fprintf (mstream, "all:");
1669 for (i = 0; i < nr; ++i)
1671 int j = ltrans_priorities[i*2 + 1];
1672 fprintf (mstream, " \\\n\t%s", output_names[j]);
1674 fprintf (mstream, "\n");
1675 fclose (mstream);
1676 if (!jobserver)
1678 /* Avoid passing --jobserver-fd= and similar flags
1679 unless jobserver mode is explicitly enabled. */
1680 putenv (xstrdup ("MAKEFLAGS="));
1681 putenv (xstrdup ("MFLAGS="));
1683 new_argv[0] = getenv ("MAKE");
1684 if (!new_argv[0])
1685 new_argv[0] = "make";
1686 new_argv[1] = "-f";
1687 new_argv[2] = makefile;
1688 i = 3;
1689 if (!jobserver)
1691 snprintf (jobs, 31, "-j%d", parallel);
1692 new_argv[i++] = jobs;
1694 new_argv[i++] = "all";
1695 new_argv[i++] = NULL;
1696 pex = collect_execute (new_argv[0], CONST_CAST (char **, new_argv),
1697 NULL, NULL, PEX_SEARCH, false);
1698 do_wait (new_argv[0], pex);
1699 maybe_unlink (makefile);
1700 makefile = NULL;
1701 for (i = 0; i < nr; ++i)
1702 maybe_unlink (input_names[i]);
1704 for (i = 0; i < nr; ++i)
1706 fputs (output_names[i], stdout);
1707 putc ('\n', stdout);
1708 free (input_names[i]);
1710 if (!skip_debug)
1712 for (i = 0; i < ltoobj_argc; ++i)
1713 if (early_debug_object_names[i] != NULL)
1714 printf ("%s\n", early_debug_object_names[i]);
1716 nr = 0;
1717 free (ltrans_priorities);
1718 free (output_names);
1719 output_names = NULL;
1720 free (early_debug_object_names);
1721 early_debug_object_names = NULL;
1722 free (input_names);
1723 free (list_option_full);
1724 obstack_free (&env_obstack, NULL);
1727 finish:
1728 XDELETE (lto_argv);
1729 obstack_free (&argv_obstack, NULL);
1733 /* Entry point. */
1736 main (int argc, char *argv[])
1738 const char *p;
1740 init_opts_obstack ();
1742 p = argv[0] + strlen (argv[0]);
1743 while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
1744 --p;
1745 progname = p;
1747 xmalloc_set_program_name (progname);
1749 gcc_init_libintl ();
1751 diagnostic_initialize (global_dc, 0);
1753 if (atexit (lto_wrapper_cleanup) != 0)
1754 fatal_error (input_location, "atexit failed");
1756 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
1757 signal (SIGINT, fatal_signal);
1758 #ifdef SIGHUP
1759 if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
1760 signal (SIGHUP, fatal_signal);
1761 #endif
1762 if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
1763 signal (SIGTERM, fatal_signal);
1764 #ifdef SIGPIPE
1765 if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
1766 signal (SIGPIPE, fatal_signal);
1767 #endif
1768 #ifdef SIGCHLD
1769 /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
1770 receive the signal. A different setting is inheritable */
1771 signal (SIGCHLD, SIG_DFL);
1772 #endif
1774 /* We may be called with all the arguments stored in some file and
1775 passed with @file. Expand them into argv before processing. */
1776 expandargv (&argc, &argv);
1778 run_gcc (argc, argv);
1780 return 0;