GCC Middle and Back End API Reference
lto-wrapper.cc File Reference
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "intl.h"
#include "diagnostic.h"
#include "obstack.h"
#include "opts.h"
#include "options.h"
#include "simple-object.h"
#include "lto-section-names.h"
#include "collect-utils.h"
#include "opts-diagnostic.h"
#include "opt-suggestions.h"
#include "opts-jobserver.h"
#include "make-unique.h"
#include "lto-ltrans-cache.h"
Include dependency graph for lto-wrapper.cc:

Data Structures

class  lto_diagnostic_option_manager
 

Macros

#define INCLUDE_STRING
 
#define INCLUDE_ARRAY
 
#define INCLUDE_MAP
 
#define INCLUDE_VECTOR
 
#define OFFLOAD_TARGET_NAMES_ENV   "OFFLOAD_TARGET_NAMES"
 
#define OFFLOAD_TARGET_DEFAULT_ENV   "OFFLOAD_TARGET_DEFAULT"
 
#define TARGET_EXECUTABLE_SUFFIX   ""
 
#define DUMPBASE_SUFFIX   "ltrans18446744073709551615"
 

Enumerations

enum  lto_mode_d { LTO_MODE_NONE , LTO_MODE_LTO , LTO_MODE_WHOPR }
 

Functions

static void ** free_array_of_ptrs (void **ptr, unsigned n)
 
void tool_cleanup (bool)
 
static void lto_wrapper_cleanup (void)
 
void maybe_unlink (const char *file)
 
static vec< cl_decoded_optionget_options_from_collect_gcc_options (const char *collect_gcc, const char *collect_gcc_options)
 
static int find_option (vec< cl_decoded_option > &options, size_t opt_index, unsigned start=0)
 
static int find_option (vec< cl_decoded_option > &options, cl_decoded_option *option)
 
static void merge_flto_options (vec< cl_decoded_option > &decoded_options, cl_decoded_option *foption, bool force)
 
static void merge_and_complain (vec< cl_decoded_option > &decoded_options, vec< cl_decoded_option > fdecoded_options, vec< cl_decoded_option > decoded_cl_options)
 
static unsigned parse_env_var (const char *str, char ***pvalues, const char *append)
 
static void append_compiler_options (obstack *argv_obstack, vec< cl_decoded_option > opts)
 
static void append_diag_options (obstack *argv_obstack, vec< cl_decoded_option > opts)
 
static void append_linker_options (obstack *argv_obstack, vec< cl_decoded_option > opts)
 
static void append_offload_options (obstack *argv_obstack, const char *target, vec< cl_decoded_option > options)
 
static int access_check (const char *name, int mode)
 
static const char * compile_offload_image (const char *target, const char *compiler_path, unsigned in_argc, char *in_argv[], vec< cl_decoded_option > compiler_opts, vec< cl_decoded_option > linker_opts, char **filename)
 
static void compile_images_for_offload_targets (unsigned in_argc, char *in_argv[], vec< cl_decoded_option > compiler_opts, vec< cl_decoded_option > linker_opts)
 
static void copy_file (const char *dest, const char *src)
 
static void find_crtoffloadtable (int save_temps, bool pie_or_shared, const char *dumppfx)
 
static bool find_and_merge_options (int fd, off_t file_offset, const char *prefix, vec< cl_decoded_option > decoded_cl_options, bool first, vec< cl_decoded_option > *opts, const char *collect_gcc)
 
const char * debug_objcopy (const char *infile, bool rename)
 
int cmp_priority (const void *a, const void *b)
 
static void init_num_threads (void)
 
void print_lto_docs_link ()
 
static bool make_exists (void)
 
static void run_gcc (unsigned argc, char *argv[])
 
int main (int argc, char *argv[])
 

Variables

static enum lto_mode_d lto_mode = LTO_MODE_NONE
 
static char * ltrans_output_file
 
static char * flto_out
 
static unsigned int nr
 
static int * ltrans_priorities
 
static char ** input_names
 
static char const ** output_names
 
static char ** offload_names
 
static char * offload_objects_file_name
 
static char * makefile
 
static unsigned int num_deb_objs
 
static const char ** early_debug_object_names
 
static bool xassembler_options_error = false
 
const char tool_name [] = "lto-wrapper"
 
static unsigned long nthreads_var = 0
 

Macro Definition Documentation

◆ DUMPBASE_SUFFIX

#define DUMPBASE_SUFFIX   "ltrans18446744073709551615"
Template of LTRANS dumpbase suffix.   

Referenced by run_gcc().

◆ INCLUDE_ARRAY

#define INCLUDE_ARRAY

◆ INCLUDE_MAP

#define INCLUDE_MAP

