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