OSDN Git Service

2008-01-24 H.J. Lu <hongjiu.lu@intel.com>
[pf3gnuchains/gcc-fork.git] / gcc / gcc.c
index bb6d5a1..b63c0da 100644 (file)
--- a/gcc/gcc.c
+++ b/gcc/gcc.c
@@ -1,13 +1,13 @@
 /* Compiler driver program that can handle many languages.
    Copyright (C) 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
-   1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation,
-   Inc.
+   1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
+   Free Software Foundation, Inc.
 
 This file is part of GCC.
 
 GCC is free software; you can redistribute it and/or modify it under
 the terms of the GNU General Public License as published by the Free
-Software Foundation; either version 2, or (at your option) any later
+Software Foundation; either version 3, or (at your option) any later
 version.
 
 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
@@ -16,9 +16,8 @@ FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 for more details.
 
 You should have received a copy of the GNU General Public License
-along with GCC; see the file COPYING.  If not, write to the Free
-Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
-02110-1301, USA.
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.
 
 This paragraph is here to try to keep Sun CC from dying.
 The number of chars here seems crucial!!!!  */
@@ -86,6 +85,7 @@ compilation is specified by a string called a "spec".  */
 #include "prefix.h"
 #include "gcc.h"
 #include "flags.h"
+#include "opts.h"
 
 /* By default there is no special suffix for target executables.  */
 /* FIXME: when autoconf is fixed, remove the host check - dj */
@@ -126,6 +126,9 @@ static const char dir_separator_str[] = { DIR_SEPARATOR, 0 };
 /* Flag set by cppspec.c to 1.  */
 int is_cpp_driver;
 
+/* Flag set to nonzero if an @file argument has been supplied to gcc.  */
+static bool at_file_supplied;
+
 /* Flag saying to pass the greatest exit code returned by a sub-process
    to the calling program.  */
 static int pass_exit_codes;
@@ -167,6 +170,11 @@ static int print_multi_lib;
 
 static int print_help_list;
 
+/* Flag saying to print the sysroot suffix used for searching for
+   headers.  */
+
+static int print_sysroot_headers_suffix;
+
 /* Flag indicating whether we should print the command and arguments */
 
 static int verbose_flag;
@@ -178,9 +186,9 @@ static int verbose_flag;
    shell scripts to capture the driver-generated command line.  */
 static int verbose_only_flag;
 
-/* Flag indicating to print target specific command line options.  */
+/* Flag indicating how to print command line options of sub-processes.  */
 
-static int target_help_flag;
+static int print_subprocess_help;
 
 /* Flag indicating whether we should report subprocess execution times
    (if this is supported by the system - see pexecute.c).  */
@@ -237,7 +245,7 @@ static const char *spec_machine = DEFAULT_TARGET_MACHINE;
 /* Nonzero if cross-compiling.
    When -b is used, the value comes from the `specs' file.  */
 
-#ifdef CROSS_COMPILE
+#ifdef CROSS_DIRECTORY_STRUCTURE
 static const char *cross_compile = "1";
 #else
 static const char *cross_compile = "0";
@@ -287,10 +295,13 @@ static char *load_specs (const char *);
 static void read_specs (const char *, int);
 static void set_spec (const char *, const char *);
 static struct compiler *lookup_compiler (const char *, size_t, const char *);
-static char *build_search_list (struct path_prefix *, const char *, int);
-static void putenv_from_prefixes (struct path_prefix *, const char *);
+static char *build_search_list (const struct path_prefix *, const char *,
+                               bool, bool);
+static void xputenv (const char *);
+static void putenv_from_prefixes (const struct path_prefix *, const char *,
+                                 bool);
 static int access_check (const char *, int);
