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