1.13 What -flto does to the link

GCC 15.2.0. To run the commands: an installed compiler.

You add -flto to a build, and the link starts to print a warning: "using serial compilation of 8 LTRANS jobs". The warning comes from a program called lto-wrapper, and nothing in your makefile runs it. Run the link again with -v and the collect2 line has more strangers on it: a -plugin option that names a shared object, and a row of -plugin-opt= options. Then look at a link from a build that never mentioned -flto. If your GCC's configure found GNU ld 2.21 or newer, the -plugin option is there too.

-flto acts at both ends of a build. At compile time, cc1 writes GCC's intermediate representation (IR) into each object. At the link, the driver puts GCC's linker plugin on the collect2 line, and the plugin, or collect2 itself when there is no plugin, runs lto-wrapper. lto-wrapper runs the driver once more, to compile the IR into the objects that the linker gets in the end. When configure found GNU ld 2.21 or newer, the plugin goes on every link, because an object with IR and no machine code needs LTO even at a link that never asked for it.

This chapter assumes Chapter 1.1 for collect2, lto-wrapper, COLLECT_GCC and the driver's table of compilers, and Chapter 1.3 for the spec language. It also uses exec_prefixes from Chapter 1.5, LINK_COMMAND_SPEC and the order of the link line from Chapter 1.7, and the COLLECT_* family from Chapter 1.9.

The driver's side of LTO ends at two interfaces. How a linker loads a plugin and calls it is the binutils plugin API, which binutils owns, and GCC's plugin uses two of its hooks. Inside the link, lto1 runs a WPA step, which splits the IR into partitions, and LTRANS steps, which compile one partition each. What lto1 does in those steps is GCC's own subject, and the internals manual describes lto1 in its LTO chapter.

.gnu.lto_ sections: the IR in slim and fat objects

-flto makes cc1 write the IR into the object file, in sections whose names start with .gnu.lto_. The GCC manual documents the IR in the object under Optimize Options. These sections are what the plugin and collect2 look for at the link, and whether the object also holds machine code decides which links can use it. The prefix is a macro, and its comment says how the rest of each name is built:

/* The string that is the prefix on the section names we make for lto.
   For decls the DECL_ASSEMBLER_NAME is appended to make the section
   name for the functions and static_initializers.  For other types of
   sections a '.' and the section type are appended.  */
#define LTO_SECTION_NAME_PREFIX ".gnu.lto_"

Listing 1.13-1: the IR section prefix (gcc/lto-section-names.h:23-27).

One of those sections, .gnu.lto_.opts, holds the options that compiled the object, and lto-wrapper reads them back at link time. The writer quotes them in the same format as COLLECT_GCC_OPTIONS (gcc/lto-opts.cc:34-36, :57-69).

A fat object holds the IR and ordinary machine code, so any link can use it. A slim object holds the IR and no machine code, so only an LTO link can use it. The -ffat-lto-objects item in Optimize Options documents both kinds, and a slim default "on targets with linker plugin support". -ffat-lto-objects has no Init in gcc/common.opt:1718-1720, so it starts off and slim is the default. The option code turns it on in one case:

      if (!opts->x_flag_fat_lto_objects
	  && (!HAVE_LTO_PLUGIN
	      || (opts_set->x_flag_use_linker_plugin
		  && !opts->x_flag_use_linker_plugin)))
	{
	  if (opts_set->x_flag_fat_lto_objects)
	    error_at (loc, "%<-fno-fat-lto-objects%> are supported only with "
		      "linker plugin");
	  opts->x_flag_fat_lto_objects = 1;
	}

Listing 1.13-2: the fallback to fat objects (gcc/opts.cc:1225-1234).

HAVE_LTO_PLUGIN is a configure-time value that says whether the linker can load GCC's plugin, and it also selects what the driver adds to the link line. A slim object is the default whenever the linker can load the plugin, unless you pass -fno-use-linker-plugin, and fat objects are the fallback for a linker that cannot.

Before GCC 4.9. Objects are fat by default. -ffat-lto-objects is Init(1) at releases/gcc-4.8.0 (gcc/common.opt:1121-1123), and has no Init at releases/gcc-4.9.0 (gcc/common.opt:1162-1164).