-static char *find_a_file (struct path_prefix *, const char *, int, int);
+static char *find_a_file (const struct path_prefix *, const char *, int, bool);
 static void add_prefix (struct path_prefix *, const char *, const char *,
                        int, int, int);
 static void add_sysrooted_prefix (struct path_prefix *, const char *,
@@ -313,13 +324,12 @@ static const char *eval_spec_function (const char *, const char *);
 static const char *handle_spec_function (const char *);
 static char *save_string (const char *, int);
 static void set_collect_gcc_options (void);
-static void do_spec_path (struct prefix_list *, const char *, int, int, int, const char *, const char *);
 static int do_spec_1 (const char *, int, const char *);
 static int do_spec_2 (const char *);
 static void do_option_spec (const char *, const char *);
 static void do_self_spec (const char *);
 static const char *find_file (const char *);
-static int is_directory (const char *, const char *, int);
+static int is_directory (const char *, bool);
 static const char *validate_switches (const char *);
 static void validate_all_switches (void);
 static inline void validate_switches_from_spec (const char *);
@@ -329,6 +339,7 @@ static int default_arg (const char *, int);
 static void set_multilib_dir (void);
 static void print_multilib_info (void);
 static void perror_with_name (const char *);
+static void fatal_ice (const char *, ...) ATTRIBUTE_PRINTF_1 ATTRIBUTE_NORETURN;
 static void notice (const char *, ...) ATTRIBUTE_PRINTF_1;
 static void display_help (void);
 static void add_preprocessor_option (const char *, int);
@@ -347,10 +358,13 @@ static void init_gcc_specs (struct obstack *, const char *, const char *,
 static const char *convert_filename (const char *, int, int);
 #endif
 
+static const char *getenv_spec_function (int, const char **);
 static const char *if_exists_spec_function (int, const char **);
 static const char *if_exists_else_spec_function (int, const char **);
 static const char *replace_outfile_spec_function (int, const char **);
 static const char *version_compare_spec_function (int, const char **);
+static const char *include_spec_function (int, const char **);
+static const char *print_asm_header_spec_function (int, const char **);
 \f
 /* The Specs Language
 
@@ -439,8 +453,8 @@ or with constant text in a single argument.
        SUFFIX characters following %O as they would following, for
        example, `.o'.
  %I    Substitute any of -iprefix (made from GCC_EXEC_PREFIX), -isysroot
-       (made from TARGET_SYSTEM_ROOT), and -isystem (made from COMPILER_PATH
-       and -B options) as necessary.
+       (made from TARGET_SYSTEM_ROOT), -isystem (made from COMPILER_PATH
+       and -B options) and -imultilib as necessary.
  %s     current argument is the name of a library or startup file of some sort.
         Search for that file in a standard list of directories
        and substitute the full name found.
@@ -506,15 +520,18 @@ or with constant text in a single argument.
           part of that switch that matched the '*'.
  %{.S:X}  substitutes X, if processing a file with suffix S.
  %{!.S:X} substitutes X, if NOT processing a file with suffix S.
-
+ %{,S:X}  substitutes X, if processing a file which will use spec S.
+ %{!,S:X} substitutes X, if NOT processing a file which will use spec S.
+         
  %{S|T:X} substitutes X if either -S or -T was given to CC.  This may be
-         combined with !, ., and * as above binding stronger than the OR.
+         combined with '!', '.', ',', and '*' as above binding stronger
+         than the OR.
          If %* appears in X, all of the alternatives must be starred, and
          only the first matching alternative is substituted.
  %{S:X;   if S was given to CC, substitutes X;
    T:Y;   else if T was given to CC, substitutes Y;
     :D}   else substitutes D.  There can be as many clauses as you need.
-          This may be combined with ., !, |, and * as above.
+          This may be combined with '.', '!', ',', '|', and '*' as above.
 
  %(Spec) processes a specification defined in a specs file as *Spec:
  %[Spec] as above, but put __ around -D arguments
@@ -639,21 +656,27 @@ proper position among the other output files.  */
 #define LINKER_NAME "collect2"
 #endif
 
+#ifdef HAVE_AS_DEBUG_PREFIX_MAP
+#define ASM_MAP " %{fdebug-prefix-map=*:--debug-prefix-map %*}"
+#else
+#define ASM_MAP ""
+#endif
+
 /* Define ASM_DEBUG_SPEC to be a spec suitable for translating '-g'
    to the assembler.  */
 #ifndef ASM_DEBUG_SPEC
 # if defined(DBX_DEBUGGING_INFO) && defined(DWARF2_DEBUGGING_INFO) \
      && defined(HAVE_AS_GDWARF2_DEBUG_FLAG) && defined(HAVE_AS_GSTABS_DEBUG_FLAG)
-#  define ASM_DEBUG_SPEC                                       \
-      (PREFERRED_DEBUGGING_TYPE == DBX_DEBUG                   \
-       ? "%{gdwarf-2*:--gdwarf2}%{!gdwarf-2*:%{g*:--gstabs}}"  \
-       : "%{gstabs*:--gstabs}%{!gstabs*:%{g*:--gdwarf2}}")
+#  define ASM_DEBUG_SPEC                                               \
+      (PREFERRED_DEBUGGING_TYPE == DBX_DEBUG                           \
+       ? "%{gdwarf-2*:--gdwarf2}%{!gdwarf-2*:%{g*:--gstabs}}" ASM_MAP  \
+       : "%{gstabs*:--gstabs}%{!gstabs*:%{g*:--gdwarf2}}" ASM_MAP)
 # else
 #  if defined(DBX_DEBUGGING_INFO) && defined(HAVE_AS_GSTABS_DEBUG_FLAG)
-#   define ASM_DEBUG_SPEC "%{g*:--gstabs}"
+#   define ASM_DEBUG_SPEC "%{g*:--gstabs}" ASM_MAP
 #  endif
 #  if defined(DWARF2_DEBUGGING_INFO) && defined(HAVE_AS_GDWARF2_DEBUG_FLAG)
-#   define ASM_DEBUG_SPEC "%{g*:--gdwarf2}"
+#   define ASM_DEBUG_SPEC "%{g*:--gdwarf2}" ASM_MAP
 #  endif
 # endif
 #endif
@@ -697,7 +720,8 @@ proper position among the other output files.  */
 %{!fsyntax-only:%{!c:%{!M:%{!MM:%{!E:%{!S:\
     %(linker) %l " LINK_PIE_SPEC "%X %{o*} %{A} %{d} %{e*} %{m} %{N} %{n} %{r}\
     %{s} %{t} %{u*} %{x} %{z} %{Z} %{!A:%{!nostdlib:%{!nostartfiles:%S}}}\
-    %{static:} %{L*} %(mfwrap) %(link_libgcc) %o %(mflib)\
+    %{static:} %{L*} %(mfwrap) %(link_libgcc) %o\
+    %{fopenmp|ftree-parallelize-loops=*:%:include(libgomp.spec)%(link_gomp)} %(mflib)\
     %{fprofile-arcs|fprofile-generate|coverage:-lgcov}\
     %{!nostdlib:%{!nodefaultlibs:%(link_ssp) %(link_gcc_c_sequence)}}\
     %{!A:%{!nostdlib:%{!nostartfiles:%E}}} %{T*} }}}}}}"
@@ -736,6 +760,7 @@ static const char *link_spec = LINK_SPEC;
 static const char *lib_spec = LIB_SPEC;
 static const char *mfwrap_spec = MFWRAP_SPEC;
 static const char *mflib_spec = MFLIB_SPEC;
+static const char *link_gomp_spec = "";
 static const char *libgcc_spec = LIBGCC_SPEC;
 static const char *endfile_spec = ENDFILE_SPEC;
 static const char *startfile_spec = STARTFILE_SPEC;
@@ -769,8 +794,9 @@ static const char *cpp_unique_options =
  %{MD:-MD %{!o:%b.d}%{o*:%.d%*}}\
  %{MMD:-MMD %{!o:%b.d}%{o*:%.d%*}}\
  %{M} %{MM} %{MF*} %{MG} %{MP} %{MQ*} %{MT*}\
- %{!E:%{!M:%{!MM:%{MD|MMD:%{o*:-MQ %*}}}}}\
- %{remap} %{g3:-dD} %{H} %C %{D*&U*&A*} %{i*} %Z %i\
+ %{!E:%{!M:%{!MM:%{!MT:%{!MQ:%{MD|MMD:%{o*:-MQ %*}}}}}}}\
+ %{remap} %{g3|ggdb3|gstabs3|gcoff3|gxcoff3|gvms3:-dD}\
+ %{H} %C %{D*&U*&A*} %{i*} %Z %i\
  %{fmudflap:-D_MUDFLAP -include mf-runtime.h}\
  %{fmudflapth:-D_MUDFLAP -D_MUDFLAPTH -include mf-runtime.h}\
  %{E|M|MM:%W{o*}}";
@@ -798,12 +824,19 @@ static const char *cc1_options =
  %{v:-version} %{pg:-p} %{p} %{f*} %{undef}\
  %{Qn:-fno-ident} %{--help:--help}\
  %{--target-help:--target-help}\
+ %{--help=*:--help=%(VALUE)}\
  %{!fsyntax-only:%{S:%W{o*}%{!o*:-o %b.s}}}\
  %{fsyntax-only:-o %j} %{-param*}\
  %{fmudflap|fmudflapth:-fno-builtin -fno-merge-constants}\
  %{coverage:-fprofile-arcs -ftest-coverage}";
 
 static const char *asm_options =
+"%{--target-help:%:print-asm-header()} "
+#if HAVE_GNU_AS
+/* If GNU AS is used, then convert -w (no warnings), -I, and -v
+   to the assembler equivalents.  */
+"%{v} %{w:-W} %{I*} "
+#endif
 "%a %Y %{c:%W{o*}%{!o*:-o %w%b%O}}%{!c:-o %d%w%u%O}";
 
 static const char *invoke_as =
@@ -834,7 +867,15 @@ static const char *const multilib_defaults_raw[] = MULTILIB_DEFAULTS;
 #define DRIVER_SELF_SPECS ""
 #endif
 
-static const char *const driver_self_specs[] = { DRIVER_SELF_SPECS };
+/* Adding -fopenmp should imply pthreads.  This is particularly important
+   for targets that use different start files and suchlike.  */
+#ifndef GOMP_SELF_SPECS
+#define GOMP_SELF_SPECS "%{fopenmp|ftree-parallelize-loops=*: -pthread}"
+#endif
+
+static const char *const driver_self_specs[] = {
+  DRIVER_SELF_SPECS, GOMP_SELF_SPECS
+};
 
 #ifndef OPTION_DEFAULT_SPECS
 #define OPTION_DEFAULT_SPECS { "", "" }
@@ -926,11 +967,10 @@ static const struct compiler default_compilers[] =
   {".CPP", "#C++", 0, 0, 0}, {".ii", "#C++", 0, 0, 0},
   {".ads", "#Ada", 0, 0, 0}, {".adb", "#Ada", 0, 0, 0},
   {".f", "#Fortran", 0, 0, 0}, {".for", "#Fortran", 0, 0, 0},
-  {".F", "#Fortran", 0, 0, 0}, {".FOR", "#Fortran", 0, 0, 0},
-  {".FPP", "#Fortran", 0, 0, 0},
-  {".f90", "#Fortran 95", 0, 0, 0}, {".f95", "#Fortran 95", 0, 0, 0},
   {".fpp", "#Fortran", 0, 0, 0}, {".F", "#Fortran", 0, 0, 0},
   {".FOR", "#Fortran", 0, 0, 0}, {".FPP", "#Fortran", 0, 0, 0},
+  {".f90", "#Fortran", 0, 0, 0}, {".f95", "#Fortran", 0, 0, 0},
+  {".F90", "#Fortran", 0, 0, 0}, {".F95", "#Fortran", 0, 0, 0},
   {".r", "#Ratfor", 0, 0, 0},
   {".p", "#Pascal", 0, 0, 0}, {".pas", "#Pascal", 0, 0, 0},
   {".java", "#Java", 0, 0, 0}, {".class", "#Java", 0, 0, 0},
@@ -983,15 +1023,16 @@ static const struct compiler default_compilers[] =
   {".s", "@assembler", 0, 1, 0},
   {"@assembler",
    "%{!M:%{!MM:%{!E:%{!S:as %(asm_debug) %(asm_options) %i %A }}}}", 0, 1, 0},
+  {".sx", "@assembler-with-cpp", 0, 1, 0},
   {".S", "@assembler-with-cpp", 0, 1, 0},
   {"@assembler-with-cpp",
 #ifdef AS_NEEDS_DASH_FOR_PIPED_INPUT
-   "%(trad_capable_cpp) -lang-asm %(cpp_options)\
+   "%(trad_capable_cpp) -lang-asm %(cpp_options) -fno-directives-only\
       %{E|M|MM:%(cpp_debug_options)}\
       %{!M:%{!MM:%{!E:%{!S:-o %|.s |\n\
        as %(asm_debug) %(asm_options) %|.s %A }}}}"
 #else
-   "%(trad_capable_cpp) -lang-asm %(cpp_options)\
+   "%(trad_capable_cpp) -lang-asm %(cpp_options) -fno-directives-only\
       %{E|M|MM:%(cpp_debug_options)}\
       %{!M:%{!MM:%{!E:%{!S:-o %|.s |\n\
        as %(asm_debug) %(asm_options) %m.s %A }}}}"
@@ -1109,6 +1150,7 @@ static const struct option_map option_map[] =
    {"--print-multi-directory", "-print-multi-directory", 0},
    {"--print-multi-os-directory", "-print-multi-os-directory", 0},
    {"--print-prog-name", "-print-prog-name=", "aj"},
+   {"--print-sysroot-headers-suffix", "-print-sysroot-headers-suffix", 0},
    {"--profile", "-p", 0},
    {"--profile-blocks", "-a", 0},
    {"--quiet", "-q", 0},
@@ -1442,7 +1484,7 @@ static const char *gcc_libexec_prefix;
 #define STANDARD_STARTFILE_PREFIX_2 "/usr/lib/"
 #endif
 
-#ifdef CROSS_COMPILE  /* Don't use these prefixes for a cross compiler.  */
+#ifdef CROSS_DIRECTORY_STRUCTURE  /* Don't use these prefixes for a cross compiler.  */
 #undef MD_EXEC_PREFIX
 #undef MD_STARTFILE_PREFIX
 #undef MD_STARTFILE_PREFIX_1
@@ -1459,25 +1501,33 @@ static const char *gcc_libexec_prefix;
 #define MD_STARTFILE_PREFIX_1 ""
 #endif
 
+/* These directories are locations set at configure-time based on the
+   --prefix option provided to configure.  Their initializers are
+   defined in Makefile.in.  These paths are not *directly* used when
+   gcc_exec_prefix is set because, in that case, we know where the
+   compiler has been installed, and use paths relative to that
+   location instead.  */
 static const char *const standard_exec_prefix = STANDARD_EXEC_PREFIX;
+static const char *const standard_libexec_prefix = STANDARD_LIBEXEC_PREFIX;
+static const char *const standard_bindir_prefix = STANDARD_BINDIR_PREFIX;
+static const char *const standard_startfile_prefix = STANDARD_STARTFILE_PREFIX;
+
+/* For native compilers, these are well-known paths containing
+   components that may be provided by the system.  For cross
+   compilers, these paths are not used.  */
 static const char *const standard_exec_prefix_1 = "/usr/libexec/gcc/";
 static const char *const standard_exec_prefix_2 = "/usr/lib/gcc/";
 static const char *md_exec_prefix = MD_EXEC_PREFIX;
-
 static const char *md_startfile_prefix = MD_STARTFILE_PREFIX;
 static const char *md_startfile_prefix_1 = MD_STARTFILE_PREFIX_1;
-static const char *const standard_startfile_prefix = STANDARD_STARTFILE_PREFIX;
 static const char *const standard_startfile_prefix_1
   = STANDARD_STARTFILE_PREFIX_1;
 static const char *const standard_startfile_prefix_2
   = STANDARD_STARTFILE_PREFIX_2;
 
+/* A relative path to be used in finding the location of tools
+   relative to the driver.  */
 static const char *const tooldir_base_prefix = TOOLDIR_BASE_PREFIX;
-static const char *tooldir_prefix;
-
-static const char *const standard_bindir_prefix = STANDARD_BINDIR_PREFIX;
-
-static const char *standard_libexec_prefix = STANDARD_LIBEXEC_PREFIX;
 
 /* Subdirectory to use for locating libraries.  Set by
    set_multilib_dir based on the compilation options.  */
@@ -1534,6 +1584,7 @@ static struct spec_list static_specs[] =
   INIT_STATIC_SPEC ("lib",                     &lib_spec),
   INIT_STATIC_SPEC ("mfwrap",                  &mfwrap_spec),
   INIT_STATIC_SPEC ("mflib",                   &mflib_spec),
+  INIT_STATIC_SPEC ("link_gomp",               &link_gomp_spec),
   INIT_STATIC_SPEC ("libgcc",                  &libgcc_spec),
   INIT_STATIC_SPEC ("startfile",               &startfile_spec),
   INIT_STATIC_SPEC ("switches_need_spaces",    &switches_need_spaces),
@@ -1577,10 +1628,16 @@ static struct spec_list *specs = (struct spec_list *) 0;
 
 static const struct spec_function static_spec_functions[] =
 {
+  { "getenv",                   getenv_spec_function },
   { "if-exists",               if_exists_spec_function },
   { "if-exists-else",          if_exists_else_spec_function },
   { "replace-outfile",         replace_outfile_spec_function },
   { "version-compare",         version_compare_spec_function },
+  { "include",                 include_spec_function },
+  { "print-asm-header",                print_asm_header_spec_function },
+#ifdef EXTRA_SPEC_FUNCTIONS
+  EXTRA_SPEC_FUNCTIONS
+#endif
   { 0, 0 }
 };
 
@@ -1601,25 +1658,30 @@ init_gcc_specs (struct obstack *obstack, const char *shared_name,
 {
   char *buf;
 
-  buf = concat ("%{static|static-libgcc:", static_name, " ", eh_name,
-               "}%{!static:%{!static-libgcc:",
+  buf = concat ("%{static|static-libgcc:", static_name, " ", eh_name, "}"
+               "%{!static:%{!static-libgcc:"
 #if USE_LD_AS_NEEDED
-               "%{!shared-libgcc:", static_name,
-               " --as-needed ", shared_name, " --no-as-needed}"
-               "%{shared-libgcc:", shared_name, "%{!shared: ", static_name,
-               "}",
+               "%{!shared-libgcc:",
+               static_name, " --as-needed ", shared_name, " --no-as-needed"
+               "}"
+               "%{shared-libgcc:",
+               shared_name, "%{!shared: ", static_name, "}"
+               "}"
 #else
-               "%{!shared:%{!shared-libgcc:", static_name, " ",
-               eh_name, "}%{shared-libgcc:", shared_name, " ",
-               static_name, "}}%{shared:",
+               "%{!shared:"
+               "%{!shared-libgcc:", static_name, " ", eh_name, "}"
+               "%{shared-libgcc:", shared_name, " ", static_name, "}"
+               "}"
 #ifdef LINK_EH_SPEC
-               "%{shared-libgcc:", shared_name,
-               "}%{!shared-libgcc:", static_name, "}",
+               "%{shared:"
+               "%{shared-libgcc:", shared_name, "}"
+               "%{!shared-libgcc:", static_name, "}"
+               "}"
 #else
-               shared_name,
+               "%{shared:", shared_name, "}"
 #endif
 #endif
-               "}}}", NULL);
+               "}}", NULL);
 
   obstack_grow (obstack, buf, strlen (buf));
   free (buf);
@@ -1657,10 +1719,6 @@ init_spec (void)
     }
 #endif
 
-  /* Initialize here, not in definition.  The IRIX 6 O32 cc sometimes chokes
-     on ?: in file-scope variable initializations.  */
-  asm_debug = ASM_DEBUG_SPEC;
-
   for (i = ARRAY_SIZE (static_specs) - 1; i >= 0; i--)
     {
       sl = &static_specs[i];
@@ -1805,7 +1863,7 @@ set_spec (const char *name, const char *spec)
   if (!sl)
     {
       /* Not found - make it.  */
-      sl = xmalloc (sizeof (struct spec_list));
+      sl = XNEW (struct spec_list);
       sl->name = xstrdup (name);
       sl->name_len = name_len;
       sl->ptr_spec = &sl->ptr;
@@ -1827,7 +1885,7 @@ set_spec (const char *name, const char *spec)
 
   /* Free the old spec.  */
   if (old_spec && sl->alloc_p)
-    free ((void *) old_spec);
+    free (CONST_CAST(char *, old_spec));
 
   sl->alloc_p = 1;
 }
@@ -1851,6 +1909,12 @@ static int argbuf_index;
 
 static int have_o_argbuf_index = 0;
 
+/* Were the options -c or -S passed.  */
+static int have_c = 0;
+
+/* Was the option -o passed.  */
+static int have_o = 0;
+
 /* This is the list of suffixes and codes (%g/%u/%U/%j) and the associated
    temp file.  If the HOST_BIT_BUCKET is used for %j, no entry is made for
    it here.  */
@@ -1882,7 +1946,7 @@ static void
 alloc_args (void)
 {
   argbuf_length = 10;
-  argbuf = xmalloc (argbuf_length * sizeof (const char *));
+  argbuf = XNEWVEC (const char *, argbuf_length);
 }
 
 /* Clear out the vector of arguments (after a command is executed).  */
@@ -1941,14 +2005,14 @@ load_specs (const char *filename)
     pfatal_with_name (filename);
 
   /* Read contents of file into BUFFER.  */
-  buffer = xmalloc ((unsigned) statbuf.st_size + 1);
+  buffer = XNEWVEC (char, statbuf.st_size + 1);
   readlen = read (desc, buffer, (unsigned) statbuf.st_size);
   if (readlen < 0)
     pfatal_with_name (filename);
   buffer[readlen] = 0;
   close (desc);
 
-  specs = xmalloc (readlen + 1);
+  specs = XNEWVEC (char, readlen + 1);
   specs_p = specs;
   for (buffer_p = buffer; buffer_p && *buffer_p; buffer_p++)
     {
@@ -2031,7 +2095,7 @@ read_specs (const char *filename, int main_p)
                       (long) (p1 - buffer + 1));
 
              p[-2] = '\0';
-             new_filename = find_a_file (&startfile_prefixes, p1, R_OK, 0);
+             new_filename = find_a_file (&startfile_prefixes, p1, R_OK, true);
              read_specs (new_filename ? new_filename : p1, FALSE);
              continue;
            }
@@ -2050,7 +2114,7 @@ read_specs (const char *filename, int main_p)
                       (long) (p1 - buffer + 1));
 
              p[-2] = '\0';
-             new_filename = find_a_file (&startfile_prefixes, p1, R_OK, 0);
+             new_filename = find_a_file (&startfile_prefixes, p1, R_OK, true);
              if (new_filename)
                read_specs (new_filename, FALSE);
              else if (verbose_flag)
@@ -2126,7 +2190,7 @@ read_specs (const char *filename, int main_p)
 
              set_spec (p2, *(sl->ptr_spec));
              if (sl->alloc_p)
-               free ((void *) *(sl->ptr_spec));
+               free (CONST_CAST (char *, *(sl->ptr_spec)));
 
              *(sl->ptr_spec) = "";
              sl->alloc_p = 0;
@@ -2264,7 +2328,7 @@ record_temp_file (const char *filename, int always_delete, int fail_delete)
        if (! strcmp (name, temp->name))
          goto already1;
 
-      temp = xmalloc (sizeof (struct temp_file));
+      temp = XNEW (struct temp_file);
       temp->next = always_delete_queue;
       temp->name = name;
       always_delete_queue = temp;
@@ -2279,7 +2343,7 @@ record_temp_file (const char *filename, int always_delete, int fail_delete)
        if (! strcmp (name, temp->name))
          goto already2;
 
-      temp = xmalloc (sizeof (struct temp_file));
+      temp = XNEW (struct temp_file);
       temp->next = failure_delete_queue;
       temp->name = name;
       failure_delete_queue = temp;
@@ -2347,64 +2411,223 @@ clear_failure_queue (void)
   failure_delete_queue = 0;
 }
 \f
-/* Build a list of search directories from PATHS.
-   PREFIX is a string to prepend to the list.
-   If CHECK_DIR_P is nonzero we ensure the directory exists.
-   This is used mostly by putenv_from_prefixes so we use `collect_obstack'.
-   It is also used by the --print-search-dirs flag.  */
-
-static char *
-build_search_list (struct path_prefix *paths, const char *prefix,
-                  int check_dir_p)
+/* Call CALLBACK for each path in PATHS, breaking out early if CALLBACK
+   returns non-NULL.
+   If DO_MULTI is true iterate over the paths twice, first with multilib
+   suffix then without, otherwise iterate over the paths once without
+   adding a multilib suffix.  When DO_MULTI is true, some attempt is made
+   to avoid visiting the same path twice, but we could do better.  For
+   instance, /usr/lib/../lib is considered different from /usr/lib.
+   At least EXTRA_SPACE chars past the end of the path passed to
+   CALLBACK are available for use by the callback.
+   CALLBACK_INFO allows extra parameters to be passed to CALLBACK.
+
+   Returns the value returned by CALLBACK.  */
+
+static void *
+for_each_path (const struct path_prefix *paths,
+              bool do_multi,
+              size_t extra_space,
+              void *(*callback) (char *, void *),
+              void *callback_info)
 {
-  int suffix_len = (machine_suffix) ? strlen (machine_suffix) : 0;
-  int just_suffix_len
-    = (just_machine_suffix) ? strlen (just_machine_suffix) : 0;
-  int first_time = TRUE;
-  struct prefix_list *pprefix;
-
-  obstack_grow (&collect_obstack, prefix, strlen (prefix));
-  obstack_1grow (&collect_obstack, '=');
-
-  for (pprefix = paths->plist; pprefix != 0; pprefix = pprefix->next)
+  struct prefix_list *pl;
+  const char *multi_dir = NULL;
+  const char *multi_os_dir = NULL;
+  const char *multi_suffix;
+  const char *just_multi_suffix;
+  char *path = NULL;
+  void *ret = NULL;
+  bool skip_multi_dir = false;
+  bool skip_multi_os_dir = false;
+
+  multi_suffix = machine_suffix;
+  just_multi_suffix = just_machine_suffix;
+  if (do_multi && multilib_dir && strcmp (multilib_dir, ".") != 0)
     {
-      int len = strlen (pprefix->prefix);
+      multi_dir = concat (multilib_dir, dir_separator_str, NULL);
+      multi_suffix = concat (multi_suffix, multi_dir, NULL);
+      just_multi_suffix = concat (just_multi_suffix, multi_dir, NULL);
+    }
+  if (do_multi && multilib_os_dir && strcmp (multilib_os_dir, ".") != 0)
+    multi_os_dir = concat (multilib_os_dir, dir_separator_str, NULL);
 
-      if (machine_suffix
-         && (! check_dir_p
-             || is_directory (pprefix->prefix, machine_suffix, 0)))
+  while (1)
+    {
+      size_t multi_dir_len = 0;
+      size_t multi_os_dir_len = 0;
+      size_t suffix_len;
+      size_t just_suffix_len;
+      size_t len;
+
+      if (multi_dir)
+       multi_dir_len = strlen (multi_dir);
+      if (multi_os_dir)
+       multi_os_dir_len = strlen (multi_os_dir);
+      suffix_len = strlen (multi_suffix);
+      just_suffix_len = strlen (just_multi_suffix);
+
+      if (path == NULL)
        {
-         if (!first_time)
-           obstack_1grow (&collect_obstack, PATH_SEPARATOR);
-
-         first_time = FALSE;
-         obstack_grow (&collect_obstack, pprefix->prefix, len);
-         obstack_grow (&collect_obstack, machine_suffix, suffix_len);
+         len = paths->max_len + extra_space + 1;
+         if (suffix_len > multi_os_dir_len)
+           len += suffix_len;
+         else
+           len += multi_os_dir_len;
+         path = XNEWVEC (char, len);
        }
 
-      if (just_machine_suffix
-         && pprefix->require_machine_suffix == 2
-         && (! check_dir_p
-             || is_directory (pprefix->prefix, just_machine_suffix, 0)))
+      for (pl = paths->plist; pl != 0; pl = pl->next)
        {
-         if (! first_time)
-           obstack_1grow (&collect_obstack, PATH_SEPARATOR);
+         len = strlen (pl->prefix);
+         memcpy (path, pl->prefix, len);
+
+         /* Look first in MACHINE/VERSION subdirectory.  */
+         if (!skip_multi_dir)
+           {
+             memcpy (path + len, multi_suffix, suffix_len + 1);
+             ret = callback (path, callback_info);
+             if (ret)
+               break;
+           }
+
+         /* Some paths are tried with just the machine (ie. target)
+            subdir.  This is used for finding as, ld, etc.  */
+         if (!skip_multi_dir
+             && pl->require_machine_suffix == 2)
+           {
+             memcpy (path + len, just_multi_suffix, just_suffix_len + 1);
+             ret = callback (path, callback_info);
+             if (ret)
+               break;
+           }
+
+         /* Now try the base path.  */
+         if (!pl->require_machine_suffix
+             && !(pl->os_multilib ? skip_multi_os_dir : skip_multi_dir))
+           {
+             const char *this_multi;
+             size_t this_multi_len;
 
-         first_time = FALSE;
-         obstack_grow (&collect_obstack, pprefix->prefix, len);
-         obstack_grow (&collect_obstack, just_machine_suffix,
-                       just_suffix_len);
+             if (pl->os_multilib)
+               {
+                 this_multi = multi_os_dir;
+                 this_multi_len = multi_os_dir_len;
+               }
+             else
+               {
+                 this_multi = multi_dir;
+                 this_multi_len = multi_dir_len;
+               }
+
+             if (this_multi_len)
+               memcpy (path + len, this_multi, this_multi_len + 1);
+             else
+               path[len] = '\0';
+
+             ret = callback (path, callback_info);
+             if (ret)
+               break;
+           }
        }
+      if (pl)
+       break;
 
-      if (! pprefix->require_machine_suffix)
-       {
-         if (! first_time)
-           obstack_1grow (&collect_obstack, PATH_SEPARATOR);
+      if (multi_dir == NULL && multi_os_dir == NULL)
+       break;
 
-         first_time = FALSE;
-         obstack_grow (&collect_obstack, pprefix->prefix, len);
+      /* Run through the paths again, this time without multilibs.
+        Don't repeat any we have already seen.  */
+      if (multi_dir)
+       {
+         free (CONST_CAST (char *, multi_dir));
+         multi_dir = NULL;
+         free (CONST_CAST (char *, multi_suffix));
+         multi_suffix = machine_suffix;
+         free (CONST_CAST (char *, just_multi_suffix));
+         just_multi_suffix = just_machine_suffix;
+       }
+      else
+       skip_multi_dir = true;
+      if (multi_os_dir)
+       {
+         free (CONST_CAST (char *, multi_os_dir));
+         multi_os_dir = NULL;
        }
+      else
+       skip_multi_os_dir = true;
+    }
+
+  if (multi_dir)
+    {
+      free (CONST_CAST (char *, multi_dir));
+      free (CONST_CAST (char *, multi_suffix));
+      free (CONST_CAST (char *, just_multi_suffix));
     }
+  if (multi_os_dir)
+    free (CONST_CAST (char *, multi_os_dir));
+  if (ret != path)
+    free (path);
+  return ret;
+}
+
+/* Callback for build_search_list.  Adds path to obstack being built.  */
+
+struct add_to_obstack_info {
+  struct obstack *ob;
+  bool check_dir;
+  bool first_time;
+};
+
+static void *
+add_to_obstack (char *path, void *data)
+{
+  struct add_to_obstack_info *info = data;
+
+  if (info->check_dir && !is_directory (path, false))
+    return NULL;
+
+  if (!info->first_time)
+    obstack_1grow (info->ob, PATH_SEPARATOR);
+
+  obstack_grow (info->ob, path, strlen (path));
+
+  info->first_time = false;
+  return NULL;
+}
+
+/* Add or change the value of an environment variable, outputting the
+   change to standard error if in verbose mode.  */
+static void
+xputenv (const char *string)
+{
+  if (verbose_flag)
+    notice ("%s\n", string);
+  putenv (CONST_CAST (char *, string));
+}
+
+/* Build a list of search directories from PATHS.
+   PREFIX is a string to prepend to the list.
+   If CHECK_DIR_P is true we ensure the directory exists.
+   If DO_MULTI is true, multilib paths are output first, then
+   non-multilib paths.
+   This is used mostly by putenv_from_prefixes so we use `collect_obstack'.
+   It is also used by the --print-search-dirs flag.  */
+
+static char *
+build_search_list (const struct path_prefix *paths, const char *prefix,
+                  bool check_dir, bool do_multi)
+{
+  struct add_to_obstack_info info;
+
+  info.ob = &collect_obstack;
+  info.check_dir = check_dir;
+  info.first_time = true;
+
+  obstack_grow (&collect_obstack, prefix, strlen (prefix));
+  obstack_1grow (&collect_obstack, '=');
+
+  for_each_path (paths, do_multi, 0, add_to_obstack, &info);
 
   obstack_1grow (&collect_obstack, '\0');
   return XOBFINISH (&collect_obstack, char *);
@@ -2414,9 +2637,10 @@ build_search_list (struct path_prefix *paths, const char *prefix,
    for collect.  */
 
 static void
-putenv_from_prefixes (struct path_prefix *paths, const char *env_var)
+putenv_from_prefixes (const struct path_prefix *paths, const char *env_var,
+                     bool do_multi)
 {
-  putenv (build_search_list (paths, env_var, 1));
+  xputenv (build_search_list (paths, env_var, true, do_multi));
 }
 \f
 /* Check whether NAME can be accessed in MODE.  This is like access,
@@ -2437,20 +2661,53 @@ access_check (const char *name, int mode)
   return access (name, mode);
 }
 
+/* Callback for find_a_file.  Appends the file name to the directory
+   path.  If the resulting file exists in the right mode, return the
+   full pathname to the file.  */
+
+struct file_at_path_info {
+  const char *name;
+  const char *suffix;
+  int name_len;
+  int suffix_len;
+  int mode;
+};
+
+static void *
+file_at_path (char *path, void *data)
+{
+  struct file_at_path_info *info = data;
+  size_t len = strlen (path);
+
+  memcpy (path + len, info->name, info->name_len);
+  len += info->name_len;
+
+  /* Some systems have a suffix for executable files.
+     So try appending that first.  */
+  if (info->suffix_len)
+    {
+      memcpy (path + len, info->suffix, info->suffix_len + 1);
+      if (access_check (path, info->mode) == 0)
+       return path;
+    }
+
+  path[len] = '\0';
+  if (access_check (path, info->mode) == 0)
+    return path;
+
+  return NULL;
+}
+
 /* Search for NAME using the prefix list PREFIXES.  MODE is passed to
-   access to check permissions.
+   access to check permissions.  If DO_MULTI is true, search multilib
+   paths then non-multilib paths, otherwise do not search multilib paths.
    Return 0 if not found, otherwise return its name, allocated with malloc.  */
 
 static char *
-find_a_file (struct path_prefix *pprefix, const char *name, int mode,
-            int multilib)
+find_a_file (const struct path_prefix *pprefix, const char *name, int mode,
+            bool do_multi)
 {
-  char *temp;
-  const char *const file_suffix =
-    ((mode & X_OK) != 0 ? HOST_EXECUTABLE_SUFFIX : "");
-  struct prefix_list *pl;
-  int len = pprefix->max_len + strlen (name) + strlen (file_suffix) + 1;
-  const char *multilib_name, *multilib_os_name;
+  struct file_at_path_info info;
 
 #ifdef DEFAULT_ASSEMBLER
   if (! strcmp (name, "as") && access (DEFAULT_ASSEMBLER, mode) == 0)
@@ -2462,112 +2719,24 @@ find_a_file (struct path_prefix *pprefix, const char *name, int mode,
     return xstrdup (DEFAULT_LINKER);
 #endif
 
-  if (machine_suffix)
-    len += strlen (machine_suffix);
-
-  multilib_name = name;
-  multilib_os_name = name;
-  if (multilib && multilib_os_dir)
-    {
-      int len1 = multilib_dir ? strlen (multilib_dir) + 1 : 0;
-      int len2 = strlen (multilib_os_dir) + 1;
-
-      len += len1 > len2 ? len1 : len2;
-      if (multilib_dir)
-       multilib_name = ACONCAT ((multilib_dir, dir_separator_str, name,
-                                 NULL));
-      if (strcmp (multilib_os_dir, ".") != 0)
-       multilib_os_name = ACONCAT ((multilib_os_dir, dir_separator_str, name,
-                                   NULL));
-    }
-
-  temp = xmalloc (len);
-
   /* Determine the filename to execute (special case for absolute paths).  */
 
   if (IS_ABSOLUTE_PATH (name))
     {
       if (access (name, mode) == 0)
-       {
-         strcpy (temp, name);
-         return temp;
-       }
-    }
-  else
-    for (pl = pprefix->plist; pl; pl = pl->next)
-      {
-       const char *this_name
-         = pl->os_multilib ? multilib_os_name : multilib_name;
-
-       if (machine_suffix)
-         {
-           /* Some systems have a suffix for executable files.
-              So try appending that first.  */
-           if (file_suffix[0] != 0)
-             {
-               strcpy (temp, pl->prefix);
-               strcat (temp, machine_suffix);
-               strcat (temp, multilib_name);
-               strcat (temp, file_suffix);
-               if (access_check (temp, mode) == 0)
-                 return temp;
-             }
-
-           /* Now try just the multilib_name.  */
-           strcpy (temp, pl->prefix);
-           strcat (temp, machine_suffix);
-           strcat (temp, multilib_name);
-           if (access_check (temp, mode) == 0)
-             return temp;
-         }
+       return xstrdup (name);
 
-       /* Certain prefixes are tried with just the machine type,
-          not the version.  This is used for finding as, ld, etc.  */
-       if (just_machine_suffix && pl->require_machine_suffix == 2)
-         {
-           /* Some systems have a suffix for executable files.
-              So try appending that first.  */
-           if (file_suffix[0] != 0)
-             {
-               strcpy (temp, pl->prefix);
-               strcat (temp, just_machine_suffix);
-               strcat (temp, multilib_name);
-               strcat (temp, file_suffix);
-               if (access_check (temp, mode) == 0)
-                 return temp;
-             }
-
-           strcpy (temp, pl->prefix);
-           strcat (temp, just_machine_suffix);
-           strcat (temp, multilib_name);
-           if (access_check (temp, mode) == 0)
-             return temp;
-         }
-
-       /* Certain prefixes can't be used without the machine suffix
-          when the machine or version is explicitly specified.  */
-       if (! pl->require_machine_suffix)
-         {
-           /* Some systems have a suffix for executable files.
-              So try appending that first.  */
-           if (file_suffix[0] != 0)
-             {
-               strcpy (temp, pl->prefix);
-               strcat (temp, this_name);
-               strcat (temp, file_suffix);
-               if (access_check (temp, mode) == 0)
-                 return temp;
-             }
+      return NULL;
+    }
 
-           strcpy (temp, pl->prefix);
-           strcat (temp, this_name);
-           if (access_check (temp, mode) == 0)
-             return temp;
-         }
-      }
+  info.name = name;
+  info.suffix = (mode & X_OK) != 0 ? HOST_EXECUTABLE_SUFFIX : "";
+  info.name_len = strlen (info.name);
+  info.suffix_len = strlen (info.suffix);
+  info.mode = mode;
 
-  free (temp);
-  return 0;
+  return for_each_path (pprefix, do_multi, info.name_len + info.suffix_len,
+                       file_at_path, &info);
 }
 
 /* Ranking of prefixes in the sort list. -B prefixes are put before
@@ -2613,7 +2782,7 @@ add_prefix (struct path_prefix *pprefix, const char *prefix,
   if (len > pprefix->max_len)
     pprefix->max_len = len;
 
-  pl = xmalloc (sizeof (struct prefix_list));
+  pl = XNEW (struct prefix_list);
   pl->prefix = prefix;
   pl->require_machine_suffix = require_machine_suffix;
   pl->priority = priority;
@@ -2625,6 +2794,7 @@ add_prefix (struct path_prefix *pprefix, const char *prefix,
 }
 
 /* Same as add_prefix, but prepending target_system_root to prefix.  */
+/* The target_system_root prefix has been relocated by gcc_exec_prefix.  */
 static void
 add_sysrooted_prefix (struct path_prefix *pprefix, const char *prefix,
                      const char *component,
@@ -2686,7 +2856,7 @@ execute (void)
 
   commands[0].prog = argbuf[0]; /* first command.  */
   commands[0].argv = &argbuf[0];
-  string = find_a_file (&exec_prefixes, commands[0].prog, X_OK, 0);
+  string = find_a_file (&exec_prefixes, commands[0].prog, X_OK, false);
 
   if (string)
     commands[0].argv[0] = string;
@@ -2701,7 +2871,7 @@ execute (void)
        commands[n_commands].prog = argbuf[i + 1];
        commands[n_commands].argv = &argbuf[i + 1];
        string = find_a_file (&exec_prefixes, commands[n_commands].prog,
-                             X_OK, 0);
+                             X_OK, false);
        if (string)
          commands[n_commands].argv[0] = string;
        n_commands++;
@@ -2812,7 +2982,7 @@ execute (void)
       errmsg = pex_run (pex,
                        ((i + 1 == n_commands ? PEX_LAST : 0)
                         | (string == commands[i].prog ? PEX_SEARCH : 0)),
-                       string, (char * const *) commands[i].argv,
+                       string, CONST_CAST (char **, commands[i].argv),
                        NULL, NULL, &err);
       if (errmsg != NULL)
        {
@@ -2826,7 +2996,7 @@ execute (void)
        }
 
       if (string != commands[i].prog)
-       free ((void *) string);
+       free (CONST_CAST (char *, string));
     }
 
   execution_count++;
@@ -2866,17 +3036,18 @@ execute (void)
               we would otherwise have succeeded.  */
            if (WTERMSIG (status) == SIGPIPE
                && (signal_count || greatest_status >= MIN_FATAL_STATUS))
-             ;
+             {
+               signal_count++;
+               ret_code = -1;
+             }
            else
 #endif
-             fatal ("\
+             fatal_ice ("\
 Internal error: %s (program %s)\n\
 Please submit a full bug report.\n\
 See %s for instructions.",
-                    strsignal (WTERMSIG (status)), commands[i].prog,
-                    bug_report_url);
-           signal_count++;
-           ret_code = -1;
+                       strsignal (WTERMSIG (status)), commands[i].prog,
+                       bug_report_url);
          }
        else if (WIFEXITED (status)
                 && WEXITSTATUS (status) >= MIN_FATAL_STATUS)
@@ -2911,24 +3082,22 @@ See %s for instructions.",
    If a switch uses following arguments, then the `part1' field
    is the switch itself and the `args' field
    is a null-terminated vector containing the following arguments.
-   The `live_cond' field is:
-   0 when initialized
-   1 if the switch is true in a conditional spec,
-   -1 if false (overridden by a later switch)
-   -2 if this switch should be ignored (used in %<S)
+   Bits in the `live_cond' field are:
+   SWITCH_LIVE to indicate this switch is true in a conditional spec.
+   SWITCH_FALSE to indicate this switch is overridden by a later switch.
+   SWITCH_IGNORE to indicate this switch should be ignored (used in %<S).
    The `validated' field is nonzero if any spec has looked at this switch;
    if it remains zero at the end of the run, it must be meaningless.  */
 
-#define SWITCH_OK       0
-#define SWITCH_FALSE   -1
-#define SWITCH_IGNORE  -2
-#define SWITCH_LIVE     1
+#define SWITCH_LIVE    0x1
+#define SWITCH_FALSE   0x2
+#define SWITCH_IGNORE  0x4
 
 struct switchstr
 {
   const char *part1;
   const char **args;
-  int live_cond;
+  unsigned int live_cond;
   unsigned char validated;
   unsigned char ordering;
 };
@@ -3039,6 +3208,8 @@ display_help (void)
   fputs (_("  -pass-exit-codes         Exit with highest error code from a phase\n"), stdout);
   fputs (_("  --help                   Display this information\n"), stdout);
   fputs (_("  --target-help            Display target specific command line options\n"), stdout);
+  fputs (_("  --help={target|optimizers|warnings|undocumented|params}[,{[^]joined|[^]separate}]\n"), stdout);
+  fputs (_("                           Display specific types of command line options\n"), stdout);
   if (! verbose_flag)
     fputs (_("  (Use '-v --help' to display command line options of sub-processes)\n"), stdout);
   fputs (_("  -dumpspecs               Display all of the built in spec strings\n"), stdout);
@@ -3053,6 +3224,7 @@ display_help (void)
   -print-multi-lib         Display the mapping between command line options and\n\
                            multiple library search directories\n"), stdout);
   fputs (_("  -print-multi-os-directory Display the relative path to OS libraries\n"), stdout);
+  fputs (_("  -print-sysroot-headers-suffix Display the sysroot suffix used to find headers\n"), stdout);
   fputs (_("  -Wa,<options>            Pass comma-separated <options> on to the assembler\n"), stdout);
   fputs (_("  -Wp,<options>            Pass comma-separated <options> on to the preprocessor\n"), stdout);
   fputs (_("  -Wl,<options>            Pass comma-separated <options> on to the linker\n"), stdout);
@@ -3067,7 +3239,7 @@ display_help (void)
   fputs (_("  -std=<standard>          Assume that the input sources are for <standard>\n"), stdout);
   fputs (_("\
   --sysroot=<directory>    Use <directory> as the root directory for headers\n\
-                           for headers and libraries\n"), stdout);
+                           and libraries\n"), stdout);
   fputs (_("  -B <directory>           Add <directory> to the compiler's search paths\n"), stdout);
   fputs (_("  -b <machine>             Run gcc for target <machine>, if installed\n"), stdout);
   fputs (_("  -V <version>             Run gcc version number <version>, if installed\n"), stdout);
@@ -3100,7 +3272,7 @@ add_preprocessor_option (const char *option, int len)
   n_preprocessor_options++;
 
   if (! preprocessor_options)
-    preprocessor_options = xmalloc (n_preprocessor_options * sizeof (char *));
+    preprocessor_options = XNEWVEC (char *, n_preprocessor_options);
   else
     preprocessor_options = xrealloc (preprocessor_options,
                                     n_preprocessor_options * sizeof (char *));
@@ -3115,7 +3287,7 @@ add_assembler_option (const char *option, int len)
   n_assembler_options++;
 
   if (! assembler_options)
-    assembler_options = xmalloc (n_assembler_options * sizeof (char *));
+    assembler_options = XNEWVEC (char *, n_assembler_options);
   else
     assembler_options = xrealloc (assembler_options,
                                  n_assembler_options * sizeof (char *));
@@ -3129,7 +3301,7 @@ add_linker_option (const char *option, int len)
   n_linker_options++;
 
   if (! linker_options)
-    linker_options = xmalloc (n_linker_options * sizeof (char *));
+    linker_options = XNEWVEC (char *, n_linker_options);
   else
     linker_options = xrealloc (linker_options,
                               n_linker_options * sizeof (char *));
@@ -3148,12 +3320,12 @@ process_command (int argc, const char **argv)
   char *temp1;
   const char *spec_lang = 0;
   int last_language_n_infiles;
-  int have_c = 0;
   int lang_n_infiles = 0;
 #ifdef MODIFY_TARGET_NAME
   int is_modify_target_name;
-  int j;
+  unsigned int j;
 #endif
+  const char *tooldir_prefix;
 
   GET_ENVIRONMENT (gcc_exec_prefix, "GCC_EXEC_PREFIX");
 
@@ -3175,11 +3347,11 @@ process_command (int argc, const char **argv)
     }
 
   /* If there is a -V or -b option (or both), process it now, before
-     trying to interpret the rest of the command line. 
+     trying to interpret the rest of the command line.
      Use heuristic that all configuration names must have at least
      one dash '-'. This allows us to pass options starting with -b.  */
   if (argc > 1 && argv[1][0] == '-'
-      && (argv[1][1] == 'V' || 
+      && (argv[1][1] == 'V' ||
         ((argv[1][1] == 'b') && (NULL != strchr(argv[1] + 2,'-')))))
     {
       const char *new_version = DEFAULT_TARGET_VERSION;
@@ -3247,14 +3419,30 @@ process_command (int argc, const char **argv)
                                                 standard_bindir_prefix,
                                                 standard_libexec_prefix);
       if (gcc_exec_prefix)
-       putenv (concat ("GCC_EXEC_PREFIX=", gcc_exec_prefix, NULL));
+       xputenv (concat ("GCC_EXEC_PREFIX=", gcc_exec_prefix, NULL));
     }
   else
-    gcc_libexec_prefix = make_relative_prefix (gcc_exec_prefix,
-                                              standard_exec_prefix,
-                                              standard_libexec_prefix);
+    {
+      /* make_relative_prefix requires a program name, but
+        GCC_EXEC_PREFIX is typically a directory name with a trailing
+        / (which is ignored by make_relative_prefix), so append a
+        program name.  */
+      char *tmp_prefix = concat (gcc_exec_prefix, "gcc", NULL);
+      gcc_libexec_prefix = make_relative_prefix (tmp_prefix,
+                                                standard_exec_prefix,
+                                                standard_libexec_prefix);
+
+      /* The path is unrelocated, so fallback to the original setting.  */
+      if (!gcc_libexec_prefix)
+       gcc_libexec_prefix = standard_libexec_prefix;
+
+      free (tmp_prefix);
+    }
 #else
 #endif
+  /* From this point onward, gcc_exec_prefix is non-null if the toolchain
+     is relocated. The toolchain was either relocated using GCC_EXEC_PREFIX
+     or an automatically created GCC_EXEC_PREFIX from argv[0].  */
 
   if (gcc_exec_prefix)
     {
@@ -3415,8 +3603,9 @@ process_command (int argc, const char **argv)
       else if (strcmp (argv[i], "-fversion") == 0)
        {
          /* translate_options () has turned --version into -fversion.  */
-         printf (_("%s (GCC) %s\n"), programname, version_string);
-         printf ("Copyright %s 2005 Free Software Foundation, Inc.\n",
+         printf (_("%s %s%s\n"), programname, pkgversion_string,
+                 version_string);
+         printf ("Copyright %s 2007 Free Software Foundation, Inc.\n",
                  _("(C)"));
          fputs (_("This is free software; see the source for copying conditions.  There is NO\n\
 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n"),
@@ -3438,10 +3627,19 @@ warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n"
          add_assembler_option ("--help", 6);
          add_linker_option ("--help", 6);
        }
+      else if (strncmp (argv[i], "-fhelp=", 7) == 0)
+       {
+         /* translate_options () has turned --help into -fhelp.  */
+         print_subprocess_help = 2;
+
+         /* We will be passing a dummy file on to the sub-processes.  */
+         n_infiles++;
+         n_switches++;
+       }
       else if (strcmp (argv[i], "-ftarget-help") == 0)
        {
          /* translate_options() has turned --target-help into -ftarget-help.  */
-         target_help_flag = 1;
+         print_subprocess_help = 1;
 
          /* We will be passing a dummy file on to the sub-processes.  */
          n_infiles++;
@@ -3472,6 +3670,8 @@ warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n"
        print_multi_directory = 1;
       else if (! strcmp (argv[i], "-print-multi-os-directory"))
        print_multi_os_directory = 1;
+      else if (! strcmp (argv[i], "-print-sysroot-headers-suffix"))
+       print_sysroot_headers_suffix = 1;
       else if (! strncmp (argv[i], "-Wa,", 4))
        {
          int prev, j;
@@ -3560,7 +3760,7 @@ warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n"
        }
       else if (strcmp (argv[i], "-specs") == 0)
        {
-         struct user_specs *user = xmalloc (sizeof (struct user_specs));
+         struct user_specs *user = XNEW (struct user_specs);
          if (++i >= argc)
            fatal ("argument to '-specs' is missing");
 
@@ -3574,7 +3774,7 @@ warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n"
        }
       else if (strncmp (argv[i], "-specs=", 7) == 0)
        {
-         struct user_specs *user = xmalloc (sizeof (struct user_specs));
+         struct user_specs *user = XNEW (struct user_specs);
          if (strlen (argv[i]) == 7)
            fatal ("argument to '-specs=' is missing");
 
@@ -3612,7 +3812,10 @@ warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n"
          switch (c)
            {
            case 'b':
-             if (NULL == strchr(argv[i] + 2, '-')) break;
+             if (NULL == strchr(argv[i] + 2, '-'))
+               goto normal_switch;
+
+             /* Fall through.  */
            case 'V':
              fatal ("'-%c' must come at the start of the command line", c);
              break;
@@ -3639,37 +3842,15 @@ warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n"
                   if appending a directory separator actually makes a
                   valid directory name.  */
                if (! IS_DIR_SEPARATOR (value [len - 1])
-                   && is_directory (value, "", 0))
+                   && is_directory (value, false))
                  {
-                   char *tmp = xmalloc (len + 2);
+                   char *tmp = XNEWVEC (char, len + 2);
                    strcpy (tmp, value);
                    tmp[len] = DIR_SEPARATOR;
                    tmp[++ len] = 0;
                    value = tmp;
                  }
 
-               /* As a kludge, if the arg is "[foo/]stageN/", just
-                  add "[foo/]include" to the include prefix.  */
-               if ((len == 7
-                    || (len > 7
-                        && (IS_DIR_SEPARATOR (value[len - 8]))))
-                   && strncmp (value + len - 7, "stage", 5) == 0
-                   && ISDIGIT (value[len - 2])
-                   && (IS_DIR_SEPARATOR (value[len - 1])))
-                 {
-                   if (len == 7)
-                     add_prefix (&include_prefixes, "./", NULL,
-                                 PREFIX_PRIORITY_B_OPT, 0, 0);
-                   else
-                     {
-                       char *string = xmalloc (len - 6);
-                       memcpy (string, value, len - 7);
-                       string[len - 7] = 0;
-                       add_prefix (&include_prefixes, string, NULL,
-                                   PREFIX_PRIORITY_B_OPT, 0, 0);
-                     }
-                 }
-
                add_prefix (&exec_prefixes, value, NULL,
                            PREFIX_PRIORITY_B_OPT, 0, 0);
                add_prefix (&startfile_prefixes, value, NULL,
@@ -3700,6 +3881,7 @@ warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n"
              goto normal_switch;
 
            case 'o':
+             have_o = 1;
 #if defined(HAVE_TARGET_EXECUTABLE_SUFFIX)
              if (! have_c)
                {
@@ -3800,62 +3982,50 @@ warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n"
       use_pipes = 0;
     }
 
-  /* Set up the search paths before we go looking for config files.  */
+  /* Set up the search paths.  We add directories that we expect to
+     contain GNU Toolchain components before directories specified by
+     the machine description so that we will find GNU components (like
+     the GNU assembler) before those of the host system.  */ 
 
-  /* These come before the md prefixes so that we will find gcc's subcommands
-     (such as cpp) rather than those of the host system.  */
-  /* Use 2 as fourth arg meaning try just the machine as a suffix,
-     as well as trying the machine and the version.  */
+  /* If we don't know where the toolchain has been installed, use the
+     configured-in locations.  */
+  if (!gcc_exec_prefix)
+    {
 #ifndef OS2
-  add_prefix (&exec_prefixes, standard_libexec_prefix, "GCC",
-             PREFIX_PRIORITY_LAST, 1, 0);
-  add_prefix (&exec_prefixes, standard_libexec_prefix, "BINUTILS",
-             PREFIX_PRIORITY_LAST, 2, 0);
-  add_prefix (&exec_prefixes, standard_exec_prefix, "BINUTILS",
-             PREFIX_PRIORITY_LAST, 2, 0);
-  add_prefix (&exec_prefixes, standard_exec_prefix_1, "BINUTILS",
-             PREFIX_PRIORITY_LAST, 2, 0);
-  add_prefix (&exec_prefixes, standard_exec_prefix_2, "BINUTILS",
-             PREFIX_PRIORITY_LAST, 2, 0);
+      add_prefix (&exec_prefixes, standard_libexec_prefix, "GCC",
+                 PREFIX_PRIORITY_LAST, 1, 0);
+      add_prefix (&exec_prefixes, standard_libexec_prefix, "BINUTILS",
+                 PREFIX_PRIORITY_LAST, 2, 0);
+      add_prefix (&exec_prefixes, standard_exec_prefix, "BINUTILS",
+                 PREFIX_PRIORITY_LAST, 2, 0);
 #endif
+      add_prefix (&startfile_prefixes, standard_exec_prefix, "BINUTILS",
+                 PREFIX_PRIORITY_LAST, 1, 0);
+    }
 
-  add_prefix (&startfile_prefixes, standard_exec_prefix, "BINUTILS",
-             PREFIX_PRIORITY_LAST, 1, 0);
-  add_prefix (&startfile_prefixes, standard_exec_prefix_2, "BINUTILS",
-             PREFIX_PRIORITY_LAST, 1, 0);
+  /* If not cross-compiling, search well-known system locations.  */
+  if (*cross_compile == '0')
+    {
+#ifndef OS2
+      add_prefix (&exec_prefixes, standard_exec_prefix_1, "BINUTILS",
+                 PREFIX_PRIORITY_LAST, 2, 0);
+      add_prefix (&exec_prefixes, standard_exec_prefix_2, "BINUTILS",
+                 PREFIX_PRIORITY_LAST, 2, 0);
+#endif
+      add_prefix (&startfile_prefixes, standard_exec_prefix_2, "BINUTILS",
+                 PREFIX_PRIORITY_LAST, 1, 0);
+    }
 
+  gcc_assert (!IS_ABSOLUTE_PATH (tooldir_base_prefix));
   tooldir_prefix = concat (tooldir_base_prefix, spec_machine,
                           dir_separator_str, NULL);
 
-  /* If tooldir is relative, base it on exec_prefixes.  A relative
-     tooldir lets us move the installed tree as a unit.
-
-     If GCC_EXEC_PREFIX is defined, then we want to add two relative
-     directories, so that we can search both the user specified directory
-     and the standard place.  */
-
-  if (!IS_ABSOLUTE_PATH (tooldir_prefix))
-    {
-      if (gcc_exec_prefix)
-       {
-         char *gcc_exec_tooldir_prefix
-           = concat (gcc_exec_prefix, spec_machine, dir_separator_str,
-                     spec_version, dir_separator_str, tooldir_prefix, NULL);
-
-         add_prefix (&exec_prefixes,
-                     concat (gcc_exec_tooldir_prefix, "bin",
-                             dir_separator_str, NULL),
-                     NULL, PREFIX_PRIORITY_LAST, 0, 0);
-         add_prefix (&startfile_prefixes,
-                     concat (gcc_exec_tooldir_prefix, "lib",
-                             dir_separator_str, NULL),
-                     NULL, PREFIX_PRIORITY_LAST, 0, 1);
-       }
-
-      tooldir_prefix = concat (standard_exec_prefix, spec_machine,
-                              dir_separator_str, spec_version,
-                              dir_separator_str, tooldir_prefix, NULL);
-    }
+  /* Look for tools relative to the location from which the driver is
+     running, or, if that is not available, the configured prefix.  */
+  tooldir_prefix
+    = concat (gcc_exec_prefix ? gcc_exec_prefix : standard_exec_prefix,
+             spec_machine, dir_separator_str,
+             spec_version, dir_separator_str, tooldir_prefix, NULL);
 
   add_prefix (&exec_prefixes,
              concat (tooldir_prefix, "bin", dir_separator_str, NULL),
@@ -3887,8 +4057,8 @@ warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n"
 
   /* Then create the space for the vectors and scan again.  */
 
-  switches = xmalloc ((n_switches + 1) * sizeof (struct switchstr));
-  infiles = xmalloc ((n_infiles + 1) * sizeof (struct infile));
+  switches = XNEWVEC (struct switchstr, n_switches + 1);
+  infiles = XNEWVEC (struct infile, n_infiles + 1);
   n_switches = 0;
   n_infiles = 0;
   last_language_n_infiles = -1;
@@ -3931,9 +4101,7 @@ warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n"
        ;
       else if (! strcmp (argv[i], "-print-multi-os-directory"))
        ;
-      else if (! strcmp (argv[i], "-ftarget-help"))
-       ;
-      else if (! strcmp (argv[i], "-fhelp"))
+      else if (! strcmp (argv[i], "-print-sysroot-headers-suffix"))
        ;
       else if (! strncmp (argv[i], "--sysroot=", strlen ("--sysroot=")))
        {
@@ -3949,7 +4117,7 @@ warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n"
             -e0 or -e1 down into the linker.  */
          switches[n_switches].part1 = &argv[i][0];
          switches[n_switches].args = 0;
-         switches[n_switches].live_cond = SWITCH_OK;
+         switches[n_switches].live_cond = 0;
          switches[n_switches].validated = 0;
          n_switches++;
        }
@@ -4038,7 +4206,7 @@ warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n"
              if (i + n_args >= argc)
                fatal ("argument to '-%s' is missing", p);
              switches[n_switches].args
-               = xmalloc ((n_args + 1) * sizeof(const char *));
+               = XNEWVEC (const char *, n_args + 1);
              while (j < n_args)
                switches[n_switches].args[j++] = argv[++i];
              /* Null-terminate the vector.  */
@@ -4048,26 +4216,28 @@ warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n"
            {
              /* On some systems, ld cannot handle some options without
                 a space.  So split the option from its argument.  */
-             char *part1 = xmalloc (2);
+             char *part1 = XNEWVEC (char, 2);
              part1[0] = c;
              part1[1] = '\0';
 
              switches[n_switches].part1 = part1;
-             switches[n_switches].args = xmalloc (2 * sizeof (const char *));
+             switches[n_switches].args = XNEWVEC (const char *, 2);
              switches[n_switches].args[0] = xstrdup (p+1);
              switches[n_switches].args[1] = 0;
            }
          else
            switches[n_switches].args = 0;
 
-         switches[n_switches].live_cond = SWITCH_OK;
+         switches[n_switches].live_cond = 0;
          switches[n_switches].validated = 0;
          switches[n_switches].ordering = 0;
-         /* These are always valid, since gcc.c itself understands them.  */
+         /* These are always valid, since gcc.c itself understands the
+            first four and gfortranspec.c understands -static-libgfortran.  */
          if (!strcmp (p, "save-temps")
              || !strcmp (p, "static-libgcc")
              || !strcmp (p, "shared-libgcc")
-             || !strcmp (p, "pipe"))
+             || !strcmp (p, "pipe")
+             || !strcmp (p, "static-libgfortran"))
            switches[n_switches].validated = 1;
          else
            {
@@ -4100,34 +4270,14 @@ warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n"
     error ("warning: '-x %s' after last input file has no effect", spec_lang);
 
   /* Ensure we only invoke each subprocess once.  */
-  if (target_help_flag || print_help_list)
+  if (print_subprocess_help || print_help_list)
     {
       n_infiles = 1;
 
-      /* Create a dummy input file, so that we can pass --target-help on to
-        the various sub-processes.  */
+      /* Create a dummy input file, so that we can pass
+        the help option on to the various sub-processes.  */
       infiles[0].language = "c";
       infiles[0].name   = "help-dummy";
-
-      if (target_help_flag)
-       {
-         switches[n_switches].part1     = "--target-help";
-         switches[n_switches].args      = 0;
-         switches[n_switches].live_cond = SWITCH_OK;
-         switches[n_switches].validated = 0;
-
-         n_switches++;
-       }
-
-      if (print_help_list)
-       {
-         switches[n_switches].part1     = "--help";
-         switches[n_switches].args      = 0;
-         switches[n_switches].live_cond = SWITCH_OK;
-         switches[n_switches].validated = 0;
-
-         n_switches++;
-       }
     }
 
   switches[n_switches].part1 = 0;
@@ -4159,7 +4309,7 @@ set_collect_gcc_options (void)
       first_time = FALSE;
 
       /* Ignore elided switches.  */
-      if (switches[i].live_cond == SWITCH_IGNORE)
+      if ((switches[i].live_cond & SWITCH_IGNORE) != 0)
        continue;
 
       obstack_grow (&collect_obstack, "'-", 2);
@@ -4188,7 +4338,7 @@ set_collect_gcc_options (void)
        }
     }
   obstack_grow (&collect_obstack, "\0", 1);
-  putenv (XOBFINISH (&collect_obstack, char *));
+  xputenv (XOBFINISH (&collect_obstack, char *));
 }
 \f
 /* Process a spec string, accumulating and running commands.  */
@@ -4241,6 +4391,26 @@ static int input_from_pipe;
    arguments.  */
 static const char *suffix_subst;
 
+/* If there is an argument being accumulated, terminate it and store it.  */
+
+static void
+end_going_arg (void)
+{
+  if (arg_going)
+    {
+      const char *string;
+
+      obstack_1grow (&obstack, 0);
+      string = XOBFINISH (&obstack, const char *);
+      if (this_is_library_file)
+       string = find_file (string);
+      store_arg (string, delete_this_arg, this_is_output_file);
+      if (this_is_output_file)
+       outfiles[input_file_number] = string;
+      arg_going = 0;
+    }
+}
+
 /* Process the spec SPEC and run the commands specified therein.
    Returns 0 if the spec is successfully processed; -1 if failed.  */
 
@@ -4270,7 +4440,6 @@ do_spec (const char *spec)
 static int
 do_spec_2 (const char *spec)
 {
-  const char *string;
   int result;
 
   clear_args ();
@@ -4283,18 +4452,7 @@ do_spec_2 (const char *spec)
 
   result = do_spec_1 (spec, 0, NULL);
 
-  /* End any pending argument.  */
-  if (arg_going)
-    {
-      obstack_1grow (&obstack, 0);
-      string = XOBFINISH (&obstack, const char *);
-      if (this_is_library_file)
-       string = find_file (string);
-      store_arg (string, delete_this_arg, this_is_output_file);
-      if (this_is_output_file)
-       outfiles[input_file_number] = string;
-      arg_going = 0;
-    }
+  end_going_arg ();
 
   return result;
 }
@@ -4379,115 +4537,62 @@ do_self_spec (const char *spec)
          sw = &switches[i + first];
          sw->part1 = &argbuf[i][1];
          sw->args = 0;
-         sw->live_cond = SWITCH_OK;
+         sw->live_cond = 0;
          sw->validated = 0;
          sw->ordering = 0;
        }
     }
 }
 
-void
-do_spec_path (struct prefix_list *pl, const char *option,
-             int omit_if_relative, int separate_options,
-             int only_subdir,
-             const char *dir_for_machine_suffix,
-             const char *dir_for_no_suffix)
+/* Callback for processing %D and %I specs.  */
+
+struct spec_path_info {
+  const char *option;
+  const char *append;
+  size_t append_len;
+  bool omit_relative;
+  bool separate_options;
+};
+
+static void *
+spec_path (char *path, void *data)
 {
-  static size_t bufsize = 0;
-  static char *buffer;
-  int idx;
-  bool multilib_p = false;
-
-  /* Used on systems which record the specified -L dirs
-     and use them to search for dynamic linking.  */
-  /* Relative directories always come from -B,
-     and it is better not to use them for searching
-     at run time.  In particular, stage1 loses.  */
-  if (omit_if_relative
-      && !IS_ABSOLUTE_PATH (pl->prefix))
-    return;
+  struct spec_path_info *info = data;
+  size_t len = 0;
+  char save = 0;
 
-  /* Try subdirectory if there is one.  */
-  if (machine_suffix && dir_for_machine_suffix)
-    {
-      if (strlen (pl->prefix) + strlen (machine_suffix)
-         >= bufsize)
-       bufsize = (strlen (pl->prefix)
-                 + strlen (machine_suffix)) * 2 + 1;
-      buffer = xrealloc (buffer, bufsize);
-      strcpy (buffer, pl->prefix);
-      strcat (buffer, machine_suffix);
-      if (is_directory (buffer, dir_for_machine_suffix, 1))
-       {
-         multilib_p = true;
-         do_spec_1 (option, separate_options, NULL);
-         if (separate_options)
-           do_spec_1 (" ", 0, NULL);
-         do_spec_1 (buffer, 1, NULL);
-         do_spec_1 (dir_for_machine_suffix, 1, NULL);
-         /* Make this a separate argument.  */
-         do_spec_1 (" ", 0, NULL);
-       }
-    }
-  if (!pl->require_machine_suffix && dir_for_no_suffix)
+  if (info->omit_relative && !IS_ABSOLUTE_PATH (path))
+    return NULL;
+
+  if (info->append_len != 0)
     {
-      if (is_directory (pl->prefix, dir_for_no_suffix, 1))
-       {
-         multilib_p = true;
-         do_spec_1 (option, separate_options, NULL);
-         if (separate_options)
-           do_spec_1 (" ", 0, NULL);
-         do_spec_1 (pl->prefix, 1, NULL);
-         do_spec_1 (dir_for_no_suffix, 1, NULL);
-         /* Make this a separate argument.  */
-         do_spec_1 (" ", 0, NULL);
-       }
+      len = strlen (path);
+      memcpy (path + len, info->append, info->append_len + 1);
     }
 
-  if (only_subdir || multilib_p)
-    return;
+  if (!is_directory (path, true))
+    return NULL;
 
-  if (machine_suffix)
-    {
-      if (is_directory (pl->prefix, machine_suffix, 1))
-       {
-         do_spec_1 (option, separate_options, NULL);
-         if (separate_options)
-           do_spec_1 (" ", 0, NULL);
-         do_spec_1 (pl->prefix, 1, NULL);
-         /* Remove slash from machine_suffix.  */
-         if (strlen (machine_suffix) >= bufsize)
-           bufsize = strlen (machine_suffix) * 2 + 1;
-         buffer = xrealloc (buffer, bufsize);
-         strcpy (buffer, machine_suffix);
-         idx = strlen (buffer);
-         if (IS_DIR_SEPARATOR (buffer[idx - 1]))
-           buffer[idx - 1] = 0;
-         do_spec_1 (buffer, 1, NULL);
-         /* Make this a separate argument.  */
-         do_spec_1 (" ", 0, NULL);
-       }
-    }
-  if (!pl->require_machine_suffix)
+  do_spec_1 (info->option, 1, NULL);
+  if (info->separate_options)
+    do_spec_1 (" ", 0, NULL);
+
+  if (info->append_len == 0)
     {
-      if (is_directory (pl->prefix, "", 1))
-       {
-         do_spec_1 (option, separate_options, NULL);
-         if (separate_options)
-           do_spec_1 (" ", 0, NULL);
-         /* Remove slash from pl->prefix.  */
-         if (strlen (pl->prefix) >= bufsize)
-           bufsize = strlen (pl->prefix) * 2 + 1;
-         buffer = xrealloc (buffer, bufsize);
-         strcpy (buffer, pl->prefix);
-         idx = strlen (buffer);
-         if (IS_DIR_SEPARATOR (buffer[idx - 1]))
-           buffer[idx - 1] = 0;
-         do_spec_1 (buffer, 1, NULL);
-         /* Make this a separate argument.  */
-         do_spec_1 (" ", 0, NULL);
-       }
+      len = strlen (path);
+      save = path[len - 1];
+      if (IS_DIR_SEPARATOR (path[len - 1]))
+       path[len - 1] = '\0';
     }
+
+  do_spec_1 (path, 1, NULL);
+  do_spec_1 (" ", 0, NULL);
+
+  /* Must not damage the original path.  */
+  if (info->append_len == 0)
+    path[len - 1] = save;
+
+  return NULL;
 }
 
 /* Process the sub-spec SPEC as a portion of a larger spec.
@@ -4508,7 +4613,6 @@ do_spec_1 (const char *spec, int inswitch, const char *soft_matched_part)
   const char *p = spec;
   int c;
   int i;
-  const char *string;
   int value;
 
   while ((c = *p++))
@@ -4517,19 +4621,7 @@ do_spec_1 (const char *spec, int inswitch, const char *soft_matched_part)
     switch (inswitch ? 'a' : c)
       {
       case '\n':
-       /* End of line: finish any pending argument,
-          then run the pending command if one has been started.  */
-       if (arg_going)
-         {
-           obstack_1grow (&obstack, 0);
-           string = XOBFINISH (&obstack, const char *);
-           if (this_is_library_file)
-             string = find_file (string);
-           store_arg (string, delete_this_arg, this_is_output_file);
-           if (this_is_output_file)
-             outfiles[input_file_number] = string;
-         }
-       arg_going = 0;
+       end_going_arg ();
 
        if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
          {
@@ -4563,17 +4655,7 @@ do_spec_1 (const char *spec, int inswitch, const char *soft_matched_part)
        break;
 
       case '|':
-       /* End any pending argument.  */
-       if (arg_going)
-         {
-           obstack_1grow (&obstack, 0);
-           string = XOBFINISH (&obstack, const char *);
-           if (this_is_library_file)
-             string = find_file (string);
-           store_arg (string, delete_this_arg, this_is_output_file);
-           if (this_is_output_file)
-             outfiles[input_file_number] = string;
-         }
+       end_going_arg ();
 
        /* Use pipe */
        obstack_1grow (&obstack, c);
@@ -4582,19 +4664,9 @@ do_spec_1 (const char *spec, int inswitch, const char *soft_matched_part)
 
       case '\t':
       case ' ':
-       /* Space or tab ends an argument if one is pending.  */
-       if (arg_going)
-         {
-           obstack_1grow (&obstack, 0);
-           string = XOBFINISH (&obstack, const char *);
-           if (this_is_library_file)
-             string = find_file (string);
-           store_arg (string, delete_this_arg, this_is_output_file);
-           if (this_is_output_file)
-             outfiles[input_file_number] = string;
-         }
+       end_going_arg ();
+
        /* Reinitialize for a new argument.  */
-       arg_going = 0;
        delete_this_arg = 0;
        this_is_output_file = 0;
        this_is_library_file = 0;
@@ -4625,23 +4697,23 @@ do_spec_1 (const char *spec, int inswitch, const char *soft_matched_part)
             that we search for startfiles.  */
          case 'D':
            {
-             struct prefix_list *pl = startfile_prefixes.plist;
+             struct spec_path_info info;
 
-             for (; pl; pl = pl->next)
-               {
-                 const char *no_suffix_multilib_dir;
-
-                 no_suffix_multilib_dir = pl->os_multilib ? multilib_os_dir
-                                          : multilib_dir;
-                 /* Do not separate options, include non-multilibbed variant.  */
-                 do_spec_path (pl, "-L",
+             info.option = "-L";
+             info.append_len = 0;
 #ifdef RELATIVE_PREFIX_NOT_LINKDIR
-                               1,
+             /* Used on systems which record the specified -L dirs
+                and use them to search for dynamic linking.
+                Relative directories always come from -B,
+                and it is better not to use them for searching
+                at run time.  In particular, stage1 loses.  */
+             info.omit_relative = true;
 #else
-                               0,
+             info.omit_relative = false;
 #endif
-                               0, 0, multilib_dir, no_suffix_multilib_dir);
-               }
+             info.separate_options = false;
+
+             for_each_path (&startfile_prefixes, true, 0, spec_path, &info);
            }
            break;
 
@@ -4749,7 +4821,7 @@ do_spec_1 (const char *spec, int inswitch, const char *soft_matched_part)
                    else
                      {
                        saved_suffix
-                         = xmalloc (suffix_length
+                         = XNEWVEC (char, suffix_length
                                     + strlen (TARGET_OBJECT_SUFFIX));
                        strncpy (saved_suffix, suffix, suffix_length);
                        strcpy (saved_suffix + suffix_length,
@@ -4768,12 +4840,14 @@ do_spec_1 (const char *spec, int inswitch, const char *soft_matched_part)
 
                if (save_temps_flag)
                  {
+                   char *tmp;
+                   
                    temp_filename_length = basename_length + suffix_length;
-                   temp_filename = alloca (temp_filename_length + 1);
-                   strncpy ((char *) temp_filename, input_basename, basename_length);
-                   strncpy ((char *) temp_filename + basename_length, suffix,
-                            suffix_length);
-                   *((char *) temp_filename + temp_filename_length) = '\0';
+                   tmp = alloca (temp_filename_length + 1);
+                   strncpy (tmp, input_basename, basename_length);
+                   strncpy (tmp + basename_length, suffix, suffix_length);
+                   tmp[temp_filename_length] = '\0';
+                   temp_filename = tmp;
                    if (strcmp (temp_filename, input_filename) != 0)
                      {
 #ifndef HOST_LACKS_INODE_NUMBERS
@@ -4878,7 +4952,16 @@ do_spec_1 (const char *spec, int inswitch, const char *soft_matched_part)
 
          case 'I':
            {
-             struct prefix_list *pl = include_prefixes.plist;
+             struct spec_path_info info;
+
+             if (multilib_dir)
+               {
+                 do_spec_1 ("-imultilib", 1, NULL);
+                 /* Make this a separate argument.  */
+                 do_spec_1 (" ", 0, NULL);
+                 do_spec_1 (multilib_dir, 1, NULL);
+                 do_spec_1 (" ", 0, NULL);
+               }
 
              if (gcc_exec_prefix)
                {
@@ -4901,9 +4984,22 @@ do_spec_1 (const char *spec, int inswitch, const char *soft_matched_part)
                  do_spec_1 (" ", 0, NULL);
                }
 
-             for (; pl; pl = pl->next)
-               /* Separate options, don't include non-suffixed variant.  */
-               do_spec_path (pl, "-isystem", 0, 1, 1, "include", "include");
+             info.option = "-isystem";
+             info.append = "include";
+             info.append_len = strlen (info.append);
+             info.omit_relative = false;
+             info.separate_options = true;
+
+             for_each_path (&include_prefixes, false, info.append_len,
+                            spec_path, &info);
+
+             info.append = "include-fixed";
+             if (*sysroot_hdrs_suffix_spec)
+               info.append = concat (info.append, dir_separator_str,
+                                     multilib_dir, NULL);
+             info.append_len = strlen (info.append);
+             for_each_path (&include_prefixes, false, info.append_len,
+                            spec_path, &info);
            }
            break;
 
@@ -4912,9 +5008,63 @@ do_spec_1 (const char *spec, int inswitch, const char *soft_matched_part)
              int max = n_infiles;
              max += lang_specific_extra_outfiles;
 
-             for (i = 0; i < max; i++)
-               if (outfiles[i])
-                 store_arg (outfiles[i], 0, 0);
+              if (HAVE_GNU_LD && at_file_supplied)
+                {
+                  /* We are going to expand `%o' to `@FILE', where FILE
+                     is a newly-created temporary filename.  The filenames
+                     that would usually be expanded in place of %o will be
+                     written to the temporary file.  */
+
+                  char *temp_file = make_temp_file ("");
+                  char *at_argument;
+                  char **argv;
+                  int n_files, j, status;
+                  FILE *f;
+
+                  at_argument = concat ("@", temp_file, NULL);
+                  store_arg (at_argument, 0, 0);
+
+                  /* Convert OUTFILES into a form suitable for writeargv.  */
+
+                  /* Determine how many are non-NULL.  */
+                  for (n_files = 0, i = 0; i < max; i++)
+                    n_files += outfiles[i] != NULL;
+
+                  argv = alloca (sizeof (char *) * (n_files + 1));
+
+                  /* Copy the strings over.  */
+                  for (i = 0, j = 0; i < max; i++)
+                    if (outfiles[i])
+                      {
+                        argv[j] = CONST_CAST (char *, outfiles[i]);
+                        j++;
+                      }
+                  argv[j] = NULL;
+
+                  f = fopen (temp_file, "w");
+
+                  if (f == NULL)
+                    fatal ("could not open temporary response file %s",
+                           temp_file);
+
+                  status = writeargv (argv, f);
+
+                  if (status)
+                    fatal ("could not write to temporary response file %s",
+                           temp_file);
+
+                  status = fclose (f);
+
+                  if (EOF == status)
+                    fatal ("could not close temporary response file %s",
+                           temp_file);
+
+                  record_temp_file (temp_file, !save_temps_flag, !save_temps_flag);
+                }
+              else
+                for (i = 0; i < max; i++)
+                 if (outfiles[i])
+                   store_arg (outfiles[i], 0, 0);
              break;
            }
 
