1 /* Wrapper to call lto. Used by collect2 and the linker plugin.
2 Copyright (C) 2009-2020 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
42 #include "coretypes.h"
44 #include "diagnostic.h"
48 #include "simple-object.h"
49 #include "lto-section-names.h"
50 #include "collect-utils.h"
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"
57 LTO_MODE_NONE
, /* Not doing LTO. */
58 LTO_MODE_LTO
, /* Normal LTO. */
59 LTO_MODE_WHOPR
/* WHOPR. */
62 /* Current LTO mode. */
63 static enum lto_mode_d lto_mode
= LTO_MODE_NONE
;
65 static char *ltrans_output_file
;
66 static char *flto_out
;
67 static unsigned int nr
;
68 static int *ltrans_priorities
;
69 static char **input_names
;
70 static char **output_names
;
71 static char **offload_names
;
72 static char *offload_objects_file_name
;
73 static char *makefile
;
74 static unsigned int num_deb_objs
;
75 static const char **early_debug_object_names
;
76 static bool xassembler_options_error
= false;
78 const char tool_name
[] = "lto-wrapper";
80 /* Delete tempfiles. Called from utils_cleanup. */
87 if (ltrans_output_file
)
88 maybe_unlink (ltrans_output_file
);
90 maybe_unlink (flto_out
);
91 if (offload_objects_file_name
)
92 maybe_unlink (offload_objects_file_name
);
94 maybe_unlink (makefile
);
95 if (early_debug_object_names
)
96 for (i
= 0; i
< num_deb_objs
; ++i
)
97 if (early_debug_object_names
[i
])
98 maybe_unlink (early_debug_object_names
[i
]);
99 for (i
= 0; i
< nr
; ++i
)
101 maybe_unlink (input_names
[i
]);
103 maybe_unlink (output_names
[i
]);
108 lto_wrapper_cleanup (void)
110 utils_cleanup (false);
113 /* Unlink a temporary LTRANS file unless requested otherwise. */
116 maybe_unlink (const char *file
)
120 if (unlink_if_ordinary (file
)
122 fatal_error (input_location
, "deleting LTRANS file %s: %m", file
);
125 fprintf (stderr
, "[Leaving LTRANS %s]\n", file
);
128 /* Template of LTRANS dumpbase suffix. */
129 #define DUMPBASE_SUFFIX ".ltrans18446744073709551615"
131 /* Create decoded options from the COLLECT_GCC and COLLECT_GCC_OPTIONS
135 get_options_from_collect_gcc_options (const char *collect_gcc
,
136 const char *collect_gcc_options
,
137 struct cl_decoded_option
**decoded_options
,
138 unsigned int *decoded_options_count
)
140 struct obstack argv_obstack
;
144 obstack_init (&argv_obstack
);
145 obstack_ptr_grow (&argv_obstack
, collect_gcc
);
147 parse_options_from_collect_gcc_options (collect_gcc_options
,
148 &argv_obstack
, &argc
);
149 argv
= XOBFINISH (&argv_obstack
, const char **);
151 decode_cmdline_options_to_array (argc
, (const char **)argv
, CL_DRIVER
,
152 decoded_options
, decoded_options_count
);
153 obstack_free (&argv_obstack
, NULL
);
156 /* Append OPTION to the options array DECODED_OPTIONS with size
157 DECODED_OPTIONS_COUNT. */
160 append_option (struct cl_decoded_option
**decoded_options
,
161 unsigned int *decoded_options_count
,
162 struct cl_decoded_option
*option
)
164 ++*decoded_options_count
;
166 = (struct cl_decoded_option
*)
167 xrealloc (*decoded_options
,
168 (*decoded_options_count
169 * sizeof (struct cl_decoded_option
)));
170 memcpy (&(*decoded_options
)[*decoded_options_count
- 1], option
,
171 sizeof (struct cl_decoded_option
));
174 /* Remove option number INDEX from DECODED_OPTIONS, update
175 DECODED_OPTIONS_COUNT. */
178 remove_option (struct cl_decoded_option
**decoded_options
,
179 int index
, unsigned int *decoded_options_count
)
181 --*decoded_options_count
;
182 memmove (&(*decoded_options
)[index
+ 1],
183 &(*decoded_options
)[index
],
184 sizeof (struct cl_decoded_option
)
185 * (*decoded_options_count
- index
));
188 /* Try to merge and complain about options FDECODED_OPTIONS when applied
189 ontop of DECODED_OPTIONS. */
192 merge_and_complain (struct cl_decoded_option
**decoded_options
,
193 unsigned int *decoded_options_count
,
194 struct cl_decoded_option
*fdecoded_options
,
195 unsigned int fdecoded_options_count
)
198 struct cl_decoded_option
*pic_option
= NULL
;
199 struct cl_decoded_option
*pie_option
= NULL
;
201 /* ??? Merge options from files. Most cases can be
202 handled by either unioning or intersecting
203 (for example -fwrapv is a case for unioning,
204 -ffast-math is for intersection). Most complaints
205 about real conflicts between different options can
206 be deferred to the compiler proper. Options that
207 we can neither safely handle by intersection nor
208 unioning would need to be complained about here.
209 Ideally we'd have a flag in the opt files that
210 tells whether to union or intersect or reject.
211 In absence of that it's unclear what a good default is.
212 It's also difficult to get positional handling correct. */
214 /* The following does what the old LTO option code did,
215 union all target and a selected set of common options. */
216 for (i
= 0; i
< fdecoded_options_count
; ++i
)
218 struct cl_decoded_option
*foption
= &fdecoded_options
[i
];
219 switch (foption
->opt_index
)
221 case OPT_SPECIAL_unknown
:
222 case OPT_SPECIAL_ignore
:
223 case OPT_SPECIAL_warn_removed
:
224 case OPT_SPECIAL_program_name
:
225 case OPT_SPECIAL_input_file
:
229 if (!(cl_options
[foption
->opt_index
].flags
& CL_TARGET
))
233 case OPT_fdiagnostics_show_caret
:
234 case OPT_fdiagnostics_show_labels
:
235 case OPT_fdiagnostics_show_line_numbers
:
236 case OPT_fdiagnostics_show_option
:
237 case OPT_fdiagnostics_show_location_
:
238 case OPT_fshow_column
:
242 /* Do what the old LTO code did - collect exactly one option
243 setting per OPT code, we pick the first we encounter.
244 ??? This doesn't make too much sense, but when it doesn't
245 then we should complain. */
246 for (j
= 0; j
< *decoded_options_count
; ++j
)
247 if ((*decoded_options
)[j
].opt_index
== foption
->opt_index
)
249 if (j
== *decoded_options_count
)
250 append_option (decoded_options
, decoded_options_count
, foption
);
253 /* Figure out what PIC/PIE level wins and merge the results. */
256 pic_option
= foption
;
260 pie_option
= foption
;
265 /* For selected options we can merge conservatively. */
266 for (j
= 0; j
< *decoded_options_count
; ++j
)
267 if ((*decoded_options
)[j
].opt_index
== foption
->opt_index
)
269 if (j
== *decoded_options_count
)
270 append_option (decoded_options
, decoded_options_count
, foption
);
271 /* -fopenmp > -fno-openmp,
272 -fopenacc > -fno-openacc */
273 else if (foption
->value
> (*decoded_options
)[j
].value
)
274 (*decoded_options
)[j
] = *foption
;
277 case OPT_fopenacc_dim_
:
278 /* Append or check identical. */
279 for (j
= 0; j
< *decoded_options_count
; ++j
)
280 if ((*decoded_options
)[j
].opt_index
== foption
->opt_index
)
282 if (j
== *decoded_options_count
)
283 append_option (decoded_options
, decoded_options_count
, foption
);
284 else if (strcmp ((*decoded_options
)[j
].arg
, foption
->arg
))
285 fatal_error (input_location
,
286 "option %s with different values",
287 foption
->orig_option_with_args_text
);
294 for (j
= 0; j
< *decoded_options_count
; ++j
)
295 if ((*decoded_options
)[j
].opt_index
== OPT_O
296 || (*decoded_options
)[j
].opt_index
== OPT_Ofast
297 || (*decoded_options
)[j
].opt_index
== OPT_Og
298 || (*decoded_options
)[j
].opt_index
== OPT_Os
)
300 if (j
== *decoded_options_count
)
301 append_option (decoded_options
, decoded_options_count
, foption
);
302 else if ((*decoded_options
)[j
].opt_index
== foption
->opt_index
303 && foption
->opt_index
!= OPT_O
)
304 /* Exact same options get merged. */
308 /* For mismatched option kinds preserve the optimization
309 level only, thus merge it as -On. This also handles
310 merging of same optimization level -On. */
312 switch (foption
->opt_index
)
315 if (foption
->arg
[0] == '\0')
316 level
= MAX (level
, 1);
318 level
= MAX (level
, atoi (foption
->arg
));
321 level
= MAX (level
, 3);
324 level
= MAX (level
, 1);
327 level
= MAX (level
, 2);
332 switch ((*decoded_options
)[j
].opt_index
)
335 if ((*decoded_options
)[j
].arg
[0] == '\0')
336 level
= MAX (level
, 1);
338 level
= MAX (level
, atoi ((*decoded_options
)[j
].arg
));
341 level
= MAX (level
, 3);
344 level
= MAX (level
, 1);
347 level
= MAX (level
, 2);
352 (*decoded_options
)[j
].opt_index
= OPT_O
;
354 tem
= xasprintf ("-O%d", level
);
355 (*decoded_options
)[j
].arg
= &tem
[2];
356 (*decoded_options
)[j
].canonical_option
[0] = tem
;
357 (*decoded_options
)[j
].value
= 1;
362 case OPT_foffload_abi_
:
363 for (j
= 0; j
< *decoded_options_count
; ++j
)
364 if ((*decoded_options
)[j
].opt_index
== foption
->opt_index
)
366 if (j
== *decoded_options_count
)
367 append_option (decoded_options
, decoded_options_count
, foption
);
368 else if (foption
->value
!= (*decoded_options
)[j
].value
)
369 fatal_error (input_location
,
370 "option %s not used consistently in all LTO input"
371 " files", foption
->orig_option_with_args_text
);
376 append_option (decoded_options
, decoded_options_count
, foption
);
381 /* Merge PIC options:
382 -fPIC + -fpic = -fpic
383 -fPIC + -fno-pic = -fno-pic
384 -fpic/-fPIC + nothing = nothing.
385 It is a common mistake to mix few -fPIC compiled objects into otherwise
386 non-PIC code. We do not want to build everything with PIC then.
388 Similarly we merge PIE options, however in addition we keep
389 -fPIC + -fPIE = -fPIE
390 -fpic + -fPIE = -fpie
391 -fPIC/-fpic + -fpie = -fpie
393 It would be good to warn on mismatches, but it is bit hard to do as
394 we do not know what nothing translates to. */
396 for (unsigned int j
= 0; j
< *decoded_options_count
;)
397 if ((*decoded_options
)[j
].opt_index
== OPT_fPIC
398 || (*decoded_options
)[j
].opt_index
== OPT_fpic
)
400 /* -fno-pic in one unit implies -fno-pic everywhere. */
401 if ((*decoded_options
)[j
].value
== 0)
403 /* If we have no pic option or merge in -fno-pic, we still may turn
404 existing pic/PIC mode into pie/PIE if -fpie/-fPIE is present. */
405 else if ((pic_option
&& pic_option
->value
== 0)
410 bool big
= (*decoded_options
)[j
].opt_index
== OPT_fPIC
411 && pie_option
->opt_index
== OPT_fPIE
;
412 (*decoded_options
)[j
].opt_index
= big
? OPT_fPIE
: OPT_fpie
;
413 if (pie_option
->value
)
414 (*decoded_options
)[j
].canonical_option
[0]
415 = big
? "-fPIE" : "-fpie";
417 (*decoded_options
)[j
].canonical_option
[0] = "-fno-pie";
418 (*decoded_options
)[j
].value
= pie_option
->value
;
423 (*decoded_options
)[j
] = *pic_option
;
426 /* We do not know if target defaults to pic or not, so just remove
427 option if it is missing in one unit but enabled in other. */
429 remove_option (decoded_options
, j
, decoded_options_count
);
431 else if (pic_option
->opt_index
== OPT_fpic
432 && (*decoded_options
)[j
].opt_index
== OPT_fPIC
)
434 (*decoded_options
)[j
] = *pic_option
;
440 else if ((*decoded_options
)[j
].opt_index
== OPT_fPIE
441 || (*decoded_options
)[j
].opt_index
== OPT_fpie
)
443 /* -fno-pie in one unit implies -fno-pie everywhere. */
444 if ((*decoded_options
)[j
].value
== 0)
446 /* If we have no pie option or merge in -fno-pie, we still preserve
447 PIE/pie if pic/PIC is present. */
448 else if ((pie_option
&& pie_option
->value
== 0)
451 /* If -fPIC/-fpic is given, merge it with -fPIE/-fpie. */
454 if (pic_option
->opt_index
== OPT_fpic
455 && (*decoded_options
)[j
].opt_index
== OPT_fPIE
)
457 (*decoded_options
)[j
].opt_index
= OPT_fpie
;
458 (*decoded_options
)[j
].canonical_option
[0]
459 = pic_option
->value
? "-fpie" : "-fno-pie";
461 else if (!pic_option
->value
)
462 (*decoded_options
)[j
].canonical_option
[0] = "-fno-pie";
463 (*decoded_options
)[j
].value
= pic_option
->value
;
468 (*decoded_options
)[j
] = *pie_option
;
471 /* Because we always append pic/PIE options this code path should
472 not happen unless the LTO object was built by old lto1 which
473 did not contain that logic yet. */
475 remove_option (decoded_options
, j
, decoded_options_count
);
477 else if (pie_option
->opt_index
== OPT_fpie
478 && (*decoded_options
)[j
].opt_index
== OPT_fPIE
)
480 (*decoded_options
)[j
] = *pie_option
;
489 if (!xassembler_options_error
)
490 for (i
= j
= 0; ; i
++, j
++)
492 for (; i
< *decoded_options_count
; i
++)
493 if ((*decoded_options
)[i
].opt_index
== OPT_Xassembler
)
496 for (; j
< fdecoded_options_count
; j
++)
497 if (fdecoded_options
[j
].opt_index
== OPT_Xassembler
)
500 if (i
== *decoded_options_count
&& j
== fdecoded_options_count
)
502 else if (i
< *decoded_options_count
&& j
== fdecoded_options_count
)
504 warning (0, "Extra option to -Xassembler: %s,"
505 " dropping all -Xassembler and -Wa options.",
506 (*decoded_options
)[i
].arg
);
507 xassembler_options_error
= true;
510 else if (i
== *decoded_options_count
&& j
< fdecoded_options_count
)
512 warning (0, "Extra option to -Xassembler: %s,"
513 " dropping all -Xassembler and -Wa options.",
514 fdecoded_options
[j
].arg
);
515 xassembler_options_error
= true;
518 else if (strcmp ((*decoded_options
)[i
].arg
, fdecoded_options
[j
].arg
))
520 warning (0, "Options to Xassembler do not match: %s, %s,"
521 " dropping all -Xassembler and -Wa options.",
522 (*decoded_options
)[i
].arg
, fdecoded_options
[j
].arg
);
523 xassembler_options_error
= true;
529 /* Auxiliary function that frees elements of PTR and PTR itself.
530 N is number of elements to be freed. If PTR is NULL, nothing is freed.
531 If an element is NULL, subsequent elements are not freed. */
534 free_array_of_ptrs (void **ptr
, unsigned n
)
538 for (unsigned i
= 0; i
< n
; i
++)
548 /* Parse STR, saving found tokens into PVALUES and return their number.
549 Tokens are assumed to be delimited by ':'. If APPEND is non-null,
550 append it to every token we find. */
553 parse_env_var (const char *str
, char ***pvalues
, const char *append
)
555 const char *curval
, *nextval
;
559 curval
= strchr (str
, ':');
563 curval
= strchr (curval
+ 1, ':');
566 values
= (char**) xmalloc (num
* sizeof (char*));
568 nextval
= strchr (curval
, ':');
570 nextval
= strchr (curval
, '\0');
572 int append_len
= append
? strlen (append
) : 0;
573 for (i
= 0; i
< num
; i
++)
575 int l
= nextval
- curval
;
576 values
[i
] = (char*) xmalloc (l
+ 1 + append_len
);
577 memcpy (values
[i
], curval
, l
);
580 strcat (values
[i
], append
);
581 curval
= nextval
+ 1;
582 nextval
= strchr (curval
, ':');
584 nextval
= strchr (curval
, '\0');
590 /* Append options OPTS from lto or offload_lto sections to ARGV_OBSTACK. */
593 append_compiler_options (obstack
*argv_obstack
, struct cl_decoded_option
*opts
,
596 /* Append compiler driver arguments as far as they were merged. */
597 for (unsigned int j
= 1; j
< count
; ++j
)
599 struct cl_decoded_option
*option
= &opts
[j
];
601 /* File options have been properly filtered by lto-opts.c. */
602 switch (option
->opt_index
)
604 /* Drop arguments that we want to take from the link line. */
607 case OPT_flto_partition_
:
614 /* For now do what the original LTO option code was doing - pass
615 on any CL_TARGET flag and a few selected others. */
616 switch (option
->opt_index
)
618 case OPT_fdiagnostics_show_caret
:
619 case OPT_fdiagnostics_show_labels
:
620 case OPT_fdiagnostics_show_line_numbers
:
621 case OPT_fdiagnostics_show_option
:
622 case OPT_fdiagnostics_show_location_
:
623 case OPT_fshow_column
:
632 case OPT_fopenacc_dim_
:
633 case OPT_foffload_abi_
:
642 /* When we detected a mismatch in assembler options between
643 the input TU's fall back to previous behavior of ignoring them. */
644 if (xassembler_options_error
)
649 if (!(cl_options
[option
->opt_index
].flags
& CL_TARGET
))
653 /* Pass the option on. */
654 for (unsigned int i
= 0; i
< option
->canonical_option_num_elements
; ++i
)
655 obstack_ptr_grow (argv_obstack
, option
->canonical_option
[i
]);
659 /* Append diag options in OPTS with length COUNT to ARGV_OBSTACK. */
662 append_diag_options (obstack
*argv_obstack
, struct cl_decoded_option
*opts
,
665 /* Append compiler driver arguments as far as they were merged. */
666 for (unsigned int j
= 1; j
< count
; ++j
)
668 struct cl_decoded_option
*option
= &opts
[j
];
670 switch (option
->opt_index
)
672 case OPT_fdiagnostics_color_
:
673 case OPT_fdiagnostics_format_
:
674 case OPT_fdiagnostics_show_caret
:
675 case OPT_fdiagnostics_show_labels
:
676 case OPT_fdiagnostics_show_line_numbers
:
677 case OPT_fdiagnostics_show_option
:
678 case OPT_fdiagnostics_show_location_
:
679 case OPT_fshow_column
:
685 /* Pass the option on. */
686 for (unsigned int i
= 0; i
< option
->canonical_option_num_elements
; ++i
)
687 obstack_ptr_grow (argv_obstack
, option
->canonical_option
[i
]);
692 /* Append linker options OPTS to ARGV_OBSTACK. */
695 append_linker_options (obstack
*argv_obstack
, struct cl_decoded_option
*opts
,
698 /* Append linker driver arguments. Compiler options from the linker
699 driver arguments will override / merge with those from the compiler. */
700 for (unsigned int j
= 1; j
< count
; ++j
)
702 struct cl_decoded_option
*option
= &opts
[j
];
704 /* Do not pass on frontend specific flags not suitable for lto. */
705 if (!(cl_options
[option
->opt_index
].flags
706 & (CL_COMMON
|CL_TARGET
|CL_DRIVER
|CL_LTO
)))
709 switch (option
->opt_index
)
714 /* We've handled these LTO options, do not pass them on. */
719 /* Ignore -fno-XXX form of these options, as otherwise
720 corresponding builtins will not be enabled. */
721 if (option
->value
== 0)
729 /* Pass the option on. */
730 for (unsigned int i
= 0; i
< option
->canonical_option_num_elements
; ++i
)
731 obstack_ptr_grow (argv_obstack
, option
->canonical_option
[i
]);
735 /* Extract options for TARGET offload compiler from OPTIONS and append
736 them to ARGV_OBSTACK. */
739 append_offload_options (obstack
*argv_obstack
, const char *target
,
740 struct cl_decoded_option
*options
,
741 unsigned int options_count
)
743 for (unsigned i
= 0; i
< options_count
; i
++)
745 const char *cur
, *next
, *opts
;
748 struct cl_decoded_option
*option
= &options
[i
];
750 if (option
->opt_index
!= OPT_foffload_
)
753 /* If option argument starts with '-' then no target is specified. That
754 means offload options are specified for all targets, so we need to
756 if (option
->arg
[0] == '-')
760 opts
= strchr (option
->arg
, '=');
761 /* If there are offload targets specified, but no actual options,
762 there is nothing to do here. */
770 next
= strchr (cur
, ',');
773 next
= (next
> opts
) ? opts
: next
;
775 /* Are we looking for this offload target? */
776 if (strlen (target
) == (size_t) (next
- cur
)
777 && strncmp (target
, cur
, next
- cur
) == 0)
780 /* Skip the comma or equal sign. */
790 argv
= buildargv (opts
);
791 for (argc
= 0; argv
[argc
]; argc
++)
792 obstack_ptr_grow (argv_obstack
, argv
[argc
]);
796 /* Check whether NAME can be accessed in MODE. This is like access,
797 except that it never considers directories to be executable. */
800 access_check (const char *name
, int mode
)
806 if (stat (name
, &st
) < 0
807 || S_ISDIR (st
.st_mode
))
811 return access (name
, mode
);
814 /* Prepare a target image for offload TARGET, using mkoffload tool from
815 COMPILER_PATH. Return the name of the resultant object file. */
818 compile_offload_image (const char *target
, const char *compiler_path
,
819 unsigned in_argc
, char *in_argv
[],
820 struct cl_decoded_option
*compiler_opts
,
821 unsigned int compiler_opt_count
,
822 struct cl_decoded_option
*linker_opts
,
823 unsigned int linker_opt_count
)
825 char *filename
= NULL
;
828 = XALLOCAVEC (char, sizeof ("/accel//mkoffload") + strlen (target
));
829 strcpy (suffix
, "/accel/");
830 strcat (suffix
, target
);
831 strcat (suffix
, "/mkoffload");
834 unsigned n_paths
= parse_env_var (compiler_path
, &paths
, suffix
);
836 const char *compiler
= NULL
;
837 for (unsigned i
= 0; i
< n_paths
; i
++)
838 if (access_check (paths
[i
], X_OK
) == 0)
845 fatal_error (input_location
,
846 "could not find %s in %s (consider using %<-B%>)",
847 suffix
+ 1, compiler_path
);
849 /* Generate temporary output file name. */
850 filename
= make_temp_file (".target.o");
852 struct obstack argv_obstack
;
853 obstack_init (&argv_obstack
);
854 obstack_ptr_grow (&argv_obstack
, compiler
);
856 obstack_ptr_grow (&argv_obstack
, "-save-temps");
858 obstack_ptr_grow (&argv_obstack
, "-v");
859 obstack_ptr_grow (&argv_obstack
, "-o");
860 obstack_ptr_grow (&argv_obstack
, filename
);
862 /* Append names of input object files. */
863 for (unsigned i
= 0; i
< in_argc
; i
++)
864 obstack_ptr_grow (&argv_obstack
, in_argv
[i
]);
866 /* Append options from offload_lto sections. */
867 append_compiler_options (&argv_obstack
, compiler_opts
,
869 append_diag_options (&argv_obstack
, linker_opts
, linker_opt_count
);
871 /* Append options specified by -foffload last. In case of conflicting
872 options we expect offload compiler to choose the latest. */
873 append_offload_options (&argv_obstack
, target
, compiler_opts
,
875 append_offload_options (&argv_obstack
, target
, linker_opts
,
878 obstack_ptr_grow (&argv_obstack
, NULL
);
879 argv
= XOBFINISH (&argv_obstack
, char **);
880 fork_execute (argv
[0], argv
, true);
881 obstack_free (&argv_obstack
, NULL
);
883 free_array_of_ptrs ((void **) paths
, n_paths
);
888 /* The main routine dealing with offloading.
889 The routine builds a target image for each offload target. IN_ARGC and
890 IN_ARGV specify options and input object files. As all of them could contain
891 target sections, we pass them all to target compilers. */
894 compile_images_for_offload_targets (unsigned in_argc
, char *in_argv
[],
895 struct cl_decoded_option
*compiler_opts
,
896 unsigned int compiler_opt_count
,
897 struct cl_decoded_option
*linker_opts
,
898 unsigned int linker_opt_count
)
901 const char *target_names
= getenv (OFFLOAD_TARGET_NAMES_ENV
);
904 unsigned num_targets
= parse_env_var (target_names
, &names
, NULL
);
906 int next_name_entry
= 0;
907 const char *compiler_path
= getenv ("COMPILER_PATH");
911 /* Prepare an image for each target and save the name of the resultant object
912 file to the OFFLOAD_NAMES array. It is terminated by a NULL entry. */
913 offload_names
= XCNEWVEC (char *, num_targets
+ 1);
914 for (unsigned i
= 0; i
< num_targets
; i
++)
916 /* HSA does not use LTO-like streaming and a different compiler, skip
918 if (strcmp (names
[i
], "hsa") == 0)
921 offload_names
[next_name_entry
]
922 = compile_offload_image (names
[i
], compiler_path
, in_argc
, in_argv
,
923 compiler_opts
, compiler_opt_count
,
924 linker_opts
, linker_opt_count
);
925 if (!offload_names
[next_name_entry
])
926 fatal_error (input_location
,
927 "problem with building target image for %s", names
[i
]);
932 free_array_of_ptrs ((void **) names
, num_targets
);
935 /* Copy a file from SRC to DEST. */
938 copy_file (const char *dest
, const char *src
)
940 FILE *d
= fopen (dest
, "wb");
941 FILE *s
= fopen (src
, "rb");
945 size_t len
= fread (buffer
, 1, 512, s
);
947 fatal_error (input_location
, "reading input file");
950 fwrite (buffer
, 1, len
, d
);
952 fatal_error (input_location
, "writing output file");
959 /* Find the crtoffloadtable.o file in LIBRARY_PATH, make copy and pass name of
960 the copy to the linker. */
963 find_crtoffloadtable (void)
966 const char *library_path
= getenv ("LIBRARY_PATH");
969 unsigned n_paths
= parse_env_var (library_path
, &paths
, "/crtoffloadtable.o");
972 for (i
= 0; i
< n_paths
; i
++)
973 if (access_check (paths
[i
], R_OK
) == 0)
975 /* The linker will delete the filename we give it, so make a copy. */
976 char *crtoffloadtable
= make_temp_file (".crtoffloadtable.o");
977 copy_file (crtoffloadtable
, paths
[i
]);
978 printf ("%s\n", crtoffloadtable
);
979 XDELETEVEC (crtoffloadtable
);
983 fatal_error (input_location
,
984 "installation error, cannot find %<crtoffloadtable.o%>");
986 free_array_of_ptrs ((void **) paths
, n_paths
);
989 /* A subroutine of run_gcc. Examine the open file FD for lto sections with
990 name prefix PREFIX, at FILE_OFFSET, and store any options we find in OPTS
991 and OPT_COUNT. Return true if we found a matchingn section, false
992 otherwise. COLLECT_GCC holds the value of the environment variable with
996 find_and_merge_options (int fd
, off_t file_offset
, const char *prefix
,
997 struct cl_decoded_option
**opts
,
998 unsigned int *opt_count
, const char *collect_gcc
)
1000 off_t offset
, length
;
1005 struct cl_decoded_option
*fdecoded_options
= *opts
;
1006 unsigned int fdecoded_options_count
= *opt_count
;
1008 simple_object_read
*sobj
;
1009 sobj
= simple_object_start_read (fd
, file_offset
, "__GNU_LTO",
1014 char *secname
= XALLOCAVEC (char, strlen (prefix
) + sizeof (".opts"));
1015 strcpy (secname
, prefix
);
1016 strcat (secname
, ".opts");
1017 if (!simple_object_find_section (sobj
, secname
, &offset
, &length
,
1020 simple_object_release_read (sobj
);
1024 lseek (fd
, file_offset
+ offset
, SEEK_SET
);
1025 data
= (char *)xmalloc (length
);
1026 read (fd
, data
, length
);
1030 struct cl_decoded_option
*f2decoded_options
;
1031 unsigned int f2decoded_options_count
;
1032 get_options_from_collect_gcc_options (collect_gcc
, fopts
,
1034 &f2decoded_options_count
);
1035 if (!fdecoded_options
)
1037 fdecoded_options
= f2decoded_options
;
1038 fdecoded_options_count
= f2decoded_options_count
;
1041 merge_and_complain (&fdecoded_options
,
1042 &fdecoded_options_count
,
1043 f2decoded_options
, f2decoded_options_count
);
1045 fopts
+= strlen (fopts
) + 1;
1047 while (fopts
- data
< length
);
1050 simple_object_release_read (sobj
);
1051 *opts
= fdecoded_options
;
1052 *opt_count
= fdecoded_options_count
;
1056 /* Copy early debug info sections from INFILE to a new file whose name
1057 is returned. Return NULL on error. */
1060 debug_objcopy (const char *infile
, bool rename
)
1067 const char *orig_infile
= infile
;
1071 if ((p
= strrchr (infile
, '@'))
1073 && sscanf (p
, "@%li%n", &loffset
, &consumed
) >= 1
1074 && strlen (p
) == (unsigned int) consumed
)
1076 char *fname
= xstrdup (infile
);
1077 fname
[p
- infile
] = '\0';
1079 inoff
= (off_t
) loffset
;
1081 int infd
= open (infile
, O_RDONLY
| O_BINARY
);
1084 simple_object_read
*inobj
= simple_object_start_read (infd
, inoff
,
1091 if (simple_object_find_section (inobj
, ".gnu.debuglto_.debug_info",
1092 &off
, &len
, &errmsg
, &err
) != 1)
1095 fatal_error (0, "%s: %s", errmsg
, xstrerror (err
));
1097 simple_object_release_read (inobj
);
1104 outfile
= (char *) xmalloc (strlen (orig_infile
)
1105 + sizeof (".debug.temp.o") + 1);
1106 strcpy (outfile
, orig_infile
);
1107 strcat (outfile
, ".debug.temp.o");
1110 outfile
= make_temp_file (".debug.temp.o");
1111 errmsg
= simple_object_copy_lto_debug_sections (inobj
, outfile
, &err
, rename
);
1114 unlink_if_ordinary (outfile
);
1115 fatal_error (0, "%s: %s", errmsg
, xstrerror (err
));
1118 simple_object_release_read (inobj
);
1124 /* Helper for qsort: compare priorities for parallel compilation. */
1127 cmp_priority (const void *a
, const void *b
)
1129 return *((const int *)b
)-*((const int *)a
);
1132 /* Number of CPUs that can be used for parallel LTRANS phase. */
1134 static unsigned long nthreads_var
= 0;
1136 #ifdef HAVE_PTHREAD_AFFINITY_NP
1137 unsigned long cpuset_size
;
1138 static unsigned long get_cpuset_size
;
1142 static cpuset_popcount (unsigned long cpusetsize
, cpu_set_t
*cpusetp
)
1145 /* glibc 2.7 and above provide a macro for this. */
1146 return CPU_COUNT_S (cpusetsize
, cpusetp
);
1149 if (cpusetsize
== sizeof (cpu_set_t
))
1150 /* glibc 2.6 and above provide a macro for this. */
1151 return CPU_COUNT (cpusetp
);
1154 unsigned long ret
= 0;
1155 STATIC_ASSERT (sizeof (cpusetp
->__bits
[0]) == sizeof (unsigned long int));
1156 for (i
= 0; i
< cpusetsize
/ sizeof (cpusetp
->__bits
[0]); i
++)
1158 unsigned long int mask
= cpusetp
->__bits
[i
];
1161 ret
+= __builtin_popcountl (mask
);
1168 /* At startup, determine the default number of threads. It would seem
1169 this should be related to the number of cpus online. */
1172 init_num_threads (void)
1174 #ifdef HAVE_PTHREAD_AFFINITY_NP
1175 #if defined (_SC_NPROCESSORS_CONF) && defined (CPU_ALLOC_SIZE)
1176 cpuset_size
= sysconf (_SC_NPROCESSORS_CONF
);
1177 cpuset_size
= CPU_ALLOC_SIZE (cpuset_size
);
1179 cpuset_size
= sizeof (cpu_set_t
);
1182 cpusetp
= (cpu_set_t
*) xmalloc (gomp_cpuset_size
);
1185 int ret
= pthread_getaffinity_np (pthread_self (), gomp_cpuset_size
,
1189 /* Count only the CPUs this process can use. */
1190 nthreads_var
= cpuset_popcount (cpuset_size
, cpusetp
);
1191 if (nthreads_var
== 0)
1193 get_cpuset_size
= cpuset_size
;
1194 #ifdef CPU_ALLOC_SIZE
1196 for (i
= cpuset_size
* 8; i
; i
--)
1197 if (CPU_ISSET_S (i
- 1, cpuset_size
, cpusetp
))
1199 cpuset_size
= CPU_ALLOC_SIZE (i
);
1205 #ifdef CPU_ALLOC_SIZE
1206 if (cpuset_size
< sizeof (cpu_set_t
))
1207 cpuset_size
= sizeof (cpu_set_t
);
1209 cpuset_size
= cpuset_size
* 2;
1210 if (cpuset_size
< 8 * sizeof (cpu_set_t
))
1212 = (cpu_set_t
*) realloc (cpusetp
, cpuset_size
);
1215 /* Avoid fatal if too large memory allocation would be
1216 requested, e.g. kernel returning EINVAL all the time. */
1217 void *p
= realloc (cpusetp
, cpuset_size
);
1220 cpusetp
= (cpu_set_t
*) p
;
1232 #ifdef _SC_NPROCESSORS_ONLN
1233 nthreads_var
= sysconf (_SC_NPROCESSORS_ONLN
);
1237 /* FIXME: once using -std=c11, we can use std::thread::hardware_concurrency. */
1239 /* Return true when a jobserver is running and can accept a job. */
1242 jobserver_active_p (void)
1244 const char *makeflags
= getenv ("MAKEFLAGS");
1245 if (makeflags
== NULL
)
1248 const char *needle
= "--jobserver-auth=";
1249 const char *n
= strstr (makeflags
, needle
);
1256 return (sscanf (n
+ strlen (needle
), "%d,%d", &rfd
, &wfd
) == 2
1259 && is_valid_fd (rfd
)
1260 && is_valid_fd (wfd
));
1263 /* Execute gcc. ARGC is the number of arguments. ARGV contains the arguments. */
1266 run_gcc (unsigned argc
, char *argv
[])
1269 const char **new_argv
;
1270 const char **argv_ptr
;
1271 char *list_option_full
= NULL
;
1272 const char *linker_output
= NULL
;
1273 const char *collect_gcc
;
1274 char *collect_gcc_options
;
1277 int auto_parallel
= 0;
1278 bool no_partition
= false;
1279 struct cl_decoded_option
*fdecoded_options
= NULL
;
1280 struct cl_decoded_option
*offload_fdecoded_options
= NULL
;
1281 unsigned int fdecoded_options_count
= 0;
1282 unsigned int offload_fdecoded_options_count
= 0;
1283 struct cl_decoded_option
*decoded_options
;
1284 unsigned int decoded_options_count
;
1285 struct obstack argv_obstack
;
1287 bool have_lto
= false;
1288 bool have_offload
= false;
1289 unsigned lto_argc
= 0, ltoobj_argc
= 0;
1290 char **lto_argv
, **ltoobj_argv
;
1291 bool linker_output_rel
= false;
1292 bool skip_debug
= false;
1293 unsigned n_debugobj
;
1295 /* Get the driver and options. */
1296 collect_gcc
= getenv ("COLLECT_GCC");
1298 fatal_error (input_location
,
1299 "environment variable %<COLLECT_GCC%> must be set");
1300 collect_gcc_options
= getenv ("COLLECT_GCC_OPTIONS");
1301 if (!collect_gcc_options
)
1302 fatal_error (input_location
,
1303 "environment variable %<COLLECT_GCC_OPTIONS%> must be set");
1305 char *collect_as_options
= getenv ("COLLECT_AS_OPTIONS");
1307 /* Prepend -Xassembler to each option, and append the string
1308 to collect_gcc_options. */
1309 if (collect_as_options
)
1311 obstack temporary_obstack
;
1312 obstack_init (&temporary_obstack
);
1314 prepend_xassembler_to_collect_as_options (collect_as_options
,
1315 &temporary_obstack
);
1316 obstack_1grow (&temporary_obstack
, '\0');
1318 char *xassembler_opts_string
1319 = XOBFINISH (&temporary_obstack
, char *);
1320 strcat (collect_gcc_options
, xassembler_opts_string
);
1323 get_options_from_collect_gcc_options (collect_gcc
, collect_gcc_options
,
1325 &decoded_options_count
);
1327 /* Allocate array for input object files with LTO IL,
1328 and for possible preceding arguments. */
1329 lto_argv
= XNEWVEC (char *, argc
);
1330 ltoobj_argv
= XNEWVEC (char *, argc
);
1332 /* Look at saved options in the IL files. */
1333 for (i
= 1; i
< argc
; ++i
)
1337 off_t file_offset
= 0;
1340 char *filename
= argv
[i
];
1342 if (strncmp (argv
[i
], "-foffload-objects=",
1343 sizeof ("-foffload-objects=") - 1) == 0)
1345 have_offload
= true;
1346 offload_objects_file_name
1347 = argv
[i
] + sizeof ("-foffload-objects=") - 1;
1351 if ((p
= strrchr (argv
[i
], '@'))
1353 && sscanf (p
, "@%li%n", &loffset
, &consumed
) >= 1
1354 && strlen (p
) == (unsigned int) consumed
)
1356 filename
= XNEWVEC (char, p
- argv
[i
] + 1);
1357 memcpy (filename
, argv
[i
], p
- argv
[i
]);
1358 filename
[p
- argv
[i
]] = '\0';
1359 file_offset
= (off_t
) loffset
;
1361 fd
= open (filename
, O_RDONLY
| O_BINARY
);
1362 /* Linker plugin passes -fresolution and -flinker-output options.
1363 -flinker-output is passed only when user did not specify one and thus
1364 we do not need to worry about duplicities with the option handling
1368 lto_argv
[lto_argc
++] = argv
[i
];
1369 if (strcmp (argv
[i
], "-flinker-output=rel") == 0)
1370 linker_output_rel
= true;
1374 if (find_and_merge_options (fd
, file_offset
, LTO_SECTION_NAME_PREFIX
,
1375 &fdecoded_options
, &fdecoded_options_count
,
1379 ltoobj_argv
[ltoobj_argc
++] = argv
[i
];
1384 /* Initalize the common arguments for the driver. */
1385 obstack_init (&argv_obstack
);
1386 obstack_ptr_grow (&argv_obstack
, collect_gcc
);
1387 obstack_ptr_grow (&argv_obstack
, "-xlto");
1388 obstack_ptr_grow (&argv_obstack
, "-c");
1390 append_compiler_options (&argv_obstack
, fdecoded_options
,
1391 fdecoded_options_count
);
1392 append_linker_options (&argv_obstack
, decoded_options
, decoded_options_count
);
1394 /* Scan linker driver arguments for things that are of relevance to us. */
1395 for (j
= 1; j
< decoded_options_count
; ++j
)
1397 struct cl_decoded_option
*option
= &decoded_options
[j
];
1398 switch (option
->opt_index
)
1401 linker_output
= option
->arg
;
1404 case OPT_save_temps
:
1412 case OPT_flto_partition_
:
1413 if (strcmp (option
->arg
, "none") == 0)
1414 no_partition
= true;
1418 if (strcmp (option
->arg
, "jobserver") == 0)
1423 else if (strcmp (option
->arg
, "auto") == 0)
1430 parallel
= atoi (option
->arg
);
1437 lto_mode
= LTO_MODE_WHOPR
;
1440 case OPT_flinker_output_
:
1441 linker_output_rel
= !strcmp (option
->arg
, "rel");
1445 /* Recognize -g0. */
1446 skip_debug
= option
->arg
&& !strcmp (option
->arg
, "0");
1454 /* Output lto-wrapper invocation command. */
1457 for (i
= 0; i
< argc
; ++i
)
1459 fputs (argv
[i
], stderr
);
1460 fputc (' ', stderr
);
1462 fputc ('\n', stderr
);
1465 if (linker_output_rel
)
1466 no_partition
= true;
1470 lto_mode
= LTO_MODE_LTO
;
1475 else if (!jobserver
&& jobserver_active_p ())
1483 char *output_dir
, *base
, *name
;
1484 bool bit_bucket
= strcmp (linker_output
, HOST_BIT_BUCKET
) == 0;
1486 output_dir
= xstrdup (linker_output
);
1488 for (name
= base
; *name
; name
++)
1489 if (IS_DIR_SEPARATOR (*name
))
1493 linker_output
= &linker_output
[base
- output_dir
];
1494 if (*output_dir
== '\0')
1496 static char current_dir
[] = { '.', DIR_SEPARATOR
, '\0' };
1497 output_dir
= current_dir
;
1501 obstack_ptr_grow (&argv_obstack
, "-dumpdir");
1502 obstack_ptr_grow (&argv_obstack
, output_dir
);
1505 obstack_ptr_grow (&argv_obstack
, "-dumpbase");
1508 /* Remember at which point we can scrub args to re-use the commons. */
1509 new_head_argc
= obstack_object_size (&argv_obstack
) / sizeof (void *);
1513 unsigned i
, num_offload_files
;
1514 char **offload_argv
;
1517 f
= fopen (offload_objects_file_name
, "r");
1519 fatal_error (input_location
, "cannot open %s: %m",
1520 offload_objects_file_name
);
1521 if (fscanf (f
, "%u ", &num_offload_files
) != 1)
1522 fatal_error (input_location
, "cannot read %s: %m",
1523 offload_objects_file_name
);
1524 offload_argv
= XCNEWVEC (char *, num_offload_files
);
1526 /* Read names of object files with offload. */
1527 for (i
= 0; i
< num_offload_files
; i
++)
1529 const unsigned piece
= 32;
1530 char *buf
, *filename
= XNEWVEC (char, piece
);
1535 if (!fgets (buf
, piece
, f
))
1537 len
= strlen (filename
);
1538 if (filename
[len
- 1] != '\n')
1540 filename
= XRESIZEVEC (char, filename
, len
+ piece
);
1541 buf
= filename
+ len
;
1544 filename
[len
- 1] = '\0';
1545 offload_argv
[i
] = filename
;
1548 if (offload_argv
[num_offload_files
- 1] == NULL
)
1549 fatal_error (input_location
, "invalid format of %s",
1550 offload_objects_file_name
);
1551 maybe_unlink (offload_objects_file_name
);
1552 offload_objects_file_name
= NULL
;
1554 /* Look at saved offload options in files. */
1555 for (i
= 0; i
< num_offload_files
; i
++)
1560 off_t file_offset
= 0;
1561 char *filename
= offload_argv
[i
];
1563 if ((p
= strrchr (offload_argv
[i
], '@'))
1564 && p
!= offload_argv
[i
]
1565 && sscanf (p
, "@%li%n", &loffset
, &consumed
) >= 1
1566 && strlen (p
) == (unsigned int) consumed
)
1568 filename
= XNEWVEC (char, p
- offload_argv
[i
] + 1);
1569 memcpy (filename
, offload_argv
[i
], p
- offload_argv
[i
]);
1570 filename
[p
- offload_argv
[i
]] = '\0';
1571 file_offset
= (off_t
) loffset
;
1573 fd
= open (filename
, O_RDONLY
| O_BINARY
);
1575 fatal_error (input_location
, "cannot open %s: %m", filename
);
1576 if (!find_and_merge_options (fd
, file_offset
,
1577 OFFLOAD_SECTION_NAME_PREFIX
,
1578 &offload_fdecoded_options
,
1579 &offload_fdecoded_options_count
,
1581 fatal_error (input_location
, "cannot read %s: %m", filename
);
1583 if (filename
!= offload_argv
[i
])
1584 XDELETEVEC (filename
);
1587 compile_images_for_offload_targets (num_offload_files
, offload_argv
,
1588 offload_fdecoded_options
,
1589 offload_fdecoded_options_count
,
1591 decoded_options_count
);
1593 free_array_of_ptrs ((void **) offload_argv
, num_offload_files
);
1597 find_crtoffloadtable ();
1598 for (i
= 0; offload_names
[i
]; i
++)
1599 printf ("%s\n", offload_names
[i
]);
1600 free_array_of_ptrs ((void **) offload_names
, i
);
1604 /* If object files contain offload sections, but do not contain LTO sections,
1605 then there is no need to perform a link-time recompilation, i.e.
1606 lto-wrapper is used only for a compilation of offload images. */
1607 if (have_offload
&& !have_lto
)
1610 if (lto_mode
== LTO_MODE_LTO
)
1614 obstack_ptr_grow (&argv_obstack
, linker_output
);
1615 flto_out
= (char *) xmalloc (strlen (linker_output
)
1616 + sizeof (".lto.o") + 1);
1617 strcpy (flto_out
, linker_output
);
1618 strcat (flto_out
, ".lto.o");
1621 flto_out
= make_temp_file (".lto.o");
1622 obstack_ptr_grow (&argv_obstack
, "-o");
1623 obstack_ptr_grow (&argv_obstack
, flto_out
);
1627 const char *list_option
= "-fltrans-output-list=";
1628 size_t list_option_len
= strlen (list_option
);
1633 char *dumpbase
= (char *) xmalloc (strlen (linker_output
)
1634 + sizeof (".wpa") + 1);
1635 strcpy (dumpbase
, linker_output
);
1636 strcat (dumpbase
, ".wpa");
1637 obstack_ptr_grow (&argv_obstack
, dumpbase
);
1640 if (linker_output
&& save_temps
)
1642 ltrans_output_file
= (char *) xmalloc (strlen (linker_output
)
1643 + sizeof (".ltrans.out") + 1);
1644 strcpy (ltrans_output_file
, linker_output
);
1645 strcat (ltrans_output_file
, ".ltrans.out");
1649 char *prefix
= NULL
;
1652 prefix
= (char *) xmalloc (strlen (linker_output
) + 2);
1653 strcpy (prefix
, linker_output
);
1654 strcat (prefix
, ".");
1657 ltrans_output_file
= make_temp_file_with_prefix (prefix
,
1661 list_option_full
= (char *) xmalloc (sizeof (char) *
1662 (strlen (ltrans_output_file
) + list_option_len
+ 1));
1663 tmp
= list_option_full
;
1665 obstack_ptr_grow (&argv_obstack
, tmp
);
1666 strcpy (tmp
, list_option
);
1667 tmp
+= list_option_len
;
1668 strcpy (tmp
, ltrans_output_file
);
1673 fprintf (stderr
, "Using make jobserver\n");
1674 obstack_ptr_grow (&argv_obstack
, xstrdup ("-fwpa=jobserver"));
1676 else if (auto_parallel
)
1679 init_num_threads ();
1681 fprintf (stderr
, "LTO parallelism level set to %ld\n",
1683 sprintf (buf
, "-fwpa=%ld", nthreads_var
);
1684 obstack_ptr_grow (&argv_obstack
, xstrdup (buf
));
1686 else if (parallel
> 1)
1689 sprintf (buf
, "-fwpa=%i", parallel
);
1690 obstack_ptr_grow (&argv_obstack
, xstrdup (buf
));
1693 obstack_ptr_grow (&argv_obstack
, "-fwpa");
1696 /* Append input arguments. */
1697 for (i
= 0; i
< lto_argc
; ++i
)
1698 obstack_ptr_grow (&argv_obstack
, lto_argv
[i
]);
1699 /* Append the input objects. */
1700 for (i
= 0; i
< ltoobj_argc
; ++i
)
1701 obstack_ptr_grow (&argv_obstack
, ltoobj_argv
[i
]);
1702 obstack_ptr_grow (&argv_obstack
, NULL
);
1704 new_argv
= XOBFINISH (&argv_obstack
, const char **);
1705 argv_ptr
= &new_argv
[new_head_argc
];
1706 fork_execute (new_argv
[0], CONST_CAST (char **, new_argv
), true);
1708 /* Copy the early generated debug info from the objects to temporary
1709 files and append those to the partial link commandline. */
1711 early_debug_object_names
= NULL
;
1714 early_debug_object_names
= XCNEWVEC (const char *, ltoobj_argc
+ 1);
1715 num_deb_objs
= ltoobj_argc
;
1716 for (i
= 0; i
< ltoobj_argc
; ++i
)
1719 if ((tem
= debug_objcopy (ltoobj_argv
[i
], !linker_output_rel
)))
1721 early_debug_object_names
[i
] = tem
;
1727 if (lto_mode
== LTO_MODE_LTO
)
1729 printf ("%s\n", flto_out
);
1732 for (i
= 0; i
< ltoobj_argc
; ++i
)
1733 if (early_debug_object_names
[i
] != NULL
)
1734 printf ("%s\n", early_debug_object_names
[i
]);
1736 /* These now belong to collect2. */
1739 free (early_debug_object_names
);
1740 early_debug_object_names
= NULL
;
1744 FILE *stream
= fopen (ltrans_output_file
, "r");
1745 FILE *mstream
= NULL
;
1746 struct obstack env_obstack
;
1750 fatal_error (input_location
, "%<fopen%>: %s: %m", ltrans_output_file
);
1752 /* Parse the list of LTRANS inputs from the WPA stage. */
1753 obstack_init (&env_obstack
);
1757 const unsigned piece
= 32;
1758 char *output_name
= NULL
;
1759 char *buf
, *input_name
= (char *)xmalloc (piece
);
1763 if (fscanf (stream
, "%i\n", &priority
) != 1)
1766 fatal_error (input_location
,
1767 "corrupted ltrans output file %s",
1768 ltrans_output_file
);
1772 if (!fgets (buf
, piece
, stream
))
1774 len
= strlen (input_name
);
1775 if (input_name
[len
- 1] != '\n')
1777 input_name
= (char *)xrealloc (input_name
, len
+ piece
);
1778 buf
= input_name
+ len
;
1781 input_name
[len
- 1] = '\0';
1783 if (input_name
[0] == '*')
1784 output_name
= &input_name
[1];
1788 = (int *)xrealloc (ltrans_priorities
, nr
* sizeof (int) * 2);
1789 input_names
= (char **)xrealloc (input_names
, nr
* sizeof (char *));
1790 output_names
= (char **)xrealloc (output_names
, nr
* sizeof (char *));
1791 ltrans_priorities
[(nr
-1)*2] = priority
;
1792 ltrans_priorities
[(nr
-1)*2+1] = nr
-1;
1793 input_names
[nr
-1] = input_name
;
1794 output_names
[nr
-1] = output_name
;
1797 maybe_unlink (ltrans_output_file
);
1798 ltrans_output_file
= NULL
;
1802 makefile
= make_temp_file (".mk");
1803 mstream
= fopen (makefile
, "w");
1804 qsort (ltrans_priorities
, nr
, sizeof (int) * 2, cmp_priority
);
1807 /* Execute the LTRANS stage for each input file (or prepare a
1808 makefile to invoke this in parallel). */
1809 for (i
= 0; i
< nr
; ++i
)
1812 char *input_name
= input_names
[i
];
1813 /* If it's a pass-through file do nothing. */
1814 if (output_names
[i
])
1817 /* Replace the .o suffix with a .ltrans.o suffix and write
1818 the resulting name to the LTRANS output list. */
1819 obstack_grow (&env_obstack
, input_name
, strlen (input_name
) - 2);
1820 obstack_grow (&env_obstack
, ".ltrans.o", sizeof (".ltrans.o"));
1821 output_name
= XOBFINISH (&env_obstack
, char *);
1823 /* Adjust the dumpbase if the linker output file was seen. */
1827 = (char *) xmalloc (strlen (linker_output
)
1828 + sizeof (DUMPBASE_SUFFIX
) + 1);
1830 strlen (linker_output
) + sizeof (DUMPBASE_SUFFIX
),
1831 "%s.ltrans%u", linker_output
, i
);
1832 argv_ptr
[0] = dumpbase
;
1835 argv_ptr
[1] = "-fltrans";
1837 argv_ptr
[3] = output_name
;
1838 argv_ptr
[4] = input_name
;
1842 fprintf (mstream
, "%s:\n\t@%s ", output_name
, new_argv
[0]);
1843 for (j
= 1; new_argv
[j
] != NULL
; ++j
)
1844 fprintf (mstream
, " '%s'", new_argv
[j
]);
1845 fprintf (mstream
, "\n");
1846 /* If we are not preserving the ltrans input files then
1847 truncate them as soon as we have processed it. This
1848 reduces temporary disk-space usage. */
1850 fprintf (mstream
, "\t@-touch -r %s %s.tem > /dev/null 2>&1 "
1851 "&& mv %s.tem %s\n",
1852 input_name
, input_name
, input_name
, input_name
);
1856 fork_execute (new_argv
[0], CONST_CAST (char **, new_argv
),
1858 maybe_unlink (input_name
);
1861 output_names
[i
] = output_name
;
1865 struct pex_obj
*pex
;
1871 for (i
= 0; i
< nr
; ++i
)
1873 int j
= ltrans_priorities
[i
*2 + 1];
1874 fprintf (mstream
, " \\\n\t%s", output_names
[j
]);
1876 fprintf (mstream
, "\n");
1880 /* Avoid passing --jobserver-fd= and similar flags
1881 unless jobserver mode is explicitly enabled. */
1882 putenv (xstrdup ("MAKEFLAGS="));
1883 putenv (xstrdup ("MFLAGS="));
1885 new_argv
[0] = getenv ("MAKE");
1887 new_argv
[0] = "make";
1889 new_argv
[2] = makefile
;
1893 snprintf (jobs
, 31, "-j%ld",
1894 auto_parallel
? nthreads_var
: parallel
);
1895 new_argv
[i
++] = jobs
;
1897 new_argv
[i
++] = "all";
1898 new_argv
[i
++] = NULL
;
1899 pex
= collect_execute (new_argv
[0], CONST_CAST (char **, new_argv
),
1900 NULL
, NULL
, PEX_SEARCH
, false);
1901 do_wait (new_argv
[0], pex
);
1902 maybe_unlink (makefile
);
1904 for (i
= 0; i
< nr
; ++i
)
1905 maybe_unlink (input_names
[i
]);
1907 for (i
= 0; i
< nr
; ++i
)
1909 fputs (output_names
[i
], stdout
);
1910 putc ('\n', stdout
);
1911 free (input_names
[i
]);
1915 for (i
= 0; i
< ltoobj_argc
; ++i
)
1916 if (early_debug_object_names
[i
] != NULL
)
1917 printf ("%s\n", early_debug_object_names
[i
]);
1920 free (ltrans_priorities
);
1921 free (output_names
);
1922 output_names
= NULL
;
1923 free (early_debug_object_names
);
1924 early_debug_object_names
= NULL
;
1926 free (list_option_full
);
1927 obstack_free (&env_obstack
, NULL
);
1932 obstack_free (&argv_obstack
, NULL
);
1939 main (int argc
, char *argv
[])
1943 init_opts_obstack ();
1945 p
= argv
[0] + strlen (argv
[0]);
1946 while (p
!= argv
[0] && !IS_DIR_SEPARATOR (p
[-1]))
1950 xmalloc_set_program_name (progname
);
1952 gcc_init_libintl ();
1954 diagnostic_initialize (global_dc
, 0);
1956 if (atexit (lto_wrapper_cleanup
) != 0)
1957 fatal_error (input_location
, "%<atexit%> failed");
1959 if (signal (SIGINT
, SIG_IGN
) != SIG_IGN
)
1960 signal (SIGINT
, fatal_signal
);
1962 if (signal (SIGHUP
, SIG_IGN
) != SIG_IGN
)
1963 signal (SIGHUP
, fatal_signal
);
1965 if (signal (SIGTERM
, SIG_IGN
) != SIG_IGN
)
1966 signal (SIGTERM
, fatal_signal
);
1968 if (signal (SIGPIPE
, SIG_IGN
) != SIG_IGN
)
1969 signal (SIGPIPE
, fatal_signal
);
1972 /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
1973 receive the signal. A different setting is inheritable */
1974 signal (SIGCHLD
, SIG_DFL
);
1977 /* We may be called with all the arguments stored in some file and
1978 passed with @file. Expand them into argv before processing. */
1979 expandargv (&argc
, &argv
);
1981 run_gcc (argc
, argv
);