OSDN Git Service

* g++.old-deja/g++.benjamin/16077.C: Adjust warnings.
[pf3gnuchains/gcc-fork.git] / gcc / collect2.c
index 732ed4b..af97c20 100644 (file)
@@ -1,39 +1,41 @@
 /* Collect static initialization info into data structures that can be
    traversed by C++ initialization and finalization routines.
    Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998,
-   1999, 2000, 2001 Free Software Foundation, Inc.
+   1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
    Contributed by Chris Smith (csmith@convex.com).
    Heavily modified by Michael Meissner (meissner@cygnus.com),
    Per Bothner (bothner@cygnus.com), and John Gilmore (gnu@cygnus.com).
 
-This file is part of GNU CC.
+This file is part of GCC.
 
-GNU CC 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 version.
+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
+version.
 
-GNU CC is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-GNU General Public License for more details.
+GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+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 GNU CC; see the file COPYING.  If not, write to
-the Free Software Foundation, 59 Temple Place - Suite 330,
-Boston, MA 02111-1307, USA.  */
+along with GCC; see the file COPYING.  If not, write to the Free
+Software Foundation, 59 Temple Place - Suite 330, Boston, MA
+02111-1307, USA.  */
 
 
 /* Build tables of static constructors and destructors and run ld.  */
 
 #include "config.h"
 #include "system.h"
+#include "coretypes.h"
+#include "tm.h"
 #include <signal.h>
 #if ! defined( SIGCHLD ) && defined( SIGCLD )
 #  define SIGCHLD SIGCLD
 #endif
 
-#ifdef vfork /* Autoconf may define this to fork for us. */
+#ifdef vfork /* Autoconf may define this to fork for us.  */
 # define VFORK_STRING "fork"
 #else
 # define VFORK_STRING "vfork"
@@ -57,10 +59,6 @@ Boston, MA 02111-1307, USA.  */
 #include "obstack.h"
 #include "intl.h"
 #include "version.h"
-
-/* Obstack allocation and deallocation routines.  */
-#define obstack_chunk_alloc xmalloc
-#define obstack_chunk_free free
 \f
 /* On certain systems, we have code that works by scanning the object file
    directly.  But this code uses system-specific header files and library
@@ -144,16 +142,23 @@ Boston, MA 02111-1307, USA.  */
 
 /* Some systems use __main in a way incompatible with its use in gcc, in these
    cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
-   give the same symbol without quotes for an alternative entry point.  You
-   must define both, or neither.  */
+   give the same symbol without quotes for an alternative entry point.  */
 #ifndef NAME__MAIN
 #define NAME__MAIN "__main"
-#define SYMBOL__MAIN __main
 #endif
 
 /* This must match tree.h.  */
 #define DEFAULT_INIT_PRIORITY 65535
 
+#ifndef COLLECT_SHARED_INIT_FUNC
+#define COLLECT_SHARED_INIT_FUNC(STREAM, FUNC) \
+  fprintf ((STREAM), "void _GLOBAL__DI() {\n\t%s();\n}\n", (FUNC))
+#endif
+#ifndef COLLECT_SHARED_FINI_FUNC
+#define COLLECT_SHARED_FINI_FUNC(STREAM, FUNC) \
+  fprintf ((STREAM), "void _GLOBAL__DD() {\n\t%s();\n}\n", (FUNC))
+#endif
+
 #if defined (LDD_SUFFIX) || SUNOS4_SHARED_LIBRARIES
 #define SCAN_LIBRARIES
 #endif
@@ -163,6 +168,10 @@ int do_collecting = 1;
 #else
 int do_collecting = 0;
 #endif
+
+/* Nonzero if we should suppress the automatic demangling of identifiers
+   in linker error messages.  Set from COLLECT_NO_DEMANGLE.  */
+int no_demangle;
 \f
 /* Linked lists of constructor and destructor names.  */
 
@@ -224,18 +233,10 @@ static struct head exports;               /* list of exported symbols */
 static struct head frame_tables;       /* list of frame unwind info tables */
 
 struct obstack temporary_obstack;
-struct obstack permanent_obstack;
 char * temporary_firstobj;
 
