]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/lto-wrapper.c
2019-04-03 Richard Biener <rguenther@suse.de>
[thirdparty/gcc.git] / gcc / lto-wrapper.c
1 /* Wrapper to call lto. Used by collect2 and the linker plugin.
2 Copyright (C) 2009-2019 Free Software Foundation, Inc.
3
4 Factored out of collect2 by Rafael Espindola <espindola@google.com>
5
6 This file is part of GCC.
7
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.
12
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.
17
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/>. */
21
22
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.
27
28 Example:
29
30 $ lto-wrapper gcc/xgcc -B gcc a.o b.o -o test -flto
31
32 The above will print something like
33 /tmp/ccwbQ8B2.lto.o
34
35 If WHOPR is used instead, more than one file might be produced
36 ./ccXj2DTk.lto.ltrans.o
37 ./ccCJuXGv.lto.ltrans.o
38 */
39
40 #include "config.h"
41 #include "system.h"
42 #include "coretypes.h"
43 #include "intl.h"
44 #include "diagnostic.h"
45 #include "obstack.h"
46 #include "opts.h"
47 #include "options.h"
48 #include "simple-object.h"
49 #include "lto-section-names.h"
50 #include "collect-utils.h"
51
52 /* Environment variable, used for passing the names of offload targets from GCC
53 driver to lto-wrapper. */
54 #define OFFLOAD_TARGET_NAMES_ENV "OFFLOAD_TARGET_NAMES"
55
56 enum lto_mode_d {
57 LTO_MODE_NONE, /* Not doing LTO. */
58 LTO_MODE_LTO, /* Normal LTO. */
59 LTO_MODE_WHOPR /* WHOPR. */
60 };
61
62 /* Current LTO mode. */
63 static enum lto_mode_d lto_mode = LTO_MODE_NONE;
64
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;
76
77 const char tool_name[] = "lto-wrapper";
78
79 /* Delete tempfiles. Called from utils_cleanup. */
80
81 void
82 tool_cleanup (bool)
83 {
84 unsigned int i;
85
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)
99 {
100 maybe_unlink (input_names[i]);
101 if (output_names[i])
102 maybe_unlink (output_names[i]);
103 }
104 }
105
106 static void
107 lto_wrapper_cleanup (void)
108 {
109 utils_cleanup (false);
110 }
111
112 /* Unlink a temporary LTRANS file unless requested otherwise. */
113
114 void
115 maybe_unlink (const char *file)
116 {
117 if (!save_temps)
118 {
119 if (unlink_if_ordinary (file)
120 && errno != ENOENT)
121 fatal_error (input_location, "deleting LTRANS file %s: %m", file);
122 }
123 else if (verbose)
124 fprintf (stderr, "[Leaving LTRANS %s]\n", file);
125 }
126
127 /* Template of LTRANS dumpbase suffix. */
128 #define DUMPBASE_SUFFIX ".ltrans18446744073709551615"
129
130 /* Create decoded options from the COLLECT_GCC and COLLECT_GCC_OPTIONS
131 environment according to LANG_MASK. */
132
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)
139 {
140 struct obstack argv_obstack;
141 char *argv_storage;
142 const char **argv;
143 int j, k, argc;
144
145 argv_storage = xstrdup (collect_gcc_options);
146 obstack_init (&argv_obstack);
147 obstack_ptr_grow (&argv_obstack, collect_gcc);
148
149 for (j = 0, k = 0; argv_storage[j] != '\0'; ++j)
150 {
151 if (argv_storage[j] == '\'')
152 {
153 obstack_ptr_grow (&argv_obstack, &argv_storage[k]);
154 ++j;
155 do
156 {
157 if (argv_storage[j] == '\0')
158 fatal_error (input_location, "malformed COLLECT_GCC_OPTIONS");
159 else if (strncmp (&argv_storage[j], "'\\''", 4) == 0)
160 {
161 argv_storage[k++] = '\'';
162 j += 4;
163 }
164 else if (argv_storage[j] == '\'')
165 break;
166 else
167 argv_storage[k++] = argv_storage[j++];
168 }
169 while (1);
170 argv_storage[k++] = '\0';
171 }
172 }
173
174 obstack_ptr_grow (&argv_obstack, NULL);
175 argc = obstack_object_size (&argv_obstack) / sizeof (void *) - 1;
176 argv = XOBFINISH (&argv_obstack, const char **);
177
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);
182 }
183
184 /* Append OPTION to the options array DECODED_OPTIONS with size
185 DECODED_OPTIONS_COUNT. */
186
187 static void
188 append_option (struct cl_decoded_option **decoded_options,
189 unsigned int *decoded_options_count,
190 struct cl_decoded_option *option)
191 {
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));
200 }
201
202 /* Remove option number INDEX from DECODED_OPTIONS, update
203 DECODED_OPTIONS_COUNT. */
204
205 static void
206 remove_option (struct cl_decoded_option **decoded_options,
207 int index, unsigned int *decoded_options_count)
208 {
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));
214 }
215
216 /* Try to merge and complain about options FDECODED_OPTIONS when applied
217 ontop of DECODED_OPTIONS. */
218
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)
224 {
225 unsigned int i, j;
226 struct cl_decoded_option *pic_option = NULL;
227 struct cl_decoded_option *pie_option = NULL;
228
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. */
241
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)
245 {
246 struct cl_decoded_option *foption = &fdecoded_options[i];
247 switch (foption->opt_index)
248 {
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;
255
256 default:
257 if (!(cl_options[foption->opt_index].flags & CL_TARGET))
258 break;
259
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;
279
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;
289
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;
303
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;
316
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. */
332 ;
333 else
334 {
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)
340 {
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 ();
358 }
359 switch ((*decoded_options)[j].opt_index)
360 {
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 ();
378 }
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;
385 }
386 break;
387
388
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;
400
401
402 case OPT_foffload_:
403 append_option (decoded_options, decoded_options_count, foption);
404 break;
405 }
406 }
407
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.
414
415 Similarly we merge PIE options, however in addition we keep
416 -fPIC + -fPIE = -fPIE
417 -fpic + -fPIE = -fpie
418 -fPIC/-fpic + -fpie = -fpie
419
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. */
422
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)
426 {
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)
434 {
435 if (pie_option)
436 {
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++;
446 }
447 else if (pic_option)
448 {
449 (*decoded_options)[j] = *pic_option;
450 j++;
451 }
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);
456 }
457 else if (pic_option->opt_index == OPT_fpic
458 && (*decoded_options)[j].opt_index == OPT_fPIC)
459 {
460 (*decoded_options)[j] = *pic_option;
461 j++;
462 }
463 else
464 j++;
465 }
466 else if ((*decoded_options)[j].opt_index == OPT_fPIE
467 || (*decoded_options)[j].opt_index == OPT_fpie)
468 {
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)
476 {
477 /* If -fPIC/-fpic is given, merge it with -fPIE/-fpie. */
478 if (pic_option)
479 {
480 if (pic_option->opt_index == OPT_fpic
481 && (*decoded_options)[j].opt_index == OPT_fPIE)
482 {
483 (*decoded_options)[j].opt_index = OPT_fpie;
484 (*decoded_options)[j].canonical_option[0]
485 = pic_option->value ? "-fpie" : "-fno-pie";
486 }
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++;
491 }
492 else if (pie_option)
493 {
494 (*decoded_options)[j] = *pie_option;
495 j++;
496 }
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);
502 }
503 else if (pie_option->opt_index == OPT_fpie
504 && (*decoded_options)[j].opt_index == OPT_fPIE)
505 {
506 (*decoded_options)[j] = *pie_option;
507 j++;
508 }
509 else
510 j++;
511 }
512 else
513 j++;
514 }
515
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. */
519
520 static void **
521 free_array_of_ptrs (void **ptr, unsigned n)
522 {
523 if (!ptr)
524 return NULL;
525 for (unsigned i = 0; i < n; i++)
526 {
527 if (!ptr[i])
528 break;
529 free (ptr[i]);
530 }
531 free (ptr);
532 return NULL;
533 }
534
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. */
538
539 static unsigned
540 parse_env_var (const char *str, char ***pvalues, const char *append)
541 {
542 const char *curval, *nextval;
543 char **values;
544 unsigned num = 1, i;
545
546 curval = strchr (str, ':');
547 while (curval)
548 {
549 num++;
550 curval = strchr (curval + 1, ':');
551 }
552
553 values = (char**) xmalloc (num * sizeof (char*));
554 curval = str;
555 nextval = strchr (curval, ':');
556 if (nextval == NULL)
557 nextval = strchr (curval, '\0');
558
559 int append_len = append ? strlen (append) : 0;
560 for (i = 0; i < num; i++)
561 {
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');
572 }
573 *pvalues = values;
574 return num;
575 }
576
577 /* Append options OPTS from lto or offload_lto sections to ARGV_OBSTACK. */
578
579 static void
580 append_compiler_options (obstack *argv_obstack, struct cl_decoded_option *opts,
581 unsigned int count)
582 {
583 /* Append compiler driver arguments as far as they were merged. */
584 for (unsigned int j = 1; j < count; ++j)
585 {
586 struct cl_decoded_option *option = &opts[j];
587
588 /* File options have been properly filtered by lto-opts.c. */
589 switch (option->opt_index)
590 {
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;
596
597 default:
598 break;
599 }
600
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)
604 {
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;
626
627 default:
628 if (!(cl_options[option->opt_index].flags & CL_TARGET))
629 continue;
630 }
631
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]);
635 }
636 }
637
638 /* Append diag options in OPTS with length COUNT to ARGV_OBSTACK. */
639
640 static void
641 append_diag_options (obstack *argv_obstack, struct cl_decoded_option *opts,
642 unsigned int count)
643 {
644 /* Append compiler driver arguments as far as they were merged. */
645 for (unsigned int j = 1; j < count; ++j)
646 {
647 struct cl_decoded_option *option = &opts[j];
648
649 switch (option->opt_index)
650 {
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;
662 }
663
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]);
667 }
668 }
669
670
671 /* Append linker options OPTS to ARGV_OBSTACK. */
672
673 static void
674 append_linker_options (obstack *argv_obstack, struct cl_decoded_option *opts,
675 unsigned int count)
676 {
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)
680 {
681 struct cl_decoded_option *option = &opts[j];
682
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;
687
688 switch (option->opt_index)
689 {
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;
695
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;
703
704 default:
705 break;
706 }
707
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]);
711 }
712 }
713
714 /* Extract options for TARGET offload compiler from OPTIONS and append
715 them to ARGV_OBSTACK. */
716
717 static void
718 append_offload_options (obstack *argv_obstack, const char *target,
719 struct cl_decoded_option *options,
720 unsigned int options_count)
721 {
722 for (unsigned i = 0; i < options_count; i++)
723 {
724 const char *cur, *next, *opts;
725 char **argv;
726 unsigned argc;
727 struct cl_decoded_option *option = &options[i];
728
729 if (option->opt_index != OPT_foffload_)
730 continue;
731
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
738 {
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;
744
745 cur = option->arg;
746
747 while (cur < opts)
748 {
749 next = strchr (cur, ',');
750 if (next == NULL)
751 next = opts;
752 next = (next > opts) ? opts : next;
753
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;
758
759 /* Skip the comma or equal sign. */
760 cur = next + 1;
761 }
762
763 if (cur >= opts)
764 continue;
765
766 opts++;
767 }
768
769 argv = buildargv (opts);
770 for (argc = 0; argv[argc]; argc++)
771 obstack_ptr_grow (argv_obstack, argv[argc]);
772 }
773 }
774
775 /* Check whether NAME can be accessed in MODE. This is like access,
776 except that it never considers directories to be executable. */
777
778 static int
779 access_check (const char *name, int mode)
780 {
781 if (mode == X_OK)
782 {
783 struct stat st;
784
785 if (stat (name, &st) < 0
786 || S_ISDIR (st.st_mode))
787 return -1;
788 }
789
790 return access (name, mode);
791 }
792
793 /* Prepare a target image for offload TARGET, using mkoffload tool from
794 COMPILER_PATH. Return the name of the resultant object file. */
795
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)
803 {
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");
811
812 char **paths = NULL;
813 unsigned n_paths = parse_env_var (compiler_path, &paths, suffix);
814
815 const char *compiler = NULL;
816 for (unsigned i = 0; i < n_paths; i++)
817 if (access_check (paths[i], X_OK) == 0)
818 {
819 compiler = paths[i];
820 break;
821 }
822
823 if (!compiler)
824 fatal_error (input_location,
825 "could not find %s in %s (consider using '-B')\n", suffix + 1,
826 compiler_path);
827
828 /* Generate temporary output file name. */
829 filename = make_temp_file (".target.o");
830
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);
840
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]);
844
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);
849
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);
856
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);
861
862 free_array_of_ptrs ((void **) paths, n_paths);
863 return filename;
864 }
865
866
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. */
871
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)
878 {
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);
884
885 int next_name_entry = 0;
886 const char *compiler_path = getenv ("COMPILER_PATH");
887 if (!compiler_path)
888 goto out;
889
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++)
894 {
895 /* HSA does not use LTO-like streaming and a different compiler, skip
896 it. */
897 if (strcmp (names[i], "hsa") == 0)
898 continue;
899
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++;
908 }
909
910 out:
911 free_array_of_ptrs ((void **) names, num_targets);
912 }
913
914 /* Copy a file from SRC to DEST. */
915
916 static void
917 copy_file (const char *dest, const char *src)
918 {
919 FILE *d = fopen (dest, "wb");
920 FILE *s = fopen (src, "rb");
921 char buffer[512];
922 while (!feof (s))
923 {
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)
928 {
929 fwrite (buffer, 1, len, d);
930 if (ferror (d) != 0)
931 fatal_error (input_location, "writing output file");
932 }
933 }
934 fclose (d);
935 fclose (s);
936 }
937
938 /* Find the crtoffloadtable.o file in LIBRARY_PATH, make copy and pass name of
939 the copy to the linker. */
940
941 static void
942 find_crtoffloadtable (void)
943 {
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");
949
950 unsigned i;
951 for (i = 0; i < n_paths; i++)
952 if (access_check (paths[i], R_OK) == 0)
953 {
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;
960 }
961 if (i == n_paths)
962 fatal_error (input_location,
963 "installation error, can%'t find crtoffloadtable.o");
964
965 free_array_of_ptrs ((void **) paths, n_paths);
966 }
967
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. */
973
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)
978 {
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;
986
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;
992
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))
998 {
999 simple_object_release_read (sobj);
1000 return false;
1001 }
1002
1003 lseek (fd, file_offset + offset, SEEK_SET);
1004 data = (char *)xmalloc (length);
1005 read (fd, data, length);
1006 fopts = data;
1007 do
1008 {
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)
1016 {
1017 fdecoded_options = f2decoded_options;
1018 fdecoded_options_count = f2decoded_options_count;
1019 }
1020 else
1021 merge_and_complain (&fdecoded_options,
1022 &fdecoded_options_count,
1023 f2decoded_options, f2decoded_options_count);
1024
1025 fopts += strlen (fopts) + 1;
1026 }
1027 while (fopts - data < length);
1028
1029 free (data);
1030 simple_object_release_read (sobj);
1031 *opts = fdecoded_options;
1032 *opt_count = fdecoded_options_count;
1033 return true;
1034 }
1035
1036 /* Copy early debug info sections from INFILE to a new file whose name
1037 is returned. Return NULL on error. */
1038
1039 const char *
1040 debug_objcopy (const char *infile, bool rename)
1041 {
1042 char *outfile;
1043 const char *errmsg;
1044 int err;
1045
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)
1054 {
1055 char *fname = xstrdup (infile);
1056 fname[p - infile] = '\0';
1057 infile = fname;
1058 inoff = (off_t) loffset;
1059 }
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;
1068
1069 off_t off, len;
1070 if (simple_object_find_section (inobj, ".gnu.debuglto_.debug_info",
1071 &off, &len, &errmsg, &err) != 1)
1072 {
1073 if (errmsg)
1074 fatal_error (0, "%s: %s\n", errmsg, xstrerror (err));
1075
1076 simple_object_release_read (inobj);
1077 close (infd);
1078 return NULL;
1079 }
1080
1081 if (save_temps)
1082 {
1083 outfile = (char *) xmalloc (strlen (infile)
1084 + sizeof (".debug.temp.o") + 1);
1085 strcpy (outfile, infile);
1086 strcat (outfile, ".debug.temp.o");
1087 }
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)
1092 {
1093 unlink_if_ordinary (outfile);
1094 fatal_error (0, "%s: %s\n", errmsg, xstrerror (err));
1095 }
1096
1097 simple_object_release_read (inobj);
1098 close (infd);
1099
1100 return outfile;
1101 }
1102
1103 /* Helper for qsort: compare priorities for parallel compilation. */
1104
1105 int
1106 cmp_priority (const void *a, const void *b)
1107 {
1108 return *((const int *)b)-*((const int *)a);
1109 }
1110
1111
1112 /* Execute gcc. ARGC is the number of arguments. ARGV contains the arguments. */
1113
1114 static void
1115 run_gcc (unsigned argc, char *argv[])
1116 {
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;
1141
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);
1155
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);
1160
1161 /* Look at saved options in the IL files. */
1162 for (i = 1; i < argc; ++i)
1163 {
1164 char *p;
1165 int fd;
1166 off_t file_offset = 0;
1167 long loffset;
1168 int consumed;
1169 char *filename = argv[i];
1170
1171 if (strncmp (argv[i], "-foffload-objects=",
1172 sizeof ("-foffload-objects=") - 1) == 0)
1173 {
1174 have_offload = true;
1175 offload_objects_file_name
1176 = argv[i] + sizeof ("-foffload-objects=") - 1;
1177 continue;
1178 }
1179
1180 if ((p = strrchr (argv[i], '@'))
1181 && p != argv[i]
1182 && sscanf (p, "@%li%n", &loffset, &consumed) >= 1
1183 && strlen (p) == (unsigned int) consumed)
1184 {
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;
1189 }
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)
1196 {
1197 lto_argv[lto_argc++] = argv[i];
1198 if (strcmp (argv[i], "-flinker-output=rel") == 0)
1199 linker_output_rel = true;
1200 continue;
1201 }
1202
1203 if (find_and_merge_options (fd, file_offset, LTO_SECTION_NAME_PREFIX,
1204 &fdecoded_options, &fdecoded_options_count,
1205 collect_gcc))
1206 {
1207 have_lto = true;
1208 ltoobj_argv[ltoobj_argc++] = argv[i];
1209 }
1210 close (fd);
1211 }
1212
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");
1218
1219 append_compiler_options (&argv_obstack, fdecoded_options,
1220 fdecoded_options_count);
1221 append_linker_options (&argv_obstack, decoded_options, decoded_options_count);
1222
1223 /* Scan linker driver arguments for things that are of relevance to us. */
1224 for (j = 1; j < decoded_options_count; ++j)
1225 {
1226 struct cl_decoded_option *option = &decoded_options[j];
1227 switch (option->opt_index)
1228 {
1229 case OPT_o:
1230 linker_output = option->arg;
1231 break;
1232
1233 case OPT_save_temps:
1234 save_temps = 1;
1235 break;
1236
1237 case OPT_v:
1238 verbose = 1;
1239 break;
1240
1241 case OPT_flto_partition_:
1242 if (strcmp (option->arg, "none") == 0)
1243 no_partition = true;
1244 break;
1245
1246 case OPT_flto_:
1247 if (strcmp (option->arg, "jobserver") == 0)
1248 {
1249 jobserver = 1;
1250 parallel = 1;
1251 }
1252 else
1253 {
1254 parallel = atoi (option->arg);
1255 if (parallel <= 1)
1256 parallel = 0;
1257 }
1258 /* Fallthru. */
1259
1260 case OPT_flto:
1261 lto_mode = LTO_MODE_WHOPR;
1262 break;
1263
1264 case OPT_flinker_output_:
1265 linker_output_rel = !strcmp (option->arg, "rel");
1266 break;
1267
1268
1269 default:
1270 break;
1271 }
1272 }
1273
1274 /* Output lto-wrapper invocation command. */
1275 if (verbose)
1276 {
1277 for (i = 0; i < argc; ++i)
1278 {
1279 fputs (argv[i], stderr);
1280 fputc (' ', stderr);
1281 }
1282 fputc ('\n', stderr);
1283 }
1284
1285 if (linker_output_rel)
1286 no_partition = true;
1287
1288 if (no_partition)
1289 {
1290 lto_mode = LTO_MODE_LTO;
1291 jobserver = 0;
1292 parallel = 0;
1293 }
1294
1295 if (linker_output)
1296 {
1297 char *output_dir, *base, *name;
1298 bool bit_bucket = strcmp (linker_output, HOST_BIT_BUCKET) == 0;
1299
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';
1306
1307 linker_output = &linker_output[base - output_dir];
1308 if (*output_dir == '\0')
1309 {
1310 static char current_dir[] = { '.', DIR_SEPARATOR, '\0' };
1311 output_dir = current_dir;
1312 }
1313 if (!bit_bucket)
1314 {
1315 obstack_ptr_grow (&argv_obstack, "-dumpdir");
1316 obstack_ptr_grow (&argv_obstack, output_dir);
1317 }
1318
1319 obstack_ptr_grow (&argv_obstack, "-dumpbase");
1320 }
1321
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 *);
1324
1325 if (have_offload)
1326 {
1327 unsigned i, num_offload_files;
1328 char **offload_argv;
1329 FILE *f;
1330
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);
1339
1340 /* Read names of object files with offload. */
1341 for (i = 0; i < num_offload_files; i++)
1342 {
1343 const unsigned piece = 32;
1344 char *buf, *filename = XNEWVEC (char, piece);
1345 size_t len;
1346
1347 buf = filename;
1348 cont1:
1349 if (!fgets (buf, piece, f))
1350 break;
1351 len = strlen (filename);
1352 if (filename[len - 1] != '\n')
1353 {
1354 filename = XRESIZEVEC (char, filename, len + piece);
1355 buf = filename + len;
1356 goto cont1;
1357 }
1358 filename[len - 1] = '\0';
1359 offload_argv[i] = filename;
1360 }
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;
1367
1368 /* Look at saved offload options in files. */
1369 for (i = 0; i < num_offload_files; i++)
1370 {
1371 char *p;
1372 long loffset;
1373 int fd, consumed;
1374 off_t file_offset = 0;
1375 char *filename = offload_argv[i];
1376
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)
1381 {
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;
1386 }
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);
1399 }
1400
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);
1406
1407 free_array_of_ptrs ((void **) offload_argv, num_offload_files);
1408
1409 if (offload_names)
1410 {
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);
1415 }
1416 }
1417
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;
1423
1424 if (lto_mode == LTO_MODE_LTO)
1425 {
1426 if (linker_output)
1427 {
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");
1433 }
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);
1438 }
1439 else
1440 {
1441 const char *list_option = "-fltrans-output-list=";
1442 size_t list_option_len = strlen (list_option);
1443 char *tmp;
1444
1445 if (linker_output)
1446 {
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);
1452 }
1453
1454 if (linker_output && save_temps)
1455 {
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");
1460 }
1461 else
1462 {
1463 char *prefix = NULL;
1464 if (linker_output)
1465 {
1466 prefix = (char *) xmalloc (strlen (linker_output) + 2);
1467 strcpy (prefix, linker_output);
1468 strcat (prefix, ".");
1469 }
1470
1471 ltrans_output_file = make_temp_file_with_prefix (prefix,
1472 ".ltrans.out");
1473 free (prefix);
1474 }
1475 list_option_full = (char *) xmalloc (sizeof (char) *
1476 (strlen (ltrans_output_file) + list_option_len + 1));
1477 tmp = list_option_full;
1478
1479 obstack_ptr_grow (&argv_obstack, tmp);
1480 strcpy (tmp, list_option);
1481 tmp += list_option_len;
1482 strcpy (tmp, ltrans_output_file);
1483
1484 if (jobserver)
1485 obstack_ptr_grow (&argv_obstack, xstrdup ("-fwpa=jobserver"));
1486 else if (parallel > 1)
1487 {
1488 char buf[256];
1489 sprintf (buf, "-fwpa=%i", parallel);
1490 obstack_ptr_grow (&argv_obstack, xstrdup (buf));
1491 }
1492 else
1493 obstack_ptr_grow (&argv_obstack, "-fwpa");
1494 }
1495
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);
1503
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);
1507
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)
1513 {
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)
1517 {
1518 const char *tem;
1519 if ((tem = debug_objcopy (ltoobj_argv[i], !linker_output_rel)))
1520 {
1521 early_debug_object_names[i] = tem;
1522 n_debugobj++;
1523 }
1524 }
1525 }
1526
1527 if (lto_mode == LTO_MODE_LTO)
1528 {
1529 printf ("%s\n", flto_out);
1530 if (!skip_debug)
1531 {
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]);
1535 }
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;
1541 }
1542 else
1543 {
1544 FILE *stream = fopen (ltrans_output_file, "r");
1545 FILE *mstream = NULL;
1546 struct obstack env_obstack;
1547 int priority;
1548
1549 if (!stream)
1550 fatal_error (input_location, "fopen: %s: %m", ltrans_output_file);
1551
1552 /* Parse the list of LTRANS inputs from the WPA stage. */
1553 obstack_init (&env_obstack);
1554 nr = 0;
1555 for (;;)
1556 {
1557 const unsigned piece = 32;
1558 char *output_name = NULL;
1559 char *buf, *input_name = (char *)xmalloc (piece);
1560 size_t len;
1561
1562 buf = input_name;
1563 if (fscanf (stream, "%i\n", &priority) != 1)
1564 {
1565 if (!feof (stream))
1566 fatal_error (input_location,
1567 "Corrupted ltrans output file %s",
1568 ltrans_output_file);
1569 break;
1570 }
1571 cont:
1572 if (!fgets (buf, piece, stream))
1573 break;
1574 len = strlen (input_name);
1575 if (input_name[len - 1] != '\n')
1576 {
1577 input_name = (char *)xrealloc (input_name, len + piece);
1578 buf = input_name + len;
1579 goto cont;
1580 }
1581 input_name[len - 1] = '\0';
1582
1583 if (input_name[0] == '*')
1584 output_name = &input_name[1];
1585
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;
1595 }
1596 fclose (stream);
1597 maybe_unlink (ltrans_output_file);
1598 ltrans_output_file = NULL;
1599
1600 if (parallel)
1601 {
1602 makefile = make_temp_file (".mk");
1603 mstream = fopen (makefile, "w");
1604 qsort (ltrans_priorities, nr, sizeof (int) * 2, cmp_priority);
1605 }
1606
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)
1610 {
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;
1616
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 *);
1622
1623 /* Adjust the dumpbase if the linker output file was seen. */
1624 if (linker_output)
1625 {
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;
1633 }
1634
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)
1641 {
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);
1653 }
1654 else
1655 {
1656 fork_execute (new_argv[0], CONST_CAST (char **, new_argv),
1657 true);
1658 maybe_unlink (input_name);
1659 }
1660
1661 output_names[i] = output_name;
1662 }
1663 if (parallel)
1664 {
1665 struct pex_obj *pex;
1666 char jobs[32];
1667
1668 fprintf (mstream,
1669 ".PHONY: all\n"
1670 "all:");
1671 for (i = 0; i < nr; ++i)
1672 {
1673 int j = ltrans_priorities[i*2 + 1];
1674 fprintf (mstream, " \\\n\t%s", output_names[j]);
1675 }
1676 fprintf (mstream, "\n");
1677 fclose (mstream);
1678 if (!jobserver)
1679 {
1680 /* Avoid passing --jobserver-fd= and similar flags
1681 unless jobserver mode is explicitly enabled. */
1682 putenv (xstrdup ("MAKEFLAGS="));
1683 putenv (xstrdup ("MFLAGS="));
1684 }
1685 new_argv[0] = getenv ("MAKE");
1686 if (!new_argv[0])
1687 new_argv[0] = "make";
1688 new_argv[1] = "-f";
1689 new_argv[2] = makefile;
1690 i = 3;
1691 if (!jobserver)
1692 {
1693 snprintf (jobs, 31, "-j%d", parallel);
1694 new_argv[i++] = jobs;
1695 }
1696 new_argv[i++] = "all";
1697 new_argv[i++] = NULL;
1698 pex = collect_execute (new_argv[0], CONST_CAST (char **, new_argv),
1699 NULL, NULL, PEX_SEARCH, false);
1700 do_wait (new_argv[0], pex);
1701 maybe_unlink (makefile);
1702 makefile = NULL;
1703 for (i = 0; i < nr; ++i)
1704 maybe_unlink (input_names[i]);
1705 }
1706 for (i = 0; i < nr; ++i)
1707 {
1708 fputs (output_names[i], stdout);
1709 putc ('\n', stdout);
1710 free (input_names[i]);
1711 }
1712 if (!skip_debug)
1713 {
1714 for (i = 0; i < ltoobj_argc; ++i)
1715 if (early_debug_object_names[i] != NULL)
1716 printf ("%s\n", early_debug_object_names[i]);
1717 }
1718 nr = 0;
1719 free (ltrans_priorities);
1720 free (output_names);
1721 output_names = NULL;
1722 free (early_debug_object_names);
1723 early_debug_object_names = NULL;
1724 free (input_names);
1725 free (list_option_full);
1726 obstack_free (&env_obstack, NULL);
1727 }
1728
1729 finish:
1730 XDELETE (lto_argv);
1731 obstack_free (&argv_obstack, NULL);
1732 }
1733
1734
1735 /* Entry point. */
1736
1737 int
1738 main (int argc, char *argv[])
1739 {
1740 const char *p;
1741
1742 init_opts_obstack ();
1743
1744 p = argv[0] + strlen (argv[0]);
1745 while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
1746 --p;
1747 progname = p;
1748
1749 xmalloc_set_program_name (progname);
1750
1751 gcc_init_libintl ();
1752
1753 diagnostic_initialize (global_dc, 0);
1754
1755 if (atexit (lto_wrapper_cleanup) != 0)
1756 fatal_error (input_location, "atexit failed");
1757
1758 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
1759 signal (SIGINT, fatal_signal);
1760 #ifdef SIGHUP
1761 if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
1762 signal (SIGHUP, fatal_signal);
1763 #endif
1764 if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
1765 signal (SIGTERM, fatal_signal);
1766 #ifdef SIGPIPE
1767 if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
1768 signal (SIGPIPE, fatal_signal);
1769 #endif
1770 #ifdef SIGCHLD
1771 /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
1772 receive the signal. A different setting is inheritable */
1773 signal (SIGCHLD, SIG_DFL);
1774 #endif
1775
1776 /* We may be called with all the arguments stored in some file and
1777 passed with @file. Expand them into argv before processing. */
1778 expandargv (&argc, &argv);
1779
1780 run_gcc (argc, argv);
1781
1782 return 0;
1783 }