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