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