-/* Holds the return value of pexecute.  */
-int pexecute_pid;
-
-/* Defined in the automatically-generated underscore.c.  */
-extern int prepends_underscore;
-
-#ifndef GET_ENV_PATH_LIST
-#define GET_ENV_PATH_LIST(VAR,NAME)    do { (VAR) = getenv (NAME); } while (0)
-#endif
+/* Holds the return value of pexecute and fork.  */
+int pid;
 
 /* Structure to hold all the directories in which to search for files to
    execute.  */
@@ -254,13 +255,13 @@ struct path_prefix
 };
 
 #ifdef COLLECT_EXPORT_LIST
-/* Lists to keep libraries to be scanned for global constructors/destructors. */
+/* Lists to keep libraries to be scanned for global constructors/destructors.  */
 static struct head libs;                    /* list of libraries */
 static struct path_prefix cmdline_lib_dirs; /* directories specified with -L */
 static struct path_prefix libpath_lib_dirs; /* directories in LIBPATH */
 static struct path_prefix *libpaths[3] = {&cmdline_lib_dirs,
                                          &libpath_lib_dirs, NULL};
-static const char *libexts[3] = {"a", "so", NULL};  /* possible library extentions */
+static const char *const libexts[3] = {"a", "so", NULL};  /* possible library extensions */
 #endif
 
 static void handler            PARAMS ((int));
@@ -292,16 +293,21 @@ static void scan_prog_file        PARAMS ((const char *, enum pass));
 #ifdef SCAN_LIBRARIES
 static void scan_libraries     PARAMS ((const char *));
 #endif
+#if LINK_ELIMINATE_DUPLICATE_LDIRECTORIES
+static int is_in_args          PARAMS ((const char *, const char **, const char **));
+#endif
 #ifdef COLLECT_EXPORT_LIST
+#if 0
 static int is_in_list          PARAMS ((const char *, struct id *));
+#endif
 static void write_aix_file     PARAMS ((FILE *, struct id *));
 static char *resolve_lib_name  PARAMS ((const char *));
-static int ignore_library      PARAMS ((const char *));
 #endif
 static char *extract_string    PARAMS ((const char **));
 \f
-#ifdef NO_DUP2
-int
+#ifndef HAVE_DUP2
+static int dup2 PARAMS ((int, int));
+static int
 dup2 (oldfd, newfd)
      int oldfd;
      int newfd;
@@ -320,7 +326,7 @@ dup2 (oldfd, newfd)
 
   return fd;
 }
-#endif
+#endif /* ! HAVE_DUP2 */
 \f
 /* Delete tempfiles and exit function.  */
 
@@ -356,19 +362,11 @@ collect_exit (status)
 void
 notice VPARAMS ((const char *msgid, ...))
 {
-#ifndef ANSI_PROTOTYPES
-  const char *msgid;
-#endif
-  va_list ap;
-
-  VA_START (ap, msgid);
-
-#ifndef ANSI_PROTOTYPES
-  msgid = va_arg (ap, const char *);
-#endif
+  VA_OPEN (ap, msgid);
+  VA_FIXEDARG (ap, const char *, msgid);
 
   vfprintf (stderr, _(msgid), ap);
-  va_end (ap);
+  VA_CLOSE (ap);
 }
 
 /* Die when sys call fails.  */
@@ -376,22 +374,15 @@ notice VPARAMS ((const char *msgid, ...))
 void
 fatal_perror VPARAMS ((const char * msgid, ...))
 {
-#ifndef ANSI_PROTOTYPES
-  const char *msgid;
-#endif
   int e = errno;
-  va_list ap;
-
-  VA_START (ap, msgid);
 
-#ifndef ANSI_PROTOTYPES
-  msgid = va_arg (ap, const char *);
-#endif
+  VA_OPEN (ap, msgid);
+  VA_FIXEDARG (ap, const char *, msgid);
 
   fprintf (stderr, "collect2: ");
   vfprintf (stderr, _(msgid), ap);
   fprintf (stderr, ": %s\n", xstrerror (e));
-  va_end (ap);
+  VA_CLOSE (ap);
 
   collect_exit (FATAL_EXIT_CODE);
 }
