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