gcc is a driver program. It performs its job by invoking a sequence of other programs to do the work of compiling, assembling and linking. GCC interprets its command-line parameters and uses these to deduce which programs it should invoke, and which command-line options it ought to place on their command lines. This behavior is controlled by spec strings. In most cases there is one spec string for each program that GCC can invoke, but a few programs have multiple spec strings to control their behavior. The spec strings built into GCC can be overridden by using the -specs= command-line switch to specify a spec file.
Spec files are plaintext files that are used to construct spec strings. They consist of a sequence of directives separated by blank lines. The type of directive is determined by the first non-whitespace character on the line, which can be one of the following:
Issues a command to the spec file processor. The commands that can appear here are:
%include <file>
Search for file and insert its text at the current point in the specs file.
%include_noerr <file>
Just like %include, but do not generate an error message if the include file cannot be found.
%rename old_namenew_name
Rename the spec string old_name to new_name.
Creates a new [``suffix`] spec` pair. All lines after this directive and up to the next directive or blank line are considered to make up the spec string for the indicated suffix. When the compiler encounters an input file with the named suffix, it processes the spec string in order to work out how to compile that file. For example:
.ZZ:
z-compile -input %i
This says that any input file whose name ends in .ZZ should be passed to the program z-compile, which should be invoked with the command-line switch -input and with the result of performing the %i substitution. (See below.)
As an alternative to providing a spec string, the text following a suffix directive can be one of the following:
This says that the suffix is an alias for a known language. This is similar to using the -x command-line switch to GCC to specify a language explicitly. For example:
.ZZ:
@c++
Says that .ZZ files are, in fact, C++ source files.
This causes an error messages saying:
``name`` compiler not installed on this system.
GCC already has an extensive list of suffixes built into it.
This directive adds an entry to the end of the list of suffixes, but since the list is searched from the end backwards, it is effectively possible to override earlier entries using this technique.
GCC has the following spec strings built into it. Spec files can
override these strings or create their own. Note that individual targets can also add their own spec strings to this list.
asm Options to pass to the assembler
asm_final Options to pass to the assembler post-processor
cpp Options to pass to the C preprocessor
cc1 Options to pass to the C compiler
cc1plus Options to pass to the C++ compiler
endfile Object files to include at the end of the link
link Options to pass to the linker
lib Libraries to include on the command line to the linker
libgcc Decides which GCC support library to pass to the linker
linker Sets the name of the linker
predefines Defines to be passed to the C preprocessor
signed_char Defines to pass to CPP to say whether ``char`` is signed
by default
startfile Object files to include at the start of the link
Here is a small example of a spec file:
%rename lib old_lib
*lib:
--start-group -lgcc -lc -leval1 --end-group %(old_lib)
This example renames the spec called lib to old_lib and then overrides the previous definition of lib with a new one. The new definition adds in some extra command-line options before including the text of the old definition.
Spec strings are a list of command-line options to be passed to their corresponding program. In addition, the spec strings can contain %-prefixed sequences to substitute variable text or to conditionally insert text into the command line. Using these constructs it is possible to generate quite complex command lines.
Here is a table of all defined %-sequences for spec strings. Note that spaces are not generated automatically around the results of expanding these sequences. Therefore you can concatenate them together or combine them with constant text in a single argument.
Call the named function function, passing it args. args is first processed as a nested spec string, then split into an argument vector in the usual fashion. The function returns a string which is processed as if it had appeared literally as part of the current spec.
The following built-in spec functions are provided:
The getenv spec function takes two arguments: an environment variable name and a string. If the environment variable is not defined, a fatal error is issued. Otherwise, the return value is the value of the environment variable concatenated with the string. For example, if TOPDIR is defined as /path/to/top, then:
%:getenv(TOPDIR /include)
expands to /path/to/top/include.
The if-exists spec function takes one argument, an absolute pathname to a file. If the file exists, if-exists returns the pathname. Here is a small example of its usage:
*startfile:
crt0%O%s %:if-exists(crti%O%s) crtbegin%O%s
The if-exists-else spec function is similar to the if-exists spec function, except that it takes two arguments. The first argument is an absolute pathname to a file. If the file exists, if-exists-else returns the pathname. If it does not exist, it returns the second argument. This way, if-exists-else can be used to select one file or another, based on the existence of the first. Here is a small example of its usage:
*startfile:
crt0%O%s %:if-exists(crti%O%s) \
%:if-exists-else(crtbeginT%O%s crtbegin%O%s)
The replace-outfile spec function takes two arguments. It looks for the first argument in the outfiles array and replaces it with the second argument. Here is a small example of its usage:
%{fgnu-runtime:%:replace-outfile(-lobjc -lobjc-gnu)}
The remove-outfile spec function takes one argument. It looks for the first argument in the outfiles array and removes it. Here is a small example its usage:
%:remove-outfile(-lm)
The pass-through-libs spec function takes any number of arguments. It finds any -l options and any non-options ending in .a (which it assumes are the names of linker input library archive files) and returns a result containing all the found arguments each prepended by -plugin-opt=-pass-through= and joined by spaces. This list is intended to be passed to the LTO linker plugin.
%:pass-through-libs(%G %L %G)
The print-asm-header function takes no arguments and simply prints a banner like:
Assembler options
=================
Use "-Wa,OPTION" to pass "OPTION" to the assembler.
It is used to separate compiler options from assembler options in the --target-help output.
Substitutes X if one or more switches whose names start with -S are specified to GCC. Normally X is substituted only once, no matter how many such switches appeared. However, if %* appears somewhere in X, then X is substituted once for each matching switch, with the %* replaced by the part of that switch matching the *.
If %* appears as the last part of a spec sequence then a space is added after the end of the last substitution. If there is more text in the sequence, however, then a space is not generated. This allows the %* substitution to be used as part of a larger string. For example, a spec string like this:
%{mcu=*:--script=%*/memory.ld}
when matching an option like -mcu=newchip produces:
--script=newchip/memory.ld
Substitutes X if either -S or -P is given to GCC. This may be combined with !, ., ,, and * sequences as well, although they have a stronger binding than the |. If %* appears in X, all of the alternatives must be starred, and only the first matching alternative is substituted.
For example, a spec string like this:
%{.c:-foo} %{!.c:-bar} %{.c|d:-baz} %{!.c|d:-boggle}
outputs the following command-line options from the following input command-line options:
fred.c -foo -baz
jim.d -bar -boggle
-d fred.c -foo -baz -boggle
-d jim.d -bar -baz -boggle
If S is given to GCC, substitutes X; else if T is given to GCC, substitutes Y; else substitutes D. There can be as many clauses as you need. This may be combined with ., ,, !, |, and * as needed.
The conditional text X in a %{S:X} or similar
construct may contain other nested % constructs or spaces, or even newlines. They are processed as usual, as described above. Trailing white space in X is ignored. White space may also appear anywhere on the left side of the colon in these constructs, except between . or * and the corresponding word.
The -O, -f, -m, and -W switches are handled specifically in these constructs. If another value of -O or the negated form of a -f, -m, or -W switch is found later in the command line, the earlier switch value is ignored, except with {S``*} where ``S is just one letter, which passes all matching options.
The character | at the beginning of the predicate text is used to indicate that a command should be piped to the following command, but only if -pipe is specified.
It is built into GCC which switches take arguments and which do not. (You might think it would be useful to generalize this to allow each compiler’s spec to say which switches take arguments. But this cannot be done in a consistent fashion. GCC cannot even decide which input files have been specified without knowing which switches take arguments, and it must know which input files to compile in order to tell which compilers to run).
GCC also knows implicitly that arguments starting in -l are to be treated as compiler output files, and passed to the linker in their proper position among the other output files.