@@ -401,21 +392,13 @@ fatal_perror VPARAMS ((const char * msgid, ...))
 void
 fatal VPARAMS ((const char * msgid, ...))
 {
-#ifndef ANSI_PROTOTYPES
-  const char *msgid;
-#endif
-  va_list ap;
-  
-  VA_START (ap, msgid);
-
-#ifndef ANSI_PROTOTYPES
-  msgid = va_arg (ap, const char *);
-#endif
+  VA_OPEN (ap, msgid);
+  VA_FIXEDARG (ap, const char *, msgid);
   
   fprintf (stderr, "collect2: ");
   vfprintf (stderr, _(msgid), ap);
   fprintf (stderr, "\n");
-  va_end (ap);
+  VA_CLOSE (ap);
 
   collect_exit (FATAL_EXIT_CODE);
 }
@@ -425,21 +408,13 @@ fatal VPARAMS ((const char * msgid, ...))
 void
 error VPARAMS ((const char * msgid, ...))
 {
-#ifndef ANSI_PROTOTYPES
-  const char * msgid;
-#endif
-  va_list ap;
-  VA_START (ap, msgid);
-  
-#ifndef ANSI_PROTOTYPES
-  msgid = va_arg (ap, const char *);
-#endif
+  VA_OPEN (ap, msgid);
+  VA_FIXEDARG (ap, const char *, msgid);
 
   fprintf (stderr, "collect2: ");
   vfprintf (stderr, _(msgid), ap);
   fprintf (stderr, "\n");
-  va_end(ap);
+  VA_CLOSE(ap);
 }
 
 /* In case obstack is linked in, and abort is defined to fancy_abort,
@@ -519,7 +494,6 @@ dump_file (name)
      const char *name;
 {
   FILE *stream = fopen (name, "r");
-  int no_demangle = !! getenv ("COLLECT_NO_DEMANGLE");
 
   if (stream == 0)
     return;
@@ -527,7 +501,7 @@ dump_file (name)
     {
       int c;
       while (c = getc (stream),
-            c != EOF && (ISALNUM (c) || c == '_' || c == '$' || c == '.'))
+            c != EOF && (ISIDNUM (c) || c == '$' || c == '.'))
        obstack_1grow (&temporary_obstack, c);
       if (obstack_object_size (&temporary_obstack) > 0)
        {
@@ -539,13 +513,13 @@ dump_file (name)
          if (*word == '.')
            ++word, putc ('.', stderr);
          p = word;
-         if (*p == '_' && prepends_underscore)
-           ++p;
+         if (!strncmp (p, USER_LABEL_PREFIX, strlen (USER_LABEL_PREFIX)))
+           p += strlen (USER_LABEL_PREFIX);
 
          if (no_demangle)
            result = 0;
          else
-           result = cplus_demangle (p, DMGL_PARAMS | DMGL_ANSI);
+           result = cplus_demangle (p, DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE);
 
          if (result)
            {
@@ -553,7 +527,7 @@ dump_file (name)
              fputs (result, stderr);
 
              diff = strlen (word) - strlen (result);
-             while (diff > 0)
+             while (diff > 0 && c == ' ')
                --diff, putc (' ', stderr);
              while (diff < 0 && c == ' ')
                ++diff, c = getc (stream);
@@ -582,13 +556,23 @@ static int
 is_ctor_dtor (s)
      const char *s;
 {
-  struct names { const char *name; int len; int ret; int two_underscores; };
+  struct names { const char *const name; const int len; const int ret;
+    const int two_underscores; };
 
-  register struct names *p;
-  register int ch;
-  register const char *orig_s = s;
+  const struct names *p;
+  int ch;
+  const char *orig_s = s;
 
-  static struct names special[] = {
+  static const struct names special[] = {
+#ifndef NO_DOLLAR_IN_LABEL
+    { "GLOBAL__I$", sizeof ("GLOBAL__I$")-1, 1, 0 },
+    { "GLOBAL__D$", sizeof ("GLOBAL__D$")-1, 2, 0 },
+#else
+#ifndef NO_DOT_IN_LABEL
+    { "GLOBAL__I.", sizeof ("GLOBAL__I.")-1, 1, 0 },
+    { "GLOBAL__D.", sizeof ("GLOBAL__D.")-1, 2, 0 },
+#endif /* NO_DOT_IN_LABEL */
+#endif /* NO_DOLLAR_IN_LABEL */
     { "GLOBAL__I_", sizeof ("GLOBAL__I_")-1, 1, 0 },
     { "GLOBAL__D_", sizeof ("GLOBAL__D_")-1, 2, 0 },
     { "GLOBAL__F_", sizeof ("GLOBAL__F_")-1, 5, 0 },
