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