GCC Middle and Back End API Reference
collect2.cc File Reference
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "filenames.h"
#include "file-find.h"
#include "simple-object.h"
#include "lto-section-names.h"
#include "collect2.h"
#include "collect2-aix.h"
#include "collect-utils.h"
#include "diagnostic.h"
#include "demangle.h"
#include "obstack.h"
#include "intl.h"
#include "version.h"
Include dependency graph for collect2.cc:

Data Structures

struct  id
 
struct  head
 
struct  lto_object
 
struct  lto_object_list
 

Macros

#define TARGET_64BIT   TARGET_64BIT_DEFAULT
 
#define LIBRARY_PATH_ENV   "LIBRARY_PATH"
 
#define COLLECT
 
#define OBJECT_FORMAT_NONE
 
#define NM_FLAGS   "-n"
 
#define NAME__MAIN   "__main"
 
#define DEFAULT_INIT_PRIORITY   65535
 
#define COLLECT_SHARED_INIT_FUNC(STREAM, FUNC)    fprintf ((STREAM), "void _GLOBAL__DI() {\n\t%s();\n}\n", (FUNC))
 
#define COLLECT_SHARED_FINI_FUNC(STREAM, FUNC)    fprintf ((STREAM), "void _GLOBAL__DD() {\n\t%s();\n}\n", (FUNC))
 
#define SHLIB_SUFFIX   ".so"
 
#define DO_COLLECT_EXPORT_LIST   0
 

Typedefs

typedef int scanfilter
 

Enumerations

enum  lto_mode_d { LTO_MODE_NONE , LTO_MODE_LTO , LTO_MODE_WHOPR }
 
enum  symkind {
  SYM_REGULAR = 0 , SYM_CTOR = 1 , SYM_DTOR = 2 , SYM_INIT = 3 ,
  SYM_FINI = 4 , SYM_DWEH = 5 , SYM_AIXI = 6 , SYM_AIXD = 7
}
 
enum  scanpass {
  PASS_FIRST , PASS_OBJ , PASS_LIB , PASS_SECOND ,
  PASS_LTOINFO
}
 
enum  scanfilter_masks {
  SCAN_NOTHING = 0 , SCAN_CTOR = 1 << SYM_CTOR , SCAN_DTOR = 1 << SYM_DTOR , SCAN_INIT = 1 << SYM_INIT ,
  SCAN_FINI = 1 << SYM_FINI , SCAN_DWEH = 1 << SYM_DWEH , SCAN_AIXI = 1 << SYM_AIXI , SCAN_AIXD = 1 << SYM_AIXD ,
  SCAN_ALL = ~0
}
 

Functions

static symkind is_ctor_dtor (const char *)
 
static void maybe_unlink_list (char **)
 
static void add_to_list (struct head *, const char *)
 
static int extract_init_priority (const char *)
 
static void sort_ids (struct head *)
 
static void write_list (FILE *, const char *, struct id *)
 
static void write_list_with_asm (FILE *, const char *, struct id *)
 
static void write_c_file (FILE *, const char *)
 
static void write_c_file_stat (FILE *, const char *)
 
static void write_c_file_glob (FILE *, const char *)
 
static charextract_string (const char **)
 
static void post_ld_pass (bool)
 
static void process_args (int *argcp, char **argv)
 
static void scan_prog_file (const char *, scanpass, scanfilter)
 
void tool_cleanup (bool from_signal)
 
static void collect_atexit (void)
 
void notice_translated (const char *cmsgid,...)
 
int file_exists (const char *name)
 
static void add_lto_object (struct lto_object_list *list, const char *name)
 
static void maybe_run_lto_and_relink (char **lto_ld_argv, char **object_lst, const char **object, bool force)
 
static void do_link (char **ld_argv, const char *atsuffix)
 
int main (int argc, char **argv)
 
void maybe_unlink (const char *file)
 
static int has_lto_section (void *data, const char *name, off_t offset, off_t length)
 
static bool is_lto_object_file (const char *prog_name)
 

Variables

int do_collecting = 0
 
int no_demangle
 
static int rflag
 
static int strip_flag
 
static enum lto_mode_d lto_mode = LTO_MODE_NONE
 
bool helpflag
 
static int shared_obj
 
static int static_obj
 
static const charc_file
 
static const charo_file
 
static char ** lto_o_files
 
static const charoutput_file
 
static const charnm_file_name
 
static const charstrip_file_name
 
const charc_file_name
 
static charinitname
 
static charfininame
 
static struct head constructors
 
static struct head destructors
 
static struct head frame_tables
 
bool at_file_supplied
 
struct obstack temporary_obstack
 
chartemporary_firstobj
 
static const chartarget_system_root = ""
 
bool may_unlink_output_file = false
 
static struct lto_object_list lto_objects
 