@@ -652,8 +636,8 @@ find_a_file (pprefix, name)
   if (debug)
     fprintf (stderr, "Looking for '%s'\n", name);
   
-#ifdef EXECUTABLE_SUFFIX
-  len += strlen (EXECUTABLE_SUFFIX);
+#ifdef HOST_EXECUTABLE_SUFFIX
+  len += strlen (HOST_EXECUTABLE_SUFFIX);
 #endif
 
   temp = xmalloc (len);
@@ -676,11 +660,11 @@ find_a_file (pprefix, name)
          return temp;
        }
 
-#ifdef EXECUTABLE_SUFFIX
+#ifdef HOST_EXECUTABLE_SUFFIX
        /* Some systems have a suffix for executable files.
           So try appending that.  */
       strcpy (temp, name);
-       strcat (temp, EXECUTABLE_SUFFIX);
+       strcat (temp, HOST_EXECUTABLE_SUFFIX);
        
        if (access (temp, X_OK) == 0)
          return temp;
@@ -702,10 +686,10 @@ find_a_file (pprefix, name)
            && access (temp, X_OK) == 0)
          return temp;
 
-#ifdef EXECUTABLE_SUFFIX
+#ifdef HOST_EXECUTABLE_SUFFIX
        /* Some systems have a suffix for executable files.
           So try appending that.  */
