GCC Middle and Back End API Reference
collect2.c File Reference
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "filenames.h"
#include "file-find.h"
#include "collect2.h"
#include "collect2-aix.h"
#include "diagnostic.h"
#include "demangle.h"
#include "obstack.h"
#include "intl.h"
#include "version.h"
Include dependency graph for collect2.c:

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, 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
}
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_ALL = ~0
}

Functions

static symkind is_ctor_dtor (const char *)
static void handler (int)
static void do_wait (const char *, struct pex_obj *)
static void fork_execute (const char *, char **)
static void maybe_unlink (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 char * extract_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)
static void collect_atexit ()
void notice ()
void notice_translated ()
static void handler ()
int file_exists ()
static char * extract_string ()
void dump_ld_file ()
static symkind is_ctor_dtor ()
static void add_lto_object ()
static void maybe_run_lto_and_relink (char **lto_ld_argv, char **object_lst, const char **object, bool force)
int main ()
int collect_wait ()
static void do_wait ()
static void fork_execute ()
static void maybe_unlink ()
static void maybe_unlink_list ()
static void add_to_list ()
static int extract_init_priority ()
static void sort_ids ()
static void write_list ()
static void write_list_with_asm ()
static void write_c_file_stat ()
static void write_c_file_glob ()
static void write_c_file ()
static bool maybe_lto_object_file ()

Variables

int do_collecting = 0
int no_demangle
bool vflag
static int rflag
static int strip_flag
static enum lto_mode_d lto_mode = LTO_MODE_NONE
bool debug
bool helpflag
static int shared_obj
static const char * c_file
static const char * o_file
static char ** lto_o_files
const char * ldout
const char * lderrout
static const char * output_file
static const char * nm_file_name
static const char * strip_file_name
const char * c_file_name
static char * initname
static char * fininame
static struct head constructors
static struct head destructors
static struct head frame_tables
static bool at_file_supplied
static char * response_file
struct obstack temporary_obstack
char * temporary_firstobj
static const char * target_system_root = ""
bool may_unlink_output_file = false
static struct lto_object_list lto_objects
static struct path_prefix cpath path
static long sequence_number = 0

Macro Definition Documentation

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

This must match tree.h.

Referenced by output_section_asm_op().

#define DO_COLLECT_EXPORT_LIST   0

Cook up an always defined indication of whether we proceed the "EXPORT_LIST" way.

#define LIBRARY_PATH_ENV   "LIBRARY_PATH"
#define NAME__MAIN   "__main"

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

#define NM_FLAGS   "-n"

Default flags to pass to nm.

#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.

#define SHLIB_SUFFIX   ".so"
#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-2013 Free Software Foundation, Inc. Contributed by Chris Smith (csmit.nosp@m.h@co.nosp@m.nvex..nosp@m.com). Heavily modified by Michael Meissner (meiss.nosp@m.ner@.nosp@m.cygnu.nosp@m.s.co.nosp@m.m), Per Bothner (bothn.nosp@m.er@c.nosp@m.ygnus.nosp@m..com), and John Gilmore (gnu@c.nosp@m.ygnu.nosp@m.s.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

typedef int scanfilter

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


Enumeration Type Documentation

enum lto_mode_d
Enumerator:
LTO_MODE_NONE 
LTO_MODE_LTO 
LTO_MODE_WHOPR 
LTO_MODE_NONE 
LTO_MODE_LTO 
LTO_MODE_WHOPR 

... and which kinds of symbols are to be considered.

Enumerator:
SCAN_NOTHING 
SCAN_CTOR 
SCAN_DTOR 
SCAN_INIT 
SCAN_FINI 
SCAN_DWEH 
SCAN_ALL 
enum scanpass

Enumerations describing which pass this is for scanning the program file ...

Enumerator:
PASS_FIRST 
PASS_OBJ 
PASS_LIB 
PASS_SECOND 
PASS_LTOINFO 
enum symkind

Special kinds of symbols that a name may denote.

Enumerator:
SYM_REGULAR 
SYM_CTOR 
SYM_DTOR 
SYM_INIT 
SYM_FINI 
SYM_DWEH 

Function Documentation

static void add_lto_object ( )
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 CONST_CAST2, lto_object_list::first, lto_objects, PASS_LTOINFO, SCAN_ALL, and scan_prog_file().

Referenced by maybe_lto_object_file().

static void add_to_list ( struct head ,
const char *   
)
static

Referenced by scan_prog_file().

static void add_to_list ( )
static

Add a name to a linked list.

Check for duplicate symbols.

References id::name, and id::next.

static void collect_atexit ( )
static

Delete tempfiles and exit function.

int collect_wait ( )

Wait for a process to finish, and exit if a nonzero status is found.

References errno.

static void do_wait ( const char *  ,
struct pex_obj *   
)
static
static void do_wait ( )
static
void dump_ld_file ( )

