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