-       strcat (temp, EXECUTABLE_SUFFIX);
+       strcat (temp, HOST_EXECUTABLE_SUFFIX);
        
        if (stat (temp, &st) >= 0
            && ! S_ISDIR (st.st_mode)
@@ -765,7 +749,7 @@ prefix_from_env (env, pprefix)
      struct path_prefix *pprefix;
 {
   const char *p;
-  GET_ENV_PATH_LIST (p, env);
+  GET_ENVIRONMENT (p, env);
 
   if (p)
     prefix_from_string (p, pprefix);
@@ -821,22 +805,48 @@ main (argc, argv)
      int argc;
      char *argv[];
 {
-  const char *ld_suffix        = "ld";
-  const char *full_ld_suffix   = ld_suffix;
-  const char *real_ld_suffix   = "real-ld";
-  const char *collect_ld_suffix = "collect-ld";
-  const char *nm_suffix        = "nm";
-  const char *full_nm_suffix   = nm_suffix;
-  const char *gnm_suffix       = "gnm";
-  const char *full_gnm_suffix  = gnm_suffix;
+  static const char *const ld_suffix   = "ld";
+  static const char *const real_ld_suffix = "real-ld";
+  static const char *const collect_ld_suffix = "collect-ld";
+  static const char *const nm_suffix   = "nm";
+  static const char *const gnm_suffix  = "gnm";
 #ifdef LDD_SUFFIX
-  const char *ldd_suffix       = LDD_SUFFIX;
-  const char *full_ldd_suffix  = ldd_suffix;
+  static const char *const ldd_suffix  = LDD_SUFFIX;
 #endif
-  const char *strip_suffix     = "strip";
-  const char *full_strip_suffix = strip_suffix;
-  const char *gstrip_suffix    = "gstrip";
-  const char *full_gstrip_suffix = gstrip_suffix;
+  static const char *const strip_suffix = "strip";
+  static const char *const gstrip_suffix = "gstrip";
+
+#ifdef CROSS_COMPILE
+  /* If we look for a program in the compiler directories, we just use
+     the short name, since these directories are already system-specific.
+     But it we look for a program in the system directories, we need to
+     qualify the program name with the target machine.  */
+
+  const char *const full_ld_suffix =
+    concat(target_machine, "-", ld_suffix, NULL);
+  const char *const full_nm_suffix =
+    concat (target_machine, "-", nm_suffix, NULL);
+  const char *const full_gnm_suffix =
+    concat (target_machine, "-", gnm_suffix, NULL);
+#ifdef LDD_SUFFIX
+  const char *const full_ldd_suffix =
+    concat (target_machine, "-", ldd_suffix, NULL);
+#endif
+  const char *const full_strip_suffix =
+    concat (target_machine, "-", strip_suffix, NULL);
+  const char *const full_gstrip_suffix =
+    concat (target_machine, "-", gstrip_suffix, NULL);
+#else
+  const char *const full_ld_suffix     = ld_suffix;
+  const char *const full_nm_suffix     = nm_suffix;
+  const char *const full_gnm_suffix    = gnm_suffix;
+#ifdef LDD_SUFFIX
+  const char *const full_ldd_suffix    = ldd_suffix;
+#endif
+  const char *const full_strip_suffix  = strip_suffix;
+  const char *const full_gstrip_suffix = gstrip_suffix;
+#endif /* CROSS_COMPILE */
+
   const char *arg;
   FILE *outf;
 #ifdef COLLECT_EXPORT_LIST
@@ -855,8 +865,13 @@ main (argc, argv)
   int first_file;
   int num_c_args       = argc+9;
 
+  no_demangle = !! getenv ("COLLECT_NO_DEMANGLE");
+
+  /* Suppress demangling by the real linker, which may be broken.  */
+  putenv (xstrdup ("COLLECT_NO_DEMANGLE="));
+
 #if defined (COLLECT2_HOST_INITIALIZATION)
-  /* Perform system dependent initialization, if neccessary.  */
+  /* Perform system dependent initialization, if necessary.  */
   COLLECT2_HOST_INITIALIZATION;
 #endif
 
@@ -866,18 +881,7 @@ main (argc, argv)
   signal (SIGCHLD, SIG_DFL);
 #endif
 
-/* LC_CTYPE determines the character set used by the terminal so it has be set
-   to output messages correctly.  */
-
-#ifdef HAVE_LC_MESSAGES
-  setlocale (LC_CTYPE, "");
-  setlocale (LC_MESSAGES, "");
-#else
-  setlocale (LC_ALL, "");
-#endif
-
-  (void) bindtextdomain (PACKAGE, localedir);
-  (void) textdomain (PACKAGE);
+  gcc_init_libintl ();
 
   /* Do not invoke xcalloc before this point, since locale needs to be
      set first, in case a diagnostic is issued.  */
@@ -909,7 +913,6 @@ main (argc, argv)
 #endif
 
   obstack_begin (&temporary_obstack, 0);
-  obstack_begin (&permanent_obstack, 0);
   temporary_firstobj = (char *) obstack_alloc (&temporary_obstack, 0);
 
   current_demangling_style = auto_demangling;
@@ -956,31 +959,6 @@ main (argc, argv)
   prefix_from_env ("COMPILER_PATH", &cpath);
   prefix_from_env ("PATH", &path);
 
-#ifdef CROSS_COMPILE
-  /* If we look for a program in the compiler directories, we just use
-     the short name, since these directories are already system-specific.
-     But it we look for a program in the system directories, we need to
-     qualify the program name with the target machine.  */
-
-  full_ld_suffix = concat(target_machine, "-", ld_suffix, NULL);
-
-#if 0
-  full_gld_suffix = concat (target_machine, "-", gld_suffix, NULL);
-#endif
-
-  full_nm_suffix = concat (target_machine, "-", nm_suffix, NULL);
-
-  full_gnm_suffix = concat (target_machine, "-", gnm_suffix, NULL);
-  
-#ifdef LDD_SUFFIX
-  full_ldd_suffix = concat (target_machine, "-", ldd_suffix, NULL);
-#endif
-
-  full_strip_suffix = concat (target_machine, "-", strip_suffix, NULL);
-  
-  full_gstrip_suffix = concat (target_machine, "-", gstrip_suffix, NULL);
-#endif /* CROSS_COMPILE */
-
   /* Try to discover a valid linker/nm/strip to use.  */
 
   /* Maybe we know the right file to use (if not cross).  */
@@ -1094,18 +1072,18 @@ main (argc, argv)
     {
       const char *q = extract_string (&p);
       if (*q == '-' && (q[1] == 'm' || q[1] == 'f'))
-       *c_ptr++ = obstack_copy0 (&permanent_obstack, q, strlen (q));
+       *c_ptr++ = xstrdup (q);
       if (strcmp (q, "-EL") == 0 || strcmp (q, "-EB") == 0)
-       *c_ptr++ = obstack_copy0 (&permanent_obstack, q, strlen (q));
-      if (strncmp (q, "-shared", sizeof ("-shared") - 1) == 0)
+       *c_ptr++ = xstrdup (q);
+      if (strcmp (q, "-shared") == 0)
        shared_obj = 1;
       if (*q == '-' && q[1] == 'B')
        {
-         *c_ptr++ = obstack_copy0 (&permanent_obstack, q, strlen (q));
+         *c_ptr++ = xstrdup (q);
          if (q[2] == 0)
            {
              q = extract_string (&p);
-             *c_ptr++ = obstack_copy0 (&permanent_obstack, q, strlen (q));
+             *c_ptr++ = xstrdup (q);
            }
        }
     }
@@ -1177,6 +1155,13 @@ main (argc, argv)
                    case 'L':
              add_prefix (&cmdline_lib_dirs, arg+2);
              break;
+#else 
+#if LINK_ELIMINATE_DUPLICATE_LDIRECTORIES
+           case 'L':
+             if (is_in_args (arg, (const char **) ld1_argv, ld1-1))
+               --ld1;
+             break;
+#endif /* LINK_ELIMINATE_DUPLICATE_LDIRECTORIES */
 #endif
 
            case 'o':
@@ -1269,9 +1254,8 @@ main (argc, argv)
 
   if (exports.first)
     {
-      char *buf = xmalloc (strlen (export_file) + 5);
-
-      sprintf (buf, "-bE:%s", export_file);
+      char *buf = concat ("-bE:", export_file, NULL);
+      
       *ld1++ = buf;
       *ld2++ = buf;
 
@@ -1418,7 +1402,7 @@ main (argc, argv)
       return 0;
     }
 
-  /* Sort ctor and dtor lists by priority. */
+  /* Sort ctor and dtor lists by priority.  */
   sort_ids (&constructors);
   sort_ids (&destructors);
 
@@ -1435,13 +1419,7 @@ main (argc, argv)
   /* Tell the linker that we have initializer and finalizer functions.  */
 #ifdef LD_INIT_SWITCH
 #ifdef COLLECT_EXPORT_LIST
-  {
-    /* option name + functions + colons + NULL */
-    char *buf = xmalloc (strlen (LD_INIT_SWITCH)
-                        + strlen(initname) + strlen(fininame) + 3);
-    sprintf (buf, "%s:%s:%s", LD_INIT_SWITCH, initname, fininame);
-    *ld2++ = buf;
-  }
+  *ld2++ = concat (LD_INIT_SWITCH, ":", initname, ":", fininame, NULL);
 #else
   *ld2++ = LD_INIT_SWITCH;
   *ld2++ = initname;
@@ -1456,12 +1434,7 @@ main (argc, argv)
       /* If we did not add export flag to link arguments before, add it to
         second link phase now.  No new exports should have been added.  */
       if (! exports.first)
-       {
-         char *buf = xmalloc (strlen (export_file) + 5);
-
-         sprintf (buf, "-bE:%s", export_file);
-         *ld2++ = buf;
-       }
+       *ld2++ = concat ("-bE:", export_file, NULL);
 
       add_to_list (&exports, initname);
       add_to_list (&exports, fininame);
@@ -1519,7 +1492,7 @@ main (argc, argv)
 }
 
 \f
-/* Wait for a process to finish, and exit if a non-zero status is found.  */
+/* Wait for a process to finish, and exit if a nonzero status is found.  */
 
 int
 collect_wait (prog)
@@ -1527,7 +1500,7 @@ collect_wait (prog)
 {
   int status;
 
-  pwait (pexecute_pid, &status, 0);
+  pwait (pid, &status, 0);
   if (status)
     {
       if (WIFSIGNALED (status))
@@ -1616,9 +1589,8 @@ collect_execute (prog, argv, redir)
       dup2 (redir_handle, STDERR_FILENO);
     }
 
-  pexecute_pid = pexecute (argv[0], argv, argv[0], NULL,
-                          &errmsg_fmt, &errmsg_arg,
-                          (PEXECUTE_FIRST | PEXECUTE_LAST | PEXECUTE_SEARCH));
+  pid = pexecute (argv[0], argv, argv[0], NULL, &errmsg_fmt, &errmsg_arg,
+                 (PEXECUTE_FIRST | PEXECUTE_LAST | PEXECUTE_SEARCH));
 
   if (redir)
     {
@@ -1626,11 +1598,11 @@ collect_execute (prog, argv, redir)
       dup2 (stdout_save, STDOUT_FILENO);
       dup2 (stderr_save, STDERR_FILENO);
 
-      /* Close reponse file.  */
+      /* Close response file.  */
       close (redir_handle);
     }
 
- if (pexecute_pid == -1)
+ if (pid == -1)
    fatal_perror (errmsg_fmt, errmsg_arg);
 }
 
@@ -1705,7 +1677,7 @@ extract_init_priority (name)
     ++pos;
   pos += 10; /* strlen ("GLOBAL__X_") */
 
-  /* Extract init_p number from ctor/dtor name. */
+  /* Extract init_p number from ctor/dtor name.  */
   pri = atoi (name + pos);
   return pri ? pri : DEFAULT_INIT_PRIORITY;
 }
@@ -1768,8 +1740,27 @@ write_list (stream, prefix, list)
     }
 }
 