◆ INCLUDE_STRING

#define INCLUDE_STRING
Wrapper to call lto.  Used by collect2 and the linker plugin.
   Copyright (C) 2009-2024 Free Software Foundation, Inc.

   Factored out of collect2 by Rafael Espindola <espindola@google.com>

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.

GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.   
This program is passed a gcc, a list of gcc arguments and a list of
  object files containing IL. It scans the argument list to check if
  we are in whopr mode or not modifies the arguments and needed and
  prints a list of output files on stdout.

  Example:

  $ lto-wrapper gcc/xgcc -B gcc a.o b.o -o test -flto

  The above will print something like
  /tmp/ccwbQ8B2.lto.o

  If WHOPR is used instead, more than one file might be produced
  ./ccXj2DTk.lto.ltrans.o
  ./ccCJuXGv.lto.ltrans.o

◆ INCLUDE_VECTOR

#define INCLUDE_VECTOR

◆ OFFLOAD_TARGET_DEFAULT_ENV

#define OFFLOAD_TARGET_DEFAULT_ENV   "OFFLOAD_TARGET_DEFAULT"

Referenced by compile_offload_image().

◆ OFFLOAD_TARGET_NAMES_ENV

#define OFFLOAD_TARGET_NAMES_ENV   "OFFLOAD_TARGET_NAMES"
Environment variable, used for passing the names of offload targets from GCC
driver to lto-wrapper.   

Referenced by compile_images_for_offload_targets().

◆ TARGET_EXECUTABLE_SUFFIX

#define TARGET_EXECUTABLE_SUFFIX   ""
By default there is no special suffix for target executables.   

Referenced by run_gcc().

Enumeration Type Documentation

◆ lto_mode_d

enum lto_mode_d
Enumerator
LTO_MODE_NONE 
LTO_MODE_LTO 
LTO_MODE_WHOPR 

Function Documentation

◆ access_check()

static int access_check ( const char * name,
int mode )
static
Check whether NAME can be accessed in MODE.  This is like access,
except that it never considers directories to be executable.   

References S_ISDIR, and X_OK.

Referenced by compile_offload_image(), and find_crtoffloadtable().

◆ append_compiler_options()

static void append_compiler_options ( obstack * argv_obstack,
vec< cl_decoded_option > opts )
static
Append options OPTS from lto or offload_lto sections to ARGV_OBSTACK.   

References cl_options, CL_TARGET, cl_option::flags, i, option(), and xassembler_options_error.

Referenced by compile_offload_image(), and run_gcc().

◆ append_diag_options()

static void append_diag_options ( obstack * argv_obstack,
vec< cl_decoded_option > opts )
static
Append diag options in OPTS to ARGV_OBSTACK.   

References i, and option().

Referenced by compile_offload_image().

◆ append_linker_options()

static void append_linker_options ( obstack * argv_obstack,
vec< cl_decoded_option > opts )
static
Append linker options OPTS to ARGV_OBSTACK.   

References CL_COMMON, CL_DRIVER, cl_options, CL_TARGET, cl_option::flags, i, and option().

Referenced by run_gcc().

◆ append_offload_options()

static void append_offload_options ( obstack * argv_obstack,
const char * target,
vec< cl_decoded_option > options )
static
Extract options for TARGET offload compiler from OPTIONS and append
them to ARGV_OBSTACK.   

References gcc_assert, i, NULL, and option().

Referenced by compile_offload_image().

◆ cmp_priority()

int cmp_priority ( const void * a,
const void * b )
Helper for qsort: compare priorities for parallel compilation.   

References a, and b.

Referenced by run_gcc().

◆ compile_images_for_offload_targets()

static void compile_images_for_offload_targets ( unsigned in_argc,
char * in_argv[],
vec< cl_decoded_option > compiler_opts,
vec< cl_decoded_option > linker_opts )
static
The main routine dealing with offloading.
The routine builds a target image for each offload target.  IN_ARGC and
IN_ARGV specify options and input object files.  As all of them could contain
target sections, we pass them all to target compilers.   

References compile_offload_image(), fatal_error(), free(), free_array_of_ptrs(), i, input_location, names, NULL, offload_names, OFFLOAD_TARGET_NAMES_ENV, and parse_env_var().

Referenced by run_gcc().

◆ compile_offload_image()

static const char * compile_offload_image ( const char * target,
const char * compiler_path,
unsigned in_argc,
char * in_argv[],
vec< cl_decoded_option > compiler_opts,
vec< cl_decoded_option > linker_opts,
char ** filename )
static
Prepare a target image for offload TARGET, using mkoffload tool from
COMPILER_PATH.  Return the name of the resultant object file.   