@@ -4944,18 +5094,7 @@ do_spec_1 (const char *spec, int inswitch, const char *soft_matched_part)
              p = handle_braces (p + 1);
              if (p == 0)
                return -1;
-             /* End any pending argument.  */
-             if (arg_going)
-               {
-                 obstack_1grow (&obstack, 0);
-                 string = XOBFINISH (&obstack, const char *);
-                 if (this_is_library_file)
-                   string = find_file (string);
-                 store_arg (string, delete_this_arg, this_is_output_file);
-                 if (this_is_output_file)
-                   outfiles[input_file_number] = string;
-                 arg_going = 0;
-               }
+             end_going_arg ();
              /* If any args were output, mark the last one for deletion
                 on failure.  */
              if (argbuf_index != cur_index)
@@ -5148,7 +5287,7 @@ do_spec_1 (const char *spec, int inswitch, const char *soft_matched_part)
                if (!strncmp (switches[i].part1, p, len - have_wildcard)
                    && (have_wildcard || switches[i].part1[len] == '\0'))
                  {
-                   switches[i].live_cond = SWITCH_IGNORE;
+                   switches[i].live_cond |= SWITCH_IGNORE;
                    switches[i].validated = 1;
                  }
 
@@ -5274,17 +5413,8 @@ do_spec_1 (const char *spec, int inswitch, const char *soft_matched_part)
 
   /* End of string.  If we are processing a spec function, we need to
      end any pending argument.  */