+#if LINK_ELIMINATE_DUPLICATE_LDIRECTORIES
+/* Given a STRING, return nonzero if it occurs in the list in range
+   [ARGS_BEGIN,ARGS_END).  */
+
+static int
+is_in_args (string, args_begin, args_end)
+     const char *string;
+     const char **args_begin;
+     const char **args_end;
+{
+  const char **args_pointer;
+  for (args_pointer = args_begin; args_pointer != args_end; ++args_pointer)
+    if (strcmp (string, *args_pointer) == 0)
+      return 1;
+  return 0;
+}
+#endif /* LINK_ELIMINATE_DUPLICATE_LDIRECTORIES */
+
 #ifdef COLLECT_EXPORT_LIST
 /* This function is really used only on AIX, but may be useful.  */
+#if 0
 static int
 is_in_list (prefix, list)
      const char *prefix;
@@ -1783,6 +1774,7 @@ is_in_list (prefix, list)
     return 0;
 }
 #endif
+#endif /* COLLECT_EXPORT_LIST */
 
 /* Added for debugging purpose.  */
 #ifdef COLLECT_EXPORT_LIST
@@ -1878,13 +1870,8 @@ write_c_file_stat (stream, name)
     notice ("\nwrite_c_file - output name is %s, prefix is %s\n",
            output_file, prefix);
 