References access_check(), append_compiler_options(), append_diag_options(), append_offload_options(), dumpbase, dumppfx, fatal_error(), fork_execute(), free_array_of_ptrs(), i, input_location, NULL, obstack, OFFLOAD_TARGET_DEFAULT_ENV, parse_env_var(), verbose, and X_OK.

Referenced by compile_images_for_offload_targets().

◆ copy_file()

static void copy_file ( const char * dest,
const char * src )
static
Copy a file from SRC to DEST.   

References fatal_error(), fopen, and input_location.

Referenced by find_crtoffloadtable().

◆ debug_objcopy()

const char * debug_objcopy ( const char * infile,
bool rename )
Copy early debug info sections from INFILE to a new file whose name
is returned.  Return NULL on error.   

References fatal_error(), NULL, O_BINARY, and O_RDONLY.

Referenced by run_gcc().

◆ find_and_merge_options()

static bool find_and_merge_options ( int fd,
off_t file_offset,
const char * prefix,
vec< cl_decoded_option > decoded_cl_options,
bool first,
vec< cl_decoded_option > * opts,
const char * collect_gcc )
static
A subroutine of run_gcc.  Examine the open file FD for lto sections with
name prefix PREFIX, at FILE_OFFSET, and store any options we find in OPTS.
Return true if we found a matching section, false
otherwise.  COLLECT_GCC holds the value of the environment variable with
the same name.   

References free(), get_options_from_collect_gcc_options(), merge_and_complain(), offset, and SEEK_SET.

Referenced by run_gcc().

◆ find_crtoffloadtable()

static void find_crtoffloadtable ( int save_temps,
bool pie_or_shared,
const char * dumppfx )
static
Find the crtoffloadtable.o file in LIBRARY_PATH, make copy and pass name of
the copy to the linker.   

References access_check(), copy_file(), dumppfx, fatal_error(), free_array_of_ptrs(), i, input_location, NULL, parse_env_var(), and R_OK.

Referenced by run_gcc().

◆ find_option() [1/2]

static int find_option ( vec< cl_decoded_option > & options,
cl_decoded_option * option )
static

References find_option(), and option().

◆ find_option() [2/2]

static int find_option ( vec< cl_decoded_option > & options,
size_t opt_index,
unsigned start = 0 )
static
Find option in OPTIONS based on OPT_INDEX, starting at START.  -1
value is returned if the option is not present.   

References i.

Referenced by find_option(), merge_and_complain(), and merge_flto_options().

◆ free_array_of_ptrs()

static void ** free_array_of_ptrs ( void ** ptr,
unsigned n )
static
Auxiliary function that frees elements of PTR and PTR itself.
N is number of elements to be freed.  If PTR is NULL, nothing is freed.
If an element is NULL, subsequent elements are not freed.   

References free(), i, and NULL.

Referenced by compile_images_for_offload_targets(), compile_offload_image(), find_crtoffloadtable(), run_gcc(), and tool_cleanup().

◆ get_options_from_collect_gcc_options()

static vec< cl_decoded_option > get_options_from_collect_gcc_options ( const char * collect_gcc,
const char * collect_gcc_options )
static
Create decoded options from the COLLECT_GCC and COLLECT_GCC_OPTIONS
environment.   

References CL_DRIVER, decode_cmdline_options_to_array(), free(), i, NULL, obstack, and parse_options_from_collect_gcc_options().

Referenced by find_and_merge_options(), and run_gcc().

◆ init_num_threads()

static void init_num_threads ( void )
static
At startup, determine the default number of threads.  It would seem
this should be related to the number of cpus online.   

References free(), i, nthreads_var, NULL, and realloc.

Referenced by run_gcc().

◆ lto_wrapper_cleanup()

static void lto_wrapper_cleanup ( void )
static

References utils_cleanup().

Referenced by main().

◆ main()

◆ make_exists()

static bool make_exists ( void )
static
Test that a make command is present and working, return true if so.   

References CONST_CAST, and NULL.

Referenced by run_gcc().

◆ maybe_unlink()

void maybe_unlink ( const char * file)
Unlink a temporary LTRANS file unless requested otherwise.   

References errno, fatal_error(), file_exists(), input_location, may_unlink_output_file, notice(), output_file, and verbose.

Referenced by run_gcc(), and tool_cleanup().

◆ merge_and_complain()

static void merge_and_complain ( vec< cl_decoded_option > & decoded_options,
vec< cl_decoded_option > fdecoded_options,
vec< cl_decoded_option > decoded_cl_options )
static

◆ merge_flto_options()

static void merge_flto_options ( vec< cl_decoded_option > & decoded_options,
cl_decoded_option * foption,
bool force )
static
Merge -flto FOPTION into vector of DECODED_OPTIONS.  If FORCE is true
then FOPTION overrides previous settings.   

