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