-#define INIT_NAME_FORMAT "_GLOBAL__FI_%s"
-  initname = xmalloc (strlen (prefix) + sizeof (INIT_NAME_FORMAT) - 2);
-  sprintf (initname, INIT_NAME_FORMAT, prefix);
-
-#define FINI_NAME_FORMAT "_GLOBAL__FD_%s"
-  fininame = xmalloc (strlen (prefix) + sizeof (FINI_NAME_FORMAT) - 2);
-  sprintf (fininame, FINI_NAME_FORMAT, prefix);
+  initname = concat ("_GLOBAL__FI_", prefix, NULL);
+  fininame = concat ("_GLOBAL__FD_", prefix, NULL);
 
   free (prefix);
 
@@ -1960,8 +1947,8 @@ write_c_file_stat (stream, name)
 
   if (shared_obj)
     {
-      fprintf (stream, "void _GLOBAL__DI() {\n\t%s();\n}\n", initname);
-      fprintf (stream, "void _GLOBAL__DD() {\n\t%s();\n}\n", fininame);
+      COLLECT_SHARED_INIT_FUNC(stream, initname);
+      COLLECT_SHARED_FINI_FUNC(stream, fininame);
     }
 }
 
@@ -2082,7 +2069,6 @@ scan_prog_file (prog_name, which_pass)
   void (*quit_handler) PARAMS ((int));
   char *real_nm_argv[4];
   const char **nm_argv = (const char **) real_nm_argv;
-  int pid;
   int argc = 0;
   int pipe_fd[2];
   char *p, buf[1024];
@@ -2142,7 +2128,7 @@ scan_prog_file (prog_name, which_pass)
        fatal_perror ("close %d", pipe_fd[1]);
 
       execv (nm_file_name, real_nm_argv);
-      fatal_perror ("execvp %s", nm_file_name);
+      fatal_perror ("execv %s", nm_file_name);
     }
 
   /* Parent context from here on.  */
@@ -2331,7 +2317,7 @@ libcompare (d1, d2)
       /* It has a valid numeric extension, prefer this one.  */
       if (*e1 == '.' && e1[1] && ISDIGIT (e1[1]))
        return 1;
-      /* It has a invalid numeric extension, must prefer the other one.  */
+      /* It has an invalid numeric extension, must prefer the other one.  */
       else
        return -1;
     }
