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