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