const char tool_name [] = "collect2"
 
static struct path_prefix cpath path
 
static long sequence_number = 0
 

Macro Definition Documentation

◆ COLLECT

#define COLLECT

◆ COLLECT_SHARED_FINI_FUNC

#define COLLECT_SHARED_FINI_FUNC ( STREAM,
FUNC )    fprintf ((STREAM), "void _GLOBAL__DD() {\n\t%s();\n}\n", (FUNC))

Referenced by write_c_file_stat().

◆ COLLECT_SHARED_INIT_FUNC

#define COLLECT_SHARED_INIT_FUNC ( STREAM,
FUNC )    fprintf ((STREAM), "void _GLOBAL__DI() {\n\t%s();\n}\n", (FUNC))

Referenced by write_c_file_stat().

◆ DEFAULT_INIT_PRIORITY

◆ DO_COLLECT_EXPORT_LIST

#define DO_COLLECT_EXPORT_LIST   0
Cook up an always defined indication of whether we proceed the
"EXPORT_LIST" way.   

Referenced by main().

◆ LIBRARY_PATH_ENV

#define LIBRARY_PATH_ENV   "LIBRARY_PATH"

Referenced by main(), and driver::maybe_run_linker().

◆ NAME__MAIN

#define NAME__MAIN   "__main"
Some systems use __main in a way incompatible with its use in gcc, in these
cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
give the same symbol without quotes for an alternative entry point.   

Referenced by write_c_file_glob().

◆ NM_FLAGS

#define NM_FLAGS   "-n"
Default flags to pass to nm.   

Referenced by scan_prog_file().

◆ OBJECT_FORMAT_NONE

#define OBJECT_FORMAT_NONE
On certain systems, we have code that works by scanning the object file
directly.  But this code uses system-specific header files and library
functions, so turn it off in a cross-compiler.  Likewise, the names of
the utilities are not correct for a cross-compiler; we have to hope that
cross-versions are in the proper directories.   
If we cannot use a special method, use the ordinary one:
run nm to find what symbols are present.
In a cross-compiler, this means you need a cross nm,
but that is not quite as unpleasant as special headers.   

◆ SHLIB_SUFFIX

#define SHLIB_SUFFIX   ".so"

Referenced by write_c_file_stat().

◆ TARGET_64BIT

