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