Make sure we output at least one space, or the demangled symbol name will run into whatever text follows.

static int extract_init_priority ( const char *  )
static
static int extract_init_priority ( )
static

Grab the init priority number from an init function name that looks like "_GLOBAL_.I.12345.foo".

Extract init_p number from ctor/dtor name.

References frame_tables, head::number, and output_file.

static char* extract_string ( const char **  )
static
static char* extract_string ( )
static

Parse a reasonable subset of shell quoting syntax.

int file_exists ( )
static void fork_execute ( const char *  ,
char **   
)
static
static void fork_execute ( )
static
static void handler ( int  )
static
static void handler ( )
static
static symkind is_ctor_dtor ( const char *  )
static
static symkind is_ctor_dtor ( )
static

Return the kind of symbol denoted by name S.

int main ( )

Main program.

 The kinds of symbols we will have to consider when scanning the
 outcome of a first pass link.  This is ALL to start with, then might
 be adjusted before getting to the first pass link per se, typically on
 AIX where we perform an early scan of objects and libraries to fetch
 the list of global ctors/dtors and make sure they are not garbage
 collected.   
 Suppress demangling by the real linker, which may be broken.   
 Unlock the stdio streams.   
 Do not invoke xcalloc before this point, since locale needs to be
 set first, in case a diagnostic is issued.   
 Parse command line early for instances of -debug.  This allows
 the debug flag to be set before functions like find_a_file()
 are called.  We also look for the -flto or -flto-partition=none flag to know
 what LTO mode we are in.   
 -fno-profile-arcs -fno-test-coverage -fno-branch-probabilities
 -fno-exceptions -w -fno-whole-program  
 Extract COMPILER_PATH and PATH into our prefix list.   
 Try to discover a valid linker/nm/strip to use.   
 Maybe we know the right file to use (if not cross).   
 Search the (target-specific) compiler dirs for ld'.   
 Likewise for `collect-ld'.   
 Search the compiler directories for `ld'.  We have protection against
 recursive calls in find_a_file.   
 Search the ordinary system bin directories
 for `ld' (if native linking) or `TARGET-ld' (if cross).   
 Determine the full path name of the C compiler to use.   
 Here it should be safe to use the system search path since we should have
 already qualified the name of the compiler when it is needed.   
 Make temp file names.   
 Get any options that the upper GCC wants to pass to the sub-GCC.

 AIX support needs to know if -shared has been specified before
 parsing commandline arguments.   
 !!! When GCC calls collect2,
 it does not know whether it is calling collect2 or ld.
 So collect2 cannot meaningfully understand any options
 except those ld understands.
 If you propose to make GCC pass some other option,
 just imagine what will happen if ld is really ld!!!   
 Parse arguments.  Remember output file spec, pass the rest to ld.   
 After the first file, put in the c++ rt0.   
                 Already parsed.   
                 Do not pass -fuse-ld={bfd|gold} to the linker.  
                 place o_file BEFORE this argument!  
                 We must strip after the nm run, otherwise C++ linking
                 will not work.  Thus we strip in the second ld run, or
                 else with strip if there is no second ld run.   
                 place o_file BEFORE this argument!  
 Load the program, searching all libraries and attempting to provide
 undefined symbols from repository information.

 If -r or they will be run via some other method, do not build the
 constructor or destructor list, just return now.   
   Perform the first pass link now, if we're about to exit or if we need
   to scan for things we haven't collected yet before pursuing further.

   On AIX, the latter typically includes nothing for shared objects or
   frame tables for an executable, out of what the required early scan on
   objects and libraries has performed above.  In the !shared_obj case, we
   expect the relevant tables to be dragged together with their associated
   functions from precise cross reference insertions by the compiler.   
 Unless we have done it all already, examine the namelist and search for
 static constructors and destructors to call.  Write the constructor and
 destructor tables to a .s file and reload.   
 If the scan exposed nothing of special interest, there's no need to
 generate the glue code and relink so return now.   
     Do tlink without additional code generation now if we didn't
     do it earlier for scanning purposes.   
     Strip now if it was requested on the command line.   
 Sort ctor and dtor lists by priority.   
 Tell the linker that we have initializer and finalizer functions.   
 End of arguments to second link phase.   
 Assemble the constructor and destructor tables.
 Link the tables in with the rest of the program.   
 Otherwise, simply call ld because tlink is already done.   
 Let scan_prog_file do any final mods (OSF/rose needs this for
 constructors/destructors in shared libraries.   
static bool maybe_lto_object_file ( )
static

Check to make sure the file is an LTO object file.

References add_lto_object(), and lto_objects.

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.

   If file contains LTO info, add it to the list of LTO objects.   
   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.   
     There is at least one object file containing LTO info,
     so we need to run the LTO back end and relink.

     To do so we build updated ld arguments with first
     LTO object replaced by all partitions and other LTO
     objects removed.   
     Add LTO objects to the wrapper command line.   
     Run the LTO back end.   
     Compute memory needed for new LD arguments.  At most number of original arguemtns
     plus number of partitions.   
     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.   
     Copy all arguments until we find first LTO file.   
     Now insert all LTO partitions.   
     ... and copy the rest.   
     Run the linker again, this time replacing the object files
     optimized by the LTO with the temporary file generated by the LTO.   
     Our caller is relying on us to do the link
     even though there is no LTO back end work to be done.   
static void maybe_unlink ( const char *  )
static
static void maybe_unlink ( )
static

Unlink FILE unless we are debugging or this is the output_file and we may not unlink it.

static void maybe_unlink_list ( char **  )
static
static void maybe_unlink_list ( )
static

Call maybe_unlink on the NULL-terminated list, FILE_LIST.

References id::name, and id::next.

void notice ( )

Notify user of a non-error.

void notice_translated ( )

Notify user of a non-error, without translating the format string.

static void post_ld_pass ( bool  temp_file)
static
static void process_args ( int *  argcp,
char **  argv 
)
static
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.

 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 we do not have an `nm', complain.   
 Trace if needed.   
 Read each line of nm output.   
         Look for the LTO info marker symbol, and add filename to
         the LTO objects list if found.   
               We need to read all the input, so we can't just
               return here.  But we can avoid useless work.   
     If it contains a constructor or destructor name, add the name
     to the appropriate list unless this is a kind of symbol we're
     not supposed to even consider.   
     Find the end of the symbol name.
     Do not include `|', because Encore nm can tack that on the end.   

References add_to_list(), constructors, destructors, fatal_error(), frame_tables, PASS_LIB, SCAN_CTOR, SCAN_DTOR, SCAN_DWEH, SCAN_FINI, SCAN_INIT, SYM_CTOR, SYM_DTOR, SYM_DWEH, SYM_FINI, and SYM_INIT.

Referenced by add_lto_object().

static void sort_ids ( struct head )
static
static void sort_ids ( )
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.

 id holds the current element to insert.  id_next holds the next
 element to insert.  id_ptr iterates through the already sorted elements
 looking for the place to insert id.   
 We don't have any sorted elements yet.   
           If the sequence numbers are the same, we put the id from the
           file later on the command line later in the list.   
           Hack: do lexical compare, too.
        || (id->sequence == (*id_ptr)->sequence
            && strcmp (id->name, (*id_ptr)->name) > 0)  
 Now set the sequence numbers properly so write_c_file works.   
static void write_c_file ( FILE *  ,
const char *   
)
static
static void write_c_file ( )
static
static void write_c_file_glob ( FILE *  ,
const char *   
)
static
static void write_c_file_glob ( )
static

Write the constructor/destructor tables.

Write the tables as C code.

     This must match what's in frame.h.   
static void write_c_file_stat ( FILE *  ,
const char *   
)
static
static void write_c_file_stat ( )
static

Write out the constructor and destructor tables statically (for a shared object), along with the functions to execute them.

 Figure out name of output_file, stripping off .so version.   
 q points to null at end of the string (or . of the .so version)  
 Write the tables as C code.   
     This must match what's in frame.h.   
static void write_list ( FILE *  ,
const char *  ,
struct id  
)
static
static void write_list ( )
static

Write: `prefix', the names on list LIST, `suffix'.

static void write_list_with_asm ( FILE *  ,
const char *  ,
struct id  
)
static
static void write_list_with_asm ( )
static

Added for debugging purpose.


Variable Documentation

bool at_file_supplied
static
const char* c_file
static
const char* c_file_name
struct head constructors
static

Referenced by scan_prog_file().

DEBUG_FUNCTION void debug

Unified dump function for a DATA_REFERENCE structure.

Generic dump for the above.

Print the slot this node is in, and its code, and address.

Referenced by debug(), find_a_file(), find_file_set_debug(), and prefix_from_string().

struct head destructors
static

Referenced by scan_prog_file().

int do_collecting = 0
char * fininame
static
struct head frame_tables
static
bool helpflag
char* initname
static
const char* lderrout
const char* ldout
enum lto_mode_d lto_mode = LTO_MODE_NONE
static

Current LTO mode.

char** lto_o_files
static
struct lto_object_list lto_objects
static
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.

const char* nm_file_name
static
int no_demangle

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

const char* o_file
static
const char* output_file
static

Referenced by extract_init_priority().

struct path_prefix cpath path
static

We maintain two prefix lists: one from COMPILER_PATH environment variable and one from the PATH variable.

Referenced by create_block_for_threading(), and read_state_type().

char* response_file
static
int rflag
static
long sequence_number = 0
static
int shared_obj
static
const char* strip_file_name
static
int strip_flag
static
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.

char* temporary_firstobj
struct obstack temporary_obstack

Referenced by obstack_fgets(), and tlink_execute().

bool vflag