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 and it can be one of the following:
%
command
%include <
file>
%include_noerr <
file>
%include
, but do not generate an error message if the include
file cannot be found.
%rename
old_name
new_name
*[
spec_name]:
+
character, in which case the text will be appended to the spec.
[
suffix]:
[
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 will 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 that follows a suffix directive can be one of the following:
@
language
-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.
#
name
name compiler not installed on this system.
GCC already has an extensive list of suffixes built into it. This directive will add 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.
%%
%
into the program name or argument.
%i
%b
%B
%b
, but include the file suffix (text after
the last period).
%d
%d
as a
temporary file name, so that that file will be deleted if GCC exits
successfully. Unlike %g
, this contributes no text to the
argument.
%g
suffix
%d
. To reduce exposure to denial-of-service attacks, the file
name is now chosen in a way that is hard to predict even when previously
chosen file names are known. For example, %g.s ... %g.o ... %g.s
might turn into ccUVUUAU.s ccXYAXZ12.o ccUVUUAU.s
. suffix matches
the regexp [.A-Za-z]*
or the special string %O
, which is
treated exactly as if %O
had been preprocessed. Previously, %g
was simply substituted with a file name chosen once per compilation,
without regard to any appended suffix (which was therefore treated
just like ordinary text), making such attacks more likely to succeed.
%u
suffix
%g
, but generates a new temporary file name even if
%u
suffix
was already seen.
%U
suffix
%u
suffix
, generating a
new one if there is no such last file name. In the absence of any
%u
suffix
, this is just like %g
suffix
, except they don't share
the same suffix space, so %g.s ... %U.s ... %g.s ... %U.s
would involve the generation of two distinct file names, one
for each %g.s
and another for each %U.s
. Previously, %U
was
simply substituted with a file name chosen for the previous %u
,
without regard to any appended suffix.
%j
suffix
HOST_BIT_BUCKET
, if any, and if it is
writable, and if save-temps is off; otherwise, substitute the name
of a temporary file, just like %u
. This temporary file is not
meant for communication between processes, but rather as a junk
disposal mechanism.
%|
suffix
%m
suffix
%g
, except if -pipe
is in effect. In that case
%|
substitutes a single dash and %m
substitutes nothing at
all. These are the two most common ways to instruct a program that it
should read from standard input or write to standard output. If you
need something more elaborate you can use an %{pipe:X}
construct: see for example f/lang-specs.h
.
%.
SUFFIX
%*
. SUFFIX is
terminated by the next space or %.
%w
%w
as the
designated output file of this compilation. This puts the argument
into the sequence of arguments that %o
will substitute later.
%o
%o
as well or the results are undefined.
%o
is for use in the specs for running the linker.
Input files whose names have no recognized suffix are not compiled
at all, but they are included among the output files, so they will
be linked.
%O
%g, %u, or %U
,
because of the need for those to form complete file names. The
handling is such that %O
is treated exactly as if it had already
been substituted, except that %g, %u, and %U
do not currently
support additional suffix characters following %O
as they would
following, for example, .o
.
%p
cpp
.
%P
%p
, but puts __
before and after the name of each
predefined macro, except for macros that start with __
or with
_
L
, where L is an uppercase letter. This is for ISO
C.
%I
-iprefix
(made from GCC_EXEC_PREFIX
),
-isysroot
(made from TARGET_SYSTEM_ROOT
), and
-isystem
(made from COMPILER_PATH
and -B
options)
as necessary.
%s
%e
str
%(
name)
%[
name]
%(...)
but put __
around -D
arguments.
%x{
option}
%X
.
%X
-Wl
or a %x
spec string.
%Y
-Wa
.
%Z
-Wp
.
%a
asm
spec. This is used to compute the
switches to be passed to the assembler.
%A
asm_final
spec. This is a spec string for
passing switches to an assembler post-processor, if such a program is
needed.
%l
link
spec. This is the spec for computing the
command line passed to the linker. Typically it will make use of the
%L %G %S %D and %E
sequences.
%D
-L
option for each directory that GCC believes might
contain startup files. If the target supports multilibs then the
current multilib directory will be prepended to each of these paths.
%M
_
. If multilib directories are not set, or the multilib directory is
.
then this option emits nothing.
%L
lib
spec. This is a spec string for deciding which
libraries should be included on the command line to the linker.
%G
libgcc
spec. This is a spec string for deciding
which GCC support library should be included on the command line to the linker.
%S
startfile
spec. This is a spec for deciding which
object files should be the first ones passed to the linker. Typically
this might be a file named crt0.o
.
%E
endfile
spec. This is a spec string that specifies
the last object files that will be passed to the linker.
%C
cpp
spec. This is used to construct the arguments
to be passed to the C preprocessor.
%c
signed_char
spec. This is intended to be used
to tell cpp whether a char is signed. It typically has the definition:
%{funsigned-char:-D__CHAR_UNSIGNED__}
%1
cc1
spec. This is used to construct the options to be
passed to the actual C compiler (cc1
).
%2
cc1plus
spec. This is used to construct the options to be
passed to the actual C++ compiler (cc1plus
).
%*
%<S
-S
from the command line. Note--this
command is position dependent. %
commands in the spec string
before this one will see -S
, %
commands in the spec string
after this one will not.
%:
function(
args)
The following built-in spec functions are provided:
if-exists
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
if-exists-else
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)
%{S}
-S
switch, if that switch was given to GCC.
If that switch was not specified, this substitutes nothing. Note that
the leading dash is omitted when specifying this option, and it is
automatically inserted if the substitution is performed. Thus the spec
string %{foo}
would match the command-line option -foo
and would output the command line option -foo
.
%W{S}
S
} but mark last argument supplied within as a file to be
deleted on failure.
%{S*}
-S
, but which also take an argument. This is used for
switches like -o
, -D
, -I
, etc.
GCC considers -o foo
as being
one switch whose names starts with o
. %{o*} would substitute this
text, including the space. Thus two arguments would be generated.
%{S*&T*}
S
*}, but preserve order of S
and T
options
(the order of S
and T
in the spec is not significant).
There can be any number of ampersand-separated variables; for each the
wild card is optional. Useful for CPP as %{D*&U*&A*}
.
%{S:X}
X
, if the -S
switch was given to GCC.
%{!S:X}
X
, if the -S
switch was not given to GCC.
%{S*:X}
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
will be substituted once
for each matching switch, with the %*
replaced by the part of
that switch that matched the *
.
%{.S:X}
X
, if processing a file with suffix S
.
%{!.S:X}
X
, if not processing a file with suffix S
.
%{S|P:X}
X
if either -S
or -P
was 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}
will output 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
%{S:X; T:Y; :D}
S
was given to GCC, substitutes X
; else if T
was
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.