-  if (processing_spec_function && arg_going)
-    {
-      obstack_1grow (&obstack, 0);
-      string = XOBFINISH (&obstack, const char *);
-      if (this_is_library_file)
-        string = find_file (string);
-      store_arg (string, delete_this_arg, this_is_output_file);
-      if (this_is_output_file)
-        outfiles[input_file_number] = string;
-      arg_going = 0;
-    }
+  if (processing_spec_function)
+    end_going_arg ();
 
   return 0;
 }
@@ -5294,20 +5424,11 @@ do_spec_1 (const char *spec, int inswitch, const char *soft_matched_part)
 static const struct spec_function *
 lookup_spec_function (const char *name)
 {
-  static const struct spec_function * const spec_function_tables[] =
-  {
-    static_spec_functions,
-    lang_specific_spec_functions,
-  };
   const struct spec_function *sf;
-  unsigned int i;
 
-  for (i = 0; i < ARRAY_SIZE (spec_function_tables); i++)
-    {
-      for (sf = spec_function_tables[i]; sf->name != NULL; sf++)
-       if (strcmp (sf->name, name) == 0)
-         return sf;
-    }
+  for (sf = static_spec_functions; sf->name != NULL; sf++)
+    if (strcmp (sf->name, name) == 0)
+      return sf;
 
   return NULL;
 }
