OSDN Git Service

PR bootstrap/14316
[pf3gnuchains/gcc-fork.git] / gcc / collect2.c
1 /* Collect static initialization info into data structures that can be
2    traversed by C++ initialization and finalization routines.
3    Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4    1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5    Contributed by Chris Smith (csmith@convex.com).
6    Heavily modified by Michael Meissner (meissner@cygnus.com),
7    Per Bothner (bothner@cygnus.com), and John Gilmore (gnu@cygnus.com).
8
9 This file is part of GCC.
10
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 2, or (at your option) any later
14 version.
15
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19 for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING.  If not, write to the Free
23 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 02111-1307, USA.  */
25
26
27 /* Build tables of static constructors and destructors and run ld.  */
28
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include <signal.h>
34 #if ! defined( SIGCHLD ) && defined( SIGCLD )
35 #  define SIGCHLD SIGCLD
36 #endif
37
38 #ifndef LIBRARY_PATH_ENV
39 #define LIBRARY_PATH_ENV "LIBRARY_PATH"
40 #endif
41
42 #define COLLECT
43
44 #include "collect2.h"
45 #include "demangle.h"
46 #include "obstack.h"
47 #include "intl.h"
48 #include "version.h"
49 \f
50 /* On certain systems, we have code that works by scanning the object file
51    directly.  But this code uses system-specific header files and library
52    functions, so turn it off in a cross-compiler.  Likewise, the names of
53    the utilities are not correct for a cross-compiler; we have to hope that
54    cross-versions are in the proper directories.  */
55
56 #ifdef CROSS_COMPILE
57 #undef OBJECT_FORMAT_COFF
58 #undef MD_EXEC_PREFIX
59 #undef REAL_LD_FILE_NAME
60 #undef REAL_NM_FILE_NAME
61 #undef REAL_STRIP_FILE_NAME
62 #endif
63
64 /* If we cannot use a special method, use the ordinary one:
65    run nm to find what symbols are present.
66    In a cross-compiler, this means you need a cross nm,
67    but that is not quite as unpleasant as special headers.  */
68
69 #if !defined (OBJECT_FORMAT_COFF)
70 #define OBJECT_FORMAT_NONE
71 #endif
72
73 #ifdef OBJECT_FORMAT_COFF
74
75 #include <a.out.h>
76 #include <ar.h>
77
78 #ifdef UMAX
79 #include <sgs.h>
80 #endif
81
82 /* Many versions of ldfcn.h define these.  */
83 #ifdef FREAD
84 #undef FREAD
85 #undef FWRITE
86 #endif
87
88 #include <ldfcn.h>
89
90 /* Some systems have an ISCOFF macro, but others do not.  In some cases
91    the macro may be wrong.  MY_ISCOFF is defined in tm.h files for machines
92    that either do not have an ISCOFF macro in /usr/include or for those
93    where it is wrong.  */
94
95 #ifndef MY_ISCOFF
96 #define MY_ISCOFF(X) ISCOFF (X)
97 #endif
98
99 #endif /* OBJECT_FORMAT_COFF */
100
101 #ifdef OBJECT_FORMAT_NONE
102
103 /* Default flags to pass to nm.  */
104 #ifndef NM_FLAGS
105 #define NM_FLAGS "-n"
106 #endif
107
108 #endif /* OBJECT_FORMAT_NONE */
109
110 /* Some systems use __main in a way incompatible with its use in gcc, in these
111    cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
112    give the same symbol without quotes for an alternative entry point.  */
113 #ifndef NAME__MAIN
114 #define NAME__MAIN "__main"
115 #endif
116
117 /* This must match tree.h.  */
118 #define DEFAULT_INIT_PRIORITY 65535
119
120 #ifndef COLLECT_SHARED_INIT_FUNC
121 #define COLLECT_SHARED_INIT_FUNC(STREAM, FUNC) \
122   fprintf ((STREAM), "void _GLOBAL__DI() {\n\t%s();\n}\n", (FUNC))
123 #endif
124 #ifndef COLLECT_SHARED_FINI_FUNC
125 #define COLLECT_SHARED_FINI_FUNC(STREAM, FUNC) \
126   fprintf ((STREAM), "void _GLOBAL__DD() {\n\t%s();\n}\n", (FUNC))
127 #endif
128
129 #ifdef LDD_SUFFIX
130 #define SCAN_LIBRARIES
131 #endif
132
133 #ifdef USE_COLLECT2
134 int do_collecting = 1;
135 #else
136 int do_collecting = 0;
137 #endif
138
139 /* Nonzero if we should suppress the automatic demangling of identifiers
140    in linker error messages.  Set from COLLECT_NO_DEMANGLE.  */
141 int no_demangle;
142 \f
143 /* Linked lists of constructor and destructor names.  */
144
145 struct id
146 {
147   struct id *next;
148   int sequence;
149   char name[1];
150 };
151
152 struct head
153 {
154   struct id *first;
155   struct id *last;
156   int number;
157 };
158
159 /* Enumeration giving which pass this is for scanning the program file.  */
160
161 enum pass {
162   PASS_FIRST,                           /* without constructors */
163   PASS_OBJ,                             /* individual objects */
164   PASS_LIB,                             /* looking for shared libraries */
165   PASS_SECOND                           /* with constructors linked in */
166 };
167
168 int vflag;                              /* true if -v */
169 static int rflag;                       /* true if -r */
170 static int strip_flag;                  /* true if -s */
171 static const char *demangle_flag;
172 #ifdef COLLECT_EXPORT_LIST
173 static int export_flag;                 /* true if -bE */
174 static int aix64_flag;                  /* true if -b64 */
175 static int aixrtl_flag;                 /* true if -brtl */
176 #endif
177
178 int debug;                              /* true if -debug */
179
180 static int shared_obj;                  /* true if -shared */
181
182 static const char *c_file;              /* <xxx>.c for constructor/destructor list.  */
183 static const char *o_file;              /* <xxx>.o for constructor/destructor list.  */
184 #ifdef COLLECT_EXPORT_LIST
185 static const char *export_file;         /* <xxx>.x for AIX export list.  */
186 #endif
187 const char *ldout;                      /* File for ld errors.  */
188 static const char *output_file;         /* Output file for ld.  */
189 static const char *nm_file_name;        /* pathname of nm */
190 #ifdef LDD_SUFFIX
191 static const char *ldd_file_name;       /* pathname of ldd (or equivalent) */
192 #endif
193 static const char *strip_file_name;             /* pathname of strip */
194 const char *c_file_name;                /* pathname of gcc */
195 static char *initname, *fininame;       /* names of init and fini funcs */
196
197 static struct head constructors;        /* list of constructors found */
198 static struct head destructors;         /* list of destructors found */
199 #ifdef COLLECT_EXPORT_LIST
200 static struct head exports;             /* list of exported symbols */
201 #endif
202 static struct head frame_tables;        /* list of frame unwind info tables */
203
204 struct obstack temporary_obstack;
205 char * temporary_firstobj;
206
207 /* Structure to hold all the directories in which to search for files to
208    execute.  */
209
210 struct prefix_list
211 {
212   const char *prefix;         /* String to prepend to the path.  */
213   struct prefix_list *next;   /* Next in linked list.  */
214 };
215
216 struct path_prefix
217 {
218   struct prefix_list *plist;  /* List of prefixes to try */
219   int max_len;                /* Max length of a prefix in PLIST */
220   const char *name;           /* Name of this list (used in config stuff) */
221 };
222
223 #ifdef COLLECT_EXPORT_LIST
224 /* Lists to keep libraries to be scanned for global constructors/destructors.  */
225 static struct head libs;                    /* list of libraries */
226 static struct path_prefix cmdline_lib_dirs; /* directories specified with -L */
227 static struct path_prefix libpath_lib_dirs; /* directories in LIBPATH */
228 static struct path_prefix *libpaths[3] = {&cmdline_lib_dirs,
229                                           &libpath_lib_dirs, NULL};
230 #endif
231
232 static void handler (int);
233 static int is_ctor_dtor (const char *);
234 static char *find_a_file (struct path_prefix *, const char *);
235 static void add_prefix (struct path_prefix *, const char *);
236 static void prefix_from_env (const char *, struct path_prefix *);
237 static void prefix_from_string (const char *, struct path_prefix *);
238 static void do_wait (const char *, struct pex_obj *);
239 static void fork_execute (const char *, char **);
240 static void maybe_unlink (const char *);
241 static void add_to_list (struct head *, const char *);
242 static int extract_init_priority (const char *);
243 static void sort_ids (struct head *);
244 static void write_list (FILE *, const char *, struct id *);
245 #ifdef COLLECT_EXPORT_LIST
246 static void dump_list (FILE *, const char *, struct id *);
247 #endif
248 #if 0
249 static void dump_prefix_list (FILE *, const char *, struct prefix_list *);
250 #endif
251 static void write_list_with_asm (FILE *, const char *, struct id *);
252 static void write_c_file (FILE *, const char *);
253 static void write_c_file_stat (FILE *, const char *);
254 #ifndef LD_INIT_SWITCH
255 static void write_c_file_glob (FILE *, const char *);
256 #endif
257 static void scan_prog_file (const char *, enum pass);
258 #ifdef SCAN_LIBRARIES
259 static void scan_libraries (const char *);
260 #endif
261 #if LINK_ELIMINATE_DUPLICATE_LDIRECTORIES
262 static int is_in_args (const char *, const char **, const char **);
263 #endif
264 #ifdef COLLECT_EXPORT_LIST
265 #if 0
266 static int is_in_list (const char *, struct id *);
267 #endif
268 static void write_aix_file (FILE *, struct id *);
269 static char *resolve_lib_name (const char *);
270 #endif
271 static char *extract_string (const char **);
272 \f
273 #ifndef HAVE_DUP2
274 static int
275 dup2 (int oldfd, int newfd)
276 {
277   int fdtmp[256];
278   int fdx = 0;
279   int fd;
280
281   if (oldfd == newfd)
282     return oldfd;
283   close (newfd);
284   while ((fd = dup (oldfd)) != newfd && fd >= 0) /* good enough for low fd's */
285     fdtmp[fdx++] = fd;
286   while (fdx > 0)
287     close (fdtmp[--fdx]);
288
289   return fd;
290 }
291 #endif /* ! HAVE_DUP2 */
292 \f
293 /* Delete tempfiles and exit function.  */
294
295 void
296 collect_exit (int status)
297 {
298   if (c_file != 0 && c_file[0])
299     maybe_unlink (c_file);
300
301   if (o_file != 0 && o_file[0])
302     maybe_unlink (o_file);
303
304 #ifdef COLLECT_EXPORT_LIST
305   if (export_file != 0 && export_file[0])
306     maybe_unlink (export_file);
307 #endif
308
309   if (ldout != 0 && ldout[0])
310     {
311       dump_file (ldout);
312       maybe_unlink (ldout);
313     }
314
315   if (status != 0 && output_file != 0 && output_file[0])
316     maybe_unlink (output_file);
317
318   exit (status);
319 }
320
321 \f
322 /* Notify user of a non-error.  */
323 void
324 notice (const char *msgid, ...)
325 {
326   va_list ap;
327
328   va_start (ap, msgid);
329   vfprintf (stderr, _(msgid), ap);
330   va_end (ap);
331 }
332
333 /* Die when sys call fails.  */
334
335 void
336 fatal_perror (const char * msgid, ...)
337 {
338   int e = errno;
339   va_list ap;
340
341   va_start (ap, msgid);
342   fprintf (stderr, "collect2: ");
343   vfprintf (stderr, _(msgid), ap);
344   fprintf (stderr, ": %s\n", xstrerror (e));
345   va_end (ap);
346
347   collect_exit (FATAL_EXIT_CODE);
348 }
349
350 /* Just die.  */
351
352 void
353 fatal (const char * msgid, ...)
354 {
355   va_list ap;
356
357   va_start (ap, msgid);
358   fprintf (stderr, "collect2: ");
359   vfprintf (stderr, _(msgid), ap);
360   fprintf (stderr, "\n");
361   va_end (ap);
362
363   collect_exit (FATAL_EXIT_CODE);
364 }
365
366 /* Write error message.  */
367
368 void
369 error (const char * msgid, ...)
370 {
371   va_list ap;
372
373   va_start (ap, msgid);
374   fprintf (stderr, "collect2: ");
375   vfprintf (stderr, _(msgid), ap);
376   fprintf (stderr, "\n");
377   va_end(ap);
378 }
379
380 /* In case obstack is linked in, and abort is defined to fancy_abort,
381    provide a default entry.  */
382
383 void
384 fancy_abort (const char *file, int line, const char *func)
385 {
386   fatal ("internal gcc abort in %s, at %s:%d", func, file, line);
387 }
388 \f
389 static void
390 handler (int signo)
391 {
392   if (c_file != 0 && c_file[0])
393     maybe_unlink (c_file);
394
395   if (o_file != 0 && o_file[0])
396     maybe_unlink (o_file);
397
398   if (ldout != 0 && ldout[0])
399     maybe_unlink (ldout);
400
401 #ifdef COLLECT_EXPORT_LIST
402   if (export_file != 0 && export_file[0])
403     maybe_unlink (export_file);
404 #endif
405
406   signal (signo, SIG_DFL);
407   raise (signo);
408 }
409
410 \f
411 int
412 file_exists (const char *name)
413 {
414   return access (name, R_OK) == 0;
415 }
416
417 /* Parse a reasonable subset of shell quoting syntax.  */
418
419 static char *
420 extract_string (const char **pp)
421 {
422   const char *p = *pp;
423   int backquote = 0;
424   int inside = 0;
425
426   for (;;)
427     {
428       char c = *p;
429       if (c == '\0')
430         break;
431       ++p;
432       if (backquote)
433         obstack_1grow (&temporary_obstack, c);
434       else if (! inside && c == ' ')
435         break;
436       else if (! inside && c == '\\')
437         backquote = 1;
438       else if (c == '\'')
439         inside = !inside;
440       else
441         obstack_1grow (&temporary_obstack, c);
442     }
443
444   obstack_1grow (&temporary_obstack, '\0');
445   *pp = p;
446   return obstack_finish (&temporary_obstack);
447 }
448 \f
449 void
450 dump_file (const char *name)
451 {
452   FILE *stream = fopen (name, "r");
453
454   if (stream == 0)
455     return;
456   while (1)
457     {
458       int c;
459       while (c = getc (stream),
460              c != EOF && (ISIDNUM (c) || c == '$' || c == '.'))
461         obstack_1grow (&temporary_obstack, c);
462       if (obstack_object_size (&temporary_obstack) > 0)
463         {
464           const char *word, *p;
465           char *result;
466           obstack_1grow (&temporary_obstack, '\0');
467           word = obstack_finish (&temporary_obstack);
468
469           if (*word == '.')
470             ++word, putc ('.', stderr);
471           p = word;
472           if (!strncmp (p, USER_LABEL_PREFIX, strlen (USER_LABEL_PREFIX)))
473             p += strlen (USER_LABEL_PREFIX);
474
475 #ifdef HAVE_LD_DEMANGLE
476           result = 0;
477 #else
478           if (no_demangle)
479             result = 0;
480           else
481             result = cplus_demangle (p, DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE);
482 #endif
483
484           if (result)
485             {
486               int diff;
487               fputs (result, stderr);
488
489               diff = strlen (word) - strlen (result);
490               while (diff > 0 && c == ' ')
491                 --diff, putc (' ', stderr);
492               while (diff < 0 && c == ' ')
493                 ++diff, c = getc (stream);
494
495               free (result);
496             }
497           else
498             fputs (word, stderr);
499
500           fflush (stderr);
501           obstack_free (&temporary_obstack, temporary_firstobj);
502         }
503       if (c == EOF)
504         break;
505       putc (c, stderr);
506     }
507   fclose (stream);
508 }
509 \f
510 /* Decide whether the given symbol is: a constructor (1), a destructor
511    (2), a routine in a shared object that calls all the constructors
512    (3) or destructors (4), a DWARF exception-handling table (5), or
513    nothing special (0).  */
514
515 static int
516 is_ctor_dtor (const char *s)
517 {
518   struct names { const char *const name; const int len; const int ret;
519     const int two_underscores; };
520
521   const struct names *p;
522   int ch;
523   const char *orig_s = s;
524
525   static const struct names special[] = {
526 #ifndef NO_DOLLAR_IN_LABEL
527     { "GLOBAL__I$", sizeof ("GLOBAL__I$")-1, 1, 0 },
528     { "GLOBAL__D$", sizeof ("GLOBAL__D$")-1, 2, 0 },
529 #else
530 #ifndef NO_DOT_IN_LABEL
531     { "GLOBAL__I.", sizeof ("GLOBAL__I.")-1, 1, 0 },
532     { "GLOBAL__D.", sizeof ("GLOBAL__D.")-1, 2, 0 },
533 #endif /* NO_DOT_IN_LABEL */
534 #endif /* NO_DOLLAR_IN_LABEL */
535     { "GLOBAL__I_", sizeof ("GLOBAL__I_")-1, 1, 0 },
536     { "GLOBAL__D_", sizeof ("GLOBAL__D_")-1, 2, 0 },
537     { "GLOBAL__F_", sizeof ("GLOBAL__F_")-1, 5, 0 },
538     { "GLOBAL__FI_", sizeof ("GLOBAL__FI_")-1, 3, 0 },
539     { "GLOBAL__FD_", sizeof ("GLOBAL__FD_")-1, 4, 0 },
540     { NULL, 0, 0, 0 }
541   };
542
543   while ((ch = *s) == '_')
544     ++s;
545
546   if (s == orig_s)
547     return 0;
548
549   for (p = &special[0]; p->len > 0; p++)
550     {
551       if (ch == p->name[0]
552           && (!p->two_underscores || ((s - orig_s) >= 2))
553           && strncmp(s, p->name, p->len) == 0)
554         {
555           return p->ret;
556         }
557     }
558   return 0;
559 }
560 \f
561 /* We maintain two prefix lists: one from COMPILER_PATH environment variable
562    and one from the PATH variable.  */
563
564 static struct path_prefix cpath, path;
565
566 #ifdef CROSS_COMPILE
567 /* This is the name of the target machine.  We use it to form the name
568    of the files to execute.  */
569
570 static const char *const target_machine = TARGET_MACHINE;
571 #endif
572
573 /* Search for NAME using prefix list PPREFIX.  We only look for executable
574    files.
575
576    Return 0 if not found, otherwise return its name, allocated with malloc.  */
577
578 static char *
579 find_a_file (struct path_prefix *pprefix, const char *name)
580 {
581   char *temp;
582   struct prefix_list *pl;
583   int len = pprefix->max_len + strlen (name) + 1;
584
585   if (debug)
586     fprintf (stderr, "Looking for '%s'\n", name);
587
588 #ifdef HOST_EXECUTABLE_SUFFIX
589   len += strlen (HOST_EXECUTABLE_SUFFIX);
590 #endif
591
592   temp = xmalloc (len);
593
594   /* Determine the filename to execute (special case for absolute paths).  */
595
596   if (*name == '/'
597 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
598       || (*name && name[1] == ':')
599 #endif
600       )
601     {
602       if (access (name, X_OK) == 0)
603         {
604           strcpy (temp, name);
605
606           if (debug)
607             fprintf (stderr, "  - found: absolute path\n");
608
609           return temp;
610         }
611
612 #ifdef HOST_EXECUTABLE_SUFFIX
613         /* Some systems have a suffix for executable files.
614            So try appending that.  */
615       strcpy (temp, name);
616         strcat (temp, HOST_EXECUTABLE_SUFFIX);
617
618         if (access (temp, X_OK) == 0)
619           return temp;
620 #endif
621
622       if (debug)
623         fprintf (stderr, "  - failed to locate using absolute path\n");
624     }
625   else
626     for (pl = pprefix->plist; pl; pl = pl->next)
627       {
628         struct stat st;
629
630         strcpy (temp, pl->prefix);
631         strcat (temp, name);
632
633         if (stat (temp, &st) >= 0
634             && ! S_ISDIR (st.st_mode)
635             && access (temp, X_OK) == 0)
636           return temp;
637
638 #ifdef HOST_EXECUTABLE_SUFFIX
639         /* Some systems have a suffix for executable files.
640            So try appending that.  */
641         strcat (temp, HOST_EXECUTABLE_SUFFIX);
642
643         if (stat (temp, &st) >= 0
644             && ! S_ISDIR (st.st_mode)
645             && access (temp, X_OK) == 0)
646           return temp;
647 #endif
648       }
649
650   if (debug && pprefix->plist == NULL)
651     fprintf (stderr, "  - failed: no entries in prefix list\n");
652
653   free (temp);
654   return 0;
655 }
656
657 /* Add an entry for PREFIX to prefix list PPREFIX.  */
658
659 static void
660 add_prefix (struct path_prefix *pprefix, const char *prefix)
661 {
662   struct prefix_list *pl, **prev;
663   int len;
664
665   if (pprefix->plist)
666     {
667       for (pl = pprefix->plist; pl->next; pl = pl->next)
668         ;
669       prev = &pl->next;
670     }
671   else
672     prev = &pprefix->plist;
673
674   /* Keep track of the longest prefix.  */
675
676   len = strlen (prefix);
677   if (len > pprefix->max_len)
678     pprefix->max_len = len;
679
680   pl = xmalloc (sizeof (struct prefix_list));
681   pl->prefix = xstrdup (prefix);
682
683   if (*prev)
684     pl->next = *prev;
685   else
686     pl->next = (struct prefix_list *) 0;
687   *prev = pl;
688 }
689 \f
690 /* Take the value of the environment variable ENV, break it into a path, and
691    add of the entries to PPREFIX.  */
692
693 static void
694 prefix_from_env (const char *env, struct path_prefix *pprefix)
695 {
696   const char *p;
697   GET_ENVIRONMENT (p, env);
698
699   if (p)
700     prefix_from_string (p, pprefix);
701 }
702
703 static void
704 prefix_from_string (const char *p, struct path_prefix *pprefix)
705 {
706   const char *startp, *endp;
707   char *nstore = xmalloc (strlen (p) + 3);
708
709   if (debug)
710     fprintf (stderr, "Convert string '%s' into prefixes, separator = '%c'\n", p, PATH_SEPARATOR);
711
712   startp = endp = p;
713   while (1)
714     {
715       if (*endp == PATH_SEPARATOR || *endp == 0)
716         {
717           strncpy (nstore, startp, endp-startp);
718           if (endp == startp)
719             {
720               strcpy (nstore, "./");
721             }
722           else if (! IS_DIR_SEPARATOR (endp[-1]))
723             {
724               nstore[endp-startp] = DIR_SEPARATOR;
725               nstore[endp-startp+1] = 0;
726             }
727           else
728             nstore[endp-startp] = 0;
729
730           if (debug)
731             fprintf (stderr, "  - add prefix: %s\n", nstore);
732
733           add_prefix (pprefix, nstore);
734           if (*endp == 0)
735             break;
736           endp = startp = endp + 1;
737         }
738       else
739         endp++;
740     }
741 }
742 \f
743 /* Main program.  */
744
745 int
746 main (int argc, char **argv)
747 {
748   static const char *const ld_suffix    = "ld";
749   static const char *const real_ld_suffix = "real-ld";
750   static const char *const collect_ld_suffix = "collect-ld";
751   static const char *const nm_suffix    = "nm";
752   static const char *const gnm_suffix   = "gnm";
753 #ifdef LDD_SUFFIX
754   static const char *const ldd_suffix   = LDD_SUFFIX;
755 #endif
756   static const char *const strip_suffix = "strip";
757   static const char *const gstrip_suffix = "gstrip";
758
759 #ifdef CROSS_COMPILE
760   /* If we look for a program in the compiler directories, we just use
761      the short name, since these directories are already system-specific.
762      But it we look for a program in the system directories, we need to
763      qualify the program name with the target machine.  */
764
765   const char *const full_ld_suffix =
766     concat(target_machine, "-", ld_suffix, NULL);
767   const char *const full_nm_suffix =
768     concat (target_machine, "-", nm_suffix, NULL);
769   const char *const full_gnm_suffix =
770     concat (target_machine, "-", gnm_suffix, NULL);
771 #ifdef LDD_SUFFIX
772   const char *const full_ldd_suffix =
773     concat (target_machine, "-", ldd_suffix, NULL);
774 #endif
775   const char *const full_strip_suffix =
776     concat (target_machine, "-", strip_suffix, NULL);
777   const char *const full_gstrip_suffix =
778     concat (target_machine, "-", gstrip_suffix, NULL);
779 #else
780   const char *const full_ld_suffix      = ld_suffix;
781   const char *const full_nm_suffix      = nm_suffix;
782   const char *const full_gnm_suffix     = gnm_suffix;
783 #ifdef LDD_SUFFIX
784   const char *const full_ldd_suffix     = ldd_suffix;
785 #endif
786   const char *const full_strip_suffix   = strip_suffix;
787   const char *const full_gstrip_suffix  = gstrip_suffix;
788 #endif /* CROSS_COMPILE */
789
790   const char *arg;
791   FILE *outf;
792 #ifdef COLLECT_EXPORT_LIST
793   FILE *exportf;
794 #endif
795   const char *ld_file_name;
796   const char *p;
797   char **c_argv;
798   const char **c_ptr;
799   char **ld1_argv;
800   const char **ld1;
801   char **ld2_argv;
802   const char **ld2;
803   char **object_lst;
804   const char **object;
805   int first_file;
806   int num_c_args        = argc+9;
807
808   no_demangle = !! getenv ("COLLECT_NO_DEMANGLE");
809
810   /* Suppress demangling by the real linker, which may be broken.  */
811   putenv (xstrdup ("COLLECT_NO_DEMANGLE="));
812
813 #if defined (COLLECT2_HOST_INITIALIZATION)
814   /* Perform system dependent initialization, if necessary.  */
815   COLLECT2_HOST_INITIALIZATION;
816 #endif
817
818 #ifdef SIGCHLD
819   /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
820      receive the signal.  A different setting is inheritable */
821   signal (SIGCHLD, SIG_DFL);
822 #endif
823
824   gcc_init_libintl ();
825
826   /* Do not invoke xcalloc before this point, since locale needs to be
827      set first, in case a diagnostic is issued.  */
828
829   ld1 = (const char **)(ld1_argv = xcalloc(sizeof (char *), argc+4));
830   ld2 = (const char **)(ld2_argv = xcalloc(sizeof (char *), argc+11));
831   object = (const char **)(object_lst = xcalloc(sizeof (char *), argc));
832
833 #ifdef DEBUG
834   debug = 1;
835 #endif
836
837   /* Parse command line early for instances of -debug.  This allows
838      the debug flag to be set before functions like find_a_file()
839      are called.  */
840   {
841     int i;
842
843     for (i = 1; argv[i] != NULL; i ++)
844       {
845         if (! strcmp (argv[i], "-debug"))
846           debug = 1;
847       }
848     vflag = debug;
849   }
850
851 #ifndef DEFAULT_A_OUT_NAME
852   output_file = "a.out";
853 #else
854   output_file = DEFAULT_A_OUT_NAME;
855 #endif
856
857   obstack_begin (&temporary_obstack, 0);
858   temporary_firstobj = obstack_alloc (&temporary_obstack, 0);
859
860 #ifndef HAVE_LD_DEMANGLE
861   current_demangling_style = auto_demangling;
862 #endif
863   p = getenv ("COLLECT_GCC_OPTIONS");
864   while (p && *p)
865     {
866       const char *q = extract_string (&p);
867       if (*q == '-' && (q[1] == 'm' || q[1] == 'f'))
868         num_c_args++;
869     }
870   obstack_free (&temporary_obstack, temporary_firstobj);
871
872   /* -fno-profile-arcs -fno-test-coverage -fno-branch-probabilities
873      -fno-exceptions -w */
874   num_c_args += 5;
875
876   c_ptr = (const char **) (c_argv = xcalloc (sizeof (char *), num_c_args));
877
878   if (argc < 2)
879     fatal ("no arguments");
880
881 #ifdef SIGQUIT
882   if (signal (SIGQUIT, SIG_IGN) != SIG_IGN)
883     signal (SIGQUIT, handler);
884 #endif
885   if (signal (SIGINT, SIG_IGN) != SIG_IGN)
886     signal (SIGINT, handler);
887 #ifdef SIGALRM
888   if (signal (SIGALRM, SIG_IGN) != SIG_IGN)
889     signal (SIGALRM, handler);
890 #endif
891 #ifdef SIGHUP
892   if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
893     signal (SIGHUP, handler);
894 #endif
895   if (signal (SIGSEGV, SIG_IGN) != SIG_IGN)
896     signal (SIGSEGV, handler);
897 #ifdef SIGBUS
898   if (signal (SIGBUS, SIG_IGN) != SIG_IGN)
899     signal (SIGBUS, handler);
900 #endif
901
902   /* Extract COMPILER_PATH and PATH into our prefix list.  */
903   prefix_from_env ("COMPILER_PATH", &cpath);
904   prefix_from_env ("PATH", &path);
905
906   /* Try to discover a valid linker/nm/strip to use.  */
907
908   /* Maybe we know the right file to use (if not cross).  */
909   ld_file_name = 0;
910 #ifdef DEFAULT_LINKER
911   if (access (DEFAULT_LINKER, X_OK) == 0)
912     ld_file_name = DEFAULT_LINKER;
913   if (ld_file_name == 0)
914 #endif
915 #ifdef REAL_LD_FILE_NAME
916   ld_file_name = find_a_file (&path, REAL_LD_FILE_NAME);
917   if (ld_file_name == 0)
918 #endif
919   /* Search the (target-specific) compiler dirs for ld'.  */
920   ld_file_name = find_a_file (&cpath, real_ld_suffix);
921   /* Likewise for `collect-ld'.  */
922   if (ld_file_name == 0)
923     ld_file_name = find_a_file (&cpath, collect_ld_suffix);
924   /* Search the compiler directories for `ld'.  We have protection against
925      recursive calls in find_a_file.  */
926   if (ld_file_name == 0)
927     ld_file_name = find_a_file (&cpath, ld_suffix);
928   /* Search the ordinary system bin directories
929      for `ld' (if native linking) or `TARGET-ld' (if cross).  */
930   if (ld_file_name == 0)
931     ld_file_name = find_a_file (&path, full_ld_suffix);
932
933 #ifdef REAL_NM_FILE_NAME
934   nm_file_name = find_a_file (&path, REAL_NM_FILE_NAME);
935   if (nm_file_name == 0)
936 #endif
937   nm_file_name = find_a_file (&cpath, gnm_suffix);
938   if (nm_file_name == 0)
939     nm_file_name = find_a_file (&path, full_gnm_suffix);
940   if (nm_file_name == 0)
941     nm_file_name = find_a_file (&cpath, nm_suffix);
942   if (nm_file_name == 0)
943     nm_file_name = find_a_file (&path, full_nm_suffix);
944
945 #ifdef LDD_SUFFIX
946   ldd_file_name = find_a_file (&cpath, ldd_suffix);
947   if (ldd_file_name == 0)
948     ldd_file_name = find_a_file (&path, full_ldd_suffix);
949 #endif
950
951 #ifdef REAL_STRIP_FILE_NAME
952   strip_file_name = find_a_file (&path, REAL_STRIP_FILE_NAME);
953   if (strip_file_name == 0)
954 #endif
955   strip_file_name = find_a_file (&cpath, gstrip_suffix);
956   if (strip_file_name == 0)
957     strip_file_name = find_a_file (&path, full_gstrip_suffix);
958   if (strip_file_name == 0)
959     strip_file_name = find_a_file (&cpath, strip_suffix);
960   if (strip_file_name == 0)
961     strip_file_name = find_a_file (&path, full_strip_suffix);
962
963   /* Determine the full path name of the C compiler to use.  */
964   c_file_name = getenv ("COLLECT_GCC");
965   if (c_file_name == 0)
966     {
967 #ifdef CROSS_COMPILE
968       c_file_name = concat (target_machine, "-gcc", NULL);
969 #else
970       c_file_name = "gcc";
971 #endif
972     }
973
974   p = find_a_file (&cpath, c_file_name);
975
976   /* Here it should be safe to use the system search path since we should have
977      already qualified the name of the compiler when it is needed.  */
978   if (p == 0)
979     p = find_a_file (&path, c_file_name);
980
981   if (p)
982     c_file_name = p;
983
984   *ld1++ = *ld2++ = ld_file_name;
985
986   /* Make temp file names.  */
987   c_file = make_temp_file (".c");
988   o_file = make_temp_file (".o");
989 #ifdef COLLECT_EXPORT_LIST
990   export_file = make_temp_file (".x");
991 #endif
992   ldout = make_temp_file (".ld");
993   *c_ptr++ = c_file_name;
994   *c_ptr++ = "-x";
995   *c_ptr++ = "c";
996   *c_ptr++ = "-c";
997   *c_ptr++ = "-o";
998   *c_ptr++ = o_file;
999
1000 #ifdef COLLECT_EXPORT_LIST
1001   /* Generate a list of directories from LIBPATH.  */
1002   prefix_from_env ("LIBPATH", &libpath_lib_dirs);
1003   /* Add to this list also two standard directories where
1004      AIX loader always searches for libraries.  */
1005   add_prefix (&libpath_lib_dirs, "/lib");
1006   add_prefix (&libpath_lib_dirs, "/usr/lib");
1007 #endif
1008
1009   /* Get any options that the upper GCC wants to pass to the sub-GCC.
1010
1011      AIX support needs to know if -shared has been specified before
1012      parsing commandline arguments.  */
1013
1014   p = getenv ("COLLECT_GCC_OPTIONS");
1015   while (p && *p)
1016     {
1017       const char *q = extract_string (&p);
1018       if (*q == '-' && (q[1] == 'm' || q[1] == 'f'))
1019         *c_ptr++ = xstrdup (q);
1020       if (strcmp (q, "-EL") == 0 || strcmp (q, "-EB") == 0)
1021         *c_ptr++ = xstrdup (q);
1022       if (strcmp (q, "-shared") == 0)
1023         shared_obj = 1;
1024       if (*q == '-' && q[1] == 'B')
1025         {
1026           *c_ptr++ = xstrdup (q);
1027           if (q[2] == 0)
1028             {
1029               q = extract_string (&p);
1030               *c_ptr++ = xstrdup (q);
1031             }
1032         }
1033     }
1034   obstack_free (&temporary_obstack, temporary_firstobj);
1035   *c_ptr++ = "-fno-profile-arcs";
1036   *c_ptr++ = "-fno-test-coverage";
1037   *c_ptr++ = "-fno-branch-probabilities";
1038   *c_ptr++ = "-fno-exceptions";
1039   *c_ptr++ = "-w";
1040
1041   /* !!! When GCC calls collect2,
1042      it does not know whether it is calling collect2 or ld.
1043      So collect2 cannot meaningfully understand any options
1044      except those ld understands.
1045      If you propose to make GCC pass some other option,
1046      just imagine what will happen if ld is really ld!!!  */
1047
1048   /* Parse arguments.  Remember output file spec, pass the rest to ld.  */
1049   /* After the first file, put in the c++ rt0.  */
1050
1051   first_file = 1;
1052 #ifdef HAVE_LD_DEMANGLE
1053   if (!demangle_flag && !no_demangle)
1054     demangle_flag = "--demangle";
1055   if (demangle_flag)
1056     *ld1++ = *ld2++ = demangle_flag;
1057 #endif
1058   while ((arg = *++argv) != (char *) 0)
1059     {
1060       *ld1++ = *ld2++ = arg;
1061
1062       if (arg[0] == '-')
1063         {
1064           switch (arg[1])
1065             {
1066 #ifdef COLLECT_EXPORT_LIST
1067             /* We want to disable automatic exports on AIX when user
1068                explicitly puts an export list in command line */
1069             case 'b':
1070               if (arg[2] == 'E' || strncmp (&arg[2], "export", 6) == 0)
1071                 export_flag = 1;
1072               else if (arg[2] == '6' && arg[3] == '4')
1073                 aix64_flag = 1;
1074               else if (arg[2] == 'r' && arg[3] == 't' && arg[4] == 'l')
1075                 aixrtl_flag = 1;
1076               break;
1077 #endif
1078
1079             case 'd':
1080               if (!strcmp (arg, "-debug"))
1081                 {
1082                   /* Already parsed.  */
1083                   ld1--;
1084                   ld2--;
1085                 }
1086               if (!strcmp (arg, "-dynamic-linker") && argv[1])
1087                 {
1088                   ++argv;
1089                   *ld1++ = *ld2++ = *argv;
1090                 }
1091               break;
1092
1093             case 'l':
1094               if (first_file)
1095                 {
1096                   /* place o_file BEFORE this argument! */
1097                   first_file = 0;
1098                   ld2--;
1099                   *ld2++ = o_file;
1100                   *ld2++ = arg;
1101                 }
1102 #ifdef COLLECT_EXPORT_LIST
1103               {
1104                 /* Resolving full library name.  */
1105                 const char *s = resolve_lib_name (arg+2);
1106
1107                 /* Saving a full library name.  */
1108                 add_to_list (&libs, s);
1109               }
1110 #endif
1111               break;
1112
1113 #ifdef COLLECT_EXPORT_LIST
1114             /* Saving directories where to search for libraries.  */
1115             case 'L':
1116               add_prefix (&cmdline_lib_dirs, arg+2);
1117               break;
1118 #else
1119 #if LINK_ELIMINATE_DUPLICATE_LDIRECTORIES
1120             case 'L':
1121               if (is_in_args (arg, (const char **) ld1_argv, ld1-1))
1122                 --ld1;
1123               break;
1124 #endif /* LINK_ELIMINATE_DUPLICATE_LDIRECTORIES */
1125 #endif
1126
1127             case 'o':
1128               if (arg[2] == '\0')
1129                 output_file = *ld1++ = *ld2++ = *++argv;
1130               else if (1
1131 #ifdef SWITCHES_NEED_SPACES
1132                        && ! strchr (SWITCHES_NEED_SPACES, arg[1])
1133 #endif
1134                        )
1135
1136                 output_file = &arg[2];
1137               break;
1138
1139             case 'r':
1140               if (arg[2] == '\0')
1141                 rflag = 1;
1142               break;
1143
1144             case 's':
1145               if (arg[2] == '\0' && do_collecting)
1146                 {
1147                   /* We must strip after the nm run, otherwise C++ linking
1148                      will not work.  Thus we strip in the second ld run, or
1149                      else with strip if there is no second ld run.  */
1150                   strip_flag = 1;
1151                   ld1--;
1152                 }
1153               break;
1154
1155             case 'v':
1156               if (arg[2] == '\0')
1157                 vflag = 1;
1158               break;
1159
1160             case '-':
1161               if (strcmp (arg, "--no-demangle") == 0)
1162                 {
1163                   demangle_flag = arg;
1164                   no_demangle = 1;
1165                   ld1--;
1166                   ld2--;
1167                 }
1168               else if (strncmp (arg, "--demangle", 10) == 0)
1169                 {
1170                   demangle_flag = arg;
1171                   no_demangle = 0;
1172 #ifndef HAVE_LD_DEMANGLE
1173                   if (arg[10] == '=')
1174                     {
1175                       enum demangling_styles style
1176                         = cplus_demangle_name_to_style (arg+11);
1177                       if (style == unknown_demangling)
1178                         error ("unknown demangling style '%s'", arg+11);
1179                       else
1180                         current_demangling_style = style;
1181                     }
1182 #endif
1183                   ld1--;
1184                   ld2--;
1185                 }
1186               break;
1187             }
1188         }
1189       else if ((p = strrchr (arg, '.')) != (char *) 0
1190                && (strcmp (p, ".o") == 0 || strcmp (p, ".a") == 0
1191                    || strcmp (p, ".so") == 0 || strcmp (p, ".lo") == 0
1192                    || strcmp (p, ".obj") == 0))
1193         {
1194           if (first_file)
1195             {
1196               first_file = 0;
1197               if (p[1] == 'o')
1198                 *ld2++ = o_file;
1199               else
1200                 {
1201                   /* place o_file BEFORE this argument! */
1202                   ld2--;
1203                   *ld2++ = o_file;
1204                   *ld2++ = arg;
1205                 }
1206             }
1207           if (p[1] == 'o' || p[1] == 'l')
1208             *object++ = arg;
1209 #ifdef COLLECT_EXPORT_LIST
1210           /* libraries can be specified directly, i.e. without -l flag.  */
1211           else
1212             {
1213               /* Saving a full library name.  */
1214               add_to_list (&libs, arg);
1215             }
1216 #endif
1217         }
1218     }
1219
1220 #ifdef COLLECT_EXPORT_LIST
1221   /* This is added only for debugging purposes.  */
1222   if (debug)
1223     {
1224       fprintf (stderr, "List of libraries:\n");
1225       dump_list (stderr, "\t", libs.first);
1226     }
1227
1228   /* The AIX linker will discard static constructors in object files if
1229      nothing else in the file is referenced, so look at them first.  */
1230   {
1231       const char **export_object_lst = (const char **)object_lst;
1232
1233       while (export_object_lst < object)
1234         scan_prog_file (*export_object_lst++, PASS_OBJ);
1235   }
1236   {
1237     struct id *list = libs.first;
1238
1239     for (; list; list = list->next)
1240       scan_prog_file (list->name, PASS_FIRST);
1241   }
1242
1243   if (exports.first)
1244     {
1245       char *buf = concat ("-bE:", export_file, NULL);
1246
1247       *ld1++ = buf;
1248       *ld2++ = buf;
1249
1250       exportf = fopen (export_file, "w");
1251       if (exportf == (FILE *) 0)
1252         fatal_perror ("fopen %s", export_file);
1253       write_aix_file (exportf, exports.first);
1254       if (fclose (exportf))
1255         fatal_perror ("fclose %s", export_file);
1256     }
1257 #endif
1258
1259   *c_ptr++ = c_file;
1260   *c_ptr = *ld1 = *object = (char *) 0;
1261
1262   if (vflag)
1263     {
1264       notice ("collect2 version %s", version_string);
1265 #ifdef TARGET_VERSION
1266       TARGET_VERSION;
1267 #endif
1268       fprintf (stderr, "\n");
1269     }
1270
1271   if (debug)
1272     {
1273       const char *ptr;
1274       fprintf (stderr, "ld_file_name        = %s\n",
1275                (ld_file_name ? ld_file_name : "not found"));
1276       fprintf (stderr, "c_file_name         = %s\n",
1277                (c_file_name ? c_file_name : "not found"));
1278       fprintf (stderr, "nm_file_name        = %s\n",
1279                (nm_file_name ? nm_file_name : "not found"));
1280 #ifdef LDD_SUFFIX
1281       fprintf (stderr, "ldd_file_name       = %s\n",
1282                (ldd_file_name ? ldd_file_name : "not found"));
1283 #endif
1284       fprintf (stderr, "strip_file_name     = %s\n",
1285                (strip_file_name ? strip_file_name : "not found"));
1286       fprintf (stderr, "c_file              = %s\n",
1287                (c_file ? c_file : "not found"));
1288       fprintf (stderr, "o_file              = %s\n",
1289                (o_file ? o_file : "not found"));
1290
1291       ptr = getenv ("COLLECT_GCC_OPTIONS");
1292       if (ptr)
1293         fprintf (stderr, "COLLECT_GCC_OPTIONS = %s\n", ptr);
1294
1295       ptr = getenv ("COLLECT_GCC");
1296       if (ptr)
1297         fprintf (stderr, "COLLECT_GCC         = %s\n", ptr);
1298
1299       ptr = getenv ("COMPILER_PATH");
1300       if (ptr)
1301         fprintf (stderr, "COMPILER_PATH       = %s\n", ptr);
1302
1303       ptr = getenv (LIBRARY_PATH_ENV);
1304       if (ptr)
1305         fprintf (stderr, "%-20s= %s\n", LIBRARY_PATH_ENV, ptr);
1306
1307       fprintf (stderr, "\n");
1308     }
1309
1310   /* Load the program, searching all libraries and attempting to provide
1311      undefined symbols from repository information.  */
1312
1313   /* On AIX we do this later.  */
1314 #ifndef COLLECT_EXPORT_LIST
1315   do_tlink (ld1_argv, object_lst);
1316 #endif
1317
1318   /* If -r or they will be run via some other method, do not build the
1319      constructor or destructor list, just return now.  */
1320   if (rflag
1321 #ifndef COLLECT_EXPORT_LIST
1322       || ! do_collecting
1323 #endif
1324       )
1325     {
1326 #ifdef COLLECT_EXPORT_LIST
1327       /* Do the link we avoided above if we are exiting.  */
1328       do_tlink (ld1_argv, object_lst);
1329
1330       /* But make sure we delete the export file we may have created.  */
1331       if (export_file != 0 && export_file[0])
1332         maybe_unlink (export_file);
1333 #endif
1334       maybe_unlink (c_file);
1335       maybe_unlink (o_file);
1336       return 0;
1337     }
1338
1339   /* Examine the namelist with nm and search it for static constructors
1340      and destructors to call.
1341      Write the constructor and destructor tables to a .s file and reload.  */
1342
1343   /* On AIX we already scanned for global constructors/destructors.  */
1344 #ifndef COLLECT_EXPORT_LIST
1345   scan_prog_file (output_file, PASS_FIRST);
1346 #endif
1347
1348 #ifdef SCAN_LIBRARIES
1349   scan_libraries (output_file);
1350 #endif
1351
1352   if (debug)
1353     {
1354       notice ("%d constructor(s) found\n", constructors.number);
1355       notice ("%d destructor(s)  found\n", destructors.number);
1356       notice ("%d frame table(s) found\n", frame_tables.number);
1357     }
1358
1359   if (constructors.number == 0 && destructors.number == 0
1360       && frame_tables.number == 0
1361 #if defined (SCAN_LIBRARIES) || defined (COLLECT_EXPORT_LIST)
1362       /* If we will be running these functions ourselves, we want to emit
1363          stubs into the shared library so that we do not have to relink
1364          dependent programs when we add static objects.  */
1365       && ! shared_obj
1366 #endif
1367       )
1368     {
1369 #ifdef COLLECT_EXPORT_LIST
1370       /* Do tlink without additional code generation.  */
1371       do_tlink (ld1_argv, object_lst);
1372 #endif
1373       /* Strip now if it was requested on the command line.  */
1374       if (strip_flag)
1375         {
1376           char **real_strip_argv = xcalloc (sizeof (char *), 3);
1377           const char ** strip_argv = (const char **) real_strip_argv;
1378
1379           strip_argv[0] = strip_file_name;
1380           strip_argv[1] = output_file;
1381           strip_argv[2] = (char *) 0;
1382           fork_execute ("strip", real_strip_argv);
1383         }
1384
1385 #ifdef COLLECT_EXPORT_LIST
1386       maybe_unlink (export_file);
1387 #endif
1388       maybe_unlink (c_file);
1389       maybe_unlink (o_file);
1390       return 0;
1391     }
1392
1393   /* Sort ctor and dtor lists by priority.  */
1394   sort_ids (&constructors);
1395   sort_ids (&destructors);
1396
1397   maybe_unlink(output_file);
1398   outf = fopen (c_file, "w");
1399   if (outf == (FILE *) 0)
1400     fatal_perror ("fopen %s", c_file);
1401
1402   write_c_file (outf, c_file);
1403
1404   if (fclose (outf))
1405     fatal_perror ("fclose %s", c_file);
1406
1407   /* Tell the linker that we have initializer and finalizer functions.  */
1408 #ifdef LD_INIT_SWITCH
1409 #ifdef COLLECT_EXPORT_LIST
1410   *ld2++ = concat (LD_INIT_SWITCH, ":", initname, ":", fininame, NULL);
1411 #else
1412   *ld2++ = LD_INIT_SWITCH;
1413   *ld2++ = initname;
1414   *ld2++ = LD_FINI_SWITCH;
1415   *ld2++ = fininame;
1416 #endif
1417 #endif
1418
1419 #ifdef COLLECT_EXPORT_LIST
1420   if (shared_obj)
1421     {
1422       /* If we did not add export flag to link arguments before, add it to
1423          second link phase now.  No new exports should have been added.  */
1424       if (! exports.first)
1425         *ld2++ = concat ("-bE:", export_file, NULL);
1426
1427 #ifndef LD_INIT_SWITCH
1428       add_to_list (&exports, initname);
1429       add_to_list (&exports, fininame);
1430       add_to_list (&exports, "_GLOBAL__DI");
1431       add_to_list (&exports, "_GLOBAL__DD");
1432 #endif
1433       exportf = fopen (export_file, "w");
1434       if (exportf == (FILE *) 0)
1435         fatal_perror ("fopen %s", export_file);
1436       write_aix_file (exportf, exports.first);
1437       if (fclose (exportf))
1438         fatal_perror ("fclose %s", export_file);
1439     }
1440 #endif
1441
1442   /* End of arguments to second link phase.  */
1443   *ld2 = (char*) 0;
1444
1445   if (debug)
1446     {
1447       fprintf (stderr, "\n========== output_file = %s, c_file = %s\n",
1448                output_file, c_file);
1449       write_c_file (stderr, "stderr");
1450       fprintf (stderr, "========== end of c_file\n\n");
1451 #ifdef COLLECT_EXPORT_LIST
1452       fprintf (stderr, "\n========== export_file = %s\n", export_file);
1453       write_aix_file (stderr, exports.first);
1454       fprintf (stderr, "========== end of export_file\n\n");
1455 #endif
1456     }
1457
1458   /* Assemble the constructor and destructor tables.
1459      Link the tables in with the rest of the program.  */
1460
1461   fork_execute ("gcc",  c_argv);
1462 #ifdef COLLECT_EXPORT_LIST
1463   /* On AIX we must call tlink because of possible templates resolution.  */
1464   do_tlink (ld2_argv, object_lst);
1465 #else
1466   /* Otherwise, simply call ld because tlink is already done.  */
1467   fork_execute ("ld", ld2_argv);
1468
1469   /* Let scan_prog_file do any final mods (OSF/rose needs this for
1470      constructors/destructors in shared libraries.  */
1471   scan_prog_file (output_file, PASS_SECOND);
1472 #endif
1473
1474   maybe_unlink (c_file);
1475   maybe_unlink (o_file);
1476
1477 #ifdef COLLECT_EXPORT_LIST
1478   maybe_unlink (export_file);
1479 #endif
1480
1481   return 0;
1482 }
1483
1484 \f
1485 /* Wait for a process to finish, and exit if a nonzero status is found.  */
1486
1487 int
1488 collect_wait (const char *prog, struct pex_obj *pex)
1489 {
1490   int status;
1491
1492   if (!pex_get_status (pex, 1, &status))
1493     fatal_perror ("can't get program status");
1494   pex_free (pex);
1495
1496   if (status)
1497     {
1498       if (WIFSIGNALED (status))
1499         {
1500           int sig = WTERMSIG (status);
1501           error ("%s terminated with signal %d [%s]%s",
1502                  prog, sig, strsignal(sig),
1503                  WCOREDUMP(status) ? ", core dumped" : "");
1504           collect_exit (FATAL_EXIT_CODE);
1505         }
1506
1507       if (WIFEXITED (status))
1508         return WEXITSTATUS (status);
1509     }
1510   return 0;
1511 }
1512
1513 static void
1514 do_wait (const char *prog, struct pex_obj *pex)
1515 {
1516   int ret = collect_wait (prog, pex);
1517   if (ret != 0)
1518     {
1519       error ("%s returned %d exit status", prog, ret);
1520       collect_exit (ret);
1521     }
1522 }
1523
1524 \f
1525 /* Execute a program, and wait for the reply.  */
1526
1527 struct pex_obj *
1528 collect_execute (const char *prog, char **argv, const char *redir)
1529 {
1530   struct pex_obj *pex;
1531   const char *errmsg;
1532   int err;
1533
1534   if (vflag || debug)
1535     {
1536       char **p_argv;
1537       const char *str;
1538
1539       if (argv[0])
1540         fprintf (stderr, "%s", argv[0]);
1541       else
1542         notice ("[cannot find %s]", prog);
1543
1544       for (p_argv = &argv[1]; (str = *p_argv) != (char *) 0; p_argv++)
1545         fprintf (stderr, " %s", str);
1546
1547       fprintf (stderr, "\n");
1548     }
1549
1550   fflush (stdout);
1551   fflush (stderr);
1552
1553   /* If we cannot find a program we need, complain error.  Do this here
1554      since we might not end up needing something that we could not find.  */
1555
1556   if (argv[0] == 0)
1557     fatal ("cannot find '%s'", prog);
1558
1559   pex = pex_init (0, "collect2", NULL);
1560   if (pex == NULL)
1561     fatal_perror ("pex_init failed");
1562
1563   errmsg = pex_run (pex,
1564                     (PEX_LAST | PEX_SEARCH
1565                      | (redir ? PEX_STDERR_TO_STDOUT : 0)),
1566                     argv[0], argv, redir, NULL, &err);
1567   if (errmsg != NULL)
1568     {
1569       if (err != 0)
1570         {
1571           errno = err;
1572           fatal_perror (errmsg);
1573         }
1574       else
1575         fatal (errmsg);
1576     }
1577
1578   return pex;
1579 }
1580
1581 static void
1582 fork_execute (const char *prog, char **argv)
1583 {
1584   struct pex_obj *pex;
1585
1586   pex = collect_execute (prog, argv, NULL);
1587   do_wait (prog, pex);
1588 }
1589 \f
1590 /* Unlink a file unless we are debugging.  */
1591
1592 static void
1593 maybe_unlink (const char *file)
1594 {
1595   if (!debug)
1596     unlink (file);
1597   else
1598     notice ("[Leaving %s]\n", file);
1599 }
1600
1601 \f
1602 static long sequence_number = 0;
1603
1604 /* Add a name to a linked list.  */
1605
1606 static void
1607 add_to_list (struct head *head_ptr, const char *name)
1608 {
1609   struct id *newid = xcalloc (sizeof (struct id) + strlen (name), 1);
1610   struct id *p;
1611   strcpy (newid->name, name);
1612
1613   if (head_ptr->first)
1614     head_ptr->last->next = newid;
1615   else
1616     head_ptr->first = newid;
1617
1618   /* Check for duplicate symbols.  */
1619   for (p = head_ptr->first;
1620        strcmp (name, p->name) != 0;
1621        p = p->next)
1622     ;
1623   if (p != newid)
1624     {
1625       head_ptr->last->next = 0;
1626       free (newid);
1627       return;
1628     }
1629
1630   newid->sequence = ++sequence_number;
1631   head_ptr->last = newid;
1632   head_ptr->number++;
1633 }
1634
1635 /* Grab the init priority number from an init function name that
1636    looks like "_GLOBAL_.I.12345.foo".  */
1637
1638 static int
1639 extract_init_priority (const char *name)
1640 {
1641   int pos = 0, pri;
1642
1643   while (name[pos] == '_')
1644     ++pos;
1645   pos += 10; /* strlen ("GLOBAL__X_") */
1646
1647   /* Extract init_p number from ctor/dtor name.  */
1648   pri = atoi (name + pos);
1649   return pri ? pri : DEFAULT_INIT_PRIORITY;
1650 }
1651
1652 /* Insertion sort the ids from ctor/dtor list HEAD_PTR in descending order.
1653    ctors will be run from right to left, dtors from left to right.  */
1654
1655 static void
1656 sort_ids (struct head *head_ptr)
1657 {
1658   /* id holds the current element to insert.  id_next holds the next
1659      element to insert.  id_ptr iterates through the already sorted elements
1660      looking for the place to insert id.  */
1661   struct id *id, *id_next, **id_ptr;
1662
1663   id = head_ptr->first;
1664
1665   /* We don't have any sorted elements yet.  */
1666   head_ptr->first = NULL;
1667
1668   for (; id; id = id_next)
1669     {
1670       id_next = id->next;
1671       id->sequence = extract_init_priority (id->name);
1672
1673       for (id_ptr = &(head_ptr->first); ; id_ptr = &((*id_ptr)->next))
1674         if (*id_ptr == NULL
1675             /* If the sequence numbers are the same, we put the id from the
1676                file later on the command line later in the list.  */
1677             || id->sequence > (*id_ptr)->sequence
1678             /* Hack: do lexical compare, too.
1679             || (id->sequence == (*id_ptr)->sequence
1680                 && strcmp (id->name, (*id_ptr)->name) > 0) */
1681             )
1682           {
1683             id->next = *id_ptr;
1684             *id_ptr = id;
1685             break;
1686           }
1687     }
1688
1689   /* Now set the sequence numbers properly so write_c_file works.  */
1690   for (id = head_ptr->first; id; id = id->next)
1691     id->sequence = ++sequence_number;
1692 }
1693
1694 /* Write: `prefix', the names on list LIST, `suffix'.  */
1695
1696 static void
1697 write_list (FILE *stream, const char *prefix, struct id *list)
1698 {
1699   while (list)
1700     {
1701       fprintf (stream, "%sx%d,\n", prefix, list->sequence);
1702       list = list->next;
1703     }
1704 }
1705
1706 #if LINK_ELIMINATE_DUPLICATE_LDIRECTORIES
1707 /* Given a STRING, return nonzero if it occurs in the list in range
1708    [ARGS_BEGIN,ARGS_END).  */
1709
1710 static int
1711 is_in_args (const char *string, const char **args_begin,
1712             const char **args_end)
1713 {
1714   const char **args_pointer;
1715   for (args_pointer = args_begin; args_pointer != args_end; ++args_pointer)
1716     if (strcmp (string, *args_pointer) == 0)
1717       return 1;
1718   return 0;
1719 }
1720 #endif /* LINK_ELIMINATE_DUPLICATE_LDIRECTORIES */
1721
1722 #ifdef COLLECT_EXPORT_LIST
1723 /* This function is really used only on AIX, but may be useful.  */
1724 #if 0
1725 static int
1726 is_in_list (const char *prefix, struct id *list)
1727 {
1728   while (list)
1729     {
1730       if (!strcmp (prefix, list->name)) return 1;
1731       list = list->next;
1732     }
1733     return 0;
1734 }
1735 #endif
1736 #endif /* COLLECT_EXPORT_LIST */
1737
1738 /* Added for debugging purpose.  */
1739 #ifdef COLLECT_EXPORT_LIST
1740 static void
1741 dump_list (FILE *stream, const char *prefix, struct id *list)
1742 {
1743   while (list)
1744     {
1745       fprintf (stream, "%s%s,\n", prefix, list->name);
1746       list = list->next;
1747     }
1748 }
1749 #endif
1750
1751 #if 0
1752 static void
1753 dump_prefix_list (FILE *stream, const char *prefix, struct prefix_list *list)
1754 {
1755   while (list)
1756     {
1757       fprintf (stream, "%s%s,\n", prefix, list->prefix);
1758       list = list->next;
1759     }
1760 }
1761 #endif
1762
1763 static void
1764 write_list_with_asm (FILE *stream, const char *prefix, struct id *list)
1765 {
1766   while (list)
1767     {
1768       fprintf (stream, "%sx%d __asm__ (\"%s\");\n",
1769                prefix, list->sequence, list->name);
1770       list = list->next;
1771     }
1772 }
1773
1774 /* Write out the constructor and destructor tables statically (for a shared
1775    object), along with the functions to execute them.  */
1776
1777 static void
1778 write_c_file_stat (FILE *stream, const char *name ATTRIBUTE_UNUSED)
1779 {
1780   const char *p, *q;
1781   char *prefix, *r;
1782   int frames = (frame_tables.number > 0);
1783
1784   /* Figure out name of output_file, stripping off .so version.  */
1785   p = strrchr (output_file, '/');
1786   if (p == 0)
1787     p = output_file;
1788   else
1789     p++;
1790   q = p;
1791   while (q)
1792     {
1793       q = strchr (q,'.');
1794       if (q == 0)
1795         {
1796           q = p + strlen (p);
1797           break;
1798         }
1799       else
1800         {
1801           if (strncmp (q, ".so", 3) == 0)
1802             {
1803               q += 3;
1804               break;
1805             }
1806           else
1807             q++;
1808         }
1809     }
1810   /* q points to null at end of the string (or . of the .so version) */
1811   prefix = xmalloc (q - p + 1);
1812   strncpy (prefix, p, q - p);
1813   prefix[q - p] = 0;
1814   for (r = prefix; *r; r++)
1815     if (!ISALNUM ((unsigned char)*r))
1816       *r = '_';
1817   if (debug)
1818     notice ("\nwrite_c_file - output name is %s, prefix is %s\n",
1819             output_file, prefix);
1820
1821   initname = concat ("_GLOBAL__FI_", prefix, NULL);
1822   fininame = concat ("_GLOBAL__FD_", prefix, NULL);
1823
1824   free (prefix);
1825
1826   /* Write the tables as C code.  */
1827
1828   fprintf (stream, "static int count;\n");
1829   fprintf (stream, "typedef void entry_pt();\n");
1830   write_list_with_asm (stream, "extern entry_pt ", constructors.first);
1831
1832   if (frames)
1833     {
1834       write_list_with_asm (stream, "extern void *", frame_tables.first);
1835
1836       fprintf (stream, "\tstatic void *frame_table[] = {\n");
1837       write_list (stream, "\t\t&", frame_tables.first);
1838       fprintf (stream, "\t0\n};\n");
1839
1840       /* This must match what's in frame.h.  */
1841       fprintf (stream, "struct object {\n");
1842       fprintf (stream, "  void *pc_begin;\n");
1843       fprintf (stream, "  void *pc_end;\n");
1844       fprintf (stream, "  void *fde_begin;\n");
1845       fprintf (stream, "  void *fde_array;\n");
1846       fprintf (stream, "  __SIZE_TYPE__ count;\n");
1847       fprintf (stream, "  struct object *next;\n");
1848       fprintf (stream, "};\n");
1849
1850       fprintf (stream, "extern void __register_frame_info_table (void *, struct object *);\n");
1851       fprintf (stream, "extern void *__deregister_frame_info (void *);\n");
1852
1853       fprintf (stream, "static void reg_frame () {\n");
1854       fprintf (stream, "\tstatic struct object ob;\n");
1855       fprintf (stream, "\t__register_frame_info_table (frame_table, &ob);\n");
1856       fprintf (stream, "\t}\n");
1857
1858       fprintf (stream, "static void dereg_frame () {\n");
1859       fprintf (stream, "\t__deregister_frame_info (frame_table);\n");
1860       fprintf (stream, "\t}\n");
1861     }
1862
1863   fprintf (stream, "void %s() {\n", initname);
1864   if (constructors.number > 0 || frames)
1865     {
1866       fprintf (stream, "\tstatic entry_pt *ctors[] = {\n");
1867       write_list (stream, "\t\t", constructors.first);
1868       if (frames)
1869         fprintf (stream, "\treg_frame,\n");
1870       fprintf (stream, "\t};\n");
1871       fprintf (stream, "\tentry_pt **p;\n");
1872       fprintf (stream, "\tif (count++ != 0) return;\n");
1873       fprintf (stream, "\tp = ctors + %d;\n", constructors.number + frames);
1874       fprintf (stream, "\twhile (p > ctors) (*--p)();\n");
1875     }
1876   else
1877     fprintf (stream, "\t++count;\n");
1878   fprintf (stream, "}\n");
1879   write_list_with_asm (stream, "extern entry_pt ", destructors.first);
1880   fprintf (stream, "void %s() {\n", fininame);
1881   if (destructors.number > 0 || frames)
1882     {
1883       fprintf (stream, "\tstatic entry_pt *dtors[] = {\n");
1884       write_list (stream, "\t\t", destructors.first);
1885       if (frames)
1886         fprintf (stream, "\tdereg_frame,\n");
1887       fprintf (stream, "\t};\n");
1888       fprintf (stream, "\tentry_pt **p;\n");
1889       fprintf (stream, "\tif (--count != 0) return;\n");
1890       fprintf (stream, "\tp = dtors;\n");
1891       fprintf (stream, "\twhile (p < dtors + %d) (*p++)();\n",
1892                destructors.number + frames);
1893     }
1894   fprintf (stream, "}\n");
1895
1896   if (shared_obj)
1897     {
1898       COLLECT_SHARED_INIT_FUNC(stream, initname);
1899       COLLECT_SHARED_FINI_FUNC(stream, fininame);
1900     }
1901 }
1902
1903 /* Write the constructor/destructor tables.  */
1904
1905 #ifndef LD_INIT_SWITCH
1906 static void
1907 write_c_file_glob (FILE *stream, const char *name ATTRIBUTE_UNUSED)
1908 {
1909   /* Write the tables as C code.  */
1910
1911   int frames = (frame_tables.number > 0);
1912
1913   fprintf (stream, "typedef void entry_pt();\n\n");
1914
1915   write_list_with_asm (stream, "extern entry_pt ", constructors.first);
1916
1917   if (frames)
1918     {
1919       write_list_with_asm (stream, "extern void *", frame_tables.first);
1920
1921       fprintf (stream, "\tstatic void *frame_table[] = {\n");
1922       write_list (stream, "\t\t&", frame_tables.first);
1923       fprintf (stream, "\t0\n};\n");
1924
1925       /* This must match what's in frame.h.  */
1926       fprintf (stream, "struct object {\n");
1927       fprintf (stream, "  void *pc_begin;\n");
1928       fprintf (stream, "  void *pc_end;\n");
1929       fprintf (stream, "  void *fde_begin;\n");
1930       fprintf (stream, "  void *fde_array;\n");
1931       fprintf (stream, "  __SIZE_TYPE__ count;\n");
1932       fprintf (stream, "  struct object *next;\n");
1933       fprintf (stream, "};\n");
1934
1935       fprintf (stream, "extern void __register_frame_info_table (void *, struct object *);\n");
1936       fprintf (stream, "extern void *__deregister_frame_info (void *);\n");
1937
1938       fprintf (stream, "static void reg_frame () {\n");
1939       fprintf (stream, "\tstatic struct object ob;\n");
1940       fprintf (stream, "\t__register_frame_info_table (frame_table, &ob);\n");
1941       fprintf (stream, "\t}\n");
1942
1943       fprintf (stream, "static void dereg_frame () {\n");
1944       fprintf (stream, "\t__deregister_frame_info (frame_table);\n");
1945       fprintf (stream, "\t}\n");
1946     }
1947
1948   fprintf (stream, "\nentry_pt * __CTOR_LIST__[] = {\n");
1949   fprintf (stream, "\t(entry_pt *) %d,\n", constructors.number + frames);
1950   write_list (stream, "\t", constructors.first);
1951   if (frames)
1952     fprintf (stream, "\treg_frame,\n");
1953   fprintf (stream, "\t0\n};\n\n");
1954
1955   write_list_with_asm (stream, "extern entry_pt ", destructors.first);
1956
1957   fprintf (stream, "\nentry_pt * __DTOR_LIST__[] = {\n");
1958   fprintf (stream, "\t(entry_pt *) %d,\n", destructors.number + frames);
1959   write_list (stream, "\t", destructors.first);
1960   if (frames)
1961     fprintf (stream, "\tdereg_frame,\n");
1962   fprintf (stream, "\t0\n};\n\n");
1963
1964   fprintf (stream, "extern entry_pt %s;\n", NAME__MAIN);
1965   fprintf (stream, "entry_pt *__main_reference = %s;\n\n", NAME__MAIN);
1966 }
1967 #endif /* ! LD_INIT_SWITCH */
1968
1969 static void
1970 write_c_file (FILE *stream, const char *name)
1971 {
1972   fprintf (stream, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n");
1973 #ifndef LD_INIT_SWITCH
1974   if (! shared_obj)
1975     write_c_file_glob (stream, name);
1976   else
1977 #endif
1978     write_c_file_stat (stream, name);
1979   fprintf (stream, "#ifdef __cplusplus\n}\n#endif\n");
1980 }
1981
1982 #ifdef COLLECT_EXPORT_LIST
1983 static void
1984 write_aix_file (FILE *stream, struct id *list)
1985 {
1986   for (; list; list = list->next)
1987     {
1988       fputs (list->name, stream);
1989       putc ('\n', stream);
1990     }
1991 }
1992 #endif
1993 \f
1994 #ifdef OBJECT_FORMAT_NONE
1995
1996 /* Generic version to scan the name list of the loaded program for
1997    the symbols g++ uses for static constructors and destructors.
1998
1999    The constructor table begins at __CTOR_LIST__ and contains a count
2000    of the number of pointers (or -1 if the constructors are built in a
2001    separate section by the linker), followed by the pointers to the
2002    constructor functions, terminated with a null pointer.  The
2003    destructor table has the same format, and begins at __DTOR_LIST__.  */
2004
2005 static void
2006 scan_prog_file (const char *prog_name, enum pass which_pass)
2007 {
2008   void (*int_handler) (int);
2009 #ifdef SIGQUIT
2010   void (*quit_handler) (int);
2011 #endif
2012   char *real_nm_argv[4];
2013   const char **nm_argv = (const char **) real_nm_argv;
2014   int argc = 0;
2015   struct pex_obj *pex;
2016   const char *errmsg;
2017   int err;
2018   char *p, buf[1024];
2019   FILE *inf;
2020
2021   if (which_pass == PASS_SECOND)
2022     return;
2023
2024   /* If we do not have an `nm', complain.  */
2025   if (nm_file_name == 0)
2026     fatal ("cannot find 'nm'");
2027
2028   nm_argv[argc++] = nm_file_name;
2029   if (NM_FLAGS[0] != '\0')
2030     nm_argv[argc++] = NM_FLAGS;
2031
2032   nm_argv[argc++] = prog_name;
2033   nm_argv[argc++] = (char *) 0;
2034
2035   /* Trace if needed.  */
2036   if (vflag)
2037     {
2038       const char **p_argv;
2039       const char *str;
2040
2041       for (p_argv = &nm_argv[0]; (str = *p_argv) != (char *) 0; p_argv++)
2042         fprintf (stderr, " %s", str);
2043
2044       fprintf (stderr, "\n");
2045     }
2046
2047   fflush (stdout);
2048   fflush (stderr);
2049
2050   pex = pex_init (PEX_USE_PIPES, "collect2", NULL);
2051   if (pex == NULL)
2052     fatal_perror ("pex_init failed");
2053
2054   errmsg = pex_run (pex, 0, nm_file_name, real_nm_argv, NULL, NULL, &err);
2055   if (errmsg != NULL)
2056     {
2057       if (err != 0)
2058         {
2059           errno = err;
2060           fatal_perror (errmsg);
2061         }
2062       else
2063         fatal (errmsg);
2064     }
2065
2066   int_handler  = (void (*) (int)) signal (SIGINT,  SIG_IGN);
2067 #ifdef SIGQUIT
2068   quit_handler = (void (*) (int)) signal (SIGQUIT, SIG_IGN);
2069 #endif
2070
2071   inf = pex_read_output (pex, 0);
2072   if (inf == NULL)
2073     fatal_perror ("can't open nm output");
2074
2075   if (debug)
2076     fprintf (stderr, "\nnm output with constructors/destructors.\n");
2077
2078   /* Read each line of nm output.  */
2079   while (fgets (buf, sizeof buf, inf) != (char *) 0)
2080     {
2081       int ch, ch2;
2082       char *name, *end;
2083
2084       /* If it contains a constructor or destructor name, add the name
2085          to the appropriate list.  */
2086
2087       for (p = buf; (ch = *p) != '\0' && ch != '\n' && ch != '_'; p++)
2088         if (ch == ' ' && p[1] == 'U' && p[2] == ' ')
2089           break;
2090
2091       if (ch != '_')
2092         continue;
2093
2094       name = p;
2095       /* Find the end of the symbol name.
2096          Do not include `|', because Encore nm can tack that on the end.  */
2097       for (end = p; (ch2 = *end) != '\0' && !ISSPACE (ch2) && ch2 != '|';
2098            end++)
2099         continue;
2100
2101
2102       *end = '\0';
2103       switch (is_ctor_dtor (name))
2104         {
2105         case 1:
2106           if (which_pass != PASS_LIB)
2107             add_to_list (&constructors, name);
2108           break;
2109
2110         case 2:
2111           if (which_pass != PASS_LIB)
2112             add_to_list (&destructors, name);
2113           break;
2114
2115         case 3:
2116           if (which_pass != PASS_LIB)
2117             fatal ("init function found in object %s", prog_name);
2118 #ifndef LD_INIT_SWITCH
2119           add_to_list (&constructors, name);
2120 #endif
2121           break;
2122
2123         case 4:
2124           if (which_pass != PASS_LIB)
2125             fatal ("fini function found in object %s", prog_name);
2126 #ifndef LD_FINI_SWITCH
2127           add_to_list (&destructors, name);
2128 #endif
2129           break;
2130
2131         case 5:
2132           if (which_pass != PASS_LIB)
2133             add_to_list (&frame_tables, name);
2134           break;
2135
2136         default:                /* not a constructor or destructor */
2137           continue;
2138         }
2139
2140       if (debug)
2141         fprintf (stderr, "\t%s\n", buf);
2142     }
2143
2144   if (debug)
2145     fprintf (stderr, "\n");
2146
2147   do_wait (nm_file_name, pex);
2148
2149   signal (SIGINT,  int_handler);
2150 #ifdef SIGQUIT
2151   signal (SIGQUIT, quit_handler);
2152 #endif
2153 }
2154
2155 #ifdef LDD_SUFFIX
2156
2157 /* Use the List Dynamic Dependencies program to find shared libraries that
2158    the output file depends upon and their initialization/finalization
2159    routines, if any.  */
2160
2161 static void
2162 scan_libraries (const char *prog_name)
2163 {
2164   static struct head libraries;         /* list of shared libraries found */
2165   struct id *list;
2166   void (*int_handler) (int);
2167 #ifdef SIGQUIT
2168   void (*quit_handler) (int);
2169 #endif
2170   char *real_ldd_argv[4];
2171   const char **ldd_argv = (const char **) real_ldd_argv;
2172   int argc = 0;
2173   struct pex_obj *pex;
2174   const char *errmsg;
2175   int err;
2176   char buf[1024];
2177   FILE *inf;
2178
2179   /* If we do not have an `ldd', complain.  */
2180   if (ldd_file_name == 0)
2181     {
2182       error ("cannot find 'ldd'");
2183       return;
2184     }
2185
2186   ldd_argv[argc++] = ldd_file_name;
2187   ldd_argv[argc++] = prog_name;
2188   ldd_argv[argc++] = (char *) 0;
2189
2190   /* Trace if needed.  */
2191   if (vflag)
2192     {
2193       const char **p_argv;
2194       const char *str;
2195
2196       for (p_argv = &ldd_argv[0]; (str = *p_argv) != (char *) 0; p_argv++)
2197         fprintf (stderr, " %s", str);
2198
2199       fprintf (stderr, "\n");
2200     }
2201
2202   fflush (stdout);
2203   fflush (stderr);
2204
2205   pex = pex_init (PEX_USE_PIPES, "collect2", NULL);
2206   if (pex == NULL)
2207     fatal_perror ("pex_init failed");
2208
2209   errmsg = pex_run (pex, 0, ldd_file_name, real_ldd_argv, NULL, NULL, &err);
2210   if (errmsg != NULL)
2211     {
2212       if (err != 0)
2213         {
2214           errno = err;
2215           fatal_perror (errmsg);
2216         }
2217       else
2218         fatal (errmsg);
2219     }
2220
2221   int_handler  = (void (*) (int)) signal (SIGINT,  SIG_IGN);
2222 #ifdef SIGQUIT
2223   quit_handler = (void (*) (int)) signal (SIGQUIT, SIG_IGN);
2224 #endif
2225
2226   inf = pex_read_output (pex, 0);
2227   if (inf == NULL)
2228     fatal_perror ("can't open ldd output");
2229
2230   if (debug)
2231     notice ("\nldd output with constructors/destructors.\n");
2232
2233   /* Read each line of ldd output.  */
2234   while (fgets (buf, sizeof buf, inf) != (char *) 0)
2235     {
2236       int ch2;
2237       char *name, *end, *p = buf;
2238
2239       /* Extract names of libraries and add to list.  */
2240       PARSE_LDD_OUTPUT (p);
2241       if (p == 0)
2242         continue;
2243
2244       name = p;
2245       if (strncmp (name, "not found", sizeof ("not found") - 1) == 0)
2246         fatal ("dynamic dependency %s not found", buf);
2247
2248       /* Find the end of the symbol name.  */
2249       for (end = p;
2250            (ch2 = *end) != '\0' && ch2 != '\n' && !ISSPACE (ch2) && ch2 != '|';
2251            end++)
2252         continue;
2253       *end = '\0';
2254
2255       if (access (name, R_OK) == 0)
2256         add_to_list (&libraries, name);
2257       else
2258         fatal ("unable to open dynamic dependency '%s'", buf);
2259
2260       if (debug)
2261         fprintf (stderr, "\t%s\n", buf);
2262     }
2263   if (debug)
2264     fprintf (stderr, "\n");
2265
2266   do_wait (ldd_file_name, pex);
2267
2268   signal (SIGINT,  int_handler);
2269 #ifdef SIGQUIT
2270   signal (SIGQUIT, quit_handler);
2271 #endif
2272
2273   /* Now iterate through the library list adding their symbols to
2274      the list.  */
2275   for (list = libraries.first; list; list = list->next)
2276     scan_prog_file (list->name, PASS_LIB);
2277 }
2278
2279 #endif /* LDD_SUFFIX */
2280
2281 #endif /* OBJECT_FORMAT_NONE */
2282
2283 \f
2284 /*
2285  * COFF specific stuff.
2286  */
2287
2288 #ifdef OBJECT_FORMAT_COFF
2289
2290 #if defined (EXTENDED_COFF)
2291
2292 #   define GCC_SYMBOLS(X)       (SYMHEADER(X).isymMax + SYMHEADER(X).iextMax)
2293 #   define GCC_SYMENT           SYMR
2294 #   define GCC_OK_SYMBOL(X)     ((X).st == stProc || (X).st == stGlobal)
2295 #   define GCC_SYMINC(X)        (1)
2296 #   define GCC_SYMZERO(X)       (SYMHEADER(X).isymMax)
2297 #   define GCC_CHECK_HDR(X)     (PSYMTAB(X) != 0)
2298
2299 #else
2300
2301 #   define GCC_SYMBOLS(X)       (HEADER(ldptr).f_nsyms)
2302 #   define GCC_SYMENT           SYMENT
2303 #   if defined (C_WEAKEXT)
2304 #     define GCC_OK_SYMBOL(X) \
2305        (((X).n_sclass == C_EXT || (X).n_sclass == C_WEAKEXT) && \
2306         ((X).n_scnum > N_UNDEF) && \
2307         (aix64_flag \
2308          || (((X).n_type & N_TMASK) == (DT_NON << N_BTSHFT) \
2309              || ((X).n_type & N_TMASK) == (DT_FCN << N_BTSHFT))))
2310 #     define GCC_UNDEF_SYMBOL(X) \
2311        (((X).n_sclass == C_EXT || (X).n_sclass == C_WEAKEXT) && \
2312         ((X).n_scnum == N_UNDEF))
2313 #   else
2314 #     define GCC_OK_SYMBOL(X) \
2315        (((X).n_sclass == C_EXT) && \
2316         ((X).n_scnum > N_UNDEF) && \
2317         (aix64_flag \
2318          || (((X).n_type & N_TMASK) == (DT_NON << N_BTSHFT) \
2319              || ((X).n_type & N_TMASK) == (DT_FCN << N_BTSHFT))))
2320 #     define GCC_UNDEF_SYMBOL(X) \
2321        (((X).n_sclass == C_EXT) && ((X).n_scnum == N_UNDEF))
2322 #   endif
2323 #   define GCC_SYMINC(X)        ((X).n_numaux+1)
2324 #   define GCC_SYMZERO(X)       0
2325
2326 /* 0757 = U803XTOCMAGIC (AIX 4.3) and 0767 = U64_TOCMAGIC (AIX V5) */
2327 #ifdef _AIX51
2328 #   define GCC_CHECK_HDR(X) \
2329      ((HEADER (X).f_magic == U802TOCMAGIC && ! aix64_flag) \
2330       || (HEADER (X).f_magic == 0767 && aix64_flag))
2331 #else
2332 #   define GCC_CHECK_HDR(X) \
2333      ((HEADER (X).f_magic == U802TOCMAGIC && ! aix64_flag) \
2334       || (HEADER (X).f_magic == 0757 && aix64_flag))
2335 #endif
2336
2337 #endif
2338
2339 #ifdef COLLECT_EXPORT_LIST
2340 /* Array of standard AIX libraries which should not
2341    be scanned for ctors/dtors.  */
2342 static const char *const aix_std_libs[] = {
2343   "/unix",
2344   "/lib/libc.a",
2345   "/lib/libm.a",
2346   "/lib/libc_r.a",
2347   "/lib/libm_r.a",
2348   "/usr/lib/libc.a",
2349   "/usr/lib/libm.a",
2350   "/usr/lib/libc_r.a",
2351   "/usr/lib/libm_r.a",
2352   "/usr/lib/threads/libc.a",
2353   "/usr/ccs/lib/libc.a",
2354   "/usr/ccs/lib/libm.a",
2355   "/usr/ccs/lib/libc_r.a",
2356   "/usr/ccs/lib/libm_r.a",
2357   NULL
2358 };
2359
2360 /* This function checks the filename and returns 1
2361    if this name matches the location of a standard AIX library.  */
2362 static int ignore_library (const char *);
2363 static int
2364 ignore_library (const char *name)
2365 {
2366   const char *const *p = &aix_std_libs[0];
2367   while (*p++ != NULL)
2368     if (! strcmp (name, *p)) return 1;
2369   return 0;
2370 }
2371 #endif /* COLLECT_EXPORT_LIST */
2372
2373 #if defined (HAVE_DECL_LDGETNAME) && !HAVE_DECL_LDGETNAME
2374 extern char *ldgetname (LDFILE *, GCC_SYMENT *);
2375 #endif
2376
2377 /* COFF version to scan the name list of the loaded program for
2378    the symbols g++ uses for static constructors and destructors.
2379
2380    The constructor table begins at __CTOR_LIST__ and contains a count
2381    of the number of pointers (or -1 if the constructors are built in a
2382    separate section by the linker), followed by the pointers to the
2383    constructor functions, terminated with a null pointer.  The
2384    destructor table has the same format, and begins at __DTOR_LIST__.  */
2385
2386 static void
2387 scan_prog_file (const char *prog_name, enum pass which_pass)
2388 {
2389   LDFILE *ldptr = NULL;
2390   int sym_index, sym_count;
2391   int is_shared = 0;
2392
2393   if (which_pass != PASS_FIRST && which_pass != PASS_OBJ)
2394     return;
2395
2396 #ifdef COLLECT_EXPORT_LIST
2397   /* We do not need scanning for some standard C libraries.  */
2398   if (which_pass == PASS_FIRST && ignore_library (prog_name))
2399     return;
2400
2401   /* On AIX we have a loop, because there is not much difference
2402      between an object and an archive. This trick allows us to
2403      eliminate scan_libraries() function.  */
2404   do
2405     {
2406 #endif
2407       /* Some platforms (e.g. OSF4) declare ldopen as taking a
2408          non-const char * filename parameter, even though it will not
2409          modify that string.  So we must cast away const-ness here,
2410          which will cause -Wcast-qual to burp.  */
2411       if ((ldptr = ldopen ((char *)prog_name, ldptr)) != NULL)
2412         {
2413           if (! MY_ISCOFF (HEADER (ldptr).f_magic))
2414             fatal ("%s: not a COFF file", prog_name);
2415
2416           if (GCC_CHECK_HDR (ldptr))
2417             {
2418               sym_count = GCC_SYMBOLS (ldptr);
2419               sym_index = GCC_SYMZERO (ldptr);
2420
2421 #ifdef COLLECT_EXPORT_LIST
2422               /* Is current archive member a shared object?  */
2423               is_shared = HEADER (ldptr).f_flags & F_SHROBJ;
2424 #endif
2425
2426               while (sym_index < sym_count)
2427                 {
2428                   GCC_SYMENT symbol;
2429
2430                   if (ldtbread (ldptr, sym_index, &symbol) <= 0)
2431                     break;
2432                   sym_index += GCC_SYMINC (symbol);
2433
2434                   if (GCC_OK_SYMBOL (symbol))
2435                     {
2436                       char *name;
2437
2438                       if ((name = ldgetname (ldptr, &symbol)) == NULL)
2439                         continue;               /* Should never happen.  */
2440
2441 #ifdef XCOFF_DEBUGGING_INFO
2442                       /* All AIX function names have a duplicate entry
2443                          beginning with a dot.  */
2444                       if (*name == '.')
2445                         ++name;
2446 #endif
2447
2448                       switch (is_ctor_dtor (name))
2449                         {
2450                         case 1:
2451                           if (! is_shared)
2452                             add_to_list (&constructors, name);
2453 #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
2454                           if (which_pass == PASS_OBJ)
2455                             add_to_list (&exports, name);
2456 #endif
2457                           break;
2458
2459                         case 2:
2460                           if (! is_shared)
2461                             add_to_list (&destructors, name);
2462 #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
2463                           if (which_pass == PASS_OBJ)
2464                             add_to_list (&exports, name);
2465 #endif
2466                           break;
2467
2468 #ifdef COLLECT_EXPORT_LIST
2469                         case 3:
2470 #ifndef LD_INIT_SWITCH
2471                           if (is_shared)
2472                             add_to_list (&constructors, name);
2473 #endif
2474                           break;
2475
2476                         case 4:
2477 #ifndef LD_INIT_SWITCH
2478                           if (is_shared)
2479                             add_to_list (&destructors, name);
2480 #endif
2481                           break;
2482 #endif
2483
2484                         case 5:
2485                           if (! is_shared)
2486                             add_to_list (&frame_tables, name);
2487 #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
2488                           if (which_pass == PASS_OBJ)
2489                             add_to_list (&exports, name);
2490 #endif
2491                           break;
2492
2493                         default:        /* not a constructor or destructor */
2494 #ifdef COLLECT_EXPORT_LIST
2495                           /* Explicitly export all global symbols when
2496                              building a shared object on AIX, but do not
2497                              re-export symbols from another shared object
2498                              and do not export symbols if the user
2499                              provides an explicit export list.  */
2500                           if (shared_obj && !is_shared
2501                               && which_pass == PASS_OBJ && !export_flag)
2502                             add_to_list (&exports, name);
2503 #endif
2504                           continue;
2505                         }
2506
2507                       if (debug)
2508 #if !defined(EXTENDED_COFF)
2509                         fprintf (stderr, "\tsec=%d class=%d type=%s%o %s\n",
2510                                  symbol.n_scnum, symbol.n_sclass,
2511                                  (symbol.n_type ? "0" : ""), symbol.n_type,
2512                                  name);
2513 #else
2514                         fprintf (stderr,
2515                                  "\tiss = %5d, value = %5ld, index = %5d, name = %s\n",
2516                                  symbol.iss, (long) symbol.value, symbol.index, name);
2517 #endif
2518                     }
2519                 }
2520             }
2521 #ifdef COLLECT_EXPORT_LIST
2522           else
2523             {
2524               /* If archive contains both 32-bit and 64-bit objects,
2525                  we want to skip objects in other mode so mismatch normal.  */
2526               if (debug)
2527                 fprintf (stderr, "%s : magic=%o aix64=%d mismatch\n",
2528                          prog_name, HEADER (ldptr).f_magic, aix64_flag);
2529             }
2530 #endif
2531         }
2532       else
2533         {
2534           fatal ("%s: cannot open as COFF file", prog_name);
2535         }
2536 #ifdef COLLECT_EXPORT_LIST
2537       /* On AIX loop continues while there are more members in archive.  */
2538     }
2539   while (ldclose (ldptr) == FAILURE);
2540 #else
2541   /* Otherwise we simply close ldptr.  */
2542   (void) ldclose(ldptr);
2543 #endif
2544 }
2545 #endif /* OBJECT_FORMAT_COFF */
2546
2547 #ifdef COLLECT_EXPORT_LIST
2548 /* Given a library name without "lib" prefix, this function
2549    returns a full library name including a path.  */
2550 static char *
2551 resolve_lib_name (const char *name)
2552 {
2553   char *lib_buf;
2554   int i, j, l = 0;
2555   /* Library extensions for AIX dynamic linking.  */
2556   const char * const libexts[2] = {"a", "so"};
2557
2558   for (i = 0; libpaths[i]; i++)
2559     if (libpaths[i]->max_len > l)
2560       l = libpaths[i]->max_len;
2561
2562   lib_buf = xmalloc (l + strlen(name) + 10);
2563
2564   for (i = 0; libpaths[i]; i++)
2565     {
2566       struct prefix_list *list = libpaths[i]->plist;
2567       for (; list; list = list->next)
2568         {
2569           /* The following lines are needed because path_prefix list
2570              may contain directories both with trailing '/' and
2571              without it.  */
2572           const char *p = "";
2573           if (list->prefix[strlen(list->prefix)-1] != '/')
2574             p = "/";
2575           for (j = 0; j < 2; j++)
2576             {
2577               sprintf (lib_buf, "%s%slib%s.%s",
2578                        list->prefix, p, name,
2579                        libexts[(j + aixrtl_flag) % 2]);
2580               if (debug) fprintf (stderr, "searching for: %s\n", lib_buf);
2581               if (file_exists (lib_buf))
2582                 {
2583                   if (debug) fprintf (stderr, "found: %s\n", lib_buf);
2584                   return (lib_buf);
2585                 }
2586             }
2587         }
2588     }
2589   if (debug)
2590     fprintf (stderr, "not found\n");
2591   else
2592     fatal ("library lib%s not found", name);
2593   return (NULL);
2594 }
2595 #endif /* COLLECT_EXPORT_LIST */