lto-wrapper.c (compile_offload_image): Start processing in_argv from 0 instead of 1.
[platform/upstream/gcc.git] / gcc / lto-wrapper.c
1 /* Wrapper to call lto.  Used by collect2 and the linker plugin.
2    Copyright (C) 2009-2014 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 char **input_names;
69 static char **output_names;
70 static char **offload_names;
71 static const char *offloadbegin, *offloadend;
72 static char *makefile;
73
74 const char tool_name[] = "lto-wrapper";
75
76 /* Delete tempfiles.  Called from utils_cleanup.  */
77
78 void
79 tool_cleanup (bool)
80 {
81   unsigned int i;
82
83   if (ltrans_output_file)
84     maybe_unlink (ltrans_output_file);
85   if (flto_out)
86     maybe_unlink (flto_out);
87   if (makefile)
88     maybe_unlink (makefile);
89   for (i = 0; i < nr; ++i)
90     {
91       maybe_unlink (input_names[i]);
92       if (output_names[i])
93         maybe_unlink (output_names[i]);
94     }
95 }
96
97 static void
98 lto_wrapper_cleanup (void)
99 {
100   utils_cleanup (false);
101 }
102
103 /* Unlink a temporary LTRANS file unless requested otherwise.  */
104
105 void
106 maybe_unlink (const char *file)
107 {
108   if (!save_temps)
109     {
110       if (unlink_if_ordinary (file)
111           && errno != ENOENT)
112         fatal_error ("deleting LTRANS file %s: %m", file);
113     }
114   else if (verbose)
115     fprintf (stderr, "[Leaving LTRANS %s]\n", file);
116 }
117
118 /* Template of LTRANS dumpbase suffix.  */
119 #define DUMPBASE_SUFFIX ".ltrans18446744073709551615"
120
121 /* Create decoded options from the COLLECT_GCC and COLLECT_GCC_OPTIONS
122    environment according to LANG_MASK.  */
123
124 static void
125 get_options_from_collect_gcc_options (const char *collect_gcc,
126                                       const char *collect_gcc_options,
127                                       unsigned int lang_mask,
128                                       struct cl_decoded_option **decoded_options,
129                                       unsigned int *decoded_options_count)
130 {
131   struct obstack argv_obstack;
132   char *argv_storage;
133   const char **argv;
134   int j, k, argc;
135
136   argv_storage = xstrdup (collect_gcc_options);
137   obstack_init (&argv_obstack);
138   obstack_ptr_grow (&argv_obstack, collect_gcc);
139
140   for (j = 0, k = 0; argv_storage[j] != '\0'; ++j)
141     {
142       if (argv_storage[j] == '\'')
143         {
144           obstack_ptr_grow (&argv_obstack, &argv_storage[k]);
145           ++j;
146           do
147             {
148               if (argv_storage[j] == '\0')
149                 fatal_error ("malformed COLLECT_GCC_OPTIONS");
150               else if (strncmp (&argv_storage[j], "'\\''", 4) == 0)
151                 {
152                   argv_storage[k++] = '\'';
153                   j += 4;
154                 }
155               else if (argv_storage[j] == '\'')
156                 break;
157               else
158                 argv_storage[k++] = argv_storage[j++];
159             }
160           while (1);
161           argv_storage[k++] = '\0';
162         }
163     }
164
165   obstack_ptr_grow (&argv_obstack, NULL);
166   argc = obstack_object_size (&argv_obstack) / sizeof (void *) - 1;
167   argv = XOBFINISH (&argv_obstack, const char **);
168
169   decode_cmdline_options_to_array (argc, (const char **)argv,
170                                    lang_mask,
171                                    decoded_options, decoded_options_count);
172   obstack_free (&argv_obstack, NULL);
173 }
174
175 /* Append OPTION to the options array DECODED_OPTIONS with size
176    DECODED_OPTIONS_COUNT.  */
177
178 static void
179 append_option (struct cl_decoded_option **decoded_options,
180                unsigned int *decoded_options_count,
181                struct cl_decoded_option *option)
182 {
183   ++*decoded_options_count;
184   *decoded_options
185     = (struct cl_decoded_option *)
186         xrealloc (*decoded_options,
187                   (*decoded_options_count
188                    * sizeof (struct cl_decoded_option)));
189   memcpy (&(*decoded_options)[*decoded_options_count - 1], option,
190           sizeof (struct cl_decoded_option));
191 }
192
193 /* Try to merge and complain about options FDECODED_OPTIONS when applied
194    ontop of DECODED_OPTIONS.  */
195
196 static void
197 merge_and_complain (struct cl_decoded_option **decoded_options,
198                     unsigned int *decoded_options_count,
199                     struct cl_decoded_option *fdecoded_options,
200                     unsigned int fdecoded_options_count)
201 {
202   unsigned int i, j;
203
204   /* ???  Merge options from files.  Most cases can be
205      handled by either unioning or intersecting
206      (for example -fwrapv is a case for unioning,
207      -ffast-math is for intersection).  Most complaints
208      about real conflicts between different options can
209      be deferred to the compiler proper.  Options that
210      we can neither safely handle by intersection nor
211      unioning would need to be complained about here.
212      Ideally we'd have a flag in the opt files that
213      tells whether to union or intersect or reject.
214      In absence of that it's unclear what a good default is.
215      It's also difficult to get positional handling correct.  */
216
217   /* The following does what the old LTO option code did,
218      union all target and a selected set of common options.  */
219   for (i = 0; i < fdecoded_options_count; ++i)
220     {
221       struct cl_decoded_option *foption = &fdecoded_options[i];
222       switch (foption->opt_index)
223         {
224         case OPT_SPECIAL_unknown:
225         case OPT_SPECIAL_ignore:
226         case OPT_SPECIAL_program_name:
227         case OPT_SPECIAL_input_file:
228           break;
229
230         default:
231           if (!(cl_options[foption->opt_index].flags & CL_TARGET))
232             break;
233
234           /* Fallthru.  */
235         case OPT_fPIC:
236         case OPT_fpic:
237         case OPT_fPIE:
238         case OPT_fpie:
239         case OPT_fcommon:
240         case OPT_fexceptions:
241         case OPT_fnon_call_exceptions:
242         case OPT_fgnu_tm:
243           /* Do what the old LTO code did - collect exactly one option
244              setting per OPT code, we pick the first we encounter.
245              ???  This doesn't make too much sense, but when it doesn't
246              then we should complain.  */
247           for (j = 0; j < *decoded_options_count; ++j)
248             if ((*decoded_options)[j].opt_index == foption->opt_index)
249               break;
250           if (j == *decoded_options_count)
251             append_option (decoded_options, decoded_options_count, foption);
252           break;
253
254         case OPT_ftrapv:
255         case OPT_fstrict_overflow:
256         case OPT_ffp_contract_:
257           /* For selected options we can merge conservatively.  */
258           for (j = 0; j < *decoded_options_count; ++j)
259             if ((*decoded_options)[j].opt_index == foption->opt_index)
260               break;
261           if (j == *decoded_options_count)
262             append_option (decoded_options, decoded_options_count, foption);
263           /* FP_CONTRACT_OFF < FP_CONTRACT_ON < FP_CONTRACT_FAST,
264              -fno-trapv < -ftrapv,
265              -fno-strict-overflow < -fstrict-overflow  */
266           else if (foption->value < (*decoded_options)[j].value)
267             (*decoded_options)[j] = *foption;
268           break;
269
270         case OPT_fmath_errno:
271         case OPT_fsigned_zeros:
272         case OPT_ftrapping_math:
273         case OPT_fwrapv:
274           /* For selected options we can merge conservatively.  */
275           for (j = 0; j < *decoded_options_count; ++j)
276             if ((*decoded_options)[j].opt_index == foption->opt_index)
277               break;
278           if (j == *decoded_options_count)
279             append_option (decoded_options, decoded_options_count, foption);
280           /* -fmath-errno > -fno-math-errno,
281              -fsigned-zeros > -fno-signed-zeros,
282              -ftrapping-math -> -fno-trapping-math,
283              -fwrapv > -fno-wrapv.  */
284           else if (foption->value > (*decoded_options)[j].value)
285             (*decoded_options)[j] = *foption;
286           break;
287
288         case OPT_freg_struct_return:
289         case OPT_fpcc_struct_return:
290         case OPT_fshort_double:
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             fatal_error ("Option %s not used consistently in all LTO input"
296                          " files", foption->orig_option_with_args_text);
297           break;
298
299         case OPT_foffload_abi_:
300           for (j = 0; j < *decoded_options_count; ++j)
301             if ((*decoded_options)[j].opt_index == foption->opt_index)
302               break;
303             if (j == *decoded_options_count)
304               append_option (decoded_options, decoded_options_count, foption);
305             else if (foption->value != (*decoded_options)[j].value)
306               fatal_error ("Option %s not used consistently in all LTO input"
307                            " files", foption->orig_option_with_args_text);
308             break;
309
310         case OPT_O:
311         case OPT_Ofast:
312         case OPT_Og:
313         case OPT_Os:
314           for (j = 0; j < *decoded_options_count; ++j)
315             if ((*decoded_options)[j].opt_index == OPT_O
316                 || (*decoded_options)[j].opt_index == OPT_Ofast
317                 || (*decoded_options)[j].opt_index == OPT_Og
318                 || (*decoded_options)[j].opt_index == OPT_Os)
319               break;
320           if (j == *decoded_options_count)
321             append_option (decoded_options, decoded_options_count, foption);
322           else if ((*decoded_options)[j].opt_index == foption->opt_index
323                    && foption->opt_index != OPT_O)
324             /* Exact same options get merged.  */
325             ;
326           else
327             {
328               /* For mismatched option kinds preserve the optimization
329                  level only, thus merge it as -On.  This also handles
330                  merging of same optimization level -On.  */
331               int level = 0;
332               switch (foption->opt_index)
333                 {
334                 case OPT_O:
335                   if (foption->arg[0] == '\0')
336                     level = MAX (level, 1);
337                   else
338                     level = MAX (level, atoi (foption->arg));
339                   break;
340                 case OPT_Ofast:
341                   level = MAX (level, 3);
342                   break;
343                 case OPT_Og:
344                   level = MAX (level, 1);
345                   break;
346                 case OPT_Os:
347                   level = MAX (level, 2);
348                   break;
349                 default:
350                   gcc_unreachable ();
351                 }
352               switch ((*decoded_options)[j].opt_index)
353                 {
354                 case OPT_O:
355                   if ((*decoded_options)[j].arg[0] == '\0')
356                     level = MAX (level, 1);
357                   else
358                     level = MAX (level, atoi ((*decoded_options)[j].arg));
359                   break;
360                 case OPT_Ofast:
361                   level = MAX (level, 3);
362                   break;
363                 case OPT_Og:
364                   level = MAX (level, 1);
365                   break;
366                 case OPT_Os:
367                   level = MAX (level, 2);
368                   break;
369                 default:
370                   gcc_unreachable ();
371                 }
372               (*decoded_options)[j].opt_index = OPT_O;
373               char *tem;
374               asprintf (&tem, "-O%d", level);
375               (*decoded_options)[j].arg = &tem[2];
376               (*decoded_options)[j].canonical_option[0] = tem;
377               (*decoded_options)[j].value = 1;
378             }
379           break;
380
381         case OPT_foffload_:
382           append_option (decoded_options, decoded_options_count, foption);
383           break;
384         }
385     }
386 }
387
388 /* Auxiliary function that frees elements of PTR and PTR itself.
389    N is number of elements to be freed.  If PTR is NULL, nothing is freed.
390    If an element is NULL, subsequent elements are not freed.  */
391
392 static void **
393 free_array_of_ptrs (void **ptr, unsigned n)
394 {
395   if (!ptr)
396     return NULL;
397   for (unsigned i = 0; i < n; i++)
398     {
399       if (!ptr[i])
400         break;
401       free (ptr[i]);
402     }
403   free (ptr);
404   return NULL;
405 }
406
407 /* Parse STR, saving found tokens into PVALUES and return their number.
408    Tokens are assumed to be delimited by ':'.  If APPEND is non-null,
409    append it to every token we find.  */
410
411 static unsigned
412 parse_env_var (const char *str, char ***pvalues, const char *append)
413 {
414   const char *curval, *nextval;
415   char **values;
416   unsigned num = 1, i;
417
418   curval = strchr (str, ':');
419   while (curval)
420     {
421       num++;
422       curval = strchr (curval + 1, ':');
423     }
424
425   values = (char**) xmalloc (num * sizeof (char*));
426   curval = str;
427   nextval = strchr (curval, ':');
428   if (nextval == NULL)
429     nextval = strchr (curval, '\0');
430
431   int append_len = append ? strlen (append) : 0;
432   for (i = 0; i < num; i++)
433     {
434       int l = nextval - curval;
435       values[i] = (char*) xmalloc (l + 1 + append_len);
436       memcpy (values[i], curval, l);
437       values[i][l] = 0;
438       if (append)
439         strcat (values[i], append);
440       curval = nextval + 1;
441       nextval = strchr (curval, ':');
442       if (nextval == NULL)
443         nextval = strchr (curval, '\0');
444     }
445   *pvalues = values;
446   return num;
447 }
448
449 /* Append options OPTS from lto or offload_lto sections to ARGV_OBSTACK.  */
450
451 static void
452 append_compiler_options (obstack *argv_obstack, struct cl_decoded_option *opts,
453                          unsigned int count)
454 {
455   /* Append compiler driver arguments as far as they were merged.  */
456   for (unsigned int j = 1; j < count; ++j)
457     {
458       struct cl_decoded_option *option = &opts[j];
459
460       /* File options have been properly filtered by lto-opts.c.  */
461       switch (option->opt_index)
462         {
463         /* Drop arguments that we want to take from the link line.  */
464         case OPT_flto_:
465         case OPT_flto:
466         case OPT_flto_partition_:
467           continue;
468
469         default:
470           break;
471         }
472
473       /* For now do what the original LTO option code was doing - pass
474          on any CL_TARGET flag and a few selected others.  */
475       switch (option->opt_index)
476         {
477         case OPT_fPIC:
478         case OPT_fpic:
479         case OPT_fPIE:
480         case OPT_fpie:
481         case OPT_fcommon:
482         case OPT_fexceptions:
483         case OPT_fnon_call_exceptions:
484         case OPT_fgnu_tm:
485         case OPT_freg_struct_return:
486         case OPT_fpcc_struct_return:
487         case OPT_fshort_double:
488         case OPT_ffp_contract_:
489         case OPT_fmath_errno:
490         case OPT_fsigned_zeros:
491         case OPT_ftrapping_math:
492         case OPT_fwrapv:
493         case OPT_ftrapv:
494         case OPT_fstrict_overflow:
495         case OPT_foffload_abi_:
496         case OPT_O:
497         case OPT_Ofast:
498         case OPT_Og:
499         case OPT_Os:
500           break;
501
502         default:
503           if (!(cl_options[option->opt_index].flags & CL_TARGET))
504             continue;
505         }
506
507       /* Pass the option on.  */
508       for (unsigned int i = 0; i < option->canonical_option_num_elements; ++i)
509         obstack_ptr_grow (argv_obstack, option->canonical_option[i]);
510     }
511 }
512
513 /* Append linker options OPTS to ARGV_OBSTACK.  */
514
515 static void
516 append_linker_options (obstack *argv_obstack, struct cl_decoded_option *opts,
517                        unsigned int count)
518 {
519   /* Append linker driver arguments.  Compiler options from the linker
520      driver arguments will override / merge with those from the compiler.  */
521   for (unsigned int j = 1; j < count; ++j)
522     {
523       struct cl_decoded_option *option = &opts[j];
524
525       /* Do not pass on frontend specific flags not suitable for lto.  */
526       if (!(cl_options[option->opt_index].flags
527             & (CL_COMMON|CL_TARGET|CL_DRIVER|CL_LTO)))
528         continue;
529
530       switch (option->opt_index)
531         {
532         case OPT_o:
533         case OPT_flto_:
534         case OPT_flto:
535           /* We've handled these LTO options, do not pass them on.  */
536           continue;
537
538         case OPT_freg_struct_return:
539         case OPT_fpcc_struct_return:
540         case OPT_fshort_double:
541           /* Ignore these, they are determined by the input files.
542              ???  We fail to diagnose a possible mismatch here.  */
543           continue;
544
545         default:
546           break;
547         }
548
549       /* Pass the option on.  */
550       for (unsigned int i = 0; i < option->canonical_option_num_elements; ++i)
551         obstack_ptr_grow (argv_obstack, option->canonical_option[i]);
552     }
553 }
554
555 /* Extract options for TARGET offload compiler from OPTIONS and append
556    them to ARGV_OBSTACK.  */
557
558 static void
559 append_offload_options (obstack *argv_obstack, const char *target,
560                         struct cl_decoded_option *options,
561                         unsigned int options_count)
562 {
563   for (unsigned i = 0; i < options_count; i++)
564     {
565       const char *cur, *next, *opts;
566       char **argv;
567       unsigned argc;
568       struct cl_decoded_option *option = &options[i];
569
570       if (option->opt_index != OPT_foffload_)
571         continue;
572
573       /* If option argument starts with '-' then no target is specified.  That
574          means offload options are specified for all targets, so we need to
575          append them.  */
576       if (option->arg[0] == '-')
577         opts = option->arg;
578       else
579         {
580           opts = strchr (option->arg, '=');
581           if (!opts)
582             continue;
583
584           cur = option->arg;
585
586           while (cur < opts)
587             {
588               next = strchr (cur, ',');
589               if (next == NULL)
590                 next = opts;
591               next = (next > opts) ? opts : next;
592
593               if (strlen (target) == (size_t) (next - cur)
594                   && strncmp (target, cur, next - cur) == 0)
595                 break;
596
597               cur = next + 1;
598             }
599
600           if (cur >= opts)
601             continue;
602
603           opts++;
604         }
605
606       argv = buildargv (opts);
607       for (argc = 0; argv[argc]; argc++)
608         obstack_ptr_grow (argv_obstack, argv[argc]);
609     }
610 }
611
612 /* Check whether NAME can be accessed in MODE.  This is like access,
613    except that it never considers directories to be executable.  */
614
615 static int
616 access_check (const char *name, int mode)
617 {
618   if (mode == X_OK)
619     {
620       struct stat st;
621
622       if (stat (name, &st) < 0
623           || S_ISDIR (st.st_mode))
624         return -1;
625     }
626
627   return access (name, mode);
628 }
629
630 /* Prepare a target image for offload TARGET, using mkoffload tool from
631    COMPILER_PATH.  Return the name of the resultant object file.  */
632
633 static char *
634 compile_offload_image (const char *target, const char *compiler_path,
635                        unsigned in_argc, char *in_argv[],
636                        struct cl_decoded_option *compiler_opts,
637                        unsigned int compiler_opt_count,
638                        struct cl_decoded_option *linker_opts,
639                        unsigned int linker_opt_count)
640 {
641   char *filename = NULL;
642   char **argv;
643   char *suffix
644     = XALLOCAVEC (char, sizeof ("/accel//mkoffload") + strlen (target));
645   strcpy (suffix, "/accel/");
646   strcat (suffix, target);
647   strcat (suffix, "/mkoffload");
648
649   char **paths = NULL;
650   unsigned n_paths = parse_env_var (compiler_path, &paths, suffix);
651
652   const char *compiler = NULL;
653   for (unsigned i = 0; i < n_paths; i++)
654     if (access_check (paths[i], X_OK) == 0)
655       {
656         compiler = paths[i];
657         break;
658       }
659
660   if (compiler)
661     {
662       /* Generate temporary output file name.  */
663       filename = make_temp_file (".target.o");
664
665       struct obstack argv_obstack;
666       obstack_init (&argv_obstack);
667       obstack_ptr_grow (&argv_obstack, compiler);
668       obstack_ptr_grow (&argv_obstack, "-o");
669       obstack_ptr_grow (&argv_obstack, filename);
670
671       /* Append names of input object files.  */
672       for (unsigned i = 0; i < in_argc; i++)
673         obstack_ptr_grow (&argv_obstack, in_argv[i]);
674
675       /* Append options from offload_lto sections.  */
676       append_compiler_options (&argv_obstack, compiler_opts,
677                                compiler_opt_count);
678
679       /* Append options specified by -foffload last.  In case of conflicting
680          options we expect offload compiler to choose the latest.  */
681       append_offload_options (&argv_obstack, target, compiler_opts,
682                               compiler_opt_count);
683       append_offload_options (&argv_obstack, target, linker_opts,
684                               linker_opt_count);
685
686       obstack_ptr_grow (&argv_obstack, NULL);
687       argv = XOBFINISH (&argv_obstack, char **);
688       fork_execute (argv[0], argv, true);
689       obstack_free (&argv_obstack, NULL);
690     }
691
692   free_array_of_ptrs ((void **) paths, n_paths);
693   return filename;
694 }
695
696
697 /* The main routine dealing with offloading.
698    The routine builds a target image for each offload target.  IN_ARGC and
699    IN_ARGV specify options and input object files.  As all of them could contain
700    target sections, we pass them all to target compilers.  */
701
702 static void
703 compile_images_for_offload_targets (unsigned in_argc, char *in_argv[],
704                                     struct cl_decoded_option *compiler_opts,
705                                     unsigned int compiler_opt_count,
706                                     struct cl_decoded_option *linker_opts,
707                                     unsigned int linker_opt_count)
708 {
709   char **names = NULL;
710   const char *target_names = getenv (OFFLOAD_TARGET_NAMES_ENV);
711   if (!target_names)
712     return;
713   unsigned num_targets = parse_env_var (target_names, &names, NULL);
714
715   const char *compiler_path = getenv ("COMPILER_PATH");
716   if (!compiler_path)
717     goto out;
718
719   /* Prepare an image for each target and save the name of the resultant object
720      file to the OFFLOAD_NAMES array.  It is terminated by a NULL entry.  */
721   offload_names = XCNEWVEC (char *, num_targets + 1);
722   for (unsigned i = 0; i < num_targets; i++)
723     {
724       offload_names[i]
725         = compile_offload_image (names[i], compiler_path, in_argc, in_argv,
726                                  compiler_opts, compiler_opt_count,
727                                  linker_opts, linker_opt_count);
728       if (!offload_names[i])
729         fatal_error ("problem with building target image for %s\n", names[i]);
730     }
731
732  out:
733   free_array_of_ptrs ((void **) names, num_targets);
734 }
735
736 /* Copy a file from SRC to DEST.  */
737
738 static void
739 copy_file (const char *dest, const char *src)
740 {
741   FILE *d = fopen (dest, "wb");
742   FILE *s = fopen (src, "rb");
743   char buffer[512];
744   while (!feof (s))
745     {
746       size_t len = fread (buffer, 1, 512, s);
747       if (ferror (s) != 0)
748         fatal_error ("reading input file");
749       if (len > 0)
750         {
751           fwrite (buffer, 1, len, d);
752           if (ferror (d) != 0)
753             fatal_error ("writing output file");
754         }
755     }
756 }
757
758 /* Find the crtoffloadbegin.o and crtoffloadend.o files in LIBRARY_PATH, make
759    copies and store the names of the copies in offloadbegin and offloadend.  */
760
761 static void
762 find_offloadbeginend (void)
763 {
764   char **paths = NULL;
765   const char *library_path = getenv ("LIBRARY_PATH");
766   if (!library_path)
767     return;
768   unsigned n_paths = parse_env_var (library_path, &paths, "/crtoffloadbegin.o");
769
770   unsigned i;
771   for (i = 0; i < n_paths; i++)
772     if (access_check (paths[i], R_OK) == 0)
773       {
774         size_t len = strlen (paths[i]);
775         char *tmp = xstrdup (paths[i]);
776         strcpy (paths[i] + len - strlen ("begin.o"), "end.o");
777         if (access_check (paths[i], R_OK) != 0)
778           fatal_error ("installation error, can't find crtoffloadend.o");
779         /* The linker will delete the filenames we give it, so make
780            copies.  */
781         offloadbegin = make_temp_file (".o");
782         offloadend = make_temp_file (".o");
783         copy_file (offloadbegin, tmp);
784         copy_file (offloadend, paths[i]);
785         free (tmp);
786         break;
787       }
788   if (i == n_paths)
789     fatal_error ("installation error, can't find crtoffloadbegin.o");
790
791   free_array_of_ptrs ((void **) paths, n_paths);
792 }
793
794 /* A subroutine of run_gcc.  Examine the open file FD for lto sections with
795    name prefix PREFIX, at FILE_OFFSET, and store any options we find in OPTS
796    and OPT_COUNT.  Return true if we found a matchingn section, false
797    otherwise.  COLLECT_GCC holds the value of the environment variable with
798    the same name.  */
799
800 static bool
801 find_and_merge_options (int fd, off_t file_offset, const char *prefix,
802                         struct cl_decoded_option **opts,
803                         unsigned int *opt_count, const char *collect_gcc)
804 {
805   off_t offset, length;
806   char *data;
807   char *fopts;
808   const char *errmsg;
809   int err;
810   struct cl_decoded_option *fdecoded_options = *opts;
811   unsigned int fdecoded_options_count = *opt_count;
812
813   simple_object_read *sobj;
814   sobj = simple_object_start_read (fd, file_offset, "__GNU_LTO",
815                                    &errmsg, &err);
816   if (!sobj)
817     return false;
818
819   char *secname = XALLOCAVEC (char, strlen (prefix) + sizeof (".opts"));
820   strcpy (secname, prefix);
821   strcat (secname, ".opts");
822   if (!simple_object_find_section (sobj, secname, &offset, &length,
823                                    &errmsg, &err))
824     {
825       simple_object_release_read (sobj);
826       return false;
827     }
828
829   lseek (fd, file_offset + offset, SEEK_SET);
830   data = (char *)xmalloc (length);
831   read (fd, data, length);
832   fopts = data;
833   do
834     {
835       struct cl_decoded_option *f2decoded_options;
836       unsigned int f2decoded_options_count;
837       get_options_from_collect_gcc_options (collect_gcc,
838                                             fopts, CL_LANG_ALL,
839                                             &f2decoded_options,
840                                             &f2decoded_options_count);
841       if (!fdecoded_options)
842        {
843          fdecoded_options = f2decoded_options;
844          fdecoded_options_count = f2decoded_options_count;
845        }
846       else
847         merge_and_complain (&fdecoded_options,
848                             &fdecoded_options_count,
849                             f2decoded_options, f2decoded_options_count);
850
851       fopts += strlen (fopts) + 1;
852     }
853   while (fopts - data < length);
854
855   free (data);
856   simple_object_release_read (sobj);
857   *opts = fdecoded_options;
858   *opt_count = fdecoded_options_count;
859   return true;
860 }
861
862 /* Execute gcc. ARGC is the number of arguments. ARGV contains the arguments. */
863
864 static void
865 run_gcc (unsigned argc, char *argv[])
866 {
867   unsigned i, j;
868   const char **new_argv;
869   const char **argv_ptr;
870   char *list_option_full = NULL;
871   const char *linker_output = NULL;
872   const char *collect_gcc, *collect_gcc_options;
873   int parallel = 0;
874   int jobserver = 0;
875   bool no_partition = false;
876   struct cl_decoded_option *fdecoded_options = NULL;
877   struct cl_decoded_option *offload_fdecoded_options = NULL;
878   unsigned int fdecoded_options_count = 0;
879   unsigned int offload_fdecoded_options_count = 0;
880   struct cl_decoded_option *decoded_options;
881   unsigned int decoded_options_count;
882   struct obstack argv_obstack;
883   int new_head_argc;
884   bool have_lto = false;
885   bool have_offload = false;
886   unsigned lto_argc = 0, offload_argc = 0;
887   char **lto_argv, **offload_argv;
888
889   /* Get the driver and options.  */
890   collect_gcc = getenv ("COLLECT_GCC");
891   if (!collect_gcc)
892     fatal_error ("environment variable COLLECT_GCC must be set");
893   collect_gcc_options = getenv ("COLLECT_GCC_OPTIONS");
894   if (!collect_gcc_options)
895     fatal_error ("environment variable COLLECT_GCC_OPTIONS must be set");
896   get_options_from_collect_gcc_options (collect_gcc, collect_gcc_options,
897                                         CL_LANG_ALL,
898                                         &decoded_options,
899                                         &decoded_options_count);
900
901   /* Allocate arrays for input object files with LTO or offload IL,
902      and for possible preceding arguments.  */
903   lto_argv = XNEWVEC (char *, argc);
904   offload_argv = XNEWVEC (char *, argc);
905
906   /* Look at saved options in the IL files.  */
907   for (i = 1; i < argc; ++i)
908     {
909       char *p;
910       int fd;
911       off_t file_offset = 0;
912       long loffset;
913       int consumed;
914       char *filename = argv[i];
915
916       if ((p = strrchr (argv[i], '@'))
917           && p != argv[i] 
918           && sscanf (p, "@%li%n", &loffset, &consumed) >= 1
919           && strlen (p) == (unsigned int) consumed)
920         {
921           filename = XNEWVEC (char, p - argv[i] + 1);
922           memcpy (filename, argv[i], p - argv[i]);
923           filename[p - argv[i]] = '\0';
924           file_offset = (off_t) loffset;
925         }
926       fd = open (argv[i], O_RDONLY);
927       if (fd == -1)
928         {
929           lto_argv[lto_argc++] = argv[i];
930           continue;
931         }
932
933       if (find_and_merge_options (fd, file_offset, LTO_SECTION_NAME_PREFIX,
934                                   &fdecoded_options, &fdecoded_options_count,
935                                   collect_gcc))
936         {
937           have_lto = true;
938           lto_argv[lto_argc++] = argv[i];
939         }
940
941       if (find_and_merge_options (fd, file_offset, OFFLOAD_SECTION_NAME_PREFIX,
942                                   &offload_fdecoded_options,
943                                   &offload_fdecoded_options_count, collect_gcc))
944         {
945           have_offload = true;
946           offload_argv[offload_argc++] = argv[i];
947         }
948
949       close (fd);
950     }
951
952   /* Initalize the common arguments for the driver.  */
953   obstack_init (&argv_obstack);
954   obstack_ptr_grow (&argv_obstack, collect_gcc);
955   obstack_ptr_grow (&argv_obstack, "-xlto");
956   obstack_ptr_grow (&argv_obstack, "-c");
957
958   append_compiler_options (&argv_obstack, fdecoded_options,
959                            fdecoded_options_count);
960   append_linker_options (&argv_obstack, decoded_options, decoded_options_count);
961
962   /* Scan linker driver arguments for things that are of relevance to us.  */
963   for (j = 1; j < decoded_options_count; ++j)
964     {
965       struct cl_decoded_option *option = &decoded_options[j];
966       switch (option->opt_index)
967         {
968         case OPT_o:
969           linker_output = option->arg;
970           break;
971
972         case OPT_save_temps:
973           save_temps = 1;
974           break;
975
976         case OPT_v:
977           verbose = 1;
978           break;
979
980         case OPT_flto_partition_:
981           if (strcmp (option->arg, "none") == 0)
982             no_partition = true;
983           break;
984
985         case OPT_flto_:
986           if (strcmp (option->arg, "jobserver") == 0)
987             {
988               jobserver = 1;
989               parallel = 1;
990             }
991           else
992             {
993               parallel = atoi (option->arg);
994               if (parallel <= 1)
995                 parallel = 0;
996             }
997           /* Fallthru.  */
998
999         case OPT_flto:
1000           lto_mode = LTO_MODE_WHOPR;
1001           break;
1002
1003         default:
1004           break;
1005         }
1006     }
1007
1008   if (no_partition)
1009     {
1010       lto_mode = LTO_MODE_LTO;
1011       jobserver = 0;
1012       parallel = 0;
1013     }
1014
1015   if (linker_output)
1016     {
1017       char *output_dir, *base, *name;
1018       bool bit_bucket = strcmp (linker_output, HOST_BIT_BUCKET) == 0;
1019
1020       output_dir = xstrdup (linker_output);
1021       base = output_dir;
1022       for (name = base; *name; name++)
1023         if (IS_DIR_SEPARATOR (*name))
1024           base = name + 1;
1025       *base = '\0';
1026
1027       linker_output = &linker_output[base - output_dir];
1028       if (*output_dir == '\0')
1029         {
1030           static char current_dir[] = { '.', DIR_SEPARATOR, '\0' };
1031           output_dir = current_dir;
1032         }
1033       if (!bit_bucket)
1034         {
1035           obstack_ptr_grow (&argv_obstack, "-dumpdir");
1036           obstack_ptr_grow (&argv_obstack, output_dir);
1037         }
1038
1039       obstack_ptr_grow (&argv_obstack, "-dumpbase");
1040     }
1041
1042   /* Remember at which point we can scrub args to re-use the commons.  */
1043   new_head_argc = obstack_object_size (&argv_obstack) / sizeof (void *);
1044
1045   if (have_offload)
1046     {
1047       compile_images_for_offload_targets (offload_argc, offload_argv,
1048                                           offload_fdecoded_options,
1049                                           offload_fdecoded_options_count,
1050                                           decoded_options,
1051                                           decoded_options_count);
1052       if (offload_names)
1053         {
1054           find_offloadbeginend ();
1055           for (i = 0; offload_names[i]; i++)
1056             printf ("%s\n", offload_names[i]);
1057           free_array_of_ptrs ((void **) offload_names, i);
1058         }
1059     }
1060
1061   if (offloadbegin)
1062     printf ("%s\n", offloadbegin);
1063
1064   /* If object files contain offload sections, but do not contain LTO sections,
1065      then there is no need to perform a link-time recompilation, i.e.
1066      lto-wrapper is used only for a compilation of offload images.  */
1067   if (have_offload && !have_lto)
1068     {
1069       for (i = 1; i < argc; ++i)
1070         if (strncmp (argv[i], "-fresolution=", sizeof ("-fresolution=") - 1))
1071           {
1072             char *out_file;
1073             /* Can be ".o" or ".so".  */
1074             char *ext = strrchr (argv[i], '.');
1075             if (ext == NULL)
1076               out_file = make_temp_file ("");
1077             else
1078               out_file = make_temp_file (ext);
1079             /* The linker will delete the files we give it, so make copies.  */
1080             copy_file (out_file, argv[i]);
1081             printf ("%s\n", out_file);
1082           }
1083       goto finish;
1084     }
1085
1086   if (lto_mode == LTO_MODE_LTO)
1087     {
1088       flto_out = make_temp_file (".lto.o");
1089       if (linker_output)
1090         obstack_ptr_grow (&argv_obstack, linker_output);
1091       obstack_ptr_grow (&argv_obstack, "-o");
1092       obstack_ptr_grow (&argv_obstack, flto_out);
1093     }
1094   else 
1095     {
1096       const char *list_option = "-fltrans-output-list=";
1097       size_t list_option_len = strlen (list_option);
1098       char *tmp;
1099
1100       if (linker_output)
1101         {
1102           char *dumpbase = (char *) xmalloc (strlen (linker_output)
1103                                              + sizeof (".wpa") + 1);
1104           strcpy (dumpbase, linker_output);
1105           strcat (dumpbase, ".wpa");
1106           obstack_ptr_grow (&argv_obstack, dumpbase);
1107         }
1108
1109       if (linker_output && save_temps)
1110         {
1111           ltrans_output_file = (char *) xmalloc (strlen (linker_output)
1112                                                  + sizeof (".ltrans.out") + 1);
1113           strcpy (ltrans_output_file, linker_output);
1114           strcat (ltrans_output_file, ".ltrans.out");
1115         }
1116       else
1117         ltrans_output_file = make_temp_file (".ltrans.out");
1118       list_option_full = (char *) xmalloc (sizeof (char) *
1119                          (strlen (ltrans_output_file) + list_option_len + 1));
1120       tmp = list_option_full;
1121
1122       obstack_ptr_grow (&argv_obstack, tmp);
1123       strcpy (tmp, list_option);
1124       tmp += list_option_len;
1125       strcpy (tmp, ltrans_output_file);
1126
1127       if (jobserver)
1128         obstack_ptr_grow (&argv_obstack, xstrdup ("-fwpa=jobserver"));
1129       else if (parallel > 1)
1130         {
1131           char buf[256];
1132           sprintf (buf, "-fwpa=%i", parallel);
1133           obstack_ptr_grow (&argv_obstack, xstrdup (buf));
1134         }
1135       else
1136         obstack_ptr_grow (&argv_obstack, "-fwpa");
1137     }
1138
1139   /* Append the input objects and possible preceding arguments.  */
1140   for (i = 0; i < lto_argc; ++i)
1141     obstack_ptr_grow (&argv_obstack, lto_argv[i]);
1142   obstack_ptr_grow (&argv_obstack, NULL);
1143
1144   new_argv = XOBFINISH (&argv_obstack, const char **);
1145   argv_ptr = &new_argv[new_head_argc];
1146   fork_execute (new_argv[0], CONST_CAST (char **, new_argv), true);
1147
1148   if (lto_mode == LTO_MODE_LTO)
1149     {
1150       printf ("%s\n", flto_out);
1151       free (flto_out);
1152       flto_out = NULL;
1153     }
1154   else
1155     {
1156       FILE *stream = fopen (ltrans_output_file, "r");
1157       FILE *mstream = NULL;
1158       struct obstack env_obstack;
1159
1160       if (!stream)
1161         fatal_error ("fopen: %s: %m", ltrans_output_file);
1162
1163       /* Parse the list of LTRANS inputs from the WPA stage.  */
1164       obstack_init (&env_obstack);
1165       nr = 0;
1166       for (;;)
1167         {
1168           const unsigned piece = 32;
1169           char *output_name = NULL;
1170           char *buf, *input_name = (char *)xmalloc (piece);
1171           size_t len;
1172
1173           buf = input_name;
1174 cont:
1175           if (!fgets (buf, piece, stream))
1176             break;
1177           len = strlen (input_name);
1178           if (input_name[len - 1] != '\n')
1179             {
1180               input_name = (char *)xrealloc (input_name, len + piece);
1181               buf = input_name + len;
1182               goto cont;
1183             }
1184           input_name[len - 1] = '\0';
1185
1186           if (input_name[0] == '*')
1187             output_name = &input_name[1];
1188
1189           nr++;
1190           input_names = (char **)xrealloc (input_names, nr * sizeof (char *));
1191           output_names = (char **)xrealloc (output_names, nr * sizeof (char *));
1192           input_names[nr-1] = input_name;
1193           output_names[nr-1] = output_name;
1194         }
1195       fclose (stream);
1196       maybe_unlink (ltrans_output_file);
1197       ltrans_output_file = NULL;
1198
1199       if (parallel)
1200         {
1201           makefile = make_temp_file (".mk");
1202           mstream = fopen (makefile, "w");
1203         }
1204
1205       /* Execute the LTRANS stage for each input file (or prepare a
1206          makefile to invoke this in parallel).  */
1207       for (i = 0; i < nr; ++i)
1208         {
1209           char *output_name;
1210           char *input_name = input_names[i];
1211           /* If it's a pass-through file do nothing.  */
1212           if (output_names[i])
1213             continue;
1214
1215           /* Replace the .o suffix with a .ltrans.o suffix and write
1216              the resulting name to the LTRANS output list.  */
1217           obstack_grow (&env_obstack, input_name, strlen (input_name) - 2);
1218           obstack_grow (&env_obstack, ".ltrans.o", sizeof (".ltrans.o"));
1219           output_name = XOBFINISH (&env_obstack, char *);
1220
1221           /* Adjust the dumpbase if the linker output file was seen.  */
1222           if (linker_output)
1223             {
1224               char *dumpbase
1225                   = (char *) xmalloc (strlen (linker_output)
1226                                       + sizeof (DUMPBASE_SUFFIX) + 1);
1227               snprintf (dumpbase,
1228                         strlen (linker_output) + sizeof (DUMPBASE_SUFFIX),
1229                         "%s.ltrans%u", linker_output, i);
1230               argv_ptr[0] = dumpbase;
1231             }
1232
1233           argv_ptr[1] = "-fltrans";
1234           argv_ptr[2] = "-o";
1235           argv_ptr[3] = output_name;
1236           argv_ptr[4] = input_name;
1237           argv_ptr[5] = NULL;
1238           if (parallel)
1239             {
1240               fprintf (mstream, "%s:\n\t@%s ", output_name, new_argv[0]);
1241               for (j = 1; new_argv[j] != NULL; ++j)
1242                 fprintf (mstream, " '%s'", new_argv[j]);
1243               fprintf (mstream, "\n");
1244               /* If we are not preserving the ltrans input files then
1245                  truncate them as soon as we have processed it.  This
1246                  reduces temporary disk-space usage.  */
1247               if (! save_temps)
1248                 fprintf (mstream, "\t@-touch -r %s %s.tem > /dev/null 2>&1 "
1249                          "&& mv %s.tem %s\n",
1250                          input_name, input_name, input_name, input_name); 
1251             }
1252           else
1253             {
1254               fork_execute (new_argv[0], CONST_CAST (char **, new_argv),
1255                             true);
1256               maybe_unlink (input_name);
1257             }
1258
1259           output_names[i] = output_name;
1260         }
1261       if (parallel)
1262         {
1263           struct pex_obj *pex;
1264           char jobs[32];
1265
1266           fprintf (mstream, "all:");
1267           for (i = 0; i < nr; ++i)
1268             fprintf (mstream, " \\\n\t%s", output_names[i]);
1269           fprintf (mstream, "\n");
1270           fclose (mstream);
1271           if (!jobserver)
1272             {
1273               /* Avoid passing --jobserver-fd= and similar flags 
1274                  unless jobserver mode is explicitly enabled.  */
1275               putenv (xstrdup ("MAKEFLAGS="));
1276               putenv (xstrdup ("MFLAGS="));
1277             }
1278           new_argv[0] = getenv ("MAKE");
1279           if (!new_argv[0])
1280             new_argv[0] = "make";
1281           new_argv[1] = "-f";
1282           new_argv[2] = makefile;
1283           i = 3;
1284           if (!jobserver)
1285             {
1286               snprintf (jobs, 31, "-j%d", parallel);
1287               new_argv[i++] = jobs;
1288             }
1289           new_argv[i++] = "all";
1290           new_argv[i++] = NULL;
1291           pex = collect_execute (new_argv[0], CONST_CAST (char **, new_argv),
1292                                  NULL, NULL, PEX_SEARCH, false);
1293           do_wait (new_argv[0], pex);
1294           maybe_unlink (makefile);
1295           makefile = NULL;
1296           for (i = 0; i < nr; ++i)
1297             maybe_unlink (input_names[i]);
1298         }
1299       for (i = 0; i < nr; ++i)
1300         {
1301           fputs (output_names[i], stdout);
1302           putc ('\n', stdout);
1303           free (input_names[i]);
1304         }
1305       nr = 0;
1306       free (output_names);
1307       free (input_names);
1308       free (list_option_full);
1309       obstack_free (&env_obstack, NULL);
1310     }
1311
1312  finish:
1313   if (offloadend)
1314     printf ("%s\n", offloadend);
1315
1316   XDELETE (lto_argv);
1317   XDELETE (offload_argv);
1318   obstack_free (&argv_obstack, NULL);
1319 }
1320
1321
1322 /* Entry point.  */
1323
1324 int
1325 main (int argc, char *argv[])
1326 {
1327   const char *p;
1328
1329   gcc_obstack_init (&opts_obstack);
1330
1331   p = argv[0] + strlen (argv[0]);
1332   while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
1333     --p;
1334   progname = p;
1335
1336   xmalloc_set_program_name (progname);
1337
1338   gcc_init_libintl ();
1339
1340   diagnostic_initialize (global_dc, 0);
1341
1342   if (atexit (lto_wrapper_cleanup) != 0)
1343     fatal_error ("atexit failed");
1344
1345   if (signal (SIGINT, SIG_IGN) != SIG_IGN)
1346     signal (SIGINT, fatal_signal);
1347 #ifdef SIGHUP
1348   if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
1349     signal (SIGHUP, fatal_signal);
1350 #endif
1351   if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
1352     signal (SIGTERM, fatal_signal);
1353 #ifdef SIGPIPE
1354   if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
1355     signal (SIGPIPE, fatal_signal);
1356 #endif
1357 #ifdef SIGCHLD
1358   /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
1359      receive the signal.  A different setting is inheritable */
1360   signal (SIGCHLD, SIG_DFL);
1361 #endif
1362
1363   /* We may be called with all the arguments stored in some file and
1364      passed with @file.  Expand them into argv before processing.  */
1365   expandargv (&argc, &argv);
1366
1367   run_gcc (argc, argv);
1368
1369   return 0;
1370 }