+
+#ifdef OBJECT_FORMAT_NONE
+
+/* Add an entry for the object file NAME to object file list LIST.
+ New entries are added at the end of the list. The original pointer
+ value of NAME is preserved, i.e., no string copy is performed. */
+
+static void
+add_lto_object (struct lto_object_list *list, const char *name)
+{
+ struct lto_object *n = XNEW (struct lto_object);
+ n->name = name;
+ n->next = NULL;
+
+ if (list->last)
+ list->last->next = n;
+ else
+ list->first = n;
+
+ list->last = n;
+}
+#endif /* OBJECT_FORMAT_NONE */
+
+
+/* Perform a link-time recompilation and relink if any of the object
+ files contain LTO info. The linker command line LTO_LD_ARGV
+ represents the linker command that would produce a final executable
+ without the use of LTO. OBJECT_LST is a vector of object file names
+ appearing in LTO_LD_ARGV that are to be considerd for link-time
+ recompilation, where OBJECT is a pointer to the last valid element.
+ (This awkward convention avoids an impedance mismatch with the
+ usage of similarly-named variables in main().) The elements of
+ OBJECT_LST must be identical, i.e., pointer equal, to the
+ corresponding arguments in LTO_LD_ARGV.
+
+ Upon entry, at least one linker run has been performed without the
+ use of any LTO info that might be present. Any recompilations
+ necessary for template instantiations have been performed, and
+ initializer/finalizer tables have been created if needed and
+ included in the linker command line LTO_LD_ARGV. If any of the
+ object files contain LTO info, we run the LTO back end on all such
+ files, and perform the final link with the LTO back end output
+ substituted for the LTO-optimized files. In some cases, a final
+ link with all link-time generated code has already been performed,
+ so there is no need to relink if no LTO info is found. In other
+ cases, our caller has not produced the final executable, and is
+ relying on us to perform the required link whether LTO info is
+ present or not. In that case, the FORCE argument should be true.
+ Note that the linker command line argument LTO_LD_ARGV passed into
+ this function may be modified in place. */
+
+static void
+maybe_run_lto_and_relink (char **lto_ld_argv, char **object_lst,
+ const char **object, bool force)
+{
+ const char **object_file = CONST_CAST2 (const char **, char **, object_lst);
+
+ int num_lto_c_args = 1; /* Allow space for the terminating NULL. */
+
+ while (object_file < object)
+ {
+ /* If file contains LTO info, add it to the list of LTO objects. */
+ scan_prog_file (*object_file++, PASS_LTOINFO, SCAN_ALL);
+
+ /* Increment the argument count by the number of object file arguments
+ we will add. An upper bound suffices, so just count all of the
+ object files regardless of whether they contain LTO info. */
+ num_lto_c_args++;
+ }
+
+ if (lto_objects.first)
+ {
+ const char *opts;
+ char **lto_c_argv;
+ const char **lto_c_ptr;
+ const char *cp;
+ const char **p, **q, **r;
+ const char **lto_o_ptr;
+ struct lto_object *list;
+ char *lto_wrapper = getenv ("COLLECT_LTO_WRAPPER");
+ struct pex_obj *pex;
+ const char *prog = "lto-wrapper";
+
+ if (!lto_wrapper)
+ fatal ("COLLECT_LTO_WRAPPER must be set.");
+
+ /* There is at least one object file containing LTO info,
+ so we need to run the LTO back end and relink. */
+
+ /* Get compiler options passed down from the parent `gcc' command.
+ These must be passed to the LTO back end. */
+ opts = getenv ("COLLECT_GCC_OPTIONS");
+
+ /* Increment the argument count by the number of inherited options.
+ Some arguments may be filtered out later. Again, an upper bound
+ suffices. */
+
+ cp = opts;
+
+ while (cp && *cp)
+ {
+ extract_string (&cp);
+ num_lto_c_args++;
+ }
+ obstack_free (&temporary_obstack, temporary_firstobj);
+
+ if (debug)
+ num_lto_c_args++;
+
+ /* Increment the argument count by the number of initial
+ arguments added below. */
+ num_lto_c_args += 9;
+
+ lto_c_argv = (char **) xcalloc (sizeof (char *), num_lto_c_args);
+ lto_c_ptr = CONST_CAST2 (const char **, char **, lto_c_argv);
+
+ *lto_c_ptr++ = lto_wrapper;
+ *lto_c_ptr++ = c_file_name;
+
+ cp = opts;
+
+ while (cp && *cp)
+ {
+ const char *s = extract_string (&cp);
+
+ /* Pass the option or argument to the wrapper. */
+ *lto_c_ptr++ = xstrdup (s);
+ }
+ obstack_free (&temporary_obstack, temporary_firstobj);
+
+ if (debug)
+ *lto_c_ptr++ = xstrdup ("-debug");
+
+ /* Add LTO objects to the wrapper command line. */
+ for (list = lto_objects.first; list; list = list->next)
+ *lto_c_ptr++ = list->name;
+
+ *lto_c_ptr = NULL;
+
+ /* Save intermediate WPA files in lto1 if debug. */
+ if (debug)
+ putenv (xstrdup ("WPA_SAVE_LTRANS=1"));
+
+ /* Run the LTO back end. */
+ pex = collect_execute (prog, lto_c_argv, NULL, NULL, PEX_SEARCH);
+ {
+ int c;
+ FILE *stream;
+ size_t i, num_files;
+ char *start, *end;
+
+ stream = pex_read_output (pex, 0);
+ gcc_assert (stream);
+
+ num_files = 0;
+ while ((c = getc (stream)) != EOF)
+ {
+ obstack_1grow (&temporary_obstack, c);
+ if (c == '\n')
+ ++num_files;
+ }
+
+ lto_o_files = XNEWVEC (char *, num_files + 1);
+ lto_o_files[num_files] = NULL;
+ start = XOBFINISH (&temporary_obstack, char *);
+ for (i = 0; i < num_files; ++i)
+ {
+ end = start;
+ while (*end != '\n')
+ ++end;
+ *end = '\0';
+
+ lto_o_files[i] = xstrdup (start);
+
+ start = end + 1;
+ }
+
+ obstack_free (&temporary_obstack, temporary_firstobj);
+ }
+ do_wait (prog, pex);
+ pex = NULL;
+
+ /* After running the LTO back end, we will relink, substituting
+ the LTO output for the object files that we submitted to the
+ LTO. Here, we modify the linker command line for the relink. */
+ p = CONST_CAST2 (const char **, char **, lto_ld_argv);
+ lto_o_ptr = CONST_CAST2 (const char **, char **, lto_o_files);
+
+ while (*p != NULL)
+ {
+ for (list = lto_objects.first; list; list = list->next)
+ {
+ if (*p == list->name) /* Note test for pointer equality! */
+ {
+ /* Excise argument from linker command line. */
+ if (*lto_o_ptr)
+ {
+ /* Replace first argument with LTO output file. */
+ *p++ = *lto_o_ptr++;
+ }
+ else
+ {
+ /* Move following arguments one position earlier,
+ overwriting the current argument. */
+ q = p;
+ r = p + 1;
+ while (*r != NULL)
+ *q++ = *r++;
+ *q = NULL;
+ }
+
+ /* No need to continue searching the LTO object list. */
+ break;
+ }
+ }
+
+ /* If we didn't find a match, move on to the next argument.
+ Otherwise, P has been set to the correct argument position
+ at which to continue. */
+ if (!list) ++p;
+ }
+
+ /* The code above assumes we will never have more lto output files than
+ input files. Otherwise, we need to resize lto_ld_argv. Check this
+ assumption. */
+ if (*lto_o_ptr)
+ fatal ("too many lto output files");
+
+ /* Run the linker again, this time replacing the object files
+ optimized by the LTO with the temporary file generated by the LTO. */
+ fork_execute ("ld", lto_ld_argv);
+
+ maybe_unlink_list (lto_o_files);
+ }
+ else if (force)
+ {
+ /* Our caller is relying on us to do the link
+ even though there is no LTO back end work to be done. */
+ fork_execute ("ld", lto_ld_argv);
+ }
+}