References cl_decoded_option::arg, and find_option().

Referenced by merge_and_complain(), and run_gcc().

◆ parse_env_var()

static unsigned parse_env_var ( const char * str,
char *** pvalues,
const char * append )
static
Parse STR, saving found tokens into PVALUES and return their number.
Tokens are assumed to be delimited by ':'.  If APPEND is non-null,
append it to every token we find.   

References i, and NULL.

Referenced by compile_images_for_offload_targets(), compile_offload_image(), and find_crtoffloadtable().

◆ print_lto_docs_link()

void print_lto_docs_link ( )
Print link to -flto documentation with a hint message.   

References global_dc, inform(), diagnostic_context::make_option_url(), and UNKNOWN_LOCATION.

Referenced by run_gcc().

◆ run_gcc()

static void run_gcc ( unsigned argc,
char * argv[] )
static
Execute gcc. ARGC is the number of arguments. ARGV contains the arguments.  

References ltrans_file_cache::add_to_cache(), append_compiler_options(), append_linker_options(), cmp_priority(), collect_execute(), compile_images_for_offload_targets(), CONST_CAST, ltrans_file_cache::creation_lock, debug_objcopy(), ltrans_file_cache::deletion_lock, diagnostic_color_init(), diagnostic_urls_init(), DIR_SEPARATOR, do_wait(), dumpbase, DUMPBASE_SUFFIX, dumppfx, early_debug_object_names, jobserver_info::error_msg, fatal_error(), find_and_merge_options(), find_crtoffloadtable(), flto_out, fopen, fork_execute(), fputc(), free(), free_array_of_ptrs(), gcc_assert, ltrans_file_cache::get_item(), get_options_from_collect_gcc_options(), global_dc, HOST_BIT_BUCKET, i, init_num_threads(), ltrans_file_cache::item::input, input_location, input_names, jobserver_info::is_active, ltrans_file_cache::load_cache(), ltrans_file_cache::item::lock, lockfile::lock_read(), lockfile::lock_write(), lockfile::lockfile_supported(), lto_mode, LTO_MODE_LTO, LTO_MODE_WHOPR, LTO_SECTION_NAME_PREFIX, ltrans_output_file, ltrans_priorities, make_exists(), makefile, maybe_unlink(), merge_flto_options(), nr, nthreads_var, NULL, num_deb_objs, O_BINARY, O_RDONLY, obstack, offload_names, offload_objects_file_name, OFFLOAD_SECTION_NAME_PREFIX, option(), ltrans_file_cache::item::output, output_names, prepend_xassembler_to_collect_as_options(), print_lto_docs_link(), qsort, ltrans_file_cache::save_cache(), diagnostic_context::set_show_highlight_colors(), startswith(), TARGET_EXECUTABLE_SUFFIX, temporary_obstack, ltrans_file_cache::try_prune(), lockfile::unlock(), verbose, vNULL, W_OK, and warning().

Referenced by main().

◆ tool_cleanup()

Variable Documentation

◆ early_debug_object_names

const char** early_debug_object_names
static

Referenced by run_gcc(), and tool_cleanup().

◆ flto_out

char* flto_out
static

Referenced by run_gcc(), and tool_cleanup().

◆ input_names

char** input_names
static

Referenced by run_gcc(), and tool_cleanup().

◆ lto_mode

enum lto_mode_d lto_mode = LTO_MODE_NONE
static
Current LTO mode.   

Referenced by run_gcc().

◆ ltrans_output_file

char* ltrans_output_file
static

Referenced by run_gcc(), and tool_cleanup().

◆ ltrans_priorities

int* ltrans_priorities
static

Referenced by run_gcc().

◆ makefile

char* makefile
static

Referenced by run_gcc(), and tool_cleanup().

◆ nr

◆ nthreads_var

unsigned long nthreads_var = 0
static
Number of CPUs that can be used for parallel LTRANS phase.   

Referenced by init_num_threads(), and run_gcc().

◆ num_deb_objs

unsigned int num_deb_objs
static

Referenced by run_gcc(), and tool_cleanup().

◆ offload_names

char** offload_names
static

◆ offload_objects_file_name

char* offload_objects_file_name
static

Referenced by run_gcc(), and tool_cleanup().

◆ output_names

char const** output_names
static

Referenced by run_gcc(), and tool_cleanup().

◆ tool_name

const char tool_name[] = "lto-wrapper"
Provided by the tool itself.   
The name of the tool, printed in error messages.   

Referenced by diagnostic_finish_va().

◆ xassembler_options_error

bool xassembler_options_error = false
static