@@ -2340,7 +2326,7 @@ libcompare (d1, d2)
       /* It has a valid numeric extension, prefer this one.  */
       if (*e2 == '.' && e2[1] && ISDIGIT (e2[1]))
        return -1;
-      /* It has a invalid numeric extension, must prefer the other one.  */
+      /* It has an invalid numeric extension, must prefer the other one.  */
       else
        return 1;
     }
@@ -2527,7 +2513,6 @@ scan_libraries (prog_name)
   void (*quit_handler) PARAMS ((int));
   char *real_ldd_argv[4];
   const char **ldd_argv = (const char **) real_ldd_argv;
-  int pid;
   int argc = 0;
   int pipe_fd[2];
   char buf[1024];
@@ -2697,7 +2682,44 @@ scan_libraries (prog_name)
 
 #endif
 
-extern char *ldgetname ();
+#ifdef COLLECT_EXPORT_LIST
+/* Array of standard AIX libraries which should not
+   be scanned for ctors/dtors.  */
+static const char *const aix_std_libs[] = {
+  "/unix",
+  "/lib/libc.a",
+  "/lib/libm.a",
+  "/lib/libc_r.a",
+  "/lib/libm_r.a",
+  "/usr/lib/libc.a",
+  "/usr/lib/libm.a",
+  "/usr/lib/libc_r.a",
+  "/usr/lib/libm_r.a",
+  "/usr/lib/threads/libc.a",
+  "/usr/ccs/lib/libc.a",
+  "/usr/ccs/lib/libm.a",
+  "/usr/ccs/lib/libc_r.a",
+  "/usr/ccs/lib/libm_r.a",
+  NULL
+};
+
+/* This function checks the filename and returns 1
+   if this name matches the location of a standard AIX library.  */
+static int ignore_library      PARAMS ((const char *));
+static int
+ignore_library (name)
+     const char *name;
+{
+  const char *const *p = &aix_std_libs[0];
+  while (*p++ != NULL)
+    if (! strcmp (name, *p)) return 1;
+  return 0;
+}
+#endif /* COLLECT_EXPORT_LIST */
+
+#if defined (HAVE_DECL_LDGETNAME) && !HAVE_DECL_LDGETNAME
+extern char *ldgetname PARAMS ((LDFILE *, GCC_SYMENT *));
+#endif
 
 /* COFF version to scan the name list of the loaded program for
    the symbols g++ uses for static constructors and destructors.
@@ -2868,7 +2890,7 @@ scan_prog_file (prog_name, which_pass)
   (void) ldclose(ldptr);
 #endif
 }
-
+#endif /* OBJECT_FORMAT_COFF */
 
 #ifdef COLLECT_EXPORT_LIST
 /* Given a library name without "lib" prefix, this function
@@ -2913,44 +2935,10 @@ if (debug) fprintf (stderr, "found: %s\n", lib_buf);
   if (debug)
     fprintf (stderr, "not found\n");
   else
-    fatal ("Library lib%s not found", name);
+    fatal ("library lib%s not found", name);
   return (NULL);
 }
-
-/* Array of standard AIX libraries which should not
-   be scanned for ctors/dtors.  */
-static const char *aix_std_libs[] = {
-  "/unix",
-  "/lib/libc.a",
-  "/lib/libm.a",
-  "/lib/libc_r.a",
-  "/lib/libm_r.a",
-  "/usr/lib/libc.a",
-  "/usr/lib/libm.a",
-  "/usr/lib/libc_r.a",
-  "/usr/lib/libm_r.a",
-  "/usr/lib/threads/libc.a",
-  "/usr/ccs/lib/libc.a",
-  "/usr/ccs/lib/libm.a",
-  "/usr/ccs/lib/libc_r.a",
-  "/usr/ccs/lib/libm_r.a",
-  NULL
-};
-
-/* This function checks the filename and returns 1
-   if this name matches the location of a standard AIX library. */
-static int
-ignore_library (name)
-     const char *name;
-{
-  const char **p = &aix_std_libs[0];
-  while (*p++ != NULL)
-    if (! strcmp (name, *p)) return 1;
-  return 0;
-}
-#endif
-
-#endif /* OBJECT_FORMAT_COFF */
+#endif /* COLLECT_EXPORT_LIST */
 
 \f
 /*