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