Branch data Line data Source code
1 : : /* Compiler driver program that can handle many languages.
2 : : Copyright (C) 1987-2025 Free Software Foundation, Inc.
3 : :
4 : : This file is part of GCC.
5 : :
6 : : GCC is free software; you can redistribute it and/or modify it under
7 : : the terms of the GNU General Public License as published by the Free
8 : : Software Foundation; either version 3, or (at your option) any later
9 : : version.
10 : :
11 : : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 : : WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 : : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 : : for more details.
15 : :
16 : : You should have received a copy of the GNU General Public License
17 : : along with GCC; see the file COPYING3. If not see
18 : : <http://www.gnu.org/licenses/>. */
19 : :
20 : : /* This program is the user interface to the C compiler and possibly to
21 : : other compilers. It is used because compilation is a complicated procedure
22 : : which involves running several programs and passing temporary files between
23 : : them, forwarding the users switches to those programs selectively,
24 : : and deleting the temporary files at the end.
25 : :
26 : : CC recognizes how to compile each input file by suffixes in the file names.
27 : : Once it knows which kind of compilation to perform, the procedure for
28 : : compilation is specified by a string called a "spec". */
29 : :
30 : : #define INCLUDE_STRING
31 : : #define INCLUDE_VECTOR
32 : : #include "config.h"
33 : : #include "system.h"
34 : : #ifdef HOST_HAS_PERSONALITY_ADDR_NO_RANDOMIZE
35 : : #include <sys/personality.h>
36 : : #endif
37 : : #include "coretypes.h"
38 : : #include "multilib.h" /* before tm.h */
39 : : #include "tm.h"
40 : : #include "xregex.h"
41 : : #include "obstack.h"
42 : : #include "intl.h"
43 : : #include "prefix.h"
44 : : #include "opt-suggestions.h"
45 : : #include "gcc.h"
46 : : #include "diagnostic.h"
47 : : #include "diagnostics/sink.h"
48 : : #include "pretty-print-urlifier.h"
49 : : #include "flags.h"
50 : : #include "opts.h"
51 : : #include "filenames.h"
52 : : #include "spellcheck.h"
53 : : #include "opts-jobserver.h"
54 : : #include "common/common-target.h"
55 : : #include "gcc-urlifier.h"
56 : : #include "opts-diagnostic.h"
57 : :
58 : : #ifndef MATH_LIBRARY
59 : : #define MATH_LIBRARY "m"
60 : : #endif
61 : :
62 : :
63 : : /* Manage the manipulation of env vars.
64 : :
65 : : We poison "getenv" and "putenv", so that all enviroment-handling is
66 : : done through this class. Note that poisoning happens in the
67 : : preprocessor at the identifier level, and doesn't distinguish between
68 : : env.getenv ();
69 : : and
70 : : getenv ();
71 : : Hence we need to use "get" for the accessor method, not "getenv". */
72 : :
73 : : struct env_manager
74 : : {
75 : : public:
76 : : void init (bool can_restore, bool debug);
77 : : const char *get (const char *name);
78 : : void xput (const char *string);
79 : : void restore ();
80 : :
81 : : private:
82 : : bool m_can_restore;
83 : : bool m_debug;
84 : : struct kv
85 : : {
86 : : char *m_key;
87 : : char *m_value;
88 : : };
89 : : vec<kv> m_keys;
90 : :
91 : : };
92 : :
93 : : /* The singleton instance of class env_manager. */
94 : :
95 : : static env_manager env;
96 : :
97 : : /* Initializer for class env_manager.
98 : :
99 : : We can't do this as a constructor since we have a statically
100 : : allocated instance ("env" above). */
101 : :
102 : : void
103 : 300399 : env_manager::init (bool can_restore, bool debug)
104 : : {
105 : 300399 : m_can_restore = can_restore;
106 : 300399 : m_debug = debug;
107 : 300399 : }
108 : :
109 : : /* Get the value of NAME within the environment. Essentially
110 : : a wrapper for ::getenv, but adding logging, and the possibility
111 : : of caching results. */
112 : :
113 : : const char *
114 : 1501078 : env_manager::get (const char *name)
115 : : {
116 : 1501078 : const char *result = ::getenv (name);
117 : 1501078 : if (m_debug)
118 : 0 : fprintf (stderr, "env_manager::getenv (%s) -> %s\n", name, result);
119 : 1501078 : return result;
120 : : }
121 : :
122 : : /* Put the given KEY=VALUE entry STRING into the environment.
123 : : If the env_manager was initialized with CAN_RESTORE set, then
124 : : also record the old value of KEY within the environment, so that it
125 : : can be later restored. */
126 : :
127 : : void
128 : 1773692 : env_manager::xput (const char *string)
129 : : {
130 : 1773692 : if (m_debug)
131 : 0 : fprintf (stderr, "env_manager::xput (%s)\n", string);
132 : 1773692 : if (verbose_flag)
133 : 5986 : fnotice (stderr, "%s\n", string);
134 : :
135 : 1773692 : if (m_can_restore)
136 : : {
137 : 6649 : char *equals = strchr (const_cast <char *> (string), '=');
138 : 6649 : gcc_assert (equals);
139 : :
140 : 6649 : struct kv kv;
141 : 6649 : kv.m_key = xstrndup (string, equals - string);
142 : 6649 : const char *cur_value = ::getenv (kv.m_key);
143 : 6649 : if (m_debug)
144 : 0 : fprintf (stderr, "saving old value: %s\n",cur_value);
145 : 6649 : kv.m_value = cur_value ? xstrdup (cur_value) : NULL;
146 : 6649 : m_keys.safe_push (kv);
147 : : }
148 : :
149 : 1773692 : ::putenv (CONST_CAST (char *, string));
150 : 1773692 : }
151 : :
152 : : /* Undo any xputenv changes made since last restore.
153 : : Can only be called if the env_manager was initialized with
154 : : CAN_RESTORE enabled. */
155 : :
156 : : void
157 : 1109 : env_manager::restore ()
158 : : {
159 : 1109 : unsigned int i;
160 : 1109 : struct kv *item;
161 : :
162 : 1109 : gcc_assert (m_can_restore);
163 : :
164 : 8867 : FOR_EACH_VEC_ELT_REVERSE (m_keys, i, item)
165 : : {
166 : 6649 : if (m_debug)
167 : 0 : printf ("restoring saved key: %s value: %s\n", item->m_key, item->m_value);
168 : 6649 : if (item->m_value)
169 : 3322 : ::setenv (item->m_key, item->m_value, 1);
170 : : else
171 : 3327 : ::unsetenv (item->m_key);
172 : 6649 : free (item->m_key);
173 : 6649 : free (item->m_value);
174 : : }
175 : :
176 : 1109 : m_keys.truncate (0);
177 : 1109 : }
178 : :
179 : : /* Forbid other uses of getenv and putenv. */
180 : : #if (GCC_VERSION >= 3000)
181 : : #pragma GCC poison getenv putenv
182 : : #endif
183 : :
184 : :
185 : :
186 : : /* By default there is no special suffix for target executables. */
187 : : #ifdef TARGET_EXECUTABLE_SUFFIX
188 : : #define HAVE_TARGET_EXECUTABLE_SUFFIX
189 : : #else
190 : : #define TARGET_EXECUTABLE_SUFFIX ""
191 : : #endif
192 : :
193 : : /* By default there is no special suffix for host executables. */
194 : : #ifdef HOST_EXECUTABLE_SUFFIX
195 : : #define HAVE_HOST_EXECUTABLE_SUFFIX
196 : : #else
197 : : #define HOST_EXECUTABLE_SUFFIX ""
198 : : #endif
199 : :
200 : : /* By default, the suffix for target object files is ".o". */
201 : : #ifdef TARGET_OBJECT_SUFFIX
202 : : #define HAVE_TARGET_OBJECT_SUFFIX
203 : : #else
204 : : #define TARGET_OBJECT_SUFFIX ".o"
205 : : #endif
206 : :
207 : : static const char dir_separator_str[] = { DIR_SEPARATOR, 0 };
208 : :
209 : : /* Most every one is fine with LIBRARY_PATH. For some, it conflicts. */
210 : : #ifndef LIBRARY_PATH_ENV
211 : : #define LIBRARY_PATH_ENV "LIBRARY_PATH"
212 : : #endif
213 : :
214 : : /* If a stage of compilation returns an exit status >= 1,
215 : : compilation of that file ceases. */
216 : :
217 : : #define MIN_FATAL_STATUS 1
218 : :
219 : : /* Flag set by cppspec.cc to 1. */
220 : : int is_cpp_driver;
221 : :
222 : : /* Flag set to nonzero if an @file argument has been supplied to gcc. */
223 : : static bool at_file_supplied;
224 : :
225 : : /* Definition of string containing the arguments given to configure. */
226 : : #include "configargs.h"
227 : :
228 : : /* Flag saying to print the command line options understood by gcc and its
229 : : sub-processes. */
230 : :
231 : : static int print_help_list;
232 : :
233 : : /* Flag saying to print the version of gcc and its sub-processes. */
234 : :
235 : : static int print_version;
236 : :
237 : : /* Flag that stores string prefix for which we provide bash completion. */
238 : :
239 : : static const char *completion = NULL;
240 : :
241 : : /* Flag indicating whether we should ONLY print the command and
242 : : arguments (like verbose_flag) without executing the command.
243 : : Displayed arguments are quoted so that the generated command
244 : : line is suitable for execution. This is intended for use in
245 : : shell scripts to capture the driver-generated command line. */
246 : : static int verbose_only_flag;
247 : :
248 : : /* Flag indicating how to print command line options of sub-processes. */
249 : :
250 : : static int print_subprocess_help;
251 : :
252 : : /* Linker suffix passed to -fuse-ld=... */
253 : : static const char *use_ld;
254 : :
255 : : /* Whether we should report subprocess execution times to a file. */
256 : :
257 : : FILE *report_times_to_file = NULL;
258 : :
259 : : /* Nonzero means place this string before uses of /, so that include
260 : : and library files can be found in an alternate location. */
261 : :
262 : : #ifdef TARGET_SYSTEM_ROOT
263 : : #define DEFAULT_TARGET_SYSTEM_ROOT (TARGET_SYSTEM_ROOT)
264 : : #else
265 : : #define DEFAULT_TARGET_SYSTEM_ROOT (0)
266 : : #endif
267 : : static const char *target_system_root = DEFAULT_TARGET_SYSTEM_ROOT;
268 : :
269 : : /* Nonzero means pass the updated target_system_root to the compiler. */
270 : :
271 : : static int target_system_root_changed;
272 : :
273 : : /* Nonzero means append this string to target_system_root. */
274 : :
275 : : static const char *target_sysroot_suffix = 0;
276 : :
277 : : /* Nonzero means append this string to target_system_root for headers. */
278 : :
279 : : static const char *target_sysroot_hdrs_suffix = 0;
280 : :
281 : : /* Nonzero means write "temp" files in source directory
282 : : and use the source file's name in them, and don't delete them. */
283 : :
284 : : static enum save_temps {
285 : : SAVE_TEMPS_NONE, /* no -save-temps */
286 : : SAVE_TEMPS_CWD, /* -save-temps in current directory */
287 : : SAVE_TEMPS_DUMP, /* -save-temps in dumpdir */
288 : : SAVE_TEMPS_OBJ /* -save-temps in object directory */
289 : : } save_temps_flag;
290 : :
291 : : /* Set this iff the dumppfx implied by a -save-temps=* option is to
292 : : override a -dumpdir option, if any. */
293 : : static bool save_temps_overrides_dumpdir = false;
294 : :
295 : : /* -dumpdir, -dumpbase and -dumpbase-ext flags passed in, possibly
296 : : rearranged as they are to be passed down, e.g., dumpbase and
297 : : dumpbase_ext may be cleared if integrated with dumpdir or
298 : : dropped. */
299 : : static char *dumpdir, *dumpbase, *dumpbase_ext;
300 : :
301 : : /* Usually the length of the string in dumpdir. However, during
302 : : linking, it may be shortened to omit a driver-added trailing dash,
303 : : by then replaced with a trailing period, that is still to be passed
304 : : to sub-processes in -dumpdir, but not to be generally used in spec
305 : : filename expansions. See maybe_run_linker. */
306 : : static size_t dumpdir_length = 0;
307 : :
308 : : /* Set if the last character in dumpdir is (or was) a dash that the
309 : : driver added to dumpdir after dumpbase or linker output name. */
310 : : static bool dumpdir_trailing_dash_added = false;
311 : :
312 : : /* True if -r, -shared, -pie, -no-pie, -z lazy, or -z norelro were
313 : : specified on the command line, and therefore -fhardened should not
314 : : add -z now/relro. */
315 : : static bool avoid_linker_hardening_p;
316 : :
317 : : /* True if -static was specified on the command line. */
318 : : static bool static_p;
319 : :
320 : : /* Basename of dump and aux outputs, computed from dumpbase (given or
321 : : derived from output name), to override input_basename in non-%w %b
322 : : et al. */
323 : : static char *outbase;
324 : : static size_t outbase_length = 0;
325 : :
326 : : /* The compiler version. */
327 : :
328 : : static const char *compiler_version;
329 : :
330 : : /* The target version. */
331 : :
332 : : static const char *const spec_version = DEFAULT_TARGET_VERSION;
333 : :
334 : : /* The target machine. */
335 : :
336 : : static const char *spec_machine = DEFAULT_TARGET_MACHINE;
337 : : static const char *spec_host_machine = DEFAULT_REAL_TARGET_MACHINE;
338 : :
339 : : /* List of offload targets. Separated by colon. Empty string for
340 : : -foffload=disable. */
341 : :
342 : : static char *offload_targets = NULL;
343 : :
344 : : #if OFFLOAD_DEFAULTED
345 : : /* Set to true if -foffload has not been used and offload_targets
346 : : is set to the configured in default. */
347 : : static bool offload_targets_default;
348 : : #endif
349 : :
350 : : /* Nonzero if cross-compiling.
351 : : When -b is used, the value comes from the `specs' file. */
352 : :
353 : : #ifdef CROSS_DIRECTORY_STRUCTURE
354 : : static const char *cross_compile = "1";
355 : : #else
356 : : static const char *cross_compile = "0";
357 : : #endif
358 : :
359 : : /* Greatest exit code of sub-processes that has been encountered up to
360 : : now. */
361 : : static int greatest_status = 1;
362 : :
363 : : /* This is the obstack which we use to allocate many strings. */
364 : :
365 : : static struct obstack obstack;
366 : :
367 : : /* This is the obstack to build an environment variable to pass to
368 : : collect2 that describes all of the relevant switches of what to
369 : : pass the compiler in building the list of pointers to constructors
370 : : and destructors. */
371 : :
372 : : static struct obstack collect_obstack;
373 : :
374 : : /* Forward declaration for prototypes. */
375 : : struct path_prefix;
376 : : struct prefix_list;
377 : :
378 : : static void init_spec (void);
379 : : static void store_arg (const char *, int, int);
380 : : static void insert_wrapper (const char *);
381 : : static char *load_specs (const char *);
382 : : static void read_specs (const char *, bool, bool);
383 : : static void set_spec (const char *, const char *, bool);
384 : : static struct compiler *lookup_compiler (const char *, size_t, const char *);
385 : : static char *build_search_list (const struct path_prefix *, const char *,
386 : : bool, bool);
387 : : static void xputenv (const char *);
388 : : static void putenv_from_prefixes (const struct path_prefix *, const char *,
389 : : bool);
390 : : static int access_check (const char *, int);
391 : : static char *find_a_file (const struct path_prefix *, const char *, int, bool);
392 : : static char *find_a_program (const char *);
393 : : static void add_prefix (struct path_prefix *, const char *, const char *,
394 : : int, int, int);
395 : : static void add_sysrooted_prefix (struct path_prefix *, const char *,
396 : : const char *, int, int, int);
397 : : static char *skip_whitespace (char *);
398 : : static void delete_if_ordinary (const char *);
399 : : static void delete_temp_files (void);
400 : : static void delete_failure_queue (void);
401 : : static void clear_failure_queue (void);
402 : : static int check_live_switch (int, int);
403 : : static const char *handle_braces (const char *);
404 : : static inline bool input_suffix_matches (const char *, const char *);
405 : : static inline bool switch_matches (const char *, const char *, int);
406 : : static inline void mark_matching_switches (const char *, const char *, int);
407 : : static inline void process_marked_switches (void);
408 : : static const char *process_brace_body (const char *, const char *, const char *, int, int);
409 : : static const struct spec_function *lookup_spec_function (const char *);
410 : : static const char *eval_spec_function (const char *, const char *, const char *);
411 : : static const char *handle_spec_function (const char *, bool *, const char *);
412 : : static char *save_string (const char *, int);
413 : : static void set_collect_gcc_options (void);
414 : : static int do_spec_1 (const char *, int, const char *);
415 : : static int do_spec_2 (const char *, const char *);
416 : : static void do_option_spec (const char *, const char *);
417 : : static void do_self_spec (const char *);
418 : : static const char *find_file (const char *);
419 : : static int is_directory (const char *);
420 : : static const char *validate_switches (const char *, bool, bool);
421 : : static void validate_all_switches (void);
422 : : static inline void validate_switches_from_spec (const char *, bool);
423 : : static void give_switch (int, int);
424 : : static int default_arg (const char *, int);
425 : : static void set_multilib_dir (void);
426 : : static void print_multilib_info (void);
427 : : static void display_help (void);
428 : : static void add_preprocessor_option (const char *, int);
429 : : static void add_assembler_option (const char *, int);
430 : : static void add_linker_option (const char *, int);
431 : : static void process_command (unsigned int, struct cl_decoded_option *);
432 : : static int execute (void);
433 : : static void alloc_args (void);
434 : : static void clear_args (void);
435 : : static void fatal_signal (int);
436 : : #if defined(ENABLE_SHARED_LIBGCC) && !defined(REAL_LIBGCC_SPEC)
437 : : static void init_gcc_specs (struct obstack *, const char *, const char *,
438 : : const char *);
439 : : #endif
440 : : #if defined(HAVE_TARGET_OBJECT_SUFFIX) || defined(HAVE_TARGET_EXECUTABLE_SUFFIX)
441 : : static const char *convert_filename (const char *, int, int);
442 : : #endif
443 : :
444 : : static void try_generate_repro (const char **argv);
445 : : static const char *getenv_spec_function (int, const char **);
446 : : static const char *if_exists_spec_function (int, const char **);
447 : : static const char *if_exists_else_spec_function (int, const char **);
448 : : static const char *if_exists_then_else_spec_function (int, const char **);
449 : : static const char *sanitize_spec_function (int, const char **);
450 : : static const char *replace_outfile_spec_function (int, const char **);
451 : : static const char *remove_outfile_spec_function (int, const char **);
452 : : static const char *version_compare_spec_function (int, const char **);
453 : : static const char *include_spec_function (int, const char **);
454 : : static const char *find_file_spec_function (int, const char **);
455 : : static const char *find_plugindir_spec_function (int, const char **);
456 : : static const char *print_asm_header_spec_function (int, const char **);
457 : : static const char *compare_debug_dump_opt_spec_function (int, const char **);
458 : : static const char *compare_debug_self_opt_spec_function (int, const char **);
459 : : static const char *pass_through_libs_spec_func (int, const char **);
460 : : static const char *dumps_spec_func (int, const char **);
461 : : static const char *greater_than_spec_func (int, const char **);
462 : : static const char *debug_level_greater_than_spec_func (int, const char **);
463 : : static const char *dwarf_version_greater_than_spec_func (int, const char **);
464 : : static const char *find_fortran_preinclude_file (int, const char **);
465 : : static const char *join_spec_func (int, const char **);
466 : : static char *convert_white_space (char *);
467 : : static char *quote_spec (char *);
468 : : static char *quote_spec_arg (char *);
469 : : static bool not_actual_file_p (const char *);
470 : :
471 : :
472 : : /* The Specs Language
473 : :
474 : : Specs are strings containing lines, each of which (if not blank)
475 : : is made up of a program name, and arguments separated by spaces.
476 : : The program name must be exact and start from root, since no path
477 : : is searched and it is unreliable to depend on the current working directory.
478 : : Redirection of input or output is not supported; the subprograms must
479 : : accept filenames saying what files to read and write.
480 : :
481 : : In addition, the specs can contain %-sequences to substitute variable text
482 : : or for conditional text. Here is a table of all defined %-sequences.
483 : : Note that spaces are not generated automatically around the results of
484 : : expanding these sequences; therefore, you can concatenate them together
485 : : or with constant text in a single argument.
486 : :
487 : : %% substitute one % into the program name or argument.
488 : : %" substitute an empty argument.
489 : : %i substitute the name of the input file being processed.
490 : : %b substitute the basename for outputs related with the input file
491 : : being processed. This is often a substring of the input file name,
492 : : up to (and not including) the last period but, unless %w is active,
493 : : it is affected by the directory selected by -save-temps=*, by
494 : : -dumpdir, and, in case of multiple compilations, even by -dumpbase
495 : : and -dumpbase-ext and, in case of linking, by the linker output
496 : : name. When %w is active, it derives the main output name only from
497 : : the input file base name; when it is not, it names aux/dump output
498 : : file.
499 : : %B same as %b, but include the input file suffix (text after the last
500 : : period).
501 : : %gSUFFIX
502 : : substitute a file name that has suffix SUFFIX and is chosen
503 : : once per compilation, and mark the argument a la %d. To reduce
504 : : exposure to denial-of-service attacks, the file name is now
505 : : chosen in a way that is hard to predict even when previously
506 : : chosen file names are known. For example, `%g.s ... %g.o ... %g.s'
507 : : might turn into `ccUVUUAU.s ccXYAXZ12.o ccUVUUAU.s'. SUFFIX matches
508 : : the regexp "[.0-9A-Za-z]*%O"; "%O" is treated exactly as if it
509 : : had been pre-processed. Previously, %g was simply substituted
510 : : with a file name chosen once per compilation, without regard
511 : : to any appended suffix (which was therefore treated just like
512 : : ordinary text), making such attacks more likely to succeed.
513 : : %|SUFFIX
514 : : like %g, but if -pipe is in effect, expands simply to "-".
515 : : %mSUFFIX
516 : : like %g, but if -pipe is in effect, expands to nothing. (We have both
517 : : %| and %m to accommodate differences between system assemblers; see
518 : : the AS_NEEDS_DASH_FOR_PIPED_INPUT target macro.)
519 : : %uSUFFIX
520 : : like %g, but generates a new temporary file name even if %uSUFFIX
521 : : was already seen.
522 : : %USUFFIX
523 : : substitutes the last file name generated with %uSUFFIX, generating a
524 : : new one if there is no such last file name. In the absence of any
525 : : %uSUFFIX, this is just like %gSUFFIX, except they don't share
526 : : the same suffix "space", so `%g.s ... %U.s ... %g.s ... %U.s'
527 : : would involve the generation of two distinct file names, one
528 : : for each `%g.s' and another for each `%U.s'. Previously, %U was
529 : : simply substituted with a file name chosen for the previous %u,
530 : : without regard to any appended suffix.
531 : : %jSUFFIX
532 : : substitutes the name of the HOST_BIT_BUCKET, if any, and if it is
533 : : writable, and if save-temps is off; otherwise, substitute the name
534 : : of a temporary file, just like %u. This temporary file is not
535 : : meant for communication between processes, but rather as a junk
536 : : disposal mechanism.
537 : : %.SUFFIX
538 : : substitutes .SUFFIX for the suffixes of a matched switch's args when
539 : : it is subsequently output with %*. SUFFIX is terminated by the next
540 : : space or %.
541 : : %d marks the argument containing or following the %d as a
542 : : temporary file name, so that file will be deleted if GCC exits
543 : : successfully. Unlike %g, this contributes no text to the argument.
544 : : %w marks the argument containing or following the %w as the
545 : : "output file" of this compilation. This puts the argument
546 : : into the sequence of arguments that %o will substitute later.
547 : : %V indicates that this compilation produces no "output file".
548 : : %W{...}
549 : : like %{...} but marks the last argument supplied within as a file
550 : : to be deleted on failure.
551 : : %@{...}
552 : : like %{...} but puts the result into a FILE and substitutes @FILE
553 : : if an @file argument has been supplied.
554 : : %o substitutes the names of all the output files, with spaces
555 : : automatically placed around them. You should write spaces
556 : : around the %o as well or the results are undefined.
557 : : %o is for use in the specs for running the linker.
558 : : Input files whose names have no recognized suffix are not compiled
559 : : at all, but they are included among the output files, so they will
560 : : be linked.
561 : : %O substitutes the suffix for object files. Note that this is
562 : : handled specially when it immediately follows %g, %u, or %U
563 : : (with or without a suffix argument) because of the need for
564 : : those to form complete file names. The handling is such that
565 : : %O is treated exactly as if it had already been substituted,
566 : : except that %g, %u, and %U do not currently support additional
567 : : SUFFIX characters following %O as they would following, for
568 : : example, `.o'.
569 : : %I Substitute any of -iprefix (made from GCC_EXEC_PREFIX), -isysroot
570 : : (made from TARGET_SYSTEM_ROOT), -isystem (made from COMPILER_PATH
571 : : and -B options) and -imultilib as necessary.
572 : : %s current argument is the name of a library or startup file of some sort.
573 : : Search for that file in a standard list of directories
574 : : and substitute the full name found.
575 : : %T current argument is the name of a linker script.
576 : : Search for that file in the current list of directories to scan for
577 : : libraries. If the file is located, insert a --script option into the
578 : : command line followed by the full path name found. If the file is
579 : : not found then generate an error message.
580 : : Note: the current working directory is not searched.
581 : : %eSTR Print STR as an error message. STR is terminated by a newline.
582 : : Use this when inconsistent options are detected.
583 : : %nSTR Print STR as a notice. STR is terminated by a newline.
584 : : %x{OPTION} Accumulate an option for %X.
585 : : %X Output the accumulated linker options specified by compilations.
586 : : %Y Output the accumulated assembler options specified by compilations.
587 : : %Z Output the accumulated preprocessor options specified by compilations.
588 : : %a process ASM_SPEC as a spec.
589 : : This allows config.h to specify part of the spec for running as.
590 : : %A process ASM_FINAL_SPEC as a spec. A capital A is actually
591 : : used here. This can be used to run a post-processor after the
592 : : assembler has done its job.
593 : : %D Dump out a -L option for each directory in startfile_prefixes.
594 : : If multilib_dir is set, extra entries are generated with it affixed.
595 : : %l process LINK_SPEC as a spec.
596 : : %L process LIB_SPEC as a spec.
597 : : %M Output multilib_os_dir.
598 : : %P Output a RUNPATH_OPTION for each directory in startfile_prefixes.
599 : : %G process LIBGCC_SPEC as a spec.
600 : : %R Output the concatenation of target_system_root and
601 : : target_sysroot_suffix.
602 : : %S process STARTFILE_SPEC as a spec. A capital S is actually used here.
603 : : %E process ENDFILE_SPEC as a spec. A capital E is actually used here.
604 : : %C process CPP_SPEC as a spec.
605 : : %1 process CC1_SPEC as a spec.
606 : : %2 process CC1PLUS_SPEC as a spec.
607 : : %* substitute the variable part of a matched option. (See below.)
608 : : Note that each comma in the substituted string is replaced by
609 : : a single space. A space is appended after the last substition
610 : : unless there is more text in current sequence.
611 : : %<S remove all occurrences of -S from the command line.
612 : : Note - this command is position dependent. % commands in the
613 : : spec string before this one will see -S, % commands in the
614 : : spec string after this one will not.
615 : : %>S Similar to "%<S", but keep it in the GCC command line.
616 : : %<S* remove all occurrences of all switches beginning with -S from the
617 : : command line.
618 : : %:function(args)
619 : : Call the named function FUNCTION, passing it ARGS. ARGS is
620 : : first processed as a nested spec string, then split into an
621 : : argument vector in the usual fashion. The function returns
622 : : a string which is processed as if it had appeared literally
623 : : as part of the current spec.
624 : : %{S} substitutes the -S switch, if that switch was given to GCC.
625 : : If that switch was not specified, this substitutes nothing.
626 : : Here S is a metasyntactic variable.
627 : : %{S*} substitutes all the switches specified to GCC whose names start
628 : : with -S. This is used for -o, -I, etc; switches that take
629 : : arguments. GCC considers `-o foo' as being one switch whose
630 : : name starts with `o'. %{o*} would substitute this text,
631 : : including the space; thus, two arguments would be generated.
632 : : %{S*&T*} likewise, but preserve order of S and T options (the order
633 : : of S and T in the spec is not significant). Can be any number
634 : : of ampersand-separated variables; for each the wild card is
635 : : optional. Useful for CPP as %{D*&U*&A*}.
636 : :
637 : : %{S:X} substitutes X, if the -S switch was given to GCC.
638 : : %{!S:X} substitutes X, if the -S switch was NOT given to GCC.
639 : : %{S*:X} substitutes X if one or more switches whose names start
640 : : with -S was given to GCC. Normally X is substituted only
641 : : once, no matter how many such switches appeared. However,
642 : : if %* appears somewhere in X, then X will be substituted
643 : : once for each matching switch, with the %* replaced by the
644 : : part of that switch that matched the '*'. A space will be
645 : : appended after the last substition unless there is more
646 : : text in current sequence.
647 : : %{.S:X} substitutes X, if processing a file with suffix S.
648 : : %{!.S:X} substitutes X, if NOT processing a file with suffix S.
649 : : %{,S:X} substitutes X, if processing a file which will use spec S.
650 : : %{!,S:X} substitutes X, if NOT processing a file which will use spec S.
651 : :
652 : : %{S|T:X} substitutes X if either -S or -T was given to GCC. This may be
653 : : combined with '!', '.', ',', and '*' as above binding stronger
654 : : than the OR.
655 : : If %* appears in X, all of the alternatives must be starred, and
656 : : only the first matching alternative is substituted.
657 : : %{%:function(args):X}
658 : : Call function named FUNCTION with args ARGS. If the function
659 : : returns non-NULL, then X is substituted, if it returns
660 : : NULL, it isn't substituted.
661 : : %{S:X; if S was given to GCC, substitutes X;
662 : : T:Y; else if T was given to GCC, substitutes Y;
663 : : :D} else substitutes D. There can be as many clauses as you need.
664 : : This may be combined with '.', '!', ',', '|', and '*' as above.
665 : :
666 : : %(Spec) processes a specification defined in a specs file as *Spec:
667 : :
668 : : The switch matching text S in a %{S}, %{S:X}, or similar construct can use
669 : : a backslash to ignore the special meaning of the character following it,
670 : : thus allowing literal matching of a character that is otherwise specially
671 : : treated. For example, %{std=iso9899\:1999:X} substitutes X if the
672 : : -std=iso9899:1999 option is given.
673 : :
674 : : The conditional text X in a %{S:X} or similar construct may contain
675 : : other nested % constructs or spaces, or even newlines. They are
676 : : processed as usual, as described above. Trailing white space in X is
677 : : ignored. White space may also appear anywhere on the left side of the
678 : : colon in these constructs, except between . or * and the corresponding
679 : : word.
680 : :
681 : : The -O, -f, -g, -m, and -W switches are handled specifically in these
682 : : constructs. If another value of -O or the negated form of a -f, -m, or
683 : : -W switch is found later in the command line, the earlier switch
684 : : value is ignored, except with {S*} where S is just one letter; this
685 : : passes all matching options.
686 : :
687 : : The character | at the beginning of the predicate text is used to indicate
688 : : that a command should be piped to the following command, but only if -pipe
689 : : is specified.
690 : :
691 : : Note that it is built into GCC which switches take arguments and which
692 : : do not. You might think it would be useful to generalize this to
693 : : allow each compiler's spec to say which switches take arguments. But
694 : : this cannot be done in a consistent fashion. GCC cannot even decide
695 : : which input files have been specified without knowing which switches
696 : : take arguments, and it must know which input files to compile in order
697 : : to tell which compilers to run.
698 : :
699 : : GCC also knows implicitly that arguments starting in `-l' are to be
700 : : treated as compiler output files, and passed to the linker in their
701 : : proper position among the other output files. */
702 : :
703 : : /* Define the macros used for specs %a, %l, %L, %S, %C, %1. */
704 : :
705 : : /* config.h can define ASM_SPEC to provide extra args to the assembler
706 : : or extra switch-translations. */
707 : : #ifndef ASM_SPEC
708 : : #define ASM_SPEC ""
709 : : #endif
710 : :
711 : : /* config.h can define ASM_FINAL_SPEC to run a post processor after
712 : : the assembler has run. */
713 : : #ifndef ASM_FINAL_SPEC
714 : : #define ASM_FINAL_SPEC \
715 : : "%{gsplit-dwarf: \n\
716 : : objcopy --extract-dwo \
717 : : %{c:%{o*:%*}%{!o*:%w%b%O}}%{!c:%U%O} \
718 : : %b.dwo \n\
719 : : objcopy --strip-dwo \
720 : : %{c:%{o*:%*}%{!o*:%w%b%O}}%{!c:%U%O} \
721 : : }"
722 : : #endif
723 : :
724 : : /* config.h can define CPP_SPEC to provide extra args to the C preprocessor
725 : : or extra switch-translations. */
726 : : #ifndef CPP_SPEC
727 : : #define CPP_SPEC ""
728 : : #endif
729 : :
730 : : /* Operating systems can define OS_CC1_SPEC to provide extra args to cc1 and
731 : : cc1plus or extra switch-translations. The OS_CC1_SPEC is appended
732 : : to CC1_SPEC in the initialization of cc1_spec. */
733 : : #ifndef OS_CC1_SPEC
734 : : #define OS_CC1_SPEC ""
735 : : #endif
736 : :
737 : : /* config.h can define CC1_SPEC to provide extra args to cc1 and cc1plus
738 : : or extra switch-translations. */
739 : : #ifndef CC1_SPEC
740 : : #define CC1_SPEC ""
741 : : #endif
742 : :
743 : : /* config.h can define CC1PLUS_SPEC to provide extra args to cc1plus
744 : : or extra switch-translations. */
745 : : #ifndef CC1PLUS_SPEC
746 : : #define CC1PLUS_SPEC ""
747 : : #endif
748 : :
749 : : /* config.h can define LINK_SPEC to provide extra args to the linker
750 : : or extra switch-translations. */
751 : : #ifndef LINK_SPEC
752 : : #define LINK_SPEC ""
753 : : #endif
754 : :
755 : : /* config.h can define LIB_SPEC to override the default libraries. */
756 : : #ifndef LIB_SPEC
757 : : #define LIB_SPEC "%{!shared:%{g*:-lg} %{!p:%{!pg:-lc}}%{p:-lc_p}%{pg:-lc_p}}"
758 : : #endif
759 : :
760 : : /* When using -fsplit-stack we need to wrap pthread_create, in order
761 : : to initialize the stack guard. We always use wrapping, rather than
762 : : shared library ordering, and we keep the wrapper function in
763 : : libgcc. This is not yet a real spec, though it could become one;
764 : : it is currently just stuffed into LINK_SPEC. FIXME: This wrapping
765 : : only works with GNU ld and gold. */
766 : : #ifdef HAVE_GOLD_NON_DEFAULT_SPLIT_STACK
767 : : #define STACK_SPLIT_SPEC " %{fsplit-stack: -fuse-ld=gold --wrap=pthread_create}"
768 : : #else
769 : : #define STACK_SPLIT_SPEC " %{fsplit-stack: --wrap=pthread_create}"
770 : : #endif
771 : :
772 : : #ifndef LIBASAN_SPEC
773 : : #define STATIC_LIBASAN_LIBS \
774 : : " %{static-libasan|static:%:include(libsanitizer.spec)%(link_libasan)}"
775 : : #ifdef LIBASAN_EARLY_SPEC
776 : : #define LIBASAN_SPEC STATIC_LIBASAN_LIBS
777 : : #elif defined(HAVE_LD_STATIC_DYNAMIC)
778 : : #define LIBASAN_SPEC "%{static-libasan:" LD_STATIC_OPTION \
779 : : "} -lasan %{static-libasan:" LD_DYNAMIC_OPTION "}" \
780 : : STATIC_LIBASAN_LIBS
781 : : #else
782 : : #define LIBASAN_SPEC "-lasan" STATIC_LIBASAN_LIBS
783 : : #endif
784 : : #endif
785 : :
786 : : #ifndef LIBASAN_EARLY_SPEC
787 : : #define LIBASAN_EARLY_SPEC ""
788 : : #endif
789 : :
790 : : #ifndef LIBHWASAN_SPEC
791 : : #define STATIC_LIBHWASAN_LIBS \
792 : : " %{static-libhwasan|static:%:include(libsanitizer.spec)%(link_libhwasan)}"
793 : : #ifdef LIBHWASAN_EARLY_SPEC
794 : : #define LIBHWASAN_SPEC STATIC_LIBHWASAN_LIBS
795 : : #elif defined(HAVE_LD_STATIC_DYNAMIC)
796 : : #define LIBHWASAN_SPEC "%{static-libhwasan:" LD_STATIC_OPTION \
797 : : "} -lhwasan %{static-libhwasan:" LD_DYNAMIC_OPTION "}" \
798 : : STATIC_LIBHWASAN_LIBS
799 : : #else
800 : : #define LIBHWASAN_SPEC "-lhwasan" STATIC_LIBHWASAN_LIBS
801 : : #endif
802 : : #endif
803 : :
804 : : #ifndef LIBHWASAN_EARLY_SPEC
805 : : #define LIBHWASAN_EARLY_SPEC ""
806 : : #endif
807 : :
808 : : #ifndef LIBTSAN_SPEC
809 : : #define STATIC_LIBTSAN_LIBS \
810 : : " %{static-libtsan|static:%:include(libsanitizer.spec)%(link_libtsan)}"
811 : : #ifdef LIBTSAN_EARLY_SPEC
812 : : #define LIBTSAN_SPEC STATIC_LIBTSAN_LIBS
813 : : #elif defined(HAVE_LD_STATIC_DYNAMIC)
814 : : #define LIBTSAN_SPEC "%{static-libtsan:" LD_STATIC_OPTION \
815 : : "} -ltsan %{static-libtsan:" LD_DYNAMIC_OPTION "}" \
816 : : STATIC_LIBTSAN_LIBS
817 : : #else
818 : : #define LIBTSAN_SPEC "-ltsan" STATIC_LIBTSAN_LIBS
819 : : #endif
820 : : #endif
821 : :
822 : : #ifndef LIBTSAN_EARLY_SPEC
823 : : #define LIBTSAN_EARLY_SPEC ""
824 : : #endif
825 : :
826 : : #ifndef LIBLSAN_SPEC
827 : : #define STATIC_LIBLSAN_LIBS \
828 : : " %{static-liblsan|static:%:include(libsanitizer.spec)%(link_liblsan)}"
829 : : #ifdef LIBLSAN_EARLY_SPEC
830 : : #define LIBLSAN_SPEC STATIC_LIBLSAN_LIBS
831 : : #elif defined(HAVE_LD_STATIC_DYNAMIC)
832 : : #define LIBLSAN_SPEC "%{static-liblsan:" LD_STATIC_OPTION \
833 : : "} -llsan %{static-liblsan:" LD_DYNAMIC_OPTION "}" \
834 : : STATIC_LIBLSAN_LIBS
835 : : #else
836 : : #define LIBLSAN_SPEC "-llsan" STATIC_LIBLSAN_LIBS
837 : : #endif
838 : : #endif
839 : :
840 : : #ifndef LIBLSAN_EARLY_SPEC
841 : : #define LIBLSAN_EARLY_SPEC ""
842 : : #endif
843 : :
844 : : #ifndef LIBUBSAN_SPEC
845 : : #define STATIC_LIBUBSAN_LIBS \
846 : : " %{static-libubsan|static:%:include(libsanitizer.spec)%(link_libubsan)}"
847 : : #ifdef HAVE_LD_STATIC_DYNAMIC
848 : : #define LIBUBSAN_SPEC "%{static-libubsan:" LD_STATIC_OPTION \
849 : : "} -lubsan %{static-libubsan:" LD_DYNAMIC_OPTION "}" \
850 : : STATIC_LIBUBSAN_LIBS
851 : : #else
852 : : #define LIBUBSAN_SPEC "-lubsan" STATIC_LIBUBSAN_LIBS
853 : : #endif
854 : : #endif
855 : :
856 : : /* Linker options for compressed debug sections. */
857 : : #if HAVE_LD_COMPRESS_DEBUG == 0
858 : : /* No linker support. */
859 : : #define LINK_COMPRESS_DEBUG_SPEC \
860 : : " %{gz*:%e-gz is not supported in this configuration} "
861 : : #elif HAVE_LD_COMPRESS_DEBUG == 1
862 : : /* ELF gABI style. */
863 : : #define LINK_COMPRESS_DEBUG_SPEC \
864 : : " %{gz|gz=zlib:" LD_COMPRESS_DEBUG_OPTION "=zlib}" \
865 : : " %{gz=none:" LD_COMPRESS_DEBUG_OPTION "=none}" \
866 : : " %{gz=zstd:%e-gz=zstd is not supported in this configuration} " \
867 : : " %{gz=zlib-gnu:}" /* Ignore silently zlib-gnu option value. */
868 : : #elif HAVE_LD_COMPRESS_DEBUG == 2
869 : : /* ELF gABI style and ZSTD. */
870 : : #define LINK_COMPRESS_DEBUG_SPEC \
871 : : " %{gz|gz=zlib:" LD_COMPRESS_DEBUG_OPTION "=zlib}" \
872 : : " %{gz=none:" LD_COMPRESS_DEBUG_OPTION "=none}" \
873 : : " %{gz=zstd:" LD_COMPRESS_DEBUG_OPTION "=zstd}" \
874 : : " %{gz=zlib-gnu:}" /* Ignore silently zlib-gnu option value. */
875 : : #else
876 : : #error Unknown value for HAVE_LD_COMPRESS_DEBUG.
877 : : #endif
878 : :
879 : : /* config.h can define LIBGCC_SPEC to override how and when libgcc.a is
880 : : included. */
881 : : #ifndef LIBGCC_SPEC
882 : : #if defined(REAL_LIBGCC_SPEC)
883 : : #define LIBGCC_SPEC REAL_LIBGCC_SPEC
884 : : #elif defined(LINK_LIBGCC_SPECIAL_1)
885 : : /* Have gcc do the search for libgcc.a. */
886 : : #define LIBGCC_SPEC "libgcc.a%s"
887 : : #else
888 : : #define LIBGCC_SPEC "-lgcc"
889 : : #endif
890 : : #endif
891 : :
892 : : /* config.h can define STARTFILE_SPEC to override the default crt0 files. */
893 : : #ifndef STARTFILE_SPEC
894 : : #define STARTFILE_SPEC \
895 : : "%{!shared:%{pg:gcrt0%O%s}%{!pg:%{p:mcrt0%O%s}%{!p:crt0%O%s}}}"
896 : : #endif
897 : :
898 : : /* config.h can define ENDFILE_SPEC to override the default crtn files. */
899 : : #ifndef ENDFILE_SPEC
900 : : #define ENDFILE_SPEC ""
901 : : #endif
902 : :
903 : : #ifndef LINKER_NAME
904 : : #define LINKER_NAME "collect2"
905 : : #endif
906 : :
907 : : #ifdef HAVE_AS_DEBUG_PREFIX_MAP
908 : : #define ASM_MAP " %{ffile-prefix-map=*:--debug-prefix-map %*} %{fdebug-prefix-map=*:--debug-prefix-map %*}"
909 : : #else
910 : : #define ASM_MAP ""
911 : : #endif
912 : :
913 : : /* Assembler options for compressed debug sections. */
914 : : #if HAVE_LD_COMPRESS_DEBUG == 0
915 : : /* Reject if the linker cannot write compressed debug sections. */
916 : : #define ASM_COMPRESS_DEBUG_SPEC \
917 : : " %{gz*:%e-gz is not supported in this configuration} "
918 : : #else /* HAVE_LD_COMPRESS_DEBUG >= 1 */
919 : : #if HAVE_AS_COMPRESS_DEBUG == 0
920 : : /* No assembler support. Ignore silently. */
921 : : #define ASM_COMPRESS_DEBUG_SPEC \
922 : : " %{gz*:} "
923 : : #elif HAVE_AS_COMPRESS_DEBUG == 1
924 : : /* ELF gABI style. */
925 : : #define ASM_COMPRESS_DEBUG_SPEC \
926 : : " %{gz|gz=zlib:" AS_COMPRESS_DEBUG_OPTION "=zlib}" \
927 : : " %{gz=none:" AS_COMPRESS_DEBUG_OPTION "=none}" \
928 : : " %{gz=zlib-gnu:}" /* Ignore silently zlib-gnu option value. */
929 : : #elif HAVE_AS_COMPRESS_DEBUG == 2
930 : : /* ELF gABI style and ZSTD. */
931 : : #define ASM_COMPRESS_DEBUG_SPEC \
932 : : " %{gz|gz=zlib:" AS_COMPRESS_DEBUG_OPTION "=zlib}" \
933 : : " %{gz=none:" AS_COMPRESS_DEBUG_OPTION "=none}" \
934 : : " %{gz=zstd:" AS_COMPRESS_DEBUG_OPTION "=zstd}" \
935 : : " %{gz=zlib-gnu:}" /* Ignore silently zlib-gnu option value. */
936 : : #else
937 : : #error Unknown value for HAVE_AS_COMPRESS_DEBUG.
938 : : #endif
939 : : #endif /* HAVE_LD_COMPRESS_DEBUG >= 1 */
940 : :
941 : : /* Define ASM_DEBUG_SPEC to be a spec suitable for translating '-g'
942 : : to the assembler, when compiling assembly sources only. */
943 : : #ifndef ASM_DEBUG_SPEC
944 : : # if defined(HAVE_AS_GDWARF_5_DEBUG_FLAG) && defined(HAVE_AS_WORKING_DWARF_N_FLAG)
945 : : /* If --gdwarf-N is supported and as can handle even compiler generated
946 : : .debug_line with it, supply --gdwarf-N in ASM_DEBUG_OPTION_SPEC rather
947 : : than in ASM_DEBUG_SPEC, so that it applies to both .s and .c etc.
948 : : compilations. */
949 : : # define ASM_DEBUG_DWARF_OPTION ""
950 : : # elif defined(HAVE_AS_GDWARF_5_DEBUG_FLAG) && !defined(HAVE_LD_BROKEN_PE_DWARF5)
951 : : # define ASM_DEBUG_DWARF_OPTION "%{%:dwarf-version-gt(4):--gdwarf-5;" \
952 : : "%:dwarf-version-gt(3):--gdwarf-4;" \
953 : : "%:dwarf-version-gt(2):--gdwarf-3;" \
954 : : ":--gdwarf2}"
955 : : # else
956 : : # define ASM_DEBUG_DWARF_OPTION "--gdwarf2"
957 : : # endif
958 : : # if defined(DWARF2_DEBUGGING_INFO) && defined(HAVE_AS_GDWARF2_DEBUG_FLAG)
959 : : # define ASM_DEBUG_SPEC "%{g*:%{%:debug-level-gt(0):" \
960 : : ASM_DEBUG_DWARF_OPTION "}}" ASM_MAP
961 : : # endif
962 : : # endif
963 : : #ifndef ASM_DEBUG_SPEC
964 : : # define ASM_DEBUG_SPEC ""
965 : : #endif
966 : :
967 : : /* Define ASM_DEBUG_OPTION_SPEC to be a spec suitable for translating '-g'
968 : : to the assembler when compiling all sources. */
969 : : #ifndef ASM_DEBUG_OPTION_SPEC
970 : : # if defined(HAVE_AS_GDWARF_5_DEBUG_FLAG) && defined(HAVE_AS_WORKING_DWARF_N_FLAG)
971 : : # define ASM_DEBUG_OPTION_DWARF_OPT \
972 : : "%{%:dwarf-version-gt(4):--gdwarf-5 ;" \
973 : : "%:dwarf-version-gt(3):--gdwarf-4 ;" \
974 : : "%:dwarf-version-gt(2):--gdwarf-3 ;" \
975 : : ":--gdwarf2 }"
976 : : # if defined(DWARF2_DEBUGGING_INFO)
977 : : # define ASM_DEBUG_OPTION_SPEC "%{g*:%{%:debug-level-gt(0):" \
978 : : ASM_DEBUG_OPTION_DWARF_OPT "}}"
979 : : # endif
980 : : # endif
981 : : #endif
982 : : #ifndef ASM_DEBUG_OPTION_SPEC
983 : : # define ASM_DEBUG_OPTION_SPEC ""
984 : : #endif
985 : :
986 : : /* Here is the spec for running the linker, after compiling all files. */
987 : :
988 : : #if defined(TARGET_PROVIDES_LIBATOMIC) && defined(USE_LD_AS_NEEDED)
989 : : #define LINK_LIBATOMIC_SPEC "%{!fno-link-libatomic:" LD_AS_NEEDED_OPTION \
990 : : " -latomic " LD_NO_AS_NEEDED_OPTION "} "
991 : : #else
992 : : #define LINK_LIBATOMIC_SPEC ""
993 : : #endif
994 : :
995 : : /* This is overridable by the target in case they need to specify the
996 : : -lgcc and -lc order specially, yet not require them to override all
997 : : of LINK_COMMAND_SPEC. */
998 : : #ifndef LINK_GCC_C_SEQUENCE_SPEC
999 : : #define LINK_GCC_C_SEQUENCE_SPEC "%G %{!nolibc:%L %G}"
1000 : : #endif
1001 : :
1002 : : #ifndef LINK_SSP_SPEC
1003 : : #ifdef TARGET_LIBC_PROVIDES_SSP
1004 : : #define LINK_SSP_SPEC "%{fstack-protector|fstack-protector-all" \
1005 : : "|fstack-protector-strong|fstack-protector-explicit:}"
1006 : : #else
1007 : : #define LINK_SSP_SPEC "%{fstack-protector|fstack-protector-all" \
1008 : : "|fstack-protector-strong|fstack-protector-explicit" \
1009 : : ":-lssp_nonshared -lssp}"
1010 : : #endif
1011 : : #endif
1012 : :
1013 : : #ifdef ENABLE_DEFAULT_PIE
1014 : : #define PIE_SPEC "!no-pie"
1015 : : #define NO_FPIE1_SPEC "fno-pie"
1016 : : #define FPIE1_SPEC NO_FPIE1_SPEC ":;"
1017 : : #define NO_FPIE2_SPEC "fno-PIE"
1018 : : #define FPIE2_SPEC NO_FPIE2_SPEC ":;"
1019 : : #define NO_FPIE_SPEC NO_FPIE1_SPEC "|" NO_FPIE2_SPEC
1020 : : #define FPIE_SPEC NO_FPIE_SPEC ":;"
1021 : : #define NO_FPIC1_SPEC "fno-pic"
1022 : : #define FPIC1_SPEC NO_FPIC1_SPEC ":;"
1023 : : #define NO_FPIC2_SPEC "fno-PIC"
1024 : : #define FPIC2_SPEC NO_FPIC2_SPEC ":;"
1025 : : #define NO_FPIC_SPEC NO_FPIC1_SPEC "|" NO_FPIC2_SPEC
1026 : : #define FPIC_SPEC NO_FPIC_SPEC ":;"
1027 : : #define NO_FPIE1_AND_FPIC1_SPEC NO_FPIE1_SPEC "|" NO_FPIC1_SPEC
1028 : : #define FPIE1_OR_FPIC1_SPEC NO_FPIE1_AND_FPIC1_SPEC ":;"
1029 : : #define NO_FPIE2_AND_FPIC2_SPEC NO_FPIE2_SPEC "|" NO_FPIC2_SPEC
1030 : : #define FPIE2_OR_FPIC2_SPEC NO_FPIE2_AND_FPIC2_SPEC ":;"
1031 : : #define NO_FPIE_AND_FPIC_SPEC NO_FPIE_SPEC "|" NO_FPIC_SPEC
1032 : : #define FPIE_OR_FPIC_SPEC NO_FPIE_AND_FPIC_SPEC ":;"
1033 : : #else
1034 : : #define PIE_SPEC "pie"
1035 : : #define FPIE1_SPEC "fpie"
1036 : : #define NO_FPIE1_SPEC FPIE1_SPEC ":;"
1037 : : #define FPIE2_SPEC "fPIE"
1038 : : #define NO_FPIE2_SPEC FPIE2_SPEC ":;"
1039 : : #define FPIE_SPEC FPIE1_SPEC "|" FPIE2_SPEC
1040 : : #define NO_FPIE_SPEC FPIE_SPEC ":;"
1041 : : #define FPIC1_SPEC "fpic"
1042 : : #define NO_FPIC1_SPEC FPIC1_SPEC ":;"
1043 : : #define FPIC2_SPEC "fPIC"
1044 : : #define NO_FPIC2_SPEC FPIC2_SPEC ":;"
1045 : : #define FPIC_SPEC FPIC1_SPEC "|" FPIC2_SPEC
1046 : : #define NO_FPIC_SPEC FPIC_SPEC ":;"
1047 : : #define FPIE1_OR_FPIC1_SPEC FPIE1_SPEC "|" FPIC1_SPEC
1048 : : #define NO_FPIE1_AND_FPIC1_SPEC FPIE1_OR_FPIC1_SPEC ":;"
1049 : : #define FPIE2_OR_FPIC2_SPEC FPIE2_SPEC "|" FPIC2_SPEC
1050 : : #define NO_FPIE2_AND_FPIC2_SPEC FPIE1_OR_FPIC2_SPEC ":;"
1051 : : #define FPIE_OR_FPIC_SPEC FPIE_SPEC "|" FPIC_SPEC
1052 : : #define NO_FPIE_AND_FPIC_SPEC FPIE_OR_FPIC_SPEC ":;"
1053 : : #endif
1054 : :
1055 : : #ifndef LINK_PIE_SPEC
1056 : : #ifdef HAVE_LD_PIE
1057 : : #ifndef LD_PIE_SPEC
1058 : : #define LD_PIE_SPEC "-pie"
1059 : : #endif
1060 : : #else
1061 : : #define LD_PIE_SPEC ""
1062 : : #endif
1063 : : #define LINK_PIE_SPEC "%{static|shared|r:;" PIE_SPEC ":" LD_PIE_SPEC "} "
1064 : : #endif
1065 : :
1066 : : #ifndef LINK_BUILDID_SPEC
1067 : : # if defined(HAVE_LD_BUILDID) && defined(ENABLE_LD_BUILDID)
1068 : : # define LINK_BUILDID_SPEC "%{!r:--build-id} "
1069 : : # endif
1070 : : #endif
1071 : :
1072 : : #ifndef LTO_PLUGIN_SPEC
1073 : : #define LTO_PLUGIN_SPEC ""
1074 : : #endif
1075 : :
1076 : : /* Conditional to test whether the LTO plugin is used or not.
1077 : : FIXME: For slim LTO we will need to enable plugin unconditionally. This
1078 : : still cause problems with PLUGIN_LD != LD and when plugin is built but
1079 : : not useable. For GCC 4.6 we don't support slim LTO and thus we can enable
1080 : : plugin only when LTO is enabled. We still honor explicit
1081 : : -fuse-linker-plugin if the linker used understands -plugin. */
1082 : :
1083 : : /* The linker has some plugin support. */
1084 : : #if HAVE_LTO_PLUGIN > 0
1085 : : /* The linker used has full plugin support, use LTO plugin by default. */
1086 : : #if HAVE_LTO_PLUGIN == 2
1087 : : #define PLUGIN_COND "!fno-use-linker-plugin:%{!fno-lto"
1088 : : #define PLUGIN_COND_CLOSE "}"
1089 : : #else
1090 : : /* The linker used has limited plugin support, use LTO plugin with explicit
1091 : : -fuse-linker-plugin. */
1092 : : #define PLUGIN_COND "fuse-linker-plugin"
1093 : : #define PLUGIN_COND_CLOSE ""
1094 : : #endif
1095 : : #define LINK_PLUGIN_SPEC \
1096 : : "%{" PLUGIN_COND": \
1097 : : -plugin %(linker_plugin_file) \
1098 : : -plugin-opt=%(lto_wrapper) \
1099 : : -plugin-opt=-fresolution=%u.res \
1100 : : " LTO_PLUGIN_SPEC "\
1101 : : %{flinker-output=*:-plugin-opt=-linker-output-known} \
1102 : : %{!nostdlib:%{!nodefaultlibs:%:pass-through-libs(%(link_gcc_c_sequence))}} \
1103 : : }" PLUGIN_COND_CLOSE
1104 : : #else
1105 : : /* The linker used doesn't support -plugin, reject -fuse-linker-plugin. */
1106 : : #define LINK_PLUGIN_SPEC "%{fuse-linker-plugin:\
1107 : : %e-fuse-linker-plugin is not supported in this configuration}"
1108 : : #endif
1109 : :
1110 : : /* Linker command line options for -fsanitize= early on the command line. */
1111 : : #ifndef SANITIZER_EARLY_SPEC
1112 : : #define SANITIZER_EARLY_SPEC "\
1113 : : %{!nostdlib:%{!r:%{!nodefaultlibs:%{%:sanitize(address):" LIBASAN_EARLY_SPEC "} \
1114 : : %{%:sanitize(hwaddress):" LIBHWASAN_EARLY_SPEC "} \
1115 : : %{%:sanitize(thread):" LIBTSAN_EARLY_SPEC "} \
1116 : : %{%:sanitize(leak):" LIBLSAN_EARLY_SPEC "}}}}"
1117 : : #endif
1118 : :
1119 : : /* Linker command line options for -fsanitize= late on the command line. */
1120 : : #ifndef SANITIZER_SPEC
1121 : : #define SANITIZER_SPEC "\
1122 : : %{!nostdlib:%{!r:%{!nodefaultlibs:%{%:sanitize(address):" LIBASAN_SPEC "\
1123 : : %{static:%ecannot specify -static with -fsanitize=address}}\
1124 : : %{%:sanitize(hwaddress):" LIBHWASAN_SPEC "\
1125 : : %{static:%ecannot specify -static with -fsanitize=hwaddress}}\
1126 : : %{%:sanitize(thread):" LIBTSAN_SPEC "\
1127 : : %{static:%ecannot specify -static with -fsanitize=thread}}\
1128 : : %{%:sanitize(undefined):" LIBUBSAN_SPEC "}\
1129 : : %{%:sanitize(leak):" LIBLSAN_SPEC "}}}}"
1130 : : #endif
1131 : :
1132 : : #ifndef POST_LINK_SPEC
1133 : : #define POST_LINK_SPEC ""
1134 : : #endif
1135 : :
1136 : : /* This is the spec to use, once the code for creating the vtable
1137 : : verification runtime library, libvtv.so, has been created. Currently
1138 : : the vtable verification runtime functions are in libstdc++, so we use
1139 : : the spec just below this one. */
1140 : : #ifndef VTABLE_VERIFICATION_SPEC
1141 : : #if ENABLE_VTABLE_VERIFY
1142 : : #define VTABLE_VERIFICATION_SPEC "\
1143 : : %{!nostdlib:%{!r:%{fvtable-verify=std: -lvtv -u_vtable_map_vars_start -u_vtable_map_vars_end}\
1144 : : %{fvtable-verify=preinit: -lvtv -u_vtable_map_vars_start -u_vtable_map_vars_end}}}"
1145 : : #else
1146 : : #define VTABLE_VERIFICATION_SPEC "\
1147 : : %{fvtable-verify=none:} \
1148 : : %{fvtable-verify=std: \
1149 : : %e-fvtable-verify=std is not supported in this configuration} \
1150 : : %{fvtable-verify=preinit: \
1151 : : %e-fvtable-verify=preinit is not supported in this configuration}"
1152 : : #endif
1153 : : #endif
1154 : :
1155 : : /* -u* was put back because both BSD and SysV seem to support it. */
1156 : : /* %{static|no-pie|static-pie:} simply prevents an error message:
1157 : : 1. If the target machine doesn't handle -static.
1158 : : 2. If PIE isn't enabled by default.
1159 : : 3. If the target machine doesn't handle -static-pie.
1160 : : */
1161 : : /* We want %{T*} after %{L*} and %D so that it can be used to specify linker
1162 : : scripts which exist in user specified directories, or in standard
1163 : : directories. */
1164 : : /* We pass any -flto flags on to the linker, which is expected
1165 : : to understand them. In practice, this means it had better be collect2. */
1166 : : /* %{e*} includes -export-dynamic; see comment in common.opt. */
1167 : : #ifndef LINK_COMMAND_SPEC
1168 : : #define LINK_COMMAND_SPEC "\
1169 : : %{!fsyntax-only:%{!c:%{!M:%{!MM:%{!E:%{!S:\
1170 : : %(linker) " \
1171 : : LINK_PLUGIN_SPEC \
1172 : : "%{flto|flto=*:%<fcompare-debug*} \
1173 : : %{flto} %{fno-lto} %{flto=*} %l " LINK_PIE_SPEC \
1174 : : "%{fuse-ld=*:-fuse-ld=%*} " LINK_COMPRESS_DEBUG_SPEC \
1175 : : "%X %{o*} %{e*} %{N} %{n} %{r}\
1176 : : %{s} %{t} %{u*} %{z} %{Z} %{!nostdlib:%{!r:%{!nostartfiles:%S}}} \
1177 : : %{static|no-pie|static-pie:} %@{L*} %(link_libgcc) " \
1178 : : VTABLE_VERIFICATION_SPEC " " SANITIZER_EARLY_SPEC " %o "" \
1179 : : %{fopenacc|fopenmp|%:gt(%{ftree-parallelize-loops=*:%*} 1):\
1180 : : %:include(libgomp.spec)%(link_gomp)}\
1181 : : %{fgnu-tm:%:include(libitm.spec)%(link_itm)}\
1182 : : " STACK_SPLIT_SPEC "\
1183 : : %{fprofile-arcs|fcondition-coverage|fpath-coverage|fprofile-generate*|coverage:-lgcov} " SANITIZER_SPEC " \
1184 : : %{!nostdlib:%{!r:%{!nodefaultlibs:%(link_ssp) %(link_gcc_c_sequence)}}}\
1185 : : %{!nostdlib:%{!r:%{!nostartfiles:%E}}} %{T*} \n%(post_link) }}}}}}"
1186 : : #endif
1187 : :
1188 : : #ifndef LINK_LIBGCC_SPEC
1189 : : /* Generate -L options for startfile prefix list. */
1190 : : # define LINK_LIBGCC_SPEC "%D"
1191 : : #endif
1192 : :
1193 : : #ifndef STARTFILE_PREFIX_SPEC
1194 : : # define STARTFILE_PREFIX_SPEC ""
1195 : : #endif
1196 : :
1197 : : #ifndef SYSROOT_SPEC
1198 : : # define SYSROOT_SPEC "--sysroot=%R"
1199 : : #endif
1200 : :
1201 : : #ifndef SYSROOT_SUFFIX_SPEC
1202 : : # define SYSROOT_SUFFIX_SPEC ""
1203 : : #endif
1204 : :
1205 : : #ifndef SYSROOT_HEADERS_SUFFIX_SPEC
1206 : : # define SYSROOT_HEADERS_SUFFIX_SPEC ""
1207 : : #endif
1208 : :
1209 : : #ifndef RUNPATH_OPTION
1210 : : # define RUNPATH_OPTION "-rpath"
1211 : : #endif
1212 : :
1213 : : static const char *asm_debug = ASM_DEBUG_SPEC;
1214 : : static const char *asm_debug_option = ASM_DEBUG_OPTION_SPEC;
1215 : : static const char *cpp_spec = CPP_SPEC;
1216 : : static const char *cc1_spec = CC1_SPEC OS_CC1_SPEC;
1217 : : static const char *cc1plus_spec = CC1PLUS_SPEC;
1218 : : static const char *link_gcc_c_sequence_spec = LINK_GCC_C_SEQUENCE_SPEC;
1219 : : static const char *link_ssp_spec = LINK_SSP_SPEC;
1220 : : static const char *asm_spec = ASM_SPEC;
1221 : : static const char *asm_final_spec = ASM_FINAL_SPEC;
1222 : : static const char *link_spec = LINK_SPEC;
1223 : : static const char *lib_spec = LIB_SPEC;
1224 : : static const char *link_gomp_spec = "";
1225 : : static const char *libgcc_spec = LIBGCC_SPEC;
1226 : : static const char *endfile_spec = ENDFILE_SPEC;
1227 : : static const char *startfile_spec = STARTFILE_SPEC;
1228 : : static const char *linker_name_spec = LINKER_NAME;
1229 : : static const char *linker_plugin_file_spec = "";
1230 : : static const char *lto_wrapper_spec = "";
1231 : : static const char *lto_gcc_spec = "";
1232 : : static const char *post_link_spec = POST_LINK_SPEC;
1233 : : static const char *link_command_spec = LINK_COMMAND_SPEC;
1234 : : static const char *link_libgcc_spec = LINK_LIBGCC_SPEC;
1235 : : static const char *startfile_prefix_spec = STARTFILE_PREFIX_SPEC;
1236 : : static const char *sysroot_spec = SYSROOT_SPEC;
1237 : : static const char *sysroot_suffix_spec = SYSROOT_SUFFIX_SPEC;
1238 : : static const char *sysroot_hdrs_suffix_spec = SYSROOT_HEADERS_SUFFIX_SPEC;
1239 : : static const char *self_spec = "";
1240 : :
1241 : : /* Standard options to cpp, cc1, and as, to reduce duplication in specs.
1242 : : There should be no need to override these in target dependent files,
1243 : : but we need to copy them to the specs file so that newer versions
1244 : : of the GCC driver can correctly drive older tool chains with the
1245 : : appropriate -B options. */
1246 : :
1247 : : /* When cpplib handles traditional preprocessing, get rid of this, and
1248 : : call cc1 (or cc1obj in objc/lang-specs.h) from the main specs so
1249 : : that we default the front end language better. */
1250 : : static const char *trad_capable_cpp =
1251 : : "cc1 -E %{traditional|traditional-cpp:-traditional-cpp}";
1252 : :
1253 : : /* We don't wrap .d files in %W{} since a missing .d file, and
1254 : : therefore no dependency entry, confuses make into thinking a .o
1255 : : file that happens to exist is up-to-date. */
1256 : : static const char *cpp_unique_options =
1257 : : "%{!Q:-quiet} %{nostdinc*} %{C} %{CC} %{v} %@{I*&F*} %{P} %I\
1258 : : %{MD:-MD %{!o:%b.d}%{o*:%.d%*}}\
1259 : : %{MMD:-MMD %{!o:%b.d}%{o*:%.d%*}}\
1260 : : %{M} %{MM} %{MF*} %{MG} %{MP} %{MQ*} %{MT*}\
1261 : : %{Mmodules} %{Mno-modules}\
1262 : : %{!E:%{!M:%{!MM:%{!MT:%{!MQ:%{MD|MMD:%{o*:-MQ %*}}}}}}}\
1263 : : %{remap} %{%:debug-level-gt(2):-dD}\
1264 : : %{!iplugindir*:%{fplugin*:%:find-plugindir()}}\
1265 : : %{H} %C %{D*&U*&A*} %{i*} %Z %i\
1266 : : %{E|M|MM:%W{o*}} %{-embed*}\
1267 : : %{fdeps-format=*:%{!fdeps-file=*:-fdeps-file=%:join(%{!o:%b.ddi}%{o*:%.ddi%*})}}\
1268 : : %{fdeps-format=*:%{!fdeps-target=*:-fdeps-target=%:join(%{!o:%b.o}%{o*:%.o%*})}}";
1269 : :
1270 : : /* This contains cpp options which are common with cc1_options and are passed
1271 : : only when preprocessing only to avoid duplication. We pass the cc1 spec
1272 : : options to the preprocessor so that it the cc1 spec may manipulate
1273 : : options used to set target flags. Those special target flags settings may
1274 : : in turn cause preprocessor symbols to be defined specially. */
1275 : : static const char *cpp_options =
1276 : : "%(cpp_unique_options) %1 %{m*} %{std*&ansi&trigraphs} %{W*&pedantic*} %{w}\
1277 : : %{f*} %{g*:%{%:debug-level-gt(0):%{g*}\
1278 : : %{!fno-working-directory:-fworking-directory}}} %{O*}\
1279 : : %{undef} %{save-temps*:-fpch-preprocess}";
1280 : :
1281 : : /* Pass -d* flags, possibly modifying -dumpdir, -dumpbase et al.
1282 : :
1283 : : Make it easy for a language to override the argument for the
1284 : : %:dumps specs function call. */
1285 : : #define DUMPS_OPTIONS(EXTS) \
1286 : : "%<dumpdir %<dumpbase %<dumpbase-ext %{d*} %:dumps(" EXTS ")"
1287 : :
1288 : : /* This contains cpp options which are not passed when the preprocessor
1289 : : output will be used by another program. */
1290 : : static const char *cpp_debug_options = DUMPS_OPTIONS ("");
1291 : :
1292 : : /* NB: This is shared amongst all front-ends, except for Ada. */
1293 : : static const char *cc1_options =
1294 : : "%{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
1295 : : %{!iplugindir*:%{fplugin*:%:find-plugindir()}}\
1296 : : %1 %{!Q:-quiet} %(cpp_debug_options) %{m*} %{aux-info*}\
1297 : : %{g*} %{O*} %{W*&pedantic*} %{w} %{std*&ansi&trigraphs}\
1298 : : %{v:-version} %{pg:-p} %{p} %{f*} %{undef}\
1299 : : %{Qn:-fno-ident} %{Qy:} %{-help:--help}\
1300 : : %{-target-help:--target-help}\
1301 : : %{-version:--version}\
1302 : : %{-help=*:--help=%*}\
1303 : : %{!fsyntax-only:%{S:%W{o*}%{!o*:-o %w%b.s}}}\
1304 : : %{fsyntax-only:-o %j} %{-param*}\
1305 : : %{coverage:-fprofile-arcs -ftest-coverage}\
1306 : : %{fprofile-arcs|fcondition-coverage|fpath-coverage|fprofile-generate*|coverage:\
1307 : : %{!fprofile-update=single:\
1308 : : %{pthread:-fprofile-update=prefer-atomic}}}";
1309 : :
1310 : : static const char *asm_options =
1311 : : "%{-target-help:%:print-asm-header()} "
1312 : : #if HAVE_GNU_AS
1313 : : /* If GNU AS is used, then convert -w (no warnings), -I, and -v
1314 : : to the assembler equivalents. */
1315 : : "%{v} %{w:-W} %{I*} "
1316 : : #endif
1317 : : "%(asm_debug_option)"
1318 : : ASM_COMPRESS_DEBUG_SPEC
1319 : : "%a %Y %{c:%W{o*}%{!o*:-o %w%b%O}}%{!c:-o %d%w%u%O}";
1320 : :
1321 : : static const char *invoke_as =
1322 : : #ifdef AS_NEEDS_DASH_FOR_PIPED_INPUT
1323 : : "%{!fwpa*:\
1324 : : %{fcompare-debug=*|fdump-final-insns=*:%:compare-debug-dump-opt()}\
1325 : : %{!S:-o %|.s |\n as %(asm_options) %|.s %A }\
1326 : : }";
1327 : : #else
1328 : : "%{!fwpa*:\
1329 : : %{fcompare-debug=*|fdump-final-insns=*:%:compare-debug-dump-opt()}\
1330 : : %{!S:-o %|.s |\n as %(asm_options) %m.s %A }\
1331 : : }";
1332 : : #endif
1333 : :
1334 : : /* Some compilers have limits on line lengths, and the multilib_select
1335 : : and/or multilib_matches strings can be very long, so we build them at
1336 : : run time. */
1337 : : static struct obstack multilib_obstack;
1338 : : static const char *multilib_select;
1339 : : static const char *multilib_matches;
1340 : : static const char *multilib_defaults;
1341 : : static const char *multilib_exclusions;
1342 : : static const char *multilib_reuse;
1343 : :
1344 : : /* Check whether a particular argument is a default argument. */
1345 : :
1346 : : #ifndef MULTILIB_DEFAULTS
1347 : : #define MULTILIB_DEFAULTS { "" }
1348 : : #endif
1349 : :
1350 : : static const char *const multilib_defaults_raw[] = MULTILIB_DEFAULTS;
1351 : :
1352 : : #ifndef DRIVER_SELF_SPECS
1353 : : #define DRIVER_SELF_SPECS ""
1354 : : #endif
1355 : :
1356 : : /* Linking to libgomp implies pthreads. This is particularly important
1357 : : for targets that use different start files and suchlike. */
1358 : : #ifndef GOMP_SELF_SPECS
1359 : : #define GOMP_SELF_SPECS \
1360 : : "%{fopenacc|fopenmp|%:gt(%{ftree-parallelize-loops=*:%*} 1): " \
1361 : : "-pthread}"
1362 : : #endif
1363 : :
1364 : : /* Likewise for -fgnu-tm. */
1365 : : #ifndef GTM_SELF_SPECS
1366 : : #define GTM_SELF_SPECS "%{fgnu-tm: -pthread}"
1367 : : #endif
1368 : :
1369 : : static const char *const driver_self_specs[] = {
1370 : : "%{fdump-final-insns:-fdump-final-insns=.} %<fdump-final-insns",
1371 : : DRIVER_SELF_SPECS, CONFIGURE_SPECS, GOMP_SELF_SPECS, GTM_SELF_SPECS,
1372 : : /* This discards -fmultiflags at the end of self specs processing in the
1373 : : driver, so that it is effectively Ignored, without actually marking it as
1374 : : Ignored, which would get it discarded before self specs could remap it. */
1375 : : "%<fmultiflags"
1376 : : };
1377 : :
1378 : : #ifndef OPTION_DEFAULT_SPECS
1379 : : #define OPTION_DEFAULT_SPECS { "", "" }
1380 : : #endif
1381 : :
1382 : : struct default_spec
1383 : : {
1384 : : const char *name;
1385 : : const char *spec;
1386 : : };
1387 : :
1388 : : static const struct default_spec
1389 : : option_default_specs[] = { OPTION_DEFAULT_SPECS };
1390 : :
1391 : : struct user_specs
1392 : : {
1393 : : struct user_specs *next;
1394 : : const char *filename;
1395 : : };
1396 : :
1397 : : static struct user_specs *user_specs_head, *user_specs_tail;
1398 : :
1399 : :
1400 : : /* Record the mapping from file suffixes for compilation specs. */
1401 : :
1402 : : struct compiler
1403 : : {
1404 : : const char *suffix; /* Use this compiler for input files
1405 : : whose names end in this suffix. */
1406 : :
1407 : : const char *spec; /* To use this compiler, run this spec. */
1408 : :
1409 : : const char *cpp_spec; /* If non-NULL, substitute this spec
1410 : : for `%C', rather than the usual
1411 : : cpp_spec. */
1412 : : int combinable; /* If nonzero, compiler can deal with
1413 : : multiple source files at once (IMA). */
1414 : : int needs_preprocessing; /* If nonzero, source files need to
1415 : : be run through a preprocessor. */
1416 : : };
1417 : :
1418 : : /* Pointer to a vector of `struct compiler' that gives the spec for
1419 : : compiling a file, based on its suffix.
1420 : : A file that does not end in any of these suffixes will be passed
1421 : : unchanged to the loader and nothing else will be done to it.
1422 : :
1423 : : An entry containing two 0s is used to terminate the vector.
1424 : :
1425 : : If multiple entries match a file, the last matching one is used. */
1426 : :
1427 : : static struct compiler *compilers;
1428 : :
1429 : : /* Number of entries in `compilers', not counting the null terminator. */
1430 : :
1431 : : static int n_compilers;
1432 : :
1433 : : /* The default list of file name suffixes and their compilation specs. */
1434 : :
1435 : : static const struct compiler default_compilers[] =
1436 : : {
1437 : : /* Add lists of suffixes of known languages here. If those languages
1438 : : were not present when we built the driver, we will hit these copies
1439 : : and be given a more meaningful error than "file not used since
1440 : : linking is not done". */
1441 : : {".m", "#Objective-C", 0, 0, 0}, {".mi", "#Objective-C", 0, 0, 0},
1442 : : {".mm", "#Objective-C++", 0, 0, 0}, {".M", "#Objective-C++", 0, 0, 0},
1443 : : {".mii", "#Objective-C++", 0, 0, 0},
1444 : : {".cc", "#C++", 0, 0, 0}, {".cxx", "#C++", 0, 0, 0},
1445 : : {".cpp", "#C++", 0, 0, 0}, {".cp", "#C++", 0, 0, 0},
1446 : : {".c++", "#C++", 0, 0, 0}, {".C", "#C++", 0, 0, 0},
1447 : : {".CPP", "#C++", 0, 0, 0}, {".ii", "#C++", 0, 0, 0},
1448 : : {".ads", "#Ada", 0, 0, 0}, {".adb", "#Ada", 0, 0, 0},
1449 : : {".f", "#Fortran", 0, 0, 0}, {".F", "#Fortran", 0, 0, 0},
1450 : : {".for", "#Fortran", 0, 0, 0}, {".FOR", "#Fortran", 0, 0, 0},
1451 : : {".ftn", "#Fortran", 0, 0, 0}, {".FTN", "#Fortran", 0, 0, 0},
1452 : : {".fpp", "#Fortran", 0, 0, 0}, {".FPP", "#Fortran", 0, 0, 0},
1453 : : {".f90", "#Fortran", 0, 0, 0}, {".F90", "#Fortran", 0, 0, 0},
1454 : : {".f95", "#Fortran", 0, 0, 0}, {".F95", "#Fortran", 0, 0, 0},
1455 : : {".f03", "#Fortran", 0, 0, 0}, {".F03", "#Fortran", 0, 0, 0},
1456 : : {".f08", "#Fortran", 0, 0, 0}, {".F08", "#Fortran", 0, 0, 0},
1457 : : {".r", "#Ratfor", 0, 0, 0},
1458 : : {".go", "#Go", 0, 1, 0},
1459 : : {".d", "#D", 0, 1, 0}, {".dd", "#D", 0, 1, 0}, {".di", "#D", 0, 1, 0},
1460 : : {".mod", "#Modula-2", 0, 0, 0}, {".m2i", "#Modula-2", 0, 0, 0},
1461 : : /* Next come the entries for C. */
1462 : : {".c", "@c", 0, 0, 1},
1463 : : {"@c",
1464 : : /* cc1 has an integrated ISO C preprocessor. We should invoke the
1465 : : external preprocessor if -save-temps is given. */
1466 : : "%{E|M|MM:%(trad_capable_cpp) %(cpp_options) %(cpp_debug_options)}\
1467 : : %{!E:%{!M:%{!MM:\
1468 : : %{traditional:\
1469 : : %eGNU C no longer supports -traditional without -E}\
1470 : : %{save-temps*|traditional-cpp|no-integrated-cpp:%(trad_capable_cpp) \
1471 : : %(cpp_options) -o %{save-temps*:%b.i} %{!save-temps*:%g.i} \n\
1472 : : cc1 -fpreprocessed %{save-temps*:%b.i} %{!save-temps*:%g.i} \
1473 : : %(cc1_options)}\
1474 : : %{!save-temps*:%{!traditional-cpp:%{!no-integrated-cpp:\
1475 : : cc1 %(cpp_unique_options) %(cc1_options)}}}\
1476 : : %{!fsyntax-only:%(invoke_as)}}}}", 0, 0, 1},
1477 : : {"-",
1478 : : "%{!E:%e-E or -x required when input is from standard input}\
1479 : : %(trad_capable_cpp) %(cpp_options) %(cpp_debug_options)", 0, 0, 0},
1480 : : {".h", "@c-header", 0, 0, 0},
1481 : : {"@c-header",
1482 : : /* cc1 has an integrated ISO C preprocessor. We should invoke the
1483 : : external preprocessor if -save-temps is given. */
1484 : : "%{E|M|MM:%(trad_capable_cpp) %(cpp_options) %(cpp_debug_options)}\
1485 : : %{!E:%{!M:%{!MM:\
1486 : : %{save-temps*|traditional-cpp|no-integrated-cpp:%(trad_capable_cpp) \
1487 : : %(cpp_options) -o %{save-temps*:%b.i} %{!save-temps*:%g.i} \n\
1488 : : cc1 -fpreprocessed %{save-temps*:%b.i} %{!save-temps*:%g.i} \
1489 : : %(cc1_options)\
1490 : : %{!fsyntax-only:%{!S:-o %g.s} \
1491 : : %{!fdump-ada-spec*:%{!o*:--output-pch %w%i.gch}\
1492 : : %W{o*:--output-pch %w%*}}%{!S:%V}}}\
1493 : : %{!save-temps*:%{!traditional-cpp:%{!no-integrated-cpp:\
1494 : : cc1 %(cpp_unique_options) %(cc1_options)\
1495 : : %{!fsyntax-only:%{!S:-o %g.s} \
1496 : : %{!fdump-ada-spec*:%{!o*:--output-pch %w%i.gch}\
1497 : : %W{o*:--output-pch %w%*}}%{!S:%V}}}}}}}}", 0, 0, 0},
1498 : : {".i", "@cpp-output", 0, 0, 0},
1499 : : {"@cpp-output",
1500 : : "%{!M:%{!MM:%{!E:cc1 -fpreprocessed %i %(cc1_options) %{!fsyntax-only:%(invoke_as)}}}}", 0, 0, 0},
1501 : : {".s", "@assembler", 0, 0, 0},
1502 : : {"@assembler",
1503 : : "%{!M:%{!MM:%{!E:%{!S:as %(asm_debug) %(asm_options) %i %A }}}}", 0, 0, 0},
1504 : : {".sx", "@assembler-with-cpp", 0, 0, 0},
1505 : : {".S", "@assembler-with-cpp", 0, 0, 0},
1506 : : {"@assembler-with-cpp",
1507 : : #ifdef AS_NEEDS_DASH_FOR_PIPED_INPUT
1508 : : "%(trad_capable_cpp) -lang-asm %(cpp_options) -fno-directives-only\
1509 : : %{E|M|MM:%(cpp_debug_options)}\
1510 : : %{!M:%{!MM:%{!E:%{!S:-o %|.s |\n\
1511 : : as %(asm_debug) %(asm_options) %|.s %A }}}}"
1512 : : #else
1513 : : "%(trad_capable_cpp) -lang-asm %(cpp_options) -fno-directives-only\
1514 : : %{E|M|MM:%(cpp_debug_options)}\
1515 : : %{!M:%{!MM:%{!E:%{!S:-o %|.s |\n\
1516 : : as %(asm_debug) %(asm_options) %m.s %A }}}}"
1517 : : #endif
1518 : : , 0, 0, 0},
1519 : :
1520 : : #include "specs.h"
1521 : : /* Mark end of table. */
1522 : : {0, 0, 0, 0, 0}
1523 : : };
1524 : :
1525 : : /* Number of elements in default_compilers, not counting the terminator. */
1526 : :
1527 : : static const int n_default_compilers = ARRAY_SIZE (default_compilers) - 1;
1528 : :
1529 : : typedef char *char_p; /* For DEF_VEC_P. */
1530 : :
1531 : : /* A vector of options to give to the linker.
1532 : : These options are accumulated by %x,
1533 : : and substituted into the linker command with %X. */
1534 : : static vec<char_p> linker_options;
1535 : :
1536 : : /* A vector of options to give to the assembler.
1537 : : These options are accumulated by -Wa,
1538 : : and substituted into the assembler command with %Y. */
1539 : : static vec<char_p> assembler_options;
1540 : :
1541 : : /* A vector of options to give to the preprocessor.
1542 : : These options are accumulated by -Wp,
1543 : : and substituted into the preprocessor command with %Z. */
1544 : : static vec<char_p> preprocessor_options;
1545 : :
1546 : : static char *
1547 : 28507899 : skip_whitespace (char *p)
1548 : : {
1549 : 66071836 : while (1)
1550 : : {
1551 : : /* A fully-blank line is a delimiter in the SPEC file and shouldn't
1552 : : be considered whitespace. */
1553 : 66071836 : if (p[0] == '\n' && p[1] == '\n' && p[2] == '\n')
1554 : 4783488 : return p + 1;
1555 : 61288348 : else if (*p == '\n' || *p == ' ' || *p == '\t')
1556 : 37446415 : p++;
1557 : 23841933 : else if (*p == '#')
1558 : : {
1559 : 3637508 : while (*p != '\n')
1560 : 3519986 : p++;
1561 : 117522 : p++;
1562 : : }
1563 : : else
1564 : : break;
1565 : : }
1566 : :
1567 : : return p;
1568 : : }
1569 : : /* Structures to keep track of prefixes to try when looking for files. */
1570 : :
1571 : : struct prefix_list
1572 : : {
1573 : : const char *prefix; /* String to prepend to the path. */
1574 : : struct prefix_list *next; /* Next in linked list. */
1575 : : int require_machine_suffix; /* Don't use without machine_suffix. */
1576 : : /* 2 means try both machine_suffix and just_machine_suffix. */
1577 : : int priority; /* Sort key - priority within list. */
1578 : : int os_multilib; /* 1 if OS multilib scheme should be used,
1579 : : 0 for GCC multilib scheme. */
1580 : : };
1581 : :
1582 : : struct path_prefix
1583 : : {
1584 : : struct prefix_list *plist; /* List of prefixes to try */
1585 : : int max_len; /* Max length of a prefix in PLIST */
1586 : : const char *name; /* Name of this list (used in config stuff) */
1587 : : };
1588 : :
1589 : : /* List of prefixes to try when looking for executables. */
1590 : :
1591 : : static struct path_prefix exec_prefixes = { 0, 0, "exec" };
1592 : :
1593 : : /* List of prefixes to try when looking for startup (crt0) files. */
1594 : :
1595 : : static struct path_prefix startfile_prefixes = { 0, 0, "startfile" };
1596 : :
1597 : : /* List of prefixes to try when looking for include files. */
1598 : :
1599 : : static struct path_prefix include_prefixes = { 0, 0, "include" };
1600 : :
1601 : : /* Suffix to attach to directories searched for commands.
1602 : : This looks like `MACHINE/VERSION/'. */
1603 : :
1604 : : static const char *machine_suffix = 0;
1605 : :
1606 : : /* Suffix to attach to directories searched for commands.
1607 : : This is just `MACHINE/'. */
1608 : :
1609 : : static const char *just_machine_suffix = 0;
1610 : :
1611 : : /* Adjusted value of GCC_EXEC_PREFIX envvar. */
1612 : :
1613 : : static const char *gcc_exec_prefix;
1614 : :
1615 : : /* Adjusted value of standard_libexec_prefix. */
1616 : :
1617 : : static const char *gcc_libexec_prefix;
1618 : :
1619 : : /* Default prefixes to attach to command names. */
1620 : :
1621 : : #ifndef STANDARD_STARTFILE_PREFIX_1
1622 : : #define STANDARD_STARTFILE_PREFIX_1 "/lib/"
1623 : : #endif
1624 : : #ifndef STANDARD_STARTFILE_PREFIX_2
1625 : : #define STANDARD_STARTFILE_PREFIX_2 "/usr/lib/"
1626 : : #endif
1627 : :
1628 : : #ifdef CROSS_DIRECTORY_STRUCTURE /* Don't use these prefixes for a cross compiler. */
1629 : : #undef MD_EXEC_PREFIX
1630 : : #undef MD_STARTFILE_PREFIX
1631 : : #undef MD_STARTFILE_PREFIX_1
1632 : : #endif
1633 : :
1634 : : /* If no prefixes defined, use the null string, which will disable them. */
1635 : : #ifndef MD_EXEC_PREFIX
1636 : : #define MD_EXEC_PREFIX ""
1637 : : #endif
1638 : : #ifndef MD_STARTFILE_PREFIX
1639 : : #define MD_STARTFILE_PREFIX ""
1640 : : #endif
1641 : : #ifndef MD_STARTFILE_PREFIX_1
1642 : : #define MD_STARTFILE_PREFIX_1 ""
1643 : : #endif
1644 : :
1645 : : /* These directories are locations set at configure-time based on the
1646 : : --prefix option provided to configure. Their initializers are
1647 : : defined in Makefile.in. These paths are not *directly* used when
1648 : : gcc_exec_prefix is set because, in that case, we know where the
1649 : : compiler has been installed, and use paths relative to that
1650 : : location instead. */
1651 : : static const char *const standard_exec_prefix = STANDARD_EXEC_PREFIX;
1652 : : static const char *const standard_libexec_prefix = STANDARD_LIBEXEC_PREFIX;
1653 : : static const char *const standard_bindir_prefix = STANDARD_BINDIR_PREFIX;
1654 : : static const char *const standard_startfile_prefix = STANDARD_STARTFILE_PREFIX;
1655 : :
1656 : : /* For native compilers, these are well-known paths containing
1657 : : components that may be provided by the system. For cross
1658 : : compilers, these paths are not used. */
1659 : : static const char *md_exec_prefix = MD_EXEC_PREFIX;
1660 : : static const char *md_startfile_prefix = MD_STARTFILE_PREFIX;
1661 : : static const char *md_startfile_prefix_1 = MD_STARTFILE_PREFIX_1;
1662 : : static const char *const standard_startfile_prefix_1
1663 : : = STANDARD_STARTFILE_PREFIX_1;
1664 : : static const char *const standard_startfile_prefix_2
1665 : : = STANDARD_STARTFILE_PREFIX_2;
1666 : :
1667 : : /* A relative path to be used in finding the location of tools
1668 : : relative to the driver. */
1669 : : static const char *const tooldir_base_prefix = TOOLDIR_BASE_PREFIX;
1670 : :
1671 : : /* A prefix to be used when this is an accelerator compiler. */
1672 : : static const char *const accel_dir_suffix = ACCEL_DIR_SUFFIX;
1673 : :
1674 : : /* Subdirectory to use for locating libraries. Set by
1675 : : set_multilib_dir based on the compilation options. */
1676 : :
1677 : : static const char *multilib_dir;
1678 : :
1679 : : /* Subdirectory to use for locating libraries in OS conventions. Set by
1680 : : set_multilib_dir based on the compilation options. */
1681 : :
1682 : : static const char *multilib_os_dir;
1683 : :
1684 : : /* Subdirectory to use for locating libraries in multiarch conventions. Set by
1685 : : set_multilib_dir based on the compilation options. */
1686 : :
1687 : : static const char *multiarch_dir;
1688 : :
1689 : : /* Structure to keep track of the specs that have been defined so far.
1690 : : These are accessed using %(specname) in a compiler or link
1691 : : spec. */
1692 : :
1693 : : struct spec_list
1694 : : {
1695 : : /* The following 2 fields must be first */
1696 : : /* to allow EXTRA_SPECS to be initialized */
1697 : : const char *name; /* name of the spec. */
1698 : : const char *ptr; /* available ptr if no static pointer */
1699 : :
1700 : : /* The following fields are not initialized */
1701 : : /* by EXTRA_SPECS */
1702 : : const char **ptr_spec; /* pointer to the spec itself. */
1703 : : struct spec_list *next; /* Next spec in linked list. */
1704 : : int name_len; /* length of the name */
1705 : : bool user_p; /* whether string come from file spec. */
1706 : : bool alloc_p; /* whether string was allocated */
1707 : : const char *default_ptr; /* The default value of *ptr_spec. */
1708 : : };
1709 : :
1710 : : #define INIT_STATIC_SPEC(NAME,PTR) \
1711 : : { NAME, NULL, PTR, (struct spec_list *) 0, sizeof (NAME) - 1, false, false, \
1712 : : *PTR }
1713 : :
1714 : : /* List of statically defined specs. */
1715 : : static struct spec_list static_specs[] =
1716 : : {
1717 : : INIT_STATIC_SPEC ("asm", &asm_spec),
1718 : : INIT_STATIC_SPEC ("asm_debug", &asm_debug),
1719 : : INIT_STATIC_SPEC ("asm_debug_option", &asm_debug_option),
1720 : : INIT_STATIC_SPEC ("asm_final", &asm_final_spec),
1721 : : INIT_STATIC_SPEC ("asm_options", &asm_options),
1722 : : INIT_STATIC_SPEC ("invoke_as", &invoke_as),
1723 : : INIT_STATIC_SPEC ("cpp", &cpp_spec),
1724 : : INIT_STATIC_SPEC ("cpp_options", &cpp_options),
1725 : : INIT_STATIC_SPEC ("cpp_debug_options", &cpp_debug_options),
1726 : : INIT_STATIC_SPEC ("cpp_unique_options", &cpp_unique_options),
1727 : : INIT_STATIC_SPEC ("trad_capable_cpp", &trad_capable_cpp),
1728 : : INIT_STATIC_SPEC ("cc1", &cc1_spec),
1729 : : INIT_STATIC_SPEC ("cc1_options", &cc1_options),
1730 : : INIT_STATIC_SPEC ("cc1plus", &cc1plus_spec),
1731 : : INIT_STATIC_SPEC ("link_gcc_c_sequence", &link_gcc_c_sequence_spec),
1732 : : INIT_STATIC_SPEC ("link_ssp", &link_ssp_spec),
1733 : : INIT_STATIC_SPEC ("endfile", &endfile_spec),
1734 : : INIT_STATIC_SPEC ("link", &link_spec),
1735 : : INIT_STATIC_SPEC ("lib", &lib_spec),
1736 : : INIT_STATIC_SPEC ("link_gomp", &link_gomp_spec),
1737 : : INIT_STATIC_SPEC ("libgcc", &libgcc_spec),
1738 : : INIT_STATIC_SPEC ("startfile", &startfile_spec),
1739 : : INIT_STATIC_SPEC ("cross_compile", &cross_compile),
1740 : : INIT_STATIC_SPEC ("version", &compiler_version),
1741 : : INIT_STATIC_SPEC ("multilib", &multilib_select),
1742 : : INIT_STATIC_SPEC ("multilib_defaults", &multilib_defaults),
1743 : : INIT_STATIC_SPEC ("multilib_extra", &multilib_extra),
1744 : : INIT_STATIC_SPEC ("multilib_matches", &multilib_matches),
1745 : : INIT_STATIC_SPEC ("multilib_exclusions", &multilib_exclusions),
1746 : : INIT_STATIC_SPEC ("multilib_options", &multilib_options),
1747 : : INIT_STATIC_SPEC ("multilib_reuse", &multilib_reuse),
1748 : : INIT_STATIC_SPEC ("linker", &linker_name_spec),
1749 : : INIT_STATIC_SPEC ("linker_plugin_file", &linker_plugin_file_spec),
1750 : : INIT_STATIC_SPEC ("lto_wrapper", <o_wrapper_spec),
1751 : : INIT_STATIC_SPEC ("lto_gcc", <o_gcc_spec),
1752 : : INIT_STATIC_SPEC ("post_link", &post_link_spec),
1753 : : INIT_STATIC_SPEC ("link_libgcc", &link_libgcc_spec),
1754 : : INIT_STATIC_SPEC ("md_exec_prefix", &md_exec_prefix),
1755 : : INIT_STATIC_SPEC ("md_startfile_prefix", &md_startfile_prefix),
1756 : : INIT_STATIC_SPEC ("md_startfile_prefix_1", &md_startfile_prefix_1),
1757 : : INIT_STATIC_SPEC ("startfile_prefix_spec", &startfile_prefix_spec),
1758 : : INIT_STATIC_SPEC ("sysroot_spec", &sysroot_spec),
1759 : : INIT_STATIC_SPEC ("sysroot_suffix_spec", &sysroot_suffix_spec),
1760 : : INIT_STATIC_SPEC ("sysroot_hdrs_suffix_spec", &sysroot_hdrs_suffix_spec),
1761 : : INIT_STATIC_SPEC ("self_spec", &self_spec),
1762 : : };
1763 : :
1764 : : #ifdef EXTRA_SPECS /* additional specs needed */
1765 : : /* Structure to keep track of just the first two args of a spec_list.
1766 : : That is all that the EXTRA_SPECS macro gives us. */
1767 : : struct spec_list_1
1768 : : {
1769 : : const char *const name;
1770 : : const char *const ptr;
1771 : : };
1772 : :
1773 : : static const struct spec_list_1 extra_specs_1[] = { EXTRA_SPECS };
1774 : : static struct spec_list *extra_specs = (struct spec_list *) 0;
1775 : : #endif
1776 : :
1777 : : /* List of dynamically allocates specs that have been defined so far. */
1778 : :
1779 : : static struct spec_list *specs = (struct spec_list *) 0;
1780 : :
1781 : : /* List of static spec functions. */
1782 : :
1783 : : static const struct spec_function static_spec_functions[] =
1784 : : {
1785 : : { "getenv", getenv_spec_function },
1786 : : { "if-exists", if_exists_spec_function },
1787 : : { "if-exists-else", if_exists_else_spec_function },
1788 : : { "if-exists-then-else", if_exists_then_else_spec_function },
1789 : : { "sanitize", sanitize_spec_function },
1790 : : { "replace-outfile", replace_outfile_spec_function },
1791 : : { "remove-outfile", remove_outfile_spec_function },
1792 : : { "version-compare", version_compare_spec_function },
1793 : : { "include", include_spec_function },
1794 : : { "find-file", find_file_spec_function },
1795 : : { "find-plugindir", find_plugindir_spec_function },
1796 : : { "print-asm-header", print_asm_header_spec_function },
1797 : : { "compare-debug-dump-opt", compare_debug_dump_opt_spec_function },
1798 : : { "compare-debug-self-opt", compare_debug_self_opt_spec_function },
1799 : : { "pass-through-libs", pass_through_libs_spec_func },
1800 : : { "dumps", dumps_spec_func },
1801 : : { "gt", greater_than_spec_func },
1802 : : { "debug-level-gt", debug_level_greater_than_spec_func },
1803 : : { "dwarf-version-gt", dwarf_version_greater_than_spec_func },
1804 : : { "fortran-preinclude-file", find_fortran_preinclude_file},
1805 : : { "join", join_spec_func},
1806 : : #ifdef EXTRA_SPEC_FUNCTIONS
1807 : : EXTRA_SPEC_FUNCTIONS
1808 : : #endif
1809 : : { 0, 0 }
1810 : : };
1811 : :
1812 : : static int processing_spec_function;
1813 : :
1814 : : /* Add appropriate libgcc specs to OBSTACK, taking into account
1815 : : various permutations of -shared-libgcc, -shared, and such. */
1816 : :
1817 : : #if defined(ENABLE_SHARED_LIBGCC) && !defined(REAL_LIBGCC_SPEC)
1818 : :
1819 : : #ifndef USE_LD_AS_NEEDED
1820 : : #define USE_LD_AS_NEEDED 0
1821 : : #endif
1822 : :
1823 : : static void
1824 : 1146 : init_gcc_specs (struct obstack *obstack, const char *shared_name,
1825 : : const char *static_name, const char *eh_name)
1826 : : {
1827 : 1146 : char *buf;
1828 : :
1829 : : #if USE_LD_AS_NEEDED
1830 : 1146 : buf = concat ("%{static|static-libgcc|static-pie:", static_name, " ", eh_name, "}"
1831 : : "%{!static:%{!static-libgcc:%{!static-pie:"
1832 : : "%{!shared-libgcc:",
1833 : : static_name, " " LD_AS_NEEDED_OPTION " ",
1834 : : shared_name, " " LD_NO_AS_NEEDED_OPTION
1835 : : "}"
1836 : : "%{shared-libgcc:",
1837 : : shared_name, "%{!shared: ", static_name, "}"
1838 : : "}}"
1839 : : #else
1840 : : buf = concat ("%{static|static-libgcc:", static_name, " ", eh_name, "}"
1841 : : "%{!static:%{!static-libgcc:"
1842 : : "%{!shared:"
1843 : : "%{!shared-libgcc:", static_name, " ", eh_name, "}"
1844 : : "%{shared-libgcc:", shared_name, " ", static_name, "}"
1845 : : "}"
1846 : : #ifdef LINK_EH_SPEC
1847 : : "%{shared:"
1848 : : "%{shared-libgcc:", shared_name, "}"
1849 : : "%{!shared-libgcc:", static_name, "}"
1850 : : "}"
1851 : : #else
1852 : : "%{shared:", shared_name, "}"
1853 : : #endif
1854 : : #endif
1855 : : "}}", NULL);
1856 : :
1857 : 1146 : obstack_grow (obstack, buf, strlen (buf));
1858 : 1146 : free (buf);
1859 : 1146 : }
1860 : : #endif /* ENABLE_SHARED_LIBGCC */
1861 : :
1862 : : /* Initialize the specs lookup routines. */
1863 : :
1864 : : static void
1865 : 1146 : init_spec (void)
1866 : : {
1867 : 1146 : struct spec_list *next = (struct spec_list *) 0;
1868 : 1146 : struct spec_list *sl = (struct spec_list *) 0;
1869 : 1146 : int i;
1870 : :
1871 : 1146 : if (specs)
1872 : : return; /* Already initialized. */
1873 : :
1874 : 1146 : if (verbose_flag)
1875 : 106 : fnotice (stderr, "Using built-in specs.\n");
1876 : :
1877 : : #ifdef EXTRA_SPECS
1878 : 1146 : extra_specs = XCNEWVEC (struct spec_list, ARRAY_SIZE (extra_specs_1));
1879 : :
1880 : 2292 : for (i = ARRAY_SIZE (extra_specs_1) - 1; i >= 0; i--)
1881 : : {
1882 : 1146 : sl = &extra_specs[i];
1883 : 1146 : sl->name = extra_specs_1[i].name;
1884 : 1146 : sl->ptr = extra_specs_1[i].ptr;
1885 : 1146 : sl->next = next;
1886 : 1146 : sl->name_len = strlen (sl->name);
1887 : 1146 : sl->ptr_spec = &sl->ptr;
1888 : 1146 : gcc_assert (sl->ptr_spec != NULL);
1889 : 1146 : sl->default_ptr = sl->ptr;
1890 : 1146 : next = sl;
1891 : : }
1892 : : #endif
1893 : :
1894 : 52716 : for (i = ARRAY_SIZE (static_specs) - 1; i >= 0; i--)
1895 : : {
1896 : 51570 : sl = &static_specs[i];
1897 : 51570 : sl->next = next;
1898 : 51570 : next = sl;
1899 : : }
1900 : :
1901 : : #if defined(ENABLE_SHARED_LIBGCC) && !defined(REAL_LIBGCC_SPEC)
1902 : : /* ??? If neither -shared-libgcc nor --static-libgcc was
1903 : : seen, then we should be making an educated guess. Some proposed
1904 : : heuristics for ELF include:
1905 : :
1906 : : (1) If "-Wl,--export-dynamic", then it's a fair bet that the
1907 : : program will be doing dynamic loading, which will likely
1908 : : need the shared libgcc.
1909 : :
1910 : : (2) If "-ldl", then it's also a fair bet that we're doing
1911 : : dynamic loading.
1912 : :
1913 : : (3) For each ET_DYN we're linking against (either through -lfoo
1914 : : or /some/path/foo.so), check to see whether it or one of
1915 : : its dependencies depends on a shared libgcc.
1916 : :
1917 : : (4) If "-shared"
1918 : :
1919 : : If the runtime is fixed to look for program headers instead
1920 : : of calling __register_frame_info at all, for each object,
1921 : : use the shared libgcc if any EH symbol referenced.
1922 : :
1923 : : If crtstuff is fixed to not invoke __register_frame_info
1924 : : automatically, for each object, use the shared libgcc if
1925 : : any non-empty unwind section found.
1926 : :
1927 : : Doing any of this probably requires invoking an external program to
1928 : : do the actual object file scanning. */
1929 : 1146 : {
1930 : 1146 : const char *p = libgcc_spec;
1931 : 1146 : int in_sep = 1;
1932 : :
1933 : : /* Transform the extant libgcc_spec into one that uses the shared libgcc
1934 : : when given the proper command line arguments. */
1935 : 2292 : while (*p)
1936 : : {
1937 : 1146 : if (in_sep && *p == '-' && startswith (p, "-lgcc"))
1938 : : {
1939 : 1146 : init_gcc_specs (&obstack,
1940 : : "-lgcc_s"
1941 : : #ifdef USE_LIBUNWIND_EXCEPTIONS
1942 : : " -lunwind"
1943 : : #endif
1944 : : ,
1945 : : "-lgcc",
1946 : : "-lgcc_eh"
1947 : : #ifdef USE_LIBUNWIND_EXCEPTIONS
1948 : : # ifdef HAVE_LD_STATIC_DYNAMIC
1949 : : " %{!static:%{!static-pie:" LD_STATIC_OPTION "}} -lunwind"
1950 : : " %{!static:%{!static-pie:" LD_DYNAMIC_OPTION "}}"
1951 : : # else
1952 : : " -lunwind"
1953 : : # endif
1954 : : #endif
1955 : : );
1956 : :
1957 : 1146 : p += 5;
1958 : 1146 : in_sep = 0;
1959 : : }
1960 : 0 : else if (in_sep && *p == 'l' && startswith (p, "libgcc.a%s"))
1961 : : {
1962 : : /* Ug. We don't know shared library extensions. Hope that
1963 : : systems that use this form don't do shared libraries. */
1964 : 0 : init_gcc_specs (&obstack,
1965 : : "-lgcc_s",
1966 : : "libgcc.a%s",
1967 : : "libgcc_eh.a%s"
1968 : : #ifdef USE_LIBUNWIND_EXCEPTIONS
1969 : : " -lunwind"
1970 : : #endif
1971 : : );
1972 : 0 : p += 10;
1973 : 0 : in_sep = 0;
1974 : : }
1975 : : else
1976 : : {
1977 : 0 : obstack_1grow (&obstack, *p);
1978 : 0 : in_sep = (*p == ' ');
1979 : 0 : p += 1;
1980 : : }
1981 : : }
1982 : :
1983 : 1146 : obstack_1grow (&obstack, '\0');
1984 : 1146 : libgcc_spec = XOBFINISH (&obstack, const char *);
1985 : : }
1986 : : #endif
1987 : : #ifdef USE_AS_TRADITIONAL_FORMAT
1988 : : /* Prepend "--traditional-format" to whatever asm_spec we had before. */
1989 : : {
1990 : : static const char tf[] = "--traditional-format ";
1991 : : obstack_grow (&obstack, tf, sizeof (tf) - 1);
1992 : : obstack_grow0 (&obstack, asm_spec, strlen (asm_spec));
1993 : : asm_spec = XOBFINISH (&obstack, const char *);
1994 : : }
1995 : : #endif
1996 : :
1997 : : #if defined LINK_EH_SPEC || defined LINK_BUILDID_SPEC || \
1998 : : defined LINKER_HASH_STYLE
1999 : : # ifdef LINK_BUILDID_SPEC
2000 : : /* Prepend LINK_BUILDID_SPEC to whatever link_spec we had before. */
2001 : : obstack_grow (&obstack, LINK_BUILDID_SPEC, sizeof (LINK_BUILDID_SPEC) - 1);
2002 : : # endif
2003 : : # ifdef LINK_EH_SPEC
2004 : : /* Prepend LINK_EH_SPEC to whatever link_spec we had before. */
2005 : 1146 : obstack_grow (&obstack, LINK_EH_SPEC, sizeof (LINK_EH_SPEC) - 1);
2006 : : # endif
2007 : : # ifdef LINKER_HASH_STYLE
2008 : : /* Prepend --hash-style=LINKER_HASH_STYLE to whatever link_spec we had
2009 : : before. */
2010 : : {
2011 : : static const char hash_style[] = "--hash-style=";
2012 : : obstack_grow (&obstack, hash_style, sizeof (hash_style) - 1);
2013 : : obstack_grow (&obstack, LINKER_HASH_STYLE, sizeof (LINKER_HASH_STYLE) - 1);
2014 : : obstack_1grow (&obstack, ' ');
2015 : : }
2016 : : # endif
2017 : 1146 : obstack_grow0 (&obstack, link_spec, strlen (link_spec));
2018 : 1146 : link_spec = XOBFINISH (&obstack, const char *);
2019 : : #endif
2020 : :
2021 : 1146 : specs = sl;
2022 : : }
2023 : :
2024 : : /* Update the entry for SPEC in the static_specs table to point to VALUE,
2025 : : ensuring that we free the previous value if necessary. Set alloc_p for the
2026 : : entry to ALLOC_P: this determines whether we take ownership of VALUE (i.e.
2027 : : whether we need to free it later on). */
2028 : : static void
2029 : 209380 : set_static_spec (const char **spec, const char *value, bool alloc_p)
2030 : : {
2031 : 209380 : struct spec_list *sl = NULL;
2032 : :
2033 : 7229254 : for (unsigned i = 0; i < ARRAY_SIZE (static_specs); i++)
2034 : : {
2035 : 7229254 : if (static_specs[i].ptr_spec == spec)
2036 : : {
2037 : 209380 : sl = static_specs + i;
2038 : 209380 : break;
2039 : : }
2040 : : }
2041 : :
2042 : 0 : gcc_assert (sl);
2043 : :
2044 : 209380 : if (sl->alloc_p)
2045 : : {
2046 : 209380 : const char *old = *spec;
2047 : 209380 : free (const_cast <char *> (old));
2048 : : }
2049 : :
2050 : 209380 : *spec = value;
2051 : 209380 : sl->alloc_p = alloc_p;
2052 : 209380 : }
2053 : :
2054 : : /* Update a static spec to a new string, taking ownership of that
2055 : : string's memory. */
2056 : 109042 : static void set_static_spec_owned (const char **spec, const char *val)
2057 : : {
2058 : 0 : return set_static_spec (spec, val, true);
2059 : : }
2060 : :
2061 : : /* Update a static spec to point to a new value, but don't take
2062 : : ownership of (i.e. don't free) that string. */
2063 : 100338 : static void set_static_spec_shared (const char **spec, const char *val)
2064 : : {
2065 : 0 : return set_static_spec (spec, val, false);
2066 : : }
2067 : :
2068 : :
2069 : : /* Change the value of spec NAME to SPEC. If SPEC is empty, then the spec is
2070 : : removed; If the spec starts with a + then SPEC is added to the end of the
2071 : : current spec. */
2072 : :
2073 : : static void
2074 : 13800770 : set_spec (const char *name, const char *spec, bool user_p)
2075 : : {
2076 : 13800770 : struct spec_list *sl;
2077 : 13800770 : const char *old_spec;
2078 : 13800770 : int name_len = strlen (name);
2079 : 13800770 : int i;
2080 : :
2081 : : /* If this is the first call, initialize the statically allocated specs. */
2082 : 13800770 : if (!specs)
2083 : : {
2084 : : struct spec_list *next = (struct spec_list *) 0;
2085 : 13752528 : for (i = ARRAY_SIZE (static_specs) - 1; i >= 0; i--)
2086 : : {
2087 : 13453560 : sl = &static_specs[i];
2088 : 13453560 : sl->next = next;
2089 : 13453560 : next = sl;
2090 : : }
2091 : 298968 : specs = sl;
2092 : : }
2093 : :
2094 : : /* See if the spec already exists. */
2095 : 324750030 : for (sl = specs; sl; sl = sl->next)
2096 : 324429815 : if (name_len == sl->name_len && !strcmp (sl->name, name))
2097 : : break;
2098 : :
2099 : 13800770 : if (!sl)
2100 : : {
2101 : : /* Not found - make it. */
2102 : 320215 : sl = XNEW (struct spec_list);
2103 : 320215 : sl->name = xstrdup (name);
2104 : 320215 : sl->name_len = name_len;
2105 : 320215 : sl->ptr_spec = &sl->ptr;
2106 : 320215 : sl->alloc_p = 0;
2107 : 320215 : *(sl->ptr_spec) = "";
2108 : 320215 : sl->next = specs;
2109 : 320215 : sl->default_ptr = NULL;
2110 : 320215 : specs = sl;
2111 : : }
2112 : :
2113 : 13800770 : old_spec = *(sl->ptr_spec);
2114 : 13800770 : *(sl->ptr_spec) = ((spec[0] == '+' && ISSPACE ((unsigned char)spec[1]))
2115 : 1 : ? concat (old_spec, spec + 1, NULL)
2116 : 13800769 : : xstrdup (spec));
2117 : :
2118 : : #ifdef DEBUG_SPECS
2119 : : if (verbose_flag)
2120 : : fnotice (stderr, "Setting spec %s to '%s'\n\n", name, *(sl->ptr_spec));
2121 : : #endif
2122 : :
2123 : : /* Free the old spec. */
2124 : 13800770 : if (old_spec && sl->alloc_p)
2125 : 5841 : free (CONST_CAST (char *, old_spec));
2126 : :
2127 : 13800770 : sl->user_p = user_p;
2128 : 13800770 : sl->alloc_p = true;
2129 : 13800770 : }
2130 : :
2131 : : /* Accumulate a command (program name and args), and run it. */
2132 : :
2133 : : typedef const char *const_char_p; /* For DEF_VEC_P. */
2134 : :
2135 : : /* Vector of pointers to arguments in the current line of specifications. */
2136 : : static vec<const_char_p> argbuf;
2137 : :
2138 : : /* Likewise, but for the current @file. */
2139 : : static vec<const_char_p> at_file_argbuf;
2140 : :
2141 : : /* Whether an @file is currently open. */
2142 : : static bool in_at_file = false;
2143 : :
2144 : : /* Were the options -c, -S or -E passed. */
2145 : : static int have_c = 0;
2146 : :
2147 : : /* Was the option -o passed. */
2148 : : static int have_o = 0;
2149 : :
2150 : : /* Was the option -E passed. */
2151 : : static int have_E = 0;
2152 : :
2153 : : /* Pointer to output file name passed in with -o. */
2154 : : static const char *output_file = 0;
2155 : :
2156 : : /* Pointer to input file name passed in with -truncate.
2157 : : This file should be truncated after linking. */
2158 : : static const char *totruncate_file = 0;
2159 : :
2160 : : /* This is the list of suffixes and codes (%g/%u/%U/%j) and the associated
2161 : : temp file. If the HOST_BIT_BUCKET is used for %j, no entry is made for
2162 : : it here. */
2163 : :
2164 : : static struct temp_name {
2165 : : const char *suffix; /* suffix associated with the code. */
2166 : : int length; /* strlen (suffix). */
2167 : : int unique; /* Indicates whether %g or %u/%U was used. */
2168 : : const char *filename; /* associated filename. */
2169 : : int filename_length; /* strlen (filename). */
2170 : : struct temp_name *next;
2171 : : } *temp_names;
2172 : :
2173 : : /* Number of commands executed so far. */
2174 : :
2175 : : static int execution_count;
2176 : :
2177 : : /* Number of commands that exited with a signal. */
2178 : :
2179 : : static int signal_count;
2180 : :
2181 : : /* Allocate the argument vector. */
2182 : :
2183 : : static void
2184 : 2377784 : alloc_args (void)
2185 : : {
2186 : 2377784 : argbuf.create (10);
2187 : 2377784 : at_file_argbuf.create (10);
2188 : 2377784 : }
2189 : :
2190 : : /* Clear out the vector of arguments (after a command is executed). */
2191 : :
2192 : : static void
2193 : 5609518 : clear_args (void)
2194 : : {
2195 : 5609518 : argbuf.truncate (0);
2196 : 5609518 : at_file_argbuf.truncate (0);
2197 : 5609518 : }
2198 : :
2199 : : /* Add one argument to the vector at the end.
2200 : : This is done when a space is seen or at the end of the line.
2201 : : If DELETE_ALWAYS is nonzero, the arg is a filename
2202 : : and the file should be deleted eventually.
2203 : : If DELETE_FAILURE is nonzero, the arg is a filename
2204 : : and the file should be deleted if this compilation fails. */
2205 : :
2206 : : static void
2207 : 20405340 : store_arg (const char *arg, int delete_always, int delete_failure)
2208 : : {
2209 : 20405340 : if (in_at_file)
2210 : 14393 : at_file_argbuf.safe_push (arg);
2211 : : else
2212 : 20390947 : argbuf.safe_push (arg);
2213 : :
2214 : 20405340 : if (delete_always || delete_failure)
2215 : : {
2216 : 524720 : const char *p;
2217 : : /* If the temporary file we should delete is specified as
2218 : : part of a joined argument extract the filename. */
2219 : 524720 : if (arg[0] == '-'
2220 : 524720 : && (p = strrchr (arg, '=')))
2221 : 91376 : arg = p + 1;
2222 : 524720 : record_temp_file (arg, delete_always, delete_failure);
2223 : : }
2224 : 20405340 : }
2225 : :
2226 : : /* Open a temporary @file into which subsequent arguments will be stored. */
2227 : :
2228 : : static void
2229 : 13358 : open_at_file (void)
2230 : : {
2231 : 13358 : if (in_at_file)
2232 : 0 : fatal_error (input_location, "cannot open nested response file");
2233 : : else
2234 : 13358 : in_at_file = true;
2235 : 13358 : }
2236 : :
2237 : : /* Create a temporary @file name. */
2238 : :
2239 : 13348 : static char *make_at_file (void)
2240 : : {
2241 : 13348 : static int fileno = 0;
2242 : 13348 : char filename[20];
2243 : 13348 : const char *base, *ext;
2244 : :
2245 : 13348 : if (!save_temps_flag)
2246 : 13310 : return make_temp_file ("");
2247 : :
2248 : 38 : base = dumpbase;
2249 : 38 : if (!(base && *base))
2250 : 11 : base = dumpdir;
2251 : 38 : if (!(base && *base))
2252 : 0 : base = "a";
2253 : :
2254 : 38 : sprintf (filename, ".args.%d", fileno++);
2255 : 38 : ext = filename;
2256 : :
2257 : 38 : if (base == dumpdir && dumpdir_trailing_dash_added)
2258 : 38 : ext++;
2259 : :
2260 : 38 : return concat (base, ext, NULL);
2261 : : }
2262 : :
2263 : : /* Close the temporary @file and add @file to the argument list. */
2264 : :
2265 : : static void
2266 : 13358 : close_at_file (void)
2267 : : {
2268 : 13358 : if (!in_at_file)
2269 : 0 : fatal_error (input_location, "cannot close nonexistent response file");
2270 : :
2271 : 13358 : in_at_file = false;
2272 : :
2273 : 13358 : const unsigned int n_args = at_file_argbuf.length ();
2274 : 13358 : if (n_args == 0)
2275 : : return;
2276 : :
2277 : 13348 : char **argv = XALLOCAVEC (char *, n_args + 1);
2278 : 13348 : char *temp_file = make_at_file ();
2279 : 13348 : char *at_argument = concat ("@", temp_file, NULL);
2280 : 13348 : FILE *f = fopen (temp_file, "w");
2281 : 13348 : int status;
2282 : 13348 : unsigned int i;
2283 : :
2284 : : /* Copy the strings over. */
2285 : 41089 : for (i = 0; i < n_args; i++)
2286 : 14393 : argv[i] = CONST_CAST (char *, at_file_argbuf[i]);
2287 : 13348 : argv[i] = NULL;
2288 : :
2289 : 13348 : at_file_argbuf.truncate (0);
2290 : :
2291 : 13348 : if (f == NULL)
2292 : 0 : fatal_error (input_location, "could not open temporary response file %s",
2293 : : temp_file);
2294 : :
2295 : 13348 : status = writeargv (argv, f);
2296 : :
2297 : 13348 : if (status)
2298 : 0 : fatal_error (input_location,
2299 : : "could not write to temporary response file %s",
2300 : : temp_file);
2301 : :
2302 : 13348 : status = fclose (f);
2303 : :
2304 : 13348 : if (status == EOF)
2305 : 0 : fatal_error (input_location, "could not close temporary response file %s",
2306 : : temp_file);
2307 : :
2308 : 13348 : store_arg (at_argument, 0, 0);
2309 : :
2310 : 13348 : record_temp_file (temp_file, !save_temps_flag, !save_temps_flag);
2311 : : }
2312 : :
2313 : : /* Load specs from a file name named FILENAME, replacing occurrences of
2314 : : various different types of line-endings, \r\n, \n\r and just \r, with
2315 : : a single \n. */
2316 : :
2317 : : static char *
2318 : 329581 : load_specs (const char *filename)
2319 : : {
2320 : 329581 : int desc;
2321 : 329581 : int readlen;
2322 : 329581 : struct stat statbuf;
2323 : 329581 : char *buffer;
2324 : 329581 : char *buffer_p;
2325 : 329581 : char *specs;
2326 : 329581 : char *specs_p;
2327 : :
2328 : 329581 : if (verbose_flag)
2329 : 1375 : fnotice (stderr, "Reading specs from %s\n", filename);
2330 : :
2331 : : /* Open and stat the file. */
2332 : 329581 : desc = open (filename, O_RDONLY, 0);
2333 : 329581 : if (desc < 0)
2334 : : {
2335 : 4 : failed:
2336 : : /* This leaves DESC open, but the OS will save us. */
2337 : 4 : fatal_error (input_location, "cannot read spec file %qs: %m", filename);
2338 : : }
2339 : :
2340 : 329577 : if (stat (filename, &statbuf) < 0)
2341 : 0 : goto failed;
2342 : :
2343 : : /* Read contents of file into BUFFER. */
2344 : 329577 : buffer = XNEWVEC (char, statbuf.st_size + 1);
2345 : 329577 : readlen = read (desc, buffer, (unsigned) statbuf.st_size);
2346 : 329577 : if (readlen < 0)
2347 : 0 : goto failed;
2348 : 329577 : buffer[readlen] = 0;
2349 : 329577 : close (desc);
2350 : :
2351 : 329577 : specs = XNEWVEC (char, readlen + 1);
2352 : 329577 : specs_p = specs;
2353 : 3023195427 : for (buffer_p = buffer; buffer_p && *buffer_p; buffer_p++)
2354 : : {
2355 : 3022865850 : int skip = 0;
2356 : 3022865850 : char c = *buffer_p;
2357 : 3022865850 : if (c == '\r')
2358 : : {
2359 : 0 : if (buffer_p > buffer && *(buffer_p - 1) == '\n') /* \n\r */
2360 : : skip = 1;
2361 : 0 : else if (*(buffer_p + 1) == '\n') /* \r\n */
2362 : : skip = 1;
2363 : : else /* \r */
2364 : : c = '\n';
2365 : : }
2366 : : if (! skip)
2367 : 3022865850 : *specs_p++ = c;
2368 : : }
2369 : 329577 : *specs_p = '\0';
2370 : :
2371 : 329577 : free (buffer);
2372 : 329577 : return (specs);
2373 : : }
2374 : :
2375 : : /* Read compilation specs from a file named FILENAME,
2376 : : replacing the default ones.
2377 : :
2378 : : A suffix which starts with `*' is a definition for
2379 : : one of the machine-specific sub-specs. The "suffix" should be
2380 : : *asm, *cc1, *cpp, *link, *startfile, etc.
2381 : : The corresponding spec is stored in asm_spec, etc.,
2382 : : rather than in the `compilers' vector.
2383 : :
2384 : : Anything invalid in the file is a fatal error. */
2385 : :
2386 : : static void
2387 : 329581 : read_specs (const char *filename, bool main_p, bool user_p)
2388 : : {
2389 : 329581 : char *buffer;
2390 : 329581 : char *p;
2391 : :
2392 : 329581 : buffer = load_specs (filename);
2393 : :
2394 : : /* Scan BUFFER for specs, putting them in the vector. */
2395 : 329581 : p = buffer;
2396 : 14429315 : while (1)
2397 : : {
2398 : 14429315 : char *suffix;
2399 : 14429315 : char *spec;
2400 : 14429315 : char *in, *out, *p1, *p2, *p3;
2401 : :
2402 : : /* Advance P in BUFFER to the next nonblank nocomment line. */
2403 : 14429315 : p = skip_whitespace (p);
2404 : 14429315 : if (*p == 0)
2405 : : break;
2406 : :
2407 : : /* Is this a special command that starts with '%'? */
2408 : : /* Don't allow this for the main specs file, since it would
2409 : : encourage people to overwrite it. */
2410 : 14099738 : if (*p == '%' && !main_p)
2411 : : {
2412 : 423080 : p1 = p;
2413 : 423080 : while (*p && *p != '\n')
2414 : 401926 : p++;
2415 : :
2416 : : /* Skip '\n'. */
2417 : 21154 : p++;
2418 : :
2419 : 21154 : if (startswith (p1, "%include")
2420 : 21154 : && (p1[sizeof "%include" - 1] == ' '
2421 : 0 : || p1[sizeof "%include" - 1] == '\t'))
2422 : : {
2423 : 0 : char *new_filename;
2424 : :
2425 : 0 : p1 += sizeof ("%include");
2426 : 0 : while (*p1 == ' ' || *p1 == '\t')
2427 : 0 : p1++;
2428 : :
2429 : 0 : if (*p1++ != '<' || p[-2] != '>')
2430 : 0 : fatal_error (input_location,
2431 : : "specs %%include syntax malformed after "
2432 : 0 : "%td characters", p1 - buffer + 1);
2433 : :
2434 : 0 : p[-2] = '\0';
2435 : 0 : new_filename = find_a_file (&startfile_prefixes, p1, R_OK, true);
2436 : 0 : read_specs (new_filename ? new_filename : p1, false, user_p);
2437 : 0 : continue;
2438 : 0 : }
2439 : 21154 : else if (startswith (p1, "%include_noerr")
2440 : 21154 : && (p1[sizeof "%include_noerr" - 1] == ' '
2441 : 0 : || p1[sizeof "%include_noerr" - 1] == '\t'))
2442 : : {
2443 : 0 : char *new_filename;
2444 : :
2445 : 0 : p1 += sizeof "%include_noerr";
2446 : 0 : while (*p1 == ' ' || *p1 == '\t')
2447 : 0 : p1++;
2448 : :
2449 : 0 : if (*p1++ != '<' || p[-2] != '>')
2450 : 0 : fatal_error (input_location,
2451 : : "specs %%include syntax malformed after "
2452 : 0 : "%td characters", p1 - buffer + 1);
2453 : :
2454 : 0 : p[-2] = '\0';
2455 : 0 : new_filename = find_a_file (&startfile_prefixes, p1, R_OK, true);
2456 : 0 : if (new_filename)
2457 : 0 : read_specs (new_filename, false, user_p);
2458 : 0 : else if (verbose_flag)
2459 : 0 : fnotice (stderr, "could not find specs file %s\n", p1);
2460 : 0 : continue;
2461 : 0 : }
2462 : 21154 : else if (startswith (p1, "%rename")
2463 : 21154 : && (p1[sizeof "%rename" - 1] == ' '
2464 : 0 : || p1[sizeof "%rename" - 1] == '\t'))
2465 : : {
2466 : 21154 : int name_len;
2467 : 21154 : struct spec_list *sl;
2468 : 21154 : struct spec_list *newsl;
2469 : :
2470 : : /* Get original name. */
2471 : 21154 : p1 += sizeof "%rename";
2472 : 21154 : while (*p1 == ' ' || *p1 == '\t')
2473 : 0 : p1++;
2474 : :
2475 : 21154 : if (! ISALPHA ((unsigned char) *p1))
2476 : 0 : fatal_error (input_location,
2477 : : "specs %%rename syntax malformed after "
2478 : : "%td characters", p1 - buffer);
2479 : :
2480 : : p2 = p1;
2481 : 84616 : while (*p2 && !ISSPACE ((unsigned char) *p2))
2482 : 63462 : p2++;
2483 : :
2484 : 21154 : if (*p2 != ' ' && *p2 != '\t')
2485 : 0 : fatal_error (input_location,
2486 : : "specs %%rename syntax malformed after "
2487 : : "%td characters", p2 - buffer);
2488 : :
2489 : 21154 : name_len = p2 - p1;
2490 : 21154 : *p2++ = '\0';
2491 : 21154 : while (*p2 == ' ' || *p2 == '\t')
2492 : 0 : p2++;
2493 : :
2494 : 21154 : if (! ISALPHA ((unsigned char) *p2))
2495 : 0 : fatal_error (input_location,
2496 : : "specs %%rename syntax malformed after "
2497 : : "%td characters", p2 - buffer);
2498 : :
2499 : : /* Get new spec name. */
2500 : : p3 = p2;
2501 : 169232 : while (*p3 && !ISSPACE ((unsigned char) *p3))
2502 : 148078 : p3++;
2503 : :
2504 : 21154 : if (p3 != p - 1)
2505 : 0 : fatal_error (input_location,
2506 : : "specs %%rename syntax malformed after "
2507 : : "%td characters", p3 - buffer);
2508 : 21154 : *p3 = '\0';
2509 : :
2510 : 423080 : for (sl = specs; sl; sl = sl->next)
2511 : 423080 : if (name_len == sl->name_len && !strcmp (sl->name, p1))
2512 : : break;
2513 : :
2514 : 21154 : if (!sl)
2515 : 0 : fatal_error (input_location,
2516 : : "specs %s spec was not found to be renamed", p1);
2517 : :
2518 : 21154 : if (strcmp (p1, p2) == 0)
2519 : 0 : continue;
2520 : :
2521 : 994238 : for (newsl = specs; newsl; newsl = newsl->next)
2522 : 973084 : if (strcmp (newsl->name, p2) == 0)
2523 : 0 : fatal_error (input_location,
2524 : : "%s: attempt to rename spec %qs to "
2525 : : "already defined spec %qs",
2526 : : filename, p1, p2);
2527 : :
2528 : 21154 : if (verbose_flag)
2529 : : {
2530 : 0 : fnotice (stderr, "rename spec %s to %s\n", p1, p2);
2531 : : #ifdef DEBUG_SPECS
2532 : : fnotice (stderr, "spec is '%s'\n\n", *(sl->ptr_spec));
2533 : : #endif
2534 : : }
2535 : :
2536 : 21154 : set_spec (p2, *(sl->ptr_spec), user_p);
2537 : 21154 : if (sl->alloc_p)
2538 : 21154 : free (CONST_CAST (char *, *(sl->ptr_spec)));
2539 : :
2540 : 21154 : *(sl->ptr_spec) = "";
2541 : 21154 : sl->alloc_p = 0;
2542 : 21154 : continue;
2543 : 21154 : }
2544 : : else
2545 : 0 : fatal_error (input_location,
2546 : : "specs unknown %% command after %td characters",
2547 : : p1 - buffer);
2548 : : }
2549 : :
2550 : : /* Find the colon that should end the suffix. */
2551 : : p1 = p;
2552 : 193304612 : while (*p1 && *p1 != ':' && *p1 != '\n')
2553 : 179226028 : p1++;
2554 : :
2555 : : /* The colon shouldn't be missing. */
2556 : 14078584 : if (*p1 != ':')
2557 : 0 : fatal_error (input_location,
2558 : : "specs file malformed after %td characters",
2559 : : p1 - buffer);
2560 : :
2561 : : /* Skip back over trailing whitespace. */
2562 : : p2 = p1;
2563 : 14078584 : while (p2 > buffer && (p2[-1] == ' ' || p2[-1] == '\t'))
2564 : 0 : p2--;
2565 : :
2566 : : /* Copy the suffix to a string. */
2567 : 14078584 : suffix = save_string (p, p2 - p);
2568 : : /* Find the next line. */
2569 : 14078584 : p = skip_whitespace (p1 + 1);
2570 : 14078584 : if (p[1] == 0)
2571 : 0 : fatal_error (input_location,
2572 : : "specs file malformed after %td characters",
2573 : : p - buffer);
2574 : :
2575 : : p1 = p;
2576 : : /* Find next blank line or end of string. */
2577 : 2797349331 : while (*p1 && !(*p1 == '\n' && (p1[1] == '\n' || p1[1] == '\0')))
2578 : 2783270747 : p1++;
2579 : :
2580 : : /* Specs end at the blank line and do not include the newline. */
2581 : 14078584 : spec = save_string (p, p1 - p);
2582 : 14078584 : p = p1;
2583 : :
2584 : : /* Delete backslash-newline sequences from the spec. */
2585 : 14078584 : in = spec;
2586 : 14078584 : out = spec;
2587 : 2811427913 : while (*in != 0)
2588 : : {
2589 : 2783270745 : if (in[0] == '\\' && in[1] == '\n')
2590 : 2 : in += 2;
2591 : 2783270743 : else if (in[0] == '#')
2592 : 0 : while (*in && *in != '\n')
2593 : 0 : in++;
2594 : :
2595 : : else
2596 : 2783270743 : *out++ = *in++;
2597 : : }
2598 : 14078584 : *out = 0;
2599 : :
2600 : 14078584 : if (suffix[0] == '*')
2601 : : {
2602 : 14078584 : if (! strcmp (suffix, "*link_command"))
2603 : 298968 : link_command_spec = spec;
2604 : : else
2605 : : {
2606 : 13779616 : set_spec (suffix + 1, spec, user_p);
2607 : 13779616 : free (spec);
2608 : : }
2609 : : }
2610 : : else
2611 : : {
2612 : : /* Add this pair to the vector. */
2613 : 0 : compilers
2614 : 0 : = XRESIZEVEC (struct compiler, compilers, n_compilers + 2);
2615 : :
2616 : 0 : compilers[n_compilers].suffix = suffix;
2617 : 0 : compilers[n_compilers].spec = spec;
2618 : 0 : n_compilers++;
2619 : 0 : memset (&compilers[n_compilers], 0, sizeof compilers[n_compilers]);
2620 : : }
2621 : :
2622 : 14078584 : if (*suffix == 0)
2623 : 0 : link_command_spec = spec;
2624 : : }
2625 : :
2626 : 329577 : if (link_command_spec == 0)
2627 : 0 : fatal_error (input_location, "spec file has no spec for linking");
2628 : :
2629 : 329577 : XDELETEVEC (buffer);
2630 : 329577 : }
2631 : :
2632 : : /* Record the names of temporary files we tell compilers to write,
2633 : : and delete them at the end of the run. */
2634 : :
2635 : : /* This is the common prefix we use to make temp file names.
2636 : : It is chosen once for each run of this program.
2637 : : It is substituted into a spec by %g or %j.
2638 : : Thus, all temp file names contain this prefix.
2639 : : In practice, all temp file names start with this prefix.
2640 : :
2641 : : This prefix comes from the envvar TMPDIR if it is defined;
2642 : : otherwise, from the P_tmpdir macro if that is defined;
2643 : : otherwise, in /usr/tmp or /tmp;
2644 : : or finally the current directory if all else fails. */
2645 : :
2646 : : static const char *temp_filename;
2647 : :
2648 : : /* Length of the prefix. */
2649 : :
2650 : : static int temp_filename_length;
2651 : :
2652 : : /* Define the list of temporary files to delete. */
2653 : :
2654 : : struct temp_file
2655 : : {
2656 : : const char *name;
2657 : : struct temp_file *next;
2658 : : };
2659 : :
2660 : : /* Queue of files to delete on success or failure of compilation. */
2661 : : static struct temp_file *always_delete_queue;
2662 : : /* Queue of files to delete on failure of compilation. */
2663 : : static struct temp_file *failure_delete_queue;
2664 : :
2665 : : /* Record FILENAME as a file to be deleted automatically.
2666 : : ALWAYS_DELETE nonzero means delete it if all compilation succeeds;
2667 : : otherwise delete it in any case.
2668 : : FAIL_DELETE nonzero means delete it if a compilation step fails;
2669 : : otherwise delete it in any case. */
2670 : :
2671 : : void
2672 : 709119 : record_temp_file (const char *filename, int always_delete, int fail_delete)
2673 : : {
2674 : 709119 : char *const name = xstrdup (filename);
2675 : :
2676 : 709119 : if (always_delete)
2677 : : {
2678 : 535237 : struct temp_file *temp;
2679 : 927801 : for (temp = always_delete_queue; temp; temp = temp->next)
2680 : 558837 : if (! filename_cmp (name, temp->name))
2681 : : {
2682 : 166273 : free (name);
2683 : 166273 : goto already1;
2684 : : }
2685 : :
2686 : 368964 : temp = XNEW (struct temp_file);
2687 : 368964 : temp->next = always_delete_queue;
2688 : 368964 : temp->name = name;
2689 : 368964 : always_delete_queue = temp;
2690 : :
2691 : 709119 : already1:;
2692 : : }
2693 : :
2694 : 709119 : if (fail_delete)
2695 : : {
2696 : 284765 : struct temp_file *temp;
2697 : 289776 : for (temp = failure_delete_queue; temp; temp = temp->next)
2698 : 5100 : if (! filename_cmp (name, temp->name))
2699 : : {
2700 : 89 : free (name);
2701 : 89 : goto already2;
2702 : : }
2703 : :
2704 : 284676 : temp = XNEW (struct temp_file);
2705 : 284676 : temp->next = failure_delete_queue;
2706 : 284676 : temp->name = name;
2707 : 284676 : failure_delete_queue = temp;
2708 : :
2709 : 709119 : already2:;
2710 : : }
2711 : 709119 : }
2712 : :
2713 : : /* Delete all the temporary files whose names we previously recorded. */
2714 : :
2715 : : #ifndef DELETE_IF_ORDINARY
2716 : : #define DELETE_IF_ORDINARY(NAME,ST,VERBOSE_FLAG) \
2717 : : do \
2718 : : { \
2719 : : if (stat (NAME, &ST) >= 0 && S_ISREG (ST.st_mode)) \
2720 : : if (unlink (NAME) < 0) \
2721 : : if (VERBOSE_FLAG) \
2722 : : error ("%s: %m", (NAME)); \
2723 : : } while (0)
2724 : : #endif
2725 : :
2726 : : static void
2727 : 392098 : delete_if_ordinary (const char *name)
2728 : : {
2729 : 392098 : struct stat st;
2730 : : #ifdef DEBUG
2731 : : int i, c;
2732 : :
2733 : : printf ("Delete %s? (y or n) ", name);
2734 : : fflush (stdout);
2735 : : i = getchar ();
2736 : : if (i != '\n')
2737 : : while ((c = getchar ()) != '\n' && c != EOF)
2738 : : ;
2739 : :
2740 : : if (i == 'y' || i == 'Y')
2741 : : #endif /* DEBUG */
2742 : 392098 : DELETE_IF_ORDINARY (name, st, verbose_flag);
2743 : 392098 : }
2744 : :
2745 : : static void
2746 : 584812 : delete_temp_files (void)
2747 : : {
2748 : 584812 : struct temp_file *temp;
2749 : :
2750 : 953776 : for (temp = always_delete_queue; temp; temp = temp->next)
2751 : 368964 : delete_if_ordinary (temp->name);
2752 : 584812 : always_delete_queue = 0;
2753 : 584812 : }
2754 : :
2755 : : /* Delete all the files to be deleted on error. */
2756 : :
2757 : : static void
2758 : 58356 : delete_failure_queue (void)
2759 : : {
2760 : 58356 : struct temp_file *temp;
2761 : :
2762 : 81490 : for (temp = failure_delete_queue; temp; temp = temp->next)
2763 : 23134 : delete_if_ordinary (temp->name);
2764 : 58356 : }
2765 : :
2766 : : static void
2767 : 544371 : clear_failure_queue (void)
2768 : : {
2769 : 544371 : failure_delete_queue = 0;
2770 : 544371 : }
2771 : :
2772 : : /* Call CALLBACK for each path in PATHS, breaking out early if CALLBACK
2773 : : returns non-NULL.
2774 : : If DO_MULTI is true iterate over the paths twice, first with multilib
2775 : : suffix then without, otherwise iterate over the paths once without
2776 : : adding a multilib suffix. When DO_MULTI is true, some attempt is made
2777 : : to avoid visiting the same path twice, but we could do better. For
2778 : : instance, /usr/lib/../lib is considered different from /usr/lib.
2779 : : At least EXTRA_SPACE chars past the end of the path passed to
2780 : : CALLBACK are available for use by the callback.
2781 : : CALLBACK_INFO allows extra parameters to be passed to CALLBACK.
2782 : :
2783 : : Returns the value returned by CALLBACK. */
2784 : :
2785 : : template<typename fun>
2786 : : auto *
2787 : 2844466 : for_each_path (const struct path_prefix *paths,
2788 : : bool do_multi,
2789 : : size_t extra_space,
2790 : : fun && callback)
2791 : : {
2792 : : struct prefix_list *pl;
2793 : 2844466 : const char *multi_dir = NULL;
2794 : 2844466 : const char *multi_os_dir = NULL;
2795 : 2844466 : const char *multiarch_suffix = NULL;
2796 : : const char *multi_suffix;
2797 : : const char *just_multi_suffix;
2798 : 2844466 : char *path = NULL;
2799 : 2844466 : decltype (callback (nullptr)) ret = nullptr;
2800 : 2844466 : bool skip_multi_dir = false;
2801 : 2844466 : bool skip_multi_os_dir = false;
2802 : :
2803 : 2844466 : multi_suffix = machine_suffix;
2804 : 2844466 : just_multi_suffix = just_machine_suffix;
2805 : 2844466 : if (do_multi && multilib_dir && strcmp (multilib_dir, ".") != 0)
2806 : : {
2807 : 15820 : multi_dir = concat (multilib_dir, dir_separator_str, NULL);
2808 : 15820 : multi_suffix = concat (multi_suffix, multi_dir, NULL);
2809 : 15820 : just_multi_suffix = concat (just_multi_suffix, multi_dir, NULL);
2810 : : }
2811 : 1237124 : if (do_multi && multilib_os_dir && strcmp (multilib_os_dir, ".") != 0)
2812 : 937009 : multi_os_dir = concat (multilib_os_dir, dir_separator_str, NULL);
2813 : 2844466 : if (multiarch_dir)
2814 : 0 : multiarch_suffix = concat (multiarch_dir, dir_separator_str, NULL);
2815 : :
2816 : : while (1)
2817 : : {
2818 : 3268682 : size_t multi_dir_len = 0;
2819 : 3268682 : size_t multi_os_dir_len = 0;
2820 : 3268682 : size_t multiarch_len = 0;
2821 : : size_t suffix_len;
2822 : : size_t just_suffix_len;
2823 : : size_t len;
2824 : :
2825 : 3268682 : if (multi_dir)
2826 : 15820 : multi_dir_len = strlen (multi_dir);
2827 : 3268682 : if (multi_os_dir)
2828 : 937009 : multi_os_dir_len = strlen (multi_os_dir);
2829 : 3268682 : if (multiarch_suffix)
2830 : 0 : multiarch_len = strlen (multiarch_suffix);
2831 : 3268682 : suffix_len = strlen (multi_suffix);
2832 : 3268682 : just_suffix_len = strlen (just_multi_suffix);
2833 : :
2834 : 3268682 : if (path == NULL)
2835 : : {
2836 : 2844466 : len = paths->max_len + extra_space + 1;
2837 : 2844466 : len += MAX (MAX (suffix_len, multi_os_dir_len), multiarch_len);
2838 : 2844466 : path = XNEWVEC (char, len);
2839 : : }
2840 : :
2841 : 12805024 : for (pl = paths->plist; pl != 0; pl = pl->next)
2842 : : {
2843 : 11219630 : len = strlen (pl->prefix);
2844 : 11219630 : memcpy (path, pl->prefix, len);
2845 : :
2846 : : /* Look first in MACHINE/VERSION subdirectory. */
2847 : 11219630 : if (!skip_multi_dir)
2848 : : {
2849 : 8209119 : memcpy (path + len, multi_suffix, suffix_len + 1);
2850 : 8209119 : ret = callback (path);
2851 : 5281511 : if (ret)
2852 : : break;
2853 : : }
2854 : :
2855 : : /* Some paths are tried with just the machine (ie. target)
2856 : : subdir. This is used for finding as, ld, etc. */
2857 : : if (!skip_multi_dir
2858 : 8209119 : && pl->require_machine_suffix == 2)
2859 : : {
2860 : 0 : memcpy (path + len, just_multi_suffix, just_suffix_len + 1);
2861 : 0 : ret = callback (path);
2862 : 0 : if (ret)
2863 : : break;
2864 : : }
2865 : :
2866 : : /* Now try the multiarch path. */
2867 : : if (!skip_multi_dir
2868 : 8209119 : && !pl->require_machine_suffix && multiarch_dir)
2869 : : {
2870 : 0 : memcpy (path + len, multiarch_suffix, multiarch_len + 1);
2871 : 0 : ret = callback (path);
2872 : 0 : if (ret)
2873 : : break;
2874 : : }
2875 : :
2876 : : /* Now try the base path. */
2877 : 11219630 : if (!pl->require_machine_suffix
2878 : 17840313 : && !(pl->os_multilib ? skip_multi_os_dir : skip_multi_dir))
2879 : : {
2880 : : const char *this_multi;
2881 : : size_t this_multi_len;
2882 : :
2883 : 10032764 : if (pl->os_multilib)
2884 : : {
2885 : : this_multi = multi_os_dir;
2886 : : this_multi_len = multi_os_dir_len;
2887 : : }
2888 : : else
2889 : : {
2890 : 5433817 : this_multi = multi_dir;
2891 : 5433817 : this_multi_len = multi_dir_len;
2892 : : }
2893 : :
2894 : 10032764 : if (this_multi_len)
2895 : 2795642 : memcpy (path + len, this_multi, this_multi_len + 1);
2896 : : else
2897 : 7237122 : path[len] = '\0';
2898 : :
2899 : 10032764 : ret = callback (path);
2900 : 5941838 : if (ret)
2901 : : break;
2902 : : }
2903 : : }
2904 : 2504339 : if (pl)
2905 : : break;
2906 : :
2907 : 1585394 : if (multi_dir == NULL && multi_os_dir == NULL)
2908 : : break;
2909 : :
2910 : : /* Run through the paths again, this time without multilibs.
2911 : : Don't repeat any we have already seen. */
2912 : 424216 : if (multi_dir)
2913 : : {
2914 : 10118 : free (CONST_CAST (char *, multi_dir));
2915 : 10118 : multi_dir = NULL;
2916 : 10118 : free (CONST_CAST (char *, multi_suffix));
2917 : 10118 : multi_suffix = machine_suffix;
2918 : 10118 : free (CONST_CAST (char *, just_multi_suffix));
2919 : 10118 : just_multi_suffix = just_machine_suffix;
2920 : : }
2921 : : else
2922 : : skip_multi_dir = true;
2923 : 424216 : if (multi_os_dir)
2924 : : {
2925 : 424216 : free (CONST_CAST (char *, multi_os_dir));
2926 : 424216 : multi_os_dir = NULL;
2927 : : }
2928 : : else
2929 : : skip_multi_os_dir = true;
2930 : : }
2931 : :
2932 : 2844466 : if (multi_dir)
2933 : : {
2934 : 5702 : free (CONST_CAST (char *, multi_dir));
2935 : 5702 : free (CONST_CAST (char *, multi_suffix));
2936 : 5702 : free (CONST_CAST (char *, just_multi_suffix));
2937 : : }
2938 : 2844466 : if (multi_os_dir)
2939 : 512793 : free (CONST_CAST (char *, multi_os_dir));
2940 : 2334904 : if (ret != path)
2941 : 1161178 : free (path);
2942 : 2844466 : return ret;
2943 : : }
2944 : :
2945 : : /* Add or change the value of an environment variable, outputting the
2946 : : change to standard error if in verbose mode. */
2947 : : static void
2948 : 1773692 : xputenv (const char *string)
2949 : : {
2950 : 0 : env.xput (string);
2951 : 137938 : }
2952 : :
2953 : : /* Build a list of search directories from PATHS.
2954 : : PREFIX is a string to prepend to the list.
2955 : : If CHECK_DIR_P is true we ensure the directory exists.
2956 : : If DO_MULTI is true, multilib paths are output first, then
2957 : : non-multilib paths.
2958 : : This is used mostly by putenv_from_prefixes so we use `collect_obstack'.
2959 : : It is also used by the --print-search-dirs flag. */
2960 : :
2961 : : static char *
2962 : 509562 : build_search_list (const struct path_prefix *paths, const char *prefix,
2963 : : bool check_dir, bool do_multi)
2964 : : {
2965 : 509562 : struct obstack *const ob = &collect_obstack;
2966 : 509562 : bool first_time = true;
2967 : :
2968 : 509562 : obstack_grow (&collect_obstack, prefix, strlen (prefix));
2969 : 509562 : obstack_1grow (&collect_obstack, '=');
2970 : :
2971 : : /* Callback adds path to obstack being built. */
2972 : 509562 : for_each_path (paths, do_multi, 0, [&](char *path) -> void*
2973 : : {
2974 : 7018534 : if (check_dir && !is_directory (path))
2975 : : return NULL;
2976 : :
2977 : 2587330 : if (!first_time)
2978 : 2078877 : obstack_1grow (ob, PATH_SEPARATOR);
2979 : :
2980 : 2587330 : obstack_grow (ob, path, strlen (path));
2981 : :
2982 : 2587330 : first_time = false;
2983 : 2587330 : return NULL;
2984 : : });
2985 : :
2986 : 509562 : obstack_1grow (&collect_obstack, '\0');
2987 : 509562 : return XOBFINISH (&collect_obstack, char *);
2988 : : }
2989 : :
2990 : : /* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables
2991 : : for collect. */
2992 : :
2993 : : static void
2994 : 509506 : putenv_from_prefixes (const struct path_prefix *paths, const char *env_var,
2995 : : bool do_multi)
2996 : : {
2997 : 509506 : xputenv (build_search_list (paths, env_var, true, do_multi));
2998 : 509506 : }
2999 : :
3000 : : /* Check whether NAME can be accessed in MODE. This is like access,
3001 : : except that it never considers directories to be executable. */
3002 : :
3003 : : static int
3004 : 7872274 : access_check (const char *name, int mode)
3005 : : {
3006 : 7872274 : if (mode == X_OK)
3007 : : {
3008 : 1521248 : struct stat st;
3009 : :
3010 : 1521248 : if (stat (name, &st) < 0
3011 : 1521248 : || S_ISDIR (st.st_mode))
3012 : 773246 : return -1;
3013 : : }
3014 : :
3015 : 7099028 : return access (name, mode);
3016 : : }
3017 : :
3018 : :
3019 : : /* Search for NAME using the prefix list PREFIXES. MODE is passed to
3020 : : access to check permissions. If DO_MULTI is true, search multilib
3021 : : paths then non-multilib paths, otherwise do not search multilib paths.
3022 : : Return 0 if not found, otherwise return its name, allocated with malloc. */
3023 : :
3024 : : static char *
3025 : 1782961 : find_a_file (const struct path_prefix *pprefix, const char *name, int mode,
3026 : : bool do_multi)
3027 : : {
3028 : : /* Find the filename in question (special case for absolute paths). */
3029 : :
3030 : 1782961 : if (IS_ABSOLUTE_PATH (name))
3031 : : {
3032 : 1 : if (access (name, mode) == 0)
3033 : 1 : return xstrdup (name);
3034 : :
3035 : : return NULL;
3036 : : }
3037 : :
3038 : 1782960 : const char *suffix = (mode & X_OK) != 0 ? HOST_EXECUTABLE_SUFFIX : "";
3039 : 1782960 : const int name_len = strlen (name);
3040 : 1782960 : const int suffix_len = strlen (suffix);
3041 : :
3042 : :
3043 : : /* Callback appends the file name to the directory path. If the
3044 : : resulting file exists in the right mode, return the full pathname
3045 : : to the file. */
3046 : 1782960 : return for_each_path (pprefix, do_multi,
3047 : : name_len + suffix_len,
3048 : 1782960 : [=](char *path) -> char*
3049 : : {
3050 : 7872274 : size_t len = strlen (path);
3051 : :
3052 : 7872274 : memcpy (path + len, name, name_len);
3053 : 7872274 : len += name_len;
3054 : :
3055 : : /* Some systems have a suffix for executable files.
3056 : : So try appending that first. */
3057 : 7872274 : if (suffix_len)
3058 : : {
3059 : 0 : memcpy (path + len, suffix, suffix_len + 1);
3060 : 0 : if (access_check (path, mode) == 0)
3061 : : return path;
3062 : : }
3063 : :
3064 : 7872274 : path[len] = '\0';
3065 : 7872274 : if (access_check (path, mode) == 0)
3066 : : return path;
3067 : :
3068 : : return NULL;
3069 : : });
3070 : : }
3071 : :
3072 : : /* Specialization of find_a_file for programs that also takes into account
3073 : : configure-specified default programs. */
3074 : :
3075 : : static char*
3076 : 754008 : find_a_program (const char *name)
3077 : : {
3078 : : /* Do not search if default matches query. */
3079 : :
3080 : : #ifdef DEFAULT_ASSEMBLER
3081 : : if (! strcmp (name, "as") && access (DEFAULT_ASSEMBLER, X_OK) == 0)
3082 : : return xstrdup (DEFAULT_ASSEMBLER);
3083 : : #endif
3084 : :
3085 : : #ifdef DEFAULT_LINKER
3086 : : if (! strcmp (name, "ld") && access (DEFAULT_LINKER, X_OK) == 0)
3087 : : return xstrdup (DEFAULT_LINKER);
3088 : : #endif
3089 : :
3090 : : #ifdef DEFAULT_DSYMUTIL
3091 : : if (! strcmp (name, "dsymutil") && access (DEFAULT_DSYMUTIL, X_OK) == 0)
3092 : : return xstrdup (DEFAULT_DSYMUTIL);
3093 : : #endif
3094 : :
3095 : 0 : return find_a_file (&exec_prefixes, name, X_OK, false);
3096 : : }
3097 : :
3098 : : /* Ranking of prefixes in the sort list. -B prefixes are put before
3099 : : all others. */
3100 : :
3101 : : enum path_prefix_priority
3102 : : {
3103 : : PREFIX_PRIORITY_B_OPT,
3104 : : PREFIX_PRIORITY_LAST
3105 : : };
3106 : :
3107 : : /* Add an entry for PREFIX in PLIST. The PLIST is kept in ascending
3108 : : order according to PRIORITY. Within each PRIORITY, new entries are
3109 : : appended.
3110 : :
3111 : : If WARN is nonzero, we will warn if no file is found
3112 : : through this prefix. WARN should point to an int
3113 : : which will be set to 1 if this entry is used.
3114 : :
3115 : : COMPONENT is the value to be passed to update_path.
3116 : :
3117 : : REQUIRE_MACHINE_SUFFIX is 1 if this prefix can't be used without
3118 : : the complete value of machine_suffix.
3119 : : 2 means try both machine_suffix and just_machine_suffix. */
3120 : :
3121 : : static void
3122 : 3919771 : add_prefix (struct path_prefix *pprefix, const char *prefix,
3123 : : const char *component, /* enum prefix_priority */ int priority,
3124 : : int require_machine_suffix, int os_multilib)
3125 : : {
3126 : 3919771 : struct prefix_list *pl, **prev;
3127 : 3919771 : int len;
3128 : :
3129 : 3919771 : for (prev = &pprefix->plist;
3130 : 12437642 : (*prev) != NULL && (*prev)->priority <= priority;
3131 : 8517871 : prev = &(*prev)->next)
3132 : : ;
3133 : :
3134 : : /* Keep track of the longest prefix. */
3135 : :
3136 : 3919771 : prefix = update_path (prefix, component);
3137 : 3919771 : len = strlen (prefix);
3138 : 3919771 : if (len > pprefix->max_len)
3139 : 2123055 : pprefix->max_len = len;
3140 : :
3141 : 3919771 : pl = XNEW (struct prefix_list);
3142 : 3919771 : pl->prefix = prefix;
3143 : 3919771 : pl->require_machine_suffix = require_machine_suffix;
3144 : 3919771 : pl->priority = priority;
3145 : 3919771 : pl->os_multilib = os_multilib;
3146 : :
3147 : : /* Insert after PREV. */
3148 : 3919771 : pl->next = (*prev);
3149 : 3919771 : (*prev) = pl;
3150 : 3919771 : }
3151 : :
3152 : : /* Same as add_prefix, but prepending target_system_root to prefix. */
3153 : : /* The target_system_root prefix has been relocated by gcc_exec_prefix. */
3154 : : static void
3155 : 600226 : add_sysrooted_prefix (struct path_prefix *pprefix, const char *prefix,
3156 : : const char *component,
3157 : : /* enum prefix_priority */ int priority,
3158 : : int require_machine_suffix, int os_multilib)
3159 : : {
3160 : 600226 : if (!IS_ABSOLUTE_PATH (prefix))
3161 : 0 : fatal_error (input_location, "system path %qs is not absolute", prefix);
3162 : :
3163 : 600226 : if (target_system_root)
3164 : : {
3165 : 0 : char *sysroot_no_trailing_dir_separator = xstrdup (target_system_root);
3166 : 0 : size_t sysroot_len = strlen (target_system_root);
3167 : :
3168 : 0 : if (sysroot_len > 0
3169 : 0 : && target_system_root[sysroot_len - 1] == DIR_SEPARATOR)
3170 : 0 : sysroot_no_trailing_dir_separator[sysroot_len - 1] = '\0';
3171 : :
3172 : 0 : if (target_sysroot_suffix)
3173 : 0 : prefix = concat (sysroot_no_trailing_dir_separator,
3174 : : target_sysroot_suffix, prefix, NULL);
3175 : : else
3176 : 0 : prefix = concat (sysroot_no_trailing_dir_separator, prefix, NULL);
3177 : :
3178 : 0 : free (sysroot_no_trailing_dir_separator);
3179 : :
3180 : : /* We have to override this because GCC's notion of sysroot
3181 : : moves along with GCC. */
3182 : 0 : component = "GCC";
3183 : : }
3184 : :
3185 : 600226 : add_prefix (pprefix, prefix, component, priority,
3186 : : require_machine_suffix, os_multilib);
3187 : 600226 : }
3188 : :
3189 : : /* Same as add_prefix, but prepending target_sysroot_hdrs_suffix to prefix. */
3190 : :
3191 : : static void
3192 : 30833 : add_sysrooted_hdrs_prefix (struct path_prefix *pprefix, const char *prefix,
3193 : : const char *component,
3194 : : /* enum prefix_priority */ int priority,
3195 : : int require_machine_suffix, int os_multilib)
3196 : : {
3197 : 30833 : if (!IS_ABSOLUTE_PATH (prefix))
3198 : 0 : fatal_error (input_location, "system path %qs is not absolute", prefix);
3199 : :
3200 : 30833 : if (target_system_root)
3201 : : {
3202 : 0 : char *sysroot_no_trailing_dir_separator = xstrdup (target_system_root);
3203 : 0 : size_t sysroot_len = strlen (target_system_root);
3204 : :
3205 : 0 : if (sysroot_len > 0
3206 : 0 : && target_system_root[sysroot_len - 1] == DIR_SEPARATOR)
3207 : 0 : sysroot_no_trailing_dir_separator[sysroot_len - 1] = '\0';
3208 : :
3209 : 0 : if (target_sysroot_hdrs_suffix)
3210 : 0 : prefix = concat (sysroot_no_trailing_dir_separator,
3211 : : target_sysroot_hdrs_suffix, prefix, NULL);
3212 : : else
3213 : 0 : prefix = concat (sysroot_no_trailing_dir_separator, prefix, NULL);
3214 : :
3215 : 0 : free (sysroot_no_trailing_dir_separator);
3216 : :
3217 : : /* We have to override this because GCC's notion of sysroot
3218 : : moves along with GCC. */
3219 : 0 : component = "GCC";
3220 : : }
3221 : :
3222 : 30833 : add_prefix (pprefix, prefix, component, priority,
3223 : : require_machine_suffix, os_multilib);
3224 : 30833 : }
3225 : :
3226 : :
3227 : : /* Execute the command specified by the arguments on the current line of spec.
3228 : : When using pipes, this includes several piped-together commands
3229 : : with `|' between them.
3230 : :
3231 : : Return 0 if successful, -1 if failed. */
3232 : :
3233 : : static int
3234 : 546703 : execute (void)
3235 : : {
3236 : 546703 : int i;
3237 : 546703 : int n_commands; /* # of command. */
3238 : 546703 : char *string;
3239 : 546703 : struct pex_obj *pex;
3240 : 546703 : struct command
3241 : : {
3242 : : const char *prog; /* program name. */
3243 : : const char **argv; /* vector of args. */
3244 : : };
3245 : 546703 : const char *arg;
3246 : :
3247 : 546703 : struct command *commands; /* each command buffer with above info. */
3248 : :
3249 : 546703 : gcc_assert (!processing_spec_function);
3250 : :
3251 : 546703 : if (wrapper_string)
3252 : : {
3253 : 0 : string = find_a_program (argbuf[0]);
3254 : 0 : if (string)
3255 : 0 : argbuf[0] = string;
3256 : 0 : insert_wrapper (wrapper_string);
3257 : : }
3258 : :
3259 : : /* Count # of piped commands. */
3260 : 17299861 : for (n_commands = 1, i = 0; argbuf.iterate (i, &arg); i++)
3261 : 16753158 : if (strcmp (arg, "|") == 0)
3262 : 0 : n_commands++;
3263 : :
3264 : : /* Get storage for each command. */
3265 : 546703 : commands = XALLOCAVEC (struct command, n_commands);
3266 : :
3267 : : /* Split argbuf into its separate piped processes,
3268 : : and record info about each one.
3269 : : Also search for the programs that are to be run. */
3270 : :
3271 : 546703 : argbuf.safe_push (0);
3272 : :
3273 : 546703 : commands[0].prog = argbuf[0]; /* first command. */
3274 : 546703 : commands[0].argv = argbuf.address ();
3275 : :
3276 : 546703 : if (!wrapper_string)
3277 : : {
3278 : 546703 : string = find_a_program(commands[0].prog);
3279 : 546703 : if (string)
3280 : 544124 : commands[0].argv[0] = string;
3281 : : }
3282 : :
3283 : 17846564 : for (n_commands = 1, i = 0; argbuf.iterate (i, &arg); i++)
3284 : 17299861 : if (arg && strcmp (arg, "|") == 0)
3285 : : { /* each command. */
3286 : : #if defined (__MSDOS__) || defined (OS2) || defined (VMS)
3287 : : fatal_error (input_location, "%<-pipe%> not supported");
3288 : : #endif
3289 : 0 : argbuf[i] = 0; /* Termination of command args. */
3290 : 0 : commands[n_commands].prog = argbuf[i + 1];
3291 : 0 : commands[n_commands].argv
3292 : 0 : = &(argbuf.address ())[i + 1];
3293 : 0 : string = find_a_program(commands[n_commands].prog);
3294 : 0 : if (string)
3295 : 0 : commands[n_commands].argv[0] = string;
3296 : 0 : n_commands++;
3297 : : }
3298 : :
3299 : : /* If -v, print what we are about to do, and maybe query. */
3300 : :
3301 : 546703 : if (verbose_flag)
3302 : : {
3303 : : /* For help listings, put a blank line between sub-processes. */
3304 : 1395 : if (print_help_list)
3305 : 9 : fputc ('\n', stderr);
3306 : :
3307 : : /* Print each piped command as a separate line. */
3308 : 2790 : for (i = 0; i < n_commands; i++)
3309 : : {
3310 : 1395 : const char *const *j;
3311 : :
3312 : 1395 : if (verbose_only_flag)
3313 : : {
3314 : 17828 : for (j = commands[i].argv; *j; j++)
3315 : : {
3316 : : const char *p;
3317 : 426049 : for (p = *j; *p; ++p)
3318 : 411712 : if (!ISALNUM ((unsigned char) *p)
3319 : 97315 : && *p != '_' && *p != '/' && *p != '-' && *p != '.')
3320 : : break;
3321 : 16815 : if (*p || !*j)
3322 : : {
3323 : 2478 : fprintf (stderr, " \"");
3324 : 128844 : for (p = *j; *p; ++p)
3325 : : {
3326 : 126366 : if (*p == '"' || *p == '\\' || *p == '$')
3327 : 0 : fputc ('\\', stderr);
3328 : 126366 : fputc (*p, stderr);
3329 : : }
3330 : 2478 : fputc ('"', stderr);
3331 : : }
3332 : : /* If it's empty, print "". */
3333 : 14337 : else if (!**j)
3334 : 0 : fprintf (stderr, " \"\"");
3335 : : else
3336 : 14337 : fprintf (stderr, " %s", *j);
3337 : : }
3338 : : }
3339 : : else
3340 : 11579 : for (j = commands[i].argv; *j; j++)
3341 : : /* If it's empty, print "". */
3342 : 11197 : if (!**j)
3343 : 0 : fprintf (stderr, " \"\"");
3344 : : else
3345 : 11197 : fprintf (stderr, " %s", *j);
3346 : :
3347 : : /* Print a pipe symbol after all but the last command. */
3348 : 1395 : if (i + 1 != n_commands)
3349 : 0 : fprintf (stderr, " |");
3350 : 1395 : fprintf (stderr, "\n");
3351 : : }
3352 : 1395 : fflush (stderr);
3353 : 1395 : if (verbose_only_flag != 0)
3354 : : {
3355 : : /* verbose_only_flag should act as if the spec was
3356 : : executed, so increment execution_count before
3357 : : returning. This prevents spurious warnings about
3358 : : unused linker input files, etc. */
3359 : 1013 : execution_count++;
3360 : 1013 : return 0;
3361 : : }
3362 : : #ifdef DEBUG
3363 : : fnotice (stderr, "\nGo ahead? (y or n) ");
3364 : : fflush (stderr);
3365 : : i = getchar ();
3366 : : if (i != '\n')
3367 : : while (getchar () != '\n')
3368 : : ;
3369 : :
3370 : : if (i != 'y' && i != 'Y')
3371 : : return 0;
3372 : : #endif /* DEBUG */
3373 : : }
3374 : :
3375 : : #ifdef ENABLE_VALGRIND_CHECKING
3376 : : /* Run the each command through valgrind. To simplify prepending the
3377 : : path to valgrind and the option "-q" (for quiet operation unless
3378 : : something triggers), we allocate a separate argv array. */
3379 : :
3380 : : for (i = 0; i < n_commands; i++)
3381 : : {
3382 : : const char **argv;
3383 : : int argc;
3384 : : int j;
3385 : :
3386 : : for (argc = 0; commands[i].argv[argc] != NULL; argc++)
3387 : : ;
3388 : :
3389 : : argv = XALLOCAVEC (const char *, argc + 3);
3390 : :
3391 : : argv[0] = VALGRIND_PATH;
3392 : : argv[1] = "-q";
3393 : : for (j = 2; j < argc + 2; j++)
3394 : : argv[j] = commands[i].argv[j - 2];
3395 : : argv[j] = NULL;
3396 : :
3397 : : commands[i].argv = argv;
3398 : : commands[i].prog = argv[0];
3399 : : }
3400 : : #endif
3401 : :
3402 : : /* Run each piped subprocess. */
3403 : :
3404 : 545690 : pex = pex_init (PEX_USE_PIPES | ((report_times || report_times_to_file)
3405 : : ? PEX_RECORD_TIMES : 0),
3406 : : progname, temp_filename);
3407 : 545690 : if (pex == NULL)
3408 : : fatal_error (input_location, "%<pex_init%> failed: %m");
3409 : :
3410 : 1091380 : for (i = 0; i < n_commands; i++)
3411 : : {
3412 : 545690 : const char *errmsg;
3413 : 545690 : int err;
3414 : 545690 : const char *string = commands[i].argv[0];
3415 : :
3416 : 545690 : errmsg = pex_run (pex,
3417 : 545690 : ((i + 1 == n_commands ? PEX_LAST : 0)
3418 : 545690 : | (string == commands[i].prog ? PEX_SEARCH : 0)),
3419 : : string, CONST_CAST (char **, commands[i].argv),
3420 : : NULL, NULL, &err);
3421 : 545690 : if (errmsg != NULL)
3422 : : {
3423 : 0 : errno = err;
3424 : 0 : fatal_error (input_location,
3425 : : err ? G_("cannot execute %qs: %s: %m")
3426 : : : G_("cannot execute %qs: %s"),
3427 : : string, errmsg);
3428 : : }
3429 : :
3430 : 545690 : if (i && string != commands[i].prog)
3431 : 0 : free (CONST_CAST (char *, string));
3432 : : }
3433 : :
3434 : 545690 : execution_count++;
3435 : :
3436 : : /* Wait for all the subprocesses to finish. */
3437 : :
3438 : 545690 : {
3439 : 545690 : int *statuses;
3440 : 545690 : struct pex_time *times = NULL;
3441 : 545690 : int ret_code = 0;
3442 : :
3443 : 545690 : statuses = XALLOCAVEC (int, n_commands);
3444 : 545690 : if (!pex_get_status (pex, n_commands, statuses))
3445 : 0 : fatal_error (input_location, "failed to get exit status: %m");
3446 : :
3447 : 545690 : if (report_times || report_times_to_file)
3448 : : {
3449 : 0 : times = XALLOCAVEC (struct pex_time, n_commands);
3450 : 0 : if (!pex_get_times (pex, n_commands, times))
3451 : 0 : fatal_error (input_location, "failed to get process times: %m");
3452 : : }
3453 : :
3454 : 545690 : pex_free (pex);
3455 : :
3456 : 1091380 : for (i = 0; i < n_commands; ++i)
3457 : : {
3458 : 545690 : int status = statuses[i];
3459 : :
3460 : 545690 : if (WIFSIGNALED (status))
3461 : 0 : switch (WTERMSIG (status))
3462 : : {
3463 : 0 : case SIGINT:
3464 : 0 : case SIGTERM:
3465 : : /* SIGQUIT and SIGKILL are not available on MinGW. */
3466 : : #ifdef SIGQUIT
3467 : 0 : case SIGQUIT:
3468 : : #endif
3469 : : #ifdef SIGKILL
3470 : 0 : case SIGKILL:
3471 : : #endif
3472 : : /* The user (or environment) did something to the
3473 : : inferior. Making this an ICE confuses the user into
3474 : : thinking there's a compiler bug. Much more likely is
3475 : : the user or OOM killer nuked it. */
3476 : 0 : fatal_error (input_location,
3477 : : "%s signal terminated program %s",
3478 : : strsignal (WTERMSIG (status)),
3479 : 0 : commands[i].prog);
3480 : 0 : break;
3481 : :
3482 : : #ifdef SIGPIPE
3483 : 0 : case SIGPIPE:
3484 : : /* SIGPIPE is a special case. It happens in -pipe mode
3485 : : when the compiler dies before the preprocessor is
3486 : : done, or the assembler dies before the compiler is
3487 : : done. There's generally been an error already, and
3488 : : this is just fallout. So don't generate another
3489 : : error unless we would otherwise have succeeded. */
3490 : 0 : if (signal_count || greatest_status >= MIN_FATAL_STATUS)
3491 : : {
3492 : 0 : signal_count++;
3493 : 0 : ret_code = -1;
3494 : 0 : break;
3495 : : }
3496 : : #endif
3497 : : /* FALLTHROUGH */
3498 : :
3499 : 0 : default:
3500 : : /* The inferior failed to catch the signal. */
3501 : 0 : internal_error_no_backtrace ("%s signal terminated program %s",
3502 : : strsignal (WTERMSIG (status)),
3503 : 0 : commands[i].prog);
3504 : : }
3505 : 545690 : else if (WIFEXITED (status)
3506 : 545690 : && WEXITSTATUS (status) >= MIN_FATAL_STATUS)
3507 : : {
3508 : : /* For ICEs in cc1, cc1obj, cc1plus see if it is
3509 : : reproducible or not. */
3510 : 29216 : const char *p;
3511 : 29216 : if (flag_report_bug
3512 : 0 : && WEXITSTATUS (status) == ICE_EXIT_CODE
3513 : 0 : && i == 0
3514 : 0 : && (p = strrchr (commands[0].argv[0], DIR_SEPARATOR))
3515 : 29216 : && startswith (p + 1, "cc1"))
3516 : 0 : try_generate_repro (commands[0].argv);
3517 : 29216 : if (WEXITSTATUS (status) > greatest_status)
3518 : 21 : greatest_status = WEXITSTATUS (status);
3519 : : ret_code = -1;
3520 : : }
3521 : :
3522 : 545690 : if (report_times || report_times_to_file)
3523 : : {
3524 : 0 : struct pex_time *pt = ×[i];
3525 : 0 : double ut, st;
3526 : :
3527 : 0 : ut = ((double) pt->user_seconds
3528 : 0 : + (double) pt->user_microseconds / 1.0e6);
3529 : 0 : st = ((double) pt->system_seconds
3530 : 0 : + (double) pt->system_microseconds / 1.0e6);
3531 : :
3532 : 0 : if (ut + st != 0)
3533 : : {
3534 : 0 : if (report_times)
3535 : 0 : fnotice (stderr, "# %s %.2f %.2f\n",
3536 : 0 : commands[i].prog, ut, st);
3537 : :
3538 : 0 : if (report_times_to_file)
3539 : : {
3540 : 0 : int c = 0;
3541 : 0 : const char *const *j;
3542 : :
3543 : 0 : fprintf (report_times_to_file, "%g %g", ut, st);
3544 : :
3545 : 0 : for (j = &commands[i].prog; *j; j = &commands[i].argv[++c])
3546 : : {
3547 : : const char *p;
3548 : 0 : for (p = *j; *p; ++p)
3549 : 0 : if (*p == '"' || *p == '\\' || *p == '$'
3550 : 0 : || ISSPACE (*p))
3551 : : break;
3552 : :
3553 : 0 : if (*p)
3554 : : {
3555 : 0 : fprintf (report_times_to_file, " \"");
3556 : 0 : for (p = *j; *p; ++p)
3557 : : {
3558 : 0 : if (*p == '"' || *p == '\\' || *p == '$')
3559 : 0 : fputc ('\\', report_times_to_file);
3560 : 0 : fputc (*p, report_times_to_file);
3561 : : }
3562 : 0 : fputc ('"', report_times_to_file);
3563 : : }
3564 : : else
3565 : 0 : fprintf (report_times_to_file, " %s", *j);
3566 : : }
3567 : :
3568 : 0 : fputc ('\n', report_times_to_file);
3569 : : }
3570 : : }
3571 : : }
3572 : : }
3573 : :
3574 : 545690 : if (commands[0].argv[0] != commands[0].prog)
3575 : 543111 : free (CONST_CAST (char *, commands[0].argv[0]));
3576 : :
3577 : : return ret_code;
3578 : : }
3579 : : }
3580 : :
3581 : : static struct switchstr *switches;
3582 : :
3583 : : static int n_switches;
3584 : :
3585 : : static int n_switches_alloc;
3586 : :
3587 : : /* Set to zero if -fcompare-debug is disabled, positive if it's
3588 : : enabled and we're running the first compilation, negative if it's
3589 : : enabled and we're running the second compilation. For most of the
3590 : : time, it's in the range -1..1, but it can be temporarily set to 2
3591 : : or 3 to indicate that the -fcompare-debug flags didn't come from
3592 : : the command-line, but rather from the GCC_COMPARE_DEBUG environment
3593 : : variable, until a synthesized -fcompare-debug flag is added to the
3594 : : command line. */
3595 : : int compare_debug;
3596 : :
3597 : : /* Set to nonzero if we've seen the -fcompare-debug-second flag. */
3598 : : int compare_debug_second;
3599 : :
3600 : : /* Set to the flags that should be passed to the second compilation in
3601 : : a -fcompare-debug compilation. */
3602 : : const char *compare_debug_opt;
3603 : :
3604 : : static struct switchstr *switches_debug_check[2];
3605 : :
3606 : : static int n_switches_debug_check[2];
3607 : :
3608 : : static int n_switches_alloc_debug_check[2];
3609 : :
3610 : : static char *debug_check_temp_file[2];
3611 : :
3612 : : /* Language is one of three things:
3613 : :
3614 : : 1) The name of a real programming language.
3615 : : 2) NULL, indicating that no one has figured out
3616 : : what it is yet.
3617 : : 3) '*', indicating that the file should be passed
3618 : : to the linker. */
3619 : : struct infile
3620 : : {
3621 : : const char *name;
3622 : : const char *language;
3623 : : struct compiler *incompiler;
3624 : : bool compiled;
3625 : : bool preprocessed;
3626 : : };
3627 : :
3628 : : /* Also a vector of input files specified. */
3629 : :
3630 : : static struct infile *infiles;
3631 : :
3632 : : int n_infiles;
3633 : :
3634 : : static int n_infiles_alloc;
3635 : :
3636 : : /* True if undefined environment variables encountered during spec processing
3637 : : are ok to ignore, typically when we're running for --help or --version. */
3638 : :
3639 : : static bool spec_undefvar_allowed;
3640 : :
3641 : : /* True if multiple input files are being compiled to a single
3642 : : assembly file. */
3643 : :
3644 : : static bool combine_inputs;
3645 : :
3646 : : /* This counts the number of libraries added by lang_specific_driver, so that
3647 : : we can tell if there were any user supplied any files or libraries. */
3648 : :
3649 : : static int added_libraries;
3650 : :
3651 : : /* And a vector of corresponding output files is made up later. */
3652 : :
3653 : : const char **outfiles;
3654 : :
3655 : : #if defined(HAVE_TARGET_OBJECT_SUFFIX) || defined(HAVE_TARGET_EXECUTABLE_SUFFIX)
3656 : :
3657 : : /* Convert NAME to a new name if it is the standard suffix. DO_EXE
3658 : : is true if we should look for an executable suffix. DO_OBJ
3659 : : is true if we should look for an object suffix. */
3660 : :
3661 : : static const char *
3662 : : convert_filename (const char *name, int do_exe ATTRIBUTE_UNUSED,
3663 : : int do_obj ATTRIBUTE_UNUSED)
3664 : : {
3665 : : #if defined(HAVE_TARGET_EXECUTABLE_SUFFIX)
3666 : : int i;
3667 : : #endif
3668 : : int len;
3669 : :
3670 : : if (name == NULL)
3671 : : return NULL;
3672 : :
3673 : : len = strlen (name);
3674 : :
3675 : : #ifdef HAVE_TARGET_OBJECT_SUFFIX
3676 : : /* Convert x.o to x.obj if TARGET_OBJECT_SUFFIX is ".obj". */
3677 : : if (do_obj && len > 2
3678 : : && name[len - 2] == '.'
3679 : : && name[len - 1] == 'o')
3680 : : {
3681 : : obstack_grow (&obstack, name, len - 2);
3682 : : obstack_grow0 (&obstack, TARGET_OBJECT_SUFFIX, strlen (TARGET_OBJECT_SUFFIX));
3683 : : name = XOBFINISH (&obstack, const char *);
3684 : : }
3685 : : #endif
3686 : :
3687 : : #if defined(HAVE_TARGET_EXECUTABLE_SUFFIX)
3688 : : /* If there is no filetype, make it the executable suffix (which includes
3689 : : the "."). But don't get confused if we have just "-o". */
3690 : : if (! do_exe || TARGET_EXECUTABLE_SUFFIX[0] == 0 || not_actual_file_p (name))
3691 : : return name;
3692 : :
3693 : : for (i = len - 1; i >= 0; i--)
3694 : : if (IS_DIR_SEPARATOR (name[i]))
3695 : : break;
3696 : :
3697 : : for (i++; i < len; i++)
3698 : : if (name[i] == '.')
3699 : : return name;
3700 : :
3701 : : obstack_grow (&obstack, name, len);
3702 : : obstack_grow0 (&obstack, TARGET_EXECUTABLE_SUFFIX,
3703 : : strlen (TARGET_EXECUTABLE_SUFFIX));
3704 : : name = XOBFINISH (&obstack, const char *);
3705 : : #endif
3706 : :
3707 : : return name;
3708 : : }
3709 : : #endif
3710 : :
3711 : : /* Display the command line switches accepted by gcc. */
3712 : : static void
3713 : 4 : display_help (void)
3714 : : {
3715 : 4 : printf (_("Usage: %s [options] file...\n"), progname);
3716 : 4 : fputs (_("Options:\n"), stdout);
3717 : :
3718 : 4 : fputs (_(" -pass-exit-codes Exit with highest error code from a phase.\n"), stdout);
3719 : 4 : fputs (_(" --help Display this information.\n"), stdout);
3720 : 4 : fputs (_(" --target-help Display target specific command line options "
3721 : : "(including assembler and linker options).\n"), stdout);
3722 : 4 : fputs (_(" --help={common|optimizers|params|target|warnings|[^]{joined|separate|undocumented}}[,...].\n"), stdout);
3723 : 4 : fputs (_(" Display specific types of command line options.\n"), stdout);
3724 : 4 : if (! verbose_flag)
3725 : 1 : fputs (_(" (Use '-v --help' to display command line options of sub-processes).\n"), stdout);
3726 : 4 : fputs (_(" --version Display compiler version information.\n"), stdout);
3727 : 4 : fputs (_(" -dumpspecs Display all of the built in spec strings.\n"), stdout);
3728 : 4 : fputs (_(" -dumpversion Display the version of the compiler.\n"), stdout);
3729 : 4 : fputs (_(" -dumpmachine Display the compiler's target processor.\n"), stdout);
3730 : 4 : fputs (_(" -foffload=<targets> Specify offloading targets.\n"), stdout);
3731 : 4 : fputs (_(" -print-search-dirs Display the directories in the compiler's search path.\n"), stdout);
3732 : 4 : fputs (_(" -print-libgcc-file-name Display the name of the compiler's companion library.\n"), stdout);
3733 : 4 : fputs (_(" -print-file-name=<lib> Display the full path to library <lib>.\n"), stdout);
3734 : 4 : fputs (_(" -print-prog-name=<prog> Display the full path to compiler component <prog>.\n"), stdout);
3735 : 4 : fputs (_("\
3736 : : -print-multiarch Display the target's normalized GNU triplet, used as\n\
3737 : : a component in the library path.\n"), stdout);
3738 : 4 : fputs (_(" -print-multi-directory Display the root directory for versions of libgcc.\n"), stdout);
3739 : 4 : fputs (_("\
3740 : : -print-multi-lib Display the mapping between command line options and\n\
3741 : : multiple library search directories.\n"), stdout);
3742 : 4 : fputs (_(" -print-multi-os-directory Display the relative path to OS libraries.\n"), stdout);
3743 : 4 : fputs (_(" -print-sysroot Display the target libraries directory.\n"), stdout);
3744 : 4 : fputs (_(" -print-sysroot-headers-suffix Display the sysroot suffix used to find headers.\n"), stdout);
3745 : 4 : fputs (_(" -Wa,<options> Pass comma-separated <options> on to the assembler.\n"), stdout);
3746 : 4 : fputs (_(" -Wp,<options> Pass comma-separated <options> on to the preprocessor.\n"), stdout);
3747 : 4 : fputs (_(" -Wl,<options> Pass comma-separated <options> on to the linker.\n"), stdout);
3748 : 4 : fputs (_(" -Xassembler <arg> Pass <arg> on to the assembler.\n"), stdout);
3749 : 4 : fputs (_(" -Xpreprocessor <arg> Pass <arg> on to the preprocessor.\n"), stdout);
3750 : 4 : fputs (_(" -Xlinker <arg> Pass <arg> on to the linker.\n"), stdout);
3751 : 4 : fputs (_(" -save-temps Do not delete intermediate files.\n"), stdout);
3752 : 4 : fputs (_(" -save-temps=<arg> Do not delete intermediate files.\n"), stdout);
3753 : 4 : fputs (_("\
3754 : : -no-canonical-prefixes Do not canonicalize paths when building relative\n\
3755 : : prefixes to other gcc components.\n"), stdout);
3756 : 4 : fputs (_(" -pipe Use pipes rather than intermediate files.\n"), stdout);
3757 : 4 : fputs (_(" -time Time the execution of each subprocess.\n"), stdout);
3758 : 4 : fputs (_(" -specs=<file> Override built-in specs with the contents of <file>.\n"), stdout);
3759 : 4 : fputs (_(" -std=<standard> Assume that the input sources are for <standard>.\n"), stdout);
3760 : 4 : fputs (_("\
3761 : : --sysroot=<directory> Use <directory> as the root directory for headers\n\
3762 : : and libraries.\n"), stdout);
3763 : 4 : fputs (_(" -B <directory> Add <directory> to the compiler's search paths.\n"), stdout);
3764 : 4 : fputs (_(" -v Display the programs invoked by the compiler.\n"), stdout);
3765 : 4 : fputs (_(" -### Like -v but options quoted and commands not executed.\n"), stdout);
3766 : 4 : fputs (_(" -E Preprocess only; do not compile, assemble or link.\n"), stdout);
3767 : 4 : fputs (_(" -S Compile only; do not assemble or link.\n"), stdout);
3768 : 4 : fputs (_(" -c Compile and assemble, but do not link.\n"), stdout);
3769 : 4 : fputs (_(" -o <file> Place the output into <file>.\n"), stdout);
3770 : 4 : fputs (_(" -pie Create a dynamically linked position independent\n\
3771 : : executable.\n"), stdout);
3772 : 4 : fputs (_(" -shared Create a shared library.\n"), stdout);
3773 : 4 : fputs (_("\
3774 : : -x <language> Specify the language of the following input files.\n\
3775 : : Permissible languages include: c c++ assembler none\n\
3776 : : 'none' means revert to the default behavior of\n\
3777 : : guessing the language based on the file's extension.\n\
3778 : : "), stdout);
3779 : :
3780 : 4 : printf (_("\
3781 : : \nOptions starting with -g, -f, -m, -O, -W, or --param are automatically\n\
3782 : : passed on to the various sub-processes invoked by %s. In order to pass\n\
3783 : : other options on to these processes the -W<letter> options must be used.\n\
3784 : : "), progname);
3785 : :
3786 : : /* The rest of the options are displayed by invocations of the various
3787 : : sub-processes. */
3788 : 4 : }
3789 : :
3790 : : static void
3791 : 0 : add_preprocessor_option (const char *option, int len)
3792 : : {
3793 : 0 : preprocessor_options.safe_push (save_string (option, len));
3794 : 0 : }
3795 : :
3796 : : static void
3797 : 183 : add_assembler_option (const char *option, int len)
3798 : : {
3799 : 183 : assembler_options.safe_push (save_string (option, len));
3800 : 183 : }
3801 : :
3802 : : static void
3803 : 82 : add_linker_option (const char *option, int len)
3804 : : {
3805 : 82 : linker_options.safe_push (save_string (option, len));
3806 : 82 : }
3807 : :
3808 : : /* Allocate space for an input file in infiles. */
3809 : :
3810 : : static void
3811 : 885400 : alloc_infile (void)
3812 : : {
3813 : 885400 : if (n_infiles_alloc == 0)
3814 : : {
3815 : 300114 : n_infiles_alloc = 16;
3816 : 300114 : infiles = XNEWVEC (struct infile, n_infiles_alloc);
3817 : : }
3818 : 585286 : else if (n_infiles_alloc == n_infiles)
3819 : : {
3820 : 247 : n_infiles_alloc *= 2;
3821 : 247 : infiles = XRESIZEVEC (struct infile, infiles, n_infiles_alloc);
3822 : : }
3823 : 885400 : }
3824 : :
3825 : : /* Store an input file with the given NAME and LANGUAGE in
3826 : : infiles. */
3827 : :
3828 : : static void
3829 : 585287 : add_infile (const char *name, const char *language)
3830 : : {
3831 : 585287 : alloc_infile ();
3832 : 585287 : infiles[n_infiles].name = name;
3833 : 585287 : infiles[n_infiles++].language = language;
3834 : 585287 : }
3835 : :
3836 : : /* Allocate space for a switch in switches. */
3837 : :
3838 : : static void
3839 : 7618547 : alloc_switch (void)
3840 : : {
3841 : 7618547 : if (n_switches_alloc == 0)
3842 : : {
3843 : 300428 : n_switches_alloc = 16;
3844 : 300428 : switches = XNEWVEC (struct switchstr, n_switches_alloc);
3845 : : }
3846 : 7318119 : else if (n_switches_alloc == n_switches)
3847 : : {
3848 : 269951 : n_switches_alloc *= 2;
3849 : 269951 : switches = XRESIZEVEC (struct switchstr, switches, n_switches_alloc);
3850 : : }
3851 : 7618547 : }
3852 : :
3853 : : /* Save an option OPT with N_ARGS arguments in array ARGS, marking it
3854 : : as validated if VALIDATED and KNOWN if it is an internal switch. */
3855 : :
3856 : : static void
3857 : 6751932 : save_switch (const char *opt, size_t n_args, const char *const *args,
3858 : : bool validated, bool known)
3859 : : {
3860 : 6751932 : alloc_switch ();
3861 : 6751932 : switches[n_switches].part1 = opt + 1;
3862 : 6751932 : if (n_args == 0)
3863 : 5217277 : switches[n_switches].args = 0;
3864 : : else
3865 : : {
3866 : 1534655 : switches[n_switches].args = XNEWVEC (const char *, n_args + 1);
3867 : 1534655 : memcpy (switches[n_switches].args, args, n_args * sizeof (const char *));
3868 : 1534655 : switches[n_switches].args[n_args] = NULL;
3869 : : }
3870 : :
3871 : 6751932 : switches[n_switches].live_cond = 0;
3872 : 6751932 : switches[n_switches].validated = validated;
3873 : 6751932 : switches[n_switches].known = known;
3874 : 6751932 : switches[n_switches].ordering = 0;
3875 : 6751932 : n_switches++;
3876 : 6751932 : }
3877 : :
3878 : : /* Set the SOURCE_DATE_EPOCH environment variable to the current time if it is
3879 : : not set already. */
3880 : :
3881 : : static void
3882 : 619 : set_source_date_epoch_envvar ()
3883 : : {
3884 : : /* Array size is 21 = ceil(log_10(2^64)) + 1 to hold string representations
3885 : : of 64 bit integers. */
3886 : 619 : char source_date_epoch[21];
3887 : 619 : time_t tt;
3888 : :
3889 : 619 : errno = 0;
3890 : 619 : tt = time (NULL);
3891 : 619 : if (tt < (time_t) 0 || errno != 0)
3892 : 0 : tt = (time_t) 0;
3893 : :
3894 : 619 : snprintf (source_date_epoch, 21, "%llu", (unsigned long long) tt);
3895 : : /* Using setenv instead of xputenv because we want the variable to remain
3896 : : after finalizing so that it's still set in the second run when using
3897 : : -fcompare-debug. */
3898 : 619 : setenv ("SOURCE_DATE_EPOCH", source_date_epoch, 0);
3899 : 619 : }
3900 : :
3901 : : /* Handle an option DECODED that is unknown to the option-processing
3902 : : machinery. */
3903 : :
3904 : : static bool
3905 : 681 : driver_unknown_option_callback (const struct cl_decoded_option *decoded)
3906 : : {
3907 : 681 : const char *opt = decoded->arg;
3908 : 681 : if (opt[1] == 'W' && opt[2] == 'n' && opt[3] == 'o' && opt[4] == '-'
3909 : 93 : && !(decoded->errors & CL_ERR_NEGATIVE))
3910 : : {
3911 : : /* Leave unknown -Wno-* options for the compiler proper, to be
3912 : : diagnosed only if there are warnings. */
3913 : 91 : save_switch (decoded->canonical_option[0],
3914 : 91 : decoded->canonical_option_num_elements - 1,
3915 : : &decoded->canonical_option[1], false, true);
3916 : 91 : return false;
3917 : : }
3918 : 590 : if (decoded->opt_index == OPT_SPECIAL_unknown)
3919 : : {
3920 : : /* Give it a chance to define it a spec file. */
3921 : 590 : save_switch (decoded->canonical_option[0],
3922 : 590 : decoded->canonical_option_num_elements - 1,
3923 : : &decoded->canonical_option[1], false, false);
3924 : 590 : return false;
3925 : : }
3926 : : else
3927 : : return true;
3928 : : }
3929 : :
3930 : : /* Handle an option DECODED that is not marked as CL_DRIVER.
3931 : : LANG_MASK will always be CL_DRIVER. */
3932 : :
3933 : : static void
3934 : 4354178 : driver_wrong_lang_callback (const struct cl_decoded_option *decoded,
3935 : : unsigned int lang_mask ATTRIBUTE_UNUSED)
3936 : : {
3937 : : /* At this point, non-driver options are accepted (and expected to
3938 : : be passed down by specs) unless marked to be rejected by the
3939 : : driver. Options to be rejected by the driver but accepted by the
3940 : : compilers proper are treated just like completely unknown
3941 : : options. */
3942 : 4354178 : const struct cl_option *option = &cl_options[decoded->opt_index];
3943 : :
3944 : 4354178 : if (option->cl_reject_driver)
3945 : 0 : error ("unrecognized command-line option %qs",
3946 : 0 : decoded->orig_option_with_args_text);
3947 : : else
3948 : 4354178 : save_switch (decoded->canonical_option[0],
3949 : 4354178 : decoded->canonical_option_num_elements - 1,
3950 : : &decoded->canonical_option[1], false, true);
3951 : 4354178 : }
3952 : :
3953 : : static const char *spec_lang = 0;
3954 : : static int last_language_n_infiles;
3955 : :
3956 : :
3957 : : /* Check that GCC is configured to support the offload target. */
3958 : :
3959 : : static bool
3960 : 148 : check_offload_target_name (const char *target, ptrdiff_t len)
3961 : : {
3962 : 148 : const char *n, *c = OFFLOAD_TARGETS;
3963 : 296 : while (c)
3964 : : {
3965 : 148 : n = strchr (c, ',');
3966 : 148 : if (n == NULL)
3967 : 148 : n = strchr (c, '\0');
3968 : 148 : if (len == n - c && strncmp (target, c, n - c) == 0)
3969 : : break;
3970 : 148 : c = *n ? n + 1 : NULL;
3971 : : }
3972 : 148 : if (!c)
3973 : : {
3974 : 148 : auto_vec<const char*> candidates;
3975 : 148 : size_t olen = strlen (OFFLOAD_TARGETS) + 1;
3976 : 148 : char *cand = XALLOCAVEC (char, olen);
3977 : 148 : memcpy (cand, OFFLOAD_TARGETS, olen);
3978 : 148 : for (c = strtok (cand, ","); c; c = strtok (NULL, ","))
3979 : 0 : candidates.safe_push (c);
3980 : 148 : candidates.safe_push ("default");
3981 : 148 : candidates.safe_push ("disable");
3982 : :
3983 : 148 : char *target2 = XALLOCAVEC (char, len + 1);
3984 : 148 : memcpy (target2, target, len);
3985 : 148 : target2[len] = '\0';
3986 : :
3987 : 148 : error ("GCC is not configured to support %qs as %<-foffload=%> argument",
3988 : : target2);
3989 : :
3990 : 148 : char *s;
3991 : 148 : const char *hint = candidates_list_and_hint (target2, s, candidates);
3992 : 148 : if (hint)
3993 : 0 : inform (UNKNOWN_LOCATION,
3994 : : "valid %<-foffload=%> arguments are: %s; "
3995 : : "did you mean %qs?", s, hint);
3996 : : else
3997 : 148 : inform (UNKNOWN_LOCATION, "valid %<-foffload=%> arguments are: %s", s);
3998 : 148 : XDELETEVEC (s);
3999 : 148 : return false;
4000 : 148 : }
4001 : : return true;
4002 : : }
4003 : :
4004 : : /* Sanity check for -foffload-options. */
4005 : :
4006 : : static void
4007 : 27 : check_foffload_target_names (const char *arg)
4008 : : {
4009 : 27 : const char *cur, *next, *end;
4010 : : /* If option argument starts with '-' then no target is specified and we
4011 : : do not need to parse it. */
4012 : 27 : if (arg[0] == '-')
4013 : : return;
4014 : 0 : end = strchr (arg, '=');
4015 : 0 : if (end == NULL)
4016 : : {
4017 : 0 : error ("%<=%>options missing after %<-foffload-options=%>target");
4018 : 0 : return;
4019 : : }
4020 : :
4021 : : cur = arg;
4022 : 0 : while (cur < end)
4023 : : {
4024 : 0 : next = strchr (cur, ',');
4025 : 0 : if (next == NULL)
4026 : 0 : next = end;
4027 : 0 : next = (next > end) ? end : next;
4028 : :
4029 : : /* Retain non-supported targets after printing an error as those will not
4030 : : be processed; each enabled target only processes its triplet. */
4031 : 0 : check_offload_target_name (cur, next - cur);
4032 : 0 : cur = next + 1;
4033 : : }
4034 : : }
4035 : :
4036 : : /* Parse -foffload option argument. */
4037 : :
4038 : : static void
4039 : 2869 : handle_foffload_option (const char *arg)
4040 : : {
4041 : 2869 : const char *c, *cur, *n, *next, *end;
4042 : 2869 : char *target;
4043 : :
4044 : : /* If option argument starts with '-' then no target is specified and we
4045 : : do not need to parse it. */
4046 : 2869 : if (arg[0] == '-')
4047 : : return;
4048 : :
4049 : 2030 : end = strchr (arg, '=');
4050 : 2030 : if (end == NULL)
4051 : 2030 : end = strchr (arg, '\0');
4052 : 2030 : cur = arg;
4053 : :
4054 : 2030 : while (cur < end)
4055 : : {
4056 : 2030 : next = strchr (cur, ',');
4057 : 2030 : if (next == NULL)
4058 : 2030 : next = end;
4059 : 2030 : next = (next > end) ? end : next;
4060 : :
4061 : 2030 : target = XNEWVEC (char, next - cur + 1);
4062 : 2030 : memcpy (target, cur, next - cur);
4063 : 2030 : target[next - cur] = '\0';
4064 : :
4065 : : /* Reset offloading list and continue. */
4066 : 2030 : if (strcmp (target, "default") == 0)
4067 : : {
4068 : 0 : free (offload_targets);
4069 : 0 : offload_targets = NULL;
4070 : 0 : goto next_item;
4071 : : }
4072 : :
4073 : : /* If 'disable' is passed to the option, clean the list of
4074 : : offload targets and return, even if more targets follow.
4075 : : Likewise if GCC is not configured to support that offload target. */
4076 : 2030 : if (strcmp (target, "disable") == 0
4077 : 2030 : || !check_offload_target_name (target, next - cur))
4078 : : {
4079 : 2030 : free (offload_targets);
4080 : 2030 : offload_targets = xstrdup ("");
4081 : 2030 : return;
4082 : : }
4083 : :
4084 : 0 : if (!offload_targets)
4085 : : {
4086 : 0 : offload_targets = target;
4087 : 0 : target = NULL;
4088 : : }
4089 : : else
4090 : : {
4091 : : /* Check that the target hasn't already presented in the list. */
4092 : : c = offload_targets;
4093 : 0 : do
4094 : : {
4095 : 0 : n = strchr (c, ':');
4096 : 0 : if (n == NULL)
4097 : 0 : n = strchr (c, '\0');
4098 : :
4099 : 0 : if (next - cur == n - c && strncmp (c, target, n - c) == 0)
4100 : : break;
4101 : :
4102 : 0 : c = n + 1;
4103 : : }
4104 : 0 : while (*n);
4105 : :
4106 : : /* If duplicate is not found, append the target to the list. */
4107 : 0 : if (c > n)
4108 : : {
4109 : 0 : size_t offload_targets_len = strlen (offload_targets);
4110 : 0 : offload_targets
4111 : 0 : = XRESIZEVEC (char, offload_targets,
4112 : : offload_targets_len + 1 + next - cur + 1);
4113 : 0 : offload_targets[offload_targets_len++] = ':';
4114 : 0 : memcpy (offload_targets + offload_targets_len, target, next - cur + 1);
4115 : : }
4116 : : }
4117 : 0 : next_item:
4118 : 0 : cur = next + 1;
4119 : 0 : XDELETEVEC (target);
4120 : : }
4121 : : }
4122 : :
4123 : : /* Forward certain options to offloading compilation. */
4124 : :
4125 : : static void
4126 : 0 : forward_offload_option (size_t opt_index, const char *arg, bool validated)
4127 : : {
4128 : 0 : switch (opt_index)
4129 : : {
4130 : 0 : case OPT_l:
4131 : : /* Use a '_GCC_' prefix and standard name ('-l_GCC_m' irrespective of the
4132 : : host's 'MATH_LIBRARY', for example), so that the 'mkoffload's can tell
4133 : : this has been synthesized here, and translate/drop as necessary. */
4134 : : /* Note that certain libraries ('-lc', '-lgcc', '-lgomp', for example)
4135 : : are injected by default in offloading compilation, and therefore not
4136 : : forwarded here. */
4137 : : /* GCC libraries. */
4138 : 0 : if (/* '-lgfortran' */ strcmp (arg, "gfortran") == 0
4139 : 0 : || /* '-lstdc++' */ strcmp (arg, "stdc++") == 0)
4140 : 0 : save_switch (concat ("-foffload-options=-l_GCC_", arg, NULL),
4141 : : 0, NULL, validated, true);
4142 : : /* Other libraries. */
4143 : : else
4144 : : {
4145 : : /* The case will need special consideration where on the host
4146 : : '!need_math', but for offloading compilation still need
4147 : : '-foffload-options=-l_GCC_m'. The problem is that we don't get
4148 : : here anything like '-lm', because it's not synthesized in
4149 : : 'gcc/fortran/gfortranspec.cc:lang_specific_driver', for example.
4150 : : Generally synthesizing '-foffload-options=-l_GCC_m' etc. in the
4151 : : language specific drivers is non-trivial, needs very careful
4152 : : review of their options handling. However, this issue is not
4153 : : actually relevant for the current set of supported host/offloading
4154 : : configurations. */
4155 : 0 : int need_math = (MATH_LIBRARY[0] != '\0');
4156 : 0 : if (/* '-lm' */ (need_math && strcmp (arg, MATH_LIBRARY) == 0))
4157 : 0 : save_switch ("-foffload-options=-l_GCC_m",
4158 : : 0, NULL, validated, true);
4159 : : }
4160 : 0 : break;
4161 : 0 : default:
4162 : 0 : gcc_unreachable ();
4163 : : }
4164 : 0 : }
4165 : :
4166 : : /* Handle a driver option; arguments and return value as for
4167 : : handle_option. */
4168 : :
4169 : : static bool
4170 : 2735432 : driver_handle_option (struct gcc_options *opts,
4171 : : struct gcc_options *opts_set,
4172 : : const struct cl_decoded_option *decoded,
4173 : : unsigned int lang_mask ATTRIBUTE_UNUSED, int kind,
4174 : : location_t loc,
4175 : : const struct cl_option_handlers *handlers ATTRIBUTE_UNUSED,
4176 : : diagnostics::context *dc,
4177 : : void (*) (void))
4178 : : {
4179 : 2735432 : size_t opt_index = decoded->opt_index;
4180 : 2735432 : const char *arg = decoded->arg;
4181 : 2735432 : const char *compare_debug_replacement_opt;
4182 : 2735432 : int value = decoded->value;
4183 : 2735432 : bool validated = false;
4184 : 2735432 : bool do_save = true;
4185 : :
4186 : 2735432 : gcc_assert (opts == &global_options);
4187 : 2735432 : gcc_assert (opts_set == &global_options_set);
4188 : 2735432 : gcc_assert (static_cast<diagnostics::kind> (kind)
4189 : : == diagnostics::kind::unspecified);
4190 : 2735432 : gcc_assert (loc == UNKNOWN_LOCATION);
4191 : 2735432 : gcc_assert (dc == global_dc);
4192 : :
4193 : 2735432 : switch (opt_index)
4194 : : {
4195 : 1 : case OPT_dumpspecs:
4196 : 1 : {
4197 : 1 : struct spec_list *sl;
4198 : 1 : init_spec ();
4199 : 47 : for (sl = specs; sl; sl = sl->next)
4200 : 46 : printf ("*%s:\n%s\n\n", sl->name, *(sl->ptr_spec));
4201 : 1 : if (link_command_spec)
4202 : 1 : printf ("*link_command:\n%s\n\n", link_command_spec);
4203 : 1 : exit (0);
4204 : : }
4205 : :
4206 : 280 : case OPT_dumpversion:
4207 : 280 : printf ("%s\n", spec_version);
4208 : 280 : exit (0);
4209 : :
4210 : 0 : case OPT_dumpmachine:
4211 : 0 : printf ("%s\n", spec_machine);
4212 : 0 : exit (0);
4213 : :
4214 : 0 : case OPT_dumpfullversion:
4215 : 0 : printf ("%s\n", BASEVER);
4216 : 0 : exit (0);
4217 : :
4218 : 78 : case OPT__version:
4219 : 78 : print_version = 1;
4220 : :
4221 : : /* CPP driver cannot obtain switch from cc1_options. */
4222 : 78 : if (is_cpp_driver)
4223 : 0 : add_preprocessor_option ("--version", strlen ("--version"));
4224 : 78 : add_assembler_option ("--version", strlen ("--version"));
4225 : 78 : add_linker_option ("--version", strlen ("--version"));
4226 : 78 : break;
4227 : :
4228 : 5 : case OPT__completion_:
4229 : 5 : validated = true;
4230 : 5 : completion = decoded->arg;
4231 : 5 : break;
4232 : :
4233 : 4 : case OPT__help:
4234 : 4 : print_help_list = 1;
4235 : :
4236 : : /* CPP driver cannot obtain switch from cc1_options. */
4237 : 4 : if (is_cpp_driver)
4238 : 0 : add_preprocessor_option ("--help", 6);
4239 : 4 : add_assembler_option ("--help", 6);
4240 : 4 : add_linker_option ("--help", 6);
4241 : 4 : break;
4242 : :
4243 : 100 : case OPT__help_:
4244 : 100 : print_subprocess_help = 2;
4245 : 100 : break;
4246 : :
4247 : 0 : case OPT__target_help:
4248 : 0 : print_subprocess_help = 1;
4249 : :
4250 : : /* CPP driver cannot obtain switch from cc1_options. */
4251 : 0 : if (is_cpp_driver)
4252 : 0 : add_preprocessor_option ("--target-help", 13);
4253 : 0 : add_assembler_option ("--target-help", 13);
4254 : 0 : add_linker_option ("--target-help", 13);
4255 : 0 : break;
4256 : :
4257 : : case OPT__no_sysroot_suffix:
4258 : : case OPT_pass_exit_codes:
4259 : : case OPT_print_search_dirs:
4260 : : case OPT_print_file_name_:
4261 : : case OPT_print_prog_name_:
4262 : : case OPT_print_multi_lib:
4263 : : case OPT_print_multi_directory:
4264 : : case OPT_print_sysroot:
4265 : : case OPT_print_multi_os_directory:
4266 : : case OPT_print_multiarch:
4267 : : case OPT_print_sysroot_headers_suffix:
4268 : : case OPT_time:
4269 : : case OPT_wrapper:
4270 : : /* These options set the variables specified in common.opt
4271 : : automatically, and do not need to be saved for spec
4272 : : processing. */
4273 : : do_save = false;
4274 : : break;
4275 : :
4276 : 392 : case OPT_print_libgcc_file_name:
4277 : 392 : print_file_name = "libgcc.a";
4278 : 392 : do_save = false;
4279 : 392 : break;
4280 : :
4281 : 0 : case OPT_fuse_ld_bfd:
4282 : 0 : use_ld = ".bfd";
4283 : 0 : break;
4284 : :
4285 : 0 : case OPT_fuse_ld_gold:
4286 : 0 : use_ld = ".gold";
4287 : 0 : break;
4288 : :
4289 : 0 : case OPT_fuse_ld_mold:
4290 : 0 : use_ld = ".mold";
4291 : 0 : break;
4292 : :
4293 : 0 : case OPT_fcompare_debug_second:
4294 : 0 : compare_debug_second = 1;
4295 : 0 : break;
4296 : :
4297 : 613 : case OPT_fcompare_debug:
4298 : 613 : switch (value)
4299 : : {
4300 : 0 : case 0:
4301 : 0 : compare_debug_replacement_opt = "-fcompare-debug=";
4302 : 0 : arg = "";
4303 : 0 : goto compare_debug_with_arg;
4304 : :
4305 : 613 : case 1:
4306 : 613 : compare_debug_replacement_opt = "-fcompare-debug=-gtoggle";
4307 : 613 : arg = "-gtoggle";
4308 : 613 : goto compare_debug_with_arg;
4309 : :
4310 : 0 : default:
4311 : 0 : gcc_unreachable ();
4312 : : }
4313 : 6 : break;
4314 : :
4315 : 6 : case OPT_fcompare_debug_:
4316 : 6 : compare_debug_replacement_opt = decoded->canonical_option[0];
4317 : 619 : compare_debug_with_arg:
4318 : 619 : gcc_assert (decoded->canonical_option_num_elements == 1);
4319 : 619 : gcc_assert (arg != NULL);
4320 : 619 : if (*arg)
4321 : 619 : compare_debug = 1;
4322 : : else
4323 : 0 : compare_debug = -1;
4324 : 619 : if (compare_debug < 0)
4325 : 0 : compare_debug_opt = NULL;
4326 : : else
4327 : 619 : compare_debug_opt = arg;
4328 : 619 : save_switch (compare_debug_replacement_opt, 0, NULL, validated, true);
4329 : 619 : set_source_date_epoch_envvar ();
4330 : 619 : return true;
4331 : :
4332 : 270780 : case OPT_fdiagnostics_color_:
4333 : 270780 : diagnostic_color_init (dc, value);
4334 : 270780 : break;
4335 : :
4336 : 265059 : case OPT_fdiagnostics_urls_:
4337 : 265059 : diagnostic_urls_init (dc, value);
4338 : 265059 : break;
4339 : :
4340 : 0 : case OPT_fdiagnostics_show_highlight_colors:
4341 : 0 : dc->set_show_highlight_colors (value);
4342 : 0 : break;
4343 : :
4344 : 0 : case OPT_fdiagnostics_format_:
4345 : 0 : {
4346 : 0 : const char *basename = (opts->x_dump_base_name ? opts->x_dump_base_name
4347 : : : opts->x_main_input_basename);
4348 : 0 : gcc_assert (dc);
4349 : 0 : diagnostics::output_format_init (*dc,
4350 : : opts->x_main_input_filename, basename,
4351 : : (enum diagnostics_output_format)value,
4352 : 0 : opts->x_flag_diagnostics_json_formatting);
4353 : 0 : break;
4354 : : }
4355 : :
4356 : 0 : case OPT_fdiagnostics_add_output_:
4357 : 0 : handle_OPT_fdiagnostics_add_output_ (*opts, *dc, arg, loc);
4358 : 0 : break;
4359 : :
4360 : 0 : case OPT_fdiagnostics_set_output_:
4361 : 0 : handle_OPT_fdiagnostics_set_output_ (*opts, *dc, arg, loc);
4362 : 0 : break;
4363 : :
4364 : 293778 : case OPT_fdiagnostics_text_art_charset_:
4365 : 293778 : dc->set_text_art_charset ((enum diagnostic_text_art_charset)value);
4366 : 293778 : break;
4367 : :
4368 : : case OPT_Wa_:
4369 : : {
4370 : : int prev, j;
4371 : : /* Pass the rest of this option to the assembler. */
4372 : :
4373 : : /* Split the argument at commas. */
4374 : : prev = 0;
4375 : 466 : for (j = 0; arg[j]; j++)
4376 : 430 : if (arg[j] == ',')
4377 : : {
4378 : 0 : add_assembler_option (arg + prev, j - prev);
4379 : 0 : prev = j + 1;
4380 : : }
4381 : :
4382 : : /* Record the part after the last comma. */
4383 : 36 : add_assembler_option (arg + prev, j - prev);
4384 : : }
4385 : 36 : do_save = false;
4386 : 36 : break;
4387 : :
4388 : : case OPT_Wp_:
4389 : : {
4390 : : int prev, j;
4391 : : /* Pass the rest of this option to the preprocessor. */
4392 : :
4393 : : /* Split the argument at commas. */
4394 : : prev = 0;
4395 : 0 : for (j = 0; arg[j]; j++)
4396 : 0 : if (arg[j] == ',')
4397 : : {
4398 : 0 : add_preprocessor_option (arg + prev, j - prev);
4399 : 0 : prev = j + 1;
4400 : : }
4401 : :
4402 : : /* Record the part after the last comma. */
4403 : 0 : add_preprocessor_option (arg + prev, j - prev);
4404 : : }
4405 : 0 : do_save = false;
4406 : 0 : break;
4407 : :
4408 : : case OPT_Wl_:
4409 : : {
4410 : : int prev, j;
4411 : : /* Split the argument at commas. */
4412 : : prev = 0;
4413 : 129320 : for (j = 0; arg[j]; j++)
4414 : 121531 : if (arg[j] == ',')
4415 : : {
4416 : 42 : add_infile (save_string (arg + prev, j - prev), "*");
4417 : 42 : prev = j + 1;
4418 : : }
4419 : : /* Record the part after the last comma. */
4420 : 7789 : add_infile (arg + prev, "*");
4421 : 7789 : if (strcmp (arg, "-z,lazy") == 0 || strcmp (arg, "-z,norelro") == 0)
4422 : 12 : avoid_linker_hardening_p = true;
4423 : : }
4424 : : do_save = false;
4425 : : break;
4426 : :
4427 : 12 : case OPT_z:
4428 : 12 : if (strcmp (arg, "lazy") == 0 || strcmp (arg, "norelro") == 0)
4429 : 12 : avoid_linker_hardening_p = true;
4430 : : break;
4431 : :
4432 : 0 : case OPT_Xlinker:
4433 : 0 : add_infile (arg, "*");
4434 : 0 : do_save = false;
4435 : 0 : break;
4436 : :
4437 : 0 : case OPT_Xpreprocessor:
4438 : 0 : add_preprocessor_option (arg, strlen (arg));
4439 : 0 : do_save = false;
4440 : 0 : break;
4441 : :
4442 : 65 : case OPT_Xassembler:
4443 : 65 : add_assembler_option (arg, strlen (arg));
4444 : 65 : do_save = false;
4445 : 65 : break;
4446 : :
4447 : 254425 : case OPT_l:
4448 : : /* POSIX allows separation of -l and the lib arg; canonicalize
4449 : : by concatenating -l with its arg */
4450 : 254425 : add_infile (concat ("-l", arg, NULL), "*");
4451 : :
4452 : : /* Forward to offloading compilation '-l[...]' flags for standard,
4453 : : well-known libraries. */
4454 : : /* Doing this processing here means that we don't get to see libraries
4455 : : injected via specs, such as '-lquadmath' injected via
4456 : : '[build]/[target]/libgfortran/libgfortran.spec'. However, this issue
4457 : : is not actually relevant for the current set of host/offloading
4458 : : configurations. */
4459 : 254425 : if (ENABLE_OFFLOADING)
4460 : : forward_offload_option (opt_index, arg, validated);
4461 : :
4462 : 254425 : do_save = false;
4463 : 254425 : break;
4464 : :
4465 : 265108 : case OPT_L:
4466 : : /* Similarly, canonicalize -L for linkers that may not accept
4467 : : separate arguments. */
4468 : 265108 : save_switch (concat ("-L", arg, NULL), 0, NULL, validated, true);
4469 : 265108 : return true;
4470 : :
4471 : 0 : case OPT_F:
4472 : : /* Likewise -F. */
4473 : 0 : save_switch (concat ("-F", arg, NULL), 0, NULL, validated, true);
4474 : 0 : return true;
4475 : :
4476 : 407 : case OPT_save_temps:
4477 : 407 : if (!save_temps_flag)
4478 : 401 : save_temps_flag = SAVE_TEMPS_DUMP;
4479 : : validated = true;
4480 : : break;
4481 : :
4482 : 58 : case OPT_save_temps_:
4483 : 58 : if (strcmp (arg, "cwd") == 0)
4484 : 29 : save_temps_flag = SAVE_TEMPS_CWD;
4485 : 29 : else if (strcmp (arg, "obj") == 0
4486 : 0 : || strcmp (arg, "object") == 0)
4487 : 29 : save_temps_flag = SAVE_TEMPS_OBJ;
4488 : : else
4489 : 0 : fatal_error (input_location, "%qs is an unknown %<-save-temps%> option",
4490 : 0 : decoded->orig_option_with_args_text);
4491 : 58 : save_temps_overrides_dumpdir = true;
4492 : 58 : break;
4493 : :
4494 : 22711 : case OPT_dumpdir:
4495 : 22711 : free (dumpdir);
4496 : 22711 : dumpdir = xstrdup (arg);
4497 : 22711 : save_temps_overrides_dumpdir = false;
4498 : 22711 : break;
4499 : :
4500 : 24137 : case OPT_dumpbase:
4501 : 24137 : free (dumpbase);
4502 : 24137 : dumpbase = xstrdup (arg);
4503 : 24137 : break;
4504 : :
4505 : 252 : case OPT_dumpbase_ext:
4506 : 252 : free (dumpbase_ext);
4507 : 252 : dumpbase_ext = xstrdup (arg);
4508 : 252 : break;
4509 : :
4510 : : case OPT_no_canonical_prefixes:
4511 : : /* Already handled as a special case, so ignored here. */
4512 : : do_save = false;
4513 : : break;
4514 : :
4515 : : case OPT_pipe:
4516 : : validated = true;
4517 : : /* These options set the variables specified in common.opt
4518 : : automatically, but do need to be saved for spec
4519 : : processing. */
4520 : : break;
4521 : :
4522 : 3 : case OPT_specs_:
4523 : 3 : {
4524 : 3 : struct user_specs *user = XNEW (struct user_specs);
4525 : :
4526 : 3 : user->next = (struct user_specs *) 0;
4527 : 3 : user->filename = arg;
4528 : 3 : if (user_specs_tail)
4529 : 0 : user_specs_tail->next = user;
4530 : : else
4531 : 3 : user_specs_head = user;
4532 : 3 : user_specs_tail = user;
4533 : : }
4534 : 3 : validated = true;
4535 : 3 : break;
4536 : :
4537 : 0 : case OPT__sysroot_:
4538 : 0 : target_system_root = arg;
4539 : 0 : target_system_root_changed = 1;
4540 : : /* Saving this option is useful to let self-specs decide to
4541 : : provide a default one. */
4542 : 0 : do_save = true;
4543 : 0 : validated = true;
4544 : 0 : break;
4545 : :
4546 : 0 : case OPT_time_:
4547 : 0 : if (report_times_to_file)
4548 : 0 : fclose (report_times_to_file);
4549 : 0 : report_times_to_file = fopen (arg, "a");
4550 : 0 : do_save = false;
4551 : 0 : break;
4552 : :
4553 : 9209 : case OPT_truncate:
4554 : 9209 : totruncate_file = arg;
4555 : 9209 : do_save = false;
4556 : 9209 : break;
4557 : :
4558 : 636 : case OPT____:
4559 : : /* "-###"
4560 : : This is similar to -v except that there is no execution
4561 : : of the commands and the echoed arguments are quoted. It
4562 : : is intended for use in shell scripts to capture the
4563 : : driver-generated command line. */
4564 : 636 : verbose_only_flag++;
4565 : 636 : verbose_flag = 1;
4566 : 636 : do_save = false;
4567 : 636 : break;
4568 : :
4569 : 493224 : case OPT_B:
4570 : 493224 : {
4571 : 493224 : size_t len = strlen (arg);
4572 : :
4573 : : /* Catch the case where the user has forgotten to append a
4574 : : directory separator to the path. Note, they may be using
4575 : : -B to add an executable name prefix, eg "i386-elf-", in
4576 : : order to distinguish between multiple installations of
4577 : : GCC in the same directory. Hence we must check to see
4578 : : if appending a directory separator actually makes a
4579 : : valid directory name. */
4580 : 493224 : if (!IS_DIR_SEPARATOR (arg[len - 1])
4581 : 493224 : && is_directory (arg))
4582 : : {
4583 : 97500 : char *tmp = XNEWVEC (char, len + 2);
4584 : 97500 : strcpy (tmp, arg);
4585 : 97500 : tmp[len] = DIR_SEPARATOR;
4586 : 97500 : tmp[++len] = 0;
4587 : 97500 : arg = tmp;
4588 : : }
4589 : :
4590 : 493224 : add_prefix (&exec_prefixes, arg, NULL,
4591 : : PREFIX_PRIORITY_B_OPT, 0, 0);
4592 : 493224 : add_prefix (&startfile_prefixes, arg, NULL,
4593 : : PREFIX_PRIORITY_B_OPT, 0, 0);
4594 : 493224 : add_prefix (&include_prefixes, arg, NULL,
4595 : : PREFIX_PRIORITY_B_OPT, 0, 0);
4596 : : }
4597 : 493224 : validated = true;
4598 : 493224 : break;
4599 : :
4600 : 2493 : case OPT_E:
4601 : 2493 : have_E = true;
4602 : 2493 : break;
4603 : :
4604 : 50949 : case OPT_x:
4605 : 50949 : spec_lang = arg;
4606 : 50949 : if (!strcmp (spec_lang, "none"))
4607 : : /* Suppress the warning if -xnone comes after the last input
4608 : : file, because alternate command interfaces like g++ might
4609 : : find it useful to place -xnone after each input file. */
4610 : 13357 : spec_lang = 0;
4611 : : else
4612 : 37592 : last_language_n_infiles = n_infiles;
4613 : : do_save = false;
4614 : : break;
4615 : :
4616 : 272304 : case OPT_o:
4617 : 272304 : have_o = 1;
4618 : : #if defined(HAVE_TARGET_EXECUTABLE_SUFFIX) || defined(HAVE_TARGET_OBJECT_SUFFIX)
4619 : : arg = convert_filename (arg, ! have_c, 0);
4620 : : #endif
4621 : 272304 : output_file = arg;
4622 : : /* On some systems, ld cannot handle "-o" without a space. So
4623 : : split the option from its argument. */
4624 : 272304 : save_switch ("-o", 1, &arg, validated, true);
4625 : 272304 : return true;
4626 : :
4627 : 2090 : case OPT_pie:
4628 : : #ifdef ENABLE_DEFAULT_PIE
4629 : : /* -pie is turned on by default. */
4630 : : validated = true;
4631 : : #endif
4632 : : /* FALLTHROUGH */
4633 : 2090 : case OPT_r:
4634 : 2090 : case OPT_shared:
4635 : 2090 : case OPT_no_pie:
4636 : 2090 : avoid_linker_hardening_p = true;
4637 : 2090 : break;
4638 : :
4639 : 102 : case OPT_static:
4640 : 102 : static_p = true;
4641 : 102 : break;
4642 : :
4643 : : case OPT_static_libgcc:
4644 : : case OPT_shared_libgcc:
4645 : : case OPT_static_libgfortran:
4646 : : case OPT_static_libquadmath:
4647 : : case OPT_static_libphobos:
4648 : : case OPT_static_libgm2:
4649 : : case OPT_static_libstdc__:
4650 : : /* These are always valid; gcc.cc itself understands the first two
4651 : : gfortranspec.cc understands -static-libgfortran,
4652 : : libgfortran.spec handles -static-libquadmath,
4653 : : d-spec.cc understands -static-libphobos,
4654 : : gm2spec.cc understands -static-libgm2,
4655 : : and g++spec.cc understands -static-libstdc++. */
4656 : : validated = true;
4657 : : break;
4658 : :
4659 : 78 : case OPT_fwpa:
4660 : 78 : flag_wpa = "";
4661 : 78 : break;
4662 : :
4663 : 27 : case OPT_foffload_options_:
4664 : 27 : check_foffload_target_names (arg);
4665 : 27 : break;
4666 : :
4667 : 2869 : case OPT_foffload_:
4668 : 2869 : handle_foffload_option (arg);
4669 : 2869 : if (arg[0] == '-' || NULL != strchr (arg, '='))
4670 : 839 : save_switch (concat ("-foffload-options=", arg, NULL),
4671 : : 0, NULL, validated, true);
4672 : : do_save = false;
4673 : : break;
4674 : :
4675 : 0 : case OPT_gcodeview:
4676 : 0 : add_infile ("--pdb=", "*");
4677 : 0 : break;
4678 : :
4679 : : default:
4680 : : /* Various driver options need no special processing at this
4681 : : point, having been handled in a prescan above or being
4682 : : handled by specs. */
4683 : : break;
4684 : : }
4685 : :
4686 : 1639754 : if (do_save)
4687 : 1856898 : save_switch (decoded->canonical_option[0],
4688 : 1856898 : decoded->canonical_option_num_elements - 1,
4689 : : &decoded->canonical_option[1], validated, true);
4690 : : return true;
4691 : : }
4692 : :
4693 : : /* Return true if F2 is F1 followed by a single suffix, i.e., by a
4694 : : period and additional characters other than a period. */
4695 : :
4696 : : static inline bool
4697 : 85181 : adds_single_suffix_p (const char *f2, const char *f1)
4698 : : {
4699 : 85181 : size_t len = strlen (f1);
4700 : :
4701 : 85181 : return (strncmp (f1, f2, len) == 0
4702 : 76987 : && f2[len] == '.'
4703 : 161703 : && strchr (f2 + len + 1, '.') == NULL);
4704 : : }
4705 : :
4706 : : /* Put the driver's standard set of option handlers in *HANDLERS. */
4707 : :
4708 : : static void
4709 : 866897 : set_option_handlers (struct cl_option_handlers *handlers)
4710 : : {
4711 : 866897 : handlers->unknown_option_callback = driver_unknown_option_callback;
4712 : 866897 : handlers->wrong_lang_callback = driver_wrong_lang_callback;
4713 : 866897 : handlers->num_handlers = 3;
4714 : 866897 : handlers->handlers[0].handler = driver_handle_option;
4715 : 866897 : handlers->handlers[0].mask = CL_DRIVER;
4716 : 866897 : handlers->handlers[1].handler = common_handle_option;
4717 : 866897 : handlers->handlers[1].mask = CL_COMMON;
4718 : 866897 : handlers->handlers[2].handler = target_handle_option;
4719 : 866897 : handlers->handlers[2].mask = CL_TARGET;
4720 : 0 : }
4721 : :
4722 : :
4723 : : /* Return the index into infiles for the single non-library
4724 : : non-lto-wpa input file, -1 if there isn't any, or -2 if there is
4725 : : more than one. */
4726 : : static inline int
4727 : 154845 : single_input_file_index ()
4728 : : {
4729 : 154845 : int ret = -1;
4730 : :
4731 : 497325 : for (int i = 0; i < n_infiles; i++)
4732 : : {
4733 : 354961 : if (infiles[i].language
4734 : 248591 : && (infiles[i].language[0] == '*'
4735 : 50009 : || (flag_wpa
4736 : 19280 : && strcmp (infiles[i].language, "lto") == 0)))
4737 : 217862 : continue;
4738 : :
4739 : 137099 : if (ret != -1)
4740 : : return -2;
4741 : :
4742 : : ret = i;
4743 : : }
4744 : :
4745 : : return ret;
4746 : : }
4747 : :
4748 : : /* Create the vector `switches' and its contents.
4749 : : Store its length in `n_switches'. */
4750 : :
4751 : : static void
4752 : 300399 : process_command (unsigned int decoded_options_count,
4753 : : struct cl_decoded_option *decoded_options)
4754 : : {
4755 : 300399 : const char *temp;
4756 : 300399 : char *temp1;
4757 : 300399 : char *tooldir_prefix, *tooldir_prefix2;
4758 : 300399 : char *(*get_relative_prefix) (const char *, const char *,
4759 : : const char *) = NULL;
4760 : 300399 : struct cl_option_handlers handlers;
4761 : 300399 : unsigned int j;
4762 : :
4763 : 300399 : gcc_exec_prefix = env.get ("GCC_EXEC_PREFIX");
4764 : :
4765 : 300399 : n_switches = 0;
4766 : 300399 : n_infiles = 0;
4767 : 300399 : added_libraries = 0;
4768 : :
4769 : : /* Figure compiler version from version string. */
4770 : :
4771 : 300399 : compiler_version = temp1 = xstrdup (version_string);
4772 : :
4773 : 2102793 : for (; *temp1; ++temp1)
4774 : : {
4775 : 2102793 : if (*temp1 == ' ')
4776 : : {
4777 : 300399 : *temp1 = '\0';
4778 : 300399 : break;
4779 : : }
4780 : : }
4781 : :
4782 : : /* Handle any -no-canonical-prefixes flag early, to assign the function
4783 : : that builds relative prefixes. This function creates default search
4784 : : paths that are needed later in normal option handling. */
4785 : :
4786 : 6771220 : for (j = 1; j < decoded_options_count; j++)
4787 : : {
4788 : 6470821 : if (decoded_options[j].opt_index == OPT_no_canonical_prefixes)
4789 : : {
4790 : : get_relative_prefix = make_relative_prefix_ignore_links;
4791 : : break;
4792 : : }
4793 : : }
4794 : 300399 : if (! get_relative_prefix)
4795 : 300399 : get_relative_prefix = make_relative_prefix;
4796 : :
4797 : : /* Set up the default search paths. If there is no GCC_EXEC_PREFIX,
4798 : : see if we can create it from the pathname specified in
4799 : : decoded_options[0].arg. */
4800 : :
4801 : 300399 : gcc_libexec_prefix = standard_libexec_prefix;
4802 : : #ifndef VMS
4803 : : /* FIXME: make_relative_prefix doesn't yet work for VMS. */
4804 : 300399 : if (!gcc_exec_prefix)
4805 : : {
4806 : 28896 : gcc_exec_prefix = get_relative_prefix (decoded_options[0].arg,
4807 : : standard_bindir_prefix,
4808 : : standard_exec_prefix);
4809 : 28896 : gcc_libexec_prefix = get_relative_prefix (decoded_options[0].arg,
4810 : : standard_bindir_prefix,
4811 : : standard_libexec_prefix);
4812 : 28896 : if (gcc_exec_prefix)
4813 : 28896 : xputenv (concat ("GCC_EXEC_PREFIX=", gcc_exec_prefix, NULL));
4814 : : }
4815 : : else
4816 : : {
4817 : : /* make_relative_prefix requires a program name, but
4818 : : GCC_EXEC_PREFIX is typically a directory name with a trailing
4819 : : / (which is ignored by make_relative_prefix), so append a
4820 : : program name. */
4821 : 271503 : char *tmp_prefix = concat (gcc_exec_prefix, "gcc", NULL);
4822 : 271503 : gcc_libexec_prefix = get_relative_prefix (tmp_prefix,
4823 : : standard_exec_prefix,
4824 : : standard_libexec_prefix);
4825 : :
4826 : : /* The path is unrelocated, so fallback to the original setting. */
4827 : 271503 : if (!gcc_libexec_prefix)
4828 : 271158 : gcc_libexec_prefix = standard_libexec_prefix;
4829 : :
4830 : 271503 : free (tmp_prefix);
4831 : : }
4832 : : #else
4833 : : #endif
4834 : : /* From this point onward, gcc_exec_prefix is non-null if the toolchain
4835 : : is relocated. The toolchain was either relocated using GCC_EXEC_PREFIX
4836 : : or an automatically created GCC_EXEC_PREFIX from
4837 : : decoded_options[0].arg. */
4838 : :
4839 : : /* Do language-specific adjustment/addition of flags. */
4840 : 300399 : lang_specific_driver (&decoded_options, &decoded_options_count,
4841 : : &added_libraries);
4842 : :
4843 : 300395 : if (gcc_exec_prefix)
4844 : : {
4845 : 300395 : int len = strlen (gcc_exec_prefix);
4846 : :
4847 : 300395 : if (len > (int) sizeof ("/lib/gcc/") - 1
4848 : 300395 : && (IS_DIR_SEPARATOR (gcc_exec_prefix[len-1])))
4849 : : {
4850 : 300395 : temp = gcc_exec_prefix + len - sizeof ("/lib/gcc/") + 1;
4851 : 300395 : if (IS_DIR_SEPARATOR (*temp)
4852 : 300395 : && filename_ncmp (temp + 1, "lib", 3) == 0
4853 : 300395 : && IS_DIR_SEPARATOR (temp[4])
4854 : 600790 : && filename_ncmp (temp + 5, "gcc", 3) == 0)
4855 : 300395 : len -= sizeof ("/lib/gcc/") - 1;
4856 : : }
4857 : :
4858 : 300395 : set_std_prefix (gcc_exec_prefix, len);
4859 : 300395 : add_prefix (&exec_prefixes, gcc_libexec_prefix, "GCC",
4860 : : PREFIX_PRIORITY_LAST, 0, 0);
4861 : 300395 : add_prefix (&startfile_prefixes, gcc_exec_prefix, "GCC",
4862 : : PREFIX_PRIORITY_LAST, 0, 0);
4863 : : }
4864 : :
4865 : : /* COMPILER_PATH and LIBRARY_PATH have values
4866 : : that are lists of directory names with colons. */
4867 : :
4868 : 300395 : temp = env.get ("COMPILER_PATH");
4869 : 300395 : if (temp)
4870 : : {
4871 : 22547 : const char *startp, *endp;
4872 : 22547 : char *nstore = (char *) alloca (strlen (temp) + 3);
4873 : :
4874 : 22547 : startp = endp = temp;
4875 : 2070524 : while (1)
4876 : : {
4877 : 2070524 : if (*endp == PATH_SEPARATOR || *endp == 0)
4878 : : {
4879 : 36775 : strncpy (nstore, startp, endp - startp);
4880 : 36775 : if (endp == startp)
4881 : 0 : strcpy (nstore, concat (".", dir_separator_str, NULL));
4882 : 36775 : else if (!IS_DIR_SEPARATOR (endp[-1]))
4883 : : {
4884 : 0 : nstore[endp - startp] = DIR_SEPARATOR;
4885 : 0 : nstore[endp - startp + 1] = 0;
4886 : : }
4887 : : else
4888 : 36775 : nstore[endp - startp] = 0;
4889 : 36775 : add_prefix (&exec_prefixes, nstore, 0,
4890 : : PREFIX_PRIORITY_LAST, 0, 0);
4891 : 36775 : add_prefix (&include_prefixes, nstore, 0,
4892 : : PREFIX_PRIORITY_LAST, 0, 0);
4893 : 36775 : if (*endp == 0)
4894 : : break;
4895 : 14228 : endp = startp = endp + 1;
4896 : : }
4897 : : else
4898 : 2033749 : endp++;
4899 : : }
4900 : : }
4901 : :
4902 : 300395 : temp = env.get (LIBRARY_PATH_ENV);
4903 : 300395 : if (temp && *cross_compile == '0')
4904 : : {
4905 : 23875 : const char *startp, *endp;
4906 : 23875 : char *nstore = (char *) alloca (strlen (temp) + 3);
4907 : :
4908 : 23875 : startp = endp = temp;
4909 : 4166080 : while (1)
4910 : : {
4911 : 4166080 : if (*endp == PATH_SEPARATOR || *endp == 0)
4912 : : {
4913 : 172695 : strncpy (nstore, startp, endp - startp);
4914 : 172695 : if (endp == startp)
4915 : 0 : strcpy (nstore, concat (".", dir_separator_str, NULL));
4916 : 172695 : else if (!IS_DIR_SEPARATOR (endp[-1]))
4917 : : {
4918 : 1328 : nstore[endp - startp] = DIR_SEPARATOR;
4919 : 1328 : nstore[endp - startp + 1] = 0;
4920 : : }
4921 : : else
4922 : 171367 : nstore[endp - startp] = 0;
4923 : 172695 : add_prefix (&startfile_prefixes, nstore, NULL,
4924 : : PREFIX_PRIORITY_LAST, 0, 1);
4925 : 172695 : if (*endp == 0)
4926 : : break;
4927 : 148820 : endp = startp = endp + 1;
4928 : : }
4929 : : else
4930 : 3993385 : endp++;
4931 : : }
4932 : : }
4933 : :
4934 : : /* Use LPATH like LIBRARY_PATH (for the CMU build program). */
4935 : 300395 : temp = env.get ("LPATH");
4936 : 300395 : if (temp && *cross_compile == '0')
4937 : : {
4938 : 0 : const char *startp, *endp;
4939 : 0 : char *nstore = (char *) alloca (strlen (temp) + 3);
4940 : :
4941 : 0 : startp = endp = temp;
4942 : 0 : while (1)
4943 : : {
4944 : 0 : if (*endp == PATH_SEPARATOR || *endp == 0)
4945 : : {
4946 : 0 : strncpy (nstore, startp, endp - startp);
4947 : 0 : if (endp == startp)
4948 : 0 : strcpy (nstore, concat (".", dir_separator_str, NULL));
4949 : 0 : else if (!IS_DIR_SEPARATOR (endp[-1]))
4950 : : {
4951 : 0 : nstore[endp - startp] = DIR_SEPARATOR;
4952 : 0 : nstore[endp - startp + 1] = 0;
4953 : : }
4954 : : else
4955 : 0 : nstore[endp - startp] = 0;
4956 : 0 : add_prefix (&startfile_prefixes, nstore, NULL,
4957 : : PREFIX_PRIORITY_LAST, 0, 1);
4958 : 0 : if (*endp == 0)
4959 : : break;
4960 : 0 : endp = startp = endp + 1;
4961 : : }
4962 : : else
4963 : 0 : endp++;
4964 : : }
4965 : : }
4966 : :
4967 : : /* Process the options and store input files and switches in their
4968 : : vectors. */
4969 : :
4970 : 300395 : last_language_n_infiles = -1;
4971 : :
4972 : 300395 : set_option_handlers (&handlers);
4973 : :
4974 : 5802913 : for (j = 1; j < decoded_options_count; j++)
4975 : : {
4976 : 5691339 : switch (decoded_options[j].opt_index)
4977 : : {
4978 : 188821 : case OPT_S:
4979 : 188821 : case OPT_c:
4980 : 188821 : case OPT_E:
4981 : 188821 : have_c = 1;
4982 : 188821 : break;
4983 : : }
4984 : 5691339 : if (have_c)
4985 : : break;
4986 : : }
4987 : :
4988 : 7145375 : for (j = 1; j < decoded_options_count; j++)
4989 : : {
4990 : 6845261 : if (decoded_options[j].opt_index == OPT_SPECIAL_input_file)
4991 : : {
4992 : 322685 : const char *arg = decoded_options[j].arg;
4993 : :
4994 : : #ifdef HAVE_TARGET_OBJECT_SUFFIX
4995 : : arg = convert_filename (arg, 0, access (arg, F_OK));
4996 : : #endif
4997 : 322685 : add_infile (arg, spec_lang);
4998 : :
4999 : 322685 : continue;
5000 : 322685 : }
5001 : :
5002 : 6522576 : read_cmdline_option (&global_options, &global_options_set,
5003 : : decoded_options + j, UNKNOWN_LOCATION,
5004 : : CL_DRIVER, &handlers, global_dc);
5005 : : }
5006 : :
5007 : : /* If the user didn't specify any, default to all configured offload
5008 : : targets. */
5009 : 300114 : if (ENABLE_OFFLOADING && offload_targets == NULL)
5010 : : {
5011 : : handle_foffload_option (OFFLOAD_TARGETS);
5012 : : #if OFFLOAD_DEFAULTED
5013 : : offload_targets_default = true;
5014 : : #endif
5015 : : }
5016 : :
5017 : : /* TODO: check if -static -pie works and maybe use it. */
5018 : 300114 : if (flag_hardened)
5019 : : {
5020 : 91 : if (!avoid_linker_hardening_p && !static_p)
5021 : : {
5022 : : #if defined HAVE_LD_PIE && defined LD_PIE_SPEC
5023 : 67 : save_switch (LD_PIE_SPEC, 0, NULL, /*validated=*/true, /*known=*/false);
5024 : : #endif
5025 : : /* These are passed straight down to collect2 so we have to break
5026 : : it up like this. */
5027 : 67 : if (HAVE_LD_NOW_SUPPORT)
5028 : : {
5029 : 67 : add_infile ("-z", "*");
5030 : 67 : add_infile ("now", "*");
5031 : : }
5032 : 67 : if (HAVE_LD_RELRO_SUPPORT)
5033 : : {
5034 : 67 : add_infile ("-z", "*");
5035 : 67 : add_infile ("relro", "*");
5036 : : }
5037 : : }
5038 : : /* We can't use OPT_Whardened yet. Sigh. */
5039 : : else
5040 : 24 : warning_at (UNKNOWN_LOCATION, 0,
5041 : : "linker hardening options not enabled by %<-fhardened%> "
5042 : : "because other link options were specified on the command "
5043 : : "line");
5044 : : }
5045 : :
5046 : : /* Handle -gtoggle as it would later in toplev.cc:process_options to
5047 : : make the debug-level-gt spec function work as expected. */
5048 : 300114 : if (flag_gtoggle)
5049 : : {
5050 : 4 : if (debug_info_level == DINFO_LEVEL_NONE)
5051 : 0 : debug_info_level = DINFO_LEVEL_NORMAL;
5052 : : else
5053 : 4 : debug_info_level = DINFO_LEVEL_NONE;
5054 : : }
5055 : :
5056 : 300114 : if (output_file
5057 : 272303 : && strcmp (output_file, "-") != 0
5058 : 272140 : && strcmp (output_file, HOST_BIT_BUCKET) != 0)
5059 : : {
5060 : : int i;
5061 : 807619 : for (i = 0; i < n_infiles; i++)
5062 : 260740 : if ((!infiles[i].language || infiles[i].language[0] != '*')
5063 : 565007 : && canonical_filename_eq (infiles[i].name, output_file))
5064 : 1 : fatal_error (input_location,
5065 : : "input file %qs is the same as output file",
5066 : : output_file);
5067 : : }
5068 : :
5069 : 300113 : if (output_file != NULL && output_file[0] == '\0')
5070 : 0 : fatal_error (input_location, "output filename may not be empty");
5071 : :
5072 : : /* -dumpdir and -save-temps=* both specify the location of aux/dump
5073 : : outputs; the one that appears last prevails. When compiling
5074 : : multiple sources, an explicit dumpbase (minus -ext) may be
5075 : : combined with an explicit or implicit dumpdir, whereas when
5076 : : linking, a specified or implied link output name (minus
5077 : : extension) may be combined with a prevailing -save-temps=* or an
5078 : : otherwise implied dumpdir, but not override a prevailing
5079 : : -dumpdir. Primary outputs (e.g., linker output when linking
5080 : : without -o, or .i, .s or .o outputs when processing multiple
5081 : : inputs with -E, -S or -c, respectively) are NOT affected by these
5082 : : -save-temps=/-dump* options, always landing in the current
5083 : : directory and with the same basename as the input when an output
5084 : : name is not given, but when they're intermediate outputs, they
5085 : : are named like other aux outputs, so the options affect their
5086 : : location and name.
5087 : :
5088 : : Here are some examples. There are several more in the
5089 : : documentation of -o and -dump*, and some quite exhaustive tests
5090 : : in gcc.misc-tests/outputs.exp.
5091 : :
5092 : : When compiling any number of sources, no -dump* nor
5093 : : -save-temps=*, all outputs in cwd without prefix:
5094 : :
5095 : : # gcc -c b.c -gsplit-dwarf
5096 : : -> cc1 [-dumpdir ./] -dumpbase b.c -dumpbase-ext .c # b.o b.dwo
5097 : :
5098 : : # gcc -c b.c d.c -gsplit-dwarf
5099 : : -> cc1 [-dumpdir ./] -dumpbase b.c -dumpbase-ext .c # b.o b.dwo
5100 : : && cc1 [-dumpdir ./] -dumpbase d.c -dumpbase-ext .c # d.o d.dwo
5101 : :
5102 : : When compiling and linking, no -dump* nor -save-temps=*, .o
5103 : : outputs are temporary, aux outputs land in the dir of the output,
5104 : : prefixed with the basename of the linker output:
5105 : :
5106 : : # gcc b.c d.c -o ab -gsplit-dwarf
5107 : : -> cc1 -dumpdir ab- -dumpbase b.c -dumpbase-ext .c # ab-b.dwo
5108 : : && cc1 -dumpdir ab- -dumpbase d.c -dumpbase-ext .c # ab-d.dwo
5109 : : && link ... -o ab
5110 : :
5111 : : # gcc b.c d.c [-o a.out] -gsplit-dwarf
5112 : : -> cc1 -dumpdir a- -dumpbase b.c -dumpbase-ext .c # a-b.dwo
5113 : : && cc1 -dumpdir a- -dumpbase d.c -dumpbase-ext .c # a-d.dwo
5114 : : && link ... [-o a.out]
5115 : :
5116 : : When compiling and linking, a prevailing -dumpdir fully overrides
5117 : : the prefix of aux outputs given by the output name:
5118 : :
5119 : : # gcc -dumpdir f b.c d.c -gsplit-dwarf [-o [dir/]whatever]
5120 : : -> cc1 -dumpdir f -dumpbase b.c -dumpbase-ext .c # fb.dwo
5121 : : && cc1 -dumpdir f -dumpbase d.c -dumpbase-ext .c # fd.dwo
5122 : : && link ... [-o whatever]
5123 : :
5124 : : When compiling multiple inputs, an explicit -dumpbase is combined
5125 : : with -dumpdir, affecting aux outputs, but not the .o outputs:
5126 : :
5127 : : # gcc -dumpdir f -dumpbase g- b.c d.c -gsplit-dwarf -c
5128 : : -> cc1 -dumpdir fg- -dumpbase b.c -dumpbase-ext .c # b.o fg-b.dwo
5129 : : && cc1 -dumpdir fg- -dumpbase d.c -dumpbase-ext .c # d.o fg-d.dwo
5130 : :
5131 : : When compiling and linking with -save-temps, the .o outputs that
5132 : : would have been temporary become aux outputs, so they get
5133 : : affected by -dump* flags:
5134 : :
5135 : : # gcc -dumpdir f -dumpbase g- -save-temps b.c d.c
5136 : : -> cc1 -dumpdir fg- -dumpbase b.c -dumpbase-ext .c # fg-b.o
5137 : : && cc1 -dumpdir fg- -dumpbase d.c -dumpbase-ext .c # fg-d.o
5138 : : && link
5139 : :
5140 : : If -save-temps=* prevails over -dumpdir, however, the explicit
5141 : : -dumpdir is discarded, as if it wasn't there. The basename of
5142 : : the implicit linker output, a.out or a.exe, becomes a- as the aux
5143 : : output prefix for all compilations:
5144 : :
5145 : : # gcc [-dumpdir f] -save-temps=cwd b.c d.c
5146 : : -> cc1 -dumpdir a- -dumpbase b.c -dumpbase-ext .c # a-b.o
5147 : : && cc1 -dumpdir a- -dumpbase d.c -dumpbase-ext .c # a-d.o
5148 : : && link
5149 : :
5150 : : A single -dumpbase, applying to multiple inputs, overrides the
5151 : : linker output name, implied or explicit, as the aux output prefix:
5152 : :
5153 : : # gcc [-dumpdir f] -dumpbase g- -save-temps=cwd b.c d.c
5154 : : -> cc1 -dumpdir g- -dumpbase b.c -dumpbase-ext .c # g-b.o
5155 : : && cc1 -dumpdir g- -dumpbase d.c -dumpbase-ext .c # g-d.o
5156 : : && link
5157 : :
5158 : : # gcc [-dumpdir f] -dumpbase g- -save-temps=cwd b.c d.c -o dir/h.out
5159 : : -> cc1 -dumpdir g- -dumpbase b.c -dumpbase-ext .c # g-b.o
5160 : : && cc1 -dumpdir g- -dumpbase d.c -dumpbase-ext .c # g-d.o
5161 : : && link -o dir/h.out
5162 : :
5163 : : Now, if the linker output is NOT overridden as a prefix, but
5164 : : -save-temps=* overrides implicit or explicit -dumpdir, the
5165 : : effective dump dir combines the dir selected by the -save-temps=*
5166 : : option with the basename of the specified or implied link output:
5167 : :
5168 : : # gcc [-dumpdir f] -save-temps=cwd b.c d.c -o dir/h.out
5169 : : -> cc1 -dumpdir h- -dumpbase b.c -dumpbase-ext .c # h-b.o
5170 : : && cc1 -dumpdir h- -dumpbase d.c -dumpbase-ext .c # h-d.o
5171 : : && link -o dir/h.out
5172 : :
5173 : : # gcc [-dumpdir f] -save-temps=obj b.c d.c -o dir/h.out
5174 : : -> cc1 -dumpdir dir/h- -dumpbase b.c -dumpbase-ext .c # dir/h-b.o
5175 : : && cc1 -dumpdir dir/h- -dumpbase d.c -dumpbase-ext .c # dir/h-d.o
5176 : : && link -o dir/h.out
5177 : :
5178 : : But then again, a single -dumpbase applying to multiple inputs
5179 : : gets used instead of the linker output basename in the combined
5180 : : dumpdir:
5181 : :
5182 : : # gcc [-dumpdir f] -dumpbase g- -save-temps=obj b.c d.c -o dir/h.out
5183 : : -> cc1 -dumpdir dir/g- -dumpbase b.c -dumpbase-ext .c # dir/g-b.o
5184 : : && cc1 -dumpdir dir/g- -dumpbase d.c -dumpbase-ext .c # dir/g-d.o
5185 : : && link -o dir/h.out
5186 : :
5187 : : With a single input being compiled, the output basename does NOT
5188 : : affect the dumpdir prefix.
5189 : :
5190 : : # gcc -save-temps=obj b.c -gsplit-dwarf -c -o dir/b.o
5191 : : -> cc1 -dumpdir dir/ -dumpbase b.c -dumpbase-ext .c # dir/b.o dir/b.dwo
5192 : :
5193 : : but when compiling and linking even a single file, it does:
5194 : :
5195 : : # gcc -save-temps=obj b.c -o dir/h.out
5196 : : -> cc1 -dumpdir dir/h- -dumpbase b.c -dumpbase-ext .c # dir/h-b.o
5197 : :
5198 : : unless an explicit -dumpdir prevails:
5199 : :
5200 : : # gcc -save-temps[=obj] -dumpdir g- b.c -o dir/h.out
5201 : : -> cc1 -dumpdir g- -dumpbase b.c -dumpbase-ext .c # g-b.o
5202 : :
5203 : : */
5204 : :
5205 : 300113 : bool explicit_dumpdir = dumpdir;
5206 : :
5207 : 300061 : if ((!save_temps_overrides_dumpdir && explicit_dumpdir)
5208 : 577471 : || (output_file && not_actual_file_p (output_file)))
5209 : : {
5210 : : /* Do nothing. */
5211 : : }
5212 : :
5213 : : /* If -save-temps=obj and -o name, create the prefix to use for %b.
5214 : : Otherwise just make -save-temps=obj the same as -save-temps=cwd. */
5215 : 275844 : else if (save_temps_flag != SAVE_TEMPS_CWD && output_file != NULL)
5216 : : {
5217 : 256747 : free (dumpdir);
5218 : 256747 : dumpdir = NULL;
5219 : 256747 : temp = lbasename (output_file);
5220 : 256747 : if (temp != output_file)
5221 : 102960 : dumpdir = xstrndup (output_file,
5222 : 102960 : strlen (output_file) - strlen (temp));
5223 : : }
5224 : 19097 : else if (dumpdir)
5225 : : {
5226 : 5 : free (dumpdir);
5227 : 5 : dumpdir = NULL;
5228 : : }
5229 : :
5230 : 300113 : if (save_temps_flag)
5231 : 459 : save_temps_flag = SAVE_TEMPS_DUMP;
5232 : :
5233 : : /* If there is any pathname component in an explicit -dumpbase, it
5234 : : overrides dumpdir entirely, so discard it right away. Although
5235 : : the presence of an explicit -dumpdir matters for the driver, it
5236 : : shouldn't matter for other processes, that get all that's needed
5237 : : from the -dumpdir and -dumpbase always passed to them. */
5238 : 300113 : if (dumpdir && dumpbase && lbasename (dumpbase) != dumpbase)
5239 : : {
5240 : 22616 : free (dumpdir);
5241 : 22616 : dumpdir = NULL;
5242 : : }
5243 : :
5244 : : /* Check that dumpbase_ext matches the end of dumpbase, drop it
5245 : : otherwise. */
5246 : 300113 : if (dumpbase_ext && dumpbase && *dumpbase)
5247 : : {
5248 : 20 : int lendb = strlen (dumpbase);
5249 : 20 : int lendbx = strlen (dumpbase_ext);
5250 : :
5251 : : /* -dumpbase-ext must be a suffix proper; discard it if it
5252 : : matches all of -dumpbase, as that would make for an empty
5253 : : basename. */
5254 : 20 : if (lendbx >= lendb
5255 : 19 : || strcmp (dumpbase + lendb - lendbx, dumpbase_ext) != 0)
5256 : : {
5257 : 1 : free (dumpbase_ext);
5258 : 1 : dumpbase_ext = NULL;
5259 : : }
5260 : : }
5261 : :
5262 : : /* -dumpbase with multiple sources goes into dumpdir. With a single
5263 : : source, it does only if linking and if dumpdir was not explicitly
5264 : : specified. */
5265 : 24137 : if (dumpbase && *dumpbase
5266 : 322807 : && (single_input_file_index () == -2
5267 : 22410 : || (!have_c && !explicit_dumpdir)))
5268 : : {
5269 : 296 : char *prefix;
5270 : :
5271 : 296 : if (dumpbase_ext)
5272 : : /* We checked that they match above. */
5273 : 6 : dumpbase[strlen (dumpbase) - strlen (dumpbase_ext)] = '\0';
5274 : :
5275 : 296 : if (dumpdir)
5276 : 13 : prefix = concat (dumpdir, dumpbase, "-", NULL);
5277 : : else
5278 : 283 : prefix = concat (dumpbase, "-", NULL);
5279 : :
5280 : 296 : free (dumpdir);
5281 : 296 : free (dumpbase);
5282 : 296 : free (dumpbase_ext);
5283 : 296 : dumpbase = dumpbase_ext = NULL;
5284 : 296 : dumpdir = prefix;
5285 : 296 : dumpdir_trailing_dash_added = true;
5286 : : }
5287 : :
5288 : : /* If dumpbase was not brought into dumpdir but we're linking, bring
5289 : : output_file into dumpdir unless dumpdir was explicitly specified.
5290 : : The test for !explicit_dumpdir is further below, because we want
5291 : : to use the obase computation for a ghost outbase, passed to
5292 : : GCC_COLLECT_OPTIONS. */
5293 : 299817 : else if (!have_c && (!explicit_dumpdir || (dumpbase && !*dumpbase)))
5294 : : {
5295 : : /* If we get here, we know dumpbase was not specified, or it was
5296 : : specified as an empty string. If it was anything else, it
5297 : : would have combined with dumpdir above, because the condition
5298 : : for dumpbase to be used when present is broader than the
5299 : : condition that gets us here. */
5300 : 111191 : gcc_assert (!dumpbase || !*dumpbase);
5301 : :
5302 : 111191 : const char *obase;
5303 : 111191 : char *tofree = NULL;
5304 : 111191 : if (!output_file || not_actual_file_p (output_file))
5305 : : obase = "a";
5306 : : else
5307 : : {
5308 : 95488 : obase = lbasename (output_file);
5309 : 95488 : size_t blen = strlen (obase), xlen;
5310 : : /* Drop the suffix if it's dumpbase_ext, if given,
5311 : : otherwise .exe or the target executable suffix, or if the
5312 : : output was explicitly named a.out, but not otherwise. */
5313 : 95488 : if (dumpbase_ext
5314 : 95488 : ? (blen > (xlen = strlen (dumpbase_ext))
5315 : 223 : && strcmp ((temp = (obase + blen - xlen)),
5316 : : dumpbase_ext) == 0)
5317 : 95265 : : ((temp = strrchr (obase + 1, '.'))
5318 : 93411 : && (xlen = strlen (temp))
5319 : 188676 : && (strcmp (temp, ".exe") == 0
5320 : : #if defined(HAVE_TARGET_EXECUTABLE_SUFFIX)
5321 : : || strcmp (temp, TARGET_EXECUTABLE_SUFFIX) == 0
5322 : : #endif
5323 : 8985 : || strcmp (obase, "a.out") == 0)))
5324 : : {
5325 : 84675 : tofree = xstrndup (obase, blen - xlen);
5326 : 84675 : obase = tofree;
5327 : : }
5328 : : }
5329 : :
5330 : : /* We wish to save this basename to the -dumpdir passed through
5331 : : GCC_COLLECT_OPTIONS within maybe_run_linker, for e.g. LTO,
5332 : : but we do NOT wish to add it to e.g. %b, so we keep
5333 : : outbase_length as zero. */
5334 : 111191 : gcc_assert (!outbase);
5335 : 111191 : outbase_length = 0;
5336 : :
5337 : : /* If we're building [dir1/]foo[.exe] out of a single input
5338 : : [dir2/]foo.c that shares the same basename, dump to
5339 : : [dir2/]foo.c.* rather than duplicating the basename into
5340 : : [dir2/]foo-foo.c.*. */
5341 : 111191 : int idxin;
5342 : 111191 : if (dumpbase
5343 : 111191 : || ((idxin = single_input_file_index ()) >= 0
5344 : 85181 : && adds_single_suffix_p (lbasename (infiles[idxin].name),
5345 : : obase)))
5346 : : {
5347 : 77960 : if (obase == tofree)
5348 : 76199 : outbase = tofree;
5349 : : else
5350 : : {
5351 : 1761 : outbase = xstrdup (obase);
5352 : 1761 : free (tofree);
5353 : : }
5354 : 111191 : obase = tofree = NULL;
5355 : : }
5356 : : else
5357 : : {
5358 : 33231 : if (dumpdir)
5359 : : {
5360 : 15192 : char *p = concat (dumpdir, obase, "-", NULL);
5361 : 15192 : free (dumpdir);
5362 : 15192 : dumpdir = p;
5363 : : }
5364 : : else
5365 : 18039 : dumpdir = concat (obase, "-", NULL);
5366 : :
5367 : 33231 : dumpdir_trailing_dash_added = true;
5368 : :
5369 : 33231 : free (tofree);
5370 : 33231 : obase = tofree = NULL;
5371 : : }
5372 : :
5373 : 111191 : if (!explicit_dumpdir || dumpbase)
5374 : : {
5375 : : /* Absent -dumpbase and present -dumpbase-ext have been applied
5376 : : to the linker output name, so compute fresh defaults for each
5377 : : compilation. */
5378 : 111191 : free (dumpbase_ext);
5379 : 111191 : dumpbase_ext = NULL;
5380 : : }
5381 : : }
5382 : :
5383 : : /* Now, if we're compiling, or if we haven't used the dumpbase
5384 : : above, then outbase (%B) is derived from dumpbase, if given, or
5385 : : from the output name, given or implied. We can't precompute
5386 : : implied output names, but that's ok, since they're derived from
5387 : : input names. Just make sure we skip this if dumpbase is the
5388 : : empty string: we want to use input names then, so don't set
5389 : : outbase. */
5390 : 300113 : if ((dumpbase || have_c)
5391 : 190281 : && !(dumpbase && !*dumpbase))
5392 : : {
5393 : 188838 : gcc_assert (!outbase);
5394 : :
5395 : 188838 : if (dumpbase)
5396 : : {
5397 : 22398 : gcc_assert (single_input_file_index () != -2);
5398 : : /* We do not want lbasename here; dumpbase with dirnames
5399 : : overrides dumpdir entirely, even if dumpdir is
5400 : : specified. */
5401 : 22398 : if (dumpbase_ext)
5402 : : /* We've already checked above that the suffix matches. */
5403 : 13 : outbase = xstrndup (dumpbase,
5404 : 13 : strlen (dumpbase) - strlen (dumpbase_ext));
5405 : : else
5406 : 22385 : outbase = xstrdup (dumpbase);
5407 : : }
5408 : 166440 : else if (output_file && !not_actual_file_p (output_file))
5409 : : {
5410 : 161496 : outbase = xstrdup (lbasename (output_file));
5411 : 161496 : char *p = strrchr (outbase + 1, '.');
5412 : 161496 : if (p)
5413 : 161496 : *p = '\0';
5414 : : }
5415 : :
5416 : 188838 : if (outbase)
5417 : 183894 : outbase_length = strlen (outbase);
5418 : : }
5419 : :
5420 : : /* If there is any pathname component in an explicit -dumpbase, do
5421 : : not use dumpdir, but retain it to pass it on to the compiler. */
5422 : 300113 : if (dumpdir)
5423 : 121369 : dumpdir_length = strlen (dumpdir);
5424 : : else
5425 : 178744 : dumpdir_length = 0;
5426 : :
5427 : : /* Check that dumpbase_ext, if still present, still matches the end
5428 : : of dumpbase, if present, and drop it otherwise. We only retained
5429 : : it above when dumpbase was absent to maybe use it to drop the
5430 : : extension from output_name before combining it with dumpdir. We
5431 : : won't deal with -dumpbase-ext when -dumpbase is not explicitly
5432 : : given, even if just to activate backward-compatible dumpbase:
5433 : : dropping it on the floor is correct, expected and documented
5434 : : behavior. Attempting to deal with a -dumpbase-ext that might
5435 : : match the end of some input filename, or of the combination of
5436 : : the output basename with the suffix of the input filename,
5437 : : possible with an intermediate .gk extension for -fcompare-debug,
5438 : : is just calling for trouble. */
5439 : 300113 : if (dumpbase_ext)
5440 : : {
5441 : 22 : if (!dumpbase || !*dumpbase)
5442 : : {
5443 : 9 : free (dumpbase_ext);
5444 : 9 : dumpbase_ext = NULL;
5445 : : }
5446 : : else
5447 : 13 : gcc_assert (strcmp (dumpbase + strlen (dumpbase)
5448 : : - strlen (dumpbase_ext), dumpbase_ext) == 0);
5449 : : }
5450 : :
5451 : 300113 : if (save_temps_flag && use_pipes)
5452 : : {
5453 : : /* -save-temps overrides -pipe, so that temp files are produced */
5454 : 0 : if (save_temps_flag)
5455 : 0 : warning (0, "%<-pipe%> ignored because %<-save-temps%> specified");
5456 : 0 : use_pipes = 0;
5457 : : }
5458 : :
5459 : 300113 : if (!compare_debug)
5460 : : {
5461 : 299494 : const char *gcd = env.get ("GCC_COMPARE_DEBUG");
5462 : :
5463 : 299494 : if (gcd && gcd[0] == '-')
5464 : : {
5465 : 0 : compare_debug = 2;
5466 : 0 : compare_debug_opt = gcd;
5467 : : }
5468 : 0 : else if (gcd && *gcd && strcmp (gcd, "0"))
5469 : : {
5470 : 0 : compare_debug = 3;
5471 : 0 : compare_debug_opt = "-gtoggle";
5472 : : }
5473 : : }
5474 : 619 : else if (compare_debug < 0)
5475 : : {
5476 : 0 : compare_debug = 0;
5477 : 0 : gcc_assert (!compare_debug_opt);
5478 : : }
5479 : :
5480 : : /* Set up the search paths. We add directories that we expect to
5481 : : contain GNU Toolchain components before directories specified by
5482 : : the machine description so that we will find GNU components (like
5483 : : the GNU assembler) before those of the host system. */
5484 : :
5485 : : /* If we don't know where the toolchain has been installed, use the
5486 : : configured-in locations. */
5487 : 300113 : if (!gcc_exec_prefix)
5488 : : {
5489 : : #ifndef OS2
5490 : 0 : add_prefix (&exec_prefixes, standard_libexec_prefix, "GCC",
5491 : : PREFIX_PRIORITY_LAST, 1, 0);
5492 : 0 : add_prefix (&exec_prefixes, standard_libexec_prefix, "BINUTILS",
5493 : : PREFIX_PRIORITY_LAST, 2, 0);
5494 : 0 : add_prefix (&exec_prefixes, standard_exec_prefix, "BINUTILS",
5495 : : PREFIX_PRIORITY_LAST, 2, 0);
5496 : : #endif
5497 : 0 : add_prefix (&startfile_prefixes, standard_exec_prefix, "BINUTILS",
5498 : : PREFIX_PRIORITY_LAST, 1, 0);
5499 : : }
5500 : :
5501 : 300113 : gcc_assert (!IS_ABSOLUTE_PATH (tooldir_base_prefix));
5502 : 300113 : tooldir_prefix2 = concat (tooldir_base_prefix, spec_machine,
5503 : : dir_separator_str, NULL);
5504 : :
5505 : : /* Look for tools relative to the location from which the driver is
5506 : : running, or, if that is not available, the configured prefix. */
5507 : 300113 : tooldir_prefix
5508 : 600226 : = concat (gcc_exec_prefix ? gcc_exec_prefix : standard_exec_prefix,
5509 : : spec_host_machine, dir_separator_str, spec_version,
5510 : : accel_dir_suffix, dir_separator_str, tooldir_prefix2, NULL);
5511 : 300113 : free (tooldir_prefix2);
5512 : :
5513 : 300113 : add_prefix (&exec_prefixes,
5514 : 300113 : concat (tooldir_prefix, "bin", dir_separator_str, NULL),
5515 : : "BINUTILS", PREFIX_PRIORITY_LAST, 0, 0);
5516 : 300113 : add_prefix (&startfile_prefixes,
5517 : 300113 : concat (tooldir_prefix, "lib", dir_separator_str, NULL),
5518 : : "BINUTILS", PREFIX_PRIORITY_LAST, 0, 1);
5519 : 300113 : free (tooldir_prefix);
5520 : :
5521 : : #if defined(TARGET_SYSTEM_ROOT_RELOCATABLE) && !defined(VMS)
5522 : : /* If the normal TARGET_SYSTEM_ROOT is inside of $exec_prefix,
5523 : : then consider it to relocate with the rest of the GCC installation
5524 : : if GCC_EXEC_PREFIX is set.
5525 : : ``make_relative_prefix'' is not compiled for VMS, so don't call it. */
5526 : : if (target_system_root && !target_system_root_changed && gcc_exec_prefix)
5527 : : {
5528 : : char *tmp_prefix = get_relative_prefix (decoded_options[0].arg,
5529 : : standard_bindir_prefix,
5530 : : target_system_root);
5531 : : if (tmp_prefix && access_check (tmp_prefix, F_OK) == 0)
5532 : : {
5533 : : target_system_root = tmp_prefix;
5534 : : target_system_root_changed = 1;
5535 : : }
5536 : : }
5537 : : #endif
5538 : :
5539 : : /* More prefixes are enabled in main, after we read the specs file
5540 : : and determine whether this is cross-compilation or not. */
5541 : :
5542 : 300113 : if (n_infiles != 0 && n_infiles == last_language_n_infiles && spec_lang != 0)
5543 : 0 : warning (0, "%<-x %s%> after last input file has no effect", spec_lang);
5544 : :
5545 : : /* Synthesize -fcompare-debug flag from the GCC_COMPARE_DEBUG
5546 : : environment variable. */
5547 : 300113 : if (compare_debug == 2 || compare_debug == 3)
5548 : : {
5549 : 0 : const char *opt = concat ("-fcompare-debug=", compare_debug_opt, NULL);
5550 : 0 : save_switch (opt, 0, NULL, false, true);
5551 : 0 : compare_debug = 1;
5552 : : }
5553 : :
5554 : : /* Ensure we only invoke each subprocess once. */
5555 : 300113 : if (n_infiles == 0
5556 : 9589 : && (print_subprocess_help || print_help_list || print_version))
5557 : : {
5558 : : /* Create a dummy input file, so that we can pass
5559 : : the help option on to the various sub-processes. */
5560 : 78 : add_infile ("help-dummy", "c");
5561 : : }
5562 : :
5563 : : /* Decide if undefined variable references are allowed in specs. */
5564 : :
5565 : : /* -v alone is safe. --version and --help alone or together are safe. Note
5566 : : that -v would make them unsafe, as they'd then be run for subprocesses as
5567 : : well, the location of which might depend on variables possibly coming
5568 : : from self-specs. Note also that the command name is counted in
5569 : : decoded_options_count. */
5570 : :
5571 : 300113 : unsigned help_version_count = 0;
5572 : :
5573 : 300113 : if (print_version)
5574 : 78 : help_version_count++;
5575 : :
5576 : 300113 : if (print_help_list)
5577 : 4 : help_version_count++;
5578 : :
5579 : 600226 : spec_undefvar_allowed =
5580 : 1479 : ((verbose_flag && decoded_options_count == 2)
5581 : 301520 : || help_version_count == decoded_options_count - 1);
5582 : :
5583 : 300113 : alloc_switch ();
5584 : 300113 : switches[n_switches].part1 = 0;
5585 : 300113 : alloc_infile ();
5586 : 300113 : infiles[n_infiles].name = 0;
5587 : 300113 : }
5588 : :
5589 : : /* Store switches not filtered out by %<S in spec in COLLECT_GCC_OPTIONS
5590 : : and place that in the environment. */
5591 : :
5592 : : static void
5593 : 826045 : set_collect_gcc_options (void)
5594 : : {
5595 : 826045 : int i;
5596 : 826045 : int first_time;
5597 : :
5598 : : /* Build COLLECT_GCC_OPTIONS to have all of the options specified to
5599 : : the compiler. */
5600 : 826045 : obstack_grow (&collect_obstack, "COLLECT_GCC_OPTIONS=",
5601 : : sizeof ("COLLECT_GCC_OPTIONS=") - 1);
5602 : :
5603 : 826045 : first_time = true;
5604 : 19950807 : for (i = 0; (int) i < n_switches; i++)
5605 : : {
5606 : 19124762 : const char *const *args;
5607 : 19124762 : const char *p, *q;
5608 : 19124762 : if (!first_time)
5609 : 18298717 : obstack_grow (&collect_obstack, " ", 1);
5610 : :
5611 : 19124762 : first_time = false;
5612 : :
5613 : : /* Ignore elided switches. */
5614 : 19255862 : if ((switches[i].live_cond
5615 : 19124762 : & (SWITCH_IGNORE | SWITCH_KEEP_FOR_GCC))
5616 : : == SWITCH_IGNORE)
5617 : 131100 : continue;
5618 : :
5619 : 18993662 : obstack_grow (&collect_obstack, "'-", 2);
5620 : 18993662 : q = switches[i].part1;
5621 : 18993662 : while ((p = strchr (q, '\'')))
5622 : : {
5623 : 0 : obstack_grow (&collect_obstack, q, p - q);
5624 : 0 : obstack_grow (&collect_obstack, "'\\''", 4);
5625 : 0 : q = ++p;
5626 : : }
5627 : 18993662 : obstack_grow (&collect_obstack, q, strlen (q));
5628 : 18993662 : obstack_grow (&collect_obstack, "'", 1);
5629 : :
5630 : 23156724 : for (args = switches[i].args; args && *args; args++)
5631 : : {
5632 : 4163062 : obstack_grow (&collect_obstack, " '", 2);
5633 : 4163062 : q = *args;
5634 : 4163062 : while ((p = strchr (q, '\'')))
5635 : : {
5636 : 0 : obstack_grow (&collect_obstack, q, p - q);
5637 : 0 : obstack_grow (&collect_obstack, "'\\''", 4);
5638 : 0 : q = ++p;
5639 : : }
5640 : 4163062 : obstack_grow (&collect_obstack, q, strlen (q));
5641 : 4163062 : obstack_grow (&collect_obstack, "'", 1);
5642 : : }
5643 : : }
5644 : :
5645 : 826045 : if (dumpdir)
5646 : : {
5647 : 582878 : if (!first_time)
5648 : 582878 : obstack_grow (&collect_obstack, " ", 1);
5649 : 582878 : first_time = false;
5650 : :
5651 : 582878 : obstack_grow (&collect_obstack, "'-dumpdir' '", 12);
5652 : 582878 : const char *p, *q;
5653 : :
5654 : 582878 : q = dumpdir;
5655 : 582878 : while ((p = strchr (q, '\'')))
5656 : : {
5657 : 0 : obstack_grow (&collect_obstack, q, p - q);
5658 : 0 : obstack_grow (&collect_obstack, "'\\''", 4);
5659 : 0 : q = ++p;
5660 : : }
5661 : 582878 : obstack_grow (&collect_obstack, q, strlen (q));
5662 : :
5663 : 582878 : obstack_grow (&collect_obstack, "'", 1);
5664 : : }
5665 : :
5666 : 826045 : obstack_grow (&collect_obstack, "\0", 1);
5667 : 826045 : xputenv (XOBFINISH (&collect_obstack, char *));
5668 : 826045 : }
5669 : :
5670 : : /* Process a spec string, accumulating and running commands. */
5671 : :
5672 : : /* These variables describe the input file name.
5673 : : input_file_number is the index on outfiles of this file,
5674 : : so that the output file name can be stored for later use by %o.
5675 : : input_basename is the start of the part of the input file
5676 : : sans all directory names, and basename_length is the number
5677 : : of characters starting there excluding the suffix .c or whatever. */
5678 : :
5679 : : static const char *gcc_input_filename;
5680 : : static int input_file_number;
5681 : : size_t input_filename_length;
5682 : : static int basename_length;
5683 : : static int suffixed_basename_length;
5684 : : static const char *input_basename;
5685 : : static const char *input_suffix;
5686 : : #ifndef HOST_LACKS_INODE_NUMBERS
5687 : : static struct stat input_stat;
5688 : : #endif
5689 : : static int input_stat_set;
5690 : :
5691 : : /* The compiler used to process the current input file. */
5692 : : static struct compiler *input_file_compiler;
5693 : :
5694 : : /* These are variables used within do_spec and do_spec_1. */
5695 : :
5696 : : /* Nonzero if an arg has been started and not yet terminated
5697 : : (with space, tab or newline). */
5698 : : static int arg_going;
5699 : :
5700 : : /* Nonzero means %d or %g has been seen; the next arg to be terminated
5701 : : is a temporary file name. */
5702 : : static int delete_this_arg;
5703 : :
5704 : : /* Nonzero means %w has been seen; the next arg to be terminated
5705 : : is the output file name of this compilation. */
5706 : : static int this_is_output_file;
5707 : :
5708 : : /* Nonzero means %s has been seen; the next arg to be terminated
5709 : : is the name of a library file and we should try the standard
5710 : : search dirs for it. */
5711 : : static int this_is_library_file;
5712 : :
5713 : : /* Nonzero means %T has been seen; the next arg to be terminated
5714 : : is the name of a linker script and we should try all of the
5715 : : standard search dirs for it. If it is found insert a --script
5716 : : command line switch and then substitute the full path in place,
5717 : : otherwise generate an error message. */
5718 : : static int this_is_linker_script;
5719 : :
5720 : : /* Nonzero means that the input of this command is coming from a pipe. */
5721 : : static int input_from_pipe;
5722 : :
5723 : : /* Nonnull means substitute this for any suffix when outputting a switches
5724 : : arguments. */
5725 : : static const char *suffix_subst;
5726 : :
5727 : : /* If there is an argument being accumulated, terminate it and store it. */
5728 : :
5729 : : static void
5730 : 81702700 : end_going_arg (void)
5731 : : {
5732 : 81702700 : if (arg_going)
5733 : : {
5734 : 20021786 : const char *string;
5735 : :
5736 : 20021786 : obstack_1grow (&obstack, 0);
5737 : 20021786 : string = XOBFINISH (&obstack, const char *);
5738 : 20021786 : if (this_is_library_file)
5739 : 541155 : string = find_file (string);
5740 : 20021786 : if (this_is_linker_script)
5741 : : {
5742 : 0 : char * full_script_path = find_a_file (&startfile_prefixes, string, R_OK, true);
5743 : :
5744 : 0 : if (full_script_path == NULL)
5745 : : {
5746 : 0 : error ("unable to locate default linker script %qs in the library search paths", string);
5747 : : /* Script was not found on search path. */
5748 : 0 : return;
5749 : : }
5750 : 0 : store_arg ("--script", false, false);
5751 : 0 : string = full_script_path;
5752 : : }
5753 : 20021786 : store_arg (string, delete_this_arg, this_is_output_file);
5754 : 20021786 : if (this_is_output_file)
5755 : 100404 : outfiles[input_file_number] = string;
5756 : 20021786 : arg_going = 0;
5757 : : }
5758 : : }
5759 : :
5760 : :
5761 : : /* Parse the WRAPPER string which is a comma separated list of the command line
5762 : : and insert them into the beginning of argbuf. */
5763 : :
5764 : : static void
5765 : 0 : insert_wrapper (const char *wrapper)
5766 : : {
5767 : 0 : int n = 0;
5768 : 0 : int i;
5769 : 0 : char *buf = xstrdup (wrapper);
5770 : 0 : char *p = buf;
5771 : 0 : unsigned int old_length = argbuf.length ();
5772 : :
5773 : 0 : do
5774 : : {
5775 : 0 : n++;
5776 : 0 : while (*p == ',')
5777 : 0 : p++;
5778 : : }
5779 : 0 : while ((p = strchr (p, ',')) != NULL);
5780 : :
5781 : 0 : argbuf.safe_grow (old_length + n, true);
5782 : 0 : memmove (argbuf.address () + n,
5783 : 0 : argbuf.address (),
5784 : 0 : old_length * sizeof (const_char_p));
5785 : :
5786 : 0 : i = 0;
5787 : 0 : p = buf;
5788 : : do
5789 : : {
5790 : 0 : while (*p == ',')
5791 : : {
5792 : 0 : *p = 0;
5793 : 0 : p++;
5794 : : }
5795 : 0 : argbuf[i] = p;
5796 : 0 : i++;
5797 : : }
5798 : 0 : while ((p = strchr (p, ',')) != NULL);
5799 : 0 : gcc_assert (i == n);
5800 : 0 : }
5801 : :
5802 : : /* Process the spec SPEC and run the commands specified therein.
5803 : : Returns 0 if the spec is successfully processed; -1 if failed. */
5804 : :
5805 : : int
5806 : 566433 : do_spec (const char *spec)
5807 : : {
5808 : 566433 : int value;
5809 : :
5810 : 566433 : value = do_spec_2 (spec, NULL);
5811 : :
5812 : : /* Force out any unfinished command.
5813 : : If -pipe, this forces out the last command if it ended in `|'. */
5814 : 566430 : if (value == 0)
5815 : : {
5816 : 560639 : if (argbuf.length () > 0
5817 : 841936 : && !strcmp (argbuf.last (), "|"))
5818 : 0 : argbuf.pop ();
5819 : :
5820 : 560639 : set_collect_gcc_options ();
5821 : :
5822 : 560639 : if (argbuf.length () > 0)
5823 : 281297 : value = execute ();
5824 : : }
5825 : :
5826 : 566430 : return value;
5827 : : }
5828 : :
5829 : : /* Process the spec SPEC, with SOFT_MATCHED_PART designating the current value
5830 : : of a matched * pattern which may be re-injected by way of %*. */
5831 : :
5832 : : static int
5833 : 5348794 : do_spec_2 (const char *spec, const char *soft_matched_part)
5834 : : {
5835 : 5348794 : int result;
5836 : :
5837 : 5348794 : clear_args ();
5838 : 5348794 : arg_going = 0;
5839 : 5348794 : delete_this_arg = 0;
5840 : 5348794 : this_is_output_file = 0;
5841 : 5348794 : this_is_library_file = 0;
5842 : 5348794 : this_is_linker_script = 0;
5843 : 5348794 : input_from_pipe = 0;
5844 : 5348794 : suffix_subst = NULL;
5845 : :
5846 : 5348794 : result = do_spec_1 (spec, 0, soft_matched_part);
5847 : :
5848 : 5348791 : end_going_arg ();
5849 : :
5850 : 5348791 : return result;
5851 : : }
5852 : :
5853 : : /* Process the given spec string and add any new options to the end
5854 : : of the switches/n_switches array. */
5855 : :
5856 : : static void
5857 : 3002410 : do_option_spec (const char *name, const char *spec)
5858 : : {
5859 : 3002410 : unsigned int i, value_count, value_len;
5860 : 3002410 : const char *p, *q, *value;
5861 : 3002410 : char *tmp_spec, *tmp_spec_p;
5862 : :
5863 : 3002410 : if (configure_default_options[0].name == NULL)
5864 : : return;
5865 : :
5866 : 8106507 : for (i = 0; i < ARRAY_SIZE (configure_default_options); i++)
5867 : 5704579 : if (strcmp (configure_default_options[i].name, name) == 0)
5868 : : break;
5869 : 3002410 : if (i == ARRAY_SIZE (configure_default_options))
5870 : : return;
5871 : :
5872 : 600482 : value = configure_default_options[i].value;
5873 : 600482 : value_len = strlen (value);
5874 : :
5875 : : /* Compute the size of the final spec. */
5876 : 600482 : value_count = 0;
5877 : 600482 : p = spec;
5878 : 1200964 : while ((p = strstr (p, "%(VALUE)")) != NULL)
5879 : : {
5880 : 600482 : p ++;
5881 : 600482 : value_count ++;
5882 : : }
5883 : :
5884 : : /* Replace each %(VALUE) by the specified value. */
5885 : 600482 : tmp_spec = (char *) alloca (strlen (spec) + 1
5886 : : + value_count * (value_len - strlen ("%(VALUE)")));
5887 : 600482 : tmp_spec_p = tmp_spec;
5888 : 600482 : q = spec;
5889 : 1200964 : while ((p = strstr (q, "%(VALUE)")) != NULL)
5890 : : {
5891 : 600482 : memcpy (tmp_spec_p, q, p - q);
5892 : 600482 : tmp_spec_p = tmp_spec_p + (p - q);
5893 : 600482 : memcpy (tmp_spec_p, value, value_len);
5894 : 600482 : tmp_spec_p += value_len;
5895 : 600482 : q = p + strlen ("%(VALUE)");
5896 : : }
5897 : 600482 : strcpy (tmp_spec_p, q);
5898 : :
5899 : 600482 : do_self_spec (tmp_spec);
5900 : : }
5901 : :
5902 : : /* Process the given spec string and add any new options to the end
5903 : : of the switches/n_switches array. */
5904 : :
5905 : : static void
5906 : 2702510 : do_self_spec (const char *spec)
5907 : : {
5908 : 2702510 : int i;
5909 : :
5910 : 2702510 : do_spec_2 (spec, NULL);
5911 : 2702510 : do_spec_1 (" ", 0, NULL);
5912 : :
5913 : : /* Mark %<S switches processed by do_self_spec to be ignored permanently.
5914 : : do_self_specs adds the replacements to switches array, so it shouldn't
5915 : : be processed afterwards. */
5916 : 65241449 : for (i = 0; i < n_switches; i++)
5917 : 59836429 : if ((switches[i].live_cond & SWITCH_IGNORE))
5918 : 667 : switches[i].live_cond |= SWITCH_IGNORE_PERMANENTLY;
5919 : :
5920 : 2702510 : if (argbuf.length () > 0)
5921 : : {
5922 : 566502 : const char **argbuf_copy;
5923 : 566502 : struct cl_decoded_option *decoded_options;
5924 : 566502 : struct cl_option_handlers handlers;
5925 : 566502 : unsigned int decoded_options_count;
5926 : 566502 : unsigned int j;
5927 : :
5928 : : /* Create a copy of argbuf with a dummy argv[0] entry for
5929 : : decode_cmdline_options_to_array. */
5930 : 566502 : argbuf_copy = XNEWVEC (const char *,
5931 : : argbuf.length () + 1);
5932 : 566502 : argbuf_copy[0] = "";
5933 : 566502 : memcpy (argbuf_copy + 1, argbuf.address (),
5934 : 566502 : argbuf.length () * sizeof (const char *));
5935 : :
5936 : 1133004 : decode_cmdline_options_to_array (argbuf.length () + 1,
5937 : : argbuf_copy,
5938 : : CL_DRIVER, &decoded_options,
5939 : : &decoded_options_count);
5940 : 566502 : free (argbuf_copy);
5941 : :
5942 : 566502 : set_option_handlers (&handlers);
5943 : :
5944 : 1135480 : for (j = 1; j < decoded_options_count; j++)
5945 : : {
5946 : 568978 : switch (decoded_options[j].opt_index)
5947 : : {
5948 : 0 : case OPT_SPECIAL_input_file:
5949 : : /* Specs should only generate options, not input
5950 : : files. */
5951 : 0 : if (strcmp (decoded_options[j].arg, "-") != 0)
5952 : 0 : fatal_error (input_location,
5953 : : "switch %qs does not start with %<-%>",
5954 : : decoded_options[j].arg);
5955 : : else
5956 : 0 : fatal_error (input_location,
5957 : : "spec-generated switch is just %<-%>");
5958 : 1238 : break;
5959 : :
5960 : 1238 : case OPT_fcompare_debug_second:
5961 : 1238 : case OPT_fcompare_debug:
5962 : 1238 : case OPT_fcompare_debug_:
5963 : 1238 : case OPT_o:
5964 : : /* Avoid duplicate processing of some options from
5965 : : compare-debug specs; just save them here. */
5966 : 1238 : save_switch (decoded_options[j].canonical_option[0],
5967 : 1238 : (decoded_options[j].canonical_option_num_elements
5968 : : - 1),
5969 : 1238 : &decoded_options[j].canonical_option[1], false, true);
5970 : 1238 : break;
5971 : :
5972 : 567740 : default:
5973 : 567740 : read_cmdline_option (&global_options, &global_options_set,
5974 : : decoded_options + j, UNKNOWN_LOCATION,
5975 : : CL_DRIVER, &handlers, global_dc);
5976 : 567740 : break;
5977 : : }
5978 : : }
5979 : :
5980 : 566502 : free (decoded_options);
5981 : :
5982 : 566502 : alloc_switch ();
5983 : 566502 : switches[n_switches].part1 = 0;
5984 : : }
5985 : 2702510 : }
5986 : :
5987 : : /* Callback for processing %D and %I specs. */
5988 : :
5989 : : struct spec_path {
5990 : : const char *option;
5991 : : const char *append;
5992 : : size_t append_len;
5993 : : bool omit_relative;
5994 : : bool separate_options;
5995 : : bool realpaths;
5996 : :
5997 : : void *operator() (char *path);
5998 : : };
5999 : :
6000 : : void *
6001 : 3351075 : spec_path::operator() (char *path)
6002 : : {
6003 : 3351075 : size_t len = 0;
6004 : 3351075 : char save = 0;
6005 : :
6006 : : /* The path must exist; we want to resolve it to the realpath so that this
6007 : : can be embedded as a runpath. */
6008 : 3351075 : if (realpaths)
6009 : 0 : path = lrealpath (path);
6010 : :
6011 : : /* However, if we failed to resolve it - perhaps because there was a bogus
6012 : : -B option on the command line, then punt on this entry. */
6013 : 3351075 : if (!path)
6014 : : return NULL;
6015 : :
6016 : 3351075 : if (omit_relative && !IS_ABSOLUTE_PATH (path))
6017 : : return NULL;
6018 : :
6019 : 3351075 : if (append_len != 0)
6020 : : {
6021 : 1395700 : len = strlen (path);
6022 : 1395700 : memcpy (path + len, append, append_len + 1);
6023 : : }
6024 : :
6025 : 3351075 : if (!is_directory (path))
6026 : : return NULL;
6027 : :
6028 : 1256433 : do_spec_1 (option, 1, NULL);
6029 : 1256433 : if (separate_options)
6030 : 448708 : do_spec_1 (" ", 0, NULL);
6031 : :
6032 : 1256433 : if (append_len == 0)
6033 : : {
6034 : 807725 : len = strlen (path);
6035 : 807725 : save = path[len - 1];
6036 : 807725 : if (IS_DIR_SEPARATOR (path[len - 1]))
6037 : 807725 : path[len - 1] = '\0';
6038 : : }
6039 : :
6040 : 1256433 : do_spec_1 (path, 1, NULL);
6041 : 1256433 : do_spec_1 (" ", 0, NULL);
6042 : :
6043 : : /* Must not damage the original path. */
6044 : 1256433 : if (append_len == 0)
6045 : 807725 : path[len - 1] = save;
6046 : :
6047 : : return NULL;
6048 : : }
6049 : :
6050 : : /* True if we should compile INFILE. */
6051 : :
6052 : : static bool
6053 : 45246 : compile_input_file_p (struct infile *infile)
6054 : : {
6055 : 28076 : if ((!infile->language) || (infile->language[0] != '*'))
6056 : 40756 : if (infile->incompiler == input_file_compiler)
6057 : 0 : return true;
6058 : : return false;
6059 : : }
6060 : :
6061 : : /* Process each member of VEC as a spec. */
6062 : :
6063 : : static void
6064 : 468988 : do_specs_vec (vec<char_p> vec)
6065 : : {
6066 : 469070 : for (char *opt : vec)
6067 : : {
6068 : 58 : do_spec_1 (opt, 1, NULL);
6069 : : /* Make each accumulated option a separate argument. */
6070 : 58 : do_spec_1 (" ", 0, NULL);
6071 : : }
6072 : 468988 : }
6073 : :
6074 : : /* Add options passed via -Xassembler or -Wa to COLLECT_AS_OPTIONS. */
6075 : :
6076 : : static void
6077 : 300112 : putenv_COLLECT_AS_OPTIONS (vec<char_p> vec)
6078 : : {
6079 : 300112 : if (vec.is_empty ())
6080 : 300112 : return;
6081 : :
6082 : 91 : obstack_init (&collect_obstack);
6083 : 91 : obstack_grow (&collect_obstack, "COLLECT_AS_OPTIONS=",
6084 : : strlen ("COLLECT_AS_OPTIONS="));
6085 : :
6086 : 91 : char *opt;
6087 : 91 : unsigned ix;
6088 : :
6089 : 274 : FOR_EACH_VEC_ELT (vec, ix, opt)
6090 : : {
6091 : 183 : obstack_1grow (&collect_obstack, '\'');
6092 : 183 : obstack_grow (&collect_obstack, opt, strlen (opt));
6093 : 183 : obstack_1grow (&collect_obstack, '\'');
6094 : 183 : if (ix < vec.length () - 1)
6095 : 92 : obstack_1grow(&collect_obstack, ' ');
6096 : : }
6097 : :
6098 : 91 : obstack_1grow (&collect_obstack, '\0');
6099 : 91 : xputenv (XOBFINISH (&collect_obstack, char *));
6100 : : }
6101 : :
6102 : : /* Process the sub-spec SPEC as a portion of a larger spec.
6103 : : This is like processing a whole spec except that we do
6104 : : not initialize at the beginning and we do not supply a
6105 : : newline by default at the end.
6106 : : INSWITCH nonzero means don't process %-sequences in SPEC;
6107 : : in this case, % is treated as an ordinary character.
6108 : : This is used while substituting switches.
6109 : : INSWITCH nonzero also causes SPC not to terminate an argument.
6110 : :
6111 : : Value is zero unless a line was finished
6112 : : and the command on that line reported an error. */
6113 : :
6114 : : static int
6115 : 52758756 : do_spec_1 (const char *spec, int inswitch, const char *soft_matched_part)
6116 : : {
6117 : 52758756 : const char *p = spec;
6118 : 52758756 : int c;
6119 : 52758756 : int i;
6120 : 52758756 : int value;
6121 : :
6122 : : /* If it's an empty string argument to a switch, keep it as is. */
6123 : 52758756 : if (inswitch && !*p)
6124 : 1 : arg_going = 1;
6125 : :
6126 : 541012267 : while ((c = *p++))
6127 : : /* If substituting a switch, treat all chars like letters.
6128 : : Otherwise, NL, SPC, TAB and % are special. */
6129 : 488302270 : switch (inswitch ? 'a' : c)
6130 : : {
6131 : 265406 : case '\n':
6132 : 265406 : end_going_arg ();
6133 : :
6134 : 265406 : if (argbuf.length () > 0
6135 : 530812 : && !strcmp (argbuf.last (), "|"))
6136 : : {
6137 : : /* A `|' before the newline means use a pipe here,
6138 : : but only if -pipe was specified.
6139 : : Otherwise, execute now and don't pass the `|' as an arg. */
6140 : 168669 : if (use_pipes)
6141 : : {
6142 : 0 : input_from_pipe = 1;
6143 : 0 : break;
6144 : : }
6145 : : else
6146 : 168669 : argbuf.pop ();
6147 : : }
6148 : :
6149 : 265406 : set_collect_gcc_options ();
6150 : :
6151 : 265406 : if (argbuf.length () > 0)
6152 : : {
6153 : 265406 : value = execute ();
6154 : 265406 : if (value)
6155 : : return value;
6156 : : }
6157 : : /* Reinitialize for a new command, and for a new argument. */
6158 : 259615 : clear_args ();
6159 : 259615 : arg_going = 0;
6160 : 259615 : delete_this_arg = 0;
6161 : 259615 : this_is_output_file = 0;
6162 : 259615 : this_is_library_file = 0;
6163 : 259615 : this_is_linker_script = 0;
6164 : 259615 : input_from_pipe = 0;
6165 : 259615 : break;
6166 : :
6167 : 168669 : case '|':
6168 : 168669 : end_going_arg ();
6169 : :
6170 : : /* Use pipe */
6171 : 168669 : obstack_1grow (&obstack, c);
6172 : 168669 : arg_going = 1;
6173 : 168669 : break;
6174 : :
6175 : 71228380 : case '\t':
6176 : 71228380 : case ' ':
6177 : 71228380 : end_going_arg ();
6178 : :
6179 : : /* Reinitialize for a new argument. */
6180 : 71228380 : delete_this_arg = 0;
6181 : 71228380 : this_is_output_file = 0;
6182 : 71228380 : this_is_library_file = 0;
6183 : 71228380 : this_is_linker_script = 0;
6184 : 71228380 : break;
6185 : :
6186 : 49412016 : case '%':
6187 : 49412016 : switch (c = *p++)
6188 : : {
6189 : 0 : case 0:
6190 : 0 : fatal_error (input_location, "spec %qs invalid", spec);
6191 : :
6192 : 3593 : case 'b':
6193 : : /* Don't use %b in the linker command. */
6194 : 3593 : gcc_assert (suffixed_basename_length);
6195 : 3593 : if (!this_is_output_file && dumpdir_length)
6196 : 689 : obstack_grow (&obstack, dumpdir, dumpdir_length);
6197 : 3593 : if (this_is_output_file || !outbase_length)
6198 : 3251 : obstack_grow (&obstack, input_basename, basename_length);
6199 : : else
6200 : 342 : obstack_grow (&obstack, outbase, outbase_length);
6201 : 3593 : if (compare_debug < 0)
6202 : 6 : obstack_grow (&obstack, ".gk", 3);
6203 : 3593 : arg_going = 1;
6204 : 3593 : break;
6205 : :
6206 : 10 : case 'B':
6207 : : /* Don't use %B in the linker command. */
6208 : 10 : gcc_assert (suffixed_basename_length);
6209 : 10 : if (!this_is_output_file && dumpdir_length)
6210 : 0 : obstack_grow (&obstack, dumpdir, dumpdir_length);
6211 : 10 : if (this_is_output_file || !outbase_length)
6212 : 5 : obstack_grow (&obstack, input_basename, basename_length);
6213 : : else
6214 : 5 : obstack_grow (&obstack, outbase, outbase_length);
6215 : 10 : if (compare_debug < 0)
6216 : 3 : obstack_grow (&obstack, ".gk", 3);
6217 : 10 : obstack_grow (&obstack, input_basename + basename_length,
6218 : : suffixed_basename_length - basename_length);
6219 : :
6220 : 10 : arg_going = 1;
6221 : 10 : break;
6222 : :
6223 : 97893 : case 'd':
6224 : 97893 : delete_this_arg = 2;
6225 : 97893 : break;
6226 : :
6227 : : /* Dump out the directories specified with LIBRARY_PATH,
6228 : : followed by the absolute directories
6229 : : that we search for startfiles. */
6230 : 105550 : case 'D':
6231 : 105550 : {
6232 : 105550 : struct spec_path info;
6233 : :
6234 : 105550 : info.option = "-L";
6235 : 105550 : info.append_len = 0;
6236 : : #ifdef RELATIVE_PREFIX_NOT_LINKDIR
6237 : : /* Used on systems which record the specified -L dirs
6238 : : and use them to search for dynamic linking.
6239 : : Relative directories always come from -B,
6240 : : and it is better not to use them for searching
6241 : : at run time. In particular, stage1 loses. */
6242 : : info.omit_relative = true;
6243 : : #else
6244 : 105550 : info.omit_relative = false;
6245 : : #endif
6246 : 105550 : info.separate_options = false;
6247 : 105550 : info.realpaths = false;
6248 : :
6249 : 105550 : for_each_path (&startfile_prefixes, true, 0, info);
6250 : : }
6251 : 105550 : break;
6252 : :
6253 : 0 : case 'P':
6254 : 0 : {
6255 : 0 : struct spec_path info;
6256 : :
6257 : 0 : info.option = RUNPATH_OPTION;
6258 : 0 : info.append_len = 0;
6259 : 0 : info.omit_relative = false;
6260 : 0 : info.separate_options = true;
6261 : : /* We want to embed the actual paths that have the libraries. */
6262 : 0 : info.realpaths = true;
6263 : :
6264 : 0 : for_each_path (&startfile_prefixes, true, 0, info);
6265 : : }
6266 : 0 : break;
6267 : :
6268 : : case 'e':
6269 : : /* %efoo means report an error with `foo' as error message
6270 : : and don't execute any more commands for this file. */
6271 : : {
6272 : : const char *q = p;
6273 : : char *buf;
6274 : 0 : while (*p != 0 && *p != '\n')
6275 : 0 : p++;
6276 : 0 : buf = (char *) alloca (p - q + 1);
6277 : 0 : strncpy (buf, q, p - q);
6278 : 0 : buf[p - q] = 0;
6279 : 0 : error ("%s", _(buf));
6280 : 0 : return -1;
6281 : : }
6282 : : break;
6283 : : case 'n':
6284 : : /* %nfoo means report a notice with `foo' on stderr. */
6285 : : {
6286 : : const char *q = p;
6287 : : char *buf;
6288 : 0 : while (*p != 0 && *p != '\n')
6289 : 0 : p++;
6290 : 0 : buf = (char *) alloca (p - q + 1);
6291 : 0 : strncpy (buf, q, p - q);
6292 : 0 : buf[p - q] = 0;
6293 : 0 : inform (UNKNOWN_LOCATION, "%s", _(buf));
6294 : 0 : if (*p)
6295 : 0 : p++;
6296 : : }
6297 : : break;
6298 : :
6299 : 881 : case 'j':
6300 : 881 : {
6301 : 881 : struct stat st;
6302 : :
6303 : : /* If save_temps_flag is off, and the HOST_BIT_BUCKET is
6304 : : defined, and it is not a directory, and it is
6305 : : writable, use it. Otherwise, treat this like any
6306 : : other temporary file. */
6307 : :
6308 : 881 : if ((!save_temps_flag)
6309 : 881 : && (stat (HOST_BIT_BUCKET, &st) == 0) && (!S_ISDIR (st.st_mode))
6310 : 1762 : && (access (HOST_BIT_BUCKET, W_OK) == 0))
6311 : : {
6312 : 881 : obstack_grow (&obstack, HOST_BIT_BUCKET,
6313 : : strlen (HOST_BIT_BUCKET));
6314 : 881 : delete_this_arg = 0;
6315 : 881 : arg_going = 1;
6316 : 881 : break;
6317 : : }
6318 : : }
6319 : 0 : goto create_temp_file;
6320 : 168669 : case '|':
6321 : 168669 : if (use_pipes)
6322 : : {
6323 : 0 : obstack_1grow (&obstack, '-');
6324 : 0 : delete_this_arg = 0;
6325 : 0 : arg_going = 1;
6326 : :
6327 : : /* consume suffix */
6328 : 0 : while (*p == '.' || ISALNUM ((unsigned char) *p))
6329 : 0 : p++;
6330 : 0 : if (p[0] == '%' && p[1] == 'O')
6331 : 0 : p += 2;
6332 : :
6333 : : break;
6334 : : }
6335 : 168669 : goto create_temp_file;
6336 : 163025 : case 'm':
6337 : 163025 : if (use_pipes)
6338 : : {
6339 : : /* consume suffix */
6340 : 0 : while (*p == '.' || ISALNUM ((unsigned char) *p))
6341 : 0 : p++;
6342 : 0 : if (p[0] == '%' && p[1] == 'O')
6343 : 0 : p += 2;
6344 : :
6345 : : break;
6346 : : }
6347 : 163025 : goto create_temp_file;
6348 : 523121 : case 'g':
6349 : 523121 : case 'u':
6350 : 523121 : case 'U':
6351 : 523121 : create_temp_file:
6352 : 523121 : {
6353 : 523121 : struct temp_name *t;
6354 : 523121 : int suffix_length;
6355 : 523121 : const char *suffix = p;
6356 : 523121 : char *saved_suffix = NULL;
6357 : :
6358 : 1558725 : while (*p == '.' || ISALNUM ((unsigned char) *p))
6359 : 1035604 : p++;
6360 : 523121 : suffix_length = p - suffix;
6361 : 523121 : if (p[0] == '%' && p[1] == 'O')
6362 : : {
6363 : 98109 : p += 2;
6364 : : /* We don't support extra suffix characters after %O. */
6365 : 98109 : if (*p == '.' || ISALNUM ((unsigned char) *p))
6366 : 0 : fatal_error (input_location,
6367 : : "spec %qs has invalid %<%%0%c%>", spec, *p);
6368 : 98109 : if (suffix_length == 0)
6369 : : suffix = TARGET_OBJECT_SUFFIX;
6370 : : else
6371 : : {
6372 : 0 : saved_suffix
6373 : 0 : = XNEWVEC (char, suffix_length
6374 : : + strlen (TARGET_OBJECT_SUFFIX) + 1);
6375 : 0 : strncpy (saved_suffix, suffix, suffix_length);
6376 : 0 : strcpy (saved_suffix + suffix_length,
6377 : : TARGET_OBJECT_SUFFIX);
6378 : : }
6379 : 98109 : suffix_length += strlen (TARGET_OBJECT_SUFFIX);
6380 : : }
6381 : :
6382 : 523121 : if (compare_debug < 0)
6383 : : {
6384 : 610 : suffix = concat (".gk", suffix, NULL);
6385 : 610 : suffix_length += 3;
6386 : : }
6387 : :
6388 : : /* If -save-temps was specified, use that for the
6389 : : temp file. */
6390 : 523121 : if (save_temps_flag)
6391 : : {
6392 : 1194 : char *tmp;
6393 : 1194 : bool adjusted_suffix = false;
6394 : 1194 : if (suffix_length
6395 : 1194 : && !outbase_length && !basename_length
6396 : 224 : && !dumpdir_trailing_dash_added)
6397 : : {
6398 : 20 : adjusted_suffix = true;
6399 : 20 : suffix++;
6400 : 20 : suffix_length--;
6401 : : }
6402 : 1194 : temp_filename_length
6403 : 1194 : = dumpdir_length + suffix_length + 1;
6404 : 1194 : if (outbase_length)
6405 : 72 : temp_filename_length += outbase_length;
6406 : : else
6407 : 1122 : temp_filename_length += basename_length;
6408 : 1194 : tmp = (char *) alloca (temp_filename_length);
6409 : 1194 : if (dumpdir_length)
6410 : 1036 : memcpy (tmp, dumpdir, dumpdir_length);
6411 : 1194 : if (outbase_length)
6412 : 72 : memcpy (tmp + dumpdir_length, outbase,
6413 : : outbase_length);
6414 : 1122 : else if (basename_length)
6415 : 898 : memcpy (tmp + dumpdir_length, input_basename,
6416 : : basename_length);
6417 : 1194 : memcpy (tmp + temp_filename_length - suffix_length - 1,
6418 : : suffix, suffix_length);
6419 : 1194 : if (adjusted_suffix)
6420 : : {
6421 : 20 : adjusted_suffix = false;
6422 : 20 : suffix--;
6423 : 20 : suffix_length++;
6424 : : }
6425 : 1194 : tmp[temp_filename_length - 1] = '\0';
6426 : 1194 : temp_filename = tmp;
6427 : :
6428 : 1194 : if (filename_cmp (temp_filename, gcc_input_filename) != 0)
6429 : : {
6430 : : #ifndef HOST_LACKS_INODE_NUMBERS
6431 : 1194 : struct stat st_temp;
6432 : :
6433 : : /* Note, set_input() resets input_stat_set to 0. */
6434 : 1194 : if (input_stat_set == 0)
6435 : : {
6436 : 557 : input_stat_set = stat (gcc_input_filename,
6437 : : &input_stat);
6438 : 557 : if (input_stat_set >= 0)
6439 : 557 : input_stat_set = 1;
6440 : : }
6441 : :
6442 : : /* If we have the stat for the gcc_input_filename
6443 : : and we can do the stat for the temp_filename
6444 : : then the they could still refer to the same
6445 : : file if st_dev/st_ino's are the same. */
6446 : 1194 : if (input_stat_set != 1
6447 : 1194 : || stat (temp_filename, &st_temp) < 0
6448 : 365 : || input_stat.st_dev != st_temp.st_dev
6449 : 1208 : || input_stat.st_ino != st_temp.st_ino)
6450 : : #else
6451 : : /* Just compare canonical pathnames. */
6452 : : char* input_realname = lrealpath (gcc_input_filename);
6453 : : char* temp_realname = lrealpath (temp_filename);
6454 : : bool files_differ = filename_cmp (input_realname, temp_realname);
6455 : : free (input_realname);
6456 : : free (temp_realname);
6457 : : if (files_differ)
6458 : : #endif
6459 : : {
6460 : 1194 : temp_filename
6461 : 1194 : = save_string (temp_filename,
6462 : : temp_filename_length - 1);
6463 : 1194 : obstack_grow (&obstack, temp_filename,
6464 : : temp_filename_length);
6465 : 1194 : arg_going = 1;
6466 : 1194 : delete_this_arg = 0;
6467 : 1194 : break;
6468 : : }
6469 : : }
6470 : : }
6471 : :
6472 : : /* See if we already have an association of %g/%u/%U and
6473 : : suffix. */
6474 : 892949 : for (t = temp_names; t; t = t->next)
6475 : 541041 : if (t->length == suffix_length
6476 : 361962 : && strncmp (t->suffix, suffix, suffix_length) == 0
6477 : 173959 : && t->unique == (c == 'u' || c == 'U' || c == 'j'))
6478 : : break;
6479 : :
6480 : : /* Make a new association if needed. %u and %j
6481 : : require one. */
6482 : 521927 : if (t == 0 || c == 'u' || c == 'j')
6483 : : {
6484 : 355654 : if (t == 0)
6485 : : {
6486 : 351908 : t = XNEW (struct temp_name);
6487 : 351908 : t->next = temp_names;
6488 : 351908 : temp_names = t;
6489 : : }
6490 : 355654 : t->length = suffix_length;
6491 : 355654 : if (saved_suffix)
6492 : : {
6493 : 0 : t->suffix = saved_suffix;
6494 : 0 : saved_suffix = NULL;
6495 : : }
6496 : : else
6497 : 355654 : t->suffix = save_string (suffix, suffix_length);
6498 : 355654 : t->unique = (c == 'u' || c == 'U' || c == 'j');
6499 : 355654 : temp_filename = make_temp_file (t->suffix);
6500 : 355654 : temp_filename_length = strlen (temp_filename);
6501 : 355654 : t->filename = temp_filename;
6502 : 355654 : t->filename_length = temp_filename_length;
6503 : : }
6504 : :
6505 : 521927 : free (saved_suffix);
6506 : :
6507 : 521927 : obstack_grow (&obstack, t->filename, t->filename_length);
6508 : 521927 : delete_this_arg = 1;
6509 : : }
6510 : 521927 : arg_going = 1;
6511 : 521927 : break;
6512 : :
6513 : 288618 : case 'i':
6514 : 288618 : if (combine_inputs)
6515 : : {
6516 : : /* We are going to expand `%i' into `@FILE', where FILE
6517 : : is a newly-created temporary filename. The filenames
6518 : : that would usually be expanded in place of %o will be
6519 : : written to the temporary file. */
6520 : 31595 : if (at_file_supplied)
6521 : 13337 : open_at_file ();
6522 : :
6523 : 76841 : for (i = 0; (int) i < n_infiles; i++)
6524 : 90492 : if (compile_input_file_p (&infiles[i]))
6525 : : {
6526 : 40694 : store_arg (infiles[i].name, 0, 0);
6527 : 40694 : infiles[i].compiled = true;
6528 : : }
6529 : :
6530 : 31595 : if (at_file_supplied)
6531 : 13337 : close_at_file ();
6532 : : }
6533 : : else
6534 : : {
6535 : 257023 : obstack_grow (&obstack, gcc_input_filename,
6536 : : input_filename_length);
6537 : 257023 : arg_going = 1;
6538 : : }
6539 : : break;
6540 : :
6541 : 223197 : case 'I':
6542 : 223197 : {
6543 : 223197 : struct spec_path info;
6544 : :
6545 : 223197 : if (multilib_dir)
6546 : : {
6547 : 5986 : do_spec_1 ("-imultilib", 1, NULL);
6548 : : /* Make this a separate argument. */
6549 : 5986 : do_spec_1 (" ", 0, NULL);
6550 : 5986 : do_spec_1 (multilib_dir, 1, NULL);
6551 : 5986 : do_spec_1 (" ", 0, NULL);
6552 : : }
6553 : :
6554 : 223197 : if (multiarch_dir)
6555 : : {
6556 : 0 : do_spec_1 ("-imultiarch", 1, NULL);
6557 : : /* Make this a separate argument. */
6558 : 0 : do_spec_1 (" ", 0, NULL);
6559 : 0 : do_spec_1 (multiarch_dir, 1, NULL);
6560 : 0 : do_spec_1 (" ", 0, NULL);
6561 : : }
6562 : :
6563 : 223197 : if (gcc_exec_prefix)
6564 : : {
6565 : 223197 : do_spec_1 ("-iprefix", 1, NULL);
6566 : : /* Make this a separate argument. */
6567 : 223197 : do_spec_1 (" ", 0, NULL);
6568 : 223197 : do_spec_1 (gcc_exec_prefix, 1, NULL);
6569 : 223197 : do_spec_1 (" ", 0, NULL);
6570 : : }
6571 : :
6572 : 223197 : if (target_system_root_changed ||
6573 : 223197 : (target_system_root && target_sysroot_hdrs_suffix))
6574 : : {
6575 : 0 : do_spec_1 ("-isysroot", 1, NULL);
6576 : : /* Make this a separate argument. */
6577 : 0 : do_spec_1 (" ", 0, NULL);
6578 : 0 : do_spec_1 (target_system_root, 1, NULL);
6579 : 0 : if (target_sysroot_hdrs_suffix)
6580 : 0 : do_spec_1 (target_sysroot_hdrs_suffix, 1, NULL);
6581 : 0 : do_spec_1 (" ", 0, NULL);
6582 : : }
6583 : :
6584 : 223197 : info.option = "-isystem";
6585 : 223197 : info.append = "include";
6586 : 223197 : info.append_len = strlen (info.append);
6587 : 223197 : info.omit_relative = false;
6588 : 223197 : info.separate_options = true;
6589 : 223197 : info.realpaths = false;
6590 : :
6591 : 223197 : for_each_path (&include_prefixes, false, info.append_len, info);
6592 : :
6593 : 223197 : info.append = "include-fixed";
6594 : 223197 : if (*sysroot_hdrs_suffix_spec)
6595 : 0 : info.append = concat (info.append, dir_separator_str,
6596 : : multilib_dir, NULL);
6597 : 223197 : else if (multiarch_dir)
6598 : : {
6599 : : /* For multiarch, search include-fixed/<multiarch-dir>
6600 : : before include-fixed. */
6601 : 0 : info.append = concat (info.append, dir_separator_str,
6602 : : multiarch_dir, NULL);
6603 : 0 : info.append_len = strlen (info.append);
6604 : 0 : for_each_path (&include_prefixes, false,
6605 : : info.append_len, info);
6606 : :
6607 : 0 : info.append = "include-fixed";
6608 : : }
6609 : 223197 : info.append_len = strlen (info.append);
6610 : 223197 : for_each_path (&include_prefixes, false, info.append_len, info);
6611 : : }
6612 : 223197 : break;
6613 : :
6614 : 95903 : case 'o':
6615 : : /* We are going to expand `%o' into `@FILE', where FILE
6616 : : is a newly-created temporary filename. The filenames
6617 : : that would usually be expanded in place of %o will be
6618 : : written to the temporary file. */
6619 : 95903 : if (at_file_supplied)
6620 : 6 : open_at_file ();
6621 : :
6622 : 425455 : for (i = 0; i < n_infiles + lang_specific_extra_outfiles; i++)
6623 : 329552 : if (outfiles[i])
6624 : 329512 : store_arg (outfiles[i], 0, 0);
6625 : :
6626 : 95903 : if (at_file_supplied)
6627 : 6 : close_at_file ();
6628 : : break;
6629 : :
6630 : 3989 : case 'O':
6631 : 3989 : obstack_grow (&obstack, TARGET_OBJECT_SUFFIX, strlen (TARGET_OBJECT_SUFFIX));
6632 : 3989 : arg_going = 1;
6633 : 3989 : break;
6634 : :
6635 : 541159 : case 's':
6636 : 541159 : this_is_library_file = 1;
6637 : 541159 : break;
6638 : :
6639 : 0 : case 'T':
6640 : 0 : this_is_linker_script = 1;
6641 : 0 : break;
6642 : :
6643 : 460 : case 'V':
6644 : 460 : outfiles[input_file_number] = NULL;
6645 : 460 : break;
6646 : :
6647 : 100871 : case 'w':
6648 : 100871 : this_is_output_file = 1;
6649 : 100871 : break;
6650 : :
6651 : 174223 : case 'W':
6652 : 174223 : {
6653 : 174223 : unsigned int cur_index = argbuf.length ();
6654 : : /* Handle the {...} following the %W. */
6655 : 174223 : if (*p != '{')
6656 : 0 : fatal_error (input_location,
6657 : : "spec %qs has invalid %<%%W%c%>", spec, *p);
6658 : 174223 : p = handle_braces (p + 1);
6659 : 174223 : if (p == 0)
6660 : : return -1;
6661 : 174223 : end_going_arg ();
6662 : : /* If any args were output, mark the last one for deletion
6663 : : on failure. */
6664 : 348446 : if (argbuf.length () != cur_index)
6665 : 171051 : record_temp_file (argbuf.last (), 0, 1);
6666 : : break;
6667 : : }
6668 : :
6669 : 304121 : case '@':
6670 : : /* Handle the {...} following the %@. */
6671 : 304121 : if (*p != '{')
6672 : 0 : fatal_error (input_location,
6673 : : "spec %qs has invalid %<%%@%c%>", spec, *p);
6674 : 304121 : if (at_file_supplied)
6675 : 15 : open_at_file ();
6676 : 304121 : p = handle_braces (p + 1);
6677 : 304121 : if (at_file_supplied)
6678 : 15 : close_at_file ();
6679 : 304121 : if (p == 0)
6680 : : return -1;
6681 : : break;
6682 : :
6683 : : /* %x{OPTION} records OPTION for %X to output. */
6684 : 0 : case 'x':
6685 : 0 : {
6686 : 0 : const char *p1 = p;
6687 : 0 : char *string;
6688 : :
6689 : : /* Skip past the option value and make a copy. */
6690 : 0 : if (*p != '{')
6691 : 0 : fatal_error (input_location,
6692 : : "spec %qs has invalid %<%%x%c%>", spec, *p);
6693 : 0 : while (*p++ != '}')
6694 : : ;
6695 : 0 : string = save_string (p1 + 1, p - p1 - 2);
6696 : :
6697 : : /* See if we already recorded this option. */
6698 : 0 : for (const char *opt : linker_options)
6699 : 0 : if (! strcmp (string, opt))
6700 : : {
6701 : 0 : free (string);
6702 : 0 : return 0;
6703 : : }
6704 : :
6705 : : /* This option is new; add it. */
6706 : 0 : add_linker_option (string, strlen (string));
6707 : 0 : free (string);
6708 : : }
6709 : 0 : break;
6710 : :
6711 : : /* Dump out the options accumulated previously using %x. */
6712 : 95903 : case 'X':
6713 : 95903 : do_specs_vec (linker_options);
6714 : 95903 : break;
6715 : :
6716 : : /* Dump out the options accumulated previously using -Wa,. */
6717 : 164867 : case 'Y':
6718 : 164867 : do_specs_vec (assembler_options);
6719 : 164867 : break;
6720 : :
6721 : : /* Dump out the options accumulated previously using -Wp,. */
6722 : 208218 : case 'Z':
6723 : 208218 : do_specs_vec (preprocessor_options);
6724 : 208218 : break;
6725 : :
6726 : : /* Here are digits and numbers that just process
6727 : : a certain constant string as a spec. */
6728 : :
6729 : 285570 : case '1':
6730 : 285570 : value = do_spec_1 (cc1_spec, 0, NULL);
6731 : 285570 : if (value != 0)
6732 : : return value;
6733 : : break;
6734 : :
6735 : 96557 : case '2':
6736 : 96557 : value = do_spec_1 (cc1plus_spec, 0, NULL);
6737 : 96557 : if (value != 0)
6738 : : return value;
6739 : : break;
6740 : :
6741 : 164867 : case 'a':
6742 : 164867 : value = do_spec_1 (asm_spec, 0, NULL);
6743 : 164867 : if (value != 0)
6744 : : return value;
6745 : : break;
6746 : :
6747 : 164867 : case 'A':
6748 : 164867 : value = do_spec_1 (asm_final_spec, 0, NULL);
6749 : 164867 : if (value != 0)
6750 : : return value;
6751 : : break;
6752 : :
6753 : 208218 : case 'C':
6754 : 208218 : {
6755 : 111787 : const char *const spec
6756 : 208218 : = (input_file_compiler->cpp_spec
6757 : 208218 : ? input_file_compiler->cpp_spec
6758 : : : cpp_spec);
6759 : 208218 : value = do_spec_1 (spec, 0, NULL);
6760 : 208218 : if (value != 0)
6761 : : return value;
6762 : : }
6763 : : break;
6764 : :
6765 : 95696 : case 'E':
6766 : 95696 : value = do_spec_1 (endfile_spec, 0, NULL);
6767 : 95696 : if (value != 0)
6768 : : return value;
6769 : : break;
6770 : :
6771 : 95903 : case 'l':
6772 : 95903 : value = do_spec_1 (link_spec, 0, NULL);
6773 : 95903 : if (value != 0)
6774 : : return value;
6775 : : break;
6776 : :
6777 : 185986 : case 'L':
6778 : 185986 : value = do_spec_1 (lib_spec, 0, NULL);
6779 : 185986 : if (value != 0)
6780 : : return value;
6781 : : break;
6782 : :
6783 : 0 : case 'M':
6784 : 0 : if (multilib_os_dir == NULL)
6785 : 0 : obstack_1grow (&obstack, '.');
6786 : : else
6787 : 0 : obstack_grow (&obstack, multilib_os_dir,
6788 : : strlen (multilib_os_dir));
6789 : : break;
6790 : :
6791 : 371776 : case 'G':
6792 : 371776 : value = do_spec_1 (libgcc_spec, 0, NULL);
6793 : 371776 : if (value != 0)
6794 : : return value;
6795 : : break;
6796 : :
6797 : 0 : case 'R':
6798 : : /* We assume there is a directory
6799 : : separator at the end of this string. */
6800 : 0 : if (target_system_root)
6801 : : {
6802 : 0 : obstack_grow (&obstack, target_system_root,
6803 : : strlen (target_system_root));
6804 : 0 : if (target_sysroot_suffix)
6805 : 0 : obstack_grow (&obstack, target_sysroot_suffix,
6806 : : strlen (target_sysroot_suffix));
6807 : : }
6808 : : break;
6809 : :
6810 : 95699 : case 'S':
6811 : 95699 : value = do_spec_1 (startfile_spec, 0, NULL);
6812 : 95699 : if (value != 0)
6813 : : return value;
6814 : : break;
6815 : :
6816 : : /* Here we define characters other than letters and digits. */
6817 : :
6818 : 40218062 : case '{':
6819 : 40218062 : p = handle_braces (p);
6820 : 40218041 : if (p == 0)
6821 : : return -1;
6822 : : break;
6823 : :
6824 : 438786 : case ':':
6825 : 438786 : p = handle_spec_function (p, NULL, soft_matched_part);
6826 : 438783 : if (p == 0)
6827 : : return -1;
6828 : : break;
6829 : :
6830 : 0 : case '%':
6831 : 0 : obstack_1grow (&obstack, '%');
6832 : 0 : break;
6833 : :
6834 : : case '.':
6835 : : {
6836 : : unsigned len = 0;
6837 : :
6838 : 11778 : while (p[len] && p[len] != ' ' && p[len] != '%')
6839 : 5907 : len++;
6840 : 5871 : suffix_subst = save_string (p - 1, len + 1);
6841 : 5871 : p += len;
6842 : : }
6843 : 5871 : break;
6844 : :
6845 : : /* Henceforth ignore the option(s) matching the pattern
6846 : : after the %<. */
6847 : 1471436 : case '<':
6848 : 1471436 : case '>':
6849 : 1471436 : {
6850 : 1471436 : unsigned len = 0;
6851 : 1471436 : int have_wildcard = 0;
6852 : 1471436 : int i;
6853 : 1471436 : int switch_option;
6854 : :
6855 : 1471436 : if (c == '>')
6856 : 1471436 : switch_option = SWITCH_IGNORE | SWITCH_KEEP_FOR_GCC;
6857 : : else
6858 : 1471414 : switch_option = SWITCH_IGNORE;
6859 : :
6860 : 17768463 : while (p[len] && p[len] != ' ' && p[len] != '\t')
6861 : 16297027 : len++;
6862 : :
6863 : 1471436 : if (p[len-1] == '*')
6864 : 15730 : have_wildcard = 1;
6865 : :
6866 : 35107160 : for (i = 0; i < n_switches; i++)
6867 : 33635724 : if (!strncmp (switches[i].part1, p, len - have_wildcard)
6868 : 49931 : && (have_wildcard || switches[i].part1[len] == '\0'))
6869 : : {
6870 : 49662 : switches[i].live_cond |= switch_option;
6871 : : /* User switch be validated from validate_all_switches.
6872 : : when the definition is seen from the spec file.
6873 : : If not defined anywhere, will be rejected. */
6874 : 49662 : if (switches[i].known)
6875 : 49662 : switches[i].validated = true;
6876 : : }
6877 : :
6878 : : p += len;
6879 : : }
6880 : : break;
6881 : :
6882 : 6804 : case '*':
6883 : 6804 : if (soft_matched_part)
6884 : : {
6885 : 6804 : if (soft_matched_part[0])
6886 : 360 : do_spec_1 (soft_matched_part, 1, NULL);
6887 : : /* Only insert a space after the substitution if it is at the
6888 : : end of the current sequence. So if:
6889 : :
6890 : : "%{foo=*:bar%*}%{foo=*:one%*two}"
6891 : :
6892 : : matches -foo=hello then it will produce:
6893 : :
6894 : : barhello onehellotwo
6895 : : */
6896 : 6804 : if (*p == 0 || *p == '}')
6897 : 6804 : do_spec_1 (" ", 0, NULL);
6898 : : }
6899 : : else
6900 : : /* Catch the case where a spec string contains something like
6901 : : '%{foo:%*}'. i.e. there is no * in the pattern on the left
6902 : : hand side of the :. */
6903 : 0 : error ("spec failure: %<%%*%> has not been initialized by pattern match");
6904 : : break;
6905 : :
6906 : : /* Process a string found as the value of a spec given by name.
6907 : : This feature allows individual machine descriptions
6908 : : to add and use their own specs. */
6909 : : case '(':
6910 : : {
6911 : 33381930 : const char *name = p;
6912 : : struct spec_list *sl;
6913 : : int len;
6914 : :
6915 : : /* The string after the S/P is the name of a spec that is to be
6916 : : processed. */
6917 : 33381930 : while (*p && *p != ')')
6918 : 30808598 : p++;
6919 : :
6920 : : /* See if it's in the list. */
6921 : 35345398 : for (len = p - name, sl = specs; sl; sl = sl->next)
6922 : 35345398 : if (sl->name_len == len && !strncmp (sl->name, name, len))
6923 : : {
6924 : 2573332 : name = *(sl->ptr_spec);
6925 : : #ifdef DEBUG_SPECS
6926 : : fnotice (stderr, "Processing spec (%s), which is '%s'\n",
6927 : : sl->name, name);
6928 : : #endif
6929 : 2573332 : break;
6930 : : }
6931 : :
6932 : 2573332 : if (sl)
6933 : : {
6934 : 2573332 : value = do_spec_1 (name, 0, NULL);
6935 : 2573332 : if (value != 0)
6936 : : return value;
6937 : : }
6938 : :
6939 : : /* Discard the closing paren. */
6940 : 2567688 : if (*p)
6941 : 2567688 : p++;
6942 : : }
6943 : : break;
6944 : :
6945 : 9 : case '"':
6946 : : /* End a previous argument, if there is one, then issue an
6947 : : empty argument. */
6948 : 9 : end_going_arg ();
6949 : 9 : arg_going = 1;
6950 : 9 : end_going_arg ();
6951 : 9 : break;
6952 : :
6953 : 0 : default:
6954 : 0 : error ("spec failure: unrecognized spec option %qc", c);
6955 : 0 : break;
6956 : : }
6957 : : break;
6958 : :
6959 : 0 : case '\\':
6960 : : /* Backslash: treat next character as ordinary. */
6961 : 0 : c = *p++;
6962 : :
6963 : : /* When adding more cases that previously matched default, make
6964 : : sure to adjust quote_spec_char_p as well. */
6965 : :
6966 : : /* Fall through. */
6967 : 367227799 : default:
6968 : : /* Ordinary character: put it into the current argument. */
6969 : 367227799 : obstack_1grow (&obstack, c);
6970 : 367227799 : arg_going = 1;
6971 : : }
6972 : :
6973 : : /* End of string. If we are processing a spec function, we need to
6974 : : end any pending argument. */
6975 : 52709997 : if (processing_spec_function)
6976 : 4517213 : end_going_arg ();
6977 : :
6978 : : return 0;
6979 : : }
6980 : :
6981 : : /* Look up a spec function. */
6982 : :
6983 : : static const struct spec_function *
6984 : 2077385 : lookup_spec_function (const char *name)
6985 : : {
6986 : 2077385 : const struct spec_function *sf;
6987 : :
6988 : 24862179 : for (sf = static_spec_functions; sf->name != NULL; sf++)
6989 : 24862179 : if (strcmp (sf->name, name) == 0)
6990 : : return sf;
6991 : :
6992 : : return NULL;
6993 : : }
6994 : :
6995 : : /* Evaluate a spec function. */
6996 : :
6997 : : static const char *
6998 : 2077385 : eval_spec_function (const char *func, const char *args,
6999 : : const char *soft_matched_part)
7000 : : {
7001 : 2077385 : const struct spec_function *sf;
7002 : 2077385 : const char *funcval;
7003 : :
7004 : : /* Saved spec processing context. */
7005 : 2077385 : vec<const_char_p> save_argbuf;
7006 : :
7007 : 2077385 : int save_arg_going;
7008 : 2077385 : int save_delete_this_arg;
7009 : 2077385 : int save_this_is_output_file;
7010 : 2077385 : int save_this_is_library_file;
7011 : 2077385 : int save_input_from_pipe;
7012 : 2077385 : int save_this_is_linker_script;
7013 : 2077385 : const char *save_suffix_subst;
7014 : :
7015 : 2077385 : int save_growing_size;
7016 : 2077385 : void *save_growing_value = NULL;
7017 : :
7018 : 2077385 : sf = lookup_spec_function (func);
7019 : 2077385 : if (sf == NULL)
7020 : 0 : fatal_error (input_location, "unknown spec function %qs", func);
7021 : :
7022 : : /* Push the spec processing context. */
7023 : 2077385 : save_argbuf = argbuf;
7024 : :
7025 : 2077385 : save_arg_going = arg_going;
7026 : 2077385 : save_delete_this_arg = delete_this_arg;
7027 : 2077385 : save_this_is_output_file = this_is_output_file;
7028 : 2077385 : save_this_is_library_file = this_is_library_file;
7029 : 2077385 : save_this_is_linker_script = this_is_linker_script;
7030 : 2077385 : save_input_from_pipe = input_from_pipe;
7031 : 2077385 : save_suffix_subst = suffix_subst;
7032 : :
7033 : : /* If we have some object growing now, finalize it so the args and function
7034 : : eval proceed from a cleared context. This is needed to prevent the first
7035 : : constructed arg from mistakenly including the growing value. We'll push
7036 : : this value back on the obstack once the function evaluation is done, to
7037 : : restore a consistent processing context for our caller. This is fine as
7038 : : the address of growing objects isn't guaranteed to remain stable until
7039 : : they are finalized, and we expect this situation to be rare enough for
7040 : : the extra copy not to be an issue. */
7041 : 2077385 : save_growing_size = obstack_object_size (&obstack);
7042 : 2077385 : if (save_growing_size > 0)
7043 : 42498 : save_growing_value = obstack_finish (&obstack);
7044 : :
7045 : : /* Create a new spec processing context, and build the function
7046 : : arguments. */
7047 : :
7048 : 2077385 : alloc_args ();
7049 : 2077385 : if (do_spec_2 (args, soft_matched_part) < 0)
7050 : 0 : fatal_error (input_location, "error in arguments to spec function %qs",
7051 : : func);
7052 : :
7053 : : /* argbuf_index is an index for the next argument to be inserted, and
7054 : : so contains the count of the args already inserted. */
7055 : :
7056 : 6232155 : funcval = (*sf->func) (argbuf.length (),
7057 : : argbuf.address ());
7058 : :
7059 : : /* Pop the spec processing context. */
7060 : 2077382 : argbuf.release ();
7061 : 2077382 : argbuf = save_argbuf;
7062 : :
7063 : 2077382 : arg_going = save_arg_going;
7064 : 2077382 : delete_this_arg = save_delete_this_arg;
7065 : 2077382 : this_is_output_file = save_this_is_output_file;
7066 : 2077382 : this_is_library_file = save_this_is_library_file;
7067 : 2077382 : this_is_linker_script = save_this_is_linker_script;
7068 : 2077382 : input_from_pipe = save_input_from_pipe;
7069 : 2077382 : suffix_subst = save_suffix_subst;
7070 : :
7071 : 2077382 : if (save_growing_size > 0)
7072 : 42498 : obstack_grow (&obstack, save_growing_value, save_growing_size);
7073 : :
7074 : 2077382 : return funcval;
7075 : : }
7076 : :
7077 : : /* Handle a spec function call of the form:
7078 : :
7079 : : %:function(args)
7080 : :
7081 : : ARGS is processed as a spec in a separate context and split into an
7082 : : argument vector in the normal fashion. The function returns a string
7083 : : containing a spec which we then process in the caller's context, or
7084 : : NULL if no processing is required.
7085 : :
7086 : : If RETVAL_NONNULL is not NULL, then store a bool whether function
7087 : : returned non-NULL.
7088 : :
7089 : : SOFT_MATCHED_PART holds the current value of a matched * pattern, which
7090 : : may be re-expanded with a %* as part of the function arguments. */
7091 : :
7092 : : static const char *
7093 : 2077385 : handle_spec_function (const char *p, bool *retval_nonnull,
7094 : : const char *soft_matched_part)
7095 : : {
7096 : 2077385 : char *func, *args;
7097 : 2077385 : const char *endp, *funcval;
7098 : 2077385 : int count;
7099 : :
7100 : 2077385 : processing_spec_function++;
7101 : :
7102 : : /* Get the function name. */
7103 : 19293989 : for (endp = p; *endp != '\0'; endp++)
7104 : : {
7105 : 19293989 : if (*endp == '(') /* ) */
7106 : : break;
7107 : : /* Only allow [A-Za-z0-9], -, and _ in function names. */
7108 : 17216604 : if (!ISALNUM (*endp) && !(*endp == '-' || *endp == '_'))
7109 : 0 : fatal_error (input_location, "malformed spec function name");
7110 : : }
7111 : 2077385 : if (*endp != '(') /* ) */
7112 : 0 : fatal_error (input_location, "no arguments for spec function");
7113 : 2077385 : func = save_string (p, endp - p);
7114 : 2077385 : p = ++endp;
7115 : :
7116 : : /* Get the arguments. */
7117 : 25298658 : for (count = 0; *endp != '\0'; endp++)
7118 : : {
7119 : : /* ( */
7120 : 25298658 : if (*endp == ')')
7121 : : {
7122 : 2167677 : if (count == 0)
7123 : : break;
7124 : 90292 : count--;
7125 : : }
7126 : 23130981 : else if (*endp == '(') /* ) */
7127 : 90292 : count++;
7128 : : }
7129 : : /* ( */
7130 : 2077385 : if (*endp != ')')
7131 : 0 : fatal_error (input_location, "malformed spec function arguments");
7132 : 2077385 : args = save_string (p, endp - p);
7133 : 2077385 : p = ++endp;
7134 : :
7135 : : /* p now points to just past the end of the spec function expression. */
7136 : :
7137 : 2077385 : funcval = eval_spec_function (func, args, soft_matched_part);
7138 : 2077382 : if (funcval != NULL && do_spec_1 (funcval, 0, NULL) < 0)
7139 : : p = NULL;
7140 : 2077382 : if (retval_nonnull)
7141 : 1638599 : *retval_nonnull = funcval != NULL;
7142 : :
7143 : 2077382 : free (func);
7144 : 2077382 : free (args);
7145 : :
7146 : 2077382 : processing_spec_function--;
7147 : :
7148 : 2077382 : return p;
7149 : : }
7150 : :
7151 : : /* Inline subroutine of handle_braces. Returns true if the current
7152 : : input suffix matches the atom bracketed by ATOM and END_ATOM. */
7153 : : static inline bool
7154 : 0 : input_suffix_matches (const char *atom, const char *end_atom)
7155 : : {
7156 : 0 : return (input_suffix
7157 : 0 : && !strncmp (input_suffix, atom, end_atom - atom)
7158 : 0 : && input_suffix[end_atom - atom] == '\0');
7159 : : }
7160 : :
7161 : : /* Subroutine of handle_braces. Returns true if the current
7162 : : input file's spec name matches the atom bracketed by ATOM and END_ATOM. */
7163 : : static bool
7164 : 0 : input_spec_matches (const char *atom, const char *end_atom)
7165 : : {
7166 : 0 : return (input_file_compiler
7167 : 0 : && input_file_compiler->suffix
7168 : 0 : && input_file_compiler->suffix[0] != '\0'
7169 : 0 : && !strncmp (input_file_compiler->suffix + 1, atom,
7170 : 0 : end_atom - atom)
7171 : 0 : && input_file_compiler->suffix[end_atom - atom + 1] == '\0');
7172 : : }
7173 : :
7174 : : /* Subroutine of handle_braces. Returns true if a switch
7175 : : matching the atom bracketed by ATOM and END_ATOM appeared on the
7176 : : command line. */
7177 : : static bool
7178 : 38660159 : switch_matches (const char *atom, const char *end_atom, int starred)
7179 : : {
7180 : 38660159 : int i;
7181 : 38660159 : int len = end_atom - atom;
7182 : 38660159 : int plen = starred ? len : -1;
7183 : :
7184 : 907887014 : for (i = 0; i < n_switches; i++)
7185 : 870598898 : if (!strncmp (switches[i].part1, atom, len)
7186 : 2317590 : && (starred || switches[i].part1[len] == '\0')
7187 : 871971558 : && check_live_switch (i, plen))
7188 : : return true;
7189 : :
7190 : : /* Check if a switch with separated form matching the atom.
7191 : : We check -D and -U switches. */
7192 : 869226856 : else if (switches[i].args != 0)
7193 : : {
7194 : 200862693 : if ((*switches[i].part1 == 'D' || *switches[i].part1 == 'U')
7195 : 8493348 : && *switches[i].part1 == atom[0])
7196 : : {
7197 : 1 : if (!strncmp (switches[i].args[0], &atom[1], len - 1)
7198 : 1 : && (starred || (switches[i].part1[1] == '\0'
7199 : 1 : && switches[i].args[0][len - 1] == '\0'))
7200 : 2 : && check_live_switch (i, (starred ? 1 : -1)))
7201 : : return true;
7202 : : }
7203 : : }
7204 : :
7205 : : return false;
7206 : : }
7207 : :
7208 : : /* Inline subroutine of handle_braces. Mark all of the switches which
7209 : : match ATOM (extends to END_ATOM; STARRED indicates whether there
7210 : : was a star after the atom) for later processing. */
7211 : : static inline void
7212 : 11217184 : mark_matching_switches (const char *atom, const char *end_atom, int starred)
7213 : : {
7214 : 11217184 : int i;
7215 : 11217184 : int len = end_atom - atom;
7216 : 11217184 : int plen = starred ? len : -1;
7217 : :
7218 : 267166033 : for (i = 0; i < n_switches; i++)
7219 : 255948849 : if (!strncmp (switches[i].part1, atom, len)
7220 : 6356021 : && (starred || switches[i].part1[len] == '\0')
7221 : 262087326 : && check_live_switch (i, plen))
7222 : 6088825 : switches[i].ordering = 1;
7223 : 11217184 : }
7224 : :
7225 : : /* Inline subroutine of handle_braces. Process all the currently
7226 : : marked switches through give_switch, and clear the marks. */
7227 : : static inline void
7228 : 9735820 : process_marked_switches (void)
7229 : : {
7230 : 9735820 : int i;
7231 : :
7232 : 231831181 : for (i = 0; i < n_switches; i++)
7233 : 222095361 : if (switches[i].ordering == 1)
7234 : : {
7235 : 6088825 : switches[i].ordering = 0;
7236 : 6088825 : give_switch (i, 0);
7237 : : }
7238 : 9735820 : }
7239 : :
7240 : : /* Handle a %{ ... } construct. P points just inside the leading {.
7241 : : Returns a pointer one past the end of the brace block, or 0
7242 : : if we call do_spec_1 and that returns -1. */
7243 : :
7244 : : static const char *
7245 : 40696406 : handle_braces (const char *p)
7246 : : {
7247 : 40696406 : const char *atom, *end_atom;
7248 : 40696406 : const char *d_atom = NULL, *d_end_atom = NULL;
7249 : 40696406 : char *esc_buf = NULL, *d_esc_buf = NULL;
7250 : 40696406 : int esc;
7251 : 40696406 : const char *orig = p;
7252 : :
7253 : 40696406 : bool a_is_suffix;
7254 : 40696406 : bool a_is_spectype;
7255 : 40696406 : bool a_is_starred;
7256 : 40696406 : bool a_is_negated;
7257 : 40696406 : bool a_matched;
7258 : :
7259 : 40696406 : bool a_must_be_last = false;
7260 : 40696406 : bool ordered_set = false;
7261 : 40696406 : bool disjunct_set = false;
7262 : 40696406 : bool disj_matched = false;
7263 : 40696406 : bool disj_starred = true;
7264 : 40696406 : bool n_way_choice = false;
7265 : 40696406 : bool n_way_matched = false;
7266 : :
7267 : : #define SKIP_WHITE() do { while (*p == ' ' || *p == '\t') p++; } while (0)
7268 : :
7269 : 54110057 : do
7270 : : {
7271 : 54110057 : if (a_must_be_last)
7272 : 0 : goto invalid;
7273 : :
7274 : : /* Scan one "atom" (S in the description above of %{}, possibly
7275 : : with '!', '.', '@', ',', or '*' modifiers). */
7276 : 54110057 : a_matched = false;
7277 : 54110057 : a_is_suffix = false;
7278 : 54110057 : a_is_starred = false;
7279 : 54110057 : a_is_negated = false;
7280 : 54110057 : a_is_spectype = false;
7281 : :
7282 : 61666373 : SKIP_WHITE ();
7283 : 54110057 : if (*p == '!')
7284 : 13073666 : p++, a_is_negated = true;
7285 : :
7286 : 54110057 : SKIP_WHITE ();
7287 : 54110057 : if (*p == '%' && p[1] == ':')
7288 : : {
7289 : 1638599 : atom = NULL;
7290 : 1638599 : end_atom = NULL;
7291 : 1638599 : p = handle_spec_function (p + 2, &a_matched, NULL);
7292 : : }
7293 : : else
7294 : : {
7295 : 52471458 : if (*p == '.')
7296 : 0 : p++, a_is_suffix = true;
7297 : 52471458 : else if (*p == ',')
7298 : 0 : p++, a_is_spectype = true;
7299 : :
7300 : 52471458 : atom = p;
7301 : 52471458 : esc = 0;
7302 : 52471458 : while (ISIDNUM (*p) || *p == '-' || *p == '+' || *p == '='
7303 : 395664928 : || *p == ',' || *p == '.' || *p == '@' || *p == '\\')
7304 : : {
7305 : 343193470 : if (*p == '\\')
7306 : : {
7307 : 0 : p++;
7308 : 0 : if (!*p)
7309 : 0 : fatal_error (input_location,
7310 : : "braced spec %qs ends in escape", orig);
7311 : 0 : esc++;
7312 : : }
7313 : 343193470 : p++;
7314 : : }
7315 : 52471458 : end_atom = p;
7316 : :
7317 : 52471458 : if (esc)
7318 : : {
7319 : 0 : const char *ap;
7320 : 0 : char *ep;
7321 : :
7322 : 0 : if (esc_buf && esc_buf != d_esc_buf)
7323 : 0 : free (esc_buf);
7324 : 0 : esc_buf = NULL;
7325 : 0 : ep = esc_buf = (char *) xmalloc (end_atom - atom - esc + 1);
7326 : 0 : for (ap = atom; ap != end_atom; ap++, ep++)
7327 : : {
7328 : 0 : if (*ap == '\\')
7329 : 0 : ap++;
7330 : 0 : *ep = *ap;
7331 : : }
7332 : 0 : *ep = '\0';
7333 : 0 : atom = esc_buf;
7334 : 0 : end_atom = ep;
7335 : : }
7336 : :
7337 : 52471458 : if (*p == '*')
7338 : 11814736 : p++, a_is_starred = 1;
7339 : : }
7340 : :
7341 : 54110057 : SKIP_WHITE ();
7342 : 54110057 : switch (*p)
7343 : : {
7344 : 11217184 : case '&': case '}':
7345 : : /* Substitute the switch(es) indicated by the current atom. */
7346 : 11217184 : ordered_set = true;
7347 : 11217184 : if (disjunct_set || n_way_choice || a_is_negated || a_is_suffix
7348 : 11217184 : || a_is_spectype || atom == end_atom)
7349 : 0 : goto invalid;
7350 : :
7351 : 11217184 : mark_matching_switches (atom, end_atom, a_is_starred);
7352 : :
7353 : 11217184 : if (*p == '}')
7354 : 9735820 : process_marked_switches ();
7355 : : break;
7356 : :
7357 : 42892873 : case '|': case ':':
7358 : : /* Substitute some text if the current atom appears as a switch
7359 : : or suffix. */
7360 : 42892873 : disjunct_set = true;
7361 : 42892873 : if (ordered_set)
7362 : 0 : goto invalid;
7363 : :
7364 : 42892873 : if (atom && atom == end_atom)
7365 : : {
7366 : 1774390 : if (!n_way_choice || disj_matched || *p == '|'
7367 : 1774390 : || a_is_negated || a_is_suffix || a_is_spectype
7368 : 1774390 : || a_is_starred)
7369 : 0 : goto invalid;
7370 : :
7371 : : /* An empty term may appear as the last choice of an
7372 : : N-way choice set; it means "otherwise". */
7373 : 1774390 : a_must_be_last = true;
7374 : 1774390 : disj_matched = !n_way_matched;
7375 : 1774390 : disj_starred = false;
7376 : : }
7377 : : else
7378 : : {
7379 : 41118483 : if ((a_is_suffix || a_is_spectype) && a_is_starred)
7380 : 0 : goto invalid;
7381 : :
7382 : 41118483 : if (!a_is_starred)
7383 : 35537731 : disj_starred = false;
7384 : :
7385 : : /* Don't bother testing this atom if we already have a
7386 : : match. */
7387 : 41118483 : if (!disj_matched && !n_way_matched)
7388 : : {
7389 : 40099624 : if (atom == NULL)
7390 : : /* a_matched is already set by handle_spec_function. */;
7391 : 38564252 : else if (a_is_suffix)
7392 : 0 : a_matched = input_suffix_matches (atom, end_atom);
7393 : 38564252 : else if (a_is_spectype)
7394 : 0 : a_matched = input_spec_matches (atom, end_atom);
7395 : : else
7396 : 38564252 : a_matched = switch_matches (atom, end_atom, a_is_starred);
7397 : :
7398 : 40099624 : if (a_matched != a_is_negated)
7399 : : {
7400 : 12944729 : disj_matched = true;
7401 : 12944729 : d_atom = atom;
7402 : 12944729 : d_end_atom = end_atom;
7403 : 12944729 : d_esc_buf = esc_buf;
7404 : : }
7405 : : }
7406 : : }
7407 : :
7408 : 42892873 : if (*p == ':')
7409 : : {
7410 : : /* Found the body, that is, the text to substitute if the
7411 : : current disjunction matches. */
7412 : 67937711 : p = process_brace_body (p + 1, d_atom, d_end_atom, disj_starred,
7413 : 33968866 : disj_matched && !n_way_matched);
7414 : 33968845 : if (p == 0)
7415 : 37300 : goto done;
7416 : :
7417 : : /* If we have an N-way choice, reset state for the next
7418 : : disjunction. */
7419 : 33931545 : if (*p == ';')
7420 : : {
7421 : 3008280 : n_way_choice = true;
7422 : 3008280 : n_way_matched |= disj_matched;
7423 : 3008280 : disj_matched = false;
7424 : 3008280 : disj_starred = true;
7425 : 3008280 : d_atom = d_end_atom = NULL;
7426 : : }
7427 : : }
7428 : : break;
7429 : :
7430 : 0 : default:
7431 : 0 : goto invalid;
7432 : : }
7433 : : }
7434 : 54072736 : while (*p++ != '}');
7435 : :
7436 : 40659085 : done:
7437 : 40696385 : if (d_esc_buf && d_esc_buf != esc_buf)
7438 : 0 : free (d_esc_buf);
7439 : 40696385 : if (esc_buf)
7440 : 0 : free (esc_buf);
7441 : :
7442 : 40696385 : return p;
7443 : :
7444 : 0 : invalid:
7445 : 0 : fatal_error (input_location, "braced spec %qs is invalid at %qc", orig, *p);
7446 : :
7447 : : #undef SKIP_WHITE
7448 : : }
7449 : :
7450 : : /* Subroutine of handle_braces. Scan and process a brace substitution body
7451 : : (X in the description of %{} syntax). P points one past the colon;
7452 : : ATOM and END_ATOM bracket the first atom which was found to be true
7453 : : (present) in the current disjunction; STARRED indicates whether all
7454 : : the atoms in the current disjunction were starred (for syntax validation);
7455 : : MATCHED indicates whether the disjunction matched or not, and therefore
7456 : : whether or not the body is to be processed through do_spec_1 or just
7457 : : skipped. Returns a pointer to the closing } or ;, or 0 if do_spec_1
7458 : : returns -1. */
7459 : :
7460 : : static const char *
7461 : 33968866 : process_brace_body (const char *p, const char *atom, const char *end_atom,
7462 : : int starred, int matched)
7463 : : {
7464 : 33968866 : const char *body, *end_body;
7465 : 33968866 : unsigned int nesting_level;
7466 : 33968866 : bool have_subst = false;
7467 : :
7468 : : /* Locate the closing } or ;, honoring nested braces.
7469 : : Trim trailing whitespace. */
7470 : 33968866 : body = p;
7471 : 33968866 : nesting_level = 1;
7472 : 11550119320 : for (;;)
7473 : : {
7474 : 5792044093 : if (*p == '{')
7475 : 170992655 : nesting_level++;
7476 : 5621051438 : else if (*p == '}')
7477 : : {
7478 : 201953241 : if (!--nesting_level)
7479 : : break;
7480 : : }
7481 : 5419098197 : else if (*p == ';' && nesting_level == 1)
7482 : : break;
7483 : 5416089917 : else if (*p == '%' && p[1] == '*' && nesting_level == 1)
7484 : : have_subst = true;
7485 : 5415302972 : else if (*p == '\0')
7486 : 0 : goto invalid;
7487 : 5758075227 : p++;
7488 : : }
7489 : :
7490 : : end_body = p;
7491 : 36742847 : while (end_body[-1] == ' ' || end_body[-1] == '\t')
7492 : 2773981 : end_body--;
7493 : :
7494 : 33968866 : if (have_subst && !starred)
7495 : 0 : goto invalid;
7496 : :
7497 : 33968866 : if (matched)
7498 : : {
7499 : : /* Copy the substitution body to permanent storage and execute it.
7500 : : If have_subst is false, this is a simple matter of running the
7501 : : body through do_spec_1... */
7502 : 13902354 : char *string = save_string (body, end_body - body);
7503 : 13902354 : if (!have_subst)
7504 : : {
7505 : 13895554 : if (do_spec_1 (string, 0, NULL) < 0)
7506 : : {
7507 : 37300 : free (string);
7508 : 37300 : return 0;
7509 : : }
7510 : : }
7511 : : else
7512 : : {
7513 : : /* ... but if have_subst is true, we have to process the
7514 : : body once for each matching switch, with %* set to the
7515 : : variant part of the switch. */
7516 : 6800 : unsigned int hard_match_len = end_atom - atom;
7517 : 6800 : int i;
7518 : :
7519 : 273051 : for (i = 0; i < n_switches; i++)
7520 : 266251 : if (!strncmp (switches[i].part1, atom, hard_match_len)
7521 : 266251 : && check_live_switch (i, hard_match_len))
7522 : : {
7523 : 6804 : if (do_spec_1 (string, 0,
7524 : : &switches[i].part1[hard_match_len]) < 0)
7525 : : {
7526 : 0 : free (string);
7527 : 0 : return 0;
7528 : : }
7529 : : /* Pass any arguments this switch has. */
7530 : 6804 : give_switch (i, 1);
7531 : 6804 : suffix_subst = NULL;
7532 : : }
7533 : : }
7534 : 13865033 : free (string);
7535 : : }
7536 : :
7537 : : return p;
7538 : :
7539 : 0 : invalid:
7540 : 0 : fatal_error (input_location, "braced spec body %qs is invalid", body);
7541 : : }
7542 : :
7543 : : /* Return 0 iff switch number SWITCHNUM is obsoleted by a later switch
7544 : : on the command line. PREFIX_LENGTH is the length of XXX in an {XXX*}
7545 : : spec, or -1 if either exact match or %* is used.
7546 : :
7547 : : A -O switch is obsoleted by a later -O switch. A -f, -g, -m, or -W switch
7548 : : whose value does not begin with "no-" is obsoleted by the same value
7549 : : with the "no-", similarly for a switch with the "no-" prefix. */
7550 : :
7551 : : static int
7552 : 7517942 : check_live_switch (int switchnum, int prefix_length)
7553 : : {
7554 : 7517942 : const char *name = switches[switchnum].part1;
7555 : 7517942 : int i;
7556 : :
7557 : : /* If we already processed this switch and determined if it was
7558 : : live or not, return our past determination. */
7559 : 7517942 : if (switches[switchnum].live_cond != 0)
7560 : 949580 : return ((switches[switchnum].live_cond & SWITCH_LIVE) != 0
7561 : 899328 : && (switches[switchnum].live_cond & SWITCH_FALSE) == 0
7562 : 1848908 : && (switches[switchnum].live_cond & SWITCH_IGNORE_PERMANENTLY)
7563 : 949580 : == 0);
7564 : :
7565 : : /* In the common case of {<at-most-one-letter>*}, a negating
7566 : : switch would always match, so ignore that case. We will just
7567 : : send the conflicting switches to the compiler phase. */
7568 : 6568362 : if (prefix_length >= 0 && prefix_length <= 1)
7569 : : return 1;
7570 : :
7571 : : /* Now search for duplicate in a manner that depends on the name. */
7572 : 883081 : switch (*name)
7573 : : {
7574 : 62 : case 'O':
7575 : 344 : for (i = switchnum + 1; i < n_switches; i++)
7576 : 287 : if (switches[i].part1[0] == 'O')
7577 : : {
7578 : 5 : switches[switchnum].validated = true;
7579 : 5 : switches[switchnum].live_cond = SWITCH_FALSE;
7580 : 5 : return 0;
7581 : : }
7582 : : break;
7583 : :
7584 : 286331 : case 'W': case 'f': case 'm': case 'g':
7585 : 286331 : if (startswith (name + 1, "no-"))
7586 : : {
7587 : : /* We have Xno-YYY, search for XYYY. */
7588 : 34762 : for (i = switchnum + 1; i < n_switches; i++)
7589 : 29232 : if (switches[i].part1[0] == name[0]
7590 : 5579 : && ! strcmp (&switches[i].part1[1], &name[4]))
7591 : : {
7592 : : /* --specs are validated with the validate_switches mechanism. */
7593 : 0 : if (switches[switchnum].known)
7594 : 0 : switches[switchnum].validated = true;
7595 : 0 : switches[switchnum].live_cond = SWITCH_FALSE;
7596 : 0 : return 0;
7597 : : }
7598 : : }
7599 : : else
7600 : : {
7601 : : /* We have XYYY, search for Xno-YYY. */
7602 : 2940075 : for (i = switchnum + 1; i < n_switches; i++)
7603 : 2659274 : if (switches[i].part1[0] == name[0]
7604 : 1608673 : && switches[i].part1[1] == 'n'
7605 : 202208 : && switches[i].part1[2] == 'o'
7606 : 202207 : && switches[i].part1[3] == '-'
7607 : 202177 : && !strcmp (&switches[i].part1[4], &name[1]))
7608 : : {
7609 : : /* --specs are validated with the validate_switches mechanism. */
7610 : 0 : if (switches[switchnum].known)
7611 : 0 : switches[switchnum].validated = true;
7612 : 0 : switches[switchnum].live_cond = SWITCH_FALSE;
7613 : 0 : return 0;
7614 : : }
7615 : : }
7616 : : break;
7617 : : }
7618 : :
7619 : : /* Otherwise the switch is live. */
7620 : 883076 : switches[switchnum].live_cond |= SWITCH_LIVE;
7621 : 883076 : return 1;
7622 : : }
7623 : :
7624 : : /* Pass a switch to the current accumulating command
7625 : : in the same form that we received it.
7626 : : SWITCHNUM identifies the switch; it is an index into
7627 : : the vector of switches gcc received, which is `switches'.
7628 : : This cannot fail since it never finishes a command line.
7629 : :
7630 : : If OMIT_FIRST_WORD is nonzero, then we omit .part1 of the argument. */
7631 : :
7632 : : static void
7633 : 6095629 : give_switch (int switchnum, int omit_first_word)
7634 : : {
7635 : 6095629 : if ((switches[switchnum].live_cond & SWITCH_IGNORE) != 0)
7636 : : return;
7637 : :
7638 : 6095618 : if (!omit_first_word)
7639 : : {
7640 : 6088814 : do_spec_1 ("-", 0, NULL);
7641 : 6088814 : do_spec_1 (switches[switchnum].part1, 1, NULL);
7642 : : }
7643 : :
7644 : 6095618 : if (switches[switchnum].args != 0)
7645 : : {
7646 : : const char **p;
7647 : 2456954 : for (p = switches[switchnum].args; *p; p++)
7648 : : {
7649 : 1228477 : const char *arg = *p;
7650 : :
7651 : 1228477 : do_spec_1 (" ", 0, NULL);
7652 : 1228477 : if (suffix_subst)
7653 : : {
7654 : 5871 : unsigned length = strlen (arg);
7655 : 5871 : int dot = 0;
7656 : :
7657 : 11742 : while (length-- && !IS_DIR_SEPARATOR (arg[length]))
7658 : 11742 : if (arg[length] == '.')
7659 : : {
7660 : 5871 : (CONST_CAST (char *, arg))[length] = 0;
7661 : 5871 : dot = 1;
7662 : 5871 : break;
7663 : : }
7664 : 5871 : do_spec_1 (arg, 1, NULL);
7665 : 5871 : if (dot)
7666 : 5871 : (CONST_CAST (char *, arg))[length] = '.';
7667 : 5871 : do_spec_1 (suffix_subst, 1, NULL);
7668 : : }
7669 : : else
7670 : 1222606 : do_spec_1 (arg, 1, NULL);
7671 : : }
7672 : : }
7673 : :
7674 : 6095618 : do_spec_1 (" ", 0, NULL);
7675 : 6095618 : switches[switchnum].validated = true;
7676 : : }
7677 : :
7678 : : /* Print GCC configuration (e.g. version, thread model, target,
7679 : : configuration_arguments) to a given FILE. */
7680 : :
7681 : : static void
7682 : 1479 : print_configuration (FILE *file)
7683 : : {
7684 : 1479 : int n;
7685 : 1479 : const char *thrmod;
7686 : :
7687 : 1479 : fnotice (file, "Target: %s\n", spec_machine);
7688 : 1479 : fnotice (file, "Configured with: %s\n", configuration_arguments);
7689 : :
7690 : : #ifdef THREAD_MODEL_SPEC
7691 : : /* We could have defined THREAD_MODEL_SPEC to "%*" by default,
7692 : : but there's no point in doing all this processing just to get
7693 : : thread_model back. */
7694 : : obstack_init (&obstack);
7695 : : do_spec_1 (THREAD_MODEL_SPEC, 0, thread_model);
7696 : : obstack_1grow (&obstack, '\0');
7697 : : thrmod = XOBFINISH (&obstack, const char *);
7698 : : #else
7699 : 1479 : thrmod = thread_model;
7700 : : #endif
7701 : :
7702 : 1479 : fnotice (file, "Thread model: %s\n", thrmod);
7703 : 1479 : fnotice (file, "Supported LTO compression algorithms: zlib");
7704 : : #ifdef HAVE_ZSTD_H
7705 : 1479 : fnotice (file, " zstd");
7706 : : #endif
7707 : 1479 : fnotice (file, "\n");
7708 : :
7709 : : /* compiler_version is truncated at the first space when initialized
7710 : : from version string, so truncate version_string at the first space
7711 : : before comparing. */
7712 : 11832 : for (n = 0; version_string[n]; n++)
7713 : 10353 : if (version_string[n] == ' ')
7714 : : break;
7715 : :
7716 : 1479 : if (! strncmp (version_string, compiler_version, n)
7717 : 1479 : && compiler_version[n] == 0)
7718 : 1479 : fnotice (file, "gcc version %s %s\n", version_string,
7719 : : pkgversion_string);
7720 : : else
7721 : 0 : fnotice (file, "gcc driver version %s %sexecuting gcc version %s\n",
7722 : : version_string, pkgversion_string, compiler_version);
7723 : :
7724 : 1479 : }
7725 : :
7726 : : #define RETRY_ICE_ATTEMPTS 3
7727 : :
7728 : : /* Returns true if FILE1 and FILE2 contain equivalent data, 0 otherwise.
7729 : : If lines start with 0x followed by 1-16 lowercase hexadecimal digits
7730 : : followed by a space, ignore anything before that space. These are
7731 : : typically function addresses from libbacktrace and those can differ
7732 : : due to ASLR. */
7733 : :
7734 : : static bool
7735 : 0 : files_equal_p (char *file1, char *file2)
7736 : : {
7737 : 0 : FILE *f1 = fopen (file1, "rb");
7738 : 0 : FILE *f2 = fopen (file2, "rb");
7739 : 0 : char line1[256], line2[256];
7740 : :
7741 : 0 : bool line_start = true;
7742 : 0 : while (fgets (line1, sizeof (line1), f1))
7743 : : {
7744 : 0 : if (!fgets (line2, sizeof (line2), f2))
7745 : 0 : goto error;
7746 : 0 : char *p1 = line1, *p2 = line2;
7747 : 0 : if (line_start
7748 : 0 : && line1[0] == '0'
7749 : 0 : && line1[1] == 'x'
7750 : 0 : && line2[0] == '0'
7751 : 0 : && line2[1] == 'x')
7752 : : {
7753 : : int i, j;
7754 : 0 : for (i = 0; i < 16; ++i)
7755 : 0 : if (!ISXDIGIT (line1[2 + i]) || ISUPPER (line1[2 + i]))
7756 : : break;
7757 : 0 : for (j = 0; j < 16; ++j)
7758 : 0 : if (!ISXDIGIT (line2[2 + j]) || ISUPPER (line2[2 + j]))
7759 : : break;
7760 : 0 : if (i && line1[2 + i] == ' ' && j && line2[2 + j] == ' ')
7761 : : {
7762 : 0 : p1 = line1 + i + 3;
7763 : 0 : p2 = line2 + j + 3;
7764 : : }
7765 : : }
7766 : 0 : if (strcmp (p1, p2) != 0)
7767 : 0 : goto error;
7768 : 0 : line_start = strchr (line1, '\n') != NULL;
7769 : : }
7770 : 0 : if (fgets (line2, sizeof (line2), f2))
7771 : 0 : goto error;
7772 : :
7773 : 0 : fclose (f1);
7774 : 0 : fclose (f2);
7775 : 0 : return 1;
7776 : :
7777 : 0 : error:
7778 : 0 : fclose (f1);
7779 : 0 : fclose (f2);
7780 : 0 : return 0;
7781 : : }
7782 : :
7783 : : /* Check that compiler's output doesn't differ across runs.
7784 : : TEMP_STDOUT_FILES and TEMP_STDERR_FILES are arrays of files, containing
7785 : : stdout and stderr for each compiler run. Return true if all of
7786 : : TEMP_STDOUT_FILES and TEMP_STDERR_FILES are equivalent. */
7787 : :
7788 : : static bool
7789 : 0 : check_repro (char **temp_stdout_files, char **temp_stderr_files)
7790 : : {
7791 : 0 : int i;
7792 : 0 : for (i = 0; i < RETRY_ICE_ATTEMPTS - 2; ++i)
7793 : : {
7794 : 0 : if (!files_equal_p (temp_stdout_files[i], temp_stdout_files[i + 1])
7795 : 0 : || !files_equal_p (temp_stderr_files[i], temp_stderr_files[i + 1]))
7796 : : {
7797 : 0 : fnotice (stderr, "The bug is not reproducible, so it is"
7798 : : " likely a hardware or OS problem.\n");
7799 : 0 : break;
7800 : : }
7801 : : }
7802 : 0 : return i == RETRY_ICE_ATTEMPTS - 2;
7803 : : }
7804 : :
7805 : : enum attempt_status {
7806 : : ATTEMPT_STATUS_FAIL_TO_RUN,
7807 : : ATTEMPT_STATUS_SUCCESS,
7808 : : ATTEMPT_STATUS_ICE
7809 : : };
7810 : :
7811 : :
7812 : : /* Run compiler with arguments NEW_ARGV to reproduce the ICE, storing stdout
7813 : : to OUT_TEMP and stderr to ERR_TEMP. If APPEND is TRUE, append to OUT_TEMP
7814 : : and ERR_TEMP instead of truncating. If EMIT_SYSTEM_INFO is TRUE, also write
7815 : : GCC configuration into to ERR_TEMP. Return ATTEMPT_STATUS_FAIL_TO_RUN if
7816 : : compiler failed to run, ATTEMPT_STATUS_ICE if compiled ICE-ed and
7817 : : ATTEMPT_STATUS_SUCCESS otherwise. */
7818 : :
7819 : : static enum attempt_status
7820 : 0 : run_attempt (const char **new_argv, const char *out_temp,
7821 : : const char *err_temp, int emit_system_info, int append)
7822 : : {
7823 : :
7824 : 0 : if (emit_system_info)
7825 : : {
7826 : 0 : FILE *file_out = fopen (err_temp, "a");
7827 : 0 : print_configuration (file_out);
7828 : 0 : fputs ("\n", file_out);
7829 : 0 : fclose (file_out);
7830 : : }
7831 : :
7832 : 0 : int exit_status;
7833 : 0 : const char *errmsg;
7834 : 0 : struct pex_obj *pex;
7835 : 0 : int err;
7836 : 0 : int pex_flags = PEX_USE_PIPES | PEX_LAST;
7837 : 0 : enum attempt_status status = ATTEMPT_STATUS_FAIL_TO_RUN;
7838 : :
7839 : 0 : if (append)
7840 : 0 : pex_flags |= PEX_STDOUT_APPEND | PEX_STDERR_APPEND;
7841 : :
7842 : 0 : pex = pex_init (PEX_USE_PIPES, new_argv[0], NULL);
7843 : 0 : if (!pex)
7844 : : fatal_error (input_location, "%<pex_init%> failed: %m");
7845 : :
7846 : 0 : errmsg = pex_run (pex, pex_flags, new_argv[0],
7847 : 0 : CONST_CAST2 (char *const *, const char **, &new_argv[1]),
7848 : : out_temp, err_temp, &err);
7849 : 0 : if (errmsg != NULL)
7850 : : {
7851 : 0 : errno = err;
7852 : 0 : fatal_error (input_location,
7853 : : err ? G_ ("cannot execute %qs: %s: %m")
7854 : : : G_ ("cannot execute %qs: %s"),
7855 : : new_argv[0], errmsg);
7856 : : }
7857 : :
7858 : 0 : if (!pex_get_status (pex, 1, &exit_status))
7859 : 0 : goto out;
7860 : :
7861 : 0 : switch (WEXITSTATUS (exit_status))
7862 : : {
7863 : : case ICE_EXIT_CODE:
7864 : 0 : status = ATTEMPT_STATUS_ICE;
7865 : : break;
7866 : :
7867 : 0 : case SUCCESS_EXIT_CODE:
7868 : 0 : status = ATTEMPT_STATUS_SUCCESS;
7869 : 0 : break;
7870 : :
7871 : 0 : default:
7872 : 0 : ;
7873 : : }
7874 : :
7875 : 0 : out:
7876 : 0 : pex_free (pex);
7877 : 0 : return status;
7878 : : }
7879 : :
7880 : : /* This routine reads lines from IN file, adds C++ style comments
7881 : : at the begining of each line and writes result into OUT. */
7882 : :
7883 : : static void
7884 : 0 : insert_comments (const char *file_in, const char *file_out)
7885 : : {
7886 : 0 : FILE *in = fopen (file_in, "rb");
7887 : 0 : FILE *out = fopen (file_out, "wb");
7888 : 0 : char line[256];
7889 : :
7890 : 0 : bool add_comment = true;
7891 : 0 : while (fgets (line, sizeof (line), in))
7892 : : {
7893 : 0 : if (add_comment)
7894 : 0 : fputs ("// ", out);
7895 : 0 : fputs (line, out);
7896 : 0 : add_comment = strchr (line, '\n') != NULL;
7897 : : }
7898 : :
7899 : 0 : fclose (in);
7900 : 0 : fclose (out);
7901 : 0 : }
7902 : :
7903 : : /* This routine adds preprocessed source code into the given ERR_FILE.
7904 : : To do this, it adds "-E" to NEW_ARGV and execute RUN_ATTEMPT routine to
7905 : : add information in report file. RUN_ATTEMPT should return
7906 : : ATTEMPT_STATUS_SUCCESS, in other case we cannot generate the report. */
7907 : :
7908 : : static void
7909 : 0 : do_report_bug (const char **new_argv, const int nargs,
7910 : : char **out_file, char **err_file)
7911 : : {
7912 : 0 : int i, status;
7913 : 0 : int fd = open (*out_file, O_RDWR | O_APPEND);
7914 : 0 : if (fd < 0)
7915 : : return;
7916 : 0 : write (fd, "\n//", 3);
7917 : 0 : for (i = 0; i < nargs; i++)
7918 : : {
7919 : 0 : write (fd, " ", 1);
7920 : 0 : write (fd, new_argv[i], strlen (new_argv[i]));
7921 : : }
7922 : 0 : write (fd, "\n\n", 2);
7923 : 0 : close (fd);
7924 : 0 : new_argv[nargs] = "-E";
7925 : 0 : new_argv[nargs + 1] = NULL;
7926 : :
7927 : 0 : status = run_attempt (new_argv, *out_file, *err_file, 0, 1);
7928 : :
7929 : 0 : if (status == ATTEMPT_STATUS_SUCCESS)
7930 : : {
7931 : 0 : fnotice (stderr, "Preprocessed source stored into %s file,"
7932 : : " please attach this to your bugreport.\n", *out_file);
7933 : : /* Make sure it is not deleted. */
7934 : 0 : free (*out_file);
7935 : 0 : *out_file = NULL;
7936 : : }
7937 : : }
7938 : :
7939 : : /* Try to reproduce ICE. If bug is reproducible, generate report .err file
7940 : : containing GCC configuration, backtrace, compiler's command line options
7941 : : and preprocessed source code. */
7942 : :
7943 : : static void
7944 : 0 : try_generate_repro (const char **argv)
7945 : : {
7946 : 0 : int i, nargs, out_arg = -1, quiet = 0, attempt;
7947 : 0 : const char **new_argv;
7948 : 0 : char *temp_files[RETRY_ICE_ATTEMPTS * 2];
7949 : 0 : char **temp_stdout_files = &temp_files[0];
7950 : 0 : char **temp_stderr_files = &temp_files[RETRY_ICE_ATTEMPTS];
7951 : :
7952 : 0 : if (gcc_input_filename == NULL || ! strcmp (gcc_input_filename, "-"))
7953 : 0 : return;
7954 : :
7955 : 0 : for (nargs = 0; argv[nargs] != NULL; ++nargs)
7956 : : /* Only retry compiler ICEs, not preprocessor ones. */
7957 : 0 : if (! strcmp (argv[nargs], "-E"))
7958 : : return;
7959 : 0 : else if (argv[nargs][0] == '-' && argv[nargs][1] == 'o')
7960 : : {
7961 : 0 : if (out_arg == -1)
7962 : : out_arg = nargs;
7963 : : else
7964 : : return;
7965 : : }
7966 : : /* If the compiler is going to output any time information,
7967 : : it might varry between invocations. */
7968 : 0 : else if (! strcmp (argv[nargs], "-quiet"))
7969 : : quiet = 1;
7970 : 0 : else if (! strcmp (argv[nargs], "-ftime-report"))
7971 : : return;
7972 : :
7973 : 0 : if (out_arg == -1 || !quiet)
7974 : : return;
7975 : :
7976 : 0 : memset (temp_files, '\0', sizeof (temp_files));
7977 : 0 : new_argv = XALLOCAVEC (const char *, nargs + 4);
7978 : 0 : memcpy (new_argv, argv, (nargs + 1) * sizeof (const char *));
7979 : 0 : new_argv[nargs++] = "-frandom-seed=0";
7980 : 0 : new_argv[nargs++] = "-fdump-noaddr";
7981 : 0 : new_argv[nargs] = NULL;
7982 : 0 : if (new_argv[out_arg][2] == '\0')
7983 : 0 : new_argv[out_arg + 1] = "-";
7984 : : else
7985 : 0 : new_argv[out_arg] = "-o-";
7986 : :
7987 : : #ifdef HOST_HAS_PERSONALITY_ADDR_NO_RANDOMIZE
7988 : 0 : personality (personality (0xffffffffU) | ADDR_NO_RANDOMIZE);
7989 : : #endif
7990 : :
7991 : 0 : int status;
7992 : 0 : for (attempt = 0; attempt < RETRY_ICE_ATTEMPTS; ++attempt)
7993 : : {
7994 : 0 : int emit_system_info = 0;
7995 : 0 : int append = 0;
7996 : 0 : temp_stdout_files[attempt] = make_temp_file (".out");
7997 : 0 : temp_stderr_files[attempt] = make_temp_file (".err");
7998 : :
7999 : 0 : if (attempt == RETRY_ICE_ATTEMPTS - 1)
8000 : : {
8001 : 0 : append = 1;
8002 : 0 : emit_system_info = 1;
8003 : : }
8004 : :
8005 : 0 : status = run_attempt (new_argv, temp_stdout_files[attempt],
8006 : : temp_stderr_files[attempt], emit_system_info,
8007 : : append);
8008 : :
8009 : 0 : if (status != ATTEMPT_STATUS_ICE)
8010 : : {
8011 : 0 : fnotice (stderr, "The bug is not reproducible, so it is"
8012 : : " likely a hardware or OS problem.\n");
8013 : 0 : goto out;
8014 : : }
8015 : : }
8016 : :
8017 : 0 : if (!check_repro (temp_stdout_files, temp_stderr_files))
8018 : 0 : goto out;
8019 : :
8020 : 0 : {
8021 : : /* Insert commented out backtrace into report file. */
8022 : 0 : char **stderr_commented = &temp_stdout_files[RETRY_ICE_ATTEMPTS - 1];
8023 : 0 : insert_comments (temp_stderr_files[RETRY_ICE_ATTEMPTS - 1],
8024 : : *stderr_commented);
8025 : :
8026 : : /* In final attempt we append compiler options and preprocesssed code to last
8027 : : generated .out file with configuration and backtrace. */
8028 : 0 : char **err = &temp_stderr_files[RETRY_ICE_ATTEMPTS - 1];
8029 : 0 : do_report_bug (new_argv, nargs, stderr_commented, err);
8030 : : }
8031 : :
8032 : : out:
8033 : 0 : for (i = 0; i < RETRY_ICE_ATTEMPTS * 2; i++)
8034 : 0 : if (temp_files[i])
8035 : : {
8036 : 0 : unlink (temp_stdout_files[i]);
8037 : 0 : free (temp_stdout_files[i]);
8038 : : }
8039 : : }
8040 : :
8041 : : /* Search for a file named NAME trying various prefixes including the
8042 : : user's -B prefix and some standard ones.
8043 : : Return the absolute file name found. If nothing is found, return NAME. */
8044 : :
8045 : : static const char *
8046 : 546068 : find_file (const char *name)
8047 : : {
8048 : 546068 : char *newname = find_a_file (&startfile_prefixes, name, R_OK, true);
8049 : 546068 : return newname ? newname : name;
8050 : : }
8051 : :
8052 : : /* Determine whether a directory exists. */
8053 : :
8054 : : static int
8055 : 10469679 : is_directory (const char *path1)
8056 : : {
8057 : 10469679 : int len1;
8058 : 10469679 : char *path;
8059 : 10469679 : char *cp;
8060 : 10469679 : struct stat st;
8061 : :
8062 : : /* Ensure the string ends with "/.". The resulting path will be a
8063 : : directory even if the given path is a symbolic link. */
8064 : 10469679 : len1 = strlen (path1);
8065 : 10469679 : path = (char *) alloca (3 + len1);
8066 : 10469679 : memcpy (path, path1, len1);
8067 : 10469679 : cp = path + len1;
8068 : 10469679 : if (!IS_DIR_SEPARATOR (cp[-1]))
8069 : 1503995 : *cp++ = DIR_SEPARATOR;
8070 : 10469679 : *cp++ = '.';
8071 : 10469679 : *cp = '\0';
8072 : :
8073 : 10469679 : return (stat (path, &st) >= 0 && S_ISDIR (st.st_mode));
8074 : : }
8075 : :
8076 : : /* Set up the various global variables to indicate that we're processing
8077 : : the input file named FILENAME. */
8078 : :
8079 : : void
8080 : 836001 : set_input (const char *filename)
8081 : : {
8082 : 836001 : const char *p;
8083 : :
8084 : 836001 : gcc_input_filename = filename;
8085 : 836001 : input_filename_length = strlen (gcc_input_filename);
8086 : 836001 : input_basename = lbasename (gcc_input_filename);
8087 : :
8088 : : /* Find a suffix starting with the last period,
8089 : : and set basename_length to exclude that suffix. */
8090 : 836001 : basename_length = strlen (input_basename);
8091 : 836001 : suffixed_basename_length = basename_length;
8092 : 836001 : p = input_basename + basename_length;
8093 : 3668189 : while (p != input_basename && *p != '.')
8094 : 2832188 : --p;
8095 : 836001 : if (*p == '.' && p != input_basename)
8096 : : {
8097 : 599659 : basename_length = p - input_basename;
8098 : 599659 : input_suffix = p + 1;
8099 : : }
8100 : : else
8101 : 236342 : input_suffix = "";
8102 : :
8103 : : /* If a spec for 'g', 'u', or 'U' is seen with -save-temps then
8104 : : we will need to do a stat on the gcc_input_filename. The
8105 : : INPUT_STAT_SET signals that the stat is needed. */
8106 : 836001 : input_stat_set = 0;
8107 : 836001 : }
8108 : :
8109 : : /* On fatal signals, delete all the temporary files. */
8110 : :
8111 : : static void
8112 : 0 : fatal_signal (int signum)
8113 : : {
8114 : 0 : signal (signum, SIG_DFL);
8115 : 0 : delete_failure_queue ();
8116 : 0 : delete_temp_files ();
8117 : : /* Get the same signal again, this time not handled,
8118 : : so its normal effect occurs. */
8119 : 0 : kill (getpid (), signum);
8120 : 0 : }
8121 : :
8122 : : /* Compare the contents of the two files named CMPFILE[0] and
8123 : : CMPFILE[1]. Return zero if they're identical, nonzero
8124 : : otherwise. */
8125 : :
8126 : : static int
8127 : 613 : compare_files (char *cmpfile[])
8128 : : {
8129 : 613 : int ret = 0;
8130 : 613 : FILE *temp[2] = { NULL, NULL };
8131 : 613 : int i;
8132 : :
8133 : : #if HAVE_MMAP_FILE
8134 : 613 : {
8135 : 613 : size_t length[2];
8136 : 613 : void *map[2] = { NULL, NULL };
8137 : :
8138 : 1839 : for (i = 0; i < 2; i++)
8139 : : {
8140 : 1226 : struct stat st;
8141 : :
8142 : 1226 : if (stat (cmpfile[i], &st) < 0 || !S_ISREG (st.st_mode))
8143 : : {
8144 : 0 : error ("%s: could not determine length of compare-debug file %s",
8145 : : gcc_input_filename, cmpfile[i]);
8146 : 0 : ret = 1;
8147 : 0 : break;
8148 : : }
8149 : :
8150 : 1226 : length[i] = st.st_size;
8151 : : }
8152 : :
8153 : 613 : if (!ret && length[0] != length[1])
8154 : : {
8155 : 31 : error ("%s: %<-fcompare-debug%> failure (length)", gcc_input_filename);
8156 : 31 : ret = 1;
8157 : : }
8158 : :
8159 : 31 : if (!ret)
8160 : 1684 : for (i = 0; i < 2; i++)
8161 : : {
8162 : 1133 : int fd = open (cmpfile[i], O_RDONLY);
8163 : 1133 : if (fd < 0)
8164 : : {
8165 : 0 : error ("%s: could not open compare-debug file %s",
8166 : : gcc_input_filename, cmpfile[i]);
8167 : 0 : ret = 1;
8168 : 0 : break;
8169 : : }
8170 : :
8171 : 1133 : map[i] = mmap (NULL, length[i], PROT_READ, MAP_PRIVATE, fd, 0);
8172 : 1133 : close (fd);
8173 : :
8174 : 1133 : if (map[i] == (void *) MAP_FAILED)
8175 : : {
8176 : : ret = -1;
8177 : : break;
8178 : : }
8179 : : }
8180 : :
8181 : 582 : if (!ret)
8182 : : {
8183 : 551 : if (memcmp (map[0], map[1], length[0]) != 0)
8184 : : {
8185 : 0 : error ("%s: %<-fcompare-debug%> failure", gcc_input_filename);
8186 : 0 : ret = 1;
8187 : : }
8188 : : }
8189 : :
8190 : 1839 : for (i = 0; i < 2; i++)
8191 : 1226 : if (map[i])
8192 : 1133 : munmap ((caddr_t) map[i], length[i]);
8193 : :
8194 : 613 : if (ret >= 0)
8195 : 582 : return ret;
8196 : :
8197 : 31 : ret = 0;
8198 : : }
8199 : : #endif
8200 : :
8201 : 93 : for (i = 0; i < 2; i++)
8202 : : {
8203 : 62 : temp[i] = fopen (cmpfile[i], "r");
8204 : 62 : if (!temp[i])
8205 : : {
8206 : 0 : error ("%s: could not open compare-debug file %s",
8207 : : gcc_input_filename, cmpfile[i]);
8208 : 0 : ret = 1;
8209 : 0 : break;
8210 : : }
8211 : : }
8212 : :
8213 : 31 : if (!ret && temp[0] && temp[1])
8214 : 31 : for (;;)
8215 : : {
8216 : 31 : int c0, c1;
8217 : 31 : c0 = fgetc (temp[0]);
8218 : 31 : c1 = fgetc (temp[1]);
8219 : :
8220 : 31 : if (c0 != c1)
8221 : : {
8222 : 0 : error ("%s: %<-fcompare-debug%> failure",
8223 : : gcc_input_filename);
8224 : 0 : ret = 1;
8225 : 0 : break;
8226 : : }
8227 : :
8228 : 31 : if (c0 == EOF)
8229 : : break;
8230 : : }
8231 : :
8232 : 93 : for (i = 1; i >= 0; i--)
8233 : : {
8234 : 62 : if (temp[i])
8235 : 62 : fclose (temp[i]);
8236 : : }
8237 : :
8238 : : return ret;
8239 : : }
8240 : :
8241 : 300399 : driver::driver (bool can_finalize, bool debug) :
8242 : 300399 : explicit_link_files (NULL),
8243 : 300399 : decoded_options (NULL)
8244 : : {
8245 : 300399 : env.init (can_finalize, debug);
8246 : 300399 : }
8247 : :
8248 : 299914 : driver::~driver ()
8249 : : {
8250 : 299914 : XDELETEVEC (explicit_link_files);
8251 : 299914 : XDELETEVEC (decoded_options);
8252 : 299914 : }
8253 : :
8254 : : /* driver::main is implemented as a series of driver:: method calls. */
8255 : :
8256 : : int
8257 : 300399 : driver::main (int argc, char **argv)
8258 : : {
8259 : 300399 : bool early_exit;
8260 : :
8261 : 300399 : set_progname (argv[0]);
8262 : 300399 : expand_at_files (&argc, &argv);
8263 : 300399 : decode_argv (argc, const_cast <const char **> (argv));
8264 : 300399 : global_initializations ();
8265 : 300399 : build_multilib_strings ();
8266 : 300399 : set_up_specs ();
8267 : 300112 : putenv_COLLECT_AS_OPTIONS (assembler_options);
8268 : 300112 : putenv_COLLECT_GCC (argv[0]);
8269 : 300112 : maybe_putenv_COLLECT_LTO_WRAPPER ();
8270 : 300112 : maybe_putenv_OFFLOAD_TARGETS ();
8271 : 300112 : handle_unrecognized_options ();
8272 : :
8273 : 300112 : if (completion)
8274 : : {
8275 : 5 : m_option_proposer.suggest_completion (completion);
8276 : 5 : return 0;
8277 : : }
8278 : :
8279 : 300107 : if (!maybe_print_and_exit ())
8280 : : return 0;
8281 : :
8282 : 285095 : early_exit = prepare_infiles ();
8283 : 284901 : if (early_exit)
8284 : 485 : return get_exit_code ();
8285 : :
8286 : 284416 : do_spec_on_infiles ();
8287 : 284416 : maybe_run_linker (argv[0]);
8288 : 284413 : final_actions ();
8289 : 284413 : return get_exit_code ();
8290 : : }
8291 : :
8292 : : /* Locate the final component of argv[0] after any leading path, and set
8293 : : the program name accordingly. */
8294 : :
8295 : : void
8296 : 300399 : driver::set_progname (const char *argv0) const
8297 : : {
8298 : 300399 : const char *p = argv0 + strlen (argv0);
8299 : 1659306 : while (p != argv0 && !IS_DIR_SEPARATOR (p[-1]))
8300 : 1358907 : --p;
8301 : 300399 : progname = p;
8302 : :
8303 : 300399 : xmalloc_set_program_name (progname);
8304 : 300399 : }
8305 : :
8306 : : /* Expand any @ files within the command-line args,
8307 : : setting at_file_supplied if any were expanded. */
8308 : :
8309 : : void
8310 : 300399 : driver::expand_at_files (int *argc, char ***argv) const
8311 : : {
8312 : 300399 : char **old_argv = *argv;
8313 : :
8314 : 300399 : expandargv (argc, argv);
8315 : :
8316 : : /* Determine if any expansions were made. */
8317 : 300399 : if (*argv != old_argv)
8318 : 13343 : at_file_supplied = true;
8319 : 300399 : }
8320 : :
8321 : : /* Decode the command-line arguments from argc/argv into the
8322 : : decoded_options array. */
8323 : :
8324 : : void
8325 : 300399 : driver::decode_argv (int argc, const char **argv)
8326 : : {
8327 : 300399 : init_opts_obstack ();
8328 : 300399 : init_options_struct (&global_options, &global_options_set);
8329 : :
8330 : 300399 : decode_cmdline_options_to_array (argc, argv,
8331 : : CL_DRIVER,
8332 : : &decoded_options, &decoded_options_count);
8333 : 300399 : }
8334 : :
8335 : : /* Perform various initializations and setup. */
8336 : :
8337 : : void
8338 : 300399 : driver::global_initializations ()
8339 : : {
8340 : : /* Unlock the stdio streams. */
8341 : 300399 : unlock_std_streams ();
8342 : :
8343 : 300399 : gcc_init_libintl ();
8344 : :
8345 : 300399 : diagnostic_initialize (global_dc, 0);
8346 : 300399 : diagnostic_color_init (global_dc);
8347 : 300399 : diagnostic_urls_init (global_dc);
8348 : 300399 : global_dc->push_owned_urlifier (make_gcc_urlifier (0));
8349 : :
8350 : : #ifdef GCC_DRIVER_HOST_INITIALIZATION
8351 : : /* Perform host dependent initialization when needed. */
8352 : : GCC_DRIVER_HOST_INITIALIZATION;
8353 : : #endif
8354 : :
8355 : 300399 : if (atexit (delete_temp_files) != 0)
8356 : 0 : fatal_error (input_location, "atexit failed");
8357 : :
8358 : 300399 : if (signal (SIGINT, SIG_IGN) != SIG_IGN)
8359 : 300248 : signal (SIGINT, fatal_signal);
8360 : : #ifdef SIGHUP
8361 : 300399 : if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
8362 : 21218 : signal (SIGHUP, fatal_signal);
8363 : : #endif
8364 : 300399 : if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
8365 : 300399 : signal (SIGTERM, fatal_signal);
8366 : : #ifdef SIGPIPE
8367 : 300399 : if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
8368 : 300399 : signal (SIGPIPE, fatal_signal);
8369 : : #endif
8370 : : #ifdef SIGCHLD
8371 : : /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
8372 : : receive the signal. A different setting is inheritable */
8373 : 300399 : signal (SIGCHLD, SIG_DFL);
8374 : : #endif
8375 : :
8376 : : /* Parsing and gimplification sometimes need quite large stack.
8377 : : Increase stack size limits if possible. */
8378 : 300399 : stack_limit_increase (64 * 1024 * 1024);
8379 : :
8380 : : /* Allocate the argument vector. */
8381 : 300399 : alloc_args ();
8382 : :
8383 : 300399 : obstack_init (&obstack);
8384 : 300399 : }
8385 : :
8386 : : /* Build multilib_select, et. al from the separate lines that make up each
8387 : : multilib selection. */
8388 : :
8389 : : void
8390 : 300399 : driver::build_multilib_strings () const
8391 : : {
8392 : 300399 : {
8393 : 300399 : const char *p;
8394 : 300399 : const char *const *q = multilib_raw;
8395 : 300399 : int need_space;
8396 : :
8397 : 300399 : obstack_init (&multilib_obstack);
8398 : 300399 : while ((p = *q++) != (char *) 0)
8399 : 1201596 : obstack_grow (&multilib_obstack, p, strlen (p));
8400 : :
8401 : 300399 : obstack_1grow (&multilib_obstack, 0);
8402 : 300399 : multilib_select = XOBFINISH (&multilib_obstack, const char *);
8403 : :
8404 : 300399 : q = multilib_matches_raw;
8405 : 300399 : while ((p = *q++) != (char *) 0)
8406 : 901197 : obstack_grow (&multilib_obstack, p, strlen (p));
8407 : :
8408 : 300399 : obstack_1grow (&multilib_obstack, 0);
8409 : 300399 : multilib_matches = XOBFINISH (&multilib_obstack, const char *);
8410 : :
8411 : 300399 : q = multilib_exclusions_raw;
8412 : 300399 : while ((p = *q++) != (char *) 0)
8413 : 300399 : obstack_grow (&multilib_obstack, p, strlen (p));
8414 : :
8415 : 300399 : obstack_1grow (&multilib_obstack, 0);
8416 : 300399 : multilib_exclusions = XOBFINISH (&multilib_obstack, const char *);
8417 : :
8418 : 300399 : q = multilib_reuse_raw;
8419 : 300399 : while ((p = *q++) != (char *) 0)
8420 : 300399 : obstack_grow (&multilib_obstack, p, strlen (p));
8421 : :
8422 : 300399 : obstack_1grow (&multilib_obstack, 0);
8423 : 300399 : multilib_reuse = XOBFINISH (&multilib_obstack, const char *);
8424 : :
8425 : 300399 : need_space = false;
8426 : 600798 : for (size_t i = 0; i < ARRAY_SIZE (multilib_defaults_raw); i++)
8427 : : {
8428 : 300399 : if (need_space)
8429 : 0 : obstack_1grow (&multilib_obstack, ' ');
8430 : 300399 : obstack_grow (&multilib_obstack,
8431 : : multilib_defaults_raw[i],
8432 : : strlen (multilib_defaults_raw[i]));
8433 : 300399 : need_space = true;
8434 : : }
8435 : :
8436 : 300399 : obstack_1grow (&multilib_obstack, 0);
8437 : 300399 : multilib_defaults = XOBFINISH (&multilib_obstack, const char *);
8438 : : }
8439 : 300399 : }
8440 : :
8441 : : /* Set up the spec-handling machinery. */
8442 : :
8443 : : void
8444 : 300399 : driver::set_up_specs () const
8445 : : {
8446 : 300399 : const char *spec_machine_suffix;
8447 : 300399 : char *specs_file;
8448 : 300399 : size_t i;
8449 : :
8450 : : #ifdef INIT_ENVIRONMENT
8451 : : /* Set up any other necessary machine specific environment variables. */
8452 : : xputenv (INIT_ENVIRONMENT);
8453 : : #endif
8454 : :
8455 : : /* Make a table of what switches there are (switches, n_switches).
8456 : : Make a table of specified input files (infiles, n_infiles).
8457 : : Decode switches that are handled locally. */
8458 : :
8459 : 300399 : process_command (decoded_options_count, decoded_options);
8460 : :
8461 : : /* Initialize the vector of specs to just the default.
8462 : : This means one element containing 0s, as a terminator. */
8463 : :
8464 : 300113 : compilers = XNEWVAR (struct compiler, sizeof default_compilers);
8465 : 300113 : memcpy (compilers, default_compilers, sizeof default_compilers);
8466 : 300113 : n_compilers = n_default_compilers;
8467 : :
8468 : : /* Read specs from a file if there is one. */
8469 : :
8470 : 300113 : machine_suffix = concat (spec_host_machine, dir_separator_str, spec_version,
8471 : : accel_dir_suffix, dir_separator_str, NULL);
8472 : 300113 : just_machine_suffix = concat (spec_machine, dir_separator_str, NULL);
8473 : :
8474 : 300113 : specs_file = find_a_file (&startfile_prefixes, "specs", R_OK, true);
8475 : : /* Read the specs file unless it is a default one. */
8476 : 300113 : if (specs_file != 0 && strcmp (specs_file, "specs"))
8477 : 298968 : read_specs (specs_file, true, false);
8478 : : else
8479 : 1145 : init_spec ();
8480 : :
8481 : : #ifdef ACCEL_COMPILER
8482 : : spec_machine_suffix = machine_suffix;
8483 : : #else
8484 : 300113 : spec_machine_suffix = just_machine_suffix;
8485 : : #endif
8486 : :
8487 : 300113 : const char *exec_prefix
8488 : 300113 : = gcc_exec_prefix ? gcc_exec_prefix : standard_exec_prefix;
8489 : : /* We need to check standard_exec_prefix/spec_machine_suffix/specs
8490 : : for any override of as, ld and libraries. */
8491 : 300113 : specs_file = (char *) alloca (
8492 : : strlen (exec_prefix) + strlen (spec_machine_suffix) + sizeof ("specs"));
8493 : 300113 : strcpy (specs_file, exec_prefix);
8494 : 300113 : strcat (specs_file, spec_machine_suffix);
8495 : 300113 : strcat (specs_file, "specs");
8496 : 300113 : if (access (specs_file, R_OK) == 0)
8497 : 0 : read_specs (specs_file, true, false);
8498 : :
8499 : : /* Process any configure-time defaults specified for the command line
8500 : : options, via OPTION_DEFAULT_SPECS. */
8501 : 3301243 : for (i = 0; i < ARRAY_SIZE (option_default_specs); i++)
8502 : 3001130 : do_option_spec (option_default_specs[i].name,
8503 : 3001130 : option_default_specs[i].spec);
8504 : :
8505 : : /* Process DRIVER_SELF_SPECS, adding any new options to the end
8506 : : of the command line. */
8507 : :
8508 : 2100791 : for (i = 0; i < ARRAY_SIZE (driver_self_specs); i++)
8509 : 1800678 : do_self_spec (driver_self_specs[i]);
8510 : :
8511 : : /* If not cross-compiling, look for executables in the standard
8512 : : places. */
8513 : 300113 : if (*cross_compile == '0')
8514 : : {
8515 : 300113 : if (*md_exec_prefix)
8516 : : {
8517 : 0 : add_prefix (&exec_prefixes, md_exec_prefix, "GCC",
8518 : : PREFIX_PRIORITY_LAST, 0, 0);
8519 : : }
8520 : : }
8521 : :
8522 : : /* Process sysroot_suffix_spec. */
8523 : 300113 : if (*sysroot_suffix_spec != 0
8524 : 0 : && !no_sysroot_suffix
8525 : 300113 : && do_spec_2 (sysroot_suffix_spec, NULL) == 0)
8526 : : {
8527 : 0 : if (argbuf.length () > 1)
8528 : 0 : error ("spec failure: more than one argument to "
8529 : : "%<SYSROOT_SUFFIX_SPEC%>");
8530 : 0 : else if (argbuf.length () == 1)
8531 : 0 : target_sysroot_suffix = xstrdup (argbuf.last ());
8532 : : }
8533 : :
8534 : : #ifdef HAVE_LD_SYSROOT
8535 : : /* Pass the --sysroot option to the linker, if it supports that. If
8536 : : there is a sysroot_suffix_spec, it has already been processed by
8537 : : this point, so target_system_root really is the system root we
8538 : : should be using. */
8539 : 300113 : if (target_system_root)
8540 : : {
8541 : 0 : obstack_grow (&obstack, "%(sysroot_spec) ", strlen ("%(sysroot_spec) "));
8542 : 0 : obstack_grow0 (&obstack, link_spec, strlen (link_spec));
8543 : 0 : set_spec ("link", XOBFINISH (&obstack, const char *), false);
8544 : : }
8545 : : #endif
8546 : :
8547 : : /* Process sysroot_hdrs_suffix_spec. */
8548 : 300113 : if (*sysroot_hdrs_suffix_spec != 0
8549 : 0 : && !no_sysroot_suffix
8550 : 300113 : && do_spec_2 (sysroot_hdrs_suffix_spec, NULL) == 0)
8551 : : {
8552 : 0 : if (argbuf.length () > 1)
8553 : 0 : error ("spec failure: more than one argument "
8554 : : "to %<SYSROOT_HEADERS_SUFFIX_SPEC%>");
8555 : 0 : else if (argbuf.length () == 1)
8556 : 0 : target_sysroot_hdrs_suffix = xstrdup (argbuf.last ());
8557 : : }
8558 : :
8559 : : /* Look for startfiles in the standard places. */
8560 : 300113 : if (*startfile_prefix_spec != 0
8561 : 0 : && do_spec_2 (startfile_prefix_spec, NULL) == 0
8562 : 300113 : && do_spec_1 (" ", 0, NULL) == 0)
8563 : : {
8564 : 0 : for (const char *arg : argbuf)
8565 : 0 : add_sysrooted_prefix (&startfile_prefixes, arg, "BINUTILS",
8566 : : PREFIX_PRIORITY_LAST, 0, 1);
8567 : : }
8568 : : /* We should eventually get rid of all these and stick to
8569 : : startfile_prefix_spec exclusively. */
8570 : 300113 : else if (*cross_compile == '0' || target_system_root)
8571 : : {
8572 : 300113 : if (*md_startfile_prefix)
8573 : 0 : add_sysrooted_prefix (&startfile_prefixes, md_startfile_prefix,
8574 : : "GCC", PREFIX_PRIORITY_LAST, 0, 1);
8575 : :
8576 : 300113 : if (*md_startfile_prefix_1)
8577 : 0 : add_sysrooted_prefix (&startfile_prefixes, md_startfile_prefix_1,
8578 : : "GCC", PREFIX_PRIORITY_LAST, 0, 1);
8579 : :
8580 : : /* If standard_startfile_prefix is relative, base it on
8581 : : standard_exec_prefix. This lets us move the installed tree
8582 : : as a unit. If GCC_EXEC_PREFIX is defined, base
8583 : : standard_startfile_prefix on that as well.
8584 : :
8585 : : If the prefix is relative, only search it for native compilers;
8586 : : otherwise we will search a directory containing host libraries. */
8587 : 300113 : if (IS_ABSOLUTE_PATH (standard_startfile_prefix))
8588 : : add_sysrooted_prefix (&startfile_prefixes,
8589 : : standard_startfile_prefix, "BINUTILS",
8590 : : PREFIX_PRIORITY_LAST, 0, 1);
8591 : 300113 : else if (*cross_compile == '0')
8592 : : {
8593 : 300113 : add_prefix (&startfile_prefixes,
8594 : 600226 : concat (gcc_exec_prefix
8595 : : ? gcc_exec_prefix : standard_exec_prefix,
8596 : : machine_suffix,
8597 : : standard_startfile_prefix, NULL),
8598 : : NULL, PREFIX_PRIORITY_LAST, 0, 1);
8599 : : }
8600 : :
8601 : : /* Sysrooted prefixes are relocated because target_system_root is
8602 : : also relocated by gcc_exec_prefix. */
8603 : 300113 : if (*standard_startfile_prefix_1)
8604 : 300113 : add_sysrooted_prefix (&startfile_prefixes,
8605 : : standard_startfile_prefix_1, "BINUTILS",
8606 : : PREFIX_PRIORITY_LAST, 0, 1);
8607 : 300113 : if (*standard_startfile_prefix_2)
8608 : 300113 : add_sysrooted_prefix (&startfile_prefixes,
8609 : : standard_startfile_prefix_2, "BINUTILS",
8610 : : PREFIX_PRIORITY_LAST, 0, 1);
8611 : : }
8612 : :
8613 : : /* Process any user specified specs in the order given on the command
8614 : : line. */
8615 : 300115 : for (struct user_specs *uptr = user_specs_head; uptr; uptr = uptr->next)
8616 : : {
8617 : 3 : char *filename = find_a_file (&startfile_prefixes, uptr->filename,
8618 : : R_OK, true);
8619 : 3 : read_specs (filename ? filename : uptr->filename, false, true);
8620 : : }
8621 : :
8622 : : /* Process any user self specs. */
8623 : 300112 : {
8624 : 300112 : struct spec_list *sl;
8625 : 14105264 : for (sl = specs; sl; sl = sl->next)
8626 : 13805152 : if (sl->name_len == sizeof "self_spec" - 1
8627 : 2100784 : && !strcmp (sl->name, "self_spec"))
8628 : 300112 : do_self_spec (*sl->ptr_spec);
8629 : : }
8630 : :
8631 : 300112 : if (compare_debug)
8632 : : {
8633 : 619 : enum save_temps save;
8634 : :
8635 : 619 : if (!compare_debug_second)
8636 : : {
8637 : 619 : n_switches_debug_check[1] = n_switches;
8638 : 619 : n_switches_alloc_debug_check[1] = n_switches_alloc;
8639 : 619 : switches_debug_check[1] = XDUPVEC (struct switchstr, switches,
8640 : : n_switches_alloc);
8641 : :
8642 : 619 : do_self_spec ("%:compare-debug-self-opt()");
8643 : 619 : n_switches_debug_check[0] = n_switches;
8644 : 619 : n_switches_alloc_debug_check[0] = n_switches_alloc;
8645 : 619 : switches_debug_check[0] = switches;
8646 : :
8647 : 619 : n_switches = n_switches_debug_check[1];
8648 : 619 : n_switches_alloc = n_switches_alloc_debug_check[1];
8649 : 619 : switches = switches_debug_check[1];
8650 : : }
8651 : :
8652 : : /* Avoid crash when computing %j in this early. */
8653 : 619 : save = save_temps_flag;
8654 : 619 : save_temps_flag = SAVE_TEMPS_NONE;
8655 : :
8656 : 619 : compare_debug = -compare_debug;
8657 : 619 : do_self_spec ("%:compare-debug-self-opt()");
8658 : :
8659 : 619 : save_temps_flag = save;
8660 : :
8661 : 619 : if (!compare_debug_second)
8662 : : {
8663 : 619 : n_switches_debug_check[1] = n_switches;
8664 : 619 : n_switches_alloc_debug_check[1] = n_switches_alloc;
8665 : 619 : switches_debug_check[1] = switches;
8666 : 619 : compare_debug = -compare_debug;
8667 : 619 : n_switches = n_switches_debug_check[0];
8668 : 619 : n_switches_alloc = n_switches_debug_check[0];
8669 : 619 : switches = switches_debug_check[0];
8670 : : }
8671 : : }
8672 : :
8673 : :
8674 : : /* If we have a GCC_EXEC_PREFIX envvar, modify it for cpp's sake. */
8675 : 300112 : if (gcc_exec_prefix)
8676 : 300112 : gcc_exec_prefix = concat (gcc_exec_prefix, spec_host_machine,
8677 : : dir_separator_str, spec_version,
8678 : : accel_dir_suffix, dir_separator_str, NULL);
8679 : :
8680 : : /* Now we have the specs.
8681 : : Set the `valid' bits for switches that match anything in any spec. */
8682 : :
8683 : 300112 : validate_all_switches ();
8684 : :
8685 : : /* Now that we have the switches and the specs, set
8686 : : the subdirectory based on the options. */
8687 : 300112 : set_multilib_dir ();
8688 : 300112 : }
8689 : :
8690 : : /* Set up to remember the pathname of gcc and any options
8691 : : needed for collect. We use argv[0] instead of progname because
8692 : : we need the complete pathname. */
8693 : :
8694 : : void
8695 : 300112 : driver::putenv_COLLECT_GCC (const char *argv0) const
8696 : : {
8697 : 300112 : obstack_init (&collect_obstack);
8698 : 300112 : obstack_grow (&collect_obstack, "COLLECT_GCC=", sizeof ("COLLECT_GCC=") - 1);
8699 : 300112 : obstack_grow (&collect_obstack, argv0, strlen (argv0) + 1);
8700 : 300112 : xputenv (XOBFINISH (&collect_obstack, char *));
8701 : 300112 : }
8702 : :
8703 : : /* Set up to remember the pathname of the lto wrapper. */
8704 : :
8705 : : void
8706 : 300112 : driver::maybe_putenv_COLLECT_LTO_WRAPPER () const
8707 : : {
8708 : 300112 : char *lto_wrapper_file;
8709 : :
8710 : 300112 : if (have_c)
8711 : : lto_wrapper_file = NULL;
8712 : : else
8713 : 111292 : lto_wrapper_file = find_a_program ("lto-wrapper");
8714 : 111292 : if (lto_wrapper_file)
8715 : : {
8716 : 218084 : lto_wrapper_file = convert_white_space (lto_wrapper_file);
8717 : 109042 : set_static_spec_owned (<o_wrapper_spec, lto_wrapper_file);
8718 : 109042 : obstack_init (&collect_obstack);
8719 : 109042 : obstack_grow (&collect_obstack, "COLLECT_LTO_WRAPPER=",
8720 : : sizeof ("COLLECT_LTO_WRAPPER=") - 1);
8721 : 109042 : obstack_grow (&collect_obstack, lto_wrapper_spec,
8722 : : strlen (lto_wrapper_spec) + 1);
8723 : 109042 : xputenv (XOBFINISH (&collect_obstack, char *));
8724 : : }
8725 : :
8726 : 300112 : }
8727 : :
8728 : : /* Set up to remember the names of offload targets. */
8729 : :
8730 : : void
8731 : 300112 : driver::maybe_putenv_OFFLOAD_TARGETS () const
8732 : : {
8733 : 300112 : if (offload_targets && offload_targets[0] != '\0')
8734 : : {
8735 : 0 : obstack_grow (&collect_obstack, "OFFLOAD_TARGET_NAMES=",
8736 : : sizeof ("OFFLOAD_TARGET_NAMES=") - 1);
8737 : 0 : obstack_grow (&collect_obstack, offload_targets,
8738 : : strlen (offload_targets) + 1);
8739 : 0 : xputenv (XOBFINISH (&collect_obstack, char *));
8740 : : #if OFFLOAD_DEFAULTED
8741 : : if (offload_targets_default)
8742 : : xputenv ("OFFLOAD_TARGET_DEFAULT=1");
8743 : : #endif
8744 : : }
8745 : :
8746 : 300112 : free (offload_targets);
8747 : 300112 : offload_targets = NULL;
8748 : 300112 : }
8749 : :
8750 : : /* Reject switches that no pass was interested in. */
8751 : :
8752 : : void
8753 : 300112 : driver::handle_unrecognized_options ()
8754 : : {
8755 : 7046459 : for (size_t i = 0; (int) i < n_switches; i++)
8756 : 6746347 : if (! switches[i].validated)
8757 : : {
8758 : 590 : const char *hint = m_option_proposer.suggest_option (switches[i].part1);
8759 : 590 : if (hint)
8760 : 214 : error ("unrecognized command-line option %<-%s%>;"
8761 : : " did you mean %<-%s%>?",
8762 : 214 : switches[i].part1, hint);
8763 : : else
8764 : 376 : error ("unrecognized command-line option %<-%s%>",
8765 : 376 : switches[i].part1);
8766 : : }
8767 : 300112 : }
8768 : :
8769 : : /* Handle the various -print-* options, returning 0 if the driver
8770 : : should exit, or nonzero if the driver should continue. */
8771 : :
8772 : : int
8773 : 300107 : driver::maybe_print_and_exit () const
8774 : : {
8775 : 300107 : if (print_search_dirs)
8776 : : {
8777 : 56 : printf (_("install: %s%s\n"),
8778 : : gcc_exec_prefix ? gcc_exec_prefix : standard_exec_prefix,
8779 : 28 : gcc_exec_prefix ? "" : machine_suffix);
8780 : 28 : printf (_("programs: %s\n"),
8781 : : build_search_list (&exec_prefixes, "", false, false));
8782 : 28 : printf (_("libraries: %s\n"),
8783 : : build_search_list (&startfile_prefixes, "", false, true));
8784 : 28 : return (0);
8785 : : }
8786 : :
8787 : 300079 : if (print_file_name)
8788 : : {
8789 : 4515 : printf ("%s\n", find_file (print_file_name));
8790 : 4515 : return (0);
8791 : : }
8792 : :
8793 : 295564 : if (print_prog_name)
8794 : : {
8795 : 106 : if (use_ld != NULL && ! strcmp (print_prog_name, "ld"))
8796 : : {
8797 : : /* Append USE_LD to the default linker. */
8798 : : #ifdef DEFAULT_LINKER
8799 : : char *ld;
8800 : : # ifdef HAVE_HOST_EXECUTABLE_SUFFIX
8801 : : int len = (sizeof (DEFAULT_LINKER)
8802 : : - sizeof (HOST_EXECUTABLE_SUFFIX));
8803 : : ld = NULL;
8804 : : if (len > 0)
8805 : : {
8806 : : char *default_linker = xstrdup (DEFAULT_LINKER);
8807 : : /* Strip HOST_EXECUTABLE_SUFFIX if DEFAULT_LINKER contains
8808 : : HOST_EXECUTABLE_SUFFIX. */
8809 : : if (! strcmp (&default_linker[len], HOST_EXECUTABLE_SUFFIX))
8810 : : {
8811 : : default_linker[len] = '\0';
8812 : : ld = concat (default_linker, use_ld,
8813 : : HOST_EXECUTABLE_SUFFIX, NULL);
8814 : : }
8815 : : }
8816 : : if (ld == NULL)
8817 : : # endif
8818 : : ld = concat (DEFAULT_LINKER, use_ld, NULL);
8819 : : if (access (ld, X_OK) == 0)
8820 : : {
8821 : : printf ("%s\n", ld);
8822 : : return (0);
8823 : : }
8824 : : #endif
8825 : 0 : print_prog_name = concat (print_prog_name, use_ld, NULL);
8826 : : }
8827 : 106 : char *newname = find_a_program (print_prog_name);
8828 : 106 : printf ("%s\n", (newname ? newname : print_prog_name));
8829 : 106 : return (0);
8830 : : }
8831 : :
8832 : 295458 : if (print_multi_lib)
8833 : : {
8834 : 5019 : print_multilib_info ();
8835 : 5019 : return (0);
8836 : : }
8837 : :
8838 : 290439 : if (print_multi_directory)
8839 : : {
8840 : 4426 : if (multilib_dir == NULL)
8841 : 4401 : printf (".\n");
8842 : : else
8843 : 25 : printf ("%s\n", multilib_dir);
8844 : 4426 : return (0);
8845 : : }
8846 : :
8847 : 286013 : if (print_multiarch)
8848 : : {
8849 : 0 : if (multiarch_dir == NULL)
8850 : 0 : printf ("\n");
8851 : : else
8852 : 0 : printf ("%s\n", multiarch_dir);
8853 : 0 : return (0);
8854 : : }
8855 : :
8856 : 286013 : if (print_sysroot)
8857 : : {
8858 : 0 : if (target_system_root)
8859 : : {
8860 : 0 : if (target_sysroot_suffix)
8861 : 0 : printf ("%s%s\n", target_system_root, target_sysroot_suffix);
8862 : : else
8863 : 0 : printf ("%s\n", target_system_root);
8864 : : }
8865 : 0 : return (0);
8866 : : }
8867 : :
8868 : 286013 : if (print_multi_os_directory)
8869 : : {
8870 : 149 : if (multilib_os_dir == NULL)
8871 : 0 : printf (".\n");
8872 : : else
8873 : 149 : printf ("%s\n", multilib_os_dir);
8874 : 149 : return (0);
8875 : : }
8876 : :
8877 : 285864 : if (print_sysroot_headers_suffix)
8878 : : {
8879 : 1 : if (*sysroot_hdrs_suffix_spec)
8880 : : {
8881 : 0 : printf("%s\n", (target_sysroot_hdrs_suffix
8882 : : ? target_sysroot_hdrs_suffix
8883 : : : ""));
8884 : 0 : return (0);
8885 : : }
8886 : : else
8887 : : /* The error status indicates that only one set of fixed
8888 : : headers should be built. */
8889 : 1 : fatal_error (input_location,
8890 : : "not configured with sysroot headers suffix");
8891 : : }
8892 : :
8893 : 285863 : if (print_help_list)
8894 : : {
8895 : 4 : display_help ();
8896 : :
8897 : 4 : if (! verbose_flag)
8898 : : {
8899 : 1 : printf (_("\nFor bug reporting instructions, please see:\n"));
8900 : 1 : printf ("%s.\n", bug_report_url);
8901 : :
8902 : 1 : return (0);
8903 : : }
8904 : :
8905 : : /* We do not exit here. Instead we have created a fake input file
8906 : : called 'help-dummy' which needs to be compiled, and we pass this
8907 : : on the various sub-processes, along with the --help switch.
8908 : : Ensure their output appears after ours. */
8909 : 3 : fputc ('\n', stdout);
8910 : 3 : fflush (stdout);
8911 : : }
8912 : :
8913 : 285862 : if (print_version)
8914 : : {
8915 : 78 : printf (_("%s %s%s\n"), progname, pkgversion_string,
8916 : : version_string);
8917 : 78 : printf ("Copyright %s 2025 Free Software Foundation, Inc.\n",
8918 : : _("(C)"));
8919 : 78 : fputs (_("This is free software; see the source for copying conditions. There is NO\n\
8920 : : warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n"),
8921 : : stdout);
8922 : 78 : if (! verbose_flag)
8923 : : return 0;
8924 : :
8925 : : /* We do not exit here. We use the same mechanism of --help to print
8926 : : the version of the sub-processes. */
8927 : 0 : fputc ('\n', stdout);
8928 : 0 : fflush (stdout);
8929 : : }
8930 : :
8931 : 285784 : if (verbose_flag)
8932 : : {
8933 : 1479 : print_configuration (stderr);
8934 : 1479 : if (n_infiles == 0)
8935 : : return (0);
8936 : : }
8937 : :
8938 : : return 1;
8939 : : }
8940 : :
8941 : : /* Figure out what to do with each input file.
8942 : : Return true if we need to exit early from "main", false otherwise. */
8943 : :
8944 : : bool
8945 : 285095 : driver::prepare_infiles ()
8946 : : {
8947 : 285095 : size_t i;
8948 : 285095 : int lang_n_infiles = 0;
8949 : :
8950 : 285095 : if (n_infiles == added_libraries)
8951 : 194 : fatal_error (input_location, "no input files");
8952 : :
8953 : 284901 : if (seen_error ())
8954 : : /* Early exit needed from main. */
8955 : : return true;
8956 : :
8957 : : /* Make a place to record the compiler output file names
8958 : : that correspond to the input files. */
8959 : :
8960 : 284416 : i = n_infiles;
8961 : 284416 : i += lang_specific_extra_outfiles;
8962 : 284416 : outfiles = XCNEWVEC (const char *, i);
8963 : :
8964 : : /* Record which files were specified explicitly as link input. */
8965 : :
8966 : 284416 : explicit_link_files = XCNEWVEC (char, n_infiles);
8967 : :
8968 : 284416 : combine_inputs = have_o || flag_wpa;
8969 : :
8970 : 837886 : for (i = 0; (int) i < n_infiles; i++)
8971 : : {
8972 : 553470 : const char *name = infiles[i].name;
8973 : 553470 : struct compiler *compiler = lookup_compiler (name,
8974 : : strlen (name),
8975 : : infiles[i].language);
8976 : :
8977 : 553470 : if (compiler && !(compiler->combinable))
8978 : 254142 : combine_inputs = false;
8979 : :
8980 : 553470 : if (lang_n_infiles > 0 && compiler != input_file_compiler
8981 : 247307 : && infiles[i].language && infiles[i].language[0] != '*')
8982 : 33 : infiles[i].incompiler = compiler;
8983 : 553437 : else if (compiler)
8984 : : {
8985 : 295402 : lang_n_infiles++;
8986 : 295402 : input_file_compiler = compiler;
8987 : 295402 : infiles[i].incompiler = compiler;
8988 : : }
8989 : : else
8990 : : {
8991 : : /* Since there is no compiler for this input file, assume it is a
8992 : : linker file. */
8993 : 258035 : explicit_link_files[i] = 1;
8994 : 258035 : infiles[i].incompiler = NULL;
8995 : : }
8996 : 553470 : infiles[i].compiled = false;
8997 : 553470 : infiles[i].preprocessed = false;
8998 : : }
8999 : :
9000 : 284416 : if (!combine_inputs && have_c && have_o && lang_n_infiles > 1)
9001 : 0 : fatal_error (input_location,
9002 : : "cannot specify %<-o%> with %<-c%>, %<-S%> or %<-E%> "
9003 : : "with multiple files");
9004 : :
9005 : : /* No early exit needed from main; we can continue. */
9006 : : return false;
9007 : : }
9008 : :
9009 : : /* Run the spec machinery on each input file. */
9010 : :
9011 : : void
9012 : 284416 : driver::do_spec_on_infiles () const
9013 : : {
9014 : 284416 : size_t i;
9015 : :
9016 : 837886 : for (i = 0; (int) i < n_infiles; i++)
9017 : : {
9018 : 553470 : int this_file_error = 0;
9019 : :
9020 : : /* Tell do_spec what to substitute for %i. */
9021 : :
9022 : 553470 : input_file_number = i;
9023 : 553470 : set_input (infiles[i].name);
9024 : :
9025 : 553470 : if (infiles[i].compiled)
9026 : 9099 : continue;
9027 : :
9028 : : /* Use the same thing in %o, unless cp->spec says otherwise. */
9029 : :
9030 : 544371 : outfiles[i] = gcc_input_filename;
9031 : :
9032 : : /* Figure out which compiler from the file's suffix. */
9033 : :
9034 : 544371 : input_file_compiler
9035 : 544371 : = lookup_compiler (infiles[i].name, input_filename_length,
9036 : : infiles[i].language);
9037 : :
9038 : 544371 : if (input_file_compiler)
9039 : : {
9040 : : /* Ok, we found an applicable compiler. Run its spec. */
9041 : :
9042 : 286336 : if (input_file_compiler->spec[0] == '#')
9043 : : {
9044 : 0 : error ("%s: %s compiler not installed on this system",
9045 : : gcc_input_filename, &input_file_compiler->spec[1]);
9046 : 0 : this_file_error = 1;
9047 : : }
9048 : : else
9049 : : {
9050 : 286336 : int value;
9051 : :
9052 : 286336 : if (compare_debug)
9053 : : {
9054 : 617 : free (debug_check_temp_file[0]);
9055 : 617 : debug_check_temp_file[0] = NULL;
9056 : :
9057 : 617 : free (debug_check_temp_file[1]);
9058 : 617 : debug_check_temp_file[1] = NULL;
9059 : : }
9060 : :
9061 : 286336 : value = do_spec (input_file_compiler->spec);
9062 : 286336 : infiles[i].compiled = true;
9063 : 286336 : if (value < 0)
9064 : : this_file_error = 1;
9065 : 257261 : else if (compare_debug && debug_check_temp_file[0])
9066 : : {
9067 : 613 : if (verbose_flag)
9068 : 0 : inform (UNKNOWN_LOCATION,
9069 : : "recompiling with %<-fcompare-debug%>");
9070 : :
9071 : 613 : compare_debug = -compare_debug;
9072 : 613 : n_switches = n_switches_debug_check[1];
9073 : 613 : n_switches_alloc = n_switches_alloc_debug_check[1];
9074 : 613 : switches = switches_debug_check[1];
9075 : :
9076 : 613 : value = do_spec (input_file_compiler->spec);
9077 : :
9078 : 613 : compare_debug = -compare_debug;
9079 : 613 : n_switches = n_switches_debug_check[0];
9080 : 613 : n_switches_alloc = n_switches_alloc_debug_check[0];
9081 : 613 : switches = switches_debug_check[0];
9082 : :
9083 : 613 : if (value < 0)
9084 : : {
9085 : 2 : error ("during %<-fcompare-debug%> recompilation");
9086 : 2 : this_file_error = 1;
9087 : : }
9088 : :
9089 : 613 : gcc_assert (debug_check_temp_file[1]
9090 : : && filename_cmp (debug_check_temp_file[0],
9091 : : debug_check_temp_file[1]));
9092 : :
9093 : 613 : if (verbose_flag)
9094 : 0 : inform (UNKNOWN_LOCATION, "comparing final insns dumps");
9095 : :
9096 : 613 : if (compare_files (debug_check_temp_file))
9097 : 29106 : this_file_error = 1;
9098 : : }
9099 : :
9100 : 286336 : if (compare_debug)
9101 : : {
9102 : 617 : free (debug_check_temp_file[0]);
9103 : 617 : debug_check_temp_file[0] = NULL;
9104 : :
9105 : 617 : free (debug_check_temp_file[1]);
9106 : 617 : debug_check_temp_file[1] = NULL;
9107 : : }
9108 : : }
9109 : : }
9110 : :
9111 : : /* If this file's name does not contain a recognized suffix,
9112 : : record it as explicit linker input. */
9113 : :
9114 : : else
9115 : 258035 : explicit_link_files[i] = 1;
9116 : :
9117 : : /* Clear the delete-on-failure queue, deleting the files in it
9118 : : if this compilation failed. */
9119 : :
9120 : 544371 : if (this_file_error)
9121 : : {
9122 : 29106 : delete_failure_queue ();
9123 : 29106 : errorcount++;
9124 : : }
9125 : : /* If this compilation succeeded, don't delete those files later. */
9126 : 544371 : clear_failure_queue ();
9127 : : }
9128 : :
9129 : : /* Reset the input file name to the first compile/object file name, for use
9130 : : with %b in LINK_SPEC. We use the first input file that we can find
9131 : : a compiler to compile it instead of using infiles.language since for
9132 : : languages other than C we use aliases that we then lookup later. */
9133 : 284416 : if (n_infiles > 0)
9134 : : {
9135 : : int i;
9136 : :
9137 : 296575 : for (i = 0; i < n_infiles ; i++)
9138 : 294690 : if (infiles[i].incompiler
9139 : 12159 : || (infiles[i].language && infiles[i].language[0] != '*'))
9140 : : {
9141 : 282531 : set_input (infiles[i].name);
9142 : 282531 : break;
9143 : : }
9144 : : }
9145 : :
9146 : 284416 : if (!seen_error ())
9147 : : {
9148 : : /* Make sure INPUT_FILE_NUMBER points to first available open
9149 : : slot. */
9150 : 255310 : input_file_number = n_infiles;
9151 : 255310 : if (lang_specific_pre_link ())
9152 : 0 : errorcount++;
9153 : : }
9154 : 284416 : }
9155 : :
9156 : : /* If we have to run the linker, do it now. */
9157 : :
9158 : : void
9159 : 284416 : driver::maybe_run_linker (const char *argv0) const
9160 : : {
9161 : 284416 : size_t i;
9162 : 284416 : int linker_was_run = 0;
9163 : 284416 : int num_linker_inputs;
9164 : :
9165 : : /* Determine if there are any linker input files. */
9166 : 284416 : num_linker_inputs = 0;
9167 : 837886 : for (i = 0; (int) i < n_infiles; i++)
9168 : 553470 : if (explicit_link_files[i] || outfiles[i] != NULL)
9169 : 543911 : num_linker_inputs++;
9170 : :
9171 : : /* Arrange for temporary file names created during linking to take
9172 : : on names related with the linker output rather than with the
9173 : : inputs when appropriate. */
9174 : 284416 : if (outbase && *outbase)
9175 : : {
9176 : 261423 : if (dumpdir)
9177 : : {
9178 : 87937 : char *tofree = dumpdir;
9179 : 87937 : gcc_checking_assert (strlen (dumpdir) == dumpdir_length);
9180 : 87937 : dumpdir = concat (dumpdir, outbase, ".", NULL);
9181 : 87937 : free (tofree);
9182 : : }
9183 : : else
9184 : 173486 : dumpdir = concat (outbase, ".", NULL);
9185 : 261423 : dumpdir_length += strlen (outbase) + 1;
9186 : 261423 : dumpdir_trailing_dash_added = true;
9187 : 261423 : }
9188 : 22993 : else if (dumpdir_trailing_dash_added)
9189 : : {
9190 : 18072 : gcc_assert (dumpdir[dumpdir_length - 1] == '-');
9191 : 18072 : dumpdir[dumpdir_length - 1] = '.';
9192 : : }
9193 : :
9194 : 284416 : if (dumpdir_trailing_dash_added)
9195 : : {
9196 : 279495 : gcc_assert (dumpdir_length > 0);
9197 : 279495 : gcc_assert (dumpdir[dumpdir_length - 1] == '.');
9198 : 279495 : dumpdir_length--;
9199 : : }
9200 : :
9201 : 284416 : free (outbase);
9202 : 284416 : input_basename = outbase = NULL;
9203 : 284416 : outbase_length = suffixed_basename_length = basename_length = 0;
9204 : :
9205 : : /* Run ld to link all the compiler output files. */
9206 : :
9207 : 284416 : if (num_linker_inputs > 0 && !seen_error () && print_subprocess_help < 2)
9208 : : {
9209 : 254753 : int tmp = execution_count;
9210 : :
9211 : 254753 : detect_jobserver ();
9212 : :
9213 : 254753 : if (! have_c)
9214 : : {
9215 : : #if HAVE_LTO_PLUGIN > 0
9216 : : #if HAVE_LTO_PLUGIN == 2
9217 : 95907 : const char *fno_use_linker_plugin = "fno-use-linker-plugin";
9218 : : #else
9219 : : const char *fuse_linker_plugin = "fuse-linker-plugin";
9220 : : #endif
9221 : : #endif
9222 : :
9223 : : /* We'll use ld if we can't find collect2. */
9224 : 95907 : if (! strcmp (linker_name_spec, "collect2"))
9225 : : {
9226 : 95907 : char *s = find_a_program ("collect2");
9227 : 95907 : if (s == NULL)
9228 : 1104 : set_static_spec_shared (&linker_name_spec, "ld");
9229 : : }
9230 : :
9231 : : #if HAVE_LTO_PLUGIN > 0
9232 : : #if HAVE_LTO_PLUGIN == 2
9233 : 95907 : if (!switch_matches (fno_use_linker_plugin,
9234 : : fno_use_linker_plugin
9235 : : + strlen (fno_use_linker_plugin), 0))
9236 : : #else
9237 : : if (switch_matches (fuse_linker_plugin,
9238 : : fuse_linker_plugin
9239 : : + strlen (fuse_linker_plugin), 0))
9240 : : #endif
9241 : : {
9242 : 90493 : char *temp_spec = find_a_file (&exec_prefixes,
9243 : : LTOPLUGINSONAME, R_OK,
9244 : : false);
9245 : 90493 : if (!temp_spec)
9246 : 0 : fatal_error (input_location,
9247 : : "%<-fuse-linker-plugin%>, but %s not found",
9248 : : LTOPLUGINSONAME);
9249 : 90493 : linker_plugin_file_spec = convert_white_space (temp_spec);
9250 : : }
9251 : : #endif
9252 : 95907 : set_static_spec_shared (<o_gcc_spec, argv0);
9253 : : }
9254 : :
9255 : : /* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables
9256 : : for collect. */
9257 : 254753 : putenv_from_prefixes (&exec_prefixes, "COMPILER_PATH", false);
9258 : 254753 : putenv_from_prefixes (&startfile_prefixes, LIBRARY_PATH_ENV, true);
9259 : :
9260 : 254753 : if (print_subprocess_help == 1)
9261 : : {
9262 : 0 : printf (_("\nLinker options\n==============\n\n"));
9263 : 0 : printf (_("Use \"-Wl,OPTION\" to pass \"OPTION\""
9264 : : " to the linker.\n\n"));
9265 : 0 : fflush (stdout);
9266 : : }
9267 : 254753 : int value = do_spec (link_command_spec);
9268 : 254750 : if (value < 0)
9269 : 139 : errorcount = 1;
9270 : 254750 : linker_was_run = (tmp != execution_count);
9271 : : }
9272 : :
9273 : : /* If options said don't run linker,
9274 : : complain about input files to be given to the linker. */
9275 : :
9276 : 284413 : if (! linker_was_run && !seen_error ())
9277 : 350766 : for (i = 0; (int) i < n_infiles; i++)
9278 : 191359 : if (explicit_link_files[i]
9279 : 22871 : && !(infiles[i].language && infiles[i].language[0] == '*'))
9280 : : {
9281 : 38 : warning (0, "%s: linker input file unused because linking not done",
9282 : 19 : outfiles[i]);
9283 : 19 : if (access (outfiles[i], F_OK) < 0)
9284 : : /* This is can be an indication the user specifed an errorneous
9285 : : separated option value, (or used the wrong prefix for an
9286 : : option). */
9287 : 7 : error ("%s: linker input file not found: %m", outfiles[i]);
9288 : : }
9289 : 284413 : }
9290 : :
9291 : : /* The end of "main". */
9292 : :
9293 : : void
9294 : 284413 : driver::final_actions () const
9295 : : {
9296 : : /* Delete some or all of the temporary files we made. */
9297 : :
9298 : 284413 : if (seen_error ())
9299 : 29250 : delete_failure_queue ();
9300 : 284413 : delete_temp_files ();
9301 : :
9302 : 284413 : if (totruncate_file != NULL && !seen_error ())
9303 : : /* Truncate file specified by -truncate.
9304 : : Used by lto-wrapper to reduce temporary disk-space usage. */
9305 : 9209 : truncate(totruncate_file, 0);
9306 : :
9307 : 284413 : if (print_help_list)
9308 : : {
9309 : 3 : printf (("\nFor bug reporting instructions, please see:\n"));
9310 : 3 : printf ("%s\n", bug_report_url);
9311 : : }
9312 : 284413 : }
9313 : :
9314 : : /* Detect whether jobserver is active and working. If not drop
9315 : : --jobserver-auth from MAKEFLAGS. */
9316 : :
9317 : : void
9318 : 254753 : driver::detect_jobserver () const
9319 : : {
9320 : 254753 : jobserver_info jinfo;
9321 : 254753 : if (!jinfo.is_active && !jinfo.skipped_makeflags.empty ())
9322 : 0 : xputenv (xstrdup (jinfo.skipped_makeflags.c_str ()));
9323 : 254753 : }
9324 : :
9325 : : /* Determine what the exit code of the driver should be. */
9326 : :
9327 : : int
9328 : 284898 : driver::get_exit_code () const
9329 : : {
9330 : 284898 : return (signal_count != 0 ? 2
9331 : 284898 : : seen_error () ? (pass_exit_codes ? greatest_status : 1)
9332 : 0 : : 0);
9333 : : }
9334 : :
9335 : : /* Find the proper compilation spec for the file name NAME,
9336 : : whose length is LENGTH. LANGUAGE is the specified language,
9337 : : or 0 if this file is to be passed to the linker. */
9338 : :
9339 : : static struct compiler *
9340 : 1097841 : lookup_compiler (const char *name, size_t length, const char *language)
9341 : : {
9342 : 1603580 : struct compiler *cp;
9343 : :
9344 : : /* If this was specified by the user to be a linker input, indicate that. */
9345 : 1603580 : if (language != 0 && language[0] == '*')
9346 : : return 0;
9347 : :
9348 : : /* Otherwise, look for the language, if one is spec'd. */
9349 : 1135332 : if (language != 0)
9350 : : {
9351 : 22983864 : for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
9352 : 22983864 : if (cp->suffix[0] == '@' && !strcmp (cp->suffix + 1, language))
9353 : : {
9354 : 581757 : if (name != NULL && strcmp (name, "-") == 0
9355 : 2068 : && (strcmp (cp->suffix, "@c-header") == 0
9356 : 2068 : || strcmp (cp->suffix, "@c++-header") == 0)
9357 : 0 : && !have_E)
9358 : 0 : fatal_error (input_location,
9359 : : "cannot use %<-%> as input filename for a "
9360 : : "precompiled header");
9361 : :
9362 : : return cp;
9363 : : }
9364 : :
9365 : 0 : error ("language %s not recognized", language);
9366 : 0 : return 0;
9367 : : }
9368 : :
9369 : : /* Look for a suffix. */
9370 : 30464889 : for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
9371 : : {
9372 : 30417067 : if (/* The suffix `-' matches only the file name `-'. */
9373 : 30417067 : (!strcmp (cp->suffix, "-") && !strcmp (name, "-"))
9374 : 30417053 : || (strlen (cp->suffix) < length
9375 : : /* See if the suffix matches the end of NAME. */
9376 : 29991028 : && !strcmp (cp->suffix,
9377 : 29991028 : name + length - strlen (cp->suffix))
9378 : : ))
9379 : : break;
9380 : : }
9381 : :
9382 : : #if defined (OS2) ||defined (HAVE_DOS_BASED_FILE_SYSTEM)
9383 : : /* Look again, but case-insensitively this time. */
9384 : : if (cp < compilers)
9385 : : for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
9386 : : {
9387 : : if (/* The suffix `-' matches only the file name `-'. */
9388 : : (!strcmp (cp->suffix, "-") && !strcmp (name, "-"))
9389 : : || (strlen (cp->suffix) < length
9390 : : /* See if the suffix matches the end of NAME. */
9391 : : && ((!strcmp (cp->suffix,
9392 : : name + length - strlen (cp->suffix))
9393 : : || !strpbrk (cp->suffix, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
9394 : : && !strcasecmp (cp->suffix,
9395 : : name + length - strlen (cp->suffix)))
9396 : : ))
9397 : : break;
9398 : : }
9399 : : #endif
9400 : :
9401 : 553575 : if (cp >= compilers)
9402 : : {
9403 : 505753 : if (cp->spec[0] != '@')
9404 : : /* A non-alias entry: return it. */
9405 : : return cp;
9406 : :
9407 : : /* An alias entry maps a suffix to a language.
9408 : : Search for the language; pass 0 for NAME and LENGTH
9409 : : to avoid infinite recursion if language not found. */
9410 : 505739 : return lookup_compiler (NULL, 0, cp->spec + 1);
9411 : : }
9412 : : return 0;
9413 : : }
9414 : :
9415 : : static char *
9416 : 46577318 : save_string (const char *s, int len)
9417 : : {
9418 : 46577318 : char *result = XNEWVEC (char, len + 1);
9419 : :
9420 : 46577318 : gcc_checking_assert (strlen (s) >= (unsigned int) len);
9421 : 46577318 : memcpy (result, s, len);
9422 : 46577318 : result[len] = 0;
9423 : 46577318 : return result;
9424 : : }
9425 : :
9426 : :
9427 : : static inline void
9428 : 46217248 : validate_switches_from_spec (const char *spec, bool user)
9429 : : {
9430 : 46217248 : const char *p = spec;
9431 : 46217248 : char c;
9432 : 572913811 : while ((c = *p++))
9433 : 480479315 : if (c == '%'
9434 : 480479315 : && (*p == '{'
9435 : 11404256 : || *p == '<'
9436 : 10503920 : || (*p == 'W' && *++p == '{')
9437 : 10503920 : || (*p == '@' && *++p == '{')))
9438 : : /* We have a switch spec. */
9439 : 45617026 : p = validate_switches (p + 1, user, *p == '{');
9440 : 46217248 : }
9441 : :
9442 : : static void
9443 : 300112 : validate_all_switches (void)
9444 : : {
9445 : 300112 : struct compiler *comp;
9446 : 300112 : struct spec_list *spec;
9447 : :
9448 : 32412096 : for (comp = compilers; comp->spec; comp++)
9449 : 32111984 : validate_switches_from_spec (comp->spec, false);
9450 : :
9451 : : /* Look through the linked list of specs read from the specs file. */
9452 : 14105264 : for (spec = specs; spec; spec = spec->next)
9453 : 13805152 : validate_switches_from_spec (*spec->ptr_spec, spec->user_p);
9454 : :
9455 : 300112 : validate_switches_from_spec (link_command_spec, false);
9456 : 300112 : }
9457 : :
9458 : : /* Look at the switch-name that comes after START and mark as valid
9459 : : all supplied switches that match it. If BRACED, handle other
9460 : : switches after '|' and '&', and specs after ':' until ';' or '}',
9461 : : going back for more switches after ';'. Without BRACED, handle
9462 : : only one atom. Return a pointer to whatever follows the handled
9463 : : items, after the closing brace if BRACED. */
9464 : :
9465 : : static const char *
9466 : 195072802 : validate_switches (const char *start, bool user_spec, bool braced)
9467 : : {
9468 : 195072802 : const char *p = start;
9469 : 251793970 : const char *atom;
9470 : 251793970 : size_t len;
9471 : 251793970 : int i;
9472 : 251793970 : bool suffix;
9473 : 251793970 : bool starred;
9474 : :
9475 : : #define SKIP_WHITE() do { while (*p == ' ' || *p == '\t') p++; } while (0)
9476 : :
9477 : 251793970 : next_member:
9478 : 251793970 : suffix = false;
9479 : 251793970 : starred = false;
9480 : :
9481 : 278804050 : SKIP_WHITE ();
9482 : :
9483 : 251793970 : if (*p == '!')
9484 : 76828673 : p++;
9485 : :
9486 : 251793970 : SKIP_WHITE ();
9487 : 251793970 : if (*p == '.' || *p == ',')
9488 : 0 : suffix = true, p++;
9489 : :
9490 : 251793970 : atom = p;
9491 : 251793970 : while (ISIDNUM (*p) || *p == '-' || *p == '+' || *p == '='
9492 : 1670123296 : || *p == ',' || *p == '.' || *p == '@')
9493 : 1418329326 : p++;
9494 : 251793970 : len = p - atom;
9495 : :
9496 : 251793970 : if (*p == '*')
9497 : 66024640 : starred = true, p++;
9498 : :
9499 : 252994418 : SKIP_WHITE ();
9500 : :
9501 : 251793970 : if (!suffix)
9502 : : {
9503 : : /* Mark all matching switches as valid. */
9504 : 5911979133 : for (i = 0; i < n_switches; i++)
9505 : 5660185163 : if (!strncmp (switches[i].part1, atom, len)
9506 : 481868296 : && (starred || switches[i].part1[len] == '\0')
9507 : 49990790 : && (switches[i].known || user_spec))
9508 : 49988378 : switches[i].validated = true;
9509 : : }
9510 : :
9511 : 251793970 : if (!braced)
9512 : : return p;
9513 : :
9514 : 250293410 : if (*p) p++;
9515 : 250293410 : if (*p && (p[-1] == '|' || p[-1] == '&'))
9516 : 38414336 : goto next_member;
9517 : :
9518 : 211879074 : if (*p && p[-1] == ':')
9519 : : {
9520 : 2825854588 : while (*p && *p != ';' && *p != '}')
9521 : : {
9522 : 2663493993 : if (*p == '%')
9523 : : {
9524 : 249393072 : p++;
9525 : 249393072 : if (*p == '{' || *p == '<')
9526 : 146754768 : p = validate_switches (p+1, user_spec, *p == '{');
9527 : 102638304 : else if (p[0] == 'W' && p[1] == '{')
9528 : 2400896 : p = validate_switches (p+2, user_spec, true);
9529 : 100237408 : else if (p[0] == '@' && p[1] == '{')
9530 : 300112 : p = validate_switches (p+2, user_spec, true);
9531 : : }
9532 : : else
9533 : 2414100921 : p++;
9534 : : }
9535 : :
9536 : 162360595 : if (*p) p++;
9537 : 162360595 : if (*p && p[-1] == ';')
9538 : 18306832 : goto next_member;
9539 : : }
9540 : :
9541 : : return p;
9542 : : #undef SKIP_WHITE
9543 : : }
9544 : :
9545 : : struct mdswitchstr
9546 : : {
9547 : : const char *str;
9548 : : int len;
9549 : : };
9550 : :
9551 : : static struct mdswitchstr *mdswitches;
9552 : : static int n_mdswitches;
9553 : :
9554 : : /* Check whether a particular argument was used. The first time we
9555 : : canonicalize the switches to keep only the ones we care about. */
9556 : :
9557 : : struct used_arg_t
9558 : : {
9559 : : public:
9560 : : int operator () (const char *p, int len);
9561 : : void finalize ();
9562 : :
9563 : : private:
9564 : : struct mswitchstr
9565 : : {
9566 : : const char *str;
9567 : : const char *replace;
9568 : : int len;
9569 : : int rep_len;
9570 : : };
9571 : :
9572 : : mswitchstr *mswitches;
9573 : : int n_mswitches;
9574 : :
9575 : : };
9576 : :
9577 : : used_arg_t used_arg;
9578 : :
9579 : : int
9580 : 1814070 : used_arg_t::operator () (const char *p, int len)
9581 : : {
9582 : 1814070 : int i, j;
9583 : :
9584 : 1814070 : if (!mswitches)
9585 : : {
9586 : 300112 : struct mswitchstr *matches;
9587 : 300112 : const char *q;
9588 : 300112 : int cnt = 0;
9589 : :
9590 : : /* Break multilib_matches into the component strings of string
9591 : : and replacement string. */
9592 : 5101904 : for (q = multilib_matches; *q != '\0'; q++)
9593 : 4801792 : if (*q == ';')
9594 : 600224 : cnt++;
9595 : :
9596 : 300112 : matches
9597 : 300112 : = (struct mswitchstr *) alloca ((sizeof (struct mswitchstr)) * cnt);
9598 : 300112 : i = 0;
9599 : 300112 : q = multilib_matches;
9600 : 900336 : while (*q != '\0')
9601 : : {
9602 : 600224 : matches[i].str = q;
9603 : 2400896 : while (*q != ' ')
9604 : : {
9605 : 1800672 : if (*q == '\0')
9606 : : {
9607 : 0 : invalid_matches:
9608 : 0 : fatal_error (input_location, "multilib spec %qs is invalid",
9609 : : multilib_matches);
9610 : : }
9611 : 1800672 : q++;
9612 : : }
9613 : 600224 : matches[i].len = q - matches[i].str;
9614 : :
9615 : 600224 : matches[i].replace = ++q;
9616 : 2400896 : while (*q != ';' && *q != '\0')
9617 : : {
9618 : 1800672 : if (*q == ' ')
9619 : 0 : goto invalid_matches;
9620 : 1800672 : q++;
9621 : : }
9622 : 600224 : matches[i].rep_len = q - matches[i].replace;
9623 : 600224 : i++;
9624 : 600224 : if (*q == ';')
9625 : 600224 : q++;
9626 : : }
9627 : :
9628 : : /* Now build a list of the replacement string for switches that we care
9629 : : about. Make sure we allocate at least one entry. This prevents
9630 : : xmalloc from calling fatal, and prevents us from re-executing this
9631 : : block of code. */
9632 : 300112 : mswitches
9633 : 600224 : = XNEWVEC (struct mswitchstr, n_mdswitches + (n_switches ? n_switches : 1));
9634 : 7046459 : for (i = 0; i < n_switches; i++)
9635 : 6746347 : if ((switches[i].live_cond & SWITCH_IGNORE) == 0)
9636 : : {
9637 : 6746341 : int xlen = strlen (switches[i].part1);
9638 : 20226289 : for (j = 0; j < cnt; j++)
9639 : 13490268 : if (xlen == matches[j].len
9640 : 19046 : && ! strncmp (switches[i].part1, matches[j].str, xlen))
9641 : : {
9642 : 10320 : mswitches[n_mswitches].str = matches[j].replace;
9643 : 10320 : mswitches[n_mswitches].len = matches[j].rep_len;
9644 : 10320 : mswitches[n_mswitches].replace = (char *) 0;
9645 : 10320 : mswitches[n_mswitches].rep_len = 0;
9646 : 10320 : n_mswitches++;
9647 : 10320 : break;
9648 : : }
9649 : : }
9650 : :
9651 : : /* Add MULTILIB_DEFAULTS switches too, as long as they were not present
9652 : : on the command line nor any options mutually incompatible with
9653 : : them. */
9654 : 600224 : for (i = 0; i < n_mdswitches; i++)
9655 : : {
9656 : 300112 : const char *r;
9657 : :
9658 : 600224 : for (q = multilib_options; *q != '\0'; *q && q++)
9659 : : {
9660 : 300112 : while (*q == ' ')
9661 : 0 : q++;
9662 : :
9663 : 300112 : r = q;
9664 : 300112 : while (strncmp (q, mdswitches[i].str, mdswitches[i].len) != 0
9665 : 300112 : || strchr (" /", q[mdswitches[i].len]) == NULL)
9666 : : {
9667 : 0 : while (*q != ' ' && *q != '/' && *q != '\0')
9668 : 0 : q++;
9669 : 0 : if (*q != '/')
9670 : : break;
9671 : 0 : q++;
9672 : : }
9673 : :
9674 : 300112 : if (*q != ' ' && *q != '\0')
9675 : : {
9676 : 597810 : while (*r != ' ' && *r != '\0')
9677 : : {
9678 : : q = r;
9679 : 2391240 : while (*q != ' ' && *q != '/' && *q != '\0')
9680 : 1793430 : q++;
9681 : :
9682 : 597810 : if (used_arg (r, q - r))
9683 : : break;
9684 : :
9685 : 587490 : if (*q != '/')
9686 : : {
9687 : 289792 : mswitches[n_mswitches].str = mdswitches[i].str;
9688 : 289792 : mswitches[n_mswitches].len = mdswitches[i].len;
9689 : 289792 : mswitches[n_mswitches].replace = (char *) 0;
9690 : 289792 : mswitches[n_mswitches].rep_len = 0;
9691 : 289792 : n_mswitches++;
9692 : 289792 : break;
9693 : : }
9694 : :
9695 : 297698 : r = q + 1;
9696 : : }
9697 : : break;
9698 : : }
9699 : : }
9700 : : }
9701 : : }
9702 : :
9703 : 2430106 : for (i = 0; i < n_mswitches; i++)
9704 : 1234486 : if (len == mswitches[i].len && ! strncmp (p, mswitches[i].str, len))
9705 : : return 1;
9706 : :
9707 : : return 0;
9708 : : }
9709 : :
9710 : 1109 : void used_arg_t::finalize ()
9711 : : {
9712 : 1109 : XDELETEVEC (mswitches);
9713 : 1109 : mswitches = NULL;
9714 : 1109 : n_mswitches = 0;
9715 : 1109 : }
9716 : :
9717 : :
9718 : : static int
9719 : 1236336 : default_arg (const char *p, int len)
9720 : : {
9721 : 1236336 : int i;
9722 : :
9723 : 1849485 : for (i = 0; i < n_mdswitches; i++)
9724 : 1236336 : if (len == mdswitches[i].len && ! strncmp (p, mdswitches[i].str, len))
9725 : : return 1;
9726 : :
9727 : : return 0;
9728 : : }
9729 : :
9730 : : /* Use multilib_dir as key to find corresponding multilib_os_dir and
9731 : : multiarch_dir. */
9732 : :
9733 : : static void
9734 : 0 : find_multilib_os_dir_by_multilib_dir (const char *multilib_dir,
9735 : : const char **p_multilib_os_dir,
9736 : : const char **p_multiarch_dir)
9737 : : {
9738 : 0 : const char *p = multilib_select;
9739 : 0 : unsigned int this_path_len;
9740 : 0 : const char *this_path;
9741 : 0 : int ok = 0;
9742 : :
9743 : 0 : while (*p != '\0')
9744 : : {
9745 : : /* Ignore newlines. */
9746 : 0 : if (*p == '\n')
9747 : : {
9748 : 0 : ++p;
9749 : 0 : continue;
9750 : : }
9751 : :
9752 : : /* Get the initial path. */
9753 : : this_path = p;
9754 : 0 : while (*p != ' ')
9755 : : {
9756 : 0 : if (*p == '\0')
9757 : : {
9758 : 0 : fatal_error (input_location, "multilib select %qs %qs is invalid",
9759 : : multilib_select, multilib_reuse);
9760 : : }
9761 : 0 : ++p;
9762 : : }
9763 : 0 : this_path_len = p - this_path;
9764 : :
9765 : 0 : ok = 0;
9766 : :
9767 : : /* Skip any arguments, we don't care at this stage. */
9768 : 0 : while (*++p != ';');
9769 : :
9770 : 0 : if (this_path_len != 1
9771 : 0 : || this_path[0] != '.')
9772 : : {
9773 : 0 : char *new_multilib_dir = XNEWVEC (char, this_path_len + 1);
9774 : 0 : char *q;
9775 : :
9776 : 0 : strncpy (new_multilib_dir, this_path, this_path_len);
9777 : 0 : new_multilib_dir[this_path_len] = '\0';
9778 : 0 : q = strchr (new_multilib_dir, ':');
9779 : 0 : if (q != NULL)
9780 : 0 : *q = '\0';
9781 : :
9782 : 0 : if (strcmp (new_multilib_dir, multilib_dir) == 0)
9783 : 0 : ok = 1;
9784 : : }
9785 : :
9786 : : /* Found matched multilib_dir, update multilib_os_dir and
9787 : : multiarch_dir. */
9788 : 0 : if (ok)
9789 : : {
9790 : 0 : const char *q = this_path, *end = this_path + this_path_len;
9791 : :
9792 : 0 : while (q < end && *q != ':')
9793 : 0 : q++;
9794 : 0 : if (q < end)
9795 : : {
9796 : 0 : const char *q2 = q + 1, *ml_end = end;
9797 : 0 : char *new_multilib_os_dir;
9798 : :
9799 : 0 : while (q2 < end && *q2 != ':')
9800 : 0 : q2++;
9801 : 0 : if (*q2 == ':')
9802 : 0 : ml_end = q2;
9803 : 0 : if (ml_end - q == 1)
9804 : 0 : *p_multilib_os_dir = xstrdup (".");
9805 : : else
9806 : : {
9807 : 0 : new_multilib_os_dir = XNEWVEC (char, ml_end - q);
9808 : 0 : memcpy (new_multilib_os_dir, q + 1, ml_end - q - 1);
9809 : 0 : new_multilib_os_dir[ml_end - q - 1] = '\0';
9810 : 0 : *p_multilib_os_dir = new_multilib_os_dir;
9811 : : }
9812 : :
9813 : 0 : if (q2 < end && *q2 == ':')
9814 : : {
9815 : 0 : char *new_multiarch_dir = XNEWVEC (char, end - q2);
9816 : 0 : memcpy (new_multiarch_dir, q2 + 1, end - q2 - 1);
9817 : 0 : new_multiarch_dir[end - q2 - 1] = '\0';
9818 : 0 : *p_multiarch_dir = new_multiarch_dir;
9819 : : }
9820 : : break;
9821 : : }
9822 : : }
9823 : 0 : ++p;
9824 : : }
9825 : 0 : }
9826 : :
9827 : : /* Work out the subdirectory to use based on the options. The format of
9828 : : multilib_select is a list of elements. Each element is a subdirectory
9829 : : name followed by a list of options followed by a semicolon. The format
9830 : : of multilib_exclusions is the same, but without the preceding
9831 : : directory. First gcc will check the exclusions, if none of the options
9832 : : beginning with an exclamation point are present, and all of the other
9833 : : options are present, then we will ignore this completely. Passing
9834 : : that, gcc will consider each multilib_select in turn using the same
9835 : : rules for matching the options. If a match is found, that subdirectory
9836 : : will be used.
9837 : : A subdirectory name is optionally followed by a colon and the corresponding
9838 : : multiarch name. */
9839 : :
9840 : : static void
9841 : 300112 : set_multilib_dir (void)
9842 : : {
9843 : 300112 : const char *p;
9844 : 300112 : unsigned int this_path_len;
9845 : 300112 : const char *this_path, *this_arg;
9846 : 300112 : const char *start, *end;
9847 : 300112 : int not_arg;
9848 : 300112 : int ok, ndfltok, first;
9849 : :
9850 : 300112 : n_mdswitches = 0;
9851 : 300112 : start = multilib_defaults;
9852 : 300112 : while (*start == ' ' || *start == '\t')
9853 : 0 : start++;
9854 : 600224 : while (*start != '\0')
9855 : : {
9856 : 300112 : n_mdswitches++;
9857 : 1200448 : while (*start != ' ' && *start != '\t' && *start != '\0')
9858 : 900336 : start++;
9859 : 300112 : while (*start == ' ' || *start == '\t')
9860 : 0 : start++;
9861 : : }
9862 : :
9863 : 300112 : if (n_mdswitches)
9864 : : {
9865 : 300112 : int i = 0;
9866 : :
9867 : 300112 : mdswitches = XNEWVEC (struct mdswitchstr, n_mdswitches);
9868 : 300112 : for (start = multilib_defaults; *start != '\0'; start = end + 1)
9869 : : {
9870 : 300112 : while (*start == ' ' || *start == '\t')
9871 : 0 : start++;
9872 : :
9873 : 300112 : if (*start == '\0')
9874 : : break;
9875 : :
9876 : 900336 : for (end = start + 1;
9877 : 900336 : *end != ' ' && *end != '\t' && *end != '\0'; end++)
9878 : : ;
9879 : :
9880 : 300112 : obstack_grow (&multilib_obstack, start, end - start);
9881 : 300112 : obstack_1grow (&multilib_obstack, 0);
9882 : 300112 : mdswitches[i].str = XOBFINISH (&multilib_obstack, const char *);
9883 : 300112 : mdswitches[i++].len = end - start;
9884 : :
9885 : 300112 : if (*end == '\0')
9886 : : break;
9887 : : }
9888 : : }
9889 : :
9890 : 300112 : p = multilib_exclusions;
9891 : 300112 : while (*p != '\0')
9892 : : {
9893 : : /* Ignore newlines. */
9894 : 0 : if (*p == '\n')
9895 : : {
9896 : 0 : ++p;
9897 : 0 : continue;
9898 : : }
9899 : :
9900 : : /* Check the arguments. */
9901 : : ok = 1;
9902 : 0 : while (*p != ';')
9903 : : {
9904 : 0 : if (*p == '\0')
9905 : : {
9906 : 0 : invalid_exclusions:
9907 : 0 : fatal_error (input_location, "multilib exclusions %qs is invalid",
9908 : : multilib_exclusions);
9909 : : }
9910 : :
9911 : 0 : if (! ok)
9912 : : {
9913 : 0 : ++p;
9914 : 0 : continue;
9915 : : }
9916 : :
9917 : 0 : this_arg = p;
9918 : 0 : while (*p != ' ' && *p != ';')
9919 : : {
9920 : 0 : if (*p == '\0')
9921 : 0 : goto invalid_exclusions;
9922 : 0 : ++p;
9923 : : }
9924 : :
9925 : 0 : if (*this_arg != '!')
9926 : : not_arg = 0;
9927 : : else
9928 : : {
9929 : 0 : not_arg = 1;
9930 : 0 : ++this_arg;
9931 : : }
9932 : :
9933 : 0 : ok = used_arg (this_arg, p - this_arg);
9934 : 0 : if (not_arg)
9935 : 0 : ok = ! ok;
9936 : :
9937 : 0 : if (*p == ' ')
9938 : 0 : ++p;
9939 : : }
9940 : :
9941 : 0 : if (ok)
9942 : : return;
9943 : :
9944 : 0 : ++p;
9945 : : }
9946 : :
9947 : 300112 : first = 1;
9948 : 300112 : p = multilib_select;
9949 : :
9950 : : /* Append multilib reuse rules if any. With those rules, we can reuse
9951 : : one multilib for certain different options sets. */
9952 : 300112 : if (strlen (multilib_reuse) > 0)
9953 : 0 : p = concat (p, multilib_reuse, NULL);
9954 : :
9955 : 608130 : while (*p != '\0')
9956 : : {
9957 : : /* Ignore newlines. */
9958 : 608130 : if (*p == '\n')
9959 : : {
9960 : 0 : ++p;
9961 : 0 : continue;
9962 : : }
9963 : :
9964 : : /* Get the initial path. */
9965 : : this_path = p;
9966 : 4280628 : while (*p != ' ')
9967 : : {
9968 : 3672498 : if (*p == '\0')
9969 : : {
9970 : 0 : invalid_select:
9971 : 0 : fatal_error (input_location, "multilib select %qs %qs is invalid",
9972 : : multilib_select, multilib_reuse);
9973 : : }
9974 : 3672498 : ++p;
9975 : : }
9976 : 608130 : this_path_len = p - this_path;
9977 : :
9978 : : /* Check the arguments. */
9979 : 608130 : ok = 1;
9980 : 608130 : ndfltok = 1;
9981 : 608130 : ++p;
9982 : 1824390 : while (*p != ';')
9983 : : {
9984 : 1216260 : if (*p == '\0')
9985 : 0 : goto invalid_select;
9986 : :
9987 : 1216260 : if (! ok)
9988 : : {
9989 : 0 : ++p;
9990 : 0 : continue;
9991 : : }
9992 : :
9993 : 5773282 : this_arg = p;
9994 : 5773282 : while (*p != ' ' && *p != ';')
9995 : : {
9996 : 4557022 : if (*p == '\0')
9997 : 0 : goto invalid_select;
9998 : 4557022 : ++p;
9999 : : }
10000 : :
10001 : 1216260 : if (*this_arg != '!')
10002 : : not_arg = 0;
10003 : : else
10004 : : {
10005 : 908242 : not_arg = 1;
10006 : 908242 : ++this_arg;
10007 : : }
10008 : :
10009 : : /* If this is a default argument, we can just ignore it.
10010 : : This is true even if this_arg begins with '!'. Beginning
10011 : : with '!' does not mean that this argument is necessarily
10012 : : inappropriate for this library: it merely means that
10013 : : there is a more specific library which uses this
10014 : : argument. If this argument is a default, we need not
10015 : : consider that more specific library. */
10016 : 1216260 : ok = used_arg (this_arg, p - this_arg);
10017 : 1216260 : if (not_arg)
10018 : 908242 : ok = ! ok;
10019 : :
10020 : 1216260 : if (! ok)
10021 : 315924 : ndfltok = 0;
10022 : :
10023 : 1216260 : if (default_arg (this_arg, p - this_arg))
10024 : 608130 : ok = 1;
10025 : :
10026 : 1216260 : if (*p == ' ')
10027 : 608130 : ++p;
10028 : : }
10029 : :
10030 : 608130 : if (ok && first)
10031 : : {
10032 : 300112 : if (this_path_len != 1
10033 : 292206 : || this_path[0] != '.')
10034 : : {
10035 : 7906 : char *new_multilib_dir = XNEWVEC (char, this_path_len + 1);
10036 : 7906 : char *q;
10037 : :
10038 : 7906 : strncpy (new_multilib_dir, this_path, this_path_len);
10039 : 7906 : new_multilib_dir[this_path_len] = '\0';
10040 : 7906 : q = strchr (new_multilib_dir, ':');
10041 : 7906 : if (q != NULL)
10042 : 7906 : *q = '\0';
10043 : 7906 : multilib_dir = new_multilib_dir;
10044 : : }
10045 : : first = 0;
10046 : : }
10047 : :
10048 : 608130 : if (ndfltok)
10049 : : {
10050 : 300112 : const char *q = this_path, *end = this_path + this_path_len;
10051 : :
10052 : 900336 : while (q < end && *q != ':')
10053 : 600224 : q++;
10054 : 300112 : if (q < end)
10055 : : {
10056 : 300112 : const char *q2 = q + 1, *ml_end = end;
10057 : 300112 : char *new_multilib_os_dir;
10058 : :
10059 : 2685196 : while (q2 < end && *q2 != ':')
10060 : 2385084 : q2++;
10061 : 300112 : if (*q2 == ':')
10062 : 0 : ml_end = q2;
10063 : 300112 : if (ml_end - q == 1)
10064 : 0 : multilib_os_dir = xstrdup (".");
10065 : : else
10066 : : {
10067 : 300112 : new_multilib_os_dir = XNEWVEC (char, ml_end - q);
10068 : 300112 : memcpy (new_multilib_os_dir, q + 1, ml_end - q - 1);
10069 : 300112 : new_multilib_os_dir[ml_end - q - 1] = '\0';
10070 : 300112 : multilib_os_dir = new_multilib_os_dir;
10071 : : }
10072 : :
10073 : 300112 : if (q2 < end && *q2 == ':')
10074 : : {
10075 : 0 : char *new_multiarch_dir = XNEWVEC (char, end - q2);
10076 : 0 : memcpy (new_multiarch_dir, q2 + 1, end - q2 - 1);
10077 : 0 : new_multiarch_dir[end - q2 - 1] = '\0';
10078 : 0 : multiarch_dir = new_multiarch_dir;
10079 : : }
10080 : : break;
10081 : : }
10082 : : }
10083 : :
10084 : 308018 : ++p;
10085 : : }
10086 : :
10087 : 600224 : multilib_dir =
10088 : 300112 : targetm_common.compute_multilib (
10089 : : switches,
10090 : : n_switches,
10091 : : multilib_dir,
10092 : : multilib_defaults,
10093 : : multilib_select,
10094 : : multilib_matches,
10095 : : multilib_exclusions,
10096 : : multilib_reuse);
10097 : :
10098 : 300112 : if (multilib_dir == NULL && multilib_os_dir != NULL
10099 : 292206 : && strcmp (multilib_os_dir, ".") == 0)
10100 : : {
10101 : 0 : free (CONST_CAST (char *, multilib_os_dir));
10102 : 0 : multilib_os_dir = NULL;
10103 : : }
10104 : 300112 : else if (multilib_dir != NULL && multilib_os_dir == NULL)
10105 : : {
10106 : : /* Give second chance to search matched multilib_os_dir again by matching
10107 : : the multilib_dir since some target may use TARGET_COMPUTE_MULTILIB
10108 : : hook rather than the builtin way. */
10109 : 0 : find_multilib_os_dir_by_multilib_dir (multilib_dir, &multilib_os_dir,
10110 : : &multiarch_dir);
10111 : :
10112 : 0 : if (multilib_os_dir == NULL)
10113 : 0 : multilib_os_dir = multilib_dir;
10114 : : }
10115 : : }
10116 : :
10117 : : /* Print out the multiple library subdirectory selection
10118 : : information. This prints out a series of lines. Each line looks
10119 : : like SUBDIRECTORY;@OPTION@OPTION, with as many options as is
10120 : : required. Only the desired options are printed out, the negative
10121 : : matches. The options are print without a leading dash. There are
10122 : : no spaces to make it easy to use the information in the shell.
10123 : : Each subdirectory is printed only once. This assumes the ordering
10124 : : generated by the genmultilib script. Also, we leave out ones that match
10125 : : the exclusions. */
10126 : :
10127 : : static void
10128 : 5019 : print_multilib_info (void)
10129 : : {
10130 : 5019 : const char *p = multilib_select;
10131 : 5019 : const char *last_path = 0, *this_path;
10132 : 5019 : int skip;
10133 : 5019 : int not_arg;
10134 : 5019 : unsigned int last_path_len = 0;
10135 : :
10136 : 20076 : while (*p != '\0')
10137 : : {
10138 : 15057 : skip = 0;
10139 : : /* Ignore newlines. */
10140 : 15057 : if (*p == '\n')
10141 : : {
10142 : 0 : ++p;
10143 : 0 : continue;
10144 : : }
10145 : :
10146 : : /* Get the initial path. */
10147 : : this_path = p;
10148 : 120456 : while (*p != ' ')
10149 : : {
10150 : 105399 : if (*p == '\0')
10151 : : {
10152 : 0 : invalid_select:
10153 : 0 : fatal_error (input_location,
10154 : : "multilib select %qs is invalid", multilib_select);
10155 : : }
10156 : :
10157 : 105399 : ++p;
10158 : : }
10159 : :
10160 : : /* When --disable-multilib was used but target defines
10161 : : MULTILIB_OSDIRNAMES, entries starting with .: (and not starting
10162 : : with .:: for multiarch configurations) are there just to find
10163 : : multilib_os_dir, so skip them from output. */
10164 : 15057 : if (this_path[0] == '.' && this_path[1] == ':' && this_path[2] != ':')
10165 : 15057 : skip = 1;
10166 : :
10167 : : /* Check for matches with the multilib_exclusions. We don't bother
10168 : : with the '!' in either list. If any of the exclusion rules match
10169 : : all of its options with the select rule, we skip it. */
10170 : 15057 : {
10171 : 15057 : const char *e = multilib_exclusions;
10172 : 15057 : const char *this_arg;
10173 : :
10174 : 15057 : while (*e != '\0')
10175 : : {
10176 : 0 : int m = 1;
10177 : : /* Ignore newlines. */
10178 : 0 : if (*e == '\n')
10179 : : {
10180 : 0 : ++e;
10181 : 0 : continue;
10182 : : }
10183 : :
10184 : : /* Check the arguments. */
10185 : 0 : while (*e != ';')
10186 : : {
10187 : 0 : const char *q;
10188 : 0 : int mp = 0;
10189 : :
10190 : 0 : if (*e == '\0')
10191 : : {
10192 : 0 : invalid_exclusion:
10193 : 0 : fatal_error (input_location,
10194 : : "multilib exclusion %qs is invalid",
10195 : : multilib_exclusions);
10196 : : }
10197 : :
10198 : 0 : if (! m)
10199 : : {
10200 : 0 : ++e;
10201 : 0 : continue;
10202 : : }
10203 : :
10204 : : this_arg = e;
10205 : :
10206 : 0 : while (*e != ' ' && *e != ';')
10207 : : {
10208 : 0 : if (*e == '\0')
10209 : 0 : goto invalid_exclusion;
10210 : 0 : ++e;
10211 : : }
10212 : :
10213 : 0 : q = p + 1;
10214 : 0 : while (*q != ';')
10215 : : {
10216 : 0 : const char *arg;
10217 : 0 : int len = e - this_arg;
10218 : :
10219 : 0 : if (*q == '\0')
10220 : 0 : goto invalid_select;
10221 : :
10222 : : arg = q;
10223 : :
10224 : 0 : while (*q != ' ' && *q != ';')
10225 : : {
10226 : 0 : if (*q == '\0')
10227 : 0 : goto invalid_select;
10228 : 0 : ++q;
10229 : : }
10230 : :
10231 : 0 : if (! strncmp (arg, this_arg,
10232 : 0 : (len < q - arg) ? q - arg : len)
10233 : 0 : || default_arg (this_arg, e - this_arg))
10234 : : {
10235 : : mp = 1;
10236 : : break;
10237 : : }
10238 : :
10239 : 0 : if (*q == ' ')
10240 : 0 : ++q;
10241 : : }
10242 : :
10243 : 0 : if (! mp)
10244 : 0 : m = 0;
10245 : :
10246 : 0 : if (*e == ' ')
10247 : 0 : ++e;
10248 : : }
10249 : :
10250 : 0 : if (m)
10251 : : {
10252 : : skip = 1;
10253 : : break;
10254 : : }
10255 : :
10256 : 0 : if (*e != '\0')
10257 : 0 : ++e;
10258 : : }
10259 : : }
10260 : :
10261 : 15057 : if (! skip)
10262 : : {
10263 : : /* If this is a duplicate, skip it. */
10264 : 30114 : skip = (last_path != 0
10265 : 10038 : && (unsigned int) (p - this_path) == last_path_len
10266 : 15057 : && ! filename_ncmp (last_path, this_path, last_path_len));
10267 : :
10268 : 15057 : last_path = this_path;
10269 : 15057 : last_path_len = p - this_path;
10270 : : }
10271 : :
10272 : : /* If all required arguments are default arguments, and no default
10273 : : arguments appear in the ! argument list, then we can skip it.
10274 : : We will already have printed a directory identical to this one
10275 : : which does not require that default argument. */
10276 : 15057 : if (! skip)
10277 : : {
10278 : 15057 : const char *q;
10279 : 15057 : bool default_arg_ok = false;
10280 : :
10281 : 15057 : q = p + 1;
10282 : 25095 : while (*q != ';')
10283 : : {
10284 : 20076 : const char *arg;
10285 : :
10286 : 20076 : if (*q == '\0')
10287 : 0 : goto invalid_select;
10288 : :
10289 : 20076 : if (*q == '!')
10290 : : {
10291 : 15057 : not_arg = 1;
10292 : 15057 : q++;
10293 : : }
10294 : : else
10295 : : not_arg = 0;
10296 : 20076 : arg = q;
10297 : :
10298 : 80304 : while (*q != ' ' && *q != ';')
10299 : : {
10300 : 60228 : if (*q == '\0')
10301 : 0 : goto invalid_select;
10302 : 60228 : ++q;
10303 : : }
10304 : :
10305 : 20076 : if (default_arg (arg, q - arg))
10306 : : {
10307 : : /* Stop checking if any default arguments appeared in not
10308 : : list. */
10309 : 15057 : if (not_arg)
10310 : : {
10311 : : default_arg_ok = false;
10312 : : break;
10313 : : }
10314 : :
10315 : : default_arg_ok = true;
10316 : : }
10317 : 5019 : else if (!not_arg)
10318 : : {
10319 : : /* Stop checking if any required argument is not provided by
10320 : : default arguments. */
10321 : : default_arg_ok = false;
10322 : : break;
10323 : : }
10324 : :
10325 : 10038 : if (*q == ' ')
10326 : 5019 : ++q;
10327 : : }
10328 : :
10329 : : /* Make sure all default argument is OK for this multi-lib set. */
10330 : 15057 : if (default_arg_ok)
10331 : : skip = 1;
10332 : : else
10333 : : skip = 0;
10334 : : }
10335 : :
10336 : : if (! skip)
10337 : : {
10338 : : const char *p1;
10339 : :
10340 : 25095 : for (p1 = last_path; p1 < p && *p1 != ':'; p1++)
10341 : 15057 : putchar (*p1);
10342 : 10038 : putchar (';');
10343 : : }
10344 : :
10345 : 15057 : ++p;
10346 : 75285 : while (*p != ';')
10347 : : {
10348 : 60228 : int use_arg;
10349 : :
10350 : 60228 : if (*p == '\0')
10351 : 0 : goto invalid_select;
10352 : :
10353 : 60228 : if (skip)
10354 : : {
10355 : 40152 : ++p;
10356 : 40152 : continue;
10357 : : }
10358 : :
10359 : 20076 : use_arg = *p != '!';
10360 : :
10361 : 20076 : if (use_arg)
10362 : 5019 : putchar ('@');
10363 : :
10364 : 95361 : while (*p != ' ' && *p != ';')
10365 : : {
10366 : 75285 : if (*p == '\0')
10367 : 0 : goto invalid_select;
10368 : 75285 : if (use_arg)
10369 : 15057 : putchar (*p);
10370 : 75285 : ++p;
10371 : : }
10372 : :
10373 : 20076 : if (*p == ' ')
10374 : 10038 : ++p;
10375 : : }
10376 : :
10377 : 15057 : if (! skip)
10378 : : {
10379 : : /* If there are extra options, print them now. */
10380 : 10038 : if (multilib_extra && *multilib_extra)
10381 : : {
10382 : : int print_at = true;
10383 : : const char *q;
10384 : :
10385 : 0 : for (q = multilib_extra; *q != '\0'; q++)
10386 : : {
10387 : 0 : if (*q == ' ')
10388 : : print_at = true;
10389 : : else
10390 : : {
10391 : 0 : if (print_at)
10392 : 0 : putchar ('@');
10393 : 0 : putchar (*q);
10394 : 0 : print_at = false;
10395 : : }
10396 : : }
10397 : : }
10398 : :
10399 : 10038 : putchar ('\n');
10400 : : }
10401 : :
10402 : 15057 : ++p;
10403 : : }
10404 : 5019 : }
10405 : :
10406 : : /* getenv built-in spec function.
10407 : :
10408 : : Returns the value of the environment variable given by its first argument,
10409 : : concatenated with the second argument. If the variable is not defined, a
10410 : : fatal error is issued unless such undefs are internally allowed, in which
10411 : : case the variable name prefixed by a '/' is used as the variable value.
10412 : :
10413 : : The leading '/' allows using the result at a spot where a full path would
10414 : : normally be expected and when the actual value doesn't really matter since
10415 : : undef vars are allowed. */
10416 : :
10417 : : static const char *
10418 : 0 : getenv_spec_function (int argc, const char **argv)
10419 : : {
10420 : 0 : const char *value;
10421 : 0 : const char *varname;
10422 : :
10423 : 0 : char *result;
10424 : 0 : char *ptr;
10425 : 0 : size_t len;
10426 : :
10427 : 0 : if (argc != 2)
10428 : : return NULL;
10429 : :
10430 : 0 : varname = argv[0];
10431 : 0 : value = env.get (varname);
10432 : :
10433 : : /* If the variable isn't defined and this is allowed, craft our expected
10434 : : return value. Assume variable names used in specs strings don't contain
10435 : : any active spec character so don't need escaping. */
10436 : 0 : if (!value && spec_undefvar_allowed)
10437 : : {
10438 : 0 : result = XNEWVAR (char, strlen(varname) + 2);
10439 : 0 : sprintf (result, "/%s", varname);
10440 : 0 : return result;
10441 : : }
10442 : :
10443 : 0 : if (!value)
10444 : 0 : fatal_error (input_location,
10445 : : "environment variable %qs not defined", varname);
10446 : :
10447 : : /* We have to escape every character of the environment variable so
10448 : : they are not interpreted as active spec characters. A
10449 : : particularly painful case is when we are reading a variable
10450 : : holding a windows path complete with \ separators. */
10451 : 0 : len = strlen (value) * 2 + strlen (argv[1]) + 1;
10452 : 0 : result = XNEWVAR (char, len);
10453 : 0 : for (ptr = result; *value; ptr += 2)
10454 : : {
10455 : 0 : ptr[0] = '\\';
10456 : 0 : ptr[1] = *value++;
10457 : : }
10458 : :
10459 : 0 : strcpy (ptr, argv[1]);
10460 : :
10461 : 0 : return result;
10462 : : }
10463 : :
10464 : : /* if-exists built-in spec function.
10465 : :
10466 : : Checks to see if the file specified by the absolute pathname in
10467 : : ARGS exists. Returns that pathname if found.
10468 : :
10469 : : The usual use for this function is to check for a library file
10470 : : (whose name has been expanded with %s). */
10471 : :
10472 : : static const char *
10473 : 0 : if_exists_spec_function (int argc, const char **argv)
10474 : : {
10475 : : /* Must have only one argument. */
10476 : 0 : if (argc == 1 && IS_ABSOLUTE_PATH (argv[0]) && ! access (argv[0], R_OK))
10477 : 0 : return argv[0];
10478 : :
10479 : : return NULL;
10480 : : }
10481 : :
10482 : : /* if-exists-else built-in spec function.
10483 : :
10484 : : This is like if-exists, but takes an additional argument which
10485 : : is returned if the first argument does not exist. */
10486 : :
10487 : : static const char *
10488 : 0 : if_exists_else_spec_function (int argc, const char **argv)
10489 : : {
10490 : : /* Must have exactly two arguments. */
10491 : 0 : if (argc != 2)
10492 : : return NULL;
10493 : :
10494 : 0 : if (IS_ABSOLUTE_PATH (argv[0]) && ! access (argv[0], R_OK))
10495 : 0 : return argv[0];
10496 : :
10497 : 0 : return argv[1];
10498 : : }
10499 : :
10500 : : /* if-exists-then-else built-in spec function.
10501 : :
10502 : : Checks to see if the file specified by the absolute pathname in
10503 : : the first arg exists. Returns the second arg if so, otherwise returns
10504 : : the third arg if it is present. */
10505 : :
10506 : : static const char *
10507 : 0 : if_exists_then_else_spec_function (int argc, const char **argv)
10508 : : {
10509 : :
10510 : : /* Must have two or three arguments. */
10511 : 0 : if (argc != 2 && argc != 3)
10512 : : return NULL;
10513 : :
10514 : 0 : if (IS_ABSOLUTE_PATH (argv[0]) && ! access (argv[0], R_OK))
10515 : 0 : return argv[1];
10516 : :
10517 : 0 : if (argc == 3)
10518 : 0 : return argv[2];
10519 : :
10520 : : return NULL;
10521 : : }
10522 : :
10523 : : /* sanitize built-in spec function.
10524 : :
10525 : : This returns non-NULL, if sanitizing address, thread or
10526 : : any of the undefined behavior sanitizers. */
10527 : :
10528 : : static const char *
10529 : 861258 : sanitize_spec_function (int argc, const char **argv)
10530 : : {
10531 : 861258 : if (argc != 1)
10532 : : return NULL;
10533 : :
10534 : 861258 : if (strcmp (argv[0], "address") == 0)
10535 : 380018 : return (flag_sanitize & SANITIZE_USER_ADDRESS) ? "" : NULL;
10536 : 669867 : if (strcmp (argv[0], "hwaddress") == 0)
10537 : 382422 : return (flag_sanitize & SANITIZE_USER_HWADDRESS) ? "" : NULL;
10538 : 478476 : if (strcmp (argv[0], "kernel-address") == 0)
10539 : 0 : return (flag_sanitize & SANITIZE_KERNEL_ADDRESS) ? "" : NULL;
10540 : 478476 : if (strcmp (argv[0], "kernel-hwaddress") == 0)
10541 : 0 : return (flag_sanitize & SANITIZE_KERNEL_HWADDRESS) ? "" : NULL;
10542 : 478476 : if (strcmp (argv[0], "thread") == 0)
10543 : 382214 : return (flag_sanitize & SANITIZE_THREAD) ? "" : NULL;
10544 : 287085 : if (strcmp (argv[0], "undefined") == 0)
10545 : 95694 : return ((flag_sanitize
10546 : 95694 : & ~flag_sanitize_trap
10547 : 95694 : & (SANITIZE_UNDEFINED | SANITIZE_UNDEFINED_NONDEFAULT)))
10548 : 189503 : ? "" : NULL;
10549 : 191391 : if (strcmp (argv[0], "leak") == 0)
10550 : 191391 : return ((flag_sanitize
10551 : 191391 : & (SANITIZE_ADDRESS | SANITIZE_LEAK | SANITIZE_THREAD))
10552 : 382782 : == SANITIZE_LEAK) ? "" : NULL;
10553 : : return NULL;
10554 : : }
10555 : :
10556 : : /* replace-outfile built-in spec function.
10557 : :
10558 : : This looks for the first argument in the outfiles array's name and
10559 : : replaces it with the second argument. */
10560 : :
10561 : : static const char *
10562 : 0 : replace_outfile_spec_function (int argc, const char **argv)
10563 : : {
10564 : 0 : int i;
10565 : : /* Must have exactly two arguments. */
10566 : 0 : if (argc != 2)
10567 : 0 : abort ();
10568 : :
10569 : 0 : for (i = 0; i < n_infiles; i++)
10570 : : {
10571 : 0 : if (outfiles[i] && !filename_cmp (outfiles[i], argv[0]))
10572 : 0 : outfiles[i] = xstrdup (argv[1]);
10573 : : }
10574 : 0 : return NULL;
10575 : : }
10576 : :
10577 : : /* remove-outfile built-in spec function.
10578 : : *
10579 : : * This looks for the first argument in the outfiles array's name and
10580 : : * removes it. */
10581 : :
10582 : : static const char *
10583 : 0 : remove_outfile_spec_function (int argc, const char **argv)
10584 : : {
10585 : 0 : int i;
10586 : : /* Must have exactly one argument. */
10587 : 0 : if (argc != 1)
10588 : 0 : abort ();
10589 : :
10590 : 0 : for (i = 0; i < n_infiles; i++)
10591 : : {
10592 : 0 : if (outfiles[i] && !filename_cmp (outfiles[i], argv[0]))
10593 : 0 : outfiles[i] = NULL;
10594 : : }
10595 : 0 : return NULL;
10596 : : }
10597 : :
10598 : : /* Given two version numbers, compares the two numbers.
10599 : : A version number must match the regular expression
10600 : : ([1-9][0-9]*|0)(\.([1-9][0-9]*|0))*
10601 : : */
10602 : : static int
10603 : 0 : compare_version_strings (const char *v1, const char *v2)
10604 : : {
10605 : 0 : int rresult;
10606 : 0 : regex_t r;
10607 : :
10608 : 0 : if (regcomp (&r, "^([1-9][0-9]*|0)(\\.([1-9][0-9]*|0))*$",
10609 : : REG_EXTENDED | REG_NOSUB) != 0)
10610 : 0 : abort ();
10611 : 0 : rresult = regexec (&r, v1, 0, NULL, 0);
10612 : 0 : if (rresult == REG_NOMATCH)
10613 : 0 : fatal_error (input_location, "invalid version number %qs", v1);
10614 : 0 : else if (rresult != 0)
10615 : 0 : abort ();
10616 : 0 : rresult = regexec (&r, v2, 0, NULL, 0);
10617 : 0 : if (rresult == REG_NOMATCH)
10618 : 0 : fatal_error (input_location, "invalid version number %qs", v2);
10619 : 0 : else if (rresult != 0)
10620 : 0 : abort ();
10621 : :
10622 : 0 : return strverscmp (v1, v2);
10623 : : }
10624 : :
10625 : :
10626 : : /* version_compare built-in spec function.
10627 : :
10628 : : This takes an argument of the following form:
10629 : :
10630 : : <comparison-op> <arg1> [<arg2>] <switch> <result>
10631 : :
10632 : : and produces "result" if the comparison evaluates to true,
10633 : : and nothing if it doesn't.
10634 : :
10635 : : The supported <comparison-op> values are:
10636 : :
10637 : : >= true if switch is a later (or same) version than arg1
10638 : : !> opposite of >=
10639 : : < true if switch is an earlier version than arg1
10640 : : !< opposite of <
10641 : : >< true if switch is arg1 or later, and earlier than arg2
10642 : : <> true if switch is earlier than arg1 or is arg2 or later
10643 : :
10644 : : If the switch is not present, the condition is false unless
10645 : : the first character of the <comparison-op> is '!'.
10646 : :
10647 : : For example,
10648 : : %:version-compare(>= 10.3 mmacosx-version-min= -lmx)
10649 : : adds -lmx if -mmacosx-version-min=10.3.9 was passed. */
10650 : :
10651 : : static const char *
10652 : 0 : version_compare_spec_function (int argc, const char **argv)
10653 : : {
10654 : 0 : int comp1, comp2;
10655 : 0 : size_t switch_len;
10656 : 0 : const char *switch_value = NULL;
10657 : 0 : int nargs = 1, i;
10658 : 0 : bool result;
10659 : :
10660 : 0 : if (argc < 3)
10661 : 0 : fatal_error (input_location, "too few arguments to %%:version-compare");
10662 : 0 : if (argv[0][0] == '\0')
10663 : 0 : abort ();
10664 : 0 : if ((argv[0][1] == '<' || argv[0][1] == '>') && argv[0][0] != '!')
10665 : 0 : nargs = 2;
10666 : 0 : if (argc != nargs + 3)
10667 : 0 : fatal_error (input_location, "too many arguments to %%:version-compare");
10668 : :
10669 : 0 : switch_len = strlen (argv[nargs + 1]);
10670 : 0 : for (i = 0; i < n_switches; i++)
10671 : 0 : if (!strncmp (switches[i].part1, argv[nargs + 1], switch_len)
10672 : 0 : && check_live_switch (i, switch_len))
10673 : 0 : switch_value = switches[i].part1 + switch_len;
10674 : :
10675 : 0 : if (switch_value == NULL)
10676 : : comp1 = comp2 = -1;
10677 : : else
10678 : : {
10679 : 0 : comp1 = compare_version_strings (switch_value, argv[1]);
10680 : 0 : if (nargs == 2)
10681 : 0 : comp2 = compare_version_strings (switch_value, argv[2]);
10682 : : else
10683 : : comp2 = -1; /* This value unused. */
10684 : : }
10685 : :
10686 : 0 : switch (argv[0][0] << 8 | argv[0][1])
10687 : : {
10688 : 0 : case '>' << 8 | '=':
10689 : 0 : result = comp1 >= 0;
10690 : 0 : break;
10691 : 0 : case '!' << 8 | '<':
10692 : 0 : result = comp1 >= 0 || switch_value == NULL;
10693 : 0 : break;
10694 : 0 : case '<' << 8:
10695 : 0 : result = comp1 < 0;
10696 : 0 : break;
10697 : 0 : case '!' << 8 | '>':
10698 : 0 : result = comp1 < 0 || switch_value == NULL;
10699 : 0 : break;
10700 : 0 : case '>' << 8 | '<':
10701 : 0 : result = comp1 >= 0 && comp2 < 0;
10702 : 0 : break;
10703 : 0 : case '<' << 8 | '>':
10704 : 0 : result = comp1 < 0 || comp2 >= 0;
10705 : 0 : break;
10706 : :
10707 : 0 : default:
10708 : 0 : fatal_error (input_location,
10709 : : "unknown operator %qs in %%:version-compare", argv[0]);
10710 : : }
10711 : 0 : if (! result)
10712 : : return NULL;
10713 : :
10714 : 0 : return argv[nargs + 2];
10715 : : }
10716 : :
10717 : : /* %:include builtin spec function. This differs from %include in that it
10718 : : can be nested inside a spec, and thus be conditionalized. It takes
10719 : : one argument, the filename, and looks for it in the startfile path.
10720 : : The result is always NULL, i.e. an empty expansion. */
10721 : :
10722 : : static const char *
10723 : 30610 : include_spec_function (int argc, const char **argv)
10724 : : {
10725 : 30610 : char *file;
10726 : :
10727 : 30610 : if (argc != 1)
10728 : 0 : abort ();
10729 : :
10730 : 30610 : file = find_a_file (&startfile_prefixes, argv[0], R_OK, true);
10731 : 30610 : read_specs (file ? file : argv[0], false, false);
10732 : :
10733 : 30607 : return NULL;
10734 : : }
10735 : :
10736 : : /* %:find-file spec function. This function replaces its argument by
10737 : : the file found through find_file, that is the -print-file-name gcc
10738 : : program option. */
10739 : : static const char *
10740 : 0 : find_file_spec_function (int argc, const char **argv)
10741 : : {
10742 : 0 : const char *file;
10743 : :
10744 : 0 : if (argc != 1)
10745 : 0 : abort ();
10746 : :
10747 : 0 : file = find_file (argv[0]);
10748 : 0 : return file;
10749 : : }
10750 : :
10751 : :
10752 : : /* %:find-plugindir spec function. This function replaces its argument
10753 : : by the -iplugindir=<dir> option. `dir' is found through find_file, that
10754 : : is the -print-file-name gcc program option. */
10755 : : static const char *
10756 : 398 : find_plugindir_spec_function (int argc, const char **argv ATTRIBUTE_UNUSED)
10757 : : {
10758 : 398 : const char *option;
10759 : :
10760 : 398 : if (argc != 0)
10761 : 0 : abort ();
10762 : :
10763 : 398 : option = concat ("-iplugindir=", find_file ("plugin"), NULL);
10764 : 398 : return option;
10765 : : }
10766 : :
10767 : :
10768 : : /* %:print-asm-header spec function. Print a banner to say that the
10769 : : following output is from the assembler. */
10770 : :
10771 : : static const char *
10772 : 0 : print_asm_header_spec_function (int arg ATTRIBUTE_UNUSED,
10773 : : const char **argv ATTRIBUTE_UNUSED)
10774 : : {
10775 : 0 : printf (_("Assembler options\n=================\n\n"));
10776 : 0 : printf (_("Use \"-Wa,OPTION\" to pass \"OPTION\" to the assembler.\n\n"));
10777 : 0 : fflush (stdout);
10778 : 0 : return NULL;
10779 : : }
10780 : :
10781 : : /* Get a random number for -frandom-seed */
10782 : :
10783 : : static unsigned HOST_WIDE_INT
10784 : 620 : get_random_number (void)
10785 : : {
10786 : 620 : unsigned HOST_WIDE_INT ret = 0;
10787 : 620 : int fd;
10788 : :
10789 : 620 : fd = open ("/dev/urandom", O_RDONLY);
10790 : 620 : if (fd >= 0)
10791 : : {
10792 : 620 : read (fd, &ret, sizeof (HOST_WIDE_INT));
10793 : 620 : close (fd);
10794 : 620 : if (ret)
10795 : : return ret;
10796 : : }
10797 : :
10798 : : /* Get some more or less random data. */
10799 : : #ifdef HAVE_GETTIMEOFDAY
10800 : 0 : {
10801 : 0 : struct timeval tv;
10802 : :
10803 : 0 : gettimeofday (&tv, NULL);
10804 : 0 : ret = tv.tv_sec * 1000 + tv.tv_usec / 1000;
10805 : : }
10806 : : #else
10807 : : {
10808 : : time_t now = time (NULL);
10809 : :
10810 : : if (now != (time_t)-1)
10811 : : ret = (unsigned) now;
10812 : : }
10813 : : #endif
10814 : :
10815 : 0 : return ret ^ getpid ();
10816 : : }
10817 : :
10818 : : /* %:compare-debug-dump-opt spec function. Save the last argument,
10819 : : expected to be the last -fdump-final-insns option, or generate a
10820 : : temporary. */
10821 : :
10822 : : static const char *
10823 : 1233 : compare_debug_dump_opt_spec_function (int arg,
10824 : : const char **argv ATTRIBUTE_UNUSED)
10825 : : {
10826 : 1233 : char *ret;
10827 : 1233 : char *name;
10828 : 1233 : int which;
10829 : 1233 : static char random_seed[HOST_BITS_PER_WIDE_INT / 4 + 3];
10830 : :
10831 : 1233 : if (arg != 0)
10832 : 0 : fatal_error (input_location,
10833 : : "too many arguments to %%:compare-debug-dump-opt");
10834 : :
10835 : 1233 : do_spec_2 ("%{fdump-final-insns=*:%*}", NULL);
10836 : 1233 : do_spec_1 (" ", 0, NULL);
10837 : :
10838 : 1233 : if (argbuf.length () > 0
10839 : 1233 : && strcmp (argv[argbuf.length () - 1], ".") != 0)
10840 : : {
10841 : 0 : if (!compare_debug)
10842 : : return NULL;
10843 : :
10844 : 0 : name = xstrdup (argv[argbuf.length () - 1]);
10845 : 0 : ret = NULL;
10846 : : }
10847 : : else
10848 : : {
10849 : 1233 : if (argbuf.length () > 0)
10850 : 6 : do_spec_2 ("%B.gkd", NULL);
10851 : 1227 : else if (!compare_debug)
10852 : : return NULL;
10853 : : else
10854 : 1227 : do_spec_2 ("%{!save-temps*:%g.gkd}%{save-temps*:%B.gkd}", NULL);
10855 : :
10856 : 1233 : do_spec_1 (" ", 0, NULL);
10857 : :
10858 : 1233 : gcc_assert (argbuf.length () > 0);
10859 : :
10860 : 1233 : name = xstrdup (argbuf.last ());
10861 : :
10862 : 1233 : char *arg = quote_spec (xstrdup (name));
10863 : 1233 : ret = concat ("-fdump-final-insns=", arg, NULL);
10864 : 1233 : free (arg);
10865 : : }
10866 : :
10867 : 1233 : which = compare_debug < 0;
10868 : 1233 : debug_check_temp_file[which] = name;
10869 : :
10870 : 1233 : if (!which)
10871 : : {
10872 : 620 : unsigned HOST_WIDE_INT value = get_random_number ();
10873 : :
10874 : 620 : sprintf (random_seed, HOST_WIDE_INT_PRINT_HEX, value);
10875 : : }
10876 : :
10877 : 1233 : if (*random_seed)
10878 : : {
10879 : 1233 : char *tmp = ret;
10880 : 1233 : ret = concat ("%{!frandom-seed=*:-frandom-seed=", random_seed, "} ",
10881 : : ret, NULL);
10882 : 1233 : free (tmp);
10883 : : }
10884 : :
10885 : 1233 : if (which)
10886 : 613 : *random_seed = 0;
10887 : :
10888 : : return ret;
10889 : : }
10890 : :
10891 : : /* %:compare-debug-self-opt spec function. Expands to the options
10892 : : that are to be passed in the second compilation of
10893 : : compare-debug. */
10894 : :
10895 : : static const char *
10896 : 1238 : compare_debug_self_opt_spec_function (int arg,
10897 : : const char **argv ATTRIBUTE_UNUSED)
10898 : : {
10899 : 1238 : if (arg != 0)
10900 : 0 : fatal_error (input_location,
10901 : : "too many arguments to %%:compare-debug-self-opt");
10902 : :
10903 : 1238 : if (compare_debug >= 0)
10904 : : return NULL;
10905 : :
10906 : 619 : return concat ("\
10907 : : %<o %<MD %<MMD %<MF* %<MG %<MP %<MQ* %<MT* \
10908 : : %<fdump-final-insns=* -w -S -o %j \
10909 : : %{!fcompare-debug-second:-fcompare-debug-second} \
10910 : 619 : ", compare_debug_opt, NULL);
10911 : : }
10912 : :
10913 : : /* %:pass-through-libs spec function. Finds all -l options and input
10914 : : file names in the lib spec passed to it, and makes a list of them
10915 : : prepended with the plugin option to cause them to be passed through
10916 : : to the final link after all the new object files have been added. */
10917 : :
10918 : : const char *
10919 : 90292 : pass_through_libs_spec_func (int argc, const char **argv)
10920 : : {
10921 : 90292 : char *prepended = xstrdup (" ");
10922 : 90292 : int n;
10923 : : /* Shlemiel the painter's algorithm. Innately horrible, but at least
10924 : : we know that there will never be more than a handful of strings to
10925 : : concat, and it's only once per run, so it's not worth optimising. */
10926 : 1226396 : for (n = 0; n < argc; n++)
10927 : : {
10928 : 1136104 : char *old = prepended;
10929 : : /* Anything that isn't an option is a full path to an output
10930 : : file; pass it through if it ends in '.a'. Among options,
10931 : : pass only -l. */
10932 : 1136104 : if (argv[n][0] == '-' && argv[n][1] == 'l')
10933 : : {
10934 : 589927 : const char *lopt = argv[n] + 2;
10935 : : /* Handle both joined and non-joined -l options. If for any
10936 : : reason there's a trailing -l with no joined or following
10937 : : arg just discard it. */
10938 : 589927 : if (!*lopt && ++n >= argc)
10939 : : break;
10940 : 589927 : else if (!*lopt)
10941 : 0 : lopt = argv[n];
10942 : 589927 : prepended = concat (prepended, "-plugin-opt=-pass-through=-l",
10943 : : lopt, " ", NULL);
10944 : 589927 : }
10945 : 546177 : else if (!strcmp (".a", argv[n] + strlen (argv[n]) - 2))
10946 : : {
10947 : 0 : prepended = concat (prepended, "-plugin-opt=-pass-through=",
10948 : : argv[n], " ", NULL);
10949 : : }
10950 : 1136104 : if (prepended != old)
10951 : 589927 : free (old);
10952 : : }
10953 : 90292 : return prepended;
10954 : : }
10955 : :
10956 : : static bool
10957 : 516885 : not_actual_file_p (const char *name)
10958 : : {
10959 : 516885 : return (strcmp (name, "-") == 0
10960 : 516885 : || strcmp (name, HOST_BIT_BUCKET) == 0);
10961 : : }
10962 : :
10963 : : /* %:dumps spec function. Take an optional argument that overrides
10964 : : the default extension for -dumpbase and -dumpbase-ext.
10965 : : Return -dumpdir, -dumpbase and -dumpbase-ext, if needed. */
10966 : : const char *
10967 : 284121 : dumps_spec_func (int argc, const char **argv ATTRIBUTE_UNUSED)
10968 : : {
10969 : 284121 : const char *ext = dumpbase_ext;
10970 : 284121 : char *p;
10971 : :
10972 : 284121 : char *args[3] = { NULL, NULL, NULL };
10973 : 284121 : int nargs = 0;
10974 : :
10975 : : /* Do not compute a default for -dumpbase-ext when -dumpbase was
10976 : : given explicitly. */
10977 : 284121 : if (dumpbase && *dumpbase && !ext)
10978 : 284121 : ext = "";
10979 : :
10980 : 284121 : if (argc == 1)
10981 : : {
10982 : : /* Do not override the explicitly-specified -dumpbase-ext with
10983 : : the specs-provided overrider. */
10984 : 0 : if (!ext)
10985 : 0 : ext = argv[0];
10986 : : }
10987 : 284121 : else if (argc != 0)
10988 : 0 : fatal_error (input_location, "too many arguments for %%:dumps");
10989 : :
10990 : 284121 : if (dumpdir)
10991 : : {
10992 : 105152 : p = quote_spec_arg (xstrdup (dumpdir));
10993 : 105152 : args[nargs++] = concat (" -dumpdir ", p, NULL);
10994 : 105152 : free (p);
10995 : : }
10996 : :
10997 : 284121 : if (!ext)
10998 : 261723 : ext = input_basename + basename_length;
10999 : :
11000 : : /* Use the precomputed outbase, or compute dumpbase from
11001 : : input_basename, just like %b would. */
11002 : 284121 : char *base;
11003 : :
11004 : 284121 : if (dumpbase && *dumpbase)
11005 : : {
11006 : 22398 : base = xstrdup (dumpbase);
11007 : 22398 : p = base + outbase_length;
11008 : 22398 : gcc_checking_assert (strncmp (base, outbase, outbase_length) == 0);
11009 : 22398 : gcc_checking_assert (strcmp (p, ext) == 0);
11010 : : }
11011 : 261723 : else if (outbase_length)
11012 : : {
11013 : 161208 : base = xstrndup (outbase, outbase_length);
11014 : 161208 : p = NULL;
11015 : : }
11016 : : else
11017 : : {
11018 : 100515 : base = xstrndup (input_basename, suffixed_basename_length);
11019 : 100515 : p = base + basename_length;
11020 : : }
11021 : :
11022 : 284121 : if (compare_debug < 0 || !p || strcmp (p, ext) != 0)
11023 : : {
11024 : 613 : if (p)
11025 : 9 : *p = '\0';
11026 : :
11027 : 161217 : const char *gk;
11028 : 161217 : if (compare_debug < 0)
11029 : : gk = ".gk";
11030 : : else
11031 : 160604 : gk = "";
11032 : :
11033 : 161217 : p = concat (base, gk, ext, NULL);
11034 : :
11035 : 161217 : free (base);
11036 : 161217 : base = p;
11037 : : }
11038 : :
11039 : 284121 : base = quote_spec_arg (base);
11040 : 284121 : args[nargs++] = concat (" -dumpbase ", base, NULL);
11041 : 284121 : free (base);
11042 : :
11043 : 284121 : if (*ext)
11044 : : {
11045 : 260687 : p = quote_spec_arg (xstrdup (ext));
11046 : 260687 : args[nargs++] = concat (" -dumpbase-ext ", p, NULL);
11047 : 260687 : free (p);
11048 : : }
11049 : :
11050 : 284121 : const char *ret = concat (args[0], args[1], args[2], NULL);
11051 : 1218202 : while (nargs > 0)
11052 : 649960 : free (args[--nargs]);
11053 : :
11054 : 284121 : return ret;
11055 : : }
11056 : :
11057 : : /* Returns "" if ARGV[ARGC - 2] is greater than ARGV[ARGC-1].
11058 : : Otherwise, return NULL. */
11059 : :
11060 : : static const char *
11061 : 396016 : greater_than_spec_func (int argc, const char **argv)
11062 : : {
11063 : 396016 : char *converted;
11064 : :
11065 : 396016 : if (argc == 1)
11066 : : return NULL;
11067 : :
11068 : 252 : gcc_assert (argc >= 2);
11069 : :
11070 : 252 : long arg = strtol (argv[argc - 2], &converted, 10);
11071 : 252 : gcc_assert (converted != argv[argc - 2]);
11072 : :
11073 : 252 : long lim = strtol (argv[argc - 1], &converted, 10);
11074 : 252 : gcc_assert (converted != argv[argc - 1]);
11075 : :
11076 : 252 : if (arg > lim)
11077 : : return "";
11078 : :
11079 : : return NULL;
11080 : : }
11081 : :
11082 : : /* Returns "" if debug_info_level is greater than ARGV[ARGC-1].
11083 : : Otherwise, return NULL. */
11084 : :
11085 : : static const char *
11086 : 253066 : debug_level_greater_than_spec_func (int argc, const char **argv)
11087 : : {
11088 : 253066 : char *converted;
11089 : :
11090 : 253066 : if (argc != 1)
11091 : 0 : fatal_error (input_location,
11092 : : "wrong number of arguments to %%:debug-level-gt");
11093 : :
11094 : 253066 : long arg = strtol (argv[0], &converted, 10);
11095 : 253066 : gcc_assert (converted != argv[0]);
11096 : :
11097 : 253066 : if (debug_info_level > arg)
11098 : 45297 : return "";
11099 : :
11100 : : return NULL;
11101 : : }
11102 : :
11103 : : /* Returns "" if dwarf_version is greater than ARGV[ARGC-1].
11104 : : Otherwise, return NULL. */
11105 : :
11106 : : static const char *
11107 : 128259 : dwarf_version_greater_than_spec_func (int argc, const char **argv)
11108 : : {
11109 : 128259 : char *converted;
11110 : :
11111 : 128259 : if (argc != 1)
11112 : 0 : fatal_error (input_location,
11113 : : "wrong number of arguments to %%:dwarf-version-gt");
11114 : :
11115 : 128259 : long arg = strtol (argv[0], &converted, 10);
11116 : 128259 : gcc_assert (converted != argv[0]);
11117 : :
11118 : 128259 : if (dwarf_version > arg)
11119 : 127388 : return "";
11120 : :
11121 : : return NULL;
11122 : : }
11123 : :
11124 : : static void
11125 : 34160 : path_prefix_reset (path_prefix *prefix)
11126 : : {
11127 : 34160 : struct prefix_list *iter, *next;
11128 : 34160 : iter = prefix->plist;
11129 : 135531 : while (iter)
11130 : : {
11131 : 101371 : next = iter->next;
11132 : 101371 : free (const_cast <char *> (iter->prefix));
11133 : 101371 : XDELETE (iter);
11134 : 101371 : iter = next;
11135 : : }
11136 : 34160 : prefix->plist = 0;
11137 : 34160 : prefix->max_len = 0;
11138 : 34160 : }
11139 : :
11140 : : /* The function takes 3 arguments: OPTION name, file name and location
11141 : : where we search for Fortran modules.
11142 : : When the FILE is found by find_file, return OPTION=path_to_file. */
11143 : :
11144 : : static const char *
11145 : 30833 : find_fortran_preinclude_file (int argc, const char **argv)
11146 : : {
11147 : 30833 : char *result = NULL;
11148 : 30833 : if (argc != 3)
11149 : : return NULL;
11150 : :
11151 : 30833 : struct path_prefix prefixes = { 0, 0, "preinclude" };
11152 : :
11153 : : /* Search first for 'finclude' folder location for a header file
11154 : : installed by the compiler (similar to omp_lib.h). */
11155 : 30833 : add_prefix (&prefixes, argv[2], NULL, 0, 0, 0);
11156 : : #ifdef TOOL_INCLUDE_DIR
11157 : : /* Then search: <prefix>/<target>/<include>/finclude */
11158 : 30833 : add_prefix (&prefixes, TOOL_INCLUDE_DIR "/finclude/",
11159 : : NULL, 0, 0, 0);
11160 : : #endif
11161 : : #ifdef NATIVE_SYSTEM_HEADER_DIR
11162 : : /* Then search: <sysroot>/usr/include/finclude/<multilib> */
11163 : 30833 : add_sysrooted_hdrs_prefix (&prefixes, NATIVE_SYSTEM_HEADER_DIR "/finclude/",
11164 : : NULL, 0, 0, 0);
11165 : : #endif
11166 : :
11167 : 30833 : const char *path = find_a_file (&include_prefixes, argv[1], R_OK, false);
11168 : 30833 : if (path != NULL)
11169 : 0 : result = concat (argv[0], path, NULL);
11170 : : else
11171 : : {
11172 : 30833 : path = find_a_file (&prefixes, argv[1], R_OK, false);
11173 : 30833 : if (path != NULL)
11174 : 30833 : result = concat (argv[0], path, NULL);
11175 : : }
11176 : :
11177 : 30833 : path_prefix_reset (&prefixes);
11178 : 30833 : return result;
11179 : : }
11180 : :
11181 : : /* The function takes any number of arguments and joins them together.
11182 : :
11183 : : This seems to be necessary to build "-fjoined=foo.b" from "-fseparate foo.a"
11184 : : with a %{fseparate*:-fjoined=%.b$*} rule without adding undesired spaces:
11185 : : when doing $* replacement we first replace $* with the rest of the switch
11186 : : (in this case ""), and then add any arguments as arguments after the result,
11187 : : resulting in "-fjoined= foo.b". Using this function with e.g.
11188 : : %{fseparate*:-fjoined=%:join(%.b$*)} gets multiple words as separate argv
11189 : : elements instead of separated by spaces, and we paste them together. */
11190 : :
11191 : : static const char *
11192 : 39 : join_spec_func (int argc, const char **argv)
11193 : : {
11194 : 39 : if (argc == 1)
11195 : 0 : return argv[0];
11196 : 117 : for (int i = 0; i < argc; ++i)
11197 : 78 : obstack_grow (&obstack, argv[i], strlen (argv[i]));
11198 : 39 : obstack_1grow (&obstack, '\0');
11199 : 39 : return XOBFINISH (&obstack, const char *);
11200 : : }
11201 : :
11202 : : /* If any character in ORIG fits QUOTE_P (_, P), reallocate the string
11203 : : so as to precede every one of them with a backslash. Return the
11204 : : original string or the reallocated one. */
11205 : :
11206 : : static inline char *
11207 : 850719 : quote_string (char *orig, bool (*quote_p)(char, void *), void *p)
11208 : : {
11209 : 850719 : int len, number_of_space = 0;
11210 : :
11211 : 19474158 : for (len = 0; orig[len]; len++)
11212 : 18623439 : if (quote_p (orig[len], p))
11213 : 0 : number_of_space++;
11214 : :
11215 : 850719 : if (number_of_space)
11216 : : {
11217 : 0 : char *new_spec = (char *) xmalloc (len + number_of_space + 1);
11218 : 0 : int j, k;
11219 : 0 : for (j = 0, k = 0; j <= len; j++, k++)
11220 : : {
11221 : 0 : if (quote_p (orig[j], p))
11222 : 0 : new_spec[k++] = '\\';
11223 : 0 : new_spec[k] = orig[j];
11224 : : }
11225 : 0 : free (orig);
11226 : 0 : return new_spec;
11227 : : }
11228 : : else
11229 : : return orig;
11230 : : }
11231 : :
11232 : : /* Return true iff C is any of the characters convert_white_space
11233 : : should quote. */
11234 : :
11235 : : static inline bool
11236 : 12388393 : whitespace_to_convert_p (char c, void *)
11237 : : {
11238 : 12388393 : return (c == ' ' || c == '\t');
11239 : : }
11240 : :
11241 : : /* Insert backslash before spaces in ORIG (usually a file path), to
11242 : : avoid being broken by spec parser.
11243 : :
11244 : : This function is needed as do_spec_1 treats white space (' ' and '\t')
11245 : : as the end of an argument. But in case of -plugin /usr/gcc install/xxx.so,
11246 : : the file name should be treated as a single argument rather than being
11247 : : broken into multiple. Solution is to insert '\\' before the space in a
11248 : : file name.
11249 : :
11250 : : This function converts and only converts all occurrence of ' '
11251 : : to '\\' + ' ' and '\t' to '\\' + '\t'. For example:
11252 : : "a b" -> "a\\ b"
11253 : : "a b" -> "a\\ \\ b"
11254 : : "a\tb" -> "a\\\tb"
11255 : : "a\\ b" -> "a\\\\ b"
11256 : :
11257 : : orig: input null-terminating string that was allocated by xalloc. The
11258 : : memory it points to might be freed in this function. Behavior undefined
11259 : : if ORIG wasn't xalloced or was freed already at entry.
11260 : :
11261 : : Return: ORIG if no conversion needed. Otherwise a newly allocated string
11262 : : that was converted from ORIG. */
11263 : :
11264 : : static char *
11265 : 199535 : convert_white_space (char *orig)
11266 : : {
11267 : 199535 : return quote_string (orig, whitespace_to_convert_p, NULL);
11268 : : }
11269 : :
11270 : : /* Return true iff C matches any of the spec active characters. */
11271 : : static inline bool
11272 : 6235046 : quote_spec_char_p (char c, void *)
11273 : : {
11274 : 6235046 : switch (c)
11275 : : {
11276 : : case ' ':
11277 : : case '\t':
11278 : : case '\n':
11279 : : case '|':
11280 : : case '%':
11281 : : case '\\':
11282 : : return true;
11283 : :
11284 : 6235046 : default:
11285 : 6235046 : return false;
11286 : : }
11287 : : }
11288 : :
11289 : : /* Like convert_white_space, but deactivate all active spec chars by
11290 : : quoting them. */
11291 : :
11292 : : static inline char *
11293 : 651184 : quote_spec (char *orig)
11294 : : {
11295 : 1233 : return quote_string (orig, quote_spec_char_p, NULL);
11296 : : }
11297 : :
11298 : : /* Like quote_spec, but also turn an empty string into the spec for an
11299 : : empty argument. */
11300 : :
11301 : : static inline char *
11302 : 649960 : quote_spec_arg (char *orig)
11303 : : {
11304 : 649960 : if (!*orig)
11305 : : {
11306 : 9 : free (orig);
11307 : 9 : return xstrdup ("%\"");
11308 : : }
11309 : :
11310 : 649951 : return quote_spec (orig);
11311 : : }
11312 : :
11313 : : /* Restore all state within gcc.cc to the initial state, so that the driver
11314 : : code can be safely re-run in-process.
11315 : :
11316 : : Many const char * variables are referenced by static specs (see
11317 : : INIT_STATIC_SPEC above). These variables are restored to their default
11318 : : values by a simple loop over the static specs.
11319 : :
11320 : : For other variables, we directly restore them all to their initial
11321 : : values (often implicitly 0).
11322 : :
11323 : : Free the various obstacks in this file, along with "opts_obstack"
11324 : : from opts.cc.
11325 : :
11326 : : This function also restores any environment variables that were changed. */
11327 : :
11328 : : void
11329 : 1109 : driver::finalize ()
11330 : : {
11331 : 1109 : env.restore ();
11332 : 1109 : diagnostic_finish (global_dc);
11333 : :
11334 : 1109 : is_cpp_driver = 0;
11335 : 1109 : at_file_supplied = 0;
11336 : 1109 : print_help_list = 0;
11337 : 1109 : print_version = 0;
11338 : 1109 : verbose_only_flag = 0;
11339 : 1109 : print_subprocess_help = 0;
11340 : 1109 : use_ld = NULL;
11341 : 1109 : report_times_to_file = NULL;
11342 : 1109 : target_system_root = DEFAULT_TARGET_SYSTEM_ROOT;
11343 : 1109 : target_system_root_changed = 0;
11344 : 1109 : target_sysroot_suffix = 0;
11345 : 1109 : target_sysroot_hdrs_suffix = 0;
11346 : 1109 : save_temps_flag = SAVE_TEMPS_NONE;
11347 : 1109 : save_temps_overrides_dumpdir = false;
11348 : 1109 : dumpdir_trailing_dash_added = false;
11349 : 1109 : free (dumpdir);
11350 : 1109 : free (dumpbase);
11351 : 1109 : free (dumpbase_ext);
11352 : 1109 : free (outbase);
11353 : 1109 : dumpdir = dumpbase = dumpbase_ext = outbase = NULL;
11354 : 1109 : dumpdir_length = outbase_length = 0;
11355 : 1109 : spec_machine = DEFAULT_TARGET_MACHINE;
11356 : 1109 : greatest_status = 1;
11357 : :
11358 : 1109 : obstack_free (&obstack, NULL);
11359 : 1109 : obstack_free (&opts_obstack, NULL); /* in opts.cc */
11360 : 1109 : obstack_free (&collect_obstack, NULL);
11361 : :
11362 : 1109 : link_command_spec = LINK_COMMAND_SPEC;
11363 : :
11364 : 1109 : obstack_free (&multilib_obstack, NULL);
11365 : :
11366 : 1109 : user_specs_head = NULL;
11367 : 1109 : user_specs_tail = NULL;
11368 : :
11369 : : /* Within the "compilers" vec, the fields "suffix" and "spec" were
11370 : : statically allocated for the default compilers, but dynamically
11371 : : allocated for additional compilers. Delete them for the latter. */
11372 : 1109 : for (int i = n_default_compilers; i < n_compilers; i++)
11373 : : {
11374 : 0 : free (const_cast <char *> (compilers[i].suffix));
11375 : 0 : free (const_cast <char *> (compilers[i].spec));
11376 : : }
11377 : 1109 : XDELETEVEC (compilers);
11378 : 1109 : compilers = NULL;
11379 : 1109 : n_compilers = 0;
11380 : :
11381 : 1109 : linker_options.truncate (0);
11382 : 1109 : assembler_options.truncate (0);
11383 : 1109 : preprocessor_options.truncate (0);
11384 : :
11385 : 1109 : path_prefix_reset (&exec_prefixes);
11386 : 1109 : path_prefix_reset (&startfile_prefixes);
11387 : 1109 : path_prefix_reset (&include_prefixes);
11388 : :
11389 : 1109 : machine_suffix = 0;
11390 : 1109 : just_machine_suffix = 0;
11391 : 1109 : gcc_exec_prefix = 0;
11392 : 1109 : gcc_libexec_prefix = 0;
11393 : 1109 : set_static_spec_shared (&md_exec_prefix, MD_EXEC_PREFIX);
11394 : 1109 : set_static_spec_shared (&md_startfile_prefix, MD_STARTFILE_PREFIX);
11395 : 1109 : set_static_spec_shared (&md_startfile_prefix_1, MD_STARTFILE_PREFIX_1);
11396 : 1109 : multilib_dir = 0;
11397 : 1109 : multilib_os_dir = 0;
11398 : 1109 : multiarch_dir = 0;
11399 : :
11400 : : /* Free any specs dynamically-allocated by set_spec.
11401 : : These will be at the head of the list, before the
11402 : : statically-allocated ones. */
11403 : 1109 : if (specs)
11404 : : {
11405 : 2218 : while (specs != static_specs)
11406 : : {
11407 : 1109 : spec_list *next = specs->next;
11408 : 1109 : free (const_cast <char *> (specs->name));
11409 : 1109 : XDELETE (specs);
11410 : 1109 : specs = next;
11411 : : }
11412 : 1109 : specs = 0;
11413 : : }
11414 : 51014 : for (unsigned i = 0; i < ARRAY_SIZE (static_specs); i++)
11415 : : {
11416 : 49905 : spec_list *sl = &static_specs[i];
11417 : 49905 : if (sl->alloc_p)
11418 : : {
11419 : 44370 : free (const_cast <char *> (*(sl->ptr_spec)));
11420 : 44370 : sl->alloc_p = false;
11421 : : }
11422 : 49905 : *(sl->ptr_spec) = sl->default_ptr;
11423 : : }
11424 : : #ifdef EXTRA_SPECS
11425 : 1109 : extra_specs = NULL;
11426 : : #endif
11427 : :
11428 : 1109 : processing_spec_function = 0;
11429 : :
11430 : 1109 : clear_args ();
11431 : :
11432 : 1109 : have_c = 0;
11433 : 1109 : have_o = 0;
11434 : :
11435 : 1109 : temp_names = NULL;
11436 : 1109 : execution_count = 0;
11437 : 1109 : signal_count = 0;
11438 : :
11439 : 1109 : temp_filename = NULL;
11440 : 1109 : temp_filename_length = 0;
11441 : 1109 : always_delete_queue = NULL;
11442 : 1109 : failure_delete_queue = NULL;
11443 : :
11444 : 1109 : XDELETEVEC (switches);
11445 : 1109 : switches = NULL;
11446 : 1109 : n_switches = 0;
11447 : 1109 : n_switches_alloc = 0;
11448 : :
11449 : 1109 : compare_debug = 0;
11450 : 1109 : compare_debug_second = 0;
11451 : 1109 : compare_debug_opt = NULL;
11452 : 3327 : for (int i = 0; i < 2; i++)
11453 : : {
11454 : 2218 : switches_debug_check[i] = NULL;
11455 : 2218 : n_switches_debug_check[i] = 0;
11456 : 2218 : n_switches_alloc_debug_check[i] = 0;
11457 : 2218 : debug_check_temp_file[i] = NULL;
11458 : : }
11459 : :
11460 : 1109 : XDELETEVEC (infiles);
11461 : 1109 : infiles = NULL;
11462 : 1109 : n_infiles = 0;
11463 : 1109 : n_infiles_alloc = 0;
11464 : :
11465 : 1109 : combine_inputs = false;
11466 : 1109 : added_libraries = 0;
11467 : 1109 : XDELETEVEC (outfiles);
11468 : 1109 : outfiles = NULL;
11469 : 1109 : spec_lang = 0;
11470 : 1109 : last_language_n_infiles = 0;
11471 : 1109 : gcc_input_filename = NULL;
11472 : 1109 : input_file_number = 0;
11473 : 1109 : input_filename_length = 0;
11474 : 1109 : basename_length = 0;
11475 : 1109 : suffixed_basename_length = 0;
11476 : 1109 : input_basename = NULL;
11477 : 1109 : input_suffix = NULL;
11478 : : /* We don't need to purge "input_stat", just to unset "input_stat_set". */
11479 : 1109 : input_stat_set = 0;
11480 : 1109 : input_file_compiler = NULL;
11481 : 1109 : arg_going = 0;
11482 : 1109 : delete_this_arg = 0;
11483 : 1109 : this_is_output_file = 0;
11484 : 1109 : this_is_library_file = 0;
11485 : 1109 : this_is_linker_script = 0;
11486 : 1109 : input_from_pipe = 0;
11487 : 1109 : suffix_subst = NULL;
11488 : :
11489 : 1109 : XDELETEVEC (mdswitches);
11490 : 1109 : mdswitches = NULL;
11491 : 1109 : n_mdswitches = 0;
11492 : :
11493 : 1109 : used_arg.finalize ();
11494 : 1109 : }
11495 : :
11496 : : /* PR jit/64810.
11497 : : Targets can provide configure-time default options in
11498 : : OPTION_DEFAULT_SPECS. The jit needs to access these, but
11499 : : they are expressed in the spec language.
11500 : :
11501 : : Run just enough of the driver to be able to expand these
11502 : : specs, and then call the callback CB on each
11503 : : such option. The options strings are *without* a leading
11504 : : '-' character e.g. ("march=x86-64"). Finally, clean up. */
11505 : :
11506 : : void
11507 : 128 : driver_get_configure_time_options (void (*cb) (const char *option,
11508 : : void *user_data),
11509 : : void *user_data)
11510 : : {
11511 : 128 : size_t i;
11512 : :
11513 : 128 : obstack_init (&obstack);
11514 : 128 : init_opts_obstack ();
11515 : 128 : n_switches = 0;
11516 : :
11517 : 1408 : for (i = 0; i < ARRAY_SIZE (option_default_specs); i++)
11518 : 1280 : do_option_spec (option_default_specs[i].name,
11519 : 1280 : option_default_specs[i].spec);
11520 : :
11521 : 384 : for (i = 0; (int) i < n_switches; i++)
11522 : : {
11523 : 256 : gcc_assert (switches[i].part1);
11524 : 256 : (*cb) (switches[i].part1, user_data);
11525 : : }
11526 : :
11527 : 128 : obstack_free (&opts_obstack, NULL);
11528 : 128 : obstack_free (&obstack, NULL);
11529 : 128 : n_switches = 0;
11530 : 128 : }
|