A slim object also gets one ordinary symbol, __gnu_lto_slim. The comment above the code that emits it says what the symbol is for:

  /* Let linker plugin know that this is a slim object and must be LTOed
     even when user did not ask for it.  */
  if (flag_generate_lto && !flag_fat_lto_objects)
    {
#if defined ASM_OUTPUT_ALIGNED_DECL_COMMON
      ASM_OUTPUT_ALIGNED_DECL_COMMON (asm_out_file, NULL_TREE, "__gnu_lto_slim",
				      HOST_WIDE_INT_1U, 8);

Listing 1.13-3: the slim marker (gcc/toplev.cc:546-561). Abridged. Two other branches emit the same symbol for other targets.

"Even when user did not ask for it" is the reason the driver puts the plugin on links with no -flto. A slim object can arrive in a library that somebody else built, at a link with no -flto on it. That link must still optimise the object, because the object has no other code. readelf shows the IR sections and the marker in an object of your own:

$ gcc -flto -c foo.c bar.c
$ readelf -S foo.o | grep '\.gnu\.lto_'      # the IR sections
$ readelf -s foo.o | grep __gnu_lto_slim     # only in a slim object

The driver adds -plugin to the link line through LINK_PLUGIN_SPEC, and the configured value of HAVE_LTO_PLUGIN decides on which links. At level 2, the value for GNU ld 2.21 or newer, that is every link, with or without -flto, which explains a -plugin option on a link from a build that never mentioned LTO. Chapter 1.7 abridged LINK_COMMAND_SPEC straight after %(linker), and the full template has LINK_PLUGIN_SPEC in that place:

#define LINK_COMMAND_SPEC "\
%{!fsyntax-only:%{!c:%{!M:%{!MM:%{!E:%{!S:\
    %(linker) " \
    LINK_PLUGIN_SPEC \
   "%{flto|flto=*:%<fcompare-debug*} \
    %{flto} %{fno-lto} %{flto=*} %l " LINK_PIE_SPEC \

Listing 1.13-4: the start of LINK_COMMAND_SPEC, unabridged (gcc/gcc.cc:1160-1165).

LINK_PLUGIN_SPEC has three definitions, and HAVE_LTO_PLUGIN selects one. Levels 1 and 2 differ only in PLUGIN_COND and PLUGIN_COND_CLOSE, the condition that wraps the -plugin options, and level 0 gets a spec that rejects -fuse-linker-plugin:

#if HAVE_LTO_PLUGIN > 0
/* The linker used has full plugin support, use LTO plugin by default.  */
#if HAVE_LTO_PLUGIN == 2
#define PLUGIN_COND "!fno-use-linker-plugin:%{!fno-lto"
#define PLUGIN_COND_CLOSE "}"
#else
...
#define PLUGIN_COND "fuse-linker-plugin"
#define PLUGIN_COND_CLOSE ""
#endif
#define LINK_PLUGIN_SPEC \
    "%{" PLUGIN_COND": \
    -plugin %(linker_plugin_file) \
    -plugin-opt=%(lto_wrapper) \
    -plugin-opt=-fresolution=%u.res \
    " LTO_PLUGIN_SPEC "\
    %{flinker-output=*:-plugin-opt=-linker-output-known} \
    %{!nostdlib:%{!nodefaultlibs:%:pass-through-libs(%(link_gcc_c_sequence))}} \
    }" PLUGIN_COND_CLOSE
#else
/* The linker used doesn't support -plugin, reject -fuse-linker-plugin.  */
#define LINK_PLUGIN_SPEC "%{fuse-linker-plugin:\
    %e-fuse-linker-plugin is not supported in this configuration}"
#endif

Listing 1.13-5: LINK_PLUGIN_SPEC (gcc/gcc.cc:1076-1100). Abridged by one comment.

Configure sets HAVE_LTO_PLUGIN from the linker it found, and only if it built the plugin (gcc/configure.ac:4327-4382, with the version tests at :4362-4373). The three values give three kinds of link line:

HAVE_LTO_PLUGINLinker that configure found-plugin on the link line
2GNU ld or gold 2.21 or newer, or moldalways, unless -fno-use-linker-plugin or -fno-lto
1gold 2.20only with -fuse-linker-plugin
0any othernever, and -fuse-linker-plugin is an error

The -fuse-linker-plugin item in Optimize Options documents the first row as a default: the option is on when GCC "was configured for use with a linker supporting plugins". The FIXME above PLUGIN_COND records the history of that default: "For slim LTO we will need to enable plugin unconditionally" (gcc.cc:1068-1073). The plugin costs little when no input holds IR, because its all-symbols-read hook returns at once if it claimed no file (lto-plugin/lto-plugin.c:780-781).

$ gcc -### hello.c 2>&1 | grep -o -- '-plugin [^ ]*'
$ gcc -### hello.c 2>&1 | grep -o -- '-plugin-opt=[^ ]*'

On a level-2 toolchain, the first command prints the plugin's full path, though nothing in the command asks for LTO.

The -plugin and -plugin-opt= options on an LTO link's collect2 line come from LINK_PLUGIN_SPEC. One of its pieces gives the linker the plugin's path. Three more give the plugin the path of lto-wrapper, a file for the symbol resolution and a second copy of the default libraries. The search for the plugin's path also explains the fatal error that a link without -flto can raise when the plugin is missing.

-plugin %(linker_plugin_file) is the plugin's full path. The driver looks it up when it is about to run the link. Two parts of the code decide the result: the find_a_file call on exec_prefixes, and the fatal_error when that call finds nothing:

#if HAVE_LTO_PLUGIN == 2
	  if (!switch_matches (fno_use_linker_plugin,
			       fno_use_linker_plugin
			       + strlen (fno_use_linker_plugin), 0))
#else
	  ...
#endif
	    {
	      char *temp_spec = find_a_file (&exec_prefixes,
					     LTOPLUGINSONAME, R_OK,
					     false);
	      if (!temp_spec)
		fatal_error (input_location,
			     "%<-fuse-linker-plugin%>, but %s not found",
			     LTOPLUGINSONAME);
	      linker_plugin_file_spec = convert_white_space (temp_spec);
	    }

Listing 1.13-6: finding the plugin (gcc/gcc.cc:9252-9270). Abridged. Level 1 tests for -fuse-linker-plugin instead.

exec_prefixes is the program list of Chapter 1.5, the one that finds cc1 and collect2, so -B moves the plugin too. The file name is liblto_plugin.so unless the host's entry in config.host changes it (gcc/config.host:77). The plugin installs into the same libexec/gcc/<target>/<version>/ directory as cc1 (lto-plugin/Makefile.am:8, :17).

-plugin-opt=%(lto_wrapper) is the path of lto-wrapper. The driver finds it at start-up and exports the same string as COLLECT_LTO_WRAPPER (gcc.cc:8726-8745). It searches only when it can link, because -c, -S and -E set have_c (gcc.cc:4995-5005), and have_c skips the search.

-plugin-opt=-fresolution=%u.res names a temporary file. The plugin writes the linker's symbol resolution into it before it runs lto-wrapper (lto-plugin.c:801), and passes the option on with the other -plugin-opt= values (:1458-1466).

%:pass-through-libs(%(link_gcc_c_sequence)) lists libgcc and the C library a second time, for the plugin. The spec function's comment gives the purpose:

/* %:pass-through-libs spec function.  Finds all -l options and input
   file names in the lib spec passed to it, and makes a list of them
   prepended with the plugin option to cause them to be passed through
   to the final link after all the new object files have been added.  */

Listing 1.13-7: the comment on %:pass-through-libs (gcc/gcc.cc:10827-10830).

The order matters because the linker takes from an archive only what is undefined when it reads that archive, as Chapter 1.7 showed. The objects that lto-wrapper makes arrive only after the linker has read -lgcc and -lc. Each -l in Listing 1.7-7 therefore becomes a -plugin-opt=-pass-through=-l… option, and the plugin gives those libraries to the linker again after the new objects (lto-plugin.c:889-904). It skips this step for gold 1.11 or newer, which does not need it.

The last LTO piece is in LINK_COMMAND_SPEC itself, where %{flto} %{fno-lto} %{flto=*} copy your LTO options onto the linker's command line. The comment above the template says who has to deal with them: "We pass any -flto flags on to the linker, which is expected to understand them. In practice, this means it had better be collect2." (gcc.cc:1156-1157). collect2 reads them and then removes them, under the comment "Do not pass LTO flag to the linker." (collect2.cc:1282-1287).

lto-wrapper with and without the linker plugin

lto-wrapper runs after the linker has read the inputs, and one of two programs starts it, depending on whether -plugin is on the link line. With it, the linker loads liblto_plugin.so, and the plugin runs lto-wrapper from inside the link. Without it, collect2 runs lto-wrapper between two links of its own. The route decides whether IR inside an archive reaches lto1.

liblto_plugin.so: claiming IR files and running lto-wrapper

The linker loads liblto_plugin.so, calls its one exported function, and then calls the plugin back at two points: for each input file, and once when it has read every symbol. The plugin claims the inputs that hold IR at the first hook and runs lto-wrapper at the second. Its header comment gives this contract from GCC's side:

/* The plugin has only one external function: onload. A linker passes it an array of
   function that the plugin uses to communicate back to the linker.

   With the functions provided by the linker, the plugin can be notified when
   the linker first analyzes a file and pass a symbol table back to the linker. The plugin
   is also notified when all symbols have been read and it is time to generate
   machine code for the necessary symbols.

Listing 1.13-8: the plugin's two hooks (lto-plugin/lto-plugin.c:19-25).

In the first hook, the plugin looks in each input for a section whose name starts with .gnu.lto_.symtab (lto-plugin.c:1097). If it finds one, it claims the file and gives the linker the symbols from the IR. Archive members are inputs too, and the plugin records each one as archive@offset (:1229-1241). The second hook runs lto-wrapper and reads what it prints:

  errmsg = pex_run (pex, 0, new_argv[0], new_argv, NULL, NULL, &t);
  check (errmsg == NULL, LDPL_FATAL, "could not run lto-wrapper");
  check (t == 0, LDPL_FATAL, "could not run lto-wrapper");

  wrapper_output = pex_read_output (pex, 0);
  check (wrapper_output, LDPL_FATAL, "could not read lto-wrapper output");

  add_output_files (wrapper_output);

  t = pex_get_status (pex, 1, &status);
  check (t == 1, LDPL_FATAL, "could not get lto-wrapper exit status");
  check (WIFEXITED (status) && WEXITSTATUS (status) == 0, LDPL_FATAL,
         "lto-wrapper failed");

Listing 1.13-9: the plugin runs lto-wrapper (lto-plugin/lto-plugin.c:735-747).

new_argv[0] is the first -plugin-opt= value, the lto-wrapper path from Listing 1.13-5. The other options and the claimed files go into a temporary file, and lto-wrapper gets that file as one @ argument (lto-plugin.c:680-700). The plugin reads lto-wrapper's standard output one line at a time and gives each line to the linker as a new input file (:629-662). It also tells lto-wrapper what the linker makes, as -flinker-output=exec, pie, dyn, rel or nolto-rel (:808-833). Link Options in the GCC manual documents -flinker-output and these values.

The plugin also reads the environment. It relies on the driver's COLLECT_* variables from Chapter 1.9, which reach it through collect2 and the linker:

  char *collect_gcc_options = getenv ("COLLECT_GCC_OPTIONS");
  if (collect_gcc_options)
    {
      /* Support -fno-use-linker-plugin by failing to load the plugin
	 for the case where it is auto-loaded by BFD.  */
      if (strstr (collect_gcc_options, "'-fno-use-linker-plugin'"))
	return LDPS_ERR;

Listing 1.13-10: the plugin refuses to load (lto-plugin/lto-plugin.c:1620-1626).

The comment records a second way in: BFD can load the plugin with no -plugin option. On that route only the plugin itself can honour -fno-use-linker-plugin, so it returns LDPS_ERR and refuses to load.

Without -plugin, collect2 does the plugin's job itself, after a first link. If any object file holds a .gnu.lto_ section, it runs lto-wrapper from COLLECT_LTO_WRAPPER and links again, with the objects that lto-wrapper printed in place of the IR objects (collect2.cc:597-638). It reads object files only, so on this route IR inside an archive never reaches lto1.

collect2 turns its own LTO code off when it sees -plugin on its command line (collect2.cc:953-958, :1032-1033). The same test makes it run the linker that configure recorded as the plugin linker, unless a -fuse-ld= option names another (:783-787, configure.ac:2787-2794).

A comment in collect2.cc gives the order. By the time collect2 does the plugin's job, "at least one linker run has been performed without the use of any LTO info" (collect2.cc:562-563). The test that collects the IR objects is where archives drop out:

  /* LTO objects must be in a known format.  This check prevents
     us from accepting an archive containing LTO objects, which
     gcc cannot currently handle.  */
  if (which_pass == PASS_LTOINFO)
    {
      if(is_lto_object_file (prog_name)) {
	add_lto_object (&lto_objects, prog_name);
      }
      return;

Listing 1.13-11: no archives on this route (gcc/collect2.cc:2352-2360).

The comment names the reason: an archive containing LTO objects is something "gcc cannot currently handle". The machine code of a fat archive member links instead of its IR.

lto-wrapper re-runs the driver from COLLECT_GCC

lto-wrapper compiles nothing itself. It runs the driver named in COLLECT_GCC again, with -xlto -c and the options merged from your objects and your link, and prints the names of the objects that come out. Because the inner run is an ordinary driver run, the driver's search lists and specs apply to it as they apply to any compile.

Its header comment says that it "is passed a gcc, a list of gcc arguments and a list of object files containing IL". GCC's comments call the IR IL. The same comment says that it "prints a list of output files on stdout" (gcc/lto-wrapper.cc:23-26). The gcc and its arguments come from COLLECT_GCC and COLLECT_GCC_OPTIONS. Listing 1.1-5 showed that lto-wrapper stops without the first, and it stops without the second too (lto-wrapper.cc:1449-1452).

lto-wrapper merges options from two sources. COLLECT_GCC_OPTIONS holds the link command's, and the .gnu.lto_.opts section of each IR object holds that object's compile command, which find_and_merge_options reads (lto-wrapper.cc:1155-1168). The manual gives the merge rules for users, starting with "Generally, options specified at link time override those specified at compile time" (gcc/doc/invoke.texi:14999-15001). Then lto-wrapper builds a driver command:

  /* Initalize the common arguments for the driver.  */
  obstack_init (&argv_obstack);
  obstack_ptr_grow (&argv_obstack, collect_gcc);
  obstack_ptr_grow (&argv_obstack, "-xlto");
  obstack_ptr_grow (&argv_obstack, "-c");

  append_compiler_options (&argv_obstack, fdecoded_options);
  append_linker_options (&argv_obstack, decoded_options);

Listing 1.13-12: the command lto-wrapper runs (gcc/lto-wrapper.cc:1534-1541).

collect_gcc is the value of COLLECT_GCC, so the command starts with the same driver you typed. -xlto -c, the merged compile options and your link options follow, and lto-wrapper runs the result (lto-wrapper.cc:1930-1931). -xlto selects one more entry in the driver's table of compilers. Listing 1.1-1 showed the @c entry, and the LTO front end adds this one:

/* LTO contributions to the "compilers" array in gcc.cc.  */

  {"@lto", "lto1 %(cc1_options) %i %{!fsyntax-only:%(invoke_as)}",
   /*cpp_spec=*/NULL, /*combinable=*/1, /*needs_preprocessing=*/0},

Listing 1.13-13: the @lto entry (gcc/lto/lang-specs.h:21-24).

The inner run rebuilds its prefix lists from the -B and --sysroot in your options, as Chapter 1.9 described. It finds lto1 on the program list as it finds cc1, and runs invoke_as from Listing 1.1-2. The %{!fwpa*: at the top of invoke_as is for this run: the driver runs no assembler for the WPA step.

-flto= and the make jobserver: how many LTRANS jobs run

lto-wrapper decides how many LTRANS jobs run at once from two inputs: the -flto= value in the merged options, and whether it finds a make jobserver. A plain -flto with no jobserver runs them one at a time, and a serial run of more than one partition prints the "using serial compilation" warning.

lto-wrapper reads -flto= from the merged options, so a value from compile time counts. A -flto= on the link command wins: "Override IL file settings with a linker -flto= option." (lto-wrapper.cc:1582-1584). A plain -flto sets no job count (:1640-1666), and lto-wrapper then looks for a make jobserver:

  else
    {
      jobserver_info jinfo;
      if (jobserver && !jinfo.is_active)
	{
	  /* Fall back to auto parallelism.  */
	  jobserver = 0;
	  auto_parallel = 1;
	}
      else if (!jobserver && jinfo.is_active)
	{
	  parallel = 1;
	  jobserver = 1;
	}
    }

Listing 1.13-14: the jobserver test (gcc/lto-wrapper.cc:1692-1706).

The if branch above it handles -flto-partition=none and incremental links, with one lto1 run and no parallel step (lto-wrapper.cc:1684-1691). Every other case ends in one cell of this table:

Optionmake jobserver foundNo jobserver
-fltothe jobserverserial
-flto=Nthe jobserverN jobs
-flto=autothe jobserverone job for each CPU thread
-flto=jobserverthe jobserveras -flto=auto

The -flto item in Optimize Options documents what -flto=N, -flto=jobserver and -flto=auto ask for. Three cells of the table are not in it: the serial plain -flto, the jobserver that wins over N, and the fallback of -flto=jobserver.

The jobs run through make, and lto-wrapper checks that make --version, or $MAKE --version, works (lto-wrapper.cc:1386-1402). If it does not, the jobs run one at a time (:1708-1710). The choice becomes a -fwpa= option for the inner driver (:1892-1917). When the WPA step makes more than one partition and parallel is still 0, lto-wrapper warns:

	  else if (parallel == 0)
	    {
	      warning (0, "using serial compilation of %d LTRANS jobs", nr);
	      print_lto_docs_link ();
	    }

Listing 1.13-15: the serial-compilation warning (gcc/lto-wrapper.cc:2030-2034).

The diagnostic carries the name lto-wrapper because the program takes its name from its own argv[0] (lto-wrapper.cc:2299-2302). The fix is -flto=auto, or a jobserver.

Before GCC 10. A plain -flto never uses the jobserver. The test arrived in GCC 10 (lto-wrapper.c:1476-1480 at releases/gcc-10.1.0), and the same block at releases/gcc-9.1.0 has no such test (lto-wrapper.c:1288-1293).

Before GCC 12. The serial run prints no warning. The warning is present at releases/gcc-12.1.0 (lto-wrapper.cc:1971-1975) and absent at releases/gcc-11.1.0.

$ gcc -flto=auto -v foo.o bar.o -o prog 2>&1 | grep 'LTO parallelism'

With no jobserver, that prints the thread count lto-wrapper chose (lto-wrapper.cc:1904-1906).

Why gcc-ar, gcc-nm and gcc-ranlib exist

gcc-ar, gcc-nm and gcc-ranlib run the real ar, nm or ranlib with a --plugin option that names GCC's plugin. They exist because a slim object has no machine code and its symbols are in an IR section. A tool that builds an archive's symbol index, or lists an object's symbols, must read that section. The internals manual states the requirement: slim objects "require that tools like ar and nm understand symbol tables of LTO sections" (gcc/doc/lto.texi:39-44).

What a plain ar or nm does when it cannot read them is binutils' behaviour, and binutils is the authority on it. On GCC's side, the -ffat-lto-objects item in Optimize Options names the three wrappers as the way to pass the right options to these tools. The first line of gcc-ar.cc says what they are: "Wrapper for ar/ranlib/nm to pass the LTO plugin." (gcc/gcc-ar.cc:1).

One source file makes all three programs, because the makefile copies gcc-ar.cc to gcc-nm.cc and gcc-ranlib.cc and compiles each with a different PERSONALITY (gcc/Makefile.in:2529-2547). Each one finds the plugin, finds the real tool, and runs the tool with two more arguments at the front:

  nargv = XCNEWVEC (const char *, ac + j + 1); /* +j plugin args +1 for NULL.  */
  nargv[0] = exe_name;
#if HAVE_LTO_PLUGIN > 0
  nargv[1] = "--plugin";
  nargv[2] = plugin;
#endif
  for (k = 1; k < ac; k++)
    nargv[j + k] = av[k];

Listing 1.13-16: gcc-ar adds --plugin (gcc/gcc-ar.cc:229-236).

j counts the plugin arguments. When HAVE_LTO_PLUGIN is 0, j is 0 and the wrapper adds nothing (gcc-ar.cc:129-134).

gcc-ar does not use the driver's search lists, and builds its own from its own location: the target's tool bin directory, then libexec/gcc/<target>/<version>/, where the plugin is (gcc-ar.cc:105-119). It looks for the plugin on that list only (:189-197). For the tool it then tries $PATH, and on a cross toolchain it looks there for <target>-ar, not ar (:199-214). A -B option puts a directory in front of both searches (:149-187).

The same -ffat-lto-objects item in the manual names a second way to get the same effect (gcc/doc/invoke.texi:15231-15234). It relies on binutils loading the plugin by itself:

Note that modern binutils provide plugin auto-load mechanism. Installing the linker plugin into $libdir/bfd-plugins has the same effect as usage of the command wrappers (gcc-ar, gcc-nm and gcc-ranlib).

Where binutils looks for such plugins, and which versions do it, is binutils' business. Listing 1.13-10 shows that GCC's plugin expects to be loaded that way.

$ ar rcs plain.a foo.o && nm plain.a       # depends on what binutils loads
$ gcc-ar rcs lto.a foo.o && gcc-nm lto.a   # passes --plugin itself

On the plugin route, -v makes every layer of an LTO link print a command line, because each layer finds -v in COLLECT_GCC_OPTIONS. -### is no substitute, because it runs nothing: it shows the plugin options and nothing that runs inside the link. The layers print in this order:

  1. The driver prints the collect2 command line, with the options of Listing 1.13-5.
  2. The plugin prints the lto-wrapper command line that it builds (lto-plugin.c:714-719). It becomes verbose when it finds '-v' in COLLECT_GCC_OPTIONS (:1634-1636).
  3. lto-wrapper prints its own command line (lto-wrapper.cc:1670-1679), then the driver command it runs. That second line shows only @ and a file name, because lto-wrapper passes the arguments in a response file (collect-utils.cc:153-195).
  4. The inner driver prints its lto1 command lines in full. It got -v because -v is a driver option (common.opt:3883-3884), and lto-wrapper passes driver options on (lto-wrapper.cc:829-832).
$ gcc -v -flto foo.o bar.o -o prog 2>&1 | grep -E 'lto-wrapper|lto1'

Three things to carry away

A slim object holds IR and no machine code, and it is the default. Only an LTO link can use it, which is why the plugin has to be on the link line.

The driver puts the plugin on the link line, not your makefile. On a level-2 toolchain it is on every link, and it does nothing when no input holds IR.

lto-wrapper is the driver calling itself. It runs COLLECT_GCC with -xlto -c and the options from your objects and your link, and the linker gets the objects it prints.

Things that surprise people

A link without -flto can fail for want of the LTO plugin. On a level-2 toolchain, the driver looks for liblto_plugin.so on every link. If the file is missing, the driver stops with a fatal error that names -fuse-linker-plugin, an option you never typed (Listing 1.13-6). -fno-lto removes -plugin from the link line but does not stop the search. Only -fno-use-linker-plugin does.

A broken LTO setup still links when the objects are fat. The internals manual says that "any mistake in the toolchain leads to LTO information not being used". It adds that "the user is not informed that the optimization has been disabled" (gcc/doc/lto.texi:52-57). If you need to know, check the -v output for an lto1 line.

Documentation coverage

-flto, -fuse-linker-plugin and -ffat-lto-objects are documented in Optimize Options (gcc/doc/invoke.texi:12669, items at :14922, :15197 and :15216), and -flinker-output in Link Options (:19096). The -ffat-lto-objects item names the three wrappers and bfd-plugins (:15222-15234). The internals manual has an LTO chapter (gcc/doc/lto.texi:7).

What is not documented:

  • That a level-2 toolchain puts -plugin on every link, with or without -flto. The -fuse-linker-plugin item says only that the option "is enabled by default" with a plugin-capable linker (invoke.texi:15211-15213).
  • That a missing plugin fails a link that never asked for LTO, and that -fno-lto does not prevent the failure.
  • The -plugin-opt=-pass-through= copies of libgcc and the C library.
  • That lto-wrapper runs the driver again with -xlto -c.
  • That a plain -flto with no make jobserver runs serially. The manual says only that GCC "tries to automatically detect a running GNU make's job server" (invoke.texi:15144-15145).
  • That gcc-ar searches its own prefixes, not the driver's program list.

The internals manual also disagrees with the source in two places:

  • It says that GCC "does not use the linker plugin by default", and that collect2 examines the objects (lto.texi:539-545). In GCC 15.2.0, that is the route for HAVE_LTO_PLUGIN 0 or 1 only. It also says that collect2 "invokes lto1" (lto.texi:84-87). Two programs sit between them, lto-wrapper and the driver.
  • It says that objects are fat by default "if no plugin support is detected for ar and nm when GCC is configured" (lto.texi:46-48). The code tests HAVE_LTO_PLUGIN (Listing 1.13-2), which configure derives from the linker alone (configure.ac:4362-4373).

Next: Chapter 1.14, -print-*, -v and -dumpspecs: asking a built compiler, every question in this part as one procedure.