C++: -Wwrite-strings: use location of string constant
[official-gcc.git] / gcc / lto-wrapper.c
blob9cfdfae24e1f9f1d98610100013efc12e981d707
1 /* Wrapper to call lto. Used by collect2 and the linker plugin.
2 Copyright (C) 2009-2018 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 char *debug_obj;
76 const char tool_name[] = "lto-wrapper";
78 /* Delete tempfiles. Called from utils_cleanup. */
80 void
81 tool_cleanup (bool)
83 unsigned int i;
85 if (ltrans_output_file)
86 maybe_unlink (ltrans_output_file);
87 if (flto_out)
88 maybe_unlink (flto_out);
89 if (offload_objects_file_name)
90 maybe_unlink (offload_objects_file_name);
91 if (makefile)
92 maybe_unlink (makefile);
93 if (debug_obj)
94 maybe_unlink (debug_obj);
95 for (i = 0; i < nr; ++i)
97 maybe_unlink (input_names[i]);
98 if (output_names[i])
99 maybe_unlink (output_names[i]);
103 static void
104 lto_wrapper_cleanup (void)
106 utils_cleanup (false);
109 /* Unlink a temporary LTRANS file unless requested otherwise. */
111 void
112 maybe_unlink (const char *file)
114 if (!save_temps)
116 if (unlink_if_ordinary (file)
117 && errno != ENOENT)
118 fatal_error (input_location, "deleting LTRANS file %s: %m", file);
120 else if (verbose)
121 fprintf (stderr, "[Leaving LTRANS %s]\n", file);
124 /* Template of LTRANS dumpbase suffix. */
125 #define DUMPBASE_SUFFIX ".ltrans18446744073709551615"
127 /* Create decoded options from the COLLECT_GCC and COLLECT_GCC_OPTIONS
128 environment according to LANG_MASK. */
130 static void
131 get_options_from_collect_gcc_options (const char *collect_gcc,
132 const char *collect_gcc_options,
133 unsigned int lang_mask,
134 struct cl_decoded_option **decoded_options,
135 unsigned int *decoded_options_count)
137 struct obstack argv_obstack;
138 char *argv_storage;
139 const char **argv;
140 int j, k, argc;
142 argv_storage = xstrdup (collect_gcc_options);
143 obstack_init (&argv_obstack);
144 obstack_ptr_grow (&argv_obstack, collect_gcc);
146 for (j = 0, k = 0; argv_storage[j] != '\0'; ++j)
148 if (argv_storage[j] == '\'')
150 obstack_ptr_grow (&argv_obstack, &argv_storage[k]);
151 ++j;
154 if (argv_storage[j] == '\0')
155 fatal_error (input_location, "malformed COLLECT_GCC_OPTIONS");
156 else if (strncmp (&argv_storage[j], "'\\''", 4) == 0)
158 argv_storage[k++] = '\'';
159 j += 4;
161 else if (argv_storage[j] == '\'')
162 break;
163 else
164 argv_storage[k++] = argv_storage[j++];
166 while (1);
167 argv_storage[k++] = '\0';
171 obstack_ptr_grow (&argv_obstack, NULL);
172 argc = obstack_object_size (&argv_obstack) / sizeof (void *) - 1;
173 argv = XOBFINISH (&argv_obstack, const char **);
175 decode_cmdline_options_to_array (argc, (const char **)argv,
176 lang_mask,
177 decoded_options, decoded_options_count);
178 obstack_free (&argv_obstack, NULL);
181 /* Append OPTION to the options array DECODED_OPTIONS with size
182 DECODED_OPTIONS_COUNT. */
184 static void
185 append_option (struct cl_decoded_option **decoded_options,
186 unsigned int *decoded_options_count,
187 struct cl_decoded_option *option)
189 ++*decoded_options_count;
190 *decoded_options
191 = (struct cl_decoded_option *)
192 xrealloc (*decoded_options,
193 (*decoded_options_count
194 * sizeof (struct cl_decoded_option)));
195 memcpy (&(*decoded_options)[*decoded_options_count - 1], option,
196 sizeof (struct cl_decoded_option));
199 /* Remove option number INDEX from DECODED_OPTIONS, update
200 DECODED_OPTIONS_COUNT. */
202 static void
203 remove_option (struct cl_decoded_option **decoded_options,
204 int index, unsigned int *decoded_options_count)
206 --*decoded_options_count;
207 memmove (&(*decoded_options)[index + 1],
208 &(*decoded_options)[index],
209 sizeof (struct cl_decoded_option)
210 * (*decoded_options_count - index));
213 /* Try to merge and complain about options FDECODED_OPTIONS when applied
214 ontop of DECODED_OPTIONS. */
216 static void
217 merge_and_complain (struct cl_decoded_option **decoded_options,
218 unsigned int *decoded_options_count,
219 struct cl_decoded_option *fdecoded_options,
220 unsigned int fdecoded_options_count)
222 unsigned int i, j;
223 struct cl_decoded_option *pic_option = NULL;
224 struct cl_decoded_option *pie_option = NULL;
226 /* ??? Merge options from files. Most cases can be
227 handled by either unioning or intersecting
228 (for example -fwrapv is a case for unioning,
229 -ffast-math is for intersection). Most complaints
230 about real conflicts between different options can
231 be deferred to the compiler proper. Options that
232 we can neither safely handle by intersection nor
233 unioning would need to be complained about here.
234 Ideally we'd have a flag in the opt files that
235 tells whether to union or intersect or reject.
236 In absence of that it's unclear what a good default is.
237 It's also difficult to get positional handling correct. */
239 /* The following does what the old LTO option code did,
240 union all target and a selected set of common options. */
241 for (i = 0; i < fdecoded_options_count; ++i)
243 struct cl_decoded_option *foption = &fdecoded_options[i];
244 switch (foption->opt_index)
246 case OPT_SPECIAL_unknown:
247 case OPT_SPECIAL_ignore:
248 case OPT_SPECIAL_deprecated:
249 case OPT_SPECIAL_program_name:
250 case OPT_SPECIAL_input_file:
251 break;
253 default:
254 if (!(cl_options[foption->opt_index].flags & CL_TARGET))
255 break;
257 /* Fallthru. */
258 case OPT_fdiagnostics_show_caret:
259 case OPT_fdiagnostics_show_labels:
260 case OPT_fdiagnostics_show_line_numbers:
261 case OPT_fdiagnostics_show_option:
262 case OPT_fdiagnostics_show_location_:
263 case OPT_fshow_column:
264 case OPT_fcommon:
265 case OPT_fgnu_tm:
266 /* Do what the old LTO code did - collect exactly one option
267 setting per OPT code, we pick the first we encounter.
268 ??? This doesn't make too much sense, but when it doesn't
269 then we should complain. */
270 for (j = 0; j < *decoded_options_count; ++j)
271 if ((*decoded_options)[j].opt_index == foption->opt_index)
272 break;
273 if (j == *decoded_options_count)
274 append_option (decoded_options, decoded_options_count, foption);
275 break;
277 /* Figure out what PIC/PIE level wins and merge the results. */
278 case OPT_fPIC:
279 case OPT_fpic:
280 pic_option = foption;
281 break;
282 case OPT_fPIE:
283 case OPT_fpie:
284 pie_option = foption;
285 break;
287 case OPT_fopenmp:
288 case OPT_fopenacc:
289 /* For selected options we can merge conservatively. */
290 for (j = 0; j < *decoded_options_count; ++j)
291 if ((*decoded_options)[j].opt_index == foption->opt_index)
292 break;
293 if (j == *decoded_options_count)
294 append_option (decoded_options, decoded_options_count, foption);
295 /* -fopenmp > -fno-openmp,
296 -fopenacc > -fno-openacc */
297 else if (foption->value > (*decoded_options)[j].value)
298 (*decoded_options)[j] = *foption;
299 break;
301 case OPT_fopenacc_dim_:
302 /* Append or check identical. */
303 for (j = 0; j < *decoded_options_count; ++j)
304 if ((*decoded_options)[j].opt_index == foption->opt_index)
305 break;
306 if (j == *decoded_options_count)
307 append_option (decoded_options, decoded_options_count, foption);
308 else if (strcmp ((*decoded_options)[j].arg, foption->arg))
309 fatal_error (input_location,
310 "Option %s with different values",
311 foption->orig_option_with_args_text);
312 break;
314 case OPT_O:
315 case OPT_Ofast:
316 case OPT_Og:
317 case OPT_Os:
318 for (j = 0; j < *decoded_options_count; ++j)
319 if ((*decoded_options)[j].opt_index == OPT_O
320 || (*decoded_options)[j].opt_index == OPT_Ofast
321 || (*decoded_options)[j].opt_index == OPT_Og
322 || (*decoded_options)[j].opt_index == OPT_Os)
323 break;
324 if (j == *decoded_options_count)
325 append_option (decoded_options, decoded_options_count, foption);
326 else if ((*decoded_options)[j].opt_index == foption->opt_index
327 && foption->opt_index != OPT_O)
328 /* Exact same options get merged. */
330 else
332 /* For mismatched option kinds preserve the optimization
333 level only, thus merge it as -On. This also handles
334 merging of same optimization level -On. */
335 int level = 0;
336 switch (foption->opt_index)
338 case OPT_O:
339 if (foption->arg[0] == '\0')
340 level = MAX (level, 1);
341 else
342 level = MAX (level, atoi (foption->arg));
343 break;
344 case OPT_Ofast:
345 level = MAX (level, 3);
346 break;
347 case OPT_Og:
348 level = MAX (level, 1);
349 break;
350 case OPT_Os:
351 level = MAX (level, 2);
352 break;
353 default:
354 gcc_unreachable ();
356 switch ((*decoded_options)[j].opt_index)
358 case OPT_O:
359 if ((*decoded_options)[j].arg[0] == '\0')
360 level = MAX (level, 1);
361 else
362 level = MAX (level, atoi ((*decoded_options)[j].arg));
363 break;
364 case OPT_Ofast:
365 level = MAX (level, 3);
366 break;
367 case OPT_Og:
368 level = MAX (level, 1);
369 break;
370 case OPT_Os:
371 level = MAX (level, 2);
372 break;
373 default:
374 gcc_unreachable ();
376 (*decoded_options)[j].opt_index = OPT_O;
377 char *tem;
378 tem = xasprintf ("-O%d", level);
379 (*decoded_options)[j].arg = &tem[2];
380 (*decoded_options)[j].canonical_option[0] = tem;
381 (*decoded_options)[j].value = 1;
383 break;
386 case OPT_foffload_abi_:
387 for (j = 0; j < *decoded_options_count; ++j)
388 if ((*decoded_options)[j].opt_index == foption->opt_index)
389 break;
390 if (j == *decoded_options_count)
391 append_option (decoded_options, decoded_options_count, foption);
392 else if (foption->value != (*decoded_options)[j].value)
393 fatal_error (input_location,
394 "Option %s not used consistently in all LTO input"
395 " files", foption->orig_option_with_args_text);
396 break;
399 case OPT_foffload_:
400 append_option (decoded_options, decoded_options_count, foption);
401 break;
405 /* Merge PIC options:
406 -fPIC + -fpic = -fpic
407 -fPIC + -fno-pic = -fno-pic
408 -fpic/-fPIC + nothin = nothing.
409 It is a common mistake to mix few -fPIC compiled objects into otherwise
410 non-PIC code. We do not want to build everything with PIC then.
412 It would be good to warn on mismatches, but it is bit hard to do as
413 we do not know what nothing translates to. */
415 for (unsigned int j = 0; j < *decoded_options_count;)
416 if ((*decoded_options)[j].opt_index == OPT_fPIC
417 || (*decoded_options)[j].opt_index == OPT_fpic)
419 if (!pic_option
420 || (pic_option->value > 0) != ((*decoded_options)[j].value > 0))
421 remove_option (decoded_options, j, decoded_options_count);
422 else if (pic_option->opt_index == OPT_fPIC
423 && (*decoded_options)[j].opt_index == OPT_fpic)
425 (*decoded_options)[j] = *pic_option;
426 j++;
428 else
429 j++;
431 else if ((*decoded_options)[j].opt_index == OPT_fPIE
432 || (*decoded_options)[j].opt_index == OPT_fpie)
434 if (!pie_option
435 || pie_option->value != (*decoded_options)[j].value)
436 remove_option (decoded_options, j, decoded_options_count);
437 else if (pie_option->opt_index == OPT_fPIE
438 && (*decoded_options)[j].opt_index == OPT_fpie)
440 (*decoded_options)[j] = *pie_option;
441 j++;
443 else
444 j++;
446 else
447 j++;
450 /* Auxiliary function that frees elements of PTR and PTR itself.
451 N is number of elements to be freed. If PTR is NULL, nothing is freed.
452 If an element is NULL, subsequent elements are not freed. */
454 static void **
455 free_array_of_ptrs (void **ptr, unsigned n)
457 if (!ptr)
458 return NULL;
459 for (unsigned i = 0; i < n; i++)
461 if (!ptr[i])
462 break;
463 free (ptr[i]);
465 free (ptr);
466 return NULL;
469 /* Parse STR, saving found tokens into PVALUES and return their number.
470 Tokens are assumed to be delimited by ':'. If APPEND is non-null,
471 append it to every token we find. */
473 static unsigned
474 parse_env_var (const char *str, char ***pvalues, const char *append)
476 const char *curval, *nextval;
477 char **values;
478 unsigned num = 1, i;
480 curval = strchr (str, ':');
481 while (curval)
483 num++;
484 curval = strchr (curval + 1, ':');
487 values = (char**) xmalloc (num * sizeof (char*));
488 curval = str;
489 nextval = strchr (curval, ':');
490 if (nextval == NULL)
491 nextval = strchr (curval, '\0');
493 int append_len = append ? strlen (append) : 0;
494 for (i = 0; i < num; i++)
496 int l = nextval - curval;
497 values[i] = (char*) xmalloc (l + 1 + append_len);
498 memcpy (values[i], curval, l);
499 values[i][l] = 0;
500 if (append)
501 strcat (values[i], append);
502 curval = nextval + 1;
503 nextval = strchr (curval, ':');
504 if (nextval == NULL)
505 nextval = strchr (curval, '\0');
507 *pvalues = values;
508 return num;
511 /* Append options OPTS from lto or offload_lto sections to ARGV_OBSTACK. */
513 static void
514 append_compiler_options (obstack *argv_obstack, struct cl_decoded_option *opts,
515 unsigned int count)
517 /* Append compiler driver arguments as far as they were merged. */
518 for (unsigned int j = 1; j < count; ++j)
520 struct cl_decoded_option *option = &opts[j];
522 /* File options have been properly filtered by lto-opts.c. */
523 switch (option->opt_index)
525 /* Drop arguments that we want to take from the link line. */
526 case OPT_flto_:
527 case OPT_flto:
528 case OPT_flto_partition_:
529 continue;
531 default:
532 break;
535 /* For now do what the original LTO option code was doing - pass
536 on any CL_TARGET flag and a few selected others. */
537 switch (option->opt_index)
539 case OPT_fdiagnostics_show_caret:
540 case OPT_fdiagnostics_show_labels:
541 case OPT_fdiagnostics_show_line_numbers:
542 case OPT_fdiagnostics_show_option:
543 case OPT_fdiagnostics_show_location_:
544 case OPT_fshow_column:
545 case OPT_fPIC:
546 case OPT_fpic:
547 case OPT_fPIE:
548 case OPT_fpie:
549 case OPT_fcommon:
550 case OPT_fgnu_tm:
551 case OPT_fopenmp:
552 case OPT_fopenacc:
553 case OPT_fopenacc_dim_:
554 case OPT_foffload_abi_:
555 case OPT_O:
556 case OPT_Ofast:
557 case OPT_Og:
558 case OPT_Os:
559 break;
561 default:
562 if (!(cl_options[option->opt_index].flags & CL_TARGET))
563 continue;
566 /* Pass the option on. */
567 for (unsigned int i = 0; i < option->canonical_option_num_elements; ++i)
568 obstack_ptr_grow (argv_obstack, option->canonical_option[i]);
572 /* Append diag options in OPTS with length COUNT to ARGV_OBSTACK. */
574 static void
575 append_diag_options (obstack *argv_obstack, struct cl_decoded_option *opts,
576 unsigned int count)
578 /* Append compiler driver arguments as far as they were merged. */
579 for (unsigned int j = 1; j < count; ++j)
581 struct cl_decoded_option *option = &opts[j];
583 switch (option->opt_index)
585 case OPT_fdiagnostics_color_:
586 case OPT_fdiagnostics_show_caret:
587 case OPT_fdiagnostics_show_labels:
588 case OPT_fdiagnostics_show_line_numbers:
589 case OPT_fdiagnostics_show_option:
590 case OPT_fdiagnostics_show_location_:
591 case OPT_fshow_column:
592 break;
593 default:
594 continue;
597 /* Pass the option on. */
598 for (unsigned int i = 0; i < option->canonical_option_num_elements; ++i)
599 obstack_ptr_grow (argv_obstack, option->canonical_option[i]);
604 /* Append linker options OPTS to ARGV_OBSTACK. */
606 static void
607 append_linker_options (obstack *argv_obstack, struct cl_decoded_option *opts,
608 unsigned int count)
610 /* Append linker driver arguments. Compiler options from the linker
611 driver arguments will override / merge with those from the compiler. */
612 for (unsigned int j = 1; j < count; ++j)
614 struct cl_decoded_option *option = &opts[j];
616 /* Do not pass on frontend specific flags not suitable for lto. */
617 if (!(cl_options[option->opt_index].flags
618 & (CL_COMMON|CL_TARGET|CL_DRIVER|CL_LTO)))
619 continue;
621 switch (option->opt_index)
623 case OPT_o:
624 case OPT_flto_:
625 case OPT_flto:
626 /* We've handled these LTO options, do not pass them on. */
627 continue;
629 case OPT_fopenmp:
630 case OPT_fopenacc:
631 /* Ignore -fno-XXX form of these options, as otherwise
632 corresponding builtins will not be enabled. */
633 if (option->value == 0)
634 continue;
635 break;
637 default:
638 break;
641 /* Pass the option on. */
642 for (unsigned int i = 0; i < option->canonical_option_num_elements; ++i)
643 obstack_ptr_grow (argv_obstack, option->canonical_option[i]);
647 /* Extract options for TARGET offload compiler from OPTIONS and append
648 them to ARGV_OBSTACK. */
650 static void
651 append_offload_options (obstack *argv_obstack, const char *target,
652 struct cl_decoded_option *options,
653 unsigned int options_count)
655 for (unsigned i = 0; i < options_count; i++)
657 const char *cur, *next, *opts;
658 char **argv;
659 unsigned argc;
660 struct cl_decoded_option *option = &options[i];
662 if (option->opt_index != OPT_foffload_)
663 continue;
665 /* If option argument starts with '-' then no target is specified. That
666 means offload options are specified for all targets, so we need to
667 append them. */
668 if (option->arg[0] == '-')
669 opts = option->arg;
670 else
672 opts = strchr (option->arg, '=');
673 /* If there are offload targets specified, but no actual options,
674 there is nothing to do here. */
675 if (!opts)
676 continue;
678 cur = option->arg;
680 while (cur < opts)
682 next = strchr (cur, ',');
683 if (next == NULL)
684 next = opts;
685 next = (next > opts) ? opts : next;
687 /* Are we looking for this offload target? */
688 if (strlen (target) == (size_t) (next - cur)
689 && strncmp (target, cur, next - cur) == 0)
690 break;
692 /* Skip the comma or equal sign. */
693 cur = next + 1;
696 if (cur >= opts)
697 continue;
699 opts++;
702 argv = buildargv (opts);
703 for (argc = 0; argv[argc]; argc++)
704 obstack_ptr_grow (argv_obstack, argv[argc]);
708 /* Check whether NAME can be accessed in MODE. This is like access,
709 except that it never considers directories to be executable. */
711 static int
712 access_check (const char *name, int mode)
714 if (mode == X_OK)
716 struct stat st;
718 if (stat (name, &st) < 0
719 || S_ISDIR (st.st_mode))
720 return -1;
723 return access (name, mode);
726 /* Prepare a target image for offload TARGET, using mkoffload tool from
727 COMPILER_PATH. Return the name of the resultant object file. */
729 static char *
730 compile_offload_image (const char *target, const char *compiler_path,
731 unsigned in_argc, char *in_argv[],
732 struct cl_decoded_option *compiler_opts,
733 unsigned int compiler_opt_count,
734 struct cl_decoded_option *linker_opts,
735 unsigned int linker_opt_count)
737 char *filename = NULL;
738 char **argv;
739 char *suffix
740 = XALLOCAVEC (char, sizeof ("/accel//mkoffload") + strlen (target));
741 strcpy (suffix, "/accel/");
742 strcat (suffix, target);
743 strcat (suffix, "/mkoffload");
745 char **paths = NULL;
746 unsigned n_paths = parse_env_var (compiler_path, &paths, suffix);
748 const char *compiler = NULL;
749 for (unsigned i = 0; i < n_paths; i++)
750 if (access_check (paths[i], X_OK) == 0)
752 compiler = paths[i];
753 break;
756 if (!compiler)
757 fatal_error (input_location,
758 "could not find %s in %s (consider using '-B')\n", suffix + 1,
759 compiler_path);
761 /* Generate temporary output file name. */
762 filename = make_temp_file (".target.o");
764 struct obstack argv_obstack;
765 obstack_init (&argv_obstack);
766 obstack_ptr_grow (&argv_obstack, compiler);
767 if (save_temps)
768 obstack_ptr_grow (&argv_obstack, "-save-temps");
769 if (verbose)
770 obstack_ptr_grow (&argv_obstack, "-v");
771 obstack_ptr_grow (&argv_obstack, "-o");
772 obstack_ptr_grow (&argv_obstack, filename);
774 /* Append names of input object files. */
775 for (unsigned i = 0; i < in_argc; i++)
776 obstack_ptr_grow (&argv_obstack, in_argv[i]);
778 /* Append options from offload_lto sections. */
779 append_compiler_options (&argv_obstack, compiler_opts,
780 compiler_opt_count);
781 append_diag_options (&argv_obstack, linker_opts, linker_opt_count);
783 /* Append options specified by -foffload last. In case of conflicting
784 options we expect offload compiler to choose the latest. */
785 append_offload_options (&argv_obstack, target, compiler_opts,
786 compiler_opt_count);
787 append_offload_options (&argv_obstack, target, linker_opts,
788 linker_opt_count);
790 obstack_ptr_grow (&argv_obstack, NULL);
791 argv = XOBFINISH (&argv_obstack, char **);
792 fork_execute (argv[0], argv, true);
793 obstack_free (&argv_obstack, NULL);
795 free_array_of_ptrs ((void **) paths, n_paths);
796 return filename;
800 /* The main routine dealing with offloading.
801 The routine builds a target image for each offload target. IN_ARGC and
802 IN_ARGV specify options and input object files. As all of them could contain
803 target sections, we pass them all to target compilers. */
805 static void
806 compile_images_for_offload_targets (unsigned in_argc, char *in_argv[],
807 struct cl_decoded_option *compiler_opts,
808 unsigned int compiler_opt_count,
809 struct cl_decoded_option *linker_opts,
810 unsigned int linker_opt_count)
812 char **names = NULL;
813 const char *target_names = getenv (OFFLOAD_TARGET_NAMES_ENV);
814 if (!target_names)
815 return;
816 unsigned num_targets = parse_env_var (target_names, &names, NULL);
818 int next_name_entry = 0;
819 const char *compiler_path = getenv ("COMPILER_PATH");
820 if (!compiler_path)
821 goto out;
823 /* Prepare an image for each target and save the name of the resultant object
824 file to the OFFLOAD_NAMES array. It is terminated by a NULL entry. */
825 offload_names = XCNEWVEC (char *, num_targets + 1);
826 for (unsigned i = 0; i < num_targets; i++)
828 /* HSA does not use LTO-like streaming and a different compiler, skip
829 it. */
830 if (strcmp (names[i], "hsa") == 0)
831 continue;
833 offload_names[next_name_entry]
834 = compile_offload_image (names[i], compiler_path, in_argc, in_argv,
835 compiler_opts, compiler_opt_count,
836 linker_opts, linker_opt_count);
837 if (!offload_names[next_name_entry])
838 fatal_error (input_location,
839 "problem with building target image for %s\n", names[i]);
840 next_name_entry++;
843 out:
844 free_array_of_ptrs ((void **) names, num_targets);
847 /* Copy a file from SRC to DEST. */
849 static void
850 copy_file (const char *dest, const char *src)
852 FILE *d = fopen (dest, "wb");
853 FILE *s = fopen (src, "rb");
854 char buffer[512];
855 while (!feof (s))
857 size_t len = fread (buffer, 1, 512, s);
858 if (ferror (s) != 0)
859 fatal_error (input_location, "reading input file");
860 if (len > 0)
862 fwrite (buffer, 1, len, d);
863 if (ferror (d) != 0)
864 fatal_error (input_location, "writing output file");
867 fclose (d);
868 fclose (s);
871 /* Find the crtoffloadtable.o file in LIBRARY_PATH, make copy and pass name of
872 the copy to the linker. */
874 static void
875 find_crtoffloadtable (void)
877 char **paths = NULL;
878 const char *library_path = getenv ("LIBRARY_PATH");
879 if (!library_path)
880 return;
881 unsigned n_paths = parse_env_var (library_path, &paths, "/crtoffloadtable.o");
883 unsigned i;
884 for (i = 0; i < n_paths; i++)
885 if (access_check (paths[i], R_OK) == 0)
887 /* The linker will delete the filename we give it, so make a copy. */
888 char *crtoffloadtable = make_temp_file (".crtoffloadtable.o");
889 copy_file (crtoffloadtable, paths[i]);
890 printf ("%s\n", crtoffloadtable);
891 XDELETEVEC (crtoffloadtable);
892 break;
894 if (i == n_paths)
895 fatal_error (input_location,
896 "installation error, can't find crtoffloadtable.o");
898 free_array_of_ptrs ((void **) paths, n_paths);
901 /* A subroutine of run_gcc. Examine the open file FD for lto sections with
902 name prefix PREFIX, at FILE_OFFSET, and store any options we find in OPTS
903 and OPT_COUNT. Return true if we found a matchingn section, false
904 otherwise. COLLECT_GCC holds the value of the environment variable with
905 the same name. */
907 static bool
908 find_and_merge_options (int fd, off_t file_offset, const char *prefix,
909 struct cl_decoded_option **opts,
910 unsigned int *opt_count, const char *collect_gcc)
912 off_t offset, length;
913 char *data;
914 char *fopts;
915 const char *errmsg;
916 int err;
917 struct cl_decoded_option *fdecoded_options = *opts;
918 unsigned int fdecoded_options_count = *opt_count;
920 simple_object_read *sobj;
921 sobj = simple_object_start_read (fd, file_offset, "__GNU_LTO",
922 &errmsg, &err);
923 if (!sobj)
924 return false;
926 char *secname = XALLOCAVEC (char, strlen (prefix) + sizeof (".opts"));
927 strcpy (secname, prefix);
928 strcat (secname, ".opts");
929 if (!simple_object_find_section (sobj, secname, &offset, &length,
930 &errmsg, &err))
932 simple_object_release_read (sobj);
933 return false;
936 lseek (fd, file_offset + offset, SEEK_SET);
937 data = (char *)xmalloc (length);
938 read (fd, data, length);
939 fopts = data;
942 struct cl_decoded_option *f2decoded_options;
943 unsigned int f2decoded_options_count;
944 get_options_from_collect_gcc_options (collect_gcc,
945 fopts, CL_LANG_ALL,
946 &f2decoded_options,
947 &f2decoded_options_count);
948 if (!fdecoded_options)
950 fdecoded_options = f2decoded_options;
951 fdecoded_options_count = f2decoded_options_count;
953 else
954 merge_and_complain (&fdecoded_options,
955 &fdecoded_options_count,
956 f2decoded_options, f2decoded_options_count);
958 fopts += strlen (fopts) + 1;
960 while (fopts - data < length);
962 free (data);
963 simple_object_release_read (sobj);
964 *opts = fdecoded_options;
965 *opt_count = fdecoded_options_count;
966 return true;
969 /* Copy early debug info sections from INFILE to a new file whose name
970 is returned. Return NULL on error. */
972 const char *
973 debug_objcopy (const char *infile, bool rename)
975 const char *outfile;
976 const char *errmsg;
977 int err;
979 const char *p;
980 off_t inoff = 0;
981 long loffset;
982 int consumed;
983 if ((p = strrchr (infile, '@'))
984 && p != infile
985 && sscanf (p, "@%li%n", &loffset, &consumed) >= 1
986 && strlen (p) == (unsigned int) consumed)
988 char *fname = xstrdup (infile);
989 fname[p - infile] = '\0';
990 infile = fname;
991 inoff = (off_t) loffset;
993 int infd = open (infile, O_RDONLY | O_BINARY);
994 if (infd == -1)
995 return NULL;
996 simple_object_read *inobj = simple_object_start_read (infd, inoff,
997 "__GNU_LTO",
998 &errmsg, &err);
999 if (!inobj)
1000 return NULL;
1002 off_t off, len;
1003 if (simple_object_find_section (inobj, ".gnu.debuglto_.debug_info",
1004 &off, &len, &errmsg, &err) != 1)
1006 if (errmsg)
1007 fatal_error (0, "%s: %s\n", errmsg, xstrerror (err));
1009 simple_object_release_read (inobj);
1010 close (infd);
1011 return NULL;
1014 outfile = make_temp_file ("debugobjtem");
1015 errmsg = simple_object_copy_lto_debug_sections (inobj, outfile, &err, rename);
1016 if (errmsg)
1018 unlink_if_ordinary (outfile);
1019 fatal_error (0, "%s: %s\n", errmsg, xstrerror (err));
1022 simple_object_release_read (inobj);
1023 close (infd);
1025 return outfile;
1028 /* Helper for qsort: compare priorities for parallel compilation. */
1031 cmp_priority (const void *a, const void *b)
1033 return *((const int *)b)-*((const int *)a);
1037 /* Execute gcc. ARGC is the number of arguments. ARGV contains the arguments. */
1039 static void
1040 run_gcc (unsigned argc, char *argv[])
1042 unsigned i, j;
1043 const char **new_argv;
1044 const char **argv_ptr;
1045 char *list_option_full = NULL;
1046 const char *linker_output = NULL;
1047 const char *collect_gcc, *collect_gcc_options;
1048 int parallel = 0;
1049 int jobserver = 0;
1050 bool no_partition = false;
1051 struct cl_decoded_option *fdecoded_options = NULL;
1052 struct cl_decoded_option *offload_fdecoded_options = NULL;
1053 unsigned int fdecoded_options_count = 0;
1054 unsigned int offload_fdecoded_options_count = 0;
1055 struct cl_decoded_option *decoded_options;
1056 unsigned int decoded_options_count;
1057 struct obstack argv_obstack;
1058 int new_head_argc;
1059 bool have_lto = false;
1060 bool have_offload = false;
1061 unsigned lto_argc = 0, ltoobj_argc = 0;
1062 char **lto_argv, **ltoobj_argv;
1063 bool linker_output_rel = false;
1064 bool skip_debug = false;
1065 unsigned n_debugobj;
1067 /* Get the driver and options. */
1068 collect_gcc = getenv ("COLLECT_GCC");
1069 if (!collect_gcc)
1070 fatal_error (input_location,
1071 "environment variable COLLECT_GCC must be set");
1072 collect_gcc_options = getenv ("COLLECT_GCC_OPTIONS");
1073 if (!collect_gcc_options)
1074 fatal_error (input_location,
1075 "environment variable COLLECT_GCC_OPTIONS must be set");
1076 get_options_from_collect_gcc_options (collect_gcc, collect_gcc_options,
1077 CL_LANG_ALL,
1078 &decoded_options,
1079 &decoded_options_count);
1081 /* Allocate array for input object files with LTO IL,
1082 and for possible preceding arguments. */
1083 lto_argv = XNEWVEC (char *, argc);
1084 ltoobj_argv = XNEWVEC (char *, argc);
1086 /* Look at saved options in the IL files. */
1087 for (i = 1; i < argc; ++i)
1089 char *p;
1090 int fd;
1091 off_t file_offset = 0;
1092 long loffset;
1093 int consumed;
1094 char *filename = argv[i];
1096 if (strncmp (argv[i], "-foffload-objects=",
1097 sizeof ("-foffload-objects=") - 1) == 0)
1099 have_offload = true;
1100 offload_objects_file_name
1101 = argv[i] + sizeof ("-foffload-objects=") - 1;
1102 continue;
1105 if ((p = strrchr (argv[i], '@'))
1106 && p != argv[i]
1107 && sscanf (p, "@%li%n", &loffset, &consumed) >= 1
1108 && strlen (p) == (unsigned int) consumed)
1110 filename = XNEWVEC (char, p - argv[i] + 1);
1111 memcpy (filename, argv[i], p - argv[i]);
1112 filename[p - argv[i]] = '\0';
1113 file_offset = (off_t) loffset;
1115 fd = open (filename, O_RDONLY | O_BINARY);
1116 /* Linker plugin passes -fresolution and -flinker-output options.
1117 -flinker-output is passed only when user did not specify one and thus
1118 we do not need to worry about duplicities with the option handling
1119 below. */
1120 if (fd == -1)
1122 lto_argv[lto_argc++] = argv[i];
1123 if (strcmp (argv[i], "-flinker-output=rel") == 0)
1124 linker_output_rel = true;
1125 continue;
1128 if (find_and_merge_options (fd, file_offset, LTO_SECTION_NAME_PREFIX,
1129 &fdecoded_options, &fdecoded_options_count,
1130 collect_gcc))
1132 have_lto = true;
1133 ltoobj_argv[ltoobj_argc++] = argv[i];
1135 close (fd);
1138 /* Initalize the common arguments for the driver. */
1139 obstack_init (&argv_obstack);
1140 obstack_ptr_grow (&argv_obstack, collect_gcc);
1141 obstack_ptr_grow (&argv_obstack, "-xlto");
1142 obstack_ptr_grow (&argv_obstack, "-c");
1144 append_compiler_options (&argv_obstack, fdecoded_options,
1145 fdecoded_options_count);
1146 append_linker_options (&argv_obstack, decoded_options, decoded_options_count);
1148 /* Scan linker driver arguments for things that are of relevance to us. */
1149 for (j = 1; j < decoded_options_count; ++j)
1151 struct cl_decoded_option *option = &decoded_options[j];
1152 switch (option->opt_index)
1154 case OPT_o:
1155 linker_output = option->arg;
1156 break;
1158 case OPT_save_temps:
1159 save_temps = 1;
1160 break;
1162 case OPT_v:
1163 verbose = 1;
1164 break;
1166 case OPT_flto_partition_:
1167 if (strcmp (option->arg, "none") == 0)
1168 no_partition = true;
1169 break;
1171 case OPT_flto_:
1172 if (strcmp (option->arg, "jobserver") == 0)
1174 jobserver = 1;
1175 parallel = 1;
1177 else
1179 parallel = atoi (option->arg);
1180 if (parallel <= 1)
1181 parallel = 0;
1183 /* Fallthru. */
1185 case OPT_flto:
1186 lto_mode = LTO_MODE_WHOPR;
1187 break;
1189 case OPT_flinker_output_:
1190 linker_output_rel = !strcmp (option->arg, "rel");
1191 break;
1194 default:
1195 break;
1199 /* Output lto-wrapper invocation command. */
1200 if (verbose)
1202 for (i = 0; i < argc; ++i)
1204 fputs (argv[i], stderr);
1205 fputc (' ', stderr);
1207 fputc ('\n', stderr);
1210 if (linker_output_rel)
1211 no_partition = true;
1213 if (no_partition)
1215 lto_mode = LTO_MODE_LTO;
1216 jobserver = 0;
1217 parallel = 0;
1220 if (linker_output)
1222 char *output_dir, *base, *name;
1223 bool bit_bucket = strcmp (linker_output, HOST_BIT_BUCKET) == 0;
1225 output_dir = xstrdup (linker_output);
1226 base = output_dir;
1227 for (name = base; *name; name++)
1228 if (IS_DIR_SEPARATOR (*name))
1229 base = name + 1;
1230 *base = '\0';
1232 linker_output = &linker_output[base - output_dir];
1233 if (*output_dir == '\0')
1235 static char current_dir[] = { '.', DIR_SEPARATOR, '\0' };
1236 output_dir = current_dir;
1238 if (!bit_bucket)
1240 obstack_ptr_grow (&argv_obstack, "-dumpdir");
1241 obstack_ptr_grow (&argv_obstack, output_dir);
1244 obstack_ptr_grow (&argv_obstack, "-dumpbase");
1247 /* Remember at which point we can scrub args to re-use the commons. */
1248 new_head_argc = obstack_object_size (&argv_obstack) / sizeof (void *);
1250 if (have_offload)
1252 unsigned i, num_offload_files;
1253 char **offload_argv;
1254 FILE *f;
1256 f = fopen (offload_objects_file_name, "r");
1257 if (f == NULL)
1258 fatal_error (input_location, "cannot open %s: %m",
1259 offload_objects_file_name);
1260 if (fscanf (f, "%u ", &num_offload_files) != 1)
1261 fatal_error (input_location, "cannot read %s: %m",
1262 offload_objects_file_name);
1263 offload_argv = XCNEWVEC (char *, num_offload_files);
1265 /* Read names of object files with offload. */
1266 for (i = 0; i < num_offload_files; i++)
1268 const unsigned piece = 32;
1269 char *buf, *filename = XNEWVEC (char, piece);
1270 size_t len;
1272 buf = filename;
1273 cont1:
1274 if (!fgets (buf, piece, f))
1275 break;
1276 len = strlen (filename);
1277 if (filename[len - 1] != '\n')
1279 filename = XRESIZEVEC (char, filename, len + piece);
1280 buf = filename + len;
1281 goto cont1;
1283 filename[len - 1] = '\0';
1284 offload_argv[i] = filename;
1286 fclose (f);
1287 if (offload_argv[num_offload_files - 1] == NULL)
1288 fatal_error (input_location, "invalid format of %s",
1289 offload_objects_file_name);
1290 maybe_unlink (offload_objects_file_name);
1291 offload_objects_file_name = NULL;
1293 /* Look at saved offload options in files. */
1294 for (i = 0; i < num_offload_files; i++)
1296 char *p;
1297 long loffset;
1298 int fd, consumed;
1299 off_t file_offset = 0;
1300 char *filename = offload_argv[i];
1302 if ((p = strrchr (offload_argv[i], '@'))
1303 && p != offload_argv[i]
1304 && sscanf (p, "@%li%n", &loffset, &consumed) >= 1
1305 && strlen (p) == (unsigned int) consumed)
1307 filename = XNEWVEC (char, p - offload_argv[i] + 1);
1308 memcpy (filename, offload_argv[i], p - offload_argv[i]);
1309 filename[p - offload_argv[i]] = '\0';
1310 file_offset = (off_t) loffset;
1312 fd = open (filename, O_RDONLY | O_BINARY);
1313 if (fd == -1)
1314 fatal_error (input_location, "cannot open %s: %m", filename);
1315 if (!find_and_merge_options (fd, file_offset,
1316 OFFLOAD_SECTION_NAME_PREFIX,
1317 &offload_fdecoded_options,
1318 &offload_fdecoded_options_count,
1319 collect_gcc))
1320 fatal_error (input_location, "cannot read %s: %m", filename);
1321 close (fd);
1322 if (filename != offload_argv[i])
1323 XDELETEVEC (filename);
1326 compile_images_for_offload_targets (num_offload_files, offload_argv,
1327 offload_fdecoded_options,
1328 offload_fdecoded_options_count,
1329 decoded_options,
1330 decoded_options_count);
1332 free_array_of_ptrs ((void **) offload_argv, num_offload_files);
1334 if (offload_names)
1336 find_crtoffloadtable ();
1337 for (i = 0; offload_names[i]; i++)
1338 printf ("%s\n", offload_names[i]);
1339 free_array_of_ptrs ((void **) offload_names, i);
1343 /* If object files contain offload sections, but do not contain LTO sections,
1344 then there is no need to perform a link-time recompilation, i.e.
1345 lto-wrapper is used only for a compilation of offload images. */
1346 if (have_offload && !have_lto)
1347 goto finish;
1349 if (lto_mode == LTO_MODE_LTO)
1351 flto_out = make_temp_file (".lto.o");
1352 if (linker_output)
1353 obstack_ptr_grow (&argv_obstack, linker_output);
1354 obstack_ptr_grow (&argv_obstack, "-o");
1355 obstack_ptr_grow (&argv_obstack, flto_out);
1357 else
1359 const char *list_option = "-fltrans-output-list=";
1360 size_t list_option_len = strlen (list_option);
1361 char *tmp;
1363 if (linker_output)
1365 char *dumpbase = (char *) xmalloc (strlen (linker_output)
1366 + sizeof (".wpa") + 1);
1367 strcpy (dumpbase, linker_output);
1368 strcat (dumpbase, ".wpa");
1369 obstack_ptr_grow (&argv_obstack, dumpbase);
1372 if (linker_output && save_temps)
1374 ltrans_output_file = (char *) xmalloc (strlen (linker_output)
1375 + sizeof (".ltrans.out") + 1);
1376 strcpy (ltrans_output_file, linker_output);
1377 strcat (ltrans_output_file, ".ltrans.out");
1379 else
1381 char *prefix = NULL;
1382 if (linker_output)
1384 prefix = (char *) xmalloc (strlen (linker_output) + 2);
1385 strcpy (prefix, linker_output);
1386 strcat (prefix, ".");
1389 ltrans_output_file = make_temp_file_with_prefix (prefix,
1390 ".ltrans.out");
1391 free (prefix);
1393 list_option_full = (char *) xmalloc (sizeof (char) *
1394 (strlen (ltrans_output_file) + list_option_len + 1));
1395 tmp = list_option_full;
1397 obstack_ptr_grow (&argv_obstack, tmp);
1398 strcpy (tmp, list_option);
1399 tmp += list_option_len;
1400 strcpy (tmp, ltrans_output_file);
1402 if (jobserver)
1403 obstack_ptr_grow (&argv_obstack, xstrdup ("-fwpa=jobserver"));
1404 else if (parallel > 1)
1406 char buf[256];
1407 sprintf (buf, "-fwpa=%i", parallel);
1408 obstack_ptr_grow (&argv_obstack, xstrdup (buf));
1410 else
1411 obstack_ptr_grow (&argv_obstack, "-fwpa");
1414 /* Append input arguments. */
1415 for (i = 0; i < lto_argc; ++i)
1416 obstack_ptr_grow (&argv_obstack, lto_argv[i]);
1417 /* Append the input objects. */
1418 for (i = 0; i < ltoobj_argc; ++i)
1419 obstack_ptr_grow (&argv_obstack, ltoobj_argv[i]);
1420 obstack_ptr_grow (&argv_obstack, NULL);
1422 new_argv = XOBFINISH (&argv_obstack, const char **);
1423 argv_ptr = &new_argv[new_head_argc];
1424 fork_execute (new_argv[0], CONST_CAST (char **, new_argv), true);
1426 /* Handle early generated debug information. At compile-time
1427 we output early DWARF debug info into .gnu.debuglto_ prefixed
1428 sections. LTRANS object DWARF debug info refers to that.
1429 So we need to transfer the .gnu.debuglto_ sections to the final
1430 link. Ideally the linker plugin interface would allow us to
1431 not claim those sections and instruct the linker to keep
1432 them, renaming them in the process. For now we extract and
1433 rename those sections via a simple-object interface to produce
1434 regular objects containing only the early debug info. We
1435 then partially link those to a single early debug info object
1436 and pass that as additional output back to the linker plugin. */
1438 /* Prepare the partial link to gather the compile-time generated
1439 debug-info into a single input for the final link. */
1440 debug_obj = make_temp_file ("debugobj");
1441 obstack_ptr_grow (&argv_obstack, collect_gcc);
1442 for (i = 1; i < decoded_options_count; ++i)
1444 /* Retain linker choice and -B. */
1445 if (decoded_options[i].opt_index == OPT_B
1446 || decoded_options[i].opt_index == OPT_fuse_ld_bfd
1447 || decoded_options[i].opt_index == OPT_fuse_ld_gold)
1448 append_linker_options (&argv_obstack, &decoded_options[i-1], 2);
1449 /* Retain all target options, this preserves -m32 for example. */
1450 if (cl_options[decoded_options[i].opt_index].flags & CL_TARGET)
1451 append_linker_options (&argv_obstack, &decoded_options[i-1], 2);
1452 /* Recognize -g0. */
1453 if (decoded_options[i].opt_index == OPT_g
1454 && strcmp (decoded_options[i].arg, "0") == 0)
1455 skip_debug = true;
1457 obstack_ptr_grow (&argv_obstack, "-r");
1458 obstack_ptr_grow (&argv_obstack, "-nostdlib");
1459 obstack_ptr_grow (&argv_obstack, "-o");
1460 obstack_ptr_grow (&argv_obstack, debug_obj);
1462 /* Copy the early generated debug info from the objects to temporary
1463 files and append those to the partial link commandline. */
1464 n_debugobj = 0;
1465 if (! skip_debug)
1466 for (i = 0; i < ltoobj_argc; ++i)
1468 const char *tem;
1469 if ((tem = debug_objcopy (ltoobj_argv[i], !linker_output_rel)))
1471 obstack_ptr_grow (&argv_obstack, tem);
1472 n_debugobj++;
1476 /* Link them all into a single object. Ideally this would reduce
1477 disk space usage mainly due to .debug_str merging but unfortunately
1478 GNU ld doesn't perform this with -r. */
1479 if (n_debugobj)
1481 obstack_ptr_grow (&argv_obstack, NULL);
1482 const char **debug_link_argv = XOBFINISH (&argv_obstack, const char **);
1483 fork_execute (debug_link_argv[0],
1484 CONST_CAST (char **, debug_link_argv), false);
1486 /* And dispose the temporaries. */
1487 for (i = 0; debug_link_argv[i]; ++i)
1489 for (--i; i > 0; --i)
1491 if (strcmp (debug_link_argv[i], debug_obj) == 0)
1492 break;
1493 maybe_unlink (debug_link_argv[i]);
1496 else
1498 unlink_if_ordinary (debug_obj);
1499 free (debug_obj);
1500 debug_obj = NULL;
1501 skip_debug = true;
1504 if (lto_mode == LTO_MODE_LTO)
1506 printf ("%s\n", flto_out);
1507 if (!skip_debug)
1509 printf ("%s\n", debug_obj);
1510 free (debug_obj);
1511 debug_obj = NULL;
1513 free (flto_out);
1514 flto_out = NULL;
1516 else
1518 FILE *stream = fopen (ltrans_output_file, "r");
1519 FILE *mstream = NULL;
1520 struct obstack env_obstack;
1521 int priority;
1523 if (!stream)
1524 fatal_error (input_location, "fopen: %s: %m", ltrans_output_file);
1526 /* Parse the list of LTRANS inputs from the WPA stage. */
1527 obstack_init (&env_obstack);
1528 nr = 0;
1529 for (;;)
1531 const unsigned piece = 32;
1532 char *output_name = NULL;
1533 char *buf, *input_name = (char *)xmalloc (piece);
1534 size_t len;
1536 buf = input_name;
1537 if (fscanf (stream, "%i\n", &priority) != 1)
1539 if (!feof (stream))
1540 fatal_error (input_location,
1541 "Corrupted ltrans output file %s",
1542 ltrans_output_file);
1543 break;
1545 cont:
1546 if (!fgets (buf, piece, stream))
1547 break;
1548 len = strlen (input_name);
1549 if (input_name[len - 1] != '\n')
1551 input_name = (char *)xrealloc (input_name, len + piece);
1552 buf = input_name + len;
1553 goto cont;
1555 input_name[len - 1] = '\0';
1557 if (input_name[0] == '*')
1558 output_name = &input_name[1];
1560 nr++;
1561 ltrans_priorities
1562 = (int *)xrealloc (ltrans_priorities, nr * sizeof (int) * 2);
1563 input_names = (char **)xrealloc (input_names, nr * sizeof (char *));
1564 output_names = (char **)xrealloc (output_names, nr * sizeof (char *));
1565 ltrans_priorities[(nr-1)*2] = priority;
1566 ltrans_priorities[(nr-1)*2+1] = nr-1;
1567 input_names[nr-1] = input_name;
1568 output_names[nr-1] = output_name;
1570 fclose (stream);
1571 maybe_unlink (ltrans_output_file);
1572 ltrans_output_file = NULL;
1574 if (parallel)
1576 makefile = make_temp_file (".mk");
1577 mstream = fopen (makefile, "w");
1578 qsort (ltrans_priorities, nr, sizeof (int) * 2, cmp_priority);
1581 /* Execute the LTRANS stage for each input file (or prepare a
1582 makefile to invoke this in parallel). */
1583 for (i = 0; i < nr; ++i)
1585 char *output_name;
1586 char *input_name = input_names[i];
1587 /* If it's a pass-through file do nothing. */
1588 if (output_names[i])
1589 continue;
1591 /* Replace the .o suffix with a .ltrans.o suffix and write
1592 the resulting name to the LTRANS output list. */
1593 obstack_grow (&env_obstack, input_name, strlen (input_name) - 2);
1594 obstack_grow (&env_obstack, ".ltrans.o", sizeof (".ltrans.o"));
1595 output_name = XOBFINISH (&env_obstack, char *);
1597 /* Adjust the dumpbase if the linker output file was seen. */
1598 if (linker_output)
1600 char *dumpbase
1601 = (char *) xmalloc (strlen (linker_output)
1602 + sizeof (DUMPBASE_SUFFIX) + 1);
1603 snprintf (dumpbase,
1604 strlen (linker_output) + sizeof (DUMPBASE_SUFFIX),
1605 "%s.ltrans%u", linker_output, i);
1606 argv_ptr[0] = dumpbase;
1609 argv_ptr[1] = "-fltrans";
1610 argv_ptr[2] = "-o";
1611 argv_ptr[3] = output_name;
1612 argv_ptr[4] = input_name;
1613 argv_ptr[5] = NULL;
1614 if (parallel)
1616 fprintf (mstream, "%s:\n\t@%s ", output_name, new_argv[0]);
1617 for (j = 1; new_argv[j] != NULL; ++j)
1618 fprintf (mstream, " '%s'", new_argv[j]);
1619 fprintf (mstream, "\n");
1620 /* If we are not preserving the ltrans input files then
1621 truncate them as soon as we have processed it. This
1622 reduces temporary disk-space usage. */
1623 if (! save_temps)
1624 fprintf (mstream, "\t@-touch -r %s %s.tem > /dev/null 2>&1 "
1625 "&& mv %s.tem %s\n",
1626 input_name, input_name, input_name, input_name);
1628 else
1630 fork_execute (new_argv[0], CONST_CAST (char **, new_argv),
1631 true);
1632 maybe_unlink (input_name);
1635 output_names[i] = output_name;
1637 if (parallel)
1639 struct pex_obj *pex;
1640 char jobs[32];
1642 fprintf (mstream, "all:");
1643 for (i = 0; i < nr; ++i)
1645 int j = ltrans_priorities[i*2 + 1];
1646 fprintf (mstream, " \\\n\t%s", output_names[j]);
1648 fprintf (mstream, "\n");
1649 fclose (mstream);
1650 if (!jobserver)
1652 /* Avoid passing --jobserver-fd= and similar flags
1653 unless jobserver mode is explicitly enabled. */
1654 putenv (xstrdup ("MAKEFLAGS="));
1655 putenv (xstrdup ("MFLAGS="));
1657 new_argv[0] = getenv ("MAKE");
1658 if (!new_argv[0])
1659 new_argv[0] = "make";
1660 new_argv[1] = "-f";
1661 new_argv[2] = makefile;
1662 i = 3;
1663 if (!jobserver)
1665 snprintf (jobs, 31, "-j%d", parallel);
1666 new_argv[i++] = jobs;
1668 new_argv[i++] = "all";
1669 new_argv[i++] = NULL;
1670 pex = collect_execute (new_argv[0], CONST_CAST (char **, new_argv),
1671 NULL, NULL, PEX_SEARCH, false);
1672 do_wait (new_argv[0], pex);
1673 maybe_unlink (makefile);
1674 makefile = NULL;
1675 for (i = 0; i < nr; ++i)
1676 maybe_unlink (input_names[i]);
1678 if (!skip_debug)
1680 printf ("%s\n", debug_obj);
1681 free (debug_obj);
1682 debug_obj = NULL;
1684 for (i = 0; i < nr; ++i)
1686 fputs (output_names[i], stdout);
1687 putc ('\n', stdout);
1688 free (input_names[i]);
1690 nr = 0;
1691 free (ltrans_priorities);
1692 free (output_names);
1693 free (input_names);
1694 free (list_option_full);
1695 obstack_free (&env_obstack, NULL);
1698 finish:
1699 XDELETE (lto_argv);
1700 obstack_free (&argv_obstack, NULL);
1704 /* Entry point. */
1707 main (int argc, char *argv[])
1709 const char *p;
1711 init_opts_obstack ();
1713 p = argv[0] + strlen (argv[0]);
1714 while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
1715 --p;
1716 progname = p;
1718 xmalloc_set_program_name (progname);
1720 gcc_init_libintl ();
1722 diagnostic_initialize (global_dc, 0);
1724 if (atexit (lto_wrapper_cleanup) != 0)
1725 fatal_error (input_location, "atexit failed");
1727 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
1728 signal (SIGINT, fatal_signal);
1729 #ifdef SIGHUP
1730 if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
1731 signal (SIGHUP, fatal_signal);
1732 #endif
1733 if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
1734 signal (SIGTERM, fatal_signal);
1735 #ifdef SIGPIPE
1736 if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
1737 signal (SIGPIPE, fatal_signal);
1738 #endif
1739 #ifdef SIGCHLD
1740 /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
1741 receive the signal. A different setting is inheritable */
1742 signal (SIGCHLD, SIG_DFL);
1743 #endif
1745 /* We may be called with all the arguments stored in some file and
1746 passed with @file. Expand them into argv before processing. */
1747 expandargv (&argc, &argv);
1749 run_gcc (argc, argv);
1751 return 0;