@@ -5447,31 +5568,28 @@ handle_spec_function (const char *p)
 static inline bool
 input_suffix_matches (const char *atom, const char *end_atom)
 {
-  /* We special case the semantics of {.s:...} and {.S:...} and their
-     negative variants.  Instead of testing the input filename suffix,
-     we test whether the input source file is an assembler file or an
-     assembler-with-cpp file respectively.  This allows us to correctly
-     handle the -x command line option.  */
-
-  if (atom + 1 == end_atom
-      && input_file_compiler
-      && input_file_compiler->suffix)
-    {
-      if (*atom == 's')
-       return !strcmp (input_file_compiler->suffix, "@assembler");
-      if (*atom == 'S')
-       return !strcmp (input_file_compiler->suffix, "@assembler-with-cpp");
-    }
-
   return (input_suffix
          && !strncmp (input_suffix, atom, end_atom - atom)
          && input_suffix[end_atom - atom] == '\0');
 }
 
-/* Inline subroutine of handle_braces.  Returns true if a switch
+/* Subroutine of handle_braces.  Returns true if the current
+   input file's spec name matches the atom bracketed by ATOM and END_ATOM.  */
+static bool
+input_spec_matches (const char *atom, const char *end_atom)
+{
+  return (input_file_compiler
+         && input_file_compiler->suffix
+         && input_file_compiler->suffix[0] != '\0'
+         && !strncmp (input_file_compiler->suffix + 1, atom,
+                      end_atom - atom)
+         && input_file_compiler->suffix[end_atom - atom + 1] == '\0');
+}
+
+/* Subroutine of handle_braces.  Returns true if a switch
    matching the atom bracketed by ATOM and END_ATOM appeared on the
    command line.  */