#define TARGET_64BIT   TARGET_64BIT_DEFAULT
Collect static initialization info into data structures that can be
   traversed by C++ initialization and finalization routines.
   Copyright (C) 1992-2024 Free Software Foundation, Inc.
   Contributed by Chris Smith (csmith@convex.com).
   Heavily modified by Michael Meissner (meissner@cygnus.com),
   Per Bothner (bothner@cygnus.com), and John Gilmore (gnu@cygnus.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/>.   
Build tables of static constructors and destructors and run ld.   
TARGET_64BIT may be defined to use driver specific functionality.  

Typedef Documentation

◆ scanfilter

This type is used for parameters and variables which hold
combinations of the flags in enum scanfilter_masks.   

Enumeration Type Documentation

◆ lto_mode_d

Enumerator
LTO_MODE_NONE 
LTO_MODE_LTO 
LTO_MODE_WHOPR 

◆ scanfilter_masks

... and which kinds of symbols are to be considered.   
Enumerator
SCAN_NOTHING 
SCAN_CTOR 
SCAN_DTOR 
SCAN_INIT 
SCAN_FINI 
SCAN_DWEH 
SCAN_AIXI 
SCAN_AIXD 
SCAN_ALL 

◆ scanpass

Enumerations describing which pass this is for scanning the
program file ...   
Enumerator
PASS_FIRST 
PASS_OBJ 
PASS_LIB 
PASS_SECOND 
PASS_LTOINFO 

◆ symkind

Special kinds of symbols that a name may denote.   
Enumerator
SYM_REGULAR 
SYM_CTOR 
SYM_DTOR 
SYM_INIT 
SYM_FINI 
SYM_DWEH 
SYM_AIXI 
SYM_AIXD 

Function Documentation

◆ add_lto_object()

static void add_lto_object ( struct lto_object_list * list,
const char * name )
static
Search for NAME using prefix list PPREFIX.  We only look for executable
files.

Return 0 if not found, otherwise return its name, allocated with malloc.   
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.   

References lto_object_list::first, ggc_alloc(), lto_object_list::last, lto_object::name, lto_object::next, and NULL.

Referenced by scan_prog_file().

◆ add_to_list()

static void add_to_list ( struct head * head_ptr,
const char * name )
static
Add a name to a linked list.   

References free(), ggc_alloc(), id::name, id::next, and sequence_number.

Referenced by main(), and scan_prog_file().

◆ collect_atexit()

static void collect_atexit ( void )
static

References tool_cleanup().

Referenced by main().

◆ do_link()

static void do_link ( char ** ld_argv,
const char * atsuffix )
static
Entry point for linker invoation.  Called from main in collect2.cc.
LD_ARGV is an array of arguments for the linker.   

References at_file_supplied, collect_execute(), collect_wait(), error(), ggc_alloc(), may_unlink_output_file, and NULL.

Referenced by main().

◆ extract_init_priority()

static int extract_init_priority ( const char * name)
static
Grab the init priority number from an init function name that
looks like "_GLOBAL_.I.12345.foo".   

References DEFAULT_INIT_PRIORITY, ggc_alloc(), INT_MIN, is_ctor_dtor(), id::name, SYM_AIXD, and SYM_AIXI.

Referenced by sort_ids().

◆ extract_string()

static char * extract_string ( const char ** pp)
static
Parse a reasonable subset of shell quoting syntax.   

References ggc_alloc(), and temporary_obstack.

Referenced by main().

◆ file_exists()

int file_exists ( const char * name)

References R_OK.

Referenced by maybe_unlink().

◆ has_lto_section()

static int has_lto_section ( void * data,
const char * name,
off_t offset,
off_t length )
static
Check to make sure the file is an LTO object file.   

References LTO_SECTION_NAME_PREFIX, id::name, OFFLOAD_SECTION_NAME_PREFIX, and startswith().

Referenced by is_lto_object_file().

◆ is_ctor_dtor()

static symkind is_ctor_dtor ( const char * s)
static
Return the kind of symbol denoted by name S.   

References ggc_alloc(), names, NULL, SYM_AIXD, SYM_AIXI, SYM_CTOR, SYM_DTOR, SYM_DWEH, SYM_FINI, SYM_INIT, and SYM_REGULAR.

Referenced by extract_init_priority(), and scan_prog_file().

◆ is_lto_object_file()

static bool is_lto_object_file ( const char * prog_name)
static

◆ main()

◆ maybe_run_lto_and_relink()

static void maybe_run_lto_and_relink ( char ** lto_ld_argv,
char ** object_lst,
const char ** object,
bool force )
static
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 considered 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.   

References at_file_supplied, collect_execute(), CONST_CAST2, do_wait(), end(), fatal_error(), lto_object_list::first, fork_execute(), free(), gcc_assert, ggc_alloc(), i, input_location, lto_o_files, lto_objects, maybe_unlink_list(), lto_object::name, lto_object::next, NULL, PASS_LTOINFO, post_ld_pass(), SCAN_ALL, scan_prog_file(), temporary_firstobj, and temporary_obstack.

Referenced by main().

◆ maybe_unlink()

void maybe_unlink ( const char * file)
Unlink FILE unless we are debugging or this is the output_file
and we may not unlink it.   

Referenced by main(), maybe_unlink_list(), and tool_cleanup().

◆ maybe_unlink_list()

static void maybe_unlink_list ( char ** file_list)
static
Call maybe_unlink on the NULL-terminated list, FILE_LIST.   

References ggc_alloc(), and maybe_unlink().

Referenced by maybe_run_lto_and_relink(), and tool_cleanup().

◆ notice_translated()

void notice_translated ( const char * cmsgid,
... )
Notify user of a non-error, without translating the format string.   

References ap, ggc_alloc(), and vfprintf().

Referenced by main().

◆ post_ld_pass()

static void post_ld_pass ( bool temp_file)
static

Referenced by main(), and maybe_run_lto_and_relink().

◆ process_args()

static void process_args ( int * argcp,
char ** argv )
static

Referenced by main(), and ref_maybe_used_by_call_p_1().

◆ scan_prog_file()

static void scan_prog_file ( const char * prog_name,
scanpass which_pass,
scanfilter filter )
static
Scan the name list of the loaded program for the symbols g++ uses for
static constructors and destructors.

The SCANPASS argument tells which collect processing pass this is for and
the SCANFILTER argument tells which kinds of symbols to consider in this
pass.  Symbols of a special kind not in the filter mask are considered as
regular ones.

The constructor table begins at __CTOR_LIST__ and contains a count of the
number of pointers (or -1 if the constructors are built in a separate
section by the linker), followed by the pointers to the constructor
functions, terminated with a null pointer.  The destructor table has the
same format, and begins at __DTOR_LIST__.   
Generic version to scan the name list of the loaded program for
the symbols g++ uses for static constructors and destructors.   

References _, add_lto_object(), add_to_list(), CONST_CAST2, constructors, debug, destructors, do_wait(), end(), errno, fatal_error(), frame_tables, ggc_alloc(), HOST_BIT_BUCKET, input_location, is_ctor_dtor(), is_lto_object_file(), lto_objects, nm_file_name, NM_FLAGS, NULL, PASS_LIB, PASS_LTOINFO, PASS_SECOND, SCAN_CTOR, SCAN_DTOR, SCAN_DWEH, SCAN_FINI, SCAN_INIT, SYM_CTOR, SYM_DTOR, SYM_DWEH, SYM_FINI, SYM_INIT, and verbose.

Referenced by main(), and maybe_run_lto_and_relink().

◆ sort_ids()

static void sort_ids ( struct head * head_ptr)
static
Insertion sort the ids from ctor/dtor list HEAD_PTR in descending order.
ctors will be run from right to left, dtors from left to right.   

References extract_init_priority(), ggc_alloc(), id::name, id::next, NULL, id::sequence, and sequence_number.

Referenced by main().

◆ tool_cleanup()

void tool_cleanup ( bool from_signal)
Delete tempfiles and exit function.   

Referenced by collect_atexit().

◆ write_c_file()

static void write_c_file ( FILE * stream,
const char * name )
static

◆ write_c_file_glob()

static void write_c_file_glob ( FILE * stream,
const char * name )
static
Write the constructor/destructor tables.   

References constructors, destructors, head::first, frame_tables, ggc_alloc(), NAME__MAIN, head::number, write_list(), and write_list_with_asm().

Referenced by write_c_file().

◆ write_c_file_stat()

static void write_c_file_stat ( FILE * stream,
const char * name )
static
Write out the constructor and destructor tables statically (for a shared
object), along with the functions to execute them.   

References COLLECT_SHARED_FINI_FUNC, COLLECT_SHARED_INIT_FUNC, constructors, debug, destructors, fininame, head::first, frame_tables, free(), ggc_alloc(), initname, notice(), NULL, head::number, output_file, r, shared_obj, SHLIB_SUFFIX, write_list(), and write_list_with_asm().

Referenced by write_c_file().

◆ write_list()

static void write_list ( FILE * stream,
const char * prefix,
struct id * list )
static
Write: `prefix', the names on list LIST, `suffix'.   

References ggc_alloc(), id::next, and id::sequence.

Referenced by write_c_file_glob(), and write_c_file_stat().

◆ write_list_with_asm()

static void write_list_with_asm ( FILE * stream,
const char * prefix,
struct id * list )
static
Added for debugging purpose.   

References ggc_alloc(), id::name, id::next, and id::sequence.

Referenced by write_c_file_glob(), and write_c_file_stat().

Variable Documentation

◆ at_file_supplied

◆ c_file

const char* c_file
static

Referenced by main(), and tool_cleanup().

◆ c_file_name

const char* c_file_name

Referenced by main().

◆ constructors

struct head constructors
static

◆ destructors

struct head destructors
static

◆ do_collecting

int do_collecting = 0

Referenced by main().

◆ fininame

char * fininame
static

Referenced by main(), and write_c_file_stat().

◆ frame_tables

struct head frame_tables
static

◆ helpflag

bool helpflag

Referenced by main().

◆ initname

char* initname
static

Referenced by main(), and write_c_file_stat().

◆ lto_mode

enum lto_mode_d lto_mode = LTO_MODE_NONE
static
Current LTO mode.   

Referenced by main().

◆ lto_o_files

char** lto_o_files
static

◆ lto_objects

struct lto_object_list lto_objects
static

◆ may_unlink_output_file

bool may_unlink_output_file = false
Whether we may unlink the output file, which should be set as soon as we
know we have successfully produced it.  This is typically useful to prevent
blindly attempting to unlink a read-only output that the target linker
would leave untouched.   

Referenced by do_link(), and maybe_unlink().

◆ nm_file_name

const char* nm_file_name
static

Referenced by main(), and scan_prog_file().

◆ no_demangle

int no_demangle
Nonzero if we should suppress the automatic demangling of identifiers
in linker error messages.  Set from COLLECT_NO_DEMANGLE.   

Referenced by main().

◆ o_file

const char* o_file
static

Referenced by main(), and tool_cleanup().

◆ output_file

const char* output_file
static

◆ path

◆ rflag

int rflag
static

Referenced by main().

◆ sequence_number

long sequence_number = 0
static

Referenced by add_to_list(), and sort_ids().

◆ shared_obj

int shared_obj
static

◆ static_obj

int static_obj
static

Referenced by main().

◆ strip_file_name

const char* strip_file_name
static

Referenced by main().

◆ strip_flag

int strip_flag
static

Referenced by main().

◆ target_system_root

const char* target_system_root = ""
static
A string that must be prepended to a target OS path in order to find
it on the host system.   

Referenced by driver::finalize(), main(), driver::maybe_print_and_exit(), and driver::set_up_specs().

◆ temporary_firstobj

char* temporary_firstobj

Referenced by main(), and maybe_run_lto_and_relink().

◆ temporary_obstack

◆ tool_name

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