lto-wrapper: Use nontemp filename with -save-temps
[gcc.git] / gcc / lto-wrapper.c
1 /* Wrapper to call lto. Used by collect2 and the linker plugin.
2 Copyright (C) 2009-2020 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 /* By default there is no special suffix for target executables. */
57 #ifdef TARGET_EXECUTABLE_SUFFIX
58 #define HAVE_TARGET_EXECUTABLE_SUFFIX
59 #else
60 #define TARGET_EXECUTABLE_SUFFIX ""
61 #endif
62
63 enum lto_mode_d {
64 LTO_MODE_NONE, /* Not doing LTO. */
65 LTO_MODE_LTO, /* Normal LTO. */
66 LTO_MODE_WHOPR /* WHOPR. */
67 };
68
69 /* Current LTO mode. */
70 static enum lto_mode_d lto_mode = LTO_MODE_NONE;
71
72 static char *ltrans_output_file;
73 static char *flto_out;
74 static unsigned int nr;
75 static int *ltrans_priorities;
76 static char **input_names;
77 static char **output_names;
78 static char **offload_names;
79 static char *offload_objects_file_name;
80 static char *makefile;
81 static unsigned int num_deb_objs;
82 static const char **early_debug_object_names;
83 static bool xassembler_options_error = false;
84
85 const char tool_name[] = "lto-wrapper";
86
87 /* Delete tempfiles. Called from utils_cleanup. */
88
89 void
90 tool_cleanup (bool)
91 {
92 unsigned int i;
93
94 if (ltrans_output_file)
95 maybe_unlink (ltrans_output_file);
96 if (flto_out)
97 maybe_unlink (flto_out);
98 if (offload_objects_file_name)
99 maybe_unlink (offload_objects_file_name);
100 if (makefile)
101 maybe_unlink (makefile);
102 if (early_debug_object_names)
103 for (i = 0; i < num_deb_objs; ++i)
104 if (early_debug_object_names[i])
105 maybe_unlink (early_debug_object_names[i]);
106 for (i = 0; i < nr; ++i)
107 {
108 maybe_unlink (input_names[i]);
109 if (output_names[i])
110 maybe_unlink (output_names[i]);
111 }
112 }
113
114 static void
115 lto_wrapper_cleanup (void)
116 {
117 utils_cleanup (false);
118 }
119
120 /* Unlink a temporary LTRANS file unless requested otherwise. */
121
122 void
123 maybe_unlink (const char *file)
124 {
125 if (!save_temps)
126 {
127 if (unlink_if_ordinary (file)
128 && errno != ENOENT)
129 fatal_error (input_location, "deleting LTRANS file %s: %m", file);
130 }
131 else if (verbose)
132 fprintf (stderr, "[Leaving LTRANS %s]\n", file);
133 }
134
135 /* Template of LTRANS dumpbase suffix. */
136 #define DUMPBASE_SUFFIX "ltrans18446744073709551615"
137
138 /* Create decoded options from the COLLECT_GCC and COLLECT_GCC_OPTIONS
139 environment. */
140
141 static void
142 get_options_from_collect_gcc_options (const char *collect_gcc,
143 const char *collect_gcc_options,
144 struct cl_decoded_option **decoded_options,
145 unsigned int *decoded_options_count)
146 {
147 struct obstack argv_obstack;
148 const char **argv;
149 int argc;
150
151 obstack_init (&argv_obstack);
152 obstack_ptr_grow (&argv_obstack, collect_gcc);
153
154 parse_options_from_collect_gcc_options (collect_gcc_options,
155 &argv_obstack, &argc);
156 argv = XOBFINISH (&argv_obstack, const char **);
157
158 decode_cmdline_options_to_array (argc, (const char **)argv, CL_DRIVER,
159 decoded_options, decoded_options_count);
160 obstack_free (&argv_obstack, NULL);
161 }
162
163 /* Append OPTION to the options array DECODED_OPTIONS with size
164 DECODED_OPTIONS_COUNT. */
165
166 static void
167 append_option (struct cl_decoded_option **decoded_options,
168 unsigned int *decoded_options_count,
169 struct cl_decoded_option *option)
170 {
171 ++*decoded_options_count;
172 *decoded_options
173 = (struct cl_decoded_option *)
174 xrealloc (*decoded_options,
175 (*decoded_options_count
176 * sizeof (struct cl_decoded_option)));
177 memcpy (&(*decoded_options)[*decoded_options_count - 1], option,
178 sizeof (struct cl_decoded_option));
179 }
180
181 /* Remove option number INDEX from DECODED_OPTIONS, update
182 DECODED_OPTIONS_COUNT. */
183
184 static void
185 remove_option (struct cl_decoded_option **decoded_options,
186 int index, unsigned int *decoded_options_count)
187 {
188 --*decoded_options_count;
189 memmove (&(*decoded_options)[index + 1],
190 &(*decoded_options)[index],
191 sizeof (struct cl_decoded_option)
192 * (*decoded_options_count - index));
193 }
194
195 /* Try to merge and complain about options FDECODED_OPTIONS when applied
196 ontop of DECODED_OPTIONS. */
197
198 static void
199 merge_and_complain (struct cl_decoded_option **decoded_options,
200 unsigned int *decoded_options_count,
201 struct cl_decoded_option *fdecoded_options,
202 unsigned int fdecoded_options_count,
203 struct cl_decoded_option *decoded_cl_options,
204 unsigned int decoded_cl_options_count)
205 {
206 unsigned int i, j;
207 struct cl_decoded_option *pic_option = NULL;
208 struct cl_decoded_option *pie_option = NULL;
209 struct cl_decoded_option *cf_protection_option = NULL;
210
211 /* ??? Merge options from files. Most cases can be
212 handled by either unioning or intersecting
213 (for example -fwrapv is a case for unioning,
214 -ffast-math is for intersection). Most complaints
215 about real conflicts between different options can
216 be deferred to the compiler proper. Options that
217 we can neither safely handle by intersection nor
218 unioning would need to be complained about here.
219 Ideally we'd have a flag in the opt files that
220 tells whether to union or intersect or reject.
221 In absence of that it's unclear what a good default is.
222 It's also difficult to get positional handling correct. */
223
224 /* Look for a -fcf-protection option in the link-time options
225 which overrides any -fcf-protection from the lto sections. */
226 for (i = 0; i < decoded_cl_options_count; ++i)
227 {
228 struct cl_decoded_option *foption = &decoded_cl_options[i];
229 if (foption->opt_index == OPT_fcf_protection_)
230 {
231 cf_protection_option = foption;
232 }
233 }
234
235 /* The following does what the old LTO option code did,
236 union all target and a selected set of common options. */
237 for (i = 0; i < fdecoded_options_count; ++i)
238 {
239 struct cl_decoded_option *foption = &fdecoded_options[i];
240 switch (foption->opt_index)
241 {
242 case OPT_SPECIAL_unknown:
243 case OPT_SPECIAL_ignore:
244 case OPT_SPECIAL_warn_removed:
245 case OPT_SPECIAL_program_name:
246 case OPT_SPECIAL_input_file:
247 break;
248
249 default:
250 if (!(cl_options[foption->opt_index].flags & CL_TARGET))
251 break;
252
253 /* Fallthru. */
254 case OPT_fdiagnostics_show_caret:
255 case OPT_fdiagnostics_show_labels:
256 case OPT_fdiagnostics_show_line_numbers:
257 case OPT_fdiagnostics_show_option:
258 case OPT_fdiagnostics_show_location_:
259 case OPT_fshow_column:
260 case OPT_fcommon:
261 case OPT_fgnu_tm:
262 case OPT_g:
263 /* Do what the old LTO code did - collect exactly one option
264 setting per OPT code, we pick the first we encounter.
265 ??? This doesn't make too much sense, but when it doesn't
266 then we should complain. */
267 for (j = 0; j < *decoded_options_count; ++j)
268 if ((*decoded_options)[j].opt_index == foption->opt_index)
269 break;
270 if (j == *decoded_options_count)
271 append_option (decoded_options, decoded_options_count, foption);
272 break;
273
274 /* Figure out what PIC/PIE level wins and merge the results. */
275 case OPT_fPIC:
276 case OPT_fpic:
277 pic_option = foption;
278 break;
279 case OPT_fPIE:
280 case OPT_fpie:
281 pie_option = foption;
282 break;
283
284 case OPT_fopenmp:
285 case OPT_fopenacc:
286 /* For selected options we can merge conservatively. */
287 for (j = 0; j < *decoded_options_count; ++j)
288 if ((*decoded_options)[j].opt_index == foption->opt_index)
289 break;
290 if (j == *decoded_options_count)
291 append_option (decoded_options, decoded_options_count, foption);
292 /* -fopenmp > -fno-openmp,
293 -fopenacc > -fno-openacc */
294 else if (foption->value > (*decoded_options)[j].value)
295 (*decoded_options)[j] = *foption;
296 break;
297
298 case OPT_fopenacc_dim_:
299 /* Append or check identical. */
300 for (j = 0; j < *decoded_options_count; ++j)
301 if ((*decoded_options)[j].opt_index == foption->opt_index)
302 break;
303 if (j == *decoded_options_count)
304 append_option (decoded_options, decoded_options_count, foption);
305 else if (strcmp ((*decoded_options)[j].arg, foption->arg))
306 fatal_error (input_location,
307 "option %s with different values",
308 foption->orig_option_with_args_text);
309 break;
310
311 case OPT_fcf_protection_:
312 /* Default to link-time option, else append or check identical. */
313 if (!cf_protection_option
314 || cf_protection_option->value == CF_CHECK)
315 {
316 for (j = 0; j < *decoded_options_count; ++j)
317 if ((*decoded_options)[j].opt_index == foption->opt_index)
318 break;
319 if (j == *decoded_options_count)
320 append_option (decoded_options, decoded_options_count, foption);
321 else if ((*decoded_options)[j].value != foption->value)
322 {
323 if (cf_protection_option
324 && cf_protection_option->value == CF_CHECK)
325 fatal_error (input_location,
326 "option -fcf-protection with mismatching values"
327 " (%s, %s)",
328 (*decoded_options)[j].arg, foption->arg);
329 else
330 {
331 /* Merge and update the -fcf-protection option. */
332 (*decoded_options)[j].value &= (foption->value
333 & CF_FULL);
334 switch ((*decoded_options)[j].value)
335 {
336 case CF_NONE:
337 (*decoded_options)[j].arg = "none";
338 break;
339 case CF_BRANCH:
340 (*decoded_options)[j].arg = "branch";
341 break;
342 case CF_RETURN:
343 (*decoded_options)[j].arg = "return";
344 break;
345 default:
346 gcc_unreachable ();
347 }
348 }
349 }
350 }
351 break;
352
353 case OPT_O:
354 case OPT_Ofast:
355 case OPT_Og:
356 case OPT_Os:
357 for (j = 0; j < *decoded_options_count; ++j)
358 if ((*decoded_options)[j].opt_index == OPT_O
359 || (*decoded_options)[j].opt_index == OPT_Ofast
360 || (*decoded_options)[j].opt_index == OPT_Og
361 || (*decoded_options)[j].opt_index == OPT_Os)
362 break;
363 if (j == *decoded_options_count)
364 append_option (decoded_options, decoded_options_count, foption);
365 else if ((*decoded_options)[j].opt_index == foption->opt_index
366 && foption->opt_index != OPT_O)
367 /* Exact same options get merged. */
368 ;
369 else
370 {
371 /* For mismatched option kinds preserve the optimization
372 level only, thus merge it as -On. This also handles
373 merging of same optimization level -On. */
374 int level = 0;
375 switch (foption->opt_index)
376 {
377 case OPT_O:
378 if (foption->arg[0] == '\0')
379 level = MAX (level, 1);
380 else
381 level = MAX (level, atoi (foption->arg));
382 break;
383 case OPT_Ofast:
384 level = MAX (level, 3);
385 break;
386 case OPT_Og:
387 level = MAX (level, 1);
388 break;
389 case OPT_Os:
390 level = MAX (level, 2);
391 break;
392 default:
393 gcc_unreachable ();
394 }
395 switch ((*decoded_options)[j].opt_index)
396 {
397 case OPT_O:
398 if ((*decoded_options)[j].arg[0] == '\0')
399 level = MAX (level, 1);
400 else
401 level = MAX (level, atoi ((*decoded_options)[j].arg));
402 break;
403 case OPT_Ofast:
404 level = MAX (level, 3);
405 break;
406 case OPT_Og:
407 level = MAX (level, 1);
408 break;
409 case OPT_Os:
410 level = MAX (level, 2);
411 break;
412 default:
413 gcc_unreachable ();
414 }
415 (*decoded_options)[j].opt_index = OPT_O;
416 char *tem;
417 tem = xasprintf ("-O%d", level);
418 (*decoded_options)[j].arg = &tem[2];
419 (*decoded_options)[j].canonical_option[0] = tem;
420 (*decoded_options)[j].value = 1;
421 }
422 break;
423
424
425 case OPT_foffload_abi_:
426 for (j = 0; j < *decoded_options_count; ++j)
427 if ((*decoded_options)[j].opt_index == foption->opt_index)
428 break;
429 if (j == *decoded_options_count)
430 append_option (decoded_options, decoded_options_count, foption);
431 else if (foption->value != (*decoded_options)[j].value)
432 fatal_error (input_location,
433 "option %s not used consistently in all LTO input"
434 " files", foption->orig_option_with_args_text);
435 break;
436
437
438 case OPT_foffload_:
439 append_option (decoded_options, decoded_options_count, foption);
440 break;
441 }
442 }
443
444 /* Merge PIC options:
445 -fPIC + -fpic = -fpic
446 -fPIC + -fno-pic = -fno-pic
447 -fpic/-fPIC + nothing = nothing.
448 It is a common mistake to mix few -fPIC compiled objects into otherwise
449 non-PIC code. We do not want to build everything with PIC then.
450
451 Similarly we merge PIE options, however in addition we keep
452 -fPIC + -fPIE = -fPIE
453 -fpic + -fPIE = -fpie
454 -fPIC/-fpic + -fpie = -fpie
455
456 It would be good to warn on mismatches, but it is bit hard to do as
457 we do not know what nothing translates to. */
458
459 for (unsigned int j = 0; j < *decoded_options_count;)
460 if ((*decoded_options)[j].opt_index == OPT_fPIC
461 || (*decoded_options)[j].opt_index == OPT_fpic)
462 {
463 /* -fno-pic in one unit implies -fno-pic everywhere. */
464 if ((*decoded_options)[j].value == 0)
465 j++;
466 /* If we have no pic option or merge in -fno-pic, we still may turn
467 existing pic/PIC mode into pie/PIE if -fpie/-fPIE is present. */
468 else if ((pic_option && pic_option->value == 0)
469 || !pic_option)
470 {
471 if (pie_option)
472 {
473 bool big = (*decoded_options)[j].opt_index == OPT_fPIC
474 && pie_option->opt_index == OPT_fPIE;
475 (*decoded_options)[j].opt_index = big ? OPT_fPIE : OPT_fpie;
476 if (pie_option->value)
477 (*decoded_options)[j].canonical_option[0]
478 = big ? "-fPIE" : "-fpie";
479 else
480 (*decoded_options)[j].canonical_option[0] = "-fno-pie";
481 (*decoded_options)[j].value = pie_option->value;
482 j++;
483 }
484 else if (pic_option)
485 {
486 (*decoded_options)[j] = *pic_option;
487 j++;
488 }
489 /* We do not know if target defaults to pic or not, so just remove
490 option if it is missing in one unit but enabled in other. */
491 else
492 remove_option (decoded_options, j, decoded_options_count);
493 }
494 else if (pic_option->opt_index == OPT_fpic
495 && (*decoded_options)[j].opt_index == OPT_fPIC)
496 {
497 (*decoded_options)[j] = *pic_option;
498 j++;
499 }
500 else
501 j++;
502 }
503 else if ((*decoded_options)[j].opt_index == OPT_fPIE
504 || (*decoded_options)[j].opt_index == OPT_fpie)
505 {
506 /* -fno-pie in one unit implies -fno-pie everywhere. */
507 if ((*decoded_options)[j].value == 0)
508 j++;
509 /* If we have no pie option or merge in -fno-pie, we still preserve
510 PIE/pie if pic/PIC is present. */
511 else if ((pie_option && pie_option->value == 0)
512 || !pie_option)
513 {
514 /* If -fPIC/-fpic is given, merge it with -fPIE/-fpie. */
515 if (pic_option)
516 {
517 if (pic_option->opt_index == OPT_fpic
518 && (*decoded_options)[j].opt_index == OPT_fPIE)
519 {
520 (*decoded_options)[j].opt_index = OPT_fpie;
521 (*decoded_options)[j].canonical_option[0]
522 = pic_option->value ? "-fpie" : "-fno-pie";
523 }
524 else if (!pic_option->value)
525 (*decoded_options)[j].canonical_option[0] = "-fno-pie";
526 (*decoded_options)[j].value = pic_option->value;
527 j++;
528 }
529 else if (pie_option)
530 {
531 (*decoded_options)[j] = *pie_option;
532 j++;
533 }
534 /* Because we always append pic/PIE options this code path should
535 not happen unless the LTO object was built by old lto1 which
536 did not contain that logic yet. */
537 else
538 remove_option (decoded_options, j, decoded_options_count);
539 }
540 else if (pie_option->opt_index == OPT_fpie
541 && (*decoded_options)[j].opt_index == OPT_fPIE)
542 {
543 (*decoded_options)[j] = *pie_option;
544 j++;
545 }
546 else
547 j++;
548 }
549 else
550 j++;
551
552 if (!xassembler_options_error)
553 for (i = j = 0; ; i++, j++)
554 {
555 for (; i < *decoded_options_count; i++)
556 if ((*decoded_options)[i].opt_index == OPT_Xassembler)
557 break;
558
559 for (; j < fdecoded_options_count; j++)
560 if (fdecoded_options[j].opt_index == OPT_Xassembler)
561 break;
562
563 if (i == *decoded_options_count && j == fdecoded_options_count)
564 break;
565 else if (i < *decoded_options_count && j == fdecoded_options_count)
566 {
567 warning (0, "Extra option to %<-Xassembler%>: %s,"
568 " dropping all %<-Xassembler%> and %<-Wa%> options.",
569 (*decoded_options)[i].arg);
570 xassembler_options_error = true;
571 break;
572 }
573 else if (i == *decoded_options_count && j < fdecoded_options_count)
574 {
575 warning (0, "Extra option to %<-Xassembler%>: %s,"
576 " dropping all %<-Xassembler%> and %<-Wa%> options.",
577 fdecoded_options[j].arg);
578 xassembler_options_error = true;
579 break;
580 }
581 else if (strcmp ((*decoded_options)[i].arg, fdecoded_options[j].arg))
582 {
583 warning (0, "Options to %<-Xassembler%> do not match: %s, %s,"
584 " dropping all %<-Xassembler%> and %<-Wa%> options.",
585 (*decoded_options)[i].arg, fdecoded_options[j].arg);
586 xassembler_options_error = true;
587 break;
588 }
589 }
590 }
591
592 /* Auxiliary function that frees elements of PTR and PTR itself.
593 N is number of elements to be freed. If PTR is NULL, nothing is freed.
594 If an element is NULL, subsequent elements are not freed. */
595
596 static void **
597 free_array_of_ptrs (void **ptr, unsigned n)
598 {
599 if (!ptr)
600 return NULL;
601 for (unsigned i = 0; i < n; i++)
602 {
603 if (!ptr[i])
604 break;
605 free (ptr[i]);
606 }
607 free (ptr);
608 return NULL;
609 }
610
611 /* Parse STR, saving found tokens into PVALUES and return their number.
612 Tokens are assumed to be delimited by ':'. If APPEND is non-null,
613 append it to every token we find. */
614
615 static unsigned
616 parse_env_var (const char *str, char ***pvalues, const char *append)
617 {
618 const char *curval, *nextval;
619 char **values;
620 unsigned num = 1, i;
621
622 curval = strchr (str, ':');
623 while (curval)
624 {
625 num++;
626 curval = strchr (curval + 1, ':');
627 }
628
629 values = (char**) xmalloc (num * sizeof (char*));
630 curval = str;
631 nextval = strchr (curval, ':');
632 if (nextval == NULL)
633 nextval = strchr (curval, '\0');
634
635 int append_len = append ? strlen (append) : 0;
636 for (i = 0; i < num; i++)
637 {
638 int l = nextval - curval;
639 values[i] = (char*) xmalloc (l + 1 + append_len);
640 memcpy (values[i], curval, l);
641 values[i][l] = 0;
642 if (append)
643 strcat (values[i], append);
644 curval = nextval + 1;
645 nextval = strchr (curval, ':');
646 if (nextval == NULL)
647 nextval = strchr (curval, '\0');
648 }
649 *pvalues = values;
650 return num;
651 }
652
653 /* Append options OPTS from lto or offload_lto sections to ARGV_OBSTACK. */
654
655 static void
656 append_compiler_options (obstack *argv_obstack, struct cl_decoded_option *opts,
657 unsigned int count)
658 {
659 /* Append compiler driver arguments as far as they were merged. */
660 for (unsigned int j = 1; j < count; ++j)
661 {
662 struct cl_decoded_option *option = &opts[j];
663
664 /* File options have been properly filtered by lto-opts.c. */
665 switch (option->opt_index)
666 {
667 /* Drop arguments that we want to take from the link line. */
668 case OPT_flto_:
669 case OPT_flto:
670 case OPT_flto_partition_:
671 continue;
672
673 default:
674 break;
675 }
676
677 /* For now do what the original LTO option code was doing - pass
678 on any CL_TARGET flag and a few selected others. */
679 switch (option->opt_index)
680 {
681 case OPT_fdiagnostics_show_caret:
682 case OPT_fdiagnostics_show_labels:
683 case OPT_fdiagnostics_show_line_numbers:
684 case OPT_fdiagnostics_show_option:
685 case OPT_fdiagnostics_show_location_:
686 case OPT_fshow_column:
687 case OPT_fPIC:
688 case OPT_fpic:
689 case OPT_fPIE:
690 case OPT_fpie:
691 case OPT_fcommon:
692 case OPT_fgnu_tm:
693 case OPT_fopenmp:
694 case OPT_fopenacc:
695 case OPT_fopenacc_dim_:
696 case OPT_foffload_abi_:
697 case OPT_fcf_protection_:
698 case OPT_g:
699 case OPT_O:
700 case OPT_Ofast:
701 case OPT_Og:
702 case OPT_Os:
703 break;
704
705 case OPT_Xassembler:
706 /* When we detected a mismatch in assembler options between
707 the input TU's fall back to previous behavior of ignoring them. */
708 if (xassembler_options_error)
709 continue;
710 break;
711
712 default:
713 if (!(cl_options[option->opt_index].flags & CL_TARGET))
714 continue;
715 }
716
717 /* Pass the option on. */
718 for (unsigned int i = 0; i < option->canonical_option_num_elements; ++i)
719 obstack_ptr_grow (argv_obstack, option->canonical_option[i]);
720 }
721 }
722
723 /* Append diag options in OPTS with length COUNT to ARGV_OBSTACK. */
724
725 static void
726 append_diag_options (obstack *argv_obstack, struct cl_decoded_option *opts,
727 unsigned int count)
728 {
729 /* Append compiler driver arguments as far as they were merged. */
730 for (unsigned int j = 1; j < count; ++j)
731 {
732 struct cl_decoded_option *option = &opts[j];
733
734 switch (option->opt_index)
735 {
736 case OPT_fdiagnostics_color_:
737 case OPT_fdiagnostics_format_:
738 case OPT_fdiagnostics_show_caret:
739 case OPT_fdiagnostics_show_labels:
740 case OPT_fdiagnostics_show_line_numbers:
741 case OPT_fdiagnostics_show_option:
742 case OPT_fdiagnostics_show_location_:
743 case OPT_fshow_column:
744 break;
745 default:
746 continue;
747 }
748
749 /* Pass the option on. */
750 for (unsigned int i = 0; i < option->canonical_option_num_elements; ++i)
751 obstack_ptr_grow (argv_obstack, option->canonical_option[i]);
752 }
753 }
754
755
756 /* Append linker options OPTS to ARGV_OBSTACK. */
757
758 static void
759 append_linker_options (obstack *argv_obstack, struct cl_decoded_option *opts,
760 unsigned int count)
761 {
762 /* Append linker driver arguments. Compiler options from the linker
763 driver arguments will override / merge with those from the compiler. */
764 for (unsigned int j = 1; j < count; ++j)
765 {
766 struct cl_decoded_option *option = &opts[j];
767
768 /* Do not pass on frontend specific flags not suitable for lto. */
769 if (!(cl_options[option->opt_index].flags
770 & (CL_COMMON|CL_TARGET|CL_DRIVER|CL_LTO)))
771 continue;
772
773 switch (option->opt_index)
774 {
775 case OPT_o:
776 case OPT_flto_:
777 case OPT_flto:
778 /* We've handled these LTO options, do not pass them on. */
779 continue;
780
781 case OPT_fopenmp:
782 case OPT_fopenacc:
783 /* Ignore -fno-XXX form of these options, as otherwise
784 corresponding builtins will not be enabled. */
785 if (option->value == 0)
786 continue;
787 break;
788
789 default:
790 break;
791 }
792
793 /* Pass the option on. */
794 for (unsigned int i = 0; i < option->canonical_option_num_elements; ++i)
795 obstack_ptr_grow (argv_obstack, option->canonical_option[i]);
796 }
797 }
798
799 /* Extract options for TARGET offload compiler from OPTIONS and append
800 them to ARGV_OBSTACK. */
801
802 static void
803 append_offload_options (obstack *argv_obstack, const char *target,
804 struct cl_decoded_option *options,
805 unsigned int options_count)
806 {
807 for (unsigned i = 0; i < options_count; i++)
808 {
809 const char *cur, *next, *opts;
810 char **argv;
811 unsigned argc;
812 struct cl_decoded_option *option = &options[i];
813
814 if (option->opt_index != OPT_foffload_)
815 continue;
816
817 /* If option argument starts with '-' then no target is specified. That
818 means offload options are specified for all targets, so we need to
819 append them. */
820 if (option->arg[0] == '-')
821 opts = option->arg;
822 else
823 {
824 opts = strchr (option->arg, '=');
825 /* If there are offload targets specified, but no actual options,
826 there is nothing to do here. */
827 if (!opts)
828 continue;
829
830 cur = option->arg;
831
832 while (cur < opts)
833 {
834 next = strchr (cur, ',');
835 if (next == NULL)
836 next = opts;
837 next = (next > opts) ? opts : next;
838
839 /* Are we looking for this offload target? */
840 if (strlen (target) == (size_t) (next - cur)
841 && strncmp (target, cur, next - cur) == 0)
842 break;
843
844 /* Skip the comma or equal sign. */
845 cur = next + 1;
846 }
847
848 if (cur >= opts)
849 continue;
850
851 opts++;
852 }
853
854 argv = buildargv (opts);
855 for (argc = 0; argv[argc]; argc++)
856 obstack_ptr_grow (argv_obstack, argv[argc]);
857 }
858 }
859
860 /* Check whether NAME can be accessed in MODE. This is like access,
861 except that it never considers directories to be executable. */
862
863 static int
864 access_check (const char *name, int mode)
865 {
866 if (mode == X_OK)
867 {
868 struct stat st;
869
870 if (stat (name, &st) < 0
871 || S_ISDIR (st.st_mode))
872 return -1;
873 }
874
875 return access (name, mode);
876 }
877
878 /* Prepare a target image for offload TARGET, using mkoffload tool from
879 COMPILER_PATH. Return the name of the resultant object file. */
880
881 static char *
882 compile_offload_image (const char *target, const char *compiler_path,
883 unsigned in_argc, char *in_argv[],
884 struct cl_decoded_option *compiler_opts,
885 unsigned int compiler_opt_count,
886 struct cl_decoded_option *linker_opts,
887 unsigned int linker_opt_count)
888 {
889 char *filename = NULL;
890 char *dumpbase;
891 char **argv;
892 char *suffix
893 = XALLOCAVEC (char, sizeof ("/accel//mkoffload") + strlen (target));
894 strcpy (suffix, "/accel/");
895 strcat (suffix, target);
896 strcat (suffix, "/mkoffload");
897
898 char **paths = NULL;
899 unsigned n_paths = parse_env_var (compiler_path, &paths, suffix);
900
901 const char *compiler = NULL;
902 for (unsigned i = 0; i < n_paths; i++)
903 if (access_check (paths[i], X_OK) == 0)
904 {
905 compiler = paths[i];
906 break;
907 }
908
909 if (!compiler)
910 fatal_error (input_location,
911 "could not find %s in %s (consider using %<-B%>)",
912 suffix + 1, compiler_path);
913
914 dumpbase = concat (dumppfx, "x", target, NULL);
915
916 /* Generate temporary output file name. */
917 if (save_temps)
918 filename = concat (dumpbase, ".o", NULL);
919 else
920 filename = make_temp_file (".target.o");
921
922 struct obstack argv_obstack;
923 obstack_init (&argv_obstack);
924 obstack_ptr_grow (&argv_obstack, compiler);
925 if (save_temps)
926 obstack_ptr_grow (&argv_obstack, "-save-temps");
927 if (verbose)
928 obstack_ptr_grow (&argv_obstack, "-v");
929 obstack_ptr_grow (&argv_obstack, "-o");
930 obstack_ptr_grow (&argv_obstack, filename);
931
932 /* Append names of input object files. */
933 for (unsigned i = 0; i < in_argc; i++)
934 obstack_ptr_grow (&argv_obstack, in_argv[i]);
935
936 /* Append options from offload_lto sections. */
937 append_compiler_options (&argv_obstack, compiler_opts,
938 compiler_opt_count);
939 append_diag_options (&argv_obstack, linker_opts, linker_opt_count);
940
941 obstack_ptr_grow (&argv_obstack, "-dumpbase");
942 obstack_ptr_grow (&argv_obstack, dumpbase);
943
944 /* Append options specified by -foffload last. In case of conflicting
945 options we expect offload compiler to choose the latest. */
946 append_offload_options (&argv_obstack, target, compiler_opts,
947 compiler_opt_count);
948 append_offload_options (&argv_obstack, target, linker_opts,
949 linker_opt_count);
950
951 obstack_ptr_grow (&argv_obstack, NULL);
952 argv = XOBFINISH (&argv_obstack, char **);
953 fork_execute (argv[0], argv, true);
954 obstack_free (&argv_obstack, NULL);
955
956 free_array_of_ptrs ((void **) paths, n_paths);
957 return filename;
958 }
959
960
961 /* The main routine dealing with offloading.
962 The routine builds a target image for each offload target. IN_ARGC and
963 IN_ARGV specify options and input object files. As all of them could contain
964 target sections, we pass them all to target compilers. */
965
966 static void
967 compile_images_for_offload_targets (unsigned in_argc, char *in_argv[],
968 struct cl_decoded_option *compiler_opts,
969 unsigned int compiler_opt_count,
970 struct cl_decoded_option *linker_opts,
971 unsigned int linker_opt_count)
972 {
973 char **names = NULL;
974 const char *target_names = getenv (OFFLOAD_TARGET_NAMES_ENV);
975 if (!target_names)
976 return;
977 unsigned num_targets = parse_env_var (target_names, &names, NULL);
978
979 const char *compiler_path = getenv ("COMPILER_PATH");
980 if (!compiler_path)
981 goto out;
982
983 /* Prepare an image for each target and save the name of the resultant object
984 file to the OFFLOAD_NAMES array. It is terminated by a NULL entry. */
985 offload_names = XCNEWVEC (char *, num_targets + 1);
986 for (unsigned i = 0; i < num_targets; i++)
987 {
988 offload_names[i]
989 = compile_offload_image (names[i], compiler_path, in_argc, in_argv,
990 compiler_opts, compiler_opt_count,
991 linker_opts, linker_opt_count);
992 if (!offload_names[i])
993 fatal_error (input_location,
994 "problem with building target image for %s", names[i]);
995 }
996
997 out:
998 free_array_of_ptrs ((void **) names, num_targets);
999 }
1000
1001 /* Copy a file from SRC to DEST. */
1002
1003 static void
1004 copy_file (const char *dest, const char *src)
1005 {
1006 FILE *d = fopen (dest, "wb");
1007 FILE *s = fopen (src, "rb");
1008 char buffer[512];
1009 while (!feof (s))
1010 {
1011 size_t len = fread (buffer, 1, 512, s);
1012 if (ferror (s) != 0)
1013 fatal_error (input_location, "reading input file");
1014 if (len > 0)
1015 {
1016 fwrite (buffer, 1, len, d);
1017 if (ferror (d) != 0)
1018 fatal_error (input_location, "writing output file");
1019 }
1020 }
1021 fclose (d);
1022 fclose (s);
1023 }
1024
1025 /* Find the crtoffloadtable.o file in LIBRARY_PATH, make copy and pass name of
1026 the copy to the linker. */
1027
1028 static void
1029 find_crtoffloadtable (int save_temps, const char *dumppfx)
1030 {
1031 char **paths = NULL;
1032 const char *library_path = getenv ("LIBRARY_PATH");
1033 if (!library_path)
1034 return;
1035 unsigned n_paths = parse_env_var (library_path, &paths, "/crtoffloadtable.o");
1036
1037 unsigned i;
1038 for (i = 0; i < n_paths; i++)
1039 if (access_check (paths[i], R_OK) == 0)
1040 {
1041 /* The linker will delete the filename we give it, so make a copy. */
1042 char *crtoffloadtable;
1043 if (!save_temps)
1044 crtoffloadtable = make_temp_file (".crtoffloadtable.o");
1045 else
1046 crtoffloadtable = concat (dumppfx, "crtoffloadtable.o");
1047 copy_file (crtoffloadtable, paths[i]);
1048 printf ("%s\n", crtoffloadtable);
1049 XDELETEVEC (crtoffloadtable);
1050 break;
1051 }
1052 if (i == n_paths)
1053 fatal_error (input_location,
1054 "installation error, cannot find %<crtoffloadtable.o%>");
1055
1056 free_array_of_ptrs ((void **) paths, n_paths);
1057 }
1058
1059 /* A subroutine of run_gcc. Examine the open file FD for lto sections with
1060 name prefix PREFIX, at FILE_OFFSET, and store any options we find in OPTS
1061 and OPT_COUNT. Return true if we found a matching section, false
1062 otherwise. COLLECT_GCC holds the value of the environment variable with
1063 the same name. */
1064
1065 static bool
1066 find_and_merge_options (int fd, off_t file_offset, const char *prefix,
1067 struct cl_decoded_option *decoded_cl_options,
1068 unsigned int decoded_cl_options_count,
1069 struct cl_decoded_option **opts,
1070 unsigned int *opt_count, const char *collect_gcc)
1071 {
1072 off_t offset, length;
1073 char *data;
1074 char *fopts;
1075 const char *errmsg;
1076 int err;
1077 struct cl_decoded_option *fdecoded_options = *opts;
1078 unsigned int fdecoded_options_count = *opt_count;
1079
1080 simple_object_read *sobj;
1081 sobj = simple_object_start_read (fd, file_offset, "__GNU_LTO",
1082 &errmsg, &err);
1083 if (!sobj)
1084 return false;
1085
1086 char *secname = XALLOCAVEC (char, strlen (prefix) + sizeof (".opts"));
1087 strcpy (secname, prefix);
1088 strcat (secname, ".opts");
1089 if (!simple_object_find_section (sobj, secname, &offset, &length,
1090 &errmsg, &err))
1091 {
1092 simple_object_release_read (sobj);
1093 return false;
1094 }
1095
1096 lseek (fd, file_offset + offset, SEEK_SET);
1097 data = (char *)xmalloc (length);
1098 read (fd, data, length);
1099 fopts = data;
1100 do
1101 {
1102 struct cl_decoded_option *f2decoded_options;
1103 unsigned int f2decoded_options_count;
1104 get_options_from_collect_gcc_options (collect_gcc, fopts,
1105 &f2decoded_options,
1106 &f2decoded_options_count);
1107 if (!fdecoded_options)
1108 {
1109 fdecoded_options = f2decoded_options;
1110 fdecoded_options_count = f2decoded_options_count;
1111 }
1112 else
1113 merge_and_complain (&fdecoded_options,
1114 &fdecoded_options_count,
1115 f2decoded_options, f2decoded_options_count,
1116 decoded_cl_options,
1117 decoded_cl_options_count);
1118
1119 fopts += strlen (fopts) + 1;
1120 }
1121 while (fopts - data < length);
1122
1123 free (data);
1124 simple_object_release_read (sobj);
1125 *opts = fdecoded_options;
1126 *opt_count = fdecoded_options_count;
1127 return true;
1128 }
1129
1130 /* Copy early debug info sections from INFILE to a new file whose name
1131 is returned. Return NULL on error. */
1132
1133 const char *
1134 debug_objcopy (const char *infile, bool rename)
1135 {
1136 char *outfile;
1137 const char *errmsg;
1138 int err;
1139
1140 const char *p;
1141 const char *orig_infile = infile;
1142 off_t inoff = 0;
1143 long loffset;
1144 int consumed;
1145 if ((p = strrchr (infile, '@'))
1146 && p != infile
1147 && sscanf (p, "@%li%n", &loffset, &consumed) >= 1
1148 && strlen (p) == (unsigned int) consumed)
1149 {
1150 char *fname = xstrdup (infile);
1151 fname[p - infile] = '\0';
1152 infile = fname;
1153 inoff = (off_t) loffset;
1154 }
1155 int infd = open (infile, O_RDONLY | O_BINARY);
1156 if (infd == -1)
1157 return NULL;
1158 simple_object_read *inobj = simple_object_start_read (infd, inoff,
1159 "__GNU_LTO",
1160 &errmsg, &err);
1161 if (!inobj)
1162 return NULL;
1163
1164 off_t off, len;
1165 if (simple_object_find_section (inobj, ".gnu.debuglto_.debug_info",
1166 &off, &len, &errmsg, &err) != 1)
1167 {
1168 if (errmsg)
1169 fatal_error (0, "%s: %s", errmsg, xstrerror (err));
1170
1171 simple_object_release_read (inobj);
1172 close (infd);
1173 return NULL;
1174 }
1175
1176 if (save_temps)
1177 outfile = concat (orig_infile, ".debug.temp.o", NULL);
1178 else
1179 outfile = make_temp_file (".debug.temp.o");
1180 errmsg = simple_object_copy_lto_debug_sections (inobj, outfile, &err, rename);
1181 if (errmsg)
1182 {
1183 unlink_if_ordinary (outfile);
1184 fatal_error (0, "%s: %s", errmsg, xstrerror (err));
1185 }
1186
1187 simple_object_release_read (inobj);
1188 close (infd);
1189
1190 return outfile;
1191 }
1192
1193 /* Helper for qsort: compare priorities for parallel compilation. */
1194
1195 int
1196 cmp_priority (const void *a, const void *b)
1197 {
1198 return *((const int *)b)-*((const int *)a);
1199 }
1200
1201 /* Number of CPUs that can be used for parallel LTRANS phase. */
1202
1203 static unsigned long nthreads_var = 0;
1204
1205 #ifdef HAVE_PTHREAD_AFFINITY_NP
1206 unsigned long cpuset_size;
1207 static unsigned long get_cpuset_size;
1208 cpu_set_t *cpusetp;
1209
1210 unsigned long
1211 static cpuset_popcount (unsigned long cpusetsize, cpu_set_t *cpusetp)
1212 {
1213 #ifdef CPU_COUNT_S
1214 /* glibc 2.7 and above provide a macro for this. */
1215 return CPU_COUNT_S (cpusetsize, cpusetp);
1216 #else
1217 #ifdef CPU_COUNT
1218 if (cpusetsize == sizeof (cpu_set_t))
1219 /* glibc 2.6 and above provide a macro for this. */
1220 return CPU_COUNT (cpusetp);
1221 #endif
1222 size_t i;
1223 unsigned long ret = 0;
1224 STATIC_ASSERT (sizeof (cpusetp->__bits[0]) == sizeof (unsigned long int));
1225 for (i = 0; i < cpusetsize / sizeof (cpusetp->__bits[0]); i++)
1226 {
1227 unsigned long int mask = cpusetp->__bits[i];
1228 if (mask == 0)
1229 continue;
1230 ret += __builtin_popcountl (mask);
1231 }
1232 return ret;
1233 #endif
1234 }
1235 #endif
1236
1237 /* At startup, determine the default number of threads. It would seem
1238 this should be related to the number of cpus online. */
1239
1240 static void
1241 init_num_threads (void)
1242 {
1243 #ifdef HAVE_PTHREAD_AFFINITY_NP
1244 #if defined (_SC_NPROCESSORS_CONF) && defined (CPU_ALLOC_SIZE)
1245 cpuset_size = sysconf (_SC_NPROCESSORS_CONF);
1246 cpuset_size = CPU_ALLOC_SIZE (cpuset_size);
1247 #else
1248 cpuset_size = sizeof (cpu_set_t);
1249 #endif
1250
1251 cpusetp = (cpu_set_t *) xmalloc (gomp_cpuset_size);
1252 do
1253 {
1254 int ret = pthread_getaffinity_np (pthread_self (), gomp_cpuset_size,
1255 cpusetp);
1256 if (ret == 0)
1257 {
1258 /* Count only the CPUs this process can use. */
1259 nthreads_var = cpuset_popcount (cpuset_size, cpusetp);
1260 if (nthreads_var == 0)
1261 break;
1262 get_cpuset_size = cpuset_size;
1263 #ifdef CPU_ALLOC_SIZE
1264 unsigned long i;
1265 for (i = cpuset_size * 8; i; i--)
1266 if (CPU_ISSET_S (i - 1, cpuset_size, cpusetp))
1267 break;
1268 cpuset_size = CPU_ALLOC_SIZE (i);
1269 #endif
1270 return;
1271 }
1272 if (ret != EINVAL)
1273 break;
1274 #ifdef CPU_ALLOC_SIZE
1275 if (cpuset_size < sizeof (cpu_set_t))
1276 cpuset_size = sizeof (cpu_set_t);
1277 else
1278 cpuset_size = cpuset_size * 2;
1279 if (cpuset_size < 8 * sizeof (cpu_set_t))
1280 cpusetp
1281 = (cpu_set_t *) realloc (cpusetp, cpuset_size);
1282 else
1283 {
1284 /* Avoid fatal if too large memory allocation would be
1285 requested, e.g. kernel returning EINVAL all the time. */
1286 void *p = realloc (cpusetp, cpuset_size);
1287 if (p == NULL)
1288 break;
1289 cpusetp = (cpu_set_t *) p;
1290 }
1291 #else
1292 break;
1293 #endif
1294 }
1295 while (1);
1296 cpuset_size = 0;
1297 nthreads_var = 1;
1298 free (cpusetp);
1299 cpusetp = NULL;
1300 #endif
1301 #ifdef _SC_NPROCESSORS_ONLN
1302 nthreads_var = sysconf (_SC_NPROCESSORS_ONLN);
1303 #endif
1304 }
1305
1306 /* FIXME: once using -std=c++11, we can use std::thread::hardware_concurrency. */
1307
1308 /* Test and return reason why a jobserver cannot be detected. */
1309
1310 static const char *
1311 jobserver_active_p (void)
1312 {
1313 #define JS_PREFIX "jobserver is not available: "
1314 #define JS_NEEDLE "--jobserver-auth="
1315
1316 const char *makeflags = getenv ("MAKEFLAGS");
1317 if (makeflags == NULL)
1318 return JS_PREFIX "%<MAKEFLAGS%> environment variable is unset";
1319
1320 const char *n = strstr (makeflags, JS_NEEDLE);
1321 if (n == NULL)
1322 return JS_PREFIX "%<" JS_NEEDLE "%> is not present in %<MAKEFLAGS%>";
1323
1324 int rfd = -1;
1325 int wfd = -1;
1326
1327 if (sscanf (n + strlen (JS_NEEDLE), "%d,%d", &rfd, &wfd) == 2
1328 && rfd > 0
1329 && wfd > 0
1330 && is_valid_fd (rfd)
1331 && is_valid_fd (wfd))
1332 return NULL;
1333 else
1334 return JS_PREFIX "cannot access %<" JS_NEEDLE "%> file descriptors";
1335 }
1336
1337 /* Execute gcc. ARGC is the number of arguments. ARGV contains the arguments. */
1338
1339 static void
1340 run_gcc (unsigned argc, char *argv[])
1341 {
1342 unsigned i, j;
1343 const char **new_argv;
1344 const char **argv_ptr;
1345 char *list_option_full = NULL;
1346 const char *linker_output = NULL;
1347 const char *collect_gcc;
1348 char *collect_gcc_options;
1349 int parallel = 0;
1350 int jobserver = 0;
1351 int auto_parallel = 0;
1352 bool no_partition = false;
1353 struct cl_decoded_option *fdecoded_options = NULL;
1354 struct cl_decoded_option *offload_fdecoded_options = NULL;
1355 unsigned int fdecoded_options_count = 0;
1356 unsigned int offload_fdecoded_options_count = 0;
1357 struct cl_decoded_option *decoded_options;
1358 unsigned int decoded_options_count;
1359 struct obstack argv_obstack;
1360 int new_head_argc;
1361 bool have_lto = false;
1362 bool have_offload = false;
1363 unsigned lto_argc = 0, ltoobj_argc = 0;
1364 char **lto_argv, **ltoobj_argv;
1365 bool linker_output_rel = false;
1366 bool skip_debug = false;
1367 unsigned n_debugobj;
1368 const char *incoming_dumppfx = dumppfx = NULL;
1369 static char current_dir[] = { '.', DIR_SEPARATOR, '\0' };
1370
1371 /* Get the driver and options. */
1372 collect_gcc = getenv ("COLLECT_GCC");
1373 if (!collect_gcc)
1374 fatal_error (input_location,
1375 "environment variable %<COLLECT_GCC%> must be set");
1376 collect_gcc_options = getenv ("COLLECT_GCC_OPTIONS");
1377 if (!collect_gcc_options)
1378 fatal_error (input_location,
1379 "environment variable %<COLLECT_GCC_OPTIONS%> must be set");
1380
1381 char *collect_as_options = getenv ("COLLECT_AS_OPTIONS");
1382
1383 /* Prepend -Xassembler to each option, and append the string
1384 to collect_gcc_options. */
1385 if (collect_as_options)
1386 {
1387 obstack temporary_obstack;
1388 obstack_init (&temporary_obstack);
1389
1390 prepend_xassembler_to_collect_as_options (collect_as_options,
1391 &temporary_obstack);
1392 obstack_1grow (&temporary_obstack, '\0');
1393
1394 char *xassembler_opts_string
1395 = XOBFINISH (&temporary_obstack, char *);
1396 collect_gcc_options = concat (collect_gcc_options, xassembler_opts_string,
1397 NULL);
1398 }
1399
1400 get_options_from_collect_gcc_options (collect_gcc, collect_gcc_options,
1401 &decoded_options,
1402 &decoded_options_count);
1403
1404 /* Allocate array for input object files with LTO IL,
1405 and for possible preceding arguments. */
1406 lto_argv = XNEWVEC (char *, argc);
1407 ltoobj_argv = XNEWVEC (char *, argc);
1408
1409 /* Look at saved options in the IL files. */
1410 for (i = 1; i < argc; ++i)
1411 {
1412 char *p;
1413 int fd;
1414 off_t file_offset = 0;
1415 long loffset;
1416 int consumed;
1417 char *filename = argv[i];
1418
1419 if (strncmp (argv[i], "-foffload-objects=",
1420 sizeof ("-foffload-objects=") - 1) == 0)
1421 {
1422 have_offload = true;
1423 offload_objects_file_name
1424 = argv[i] + sizeof ("-foffload-objects=") - 1;
1425 continue;
1426 }
1427
1428 if ((p = strrchr (argv[i], '@'))
1429 && p != argv[i]
1430 && sscanf (p, "@%li%n", &loffset, &consumed) >= 1
1431 && strlen (p) == (unsigned int) consumed)
1432 {
1433 filename = XNEWVEC (char, p - argv[i] + 1);
1434 memcpy (filename, argv[i], p - argv[i]);
1435 filename[p - argv[i]] = '\0';
1436 file_offset = (off_t) loffset;
1437 }
1438 fd = open (filename, O_RDONLY | O_BINARY);
1439 /* Linker plugin passes -fresolution and -flinker-output options.
1440 -flinker-output is passed only when user did not specify one and thus
1441 we do not need to worry about duplicities with the option handling
1442 below. */
1443 if (fd == -1)
1444 {
1445 lto_argv[lto_argc++] = argv[i];
1446 if (strcmp (argv[i], "-flinker-output=rel") == 0)
1447 linker_output_rel = true;
1448 continue;
1449 }
1450
1451 if (find_and_merge_options (fd, file_offset, LTO_SECTION_NAME_PREFIX,
1452 decoded_options, decoded_options_count,
1453 &fdecoded_options, &fdecoded_options_count,
1454 collect_gcc))
1455 {
1456 have_lto = true;
1457 ltoobj_argv[ltoobj_argc++] = argv[i];
1458 }
1459 close (fd);
1460 }
1461
1462 /* Initalize the common arguments for the driver. */
1463 obstack_init (&argv_obstack);
1464 obstack_ptr_grow (&argv_obstack, collect_gcc);
1465 obstack_ptr_grow (&argv_obstack, "-xlto");
1466 obstack_ptr_grow (&argv_obstack, "-c");
1467
1468 append_compiler_options (&argv_obstack, fdecoded_options,
1469 fdecoded_options_count);
1470 append_linker_options (&argv_obstack, decoded_options, decoded_options_count);
1471
1472 /* Scan linker driver arguments for things that are of relevance to us. */
1473 for (j = 1; j < decoded_options_count; ++j)
1474 {
1475 struct cl_decoded_option *option = &decoded_options[j];
1476 switch (option->opt_index)
1477 {
1478 case OPT_o:
1479 linker_output = option->arg;
1480 break;
1481
1482 /* We don't have to distinguish between -save-temps=* and
1483 -save-temps, -dumpdir already carries that
1484 information. */
1485 case OPT_save_temps_:
1486 case OPT_save_temps:
1487 save_temps = 1;
1488 break;
1489
1490 case OPT_v:
1491 verbose = 1;
1492 break;
1493
1494 case OPT_flto_partition_:
1495 if (strcmp (option->arg, "none") == 0)
1496 no_partition = true;
1497 break;
1498
1499 case OPT_flto_:
1500 if (strcmp (option->arg, "jobserver") == 0)
1501 {
1502 parallel = 1;
1503 jobserver = 1;
1504 }
1505 else if (strcmp (option->arg, "auto") == 0)
1506 {
1507 parallel = 1;
1508 auto_parallel = 1;
1509 }
1510 else
1511 {
1512 parallel = atoi (option->arg);
1513 if (parallel <= 1)
1514 parallel = 0;
1515 }
1516 /* Fallthru. */
1517
1518 case OPT_flto:
1519 lto_mode = LTO_MODE_WHOPR;
1520 break;
1521
1522 case OPT_flinker_output_:
1523 linker_output_rel = !strcmp (option->arg, "rel");
1524 break;
1525
1526 case OPT_g:
1527 /* Recognize -g0. */
1528 skip_debug = option->arg && !strcmp (option->arg, "0");
1529 break;
1530
1531 case OPT_dumpdir:
1532 incoming_dumppfx = dumppfx = option->arg;
1533 break;
1534
1535 default:
1536 break;
1537 }
1538 }
1539
1540 /* Output lto-wrapper invocation command. */
1541 if (verbose)
1542 {
1543 for (i = 0; i < argc; ++i)
1544 {
1545 fputs (argv[i], stderr);
1546 fputc (' ', stderr);
1547 }
1548 fputc ('\n', stderr);
1549 }
1550
1551 if (linker_output_rel)
1552 no_partition = true;
1553
1554 if (no_partition)
1555 {
1556 lto_mode = LTO_MODE_LTO;
1557 jobserver = 0;
1558 auto_parallel = 0;
1559 parallel = 0;
1560 }
1561 else
1562 {
1563 const char *jobserver_error = jobserver_active_p ();
1564 if (jobserver && jobserver_error != NULL)
1565 warning (0, jobserver_error);
1566 else if (!jobserver && jobserver_error == NULL)
1567 {
1568 parallel = 1;
1569 jobserver = 1;
1570 }
1571 }
1572
1573 if (!dumppfx)
1574 {
1575 if (!linker_output
1576 || strcmp (linker_output, HOST_BIT_BUCKET) == 0)
1577 dumppfx = "a.";
1578 else
1579 {
1580 const char *obase = lbasename (linker_output), *temp;
1581
1582 /* Strip the executable extension. */
1583 size_t blen = strlen (obase), xlen;
1584 if ((temp = strrchr (obase + 1, '.'))
1585 && (xlen = strlen (temp))
1586 && (strcmp (temp, ".exe") == 0
1587 #if defined(HAVE_TARGET_EXECUTABLE_SUFFIX)
1588 || strcmp (temp, TARGET_EXECUTABLE_SUFFIX) == 0
1589 #endif
1590 || strcmp (obase, "a.out") == 0))
1591 dumppfx = xstrndup (linker_output,
1592 obase - linker_output + blen - xlen + 1);
1593 else
1594 dumppfx = concat (linker_output, ".", NULL);
1595 }
1596 }
1597
1598 /* If there's no directory component in the dumppfx, add one, so
1599 that, when it is used as -dumpbase, it overrides any occurrence
1600 of -dumpdir that might have been passed in. */
1601 if (!dumppfx || lbasename (dumppfx) == dumppfx)
1602 dumppfx = concat (current_dir, dumppfx, NULL);
1603
1604 /* Make sure some -dumpdir is passed, so as to get predictable
1605 -dumpbase overriding semantics. If we got an incoming -dumpdir
1606 argument, we'll pass it on, so don't bother with another one
1607 then. */
1608 if (!incoming_dumppfx)
1609 {
1610 obstack_ptr_grow (&argv_obstack, "-dumpdir");
1611 obstack_ptr_grow (&argv_obstack, "");
1612 }
1613 obstack_ptr_grow (&argv_obstack, "-dumpbase");
1614
1615 /* Remember at which point we can scrub args to re-use the commons. */
1616 new_head_argc = obstack_object_size (&argv_obstack) / sizeof (void *);
1617
1618 if (have_offload)
1619 {
1620 unsigned i, num_offload_files;
1621 char **offload_argv;
1622 FILE *f;
1623
1624 f = fopen (offload_objects_file_name, "r");
1625 if (f == NULL)
1626 fatal_error (input_location, "cannot open %s: %m",
1627 offload_objects_file_name);
1628 if (fscanf (f, "%u ", &num_offload_files) != 1)
1629 fatal_error (input_location, "cannot read %s: %m",
1630 offload_objects_file_name);
1631 offload_argv = XCNEWVEC (char *, num_offload_files);
1632
1633 /* Read names of object files with offload. */
1634 for (i = 0; i < num_offload_files; i++)
1635 {
1636 const unsigned piece = 32;
1637 char *buf, *filename = XNEWVEC (char, piece);
1638 size_t len;
1639
1640 buf = filename;
1641 cont1:
1642 if (!fgets (buf, piece, f))
1643 break;
1644 len = strlen (filename);
1645 if (filename[len - 1] != '\n')
1646 {
1647 filename = XRESIZEVEC (char, filename, len + piece);
1648 buf = filename + len;
1649 goto cont1;
1650 }
1651 filename[len - 1] = '\0';
1652 offload_argv[i] = filename;
1653 }
1654 fclose (f);
1655 if (offload_argv[num_offload_files - 1] == NULL)
1656 fatal_error (input_location, "invalid format of %s",
1657 offload_objects_file_name);
1658 maybe_unlink (offload_objects_file_name);
1659 offload_objects_file_name = NULL;
1660
1661 /* Look at saved offload options in files. */
1662 for (i = 0; i < num_offload_files; i++)
1663 {
1664 char *p;
1665 long loffset;
1666 int fd, consumed;
1667 off_t file_offset = 0;
1668 char *filename = offload_argv[i];
1669
1670 if ((p = strrchr (offload_argv[i], '@'))
1671 && p != offload_argv[i]
1672 && sscanf (p, "@%li%n", &loffset, &consumed) >= 1
1673 && strlen (p) == (unsigned int) consumed)
1674 {
1675 filename = XNEWVEC (char, p - offload_argv[i] + 1);
1676 memcpy (filename, offload_argv[i], p - offload_argv[i]);
1677 filename[p - offload_argv[i]] = '\0';
1678 file_offset = (off_t) loffset;
1679 }
1680 fd = open (filename, O_RDONLY | O_BINARY);
1681 if (fd == -1)
1682 fatal_error (input_location, "cannot open %s: %m", filename);
1683 if (!find_and_merge_options (fd, file_offset,
1684 OFFLOAD_SECTION_NAME_PREFIX,
1685 decoded_options, decoded_options_count,
1686 &offload_fdecoded_options,
1687 &offload_fdecoded_options_count,
1688 collect_gcc))
1689 fatal_error (input_location, "cannot read %s: %m", filename);
1690 close (fd);
1691 if (filename != offload_argv[i])
1692 XDELETEVEC (filename);
1693 }
1694
1695 compile_images_for_offload_targets (num_offload_files, offload_argv,
1696 offload_fdecoded_options,
1697 offload_fdecoded_options_count,
1698 decoded_options,
1699 decoded_options_count);
1700
1701 free_array_of_ptrs ((void **) offload_argv, num_offload_files);
1702
1703 if (offload_names)
1704 {
1705 find_crtoffloadtable (save_temps, dumppfx);
1706 for (i = 0; offload_names[i]; i++)
1707 printf ("%s\n", offload_names[i]);
1708 free_array_of_ptrs ((void **) offload_names, i);
1709 }
1710 }
1711
1712 /* If object files contain offload sections, but do not contain LTO sections,
1713 then there is no need to perform a link-time recompilation, i.e.
1714 lto-wrapper is used only for a compilation of offload images. */
1715 if (have_offload && !have_lto)
1716 goto finish;
1717
1718 if (lto_mode == LTO_MODE_LTO)
1719 {
1720 /* -dumpbase argument for LTO. */
1721 flto_out = concat (dumppfx, "lto.o", NULL);
1722 obstack_ptr_grow (&argv_obstack, flto_out);
1723
1724 if (!save_temps)
1725 flto_out = make_temp_file (".lto.o");
1726 obstack_ptr_grow (&argv_obstack, "-o");
1727 obstack_ptr_grow (&argv_obstack, flto_out);
1728 }
1729 else
1730 {
1731 const char *list_option = "-fltrans-output-list=";
1732
1733 /* -dumpbase argument for WPA. */
1734 char *dumpbase = concat (dumppfx, "wpa", NULL);
1735 obstack_ptr_grow (&argv_obstack, dumpbase);
1736
1737 if (save_temps)
1738 ltrans_output_file = concat (dumppfx, "ltrans.out", NULL);
1739 else
1740 ltrans_output_file = make_temp_file (".ltrans.out");
1741 list_option_full = concat (list_option, ltrans_output_file, NULL);
1742 obstack_ptr_grow (&argv_obstack, list_option_full);
1743
1744 if (jobserver)
1745 {
1746 if (verbose)
1747 fprintf (stderr, "Using make jobserver\n");
1748 obstack_ptr_grow (&argv_obstack, xstrdup ("-fwpa=jobserver"));
1749 }
1750 else if (auto_parallel)
1751 {
1752 char buf[256];
1753 init_num_threads ();
1754 if (verbose)
1755 fprintf (stderr, "LTO parallelism level set to %ld\n",
1756 nthreads_var);
1757 sprintf (buf, "-fwpa=%ld", nthreads_var);
1758 obstack_ptr_grow (&argv_obstack, xstrdup (buf));
1759 }
1760 else if (parallel > 1)
1761 {
1762 char buf[256];
1763 sprintf (buf, "-fwpa=%i", parallel);
1764 obstack_ptr_grow (&argv_obstack, xstrdup (buf));
1765 }
1766 else
1767 obstack_ptr_grow (&argv_obstack, "-fwpa");
1768 }
1769
1770 /* Append input arguments. */
1771 for (i = 0; i < lto_argc; ++i)
1772 obstack_ptr_grow (&argv_obstack, lto_argv[i]);
1773 /* Append the input objects. */
1774 for (i = 0; i < ltoobj_argc; ++i)
1775 obstack_ptr_grow (&argv_obstack, ltoobj_argv[i]);
1776 obstack_ptr_grow (&argv_obstack, NULL);
1777
1778 new_argv = XOBFINISH (&argv_obstack, const char **);
1779 argv_ptr = &new_argv[new_head_argc];
1780 fork_execute (new_argv[0], CONST_CAST (char **, new_argv), true);
1781
1782 /* Copy the early generated debug info from the objects to temporary
1783 files and append those to the partial link commandline. */
1784 n_debugobj = 0;
1785 early_debug_object_names = NULL;
1786 if (! skip_debug)
1787 {
1788 early_debug_object_names = XCNEWVEC (const char *, ltoobj_argc+ 1);
1789 num_deb_objs = ltoobj_argc;
1790 for (i = 0; i < ltoobj_argc; ++i)
1791 {
1792 const char *tem;
1793 if ((tem = debug_objcopy (ltoobj_argv[i], !linker_output_rel)))
1794 {
1795 early_debug_object_names[i] = tem;
1796 n_debugobj++;
1797 }
1798 }
1799 }
1800
1801 if (lto_mode == LTO_MODE_LTO)
1802 {
1803 printf ("%s\n", flto_out);
1804 if (!skip_debug)
1805 {
1806 for (i = 0; i < ltoobj_argc; ++i)
1807 if (early_debug_object_names[i] != NULL)
1808 printf ("%s\n", early_debug_object_names[i]);
1809 }
1810 /* These now belong to collect2. */
1811 free (flto_out);
1812 flto_out = NULL;
1813 free (early_debug_object_names);
1814 early_debug_object_names = NULL;
1815 }
1816 else
1817 {
1818 FILE *stream = fopen (ltrans_output_file, "r");
1819 FILE *mstream = NULL;
1820 struct obstack env_obstack;
1821 int priority;
1822
1823 if (!stream)
1824 fatal_error (input_location, "%<fopen%>: %s: %m", ltrans_output_file);
1825
1826 /* Parse the list of LTRANS inputs from the WPA stage. */
1827 obstack_init (&env_obstack);
1828 nr = 0;
1829 for (;;)
1830 {
1831 const unsigned piece = 32;
1832 char *output_name = NULL;
1833 char *buf, *input_name = (char *)xmalloc (piece);
1834 size_t len;
1835
1836 buf = input_name;
1837 if (fscanf (stream, "%i\n", &priority) != 1)
1838 {
1839 if (!feof (stream))
1840 fatal_error (input_location,
1841 "corrupted ltrans output file %s",
1842 ltrans_output_file);
1843 break;
1844 }
1845 cont:
1846 if (!fgets (buf, piece, stream))
1847 break;
1848 len = strlen (input_name);
1849 if (input_name[len - 1] != '\n')
1850 {
1851 input_name = (char *)xrealloc (input_name, len + piece);
1852 buf = input_name + len;
1853 goto cont;
1854 }
1855 input_name[len - 1] = '\0';
1856
1857 if (input_name[0] == '*')
1858 output_name = &input_name[1];
1859
1860 nr++;
1861 ltrans_priorities
1862 = (int *)xrealloc (ltrans_priorities, nr * sizeof (int) * 2);
1863 input_names = (char **)xrealloc (input_names, nr * sizeof (char *));
1864 output_names = (char **)xrealloc (output_names, nr * sizeof (char *));
1865 ltrans_priorities[(nr-1)*2] = priority;
1866 ltrans_priorities[(nr-1)*2+1] = nr-1;
1867 input_names[nr-1] = input_name;
1868 output_names[nr-1] = output_name;
1869 }
1870 fclose (stream);
1871 maybe_unlink (ltrans_output_file);
1872 ltrans_output_file = NULL;
1873
1874 if (parallel)
1875 {
1876 makefile = make_temp_file (".mk");
1877 mstream = fopen (makefile, "w");
1878 qsort (ltrans_priorities, nr, sizeof (int) * 2, cmp_priority);
1879 }
1880
1881 /* Execute the LTRANS stage for each input file (or prepare a
1882 makefile to invoke this in parallel). */
1883 for (i = 0; i < nr; ++i)
1884 {
1885 char *output_name;
1886 char *input_name = input_names[i];
1887 /* If it's a pass-through file do nothing. */
1888 if (output_names[i])
1889 continue;
1890
1891 /* Replace the .o suffix with a .ltrans.o suffix and write
1892 the resulting name to the LTRANS output list. */
1893 obstack_grow (&env_obstack, input_name, strlen (input_name) - 2);
1894 obstack_grow (&env_obstack, ".ltrans.o", sizeof (".ltrans.o"));
1895 output_name = XOBFINISH (&env_obstack, char *);
1896
1897 /* Adjust the dumpbase if the linker output file was seen. */
1898 int dumpbase_len = (strlen (dumppfx) + sizeof (DUMPBASE_SUFFIX));
1899 char *dumpbase = (char *) xmalloc (dumpbase_len + 1);
1900 snprintf (dumpbase, dumpbase_len, "%sltrans%u.ltrans", dumppfx, i);
1901 argv_ptr[0] = dumpbase;
1902
1903 argv_ptr[1] = "-fltrans";
1904 argv_ptr[2] = "-o";
1905 argv_ptr[3] = output_name;
1906 argv_ptr[4] = input_name;
1907 argv_ptr[5] = NULL;
1908 if (parallel)
1909 {
1910 fprintf (mstream, "%s:\n\t@%s ", output_name, new_argv[0]);
1911 for (j = 1; new_argv[j] != NULL; ++j)
1912 fprintf (mstream, " '%s'", new_argv[j]);
1913 fprintf (mstream, "\n");
1914 /* If we are not preserving the ltrans input files then
1915 truncate them as soon as we have processed it. This
1916 reduces temporary disk-space usage. */
1917 if (! save_temps)
1918 fprintf (mstream, "\t@-touch -r %s %s.tem > /dev/null 2>&1 "
1919 "&& mv %s.tem %s\n",
1920 input_name, input_name, input_name, input_name);
1921 }
1922 else
1923 {
1924 fork_execute (new_argv[0], CONST_CAST (char **, new_argv),
1925 true);
1926 maybe_unlink (input_name);
1927 }
1928
1929 output_names[i] = output_name;
1930 }
1931 if (parallel)
1932 {
1933 struct pex_obj *pex;
1934 char jobs[32];
1935
1936 fprintf (mstream,
1937 ".PHONY: all\n"
1938 "all:");
1939 for (i = 0; i < nr; ++i)
1940 {
1941 int j = ltrans_priorities[i*2 + 1];
1942 fprintf (mstream, " \\\n\t%s", output_names[j]);
1943 }
1944 fprintf (mstream, "\n");
1945 fclose (mstream);
1946 if (!jobserver)
1947 {
1948 /* Avoid passing --jobserver-fd= and similar flags
1949 unless jobserver mode is explicitly enabled. */
1950 putenv (xstrdup ("MAKEFLAGS="));
1951 putenv (xstrdup ("MFLAGS="));
1952 }
1953
1954 char **make_argv = buildargv (getenv ("MAKE"));
1955 if (make_argv)
1956 {
1957 for (unsigned argc = 0; make_argv[argc]; argc++)
1958 obstack_ptr_grow (&argv_obstack, make_argv[argc]);
1959 }
1960 else
1961 obstack_ptr_grow (&argv_obstack, "make");
1962
1963 obstack_ptr_grow (&argv_obstack, "-f");
1964 obstack_ptr_grow (&argv_obstack, makefile);
1965 if (!jobserver)
1966 {
1967 snprintf (jobs, 31, "-j%ld",
1968 auto_parallel ? nthreads_var : parallel);
1969 obstack_ptr_grow (&argv_obstack, jobs);
1970 }
1971 obstack_ptr_grow (&argv_obstack, "all");
1972 obstack_ptr_grow (&argv_obstack, NULL);
1973 new_argv = XOBFINISH (&argv_obstack, const char **);
1974
1975 pex = collect_execute (new_argv[0], CONST_CAST (char **, new_argv),
1976 NULL, NULL, PEX_SEARCH, false);
1977 do_wait (new_argv[0], pex);
1978 freeargv (make_argv);
1979 maybe_unlink (makefile);
1980 makefile = NULL;
1981 for (i = 0; i < nr; ++i)
1982 maybe_unlink (input_names[i]);
1983 }
1984 for (i = 0; i < nr; ++i)
1985 {
1986 fputs (output_names[i], stdout);
1987 putc ('\n', stdout);
1988 free (input_names[i]);
1989 }
1990 if (!skip_debug)
1991 {
1992 for (i = 0; i < ltoobj_argc; ++i)
1993 if (early_debug_object_names[i] != NULL)
1994 printf ("%s\n", early_debug_object_names[i]);
1995 }
1996 nr = 0;
1997 free (ltrans_priorities);
1998 free (output_names);
1999 output_names = NULL;
2000 free (early_debug_object_names);
2001 early_debug_object_names = NULL;
2002 free (input_names);
2003 free (list_option_full);
2004 obstack_free (&env_obstack, NULL);
2005 }
2006
2007 finish:
2008 XDELETE (lto_argv);
2009 obstack_free (&argv_obstack, NULL);
2010 }
2011
2012
2013 /* Entry point. */
2014
2015 int
2016 main (int argc, char *argv[])
2017 {
2018 const char *p;
2019
2020 init_opts_obstack ();
2021
2022 p = argv[0] + strlen (argv[0]);
2023 while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
2024 --p;
2025 progname = p;
2026
2027 xmalloc_set_program_name (progname);
2028
2029 gcc_init_libintl ();
2030
2031 diagnostic_initialize (global_dc, 0);
2032
2033 if (atexit (lto_wrapper_cleanup) != 0)
2034 fatal_error (input_location, "%<atexit%> failed");
2035
2036 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
2037 signal (SIGINT, fatal_signal);
2038 #ifdef SIGHUP
2039 if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
2040 signal (SIGHUP, fatal_signal);
2041 #endif
2042 if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
2043 signal (SIGTERM, fatal_signal);
2044 #ifdef SIGPIPE
2045 if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
2046 signal (SIGPIPE, fatal_signal);
2047 #endif
2048 #ifdef SIGCHLD
2049 /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
2050 receive the signal. A different setting is inheritable */
2051 signal (SIGCHLD, SIG_DFL);
2052 #endif
2053
2054 /* We may be called with all the arguments stored in some file and
2055 passed with @file. Expand them into argv before processing. */
2056 expandargv (&argc, &argv);
2057
2058 run_gcc (argc, argv);
2059
2060 return 0;
2061 }