]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/gcc.c
rx.c (rx_get_stack_layout): Only save call clobbered registers inside interrupt handl...
[thirdparty/gcc.git] / gcc / gcc.c
CommitLineData
ed1f651b 1/* Compiler driver program that can handle many languages.
9218435e 2 Copyright (C) 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
d7fb0a6d 3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
9f8e43c0 4 2010, 2011
c662432e 5 Free Software Foundation, Inc.
ed1f651b 6
1322177d 7This file is part of GCC.
ed1f651b 8
1322177d
LB
9GCC is free software; you can redistribute it and/or modify it under
10the terms of the GNU General Public License as published by the Free
9dcd6f09 11Software Foundation; either version 3, or (at your option) any later
1322177d 12version.
ed1f651b 13
1322177d
LB
14GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15WARRANTY; without even the implied warranty of MERCHANTABILITY or
16FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17for more details.
ed1f651b
RS
18
19You should have received a copy of the GNU General Public License
9dcd6f09 20along with GCC; see the file COPYING3. If not see
8a554c70 21<http://www.gnu.org/licenses/>. */
ed1f651b
RS
22
23/* This program is the user interface to the C compiler and possibly to
24other compilers. It is used because compilation is a complicated procedure
25which involves running several programs and passing temporary files between
26them, forwarding the users switches to those programs selectively,
27and deleting the temporary files at the end.
28
29CC recognizes how to compile each input file by suffixes in the file names.
30Once it knows which kind of compilation to perform, the procedure for
31compilation is specified by a string called a "spec". */
c5c76735 32
e9a25f70 33#include "config.h"
670ee920 34#include "system.h"
4977bab6 35#include "coretypes.h"
1713a69f 36#include "multilib.h" /* before tm.h */
4977bab6 37#include "tm.h"
ed5b9f96 38#include "xregex.h"
17248a6b 39#include "obstack.h"
ab87f8c8 40#include "intl.h"
460ee112 41#include "prefix.h"
9257393c 42#include "gcc.h"
6afbc885 43#include "diagnostic.h"
8a63621f 44#include "flags.h"
14c7833c 45#include "opts.h"
5b634ee0 46#include "vec.h"
c10d53dd 47
45936a85
DD
48/* By default there is no special suffix for target executables. */
49/* FIXME: when autoconf is fixed, remove the host check - dj */
50#if defined(TARGET_EXECUTABLE_SUFFIX) && defined(HOST_EXECUTABLE_SUFFIX)
51#define HAVE_TARGET_EXECUTABLE_SUFFIX
ed1f651b 52#endif
f6ec7e54 53
45936a85
DD
54/* By default there is no special suffix for host executables. */
55#ifdef HOST_EXECUTABLE_SUFFIX
56#define HAVE_HOST_EXECUTABLE_SUFFIX
f70165f6 57#else
45936a85
DD
58#define HOST_EXECUTABLE_SUFFIX ""
59#endif
60
61/* By default, the suffix for target object files is ".o". */
62#ifdef TARGET_OBJECT_SUFFIX
63#define HAVE_TARGET_OBJECT_SUFFIX
64#else
65#define TARGET_OBJECT_SUFFIX ".o"
ed7dae04
RK
66#endif
67
8b60264b 68static const char dir_separator_str[] = { DIR_SEPARATOR, 0 };
48ff801b 69
512b62fb
JM
70/* Most every one is fine with LIBRARY_PATH. For some, it conflicts. */
71#ifndef LIBRARY_PATH_ENV
72#define LIBRARY_PATH_ENV "LIBRARY_PATH"
73#endif
74
ed1f651b
RS
75/* If a stage of compilation returns an exit status >= 1,
76 compilation of that file ceases. */
77
78#define MIN_FATAL_STATUS 1
79
69927b59
NB
80/* Flag set by cppspec.c to 1. */
81int is_cpp_driver;
82
110abdbc 83/* Flag set to nonzero if an @file argument has been supplied to gcc. */
2091ff66
NF
84static bool at_file_supplied;
85
f5fa9a5b
PE
86/* Definition of string containing the arguments given to configure. */
87#include "configargs.h"
88
b8468bc7
NC
89/* Flag saying to print the command line options understood by gcc and its
90 sub-processes. */
91
92static int print_help_list;
93
41fd0f9b
RAE
94/* Flag saying to print the version of gcc and its sub-processes. */
95
96static int print_version;
97
99f78cdd
IR
98/* Flag indicating whether we should ONLY print the command and
99 arguments (like verbose_flag) without executing the command.
100 Displayed arguments are quoted so that the generated command
101 line is suitable for execution. This is intended for use in
102 shell scripts to capture the driver-generated command line. */
103static int verbose_only_flag;
104
c662432e 105/* Flag indicating how to print command line options of sub-processes. */
91606ce2 106
c662432e 107static int print_subprocess_help;
91606ce2 108
bdde878c
AO
109/* Whether we should report subprocess execution times to a file. */
110
111FILE *report_times_to_file = NULL;
112
4977bab6
ZW
113/* Nonzero means place this string before uses of /, so that include
114 and library files can be found in an alternate location. */
115
db720d9a 116#ifdef TARGET_SYSTEM_ROOT
4977bab6 117static const char *target_system_root = TARGET_SYSTEM_ROOT;
db720d9a
DJ
118#else
119static const char *target_system_root = 0;
120#endif
4977bab6 121
047d636f
DJ
122/* Nonzero means pass the updated target_system_root to the compiler. */
123
124static int target_system_root_changed;
125
e7f13528
GP
126/* Nonzero means append this string to target_system_root. */
127
128static const char *target_sysroot_suffix = 0;
129
130/* Nonzero means append this string to target_system_root for headers. */
131
132static const char *target_sysroot_hdrs_suffix = 0;
133
ed1f651b
RS
134/* Nonzero means write "temp" files in source directory
135 and use the source file's name in them, and don't delete them. */
136
14fdc613
MM
137static enum save_temps {
138 SAVE_TEMPS_NONE, /* no -save-temps */
139 SAVE_TEMPS_CWD, /* -save-temps in current directory */
140 SAVE_TEMPS_OBJ /* -save-temps in object directory */
141} save_temps_flag;
142
143/* Output file to use to get the object directory for -save-temps=obj */
144static char *save_temps_prefix = 0;
145static size_t save_temps_length = 0;
ed1f651b 146
53117a2f 147/* The compiler version. */
ed1f651b 148
3b304f5b 149static const char *compiler_version;
53117a2f 150
1401cf37 151/* The target version. */
53117a2f 152
37a4aa31 153static const char *const spec_version = DEFAULT_TARGET_VERSION;
ed1f651b 154
1401cf37 155/* The target machine. */
ed1f651b 156
878f32c3 157static const char *spec_machine = DEFAULT_TARGET_MACHINE;
ed1f651b 158
004fd4d5
RS
159/* Nonzero if cross-compiling.
160 When -b is used, the value comes from the `specs' file. */
161
2989d30c 162#ifdef CROSS_DIRECTORY_STRUCTURE
3b304f5b 163static const char *cross_compile = "1";
004fd4d5 164#else
3b304f5b 165static const char *cross_compile = "0";
004fd4d5
RS
166#endif
167
14a774a9
RK
168/* Greatest exit code of sub-processes that has been encountered up to
169 now. */
170static int greatest_status = 1;
171
ed1f651b
RS
172/* This is the obstack which we use to allocate many strings. */
173
174static struct obstack obstack;
175
b3865ca9 176/* This is the obstack to build an environment variable to pass to
6dc42e49 177 collect2 that describes all of the relevant switches of what to
b3865ca9
RS
178 pass the compiler in building the list of pointers to constructors
179 and destructors. */
180
181static struct obstack collect_obstack;
182
99360286
DE
183/* Forward declaration for prototypes. */
184struct path_prefix;
76391e5a 185struct prefix_list;
99360286 186
1d088dee
AJ
187static void init_spec (void);
188static void store_arg (const char *, int, int);
fe7df9c4 189static void insert_wrapper (const char *);
1d088dee
AJ
190static char *load_specs (const char *);
191static void read_specs (const char *, int);
192static void set_spec (const char *, const char *);
193static struct compiler *lookup_compiler (const char *, size_t, const char *);
00dcee0c
AM
194static char *build_search_list (const struct path_prefix *, const char *,
195 bool, bool);
e9c15f6e 196static void xputenv (const char *);
00dcee0c
AM
197static void putenv_from_prefixes (const struct path_prefix *, const char *,
198 bool);
1d088dee 199static int access_check (const char *, int);
00dcee0c 200static char *find_a_file (const struct path_prefix *, const char *, int, bool);
1d088dee 201static void add_prefix (struct path_prefix *, const char *, const char *,
1a5d37a1 202 int, int, int);
1d088dee 203static void add_sysrooted_prefix (struct path_prefix *, const char *,
1a5d37a1 204 const char *, int, int, int);
1d088dee
AJ
205static char *skip_whitespace (char *);
206static void delete_if_ordinary (const char *);
207static void delete_temp_files (void);
208static void delete_failure_queue (void);
209static void clear_failure_queue (void);
210static int check_live_switch (int, int);
211static const char *handle_braces (const char *);
212static inline bool input_suffix_matches (const char *, const char *);
213static inline bool switch_matches (const char *, const char *, int);
214static inline void mark_matching_switches (const char *, const char *, int);
215static inline void process_marked_switches (void);
216static const char *process_brace_body (const char *, const char *, const char *, int, int);
217static const struct spec_function *lookup_spec_function (const char *);
218static const char *eval_spec_function (const char *, const char *);
219static const char *handle_spec_function (const char *);
220static char *save_string (const char *, int);
221static void set_collect_gcc_options (void);
222static int do_spec_1 (const char *, int, const char *);
223static int do_spec_2 (const char *);
224static void do_option_spec (const char *, const char *);
225static void do_self_spec (const char *);
226static const char *find_file (const char *);
00dcee0c 227static int is_directory (const char *, bool);
1d088dee
AJ
228static const char *validate_switches (const char *);
229static void validate_all_switches (void);
230static inline void validate_switches_from_spec (const char *);
231static void give_switch (int, int);
232static int used_arg (const char *, int);
233static int default_arg (const char *, int);
234static void set_multilib_dir (void);
235static void print_multilib_info (void);
236static void perror_with_name (const char *);
1d088dee
AJ
237static void display_help (void);
238static void add_preprocessor_option (const char *, int);
239static void add_assembler_option (const char *, int);
240static void add_linker_option (const char *, int);
60cf253a 241static void process_command (unsigned int, struct cl_decoded_option *);
1d088dee
AJ
242static int execute (void);
243static void alloc_args (void);
244static void clear_args (void);
2dec80c7 245static void fatal_signal (int);
328163dc 246#if defined(ENABLE_SHARED_LIBGCC) && !defined(REAL_LIBGCC_SPEC)
1d088dee
AJ
247static void init_gcc_specs (struct obstack *, const char *, const char *,
248 const char *);
6894579f 249#endif
40cdfca6 250#if defined(HAVE_TARGET_OBJECT_SUFFIX) || defined(HAVE_TARGET_EXECUTABLE_SUFFIX)
1d088dee 251static const char *convert_filename (const char *, int, int);
40cdfca6 252#endif
f3226a90 253
30d8946b 254static const char *getenv_spec_function (int, const char **);
1d088dee
AJ
255static const char *if_exists_spec_function (int, const char **);
256static const char *if_exists_else_spec_function (int, const char **);
3dd53121 257static const char *replace_outfile_spec_function (int, const char **);
2642f659 258static const char *remove_outfile_spec_function (int, const char **);
ed5b9f96 259static const char *version_compare_spec_function (int, const char **);
953ff289 260static const char *include_spec_function (int, const char **);
4adbd5dd 261static const char *find_file_spec_function (int, const char **);
c1ce46a5 262static const char *find_plugindir_spec_function (int, const char **);
a0f87454 263static const char *print_asm_header_spec_function (int, const char **);
2153915d
AO
264static const char *compare_debug_dump_opt_spec_function (int, const char **);
265static const char *compare_debug_self_opt_spec_function (int, const char **);
266static const char *compare_debug_auxbase_opt_spec_function (int, const char **);
9a37bc07 267static const char *pass_through_libs_spec_func (int, const char **);
ed1f651b 268\f
d991c721
BK
269/* The Specs Language
270
271Specs are strings containing lines, each of which (if not blank)
ed1f651b
RS
272is made up of a program name, and arguments separated by spaces.
273The program name must be exact and start from root, since no path
274is searched and it is unreliable to depend on the current working directory.
275Redirection of input or output is not supported; the subprograms must
276accept filenames saying what files to read and write.
277
278In addition, the specs can contain %-sequences to substitute variable text
279or for conditional text. Here is a table of all defined %-sequences.
280Note that spaces are not generated automatically around the results of
281expanding these sequences; therefore, you can concatenate them together
282or with constant text in a single argument.
283
284 %% substitute one % into the program name or argument.
285 %i substitute the name of the input file being processed.
286 %b substitute the basename of the input file being processed.
287 This is the substring up to (and not including) the last period
14fdc613 288 and not including the directory unless -save-temps was specified
3beb864c 289 to put temporaries in a different location.
ea414c97 290 %B same as %b, but include the file suffix (text after the last period).
dd75c292
CB
291 %gSUFFIX
292 substitute a file name that has suffix SUFFIX and is chosen
293 once per compilation, and mark the argument a la %d. To reduce
294 exposure to denial-of-service attacks, the file name is now
295 chosen in a way that is hard to predict even when previously
296 chosen file names are known. For example, `%g.s ... %g.o ... %g.s'
297 might turn into `ccUVUUAU.s ccXYAXZ12.o ccUVUUAU.s'. SUFFIX matches
a92dd235 298 the regexp "[.0-9A-Za-z]*%O"; "%O" is treated exactly as if it
a1d9074c
TT
299 had been pre-processed. Previously, %g was simply substituted
300 with a file name chosen once per compilation, without regard
301 to any appended suffix (which was therefore treated just like
302 ordinary text), making such attacks more likely to succeed.
4977bab6
ZW
303 %|SUFFIX
304 like %g, but if -pipe is in effect, expands simply to "-".
305 %mSUFFIX
306 like %g, but if -pipe is in effect, expands to nothing. (We have both
307 %| and %m to accommodate differences between system assemblers; see
308 the AS_NEEDS_DASH_FOR_PIPED_INPUT target macro.)
dd75c292
CB
309 %uSUFFIX
310 like %g, but generates a new temporary file name even if %uSUFFIX
311 was already seen.
312 %USUFFIX
313 substitutes the last file name generated with %uSUFFIX, generating a
314 new one if there is no such last file name. In the absence of any
315 %uSUFFIX, this is just like %gSUFFIX, except they don't share
316 the same suffix "space", so `%g.s ... %U.s ... %g.s ... %U.s'
317 would involve the generation of two distinct file names, one
318 for each `%g.s' and another for each `%U.s'. Previously, %U was
319 simply substituted with a file name chosen for the previous %u,
320 without regard to any appended suffix.
49009afd
JL
321 %jSUFFIX
322 substitutes the name of the HOST_BIT_BUCKET, if any, and if it is
323 writable, and if save-temps is off; otherwise, substitute the name
324 of a temporary file, just like %u. This temporary file is not
325 meant for communication between processes, but rather as a junk
326 disposal mechanism.
11972f66
NS
327 %.SUFFIX
328 substitutes .SUFFIX for the suffixes of a matched switch's args when
329 it is subsequently output with %*. SUFFIX is terminated by the next
330 space or %.
ed1f651b 331 %d marks the argument containing or following the %d as a
ee618c17 332 temporary file name, so that that file will be deleted if GCC exits
ed1f651b
RS
333 successfully. Unlike %g, this contributes no text to the argument.
334 %w marks the argument containing or following the %w as the
335 "output file" of this compilation. This puts the argument
336 into the sequence of arguments that %o will substitute later.
17211ab5 337 %V indicates that this compilation produces no "output file".
ed1f651b
RS
338 %W{...}
339 like %{...} but mark last argument supplied within
340 as a file to be deleted on failure.
341 %o substitutes the names of all the output files, with spaces
342 automatically placed around them. You should write spaces
343 around the %o as well or the results are undefined.
344 %o is for use in the specs for running the linker.
345 Input files whose names have no recognized suffix are not compiled
346 at all, but they are included among the output files, so they will
347 be linked.
dd75c292 348 %O substitutes the suffix for object files. Note that this is
a1d9074c
TT
349 handled specially when it immediately follows %g, %u, or %U
350 (with or without a suffix argument) because of the need for
351 those to form complete file names. The handling is such that
352 %O is treated exactly as if it had already been substituted,
353 except that %g, %u, and %U do not currently support additional
354 SUFFIX characters following %O as they would following, for
355 example, `.o'.
047d636f 356 %I Substitute any of -iprefix (made from GCC_EXEC_PREFIX), -isysroot
2b6dd222
JM
357 (made from TARGET_SYSTEM_ROOT), -isystem (made from COMPILER_PATH
358 and -B options) and -imultilib as necessary.
ed1f651b
RS
359 %s current argument is the name of a library or startup file of some sort.
360 Search for that file in a standard list of directories
361 and substitute the full name found.
362 %eSTR Print STR as an error message. STR is terminated by a newline.
363 Use this when inconsistent options are detected.
09da1532 364 %nSTR Print STR as a notice. STR is terminated by a newline.
ed1f651b
RS
365 %x{OPTION} Accumulate an option for %X.
366 %X Output the accumulated linker options specified by compilations.
c9ebacb8 367 %Y Output the accumulated assembler options specified by compilations.
57cb9b60 368 %Z Output the accumulated preprocessor options specified by compilations.
ed1f651b
RS
369 %a process ASM_SPEC as a spec.
370 This allows config.h to specify part of the spec for running as.
371 %A process ASM_FINAL_SPEC as a spec. A capital A is actually
372 used here. This can be used to run a post-processor after the
9ec36da5 373 assembler has done its job.
48ff801b 374 %D Dump out a -L option for each directory in startfile_prefixes.
60103a34 375 If multilib_dir is set, extra entries are generated with it affixed.
ed1f651b
RS
376 %l process LINK_SPEC as a spec.
377 %L process LIB_SPEC as a spec.
68d69835 378 %G process LIBGCC_SPEC as a spec.
380e5ca4
MM
379 %R Output the concatenation of target_system_root and
380 target_sysroot_suffix.
ed1f651b
RS
381 %S process STARTFILE_SPEC as a spec. A capital S is actually used here.
382 %E process ENDFILE_SPEC as a spec. A capital E is actually used here.
9db0819e 383 %C process CPP_SPEC as a spec.
ed1f651b
RS
384 %1 process CC1_SPEC as a spec.
385 %2 process CC1PLUS_SPEC as a spec.
386 %* substitute the variable part of a matched option. (See below.)
387 Note that each comma in the substituted string is replaced by
388 a single space.
4977bab6
ZW
389 %<S remove all occurrences of -S from the command line.
390 Note - this command is position dependent. % commands in the
391 spec string before this one will see -S, % commands in the
392 spec string after this one will not.
96f5b137 393 %>S Similar to "%<S", but keep it in the GCC command line.
4977bab6
ZW
394 %<S* remove all occurrences of all switches beginning with -S from the
395 command line.
f3226a90
JT
396 %:function(args)
397 Call the named function FUNCTION, passing it ARGS. ARGS is
398 first processed as a nested spec string, then split into an
399 argument vector in the usual fashion. The function returns
400 a string which is processed as if it had appeared literally
401 as part of the current spec.
ee618c17 402 %{S} substitutes the -S switch, if that switch was given to GCC.
ed1f651b
RS
403 If that switch was not specified, this substitutes nothing.
404 Here S is a metasyntactic variable.
ee618c17 405 %{S*} substitutes all the switches specified to GCC whose names start
196a37f4 406 with -S. This is used for -o, -I, etc; switches that take
ee618c17 407 arguments. GCC considers `-o foo' as being one switch whose
ed1f651b
RS
408 name starts with `o'. %{o*} would substitute this text,
409 including the space; thus, two arguments would be generated.
196a37f4 410 %{S*&T*} likewise, but preserve order of S and T options (the order
1d088dee
AJ
411 of S and T in the spec is not significant). Can be any number
412 of ampersand-separated variables; for each the wild card is
413 optional. Useful for CPP as %{D*&U*&A*}.
4977bab6 414
ee618c17
BE
415 %{S:X} substitutes X, if the -S switch was given to GCC.
416 %{!S:X} substitutes X, if the -S switch was NOT given to GCC.
4977bab6 417 %{S*:X} substitutes X if one or more switches whose names start
ee618c17 418 with -S was given to GCC. Normally X is substituted only
4977bab6
ZW
419 once, no matter how many such switches appeared. However,
420 if %* appears somewhere in X, then X will be substituted
421 once for each matching switch, with the %* replaced by the
422 part of that switch that matched the '*'.
423 %{.S:X} substitutes X, if processing a file with suffix S.
424 %{!.S:X} substitutes X, if NOT processing a file with suffix S.
48137d59
GK
425 %{,S:X} substitutes X, if processing a file which will use spec S.
426 %{!,S:X} substitutes X, if NOT processing a file which will use spec S.
3beb864c 427
ee618c17 428 %{S|T:X} substitutes X if either -S or -T was given to GCC. This may be
48137d59
GK
429 combined with '!', '.', ',', and '*' as above binding stronger
430 than the OR.
4977bab6
ZW
431 If %* appears in X, all of the alternatives must be starred, and
432 only the first matching alternative is substituted.
ee618c17
BE
433 %{S:X; if S was given to GCC, substitutes X;
434 T:Y; else if T was given to GCC, substitutes Y;
4977bab6 435 :D} else substitutes D. There can be as many clauses as you need.
48137d59 436 This may be combined with '.', '!', ',', '|', and '*' as above.
4977bab6 437
b3865ca9 438 %(Spec) processes a specification defined in a specs file as *Spec:
4089dfab 439 %[Spec] as above, but put __ around -D arguments
ed1f651b 440
4977bab6 441The conditional text X in a %{S:X} or similar construct may contain
ed1f651b 442other nested % constructs or spaces, or even newlines. They are
4977bab6
ZW
443processed as usual, as described above. Trailing white space in X is
444ignored. White space may also appear anywhere on the left side of the
445colon in these constructs, except between . or * and the corresponding
446word.
ed1f651b 447
6c396fb5 448The -O, -f, -m, and -W switches are handled specifically in these
f5b0eb4e
RK
449constructs. If another value of -O or the negated form of a -f, -m, or
450-W switch is found later in the command line, the earlier switch
6c396fb5
RK
451value is ignored, except with {S*} where S is just one letter; this
452passes all matching options.
f5b0eb4e 453
9bf09437
RH
454The character | at the beginning of the predicate text is used to indicate
455that a command should be piped to the following command, but only if -pipe
456is specified.
ed1f651b 457
ee618c17 458Note that it is built into GCC which switches take arguments and which
ed1f651b
RS
459do not. You might think it would be useful to generalize this to
460allow each compiler's spec to say which switches take arguments. But
ee618c17 461this cannot be done in a consistent fashion. GCC cannot even decide
ed1f651b
RS
462which input files have been specified without knowing which switches
463take arguments, and it must know which input files to compile in order
464to tell which compilers to run.
465
ee618c17 466GCC also knows implicitly that arguments starting in `-l' are to be
ed1f651b
RS
467treated as compiler output files, and passed to the linker in their
468proper position among the other output files. */
469\f
0fef3fd0 470/* Define the macros used for specs %a, %l, %L, %S, %C, %1. */
ed1f651b
RS
471
472/* config.h can define ASM_SPEC to provide extra args to the assembler
473 or extra switch-translations. */
474#ifndef ASM_SPEC
475#define ASM_SPEC ""
476#endif
477
478/* config.h can define ASM_FINAL_SPEC to run a post processor after
479 the assembler has run. */
480#ifndef ASM_FINAL_SPEC
481#define ASM_FINAL_SPEC ""
482#endif
483
484/* config.h can define CPP_SPEC to provide extra args to the C preprocessor
485 or extra switch-translations. */
486#ifndef CPP_SPEC
487#define CPP_SPEC ""
488#endif
489
490/* config.h can define CC1_SPEC to provide extra args to cc1 and cc1plus
491 or extra switch-translations. */
492#ifndef CC1_SPEC
493#define CC1_SPEC ""
494#endif
495
496/* config.h can define CC1PLUS_SPEC to provide extra args to cc1plus
497 or extra switch-translations. */
498#ifndef CC1PLUS_SPEC
499#define CC1PLUS_SPEC ""
500#endif
501
502/* config.h can define LINK_SPEC to provide extra args to the linker
503 or extra switch-translations. */
504#ifndef LINK_SPEC
505#define LINK_SPEC ""
506#endif
507
508/* config.h can define LIB_SPEC to override the default libraries. */
509#ifndef LIB_SPEC
68d69835
JM
510#define LIB_SPEC "%{!shared:%{g*:-lg} %{!p:%{!pg:-lc}}%{p:-lc_p}%{pg:-lc_p}}"
511#endif
512
6de9cd9a
DN
513/* mudflap specs */
514#ifndef MFWRAP_SPEC
515/* XXX: valid only for GNU ld */
516/* XXX: should exactly match hooks provided by libmudflap.a */
517#define MFWRAP_SPEC " %{static: %{fmudflap|fmudflapth: \
518 --wrap=malloc --wrap=free --wrap=calloc --wrap=realloc\
519 --wrap=mmap --wrap=munmap --wrap=alloca\
7544a87f 520} %{fmudflapth: --wrap=pthread_create\
6de9cd9a
DN
521}} %{fmudflap|fmudflapth: --wrap=main}"
522#endif
523#ifndef MFLIB_SPEC
7904f95f 524#define MFLIB_SPEC "%{fmudflap|fmudflapth: -export-dynamic}"
6de9cd9a
DN
525#endif
526
7458026b
ILT
527/* When using -fsplit-stack we need to wrap pthread_create, in order
528 to initialize the stack guard. We always use wrapping, rather than
529 shared library ordering, and we keep the wrapper function in
530 libgcc. This is not yet a real spec, though it could become one;
531 it is currently just stuffed into LINK_SPEC. FIXME: This wrapping
532 only works with GNU ld and gold. FIXME: This is incompatible with
533 -fmudflap when linking statically, which wants to do its own
534 wrapping. */
535#define STACK_SPLIT_SPEC " %{fsplit-stack: --wrap=pthread_create}"
536
68d69835
JM
537/* config.h can define LIBGCC_SPEC to override how and when libgcc.a is
538 included. */
539#ifndef LIBGCC_SPEC
328163dc
MA
540#if defined(REAL_LIBGCC_SPEC)
541#define LIBGCC_SPEC REAL_LIBGCC_SPEC
57642751 542#elif defined(LINK_LIBGCC_SPECIAL_1)
68d69835 543/* Have gcc do the search for libgcc.a. */
ba2b7435 544#define LIBGCC_SPEC "libgcc.a%s"
68d69835 545#else
ba2b7435 546#define LIBGCC_SPEC "-lgcc"
68d69835 547#endif
ed1f651b
RS
548#endif
549
550/* config.h can define STARTFILE_SPEC to override the default crt0 files. */
551#ifndef STARTFILE_SPEC
552#define STARTFILE_SPEC \
adcb8d7d 553 "%{!shared:%{pg:gcrt0%O%s}%{!pg:%{p:mcrt0%O%s}%{!p:crt0%O%s}}}"
ed1f651b
RS
554#endif
555
ed1f651b
RS
556/* config.h can define ENDFILE_SPEC to override the default crtn files. */
557#ifndef ENDFILE_SPEC
558#define ENDFILE_SPEC ""
559#endif
560
10da1131
BM
561#ifndef LINKER_NAME
562#define LINKER_NAME "collect2"
563#endif
564
c8aea42c
PB
565#ifdef HAVE_AS_DEBUG_PREFIX_MAP
566#define ASM_MAP " %{fdebug-prefix-map=*:--debug-prefix-map %*}"
567#else
568#define ASM_MAP ""
569#endif
570
5f0e9ea2
GK
571/* Define ASM_DEBUG_SPEC to be a spec suitable for translating '-g'
572 to the assembler. */
573#ifndef ASM_DEBUG_SPEC
b2ace8a4
JJ
574# if defined(DBX_DEBUGGING_INFO) && defined(DWARF2_DEBUGGING_INFO) \
575 && defined(HAVE_AS_GDWARF2_DEBUG_FLAG) && defined(HAVE_AS_GSTABS_DEBUG_FLAG)
c8aea42c
PB
576# define ASM_DEBUG_SPEC \
577 (PREFERRED_DEBUGGING_TYPE == DBX_DEBUG \
c7b94907
MM
578 ? "%{!g0:%{gdwarf-2*:--gdwarf2}%{!gdwarf-2*:%{g*:--gstabs}}}" ASM_MAP \
579 : "%{!g0:%{gstabs*:--gstabs}%{!gstabs*:%{g*:--gdwarf2}}}" ASM_MAP)
b2ace8a4
JJ
580# else
581# if defined(DBX_DEBUGGING_INFO) && defined(HAVE_AS_GSTABS_DEBUG_FLAG)
c7b94907 582# define ASM_DEBUG_SPEC "%{g*:%{!g0:--gstabs}}" ASM_MAP
b2ace8a4
JJ
583# endif
584# if defined(DWARF2_DEBUGGING_INFO) && defined(HAVE_AS_GDWARF2_DEBUG_FLAG)
c7b94907 585# define ASM_DEBUG_SPEC "%{g*:%{!g0:--gdwarf2}}" ASM_MAP
5f0e9ea2 586# endif
5f0e9ea2
GK
587# endif
588#endif
8a63621f
JJ
589#ifndef ASM_DEBUG_SPEC
590# define ASM_DEBUG_SPEC ""
591#endif
5f0e9ea2 592
ea414c97
ZW
593/* Here is the spec for running the linker, after compiling all files. */
594
bbd7687d
DM
595/* This is overridable by the target in case they need to specify the
596 -lgcc and -lc order specially, yet not require them to override all
597 of LINK_COMMAND_SPEC. */
598#ifndef LINK_GCC_C_SEQUENCE_SPEC
599#define LINK_GCC_C_SEQUENCE_SPEC "%G %L %G"
600#endif
601
77008252
JJ
602#ifndef LINK_SSP_SPEC
603#ifdef TARGET_LIBC_PROVIDES_SSP
604#define LINK_SSP_SPEC "%{fstack-protector:}"
605#else
ec92bd4b 606#define LINK_SSP_SPEC "%{fstack-protector|fstack-protector-all:-lssp_nonshared -lssp}"
77008252
JJ
607#endif
608#endif
609
24a4dd31
JJ
610#ifndef LINK_PIE_SPEC
611#ifdef HAVE_LD_PIE
612#define LINK_PIE_SPEC "%{pie:-pie} "
613#else
614#define LINK_PIE_SPEC "%{pie:} "
615#endif
616#endif
617
cd533689
PP
618#ifndef LINK_BUILDID_SPEC
619# if defined(HAVE_LD_BUILDID) && defined(ENABLE_LD_BUILDID)
620# define LINK_BUILDID_SPEC "%{!r:--build-id} "
621# endif
622#endif
623
56e22cab
JH
624/* Conditional to test whether plugin is used or not.
625 FIXME: For slim LTO we will need to enable plugin unconditionally. This
626 still cause problems with PLUGIN_LD != LD and when plugin is built but
627 not useable. For GCC 4.6 we don't support slim LTO and thus we can enable
628 plugin only when LTO is enabled. We still honor explicit
629 -fuse-linker-plugin. */
96bdf9b4 630#ifdef HAVE_LTO_PLUGIN
56e22cab
JH
631#define PLUGIN_COND "!fno-use-linker-plugin:%{flto|flto=*|fuse-linker-plugin"
632#define PLUGIN_COND_CLOSE "}"
96bdf9b4
JH
633#else
634#define PLUGIN_COND "fuse-linker-plugin"
56e22cab 635#define PLUGIN_COND_CLOSE ""
96bdf9b4
JH
636#endif
637
cd533689 638
ea414c97
ZW
639/* -u* was put back because both BSD and SysV seem to support it. */
640/* %{static:} simply prevents an error message if the target machine
641 doesn't handle -static. */
642/* We want %{T*} after %{L*} and %D so that it can be used to specify linker
643 scripts which exist in user specified directories, or in standard
644 directories. */
014d92e1 645/* We pass any -flto flags on to the linker, which is expected
d7f09764 646 to understand them. In practice, this means it had better be collect2. */
ea414c97
ZW
647#ifndef LINK_COMMAND_SPEC
648#define LINK_COMMAND_SPEC "\
649%{!fsyntax-only:%{!c:%{!M:%{!MM:%{!E:%{!S:\
d7f09764 650 %(linker) \
96bdf9b4 651 %{"PLUGIN_COND": \
d7f09764
DN
652 -plugin %(linker_plugin_file) \
653 -plugin-opt=%(lto_wrapper) \
5cd0e96b 654 -plugin-opt=-fresolution=%u.res \
9a37bc07 655 %{!nostdlib:%{!nodefaultlibs:%:pass-through-libs(%(link_gcc_c_sequence))}} \
56e22cab
JH
656 }"PLUGIN_COND_CLOSE" \
657 %{flto|flto=*:%<fcompare-debug*} \
658 %{flto} %{flto=*} %l " LINK_PIE_SPEC \
12b57b0f
JM
659 "%X %{o*} %{e*} %{N} %{n} %{r}\
660 %{s} %{t} %{u*} %{z} %{Z} %{!nostdlib:%{!nostartfiles:%S}}\
8beb0d9e 661 %{static:} %{L*} %(mfwrap) %(link_libgcc) %o\
7458026b
ILT
662 %{fopenmp|ftree-parallelize-loops=*:%:include(libgomp.spec)%(link_gomp)}\
663 %(mflib) " STACK_SPLIT_SPEC "\
a123d175 664 %{fprofile-arcs|fprofile-generate*|coverage:-lgcov}\
9714c911 665 %{!nostdlib:%{!nodefaultlibs:%(link_ssp) %(link_gcc_c_sequence)}}\
12b57b0f 666 %{!nostdlib:%{!nostartfiles:%E}} %{T*} }}}}}}"
ea414c97
ZW
667#endif
668
669#ifndef LINK_LIBGCC_SPEC
57642751
DJ
670/* Generate -L options for startfile prefix list. */
671# define LINK_LIBGCC_SPEC "%D"
ea414c97
ZW
672#endif
673
343f59d9
AM
674#ifndef STARTFILE_PREFIX_SPEC
675# define STARTFILE_PREFIX_SPEC ""
676#endif
677
380e5ca4
MM
678#ifndef SYSROOT_SPEC
679# define SYSROOT_SPEC "--sysroot=%R"
680#endif
681
e7f13528
GP
682#ifndef SYSROOT_SUFFIX_SPEC
683# define SYSROOT_SUFFIX_SPEC ""
684#endif
685
686#ifndef SYSROOT_HEADERS_SUFFIX_SPEC
687# define SYSROOT_HEADERS_SUFFIX_SPEC ""
688#endif
689
81bca2f5 690static const char *asm_debug;
3b304f5b 691static const char *cpp_spec = CPP_SPEC;
3b304f5b
ZW
692static const char *cc1_spec = CC1_SPEC;
693static const char *cc1plus_spec = CC1PLUS_SPEC;
bbd7687d 694static const char *link_gcc_c_sequence_spec = LINK_GCC_C_SEQUENCE_SPEC;
77008252 695static const char *link_ssp_spec = LINK_SSP_SPEC;
3b304f5b
ZW
696static const char *asm_spec = ASM_SPEC;
697static const char *asm_final_spec = ASM_FINAL_SPEC;
698static const char *link_spec = LINK_SPEC;
699static const char *lib_spec = LIB_SPEC;
6de9cd9a
DN
700static const char *mfwrap_spec = MFWRAP_SPEC;
701static const char *mflib_spec = MFLIB_SPEC;
953ff289 702static const char *link_gomp_spec = "";
3b304f5b
ZW
703static const char *libgcc_spec = LIBGCC_SPEC;
704static const char *endfile_spec = ENDFILE_SPEC;
705static const char *startfile_spec = STARTFILE_SPEC;
3b304f5b 706static const char *linker_name_spec = LINKER_NAME;
d7f09764
DN
707static const char *linker_plugin_file_spec = "";
708static const char *lto_wrapper_spec = "";
709static const char *lto_gcc_spec = "";
ea414c97
ZW
710static const char *link_command_spec = LINK_COMMAND_SPEC;
711static const char *link_libgcc_spec = LINK_LIBGCC_SPEC;
343f59d9 712static const char *startfile_prefix_spec = STARTFILE_PREFIX_SPEC;
380e5ca4 713static const char *sysroot_spec = SYSROOT_SPEC;
e7f13528
GP
714static const char *sysroot_suffix_spec = SYSROOT_SUFFIX_SPEC;
715static const char *sysroot_hdrs_suffix_spec = SYSROOT_HEADERS_SUFFIX_SPEC;
ea414c97
ZW
716
717/* Standard options to cpp, cc1, and as, to reduce duplication in specs.
718 There should be no need to override these in target dependent files,
719 but we need to copy them to the specs file so that newer versions
720 of the GCC driver can correctly drive older tool chains with the
721 appropriate -B options. */
722
e5f5feea
NB
723/* When cpplib handles traditional preprocessing, get rid of this, and
724 call cc1 (or cc1obj in objc/lang-specs.h) from the main specs so
725 that we default the front end language better. */
ea414c97 726static const char *trad_capable_cpp =
3028a95c 727"cc1 -E %{traditional|traditional-cpp:-traditional-cpp}";
ea414c97 728
4ca1256f
NB
729/* We don't wrap .d files in %W{} since a missing .d file, and
730 therefore no dependency entry, confuses make into thinking a .o
731 file that happens to exist is up-to-date. */
ffdeea47 732static const char *cpp_unique_options =
9cc54940 733"%{!Q:-quiet} %{nostdinc*} %{C} %{CC} %{v} %{I*&F*} %{P} %I\
c878765b
JM
734 %{MD:-MD %{!o:%b.d}%{o*:%.d%*}}\
735 %{MMD:-MMD %{!o:%b.d}%{o*:%.d%*}}\
4ca1256f 736 %{M} %{MM} %{MF*} %{MG} %{MP} %{MQ*} %{MT*}\
3dfe046f 737 %{!E:%{!M:%{!MM:%{!MT:%{!MQ:%{MD|MMD:%{o*:-MQ %*}}}}}}}\
3a610de8 738 %{remap} %{g3|ggdb3|gstabs3|gcoff3|gxcoff3|gvms3:-dD}\
c1ce46a5 739 %{!iplugindir*:%{fplugin*:%:find-plugindir()}}\
3a610de8 740 %{H} %C %{D*&U*&A*} %{i*} %Z %i\
6de9cd9a
DN
741 %{fmudflap:-D_MUDFLAP -include mf-runtime.h}\
742 %{fmudflapth:-D_MUDFLAP -D_MUDFLAPTH -include mf-runtime.h}\
56cd5b95 743 %{E|M|MM:%W{o*}}";
ffdeea47
JJ
744
745/* This contains cpp options which are common with cc1_options and are passed
059ba716
CD
746 only when preprocessing only to avoid duplication. We pass the cc1 spec
747 options to the preprocessor so that it the cc1 spec may manipulate
748 options used to set target flags. Those special target flags settings may
749 in turn cause preprocessor symbols to be defined specially. */
ffdeea47 750static const char *cpp_options =
740ca4b2 751"%(cpp_unique_options) %1 %{m*} %{std*&ansi&trigraphs} %{W*&pedantic*} %{w}\
09828811 752 %{f*} %{g*:%{!g0:%{g*} %{!fno-working-directory:-fworking-directory}}} %{O*}\
14fdc613 753 %{undef} %{save-temps*:-fpch-preprocess}";
ea414c97 754
8b968bd1
MW
755/* This contains cpp options which are not passed when the preprocessor
756 output will be used by another program. */
757static const char *cpp_debug_options = "%{d*}";
758
708bceb7 759/* NB: This is shared amongst all front-ends, except for Ada. */
ea414c97
ZW
760static const char *cc1_options =
761"%{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
c1ce46a5 762 %{!iplugindir*:%{fplugin*:%:find-plugindir()}}\
1890bccc 763 %1 %{!Q:-quiet} %{!dumpbase:-dumpbase %B} %{d*} %{m*} %{aux-info*}\
2153915d
AO
764 %{fcompare-debug-second:%:compare-debug-auxbase-opt(%b)} \
765 %{!fcompare-debug-second:%{c|S:%{o*:-auxbase-strip %*}%{!o*:-auxbase %b}}}%{!c:%{!S:-auxbase %b}} \
740ca4b2 766 %{g*} %{O*} %{W*&pedantic*} %{w} %{std*&ansi&trigraphs}\
3df89291 767 %{v:-version} %{pg:-p} %{p} %{f*} %{undef}\
dd9f93dc 768 %{Qn:-fno-ident} %{Qy:} %{-help:--help}\
e200444e
JM
769 %{-target-help:--target-help}\
770 %{-version:--version}\
771 %{-help=*:--help=%*}\
49009afd 772 %{!fsyntax-only:%{S:%W{o*}%{!o*:-o %b.s}}}\
6de9cd9a 773 %{fsyntax-only:-o %j} %{-param*}\
ee4c708e
BE
774 %{fmudflap|fmudflapth:-fno-builtin -fno-merge-constants}\
775 %{coverage:-fprofile-arcs -ftest-coverage}";
ea414c97
ZW
776
777static const char *asm_options =
dc60b775
CD
778"%{--target-help:%:print-asm-header()} "
779#if HAVE_GNU_AS
780/* If GNU AS is used, then convert -w (no warnings), -I, and -v
781 to the assembler equivalents. */
782"%{v} %{w:-W} %{I*} "
783#endif
784"%a %Y %{c:%W{o*}%{!o*:-o %w%b%O}}%{!c:-o %d%w%u%O}";
ffd86336 785
5a8e2650 786static const char *invoke_as =
4977bab6 787#ifdef AS_NEEDS_DASH_FOR_PIPED_INPUT
d7f09764
DN
788"%{!fwpa:\
789 %{fcompare-debug=*|fdump-final-insns=*:%:compare-debug-dump-opt()}\
790 %{!S:-o %|.s |\n as %(asm_options) %|.s %A }\
791 }";
4977bab6 792#else
d7f09764
DN
793"%{!fwpa:\
794 %{fcompare-debug=*|fdump-final-insns=*:%:compare-debug-dump-opt()}\
795 %{!S:-o %|.s |\n as %(asm_options) %m.s %A }\
796 }";
4977bab6 797#endif
5a8e2650 798
ffd86336 799/* Some compilers have limits on line lengths, and the multilib_select
961b7009
MM
800 and/or multilib_matches strings can be very long, so we build them at
801 run time. */
ffd86336 802static struct obstack multilib_obstack;
3b304f5b
ZW
803static const char *multilib_select;
804static const char *multilib_matches;
805static const char *multilib_defaults;
806static const char *multilib_exclusions;
961b7009
MM
807
808/* Check whether a particular argument is a default argument. */
809
810#ifndef MULTILIB_DEFAULTS
811#define MULTILIB_DEFAULTS { "" }
812#endif
813
d25a45d4 814static const char *const multilib_defaults_raw[] = MULTILIB_DEFAULTS;
ed1f651b 815
db36994b
RS
816#ifndef DRIVER_SELF_SPECS
817#define DRIVER_SELF_SPECS ""
818#endif
819
953ff289
DN
820/* Adding -fopenmp should imply pthreads. This is particularly important
821 for targets that use different start files and suchlike. */
822#ifndef GOMP_SELF_SPECS
daf49354 823#define GOMP_SELF_SPECS "%{fopenmp|ftree-parallelize-loops=*: -pthread}"
953ff289
DN
824#endif
825
826static const char *const driver_self_specs[] = {
b5b8b0ac 827 "%{fdump-final-insns:-fdump-final-insns=.} %<fdump-final-insns",
e11ddaf5 828 DRIVER_SELF_SPECS, CONFIGURE_SPECS, GOMP_SELF_SPECS
953ff289 829};
db36994b 830
7816bea0
DJ
831#ifndef OPTION_DEFAULT_SPECS
832#define OPTION_DEFAULT_SPECS { "", "" }
833#endif
834
835struct default_spec
836{
837 const char *name;
838 const char *spec;
839};
840
841static const struct default_spec
842 option_default_specs[] = { OPTION_DEFAULT_SPECS };
843
3ac63d94
NC
844struct user_specs
845{
d9ac3a07 846 struct user_specs *next;
878f32c3 847 const char *filename;
d9ac3a07
MM
848};
849
850static struct user_specs *user_specs_head, *user_specs_tail;
851
ed1f651b
RS
852\f
853/* Record the mapping from file suffixes for compilation specs. */
854
855struct compiler
856{
878f32c3 857 const char *suffix; /* Use this compiler for input files
ed1f651b 858 whose names end in this suffix. */
ec32609a 859
ea414c97 860 const char *spec; /* To use this compiler, run this spec. */
a9374841
MM
861
862 const char *cpp_spec; /* If non-NULL, substitute this spec
863 for `%C', rather than the usual
864 cpp_spec. */
1ea7e6ad 865 const int combinable; /* If nonzero, compiler can deal with
0855eab7 866 multiple source files at once (IMA). */
1ea7e6ad 867 const int needs_preprocessing; /* If nonzero, source files need to
0855eab7 868 be run through a preprocessor. */
ed1f651b
RS
869};
870
871/* Pointer to a vector of `struct compiler' that gives the spec for
872 compiling a file, based on its suffix.
873 A file that does not end in any of these suffixes will be passed
874 unchanged to the loader and nothing else will be done to it.
875
876 An entry containing two 0s is used to terminate the vector.
877
878 If multiple entries match a file, the last matching one is used. */
879
880static struct compiler *compilers;
881
882/* Number of entries in `compilers', not counting the null terminator. */
883
884static int n_compilers;
885
886/* The default list of file name suffixes and their compilation specs. */
887
5e65297b 888static const struct compiler default_compilers[] =
ed1f651b 889{
4689ad58 890 /* Add lists of suffixes of known languages here. If those languages
e9a25f70
JL
891 were not present when we built the driver, we will hit these copies
892 and be given a more meaningful error than "file not used since
4689ad58 893 linking is not done". */
0855eab7 894 {".m", "#Objective-C", 0, 0, 0}, {".mi", "#Objective-C", 0, 0, 0},
ad8c162b
ZL
895 {".mm", "#Objective-C++", 0, 0, 0}, {".M", "#Objective-C++", 0, 0, 0},
896 {".mii", "#Objective-C++", 0, 0, 0},
7904f95f
EC
897 {".cc", "#C++", 0, 0, 0}, {".cxx", "#C++", 0, 0, 0},
898 {".cpp", "#C++", 0, 0, 0}, {".cp", "#C++", 0, 0, 0},
0855eab7
CT
899 {".c++", "#C++", 0, 0, 0}, {".C", "#C++", 0, 0, 0},
900 {".CPP", "#C++", 0, 0, 0}, {".ii", "#C++", 0, 0, 0},
901 {".ads", "#Ada", 0, 0, 0}, {".adb", "#Ada", 0, 0, 0},
3135ce84
FXC
902 {".f", "#Fortran", 0, 0, 0}, {".F", "#Fortran", 0, 0, 0},
903 {".for", "#Fortran", 0, 0, 0}, {".FOR", "#Fortran", 0, 0, 0},
904 {".ftn", "#Fortran", 0, 0, 0}, {".FTN", "#Fortran", 0, 0, 0},
905 {".fpp", "#Fortran", 0, 0, 0}, {".FPP", "#Fortran", 0, 0, 0},
906 {".f90", "#Fortran", 0, 0, 0}, {".F90", "#Fortran", 0, 0, 0},
907 {".f95", "#Fortran", 0, 0, 0}, {".F95", "#Fortran", 0, 0, 0},
908 {".f03", "#Fortran", 0, 0, 0}, {".F03", "#Fortran", 0, 0, 0},
909 {".f08", "#Fortran", 0, 0, 0}, {".F08", "#Fortran", 0, 0, 0},
0855eab7
CT
910 {".r", "#Ratfor", 0, 0, 0},
911 {".p", "#Pascal", 0, 0, 0}, {".pas", "#Pascal", 0, 0, 0},
912 {".java", "#Java", 0, 0, 0}, {".class", "#Java", 0, 0, 0},
913 {".zip", "#Java", 0, 0, 0}, {".jar", "#Java", 0, 0, 0},
7a938933 914 {".go", "#Go", 0, 1, 0},
4689ad58 915 /* Next come the entries for C. */
c3224d6f 916 {".c", "@c", 0, 0, 1},
ed1f651b 917 {"@c",
5a8e2650 918 /* cc1 has an integrated ISO C preprocessor. We should invoke the
f458d1d5 919 external preprocessor if -save-temps is given. */
f749a36b 920 "%{E|M|MM:%(trad_capable_cpp) %(cpp_options) %(cpp_debug_options)}\
5a8e2650 921 %{!E:%{!M:%{!MM:\
3028a95c 922 %{traditional:\
f458d1d5 923%eGNU C no longer supports -traditional without -E}\
c3224d6f
RG
924 %{save-temps*|traditional-cpp|no-integrated-cpp:%(trad_capable_cpp) \
925 %(cpp_options) -o %{save-temps*:%b.i} %{!save-temps*:%g.i} \n\
926 cc1 -fpreprocessed %{save-temps*:%b.i} %{!save-temps*:%g.i} \
927 %(cc1_options)}\
928 %{!save-temps*:%{!traditional-cpp:%{!no-integrated-cpp:\
929 cc1 %(cpp_unique_options) %(cc1_options)}}}\
930 %{!fsyntax-only:%(invoke_as)}}}}", 0, 0, 1},
ed1f651b 931 {"-",
6ba40dd7 932 "%{!E:%e-E or -x required when input is from standard input}\
0855eab7
CT
933 %(trad_capable_cpp) %(cpp_options) %(cpp_debug_options)", 0, 0, 0},
934 {".h", "@c-header", 0, 0, 0},
ed1f651b 935 {"@c-header",
17211ab5
GK
936 /* cc1 has an integrated ISO C preprocessor. We should invoke the
937 external preprocessor if -save-temps is given. */
938 "%{E|M|MM:%(trad_capable_cpp) %(cpp_options) %(cpp_debug_options)}\
939 %{!E:%{!M:%{!MM:\
14fdc613
MM
940 %{save-temps*|traditional-cpp|no-integrated-cpp:%(trad_capable_cpp) \
941 %(cpp_options) -o %{save-temps*:%b.i} %{!save-temps*:%g.i} \n\
942 cc1 -fpreprocessed %{save-temps*:%b.i} %{!save-temps*:%g.i} \
0de12fcc 943 %(cc1_options)\
9cc54940
AC
944 %{!fdump-ada-spec*:-o %g.s %{!o*:--output-pch=%i.gch}\
945 %W{o*:--output-pch=%*}}%V}\
14fdc613 946 %{!save-temps*:%{!traditional-cpp:%{!no-integrated-cpp:\
17211ab5 947 cc1 %(cpp_unique_options) %(cc1_options)\
9cc54940
AC
948 %{!fdump-ada-spec*:-o %g.s %{!o*:--output-pch=%i.gch}\
949 %W{o*:--output-pch=%*}}%V}}}}}}", 0, 0, 0},
c3224d6f 950 {".i", "@cpp-output", 0, 0, 0},
ed1f651b 951 {"@cpp-output",
2a782c52 952 "%{!M:%{!MM:%{!E:cc1 -fpreprocessed %i %(cc1_options) %{!fsyntax-only:%(invoke_as)}}}}", 0, 1, 0},
a0d43bac 953 {".s", "@assembler", 0, 0, 0},
ed1f651b 954 {"@assembler",
a0d43bac
JZ
955 "%{!M:%{!MM:%{!E:%{!S:as %(asm_debug) %(asm_options) %i %A }}}}", 0, 0, 0},
956 {".sx", "@assembler-with-cpp", 0, 0, 0},
957 {".S", "@assembler-with-cpp", 0, 0, 0},
ed1f651b 958 {"@assembler-with-cpp",
4977bab6 959#ifdef AS_NEEDS_DASH_FOR_PIPED_INPUT
2710d6d7 960 "%(trad_capable_cpp) -lang-asm %(cpp_options) -fno-directives-only\
8b968bd1 961 %{E|M|MM:%(cpp_debug_options)}\
4977bab6
ZW
962 %{!M:%{!MM:%{!E:%{!S:-o %|.s |\n\
963 as %(asm_debug) %(asm_options) %|.s %A }}}}"
964#else
2710d6d7 965 "%(trad_capable_cpp) -lang-asm %(cpp_options) -fno-directives-only\
4977bab6
ZW
966 %{E|M|MM:%(cpp_debug_options)}\
967 %{!M:%{!MM:%{!E:%{!S:-o %|.s |\n\
968 as %(asm_debug) %(asm_options) %m.s %A }}}}"
969#endif
a0d43bac 970 , 0, 0, 0},
1d088dee 971
1346ae41 972#include "specs.h"
f9da5064 973 /* Mark end of table. */
0855eab7 974 {0, 0, 0, 0, 0}
ed1f651b
RS
975};
976
977/* Number of elements in default_compilers, not counting the terminator. */
978
ca7558fc 979static const int n_default_compilers = ARRAY_SIZE (default_compilers) - 1;
ed1f651b 980
69d540bd
NF
981typedef char *char_p; /* For DEF_VEC_P. */
982DEF_VEC_P(char_p);
983DEF_VEC_ALLOC_P(char_p,heap);
984
ed1f651b 985/* A vector of options to give to the linker.
368dfd3a 986 These options are accumulated by %x,
ed1f651b 987 and substituted into the linker command with %X. */
69d540bd 988static VEC(char_p,heap) *linker_options;
c9ebacb8
RS
989
990/* A vector of options to give to the assembler.
991 These options are accumulated by -Wa,
57cb9b60 992 and substituted into the assembler command with %Y. */
69d540bd 993static VEC(char_p,heap) *assembler_options;
57cb9b60
JW
994
995/* A vector of options to give to the preprocessor.
996 These options are accumulated by -Wp,
997 and substituted into the preprocessor command with %Z. */
69d540bd 998static VEC(char_p,heap) *preprocessor_options;
ed1f651b 999\f
ed1f651b 1000static char *
1d088dee 1001skip_whitespace (char *p)
ed1f651b
RS
1002{
1003 while (1)
1004 {
1005 /* A fully-blank line is a delimiter in the SPEC file and shouldn't
1006 be considered whitespace. */
1007 if (p[0] == '\n' && p[1] == '\n' && p[2] == '\n')
1008 return p + 1;
1009 else if (*p == '\n' || *p == ' ' || *p == '\t')
1010 p++;
1011 else if (*p == '#')
1012 {
d25a45d4
KH
1013 while (*p != '\n')
1014 p++;
ed1f651b
RS
1015 p++;
1016 }
1017 else
1018 break;
1019 }
1020
1021 return p;
1022}
2296d164
RK
1023/* Structures to keep track of prefixes to try when looking for files. */
1024
1025struct prefix_list
1026{
8060c8ee 1027 const char *prefix; /* String to prepend to the path. */
2296d164
RK
1028 struct prefix_list *next; /* Next in linked list. */
1029 int require_machine_suffix; /* Don't use without machine_suffix. */
1030 /* 2 means try both machine_suffix and just_machine_suffix. */
5bbcd587
JJ
1031 int priority; /* Sort key - priority within list. */
1032 int os_multilib; /* 1 if OS multilib scheme should be used,
1033 0 for GCC multilib scheme. */
2296d164
RK
1034};
1035
1036struct path_prefix
1037{
1038 struct prefix_list *plist; /* List of prefixes to try */
1039 int max_len; /* Max length of a prefix in PLIST */
1040 const char *name; /* Name of this list (used in config stuff) */
1041};
1042
1043/* List of prefixes to try when looking for executables. */
1044
1045static struct path_prefix exec_prefixes = { 0, 0, "exec" };
1046
1047/* List of prefixes to try when looking for startup (crt0) files. */
1048
1049static struct path_prefix startfile_prefixes = { 0, 0, "startfile" };
1050
1051/* List of prefixes to try when looking for include files. */
1052
1053static struct path_prefix include_prefixes = { 0, 0, "include" };
1054
1055/* Suffix to attach to directories searched for commands.
1056 This looks like `MACHINE/VERSION/'. */
1057
1058static const char *machine_suffix = 0;
1059
1060/* Suffix to attach to directories searched for commands.
1061 This is just `MACHINE/'. */
1062
1063static const char *just_machine_suffix = 0;
1064
1065/* Adjusted value of GCC_EXEC_PREFIX envvar. */
1066
1067static const char *gcc_exec_prefix;
1068
a8ee6e2d
GK
1069/* Adjusted value of standard_libexec_prefix. */
1070
1071static const char *gcc_libexec_prefix;
1072
2296d164
RK
1073/* Default prefixes to attach to command names. */
1074
656c7a3a
AL
1075#ifndef STANDARD_STARTFILE_PREFIX_1
1076#define STANDARD_STARTFILE_PREFIX_1 "/lib/"
1077#endif
1078#ifndef STANDARD_STARTFILE_PREFIX_2
1079#define STANDARD_STARTFILE_PREFIX_2 "/usr/lib/"
1080#endif
7904f95f 1081
2989d30c 1082#ifdef CROSS_DIRECTORY_STRUCTURE /* Don't use these prefixes for a cross compiler. */
2296d164
RK
1083#undef MD_EXEC_PREFIX
1084#undef MD_STARTFILE_PREFIX
1085#undef MD_STARTFILE_PREFIX_1
1086#endif
1087
1088/* If no prefixes defined, use the null string, which will disable them. */
1089#ifndef MD_EXEC_PREFIX
1090#define MD_EXEC_PREFIX ""
1091#endif
1092#ifndef MD_STARTFILE_PREFIX
1093#define MD_STARTFILE_PREFIX ""
1094#endif
1095#ifndef MD_STARTFILE_PREFIX_1
1096#define MD_STARTFILE_PREFIX_1 ""
1097#endif
1098
f4c0a303
CD
1099/* These directories are locations set at configure-time based on the
1100 --prefix option provided to configure. Their initializers are
1101 defined in Makefile.in. These paths are not *directly* used when
1102 gcc_exec_prefix is set because, in that case, we know where the
1103 compiler has been installed, and use paths relative to that
1104 location instead. */
27c38fbe 1105static const char *const standard_exec_prefix = STANDARD_EXEC_PREFIX;
f4c0a303
CD
1106static const char *const standard_libexec_prefix = STANDARD_LIBEXEC_PREFIX;
1107static const char *const standard_bindir_prefix = STANDARD_BINDIR_PREFIX;
1108static const char *const standard_startfile_prefix = STANDARD_STARTFILE_PREFIX;
1109
1110/* For native compilers, these are well-known paths containing
1111 components that may be provided by the system. For cross
1112 compilers, these paths are not used. */
2296d164 1113static const char *md_exec_prefix = MD_EXEC_PREFIX;
2296d164
RK
1114static const char *md_startfile_prefix = MD_STARTFILE_PREFIX;
1115static const char *md_startfile_prefix_1 = MD_STARTFILE_PREFIX_1;
c662432e 1116static const char *const standard_startfile_prefix_1
656c7a3a
AL
1117 = STANDARD_STARTFILE_PREFIX_1;
1118static const char *const standard_startfile_prefix_2
1119 = STANDARD_STARTFILE_PREFIX_2;
2296d164 1120
f4c0a303
CD
1121/* A relative path to be used in finding the location of tools
1122 relative to the driver. */
27c38fbe 1123static const char *const tooldir_base_prefix = TOOLDIR_BASE_PREFIX;
a8ee6e2d 1124
2296d164
RK
1125/* Subdirectory to use for locating libraries. Set by
1126 set_multilib_dir based on the compilation options. */
1127
1128static const char *multilib_dir;
5bbcd587
JJ
1129
1130/* Subdirectory to use for locating libraries in OS conventions. Set by
1131 set_multilib_dir based on the compilation options. */
1132
1133static const char *multilib_os_dir;
ed1f651b 1134\f
0f41302f 1135/* Structure to keep track of the specs that have been defined so far.
4089dfab
JL
1136 These are accessed using %(specname) or %[specname] in a compiler
1137 or link spec. */
ed1f651b
RS
1138
1139struct spec_list
1140{
79aff5ac
MM
1141 /* The following 2 fields must be first */
1142 /* to allow EXTRA_SPECS to be initialized */
3b304f5b
ZW
1143 const char *name; /* name of the spec. */
1144 const char *ptr; /* available ptr if no static pointer */
79aff5ac
MM
1145
1146 /* The following fields are not initialized */
1147 /* by EXTRA_SPECS */
3b304f5b 1148 const char **ptr_spec; /* pointer to the spec itself. */
79aff5ac
MM
1149 struct spec_list *next; /* Next spec in linked list. */
1150 int name_len; /* length of the name */
1151 int alloc_p; /* whether string was allocated */
ed1f651b
RS
1152};
1153
79aff5ac 1154#define INIT_STATIC_SPEC(NAME,PTR) \
6496a589 1155{ NAME, NULL, PTR, (struct spec_list *) 0, sizeof (NAME) - 1, 0 }
79aff5ac 1156
3ac63d94
NC
1157/* List of statically defined specs. */
1158static struct spec_list static_specs[] =
1159{
79aff5ac 1160 INIT_STATIC_SPEC ("asm", &asm_spec),
5f0e9ea2 1161 INIT_STATIC_SPEC ("asm_debug", &asm_debug),
79aff5ac 1162 INIT_STATIC_SPEC ("asm_final", &asm_final_spec),
ea414c97 1163 INIT_STATIC_SPEC ("asm_options", &asm_options),
5a8e2650 1164 INIT_STATIC_SPEC ("invoke_as", &invoke_as),
79aff5ac 1165 INIT_STATIC_SPEC ("cpp", &cpp_spec),
ea414c97 1166 INIT_STATIC_SPEC ("cpp_options", &cpp_options),
8b968bd1 1167 INIT_STATIC_SPEC ("cpp_debug_options", &cpp_debug_options),
ffdeea47 1168 INIT_STATIC_SPEC ("cpp_unique_options", &cpp_unique_options),
ea414c97 1169 INIT_STATIC_SPEC ("trad_capable_cpp", &trad_capable_cpp),
79aff5ac 1170 INIT_STATIC_SPEC ("cc1", &cc1_spec),
ea414c97 1171 INIT_STATIC_SPEC ("cc1_options", &cc1_options),
79aff5ac 1172 INIT_STATIC_SPEC ("cc1plus", &cc1plus_spec),
bbd7687d 1173 INIT_STATIC_SPEC ("link_gcc_c_sequence", &link_gcc_c_sequence_spec),
77008252 1174 INIT_STATIC_SPEC ("link_ssp", &link_ssp_spec),
79aff5ac
MM
1175 INIT_STATIC_SPEC ("endfile", &endfile_spec),
1176 INIT_STATIC_SPEC ("link", &link_spec),
1177 INIT_STATIC_SPEC ("lib", &lib_spec),
6de9cd9a
DN
1178 INIT_STATIC_SPEC ("mfwrap", &mfwrap_spec),
1179 INIT_STATIC_SPEC ("mflib", &mflib_spec),
953ff289 1180 INIT_STATIC_SPEC ("link_gomp", &link_gomp_spec),
79aff5ac
MM
1181 INIT_STATIC_SPEC ("libgcc", &libgcc_spec),
1182 INIT_STATIC_SPEC ("startfile", &startfile_spec),
79aff5ac
MM
1183 INIT_STATIC_SPEC ("cross_compile", &cross_compile),
1184 INIT_STATIC_SPEC ("version", &compiler_version),
1185 INIT_STATIC_SPEC ("multilib", &multilib_select),
1186 INIT_STATIC_SPEC ("multilib_defaults", &multilib_defaults),
1187 INIT_STATIC_SPEC ("multilib_extra", &multilib_extra),
1188 INIT_STATIC_SPEC ("multilib_matches", &multilib_matches),
0a8d6618 1189 INIT_STATIC_SPEC ("multilib_exclusions", &multilib_exclusions),
5bbcd587 1190 INIT_STATIC_SPEC ("multilib_options", &multilib_options),
10da1131 1191 INIT_STATIC_SPEC ("linker", &linker_name_spec),
d7f09764
DN
1192 INIT_STATIC_SPEC ("linker_plugin_file", &linker_plugin_file_spec),
1193 INIT_STATIC_SPEC ("lto_wrapper", &lto_wrapper_spec),
1194 INIT_STATIC_SPEC ("lto_gcc", &lto_gcc_spec),
ea414c97 1195 INIT_STATIC_SPEC ("link_libgcc", &link_libgcc_spec),
2296d164
RK
1196 INIT_STATIC_SPEC ("md_exec_prefix", &md_exec_prefix),
1197 INIT_STATIC_SPEC ("md_startfile_prefix", &md_startfile_prefix),
1198 INIT_STATIC_SPEC ("md_startfile_prefix_1", &md_startfile_prefix_1),
343f59d9 1199 INIT_STATIC_SPEC ("startfile_prefix_spec", &startfile_prefix_spec),
380e5ca4 1200 INIT_STATIC_SPEC ("sysroot_spec", &sysroot_spec),
e7f13528
GP
1201 INIT_STATIC_SPEC ("sysroot_suffix_spec", &sysroot_suffix_spec),
1202 INIT_STATIC_SPEC ("sysroot_hdrs_suffix_spec", &sysroot_hdrs_suffix_spec),
79aff5ac
MM
1203};
1204
1205#ifdef EXTRA_SPECS /* additional specs needed */
829245be 1206/* Structure to keep track of just the first two args of a spec_list.
3ac63d94 1207 That is all that the EXTRA_SPECS macro gives us. */
829245be
KG
1208struct spec_list_1
1209{
8b60264b
KG
1210 const char *const name;
1211 const char *const ptr;
829245be
KG
1212};
1213
8b60264b 1214static const struct spec_list_1 extra_specs_1[] = { EXTRA_SPECS };
d25a45d4 1215static struct spec_list *extra_specs = (struct spec_list *) 0;
79aff5ac
MM
1216#endif
1217
1218/* List of dynamically allocates specs that have been defined so far. */
1219
9218435e 1220static struct spec_list *specs = (struct spec_list *) 0;
79aff5ac 1221\f
f3226a90
JT
1222/* List of static spec functions. */
1223
1224static const struct spec_function static_spec_functions[] =
1225{
30d8946b 1226 { "getenv", getenv_spec_function },
f3226a90 1227 { "if-exists", if_exists_spec_function },
152a5a9c 1228 { "if-exists-else", if_exists_else_spec_function },
3dd53121 1229 { "replace-outfile", replace_outfile_spec_function },
2642f659 1230 { "remove-outfile", remove_outfile_spec_function },
ed5b9f96 1231 { "version-compare", version_compare_spec_function },
953ff289 1232 { "include", include_spec_function },
4adbd5dd 1233 { "find-file", find_file_spec_function },
c1ce46a5 1234 { "find-plugindir", find_plugindir_spec_function },
a0f87454 1235 { "print-asm-header", print_asm_header_spec_function },
2153915d
AO
1236 { "compare-debug-dump-opt", compare_debug_dump_opt_spec_function },
1237 { "compare-debug-self-opt", compare_debug_self_opt_spec_function },
1238 { "compare-debug-auxbase-opt", compare_debug_auxbase_opt_spec_function },
9a37bc07 1239 { "pass-through-libs", pass_through_libs_spec_func },
fa959ce4
MM
1240#ifdef EXTRA_SPEC_FUNCTIONS
1241 EXTRA_SPEC_FUNCTIONS
1242#endif
f3226a90
JT
1243 { 0, 0 }
1244};
1245
1246static int processing_spec_function;
1247\f
049f6ec9
MM
1248/* Add appropriate libgcc specs to OBSTACK, taking into account
1249 various permutations of -shared-libgcc, -shared, and such. */
1250
328163dc 1251#if defined(ENABLE_SHARED_LIBGCC) && !defined(REAL_LIBGCC_SPEC)
8efcd34f
AM
1252
1253#ifndef USE_LD_AS_NEEDED
1254#define USE_LD_AS_NEEDED 0
1255#endif
1256
049f6ec9 1257static void
1d088dee
AJ
1258init_gcc_specs (struct obstack *obstack, const char *shared_name,
1259 const char *static_name, const char *eh_name)
049f6ec9 1260{
5c181756
AO
1261 char *buf;
1262
5e4f1974
AM
1263 buf = concat ("%{static|static-libgcc:", static_name, " ", eh_name, "}"
1264 "%{!static:%{!static-libgcc:"
8efcd34f 1265#if USE_LD_AS_NEEDED
5e4f1974
AM
1266 "%{!shared-libgcc:",
1267 static_name, " --as-needed ", shared_name, " --no-as-needed"
1268 "}"
1269 "%{shared-libgcc:",
1270 shared_name, "%{!shared: ", static_name, "}"
1271 "}"
765f1bf9 1272#else
5e4f1974
AM
1273 "%{!shared:"
1274 "%{!shared-libgcc:", static_name, " ", eh_name, "}"
1275 "%{shared-libgcc:", shared_name, " ", static_name, "}"
1276 "}"
275b60d6 1277#ifdef LINK_EH_SPEC
5e4f1974
AM
1278 "%{shared:"
1279 "%{shared-libgcc:", shared_name, "}"
1280 "%{!shared-libgcc:", static_name, "}"
1281 "}"
275b60d6 1282#else
5e4f1974 1283 "%{shared:", shared_name, "}"
765f1bf9 1284#endif
275b60d6 1285#endif
5e4f1974 1286 "}}", NULL);
5c181756
AO
1287
1288 obstack_grow (obstack, buf, strlen (buf));
1289 free (buf);
049f6ec9 1290}
6894579f 1291#endif /* ENABLE_SHARED_LIBGCC */
049f6ec9 1292
79aff5ac
MM
1293/* Initialize the specs lookup routines. */
1294
1295static void
7e51717c 1296init_spec (void)
79aff5ac 1297{
9218435e
KH
1298 struct spec_list *next = (struct spec_list *) 0;
1299 struct spec_list *sl = (struct spec_list *) 0;
79aff5ac
MM
1300 int i;
1301
1302 if (specs)
3ac63d94 1303 return; /* Already initialized. */
79aff5ac 1304
20df0482 1305 if (verbose_flag)
2dec80c7 1306 fnotice (stderr, "Using built-in specs.\n");
20df0482 1307
79aff5ac 1308#ifdef EXTRA_SPECS
e1e4cdc4 1309 extra_specs = XCNEWVEC (struct spec_list, ARRAY_SIZE (extra_specs_1));
9218435e 1310
b6a1cbae 1311 for (i = ARRAY_SIZE (extra_specs_1) - 1; i >= 0; i--)
03fc1620
JW
1312 {
1313 sl = &extra_specs[i];
829245be
KG
1314 sl->name = extra_specs_1[i].name;
1315 sl->ptr = extra_specs_1[i].ptr;
03fc1620
JW
1316 sl->next = next;
1317 sl->name_len = strlen (sl->name);
1318 sl->ptr_spec = &sl->ptr;
1319 next = sl;
1320 }
79aff5ac
MM
1321#endif
1322
b6a1cbae 1323 for (i = ARRAY_SIZE (static_specs) - 1; i >= 0; i--)
79aff5ac
MM
1324 {
1325 sl = &static_specs[i];
1326 sl->next = next;
1327 next = sl;
1328 }
1329
328163dc 1330#if defined(ENABLE_SHARED_LIBGCC) && !defined(REAL_LIBGCC_SPEC)
9db0819e
RH
1331 /* ??? If neither -shared-libgcc nor --static-libgcc was
1332 seen, then we should be making an educated guess. Some proposed
1333 heuristics for ELF include:
1334
1335 (1) If "-Wl,--export-dynamic", then it's a fair bet that the
1336 program will be doing dynamic loading, which will likely
1337 need the shared libgcc.
1338
1339 (2) If "-ldl", then it's also a fair bet that we're doing
1340 dynamic loading.
1341
1342 (3) For each ET_DYN we're linking against (either through -lfoo
1343 or /some/path/foo.so), check to see whether it or one of
ff7cc307 1344 its dependencies depends on a shared libgcc.
9db0819e
RH
1345
1346 (4) If "-shared"
1347
1348 If the runtime is fixed to look for program headers instead
1349 of calling __register_frame_info at all, for each object,
1350 use the shared libgcc if any EH symbol referenced.
1351
1352 If crtstuff is fixed to not invoke __register_frame_info
1353 automatically, for each object, use the shared libgcc if
1354 any non-empty unwind section found.
1355
1356 Doing any of this probably requires invoking an external program to
1357 do the actual object file scanning. */
1358 {
1359 const char *p = libgcc_spec;
1360 int in_sep = 1;
589005ff 1361
16757495 1362 /* Transform the extant libgcc_spec into one that uses the shared libgcc
9db0819e
RH
1363 when given the proper command line arguments. */
1364 while (*p)
1365 {
589005ff 1366 if (in_sep && *p == '-' && strncmp (p, "-lgcc", 5) == 0)
9db0819e 1367 {
049f6ec9 1368 init_gcc_specs (&obstack,
049f6ec9 1369 "-lgcc_s"
443728bb
L
1370#ifdef USE_LIBUNWIND_EXCEPTIONS
1371 " -lunwind"
4977bab6 1372#endif
348d71c7
JW
1373 ,
1374 "-lgcc",
1375 "-lgcc_eh"
4977bab6 1376#ifdef USE_LIBUNWIND_EXCEPTIONS
7e9d8517
L
1377# ifdef HAVE_LD_STATIC_DYNAMIC
1378 " %{!static:-Bstatic} -lunwind %{!static:-Bdynamic}"
1379# else
4977bab6 1380 " -lunwind"
7e9d8517 1381# endif
9db0819e 1382#endif
348d71c7
JW
1383 );
1384
ba2b7435 1385 p += 5;
9db0819e
RH
1386 in_sep = 0;
1387 }
ba2b7435 1388 else if (in_sep && *p == 'l' && strncmp (p, "libgcc.a%s", 10) == 0)
9db0819e
RH
1389 {
1390 /* Ug. We don't know shared library extensions. Hope that
1391 systems that use this form don't do shared libraries. */
049f6ec9 1392 init_gcc_specs (&obstack,
4e8d0554 1393 "-lgcc_s",
275b60d6 1394 "libgcc.a%s",
aedec8dd
JW
1395 "libgcc_eh.a%s"
1396#ifdef USE_LIBUNWIND_EXCEPTIONS
1397 " -lunwind"
1398#endif
1399 );
ba2b7435 1400 p += 10;
9db0819e
RH
1401 in_sep = 0;
1402 }
1403 else
1404 {
1405 obstack_1grow (&obstack, *p);
1406 in_sep = (*p == ' ');
1407 p += 1;
1408 }
1409 }
1410
1411 obstack_1grow (&obstack, '\0');
7973fd2a 1412 libgcc_spec = XOBFINISH (&obstack, const char *);
9db0819e
RH
1413 }
1414#endif
c64688ae
RH
1415#ifdef USE_AS_TRADITIONAL_FORMAT
1416 /* Prepend "--traditional-format" to whatever asm_spec we had before. */
1417 {
8b60264b 1418 static const char tf[] = "--traditional-format ";
c64688ae
RH
1419 obstack_grow (&obstack, tf, sizeof(tf) - 1);
1420 obstack_grow0 (&obstack, asm_spec, strlen (asm_spec));
7973fd2a 1421 asm_spec = XOBFINISH (&obstack, const char *);
c64688ae
RH
1422 }
1423#endif
cd533689
PP
1424
1425#if defined LINK_EH_SPEC || defined LINK_BUILDID_SPEC
1426# ifdef LINK_BUILDID_SPEC
1427 /* Prepend LINK_BUILDID_SPEC to whatever link_spec we had before. */
1428 obstack_grow (&obstack, LINK_BUILDID_SPEC, sizeof(LINK_BUILDID_SPEC) - 1);
1429# endif
1430# ifdef LINK_EH_SPEC
275b60d6
JJ
1431 /* Prepend LINK_EH_SPEC to whatever link_spec we had before. */
1432 obstack_grow (&obstack, LINK_EH_SPEC, sizeof(LINK_EH_SPEC) - 1);
cd533689 1433# endif
275b60d6 1434 obstack_grow0 (&obstack, link_spec, strlen (link_spec));
7973fd2a 1435 link_spec = XOBFINISH (&obstack, const char *);
275b60d6 1436#endif
9db0819e 1437
79aff5ac
MM
1438 specs = sl;
1439}
ed1f651b
RS
1440\f
1441/* Change the value of spec NAME to SPEC. If SPEC is empty, then the spec is
1442 removed; If the spec starts with a + then SPEC is added to the end of the
0f41302f 1443 current spec. */
ed1f651b
RS
1444
1445static void
1d088dee 1446set_spec (const char *name, const char *spec)
ed1f651b
RS
1447{
1448 struct spec_list *sl;
3b304f5b 1449 const char *old_spec;
79aff5ac
MM
1450 int name_len = strlen (name);
1451 int i;
1452
3ac63d94 1453 /* If this is the first call, initialize the statically allocated specs. */
20df0482
MM
1454 if (!specs)
1455 {
9218435e 1456 struct spec_list *next = (struct spec_list *) 0;
b6a1cbae 1457 for (i = ARRAY_SIZE (static_specs) - 1; i >= 0; i--)
20df0482
MM
1458 {
1459 sl = &static_specs[i];
1460 sl->next = next;
1461 next = sl;
1462 }
1463 specs = sl;
1464 }
1465
3ac63d94 1466 /* See if the spec already exists. */
ed1f651b 1467 for (sl = specs; sl; sl = sl->next)
79aff5ac 1468 if (name_len == sl->name_len && !strcmp (sl->name, name))
ed1f651b
RS
1469 break;
1470
1471 if (!sl)
1472 {
3ac63d94 1473 /* Not found - make it. */
5ed6ace5 1474 sl = XNEW (struct spec_list);
ad85216e 1475 sl->name = xstrdup (name);
79aff5ac
MM
1476 sl->name_len = name_len;
1477 sl->ptr_spec = &sl->ptr;
1478 sl->alloc_p = 0;
1479 *(sl->ptr_spec) = "";
ed1f651b
RS
1480 sl->next = specs;
1481 specs = sl;
1482 }
1483
79aff5ac 1484 old_spec = *(sl->ptr_spec);
e51712db 1485 *(sl->ptr_spec) = ((spec[0] == '+' && ISSPACE ((unsigned char)spec[1]))
d4f2852f 1486 ? concat (old_spec, spec + 1, NULL)
ad85216e 1487 : xstrdup (spec));
841faeed 1488
20df0482
MM
1489#ifdef DEBUG_SPECS
1490 if (verbose_flag)
2dec80c7 1491 fnotice (stderr, "Setting spec %s to '%s'\n\n", name, *(sl->ptr_spec));
20df0482
MM
1492#endif
1493
3ac63d94 1494 /* Free the old spec. */
79aff5ac 1495 if (old_spec && sl->alloc_p)
b1d5455a 1496 free (CONST_CAST(char *, old_spec));
79aff5ac
MM
1497
1498 sl->alloc_p = 1;
ed1f651b
RS
1499}
1500\f
1501/* Accumulate a command (program name and args), and run it. */
1502
5b634ee0
JM
1503typedef const char *const_char_p; /* For DEF_VEC_P. */
1504DEF_VEC_P(const_char_p);
1505DEF_VEC_ALLOC_P(const_char_p,heap);
ed1f651b 1506
5b634ee0 1507/* Vector of pointers to arguments in the current line of specifications. */
ed1f651b 1508
5b634ee0 1509static VEC(const_char_p,heap) *argbuf;
ed1f651b 1510
5b634ee0 1511/* Position in the argbuf vector containing the name of the output file
0855eab7
CT
1512 (the value associated with the "-o" flag). */
1513
1514static int have_o_argbuf_index = 0;
1515
1b5bc8e9 1516/* Were the options -c, -S or -E passed. */
af41c57d
AP
1517static int have_c = 0;
1518
1519/* Was the option -o passed. */
1520static int have_o = 0;
1521
49009afd
JL
1522/* This is the list of suffixes and codes (%g/%u/%U/%j) and the associated
1523 temp file. If the HOST_BIT_BUCKET is used for %j, no entry is made for
1524 it here. */
fb266030
TW
1525
1526static struct temp_name {
878f32c3 1527 const char *suffix; /* suffix associated with the code. */
fb266030
TW
1528 int length; /* strlen (suffix). */
1529 int unique; /* Indicates whether %g or %u/%U was used. */
878f32c3 1530 const char *filename; /* associated filename. */
fb266030
TW
1531 int filename_length; /* strlen (filename). */
1532 struct temp_name *next;
1533} *temp_names;
39d45901 1534
ed1f651b
RS
1535/* Number of commands executed so far. */
1536
1537static int execution_count;
1538
3b9b4d3f
RS
1539/* Number of commands that exited with a signal. */
1540
1541static int signal_count;
ed1f651b 1542\f
f3226a90
JT
1543/* Allocate the argument vector. */
1544
1545static void
7e51717c 1546alloc_args (void)
f3226a90 1547{
5b634ee0 1548 argbuf = VEC_alloc (const_char_p, heap, 10);
f3226a90
JT
1549}
1550
ed1f651b
RS
1551/* Clear out the vector of arguments (after a command is executed). */
1552
1553static void
1d088dee 1554clear_args (void)
ed1f651b 1555{
5b634ee0 1556 VEC_truncate (const_char_p, argbuf, 0);
ed1f651b
RS
1557}
1558
1559/* Add one argument to the vector at the end.
1560 This is done when a space is seen or at the end of the line.
1561 If DELETE_ALWAYS is nonzero, the arg is a filename
1562 and the file should be deleted eventually.
1563 If DELETE_FAILURE is nonzero, the arg is a filename
1564 and the file should be deleted if this compilation fails. */
1565
1566static void
1d088dee 1567store_arg (const char *arg, int delete_always, int delete_failure)
ed1f651b 1568{
5b634ee0 1569 VEC_safe_push (const_char_p, heap, argbuf, arg);
ed1f651b 1570
0855eab7 1571 if (strcmp (arg, "-o") == 0)
5b634ee0 1572 have_o_argbuf_index = VEC_length (const_char_p, argbuf);
ed1f651b 1573 if (delete_always || delete_failure)
d6205185
RG
1574 {
1575 const char *p;
1576 /* If the temporary file we should delete is specified as
1577 part of a joined argument extract the filename. */
1578 if (arg[0] == '-'
1579 && (p = strrchr (arg, '=')))
1580 arg = p + 1;
1581 record_temp_file (arg, delete_always, delete_failure);
1582 }
ed1f651b
RS
1583}
1584\f
ff7cc307 1585/* Load specs from a file name named FILENAME, replacing occurrences of
9218435e 1586 various different types of line-endings, \r\n, \n\r and just \r, with
b633b6c0 1587 a single \n. */
20df0482 1588
d25a45d4 1589static char *
1d088dee 1590load_specs (const char *filename)
20df0482
MM
1591{
1592 int desc;
1593 int readlen;
1594 struct stat statbuf;
1595 char *buffer;
b633b6c0
MK
1596 char *buffer_p;
1597 char *specs;
1598 char *specs_p;
20df0482
MM
1599
1600 if (verbose_flag)
2dec80c7 1601 fnotice (stderr, "Reading specs from %s\n", filename);
20df0482
MM
1602
1603 /* Open and stat the file. */
1604 desc = open (filename, O_RDONLY, 0);
1605 if (desc < 0)
1606 pfatal_with_name (filename);
1607 if (stat (filename, &statbuf) < 0)
1608 pfatal_with_name (filename);
1609
1610 /* Read contents of file into BUFFER. */
5ed6ace5 1611 buffer = XNEWVEC (char, statbuf.st_size + 1);
20df0482
MM
1612 readlen = read (desc, buffer, (unsigned) statbuf.st_size);
1613 if (readlen < 0)
1614 pfatal_with_name (filename);
1615 buffer[readlen] = 0;
1616 close (desc);
1617
5ed6ace5 1618 specs = XNEWVEC (char, readlen + 1);
b633b6c0
MK
1619 specs_p = specs;
1620 for (buffer_p = buffer; buffer_p && *buffer_p; buffer_p++)
1621 {
1622 int skip = 0;
1623 char c = *buffer_p;
1624 if (c == '\r')
d25a45d4
KH
1625 {
1626 if (buffer_p > buffer && *(buffer_p - 1) == '\n') /* \n\r */
b633b6c0 1627 skip = 1;
d25a45d4 1628 else if (*(buffer_p + 1) == '\n') /* \r\n */
b633b6c0
MK
1629 skip = 1;
1630 else /* \r */
1631 c = '\n';
1632 }
1633 if (! skip)
1634 *specs_p++ = c;
1635 }
1636 *specs_p = '\0';
1637
1638 free (buffer);
1639 return (specs);
1640}
1641
1642/* Read compilation specs from a file named FILENAME,
1643 replacing the default ones.
1644
1645 A suffix which starts with `*' is a definition for
1646 one of the machine-specific sub-specs. The "suffix" should be
0fef3fd0 1647 *asm, *cc1, *cpp, *link, *startfile, etc.
b633b6c0
MK
1648 The corresponding spec is stored in asm_spec, etc.,
1649 rather than in the `compilers' vector.
1650
1651 Anything invalid in the file is a fatal error. */
1652
1653static void
1d088dee 1654read_specs (const char *filename, int main_p)
b633b6c0
MK
1655{
1656 char *buffer;
b3694847 1657 char *p;
b633b6c0
MK
1658
1659 buffer = load_specs (filename);
1660
20df0482
MM
1661 /* Scan BUFFER for specs, putting them in the vector. */
1662 p = buffer;
1663 while (1)
1664 {
1665 char *suffix;
1666 char *spec;
1667 char *in, *out, *p1, *p2, *p3;
1668
1669 /* Advance P in BUFFER to the next nonblank nocomment line. */
1670 p = skip_whitespace (p);
1671 if (*p == 0)
1672 break;
1673
1674 /* Is this a special command that starts with '%'? */
1675 /* Don't allow this for the main specs file, since it would
1676 encourage people to overwrite it. */
1677 if (*p == '%' && !main_p)
1678 {
1679 p1 = p;
e9a25f70
JL
1680 while (*p && *p != '\n')
1681 p++;
1682
d25a45d4
KH
1683 /* Skip '\n'. */
1684 p++;
20df0482 1685
d25a45d4 1686 if (!strncmp (p1, "%include", sizeof ("%include") - 1)
e9a25f70
JL
1687 && (p1[sizeof "%include" - 1] == ' '
1688 || p1[sizeof "%include" - 1] == '\t'))
20df0482
MM
1689 {
1690 char *new_filename;
1691
1692 p1 += sizeof ("%include");
e9a25f70
JL
1693 while (*p1 == ' ' || *p1 == '\t')
1694 p1++;
20df0482
MM
1695
1696 if (*p1++ != '<' || p[-2] != '>')
2dec80c7
JM
1697 fatal_error ("specs %%include syntax malformed after "
1698 "%ld characters",
1699 (long) (p1 - buffer + 1));
20df0482
MM
1700
1701 p[-2] = '\0';
00dcee0c 1702 new_filename = find_a_file (&startfile_prefixes, p1, R_OK, true);
20df0482
MM
1703 read_specs (new_filename ? new_filename : p1, FALSE);
1704 continue;
1705 }
e9a25f70
JL
1706 else if (!strncmp (p1, "%include_noerr", sizeof "%include_noerr" - 1)
1707 && (p1[sizeof "%include_noerr" - 1] == ' '
1708 || p1[sizeof "%include_noerr" - 1] == '\t'))
20df0482
MM
1709 {
1710 char *new_filename;
1711
e9a25f70 1712 p1 += sizeof "%include_noerr";
d25a45d4
KH
1713 while (*p1 == ' ' || *p1 == '\t')
1714 p1++;
20df0482
MM
1715
1716 if (*p1++ != '<' || p[-2] != '>')
2dec80c7
JM
1717 fatal_error ("specs %%include syntax malformed after "
1718 "%ld characters",
1719 (long) (p1 - buffer + 1));
20df0482
MM
1720
1721 p[-2] = '\0';
00dcee0c 1722 new_filename = find_a_file (&startfile_prefixes, p1, R_OK, true);
20df0482
MM
1723 if (new_filename)
1724 read_specs (new_filename, FALSE);
1725 else if (verbose_flag)
2dec80c7 1726 fnotice (stderr, "could not find specs file %s\n", p1);
20df0482
MM
1727 continue;
1728 }
e9a25f70
JL
1729 else if (!strncmp (p1, "%rename", sizeof "%rename" - 1)
1730 && (p1[sizeof "%rename" - 1] == ' '
1731 || p1[sizeof "%rename" - 1] == '\t'))
20df0482
MM
1732 {
1733 int name_len;
1734 struct spec_list *sl;
169ce44d 1735 struct spec_list *newsl;
20df0482 1736
169ce44d 1737 /* Get original name. */
e9a25f70
JL
1738 p1 += sizeof "%rename";
1739 while (*p1 == ' ' || *p1 == '\t')
1740 p1++;
1741
9218435e 1742 if (! ISALPHA ((unsigned char) *p1))
2dec80c7
JM
1743 fatal_error ("specs %%rename syntax malformed after "
1744 "%ld characters",
1745 (long) (p1 - buffer));
20df0482
MM
1746
1747 p2 = p1;
9218435e 1748 while (*p2 && !ISSPACE ((unsigned char) *p2))
e9a25f70
JL
1749 p2++;
1750
20df0482 1751 if (*p2 != ' ' && *p2 != '\t')
2dec80c7
JM
1752 fatal_error ("specs %%rename syntax malformed after "
1753 "%ld characters",
1754 (long) (p2 - buffer));
20df0482
MM
1755
1756 name_len = p2 - p1;
1757 *p2++ = '\0';
e9a25f70
JL
1758 while (*p2 == ' ' || *p2 == '\t')
1759 p2++;
1760
9218435e 1761 if (! ISALPHA ((unsigned char) *p2))
2dec80c7
JM
1762 fatal_error ("specs %%rename syntax malformed after "
1763 "%ld characters",
1764 (long) (p2 - buffer));
20df0482 1765
d25a45d4 1766 /* Get new spec name. */
20df0482 1767 p3 = p2;
9218435e 1768 while (*p3 && !ISSPACE ((unsigned char) *p3))
e9a25f70
JL
1769 p3++;
1770
d25a45d4 1771 if (p3 != p - 1)
2dec80c7
JM
1772 fatal_error ("specs %%rename syntax malformed after "
1773 "%ld characters",
1774 (long) (p3 - buffer));
20df0482
MM
1775 *p3 = '\0';
1776
1777 for (sl = specs; sl; sl = sl->next)
1778 if (name_len == sl->name_len && !strcmp (sl->name, p1))
1779 break;
1780
1781 if (!sl)
2dec80c7 1782 fatal_error ("specs %s spec was not found to be renamed", p1);
20df0482 1783
e9a25f70 1784 if (strcmp (p1, p2) == 0)
20df0482
MM
1785 continue;
1786
169ce44d
NC
1787 for (newsl = specs; newsl; newsl = newsl->next)
1788 if (strcmp (newsl->name, p2) == 0)
bdc6b402
JM
1789 fatal_error ("%s: attempt to rename spec %qs to "
1790 "already defined spec %qs",
169ce44d
NC
1791 filename, p1, p2);
1792
20df0482
MM
1793 if (verbose_flag)
1794 {
2dec80c7 1795 fnotice (stderr, "rename spec %s to %s\n", p1, p2);
20df0482 1796#ifdef DEBUG_SPECS
2dec80c7 1797 fnotice (stderr, "spec is '%s'\n\n", *(sl->ptr_spec));
20df0482
MM
1798#endif
1799 }
1800
1801 set_spec (p2, *(sl->ptr_spec));
1802 if (sl->alloc_p)
b1d5455a 1803 free (CONST_CAST (char *, *(sl->ptr_spec)));
20df0482
MM
1804
1805 *(sl->ptr_spec) = "";
1806 sl->alloc_p = 0;
1807 continue;
1808 }
1809 else
2dec80c7
JM
1810 fatal_error ("specs unknown %% command after %ld characters",
1811 (long) (p1 - buffer));
20df0482
MM
1812 }
1813
1814 /* Find the colon that should end the suffix. */
1815 p1 = p;
e9a25f70
JL
1816 while (*p1 && *p1 != ':' && *p1 != '\n')
1817 p1++;
1818
20df0482
MM
1819 /* The colon shouldn't be missing. */
1820 if (*p1 != ':')
2dec80c7
JM
1821 fatal_error ("specs file malformed after %ld characters",
1822 (long) (p1 - buffer));
e9a25f70 1823
20df0482
MM
1824 /* Skip back over trailing whitespace. */
1825 p2 = p1;
e9a25f70
JL
1826 while (p2 > buffer && (p2[-1] == ' ' || p2[-1] == '\t'))
1827 p2--;
1828
20df0482
MM
1829 /* Copy the suffix to a string. */
1830 suffix = save_string (p, p2 - p);
1831 /* Find the next line. */
1832 p = skip_whitespace (p1 + 1);
1833 if (p[1] == 0)
2dec80c7
JM
1834 fatal_error ("specs file malformed after %ld characters",
1835 (long) (p - buffer));
e9a25f70 1836
20df0482 1837 p1 = p;
bbeb7b65
JW
1838 /* Find next blank line or end of string. */
1839 while (*p1 && !(*p1 == '\n' && (p1[1] == '\n' || p1[1] == '\0')))
e9a25f70
JL
1840 p1++;
1841
20df0482
MM
1842 /* Specs end at the blank line and do not include the newline. */
1843 spec = save_string (p, p1 - p);
1844 p = p1;
1845
1846 /* Delete backslash-newline sequences from the spec. */
1847 in = spec;
1848 out = spec;
1849 while (*in != 0)
1850 {
1851 if (in[0] == '\\' && in[1] == '\n')
1852 in += 2;
1853 else if (in[0] == '#')
e9a25f70
JL
1854 while (*in && *in != '\n')
1855 in++;
1856
20df0482
MM
1857 else
1858 *out++ = *in++;
1859 }
1860 *out = 0;
1861
1862 if (suffix[0] == '*')
1863 {
1864 if (! strcmp (suffix, "*link_command"))
1865 link_command_spec = spec;
1866 else
1867 set_spec (suffix + 1, spec);
1868 }
1869 else
1870 {
1871 /* Add this pair to the vector. */
1872 compilers
e1e4cdc4 1873 = XRESIZEVEC (struct compiler, compilers, n_compilers + 2);
e9a25f70 1874
20df0482 1875 compilers[n_compilers].suffix = suffix;
ea414c97 1876 compilers[n_compilers].spec = spec;
20df0482 1877 n_compilers++;
9257393c 1878 memset (&compilers[n_compilers], 0, sizeof compilers[n_compilers]);
20df0482
MM
1879 }
1880
1881 if (*suffix == 0)
1882 link_command_spec = spec;
1883 }
1884
1885 if (link_command_spec == 0)
2dec80c7 1886 fatal_error ("spec file has no spec for linking");
20df0482
MM
1887}
1888\f
ed1f651b
RS
1889/* Record the names of temporary files we tell compilers to write,
1890 and delete them at the end of the run. */
1891
aa34f42c
RG
1892/* This is the common prefix we use to make temp file names.
1893 It is chosen once for each run of this program.
1894 It is substituted into a spec by %g or %j.
1895 Thus, all temp file names contain this prefix.
1896 In practice, all temp file names start with this prefix.
1897
1898 This prefix comes from the envvar TMPDIR if it is defined;
1899 otherwise, from the P_tmpdir macro if that is defined;
1900 otherwise, in /usr/tmp or /tmp;
1901 or finally the current directory if all else fails. */
1902
1903static const char *temp_filename;
1904
1905/* Length of the prefix. */
1906
1907static int temp_filename_length;
1908
ed1f651b
RS
1909/* Define the list of temporary files to delete. */
1910
1911struct temp_file
1912{
878f32c3 1913 const char *name;
ed1f651b
RS
1914 struct temp_file *next;
1915};
1916
1917/* Queue of files to delete on success or failure of compilation. */
1918static struct temp_file *always_delete_queue;
1919/* Queue of files to delete on failure of compilation. */
1920static struct temp_file *failure_delete_queue;
1921
1922/* Record FILENAME as a file to be deleted automatically.
1923 ALWAYS_DELETE nonzero means delete it if all compilation succeeds;
1924 otherwise delete it in any case.
1925 FAIL_DELETE nonzero means delete it if a compilation step fails;
1926 otherwise delete it in any case. */
1927
d991c721 1928void
1d088dee 1929record_temp_file (const char *filename, int always_delete, int fail_delete)
ed1f651b 1930{
b3694847 1931 char *const name = xstrdup (filename);
ed1f651b
RS
1932
1933 if (always_delete)
1934 {
b3694847 1935 struct temp_file *temp;
ed1f651b
RS
1936 for (temp = always_delete_queue; temp; temp = temp->next)
1937 if (! strcmp (name, temp->name))
1938 goto already1;
e9a25f70 1939
5ed6ace5 1940 temp = XNEW (struct temp_file);
ed1f651b
RS
1941 temp->next = always_delete_queue;
1942 temp->name = name;
1943 always_delete_queue = temp;
e9a25f70 1944
ed1f651b
RS
1945 already1:;
1946 }
1947
1948 if (fail_delete)
1949 {
b3694847 1950 struct temp_file *temp;
ed1f651b
RS
1951 for (temp = failure_delete_queue; temp; temp = temp->next)
1952 if (! strcmp (name, temp->name))
1953 goto already2;
e9a25f70 1954
5ed6ace5 1955 temp = XNEW (struct temp_file);
ed1f651b
RS
1956 temp->next = failure_delete_queue;
1957 temp->name = name;
1958 failure_delete_queue = temp;
e9a25f70 1959
ed1f651b
RS
1960 already2:;
1961 }
1962}
1963
1964/* Delete all the temporary files whose names we previously recorded. */
1965
8fd58397
DR
1966#ifndef DELETE_IF_ORDINARY
1967#define DELETE_IF_ORDINARY(NAME,ST,VERBOSE_FLAG) \
1968do \
1969 { \
1970 if (stat (NAME, &ST) >= 0 && S_ISREG (ST.st_mode)) \
1971 if (unlink (NAME) < 0) \
1972 if (VERBOSE_FLAG) \
1973 perror_with_name (NAME); \
1974 } while (0)
1975#endif
1976
d5ea2ac4 1977static void
1d088dee 1978delete_if_ordinary (const char *name)
d5ea2ac4
RK
1979{
1980 struct stat st;
1981#ifdef DEBUG
1982 int i, c;
1983
1984 printf ("Delete %s? (y or n) ", name);
1985 fflush (stdout);
1986 i = getchar ();
1987 if (i != '\n')
e9a25f70
JL
1988 while ((c = getchar ()) != '\n' && c != EOF)
1989 ;
1990
d5ea2ac4
RK
1991 if (i == 'y' || i == 'Y')
1992#endif /* DEBUG */
8fd58397 1993 DELETE_IF_ORDINARY (name, st, verbose_flag);
d5ea2ac4
RK
1994}
1995
ed1f651b 1996static void
1d088dee 1997delete_temp_files (void)
ed1f651b 1998{
b3694847 1999 struct temp_file *temp;
ed1f651b
RS
2000
2001 for (temp = always_delete_queue; temp; temp = temp->next)
d5ea2ac4 2002 delete_if_ordinary (temp->name);
ed1f651b
RS
2003 always_delete_queue = 0;
2004}
2005
2006/* Delete all the files to be deleted on error. */
2007
2008static void
1d088dee 2009delete_failure_queue (void)
ed1f651b 2010{
b3694847 2011 struct temp_file *temp;
ed1f651b
RS
2012
2013 for (temp = failure_delete_queue; temp; temp = temp->next)
d5ea2ac4 2014 delete_if_ordinary (temp->name);
ed1f651b
RS
2015}
2016
2017static void
1d088dee 2018clear_failure_queue (void)
ed1f651b
RS
2019{
2020 failure_delete_queue = 0;
2021}
b3865ca9 2022\f
00dcee0c
AM
2023/* Call CALLBACK for each path in PATHS, breaking out early if CALLBACK
2024 returns non-NULL.
2025 If DO_MULTI is true iterate over the paths twice, first with multilib
2026 suffix then without, otherwise iterate over the paths once without
2027 adding a multilib suffix. When DO_MULTI is true, some attempt is made
2028 to avoid visiting the same path twice, but we could do better. For
2029 instance, /usr/lib/../lib is considered different from /usr/lib.
2030 At least EXTRA_SPACE chars past the end of the path passed to
2031 CALLBACK are available for use by the callback.
2032 CALLBACK_INFO allows extra parameters to be passed to CALLBACK.
2033
2034 Returns the value returned by CALLBACK. */
2035
2036static void *
2037for_each_path (const struct path_prefix *paths,
2038 bool do_multi,
2039 size_t extra_space,
2040 void *(*callback) (char *, void *),
2041 void *callback_info)
b3865ca9 2042{
00dcee0c
AM
2043 struct prefix_list *pl;
2044 const char *multi_dir = NULL;
2045 const char *multi_os_dir = NULL;
2046 const char *multi_suffix;
2047 const char *just_multi_suffix;
2048 char *path = NULL;
2049 void *ret = NULL;
2050 bool skip_multi_dir = false;
2051 bool skip_multi_os_dir = false;
2052
2053 multi_suffix = machine_suffix;
2054 just_multi_suffix = just_machine_suffix;
2055 if (do_multi && multilib_dir && strcmp (multilib_dir, ".") != 0)
b3865ca9 2056 {
00dcee0c
AM
2057 multi_dir = concat (multilib_dir, dir_separator_str, NULL);
2058 multi_suffix = concat (multi_suffix, multi_dir, NULL);
2059 just_multi_suffix = concat (just_multi_suffix, multi_dir, NULL);
2060 }
2061 if (do_multi && multilib_os_dir && strcmp (multilib_os_dir, ".") != 0)
2062 multi_os_dir = concat (multilib_os_dir, dir_separator_str, NULL);
b3865ca9 2063
00dcee0c
AM
2064 while (1)
2065 {
2066 size_t multi_dir_len = 0;
2067 size_t multi_os_dir_len = 0;
2068 size_t suffix_len;
2069 size_t just_suffix_len;
2070 size_t len;
2071
2072 if (multi_dir)
2073 multi_dir_len = strlen (multi_dir);
2074 if (multi_os_dir)
2075 multi_os_dir_len = strlen (multi_os_dir);
2076 suffix_len = strlen (multi_suffix);
2077 just_suffix_len = strlen (just_multi_suffix);
2078
2079 if (path == NULL)
b3865ca9 2080 {
00dcee0c
AM
2081 len = paths->max_len + extra_space + 1;
2082 if (suffix_len > multi_os_dir_len)
2083 len += suffix_len;
2084 else
2085 len += multi_os_dir_len;
5ed6ace5 2086 path = XNEWVEC (char, len);
b3865ca9
RS
2087 }
2088
00dcee0c 2089 for (pl = paths->plist; pl != 0; pl = pl->next)
ae04227b 2090 {
00dcee0c
AM
2091 len = strlen (pl->prefix);
2092 memcpy (path, pl->prefix, len);
9218435e 2093
00dcee0c
AM
2094 /* Look first in MACHINE/VERSION subdirectory. */
2095 if (!skip_multi_dir)
2096 {
2097 memcpy (path + len, multi_suffix, suffix_len + 1);
2098 ret = callback (path, callback_info);
2099 if (ret)
2100 break;
2101 }
2102
2103 /* Some paths are tried with just the machine (ie. target)
2104 subdir. This is used for finding as, ld, etc. */
2105 if (!skip_multi_dir
2106 && pl->require_machine_suffix == 2)
2107 {
2108 memcpy (path + len, just_multi_suffix, just_suffix_len + 1);
2109 ret = callback (path, callback_info);
2110 if (ret)
2111 break;
2112 }
2113
2114 /* Now try the base path. */
2115 if (!pl->require_machine_suffix
2116 && !(pl->os_multilib ? skip_multi_os_dir : skip_multi_dir))
2117 {
2118 const char *this_multi;
2119 size_t this_multi_len;
2120
2121 if (pl->os_multilib)
2122 {
2123 this_multi = multi_os_dir;
2124 this_multi_len = multi_os_dir_len;
2125 }
2126 else
2127 {
2128 this_multi = multi_dir;
2129 this_multi_len = multi_dir_len;
2130 }
2131
2132 if (this_multi_len)
2133 memcpy (path + len, this_multi, this_multi_len + 1);
2134 else
2135 path[len] = '\0';
2136
2137 ret = callback (path, callback_info);
2138 if (ret)
2139 break;
2140 }
ae04227b 2141 }
00dcee0c
AM
2142 if (pl)
2143 break;
ae04227b 2144
00dcee0c
AM
2145 if (multi_dir == NULL && multi_os_dir == NULL)
2146 break;
b3865ca9 2147
00dcee0c
AM
2148 /* Run through the paths again, this time without multilibs.
2149 Don't repeat any we have already seen. */
2150 if (multi_dir)
2151 {
b1d5455a 2152 free (CONST_CAST (char *, multi_dir));
00dcee0c 2153 multi_dir = NULL;
b1d5455a 2154 free (CONST_CAST (char *, multi_suffix));
00dcee0c 2155 multi_suffix = machine_suffix;
b1d5455a 2156 free (CONST_CAST (char *, just_multi_suffix));
00dcee0c 2157 just_multi_suffix = just_machine_suffix;
b3865ca9 2158 }
00dcee0c
AM
2159 else
2160 skip_multi_dir = true;
2161 if (multi_os_dir)
2162 {
b1d5455a 2163 free (CONST_CAST (char *, multi_os_dir));
00dcee0c
AM
2164 multi_os_dir = NULL;
2165 }
2166 else
2167 skip_multi_os_dir = true;
2168 }
2169
2170 if (multi_dir)
2171 {
b1d5455a
KG
2172 free (CONST_CAST (char *, multi_dir));
2173 free (CONST_CAST (char *, multi_suffix));
2174 free (CONST_CAST (char *, just_multi_suffix));
b3865ca9 2175 }
00dcee0c 2176 if (multi_os_dir)
b1d5455a 2177 free (CONST_CAST (char *, multi_os_dir));
00dcee0c
AM
2178 if (ret != path)
2179 free (path);
2180 return ret;
2181}
2182
2183/* Callback for build_search_list. Adds path to obstack being built. */
2184
2185struct add_to_obstack_info {
2186 struct obstack *ob;
2187 bool check_dir;
2188 bool first_time;
2189};
2190
2191static void *
2192add_to_obstack (char *path, void *data)
2193{
e1e4cdc4 2194 struct add_to_obstack_info *info = (struct add_to_obstack_info *) data;
00dcee0c
AM
2195
2196 if (info->check_dir && !is_directory (path, false))
2197 return NULL;
2198
2199 if (!info->first_time)
2200 obstack_1grow (info->ob, PATH_SEPARATOR);
2201
2202 obstack_grow (info->ob, path, strlen (path));
2203
2204 info->first_time = false;
2205 return NULL;
2206}
2207
47d33318
FXC
2208/* Add or change the value of an environment variable, outputting the
2209 change to standard error if in verbose mode. */
2210static void
e9c15f6e 2211xputenv (const char *string)
47d33318
FXC
2212{
2213 if (verbose_flag)
2dec80c7 2214 fnotice (stderr, "%s\n", string);
e9c15f6e 2215 putenv (CONST_CAST (char *, string));
47d33318
FXC
2216}
2217
00dcee0c
AM
2218/* Build a list of search directories from PATHS.
2219 PREFIX is a string to prepend to the list.
2220 If CHECK_DIR_P is true we ensure the directory exists.
2221 If DO_MULTI is true, multilib paths are output first, then
2222 non-multilib paths.
2223 This is used mostly by putenv_from_prefixes so we use `collect_obstack'.
2224 It is also used by the --print-search-dirs flag. */
2225
2226static char *
2227build_search_list (const struct path_prefix *paths, const char *prefix,
2228 bool check_dir, bool do_multi)
2229{
2230 struct add_to_obstack_info info;
2231
2232 info.ob = &collect_obstack;
2233 info.check_dir = check_dir;
2234 info.first_time = true;
2235
2236 obstack_grow (&collect_obstack, prefix, strlen (prefix));
2237 obstack_1grow (&collect_obstack, '=');
2238
2239 for_each_path (paths, do_multi, 0, add_to_obstack, &info);
e9a25f70 2240
3ae7de4e 2241 obstack_1grow (&collect_obstack, '\0');
7973fd2a 2242 return XOBFINISH (&collect_obstack, char *);
b3865ca9
RS
2243}
2244
0f41302f
MS
2245/* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables
2246 for collect. */
2628b9d3
DE
2247
2248static void
00dcee0c
AM
2249putenv_from_prefixes (const struct path_prefix *paths, const char *env_var,
2250 bool do_multi)
2628b9d3 2251{
47d33318 2252 xputenv (build_search_list (paths, env_var, true, do_multi));
2628b9d3 2253}
ed1f651b 2254\f
ca606201
ILT
2255/* Check whether NAME can be accessed in MODE. This is like access,
2256 except that it never considers directories to be executable. */
2257
2258static int
1d088dee 2259access_check (const char *name, int mode)
ca606201
ILT
2260{
2261 if (mode == X_OK)
2262 {
2263 struct stat st;
2264
2265 if (stat (name, &st) < 0
2266 || S_ISDIR (st.st_mode))
2267 return -1;
2268 }
2269
2270 return access (name, mode);
2271}
2272
00dcee0c
AM
2273/* Callback for find_a_file. Appends the file name to the directory
2274 path. If the resulting file exists in the right mode, return the
2275 full pathname to the file. */
2276
2277struct file_at_path_info {
2278 const char *name;
2279 const char *suffix;
2280 int name_len;
2281 int suffix_len;
2282 int mode;
2283};
2284
2285static void *
2286file_at_path (char *path, void *data)
2287{
e1e4cdc4 2288 struct file_at_path_info *info = (struct file_at_path_info *) data;
00dcee0c
AM
2289 size_t len = strlen (path);
2290
2291 memcpy (path + len, info->name, info->name_len);
2292 len += info->name_len;
2293
2294 /* Some systems have a suffix for executable files.
2295 So try appending that first. */
2296 if (info->suffix_len)
2297 {
2298 memcpy (path + len, info->suffix, info->suffix_len + 1);
2299 if (access_check (path, info->mode) == 0)
2300 return path;
2301 }
2302
2303 path[len] = '\0';
2304 if (access_check (path, info->mode) == 0)
2305 return path;
2306
2307 return NULL;
2308}
2309
ed1f651b 2310/* Search for NAME using the prefix list PREFIXES. MODE is passed to
00dcee0c
AM
2311 access to check permissions. If DO_MULTI is true, search multilib
2312 paths then non-multilib paths, otherwise do not search multilib paths.
0f41302f 2313 Return 0 if not found, otherwise return its name, allocated with malloc. */
ed1f651b
RS
2314
2315static char *
00dcee0c
AM
2316find_a_file (const struct path_prefix *pprefix, const char *name, int mode,
2317 bool do_multi)
ed1f651b 2318{
00dcee0c 2319 struct file_at_path_info info;
ed1f651b 2320
ab339d62 2321#ifdef DEFAULT_ASSEMBLER
c5c0b3d9 2322 if (! strcmp (name, "as") && access (DEFAULT_ASSEMBLER, mode) == 0)
ad85216e 2323 return xstrdup (DEFAULT_ASSEMBLER);
ab339d62
AO
2324#endif
2325
2326#ifdef DEFAULT_LINKER
ad85216e
KG
2327 if (! strcmp(name, "ld") && access (DEFAULT_LINKER, mode) == 0)
2328 return xstrdup (DEFAULT_LINKER);
ab339d62
AO
2329#endif
2330
ed1f651b
RS
2331 /* Determine the filename to execute (special case for absolute paths). */
2332
3dce1408 2333 if (IS_ABSOLUTE_PATH (name))
ed1f651b 2334 {
ab339d62 2335 if (access (name, mode) == 0)
00dcee0c 2336 return xstrdup (name);
460dcab4 2337
00dcee0c
AM
2338 return NULL;
2339 }
460dcab4 2340
00dcee0c
AM
2341 info.name = name;
2342 info.suffix = (mode & X_OK) != 0 ? HOST_EXECUTABLE_SUFFIX : "";
2343 info.name_len = strlen (info.name);
2344 info.suffix_len = strlen (info.suffix);
2345 info.mode = mode;
ed1f651b 2346
e1e4cdc4
KG
2347 return (char*) for_each_path (pprefix, do_multi,
2348 info.name_len + info.suffix_len,
2349 file_at_path, &info);
ed1f651b
RS
2350}
2351
922a4beb 2352/* Ranking of prefixes in the sort list. -B prefixes are put before
9218435e 2353 all others. */
922a4beb
AC
2354
2355enum path_prefix_priority
2356{
2357 PREFIX_PRIORITY_B_OPT,
2358 PREFIX_PRIORITY_LAST
2359};
2360
fbe5a4a6 2361/* Add an entry for PREFIX in PLIST. The PLIST is kept in ascending
922a4beb
AC
2362 order according to PRIORITY. Within each PRIORITY, new entries are
2363 appended.
ed1f651b
RS
2364
2365 If WARN is nonzero, we will warn if no file is found
2366 through this prefix. WARN should point to an int
ae04227b
CH
2367 which will be set to 1 if this entry is used.
2368
e9a25f70
JL
2369 COMPONENT is the value to be passed to update_path.
2370
ae04227b
CH
2371 REQUIRE_MACHINE_SUFFIX is 1 if this prefix can't be used without
2372 the complete value of machine_suffix.
2373 2 means try both machine_suffix and just_machine_suffix. */
ed1f651b
RS
2374
2375static void
1d088dee
AJ
2376add_prefix (struct path_prefix *pprefix, const char *prefix,
2377 const char *component, /* enum prefix_priority */ int priority,
1a5d37a1 2378 int require_machine_suffix, int os_multilib)
ed1f651b
RS
2379{
2380 struct prefix_list *pl, **prev;
2381 int len;
2382
922a4beb
AC
2383 for (prev = &pprefix->plist;
2384 (*prev) != NULL && (*prev)->priority <= priority;
2385 prev = &(*prev)->next)
2386 ;
ed1f651b 2387
f9da5064 2388 /* Keep track of the longest prefix. */
ed1f651b 2389
e9a25f70 2390 prefix = update_path (prefix, component);
ed1f651b
RS
2391 len = strlen (prefix);
2392 if (len > pprefix->max_len)
2393 pprefix->max_len = len;
2394
5ed6ace5 2395 pl = XNEW (struct prefix_list);
51c04256 2396 pl->prefix = prefix;
ed1f651b 2397 pl->require_machine_suffix = require_machine_suffix;
922a4beb 2398 pl->priority = priority;
5bbcd587 2399 pl->os_multilib = os_multilib;
ed1f651b 2400
f9da5064 2401 /* Insert after PREV. */
922a4beb
AC
2402 pl->next = (*prev);
2403 (*prev) = pl;
ed1f651b 2404}
4977bab6
ZW
2405
2406/* Same as add_prefix, but prepending target_system_root to prefix. */
f4c0a303 2407/* The target_system_root prefix has been relocated by gcc_exec_prefix. */
4977bab6 2408static void
1d088dee
AJ
2409add_sysrooted_prefix (struct path_prefix *pprefix, const char *prefix,
2410 const char *component,
2411 /* enum prefix_priority */ int priority,
1a5d37a1 2412 int require_machine_suffix, int os_multilib)
4977bab6 2413{
3dce1408 2414 if (!IS_ABSOLUTE_PATH (prefix))
bdc6b402 2415 fatal_error ("system path %qs is not absolute", prefix);
4977bab6
ZW
2416
2417 if (target_system_root)
2418 {
e7f13528
GP
2419 if (target_sysroot_suffix)
2420 prefix = concat (target_sysroot_suffix, prefix, NULL);
4977bab6 2421 prefix = concat (target_system_root, prefix, NULL);
e7f13528 2422
4977bab6
ZW
2423 /* We have to override this because GCC's notion of sysroot
2424 moves along with GCC. */
2425 component = "GCC";
2426 }
2427
2428 add_prefix (pprefix, prefix, component, priority,
1a5d37a1 2429 require_machine_suffix, os_multilib);
4977bab6 2430}
ed1f651b
RS
2431\f
2432/* Execute the command specified by the arguments on the current line of spec.
2433 When using pipes, this includes several piped-together commands
2434 with `|' between them.
2435
2436 Return 0 if successful, -1 if failed. */
2437
2438static int
1d088dee 2439execute (void)
ed1f651b
RS
2440{
2441 int i;
2442 int n_commands; /* # of command. */
2443 char *string;
054e88a8 2444 struct pex_obj *pex;
ed1f651b 2445 struct command
d25a45d4
KH
2446 {
2447 const char *prog; /* program name. */
2448 const char **argv; /* vector of args. */
d25a45d4 2449 };
5b634ee0 2450 const char *arg;
ed1f651b
RS
2451
2452 struct command *commands; /* each command buffer with above info. */
2453
3b5edfee 2454 gcc_assert (!processing_spec_function);
f3226a90 2455
fe7df9c4
SP
2456 if (wrapper_string)
2457 {
5b634ee0
JM
2458 string = find_a_file (&exec_prefixes,
2459 VEC_index (const_char_p, argbuf, 0), X_OK, false);
2460 if (string)
2461 VEC_replace (const_char_p, argbuf, 0, string);
fe7df9c4
SP
2462 insert_wrapper (wrapper_string);
2463 }
2464
ed1f651b 2465 /* Count # of piped commands. */
5b634ee0
JM
2466 for (n_commands = 1, i = 0; VEC_iterate (const_char_p, argbuf, i, arg); i++)
2467 if (strcmp (arg, "|") == 0)
ed1f651b
RS
2468 n_commands++;
2469
2470 /* Get storage for each command. */
e1e4cdc4 2471 commands = (struct command *) alloca (n_commands * sizeof (struct command));
ed1f651b
RS
2472
2473 /* Split argbuf into its separate piped processes,
2474 and record info about each one.
2475 Also search for the programs that are to be run. */
2476
5b634ee0
JM
2477 VEC_safe_push (const_char_p, heap, argbuf, 0);
2478
2479 commands[0].prog = VEC_index (const_char_p, argbuf, 0); /* first command. */
2480 commands[0].argv = VEC_address (const_char_p, argbuf);
b8698a0f 2481
fe7df9c4
SP
2482 if (!wrapper_string)
2483 {
2484 string = find_a_file (&exec_prefixes, commands[0].prog, X_OK, false);
2485 commands[0].argv[0] = (string) ? string : commands[0].argv[0];
2486 }
ed1f651b 2487
5b634ee0
JM
2488 for (n_commands = 1, i = 0; VEC_iterate (const_char_p, argbuf, i, arg); i++)
2489 if (arg && strcmp (arg, "|") == 0)
ed1f651b 2490 { /* each command. */
6405c0ec 2491#if defined (__MSDOS__) || defined (OS2) || defined (VMS)
2dec80c7 2492 fatal_error ("-pipe not supported");
ed1f651b 2493#endif
5b634ee0
JM
2494 VEC_replace (const_char_p, argbuf, i, 0); /* Termination of
2495 command args. */
2496 commands[n_commands].prog = VEC_index (const_char_p, argbuf, i + 1);
2497 commands[n_commands].argv
2498 = &(VEC_address (const_char_p, argbuf))[i + 1];
5bbcd587 2499 string = find_a_file (&exec_prefixes, commands[n_commands].prog,
00dcee0c 2500 X_OK, false);
ed1f651b
RS
2501 if (string)
2502 commands[n_commands].argv[0] = string;
2503 n_commands++;
2504 }
2505
ed1f651b
RS
2506 /* If -v, print what we are about to do, and maybe query. */
2507
b3865ca9 2508 if (verbose_flag)
ed1f651b 2509 {
b8468bc7
NC
2510 /* For help listings, put a blank line between sub-processes. */
2511 if (print_help_list)
2512 fputc ('\n', stderr);
9218435e 2513
ed1f651b 2514 /* Print each piped command as a separate line. */
d25a45d4 2515 for (i = 0; i < n_commands; i++)
ed1f651b 2516 {
37620334 2517 const char *const *j;
ed1f651b 2518
589005ff
KH
2519 if (verbose_only_flag)
2520 {
99f78cdd
IR
2521 for (j = commands[i].argv; *j; j++)
2522 {
88f92c0f 2523 const char *p;
99f78cdd 2524 for (p = *j; *p; ++p)
09b201fc
JJ
2525 if (!ISALNUM ((unsigned char) *p)
2526 && *p != '_' && *p != '/' && *p != '-' && *p != '.')
2527 break;
2528 if (*p || !*j)
99f78cdd 2529 {
09b201fc
JJ
2530 fprintf (stderr, " \"");
2531 for (p = *j; *p; ++p)
2532 {
2533 if (*p == '"' || *p == '\\' || *p == '$')
2534 fputc ('\\', stderr);
2535 fputc (*p, stderr);
2536 }
2537 fputc ('"', stderr);
99f78cdd 2538 }
09b201fc
JJ
2539 else
2540 fprintf (stderr, " %s", *j);
99f78cdd 2541 }
589005ff
KH
2542 }
2543 else
99f78cdd
IR
2544 for (j = commands[i].argv; *j; j++)
2545 fprintf (stderr, " %s", *j);
ed1f651b
RS
2546
2547 /* Print a pipe symbol after all but the last command. */
2548 if (i + 1 != n_commands)
2549 fprintf (stderr, " |");
2550 fprintf (stderr, "\n");
2551 }
2552 fflush (stderr);
99f78cdd 2553 if (verbose_only_flag != 0)
6a40fb21
AP
2554 {
2555 /* verbose_only_flag should act as if the spec was
2556 executed, so increment execution_count before
ba228239 2557 returning. This prevents spurious warnings about
6a40fb21
AP
2558 unused linker input files, etc. */
2559 execution_count++;
2560 return 0;
2561 }
ed1f651b 2562#ifdef DEBUG
2dec80c7 2563 fnotice (stderr, "\nGo ahead? (y or n) ");
ed1f651b
RS
2564 fflush (stderr);
2565 i = getchar ();
2566 if (i != '\n')
e9a25f70
JL
2567 while (getchar () != '\n')
2568 ;
2569
ed1f651b
RS
2570 if (i != 'y' && i != 'Y')
2571 return 0;
2572#endif /* DEBUG */
2573 }
2574
414d23ae 2575#ifdef ENABLE_VALGRIND_CHECKING
fbe5a4a6 2576 /* Run the each command through valgrind. To simplify prepending the
414d23ae
HPN
2577 path to valgrind and the option "-q" (for quiet operation unless
2578 something triggers), we allocate a separate argv array. */
2579
2580 for (i = 0; i < n_commands; i++)
2581 {
2582 const char **argv;
2583 int argc;
2584 int j;
2585
2586 for (argc = 0; commands[i].argv[argc] != NULL; argc++)
2587 ;
2588
63ab5b8c 2589 argv = XALLOCAVEC (const char *, argc + 3);
414d23ae
HPN
2590
2591 argv[0] = VALGRIND_PATH;
2592 argv[1] = "-q";
2593 for (j = 2; j < argc + 2; j++)
2594 argv[j] = commands[i].argv[j - 2];
2595 argv[j] = NULL;
2596
2597 commands[i].argv = argv;
2598 commands[i].prog = argv[0];
2599 }
2600#endif
2601
ed1f651b
RS
2602 /* Run each piped subprocess. */
2603
bdde878c
AO
2604 pex = pex_init (PEX_USE_PIPES | ((report_times || report_times_to_file)
2605 ? PEX_RECORD_TIMES : 0),
6afbc885 2606 progname, temp_filename);
054e88a8 2607 if (pex == NULL)
bdc6b402 2608 fatal_error ("pex_init failed: %m");
054e88a8 2609
ed1f651b
RS
2610 for (i = 0; i < n_commands; i++)
2611 {
054e88a8
ILT
2612 const char *errmsg;
2613 int err;
fbd40359 2614 const char *string = commands[i].argv[0];
ed1f651b 2615
054e88a8
ILT
2616 errmsg = pex_run (pex,
2617 ((i + 1 == n_commands ? PEX_LAST : 0)
2618 | (string == commands[i].prog ? PEX_SEARCH : 0)),
b1d5455a 2619 string, CONST_CAST (char **, commands[i].argv),
054e88a8
ILT
2620 NULL, NULL, &err);
2621 if (errmsg != NULL)
2622 {
2623 if (err == 0)
2dec80c7 2624 fatal_error (errmsg);
054e88a8
ILT
2625 else
2626 {
2627 errno = err;
2628 pfatal_with_name (errmsg);
2629 }
2630 }
ed1f651b
RS
2631
2632 if (string != commands[i].prog)
b1d5455a 2633 free (CONST_CAST (char *, string));
ed1f651b
RS
2634 }
2635
2636 execution_count++;
2637
054e88a8 2638 /* Wait for all the subprocesses to finish. */
ed1f651b
RS
2639
2640 {
054e88a8
ILT
2641 int *statuses;
2642 struct pex_time *times = NULL;
ed1f651b
RS
2643 int ret_code = 0;
2644
e1e4cdc4 2645 statuses = (int *) alloca (n_commands * sizeof (int));
054e88a8 2646 if (!pex_get_status (pex, n_commands, statuses))
bdc6b402 2647 fatal_error ("failed to get exit status: %m");
054e88a8 2648
bdde878c 2649 if (report_times || report_times_to_file)
ed1f651b 2650 {
e1e4cdc4 2651 times = (struct pex_time *) alloca (n_commands * sizeof (struct pex_time));
054e88a8 2652 if (!pex_get_times (pex, n_commands, times))
bdc6b402 2653 fatal_error ("failed to get process times: %m");
054e88a8 2654 }
ed1f651b 2655
054e88a8 2656 pex_free (pex);
ed1f651b 2657
054e88a8
ILT
2658 for (i = 0; i < n_commands; ++i)
2659 {
2660 int status = statuses[i];
03c41c05 2661
054e88a8
ILT
2662 if (WIFSIGNALED (status))
2663 {
c334349b 2664#ifdef SIGPIPE
054e88a8
ILT
2665 /* SIGPIPE is a special case. It happens in -pipe mode
2666 when the compiler dies before the preprocessor is done,
2667 or the assembler dies before the compiler is done.
2668 There's generally been an error already, and this is
2669 just fallout. So don't generate another error unless
2670 we would otherwise have succeeded. */
2671 if (WTERMSIG (status) == SIGPIPE
2672 && (signal_count || greatest_status >= MIN_FATAL_STATUS))
eeac616e
VR
2673 {
2674 signal_count++;
2675 ret_code = -1;
2676 }
054e88a8
ILT
2677 else
2678#endif
6afbc885
JM
2679 internal_error ("%s (program %s)",
2680 strsignal (WTERMSIG (status)), commands[i].prog);
054e88a8
ILT
2681 }
2682 else if (WIFEXITED (status)
2683 && WEXITSTATUS (status) >= MIN_FATAL_STATUS)
2684 {
2685 if (WEXITSTATUS (status) > greatest_status)
2686 greatest_status = WEXITSTATUS (status);
2687 ret_code = -1;
2688 }
2689
bdde878c 2690 if (report_times || report_times_to_file)
054e88a8
ILT
2691 {
2692 struct pex_time *pt = &times[i];
2693 double ut, st;
2694
2695 ut = ((double) pt->user_seconds
2696 + (double) pt->user_microseconds / 1.0e6);
2697 st = ((double) pt->system_seconds
2698 + (double) pt->system_microseconds / 1.0e6);
2699
2700 if (ut + st != 0)
bdde878c
AO
2701 {
2702 if (report_times)
2dec80c7
JM
2703 fnotice (stderr, "# %s %.2f %.2f\n",
2704 commands[i].prog, ut, st);
bdde878c
AO
2705
2706 if (report_times_to_file)
2707 {
2708 int c = 0;
2709 const char *const *j;
2710
2711 fprintf (report_times_to_file, "%g %g", ut, st);
2712
2713 for (j = &commands[i].prog; *j; j = &commands[i].argv[++c])
2714 {
2715 const char *p;
2716 for (p = *j; *p; ++p)
2717 if (*p == '"' || *p == '\\' || *p == '$'
2718 || ISSPACE (*p))
2719 break;
2720
2721 if (*p)
2722 {
2723 fprintf (report_times_to_file, " \"");
2724 for (p = *j; *p; ++p)
2725 {
2726 if (*p == '"' || *p == '\\' || *p == '$')
2727 fputc ('\\', report_times_to_file);
2728 fputc (*p, report_times_to_file);
2729 }
2730 fputc ('"', report_times_to_file);
2731 }
2732 else
2733 fprintf (report_times_to_file, " %s", *j);
2734 }
2735
2736 fputc ('\n', report_times_to_file);
2737 }
2738 }
054e88a8 2739 }
ed1f651b 2740 }
054e88a8 2741
ed1f651b
RS
2742 return ret_code;
2743 }
2744}
2745\f
2746/* Find all the switches given to us
2747 and make a vector describing them.
2748 The elements of the vector are strings, one per switch given.
2749 If a switch uses following arguments, then the `part1' field
2750 is the switch itself and the `args' field
2751 is a null-terminated vector containing the following arguments.
3371362c
L
2752 Bits in the `live_cond' field are:
2753 SWITCH_LIVE to indicate this switch is true in a conditional spec.
2754 SWITCH_FALSE to indicate this switch is overridden by a later switch.
2755 SWITCH_IGNORE to indicate this switch should be ignored (used in %<S).
94b90527
JJ
2756 SWITCH_IGNORE_PERMANENTLY to indicate this switch should be ignored
2757 in all do_spec calls afterwards. Used for %<S from self specs.
ab87f8c8 2758 The `validated' field is nonzero if any spec has looked at this switch;
ed1f651b
RS
2759 if it remains zero at the end of the run, it must be meaningless. */
2760
96f5b137
L
2761#define SWITCH_LIVE (1 << 0)
2762#define SWITCH_FALSE (1 << 1)
2763#define SWITCH_IGNORE (1 << 2)
2764#define SWITCH_IGNORE_PERMANENTLY (1 << 3)
2765#define SWITCH_KEEP_FOR_GCC (1 << 4)
8097c429 2766
ed1f651b
RS
2767struct switchstr
2768{
878f32c3 2769 const char *part1;
fbd40359 2770 const char **args;
3371362c 2771 unsigned int live_cond;
196a37f4
NB
2772 unsigned char validated;
2773 unsigned char ordering;
ed1f651b
RS
2774};
2775
2776static struct switchstr *switches;
2777
2778static int n_switches;
2779
922e1882
JM
2780static int n_switches_alloc;
2781
2153915d
AO
2782/* Set to zero if -fcompare-debug is disabled, positive if it's
2783 enabled and we're running the first compilation, negative if it's
2784 enabled and we're running the second compilation. For most of the
2785 time, it's in the range -1..1, but it can be temporarily set to 2
2786 or 3 to indicate that the -fcompare-debug flags didn't come from
2787 the command-line, but rather from the GCC_COMPARE_DEBUG environment
2788 variable, until a synthesized -fcompare-debug flag is added to the
2789 command line. */
2790int compare_debug;
2791
2792/* Set to nonzero if we've seen the -fcompare-debug-second flag. */
2793int compare_debug_second;
2794
2795/* Set to the flags that should be passed to the second compilation in
2796 a -fcompare-debug compilation. */
2797const char *compare_debug_opt;
2798
2799static struct switchstr *switches_debug_check[2];
2800
2801static int n_switches_debug_check[2];
2802
efe5e5a0
JM
2803static int n_switches_alloc_debug_check[2];
2804
2153915d
AO
2805static char *debug_check_temp_file[2];
2806
817a8255
EC
2807/* Language is one of three things:
2808
2809 1) The name of a real programming language.
2810 2) NULL, indicating that no one has figured out
2811 what it is yet.
2812 3) '*', indicating that the file should be passed
2813 to the linker. */
ed1f651b
RS
2814struct infile
2815{
878f32c3
KG
2816 const char *name;
2817 const char *language;
0855eab7
CT
2818 struct compiler *incompiler;
2819 bool compiled;
2820 bool preprocessed;
ed1f651b
RS
2821};
2822
2823/* Also a vector of input files specified. */
2824
2825static struct infile *infiles;
2826
3a5a9edc 2827int n_infiles;
ed1f651b 2828
922e1882
JM
2829static int n_infiles_alloc;
2830
d1bd0ded
GK
2831/* True if multiple input files are being compiled to a single
2832 assembly file. */
2833
2834static bool combine_inputs;
2835
08dc830e 2836/* This counts the number of libraries added by lang_specific_driver, so that
a2a05b0a
JW
2837 we can tell if there were any user supplied any files or libraries. */
2838
2839static int added_libraries;
2840
ed1f651b
RS
2841/* And a vector of corresponding output files is made up later. */
2842
3a5a9edc 2843const char **outfiles;
853e0b2d 2844\f
45936a85 2845#if defined(HAVE_TARGET_OBJECT_SUFFIX) || defined(HAVE_TARGET_EXECUTABLE_SUFFIX)
853e0b2d
RK
2846
2847/* Convert NAME to a new name if it is the standard suffix. DO_EXE
a9657ce8
DR
2848 is true if we should look for an executable suffix. DO_OBJ
2849 is true if we should look for an object suffix. */
853e0b2d 2850
40cdfca6 2851static const char *
1d088dee
AJ
2852convert_filename (const char *name, int do_exe ATTRIBUTE_UNUSED,
2853 int do_obj ATTRIBUTE_UNUSED)
853e0b2d 2854{
40cdfca6 2855#if defined(HAVE_TARGET_EXECUTABLE_SUFFIX)
853e0b2d 2856 int i;
40cdfca6 2857#endif
87e690e2
MK
2858 int len;
2859
2860 if (name == NULL)
2861 return NULL;
9218435e 2862
87e690e2 2863 len = strlen (name);
853e0b2d 2864
45936a85
DD
2865#ifdef HAVE_TARGET_OBJECT_SUFFIX
2866 /* Convert x.o to x.obj if TARGET_OBJECT_SUFFIX is ".obj". */
a9657ce8 2867 if (do_obj && len > 2
853e0b2d
RK
2868 && name[len - 2] == '.'
2869 && name[len - 1] == 'o')
2870 {
bdc5ed93 2871 obstack_grow (&obstack, name, len - 2);
45936a85 2872 obstack_grow0 (&obstack, TARGET_OBJECT_SUFFIX, strlen (TARGET_OBJECT_SUFFIX));
7973fd2a 2873 name = XOBFINISH (&obstack, const char *);
853e0b2d
RK
2874 }
2875#endif
2876
45936a85 2877#if defined(HAVE_TARGET_EXECUTABLE_SUFFIX)
853e0b2d
RK
2878 /* If there is no filetype, make it the executable suffix (which includes
2879 the "."). But don't get confused if we have just "-o". */
45936a85 2880 if (! do_exe || TARGET_EXECUTABLE_SUFFIX[0] == 0 || (len == 2 && name[0] == '-'))
853e0b2d
RK
2881 return name;
2882
e0040a8e 2883 for (i = len - 1; i >= 0; i--)
509781a4 2884 if (IS_DIR_SEPARATOR (name[i]))
e0040a8e
RK
2885 break;
2886
2887 for (i++; i < len; i++)
853e0b2d
RK
2888 if (name[i] == '.')
2889 return name;
2890
2891 obstack_grow (&obstack, name, len);
5d9669fd
RK
2892 obstack_grow0 (&obstack, TARGET_EXECUTABLE_SUFFIX,
2893 strlen (TARGET_EXECUTABLE_SUFFIX));
7973fd2a 2894 name = XOBFINISH (&obstack, const char *);
853e0b2d
RK
2895#endif
2896
2897 return name;
2898}
2899#endif
b8468bc7
NC
2900\f
2901/* Display the command line switches accepted by gcc. */
2902static void
1d088dee 2903display_help (void)
b8468bc7 2904{
6afbc885 2905 printf (_("Usage: %s [options] file...\n"), progname);
5e4adfba 2906 fputs (_("Options:\n"), stdout);
b8468bc7 2907
5e4adfba
PT
2908 fputs (_(" -pass-exit-codes Exit with highest error code from a phase\n"), stdout);
2909 fputs (_(" --help Display this information\n"), stdout);
91606ce2 2910 fputs (_(" --target-help Display target specific command line options\n"), stdout);
0631b69f 2911 fputs (_(" --help={target|optimizers|warnings|params|[^]{joined|separate|undocumented}}[,...]\n"), stdout);
c662432e 2912 fputs (_(" Display specific types of command line options\n"), stdout);
b8468bc7 2913 if (! verbose_flag)
5e4adfba 2914 fputs (_(" (Use '-v --help' to display command line options of sub-processes)\n"), stdout);
1156c176 2915 fputs (_(" --version Display compiler version information\n"), stdout);
5e4adfba
PT
2916 fputs (_(" -dumpspecs Display all of the built in spec strings\n"), stdout);
2917 fputs (_(" -dumpversion Display the version of the compiler\n"), stdout);
2918 fputs (_(" -dumpmachine Display the compiler's target processor\n"), stdout);
2919 fputs (_(" -print-search-dirs Display the directories in the compiler's search path\n"), stdout);
2920 fputs (_(" -print-libgcc-file-name Display the name of the compiler's companion library\n"), stdout);
2921 fputs (_(" -print-file-name=<lib> Display the full path to library <lib>\n"), stdout);
2922 fputs (_(" -print-prog-name=<prog> Display the full path to compiler component <prog>\n"), stdout);
2923 fputs (_(" -print-multi-directory Display the root directory for versions of libgcc\n"), stdout);
2924 fputs (_("\
2925 -print-multi-lib Display the mapping between command line options and\n\
2926 multiple library search directories\n"), stdout);
5bbcd587 2927 fputs (_(" -print-multi-os-directory Display the relative path to OS libraries\n"), stdout);
3def1397 2928 fputs (_(" -print-sysroot Display the target libraries directory\n"), stdout);
14da6073 2929 fputs (_(" -print-sysroot-headers-suffix Display the sysroot suffix used to find headers\n"), stdout);
5e4adfba
PT
2930 fputs (_(" -Wa,<options> Pass comma-separated <options> on to the assembler\n"), stdout);
2931 fputs (_(" -Wp,<options> Pass comma-separated <options> on to the preprocessor\n"), stdout);
2932 fputs (_(" -Wl,<options> Pass comma-separated <options> on to the linker\n"), stdout);
4977bab6
ZW
2933 fputs (_(" -Xassembler <arg> Pass <arg> on to the assembler\n"), stdout);
2934 fputs (_(" -Xpreprocessor <arg> Pass <arg> on to the preprocessor\n"), stdout);
5e4adfba
PT
2935 fputs (_(" -Xlinker <arg> Pass <arg> on to the linker\n"), stdout);
2936 fputs (_(" -save-temps Do not delete intermediate files\n"), stdout);
14fdc613 2937 fputs (_(" -save-temps=<arg> Do not delete intermediate files\n"), stdout);
ba0c638e
SB
2938 fputs (_("\
2939 -no-canonical-prefixes Do not canonicalize paths when building relative\n\
2940 prefixes to other gcc components\n"), stdout);
5e4adfba
PT
2941 fputs (_(" -pipe Use pipes rather than intermediate files\n"), stdout);
2942 fputs (_(" -time Time the execution of each subprocess\n"), stdout);
b0287a90 2943 fputs (_(" -specs=<file> Override built-in specs with the contents of <file>\n"), stdout);
5e4adfba 2944 fputs (_(" -std=<standard> Assume that the input sources are for <standard>\n"), stdout);
160633c6
MM
2945 fputs (_("\
2946 --sysroot=<directory> Use <directory> as the root directory for headers\n\
b3cccd58 2947 and libraries\n"), stdout);
5e4adfba 2948 fputs (_(" -B <directory> Add <directory> to the compiler's search paths\n"), stdout);
5e4adfba 2949 fputs (_(" -v Display the programs invoked by the compiler\n"), stdout);
99f78cdd 2950 fputs (_(" -### Like -v but options quoted and commands not executed\n"), stdout);
5e4adfba
PT
2951 fputs (_(" -E Preprocess only; do not compile, assemble or link\n"), stdout);
2952 fputs (_(" -S Compile only; do not assemble or link\n"), stdout);
2953 fputs (_(" -c Compile and assemble, but do not link\n"), stdout);
2954 fputs (_(" -o <file> Place the output into <file>\n"), stdout);
2955 fputs (_("\
2956 -x <language> Specify the language of the following input files\n\
8e854b76 2957 Permissible languages include: c c++ assembler none\n\
1737c953 2958 'none' means revert to the default behavior of\n\
5e4adfba
PT
2959 guessing the language based on the file's extension\n\
2960"), stdout);
2961
0c386769 2962 printf (_("\
d991c721
BK
2963\nOptions starting with -g, -f, -m, -O, -W, or --param are automatically\n\
2964 passed on to the various sub-processes invoked by %s. In order to pass\n\
2965 other options on to these processes the -W<letter> options must be used.\n\
6afbc885 2966"), progname);
b8468bc7
NC
2967
2968 /* The rest of the options are displayed by invocations of the various
2969 sub-processes. */
2970}
2971
9218435e 2972static void
1d088dee 2973add_preprocessor_option (const char *option, int len)
9218435e 2974{
69d540bd
NF
2975 VEC_safe_push (char_p, heap, preprocessor_options,
2976 save_string (option, len));
40f943dd 2977}
9218435e
KH
2978
2979static void
1d088dee 2980add_assembler_option (const char *option, int len)
878f32c3 2981{
69d540bd 2982 VEC_safe_push (char_p, heap, assembler_options, save_string (option, len));
b8468bc7 2983}
9218435e
KH
2984
2985static void
1d088dee 2986add_linker_option (const char *option, int len)
878f32c3 2987{
69d540bd 2988 VEC_safe_push (char_p, heap, linker_options, save_string (option, len));
40f943dd 2989}
853e0b2d 2990\f
922e1882
JM
2991/* Allocate space for an input file in infiles. */
2992
2993static void
2994alloc_infile (void)
2995{
2996 if (n_infiles_alloc == 0)
2997 {
2998 n_infiles_alloc = 16;
2999 infiles = XNEWVEC (struct infile, n_infiles_alloc);
3000 }
3001 else if (n_infiles_alloc == n_infiles)
3002 {
3003 n_infiles_alloc *= 2;
3004 infiles = XRESIZEVEC (struct infile, infiles, n_infiles_alloc);
3005 }
3006}
3007
3008/* Store an input file with the given NAME and LANGUAGE in
3009 infiles. */
3010
3011static void
3012add_infile (const char *name, const char *language)
3013{
3014 alloc_infile ();
3015 infiles[n_infiles].name = name;
3016 infiles[n_infiles++].language = language;
3017}
3018
3019/* Allocate space for a switch in switches. */
3020
3021static void
3022alloc_switch (void)
3023{
3024 if (n_switches_alloc == 0)
3025 {
3026 n_switches_alloc = 16;
3027 switches = XNEWVEC (struct switchstr, n_switches_alloc);
3028 }
3029 else if (n_switches_alloc == n_switches)
3030 {
3031 n_switches_alloc *= 2;
3032 switches = XRESIZEVEC (struct switchstr, switches, n_switches_alloc);
3033 }
3034}
3035
603349bf
JM
3036/* Save an option OPT with N_ARGS arguments in array ARGS, marking it
3037 as validated if VALIDATED. */
3038
3039static void
3040save_switch (const char *opt, size_t n_args, const char *const *args,
3041 bool validated)
3042{
3043 alloc_switch ();
3044 switches[n_switches].part1 = opt + 1;
3045 if (n_args == 0)
3046 switches[n_switches].args = 0;
3047 else
3048 {
3049 switches[n_switches].args = XNEWVEC (const char *, n_args + 1);
3050 memcpy (switches[n_switches].args, args, n_args * sizeof (const char *));
3051 switches[n_switches].args[n_args] = NULL;
3052 }
3053
3054 switches[n_switches].live_cond = 0;
3055 switches[n_switches].validated = validated;
3056 switches[n_switches].ordering = 0;
3057 n_switches++;
3058}
3059
3060/* Handle an option DECODED that is unknown to the option-processing
3061 machinery, but may be known to specs. */
3062
3063static bool
3064driver_unknown_option_callback (const struct cl_decoded_option *decoded)
3065{
3066 save_switch (decoded->canonical_option[0],
3067 decoded->canonical_option_num_elements - 1,
3068 &decoded->canonical_option[1], false);
3069
3070 return false;
3071}
3072
3073/* Handle an option DECODED that is not marked as CL_DRIVER.
3074 LANG_MASK will always be CL_DRIVER. */
3075
3076static void
3077driver_wrong_lang_callback (const struct cl_decoded_option *decoded,
3078 unsigned int lang_mask ATTRIBUTE_UNUSED)
3079{
3080 /* At this point, non-driver options are accepted (and expected to
3081 be passed down by specs) unless marked to be rejected by the
3082 driver. Options to be rejected by the driver but accepted by the
3083 compilers proper are treated just like completely unknown
3084 options. */
3085 const struct cl_option *option = &cl_options[decoded->opt_index];
3086
3087 if (option->flags & CL_REJECT_DRIVER)
3088 error ("unrecognized command line option %qs",
3089 decoded->orig_option_with_args_text);
3090 else
3091 driver_unknown_option_callback (decoded);
3092}
3093
3094/* Note that an option (index OPT_INDEX, argument ARG, value VALUE)
3095 has been successfully handled with a handler for mask MASK. */
3096
3097static void
3098driver_post_handling_callback (const struct cl_decoded_option *decoded ATTRIBUTE_UNUSED,
3099 unsigned int mask ATTRIBUTE_UNUSED)
3100{
3101 /* Nothing to do here. */
3102}
3103
3104static const char *spec_lang = 0;
3105static int last_language_n_infiles;
3106
3107/* Handle a driver option; arguments and return value as for
3108 handle_option. */
3109
3110static bool
46625112 3111driver_handle_option (struct gcc_options *opts,
d4d24ba4 3112 struct gcc_options *opts_set,
46625112 3113 const struct cl_decoded_option *decoded,
603349bf 3114 unsigned int lang_mask ATTRIBUTE_UNUSED, int kind,
a4d8c676 3115 location_t loc,
d5478783
JM
3116 const struct cl_option_handlers *handlers ATTRIBUTE_UNUSED,
3117 diagnostic_context *dc)
603349bf
JM
3118{
3119 size_t opt_index = decoded->opt_index;
3120 const char *arg = decoded->arg;
3121 const char *compare_debug_replacement_opt;
3122 int value = decoded->value;
3123 bool validated = false;
3124 bool do_save = true;
3125
46625112 3126 gcc_assert (opts == &global_options);
d4d24ba4 3127 gcc_assert (opts_set == &global_options_set);
603349bf 3128 gcc_assert (kind == DK_UNSPECIFIED);
a4d8c676 3129 gcc_assert (loc == UNKNOWN_LOCATION);
d5478783 3130 gcc_assert (dc == global_dc);
603349bf
JM
3131
3132 switch (opt_index)
3133 {
3134 case OPT_dumpspecs:
3135 {
3136 struct spec_list *sl;
3137 init_spec ();
3138 for (sl = specs; sl; sl = sl->next)
3139 printf ("*%s:\n%s\n\n", sl->name, *(sl->ptr_spec));
3140 if (link_command_spec)
3141 printf ("*link_command:\n%s\n\n", link_command_spec);
3142 exit (0);
3143 }
3144
3145 case OPT_dumpversion:
3146 printf ("%s\n", spec_version);
3147 exit (0);
3148
3149 case OPT_dumpmachine:
3150 printf ("%s\n", spec_machine);
3151 exit (0);
3152
e200444e 3153 case OPT__version:
603349bf
JM
3154 print_version = 1;
3155
3156 /* CPP driver cannot obtain switch from cc1_options. */
3157 if (is_cpp_driver)
3158 add_preprocessor_option ("--version", strlen ("--version"));
3159 add_assembler_option ("--version", strlen ("--version"));
3160 add_linker_option ("--version", strlen ("--version"));
3161 break;
3162
e200444e 3163 case OPT__help:
603349bf
JM
3164 print_help_list = 1;
3165
3166 /* CPP driver cannot obtain switch from cc1_options. */
3167 if (is_cpp_driver)
3168 add_preprocessor_option ("--help", 6);
3169 add_assembler_option ("--help", 6);
3170 add_linker_option ("--help", 6);
3171 break;
3172
e200444e 3173 case OPT__help_:
603349bf
JM
3174 print_subprocess_help = 2;
3175 break;
3176
e200444e 3177 case OPT__target_help:
603349bf
JM
3178 print_subprocess_help = 1;
3179
3180 /* CPP driver cannot obtain switch from cc1_options. */
3181 if (is_cpp_driver)
3182 add_preprocessor_option ("--target-help", 13);
3183 add_assembler_option ("--target-help", 13);
3184 add_linker_option ("--target-help", 13);
3185 break;
3186
3187 case OPT_pass_exit_codes:
3188 case OPT_print_search_dirs:
3189 case OPT_print_file_name_:
3190 case OPT_print_prog_name_:
3191 case OPT_print_multi_lib:
3192 case OPT_print_multi_directory:
3193 case OPT_print_sysroot:
3194 case OPT_print_multi_os_directory:
3195 case OPT_print_sysroot_headers_suffix:
3196 case OPT_time:
3197 case OPT_wrapper:
3198 /* These options set the variables specified in common.opt
3199 automatically, and do not need to be saved for spec
3200 processing. */
3201 do_save = false;
3202 break;
3203
3204 case OPT_print_libgcc_file_name:
3205 print_file_name = "libgcc.a";
3206 do_save = false;
3207 break;
3208
3209 case OPT_fcompare_debug_second:
3210 compare_debug_second = 1;
3211 break;
3212
3213 case OPT_fcompare_debug:
3214 switch (value)
3215 {
3216 case 0:
3217 compare_debug_replacement_opt = "-fcompare-debug=";
3218 arg = "";
3219 goto compare_debug_with_arg;
3220
3221 case 1:
3222 compare_debug_replacement_opt = "-fcompare-debug=-gtoggle";
3223 arg = "-gtoggle";
3224 goto compare_debug_with_arg;
3225
3226 default:
3227 gcc_unreachable ();
3228 }
3229 break;
3230
3231 case OPT_fcompare_debug_:
3232 compare_debug_replacement_opt = decoded->canonical_option[0];
3233 compare_debug_with_arg:
3234 gcc_assert (decoded->canonical_option_num_elements == 1);
3235 gcc_assert (arg != NULL);
3236 if (arg)
3237 compare_debug = 1;
3238 else
3239 compare_debug = -1;
3240 if (compare_debug < 0)
3241 compare_debug_opt = NULL;
3242 else
3243 compare_debug_opt = arg;
3244 save_switch (compare_debug_replacement_opt, 0, NULL, validated);
3245 return true;
3246
3247 case OPT_Wa_:
3248 {
3249 int prev, j;
3250 /* Pass the rest of this option to the assembler. */
3251
3252 /* Split the argument at commas. */
3253 prev = 0;
3254 for (j = 0; arg[j]; j++)
3255 if (arg[j] == ',')
3256 {
3257 add_assembler_option (arg + prev, j - prev);
3258 prev = j + 1;
3259 }
3260
3261 /* Record the part after the last comma. */
3262 add_assembler_option (arg + prev, j - prev);
3263 }
3264 do_save = false;
3265 break;
3266
3267 case OPT_Wp_:
3268 {
3269 int prev, j;
3270 /* Pass the rest of this option to the preprocessor. */
3271
3272 /* Split the argument at commas. */
3273 prev = 0;
3274 for (j = 0; arg[j]; j++)
3275 if (arg[j] == ',')
3276 {
3277 add_preprocessor_option (arg + prev, j - prev);
3278 prev = j + 1;
3279 }
3280
3281 /* Record the part after the last comma. */
3282 add_preprocessor_option (arg + prev, j - prev);
3283 }
3284 do_save = false;
3285 break;
3286
3287 case OPT_Wl_:
3288 {
3289 int prev, j;
3290 /* Split the argument at commas. */
3291 prev = 0;
3292 for (j = 0; arg[j]; j++)
3293 if (arg[j] == ',')
3294 {
3295 add_infile (save_string (arg + prev, j - prev), "*");
3296 prev = j + 1;
3297 }
3298 /* Record the part after the last comma. */
3299 add_infile (arg + prev, "*");
3300 }
3301 do_save = false;
3302 break;
3303
3304 case OPT_Xlinker:
3305 add_infile (arg, "*");
3306 do_save = false;
3307 break;
3308
3309 case OPT_Xpreprocessor:
3310 add_preprocessor_option (arg, strlen (arg));
3311 do_save = false;
3312 break;
3313
3314 case OPT_Xassembler:
3315 add_assembler_option (arg, strlen (arg));
3316 do_save = false;
3317 break;
3318
3319 case OPT_l:
3320 /* POSIX allows separation of -l and the lib arg; canonicalize
3321 by concatenating -l with its arg */
3322 add_infile (concat ("-l", arg, NULL), "*");
3323 do_save = false;
3324 break;
3325
5de8299c
JM
3326 case OPT_L:
3327 /* Similarly, canonicalize -L for linkers that may not accept
3328 separate arguments. */
3329 save_switch (concat ("-L", arg, NULL), 0, NULL, validated);
3330 return true;
3331
603349bf
JM
3332 case OPT_save_temps:
3333 save_temps_flag = SAVE_TEMPS_CWD;
3334 validated = true;
3335 break;
3336
3337 case OPT_save_temps_:
3338 if (strcmp (arg, "cwd") == 0)
3339 save_temps_flag = SAVE_TEMPS_CWD;
3340 else if (strcmp (arg, "obj") == 0
3341 || strcmp (arg, "object") == 0)
3342 save_temps_flag = SAVE_TEMPS_OBJ;
3343 else
3344 fatal_error ("%qs is an unknown -save-temps option",
3345 decoded->orig_option_with_args_text);
3346 break;
3347
3348 case OPT_no_canonical_prefixes:
3349 /* Already handled as a special case, so ignored here. */
3350 do_save = false;
3351 break;
3352
3353 case OPT_pipe:
3354 validated = true;
603349bf
JM
3355 /* These options set the variables specified in common.opt
3356 automatically, but do need to be saved for spec
3357 processing. */
3358 break;
3359
603349bf
JM
3360 case OPT_specs_:
3361 {
3362 struct user_specs *user = XNEW (struct user_specs);
3363
3364 user->next = (struct user_specs *) 0;
3365 user->filename = arg;
3366 if (user_specs_tail)
3367 user_specs_tail->next = user;
3368 else
3369 user_specs_head = user;
3370 user_specs_tail = user;
3371 }
3372 do_save = false;
3373 break;
3374
3375 case OPT__sysroot_:
3376 target_system_root = arg;
3377 target_system_root_changed = 1;
3378 do_save = false;
3379 break;
3380
3381 case OPT_time_:
3382 if (report_times_to_file)
3383 fclose (report_times_to_file);
3384 report_times_to_file = fopen (arg, "a");
3385 do_save = false;
3386 break;
3387
3388 case OPT____:
3389 /* "-###"
3390 This is similar to -v except that there is no execution
3391 of the commands and the echoed arguments are quoted. It
3392 is intended for use in shell scripts to capture the
3393 driver-generated command line. */
3394 verbose_only_flag++;
d5478783 3395 verbose_flag = 1;
603349bf
JM
3396 do_save = false;
3397 break;
3398
3399 case OPT_B:
3400 {
3401 size_t len = strlen (arg);
3402
3403 /* Catch the case where the user has forgotten to append a
3404 directory separator to the path. Note, they may be using
3405 -B to add an executable name prefix, eg "i386-elf-", in
3406 order to distinguish between multiple installations of
3407 GCC in the same directory. Hence we must check to see
3408 if appending a directory separator actually makes a
3409 valid directory name. */
3410 if (!IS_DIR_SEPARATOR (arg[len - 1])
3411 && is_directory (arg, false))
3412 {
3413 char *tmp = XNEWVEC (char, len + 2);
3414 strcpy (tmp, arg);
3415 tmp[len] = DIR_SEPARATOR;
3416 tmp[++len] = 0;
3417 arg = tmp;
3418 }
3419
3420 add_prefix (&exec_prefixes, arg, NULL,
3421 PREFIX_PRIORITY_B_OPT, 0, 0);
3422 add_prefix (&startfile_prefixes, arg, NULL,
3423 PREFIX_PRIORITY_B_OPT, 0, 0);
3424 add_prefix (&include_prefixes, arg, NULL,
3425 PREFIX_PRIORITY_B_OPT, 0, 0);
3426 }
3427 validated = true;
3428 break;
3429
603349bf
JM
3430 case OPT_x:
3431 spec_lang = arg;
3432 if (!strcmp (spec_lang, "none"))
3433 /* Suppress the warning if -xnone comes after the last input
3434 file, because alternate command interfaces like g++ might
3435 find it useful to place -xnone after each input file. */
3436 spec_lang = 0;
3437 else
3438 last_language_n_infiles = n_infiles;
3439 do_save = false;
3440 break;
3441
603349bf
JM
3442 case OPT_o:
3443 have_o = 1;
3444#if defined(HAVE_TARGET_EXECUTABLE_SUFFIX) || defined(HAVE_TARGET_OBJECT_SUFFIX)
3445 arg = convert_filename (arg, ! have_c, 0);
3446#endif
3447 /* Save the output name in case -save-temps=obj was used. */
3448 save_temps_prefix = xstrdup (arg);
3449 /* On some systems, ld cannot handle "-o" without a space. So
3450 split the option from its argument. */
3451 save_switch ("-o", 1, &arg, validated);
3452 return true;
3453
3454 case OPT_static_libgcc:
3455 case OPT_shared_libgcc:
3456 case OPT_static_libgfortran:
3457 case OPT_static_libstdc__:
3458 /* These are always valid, since gcc.c itself understands the
3459 first two, gfortranspec.c understands -static-libgfortran and
3460 g++spec.c understands -static-libstdc++ */
3461 validated = true;
3462 break;
3463
3464 default:
d9d16a19
JM
3465 /* Various driver options need no special processing at this
3466 point, having been handled in a prescan above or being
3467 handled by specs. */
3468 break;
603349bf
JM
3469 }
3470
3471 if (do_save)
3472 save_switch (decoded->canonical_option[0],
3473 decoded->canonical_option_num_elements - 1,
3474 &decoded->canonical_option[1], validated);
3475 return true;
3476}
3477
efe5e5a0
JM
3478/* Put the driver's standard set of option handlers in *HANDLERS. */
3479
3480static void
3481set_option_handlers (struct cl_option_handlers *handlers)
3482{
3483 handlers->unknown_option_callback = driver_unknown_option_callback;
3484 handlers->wrong_lang_callback = driver_wrong_lang_callback;
3485 handlers->post_handling_callback = driver_post_handling_callback;
3486 handlers->num_handlers = 1;
3487 handlers->handlers[0].handler = driver_handle_option;
3488 handlers->handlers[0].mask = CL_DRIVER;
3489}
3490
ed1f651b
RS
3491/* Create the vector `switches' and its contents.
3492 Store its length in `n_switches'. */
3493
3494static void
60cf253a
JM
3495process_command (unsigned int decoded_options_count,
3496 struct cl_decoded_option *decoded_options)
ed1f651b 3497{
878f32c3
KG
3498 const char *temp;
3499 char *temp1;
f4c0a303 3500 const char *tooldir_prefix;
ba0c638e
SB
3501 char *(*get_relative_prefix) (const char *, const char *,
3502 const char *) = NULL;
603349bf 3503 struct cl_option_handlers handlers;
60cf253a 3504 unsigned int j;
ed1f651b 3505
71f3e391 3506 gcc_exec_prefix = getenv ("GCC_EXEC_PREFIX");
8eebb258 3507
ed1f651b
RS
3508 n_switches = 0;
3509 n_infiles = 0;
a2a05b0a 3510 added_libraries = 0;
2484b6d2 3511
53117a2f
RK
3512 /* Figure compiler version from version string. */
3513
9218435e 3514 compiler_version = temp1 = xstrdup (version_string);
ad85216e 3515
878f32c3 3516 for (; *temp1; ++temp1)
53117a2f 3517 {
878f32c3 3518 if (*temp1 == ' ')
53117a2f 3519 {
878f32c3 3520 *temp1 = '\0';
53117a2f
RK
3521 break;
3522 }
3523 }
ed1f651b 3524
ba0c638e
SB
3525 /* Handle any -no-canonical-prefixes flag early, to assign the function
3526 that builds relative prefixes. This function creates default search
3527 paths that are needed later in normal option handling. */
3528
e200444e 3529 for (j = 1; j < decoded_options_count; j++)
ba0c638e 3530 {
e200444e 3531 if (decoded_options[j].opt_index == OPT_no_canonical_prefixes)
ba0c638e
SB
3532 {
3533 get_relative_prefix = make_relative_prefix_ignore_links;
3534 break;
3535 }
3536 }
3537 if (! get_relative_prefix)
3538 get_relative_prefix = make_relative_prefix;
3539
0deb20df 3540 /* Set up the default search paths. If there is no GCC_EXEC_PREFIX,
60cf253a
JM
3541 see if we can create it from the pathname specified in
3542 decoded_options[0].arg. */
0deb20df 3543
a8ee6e2d 3544 gcc_libexec_prefix = standard_libexec_prefix;
0deb20df
TT
3545#ifndef VMS
3546 /* FIXME: make_relative_prefix doesn't yet work for VMS. */
3547 if (!gcc_exec_prefix)
3548 {
60cf253a 3549 gcc_exec_prefix = get_relative_prefix (decoded_options[0].arg,
ba0c638e
SB
3550 standard_bindir_prefix,
3551 standard_exec_prefix);
60cf253a 3552 gcc_libexec_prefix = get_relative_prefix (decoded_options[0].arg,
ba0c638e
SB
3553 standard_bindir_prefix,
3554 standard_libexec_prefix);
0deb20df 3555 if (gcc_exec_prefix)
47d33318 3556 xputenv (concat ("GCC_EXEC_PREFIX=", gcc_exec_prefix, NULL));
0deb20df 3557 }
a8ee6e2d 3558 else
c7370b83
JM
3559 {
3560 /* make_relative_prefix requires a program name, but
3561 GCC_EXEC_PREFIX is typically a directory name with a trailing
3562 / (which is ignored by make_relative_prefix), so append a
3563 program name. */
3564 char *tmp_prefix = concat (gcc_exec_prefix, "gcc", NULL);
ba0c638e
SB
3565 gcc_libexec_prefix = get_relative_prefix (tmp_prefix,
3566 standard_exec_prefix,
3567 standard_libexec_prefix);
f4c0a303
CD
3568
3569 /* The path is unrelocated, so fallback to the original setting. */
3570 if (!gcc_libexec_prefix)
3571 gcc_libexec_prefix = standard_libexec_prefix;
3572
c7370b83
JM
3573 free (tmp_prefix);
3574 }
a8ee6e2d 3575#else
0deb20df 3576#endif
f4c0a303
CD
3577 /* From this point onward, gcc_exec_prefix is non-null if the toolchain
3578 is relocated. The toolchain was either relocated using GCC_EXEC_PREFIX
60cf253a
JM
3579 or an automatically created GCC_EXEC_PREFIX from
3580 decoded_options[0].arg. */
ed1f651b 3581
a288b143 3582 /* Do language-specific adjustment/addition of flags. */
d9d16a19 3583 lang_specific_driver (&decoded_options, &decoded_options_count,
a288b143
AH
3584 &added_libraries);
3585
8eebb258 3586 if (gcc_exec_prefix)
ed1f651b 3587 {
6ed4bb9a 3588 int len = strlen (gcc_exec_prefix);
c5c0b3d9 3589
a8ee6e2d 3590 if (len > (int) sizeof ("/lib/gcc/") - 1
509781a4 3591 && (IS_DIR_SEPARATOR (gcc_exec_prefix[len-1])))
6ed4bb9a 3592 {
a8ee6e2d 3593 temp = gcc_exec_prefix + len - sizeof ("/lib/gcc/") + 1;
509781a4 3594 if (IS_DIR_SEPARATOR (*temp)
d25a45d4 3595 && strncmp (temp + 1, "lib", 3) == 0
509781a4 3596 && IS_DIR_SEPARATOR (temp[4])
c5ef564b 3597 && strncmp (temp + 5, "gcc", 3) == 0)
a8ee6e2d 3598 len -= sizeof ("/lib/gcc/") - 1;
6ed4bb9a
MM
3599 }
3600
3601 set_std_prefix (gcc_exec_prefix, len);
a8ee6e2d 3602 add_prefix (&exec_prefixes, gcc_libexec_prefix, "GCC",
1a5d37a1 3603 PREFIX_PRIORITY_LAST, 0, 0);
922a4beb 3604 add_prefix (&startfile_prefixes, gcc_exec_prefix, "GCC",
1a5d37a1 3605 PREFIX_PRIORITY_LAST, 0, 0);
ed1f651b
RS
3606 }
3607
3608 /* COMPILER_PATH and LIBRARY_PATH have values
3609 that are lists of directory names with colons. */
3610
71f3e391 3611 temp = getenv ("COMPILER_PATH");
ed1f651b
RS
3612 if (temp)
3613 {
878f32c3 3614 const char *startp, *endp;
e1e4cdc4 3615 char *nstore = (char *) alloca (strlen (temp) + 3);
ed1f651b
RS
3616
3617 startp = endp = temp;
3618 while (1)
3619 {
f6ec7e54 3620 if (*endp == PATH_SEPARATOR || *endp == 0)
ed1f651b 3621 {
d25a45d4 3622 strncpy (nstore, startp, endp - startp);
ed1f651b 3623 if (endp == startp)
d4f2852f 3624 strcpy (nstore, concat (".", dir_separator_str, NULL));
509781a4 3625 else if (!IS_DIR_SEPARATOR (endp[-1]))
ed1f651b 3626 {
d25a45d4
KH
3627 nstore[endp - startp] = DIR_SEPARATOR;
3628 nstore[endp - startp + 1] = 0;
ed1f651b
RS
3629 }
3630 else
d25a45d4 3631 nstore[endp - startp] = 0;
922a4beb 3632 add_prefix (&exec_prefixes, nstore, 0,
1a5d37a1 3633 PREFIX_PRIORITY_LAST, 0, 0);
76391e5a 3634 add_prefix (&include_prefixes, nstore, 0,
1a5d37a1 3635 PREFIX_PRIORITY_LAST, 0, 0);
ed1f651b
RS
3636 if (*endp == 0)
3637 break;
3638 endp = startp = endp + 1;
3639 }
3640 else
3641 endp++;
3642 }
3643 }
3644
71f3e391 3645 temp = getenv (LIBRARY_PATH_ENV);
fcc9ad83 3646 if (temp && *cross_compile == '0')
ed1f651b 3647 {
878f32c3 3648 const char *startp, *endp;
e1e4cdc4 3649 char *nstore = (char *) alloca (strlen (temp) + 3);
ed1f651b
RS
3650
3651 startp = endp = temp;
3652 while (1)
3653 {
f6ec7e54 3654 if (*endp == PATH_SEPARATOR || *endp == 0)
ed1f651b 3655 {
d25a45d4 3656 strncpy (nstore, startp, endp - startp);
ed1f651b 3657 if (endp == startp)
d4f2852f 3658 strcpy (nstore, concat (".", dir_separator_str, NULL));
509781a4 3659 else if (!IS_DIR_SEPARATOR (endp[-1]))
ed1f651b 3660 {
d25a45d4
KH
3661 nstore[endp - startp] = DIR_SEPARATOR;
3662 nstore[endp - startp + 1] = 0;
ed1f651b
RS
3663 }
3664 else
d25a45d4 3665 nstore[endp - startp] = 0;
6496a589 3666 add_prefix (&startfile_prefixes, nstore, NULL,
1a5d37a1 3667 PREFIX_PRIORITY_LAST, 0, 1);
ed1f651b
RS
3668 if (*endp == 0)
3669 break;
3670 endp = startp = endp + 1;
3671 }
3672 else
3673 endp++;
3674 }
3675 }
3676
3677 /* Use LPATH like LIBRARY_PATH (for the CMU build program). */
71f3e391 3678 temp = getenv ("LPATH");
fcc9ad83 3679 if (temp && *cross_compile == '0')
ed1f651b 3680 {
878f32c3 3681 const char *startp, *endp;
e1e4cdc4 3682 char *nstore = (char *) alloca (strlen (temp) + 3);
ed1f651b
RS
3683
3684 startp = endp = temp;
3685 while (1)
3686 {
f6ec7e54 3687 if (*endp == PATH_SEPARATOR || *endp == 0)
ed1f651b 3688 {
d25a45d4 3689 strncpy (nstore, startp, endp - startp);
ed1f651b 3690 if (endp == startp)
d4f2852f 3691 strcpy (nstore, concat (".", dir_separator_str, NULL));
509781a4 3692 else if (!IS_DIR_SEPARATOR (endp[-1]))
ed1f651b 3693 {
d25a45d4
KH
3694 nstore[endp - startp] = DIR_SEPARATOR;
3695 nstore[endp - startp + 1] = 0;
ed1f651b
RS
3696 }
3697 else
d25a45d4 3698 nstore[endp - startp] = 0;
6496a589 3699 add_prefix (&startfile_prefixes, nstore, NULL,
1a5d37a1 3700 PREFIX_PRIORITY_LAST, 0, 1);
ed1f651b
RS
3701 if (*endp == 0)
3702 break;
3703 endp = startp = endp + 1;
3704 }
3705 else
3706 endp++;
3707 }
3708 }
3709
922e1882
JM
3710 /* Process the options and store input files and switches in their
3711 vectors. */
3712
3713 last_language_n_infiles = -1;
ed1f651b 3714
efe5e5a0 3715 set_option_handlers (&handlers);
fe7df9c4 3716
603349bf
JM
3717 for (j = 1; j < decoded_options_count; j++)
3718 {
3719 switch (decoded_options[j].opt_index)
99f78cdd 3720 {
603349bf
JM
3721 case OPT_S:
3722 case OPT_c:
3723 case OPT_E:
3724 have_c = 1;
3725 break;
99f78cdd 3726 }
603349bf
JM
3727 if (have_c)
3728 break;
3729 }
ed1f651b 3730
603349bf
JM
3731 for (j = 1; j < decoded_options_count; j++)
3732 {
3733 if (decoded_options[j].opt_index == OPT_SPECIAL_input_file)
3a265431 3734 {
603349bf
JM
3735 const char *arg = decoded_options[j].arg;
3736 const char *p = strrchr (arg, '@');
922e1882
JM
3737 char *fname;
3738 long offset;
3739 int consumed;
3740#ifdef HAVE_TARGET_OBJECT_SUFFIX
603349bf 3741 arg = convert_filename (arg, 0, access (arg, F_OK));
922e1882
JM
3742#endif
3743 /* For LTO static archive support we handle input file
3744 specifications that are composed of a filename and
3745 an offset like FNAME@OFFSET. */
3746 if (p
603349bf 3747 && p != arg
922e1882
JM
3748 && sscanf (p, "@%li%n", &offset, &consumed) >= 1
3749 && strlen (p) == (unsigned int)consumed)
3750 {
603349bf
JM
3751 fname = (char *)xmalloc (p - arg + 1);
3752 memcpy (fname, arg, p - arg);
3753 fname[p - arg] = '\0';
922e1882
JM
3754 /* Only accept non-stdin and existing FNAME parts, otherwise
3755 try with the full name. */
3756 if (strcmp (fname, "-") == 0 || access (fname, F_OK) < 0)
3757 {
3758 free (fname);
603349bf 3759 fname = xstrdup (arg);
922e1882
JM
3760 }
3761 }
3762 else
603349bf 3763 fname = xstrdup (arg);
922e1882
JM
3764
3765 if (strcmp (fname, "-") != 0 && access (fname, F_OK) < 0)
3766 perror_with_name (fname);
3767 else
603349bf 3768 add_infile (arg, spec_lang);
922e1882
JM
3769
3770 free (fname);
603349bf 3771 continue;
3a265431 3772 }
603349bf 3773
d4d24ba4 3774 read_cmdline_option (&global_options, &global_options_set,
a4d8c676
JM
3775 decoded_options + j, UNKNOWN_LOCATION,
3776 CL_DRIVER, &handlers, global_dc);
ed1f651b
RS
3777 }
3778
14fdc613
MM
3779 /* If -save-temps=obj and -o name, create the prefix to use for %b.
3780 Otherwise just make -save-temps=obj the same as -save-temps=cwd. */
3781 if (save_temps_flag == SAVE_TEMPS_OBJ && save_temps_prefix != NULL)
3782 {
3783 save_temps_length = strlen (save_temps_prefix);
3784 temp = strrchr (lbasename (save_temps_prefix), '.');
3785 if (temp)
3786 {
3787 save_temps_length -= strlen (temp);
3788 save_temps_prefix[save_temps_length] = '\0';
3789 }
3790
3791 }
3792 else if (save_temps_prefix != NULL)
3793 {
3794 free (save_temps_prefix);
3795 save_temps_prefix = NULL;
3796 }
3797
054e88a8 3798 if (save_temps_flag && use_pipes)
4977bab6
ZW
3799 {
3800 /* -save-temps overrides -pipe, so that temp files are produced */
3801 if (save_temps_flag)
2dec80c7 3802 warning (0, "-pipe ignored because -save-temps specified");
4977bab6
ZW
3803 use_pipes = 0;
3804 }
1d088dee 3805
2153915d
AO
3806 if (!compare_debug)
3807 {
3808 const char *gcd = getenv ("GCC_COMPARE_DEBUG");
3809
3810 if (gcd && gcd[0] == '-')
3811 {
3812 compare_debug = 2;
3813 compare_debug_opt = gcd;
2153915d
AO
3814 }
3815 else if (gcd && *gcd && strcmp (gcd, "0"))
3816 {
3817 compare_debug = 3;
3818 compare_debug_opt = "-gtoggle";
2153915d
AO
3819 }
3820 }
3821 else if (compare_debug < 0)
3822 {
3823 compare_debug = 0;
3824 gcc_assert (!compare_debug_opt);
3825 }
3826
f4c0a303
CD
3827 /* Set up the search paths. We add directories that we expect to
3828 contain GNU Toolchain components before directories specified by
3829 the machine description so that we will find GNU components (like
b8698a0f 3830 the GNU assembler) before those of the host system. */
ed1f651b 3831
f4c0a303
CD
3832 /* If we don't know where the toolchain has been installed, use the
3833 configured-in locations. */
3834 if (!gcc_exec_prefix)
3835 {
48ff801b 3836#ifndef OS2
f4c0a303
CD
3837 add_prefix (&exec_prefixes, standard_libexec_prefix, "GCC",
3838 PREFIX_PRIORITY_LAST, 1, 0);
3839 add_prefix (&exec_prefixes, standard_libexec_prefix, "BINUTILS",
3840 PREFIX_PRIORITY_LAST, 2, 0);
3841 add_prefix (&exec_prefixes, standard_exec_prefix, "BINUTILS",
3842 PREFIX_PRIORITY_LAST, 2, 0);
48ff801b 3843#endif
f4c0a303
CD
3844 add_prefix (&startfile_prefixes, standard_exec_prefix, "BINUTILS",
3845 PREFIX_PRIORITY_LAST, 1, 0);
3846 }
ed1f651b 3847
f4c0a303 3848 gcc_assert (!IS_ABSOLUTE_PATH (tooldir_base_prefix));
9218435e 3849 tooldir_prefix = concat (tooldir_base_prefix, spec_machine,
d4f2852f 3850 dir_separator_str, NULL);
c648ab8a 3851
f4c0a303
CD
3852 /* Look for tools relative to the location from which the driver is
3853 running, or, if that is not available, the configured prefix. */
3854 tooldir_prefix
3855 = concat (gcc_exec_prefix ? gcc_exec_prefix : standard_exec_prefix,
3856 spec_machine, dir_separator_str,
3857 spec_version, dir_separator_str, tooldir_prefix, NULL);
c648ab8a 3858
9218435e 3859 add_prefix (&exec_prefixes,
d4f2852f 3860 concat (tooldir_prefix, "bin", dir_separator_str, NULL),
1a5d37a1 3861 "BINUTILS", PREFIX_PRIORITY_LAST, 0, 0);
48ff801b 3862 add_prefix (&startfile_prefixes,
d4f2852f 3863 concat (tooldir_prefix, "lib", dir_separator_str, NULL),
1a5d37a1 3864 "BINUTILS", PREFIX_PRIORITY_LAST, 0, 1);
f18fd956 3865
047d636f
DJ
3866#if defined(TARGET_SYSTEM_ROOT_RELOCATABLE) && !defined(VMS)
3867 /* If the normal TARGET_SYSTEM_ROOT is inside of $exec_prefix,
3868 then consider it to relocate with the rest of the GCC installation
3869 if GCC_EXEC_PREFIX is set.
3870 ``make_relative_prefix'' is not compiled for VMS, so don't call it. */
922e1882 3871 if (target_system_root && !target_system_root_changed && gcc_exec_prefix)
4977bab6 3872 {
60cf253a 3873 char *tmp_prefix = get_relative_prefix (decoded_options[0].arg,
ba0c638e
SB
3874 standard_bindir_prefix,
3875 target_system_root);
4977bab6 3876 if (tmp_prefix && access_check (tmp_prefix, F_OK) == 0)
047d636f
DJ
3877 {
3878 target_system_root = tmp_prefix;
3879 target_system_root_changed = 1;
3880 }
4977bab6 3881 }
047d636f 3882#endif
4977bab6 3883
004fd4d5
RS
3884 /* More prefixes are enabled in main, after we read the specs file
3885 and determine whether this is cross-compilation or not. */
ed1f651b 3886
fa0d5369 3887 if (n_infiles == last_language_n_infiles && spec_lang != 0)
bdc6b402 3888 warning (0, "%<-x %s%> after last input file has no effect", spec_lang);
ed1f651b 3889
2153915d
AO
3890 if (compare_debug == 2 || compare_debug == 3)
3891 {
922e1882 3892 alloc_switch ();
2153915d
AO
3893 switches[n_switches].part1 = concat ("fcompare-debug=",
3894 compare_debug_opt,
3895 NULL);
3896 switches[n_switches].args = 0;
3897 switches[n_switches].live_cond = 0;
3898 switches[n_switches].validated = 0;
3899 switches[n_switches].ordering = 0;
3900 n_switches++;
3901 compare_debug = 1;
3902 }
3903
69927b59 3904 /* Ensure we only invoke each subprocess once. */
41fd0f9b 3905 if (print_subprocess_help || print_help_list || print_version)
69927b59 3906 {
922e1882 3907 n_infiles = 0;
69927b59 3908
c662432e
NC
3909 /* Create a dummy input file, so that we can pass
3910 the help option on to the various sub-processes. */
922e1882 3911 add_infile ("help-dummy", "c");
69927b59
NB
3912 }
3913
922e1882 3914 alloc_switch ();
ed1f651b 3915 switches[n_switches].part1 = 0;
922e1882 3916 alloc_infile ();
ed1f651b
RS
3917 infiles[n_infiles].name = 0;
3918}
b856c15d 3919
4977bab6 3920/* Store switches not filtered out by %<S in spec in COLLECT_GCC_OPTIONS
b856c15d
RO
3921 and place that in the environment. */
3922
3923static void
1d088dee 3924set_collect_gcc_options (void)
b856c15d
RO
3925{
3926 int i;
3927 int first_time;
3928
3929 /* Build COLLECT_GCC_OPTIONS to have all of the options specified to
3930 the compiler. */
3931 obstack_grow (&collect_obstack, "COLLECT_GCC_OPTIONS=",
3932 sizeof ("COLLECT_GCC_OPTIONS=") - 1);
3933
3934 first_time = TRUE;
3935 for (i = 0; (int) i < n_switches; i++)
3936 {
3937 const char *const *args;
3938 const char *p, *q;
3939 if (!first_time)
3940 obstack_grow (&collect_obstack, " ", 1);
3941
3942 first_time = FALSE;
3943
3944 /* Ignore elided switches. */
96f5b137
L
3945 if ((switches[i].live_cond
3946 & (SWITCH_IGNORE | SWITCH_KEEP_FOR_GCC))
3947 == SWITCH_IGNORE)
b856c15d
RO
3948 continue;
3949
3950 obstack_grow (&collect_obstack, "'-", 2);
3951 q = switches[i].part1;
3952 while ((p = strchr (q, '\'')))
3953 {
3954 obstack_grow (&collect_obstack, q, p - q);
3955 obstack_grow (&collect_obstack, "'\\''", 4);
3956 q = ++p;
3957 }
3958 obstack_grow (&collect_obstack, q, strlen (q));
3959 obstack_grow (&collect_obstack, "'", 1);
3960
3961 for (args = switches[i].args; args && *args; args++)
3962 {
3963 obstack_grow (&collect_obstack, " '", 2);
3964 q = *args;
3965 while ((p = strchr (q, '\'')))
3966 {
3967 obstack_grow (&collect_obstack, q, p - q);
3968 obstack_grow (&collect_obstack, "'\\''", 4);
3969 q = ++p;
3970 }
3971 obstack_grow (&collect_obstack, q, strlen (q));
3972 obstack_grow (&collect_obstack, "'", 1);
3973 }
3974 }
3975 obstack_grow (&collect_obstack, "\0", 1);
47d33318 3976 xputenv (XOBFINISH (&collect_obstack, char *));
b856c15d 3977}
ed1f651b
RS
3978\f
3979/* Process a spec string, accumulating and running commands. */
3980
3981/* These variables describe the input file name.
3982 input_file_number is the index on outfiles of this file,
3983 so that the output file name can be stored for later use by %o.
3984 input_basename is the start of the part of the input file
3985 sans all directory names, and basename_length is the number
3986 of characters starting there excluding the suffix .c or whatever. */
3987
6afbc885 3988static const char *gcc_input_filename;
ed1f651b 3989static int input_file_number;
f271358e 3990size_t input_filename_length;
ed1f651b 3991static int basename_length;
ea414c97 3992static int suffixed_basename_length;
878f32c3
KG
3993static const char *input_basename;
3994static const char *input_suffix;
a9024779 3995#ifndef HOST_LACKS_INODE_NUMBERS
99f78cdd 3996static struct stat input_stat;
a9024779 3997#endif
99f78cdd 3998static int input_stat_set;
ed1f651b 3999
a9374841
MM
4000/* The compiler used to process the current input file. */
4001static struct compiler *input_file_compiler;
4002
ed1f651b
RS
4003/* These are variables used within do_spec and do_spec_1. */
4004
4005/* Nonzero if an arg has been started and not yet terminated
4006 (with space, tab or newline). */
4007static int arg_going;
4008
4009/* Nonzero means %d or %g has been seen; the next arg to be terminated
4010 is a temporary file name. */
4011static int delete_this_arg;
4012
4013/* Nonzero means %w has been seen; the next arg to be terminated
4014 is the output file name of this compilation. */
4015static int this_is_output_file;
4016
4017/* Nonzero means %s has been seen; the next arg to be terminated
4018 is the name of a library file and we should try the standard
4019 search dirs for it. */
4020static int this_is_library_file;
4021
3beb864c
NC
4022/* Nonzero means %T has been seen; the next arg to be terminated
4023 is the name of a linker script and we should try all of the
4024 standard search dirs for it. If it is found insert a --script
4025 command line switch and then substitute the full path in place,
4026 otherwise generate an error message. */
4027static int this_is_linker_script;
4028
a99bf70c
JW
4029/* Nonzero means that the input of this command is coming from a pipe. */
4030static int input_from_pipe;
4031
11972f66 4032/* Nonnull means substitute this for any suffix when outputting a switches
dc297297 4033 arguments. */
11972f66
NS
4034static const char *suffix_subst;
4035
13e7cedb
DH
4036/* If there is an argument being accumulated, terminate it and store it. */
4037
4038static void
4039end_going_arg (void)
4040{
4041 if (arg_going)
4042 {
4043 const char *string;
4044
4045 obstack_1grow (&obstack, 0);
4046 string = XOBFINISH (&obstack, const char *);
4047 if (this_is_library_file)
4048 string = find_file (string);
3beb864c
NC
4049 if (this_is_linker_script)
4050 {
4051 char * full_script_path = find_a_file (&startfile_prefixes, string, R_OK, true);
4052
4053 if (full_script_path == NULL)
4054 {
bdc6b402 4055 error ("unable to locate default linker script %qs in the library search paths", string);
3beb864c
NC
4056 /* Script was not found on search path. */
4057 return;
4058 }
4059 store_arg ("--script", false, false);
4060 string = full_script_path;
4061 }
13e7cedb
DH
4062 store_arg (string, delete_this_arg, this_is_output_file);
4063 if (this_is_output_file)
4064 outfiles[input_file_number] = string;
4065 arg_going = 0;
4066 }
4067}
4068
fe7df9c4
SP
4069
4070/* Parse the WRAPPER string which is a comma separated list of the command line
4071 and insert them into the beginning of argbuf. */
4072
4073static void
4074insert_wrapper (const char *wrapper)
4075{
4076 int n = 0;
4077 int i;
4078 char *buf = xstrdup (wrapper);
4079 char *p = buf;
5b634ee0 4080 unsigned int old_length = VEC_length (const_char_p, argbuf);
fe7df9c4
SP
4081
4082 do
4083 {
4084 n++;
4085 while (*p == ',')
4086 p++;
4087 }
4088 while ((p = strchr (p, ',')) != NULL);
4089
5b634ee0
JM
4090 VEC_safe_grow (const_char_p, heap, argbuf, old_length + n);
4091 memmove (VEC_address (const_char_p, argbuf) + n,
4092 VEC_address (const_char_p, argbuf),
4093 old_length * sizeof (const_char_p));
fe7df9c4
SP
4094
4095 i = 0;
4096 p = buf;
4097 do
4098 {
4099 while (*p == ',')
4100 {
4101 *p = 0;
4102 p++;
4103 }
5b634ee0
JM
4104 VEC_replace (const_char_p, argbuf, i, p);
4105 i++;
fe7df9c4
SP
4106 }
4107 while ((p = strchr (p, ',')) != NULL);
4108 gcc_assert (i == n);
fe7df9c4
SP
4109}
4110
ed1f651b
RS
4111/* Process the spec SPEC and run the commands specified therein.
4112 Returns 0 if the spec is successfully processed; -1 if failed. */
4113
f271358e 4114int
1d088dee 4115do_spec (const char *spec)
ed1f651b
RS
4116{
4117 int value;
4118
343f59d9 4119 value = do_spec_2 (spec);
ed1f651b
RS
4120
4121 /* Force out any unfinished command.
4122 If -pipe, this forces out the last command if it ended in `|'. */
4123 if (value == 0)
4124 {
5b634ee0
JM
4125 if (VEC_length (const_char_p, argbuf) > 0
4126 && !strcmp (VEC_last (const_char_p, argbuf), "|"))
4127 VEC_pop (const_char_p, argbuf);
ed1f651b 4128
b856c15d
RO
4129 set_collect_gcc_options ();
4130
5b634ee0 4131 if (VEC_length (const_char_p, argbuf) > 0)
ed1f651b
RS
4132 value = execute ();
4133 }
4134
4135 return value;
4136}
4137
343f59d9 4138static int
1d088dee 4139do_spec_2 (const char *spec)
343f59d9 4140{
6544fbcb
RS
4141 int result;
4142
343f59d9
AM
4143 clear_args ();
4144 arg_going = 0;
4145 delete_this_arg = 0;
4146 this_is_output_file = 0;
4147 this_is_library_file = 0;
3beb864c 4148 this_is_linker_script = 0;
343f59d9
AM
4149 input_from_pipe = 0;
4150 suffix_subst = NULL;
4151
6544fbcb
RS
4152 result = do_spec_1 (spec, 0, NULL);
4153
13e7cedb 4154 end_going_arg ();
6544fbcb
RS
4155
4156 return result;
343f59d9
AM
4157}
4158
db36994b 4159
7816bea0
DJ
4160/* Process the given spec string and add any new options to the end
4161 of the switches/n_switches array. */
4162
4163static void
1d088dee 4164do_option_spec (const char *name, const char *spec)
7816bea0
DJ
4165{
4166 unsigned int i, value_count, value_len;
4167 const char *p, *q, *value;
4168 char *tmp_spec, *tmp_spec_p;
4169
4170 if (configure_default_options[0].name == NULL)
4171 return;
4172
4173 for (i = 0; i < ARRAY_SIZE (configure_default_options); i++)
4174 if (strcmp (configure_default_options[i].name, name) == 0)
4175 break;
4176 if (i == ARRAY_SIZE (configure_default_options))
4177 return;
4178
4179 value = configure_default_options[i].value;
4180 value_len = strlen (value);
4181
4182 /* Compute the size of the final spec. */
4183 value_count = 0;
4184 p = spec;
4185 while ((p = strstr (p, "%(VALUE)")) != NULL)
4186 {
4187 p ++;
4188 value_count ++;
4189 }
4190
4191 /* Replace each %(VALUE) by the specified value. */
e1e4cdc4 4192 tmp_spec = (char *) alloca (strlen (spec) + 1
7816bea0
DJ
4193 + value_count * (value_len - strlen ("%(VALUE)")));
4194 tmp_spec_p = tmp_spec;
4195 q = spec;
4196 while ((p = strstr (q, "%(VALUE)")) != NULL)
4197 {
4198 memcpy (tmp_spec_p, q, p - q);
4199 tmp_spec_p = tmp_spec_p + (p - q);
4200 memcpy (tmp_spec_p, value, value_len);
4201 tmp_spec_p += value_len;
4202 q = p + strlen ("%(VALUE)");
4203 }
4204 strcpy (tmp_spec_p, q);
4205
4206 do_self_spec (tmp_spec);
4207}
4208
db36994b
RS
4209/* Process the given spec string and add any new options to the end
4210 of the switches/n_switches array. */
4211
4212static void
1d088dee 4213do_self_spec (const char *spec)
db36994b 4214{
94b90527
JJ
4215 int i;
4216
db36994b
RS
4217 do_spec_2 (spec);
4218 do_spec_1 (" ", 0, NULL);
4219
94b90527
JJ
4220 /* Mark %<S switches processed by do_self_spec to be ignored permanently.
4221 do_self_specs adds the replacements to switches array, so it shouldn't
4222 be processed afterwards. */
4223 for (i = 0; i < n_switches; i++)
4224 if ((switches[i].live_cond & SWITCH_IGNORE))
4225 switches[i].live_cond |= SWITCH_IGNORE_PERMANENTLY;
4226
5b634ee0 4227 if (VEC_length (const_char_p, argbuf) > 0)
db36994b 4228 {
efe5e5a0
JM
4229 const char **argbuf_copy;
4230 struct cl_decoded_option *decoded_options;
4231 struct cl_option_handlers handlers;
4232 unsigned int decoded_options_count;
4233 unsigned int j;
db36994b 4234
efe5e5a0
JM
4235 /* Create a copy of argbuf with a dummy argv[0] entry for
4236 decode_cmdline_options_to_array. */
5b634ee0
JM
4237 argbuf_copy = XNEWVEC (const char *,
4238 VEC_length (const_char_p, argbuf) + 1);
efe5e5a0 4239 argbuf_copy[0] = "";
5b634ee0
JM
4240 memcpy (argbuf_copy + 1, VEC_address (const_char_p, argbuf),
4241 VEC_length (const_char_p, argbuf) * sizeof (const char *));
db36994b 4242
5b634ee0
JM
4243 decode_cmdline_options_to_array (VEC_length (const_char_p, argbuf) + 1,
4244 argbuf_copy,
efe5e5a0
JM
4245 CL_DRIVER, &decoded_options,
4246 &decoded_options_count);
2153915d 4247
efe5e5a0 4248 set_option_handlers (&handlers);
2153915d 4249
efe5e5a0
JM
4250 for (j = 1; j < decoded_options_count; j++)
4251 {
4252 switch (decoded_options[j].opt_index)
2153915d 4253 {
efe5e5a0
JM
4254 case OPT_SPECIAL_input_file:
4255 /* Specs should only generate options, not input
4256 files. */
4257 if (strcmp (decoded_options[j].arg, "-") != 0)
4258 fatal_error ("switch %qs does not start with %<-%>",
4259 decoded_options[j].arg);
4260 else
4261 fatal_error ("spec-generated switch is just %<-%>");
4262 break;
2153915d 4263
efe5e5a0
JM
4264 case OPT_fcompare_debug_second:
4265 case OPT_fcompare_debug:
4266 case OPT_fcompare_debug_:
4267 case OPT_o:
4268 /* Avoid duplicate processing of some options from
4269 compare-debug specs; just save them here. */
4270 save_switch (decoded_options[j].canonical_option[0],
4271 (decoded_options[j].canonical_option_num_elements
4272 - 1),
4273 &decoded_options[j].canonical_option[1], false);
4274 break;
4275
4276 default:
4277 read_cmdline_option (&global_options, &global_options_set,
a4d8c676
JM
4278 decoded_options + j, UNKNOWN_LOCATION,
4279 CL_DRIVER, &handlers, global_dc);
efe5e5a0 4280 break;
2153915d 4281 }
db36994b 4282 }
2153915d 4283
efe5e5a0 4284 alloc_switch ();
2153915d 4285 switches[n_switches].part1 = 0;
db36994b
RS
4286 }
4287}
4288
00dcee0c
AM
4289/* Callback for processing %D and %I specs. */
4290
4291struct spec_path_info {
4292 const char *option;
4293 const char *append;
4294 size_t append_len;
4295 bool omit_relative;
4296 bool separate_options;
4297};
4298
4299static void *
4300spec_path (char *path, void *data)
76391e5a 4301{
e1e4cdc4 4302 struct spec_path_info *info = (struct spec_path_info *) data;
00dcee0c
AM
4303 size_t len = 0;
4304 char save = 0;
76391e5a 4305
00dcee0c
AM
4306 if (info->omit_relative && !IS_ABSOLUTE_PATH (path))
4307 return NULL;
4308
4309 if (info->append_len != 0)
76391e5a 4310 {
00dcee0c
AM
4311 len = strlen (path);
4312 memcpy (path + len, info->append, info->append_len + 1);
76391e5a
PB
4313 }
4314
00dcee0c
AM
4315 if (!is_directory (path, true))
4316 return NULL;
76391e5a 4317
00dcee0c
AM
4318 do_spec_1 (info->option, 1, NULL);
4319 if (info->separate_options)
4320 do_spec_1 (" ", 0, NULL);
4321
4322 if (info->append_len == 0)
76391e5a 4323 {
00dcee0c
AM
4324 len = strlen (path);
4325 save = path[len - 1];
4326 if (IS_DIR_SEPARATOR (path[len - 1]))
4327 path[len - 1] = '\0';
76391e5a 4328 }
00dcee0c
AM
4329
4330 do_spec_1 (path, 1, NULL);
4331 do_spec_1 (" ", 0, NULL);
4332
4333 /* Must not damage the original path. */
4334 if (info->append_len == 0)
4335 path[len - 1] = save;
4336
4337 return NULL;
76391e5a
PB
4338}
4339
5a691e98
RAE
4340/* Create a temporary FILE with the contents of ARGV. Add @FILE to the
4341 argument list. */
4342
4343static void
4344create_at_file (char **argv)
4345{
4346 char *temp_file = make_temp_file ("");
4347 char *at_argument = concat ("@", temp_file, NULL);
4348 FILE *f = fopen (temp_file, "w");
4349 int status;
4350
4351 if (f == NULL)
2dec80c7
JM
4352 fatal_error ("could not open temporary response file %s",
4353 temp_file);
5a691e98
RAE
4354
4355 status = writeargv (argv, f);
4356
4357 if (status)
2dec80c7
JM
4358 fatal_error ("could not write to temporary response file %s",
4359 temp_file);
5a691e98
RAE
4360
4361 status = fclose (f);
4362
4363 if (EOF == status)
2dec80c7
JM
4364 fatal_error ("could not close temporary response file %s",
4365 temp_file);
5a691e98
RAE
4366
4367 store_arg (at_argument, 0, 0);
4368
4369 record_temp_file (temp_file, !save_temps_flag, !save_temps_flag);
4370}
4371
4372/* True if we should compile INFILE. */
4373
4374static bool
4375compile_input_file_p (struct infile *infile)
4376{
4377 if ((!infile->language) || (infile->language[0] != '*'))
4378 if (infile->incompiler == input_file_compiler)
4379 return true;
4380 return false;
4381}
4382
69d540bd
NF
4383/* Process each member of VEC as a spec. */
4384
4385static void
4386do_specs_vec (VEC(char_p,heap) *vec)
4387{
4388 unsigned ix;
4389 char *opt;
4390
4391 FOR_EACH_VEC_ELT (char_p, vec, ix, opt)
4392 {
4393 do_spec_1 (opt, 1, NULL);
4394 /* Make each accumulated option a separate argument. */
4395 do_spec_1 (" ", 0, NULL);
4396 }
4397}
4398
ed1f651b
RS
4399/* Process the sub-spec SPEC as a portion of a larger spec.
4400 This is like processing a whole spec except that we do
4401 not initialize at the beginning and we do not supply a
4402 newline by default at the end.
4403 INSWITCH nonzero means don't process %-sequences in SPEC;
4404 in this case, % is treated as an ordinary character.
4405 This is used while substituting switches.
4406 INSWITCH nonzero also causes SPC not to terminate an argument.
4407
4408 Value is zero unless a line was finished
4409 and the command on that line reported an error. */
4410
4411static int
1d088dee 4412do_spec_1 (const char *spec, int inswitch, const char *soft_matched_part)
ed1f651b 4413{
b3694847
SS
4414 const char *p = spec;
4415 int c;
ed1f651b 4416 int i;
3279bba6 4417 int value;
ed1f651b 4418
ededb2fc 4419 while ((c = *p++))
ed1f651b
RS
4420 /* If substituting a switch, treat all chars like letters.
4421 Otherwise, NL, SPC, TAB and % are special. */
4422 switch (inswitch ? 'a' : c)
4423 {
4424 case '\n':
13e7cedb 4425 end_going_arg ();
ed1f651b 4426
5b634ee0
JM
4427 if (VEC_length (const_char_p, argbuf) > 0
4428 && !strcmp (VEC_last (const_char_p, argbuf), "|"))
ed1f651b 4429 {
ed1f651b
RS
4430 /* A `|' before the newline means use a pipe here,
4431 but only if -pipe was specified.
4432 Otherwise, execute now and don't pass the `|' as an arg. */
4977bab6 4433 if (use_pipes)
ed1f651b 4434 {
a99bf70c 4435 input_from_pipe = 1;
ed1f651b
RS
4436 break;
4437 }
4438 else
5b634ee0 4439 VEC_pop (const_char_p, argbuf);
ed1f651b
RS
4440 }
4441
b856c15d
RO
4442 set_collect_gcc_options ();
4443
5b634ee0 4444 if (VEC_length (const_char_p, argbuf) > 0)
ed1f651b 4445 {
3279bba6 4446 value = execute ();
ed1f651b
RS
4447 if (value)
4448 return value;
4449 }
4450 /* Reinitialize for a new command, and for a new argument. */
4451 clear_args ();
4452 arg_going = 0;
4453 delete_this_arg = 0;
4454 this_is_output_file = 0;
4455 this_is_library_file = 0;
3beb864c 4456 this_is_linker_script = 0;
a99bf70c 4457 input_from_pipe = 0;
ed1f651b
RS
4458 break;
4459
4460 case '|':
13e7cedb 4461 end_going_arg ();
ed1f651b
RS
4462
4463 /* Use pipe */
4464 obstack_1grow (&obstack, c);
4465 arg_going = 1;
4466 break;
4467
4468 case '\t':
4469 case ' ':
13e7cedb
DH
4470 end_going_arg ();
4471
ed1f651b 4472 /* Reinitialize for a new argument. */
ed1f651b
RS
4473 delete_this_arg = 0;
4474 this_is_output_file = 0;
4475 this_is_library_file = 0;
3beb864c 4476 this_is_linker_script = 0;
ed1f651b
RS
4477 break;
4478
4479 case '%':
4480 switch (c = *p++)
4481 {
4482 case 0:
bdc6b402 4483 fatal_error ("spec %qs invalid", spec);
ed1f651b
RS
4484
4485 case 'b':
14fdc613
MM
4486 if (save_temps_length)
4487 obstack_grow (&obstack, save_temps_prefix, save_temps_length);
4488 else
4489 obstack_grow (&obstack, input_basename, basename_length);
2153915d
AO
4490 if (compare_debug < 0)
4491 obstack_grow (&obstack, ".gk", 3);
ed1f651b
RS
4492 arg_going = 1;
4493 break;
4494
ea414c97 4495 case 'B':
14fdc613
MM
4496 if (save_temps_length)
4497 obstack_grow (&obstack, save_temps_prefix, save_temps_length);
4498 else
4499 obstack_grow (&obstack, input_basename, suffixed_basename_length);
2153915d
AO
4500 if (compare_debug < 0)
4501 obstack_grow (&obstack, ".gk", 3);
ea414c97
ZW
4502 arg_going = 1;
4503 break;
4504
ed1f651b
RS
4505 case 'd':
4506 delete_this_arg = 2;
4507 break;
4508
4509 /* Dump out the directories specified with LIBRARY_PATH,
004fd4d5
RS
4510 followed by the absolute directories
4511 that we search for startfiles. */
ed1f651b 4512 case 'D':
8cacec76 4513 {
00dcee0c 4514 struct spec_path_info info;
59014d0a 4515
00dcee0c
AM
4516 info.option = "-L";
4517 info.append_len = 0;
76391e5a 4518#ifdef RELATIVE_PREFIX_NOT_LINKDIR
00dcee0c
AM
4519 /* Used on systems which record the specified -L dirs
4520 and use them to search for dynamic linking.
4521 Relative directories always come from -B,
4522 and it is better not to use them for searching
4523 at run time. In particular, stage1 loses. */
4524 info.omit_relative = true;
76391e5a 4525#else
00dcee0c 4526 info.omit_relative = false;
004fd4d5 4527#endif
00dcee0c
AM
4528 info.separate_options = false;
4529
4530 for_each_path (&startfile_prefixes, true, 0, spec_path, &info);
8cacec76 4531 }
ed1f651b
RS
4532 break;
4533
4534 case 'e':
ab87f8c8 4535 /* %efoo means report an error with `foo' as error message
ed1f651b
RS
4536 and don't execute any more commands for this file. */
4537 {
878f32c3 4538 const char *q = p;
ed1f651b 4539 char *buf;
d25a45d4
KH
4540 while (*p != 0 && *p != '\n')
4541 p++;
e1e4cdc4 4542 buf = (char *) alloca (p - q + 1);
ed1f651b
RS
4543 strncpy (buf, q, p - q);
4544 buf[p - q] = 0;
5bc69b92 4545 error ("%s", _(buf));
ed1f651b
RS
4546 return -1;
4547 }
4548 break;
4a88a060 4549 case 'n':
09da1532 4550 /* %nfoo means report a notice with `foo' on stderr. */
4a88a060
JH
4551 {
4552 const char *q = p;
4553 char *buf;
4554 while (*p != 0 && *p != '\n')
4555 p++;
e1e4cdc4 4556 buf = (char *) alloca (p - q + 1);
4a88a060
JH
4557 strncpy (buf, q, p - q);
4558 buf[p - q] = 0;
2dec80c7 4559 inform (0, "%s", _(buf));
4a88a060
JH
4560 if (*p)
4561 p++;
4562 }
4563 break;
ed1f651b 4564
d25a45d4
KH
4565 case 'j':
4566 {
4567 struct stat st;
4568
4977bab6
ZW
4569 /* If save_temps_flag is off, and the HOST_BIT_BUCKET is
4570 defined, and it is not a directory, and it is
4571 writable, use it. Otherwise, treat this like any
4572 other temporary file. */
d25a45d4
KH
4573
4574 if ((!save_temps_flag)
4575 && (stat (HOST_BIT_BUCKET, &st) == 0) && (!S_ISDIR (st.st_mode))
4576 && (access (HOST_BIT_BUCKET, W_OK) == 0))
4577 {
4578 obstack_grow (&obstack, HOST_BIT_BUCKET,
4579 strlen (HOST_BIT_BUCKET));
4580 delete_this_arg = 0;
4581 arg_going = 1;
4582 break;
4583 }
4584 }
4977bab6
ZW
4585 goto create_temp_file;
4586 case '|':
4587 if (use_pipes)
4588 {
4589 obstack_1grow (&obstack, '-');
4590 delete_this_arg = 0;
4591 arg_going = 1;
4592
4593 /* consume suffix */
a92dd235 4594 while (*p == '.' || ISALNUM ((unsigned char) *p))
4977bab6
ZW
4595 p++;
4596 if (p[0] == '%' && p[1] == 'O')
4597 p += 2;
1d088dee 4598
4977bab6
ZW
4599 break;
4600 }
4601 goto create_temp_file;
4602 case 'm':
4603 if (use_pipes)
4604 {
4605 /* consume suffix */
a92dd235 4606 while (*p == '.' || ISALNUM ((unsigned char) *p))
4977bab6
ZW
4607 p++;
4608 if (p[0] == '%' && p[1] == 'O')
4609 p += 2;
1d088dee 4610
4977bab6
ZW
4611 break;
4612 }
4613 goto create_temp_file;
ed1f651b 4614 case 'g':
d887e808 4615 case 'u':
4401b31c 4616 case 'U':
4977bab6 4617 create_temp_file:
ed1f651b 4618 {
fb266030 4619 struct temp_name *t;
dd75c292 4620 int suffix_length;
878f32c3 4621 const char *suffix = p;
a1d9074c 4622 char *saved_suffix = NULL;
dd75c292 4623
a92dd235 4624 while (*p == '.' || ISALNUM ((unsigned char) *p))
a1d9074c
TT
4625 p++;
4626 suffix_length = p - suffix;
dd75c292
CB
4627 if (p[0] == '%' && p[1] == 'O')
4628 {
cbc54665 4629 p += 2;
dd75c292 4630 /* We don't support extra suffix characters after %O. */
a92dd235 4631 if (*p == '.' || ISALNUM ((unsigned char) *p))
bdc6b402 4632 fatal_error ("spec %qs has invalid %<%%0%c%>", spec, *p);
a1d9074c 4633 if (suffix_length == 0)
45936a85 4634 suffix = TARGET_OBJECT_SUFFIX;
a1d9074c
TT
4635 else
4636 {
4637 saved_suffix
5ed6ace5 4638 = XNEWVEC (char, suffix_length
703ad42b 4639 + strlen (TARGET_OBJECT_SUFFIX));
a1d9074c
TT
4640 strncpy (saved_suffix, suffix, suffix_length);
4641 strcpy (saved_suffix + suffix_length,
45936a85 4642 TARGET_OBJECT_SUFFIX);
a1d9074c 4643 }
45936a85 4644 suffix_length += strlen (TARGET_OBJECT_SUFFIX);
dd75c292 4645 }
589005ff 4646
2153915d
AO
4647 if (compare_debug < 0)
4648 {
4649 suffix = concat (".gk", suffix, NULL);
4650 suffix_length += 3;
4651 }
4652
14fdc613
MM
4653 /* If -save-temps=obj and -o were specified, use that for the
4654 temp file. */
4655 if (save_temps_length)
4656 {
4657 char *tmp;
4658 temp_filename_length
4659 = save_temps_length + suffix_length + 1;
4660 tmp = (char *) alloca (temp_filename_length);
4661 memcpy (tmp, save_temps_prefix, save_temps_length);
4662 memcpy (tmp + save_temps_length, suffix, suffix_length);
4663 tmp[save_temps_length + suffix_length] = '\0';
4664 temp_filename = save_string (tmp,
4665 temp_filename_length + 1);
4666 obstack_grow (&obstack, temp_filename,
4667 temp_filename_length);
4668 arg_going = 1;
4669 delete_this_arg = 0;
4670 break;
4671 }
4672
6afbc885 4673 /* If the gcc_input_filename has the same suffix specified
99f78cdd
IR
4674 for the %g, %u, or %U, and -save-temps is specified,
4675 we could end up using that file as an intermediate
4676 thus clobbering the user's source file (.e.g.,
4677 gcc -save-temps foo.s would clobber foo.s with the
4678 output of cpp0). So check for this condition and
4679 generate a temp file as the intermediate. */
589005ff 4680
99f78cdd
IR
4681 if (save_temps_flag)
4682 {
6ea2b70d 4683 char *tmp;
14fdc613
MM
4684 temp_filename_length = basename_length + suffix_length + 1;
4685 tmp = (char *) alloca (temp_filename_length);
4686 memcpy (tmp, input_basename, basename_length);
4687 memcpy (tmp + basename_length, suffix, suffix_length);
4688 tmp[basename_length + suffix_length] = '\0';
6ea2b70d 4689 temp_filename = tmp;
14fdc613 4690
6afbc885 4691 if (strcmp (temp_filename, gcc_input_filename) != 0)
99f78cdd 4692 {
a9024779 4693#ifndef HOST_LACKS_INODE_NUMBERS
1d088dee 4694 struct stat st_temp;
589005ff 4695
1d088dee
AJ
4696 /* Note, set_input() resets input_stat_set to 0. */
4697 if (input_stat_set == 0)
4698 {
6afbc885
JM
4699 input_stat_set = stat (gcc_input_filename,
4700 &input_stat);
1d088dee
AJ
4701 if (input_stat_set >= 0)
4702 input_stat_set = 1;
4703 }
589005ff 4704
6afbc885 4705 /* If we have the stat for the gcc_input_filename
1d088dee
AJ
4706 and we can do the stat for the temp_filename
4707 then the they could still refer to the same
4708 file if st_dev/st_ino's are the same. */
99f78cdd
IR
4709 if (input_stat_set != 1
4710 || stat (temp_filename, &st_temp) < 0
4711 || input_stat.st_dev != st_temp.st_dev
4712 || input_stat.st_ino != st_temp.st_ino)
a9024779
DS
4713#else
4714 /* Just compare canonical pathnames. */
6afbc885 4715 char* input_realname = lrealpath (gcc_input_filename);
a9024779
DS
4716 char* temp_realname = lrealpath (temp_filename);
4717 bool files_differ = strcmp (input_realname, temp_realname);
4718 free (input_realname);
4719 free (temp_realname);
7904f95f 4720 if (files_differ)
a9024779 4721#endif
589005ff 4722 {
99f78cdd
IR
4723 temp_filename = save_string (temp_filename,
4724 temp_filename_length + 1);
4725 obstack_grow (&obstack, temp_filename,
1d088dee 4726 temp_filename_length);
99f78cdd 4727 arg_going = 1;
d2dff06b 4728 delete_this_arg = 0;
99f78cdd
IR
4729 break;
4730 }
4731 }
4732 }
fb266030
TW
4733
4734 /* See if we already have an association of %g/%u/%U and
4735 suffix. */
4736 for (t = temp_names; t; t = t->next)
dd75c292
CB
4737 if (t->length == suffix_length
4738 && strncmp (t->suffix, suffix, suffix_length) == 0
95456d43 4739 && t->unique == (c == 'u' || c == 'U' || c == 'j'))
fb266030
TW
4740 break;
4741
2297fdf1
TT
4742 /* Make a new association if needed. %u and %j
4743 require one. */
49009afd 4744 if (t == 0 || c == 'u' || c == 'j')
fb266030
TW
4745 {
4746 if (t == 0)
4747 {
e1e4cdc4 4748 t = XNEW (struct temp_name);
fb266030
TW
4749 t->next = temp_names;
4750 temp_names = t;
4751 }
dd75c292 4752 t->length = suffix_length;
2297fdf1
TT
4753 if (saved_suffix)
4754 {
4755 t->suffix = saved_suffix;
4756 saved_suffix = NULL;
4757 }
4758 else
4759 t->suffix = save_string (suffix, suffix_length);
95456d43 4760 t->unique = (c == 'u' || c == 'U' || c == 'j');
dd75c292 4761 temp_filename = make_temp_file (t->suffix);
6aa62cff 4762 temp_filename_length = strlen (temp_filename);
fb266030
TW
4763 t->filename = temp_filename;
4764 t->filename_length = temp_filename_length;
4765 }
4766
a1d9074c
TT
4767 if (saved_suffix)
4768 free (saved_suffix);
4769
fb266030 4770 obstack_grow (&obstack, t->filename, t->filename_length);
b9490a6e 4771 delete_this_arg = 1;
ed1f651b
RS
4772 }
4773 arg_going = 1;
4774 break;
4775
4776 case 'i':
d1bd0ded
GK
4777 if (combine_inputs)
4778 {
5a691e98
RAE
4779 if (at_file_supplied)
4780 {
4781 /* We are going to expand `%i' to `@FILE', where FILE
4782 is a newly-created temporary filename. The filenames
4783 that would usually be expanded in place of %o will be
4784 written to the temporary file. */
4785 char **argv;
4786 int n_files = 0;
4787 int j;
4788
4789 for (i = 0; i < n_infiles; i++)
4790 if (compile_input_file_p (&infiles[i]))
4791 n_files++;
4792
4793 argv = (char **) alloca (sizeof (char *) * (n_files + 1));
4794
4795 /* Copy the strings over. */
4796 for (i = 0, j = 0; i < n_infiles; i++)
4797 if (compile_input_file_p (&infiles[i]))
4798 {
4799 argv[j] = CONST_CAST (char *, infiles[i].name);
4800 infiles[i].compiled = true;
4801 j++;
4802 }
4803 argv[j] = NULL;
4804
4805 create_at_file (argv);
4806 }
4807 else
4808 for (i = 0; (int) i < n_infiles; i++)
4809 if (compile_input_file_p (&infiles[i]))
0855eab7
CT
4810 {
4811 store_arg (infiles[i].name, 0, 0);
4812 infiles[i].compiled = true;
4813 }
d1bd0ded
GK
4814 }
4815 else
4816 {
6afbc885
JM
4817 obstack_grow (&obstack, gcc_input_filename,
4818 input_filename_length);
d1bd0ded
GK
4819 arg_going = 1;
4820 }
ed1f651b
RS
4821 break;
4822
8eebb258 4823 case 'I':
2d879387 4824 {
00dcee0c 4825 struct spec_path_info info;
2d879387 4826
2b6dd222
JM
4827 if (multilib_dir)
4828 {
4829 do_spec_1 ("-imultilib", 1, NULL);
4830 /* Make this a separate argument. */
4831 do_spec_1 (" ", 0, NULL);
4832 do_spec_1 (multilib_dir, 1, NULL);
4833 do_spec_1 (" ", 0, NULL);
4834 }
4835
2d879387
JW
4836 if (gcc_exec_prefix)
4837 {
6496a589 4838 do_spec_1 ("-iprefix", 1, NULL);
2d879387 4839 /* Make this a separate argument. */
6496a589
KG
4840 do_spec_1 (" ", 0, NULL);
4841 do_spec_1 (gcc_exec_prefix, 1, NULL);
4842 do_spec_1 (" ", 0, NULL);
2d879387
JW
4843 }
4844
e7f13528
GP
4845 if (target_system_root_changed ||
4846 (target_system_root && target_sysroot_hdrs_suffix))
047d636f
DJ
4847 {
4848 do_spec_1 ("-isysroot", 1, NULL);
4849 /* Make this a separate argument. */
4850 do_spec_1 (" ", 0, NULL);
4851 do_spec_1 (target_system_root, 1, NULL);
e7f13528
GP
4852 if (target_sysroot_hdrs_suffix)
4853 do_spec_1 (target_sysroot_hdrs_suffix, 1, NULL);
047d636f
DJ
4854 do_spec_1 (" ", 0, NULL);
4855 }
4856
00dcee0c
AM
4857 info.option = "-isystem";
4858 info.append = "include";
4859 info.append_len = strlen (info.append);
4860 info.omit_relative = false;
4861 info.separate_options = true;
4862
4863 for_each_path (&include_prefixes, false, info.append_len,
4864 spec_path, &info);
f686ec05
JM
4865
4866 info.append = "include-fixed";
14da6073
JM
4867 if (*sysroot_hdrs_suffix_spec)
4868 info.append = concat (info.append, dir_separator_str,
4869 multilib_dir, NULL);
f686ec05
JM
4870 info.append_len = strlen (info.append);
4871 for_each_path (&include_prefixes, false, info.append_len,
4872 spec_path, &info);
2d879387 4873 }
8eebb258
RS
4874 break;
4875
ed1f651b 4876 case 'o':
15c5edb9
TT
4877 {
4878 int max = n_infiles;
15c5edb9 4879 max += lang_specific_extra_outfiles;
08dc830e 4880
2091ff66
NF
4881 if (HAVE_GNU_LD && at_file_supplied)
4882 {
4883 /* We are going to expand `%o' to `@FILE', where FILE
4884 is a newly-created temporary filename. The filenames
4885 that would usually be expanded in place of %o will be
4886 written to the temporary file. */
4887
2091ff66 4888 char **argv;
5a691e98 4889 int n_files, j;
2091ff66
NF
4890
4891 /* Convert OUTFILES into a form suitable for writeargv. */
4892
4893 /* Determine how many are non-NULL. */
4894 for (n_files = 0, i = 0; i < max; i++)
4895 n_files += outfiles[i] != NULL;
4896
e1e4cdc4 4897 argv = (char **) alloca (sizeof (char *) * (n_files + 1));
2091ff66
NF
4898
4899 /* Copy the strings over. */
4900 for (i = 0, j = 0; i < max; i++)
4901 if (outfiles[i])
4902 {
b1d5455a 4903 argv[j] = CONST_CAST (char *, outfiles[i]);
2091ff66
NF
4904 j++;
4905 }
4906 argv[j] = NULL;
4907
5a691e98 4908 create_at_file (argv);
2091ff66
NF
4909 }
4910 else
4911 for (i = 0; i < max; i++)
4912 if (outfiles[i])
4913 store_arg (outfiles[i], 0, 0);
15c5edb9
TT
4914 break;
4915 }
ed1f651b 4916
ed7dae04 4917 case 'O':
45936a85 4918 obstack_grow (&obstack, TARGET_OBJECT_SUFFIX, strlen (TARGET_OBJECT_SUFFIX));
ed7dae04
RK
4919 arg_going = 1;
4920 break;
4921
ed1f651b
RS
4922 case 's':
4923 this_is_library_file = 1;
4924 break;
4925
3beb864c
NC
4926 case 'T':
4927 this_is_linker_script = 1;
4928 break;
4929
17211ab5
GK
4930 case 'V':
4931 outfiles[input_file_number] = NULL;
4932 break;
4933
ed1f651b
RS
4934 case 'w':
4935 this_is_output_file = 1;
4936 break;
4937
4938 case 'W':
4939 {
5b634ee0 4940 unsigned int cur_index = VEC_length (const_char_p, argbuf);
ed1f651b
RS
4941 /* Handle the {...} following the %W. */
4942 if (*p != '{')
bdc6b402 4943 fatal_error ("spec %qs has invalid %<%%W%c%>", spec, *p);
ed1f651b
RS
4944 p = handle_braces (p + 1);
4945 if (p == 0)
4946 return -1;
13e7cedb 4947 end_going_arg ();
ed1f651b
RS
4948 /* If any args were output, mark the last one for deletion
4949 on failure. */
5b634ee0
JM
4950 if (VEC_length (const_char_p, argbuf) != cur_index)
4951 record_temp_file (VEC_last (const_char_p, argbuf), 0, 1);
ed1f651b
RS
4952 break;
4953 }
4954
4955 /* %x{OPTION} records OPTION for %X to output. */
4956 case 'x':
4957 {
878f32c3 4958 const char *p1 = p;
ed1f651b 4959 char *string;
69d540bd
NF
4960 char *opt;
4961 unsigned ix;
ed1f651b
RS
4962
4963 /* Skip past the option value and make a copy. */
4964 if (*p != '{')
bdc6b402 4965 fatal_error ("spec %qs has invalid %<%%x%c%>", spec, *p);
ed1f651b
RS
4966 while (*p++ != '}')
4967 ;
4968 string = save_string (p1 + 1, p - p1 - 2);
4969
4970 /* See if we already recorded this option. */
69d540bd
NF
4971 FOR_EACH_VEC_ELT (char_p, linker_options, ix, opt)
4972 if (! strcmp (string, opt))
ed1f651b
RS
4973 {
4974 free (string);
4975 return 0;
4976 }
4977
4978 /* This option is new; add it. */
b8468bc7 4979 add_linker_option (string, strlen (string));
ed1f651b
RS
4980 }
4981 break;
4982
368dfd3a 4983 /* Dump out the options accumulated previously using %x. */
ed1f651b 4984 case 'X':
69d540bd 4985 do_specs_vec (linker_options);
ed1f651b
RS
4986 break;
4987
c9ebacb8
RS
4988 /* Dump out the options accumulated previously using -Wa,. */
4989 case 'Y':
69d540bd 4990 do_specs_vec (assembler_options);
c9ebacb8
RS
4991 break;
4992
57cb9b60
JW
4993 /* Dump out the options accumulated previously using -Wp,. */
4994 case 'Z':
69d540bd 4995 do_specs_vec (preprocessor_options);
57cb9b60
JW
4996 break;
4997
ed1f651b
RS
4998 /* Here are digits and numbers that just process
4999 a certain constant string as a spec. */
5000
5001 case '1':
6496a589 5002 value = do_spec_1 (cc1_spec, 0, NULL);
3279bba6
RS
5003 if (value != 0)
5004 return value;
ed1f651b
RS
5005 break;
5006
5007 case '2':
6496a589 5008 value = do_spec_1 (cc1plus_spec, 0, NULL);
3279bba6
RS
5009 if (value != 0)
5010 return value;
ed1f651b
RS
5011 break;
5012
5013 case 'a':
6496a589 5014 value = do_spec_1 (asm_spec, 0, NULL);
3279bba6
RS
5015 if (value != 0)
5016 return value;
ed1f651b
RS
5017 break;
5018
5019 case 'A':
6496a589 5020 value = do_spec_1 (asm_final_spec, 0, NULL);
3279bba6
RS
5021 if (value != 0)
5022 return value;
ed1f651b
RS
5023 break;
5024
ed1f651b 5025 case 'C':
a9374841 5026 {
83182544 5027 const char *const spec
589005ff
KH
5028 = (input_file_compiler->cpp_spec
5029 ? input_file_compiler->cpp_spec
a9374841 5030 : cpp_spec);
6496a589 5031 value = do_spec_1 (spec, 0, NULL);
a9374841
MM
5032 if (value != 0)
5033 return value;
5034 }
ed1f651b
RS
5035 break;
5036
5037 case 'E':
6496a589 5038 value = do_spec_1 (endfile_spec, 0, NULL);
3279bba6
RS
5039 if (value != 0)
5040 return value;
ed1f651b
RS
5041 break;
5042
5043 case 'l':
6496a589 5044 value = do_spec_1 (link_spec, 0, NULL);
3279bba6
RS
5045 if (value != 0)
5046 return value;
ed1f651b
RS
5047 break;
5048
5049 case 'L':
6496a589 5050 value = do_spec_1 (lib_spec, 0, NULL);
3279bba6
RS
5051 if (value != 0)
5052 return value;
ed1f651b
RS
5053 break;
5054
68d69835 5055 case 'G':
6496a589 5056 value = do_spec_1 (libgcc_spec, 0, NULL);
68d69835
JM
5057 if (value != 0)
5058 return value;
5059 break;
5060
4977bab6
ZW
5061 case 'R':
5062 /* We assume there is a directory
5063 separator at the end of this string. */
5064 if (target_system_root)
1d088dee
AJ
5065 {
5066 obstack_grow (&obstack, target_system_root,
e7f13528
GP
5067 strlen (target_system_root));
5068 if (target_sysroot_suffix)
1d088dee 5069 obstack_grow (&obstack, target_sysroot_suffix,
e7f13528
GP
5070 strlen (target_sysroot_suffix));
5071 }
4977bab6
ZW
5072 break;
5073
ed1f651b 5074 case 'S':
6496a589 5075 value = do_spec_1 (startfile_spec, 0, NULL);
3279bba6
RS
5076 if (value != 0)
5077 return value;
ed1f651b
RS
5078 break;
5079
5080 /* Here we define characters other than letters and digits. */
5081
5082 case '{':
5083 p = handle_braces (p);
5084 if (p == 0)
5085 return -1;
5086 break;
5087
f3226a90
JT
5088 case ':':
5089 p = handle_spec_function (p);
5090 if (p == 0)
5091 return -1;
5092 break;
5093
ed1f651b
RS
5094 case '%':
5095 obstack_1grow (&obstack, '%');
5096 break;
5097
589005ff
KH
5098 case '.':
5099 {
5100 unsigned len = 0;
11972f66 5101
589005ff
KH
5102 while (p[len] && p[len] != ' ' && p[len] != '%')
5103 len++;
5104 suffix_subst = save_string (p - 1, len + 1);
5105 p += len;
5106 }
11972f66 5107 break;
589005ff 5108
4977bab6
ZW
5109 /* Henceforth ignore the option(s) matching the pattern
5110 after the %<. */
5111 case '<':
96f5b137 5112 case '>':
4977bab6
ZW
5113 {
5114 unsigned len = 0;
5115 int have_wildcard = 0;
5116 int i;
96f5b137
L
5117 int switch_option;
5118
5119 if (c == '>')
5120 switch_option = SWITCH_IGNORE | SWITCH_KEEP_FOR_GCC;
5121 else
5122 switch_option = SWITCH_IGNORE;
4977bab6
ZW
5123
5124 while (p[len] && p[len] != ' ' && p[len] != '\t')
5125 len++;
5126
5127 if (p[len-1] == '*')
5128 have_wildcard = 1;
5129
5130 for (i = 0; i < n_switches; i++)
5131 if (!strncmp (switches[i].part1, p, len - have_wildcard)
5132 && (have_wildcard || switches[i].part1[len] == '\0'))
5133 {
96f5b137 5134 switches[i].live_cond |= switch_option;
4977bab6
ZW
5135 switches[i].validated = 1;
5136 }
5137
5138 p += len;
5139 }
5140 break;
5141
ed1f651b 5142 case '*':
3ac63d94
NC
5143 if (soft_matched_part)
5144 {
6496a589
KG
5145 do_spec_1 (soft_matched_part, 1, NULL);
5146 do_spec_1 (" ", 0, NULL);
3ac63d94
NC
5147 }
5148 else
5149 /* Catch the case where a spec string contains something like
454ff5cb 5150 '%{foo:%*}'. i.e. there is no * in the pattern on the left
3ac63d94 5151 hand side of the :. */
bdc6b402 5152 error ("spec failure: %<%%*%> has not been initialized by pattern match");
ed1f651b
RS
5153 break;
5154
5155 /* Process a string found as the value of a spec given by name.
5156 This feature allows individual machine descriptions
4089dfab
JL
5157 to add and use their own specs.
5158 %[...] modifies -D options the way %P does;
5159 %(...) uses the spec unmodified. */
5160 case '[':
2dec80c7 5161 warning (0, "use of obsolete %%[ operator in specs");
ed1f651b 5162 case '(':
ed1f651b 5163 {
878f32c3 5164 const char *name = p;
ed1f651b
RS
5165 struct spec_list *sl;
5166 int len;
5167
5168 /* The string after the S/P is the name of a spec that is to be
0f41302f 5169 processed. */
4089dfab 5170 while (*p && *p != ')' && *p != ']')
ed1f651b
RS
5171 p++;
5172
3ac63d94 5173 /* See if it's in the list. */
ed1f651b 5174 for (len = p - name, sl = specs; sl; sl = sl->next)
79aff5ac 5175 if (sl->name_len == len && !strncmp (sl->name, name, len))
ed1f651b 5176 {
79aff5ac 5177 name = *(sl->ptr_spec);
20df0482 5178#ifdef DEBUG_SPECS
2dec80c7 5179 fnotice (stderr, "Processing spec %c%s%c, which is '%s'\n",
ab87f8c8 5180 c, sl->name, (c == '(') ? ')' : ']', name);
20df0482 5181#endif
ed1f651b
RS
5182 break;
5183 }
5184
5185 if (sl)
5186 {
4089dfab
JL
5187 if (c == '(')
5188 {
6496a589 5189 value = do_spec_1 (name, 0, NULL);
4089dfab
JL
5190 if (value != 0)
5191 return value;
5192 }
5193 else
5194 {
e1e4cdc4 5195 char *x = (char *) alloca (strlen (name) * 2 + 1);
4089dfab 5196 char *buf = x;
878f32c3 5197 const char *y = name;
4089dfab
JL
5198 int flag = 0;
5199
5200 /* Copy all of NAME into BUF, but put __ after
3ac63d94 5201 every -D and at the end of each arg. */
4089dfab
JL
5202 while (1)
5203 {
5204 if (! strncmp (y, "-D", 2))
5205 {
5206 *x++ = '-';
5207 *x++ = 'D';
5208 *x++ = '_';
5209 *x++ = '_';
5210 y += 2;
5211 flag = 1;
5212 continue;
5213 }
d25a45d4
KH
5214 else if (flag
5215 && (*y == ' ' || *y == '\t' || *y == '='
5216 || *y == '}' || *y == 0))
4089dfab
JL
5217 {
5218 *x++ = '_';
5219 *x++ = '_';
5220 flag = 0;
5221 }
d25a45d4 5222 if (*y == 0)
4089dfab
JL
5223 break;
5224 else
5225 *x++ = *y++;
5226 }
5227 *x = 0;
5228
6496a589 5229 value = do_spec_1 (buf, 0, NULL);
4089dfab
JL
5230 if (value != 0)
5231 return value;
5232 }
ed1f651b 5233 }
b3865ca9 5234
4089dfab 5235 /* Discard the closing paren or bracket. */
b3865ca9
RS
5236 if (*p)
5237 p++;
ed1f651b
RS
5238 }
5239 break;
5240
5241 default:
bdc6b402 5242 error ("spec failure: unrecognized spec option %qc", c);
3ac63d94 5243 break;
ed1f651b
RS
5244 }
5245 break;
5246
5247 case '\\':
5248 /* Backslash: treat next character as ordinary. */
5249 c = *p++;
5250
938d968e 5251 /* Fall through. */
ed1f651b
RS
5252 default:
5253 /* Ordinary character: put it into the current argument. */
5254 obstack_1grow (&obstack, c);
5255 arg_going = 1;
5256 }
5257
f3226a90
JT
5258 /* End of string. If we are processing a spec function, we need to
5259 end any pending argument. */
13e7cedb
DH
5260 if (processing_spec_function)
5261 end_going_arg ();
f3226a90 5262
d25a45d4 5263 return 0;
ed1f651b
RS
5264}
5265
f3226a90
JT
5266/* Look up a spec function. */
5267
5268static const struct spec_function *
1d088dee 5269lookup_spec_function (const char *name)
f3226a90 5270{
f3226a90 5271 const struct spec_function *sf;
f3226a90 5272
4c360e1f
RE
5273 for (sf = static_spec_functions; sf->name != NULL; sf++)
5274 if (strcmp (sf->name, name) == 0)
5275 return sf;
f3226a90
JT
5276
5277 return NULL;
5278}
5279
5280/* Evaluate a spec function. */
5281
5282static const char *
1d088dee 5283eval_spec_function (const char *func, const char *args)
f3226a90
JT
5284{
5285 const struct spec_function *sf;
5286 const char *funcval;
5287
5288 /* Saved spec processing context. */
5b634ee0 5289 VEC(const_char_p,heap) *save_argbuf;
f3226a90
JT
5290
5291 int save_arg_going;
5292 int save_delete_this_arg;
5293 int save_this_is_output_file;
5294 int save_this_is_library_file;
5295 int save_input_from_pipe;
3beb864c 5296 int save_this_is_linker_script;
f3226a90
JT
5297 const char *save_suffix_subst;
5298
5299
5300 sf = lookup_spec_function (func);
5301 if (sf == NULL)
bdc6b402 5302 fatal_error ("unknown spec function %qs", func);
f3226a90
JT
5303
5304 /* Push the spec processing context. */
f3226a90
JT
5305 save_argbuf = argbuf;
5306
5307 save_arg_going = arg_going;
5308 save_delete_this_arg = delete_this_arg;
5309 save_this_is_output_file = this_is_output_file;
5310 save_this_is_library_file = this_is_library_file;
3beb864c 5311 save_this_is_linker_script = this_is_linker_script;
f3226a90
JT
5312 save_input_from_pipe = input_from_pipe;
5313 save_suffix_subst = suffix_subst;
5314
5315 /* Create a new spec processing context, and build the function
5316 arguments. */
5317
5318 alloc_args ();
5319 if (do_spec_2 (args) < 0)
bdc6b402 5320 fatal_error ("error in args to spec function %qs", func);
f3226a90
JT
5321
5322 /* argbuf_index is an index for the next argument to be inserted, and
5323 so contains the count of the args already inserted. */
5324
5b634ee0
JM
5325 funcval = (*sf->func) (VEC_length (const_char_p, argbuf),
5326 VEC_address (const_char_p, argbuf));
f3226a90
JT
5327
5328 /* Pop the spec processing context. */
5b634ee0 5329 VEC_free (const_char_p, heap, argbuf);
f3226a90
JT
5330 argbuf = save_argbuf;
5331
5332 arg_going = save_arg_going;
5333 delete_this_arg = save_delete_this_arg;
5334 this_is_output_file = save_this_is_output_file;
5335 this_is_library_file = save_this_is_library_file;
3beb864c 5336 this_is_linker_script = save_this_is_linker_script;
f3226a90
JT
5337 input_from_pipe = save_input_from_pipe;
5338 suffix_subst = save_suffix_subst;
5339
5340 return funcval;
5341}
5342
5343/* Handle a spec function call of the form:
5344
5345 %:function(args)
5346
5347 ARGS is processed as a spec in a separate context and split into an
5348 argument vector in the normal fashion. The function returns a string
5349 containing a spec which we then process in the caller's context, or
5350 NULL if no processing is required. */
5351
5352static const char *
1d088dee 5353handle_spec_function (const char *p)
f3226a90
JT
5354{
5355 char *func, *args;
5356 const char *endp, *funcval;
5357 int count;
5358
5359 processing_spec_function++;
5360
5361 /* Get the function name. */
5362 for (endp = p; *endp != '\0'; endp++)
5363 {
5364 if (*endp == '(') /* ) */
5365 break;
5366 /* Only allow [A-Za-z0-9], -, and _ in function names. */
5367 if (!ISALNUM (*endp) && !(*endp == '-' || *endp == '_'))
2dec80c7 5368 fatal_error ("malformed spec function name");
f3226a90
JT
5369 }
5370 if (*endp != '(') /* ) */
2dec80c7 5371 fatal_error ("no arguments for spec function");
f3226a90
JT
5372 func = save_string (p, endp - p);
5373 p = ++endp;
5374
5375 /* Get the arguments. */
5376 for (count = 0; *endp != '\0'; endp++)
5377 {
5378 /* ( */
5379 if (*endp == ')')
5380 {
5381 if (count == 0)
5382 break;
5383 count--;
5384 }
5385 else if (*endp == '(') /* ) */
5386 count++;
5387 }
5388 /* ( */
5389 if (*endp != ')')
2dec80c7 5390 fatal_error ("malformed spec function arguments");
f3226a90
JT
5391 args = save_string (p, endp - p);
5392 p = ++endp;
5393
5394 /* p now points to just past the end of the spec function expression. */
5395
5396 funcval = eval_spec_function (func, args);
5397 if (funcval != NULL && do_spec_1 (funcval, 0, NULL) < 0)
5398 p = NULL;
5399
5400 free (func);
5401 free (args);
5402
5403 processing_spec_function--;
5404
5405 return p;
5406}
5407
4977bab6
ZW
5408/* Inline subroutine of handle_braces. Returns true if the current
5409 input suffix matches the atom bracketed by ATOM and END_ATOM. */
5410static inline bool
1d088dee 5411input_suffix_matches (const char *atom, const char *end_atom)
4977bab6
ZW
5412{
5413 return (input_suffix
5414 && !strncmp (input_suffix, atom, end_atom - atom)
5415 && input_suffix[end_atom - atom] == '\0');
5416}
ed1f651b 5417
48137d59
GK
5418/* Subroutine of handle_braces. Returns true if the current
5419 input file's spec name matches the atom bracketed by ATOM and END_ATOM. */
5420static bool
5421input_spec_matches (const char *atom, const char *end_atom)
5422{
5423 return (input_file_compiler
5424 && input_file_compiler->suffix
5425 && input_file_compiler->suffix[0] != '\0'
5426 && !strncmp (input_file_compiler->suffix + 1, atom,
5427 end_atom - atom)
5428 && input_file_compiler->suffix[end_atom - atom + 1] == '\0');
5429}
5430
953ff289 5431/* Subroutine of handle_braces. Returns true if a switch
4977bab6
ZW
5432 matching the atom bracketed by ATOM and END_ATOM appeared on the
5433 command line. */
953ff289 5434static bool
1d088dee 5435switch_matches (const char *atom, const char *end_atom, int starred)
ed1f651b 5436{
4977bab6
ZW
5437 int i;
5438 int len = end_atom - atom;
5439 int plen = starred ? len : -1;
ed1f651b 5440
4977bab6
ZW
5441 for (i = 0; i < n_switches; i++)
5442 if (!strncmp (switches[i].part1, atom, len)
5443 && (starred || switches[i].part1[len] == '\0')
5444 && check_live_switch (i, plen))
5445 return true;
8097c429 5446
4977bab6
ZW
5447 return false;
5448}
ed1f651b 5449
4977bab6
ZW
5450/* Inline subroutine of handle_braces. Mark all of the switches which
5451 match ATOM (extends to END_ATOM; STARRED indicates whether there
5452 was a star after the atom) for later processing. */
5453static inline void
1d088dee 5454mark_matching_switches (const char *atom, const char *end_atom, int starred)
4977bab6
ZW
5455{
5456 int i;
5457 int len = end_atom - atom;
5458 int plen = starred ? len : -1;
5459
5460 for (i = 0; i < n_switches; i++)
5461 if (!strncmp (switches[i].part1, atom, len)
5462 && (starred || switches[i].part1[len] == '\0')
5463 && check_live_switch (i, plen))
5464 switches[i].ordering = 1;
5465}
9bf09437 5466
4977bab6
ZW
5467/* Inline subroutine of handle_braces. Process all the currently
5468 marked switches through give_switch, and clear the marks. */
5469static inline void
1d088dee 5470process_marked_switches (void)
4977bab6
ZW
5471{
5472 int i;
ed1f651b 5473
4977bab6
ZW
5474 for (i = 0; i < n_switches; i++)
5475 if (switches[i].ordering == 1)
5476 {
5477 switches[i].ordering = 0;
5478 give_switch (i, 0);
5479 }
5480}
ed1f651b 5481
4977bab6
ZW
5482/* Handle a %{ ... } construct. P points just inside the leading {.
5483 Returns a pointer one past the end of the brace block, or 0
5484 if we call do_spec_1 and that returns -1. */
ed1f651b 5485
4977bab6 5486static const char *
1d088dee 5487handle_braces (const char *p)
4977bab6
ZW
5488{
5489 const char *atom, *end_atom;
5490 const char *d_atom = NULL, *d_end_atom = NULL;
3b5edfee 5491 const char *orig = p;
8097c429 5492
4977bab6 5493 bool a_is_suffix;
48137d59 5494 bool a_is_spectype;
4977bab6
ZW
5495 bool a_is_starred;
5496 bool a_is_negated;
5497 bool a_matched;
9bf09437 5498
4977bab6
ZW
5499 bool a_must_be_last = false;
5500 bool ordered_set = false;
5501 bool disjunct_set = false;
5502 bool disj_matched = false;
5503 bool disj_starred = true;
5504 bool n_way_choice = false;
5505 bool n_way_matched = false;
5506
5507#define SKIP_WHITE() do { while (*p == ' ' || *p == '\t') p++; } while (0)
9bf09437 5508
4977bab6 5509 do
ed1f651b 5510 {
4977bab6 5511 if (a_must_be_last)
3b5edfee 5512 goto invalid;
9bf09437 5513
4977bab6 5514 /* Scan one "atom" (S in the description above of %{}, possibly
48137d59
GK
5515 with '!', '.', '@', ',', or '*' modifiers). */
5516 a_matched = false;
5517 a_is_suffix = false;
5518 a_is_starred = false;
5519 a_is_negated = false;
5520 a_is_spectype = false;
9218435e 5521
4977bab6
ZW
5522 SKIP_WHITE();
5523 if (*p == '!')
5524 p++, a_is_negated = true;
ed1f651b 5525
4977bab6
ZW
5526 SKIP_WHITE();
5527 if (*p == '.')
5528 p++, a_is_suffix = true;
48137d59
GK
5529 else if (*p == ',')
5530 p++, a_is_spectype = true;
ed1f651b 5531
4977bab6
ZW
5532 atom = p;
5533 while (ISIDNUM(*p) || *p == '-' || *p == '+' || *p == '='
cde26509 5534 || *p == ',' || *p == '.' || *p == '@')
4977bab6
ZW
5535 p++;
5536 end_atom = p;
ed1f651b 5537
4977bab6
ZW
5538 if (*p == '*')
5539 p++, a_is_starred = 1;
196a37f4 5540
4977bab6 5541 SKIP_WHITE();
3b5edfee 5542 switch (*p)
4977bab6 5543 {
3b5edfee 5544 case '&': case '}':
4977bab6
ZW
5545 /* Substitute the switch(es) indicated by the current atom. */
5546 ordered_set = true;
5547 if (disjunct_set || n_way_choice || a_is_negated || a_is_suffix
48137d59 5548 || a_is_spectype || atom == end_atom)
3b5edfee 5549 goto invalid;
ed1f651b 5550
4977bab6
ZW
5551 mark_matching_switches (atom, end_atom, a_is_starred);
5552
5553 if (*p == '}')
5554 process_marked_switches ();
3b5edfee
NS
5555 break;
5556
5557 case '|': case ':':
4977bab6
ZW
5558 /* Substitute some text if the current atom appears as a switch
5559 or suffix. */
5560 disjunct_set = true;
5561 if (ordered_set)
3b5edfee 5562 goto invalid;
ed1f651b 5563
4977bab6 5564 if (atom == end_atom)
ed1f651b 5565 {
4977bab6 5566 if (!n_way_choice || disj_matched || *p == '|'
b8698a0f 5567 || a_is_negated || a_is_suffix || a_is_spectype
48137d59 5568 || a_is_starred)
3b5edfee 5569 goto invalid;
4977bab6
ZW
5570
5571 /* An empty term may appear as the last choice of an
5572 N-way choice set; it means "otherwise". */
5573 a_must_be_last = true;
5574 disj_matched = !n_way_matched;
5575 disj_starred = false;
ed1f651b 5576 }
4977bab6 5577 else
ed1f651b 5578 {
48137d59
GK
5579 if ((a_is_suffix || a_is_spectype) && a_is_starred)
5580 goto invalid;
3beb864c 5581
48137d59
GK
5582 if (!a_is_starred)
5583 disj_starred = false;
5584
5585 /* Don't bother testing this atom if we already have a
5586 match. */
5587 if (!disj_matched && !n_way_matched)
5588 {
5589 if (a_is_suffix)
5590 a_matched = input_suffix_matches (atom, end_atom);
5591 else if (a_is_spectype)
5592 a_matched = input_spec_matches (atom, end_atom);
5593 else
5594 a_matched = switch_matches (atom, end_atom, a_is_starred);
3beb864c 5595
48137d59
GK
5596 if (a_matched != a_is_negated)
5597 {
5598 disj_matched = true;
5599 d_atom = atom;
5600 d_end_atom = end_atom;
5601 }
5602 }
ed1f651b 5603 }
ed1f651b 5604
4977bab6 5605 if (*p == ':')
ed1f651b 5606 {
4977bab6
ZW
5607 /* Found the body, that is, the text to substitute if the
5608 current disjunction matches. */
5609 p = process_brace_body (p + 1, d_atom, d_end_atom, disj_starred,
5610 disj_matched && !n_way_matched);
5611 if (p == 0)
5612 return 0;
ed1f651b 5613
4977bab6
ZW
5614 /* If we have an N-way choice, reset state for the next
5615 disjunction. */
5616 if (*p == ';')
ed1f651b 5617 {
4977bab6
ZW
5618 n_way_choice = true;
5619 n_way_matched |= disj_matched;
5620 disj_matched = false;
5621 disj_starred = true;
5622 d_atom = d_end_atom = NULL;
ed1f651b
RS
5623 }
5624 }
3b5edfee
NS
5625 break;
5626
5627 default:
5628 goto invalid;
ed1f651b 5629 }
4977bab6
ZW
5630 }
5631 while (*p++ != '}');
ed1f651b 5632
4977bab6 5633 return p;
7904f95f 5634
3b5edfee 5635 invalid:
bdc6b402 5636 fatal_error ("braced spec %qs is invalid at %qc", orig, *p);
7904f95f 5637
4977bab6
ZW
5638#undef SKIP_WHITE
5639}
5640
5641/* Subroutine of handle_braces. Scan and process a brace substitution body
5642 (X in the description of %{} syntax). P points one past the colon;
5643 ATOM and END_ATOM bracket the first atom which was found to be true
5644 (present) in the current disjunction; STARRED indicates whether all
5645 the atoms in the current disjunction were starred (for syntax validation);
5646 MATCHED indicates whether the disjunction matched or not, and therefore
5647 whether or not the body is to be processed through do_spec_1 or just
5648 skipped. Returns a pointer to the closing } or ;, or 0 if do_spec_1
5649 returns -1. */
5650
5651static const char *
1d088dee
AJ
5652process_brace_body (const char *p, const char *atom, const char *end_atom,
5653 int starred, int matched)
4977bab6
ZW
5654{
5655 const char *body, *end_body;
5656 unsigned int nesting_level;
5657 bool have_subst = false;
5658
5659 /* Locate the closing } or ;, honoring nested braces.
5660 Trim trailing whitespace. */
5661 body = p;
5662 nesting_level = 1;
5663 for (;;)
5664 {
5665 if (*p == '{')
5666 nesting_level++;
5667 else if (*p == '}')
ed1f651b 5668 {
4977bab6
ZW
5669 if (!--nesting_level)
5670 break;
ed1f651b 5671 }
4977bab6
ZW
5672 else if (*p == ';' && nesting_level == 1)
5673 break;
5674 else if (*p == '%' && p[1] == '*' && nesting_level == 1)
5675 have_subst = true;
5676 else if (*p == '\0')
3b5edfee 5677 goto invalid;
4977bab6 5678 p++;
ed1f651b 5679 }
1d088dee 5680
4977bab6
ZW
5681 end_body = p;
5682 while (end_body[-1] == ' ' || end_body[-1] == '\t')
5683 end_body--;
ed1f651b 5684
4977bab6 5685 if (have_subst && !starred)
3b5edfee 5686 goto invalid;
9bf09437 5687
4977bab6 5688 if (matched)
196a37f4 5689 {
4977bab6
ZW
5690 /* Copy the substitution body to permanent storage and execute it.
5691 If have_subst is false, this is a simple matter of running the
5692 body through do_spec_1... */
5693 char *string = save_string (body, end_body - body);
5694 if (!have_subst)
5695 {
5696 if (do_spec_1 (string, 0, NULL) < 0)
5697 return 0;
5698 }
5699 else
5700 {
5701 /* ... but if have_subst is true, we have to process the
5702 body once for each matching switch, with %* set to the
5703 variant part of the switch. */
5704 unsigned int hard_match_len = end_atom - atom;
5705 int i;
196a37f4 5706
4977bab6
ZW
5707 for (i = 0; i < n_switches; i++)
5708 if (!strncmp (switches[i].part1, atom, hard_match_len)
5709 && check_live_switch (i, hard_match_len))
5710 {
5711 if (do_spec_1 (string, 0,
5712 &switches[i].part1[hard_match_len]) < 0)
5713 return 0;
5714 /* Pass any arguments this switch has. */
5715 give_switch (i, 1);
5716 suffix_subst = NULL;
5717 }
5718 }
10ebf5fe
NB
5719 }
5720
4977bab6 5721 return p;
3b5edfee
NS
5722
5723 invalid:
bdc6b402 5724 fatal_error ("braced spec body %qs is invalid", body);
ed1f651b 5725}
f5b0eb4e 5726\f
6c396fb5
RK
5727/* Return 0 iff switch number SWITCHNUM is obsoleted by a later switch
5728 on the command line. PREFIX_LENGTH is the length of XXX in an {XXX*}
5729 spec, or -1 if either exact match or %* is used.
f5b0eb4e
RK
5730
5731 A -O switch is obsoleted by a later -O switch. A -f, -m, or -W switch
5732 whose value does not begin with "no-" is obsoleted by the same value
5733 with the "no-", similarly for a switch with the "no-" prefix. */
5734
5735static int
1d088dee 5736check_live_switch (int switchnum, int prefix_length)
f5b0eb4e 5737{
878f32c3 5738 const char *name = switches[switchnum].part1;
f5b0eb4e
RK
5739 int i;
5740
2153915d
AO
5741 /* If we already processed this switch and determined if it was
5742 live or not, return our past determination. */
5743 if (switches[switchnum].live_cond != 0)
5744 return ((switches[switchnum].live_cond & SWITCH_LIVE) != 0
5745 && (switches[switchnum].live_cond & SWITCH_FALSE) == 0
94b90527
JJ
5746 && (switches[switchnum].live_cond & SWITCH_IGNORE_PERMANENTLY)
5747 == 0);
2153915d 5748
6c396fb5 5749 /* In the common case of {<at-most-one-letter>*}, a negating
f5b0eb4e
RK
5750 switch would always match, so ignore that case. We will just
5751 send the conflicting switches to the compiler phase. */
6c396fb5 5752 if (prefix_length >= 0 && prefix_length <= 1)
f5b0eb4e
RK
5753 return 1;
5754
f5b0eb4e
RK
5755 /* Now search for duplicate in a manner that depends on the name. */
5756 switch (*name)
5757 {
5758 case 'O':
d25a45d4
KH
5759 for (i = switchnum + 1; i < n_switches; i++)
5760 if (switches[i].part1[0] == 'O')
5761 {
5762 switches[switchnum].validated = 1;
5763 switches[switchnum].live_cond = SWITCH_FALSE;
5764 return 0;
5765 }
f5b0eb4e 5766 break;
ed1f651b 5767
f5b0eb4e 5768 case 'W': case 'f': case 'm':
6c396fb5 5769 if (! strncmp (name + 1, "no-", 3))
f5b0eb4e 5770 {
0f41302f 5771 /* We have Xno-YYY, search for XYYY. */
f5b0eb4e
RK
5772 for (i = switchnum + 1; i < n_switches; i++)
5773 if (switches[i].part1[0] == name[0]
5774 && ! strcmp (&switches[i].part1[1], &name[4]))
d25a45d4
KH
5775 {
5776 switches[switchnum].validated = 1;
5777 switches[switchnum].live_cond = SWITCH_FALSE;
5778 return 0;
5779 }
f5b0eb4e
RK
5780 }
5781 else
5782 {
5783 /* We have XYYY, search for Xno-YYY. */
5784 for (i = switchnum + 1; i < n_switches; i++)
5785 if (switches[i].part1[0] == name[0]
5786 && switches[i].part1[1] == 'n'
5787 && switches[i].part1[2] == 'o'
5788 && switches[i].part1[3] == '-'
5789 && !strcmp (&switches[i].part1[4], &name[1]))
d25a45d4
KH
5790 {
5791 switches[switchnum].validated = 1;
5792 switches[switchnum].live_cond = SWITCH_FALSE;
5793 return 0;
5794 }
f5b0eb4e
RK
5795 }
5796 break;
5797 }
5798
5799 /* Otherwise the switch is live. */
3371362c 5800 switches[switchnum].live_cond |= SWITCH_LIVE;
f5b0eb4e
RK
5801 return 1;
5802}
5803\f
ed1f651b
RS
5804/* Pass a switch to the current accumulating command
5805 in the same form that we received it.
5806 SWITCHNUM identifies the switch; it is an index into
5807 the vector of switches gcc received, which is `switches'.
5808 This cannot fail since it never finishes a command line.
5809
4977bab6 5810 If OMIT_FIRST_WORD is nonzero, then we omit .part1 of the argument. */
ed1f651b
RS
5811
5812static void
1d088dee 5813give_switch (int switchnum, int omit_first_word)
ed1f651b 5814{
3371362c 5815 if ((switches[switchnum].live_cond & SWITCH_IGNORE) != 0)
8097c429
TT
5816 return;
5817
ed1f651b
RS
5818 if (!omit_first_word)
5819 {
6496a589
KG
5820 do_spec_1 ("-", 0, NULL);
5821 do_spec_1 (switches[switchnum].part1, 1, NULL);
ed1f651b 5822 }
1ba9a487 5823
ed1f651b
RS
5824 if (switches[switchnum].args != 0)
5825 {
fbd40359 5826 const char **p;
ed1f651b
RS
5827 for (p = switches[switchnum].args; *p; p++)
5828 {
11972f66
NS
5829 const char *arg = *p;
5830
4977bab6 5831 do_spec_1 (" ", 0, NULL);
11972f66
NS
5832 if (suffix_subst)
5833 {
5834 unsigned length = strlen (arg);
e65677af 5835 int dot = 0;
11972f66
NS
5836
5837 while (length-- && !IS_DIR_SEPARATOR (arg[length]))
5838 if (arg[length] == '.')
5839 {
b1d5455a 5840 (CONST_CAST(char *, arg))[length] = 0;
e65677af 5841 dot = 1;
11972f66
NS
5842 break;
5843 }
6496a589 5844 do_spec_1 (arg, 1, NULL);
e65677af 5845 if (dot)
b1d5455a 5846 (CONST_CAST(char *, arg))[length] = '.';
e65677af 5847 do_spec_1 (suffix_subst, 1, NULL);
11972f66
NS
5848 }
5849 else
6496a589 5850 do_spec_1 (arg, 1, NULL);
ed1f651b
RS
5851 }
5852 }
1ba9a487 5853
6496a589 5854 do_spec_1 (" ", 0, NULL);
ab87f8c8 5855 switches[switchnum].validated = 1;
ed1f651b
RS
5856}
5857\f
5858/* Search for a file named NAME trying various prefixes including the
5859 user's -B prefix and some standard ones.
5860 Return the absolute file name found. If nothing is found, return NAME. */
5861
878f32c3 5862static const char *
1d088dee 5863find_file (const char *name)
ed1f651b 5864{
00dcee0c 5865 char *newname = find_a_file (&startfile_prefixes, name, R_OK, true);
ed1f651b
RS
5866 return newname ? newname : name;
5867}
5868
0ad5835e 5869/* Determine whether a directory exists. If LINKER, return 0 for
00dcee0c 5870 certain fixed names not needed by the linker. */
ed1f651b
RS
5871
5872static int
00dcee0c 5873is_directory (const char *path1, bool linker)
ed1f651b 5874{
00dcee0c
AM
5875 int len1;
5876 char *path;
ed1f651b
RS
5877 char *cp;
5878 struct stat st;
5879
00dcee0c
AM
5880 /* Ensure the string ends with "/.". The resulting path will be a
5881 directory even if the given path is a symbolic link. */
5882 len1 = strlen (path1);
e1e4cdc4 5883 path = (char *) alloca (3 + len1);
7e2231e7 5884 memcpy (path, path1, len1);
00dcee0c 5885 cp = path + len1;
509781a4 5886 if (!IS_DIR_SEPARATOR (cp[-1]))
48ff801b 5887 *cp++ = DIR_SEPARATOR;
ed1f651b
RS
5888 *cp++ = '.';
5889 *cp = '\0';
5890
5891 /* Exclude directories that the linker is known to search. */
0ad5835e 5892 if (linker
00dcee0c 5893 && IS_DIR_SEPARATOR (path[0])
48ff801b 5894 && ((cp - path == 6
00dcee0c 5895 && strncmp (path + 1, "lib", 3) == 0)
48ff801b 5896 || (cp - path == 10
00dcee0c
AM
5897 && strncmp (path + 1, "usr", 3) == 0
5898 && IS_DIR_SEPARATOR (path[4])
5899 && strncmp (path + 5, "lib", 3) == 0)))
ed1f651b
RS
5900 return 0;
5901
5902 return (stat (path, &st) >= 0 && S_ISDIR (st.st_mode));
5903}
512b62fb
JM
5904
5905/* Set up the various global variables to indicate that we're processing
5906 the input file named FILENAME. */
5907
b856c15d 5908void
1d088dee 5909set_input (const char *filename)
512b62fb 5910{
b3694847 5911 const char *p;
512b62fb 5912
6afbc885
JM
5913 gcc_input_filename = filename;
5914 input_filename_length = strlen (gcc_input_filename);
5915 input_basename = lbasename (gcc_input_filename);
512b62fb
JM
5916
5917 /* Find a suffix starting with the last period,
5918 and set basename_length to exclude that suffix. */
5919 basename_length = strlen (input_basename);
ea414c97 5920 suffixed_basename_length = basename_length;
512b62fb 5921 p = input_basename + basename_length;
d25a45d4
KH
5922 while (p != input_basename && *p != '.')
5923 --p;
512b62fb
JM
5924 if (*p == '.' && p != input_basename)
5925 {
5926 basename_length = p - input_basename;
5927 input_suffix = p + 1;
5928 }
5929 else
5930 input_suffix = "";
589005ff 5931
99f78cdd 5932 /* If a spec for 'g', 'u', or 'U' is seen with -save-temps then
6afbc885 5933 we will need to do a stat on the gcc_input_filename. The
99f78cdd
IR
5934 INPUT_STAT_SET signals that the stat is needed. */
5935 input_stat_set = 0;
512b62fb 5936}
ed1f651b
RS
5937\f
5938/* On fatal signals, delete all the temporary files. */
5939
5940static void
2dec80c7 5941fatal_signal (int signum)
ed1f651b
RS
5942{
5943 signal (signum, SIG_DFL);
5944 delete_failure_queue ();
5945 delete_temp_files ();
5946 /* Get the same signal again, this time not handled,
5947 so its normal effect occurs. */
5948 kill (getpid (), signum);
5949}
5950
2153915d
AO
5951/* Compare the contents of the two files named CMPFILE[0] and
5952 CMPFILE[1]. Return zero if they're identical, nonzero
5953 otherwise. */
5954
5955static int
5956compare_files (char *cmpfile[])
5957{
5958 int ret = 0;
5959 FILE *temp[2] = { NULL, NULL };
5960 int i;
5961
5962#if HAVE_MMAP_FILE
5963 {
5964 size_t length[2];
5965 void *map[2] = { NULL, NULL };
5966
5967 for (i = 0; i < 2; i++)
5968 {
5969 struct stat st;
5970
5971 if (stat (cmpfile[i], &st) < 0 || !S_ISREG (st.st_mode))
5972 {
5973 error ("%s: could not determine length of compare-debug file %s",
6afbc885 5974 gcc_input_filename, cmpfile[i]);
2153915d
AO
5975 ret = 1;
5976 break;
5977 }
5978
5979 length[i] = st.st_size;
5980 }
5981
5982 if (!ret && length[0] != length[1])
5983 {
6afbc885 5984 error ("%s: -fcompare-debug failure (length)", gcc_input_filename);
2153915d
AO
5985 ret = 1;
5986 }
5987
5988 if (!ret)
5989 for (i = 0; i < 2; i++)
5990 {
5991 int fd = open (cmpfile[i], O_RDONLY);
5992 if (fd < 0)
5993 {
5994 error ("%s: could not open compare-debug file %s",
6afbc885 5995 gcc_input_filename, cmpfile[i]);
2153915d
AO
5996 ret = 1;
5997 break;
5998 }
5999
6000 map[i] = mmap (NULL, length[i], PROT_READ, MAP_PRIVATE, fd, 0);
6001 close (fd);
6002
6003 if (map[i] == (void *) MAP_FAILED)
6004 {
6005 ret = -1;
6006 break;
6007 }
6008 }
6009
6010 if (!ret)
6011 {
6012 if (memcmp (map[0], map[1], length[0]) != 0)
6013 {
6afbc885 6014 error ("%s: -fcompare-debug failure", gcc_input_filename);
2153915d
AO
6015 ret = 1;
6016 }
6017 }
6018
6019 for (i = 0; i < 2; i++)
6020 if (map[i])
e7aae3e8 6021 munmap ((caddr_t) map[i], length[i]);
2153915d
AO
6022
6023 if (ret >= 0)
6024 return ret;
6025
6026 ret = 0;
6027 }
6028#endif
6029
6030 for (i = 0; i < 2; i++)
6031 {
6032 temp[i] = fopen (cmpfile[i], "r");
6033 if (!temp[i])
6034 {
6035 error ("%s: could not open compare-debug file %s",
6afbc885 6036 gcc_input_filename, cmpfile[i]);
2153915d
AO
6037 ret = 1;
6038 break;
6039 }
6040 }
6041
6042 if (!ret && temp[0] && temp[1])
6043 for (;;)
6044 {
6045 int c0, c1;
6046 c0 = fgetc (temp[0]);
6047 c1 = fgetc (temp[1]);
6048
6049 if (c0 != c1)
6050 {
6051 error ("%s: -fcompare-debug failure",
6afbc885 6052 gcc_input_filename);
2153915d
AO
6053 ret = 1;
6054 break;
6055 }
6056
6057 if (c0 == EOF)
6058 break;
6059 }
6060
6061 for (i = 1; i >= 0; i--)
6062 {
6063 if (temp[i])
6064 fclose (temp[i]);
6065 }
6066
6067 return ret;
6068}
6069
07a3df01 6070extern int main (int, char **);
a8f227e7 6071
ed1f651b 6072int
07a3df01 6073main (int argc, char **argv)
ed1f651b 6074{
ea414c97 6075 size_t i;
ed1f651b 6076 int value;
ed1f651b 6077 int linker_was_run = 0;
0855eab7 6078 int lang_n_infiles = 0;
17211ab5 6079 int num_linker_inputs = 0;
ed1f651b
RS
6080 char *explicit_link_files;
6081 char *specs_file;
de9a793e 6082 char *lto_wrapper_file;
878f32c3 6083 const char *p;
d9ac3a07 6084 struct user_specs *uptr;
2091ff66 6085 char **old_argv = argv;
60cf253a
JM
6086 struct cl_decoded_option *decoded_options;
6087 unsigned int decoded_options_count;
ed1f651b 6088
b481444e
JJ
6089 /* Initialize here, not in definition. The IRIX 6 O32 cc sometimes chokes
6090 on ?: in file-scope variable initializations. */
6091 asm_debug = ASM_DEBUG_SPEC;
6092
afcd8a02 6093 p = argv[0] + strlen (argv[0]);
509781a4
ME
6094 while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
6095 --p;
6afbc885 6096 progname = p;
ed1f651b 6097
6afbc885 6098 xmalloc_set_program_name (progname);
e1a132c6 6099
9d530538
MM
6100 expandargv (&argc, &argv);
6101
2091ff66
NF
6102 /* Determine if any expansions were made. */
6103 if (argv != old_argv)
6104 at_file_supplied = true;
6105
a75bfaa6
JM
6106 global_options = global_options_init;
6107
60cf253a
JM
6108 decode_cmdline_options_to_array (argc, CONST_CAST2 (const char **, char **,
6109 argv),
6110 CL_DRIVER,
6111 &decoded_options, &decoded_options_count);
14c7833c 6112
93284395 6113#ifdef GCC_DRIVER_HOST_INITIALIZATION
ff7cc307 6114 /* Perform host dependent initialization when needed. */
93284395
ME
6115 GCC_DRIVER_HOST_INITIALIZATION;
6116#endif
6117
98a3dad4 6118 /* Unlock the stdio streams. */
2653bb0c 6119 unlock_std_streams ();
98a3dad4 6120
191bf464 6121 gcc_init_libintl ();
ab87f8c8 6122
6afbc885
JM
6123 diagnostic_initialize (global_dc, 0);
6124 if (atexit (delete_temp_files) != 0)
6125 fatal_error ("atexit failed");
6126
ed1f651b 6127 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
2dec80c7 6128 signal (SIGINT, fatal_signal);
2a353d3a 6129#ifdef SIGHUP
ed1f651b 6130 if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
2dec80c7 6131 signal (SIGHUP, fatal_signal);
2a353d3a 6132#endif
ed1f651b 6133 if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
2dec80c7 6134 signal (SIGTERM, fatal_signal);
ed1f651b
RS
6135#ifdef SIGPIPE
6136 if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
2dec80c7 6137 signal (SIGPIPE, fatal_signal);
ed1f651b 6138#endif
798bdf70 6139#ifdef SIGCHLD
0bf679a3
BK
6140 /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
6141 receive the signal. A different setting is inheritable */
6142 signal (SIGCHLD, SIG_DFL);
798bdf70 6143#endif
ed1f651b 6144
f3226a90
JT
6145 /* Allocate the argument vector. */
6146 alloc_args ();
ed1f651b
RS
6147
6148 obstack_init (&obstack);
6149
961b7009
MM
6150 /* Build multilib_select, et. al from the separate lines that make up each
6151 multilib selection. */
ffd86336 6152 {
3b304f5b 6153 const char *const *q = multilib_raw;
961b7009 6154 int need_space;
ffd86336
JW
6155
6156 obstack_init (&multilib_obstack);
0f41302f 6157 while ((p = *q++) != (char *) 0)
ffd86336
JW
6158 obstack_grow (&multilib_obstack, p, strlen (p));
6159
6160 obstack_1grow (&multilib_obstack, 0);
7973fd2a 6161 multilib_select = XOBFINISH (&multilib_obstack, const char *);
961b7009
MM
6162
6163 q = multilib_matches_raw;
6164 while ((p = *q++) != (char *) 0)
6165 obstack_grow (&multilib_obstack, p, strlen (p));
6166
6167 obstack_1grow (&multilib_obstack, 0);
7973fd2a 6168 multilib_matches = XOBFINISH (&multilib_obstack, const char *);
961b7009 6169
0a8d6618
BC
6170 q = multilib_exclusions_raw;
6171 while ((p = *q++) != (char *) 0)
d25a45d4 6172 obstack_grow (&multilib_obstack, p, strlen (p));
0a8d6618
BC
6173
6174 obstack_1grow (&multilib_obstack, 0);
7973fd2a 6175 multilib_exclusions = XOBFINISH (&multilib_obstack, const char *);
9218435e 6176
961b7009 6177 need_space = FALSE;
b6a1cbae 6178 for (i = 0; i < ARRAY_SIZE (multilib_defaults_raw); i++)
961b7009
MM
6179 {
6180 if (need_space)
6181 obstack_1grow (&multilib_obstack, ' ');
6182 obstack_grow (&multilib_obstack,
6183 multilib_defaults_raw[i],
6184 strlen (multilib_defaults_raw[i]));
6185 need_space = TRUE;
6186 }
6187
6188 obstack_1grow (&multilib_obstack, 0);
7973fd2a 6189 multilib_defaults = XOBFINISH (&multilib_obstack, const char *);
ffd86336
JW
6190 }
6191
8faf4a68
JW
6192#ifdef INIT_ENVIRONMENT
6193 /* Set up any other necessary machine specific environment variables. */
47d33318 6194 xputenv (INIT_ENVIRONMENT);
8faf4a68
JW
6195#endif
6196
ed1f651b
RS
6197 /* Make a table of what switches there are (switches, n_switches).
6198 Make a table of specified input files (infiles, n_infiles).
6199 Decode switches that are handled locally. */
6200
60cf253a 6201 process_command (decoded_options_count, decoded_options);
ed1f651b
RS
6202
6203 /* Initialize the vector of specs to just the default.
6204 This means one element containing 0s, as a terminator. */
6205
e1e4cdc4 6206 compilers = XNEWVAR (struct compiler, sizeof default_compilers);
703ad42b 6207 memcpy (compilers, default_compilers, sizeof default_compilers);
ed1f651b
RS
6208 n_compilers = n_default_compilers;
6209
6210 /* Read specs from a file if there is one. */
6211
6aa62cff 6212 machine_suffix = concat (spec_machine, dir_separator_str,
d4f2852f
KG
6213 spec_version, dir_separator_str, NULL);
6214 just_machine_suffix = concat (spec_machine, dir_separator_str, NULL);
ed1f651b 6215
00dcee0c 6216 specs_file = find_a_file (&startfile_prefixes, "specs", R_OK, true);
ed1f651b
RS
6217 /* Read the specs file unless it is a default one. */
6218 if (specs_file != 0 && strcmp (specs_file, "specs"))
20df0482
MM
6219 read_specs (specs_file, TRUE);
6220 else
6221 init_spec ();
841faeed 6222
5bb67e36 6223 /* We need to check standard_exec_prefix/just_machine_suffix/specs
3ac63d94 6224 for any override of as, ld and libraries. */
e1e4cdc4 6225 specs_file = (char *) alloca (strlen (standard_exec_prefix)
703ad42b 6226 + strlen (just_machine_suffix) + sizeof ("specs"));
5bb67e36
RK
6227
6228 strcpy (specs_file, standard_exec_prefix);
6229 strcat (specs_file, just_machine_suffix);
6230 strcat (specs_file, "specs");
6231 if (access (specs_file, R_OK) == 0)
6232 read_specs (specs_file, TRUE);
9218435e 6233
7816bea0
DJ
6234 /* Process any configure-time defaults specified for the command line
6235 options, via OPTION_DEFAULT_SPECS. */
6236 for (i = 0; i < ARRAY_SIZE (option_default_specs); i++)
6237 do_option_spec (option_default_specs[i].name,
6238 option_default_specs[i].spec);
6239
3bd6d4c4
AO
6240 /* Process DRIVER_SELF_SPECS, adding any new options to the end
6241 of the command line. */
6242
6243 for (i = 0; i < ARRAY_SIZE (driver_self_specs); i++)
6244 do_self_spec (driver_self_specs[i]);
6245
2153915d
AO
6246 if (compare_debug)
6247 {
6248 enum save_temps save;
6249
6250 if (!compare_debug_second)
6251 {
6252 n_switches_debug_check[1] = n_switches;
efe5e5a0 6253 n_switches_alloc_debug_check[1] = n_switches_alloc;
2153915d 6254 switches_debug_check[1] = XDUPVEC (struct switchstr, switches,
efe5e5a0 6255 n_switches_alloc);
2153915d
AO
6256
6257 do_self_spec ("%:compare-debug-self-opt()");
6258 n_switches_debug_check[0] = n_switches;
efe5e5a0 6259 n_switches_alloc_debug_check[0] = n_switches_alloc;
2153915d
AO
6260 switches_debug_check[0] = switches;
6261
6262 n_switches = n_switches_debug_check[1];
efe5e5a0 6263 n_switches_alloc = n_switches_alloc_debug_check[1];
2153915d
AO
6264 switches = switches_debug_check[1];
6265 }
6266
6267 /* Avoid crash when computing %j in this early. */
6268 save = save_temps_flag;
6269 save_temps_flag = SAVE_TEMPS_NONE;
6270
6271 compare_debug = -compare_debug;
6272 do_self_spec ("%:compare-debug-self-opt()");
6273
6274 save_temps_flag = save;
6275
6276 if (!compare_debug_second)
6277 {
6278 n_switches_debug_check[1] = n_switches;
efe5e5a0 6279 n_switches_alloc_debug_check[1] = n_switches_alloc;
2153915d
AO
6280 switches_debug_check[1] = switches;
6281 compare_debug = -compare_debug;
6282 n_switches = n_switches_debug_check[0];
efe5e5a0 6283 n_switches_alloc = n_switches_debug_check[0];
2153915d
AO
6284 switches = switches_debug_check[0];
6285 }
6286 }
6287
4977bab6
ZW
6288 /* If not cross-compiling, look for executables in the standard
6289 places. */
6290 if (*cross_compile == '0')
004fd4d5 6291 {
2296d164
RK
6292 if (*md_exec_prefix)
6293 {
6294 add_prefix (&exec_prefixes, md_exec_prefix, "GCC",
1a5d37a1 6295 PREFIX_PRIORITY_LAST, 0, 0);
2296d164 6296 }
4977bab6
ZW
6297 }
6298
71c0e7fc 6299 /* Process sysroot_suffix_spec. */
e7f13528
GP
6300 if (*sysroot_suffix_spec != 0
6301 && do_spec_2 (sysroot_suffix_spec) == 0)
6302 {
5b634ee0 6303 if (VEC_length (const_char_p, argbuf) > 1)
ab532386 6304 error ("spec failure: more than one arg to SYSROOT_SUFFIX_SPEC");
5b634ee0
JM
6305 else if (VEC_length (const_char_p, argbuf) == 1)
6306 target_sysroot_suffix = xstrdup (VEC_last (const_char_p, argbuf));
e7f13528
GP
6307 }
6308
380e5ca4
MM
6309#ifdef HAVE_LD_SYSROOT
6310 /* Pass the --sysroot option to the linker, if it supports that. If
6311 there is a sysroot_suffix_spec, it has already been processed by
6312 this point, so target_system_root really is the system root we
6313 should be using. */
6314 if (target_system_root)
6315 {
6316 obstack_grow (&obstack, "%(sysroot_spec) ", strlen ("%(sysroot_spec) "));
6317 obstack_grow0 (&obstack, link_spec, strlen (link_spec));
7973fd2a 6318 set_spec ("link", XOBFINISH (&obstack, const char *));
380e5ca4
MM
6319 }
6320#endif
6321
71c0e7fc 6322 /* Process sysroot_hdrs_suffix_spec. */
e7f13528
GP
6323 if (*sysroot_hdrs_suffix_spec != 0
6324 && do_spec_2 (sysroot_hdrs_suffix_spec) == 0)
6325 {
5b634ee0 6326 if (VEC_length (const_char_p, argbuf) > 1)
ab532386 6327 error ("spec failure: more than one arg to SYSROOT_HEADERS_SUFFIX_SPEC");
5b634ee0
JM
6328 else if (VEC_length (const_char_p, argbuf) == 1)
6329 target_sysroot_hdrs_suffix = xstrdup (VEC_last (const_char_p, argbuf));
e7f13528
GP
6330 }
6331
4977bab6
ZW
6332 /* Look for startfiles in the standard places. */
6333 if (*startfile_prefix_spec != 0
6334 && do_spec_2 (startfile_prefix_spec) == 0
6335 && do_spec_1 (" ", 0, NULL) == 0)
6336 {
5b634ee0 6337 const char *arg;
4977bab6 6338 int ndx;
5b634ee0
JM
6339 FOR_EACH_VEC_ELT (const_char_p, argbuf, ndx, arg)
6340 add_sysrooted_prefix (&startfile_prefixes, arg, "BINUTILS",
1a5d37a1 6341 PREFIX_PRIORITY_LAST, 0, 1);
4977bab6
ZW
6342 }
6343 /* We should eventually get rid of all these and stick to
6344 startfile_prefix_spec exclusively. */
6345 else if (*cross_compile == '0' || target_system_root)
6346 {
2296d164 6347 if (*md_startfile_prefix)
4977bab6 6348 add_sysrooted_prefix (&startfile_prefixes, md_startfile_prefix,
1a5d37a1 6349 "GCC", PREFIX_PRIORITY_LAST, 0, 1);
004fd4d5 6350
2296d164 6351 if (*md_startfile_prefix_1)
4977bab6 6352 add_sysrooted_prefix (&startfile_prefixes, md_startfile_prefix_1,
1a5d37a1 6353 "GCC", PREFIX_PRIORITY_LAST, 0, 1);
607a4f7d 6354
4dbc7773
ILT
6355 /* If standard_startfile_prefix is relative, base it on
6356 standard_exec_prefix. This lets us move the installed tree
6357 as a unit. If GCC_EXEC_PREFIX is defined, base
0d037580
DJ
6358 standard_startfile_prefix on that as well.
6359
6360 If the prefix is relative, only search it for native compilers;
6361 otherwise we will search a directory containing host libraries. */
3dce1408 6362 if (IS_ABSOLUTE_PATH (standard_startfile_prefix))
4977bab6
ZW
6363 add_sysrooted_prefix (&startfile_prefixes,
6364 standard_startfile_prefix, "BINUTILS",
1a5d37a1 6365 PREFIX_PRIORITY_LAST, 0, 1);
0d037580 6366 else if (*cross_compile == '0')
4dbc7773 6367 {
48ff801b 6368 add_prefix (&startfile_prefixes,
b8698a0f
L
6369 concat (gcc_exec_prefix
6370 ? gcc_exec_prefix : standard_exec_prefix,
6371 machine_suffix,
d4f2852f 6372 standard_startfile_prefix, NULL),
1a5d37a1 6373 NULL, PREFIX_PRIORITY_LAST, 0, 1);
9218435e 6374 }
4dbc7773 6375
f4c0a303
CD
6376 /* Sysrooted prefixes are relocated because target_system_root is
6377 also relocated by gcc_exec_prefix. */
656c7a3a
AL
6378 if (*standard_startfile_prefix_1)
6379 add_sysrooted_prefix (&startfile_prefixes,
6380 standard_startfile_prefix_1, "BINUTILS",
1a5d37a1 6381 PREFIX_PRIORITY_LAST, 0, 1);
656c7a3a
AL
6382 if (*standard_startfile_prefix_2)
6383 add_sysrooted_prefix (&startfile_prefixes,
6384 standard_startfile_prefix_2, "BINUTILS",
1a5d37a1 6385 PREFIX_PRIORITY_LAST, 0, 1);
004fd4d5 6386 }
343f59d9 6387
8607048d
TT
6388 /* Process any user specified specs in the order given on the command
6389 line. */
6390 for (uptr = user_specs_head; uptr; uptr = uptr->next)
6391 {
5bbcd587 6392 char *filename = find_a_file (&startfile_prefixes, uptr->filename,
00dcee0c 6393 R_OK, true);
8607048d
TT
6394 read_specs (filename ? filename : uptr->filename, FALSE);
6395 }
6396
e8601ecb
JW
6397 /* If we have a GCC_EXEC_PREFIX envvar, modify it for cpp's sake. */
6398 if (gcc_exec_prefix)
cb6edbcb
KG
6399 gcc_exec_prefix = concat (gcc_exec_prefix, spec_machine, dir_separator_str,
6400 spec_version, dir_separator_str, NULL);
004fd4d5 6401
ed1f651b
RS
6402 /* Now we have the specs.
6403 Set the `valid' bits for switches that match anything in any spec. */
6404
6405 validate_all_switches ();
6406
60103a34
DE
6407 /* Now that we have the switches and the specs, set
6408 the subdirectory based on the options. */
6409 set_multilib_dir ();
6410
d7f09764 6411 /* Set up to remember the pathname of gcc and any options
6afbc885 6412 needed for collect. We use argv[0] instead of progname because
d7f09764
DN
6413 we need the complete pathname. */
6414 obstack_init (&collect_obstack);
6415 obstack_grow (&collect_obstack, "COLLECT_GCC=", sizeof ("COLLECT_GCC=") - 1);
6416 obstack_grow (&collect_obstack, argv[0], strlen (argv[0]) + 1);
6417 xputenv (XOBFINISH (&collect_obstack, char *));
6418
6419 /* Set up to remember the pathname of the lto wrapper. */
6420
de9a793e
DG
6421 lto_wrapper_file = find_a_file (&exec_prefixes, "lto-wrapper", X_OK, false);
6422 if (lto_wrapper_file)
d7f09764 6423 {
de9a793e 6424 lto_wrapper_spec = lto_wrapper_file;
d7f09764
DN
6425 obstack_init (&collect_obstack);
6426 obstack_grow (&collect_obstack, "COLLECT_LTO_WRAPPER=",
6427 sizeof ("COLLECT_LTO_WRAPPER=") - 1);
6428 obstack_grow (&collect_obstack, lto_wrapper_spec,
6429 strlen (lto_wrapper_spec) + 1);
6430 xputenv (XOBFINISH (&collect_obstack, char *));
6431 }
6432
ed1f651b
RS
6433 /* Warn about any switches that no pass was interested in. */
6434
d25a45d4 6435 for (i = 0; (int) i < n_switches; i++)
ab87f8c8 6436 if (! switches[i].validated)
bdc6b402 6437 error ("unrecognized option %<-%s%>", switches[i].part1);
ed1f651b 6438
6a9e290e
RK
6439 /* Obey some of the options. */
6440
2628b9d3
DE
6441 if (print_search_dirs)
6442 {
f4c0a303
CD
6443 printf (_("install: %s%s\n"),
6444 gcc_exec_prefix ? gcc_exec_prefix : standard_exec_prefix,
6445 gcc_exec_prefix ? "" : machine_suffix);
00dcee0c
AM
6446 printf (_("programs: %s\n"),
6447 build_search_list (&exec_prefixes, "", false, false));
6448 printf (_("libraries: %s\n"),
6449 build_search_list (&startfile_prefixes, "", false, true));
9257393c 6450 return (0);
2628b9d3
DE
6451 }
6452
6a9e290e 6453 if (print_file_name)
2dcb563f 6454 {
6a9e290e 6455 printf ("%s\n", find_file (print_file_name));
9257393c 6456 return (0);
2dcb563f
RS
6457 }
6458
6a9e290e
RK
6459 if (print_prog_name)
6460 {
5bbcd587 6461 char *newname = find_a_file (&exec_prefixes, print_prog_name, X_OK, 0);
6a9e290e 6462 printf ("%s\n", (newname ? newname : print_prog_name));
9257393c 6463 return (0);
6a9e290e 6464 }
ed1f651b 6465
60103a34
DE
6466 if (print_multi_lib)
6467 {
6468 print_multilib_info ();
9257393c 6469 return (0);
60103a34
DE
6470 }
6471
6472 if (print_multi_directory)
6473 {
6474 if (multilib_dir == NULL)
6475 printf (".\n");
6476 else
6477 printf ("%s\n", multilib_dir);
9257393c 6478 return (0);
60103a34
DE
6479 }
6480
3def1397
VP
6481 if (print_sysroot)
6482 {
6483 if (target_system_root)
6484 {
6485 if (target_sysroot_suffix)
6486 printf ("%s%s\n", target_system_root, target_sysroot_suffix);
6487 else
6488 printf ("%s\n", target_system_root);
6489 }
6490 return (0);
6491 }
6492
5bbcd587
JJ
6493 if (print_multi_os_directory)
6494 {
6495 if (multilib_os_dir == NULL)
6496 printf (".\n");
6497 else
6498 printf ("%s\n", multilib_os_dir);
6499 return (0);
6500 }
6501
14da6073
JM
6502 if (print_sysroot_headers_suffix)
6503 {
6504 if (*sysroot_hdrs_suffix_spec)
6505 {
dc5bbad3
JM
6506 printf("%s\n", (target_sysroot_hdrs_suffix
6507 ? target_sysroot_hdrs_suffix
6508 : ""));
14da6073
JM
6509 return (0);
6510 }
6511 else
6512 /* The error status indicates that only one set of fixed
6513 headers should be built. */
2dec80c7 6514 fatal_error ("not configured with sysroot headers suffix");
14da6073
JM
6515 }
6516
b8468bc7
NC
6517 if (print_help_list)
6518 {
6519 display_help ();
6520
6521 if (! verbose_flag)
6522 {
5e4adfba 6523 printf (_("\nFor bug reporting instructions, please see:\n"));
a976603e 6524 printf ("%s.\n", bug_report_url);
9218435e 6525
9257393c 6526 return (0);
b8468bc7
NC
6527 }
6528
6529 /* We do not exit here. Instead we have created a fake input file
6530 called 'help-dummy' which needs to be compiled, and we pass this
033505fc
RW
6531 on the various sub-processes, along with the --help switch.
6532 Ensure their output appears after ours. */
6533 fputc ('\n', stdout);
6534 fflush (stdout);
b8468bc7 6535 }
9218435e 6536
41fd0f9b
RAE
6537 if (print_version)
6538 {
6afbc885 6539 printf (_("%s %s%s\n"), progname, pkgversion_string,
41fd0f9b 6540 version_string);
9f8e43c0 6541 printf ("Copyright %s 2011 Free Software Foundation, Inc.\n",
41fd0f9b
RAE
6542 _("(C)"));
6543 fputs (_("This is free software; see the source for copying conditions. There is NO\n\
6544warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n"),
6545 stdout);
6546 if (! verbose_flag)
6547 return 0;
6548
6549 /* We do not exit here. We use the same mechanism of --help to print
6550 the version of the sub-processes. */
6551 fputc ('\n', stdout);
6552 fflush (stdout);
6553 }
6554
ed1f651b
RS
6555 if (verbose_flag)
6556 {
7ad9ff7a 6557 int n;
008355a6 6558 const char *thrmod;
7ad9ff7a 6559
2dec80c7
JM
6560 fnotice (stderr, "Target: %s\n", spec_machine);
6561 fnotice (stderr, "Configured with: %s\n", configuration_arguments);
f5fa9a5b 6562
008355a6
AO
6563#ifdef THREAD_MODEL_SPEC
6564 /* We could have defined THREAD_MODEL_SPEC to "%*" by default,
6565 but there's no point in doing all this processing just to get
6566 thread_model back. */
6567 obstack_init (&obstack);
6568 do_spec_1 (THREAD_MODEL_SPEC, 0, thread_model);
6569 obstack_1grow (&obstack, '\0');
7973fd2a 6570 thrmod = XOBFINISH (&obstack, const char *);
008355a6
AO
6571#else
6572 thrmod = thread_model;
6573#endif
6574
2dec80c7 6575 fnotice (stderr, "Thread model: %s\n", thrmod);
a6687d2b 6576
7ad9ff7a
DE
6577 /* compiler_version is truncated at the first space when initialized
6578 from version string, so truncate version_string at the first space
6579 before comparing. */
6580 for (n = 0; version_string[n]; n++)
6581 if (version_string[n] == ' ')
6582 break;
6583
6584 if (! strncmp (version_string, compiler_version, n)
6585 && compiler_version[n] == 0)
2dec80c7
JM
6586 fnotice (stderr, "gcc version %s %s\n", version_string,
6587 pkgversion_string);
9c4faac1 6588 else
2dec80c7
JM
6589 fnotice (stderr, "gcc driver version %s %sexecuting gcc version %s\n",
6590 version_string, pkgversion_string, compiler_version);
9c4faac1 6591
ed1f651b 6592 if (n_infiles == 0)
9257393c 6593 return (0);
ed1f651b
RS
6594 }
6595
a2a05b0a 6596 if (n_infiles == added_libraries)
2dec80c7 6597 fatal_error ("no input files");
ed1f651b
RS
6598
6599 /* Make a place to record the compiler output file names
6600 that correspond to the input files. */
6601
f271358e 6602 i = n_infiles;
e37cda9b 6603 i += lang_specific_extra_outfiles;
5ed6ace5 6604 outfiles = XCNEWVEC (const char *, i);
ed1f651b
RS
6605
6606 /* Record which files were specified explicitly as link input. */
6607
d900f77d 6608 explicit_link_files = XCNEWVEC (char, n_infiles);
ed1f651b 6609
56b721c5 6610 combine_inputs = have_o || flag_wpa;
0855eab7
CT
6611
6612 for (i = 0; (int) i < n_infiles; i++)
d1bd0ded 6613 {
0855eab7 6614 const char *name = infiles[i].name;
7904f95f
EC
6615 struct compiler *compiler = lookup_compiler (name,
6616 strlen (name),
0855eab7 6617 infiles[i].language);
7904f95f 6618
0855eab7
CT
6619 if (compiler && !(compiler->combinable))
6620 combine_inputs = false;
7904f95f 6621
0855eab7
CT
6622 if (lang_n_infiles > 0 && compiler != input_file_compiler
6623 && infiles[i].language && infiles[i].language[0] != '*')
6624 infiles[i].incompiler = compiler;
6625 else if (compiler)
6626 {
6627 lang_n_infiles++;
6628 input_file_compiler = compiler;
6629 infiles[i].incompiler = compiler;
6630 }
6631 else
6632 {
6633 /* Since there is no compiler for this input file, assume it is a
9cf737f8 6634 linker file. */
0855eab7
CT
6635 explicit_link_files[i] = 1;
6636 infiles[i].incompiler = NULL;
6637 }
6638 infiles[i].compiled = false;
6639 infiles[i].preprocessed = false;
d1bd0ded 6640 }
953ff289 6641
de19a50e 6642 if (!combine_inputs && have_c && have_o && lang_n_infiles > 1)
c3224d6f 6643 fatal_error ("cannot specify -o with -c, -S or -E with multiple files");
0855eab7
CT
6644
6645 for (i = 0; (int) i < n_infiles; i++)
ed1f651b 6646 {
ed1f651b
RS
6647 int this_file_error = 0;
6648
6649 /* Tell do_spec what to substitute for %i. */
6650
ed1f651b 6651 input_file_number = i;
512b62fb 6652 set_input (infiles[i].name);
ed1f651b 6653
0855eab7
CT
6654 if (infiles[i].compiled)
6655 continue;
6656
ed1f651b
RS
6657 /* Use the same thing in %o, unless cp->spec says otherwise. */
6658
6afbc885 6659 outfiles[i] = gcc_input_filename;
ed1f651b
RS
6660
6661 /* Figure out which compiler from the file's suffix. */
6662
c3224d6f
RG
6663 input_file_compiler
6664 = lookup_compiler (infiles[i].name, input_filename_length,
6665 infiles[i].language);
589005ff 6666
a9374841 6667 if (input_file_compiler)
ed1f651b
RS
6668 {
6669 /* Ok, we found an applicable compiler. Run its spec. */
ed1f651b 6670
a9374841 6671 if (input_file_compiler->spec[0] == '#')
d644be7b
ZW
6672 {
6673 error ("%s: %s compiler not installed on this system",
6afbc885 6674 gcc_input_filename, &input_file_compiler->spec[1]);
d644be7b
ZW
6675 this_file_error = 1;
6676 }
6677 else
6678 {
2153915d
AO
6679 if (compare_debug)
6680 {
6681 if (debug_check_temp_file[0])
6682 free (debug_check_temp_file[0]);
6683 debug_check_temp_file[0] = NULL;
6684
6685 if (debug_check_temp_file[1])
6686 free (debug_check_temp_file[1]);
6687 debug_check_temp_file[1] = NULL;
6688 }
6689
d644be7b 6690 value = do_spec (input_file_compiler->spec);
0855eab7 6691 infiles[i].compiled = true;
d644be7b 6692 if (value < 0)
b21292d0 6693 this_file_error = 1;
2153915d
AO
6694 else if (compare_debug && debug_check_temp_file[0])
6695 {
6696 if (verbose_flag)
2dec80c7 6697 inform (0, "recompiling with -fcompare-debug");
2153915d
AO
6698
6699 compare_debug = -compare_debug;
6700 n_switches = n_switches_debug_check[1];
efe5e5a0 6701 n_switches_alloc = n_switches_alloc_debug_check[1];
2153915d
AO
6702 switches = switches_debug_check[1];
6703
6704 value = do_spec (input_file_compiler->spec);
6705
6706 compare_debug = -compare_debug;
6707 n_switches = n_switches_debug_check[0];
efe5e5a0 6708 n_switches_alloc = n_switches_alloc_debug_check[0];
2153915d
AO
6709 switches = switches_debug_check[0];
6710
6711 if (value < 0)
6712 {
6713 error ("during -fcompare-debug recompilation");
6714 this_file_error = 1;
6715 }
6716
6717 gcc_assert (debug_check_temp_file[1]
6718 && strcmp (debug_check_temp_file[0],
6719 debug_check_temp_file[1]));
6720
6721 if (verbose_flag)
2dec80c7 6722 inform (0, "comparing final insns dumps");
2153915d
AO
6723
6724 if (compare_files (debug_check_temp_file))
6725 this_file_error = 1;
6726 }
6727
6728 if (compare_debug)
6729 {
6730 if (debug_check_temp_file[0])
6731 free (debug_check_temp_file[0]);
6732 debug_check_temp_file[0] = NULL;
6733
6734 if (debug_check_temp_file[1])
6735 free (debug_check_temp_file[1]);
6736 debug_check_temp_file[1] = NULL;
6737 }
d644be7b 6738 }
ed1f651b
RS
6739 }
6740
6741 /* If this file's name does not contain a recognized suffix,
6742 record it as explicit linker input. */
6743
6744 else
6745 explicit_link_files[i] = 1;
6746
6747 /* Clear the delete-on-failure queue, deleting the files in it
6748 if this compilation failed. */
6749
6750 if (this_file_error)
6751 {
6752 delete_failure_queue ();
6afbc885 6753 errorcount++;
ed1f651b
RS
6754 }
6755 /* If this compilation succeeded, don't delete those files later. */
6756 clear_failure_queue ();
6757 }
6758
817a8255
EC
6759 /* Reset the input file name to the first compile/object file name, for use
6760 with %b in LINK_SPEC. We use the first input file that we can find
7904f95f 6761 a compiler to compile it instead of using infiles.language since for
817a8255 6762 languages other than C we use aliases that we then lookup later. */
512b62fb 6763 if (n_infiles > 0)
817a8255
EC
6764 {
6765 int i;
6766
6767 for (i = 0; i < n_infiles ; i++)
42113d6b
RG
6768 if (infiles[i].incompiler
6769 || (infiles[i].language && infiles[i].language[0] != '*'))
817a8255
EC
6770 {
6771 set_input (infiles[i].name);
6772 break;
6773 }
6774 }
512b62fb 6775
6afbc885 6776 if (!seen_error ())
15c5edb9
TT
6777 {
6778 /* Make sure INPUT_FILE_NUMBER points to first available open
6779 slot. */
6780 input_file_number = n_infiles;
6781 if (lang_specific_pre_link ())
6afbc885 6782 errorcount++;
15c5edb9 6783 }
f271358e 6784
17211ab5
GK
6785 /* Determine if there are any linker input files. */
6786 num_linker_inputs = 0;
6787 for (i = 0; (int) i < n_infiles; i++)
6788 if (explicit_link_files[i] || outfiles[i] != NULL)
6789 num_linker_inputs++;
6790
ed1f651b
RS
6791 /* Run ld to link all the compiler output files. */
6792
6afbc885 6793 if (num_linker_inputs > 0 && !seen_error () && print_subprocess_help < 2)
ed1f651b
RS
6794 {
6795 int tmp = execution_count;
96bdf9b4
JH
6796#ifdef HAVE_LTO_PLUGIN
6797 const char *fno_use_linker_plugin = "fno-use-linker-plugin";
6798#else
da18ea94 6799 const char *fuse_linker_plugin = "fuse-linker-plugin";
96bdf9b4 6800#endif
b3865ca9 6801
9218435e 6802 /* We'll use ld if we can't find collect2. */
10da1131
BM
6803 if (! strcmp (linker_name_spec, "collect2"))
6804 {
00dcee0c 6805 char *s = find_a_file (&exec_prefixes, "collect2", X_OK, false);
10da1131
BM
6806 if (s == NULL)
6807 linker_name_spec = "ld";
6808 }
d7f09764 6809
96bdf9b4
JH
6810#ifdef HAVE_LTO_PLUGIN
6811 if (!switch_matches (fno_use_linker_plugin,
6812 fno_use_linker_plugin + strlen (fno_use_linker_plugin), 0))
6813#else
da18ea94
RAE
6814 if (switch_matches (fuse_linker_plugin,
6815 fuse_linker_plugin + strlen (fuse_linker_plugin), 0))
96bdf9b4 6816#endif
d7f09764
DN
6817 {
6818 linker_plugin_file_spec = find_a_file (&exec_prefixes,
1cd0b716 6819 LTOPLUGINSONAME, R_OK,
d7f09764
DN
6820 false);
6821 if (!linker_plugin_file_spec)
1cd0b716 6822 fatal_error ("-fuse-linker-plugin, but " LTOPLUGINSONAME " not found");
d7f09764
DN
6823 }
6824 lto_gcc_spec = argv[0];
6825
b3865ca9
RS
6826 /* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables
6827 for collect. */
00dcee0c
AM
6828 putenv_from_prefixes (&exec_prefixes, "COMPILER_PATH", false);
6829 putenv_from_prefixes (&startfile_prefixes, LIBRARY_PATH_ENV, true);
b3865ca9 6830
a0f87454
RS
6831 if (print_subprocess_help == 1)
6832 {
6833 printf (_("\nLinker options\n==============\n\n"));
6834 printf (_("Use \"-Wl,OPTION\" to pass \"OPTION\""
6835 " to the linker.\n\n"));
6836 fflush (stdout);
6837 }
ed1f651b
RS
6838 value = do_spec (link_command_spec);
6839 if (value < 0)
6afbc885 6840 errorcount = 1;
ed1f651b
RS
6841 linker_was_run = (tmp != execution_count);
6842 }
6843
ed1f651b
RS
6844 /* If options said don't run linker,
6845 complain about input files to be given to the linker. */
6846
6afbc885 6847 if (! linker_was_run && !seen_error ())
d25a45d4 6848 for (i = 0; (int) i < n_infiles; i++)
01e4dd0d
KH
6849 if (explicit_link_files[i]
6850 && !(infiles[i].language && infiles[i].language[0] == '*'))
2dec80c7
JM
6851 warning (0, "%s: linker input file unused because linking not done",
6852 outfiles[i]);
ed1f651b
RS
6853
6854 /* Delete some or all of the temporary files we made. */
6855
6afbc885 6856 if (seen_error ())
ed1f651b
RS
6857 delete_failure_queue ();
6858 delete_temp_files ();
6859
b8468bc7
NC
6860 if (print_help_list)
6861 {
5e4adfba 6862 printf (("\nFor bug reporting instructions, please see:\n"));
a976603e 6863 printf ("%s\n", bug_report_url);
b8468bc7 6864 }
9218435e 6865
14a774a9 6866 return (signal_count != 0 ? 2
6afbc885 6867 : seen_error () ? (pass_exit_codes ? greatest_status : 1)
14a774a9 6868 : 0);
ed1f651b
RS
6869}
6870
6871/* Find the proper compilation spec for the file name NAME,
004fd4d5 6872 whose length is LENGTH. LANGUAGE is the specified language,
e5e809f4 6873 or 0 if this file is to be passed to the linker. */
ed1f651b
RS
6874
6875static struct compiler *
1d088dee 6876lookup_compiler (const char *name, size_t length, const char *language)
ed1f651b
RS
6877{
6878 struct compiler *cp;
6879
3ac63d94 6880 /* If this was specified by the user to be a linker input, indicate that. */
e5e809f4
JL
6881 if (language != 0 && language[0] == '*')
6882 return 0;
6883
6884 /* Otherwise, look for the language, if one is spec'd. */
ed1f651b
RS
6885 if (language != 0)
6886 {
6887 for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
e5e809f4
JL
6888 if (cp->suffix[0] == '@' && !strcmp (cp->suffix + 1, language))
6889 return cp;
6890
ed1f651b 6891 error ("language %s not recognized", language);
e5e809f4 6892 return 0;
ed1f651b
RS
6893 }
6894
6895 /* Look for a suffix. */
6896 for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
6897 {
4cf3301c
RS
6898 if (/* The suffix `-' matches only the file name `-'. */
6899 (!strcmp (cp->suffix, "-") && !strcmp (name, "-"))
e5e809f4
JL
6900 || (strlen (cp->suffix) < length
6901 /* See if the suffix matches the end of NAME. */
e5e809f4
JL
6902 && !strcmp (cp->suffix,
6903 name + length - strlen (cp->suffix))
e5e809f4 6904 ))
589005ff 6905 break;
03bf1c28 6906 }
e5e809f4 6907
03bf1c28 6908#if defined (OS2) ||defined (HAVE_DOS_BASED_FILE_SYSTEM)
a1105617 6909 /* Look again, but case-insensitively this time. */
03bf1c28
MK
6910 if (cp < compilers)
6911 for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
6912 {
6913 if (/* The suffix `-' matches only the file name `-'. */
6914 (!strcmp (cp->suffix, "-") && !strcmp (name, "-"))
6915 || (strlen (cp->suffix) < length
6916 /* See if the suffix matches the end of NAME. */
6917 && ((!strcmp (cp->suffix,
6918 name + length - strlen (cp->suffix))
6919 || !strpbrk (cp->suffix, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
6920 && !strcasecmp (cp->suffix,
6921 name + length - strlen (cp->suffix)))
6922 ))
6923 break;
6924 }
6925#endif
6926
03bf1c28
MK
6927 if (cp >= compilers)
6928 {
ea414c97
ZW
6929 if (cp->spec[0] != '@')
6930 /* A non-alias entry: return it. */
6931 return cp;
9218435e 6932
ea414c97
ZW
6933 /* An alias entry maps a suffix to a language.
6934 Search for the language; pass 0 for NAME and LENGTH
6935 to avoid infinite recursion if language not found. */
6496a589 6936 return lookup_compiler (NULL, 0, cp->spec + 1);
ed1f651b 6937 }
ed1f651b
RS
6938 return 0;
6939}
6940\f
ed1f651b 6941static char *
1d088dee 6942save_string (const char *s, int len)
ed1f651b 6943{
5ed6ace5 6944 char *result = XNEWVEC (char, len + 1);
ed1f651b 6945
da61dec9 6946 memcpy (result, s, len);
ed1f651b
RS
6947 result[len] = 0;
6948 return result;
6949}
6950
d991c721 6951void
1d088dee 6952pfatal_with_name (const char *name)
ed1f651b 6953{
ab87f8c8
JL
6954 perror_with_name (name);
6955 delete_temp_files ();
6956 exit (1);
ed1f651b
RS
6957}
6958
6959static void
1d088dee 6960perror_with_name (const char *name)
ed1f651b 6961{
bdc6b402 6962 error ("%s: %m", name);
ed1f651b 6963}
ed1f651b 6964\f
4977bab6 6965static inline void
1d088dee 6966validate_switches_from_spec (const char *spec)
4977bab6
ZW
6967{
6968 const char *p = spec;
6969 char c;
6970 while ((c = *p++))
6971 if (c == '%' && (*p == '{' || *p == '<' || (*p == 'W' && *++p == '{')))
6972 /* We have a switch spec. */
6973 p = validate_switches (p + 1);
6974}
6975
ed1f651b 6976static void
1d088dee 6977validate_all_switches (void)
ed1f651b
RS
6978{
6979 struct compiler *comp;
b3865ca9 6980 struct spec_list *spec;
ed1f651b 6981
ea414c97 6982 for (comp = compilers; comp->spec; comp++)
4977bab6 6983 validate_switches_from_spec (comp->spec);
ed1f651b 6984
3ac63d94 6985 /* Look through the linked list of specs read from the specs file. */
d25a45d4 6986 for (spec = specs; spec; spec = spec->next)
4977bab6 6987 validate_switches_from_spec (*spec->ptr_spec);
b3865ca9 6988
4977bab6 6989 validate_switches_from_spec (link_command_spec);
ed1f651b
RS
6990}
6991
6992/* Look at the switch-name that comes after START
6993 and mark as valid all supplied switches that match it. */
6994
4977bab6 6995static const char *
1d088dee 6996validate_switches (const char *start)
ed1f651b 6997{
b3694847 6998 const char *p = start;
4977bab6
ZW
6999 const char *atom;
7000 size_t len;
b3694847 7001 int i;
4977bab6
ZW
7002 bool suffix = false;
7003 bool starred = false;
1d088dee 7004
4977bab6 7005#define SKIP_WHITE() do { while (*p == ' ' || *p == '\t') p++; } while (0)
1d088dee 7006
10ebf5fe 7007next_member:
4977bab6
ZW
7008 SKIP_WHITE ();
7009
ed1f651b 7010 if (*p == '!')
4977bab6 7011 p++;
ed1f651b 7012
4977bab6 7013 SKIP_WHITE ();
48137d59 7014 if (*p == '.' || *p == ',')
4977bab6 7015 suffix = true, p++;
ed1f651b 7016
4977bab6
ZW
7017 atom = p;
7018 while (ISIDNUM (*p) || *p == '-' || *p == '+' || *p == '='
7a975113 7019 || *p == ',' || *p == '.' || *p == '@')
d25a45d4 7020 p++;
4977bab6 7021 len = p - atom;
ed1f651b 7022
4977bab6
ZW
7023 if (*p == '*')
7024 starred = true, p++;
7025
7026 SKIP_WHITE ();
7027
7028 if (!suffix)
ed1f651b
RS
7029 {
7030 /* Mark all matching switches as valid. */
ed1f651b 7031 for (i = 0; i < n_switches; i++)
4977bab6
ZW
7032 if (!strncmp (switches[i].part1, atom, len)
7033 && (starred || switches[i].part1[len] == 0))
ab87f8c8 7034 switches[i].validated = 1;
ed1f651b 7035 }
4977bab6 7036
5a0ba8c9
LJR
7037 if (*p) p++;
7038 if (*p && (p[-1] == '|' || p[-1] == '&'))
4977bab6
ZW
7039 goto next_member;
7040
5a0ba8c9 7041 if (*p && p[-1] == ':')
ed1f651b 7042 {
4977bab6 7043 while (*p && *p != ';' && *p != '}')
ed1f651b 7044 {
4977bab6
ZW
7045 if (*p == '%')
7046 {
7047 p++;
7048 if (*p == '{' || *p == '<')
7049 p = validate_switches (p+1);
7050 else if (p[0] == 'W' && p[1] == '{')
7051 p = validate_switches (p+2);
7052 }
e17aafd1
GK
7053 else
7054 p++;
ed1f651b 7055 }
4977bab6 7056
5a0ba8c9
LJR
7057 if (*p) p++;
7058 if (*p && p[-1] == ';')
4977bab6 7059 goto next_member;
ed1f651b 7060 }
10ebf5fe 7061
4977bab6
ZW
7062 return p;
7063#undef SKIP_WHITE
ed1f651b 7064}
60103a34 7065\f
5bbcd587
JJ
7066struct mdswitchstr
7067{
7068 const char *str;
7069 int len;
7070};
7071
7072static struct mdswitchstr *mdswitches;
7073static int n_mdswitches;
7074
961b7009 7075/* Check whether a particular argument was used. The first time we
956d6950 7076 canonicalize the switches to keep only the ones we care about. */
60103a34
DE
7077
7078static int
1d088dee 7079used_arg (const char *p, int len)
60103a34 7080{
3ac63d94
NC
7081 struct mswitchstr
7082 {
3b304f5b
ZW
7083 const char *str;
7084 const char *replace;
961b7009
MM
7085 int len;
7086 int rep_len;
7087 };
7088
7089 static struct mswitchstr *mswitches;
7090 static int n_mswitches;
7091 int i, j;
7092
7093 if (!mswitches)
7094 {
7095 struct mswitchstr *matches;
3b304f5b 7096 const char *q;
c8c2dcdc 7097 int cnt = 0;
961b7009 7098
d25a45d4
KH
7099 /* Break multilib_matches into the component strings of string
7100 and replacement string. */
1a0bdd29
RK
7101 for (q = multilib_matches; *q != '\0'; q++)
7102 if (*q == ';')
961b7009
MM
7103 cnt++;
7104
e1e4cdc4
KG
7105 matches
7106 = (struct mswitchstr *) alloca ((sizeof (struct mswitchstr)) * cnt);
961b7009
MM
7107 i = 0;
7108 q = multilib_matches;
7109 while (*q != '\0')
7110 {
7111 matches[i].str = q;
7112 while (*q != ' ')
7113 {
7114 if (*q == '\0')
3b5edfee
NS
7115 {
7116 invalid_matches:
bdc6b402 7117 fatal_error ("multilib spec %qs is invalid",
2dec80c7 7118 multilib_matches);
3b5edfee 7119 }
961b7009
MM
7120 q++;
7121 }
961b7009 7122 matches[i].len = q - matches[i].str;
60103a34 7123
961b7009
MM
7124 matches[i].replace = ++q;
7125 while (*q != ';' && *q != '\0')
7126 {
7127 if (*q == ' ')
3b5edfee 7128 goto invalid_matches;
961b7009
MM
7129 q++;
7130 }
7131 matches[i].rep_len = q - matches[i].replace;
7132 i++;
83a0c799
ZW
7133 if (*q == ';')
7134 q++;
961b7009 7135 }
60103a34 7136
71591a1d
JW
7137 /* Now build a list of the replacement string for switches that we care
7138 about. Make sure we allocate at least one entry. This prevents
7139 xmalloc from calling fatal, and prevents us from re-executing this
7140 block of code. */
7141 mswitches
5ed6ace5 7142 = XNEWVEC (struct mswitchstr, n_mdswitches + (n_switches ? n_switches : 1));
961b7009 7143 for (i = 0; i < n_switches; i++)
3371362c 7144 if ((switches[i].live_cond & SWITCH_IGNORE) == 0)
f645e2bd
RS
7145 {
7146 int xlen = strlen (switches[i].part1);
7147 for (j = 0; j < cnt; j++)
7148 if (xlen == matches[j].len
7149 && ! strncmp (switches[i].part1, matches[j].str, xlen))
7150 {
7151 mswitches[n_mswitches].str = matches[j].replace;
7152 mswitches[n_mswitches].len = matches[j].rep_len;
7153 mswitches[n_mswitches].replace = (char *) 0;
7154 mswitches[n_mswitches].rep_len = 0;
7155 n_mswitches++;
7156 break;
7157 }
7158 }
5bbcd587
JJ
7159
7160 /* Add MULTILIB_DEFAULTS switches too, as long as they were not present
7161 on the command line nor any options mutually incompatible with
7162 them. */
7163 for (i = 0; i < n_mdswitches; i++)
7164 {
7165 const char *r;
7166
7167 for (q = multilib_options; *q != '\0'; q++)
7168 {
7169 while (*q == ' ')
7170 q++;
7171
7172 r = q;
7173 while (strncmp (q, mdswitches[i].str, mdswitches[i].len) != 0
7174 || strchr (" /", q[mdswitches[i].len]) == NULL)
7175 {
7176 while (*q != ' ' && *q != '/' && *q != '\0')
7177 q++;
7178 if (*q != '/')
7179 break;
7180 q++;
7181 }
7182
7183 if (*q != ' ' && *q != '\0')
7184 {
7185 while (*r != ' ' && *r != '\0')
7186 {
7187 q = r;
7188 while (*q != ' ' && *q != '/' && *q != '\0')
7189 q++;
7190
7191 if (used_arg (r, q - r))
7192 break;
7193
7194 if (*q != '/')
7195 {
7196 mswitches[n_mswitches].str = mdswitches[i].str;
7197 mswitches[n_mswitches].len = mdswitches[i].len;
7198 mswitches[n_mswitches].replace = (char *) 0;
7199 mswitches[n_mswitches].rep_len = 0;
7200 n_mswitches++;
7201 break;
7202 }
7203
7204 r = q + 1;
7205 }
7206 break;
7207 }
7208 }
7209 }
961b7009 7210 }
03c42484 7211
961b7009
MM
7212 for (i = 0; i < n_mswitches; i++)
7213 if (len == mswitches[i].len && ! strncmp (p, mswitches[i].str, len))
7214 return 1;
03c42484 7215
961b7009
MM
7216 return 0;
7217}
03c42484
RK
7218
7219static int
1d088dee 7220default_arg (const char *p, int len)
03c42484 7221{
5bbcd587 7222 int i;
e29ef920 7223
5bbcd587
JJ
7224 for (i = 0; i < n_mdswitches; i++)
7225 if (len == mdswitches[i].len && ! strncmp (p, mdswitches[i].str, len))
7226 return 1;
03c42484
RK
7227
7228 return 0;
7229}
7230
0a8d6618
BC
7231/* Work out the subdirectory to use based on the options. The format of
7232 multilib_select is a list of elements. Each element is a subdirectory
7233 name followed by a list of options followed by a semicolon. The format
7234 of multilib_exclusions is the same, but without the preceding
7235 directory. First gcc will check the exclusions, if none of the options
7236 beginning with an exclamation point are present, and all of the other
7237 options are present, then we will ignore this completely. Passing
7238 that, gcc will consider each multilib_select in turn using the same
7239 rules for matching the options. If a match is found, that subdirectory
7240 will be used. */
60103a34
DE
7241
7242static void
1d088dee 7243set_multilib_dir (void)
60103a34 7244{
3b304f5b 7245 const char *p;
3ac63d94 7246 unsigned int this_path_len;
3b304f5b 7247 const char *this_path, *this_arg;
5bbcd587 7248 const char *start, *end;
03c42484 7249 int not_arg;
5bbcd587
JJ
7250 int ok, ndfltok, first;
7251
7252 n_mdswitches = 0;
7253 start = multilib_defaults;
7254 while (*start == ' ' || *start == '\t')
7255 start++;
7256 while (*start != '\0')
7257 {
7258 n_mdswitches++;
7259 while (*start != ' ' && *start != '\t' && *start != '\0')
7260 start++;
7261 while (*start == ' ' || *start == '\t')
7262 start++;
7263 }
7264
7265 if (n_mdswitches)
7266 {
7267 int i = 0;
7268
5ed6ace5 7269 mdswitches = XNEWVEC (struct mdswitchstr, n_mdswitches);
5bbcd587
JJ
7270 for (start = multilib_defaults; *start != '\0'; start = end + 1)
7271 {
7272 while (*start == ' ' || *start == '\t')
7273 start++;
7274
7275 if (*start == '\0')
7276 break;
1d088dee 7277
5bbcd587
JJ
7278 for (end = start + 1;
7279 *end != ' ' && *end != '\t' && *end != '\0'; end++)
7280 ;
7281
7282 obstack_grow (&multilib_obstack, start, end - start);
7283 obstack_1grow (&multilib_obstack, 0);
7973fd2a 7284 mdswitches[i].str = XOBFINISH (&multilib_obstack, const char *);
5bbcd587
JJ
7285 mdswitches[i++].len = end - start;
7286
7287 if (*end == '\0')
7288 break;
7289 }
7290 }
60103a34 7291
0a8d6618
BC
7292 p = multilib_exclusions;
7293 while (*p != '\0')
7294 {
7295 /* Ignore newlines. */
7296 if (*p == '\n')
d25a45d4
KH
7297 {
7298 ++p;
7299 continue;
7300 }
0a8d6618
BC
7301
7302 /* Check the arguments. */
7303 ok = 1;
7304 while (*p != ';')
d25a45d4
KH
7305 {
7306 if (*p == '\0')
3b5edfee
NS
7307 {
7308 invalid_exclusions:
bdc6b402 7309 fatal_error ("multilib exclusions %qs is invalid",
2dec80c7 7310 multilib_exclusions);
3b5edfee 7311 }
d25a45d4
KH
7312
7313 if (! ok)
7314 {
7315 ++p;
7316 continue;
7317 }
7318
7319 this_arg = p;
7320 while (*p != ' ' && *p != ';')
7321 {
7322 if (*p == '\0')
3b5edfee 7323 goto invalid_exclusions;
d25a45d4
KH
7324 ++p;
7325 }
7326
7327 if (*this_arg != '!')
7328 not_arg = 0;
7329 else
7330 {
7331 not_arg = 1;
7332 ++this_arg;
7333 }
9218435e 7334
0a8d6618
BC
7335 ok = used_arg (this_arg, p - this_arg);
7336 if (not_arg)
7337 ok = ! ok;
7338
d25a45d4
KH
7339 if (*p == ' ')
7340 ++p;
7341 }
0a8d6618
BC
7342
7343 if (ok)
3ac63d94 7344 return;
0a8d6618
BC
7345
7346 ++p;
7347 }
7348
5bbcd587 7349 first = 1;
0a8d6618 7350 p = multilib_select;
60103a34
DE
7351 while (*p != '\0')
7352 {
7353 /* Ignore newlines. */
7354 if (*p == '\n')
7355 {
7356 ++p;
7357 continue;
7358 }
7359
7360 /* Get the initial path. */
7361 this_path = p;
7362 while (*p != ' ')
7363 {
7364 if (*p == '\0')
3b5edfee
NS
7365 {
7366 invalid_select:
bdc6b402 7367 fatal_error ("multilib select %qs is invalid",
2dec80c7 7368 multilib_select);
3b5edfee 7369 }
60103a34
DE
7370 ++p;
7371 }
7372 this_path_len = p - this_path;
7373
7374 /* Check the arguments. */
03c42484 7375 ok = 1;
5bbcd587 7376 ndfltok = 1;
60103a34
DE
7377 ++p;
7378 while (*p != ';')
7379 {
7380 if (*p == '\0')
3b5edfee 7381 goto invalid_select;
60103a34 7382
03c42484 7383 if (! ok)
60103a34
DE
7384 {
7385 ++p;
7386 continue;
7387 }
7388
7389 this_arg = p;
7390 while (*p != ' ' && *p != ';')
7391 {
7392 if (*p == '\0')
3b5edfee 7393 goto invalid_select;
60103a34
DE
7394 ++p;
7395 }
7396
03c42484
RK
7397 if (*this_arg != '!')
7398 not_arg = 0;
60103a34 7399 else
03c42484
RK
7400 {
7401 not_arg = 1;
7402 ++this_arg;
7403 }
7404
7405 /* If this is a default argument, we can just ignore it.
7406 This is true even if this_arg begins with '!'. Beginning
7407 with '!' does not mean that this argument is necessarily
7408 inappropriate for this library: it merely means that
7409 there is a more specific library which uses this
7410 argument. If this argument is a default, we need not
7411 consider that more specific library. */
5bbcd587
JJ
7412 ok = used_arg (this_arg, p - this_arg);
7413 if (not_arg)
7414 ok = ! ok;
7415
7416 if (! ok)
7417 ndfltok = 0;
7418
7419 if (default_arg (this_arg, p - this_arg))
7420 ok = 1;
60103a34
DE
7421
7422 if (*p == ' ')
7423 ++p;
7424 }
7425
5bbcd587 7426 if (ok && first)
60103a34
DE
7427 {
7428 if (this_path_len != 1
7429 || this_path[0] != '.')
7430 {
5ed6ace5 7431 char *new_multilib_dir = XNEWVEC (char, this_path_len + 1);
5bbcd587
JJ
7432 char *q;
7433
878f32c3
KG
7434 strncpy (new_multilib_dir, this_path, this_path_len);
7435 new_multilib_dir[this_path_len] = '\0';
5bbcd587
JJ
7436 q = strchr (new_multilib_dir, ':');
7437 if (q != NULL)
7438 *q = '\0';
878f32c3 7439 multilib_dir = new_multilib_dir;
60103a34 7440 }
5bbcd587
JJ
7441 first = 0;
7442 }
7443
7444 if (ndfltok)
7445 {
7446 const char *q = this_path, *end = this_path + this_path_len;
7447
7448 while (q < end && *q != ':')
7449 q++;
c49d2df6 7450 if (q < end)
5bbcd587 7451 {
5ed6ace5 7452 char *new_multilib_os_dir = XNEWVEC (char, end - q);
c49d2df6
JJ
7453 memcpy (new_multilib_os_dir, q + 1, end - q - 1);
7454 new_multilib_os_dir[end - q - 1] = '\0';
5bbcd587
JJ
7455 multilib_os_dir = new_multilib_os_dir;
7456 break;
7457 }
60103a34
DE
7458 }
7459
7460 ++p;
9218435e 7461 }
5bbcd587
JJ
7462
7463 if (multilib_dir == NULL && multilib_os_dir != NULL
7464 && strcmp (multilib_os_dir, ".") == 0)
7465 {
b1d5455a 7466 free (CONST_CAST (char *, multilib_os_dir));
5bbcd587
JJ
7467 multilib_os_dir = NULL;
7468 }
7469 else if (multilib_dir != NULL && multilib_os_dir == NULL)
7470 multilib_os_dir = multilib_dir;
60103a34
DE
7471}
7472
7473/* Print out the multiple library subdirectory selection
7474 information. This prints out a series of lines. Each line looks
7475 like SUBDIRECTORY;@OPTION@OPTION, with as many options as is
7476 required. Only the desired options are printed out, the negative
7477 matches. The options are print without a leading dash. There are
7478 no spaces to make it easy to use the information in the shell.
7479 Each subdirectory is printed only once. This assumes the ordering
0a8d6618
BC
7480 generated by the genmultilib script. Also, we leave out ones that match
7481 the exclusions. */
60103a34
DE
7482
7483static void
1d088dee 7484print_multilib_info (void)
60103a34 7485{
3b304f5b
ZW
7486 const char *p = multilib_select;
7487 const char *last_path = 0, *this_path;
03c42484 7488 int skip;
3ac63d94 7489 unsigned int last_path_len = 0;
60103a34
DE
7490
7491 while (*p != '\0')
7492 {
0a8d6618 7493 skip = 0;
60103a34
DE
7494 /* Ignore newlines. */
7495 if (*p == '\n')
7496 {
7497 ++p;
7498 continue;
7499 }
7500
7501 /* Get the initial path. */
7502 this_path = p;
7503 while (*p != ' ')
7504 {
7505 if (*p == '\0')
3b5edfee
NS
7506 {
7507 invalid_select:
bdc6b402 7508 fatal_error ("multilib select %qs is invalid", multilib_select);
3b5edfee 7509 }
7904f95f 7510
60103a34
DE
7511 ++p;
7512 }
7513
c49d2df6
JJ
7514 /* When --disable-multilib was used but target defines
7515 MULTILIB_OSDIRNAMES, entries starting with .: are there just
7516 to find multilib_os_dir, so skip them from output. */
7517 if (this_path[0] == '.' && this_path[1] == ':')
7518 skip = 1;
7519
0a8d6618
BC
7520 /* Check for matches with the multilib_exclusions. We don't bother
7521 with the '!' in either list. If any of the exclusion rules match
7522 all of its options with the select rule, we skip it. */
d25a45d4
KH
7523 {
7524 const char *e = multilib_exclusions;
7525 const char *this_arg;
0a8d6618 7526
d25a45d4
KH
7527 while (*e != '\0')
7528 {
7529 int m = 1;
7530 /* Ignore newlines. */
7531 if (*e == '\n')
7532 {
7533 ++e;
7534 continue;
7535 }
0a8d6618 7536
d25a45d4
KH
7537 /* Check the arguments. */
7538 while (*e != ';')
7539 {
7540 const char *q;
7541 int mp = 0;
0a8d6618 7542
d25a45d4 7543 if (*e == '\0')
3b5edfee
NS
7544 {
7545 invalid_exclusion:
bdc6b402 7546 fatal_error ("multilib exclusion %qs is invalid",
2dec80c7 7547 multilib_exclusions);
3b5edfee 7548 }
9218435e 7549
d25a45d4
KH
7550 if (! m)
7551 {
7552 ++e;
7553 continue;
7554 }
0a8d6618 7555
d25a45d4 7556 this_arg = e;
9218435e 7557
d25a45d4
KH
7558 while (*e != ' ' && *e != ';')
7559 {
7560 if (*e == '\0')
3b5edfee 7561 goto invalid_exclusion;
d25a45d4
KH
7562 ++e;
7563 }
0a8d6618 7564
d25a45d4
KH
7565 q = p + 1;
7566 while (*q != ';')
7567 {
7568 const char *arg;
7569 int len = e - this_arg;
0a8d6618 7570
d25a45d4 7571 if (*q == '\0')
3b5edfee 7572 goto invalid_select;
0a8d6618 7573
d25a45d4
KH
7574 arg = q;
7575
7576 while (*q != ' ' && *q != ';')
7577 {
7578 if (*q == '\0')
3b5edfee 7579 goto invalid_select;
0a8d6618 7580 ++q;
d25a45d4 7581 }
0a8d6618 7582
3b5edfee
NS
7583 if (! strncmp (arg, this_arg,
7584 (len < q - arg) ? q - arg : len)
7585 || default_arg (this_arg, e - this_arg))
d25a45d4
KH
7586 {
7587 mp = 1;
7588 break;
7589 }
0a8d6618 7590
d25a45d4
KH
7591 if (*q == ' ')
7592 ++q;
7593 }
9218435e 7594
d25a45d4
KH
7595 if (! mp)
7596 m = 0;
0a8d6618 7597
d25a45d4
KH
7598 if (*e == ' ')
7599 ++e;
7600 }
7601
7602 if (m)
7603 {
7604 skip = 1;
7605 break;
7606 }
7607
7608 if (*e != '\0')
7609 ++e;
7610 }
7611 }
0a8d6618
BC
7612
7613 if (! skip)
d25a45d4
KH
7614 {
7615 /* If this is a duplicate, skip it. */
3b5edfee
NS
7616 skip = (last_path != 0
7617 && (unsigned int) (p - this_path) == last_path_len
d25a45d4 7618 && ! strncmp (last_path, this_path, last_path_len));
60103a34 7619
d25a45d4
KH
7620 last_path = this_path;
7621 last_path_len = p - this_path;
0a8d6618 7622 }
60103a34 7623
03c42484
RK
7624 /* If this directory requires any default arguments, we can skip
7625 it. We will already have printed a directory identical to
7626 this one which does not require that default argument. */
7627 if (! skip)
7628 {
3b304f5b 7629 const char *q;
03c42484
RK
7630
7631 q = p + 1;
7632 while (*q != ';')
7633 {
3b304f5b 7634 const char *arg;
03c42484
RK
7635
7636 if (*q == '\0')
3b5edfee 7637 goto invalid_select;
03c42484
RK
7638
7639 if (*q == '!')
7640 arg = NULL;
7641 else
7642 arg = q;
7643
7644 while (*q != ' ' && *q != ';')
7645 {
7646 if (*q == '\0')
3b5edfee 7647 goto invalid_select;
03c42484
RK
7648 ++q;
7649 }
7650
7651 if (arg != NULL
7652 && default_arg (arg, q - arg))
7653 {
7654 skip = 1;
7655 break;
7656 }
7657
7658 if (*q == ' ')
7659 ++q;
7660 }
7661 }
7662
60103a34
DE
7663 if (! skip)
7664 {
3b304f5b 7665 const char *p1;
60103a34 7666
5bbcd587 7667 for (p1 = last_path; p1 < p && *p1 != ':'; p1++)
60103a34
DE
7668 putchar (*p1);
7669 putchar (';');
7670 }
7671
7672 ++p;
7673 while (*p != ';')
7674 {
7675 int use_arg;
7676
7677 if (*p == '\0')
3b5edfee 7678 goto invalid_select;
60103a34
DE
7679
7680 if (skip)
7681 {
7682 ++p;
7683 continue;
7684 }
7685
7686 use_arg = *p != '!';
7687
7688 if (use_arg)
7689 putchar ('@');
7690
7691 while (*p != ' ' && *p != ';')
7692 {
7693 if (*p == '\0')
3b5edfee 7694 goto invalid_select;
60103a34
DE
7695 if (use_arg)
7696 putchar (*p);
7697 ++p;
7698 }
7699
7700 if (*p == ' ')
7701 ++p;
7702 }
7703
7704 if (! skip)
961b7009 7705 {
3ac63d94 7706 /* If there are extra options, print them now. */
961b7009
MM
7707 if (multilib_extra && *multilib_extra)
7708 {
7709 int print_at = TRUE;
3b304f5b 7710 const char *q;
961b7009
MM
7711
7712 for (q = multilib_extra; *q != '\0'; q++)
7713 {
7714 if (*q == ' ')
7715 print_at = TRUE;
7716 else
7717 {
7718 if (print_at)
7719 putchar ('@');
7720 putchar (*q);
7721 print_at = FALSE;
7722 }
7723 }
7724 }
9218435e 7725
961b7009
MM
7726 putchar ('\n');
7727 }
60103a34
DE
7728
7729 ++p;
7730 }
7731}
f3226a90 7732\f
30d8946b
MM
7733/* getenv built-in spec function.
7734
7735 Returns the value of the environment variable given by its first
7736 argument, concatenated with the second argument. If the
7737 environment variable is not defined, a fatal error is issued. */
7738
7739static const char *
7740getenv_spec_function (int argc, const char **argv)
7741{
7742 char *value;
5557813a
NS
7743 char *result;
7744 char *ptr;
7745 size_t len;
30d8946b
MM
7746
7747 if (argc != 2)
7748 return NULL;
7749
7750 value = getenv (argv[0]);
7751 if (!value)
bdc6b402 7752 fatal_error ("environment variable %qs not defined", argv[0]);
30d8946b 7753
5557813a 7754 /* We have to escape every character of the environment variable so
fa10beec
RW
7755 they are not interpreted as active spec characters. A
7756 particularly painful case is when we are reading a variable
5557813a
NS
7757 holding a windows path complete with \ separators. */
7758 len = strlen (value) * 2 + strlen (argv[1]) + 1;
e1e4cdc4 7759 result = XNEWVAR (char, len);
5557813a
NS
7760 for (ptr = result; *value; ptr += 2)
7761 {
7762 ptr[0] = '\\';
7763 ptr[1] = *value++;
7764 }
b8698a0f 7765
5557813a 7766 strcpy (ptr, argv[1]);
b8698a0f 7767
5557813a 7768 return result;
30d8946b
MM
7769}
7770
f3226a90
JT
7771/* if-exists built-in spec function.
7772
7773 Checks to see if the file specified by the absolute pathname in
7774 ARGS exists. Returns that pathname if found.
7775
7776 The usual use for this function is to check for a library file
7777 (whose name has been expanded with %s). */
7778
7779static const char *
1d088dee 7780if_exists_spec_function (int argc, const char **argv)
f3226a90
JT
7781{
7782 /* Must have only one argument. */
3dce1408 7783 if (argc == 1 && IS_ABSOLUTE_PATH (argv[0]) && ! access (argv[0], R_OK))
f3226a90
JT
7784 return argv[0];
7785
7786 return NULL;
7787}
152a5a9c
JT
7788
7789/* if-exists-else built-in spec function.
7790
7791 This is like if-exists, but takes an additional argument which
7792 is returned if the first argument does not exist. */
7793
7794static const char *
1d088dee 7795if_exists_else_spec_function (int argc, const char **argv)
152a5a9c
JT
7796{
7797 /* Must have exactly two arguments. */
7798 if (argc != 2)
7799 return NULL;
7800
3dce1408 7801 if (IS_ABSOLUTE_PATH (argv[0]) && ! access (argv[0], R_OK))
152a5a9c
JT
7802 return argv[0];
7803
7804 return argv[1];
7805}
3dd53121
AP
7806
7807/* replace-outfile built-in spec function.
ed5b9f96
GK
7808
7809 This looks for the first argument in the outfiles array's name and
7810 replaces it with the second argument. */
3dd53121
AP
7811
7812static const char *
7813replace_outfile_spec_function (int argc, const char **argv)
7814{
7815 int i;
7816 /* Must have exactly two arguments. */
7817 if (argc != 2)
7818 abort ();
7904f95f 7819
3dd53121
AP
7820 for (i = 0; i < n_infiles; i++)
7821 {
7822 if (outfiles[i] && !strcmp (outfiles[i], argv[0]))
7823 outfiles[i] = xstrdup (argv[1]);
7824 }
7825 return NULL;
7826}
7827
2642f659
JH
7828/* remove-outfile built-in spec function.
7829 *
7830 * This looks for the first argument in the outfiles array's name and
7831 * removes it. */
7832
7833static const char *
7834remove_outfile_spec_function (int argc, const char **argv)
7835{
7836 int i;
7837 /* Must have exactly one argument. */
7838 if (argc != 1)
7839 abort ();
7840
7841 for (i = 0; i < n_infiles; i++)
7842 {
7843 if (outfiles[i] && !strcmp (outfiles[i], argv[0]))
7844 outfiles[i] = NULL;
7845 }
7846 return NULL;
7847}
7848
7904f95f 7849/* Given two version numbers, compares the two numbers.
ed5b9f96
GK
7850 A version number must match the regular expression
7851 ([1-9][0-9]*|0)(\.([1-9][0-9]*|0))*
7852*/
7853static int
7854compare_version_strings (const char *v1, const char *v2)
7855{
7856 int rresult;
7857 regex_t r;
7904f95f 7858
ed5b9f96
GK
7859 if (regcomp (&r, "^([1-9][0-9]*|0)(\\.([1-9][0-9]*|0))*$",
7860 REG_EXTENDED | REG_NOSUB) != 0)
7861 abort ();
7862 rresult = regexec (&r, v1, 0, NULL, 0);
7863 if (rresult == REG_NOMATCH)
bdc6b402 7864 fatal_error ("invalid version number %qs", v1);
ed5b9f96
GK
7865 else if (rresult != 0)
7866 abort ();
7867 rresult = regexec (&r, v2, 0, NULL, 0);
7868 if (rresult == REG_NOMATCH)
bdc6b402 7869 fatal_error ("invalid version number %qs", v2);
ed5b9f96
GK
7870 else if (rresult != 0)
7871 abort ();
7872
7873 return strverscmp (v1, v2);
7874}
7875
7876
7877/* version_compare built-in spec function.
7878
7879 This takes an argument of the following form:
7880
7881 <comparison-op> <arg1> [<arg2>] <switch> <result>
7882
7883 and produces "result" if the comparison evaluates to true,
7884 and nothing if it doesn't.
7885
7886 The supported <comparison-op> values are:
7904f95f 7887
ed5b9f96
GK
7888 >= true if switch is a later (or same) version than arg1
7889 !> opposite of >=
7890 < true if switch is an earlier version than arg1
7891 !< opposite of <
7892 >< true if switch is arg1 or later, and earlier than arg2
7893 <> true if switch is earlier than arg1 or is arg2 or later
7894
7895 If the switch is not present, the condition is false unless
7896 the first character of the <comparison-op> is '!'.
7897
7898 For example,
7899 %:version-compare(>= 10.3 mmacosx-version-min= -lmx)
7900 adds -lmx if -mmacosx-version-min=10.3.9 was passed. */
7901
7902static const char *
7903version_compare_spec_function (int argc, const char **argv)
7904{
7905 int comp1, comp2;
7906 size_t switch_len;
7907 const char *switch_value = NULL;
7908 int nargs = 1, i;
7909 bool result;
7910
7911 if (argc < 3)
2dec80c7 7912 fatal_error ("too few arguments to %%:version-compare");
ed5b9f96
GK
7913 if (argv[0][0] == '\0')
7914 abort ();
7915 if ((argv[0][1] == '<' || argv[0][1] == '>') && argv[0][0] != '!')
7916 nargs = 2;
7917 if (argc != nargs + 3)
2dec80c7 7918 fatal_error ("too many arguments to %%:version-compare");
ed5b9f96
GK
7919
7920 switch_len = strlen (argv[nargs + 1]);
7921 for (i = 0; i < n_switches; i++)
7922 if (!strncmp (switches[i].part1, argv[nargs + 1], switch_len)
7923 && check_live_switch (i, switch_len))
7924 switch_value = switches[i].part1 + switch_len;
7925
7926 if (switch_value == NULL)
7927 comp1 = comp2 = -1;
7928 else
7929 {
7930 comp1 = compare_version_strings (switch_value, argv[1]);
7931 if (nargs == 2)
7932 comp2 = compare_version_strings (switch_value, argv[2]);
7933 else
7934 comp2 = -1; /* This value unused. */
7935 }
7936
7937 switch (argv[0][0] << 8 | argv[0][1])
7938 {
7939 case '>' << 8 | '=':
7940 result = comp1 >= 0;
7941 break;
7942 case '!' << 8 | '<':
7943 result = comp1 >= 0 || switch_value == NULL;
7944 break;
7945 case '<' << 8:
7946 result = comp1 < 0;
7947 break;
7948 case '!' << 8 | '>':
7949 result = comp1 < 0 || switch_value == NULL;
7950 break;
7951 case '>' << 8 | '<':
7952 result = comp1 >= 0 && comp2 < 0;
7953 break;
7954 case '<' << 8 | '>':
7955 result = comp1 < 0 || comp2 >= 0;
7956 break;
7904f95f 7957
ed5b9f96 7958 default:
bdc6b402 7959 fatal_error ("unknown operator %qs in %%:version-compare", argv[0]);
ed5b9f96
GK
7960 }
7961 if (! result)
7962 return NULL;
7963
7964 return argv[nargs + 2];
7965}
953ff289
DN
7966
7967/* %:include builtin spec function. This differs from %include in that it
7968 can be nested inside a spec, and thus be conditionalized. It takes
7969 one argument, the filename, and looks for it in the startfile path.
7970 The result is always NULL, i.e. an empty expansion. */
7971
7972static const char *
7973include_spec_function (int argc, const char **argv)
7974{
7975 char *file;
7976
7977 if (argc != 1)
7978 abort ();
7979
4d2b059d 7980 file = find_a_file (&startfile_prefixes, argv[0], R_OK, true);
953ff289
DN
7981 read_specs (file ? file : argv[0], FALSE);
7982
7983 return NULL;
7984}
a0f87454 7985
c1ce46a5 7986/* %:find-file spec function. This function replaces its argument by
4adbd5dd
MK
7987 the file found thru find_file, that is the -print-file-name gcc
7988 program option. */
7989static const char *
c1ce46a5 7990find_file_spec_function (int argc, const char **argv)
4adbd5dd
MK
7991{
7992 const char *file;
7993
7994 if (argc != 1)
7995 abort ();
7996
7997 file = find_file (argv[0]);
7998 return file;
7999}
8000
8001
c1ce46a5
MK
8002/* %:find-plugindir spec function. This function replaces its argument
8003 by the -iplugindir=<dir> option. `dir' is found thru find_file, that
8004 is the -print-file-name gcc program option. */
8005static const char *
8006find_plugindir_spec_function (int argc, const char **argv ATTRIBUTE_UNUSED)
8007{
8008 const char *option;
8009
8010 if (argc != 0)
8011 abort ();
8012
8013 option = concat ("-iplugindir=", find_file ("plugin"), NULL);
8014 return option;
8015}
8016
8017
a0f87454
RS
8018/* %:print-asm-header spec function. Print a banner to say that the
8019 following output is from the assembler. */
8020
8021static const char *
8022print_asm_header_spec_function (int arg ATTRIBUTE_UNUSED,
8023 const char **argv ATTRIBUTE_UNUSED)
8024{
4dad0aca 8025 printf (_("Assembler options\n=================\n\n"));
a0f87454
RS
8026 printf (_("Use \"-Wa,OPTION\" to pass \"OPTION\" to the assembler.\n\n"));
8027 fflush (stdout);
8028 return NULL;
8029}
2153915d 8030
b5b8b0ac
AO
8031/* Compute a timestamp to initialize flag_random_seed. */
8032
8033static unsigned
8034get_local_tick (void)
8035{
8036 unsigned ret = 0;
8037
8038 /* Get some more or less random data. */
8039#ifdef HAVE_GETTIMEOFDAY
8040 {
8041 struct timeval tv;
8042
8043 gettimeofday (&tv, NULL);
8044 ret = tv.tv_sec * 1000 + tv.tv_usec / 1000;
8045 }
8046#else
8047 {
8048 time_t now = time (NULL);
8049
8050 if (now != (time_t)-1)
8051 ret = (unsigned) now;
8052 }
8053#endif
8054
8055 return ret;
8056}
8057
2153915d
AO
8058/* %:compare-debug-dump-opt spec function. Save the last argument,
8059 expected to be the last -fdump-final-insns option, or generate a
8060 temporary. */
8061
8062static const char *
8063compare_debug_dump_opt_spec_function (int arg,
8064 const char **argv ATTRIBUTE_UNUSED)
8065{
8066 const char *ret;
8067 char *name;
8068 int which;
b5b8b0ac 8069 static char random_seed[HOST_BITS_PER_WIDE_INT / 4 + 3];
2153915d
AO
8070
8071 if (arg != 0)
2dec80c7 8072 fatal_error ("too many arguments to %%:compare-debug-dump-opt");
2153915d 8073
2153915d
AO
8074 do_spec_2 ("%{fdump-final-insns=*:%*}");
8075 do_spec_1 (" ", 0, NULL);
8076
5b634ee0
JM
8077 if (VEC_length (const_char_p, argbuf) > 0
8078 && strcmp (argv[VEC_length (const_char_p, argbuf) - 1], "."))
2153915d 8079 {
b5b8b0ac
AO
8080 if (!compare_debug)
8081 return NULL;
8082
5b634ee0 8083 name = xstrdup (argv[VEC_length (const_char_p, argbuf) - 1]);
2153915d
AO
8084 ret = NULL;
8085 }
8086 else
8087 {
b5b8b0ac
AO
8088 const char *ext = NULL;
8089
5b634ee0 8090 if (VEC_length (const_char_p, argbuf) > 0)
b5b8b0ac
AO
8091 {
8092 do_spec_2 ("%{o*:%*}%{!o:%{!S:%b%O}%{S:%b.s}}");
8093 ext = ".gkd";
8094 }
8095 else if (!compare_debug)
8096 return NULL;
8097 else
8098 do_spec_2 ("%g.gkd");
2153915d 8099
2153915d 8100 do_spec_1 (" ", 0, NULL);
2153915d 8101
5b634ee0 8102 gcc_assert (VEC_length (const_char_p, argbuf) > 0);
2153915d 8103
5b634ee0 8104 name = concat (VEC_last (const_char_p, argbuf), ext, NULL);
b5b8b0ac
AO
8105
8106 ret = concat ("-fdump-final-insns=", name, NULL);
2153915d
AO
8107 }
8108
8109 which = compare_debug < 0;
8110 debug_check_temp_file[which] = name;
8111
b5b8b0ac
AO
8112 if (!which)
8113 {
8114 unsigned HOST_WIDE_INT value = get_local_tick () ^ getpid ();
8115
8116 sprintf (random_seed, HOST_WIDE_INT_PRINT_HEX, value);
8117 }
8118
8119 if (*random_seed)
8120 ret = concat ("%{!frandom-seed=*:-frandom-seed=", random_seed, "} ",
8121 ret, NULL);
8122
8123 if (which)
8124 *random_seed = 0;
2153915d
AO
8125
8126 return ret;
8127}
8128
8129static const char *debug_auxbase_opt;
8130
8131/* %:compare-debug-self-opt spec function. Expands to the options
8132 that are to be passed in the second compilation of
8133 compare-debug. */
8134
8135static const char *
8136compare_debug_self_opt_spec_function (int arg,
8137 const char **argv ATTRIBUTE_UNUSED)
8138{
8139 if (arg != 0)
2dec80c7 8140 fatal_error ("too many arguments to %%:compare-debug-self-opt");
2153915d
AO
8141
8142 if (compare_debug >= 0)
8143 return NULL;
8144
8145 do_spec_2 ("%{c|S:%{o*:%*}}");
8146 do_spec_1 (" ", 0, NULL);
8147
5b634ee0 8148 if (VEC_length (const_char_p, argbuf) > 0)
2153915d 8149 debug_auxbase_opt = concat ("-auxbase-strip ",
5b634ee0 8150 VEC_last (const_char_p, argbuf),
2153915d
AO
8151 NULL);
8152 else
8153 debug_auxbase_opt = NULL;
8154
8155 return concat ("\
8156%<o %<MD %<MMD %<MF* %<MG %<MP %<MQ* %<MT* \
8157%<fdump-final-insns=* -w -S -o %j \
8158%{!fcompare-debug-second:-fcompare-debug-second} \
8159", compare_debug_opt, NULL);
8160}
8161
8162/* %:compare-debug-auxbase-opt spec function. Expands to the auxbase
8163 options that are to be passed in the second compilation of
8164 compare-debug. It expects, as an argument, the basename of the
8165 current input file name, with the .gk suffix appended to it. */
8166
8167static const char *
8168compare_debug_auxbase_opt_spec_function (int arg,
8169 const char **argv)
8170{
8171 char *name;
8172 int len;
8173
8174 if (arg == 0)
2dec80c7 8175 fatal_error ("too few arguments to %%:compare-debug-auxbase-opt");
2153915d
AO
8176
8177 if (arg != 1)
2dec80c7 8178 fatal_error ("too many arguments to %%:compare-debug-auxbase-opt");
2153915d
AO
8179
8180 if (compare_debug >= 0)
8181 return NULL;
8182
8183 len = strlen (argv[0]);
8184 if (len < 3 || strcmp (argv[0] + len - 3, ".gk") != 0)
2dec80c7
JM
8185 fatal_error ("argument to %%:compare-debug-auxbase-opt "
8186 "does not end in .gk");
2153915d
AO
8187
8188 if (debug_auxbase_opt)
8189 return debug_auxbase_opt;
8190
8191#define OPT "-auxbase "
8192
8193 len -= 3;
8194 name = (char*) xmalloc (sizeof (OPT) + len);
8195 memcpy (name, OPT, sizeof (OPT) - 1);
8196 memcpy (name + sizeof (OPT) - 1, argv[0], len);
8197 name[sizeof (OPT) - 1 + len] = '\0';
8198
b5b8b0ac
AO
8199#undef OPT
8200
2153915d
AO
8201 return name;
8202}
9a37bc07
DK
8203
8204/* %:pass-through-libs spec function. Finds all -l options and input
8205 file names in the lib spec passed to it, and makes a list of them
8206 prepended with the plugin option to cause them to be passed through
8207 to the final link after all the new object files have been added. */
8208
8209const char *
8210pass_through_libs_spec_func (int argc, const char **argv)
8211{
8212 char *prepended = xstrdup (" ");
8213 int n;
8214 /* Shlemiel the painter's algorithm. Innately horrible, but at least
8215 we know that there will never be more than a handful of strings to
8216 concat, and it's only once per run, so it's not worth optimising. */
8217 for (n = 0; n < argc; n++)
8218 {
8219 char *old = prepended;
8220 /* Anything that isn't an option is a full path to an output
8221 file; pass it through if it ends in '.a'. Among options,
8222 pass only -l. */
8223 if (argv[n][0] == '-' && argv[n][1] == 'l')
8224 {
8225 const char *lopt = argv[n] + 2;
8226 /* Handle both joined and non-joined -l options. If for any
8227 reason there's a trailing -l with no joined or following
8228 arg just discard it. */
8229 if (!*lopt && ++n >= argc)
8230 break;
8231 else if (!*lopt)
8232 lopt = argv[n];
8233 prepended = concat (prepended, "-plugin-opt=-pass-through=-l",
8234 lopt, " ", NULL);
8235 }
8236 else if (!strcmp (".a", argv[n] + strlen (argv[n]) - 2))
8237 {
8238 prepended = concat (prepended, "-plugin-opt=-pass-through=",
8239 argv[n], " ", NULL);
8240 }
8241 if (prepended != old)
8242 free (old);
8243 }
8244 return prepended;
8245}