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