-static inline bool
+static bool
 switch_matches (const char *atom, const char *end_atom, int starred)
 {
   int i;
@@ -5531,6 +5649,7 @@ handle_braces (const char *p)
   const char *orig = p;
 
   bool a_is_suffix;
+  bool a_is_spectype;
   bool a_is_starred;
   bool a_is_negated;
   bool a_matched;
@@ -5551,8 +5670,12 @@ handle_braces (const char *p)
        goto invalid;
 
       /* Scan one "atom" (S in the description above of %{}, possibly
-        with !, ., or * modifiers).  */
-      a_matched = a_is_suffix = a_is_starred = a_is_negated = false;
+        with '!', '.', '@', ',', or '*' modifiers).  */
+      a_matched = false;
+      a_is_suffix = false;
+      a_is_starred = false;
+      a_is_negated = false;
+      a_is_spectype = false;
 
       SKIP_WHITE();
       if (*p == '!')
@@ -5561,6 +5684,8 @@ handle_braces (const char *p)
       SKIP_WHITE();
       if (*p == '.')
        p++, a_is_suffix = true;
+      else if (*p == ',')
+       p++, a_is_spectype = true;
 
       atom = p;
       while (ISIDNUM(*p) || *p == '-' || *p == '+' || *p == '='
@@ -5578,7 +5703,7 @@ handle_braces (const char *p)
          /* Substitute the switch(es) indicated by the current atom.  */
          ordered_set = true;
          if (disjunct_set || n_way_choice || a_is_negated || a_is_suffix
-             || atom == end_atom)
+             || a_is_spectype || atom == end_atom)
            goto invalid;
 
          mark_matching_switches (atom, end_atom, a_is_starred);
