1 /* Wrapper to call lto. Used by collect2 and the linker plugin.
2 Copyright (C) 2009-2025 Free Software Foundation, Inc.
4 Factored out of collect2 by Rafael Espindola <espindola@google.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
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.
30 $ lto-wrapper gcc/xgcc -B gcc a.o b.o -o test -flto
32 The above will print something like
35 If WHOPR is used instead, more than one file might be produced
36 ./ccXj2DTk.lto.ltrans.o
37 ./ccCJuXGv.lto.ltrans.o
40 #define INCLUDE_STRING
43 #define INCLUDE_VECTOR
46 #include "coretypes.h"
48 #include "diagnostic.h"
52 #include "simple-object.h"
53 #include "lto-section-names.h"
54 #include "collect-utils.h"
55 #include "opts-diagnostic.h"
56 #include "opt-suggestions.h"
57 #include "opts-jobserver.h"
58 #include "make-unique.h"
59 #include "lto-ltrans-cache.h"
61 /* Environment variable, used for passing the names of offload targets from GCC
62 driver to lto-wrapper. */
63 #define OFFLOAD_TARGET_NAMES_ENV "OFFLOAD_TARGET_NAMES"
64 #define OFFLOAD_TARGET_DEFAULT_ENV "OFFLOAD_TARGET_DEFAULT"
66 /* By default there is no special suffix for target executables. */
67 #ifdef TARGET_EXECUTABLE_SUFFIX
68 #define HAVE_TARGET_EXECUTABLE_SUFFIX
70 #define TARGET_EXECUTABLE_SUFFIX ""
74 LTO_MODE_NONE
, /* Not doing LTO. */
75 LTO_MODE_LTO
, /* Normal LTO. */
76 LTO_MODE_WHOPR
/* WHOPR. */
79 /* Current LTO mode. */
80 static enum lto_mode_d lto_mode
= LTO_MODE_NONE
;
82 static char *ltrans_output_file
;
83 static char *flto_out
;
84 static unsigned int nr
;
85 static int *ltrans_priorities
;
86 static char **input_names
;
87 static char const**output_names
;
88 static char **offload_names
;
89 static char *offload_objects_file_name
;
90 static char *makefile
;
91 static unsigned int num_deb_objs
;
92 static const char **early_debug_object_names
;
93 static bool xassembler_options_error
= false;
95 const char tool_name
[] = "lto-wrapper";
97 /* Auxiliary function that frees elements of PTR and PTR itself.
98 N is number of elements to be freed. If PTR is NULL, nothing is freed.
99 If an element is NULL, subsequent elements are not freed. */
102 free_array_of_ptrs (void **ptr
, unsigned n
)
106 for (unsigned i
= 0; i
< n
; i
++)
116 /* Delete tempfiles. Called from utils_cleanup. */
123 if (ltrans_output_file
)
124 maybe_unlink (ltrans_output_file
);
126 maybe_unlink (flto_out
);
127 if (offload_objects_file_name
)
128 maybe_unlink (offload_objects_file_name
);
130 maybe_unlink (makefile
);
131 if (early_debug_object_names
)
132 for (i
= 0; i
< num_deb_objs
; ++i
)
133 if (early_debug_object_names
[i
])
134 maybe_unlink (early_debug_object_names
[i
]);
135 for (i
= 0; i
< nr
; ++i
)
137 maybe_unlink (input_names
[i
]);
139 maybe_unlink (output_names
[i
]);
143 for (i
= 0; offload_names
[i
]; i
++)
144 maybe_unlink (offload_names
[i
]);
145 free_array_of_ptrs ((void **) offload_names
, i
);
150 lto_wrapper_cleanup (void)
152 utils_cleanup (false);
155 /* Unlink a temporary LTRANS file unless requested otherwise. */
158 maybe_unlink (const char *file
)
162 if (unlink_if_ordinary (file
)
164 fatal_error (input_location
, "deleting LTRANS file %s: %m", file
);
167 fprintf (stderr
, "[Leaving LTRANS %s]\n", file
);
170 /* Template of LTRANS dumpbase suffix. */
171 #define DUMPBASE_SUFFIX "ltrans18446744073709551615"
173 /* Create decoded options from the COLLECT_GCC and COLLECT_GCC_OPTIONS
176 static vec
<cl_decoded_option
>
177 get_options_from_collect_gcc_options (const char *collect_gcc
,
178 const char *collect_gcc_options
)
180 cl_decoded_option
*decoded_options
;
181 unsigned int decoded_options_count
;
182 struct obstack argv_obstack
;
186 obstack_init (&argv_obstack
);
187 obstack_ptr_grow (&argv_obstack
, collect_gcc
);
189 parse_options_from_collect_gcc_options (collect_gcc_options
,
190 &argv_obstack
, &argc
);
191 argv
= XOBFINISH (&argv_obstack
, const char **);
193 decode_cmdline_options_to_array (argc
, (const char **)argv
, CL_DRIVER
,
194 &decoded_options
, &decoded_options_count
);
195 vec
<cl_decoded_option
> decoded
;
196 decoded
.create (decoded_options_count
);
197 for (unsigned i
= 0; i
< decoded_options_count
; ++i
)
198 decoded
.quick_push (decoded_options
[i
]);
199 free (decoded_options
);
201 obstack_free (&argv_obstack
, NULL
);
206 /* Find option in OPTIONS based on OPT_INDEX, starting at START. -1
207 value is returned if the option is not present. */
210 find_option (vec
<cl_decoded_option
> &options
, size_t opt_index
,
213 for (unsigned i
= start
; i
< options
.length (); ++i
)
214 if (options
[i
].opt_index
== opt_index
)
221 find_option (vec
<cl_decoded_option
> &options
, cl_decoded_option
*option
)
223 return find_option (options
, option
->opt_index
);
226 /* Merge -flto FOPTION into vector of DECODED_OPTIONS. If FORCE is true
227 then FOPTION overrides previous settings. */
230 merge_flto_options (vec
<cl_decoded_option
> &decoded_options
,
231 cl_decoded_option
*foption
, bool force
)
233 int existing_opt
= find_option (decoded_options
, foption
);
234 if (existing_opt
== -1)
235 decoded_options
.safe_push (*foption
);
237 decoded_options
[existing_opt
].arg
= foption
->arg
;
240 if (strcmp (foption
->arg
, decoded_options
[existing_opt
].arg
) != 0)
242 /* -flto=auto is preferred. */
243 if (strcmp (decoded_options
[existing_opt
].arg
, "auto") == 0)
245 else if (strcmp (foption
->arg
, "auto") == 0
246 || strcmp (foption
->arg
, "jobserver") == 0)
247 decoded_options
[existing_opt
].arg
= foption
->arg
;
248 else if (strcmp (decoded_options
[existing_opt
].arg
,
251 int n
= atoi (foption
->arg
);
252 int original_n
= atoi (decoded_options
[existing_opt
].arg
);
254 decoded_options
[existing_opt
].arg
= foption
->arg
;
260 /* Try to merge and complain about options FDECODED_OPTIONS when applied
261 ontop of DECODED_OPTIONS. */
264 merge_and_complain (vec
<cl_decoded_option
> &decoded_options
,
265 vec
<cl_decoded_option
> fdecoded_options
,
266 vec
<cl_decoded_option
> decoded_cl_options
)
269 cl_decoded_option
*pic_option
= NULL
;
270 cl_decoded_option
*pie_option
= NULL
;
271 cl_decoded_option
*cf_protection_option
= NULL
;
273 /* ??? Merge options from files. Most cases can be
274 handled by either unioning or intersecting
275 (for example -fwrapv is a case for unioning,
276 -ffast-math is for intersection). Most complaints
277 about real conflicts between different options can
278 be deferred to the compiler proper. Options that
279 we can neither safely handle by intersection nor
280 unioning would need to be complained about here.
281 Ideally we'd have a flag in the opt files that
282 tells whether to union or intersect or reject.
283 In absence of that it's unclear what a good default is.
284 It's also difficult to get positional handling correct. */
286 /* Look for a -fcf-protection option in the link-time options
287 which overrides any -fcf-protection from the lto sections. */
288 for (i
= 0; i
< decoded_cl_options
.length (); ++i
)
290 cl_decoded_option
*foption
= &decoded_cl_options
[i
];
291 if (foption
->opt_index
== OPT_fcf_protection_
)
293 cf_protection_option
= foption
;
297 /* The following does what the old LTO option code did,
298 union all target and a selected set of common options. */
299 for (i
= 0; i
< fdecoded_options
.length (); ++i
)
301 cl_decoded_option
*foption
= &fdecoded_options
[i
];
302 int existing_opt
= find_option (decoded_options
, foption
);
303 switch (foption
->opt_index
)
305 case OPT_SPECIAL_unknown
:
306 case OPT_SPECIAL_ignore
:
307 case OPT_SPECIAL_warn_removed
:
308 case OPT_SPECIAL_program_name
:
309 case OPT_SPECIAL_input_file
:
313 if (!(cl_options
[foption
->opt_index
].flags
& CL_TARGET
))
317 case OPT_fdiagnostics_show_caret
:
318 case OPT_fdiagnostics_show_event_links
:
319 case OPT_fdiagnostics_show_highlight_colors
:
320 case OPT_fdiagnostics_show_labels
:
321 case OPT_fdiagnostics_show_line_numbers
:
322 case OPT_fdiagnostics_show_option
:
323 case OPT_fdiagnostics_show_location_
:
324 case OPT_fshow_column
:
328 /* Do what the old LTO code did - collect exactly one option
329 setting per OPT code, we pick the first we encounter.
330 ??? This doesn't make too much sense, but when it doesn't
331 then we should complain. */
332 if (existing_opt
== -1)
333 decoded_options
.safe_push (*foption
);
336 /* Figure out what PIC/PIE level wins and merge the results. */
339 pic_option
= foption
;
343 pie_option
= foption
;
348 case OPT_fasynchronous_unwind_tables
:
349 case OPT_funwind_tables
:
350 /* For selected options we can merge conservatively. */
351 if (existing_opt
== -1)
352 decoded_options
.safe_push (*foption
);
353 /* -fopenmp > -fno-openmp,
354 -fopenacc > -fno-openacc */
355 else if (foption
->value
> decoded_options
[existing_opt
].value
)
356 decoded_options
[existing_opt
] = *foption
;
359 case OPT_fopenacc_dim_
:
360 /* Append or check identical. */
361 if (existing_opt
== -1)
362 decoded_options
.safe_push (*foption
);
363 else if (strcmp (decoded_options
[existing_opt
].arg
, foption
->arg
))
364 fatal_error (input_location
,
365 "option %s with different values",
366 foption
->orig_option_with_args_text
);
369 case OPT_fcf_protection_
:
370 /* Default to link-time option, else append or check identical. */
371 if (!cf_protection_option
372 || cf_protection_option
->value
== CF_CHECK
)
374 if (existing_opt
== -1)
375 decoded_options
.safe_push (*foption
);
376 else if (decoded_options
[existing_opt
].value
!= foption
->value
)
378 if (cf_protection_option
379 && cf_protection_option
->value
== CF_CHECK
)
380 fatal_error (input_location
,
381 "option %qs with mismatching values"
384 decoded_options
[existing_opt
].arg
,
388 /* Merge and update the -fcf-protection option. */
389 decoded_options
[existing_opt
].value
390 &= (foption
->value
& CF_FULL
);
391 switch (decoded_options
[existing_opt
].value
)
394 decoded_options
[existing_opt
].arg
= "none";
397 decoded_options
[existing_opt
].arg
= "branch";
400 decoded_options
[existing_opt
].arg
= "return";
416 for (j
= 0; j
< decoded_options
.length (); ++j
)
417 if (decoded_options
[j
].opt_index
== OPT_O
418 || decoded_options
[j
].opt_index
== OPT_Ofast
419 || decoded_options
[j
].opt_index
== OPT_Og
420 || decoded_options
[j
].opt_index
== OPT_Os
421 || decoded_options
[j
].opt_index
== OPT_Oz
)
426 if (existing_opt
== -1)
427 decoded_options
.safe_push (*foption
);
428 else if (decoded_options
[existing_opt
].opt_index
== foption
->opt_index
429 && foption
->opt_index
!= OPT_O
)
430 /* Exact same options get merged. */
434 /* For mismatched option kinds preserve the optimization
435 level only, thus merge it as -On. This also handles
436 merging of same optimization level -On. */
438 switch (foption
->opt_index
)
441 if (foption
->arg
[0] == '\0')
442 level
= MAX (level
, 1);
444 level
= MAX (level
, atoi (foption
->arg
));
447 level
= MAX (level
, 3);
450 level
= MAX (level
, 1);
454 level
= MAX (level
, 2);
459 switch (decoded_options
[existing_opt
].opt_index
)
462 if (decoded_options
[existing_opt
].arg
[0] == '\0')
463 level
= MAX (level
, 1);
466 atoi (decoded_options
[existing_opt
].arg
));
469 level
= MAX (level
, 3);
472 level
= MAX (level
, 1);
476 level
= MAX (level
, 2);
481 decoded_options
[existing_opt
].opt_index
= OPT_O
;
483 tem
= xasprintf ("-O%d", level
);
484 decoded_options
[existing_opt
].arg
= &tem
[2];
485 decoded_options
[existing_opt
].canonical_option
[0] = tem
;
486 decoded_options
[existing_opt
].value
= 1;
491 case OPT_foffload_abi_
:
492 case OPT_foffload_abi_host_opts_
:
493 if (existing_opt
== -1)
494 decoded_options
.safe_push (*foption
);
495 else if (foption
->value
!= decoded_options
[existing_opt
].value
)
496 fatal_error (input_location
,
497 "option %s not used consistently in all LTO input"
498 " files", foption
->orig_option_with_args_text
);
502 case OPT_foffload_options_
:
503 decoded_options
.safe_push (*foption
);
507 merge_flto_options (decoded_options
, foption
, false);
512 /* Merge PIC options:
513 -fPIC + -fpic = -fpic
514 -fPIC + -fno-pic = -fno-pic
515 -fpic/-fPIC + nothing = nothing.
516 It is a common mistake to mix few -fPIC compiled objects into otherwise
517 non-PIC code. We do not want to build everything with PIC then.
519 Similarly we merge PIE options, however in addition we keep
520 -fPIC + -fPIE = -fPIE
521 -fpic + -fPIE = -fpie
522 -fPIC/-fpic + -fpie = -fpie
524 It would be good to warn on mismatches, but it is bit hard to do as
525 we do not know what nothing translates to. */
527 for (unsigned int j
= 0; j
< decoded_options
.length ();)
528 if (decoded_options
[j
].opt_index
== OPT_fPIC
529 || decoded_options
[j
].opt_index
== OPT_fpic
)
531 /* -fno-pic in one unit implies -fno-pic everywhere. */
532 if (decoded_options
[j
].value
== 0)
534 /* If we have no pic option or merge in -fno-pic, we still may turn
535 existing pic/PIC mode into pie/PIE if -fpie/-fPIE is present. */
536 else if ((pic_option
&& pic_option
->value
== 0)
541 bool big
= decoded_options
[j
].opt_index
== OPT_fPIC
542 && pie_option
->opt_index
== OPT_fPIE
;
543 decoded_options
[j
].opt_index
= big
? OPT_fPIE
: OPT_fpie
;
544 if (pie_option
->value
)
545 decoded_options
[j
].canonical_option
[0]
546 = big
? "-fPIE" : "-fpie";
548 decoded_options
[j
].canonical_option
[0] = "-fno-pie";
549 decoded_options
[j
].value
= pie_option
->value
;
554 decoded_options
[j
] = *pic_option
;
557 /* We do not know if target defaults to pic or not, so just remove
558 option if it is missing in one unit but enabled in other. */
560 decoded_options
.ordered_remove (j
);
562 else if (pic_option
->opt_index
== OPT_fpic
563 && decoded_options
[j
].opt_index
== OPT_fPIC
)
565 decoded_options
[j
] = *pic_option
;
571 else if (decoded_options
[j
].opt_index
== OPT_fPIE
572 || decoded_options
[j
].opt_index
== OPT_fpie
)
574 /* -fno-pie in one unit implies -fno-pie everywhere. */
575 if (decoded_options
[j
].value
== 0)
577 /* If we have no pie option or merge in -fno-pie, we still preserve
578 PIE/pie if pic/PIC is present. */
579 else if ((pie_option
&& pie_option
->value
== 0)
582 /* If -fPIC/-fpic is given, merge it with -fPIE/-fpie. */
585 if (pic_option
->opt_index
== OPT_fpic
586 && decoded_options
[j
].opt_index
== OPT_fPIE
)
588 decoded_options
[j
].opt_index
= OPT_fpie
;
589 decoded_options
[j
].canonical_option
[0]
590 = pic_option
->value
? "-fpie" : "-fno-pie";
592 else if (!pic_option
->value
)
593 decoded_options
[j
].canonical_option
[0] = "-fno-pie";
594 decoded_options
[j
].value
= pic_option
->value
;
599 decoded_options
[j
] = *pie_option
;
602 /* Because we always append pic/PIE options this code path should
603 not happen unless the LTO object was built by old lto1 which
604 did not contain that logic yet. */
606 decoded_options
.ordered_remove (j
);
608 else if (pie_option
->opt_index
== OPT_fpie
609 && decoded_options
[j
].opt_index
== OPT_fPIE
)
611 decoded_options
[j
] = *pie_option
;
620 int existing_opt_index
, existing_opt2_index
;
621 if (!xassembler_options_error
)
622 for (existing_opt_index
= existing_opt2_index
= 0; ;
623 existing_opt_index
++, existing_opt2_index
++)
626 = find_option (decoded_options
, OPT_Xassembler
, existing_opt_index
);
628 = find_option (fdecoded_options
, OPT_Xassembler
,
629 existing_opt2_index
);
631 cl_decoded_option
*existing_opt
= NULL
;
632 cl_decoded_option
*existing_opt2
= NULL
;
633 if (existing_opt_index
!= -1)
634 existing_opt
= &decoded_options
[existing_opt_index
];
635 if (existing_opt2_index
!= -1)
636 existing_opt2
= &fdecoded_options
[existing_opt2_index
];
638 if (existing_opt
== NULL
&& existing_opt2
== NULL
)
640 else if (existing_opt
!= NULL
&& existing_opt2
== NULL
)
642 warning (0, "Extra option to %<-Xassembler%>: %s,"
643 " dropping all %<-Xassembler%> and %<-Wa%> options.",
645 xassembler_options_error
= true;
648 else if (existing_opt
== NULL
&& existing_opt2
!= NULL
)
650 warning (0, "Extra option to %<-Xassembler%>: %s,"
651 " dropping all %<-Xassembler%> and %<-Wa%> options.",
653 xassembler_options_error
= true;
656 else if (strcmp (existing_opt
->arg
, existing_opt2
->arg
) != 0)
658 warning (0, "Options to %<-Xassembler%> do not match: %s, %s,"
659 " dropping all %<-Xassembler%> and %<-Wa%> options.",
660 existing_opt
->arg
, existing_opt2
->arg
);
661 xassembler_options_error
= true;
667 /* Parse STR, saving found tokens into PVALUES and return their number.
668 Tokens are assumed to be delimited by ':'. If APPEND is non-null,
669 append it to every token we find. */
672 parse_env_var (const char *str
, char ***pvalues
, const char *append
)
674 const char *curval
, *nextval
;
678 curval
= strchr (str
, ':');
682 curval
= strchr (curval
+ 1, ':');
685 values
= (char**) xmalloc (num
* sizeof (char*));
687 nextval
= strchr (curval
, ':');
689 nextval
= strchr (curval
, '\0');
691 int append_len
= append
? strlen (append
) : 0;
692 for (i
= 0; i
< num
; i
++)
694 int l
= nextval
- curval
;
695 values
[i
] = (char*) xmalloc (l
+ 1 + append_len
);
696 memcpy (values
[i
], curval
, l
);
699 strcat (values
[i
], append
);
700 curval
= nextval
+ 1;
701 nextval
= strchr (curval
, ':');
703 nextval
= strchr (curval
, '\0');
709 /* Append options OPTS from lto or offload_lto sections to ARGV_OBSTACK. */
712 append_compiler_options (obstack
*argv_obstack
, vec
<cl_decoded_option
> opts
)
714 /* Append compiler driver arguments as far as they were merged. */
715 for (unsigned int j
= 1; j
< opts
.length (); ++j
)
717 cl_decoded_option
*option
= &opts
[j
];
719 /* File options have been properly filtered by lto-opts.cc. */
720 switch (option
->opt_index
)
722 /* Drop arguments that we want to take from the link line. */
725 case OPT_flto_partition_
:
732 /* For now do what the original LTO option code was doing - pass
733 on any CL_TARGET flag and a few selected others. */
734 switch (option
->opt_index
)
736 case OPT_fdiagnostics_show_caret
:
737 case OPT_fdiagnostics_show_event_links
:
738 case OPT_fdiagnostics_show_highlight_colors
:
739 case OPT_fdiagnostics_show_labels
:
740 case OPT_fdiagnostics_show_line_numbers
:
741 case OPT_fdiagnostics_show_option
:
742 case OPT_fdiagnostics_show_location_
:
743 case OPT_fshow_column
:
752 case OPT_fopenacc_dim_
:
753 case OPT_foffload_abi_
:
754 case OPT_foffload_abi_host_opts_
:
755 case OPT_fcf_protection_
:
756 case OPT_fasynchronous_unwind_tables
:
757 case OPT_funwind_tables
:
767 /* When we detected a mismatch in assembler options between
768 the input TU's fall back to previous behavior of ignoring them. */
769 if (xassembler_options_error
)
774 if (!(cl_options
[option
->opt_index
].flags
& CL_TARGET
))
778 /* Pass the option on. */
779 for (unsigned int i
= 0; i
< option
->canonical_option_num_elements
; ++i
)
780 obstack_ptr_grow (argv_obstack
, option
->canonical_option
[i
]);
784 /* Append diag options in OPTS to ARGV_OBSTACK. */
787 append_diag_options (obstack
*argv_obstack
, vec
<cl_decoded_option
> opts
)
789 /* Append compiler driver arguments as far as they were merged. */
790 for (unsigned int j
= 1; j
< opts
.length (); ++j
)
792 cl_decoded_option
*option
= &opts
[j
];
794 switch (option
->opt_index
)
796 case OPT_fdiagnostics_color_
:
797 case OPT_fdiagnostics_format_
:
798 case OPT_fdiagnostics_show_caret
:
799 case OPT_fdiagnostics_show_event_links
:
800 case OPT_fdiagnostics_show_highlight_colors
:
801 case OPT_fdiagnostics_show_labels
:
802 case OPT_fdiagnostics_show_line_numbers
:
803 case OPT_fdiagnostics_show_option
:
804 case OPT_fdiagnostics_show_location_
:
805 case OPT_fshow_column
:
811 /* Pass the option on. */
812 for (unsigned int i
= 0; i
< option
->canonical_option_num_elements
; ++i
)
813 obstack_ptr_grow (argv_obstack
, option
->canonical_option
[i
]);
818 /* Append linker options OPTS to ARGV_OBSTACK. */
821 append_linker_options (obstack
*argv_obstack
, vec
<cl_decoded_option
> opts
)
823 /* Append linker driver arguments. Compiler options from the linker
824 driver arguments will override / merge with those from the compiler. */
825 for (unsigned int j
= 1; j
< opts
.length (); ++j
)
827 cl_decoded_option
*option
= &opts
[j
];
829 /* Do not pass on frontend specific flags not suitable for lto. */
830 if (!(cl_options
[option
->opt_index
].flags
831 & (CL_COMMON
|CL_TARGET
|CL_DRIVER
|CL_LTO
)))
834 switch (option
->opt_index
)
839 /* We've handled these LTO options, do not pass them on. */
844 /* Ignore -fno-XXX form of these options, as otherwise
845 corresponding builtins will not be enabled. */
846 if (option
->value
== 0)
854 /* Pass the option on. */
855 for (unsigned int i
= 0; i
< option
->canonical_option_num_elements
; ++i
)
856 obstack_ptr_grow (argv_obstack
, option
->canonical_option
[i
]);
860 /* Extract options for TARGET offload compiler from OPTIONS and append
861 them to ARGV_OBSTACK. */
864 append_offload_options (obstack
*argv_obstack
, const char *target
,
865 vec
<cl_decoded_option
> options
)
867 for (unsigned i
= 0; i
< options
.length (); i
++)
869 const char *cur
, *next
, *opts
;
872 cl_decoded_option
*option
= &options
[i
];
874 if (option
->opt_index
!= OPT_foffload_options_
)
877 /* If option argument starts with '-' then no target is specified. That
878 means offload options are specified for all targets, so we need to
880 if (option
->arg
[0] == '-')
884 opts
= strchr (option
->arg
, '=');
890 next
= strchr (cur
, ',');
893 next
= (next
> opts
) ? opts
: next
;
895 /* Are we looking for this offload target? */
896 if (strlen (target
) == (size_t) (next
- cur
)
897 && strncmp (target
, cur
, next
- cur
) == 0)
900 /* Skip the comma or equal sign. */
910 argv
= buildargv (opts
);
911 for (argc
= 0; argv
[argc
]; argc
++)
912 obstack_ptr_grow (argv_obstack
, argv
[argc
]);
916 /* Check whether NAME can be accessed in MODE. This is like access,
917 except that it never considers directories to be executable. */
920 access_check (const char *name
, int mode
)
926 if (stat (name
, &st
) < 0
927 || S_ISDIR (st
.st_mode
))
931 return access (name
, mode
);
934 /* Prepare a target image for offload TARGET, using mkoffload tool from
935 COMPILER_PATH. Return the name of the resultant object file. */
938 compile_offload_image (const char *target
, const char *compiler_path
,
939 unsigned in_argc
, char *in_argv
[],
940 vec
<cl_decoded_option
> compiler_opts
,
941 vec
<cl_decoded_option
> linker_opts
,
947 = XALLOCAVEC (char, sizeof ("/accel//mkoffload") + strlen (target
));
948 strcpy (suffix
, "/accel/");
949 strcat (suffix
, target
);
950 strcat (suffix
, "/mkoffload");
954 unsigned n_paths
= parse_env_var (compiler_path
, &paths
, suffix
);
956 const char *compiler
= NULL
;
957 for (unsigned i
= 0; i
< n_paths
; i
++)
958 if (access_check (paths
[i
], X_OK
) == 0)
963 #if OFFLOAD_DEFAULTED
964 if (!compiler
&& getenv (OFFLOAD_TARGET_DEFAULT_ENV
))
966 free_array_of_ptrs ((void **) paths
, n_paths
);
972 fatal_error (input_location
,
973 "could not find %s in %s (consider using %<-B%>)",
974 suffix
+ 1, compiler_path
);
976 dumpbase
= concat (dumppfx
, "x", target
, NULL
);
978 /* Generate temporary output file name. */
980 *filename
= concat (dumpbase
, ".o", NULL
);
982 *filename
= make_temp_file (".target.o");
984 struct obstack argv_obstack
;
985 obstack_init (&argv_obstack
);
986 obstack_ptr_grow (&argv_obstack
, compiler
);
988 obstack_ptr_grow (&argv_obstack
, "-save-temps");
990 obstack_ptr_grow (&argv_obstack
, "-v");
991 obstack_ptr_grow (&argv_obstack
, "-o");
992 obstack_ptr_grow (&argv_obstack
, *filename
);
994 /* Append names of input object files. */
995 for (unsigned i
= 0; i
< in_argc
; i
++)
996 obstack_ptr_grow (&argv_obstack
, in_argv
[i
]);
998 /* Append options from offload_lto sections. */
999 append_compiler_options (&argv_obstack
, compiler_opts
);
1000 append_diag_options (&argv_obstack
, linker_opts
);
1002 obstack_ptr_grow (&argv_obstack
, "-dumpbase");
1003 obstack_ptr_grow (&argv_obstack
, dumpbase
);
1005 /* Append options specified by -foffload last. In case of conflicting
1006 options we expect offload compiler to choose the latest. */
1007 append_offload_options (&argv_obstack
, target
, compiler_opts
);
1008 append_offload_options (&argv_obstack
, target
, linker_opts
);
1010 obstack_ptr_grow (&argv_obstack
, NULL
);
1011 argv
= XOBFINISH (&argv_obstack
, char **);
1012 suffix
= concat (target
, ".offload_args", NULL
);
1013 fork_execute (argv
[0], argv
, true, suffix
);
1014 obstack_free (&argv_obstack
, NULL
);
1016 free_array_of_ptrs ((void **) paths
, n_paths
);
1021 /* The main routine dealing with offloading.
1022 The routine builds a target image for each offload target. IN_ARGC and
1023 IN_ARGV specify options and input object files. As all of them could contain
1024 target sections, we pass them all to target compilers. */
1027 compile_images_for_offload_targets (unsigned in_argc
, char *in_argv
[],
1028 vec
<cl_decoded_option
> compiler_opts
,
1029 vec
<cl_decoded_option
> linker_opts
)
1031 char **names
= NULL
;
1032 const char *target_names
= getenv (OFFLOAD_TARGET_NAMES_ENV
);
1035 unsigned num_targets
= parse_env_var (target_names
, &names
, NULL
);
1036 int next_name_entry
= 0;
1038 const char *compiler_path
= getenv ("COMPILER_PATH");
1042 /* Prepare an image for each target and save the name of the resultant object
1043 file to the OFFLOAD_NAMES array. It is terminated by a NULL entry. */
1044 offload_names
= XCNEWVEC (char *, num_targets
+ 1);
1045 for (unsigned i
= 0; i
< num_targets
; i
++)
1047 if (!compile_offload_image (names
[i
], compiler_path
, in_argc
, in_argv
,
1048 compiler_opts
, linker_opts
,
1049 &offload_names
[next_name_entry
]))
1050 #if OFFLOAD_DEFAULTED
1053 fatal_error (input_location
,
1054 "problem with building target image for %s", names
[i
]);
1059 #if OFFLOAD_DEFAULTED
1060 if (next_name_entry
== 0)
1062 free (offload_names
);
1063 offload_names
= NULL
;
1068 free_array_of_ptrs ((void **) names
, num_targets
);
1071 /* Copy a file from SRC to DEST. */
1074 copy_file (const char *dest
, const char *src
)
1076 FILE *d
= fopen (dest
, "wb");
1077 FILE *s
= fopen (src
, "rb");
1081 size_t len
= fread (buffer
, 1, 512, s
);
1082 if (ferror (s
) != 0)
1083 fatal_error (input_location
, "reading input file");
1086 fwrite (buffer
, 1, len
, d
);
1087 if (ferror (d
) != 0)
1088 fatal_error (input_location
, "writing output file");
1095 /* Find the crtoffloadtable.o file in LIBRARY_PATH, make copy and pass name of
1096 the copy to the linker. */
1099 find_crtoffloadtable (int save_temps
, bool pie_or_shared
, const char *dumppfx
)
1101 char **paths
= NULL
;
1102 const char *library_path
= getenv ("LIBRARY_PATH");
1105 unsigned n_paths
= parse_env_var (library_path
, &paths
,
1107 ? "/crtoffloadtableS.o"
1108 : "/crtoffloadtable.o");
1111 for (i
= 0; i
< n_paths
; i
++)
1112 if (access_check (paths
[i
], R_OK
) == 0)
1114 /* The linker will delete the filename we give it, so make a copy. */
1115 char *crtoffloadtable
;
1117 crtoffloadtable
= make_temp_file (".crtoffloadtable.o");
1119 crtoffloadtable
= concat (dumppfx
, "crtoffloadtable.o", NULL
);
1120 copy_file (crtoffloadtable
, paths
[i
]);
1121 printf ("%s\n", crtoffloadtable
);
1122 XDELETEVEC (crtoffloadtable
);
1126 fatal_error (input_location
,
1127 "installation error, cannot find %<crtoffloadtable%s.o%>",
1128 pie_or_shared
? "S" : "");
1130 free_array_of_ptrs ((void **) paths
, n_paths
);
1133 /* A subroutine of run_gcc. Examine the open file FD for lto sections with
1134 name prefix PREFIX, at FILE_OFFSET, and store any options we find in OPTS.
1135 Return true if we found a matching section, false
1136 otherwise. COLLECT_GCC holds the value of the environment variable with
1140 find_and_merge_options (int fd
, off_t file_offset
, const char *prefix
,
1141 vec
<cl_decoded_option
> decoded_cl_options
, bool first
,
1142 vec
<cl_decoded_option
> *opts
, const char *collect_gcc
)
1144 off_t offset
, length
;
1149 vec
<cl_decoded_option
> fdecoded_options
;
1152 fdecoded_options
= *opts
;
1154 simple_object_read
*sobj
;
1155 sobj
= simple_object_start_read (fd
, file_offset
, "__GNU_LTO",
1160 char *secname
= XALLOCAVEC (char, strlen (prefix
) + sizeof (".opts"));
1161 strcpy (secname
, prefix
);
1162 strcat (secname
, ".opts");
1163 if (!simple_object_find_section (sobj
, secname
, &offset
, &length
,
1166 simple_object_release_read (sobj
);
1170 lseek (fd
, file_offset
+ offset
, SEEK_SET
);
1171 data
= (char *)xmalloc (length
);
1172 read (fd
, data
, length
);
1176 vec
<cl_decoded_option
> f2decoded_options
1177 = get_options_from_collect_gcc_options (collect_gcc
, fopts
);
1180 fdecoded_options
= f2decoded_options
;
1184 merge_and_complain (fdecoded_options
, f2decoded_options
,
1185 decoded_cl_options
);
1187 fopts
+= strlen (fopts
) + 1;
1189 while (fopts
- data
< length
);
1192 simple_object_release_read (sobj
);
1193 *opts
= fdecoded_options
;
1197 /* Copy early debug info sections from INFILE to a new file whose name
1198 is returned. Return NULL on error. */
1201 debug_objcopy (const char *infile
, bool rename
)
1208 const char *orig_infile
= infile
;
1212 if ((p
= strrchr (infile
, '@'))
1214 && sscanf (p
, "@%li%n", &loffset
, &consumed
) >= 1
1215 && strlen (p
) == (unsigned int) consumed
)
1217 char *fname
= xstrdup (infile
);
1218 fname
[p
- infile
] = '\0';
1220 inoff
= (off_t
) loffset
;
1222 int infd
= open (infile
, O_RDONLY
| O_BINARY
);
1225 simple_object_read
*inobj
= simple_object_start_read (infd
, inoff
,
1232 if (simple_object_find_section (inobj
, ".gnu.debuglto_.debug_info",
1233 &off
, &len
, &errmsg
, &err
) != 1)
1236 fatal_error (0, "%s: %s", errmsg
, xstrerror (err
));
1238 simple_object_release_read (inobj
);
1244 outfile
= concat (orig_infile
, ".debug.temp.o", NULL
);
1246 outfile
= make_temp_file (".debug.temp.o");
1247 errmsg
= simple_object_copy_lto_debug_sections (inobj
, outfile
, &err
, rename
);
1250 unlink_if_ordinary (outfile
);
1251 fatal_error (0, "%s: %s", errmsg
, xstrerror (err
));
1254 simple_object_release_read (inobj
);
1260 /* Helper for qsort: compare priorities for parallel compilation. */
1263 cmp_priority (const void *a
, const void *b
)
1265 return *((const int *)b
)-*((const int *)a
);
1268 /* Number of CPUs that can be used for parallel LTRANS phase. */
1270 static unsigned long nthreads_var
= 0;
1272 #ifdef HAVE_PTHREAD_AFFINITY_NP
1273 unsigned long cpuset_size
;
1274 static unsigned long get_cpuset_size
;
1278 static cpuset_popcount (unsigned long cpusetsize
, cpu_set_t
*cpusetp
)
1281 /* glibc 2.7 and above provide a macro for this. */
1282 return CPU_COUNT_S (cpusetsize
, cpusetp
);
1285 if (cpusetsize
== sizeof (cpu_set_t
))
1286 /* glibc 2.6 and above provide a macro for this. */
1287 return CPU_COUNT (cpusetp
);
1290 unsigned long ret
= 0;
1291 STATIC_ASSERT (sizeof (cpusetp
->__bits
[0]) == sizeof (unsigned long int));
1292 for (i
= 0; i
< cpusetsize
/ sizeof (cpusetp
->__bits
[0]); i
++)
1294 unsigned long int mask
= cpusetp
->__bits
[i
];
1297 ret
+= __builtin_popcountl (mask
);
1304 /* At startup, determine the default number of threads. It would seem
1305 this should be related to the number of cpus online. */
1308 init_num_threads (void)
1310 #ifdef HAVE_PTHREAD_AFFINITY_NP
1311 #if defined (_SC_NPROCESSORS_CONF) && defined (CPU_ALLOC_SIZE)
1312 cpuset_size
= sysconf (_SC_NPROCESSORS_CONF
);
1313 cpuset_size
= CPU_ALLOC_SIZE (cpuset_size
);
1315 cpuset_size
= sizeof (cpu_set_t
);
1318 cpusetp
= (cpu_set_t
*) xmalloc (gomp_cpuset_size
);
1321 int ret
= pthread_getaffinity_np (pthread_self (), gomp_cpuset_size
,
1325 /* Count only the CPUs this process can use. */
1326 nthreads_var
= cpuset_popcount (cpuset_size
, cpusetp
);
1327 if (nthreads_var
== 0)
1329 get_cpuset_size
= cpuset_size
;
1330 #ifdef CPU_ALLOC_SIZE
1332 for (i
= cpuset_size
* 8; i
; i
--)
1333 if (CPU_ISSET_S (i
- 1, cpuset_size
, cpusetp
))
1335 cpuset_size
= CPU_ALLOC_SIZE (i
);
1341 #ifdef CPU_ALLOC_SIZE
1342 if (cpuset_size
< sizeof (cpu_set_t
))
1343 cpuset_size
= sizeof (cpu_set_t
);
1345 cpuset_size
= cpuset_size
* 2;
1346 if (cpuset_size
< 8 * sizeof (cpu_set_t
))
1348 = (cpu_set_t
*) realloc (cpusetp
, cpuset_size
);
1351 /* Avoid fatal if too large memory allocation would be
1352 requested, e.g. kernel returning EINVAL all the time. */
1353 void *p
= realloc (cpusetp
, cpuset_size
);
1356 cpusetp
= (cpu_set_t
*) p
;
1368 #ifdef _SC_NPROCESSORS_ONLN
1369 nthreads_var
= sysconf (_SC_NPROCESSORS_ONLN
);
1373 /* Print link to -flto documentation with a hint message. */
1376 print_lto_docs_link ()
1378 label_text url
= label_text::take (global_dc
->make_option_url (OPT_flto
));
1379 inform (UNKNOWN_LOCATION
,
1380 "see the %{%<-flto%> option documentation%} for more information",
1384 /* Test that a make command is present and working, return true if so. */
1389 const char *make
= "make";
1390 char **make_argv
= buildargv (getenv ("MAKE"));
1392 make
= make_argv
[0];
1393 const char *make_args
[] = {make
, "--version", NULL
};
1395 int exit_status
= 0;
1398 = pex_one (PEX_SEARCH
, make_args
[0], CONST_CAST (char **, make_args
),
1399 "make", NULL
, NULL
, &exit_status
, &err
);
1400 freeargv (make_argv
);
1401 return errmsg
== NULL
&& exit_status
== 0 && err
== 0;
1404 /* Execute gcc. ARGC is the number of arguments. ARGV contains the arguments. */
1407 run_gcc (unsigned argc
, char *argv
[])
1410 const char **new_argv
;
1411 const char **argv_ptr
;
1412 char *list_option_full
= NULL
;
1413 const char *linker_output
= NULL
;
1414 const char *collect_gcc
;
1415 char *collect_gcc_options
;
1418 bool jobserver_requested
= false;
1419 int auto_parallel
= 0;
1420 bool no_partition
= false;
1421 bool fdecoded_options_first
= true;
1422 vec
<cl_decoded_option
> fdecoded_options
;
1423 fdecoded_options
.create (16);
1424 bool offload_fdecoded_options_first
= true;
1425 vec
<cl_decoded_option
> offload_fdecoded_options
= vNULL
;
1426 struct obstack argv_obstack
;
1428 bool have_lto
= false;
1429 bool have_offload
= false;
1430 unsigned lto_argc
= 0, ltoobj_argc
= 0;
1431 char **lto_argv
, **ltoobj_argv
;
1432 bool linker_output_rel
= false;
1433 bool skip_debug
= false;
1434 #ifdef ENABLE_DEFAULT_PIE
1435 bool pie_or_shared
= true;
1437 bool pie_or_shared
= false;
1439 const char *incoming_dumppfx
= dumppfx
= NULL
;
1440 static char current_dir
[] = { '.', DIR_SEPARATOR
, '\0' };
1441 const char *ltrans_cache_dir
= NULL
;
1442 size_t ltrans_cache_size
= 4096;
1444 /* Get the driver and options. */
1445 collect_gcc
= getenv ("COLLECT_GCC");
1447 fatal_error (input_location
,
1448 "environment variable %<COLLECT_GCC%> must be set");
1449 collect_gcc_options
= getenv ("COLLECT_GCC_OPTIONS");
1450 if (!collect_gcc_options
)
1451 fatal_error (input_location
,
1452 "environment variable %<COLLECT_GCC_OPTIONS%> must be set");
1454 char *collect_as_options
= getenv ("COLLECT_AS_OPTIONS");
1456 /* Prepend -Xassembler to each option, and append the string
1457 to collect_gcc_options. */
1458 if (collect_as_options
)
1460 obstack temporary_obstack
;
1461 obstack_init (&temporary_obstack
);
1463 prepend_xassembler_to_collect_as_options (collect_as_options
,
1464 &temporary_obstack
);
1465 obstack_1grow (&temporary_obstack
, '\0');
1467 char *xassembler_opts_string
1468 = XOBFINISH (&temporary_obstack
, char *);
1469 collect_gcc_options
= concat (collect_gcc_options
, xassembler_opts_string
,
1473 vec
<cl_decoded_option
> decoded_options
1474 = get_options_from_collect_gcc_options (collect_gcc
, collect_gcc_options
);
1476 /* Allocate array for input object files with LTO IL,
1477 and for possible preceding arguments. */
1478 lto_argv
= XNEWVEC (char *, argc
);
1479 ltoobj_argv
= XNEWVEC (char *, argc
);
1481 /* Look at saved options in the IL files. */
1482 for (i
= 1; i
< argc
; ++i
)
1486 off_t file_offset
= 0;
1489 char *filename
= argv
[i
];
1491 if (startswith (argv
[i
], "-foffload-objects="))
1493 have_offload
= true;
1494 offload_objects_file_name
1495 = argv
[i
] + sizeof ("-foffload-objects=") - 1;
1499 if ((p
= strrchr (argv
[i
], '@'))
1501 && sscanf (p
, "@%li%n", &loffset
, &consumed
) >= 1
1502 && strlen (p
) == (unsigned int) consumed
)
1504 filename
= XNEWVEC (char, p
- argv
[i
] + 1);
1505 memcpy (filename
, argv
[i
], p
- argv
[i
]);
1506 filename
[p
- argv
[i
]] = '\0';
1507 file_offset
= (off_t
) loffset
;
1509 fd
= open (filename
, O_RDONLY
| O_BINARY
);
1510 /* Linker plugin passes -fresolution and -flinker-output options.
1511 -flinker-output is passed only when user did not specify one and thus
1512 we do not need to worry about duplicities with the option handling
1516 lto_argv
[lto_argc
++] = argv
[i
];
1517 if (strcmp (argv
[i
], "-flinker-output=rel") == 0)
1518 linker_output_rel
= true;
1522 if (find_and_merge_options (fd
, file_offset
, LTO_SECTION_NAME_PREFIX
,
1523 decoded_options
, fdecoded_options_first
,
1528 ltoobj_argv
[ltoobj_argc
++] = argv
[i
];
1529 fdecoded_options_first
= false;
1534 /* Initalize the common arguments for the driver. */
1535 obstack_init (&argv_obstack
);
1536 obstack_ptr_grow (&argv_obstack
, collect_gcc
);
1537 obstack_ptr_grow (&argv_obstack
, "-xlto");
1538 obstack_ptr_grow (&argv_obstack
, "-c");
1540 append_compiler_options (&argv_obstack
, fdecoded_options
);
1541 append_linker_options (&argv_obstack
, decoded_options
);
1543 /* Scan linker driver arguments for things that are of relevance to us. */
1544 for (j
= 1; j
< decoded_options
.length (); ++j
)
1546 cl_decoded_option
*option
= &decoded_options
[j
];
1547 switch (option
->opt_index
)
1550 linker_output
= option
->arg
;
1553 /* We don't have to distinguish between -save-temps=* and
1554 -save-temps, -dumpdir already carries that
1556 case OPT_save_temps_
:
1557 case OPT_save_temps
:
1565 case OPT_flto_partition_
:
1566 if (strcmp (option
->arg
, "none") == 0)
1567 no_partition
= true;
1570 case OPT_flto_incremental_
:
1572 if (access (option
->arg
, W_OK
) == 0)
1573 ltrans_cache_dir
= option
->arg
;
1575 fatal_error (input_location
, "missing directory: %s", option
->arg
);
1578 case OPT_flto_incremental_cache_size_
:
1579 ltrans_cache_size
= atoi (option
->arg
);
1583 /* Override IL file settings with a linker -flto= option. */
1584 merge_flto_options (fdecoded_options
, option
, true);
1585 if (strcmp (option
->arg
, "jobserver") == 0)
1586 jobserver_requested
= true;
1589 case OPT_flinker_output_
:
1590 linker_output_rel
= !strcmp (option
->arg
, "rel");
1594 /* Recognize -g0. */
1595 skip_debug
= option
->arg
&& !strcmp (option
->arg
, "0");
1604 /* Negative forms, if allowed, enable debug info as well. */
1609 incoming_dumppfx
= dumppfx
= option
->arg
;
1612 case OPT_fdiagnostics_urls_
:
1613 diagnostic_urls_init (global_dc
, option
->value
);
1616 case OPT_fdiagnostics_color_
:
1617 diagnostic_color_init (global_dc
, option
->value
);
1620 case OPT_fdiagnostics_show_highlight_colors
:
1621 global_dc
->set_show_highlight_colors (option
->value
);
1626 case OPT_static_pie
:
1627 pie_or_shared
= true;
1631 pie_or_shared
= false;
1639 /* Process LTO-related options on merged options. */
1640 for (j
= 1; j
< fdecoded_options
.length (); ++j
)
1642 cl_decoded_option
*option
= &fdecoded_options
[j
];
1643 switch (option
->opt_index
)
1646 if (strcmp (option
->arg
, "jobserver") == 0)
1651 else if (strcmp (option
->arg
, "auto") == 0)
1658 parallel
= atoi (option
->arg
);
1665 lto_mode
= LTO_MODE_WHOPR
;
1670 /* Output lto-wrapper invocation command. */
1673 for (i
= 0; i
< argc
; ++i
)
1675 fputs (argv
[i
], stderr
);
1676 fputc (' ', stderr
);
1678 fputc ('\n', stderr
);
1681 if (linker_output_rel
)
1682 no_partition
= true;
1686 lto_mode
= LTO_MODE_LTO
;
1688 jobserver_requested
= false;
1694 jobserver_info jinfo
;
1695 if (jobserver
&& !jinfo
.is_active
)
1697 /* Fall back to auto parallelism. */
1701 else if (!jobserver
&& jinfo
.is_active
)
1708 /* We need make working for a parallel execution. */
1709 if (parallel
&& !make_exists ())
1715 || strcmp (linker_output
, HOST_BIT_BUCKET
) == 0)
1719 const char *obase
= lbasename (linker_output
), *temp
;
1721 /* Strip the executable extension. */
1722 size_t blen
= strlen (obase
), xlen
;
1723 if ((temp
= strrchr (obase
+ 1, '.'))
1724 && (xlen
= strlen (temp
))
1725 && (strcmp (temp
, ".exe") == 0
1726 #if defined(HAVE_TARGET_EXECUTABLE_SUFFIX)
1727 || strcmp (temp
, TARGET_EXECUTABLE_SUFFIX
) == 0
1729 || strcmp (obase
, "a.out") == 0))
1730 dumppfx
= xstrndup (linker_output
,
1731 obase
- linker_output
+ blen
- xlen
+ 1);
1733 dumppfx
= concat (linker_output
, ".", NULL
);
1737 /* If there's no directory component in the dumppfx, add one, so
1738 that, when it is used as -dumpbase, it overrides any occurrence
1739 of -dumpdir that might have been passed in. */
1740 if (!dumppfx
|| lbasename (dumppfx
) == dumppfx
)
1741 dumppfx
= concat (current_dir
, dumppfx
, NULL
);
1743 /* Make sure some -dumpdir is passed, so as to get predictable
1744 -dumpbase overriding semantics. If we got an incoming -dumpdir
1745 argument, we'll pass it on, so don't bother with another one
1747 if (!incoming_dumppfx
)
1749 obstack_ptr_grow (&argv_obstack
, "-dumpdir");
1750 obstack_ptr_grow (&argv_obstack
, "");
1752 obstack_ptr_grow (&argv_obstack
, "-dumpbase");
1754 /* Remember at which point we can scrub args to re-use the commons. */
1755 new_head_argc
= obstack_object_size (&argv_obstack
) / sizeof (void *);
1759 unsigned i
, num_offload_files
;
1760 char **offload_argv
;
1763 f
= fopen (offload_objects_file_name
, "r");
1765 fatal_error (input_location
, "cannot open %s: %m",
1766 offload_objects_file_name
);
1767 if (fscanf (f
, "%u ", &num_offload_files
) != 1)
1768 fatal_error (input_location
, "cannot read %s: %m",
1769 offload_objects_file_name
);
1770 offload_argv
= XCNEWVEC (char *, num_offload_files
);
1772 /* Read names of object files with offload. */
1773 for (i
= 0; i
< num_offload_files
; i
++)
1775 const unsigned piece
= 32;
1776 char *buf
, *filename
= XNEWVEC (char, piece
);
1781 if (!fgets (buf
, piece
, f
))
1783 len
= strlen (filename
);
1784 if (filename
[len
- 1] != '\n')
1786 filename
= XRESIZEVEC (char, filename
, len
+ piece
);
1787 buf
= filename
+ len
;
1790 filename
[len
- 1] = '\0';
1791 offload_argv
[i
] = filename
;
1794 if (offload_argv
[num_offload_files
- 1] == NULL
)
1795 fatal_error (input_location
, "invalid format of %s",
1796 offload_objects_file_name
);
1797 maybe_unlink (offload_objects_file_name
);
1798 offload_objects_file_name
= NULL
;
1800 /* Look at saved offload options in files. */
1801 for (i
= 0; i
< num_offload_files
; i
++)
1806 off_t file_offset
= 0;
1807 char *filename
= offload_argv
[i
];
1809 if ((p
= strrchr (offload_argv
[i
], '@'))
1810 && p
!= offload_argv
[i
]
1811 && sscanf (p
, "@%li%n", &loffset
, &consumed
) >= 1
1812 && strlen (p
) == (unsigned int) consumed
)
1814 filename
= XNEWVEC (char, p
- offload_argv
[i
] + 1);
1815 memcpy (filename
, offload_argv
[i
], p
- offload_argv
[i
]);
1816 filename
[p
- offload_argv
[i
]] = '\0';
1817 file_offset
= (off_t
) loffset
;
1819 fd
= open (filename
, O_RDONLY
| O_BINARY
);
1821 fatal_error (input_location
, "cannot open %s: %m", filename
);
1822 if (!find_and_merge_options (fd
, file_offset
,
1823 OFFLOAD_SECTION_NAME_PREFIX
,
1825 offload_fdecoded_options_first
,
1826 &offload_fdecoded_options
,
1828 fatal_error (input_location
, "cannot read %s: %m", filename
);
1829 offload_fdecoded_options_first
= false;
1831 if (filename
!= offload_argv
[i
])
1832 XDELETEVEC (filename
);
1835 compile_images_for_offload_targets (num_offload_files
, offload_argv
,
1836 offload_fdecoded_options
, decoded_options
);
1838 free_array_of_ptrs ((void **) offload_argv
, num_offload_files
);
1842 find_crtoffloadtable (save_temps
, pie_or_shared
, dumppfx
);
1843 for (i
= 0; offload_names
[i
]; i
++)
1844 printf ("%s\n", offload_names
[i
]);
1845 free_array_of_ptrs ((void **) offload_names
, i
);
1846 offload_names
= NULL
;
1850 /* If object files contain offload sections, but do not contain LTO sections,
1851 then there is no need to perform a link-time recompilation, i.e.
1852 lto-wrapper is used only for a compilation of offload images. */
1853 if (have_offload
&& !have_lto
)
1856 if (lto_mode
== LTO_MODE_LTO
)
1858 /* -dumpbase argument for LTO. */
1859 flto_out
= concat (dumppfx
, "lto.o", NULL
);
1860 obstack_ptr_grow (&argv_obstack
, flto_out
);
1863 flto_out
= make_temp_file (".lto.o");
1864 obstack_ptr_grow (&argv_obstack
, "-o");
1865 obstack_ptr_grow (&argv_obstack
, flto_out
);
1869 const char *list_option
= "-fltrans-output-list=";
1871 /* -dumpbase argument for WPA. */
1872 char *dumpbase
= concat (dumppfx
, "wpa", NULL
);
1873 obstack_ptr_grow (&argv_obstack
, dumpbase
);
1875 if (ltrans_cache_dir
)
1877 /* Results of wpa phase must be on the same disk partition as
1879 char* file
= concat (ltrans_cache_dir
, "/ccXXXXXX.ltrans.out", NULL
);
1880 int fd
= mkstemps (file
, strlen (".ltrans.out"));
1881 gcc_assert (fd
!= -1 && !close (fd
));
1883 ltrans_output_file
= file
;
1885 else if (save_temps
)
1886 ltrans_output_file
= concat (dumppfx
, "ltrans.out", NULL
);
1888 ltrans_output_file
= make_temp_file (".ltrans.out");
1889 list_option_full
= concat (list_option
, ltrans_output_file
, NULL
);
1890 obstack_ptr_grow (&argv_obstack
, list_option_full
);
1895 fprintf (stderr
, "Using make jobserver\n");
1896 obstack_ptr_grow (&argv_obstack
, xstrdup ("-fwpa=jobserver"));
1898 else if (auto_parallel
)
1901 init_num_threads ();
1902 if (nthreads_var
== 0)
1905 fprintf (stderr
, "LTO parallelism level set to %ld\n",
1907 sprintf (buf
, "-fwpa=%ld", nthreads_var
);
1908 obstack_ptr_grow (&argv_obstack
, xstrdup (buf
));
1910 else if (parallel
> 1)
1913 sprintf (buf
, "-fwpa=%i", parallel
);
1914 obstack_ptr_grow (&argv_obstack
, xstrdup (buf
));
1917 obstack_ptr_grow (&argv_obstack
, "-fwpa");
1920 /* Append input arguments. */
1921 for (i
= 0; i
< lto_argc
; ++i
)
1922 obstack_ptr_grow (&argv_obstack
, lto_argv
[i
]);
1923 /* Append the input objects. */
1924 for (i
= 0; i
< ltoobj_argc
; ++i
)
1925 obstack_ptr_grow (&argv_obstack
, ltoobj_argv
[i
]);
1926 obstack_ptr_grow (&argv_obstack
, NULL
);
1928 new_argv
= XOBFINISH (&argv_obstack
, const char **);
1929 argv_ptr
= &new_argv
[new_head_argc
];
1930 fork_execute (new_argv
[0], CONST_CAST (char **, new_argv
), true,
1933 /* Copy the early generated debug info from the objects to temporary
1934 files and append those to the partial link commandline. */
1935 early_debug_object_names
= NULL
;
1938 early_debug_object_names
= XCNEWVEC (const char *, ltoobj_argc
+ 1);
1939 num_deb_objs
= ltoobj_argc
;
1940 for (i
= 0; i
< ltoobj_argc
; ++i
)
1943 if ((tem
= debug_objcopy (ltoobj_argv
[i
], !linker_output_rel
)))
1944 early_debug_object_names
[i
] = tem
;
1948 if (lto_mode
== LTO_MODE_LTO
)
1950 printf ("%s\n", flto_out
);
1953 for (i
= 0; i
< ltoobj_argc
; ++i
)
1954 if (early_debug_object_names
[i
] != NULL
)
1955 printf ("%s\n", early_debug_object_names
[i
]);
1957 /* These now belong to collect2. */
1960 free (early_debug_object_names
);
1961 early_debug_object_names
= NULL
;
1965 FILE *stream
= fopen (ltrans_output_file
, "r");
1966 FILE *mstream
= NULL
;
1967 struct obstack env_obstack
;
1971 fatal_error (input_location
, "%<fopen%>: %s: %m", ltrans_output_file
);
1973 /* Parse the list of LTRANS inputs from the WPA stage. */
1974 obstack_init (&env_obstack
);
1978 const unsigned piece
= 32;
1979 char *output_name
= NULL
;
1980 char *buf
, *input_name
= (char *)xmalloc (piece
);
1984 if (fscanf (stream
, "%i\n", &priority
) != 1)
1987 fatal_error (input_location
,
1988 "corrupted ltrans output file %s",
1989 ltrans_output_file
);
1993 if (!fgets (buf
, piece
, stream
))
1995 len
= strlen (input_name
);
1996 if (input_name
[len
- 1] != '\n')
1998 input_name
= (char *)xrealloc (input_name
, len
+ piece
);
1999 buf
= input_name
+ len
;
2002 input_name
[len
- 1] = '\0';
2004 if (input_name
[0] == '*')
2005 output_name
= &input_name
[1];
2009 = (int *)xrealloc (ltrans_priorities
, nr
* sizeof (int) * 2);
2010 input_names
= (char **)xrealloc (input_names
, nr
* sizeof (char *));
2011 output_names
= (char const**)
2012 xrealloc (output_names
, nr
* sizeof (char const*));
2013 ltrans_priorities
[(nr
-1)*2] = priority
;
2014 ltrans_priorities
[(nr
-1)*2+1] = nr
-1;
2015 input_names
[nr
-1] = input_name
;
2016 output_names
[nr
-1] = output_name
;
2019 maybe_unlink (ltrans_output_file
);
2020 ltrans_output_file
= NULL
;
2024 jobserver_info jinfo
;
2025 if (jobserver_requested
&& !jinfo
.is_active
)
2027 warning (0, jinfo
.error_msg
.c_str ());
2028 print_lto_docs_link ();
2030 else if (parallel
== 0)
2032 warning (0, "using serial compilation of %d LTRANS jobs", nr
);
2033 print_lto_docs_link ();
2040 makefile
= concat (dumppfx
, "ltrans.mk", NULL
);
2042 makefile
= make_temp_file (".mk");
2043 mstream
= fopen (makefile
, "w");
2044 qsort (ltrans_priorities
, nr
, sizeof (int) * 2, cmp_priority
);
2047 ltrans_file_cache
ltrans_cache (ltrans_cache_dir
, "ltrans", ".o",
2052 if (!lockfile::lockfile_supported ())
2054 warning (0, "using ltrans cache without file locking support,"
2055 " do not use in parallel");
2057 ltrans_cache
.deletion_lock
.lock_read ();
2058 ltrans_cache
.creation_lock
.lock_write ();
2060 ltrans_cache
.load_cache ();
2062 int recompiling
= 0;
2064 for (i
= 0; i
< nr
; ++i
)
2066 /* If it's a pass-through file do nothing. */
2067 if (output_names
[i
])
2070 ltrans_file_cache::item
* item
;
2071 bool existed
= ltrans_cache
.add_to_cache (input_names
[i
], item
);
2072 free (input_names
[i
]);
2073 input_names
[i
] = xstrdup (item
->input
.c_str ());
2077 /* Fill the output_name to skip compilation. */
2078 output_names
[i
] = item
->output
.c_str ();
2082 /* Lock so no other process can access until the file is
2084 item
->lock
.lock_write ();
2089 fprintf (stderr
, "LTRANS: recompiling %d/%d\n", recompiling
, nr
);
2091 ltrans_cache
.save_cache ();
2092 ltrans_cache
.creation_lock
.unlock ();
2095 /* Execute the LTRANS stage for each input file (or prepare a
2096 makefile to invoke this in parallel). */
2097 for (i
= 0; i
< nr
; ++i
)
2099 char const* output_name
;
2100 char *input_name
= input_names
[i
];
2101 /* If it's a pass-through or cached file do nothing. */
2102 if (output_names
[i
])
2107 ltrans_file_cache::item
* item
;
2108 item
= ltrans_cache
.get_item (input_name
);
2111 output_name
= item
->output
.c_str ();
2115 /* Replace the .o suffix with a .ltrans.o suffix and write
2116 the resulting name to the LTRANS output list. */
2117 obstack_grow (&env_obstack
, input_name
, strlen (input_name
) - 2);
2118 obstack_grow (&env_obstack
, ".ltrans.o", sizeof (".ltrans.o"));
2119 output_name
= XOBFINISH (&env_obstack
, char const*);
2122 /* Adjust the dumpbase if the linker output file was seen. */
2123 int dumpbase_len
= (strlen (dumppfx
)
2124 + sizeof (DUMPBASE_SUFFIX
)
2125 + sizeof (".ltrans"));
2126 char *dumpbase
= (char *) xmalloc (dumpbase_len
+ 1);
2127 snprintf (dumpbase
, dumpbase_len
, "%sltrans%u.ltrans", dumppfx
, i
);
2128 argv_ptr
[0] = dumpbase
;
2130 argv_ptr
[1] = "-fltrans";
2132 argv_ptr
[3] = output_name
;
2133 argv_ptr
[4] = input_name
;
2137 fprintf (mstream
, "%s:\n\t@%s ", output_name
, new_argv
[0]);
2138 for (j
= 1; new_argv
[j
] != NULL
; ++j
)
2139 fprintf (mstream
, " '%s'", new_argv
[j
]);
2140 /* If we are not preserving the ltrans input files then
2141 truncate them as soon as we have processed it. This
2142 reduces temporary disk-space usage. */
2143 if (!ltrans_cache
&& !save_temps
)
2144 fprintf (mstream
, " -truncate '%s'", input_name
);
2145 fprintf (mstream
, "\n");
2149 char argsuffix
[sizeof (DUMPBASE_SUFFIX
)
2150 + sizeof (".ltrans_args") + 1];
2152 snprintf (argsuffix
,
2153 sizeof (DUMPBASE_SUFFIX
) + sizeof (".ltrans_args"),
2154 "ltrans%u.ltrans_args", i
);
2155 fork_execute (new_argv
[0], CONST_CAST (char **, new_argv
),
2156 true, save_temps
? argsuffix
: NULL
);
2158 maybe_unlink (input_names
[i
]);
2161 output_names
[i
] = output_name
;
2165 struct pex_obj
*pex
;
2171 for (i
= 0; i
< nr
; ++i
)
2173 int j
= ltrans_priorities
[i
*2 + 1];
2174 fprintf (mstream
, " \\\n\t%s", output_names
[j
]);
2176 fprintf (mstream
, "\n");
2180 /* Avoid passing --jobserver-fd= and similar flags
2181 unless jobserver mode is explicitly enabled. */
2182 putenv (xstrdup ("MAKEFLAGS="));
2183 putenv (xstrdup ("MFLAGS="));
2186 char **make_argv
= buildargv (getenv ("MAKE"));
2189 for (unsigned argc
= 0; make_argv
[argc
]; argc
++)
2190 obstack_ptr_grow (&argv_obstack
, make_argv
[argc
]);
2193 obstack_ptr_grow (&argv_obstack
, "make");
2195 obstack_ptr_grow (&argv_obstack
, "-f");
2196 obstack_ptr_grow (&argv_obstack
, makefile
);
2199 snprintf (jobs
, 31, "-j%ld",
2200 auto_parallel
? nthreads_var
: parallel
);
2201 obstack_ptr_grow (&argv_obstack
, jobs
);
2203 obstack_ptr_grow (&argv_obstack
, "all");
2204 obstack_ptr_grow (&argv_obstack
, NULL
);
2205 new_argv
= XOBFINISH (&argv_obstack
, const char **);
2207 pex
= collect_execute (new_argv
[0], CONST_CAST (char **, new_argv
),
2208 NULL
, NULL
, PEX_SEARCH
, false, NULL
);
2209 do_wait (new_argv
[0], pex
);
2210 freeargv (make_argv
);
2211 maybe_unlink (makefile
);
2215 for (i
= 0; i
< nr
; ++i
)
2216 maybe_unlink (input_names
[i
]);
2221 for (i
= 0; i
< nr
; ++i
)
2223 ltrans_file_cache::item
* item
;
2224 item
= ltrans_cache
.get_item (input_names
[i
]);
2228 /* Ensure LTRANS for this item finished. */
2229 item
->lock
.lock_read ();
2230 item
->lock
.unlock ();
2234 ltrans_cache
.deletion_lock
.unlock ();
2237 for (i
= 0; i
< nr
; ++i
)
2239 fputs (output_names
[i
], stdout
);
2240 putc ('\n', stdout
);
2241 free (input_names
[i
]);
2244 if (ltrans_cache
&& !save_temps
)
2245 ltrans_cache
.try_prune ();
2249 for (i
= 0; i
< ltoobj_argc
; ++i
)
2250 if (early_debug_object_names
[i
] != NULL
)
2251 printf ("%s\n", early_debug_object_names
[i
]);
2254 free (ltrans_priorities
);
2255 free (output_names
);
2256 output_names
= NULL
;
2257 free (early_debug_object_names
);
2258 early_debug_object_names
= NULL
;
2260 free (list_option_full
);
2261 obstack_free (&env_obstack
, NULL
);
2266 obstack_free (&argv_obstack
, NULL
);
2269 /* Concrete implementation of diagnostic_option_manager for LTO. */
2271 class lto_diagnostic_option_manager
: public gcc_diagnostic_option_manager
2274 lto_diagnostic_option_manager ()
2275 : gcc_diagnostic_option_manager (0 /* lang_mask */)
2278 int option_enabled_p (diagnostic_option_id
) const final override
2282 char *make_option_name (diagnostic_option_id
,
2284 diagnostic_t
) const final override
2293 main (int argc
, char *argv
[])
2297 init_opts_obstack ();
2299 p
= argv
[0] + strlen (argv
[0]);
2300 while (p
!= argv
[0] && !IS_DIR_SEPARATOR (p
[-1]))
2304 xmalloc_set_program_name (progname
);
2306 gcc_init_libintl ();
2308 diagnostic_initialize (global_dc
, 0);
2309 diagnostic_color_init (global_dc
);
2310 diagnostic_urls_init (global_dc
);
2311 global_dc
->set_option_manager
2312 (::make_unique
<lto_diagnostic_option_manager
> (), 0);
2314 if (atexit (lto_wrapper_cleanup
) != 0)
2315 fatal_error (input_location
, "%<atexit%> failed");
2319 /* We may be called with all the arguments stored in some file and
2320 passed with @file. Expand them into argv before processing. */
2321 expandargv (&argc
, &argv
);
2323 run_gcc (argc
, argv
);