@@ -5597,7 +5722,8 @@ handle_braces (const char *p)
          if (atom == end_atom)
            {
              if (!n_way_choice || disj_matched || *p == '|'
-                 || a_is_negated || a_is_suffix || a_is_starred)
+                 || a_is_negated || a_is_suffix || a_is_spectype 
+                 || a_is_starred)
                goto invalid;
 
              /* An empty term may appear as the last choice of an
@@ -5608,28 +5734,30 @@ handle_braces (const char *p)
            }
          else
            {
-              if (a_is_suffix && a_is_starred)
-                goto invalid;
-
-              if (!a_is_starred)
-                disj_starred = false;
-
-              /* Don't bother testing this atom if we already have a
-                  match.  */
-              if (!disj_matched && !n_way_matched)
-                {
-                  if (a_is_suffix)
-                    a_matched = input_suffix_matches (atom, end_atom);
-                  else
-                    a_matched = switch_matches (atom, end_atom, a_is_starred);
-
-                  if (a_matched != a_is_negated)
-                    {
-                      disj_matched = true;
-                      d_atom = atom;
-                      d_end_atom = end_atom;
-                    }
-                }
+             if ((a_is_suffix || a_is_spectype) && a_is_starred)
+               goto invalid;
+             
+             if (!a_is_starred)
+               disj_starred = false;
+
+             /* Don't bother testing this atom if we already have a
+                match.  */
+             if (!disj_matched && !n_way_matched)
+               {
+                 if (a_is_suffix)
+                   a_matched = input_suffix_matches (atom, end_atom);
+                 else if (a_is_spectype)
+                   a_matched = input_spec_matches (atom, end_atom);
+                 else
+                   a_matched = switch_matches (atom, end_atom, a_is_starred);
+                 
+                 if (a_matched != a_is_negated)
+                   {
+                     disj_matched = true;
+                     d_atom = atom;
+                     d_end_atom = end_atom;
+                   }
+               }
            }
 
          if (*p == ':')
@@ -5777,7 +5905,8 @@ check_live_switch (int switchnum, int prefix_length)
   /* If we already processed this switch and determined if it was
      live or not, return our past determination.  */
   if (switches[switchnum].live_cond != 0)
-    return switches[switchnum].live_cond > 0;
+    return ((switches[switchnum].live_cond & SWITCH_LIVE) != 0
+           && (switches[switchnum].live_cond & SWITCH_FALSE) == 0);
 
   /* Now search for duplicate in a manner that depends on the name.  */
   switch (*name)
@@ -5824,7 +5953,7 @@ check_live_switch (int switchnum, int prefix_length)
     }
 
   /* Otherwise the switch is live.  */
-  switches[switchnum].live_cond = SWITCH_LIVE;
+  switches[switchnum].live_cond |= SWITCH_LIVE;
   return 1;
 }
 \f
@@ -5839,7 +5968,7 @@ check_live_switch (int switchnum, int prefix_length)
 static void
 give_switch (int switchnum, int omit_first_word)
 {
-  if (switches[switchnum].live_cond == SWITCH_IGNORE)
+  if ((switches[switchnum].live_cond & SWITCH_IGNORE) != 0)
     return;
 
   if (!omit_first_word)
@@ -5864,13 +5993,13 @@ give_switch (int switchnum, int omit_first_word)
              while (length-- && !IS_DIR_SEPARATOR (arg[length]))
                if (arg[length] == '.')
                  {
-                   ((char *)arg)[length] = 0;
+                   (CONST_CAST(char *, arg))[length] = 0;
                    dot = 1;
                    break;
                  }
              do_spec_1 (arg, 1, NULL);
              if (dot)
-               ((char *)arg)[length] = '.';
+               (CONST_CAST(char *, arg))[length] = '.';
              do_spec_1 (suffix_subst, 1, NULL);
            }
          else
@@ -5889,48 +6018,27 @@ give_switch (int switchnum, int omit_first_word)
 static const char *
 find_file (const char *name)
 {
-  char *newname;
-
-  /* Try multilib_dir if it is defined.  */
-  if (multilib_os_dir != NULL)
-    {
-      newname = find_a_file (&startfile_prefixes, name, R_OK, 1);
-
-      /* If we don't find it in the multi library dir, then fall
-        through and look for it in the normal places.  */
-      if (newname != NULL)
-       return newname;
-    }
-
-  newname = find_a_file (&startfile_prefixes, name, R_OK, 0);
+  char *newname = find_a_file (&startfile_prefixes, name, R_OK, true);
   return newname ? newname : name;
 }
 
 /* Determine whether a directory exists.  If LINKER, return 0 for
-   certain fixed names not needed by the linker.  If not LINKER, it is
-   only important to return 0 if the host machine has a small ARG_MAX
-   limit.  */
+   certain fixed names not needed by the linker.  */
 
 static int
-is_directory (const char *path1, const char *path2, int linker)
+is_directory (const char *path1, bool linker)
 {
-  int len1 = strlen (path1);
-  int len2 = strlen (path2);
-  char *path = alloca (3 + len1 + len2);
+  int len1;
+  char *path;
   char *cp;
   struct stat st;
 
-#ifndef SMALL_ARG_MAX
-  if (! linker)
-    return 1;
-#endif
-
-  /* Construct the path from the two parts.  Ensure the string ends with "/.".
-     The resulting path will be a directory even if the given path is a
-     symbolic link.  */
+  /* Ensure the string ends with "/.".  The resulting path will be a
+     directory even if the given path is a symbolic link.  */
+  len1 = strlen (path1);
+  path = alloca (3 + len1);
   memcpy (path, path1, len1);
-  memcpy (path + len1, path2, len2);
-  cp = path + len1 + len2;
+  cp = path + len1;
   if (!IS_DIR_SEPARATOR (cp[-1]))
     *cp++ = DIR_SEPARATOR;
   *cp++ = '.';
@@ -5938,13 +6046,13 @@ is_directory (const char *path1, const char *path2, int linker)
 
   /* Exclude directories that the linker is known to search.  */
   if (linker
+      && IS_DIR_SEPARATOR (path[0])
       && ((cp - path == 6
-          && strcmp (path, concat (dir_separator_str, "lib",
-                                   dir_separator_str, ".", NULL)) == 0)
+          && strncmp (path + 1, "lib", 3) == 0)
          || (cp - path == 10
-             && strcmp (path, concat (dir_separator_str, "usr",
-                                      dir_separator_str, "lib",
-                                      dir_separator_str, ".", NULL)) == 0)))
+             && strncmp (path + 1, "usr", 3) == 0
+             && IS_DIR_SEPARATOR (path[4])
+             && strncmp (path + 5, "lib", 3) == 0)))
     return 0;
 
   return (stat (path, &st) >= 0 && S_ISDIR (st.st_mode));
@@ -6005,10 +6113,10 @@ fatal_error (int signum)
   kill (getpid (), signum);
 }
 
-extern int main (int, const char **);
+extern int main (int, char **);
 
 int
-main (int argc, const char **argv)
+main (int argc, char **argv)
 {
   size_t i;
   int value;
@@ -6019,6 +6127,11 @@ main (int argc, const char **argv)
   char *specs_file;
   const char *p;
   struct user_specs *uptr;
+  char **old_argv = argv;
+
+  /* Initialize here, not in definition.  The IRIX 6 O32 cc sometimes chokes
+     on ?: in file-scope variable initializations.  */
+  asm_debug = ASM_DEBUG_SPEC;
 
   p = argv[0] + strlen (argv[0]);
   while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
@@ -6027,6 +6140,14 @@ main (int argc, const char **argv)
 
   xmalloc_set_program_name (programname);
 
+  expandargv (&argc, &argv);
+
+  /* Determine if any expansions were made.  */
+  if (argv != old_argv)
+    at_file_supplied = true;
+
+  prune_options (&argc, &argv);
+
 #ifdef GCC_DRIVER_HOST_INITIALIZATION
   /* Perform host dependent initialization when needed.  */
   GCC_DRIVER_HOST_INITIALIZATION;
@@ -6108,18 +6229,18 @@ main (int argc, const char **argv)
   obstack_init (&collect_obstack);
   obstack_grow (&collect_obstack, "COLLECT_GCC=", sizeof ("COLLECT_GCC=") - 1);
   obstack_grow (&collect_obstack, argv[0], strlen (argv[0]) + 1);
-  putenv (XOBFINISH (&collect_obstack, char *));
+  xputenv (XOBFINISH (&collect_obstack, char *));
 
 #ifdef INIT_ENVIRONMENT
   /* Set up any other necessary machine specific environment variables.  */
-  putenv (INIT_ENVIRONMENT);
+  xputenv (INIT_ENVIRONMENT);
 #endif
 
   /* Make a table of what switches there are (switches, n_switches).
      Make a table of specified input files (infiles, n_infiles).
      Decode switches that are handled locally.  */
 
-  process_command (argc, argv);
+  process_command (argc, (const char **) argv);
 
   /* Initialize the vector of specs to just the default.
      This means one element containing 0s, as a terminator.  */
@@ -6134,7 +6255,7 @@ main (int argc, const char **argv)
                           spec_version, dir_separator_str, NULL);
   just_machine_suffix = concat (spec_machine, dir_separator_str, NULL);
 
-  specs_file = find_a_file (&startfile_prefixes, "specs", R_OK, 0);
+  specs_file = find_a_file (&startfile_prefixes, "specs", R_OK, true);
   /* Read the specs file unless it is a default one.  */
   if (specs_file != 0 && strcmp (specs_file, "specs"))
     read_specs (specs_file, TRUE);
@@ -6243,18 +6364,16 @@ main (int argc, const char **argv)
                              PREFIX_PRIORITY_LAST, 0, 1);
       else if (*cross_compile == '0')
        {
-         if (gcc_exec_prefix)
-           add_prefix (&startfile_prefixes,
-                       concat (gcc_exec_prefix, machine_suffix,
-                               standard_startfile_prefix, NULL),
-                       NULL, PREFIX_PRIORITY_LAST, 0, 1);
          add_prefix (&startfile_prefixes,
-                     concat (standard_exec_prefix,
-                             machine_suffix,
+                     concat (gcc_exec_prefix 
+                             ? gcc_exec_prefix : standard_exec_prefix, 
+                             machine_suffix, 
                              standard_startfile_prefix, NULL),
                      NULL, PREFIX_PRIORITY_LAST, 0, 1);
        }
 
+      /* Sysrooted prefixes are relocated because target_system_root is
+        also relocated by gcc_exec_prefix.  */
       if (*standard_startfile_prefix_1)
        add_sysrooted_prefix (&startfile_prefixes,
                              standard_startfile_prefix_1, "BINUTILS",
@@ -6270,7 +6389,7 @@ main (int argc, const char **argv)
   for (uptr = user_specs_head; uptr; uptr = uptr->next)
     {
       char *filename = find_a_file (&startfile_prefixes, uptr->filename,
-                                   R_OK, 0);
+                                   R_OK, true);
       read_specs (filename ? filename : uptr->filename, FALSE);
     }
 
@@ -6298,9 +6417,13 @@ main (int argc, const char **argv)
 
   if (print_search_dirs)
     {
-      printf (_("install: %s%s\n"), standard_exec_prefix, machine_suffix);
-      printf (_("programs: %s\n"), build_search_list (&exec_prefixes, "", 0));
-      printf (_("libraries: %s\n"), build_search_list (&startfile_prefixes, "", 0));
+      printf (_("install: %s%s\n"),
+             gcc_exec_prefix ? gcc_exec_prefix : standard_exec_prefix,
+             gcc_exec_prefix ? "" : machine_suffix);
+      printf (_("programs: %s\n"),
+             build_search_list (&exec_prefixes, "", false, false));
+      printf (_("libraries: %s\n"),
+             build_search_list (&startfile_prefixes, "", false, true));
       return (0);
     }
 
@@ -6341,14 +6464,19 @@ main (int argc, const char **argv)
       return (0);
     }
 
-  if (target_help_flag)
-   {
-      /* Print if any target specific options.  */
-
-      /* We do not exit here. Instead we have created a fake input file
-         called 'target-dummy' which needs to be compiled, and we pass this
-         on to the various sub-processes, along with the --target-help
-         switch.  */
+  if (print_sysroot_headers_suffix)
+    {
+      if (*sysroot_hdrs_suffix_spec)
+       {
+         printf("%s\n", (target_sysroot_hdrs_suffix
+                         ? target_sysroot_hdrs_suffix
+                         : ""));
+         return (0);
+       }
+      else
+       /* The error status indicates that only one set of fixed
+          headers should be built.  */
+       fatal ("not configured with sysroot headers suffix");
     }
 
   if (print_help_list)
@@ -6399,10 +6527,10 @@ main (int argc, const char **argv)
 
       if (! strncmp (version_string, compiler_version, n)
          && compiler_version[n] == 0)
-       notice ("gcc version %s\n", version_string);
+       notice ("gcc version %s %s\n", version_string, pkgversion_string);
       else
-       notice ("gcc driver version %s executing gcc version %s\n",
-               version_string, compiler_version);
+       notice ("gcc driver version %s %sexecuting gcc version %s\n",
+               version_string, pkgversion_string, compiler_version);
 
       if (n_infiles == 0)
        return (0);
@@ -6416,11 +6544,11 @@ main (int argc, const char **argv)
 
   i = n_infiles;
   i += lang_specific_extra_outfiles;
-  outfiles = xcalloc (i, sizeof (char *));
+  outfiles = XCNEWVEC (const char *, i);
 
   /* Record which files were specified explicitly as link input.  */
 
-  explicit_link_files = xcalloc (1, n_infiles);
+  explicit_link_files = XCNEWVEC (char, n_infiles);
 
   if (combine_flag)
     combine_inputs = true;
@@ -6457,6 +6585,9 @@ main (int argc, const char **argv)
       infiles[i].preprocessed = false;
     }
 
+  if (!combine_inputs && have_c && have_o && lang_n_infiles > 1)
+   fatal ("cannot specify -o with -c or -S with multiple files");
+
   if (combine_flag && save_temps_flag)
     {
       bool save_combine_inputs = combine_inputs;
@@ -6607,22 +6738,29 @@ main (int argc, const char **argv)
 
   /* Run ld to link all the compiler output files.  */
 
-  if (num_linker_inputs > 0 && error_count == 0)
+  if (num_linker_inputs > 0 && error_count == 0 && print_subprocess_help < 2)
     {
       int tmp = execution_count;
 
       /* We'll use ld if we can't find collect2.  */
       if (! strcmp (linker_name_spec, "collect2"))
        {
-         char *s = find_a_file (&exec_prefixes, "collect2", X_OK, 0);
+         char *s = find_a_file (&exec_prefixes, "collect2", X_OK, false);
          if (s == NULL)
            linker_name_spec = "ld";
        }
       /* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables
         for collect.  */
-      putenv_from_prefixes (&exec_prefixes, "COMPILER_PATH");
-      putenv_from_prefixes (&startfile_prefixes, LIBRARY_PATH_ENV);
+      putenv_from_prefixes (&exec_prefixes, "COMPILER_PATH", false);
+      putenv_from_prefixes (&startfile_prefixes, LIBRARY_PATH_ENV, true);
 
+      if (print_subprocess_help == 1)
+       {
+         printf (_("\nLinker options\n==============\n\n"));
+         printf (_("Use \"-Wl,OPTION\" to pass \"OPTION\""
+                   " to the linker.\n\n"));
+         fflush (stdout);
+       }
       value = do_spec (link_command_spec);
       if (value < 0)
        error_count = 1;
@@ -6634,7 +6772,8 @@ main (int argc, const char **argv)
 
   if (! linker_was_run && error_count == 0)
     for (i = 0; (int) i < n_infiles; i++)
-      if (explicit_link_files[i])
+      if (explicit_link_files[i]
+         && !(infiles[i].language && infiles[i].language[0] == '*'))
        error ("%s: linker input file unused because linking not done",
               outfiles[i]);
 
@@ -6728,7 +6867,7 @@ lookup_compiler (const char *name, size_t length, const char *language)
 static char *
 save_string (const char *s, int len)
 {
-  char *result = xmalloc (len + 1);
+  char *result = XNEWVEC (char, len + 1);
 
   memcpy (result, s, len);
   result[len] = 0;
@@ -6754,12 +6893,27 @@ perror_with_name (const char *name)
 void
 fancy_abort (const char *file, int line, const char *func)
 {
-  fatal ("internal gcc abort in %s, at %s:%d", func, file, line);
+  fatal_ice ("internal gcc abort in %s, at %s:%d", func, file, line);
 }
 \f
 /* Output an error message and exit.  */
 
 void
+fatal_ice (const char *cmsgid, ...)
+{
+  va_list ap;
+
+  va_start (ap, cmsgid);
+
+  fprintf (stderr, "%s: ", programname);
+  vfprintf (stderr, _(cmsgid), ap);
+  va_end (ap);
+  fprintf (stderr, "\n");
+  delete_temp_files ();
+  exit (pass_exit_codes ? ICE_EXIT_CODE : 1);
+}
+
+void
 fatal (const char *cmsgid, ...)
 {
   va_list ap;
@@ -6852,7 +7006,7 @@ next_member:
     p++;
 
   SKIP_WHITE ();
-  if (*p == '.')
+  if (*p == '.' || *p == ',')
     suffix = true, p++;
 
   atom = p;
@@ -6978,10 +7132,9 @@ used_arg (const char *p, int len)
         xmalloc from calling fatal, and prevents us from re-executing this
         block of code.  */
       mswitches
-       = xmalloc (sizeof (struct mswitchstr)
-                  * (n_mdswitches + (n_switches ? n_switches : 1)));
+       = XNEWVEC (struct mswitchstr, n_mdswitches + (n_switches ? n_switches : 1));
       for (i = 0; i < n_switches; i++)
-       if (switches[i].live_cond != SWITCH_IGNORE)
+       if ((switches[i].live_cond & SWITCH_IGNORE) == 0)
          {
            int xlen = strlen (switches[i].part1);
            for (j = 0; j < cnt; j++)
@@ -7106,7 +7259,7 @@ set_multilib_dir (void)
     {
       int i = 0;
 
-      mdswitches = xmalloc (sizeof (struct mdswitchstr) * n_mdswitches);
+      mdswitches = XNEWVEC (struct mdswitchstr, n_mdswitches);
       for (start = multilib_defaults; *start != '\0'; start = end + 1)
        {
          while (*start == ' ' || *start == '\t')
@@ -7268,7 +7421,7 @@ set_multilib_dir (void)
          if (this_path_len != 1
              || this_path[0] != '.')
            {
-             char *new_multilib_dir = xmalloc (this_path_len + 1);
+             char *new_multilib_dir = XNEWVEC (char, this_path_len + 1);
              char *q;
 
              strncpy (new_multilib_dir, this_path, this_path_len);
@@ -7289,7 +7442,7 @@ set_multilib_dir (void)
            q++;
          if (q < end)
            {
-             char *new_multilib_os_dir = xmalloc (end - q);
+             char *new_multilib_os_dir = XNEWVEC (char, end - q);
              memcpy (new_multilib_os_dir, q + 1, end - q - 1);
              new_multilib_os_dir[end - q - 1] = '\0';
              multilib_os_dir = new_multilib_os_dir;
@@ -7303,7 +7456,7 @@ set_multilib_dir (void)
   if (multilib_dir == NULL && multilib_os_dir != NULL
       && strcmp (multilib_os_dir, ".") == 0)
     {
-      free ((char *) multilib_os_dir);
+      free (CONST_CAST (char *, multilib_os_dir));
       multilib_os_dir = NULL;
     }
   else if (multilib_dir != NULL && multilib_os_dir == NULL)
@@ -7570,6 +7723,44 @@ print_multilib_info (void)
     }
 }
 \f
+/* getenv built-in spec function.
+
+   Returns the value of the environment variable given by its first
+   argument, concatenated with the second argument.  If the
+   environment variable is not defined, a fatal error is issued.  */
+
+static const char *
+getenv_spec_function (int argc, const char **argv)
+{
+  char *value;
+  char *result;
+  char *ptr;
+  size_t len;
+
+  if (argc != 2)
+    return NULL;
+
+  value = getenv (argv[0]);
+  if (!value)
+    fatal ("environment variable \"%s\" not defined", argv[0]);
+
+  /* We have to escape every character of the environment variable so
+     they are not interpretted as active spec characters.  A
+     particulaly painful case is when we are reading a variable
+     holding a windows path complete with \ separators.  */
+  len = strlen (value) * 2 + strlen (argv[1]) + 1;
+  result = xmalloc (len);
+  for (ptr = result; *value; ptr += 2)
+    {
+      ptr[0] = '\\';
+      ptr[1] = *value++;
+    }
+  
+  strcpy (ptr, argv[1]);
+  
+  return result;
+}
+
 /* if-exists built-in spec function.
 
    Checks to see if the file specified by the absolute pathname in
@@ -7690,13 +7881,13 @@ version_compare_spec_function (int argc, const char **argv)
   bool result;
 
   if (argc < 3)
-    abort ();
+    fatal ("too few arguments to %%:version-compare");
   if (argv[0][0] == '\0')
     abort ();
   if ((argv[0][1] == '<' || argv[0][1] == '>') && argv[0][0] != '!')
     nargs = 2;
   if (argc != nargs + 3)
-    abort ();
+    fatal ("too many arguments to %%:version-compare");
 
   switch_len = strlen (argv[nargs + 1]);
   for (i = 0; i < n_switches; i++)
@@ -7737,10 +7928,42 @@ version_compare_spec_function (int argc, const char **argv)
       break;
 
     default:
-      abort ();
+      fatal ("unknown operator '%s' in %%:version-compare", argv[0]);
     }
   if (! result)
     return NULL;
 
   return argv[nargs + 2];
 }
+
+/* %:include builtin spec function.  This differs from %include in that it
+   can be nested inside a spec, and thus be conditionalized.  It takes
+   one argument, the filename, and looks for it in the startfile path.
+   The result is always NULL, i.e. an empty expansion.  */
+
+static const char *
+include_spec_function (int argc, const char **argv)
+{
+  char *file;
+
+  if (argc != 1)
+    abort ();
+
+  file = find_a_file (&startfile_prefixes, argv[0], R_OK, 0);
+  read_specs (file ? file : argv[0], FALSE);
+
+  return NULL;
+}
+
+/* %:print-asm-header spec function.  Print a banner to say that the
+   following output is from the assembler.  */
+
+static const char *
+print_asm_header_spec_function (int arg ATTRIBUTE_UNUSED,
+                               const char **argv ATTRIBUTE_UNUSED)
+{
+  printf (_("Assembler options\n=================\n\n"));
+  printf (_("Use \"-Wa,OPTION\" to pass \"OPTION\" to the assembler.\n\n"));
+  fflush (stdout);
+  return NULL;
+}