contrib.texi: Update contributions.
[official-gcc.git] / gcc / collect2.c
blobcdfe4e6be6d61aee8bf499e14e106cf22086cdd6
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, 2007 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).
9 This file is part of GCC.
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 3, or (at your option) any later
14 version.
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.
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
26 /* Build tables of static constructors and destructors and run ld. */
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include <signal.h>
33 #if ! defined( SIGCHLD ) && defined( SIGCLD )
34 # define SIGCHLD SIGCLD
35 #endif
37 #ifndef LIBRARY_PATH_ENV
38 #define LIBRARY_PATH_ENV "LIBRARY_PATH"
39 #endif
41 #define COLLECT
43 #include "collect2.h"
44 #include "demangle.h"
45 #include "obstack.h"
46 #include "intl.h"
47 #include "version.h"
49 /* On certain systems, we have code that works by scanning the object file
50 directly. But this code uses system-specific header files and library
51 functions, so turn it off in a cross-compiler. Likewise, the names of
52 the utilities are not correct for a cross-compiler; we have to hope that
53 cross-versions are in the proper directories. */
55 #ifdef CROSS_DIRECTORY_STRUCTURE
56 #undef OBJECT_FORMAT_COFF
57 #undef MD_EXEC_PREFIX
58 #undef REAL_LD_FILE_NAME
59 #undef REAL_NM_FILE_NAME
60 #undef REAL_STRIP_FILE_NAME
61 #endif
63 /* If we cannot use a special method, use the ordinary one:
64 run nm to find what symbols are present.
65 In a cross-compiler, this means you need a cross nm,
66 but that is not quite as unpleasant as special headers. */
68 #if !defined (OBJECT_FORMAT_COFF)
69 #define OBJECT_FORMAT_NONE
70 #endif
72 #ifdef OBJECT_FORMAT_COFF
74 #include <a.out.h>
75 #include <ar.h>
77 #ifdef UMAX
78 #include <sgs.h>
79 #endif
81 /* Many versions of ldfcn.h define these. */
82 #ifdef FREAD
83 #undef FREAD
84 #undef FWRITE
85 #endif
87 #include <ldfcn.h>
89 /* Some systems have an ISCOFF macro, but others do not. In some cases
90 the macro may be wrong. MY_ISCOFF is defined in tm.h files for machines
91 that either do not have an ISCOFF macro in /usr/include or for those
92 where it is wrong. */
94 #ifndef MY_ISCOFF
95 #define MY_ISCOFF(X) ISCOFF (X)
96 #endif
98 #endif /* OBJECT_FORMAT_COFF */
100 #ifdef OBJECT_FORMAT_NONE
102 /* Default flags to pass to nm. */
103 #ifndef NM_FLAGS
104 #define NM_FLAGS "-n"
105 #endif
107 #endif /* OBJECT_FORMAT_NONE */
109 /* Some systems use __main in a way incompatible with its use in gcc, in these
110 cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
111 give the same symbol without quotes for an alternative entry point. */
112 #ifndef NAME__MAIN
113 #define NAME__MAIN "__main"
114 #endif
116 /* This must match tree.h. */
117 #define DEFAULT_INIT_PRIORITY 65535
119 #ifndef COLLECT_SHARED_INIT_FUNC
120 #define COLLECT_SHARED_INIT_FUNC(STREAM, FUNC) \
121 fprintf ((STREAM), "void _GLOBAL__DI() {\n\t%s();\n}\n", (FUNC))
122 #endif
123 #ifndef COLLECT_SHARED_FINI_FUNC
124 #define COLLECT_SHARED_FINI_FUNC(STREAM, FUNC) \
125 fprintf ((STREAM), "void _GLOBAL__DD() {\n\t%s();\n}\n", (FUNC))
126 #endif
128 #ifdef LDD_SUFFIX
129 #define SCAN_LIBRARIES
130 #endif
132 #ifndef SHLIB_SUFFIX
133 #define SHLIB_SUFFIX ".so"
134 #endif
136 #ifdef USE_COLLECT2
137 int do_collecting = 1;
138 #else
139 int do_collecting = 0;
140 #endif
142 /* Nonzero if we should suppress the automatic demangling of identifiers
143 in linker error messages. Set from COLLECT_NO_DEMANGLE. */
144 int no_demangle;
146 /* Linked lists of constructor and destructor names. */
148 struct id
150 struct id *next;
151 int sequence;
152 char name[1];
155 struct head
157 struct id *first;
158 struct id *last;
159 int number;
162 /* Enumeration giving which pass this is for scanning the program file. */
164 enum pass {
165 PASS_FIRST, /* without constructors */
166 PASS_OBJ, /* individual objects */
167 PASS_LIB, /* looking for shared libraries */
168 PASS_SECOND /* with constructors linked in */
171 int vflag; /* true if -v */
172 static int rflag; /* true if -r */
173 static int strip_flag; /* true if -s */
174 static const char *demangle_flag;
175 #ifdef COLLECT_EXPORT_LIST
176 static int export_flag; /* true if -bE */
177 static int aix64_flag; /* true if -b64 */
178 static int aixrtl_flag; /* true if -brtl */
179 #endif
181 int debug; /* true if -debug */
183 static int shared_obj; /* true if -shared */
185 static const char *c_file; /* <xxx>.c for constructor/destructor list. */
186 static const char *o_file; /* <xxx>.o for constructor/destructor list. */
187 #ifdef COLLECT_EXPORT_LIST
188 static const char *export_file; /* <xxx>.x for AIX export list. */
189 #endif
190 const char *ldout; /* File for ld stdout. */
191 const char *lderrout; /* File for ld stderr. */
192 static const char *output_file; /* Output file for ld. */
193 static const char *nm_file_name; /* pathname of nm */
194 #ifdef LDD_SUFFIX
195 static const char *ldd_file_name; /* pathname of ldd (or equivalent) */
196 #endif
197 static const char *strip_file_name; /* pathname of strip */
198 const char *c_file_name; /* pathname of gcc */
199 static char *initname, *fininame; /* names of init and fini funcs */
201 static struct head constructors; /* list of constructors found */
202 static struct head destructors; /* list of destructors found */
203 #ifdef COLLECT_EXPORT_LIST
204 static struct head exports; /* list of exported symbols */
205 #endif
206 static struct head frame_tables; /* list of frame unwind info tables */
208 static bool at_file_supplied; /* Whether to use @file arguments */
209 static char *response_file; /* Name of any current response file */
211 struct obstack temporary_obstack;
212 char * temporary_firstobj;
214 /* Structure to hold all the directories in which to search for files to
215 execute. */
217 struct prefix_list
219 const char *prefix; /* String to prepend to the path. */
220 struct prefix_list *next; /* Next in linked list. */
223 struct path_prefix
225 struct prefix_list *plist; /* List of prefixes to try */
226 int max_len; /* Max length of a prefix in PLIST */
227 const char *name; /* Name of this list (used in config stuff) */
230 #ifdef COLLECT_EXPORT_LIST
231 /* Lists to keep libraries to be scanned for global constructors/destructors. */
232 static struct head libs; /* list of libraries */
233 static struct path_prefix cmdline_lib_dirs; /* directories specified with -L */
234 static struct path_prefix libpath_lib_dirs; /* directories in LIBPATH */
235 static struct path_prefix *libpaths[3] = {&cmdline_lib_dirs,
236 &libpath_lib_dirs, NULL};
237 #endif
239 /* Special kinds of symbols that a name may denote. */
241 typedef enum {
242 SYM_REGULAR = 0, /* nothing special */
244 SYM_CTOR = 1, /* constructor */
245 SYM_DTOR = 2, /* destructor */
246 SYM_INIT = 3, /* shared object routine that calls all the ctors */
247 SYM_FINI = 4, /* shared object routine that calls all the dtors */
248 SYM_DWEH = 5 /* DWARF exception handling table */
249 } symkind;
251 static symkind is_ctor_dtor (const char *);
253 static void handler (int);
254 static char *find_a_file (struct path_prefix *, const char *);
255 static void add_prefix (struct path_prefix *, const char *);
256 static void prefix_from_env (const char *, struct path_prefix *);
257 static void prefix_from_string (const char *, struct path_prefix *);
258 static void do_wait (const char *, struct pex_obj *);
259 static void fork_execute (const char *, char **);
260 static void maybe_unlink (const char *);
261 static void add_to_list (struct head *, const char *);
262 static int extract_init_priority (const char *);
263 static void sort_ids (struct head *);
264 static void write_list (FILE *, const char *, struct id *);
265 #ifdef COLLECT_EXPORT_LIST
266 static void dump_list (FILE *, const char *, struct id *);
267 #endif
268 #if 0
269 static void dump_prefix_list (FILE *, const char *, struct prefix_list *);
270 #endif
271 static void write_list_with_asm (FILE *, const char *, struct id *);
272 static void write_c_file (FILE *, const char *);
273 static void write_c_file_stat (FILE *, const char *);
274 #ifndef LD_INIT_SWITCH
275 static void write_c_file_glob (FILE *, const char *);
276 #endif
277 static void scan_prog_file (const char *, enum pass);
278 #ifdef SCAN_LIBRARIES
279 static void scan_libraries (const char *);
280 #endif
281 #if LINK_ELIMINATE_DUPLICATE_LDIRECTORIES
282 static int is_in_args (const char *, const char **, const char **);
283 #endif
284 #ifdef COLLECT_EXPORT_LIST
285 #if 0
286 static int is_in_list (const char *, struct id *);
287 #endif
288 static void write_aix_file (FILE *, struct id *);
289 static char *resolve_lib_name (const char *);
290 #endif
291 static char *extract_string (const char **);
293 /* Delete tempfiles and exit function. */
295 void
296 collect_exit (int status)
298 if (c_file != 0 && c_file[0])
299 maybe_unlink (c_file);
301 if (o_file != 0 && o_file[0])
302 maybe_unlink (o_file);
304 #ifdef COLLECT_EXPORT_LIST
305 if (export_file != 0 && export_file[0])
306 maybe_unlink (export_file);
307 #endif
309 if (ldout != 0 && ldout[0])
311 dump_file (ldout, stdout);
312 maybe_unlink (ldout);
315 if (lderrout != 0 && lderrout[0])
317 dump_file (lderrout, stderr);
318 maybe_unlink (lderrout);
321 if (status != 0 && output_file != 0 && output_file[0])
322 maybe_unlink (output_file);
324 if (response_file)
325 maybe_unlink (response_file);
327 exit (status);
331 /* Notify user of a non-error. */
332 void
333 notice (const char *cmsgid, ...)
335 va_list ap;
337 va_start (ap, cmsgid);
338 vfprintf (stderr, _(cmsgid), ap);
339 va_end (ap);
342 /* Die when sys call fails. */
344 void
345 fatal_perror (const char * cmsgid, ...)
347 int e = errno;
348 va_list ap;
350 va_start (ap, cmsgid);
351 fprintf (stderr, "collect2: ");
352 vfprintf (stderr, _(cmsgid), ap);
353 fprintf (stderr, ": %s\n", xstrerror (e));
354 va_end (ap);
356 collect_exit (FATAL_EXIT_CODE);
359 /* Just die. */
361 void
362 fatal (const char * cmsgid, ...)
364 va_list ap;
366 va_start (ap, cmsgid);
367 fprintf (stderr, "collect2: ");
368 vfprintf (stderr, _(cmsgid), ap);
369 fprintf (stderr, "\n");
370 va_end (ap);
372 collect_exit (FATAL_EXIT_CODE);
375 /* Write error message. */
377 void
378 error (const char * gmsgid, ...)
380 va_list ap;
382 va_start (ap, gmsgid);
383 fprintf (stderr, "collect2: ");
384 vfprintf (stderr, _(gmsgid), ap);
385 fprintf (stderr, "\n");
386 va_end(ap);
389 /* In case obstack is linked in, and abort is defined to fancy_abort,
390 provide a default entry. */
392 void
393 fancy_abort (const char *file, int line, const char *func)
395 fatal ("internal gcc abort in %s, at %s:%d", func, file, line);
398 static void
399 handler (int signo)
401 if (c_file != 0 && c_file[0])
402 maybe_unlink (c_file);
404 if (o_file != 0 && o_file[0])
405 maybe_unlink (o_file);
407 if (ldout != 0 && ldout[0])
408 maybe_unlink (ldout);
410 if (lderrout != 0 && lderrout[0])
411 maybe_unlink (lderrout);
413 #ifdef COLLECT_EXPORT_LIST
414 if (export_file != 0 && export_file[0])
415 maybe_unlink (export_file);
416 #endif
418 if (response_file)
419 maybe_unlink (response_file);
421 signal (signo, SIG_DFL);
422 raise (signo);
427 file_exists (const char *name)
429 return access (name, R_OK) == 0;
432 /* Parse a reasonable subset of shell quoting syntax. */
434 static char *
435 extract_string (const char **pp)
437 const char *p = *pp;
438 int backquote = 0;
439 int inside = 0;
441 for (;;)
443 char c = *p;
444 if (c == '\0')
445 break;
446 ++p;
447 if (backquote)
448 obstack_1grow (&temporary_obstack, c);
449 else if (! inside && c == ' ')
450 break;
451 else if (! inside && c == '\\')
452 backquote = 1;
453 else if (c == '\'')
454 inside = !inside;
455 else
456 obstack_1grow (&temporary_obstack, c);
459 obstack_1grow (&temporary_obstack, '\0');
460 *pp = p;
461 return XOBFINISH (&temporary_obstack, char *);
464 void
465 dump_file (const char *name, FILE *to)
467 FILE *stream = fopen (name, "r");
469 if (stream == 0)
470 return;
471 while (1)
473 int c;
474 while (c = getc (stream),
475 c != EOF && (ISIDNUM (c) || c == '$' || c == '.'))
476 obstack_1grow (&temporary_obstack, c);
477 if (obstack_object_size (&temporary_obstack) > 0)
479 const char *word, *p;
480 char *result;
481 obstack_1grow (&temporary_obstack, '\0');
482 word = XOBFINISH (&temporary_obstack, const char *);
484 if (*word == '.')
485 ++word, putc ('.', to);
486 p = word;
487 if (!strncmp (p, USER_LABEL_PREFIX, strlen (USER_LABEL_PREFIX)))
488 p += strlen (USER_LABEL_PREFIX);
490 #ifdef HAVE_LD_DEMANGLE
491 result = 0;
492 #else
493 if (no_demangle)
494 result = 0;
495 else
496 result = cplus_demangle (p, DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE);
497 #endif
499 if (result)
501 int diff;
502 fputs (result, to);
504 diff = strlen (word) - strlen (result);
505 while (diff > 0 && c == ' ')
506 --diff, putc (' ', to);
507 if (diff < 0 && c == ' ')
509 while (diff < 0 && c == ' ')
510 ++diff, c = getc (stream);
511 if (!ISSPACE (c))
513 /* Make sure we output at least one space, or
514 the demangled symbol name will run into
515 whatever text follows. */
516 putc (' ', to);
520 free (result);
522 else
523 fputs (word, to);
525 fflush (to);
526 obstack_free (&temporary_obstack, temporary_firstobj);
528 if (c == EOF)
529 break;
530 putc (c, to);
532 fclose (stream);
535 /* Return the kind of symbol denoted by name S. */
537 static symkind
538 is_ctor_dtor (const char *s)
540 struct names { const char *const name; const int len; const int ret;
541 const int two_underscores; };
543 const struct names *p;
544 int ch;
545 const char *orig_s = s;
547 static const struct names special[] = {
548 #ifndef NO_DOLLAR_IN_LABEL
549 { "GLOBAL__I$", sizeof ("GLOBAL__I$")-1, SYM_CTOR, 0 },
550 { "GLOBAL__D$", sizeof ("GLOBAL__D$")-1, SYM_DTOR, 0 },
551 #else
552 #ifndef NO_DOT_IN_LABEL
553 { "GLOBAL__I.", sizeof ("GLOBAL__I.")-1, SYM_CTOR, 0 },
554 { "GLOBAL__D.", sizeof ("GLOBAL__D.")-1, SYM_DTOR, 0 },
555 #endif /* NO_DOT_IN_LABEL */
556 #endif /* NO_DOLLAR_IN_LABEL */
557 { "GLOBAL__I_", sizeof ("GLOBAL__I_")-1, SYM_CTOR, 0 },
558 { "GLOBAL__D_", sizeof ("GLOBAL__D_")-1, SYM_DTOR, 0 },
559 { "GLOBAL__F_", sizeof ("GLOBAL__F_")-1, SYM_DWEH, 0 },
560 { "GLOBAL__FI_", sizeof ("GLOBAL__FI_")-1, SYM_INIT, 0 },
561 { "GLOBAL__FD_", sizeof ("GLOBAL__FD_")-1, SYM_FINI, 0 },
562 { NULL, 0, SYM_REGULAR, 0 }
565 while ((ch = *s) == '_')
566 ++s;
568 if (s == orig_s)
569 return SYM_REGULAR;
571 for (p = &special[0]; p->len > 0; p++)
573 if (ch == p->name[0]
574 && (!p->two_underscores || ((s - orig_s) >= 2))
575 && strncmp(s, p->name, p->len) == 0)
577 return p->ret;
580 return SYM_REGULAR;
583 /* We maintain two prefix lists: one from COMPILER_PATH environment variable
584 and one from the PATH variable. */
586 static struct path_prefix cpath, path;
588 #ifdef CROSS_DIRECTORY_STRUCTURE
589 /* This is the name of the target machine. We use it to form the name
590 of the files to execute. */
592 static const char *const target_machine = TARGET_MACHINE;
593 #endif
595 /* Search for NAME using prefix list PPREFIX. We only look for executable
596 files.
598 Return 0 if not found, otherwise return its name, allocated with malloc. */
600 static char *
601 find_a_file (struct path_prefix *pprefix, const char *name)
603 char *temp;
604 struct prefix_list *pl;
605 int len = pprefix->max_len + strlen (name) + 1;
607 if (debug)
608 fprintf (stderr, "Looking for '%s'\n", name);
610 #ifdef HOST_EXECUTABLE_SUFFIX
611 len += strlen (HOST_EXECUTABLE_SUFFIX);
612 #endif
614 temp = XNEWVEC (char, len);
616 /* Determine the filename to execute (special case for absolute paths). */
618 if (IS_ABSOLUTE_PATH (name))
620 if (access (name, X_OK) == 0)
622 strcpy (temp, name);
624 if (debug)
625 fprintf (stderr, " - found: absolute path\n");
627 return temp;
630 #ifdef HOST_EXECUTABLE_SUFFIX
631 /* Some systems have a suffix for executable files.
632 So try appending that. */
633 strcpy (temp, name);
634 strcat (temp, HOST_EXECUTABLE_SUFFIX);
636 if (access (temp, X_OK) == 0)
637 return temp;
638 #endif
640 if (debug)
641 fprintf (stderr, " - failed to locate using absolute path\n");
643 else
644 for (pl = pprefix->plist; pl; pl = pl->next)
646 struct stat st;
648 strcpy (temp, pl->prefix);
649 strcat (temp, name);
651 if (stat (temp, &st) >= 0
652 && ! S_ISDIR (st.st_mode)
653 && access (temp, X_OK) == 0)
654 return temp;
656 #ifdef HOST_EXECUTABLE_SUFFIX
657 /* Some systems have a suffix for executable files.
658 So try appending that. */
659 strcat (temp, HOST_EXECUTABLE_SUFFIX);
661 if (stat (temp, &st) >= 0
662 && ! S_ISDIR (st.st_mode)
663 && access (temp, X_OK) == 0)
664 return temp;
665 #endif
668 if (debug && pprefix->plist == NULL)
669 fprintf (stderr, " - failed: no entries in prefix list\n");
671 free (temp);
672 return 0;
675 /* Add an entry for PREFIX to prefix list PPREFIX. */
677 static void
678 add_prefix (struct path_prefix *pprefix, const char *prefix)
680 struct prefix_list *pl, **prev;
681 int len;
683 if (pprefix->plist)
685 for (pl = pprefix->plist; pl->next; pl = pl->next)
687 prev = &pl->next;
689 else
690 prev = &pprefix->plist;
692 /* Keep track of the longest prefix. */
694 len = strlen (prefix);
695 if (len > pprefix->max_len)
696 pprefix->max_len = len;
698 pl = XNEW (struct prefix_list);
699 pl->prefix = xstrdup (prefix);
701 if (*prev)
702 pl->next = *prev;
703 else
704 pl->next = (struct prefix_list *) 0;
705 *prev = pl;
708 /* Take the value of the environment variable ENV, break it into a path, and
709 add of the entries to PPREFIX. */
711 static void
712 prefix_from_env (const char *env, struct path_prefix *pprefix)
714 const char *p;
715 GET_ENVIRONMENT (p, env);
717 if (p)
718 prefix_from_string (p, pprefix);
721 static void
722 prefix_from_string (const char *p, struct path_prefix *pprefix)
724 const char *startp, *endp;
725 char *nstore = XNEWVEC (char, strlen (p) + 3);
727 if (debug)
728 fprintf (stderr, "Convert string '%s' into prefixes, separator = '%c'\n", p, PATH_SEPARATOR);
730 startp = endp = p;
731 while (1)
733 if (*endp == PATH_SEPARATOR || *endp == 0)
735 strncpy (nstore, startp, endp-startp);
736 if (endp == startp)
738 strcpy (nstore, "./");
740 else if (! IS_DIR_SEPARATOR (endp[-1]))
742 nstore[endp-startp] = DIR_SEPARATOR;
743 nstore[endp-startp+1] = 0;
745 else
746 nstore[endp-startp] = 0;
748 if (debug)
749 fprintf (stderr, " - add prefix: %s\n", nstore);
751 add_prefix (pprefix, nstore);
752 if (*endp == 0)
753 break;
754 endp = startp = endp + 1;
756 else
757 endp++;
759 free (nstore);
762 /* Main program. */
765 main (int argc, char **argv)
767 static const char *const ld_suffix = "ld";
768 static const char *const real_ld_suffix = "real-ld";
769 static const char *const collect_ld_suffix = "collect-ld";
770 static const char *const nm_suffix = "nm";
771 static const char *const gnm_suffix = "gnm";
772 #ifdef LDD_SUFFIX
773 static const char *const ldd_suffix = LDD_SUFFIX;
774 #endif
775 static const char *const strip_suffix = "strip";
776 static const char *const gstrip_suffix = "gstrip";
778 #ifdef CROSS_DIRECTORY_STRUCTURE
779 /* If we look for a program in the compiler directories, we just use
780 the short name, since these directories are already system-specific.
781 But it we look for a program in the system directories, we need to
782 qualify the program name with the target machine. */
784 const char *const full_ld_suffix =
785 concat(target_machine, "-", ld_suffix, NULL);
786 const char *const full_nm_suffix =
787 concat (target_machine, "-", nm_suffix, NULL);
788 const char *const full_gnm_suffix =
789 concat (target_machine, "-", gnm_suffix, NULL);
790 #ifdef LDD_SUFFIX
791 const char *const full_ldd_suffix =
792 concat (target_machine, "-", ldd_suffix, NULL);
793 #endif
794 const char *const full_strip_suffix =
795 concat (target_machine, "-", strip_suffix, NULL);
796 const char *const full_gstrip_suffix =
797 concat (target_machine, "-", gstrip_suffix, NULL);
798 #else
799 const char *const full_ld_suffix = ld_suffix;
800 const char *const full_nm_suffix = nm_suffix;
801 const char *const full_gnm_suffix = gnm_suffix;
802 #ifdef LDD_SUFFIX
803 const char *const full_ldd_suffix = ldd_suffix;
804 #endif
805 const char *const full_strip_suffix = strip_suffix;
806 const char *const full_gstrip_suffix = gstrip_suffix;
807 #endif /* CROSS_DIRECTORY_STRUCTURE */
809 const char *arg;
810 FILE *outf;
811 #ifdef COLLECT_EXPORT_LIST
812 FILE *exportf;
813 #endif
814 const char *ld_file_name;
815 const char *p;
816 char **c_argv;
817 const char **c_ptr;
818 char **ld1_argv;
819 const char **ld1;
820 char **ld2_argv;
821 const char **ld2;
822 char **object_lst;
823 const char **object;
824 int first_file;
825 int num_c_args;
826 char **old_argv;
828 old_argv = argv;
829 expandargv (&argc, &argv);
830 if (argv != old_argv)
831 at_file_supplied = 1;
833 num_c_args = argc + 9;
835 no_demangle = !! getenv ("COLLECT_NO_DEMANGLE");
837 /* Suppress demangling by the real linker, which may be broken. */
838 putenv (xstrdup ("COLLECT_NO_DEMANGLE="));
840 #if defined (COLLECT2_HOST_INITIALIZATION)
841 /* Perform system dependent initialization, if necessary. */
842 COLLECT2_HOST_INITIALIZATION;
843 #endif
845 #ifdef SIGCHLD
846 /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
847 receive the signal. A different setting is inheritable */
848 signal (SIGCHLD, SIG_DFL);
849 #endif
851 /* Unlock the stdio streams. */
852 unlock_std_streams ();
854 gcc_init_libintl ();
856 /* Do not invoke xcalloc before this point, since locale needs to be
857 set first, in case a diagnostic is issued. */
859 ld1 = (const char **)(ld1_argv = XCNEWVEC (char *, argc+4));
860 ld2 = (const char **)(ld2_argv = XCNEWVEC (char *, argc+11));
861 object = (const char **)(object_lst = XCNEWVEC (char *, argc));
863 #ifdef DEBUG
864 debug = 1;
865 #endif
867 /* Parse command line early for instances of -debug. This allows
868 the debug flag to be set before functions like find_a_file()
869 are called. */
871 int i;
873 for (i = 1; argv[i] != NULL; i ++)
875 if (! strcmp (argv[i], "-debug"))
876 debug = 1;
878 vflag = debug;
881 #ifndef DEFAULT_A_OUT_NAME
882 output_file = "a.out";
883 #else
884 output_file = DEFAULT_A_OUT_NAME;
885 #endif
887 obstack_begin (&temporary_obstack, 0);
888 temporary_firstobj = (char *) obstack_alloc (&temporary_obstack, 0);
890 #ifndef HAVE_LD_DEMANGLE
891 current_demangling_style = auto_demangling;
892 #endif
893 p = getenv ("COLLECT_GCC_OPTIONS");
894 while (p && *p)
896 const char *q = extract_string (&p);
897 if (*q == '-' && (q[1] == 'm' || q[1] == 'f'))
898 num_c_args++;
900 obstack_free (&temporary_obstack, temporary_firstobj);
902 /* -fno-profile-arcs -fno-test-coverage -fno-branch-probabilities
903 -fno-exceptions -w */
904 num_c_args += 5;
906 c_ptr = (const char **) (c_argv = XCNEWVEC (char *, num_c_args));
908 if (argc < 2)
909 fatal ("no arguments");
911 #ifdef SIGQUIT
912 if (signal (SIGQUIT, SIG_IGN) != SIG_IGN)
913 signal (SIGQUIT, handler);
914 #endif
915 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
916 signal (SIGINT, handler);
917 #ifdef SIGALRM
918 if (signal (SIGALRM, SIG_IGN) != SIG_IGN)
919 signal (SIGALRM, handler);
920 #endif
921 #ifdef SIGHUP
922 if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
923 signal (SIGHUP, handler);
924 #endif
925 if (signal (SIGSEGV, SIG_IGN) != SIG_IGN)
926 signal (SIGSEGV, handler);
927 #ifdef SIGBUS
928 if (signal (SIGBUS, SIG_IGN) != SIG_IGN)
929 signal (SIGBUS, handler);
930 #endif
932 /* Extract COMPILER_PATH and PATH into our prefix list. */
933 prefix_from_env ("COMPILER_PATH", &cpath);
934 prefix_from_env ("PATH", &path);
936 /* Try to discover a valid linker/nm/strip to use. */
938 /* Maybe we know the right file to use (if not cross). */
939 ld_file_name = 0;
940 #ifdef DEFAULT_LINKER
941 if (access (DEFAULT_LINKER, X_OK) == 0)
942 ld_file_name = DEFAULT_LINKER;
943 if (ld_file_name == 0)
944 #endif
945 #ifdef REAL_LD_FILE_NAME
946 ld_file_name = find_a_file (&path, REAL_LD_FILE_NAME);
947 if (ld_file_name == 0)
948 #endif
949 /* Search the (target-specific) compiler dirs for ld'. */
950 ld_file_name = find_a_file (&cpath, real_ld_suffix);
951 /* Likewise for `collect-ld'. */
952 if (ld_file_name == 0)
953 ld_file_name = find_a_file (&cpath, collect_ld_suffix);
954 /* Search the compiler directories for `ld'. We have protection against
955 recursive calls in find_a_file. */
956 if (ld_file_name == 0)
957 ld_file_name = find_a_file (&cpath, ld_suffix);
958 /* Search the ordinary system bin directories
959 for `ld' (if native linking) or `TARGET-ld' (if cross). */
960 if (ld_file_name == 0)
961 ld_file_name = find_a_file (&path, full_ld_suffix);
963 #ifdef REAL_NM_FILE_NAME
964 nm_file_name = find_a_file (&path, REAL_NM_FILE_NAME);
965 if (nm_file_name == 0)
966 #endif
967 nm_file_name = find_a_file (&cpath, gnm_suffix);
968 if (nm_file_name == 0)
969 nm_file_name = find_a_file (&path, full_gnm_suffix);
970 if (nm_file_name == 0)
971 nm_file_name = find_a_file (&cpath, nm_suffix);
972 if (nm_file_name == 0)
973 nm_file_name = find_a_file (&path, full_nm_suffix);
975 #ifdef LDD_SUFFIX
976 ldd_file_name = find_a_file (&cpath, ldd_suffix);
977 if (ldd_file_name == 0)
978 ldd_file_name = find_a_file (&path, full_ldd_suffix);
979 #endif
981 #ifdef REAL_STRIP_FILE_NAME
982 strip_file_name = find_a_file (&path, REAL_STRIP_FILE_NAME);
983 if (strip_file_name == 0)
984 #endif
985 strip_file_name = find_a_file (&cpath, gstrip_suffix);
986 if (strip_file_name == 0)
987 strip_file_name = find_a_file (&path, full_gstrip_suffix);
988 if (strip_file_name == 0)
989 strip_file_name = find_a_file (&cpath, strip_suffix);
990 if (strip_file_name == 0)
991 strip_file_name = find_a_file (&path, full_strip_suffix);
993 /* Determine the full path name of the C compiler to use. */
994 c_file_name = getenv ("COLLECT_GCC");
995 if (c_file_name == 0)
997 #ifdef CROSS_DIRECTORY_STRUCTURE
998 c_file_name = concat (target_machine, "-gcc", NULL);
999 #else
1000 c_file_name = "gcc";
1001 #endif
1004 p = find_a_file (&cpath, c_file_name);
1006 /* Here it should be safe to use the system search path since we should have
1007 already qualified the name of the compiler when it is needed. */
1008 if (p == 0)
1009 p = find_a_file (&path, c_file_name);
1011 if (p)
1012 c_file_name = p;
1014 *ld1++ = *ld2++ = ld_file_name;
1016 /* Make temp file names. */
1017 c_file = make_temp_file (".c");
1018 o_file = make_temp_file (".o");
1019 #ifdef COLLECT_EXPORT_LIST
1020 export_file = make_temp_file (".x");
1021 #endif
1022 ldout = make_temp_file (".ld");
1023 lderrout = make_temp_file (".le");
1024 *c_ptr++ = c_file_name;
1025 *c_ptr++ = "-x";
1026 *c_ptr++ = "c";
1027 *c_ptr++ = "-c";
1028 *c_ptr++ = "-o";
1029 *c_ptr++ = o_file;
1031 #ifdef COLLECT_EXPORT_LIST
1032 /* Generate a list of directories from LIBPATH. */
1033 prefix_from_env ("LIBPATH", &libpath_lib_dirs);
1034 /* Add to this list also two standard directories where
1035 AIX loader always searches for libraries. */
1036 add_prefix (&libpath_lib_dirs, "/lib");
1037 add_prefix (&libpath_lib_dirs, "/usr/lib");
1038 #endif
1040 /* Get any options that the upper GCC wants to pass to the sub-GCC.
1042 AIX support needs to know if -shared has been specified before
1043 parsing commandline arguments. */
1045 p = getenv ("COLLECT_GCC_OPTIONS");
1046 while (p && *p)
1048 const char *q = extract_string (&p);
1049 if (*q == '-' && (q[1] == 'm' || q[1] == 'f'))
1050 *c_ptr++ = xstrdup (q);
1051 if (strcmp (q, "-EL") == 0 || strcmp (q, "-EB") == 0)
1052 *c_ptr++ = xstrdup (q);
1053 if (strcmp (q, "-shared") == 0)
1054 shared_obj = 1;
1055 if (*q == '-' && q[1] == 'B')
1057 *c_ptr++ = xstrdup (q);
1058 if (q[2] == 0)
1060 q = extract_string (&p);
1061 *c_ptr++ = xstrdup (q);
1065 obstack_free (&temporary_obstack, temporary_firstobj);
1066 *c_ptr++ = "-fno-profile-arcs";
1067 *c_ptr++ = "-fno-test-coverage";
1068 *c_ptr++ = "-fno-branch-probabilities";
1069 *c_ptr++ = "-fno-exceptions";
1070 *c_ptr++ = "-w";
1072 /* !!! When GCC calls collect2,
1073 it does not know whether it is calling collect2 or ld.
1074 So collect2 cannot meaningfully understand any options
1075 except those ld understands.
1076 If you propose to make GCC pass some other option,
1077 just imagine what will happen if ld is really ld!!! */
1079 /* Parse arguments. Remember output file spec, pass the rest to ld. */
1080 /* After the first file, put in the c++ rt0. */
1082 first_file = 1;
1083 #ifdef HAVE_LD_DEMANGLE
1084 if (!demangle_flag && !no_demangle)
1085 demangle_flag = "--demangle";
1086 if (demangle_flag)
1087 *ld1++ = *ld2++ = demangle_flag;
1088 #endif
1089 while ((arg = *++argv) != (char *) 0)
1091 *ld1++ = *ld2++ = arg;
1093 if (arg[0] == '-')
1095 switch (arg[1])
1097 #ifdef COLLECT_EXPORT_LIST
1098 /* We want to disable automatic exports on AIX when user
1099 explicitly puts an export list in command line */
1100 case 'b':
1101 if (arg[2] == 'E' || strncmp (&arg[2], "export", 6) == 0)
1102 export_flag = 1;
1103 else if (arg[2] == '6' && arg[3] == '4')
1104 aix64_flag = 1;
1105 else if (arg[2] == 'r' && arg[3] == 't' && arg[4] == 'l')
1106 aixrtl_flag = 1;
1107 break;
1108 #endif
1110 case 'd':
1111 if (!strcmp (arg, "-debug"))
1113 /* Already parsed. */
1114 ld1--;
1115 ld2--;
1117 if (!strcmp (arg, "-dynamic-linker") && argv[1])
1119 ++argv;
1120 *ld1++ = *ld2++ = *argv;
1122 break;
1124 case 'l':
1125 if (first_file)
1127 /* place o_file BEFORE this argument! */
1128 first_file = 0;
1129 ld2--;
1130 *ld2++ = o_file;
1131 *ld2++ = arg;
1133 #ifdef COLLECT_EXPORT_LIST
1135 /* Resolving full library name. */
1136 const char *s = resolve_lib_name (arg+2);
1138 /* Saving a full library name. */
1139 add_to_list (&libs, s);
1141 #endif
1142 break;
1144 #ifdef COLLECT_EXPORT_LIST
1145 /* Saving directories where to search for libraries. */
1146 case 'L':
1147 add_prefix (&cmdline_lib_dirs, arg+2);
1148 break;
1149 #else
1150 #if LINK_ELIMINATE_DUPLICATE_LDIRECTORIES
1151 case 'L':
1152 if (is_in_args (arg, (const char **) ld1_argv, ld1-1))
1153 --ld1;
1154 break;
1155 #endif /* LINK_ELIMINATE_DUPLICATE_LDIRECTORIES */
1156 #endif
1158 case 'o':
1159 if (arg[2] == '\0')
1160 output_file = *ld1++ = *ld2++ = *++argv;
1161 else if (1
1162 #ifdef SWITCHES_NEED_SPACES
1163 && ! strchr (SWITCHES_NEED_SPACES, arg[1])
1164 #endif
1167 output_file = &arg[2];
1168 break;
1170 case 'r':
1171 if (arg[2] == '\0')
1172 rflag = 1;
1173 break;
1175 case 's':
1176 if (arg[2] == '\0' && do_collecting)
1178 /* We must strip after the nm run, otherwise C++ linking
1179 will not work. Thus we strip in the second ld run, or
1180 else with strip if there is no second ld run. */
1181 strip_flag = 1;
1182 ld1--;
1184 break;
1186 case 'v':
1187 if (arg[2] == '\0')
1188 vflag = 1;
1189 break;
1191 case '-':
1192 if (strcmp (arg, "--no-demangle") == 0)
1194 demangle_flag = arg;
1195 no_demangle = 1;
1196 ld1--;
1197 ld2--;
1199 else if (strncmp (arg, "--demangle", 10) == 0)
1201 demangle_flag = arg;
1202 no_demangle = 0;
1203 #ifndef HAVE_LD_DEMANGLE
1204 if (arg[10] == '=')
1206 enum demangling_styles style
1207 = cplus_demangle_name_to_style (arg+11);
1208 if (style == unknown_demangling)
1209 error ("unknown demangling style '%s'", arg+11);
1210 else
1211 current_demangling_style = style;
1213 #endif
1214 ld1--;
1215 ld2--;
1217 break;
1220 else if ((p = strrchr (arg, '.')) != (char *) 0
1221 && (strcmp (p, ".o") == 0 || strcmp (p, ".a") == 0
1222 || strcmp (p, ".so") == 0 || strcmp (p, ".lo") == 0
1223 || strcmp (p, ".obj") == 0))
1225 if (first_file)
1227 first_file = 0;
1228 if (p[1] == 'o')
1229 *ld2++ = o_file;
1230 else
1232 /* place o_file BEFORE this argument! */
1233 ld2--;
1234 *ld2++ = o_file;
1235 *ld2++ = arg;
1238 if (p[1] == 'o' || p[1] == 'l')
1239 *object++ = arg;
1240 #ifdef COLLECT_EXPORT_LIST
1241 /* libraries can be specified directly, i.e. without -l flag. */
1242 else
1244 /* Saving a full library name. */
1245 add_to_list (&libs, arg);
1247 #endif
1251 #ifdef COLLECT_EXPORT_LIST
1252 /* This is added only for debugging purposes. */
1253 if (debug)
1255 fprintf (stderr, "List of libraries:\n");
1256 dump_list (stderr, "\t", libs.first);
1259 /* The AIX linker will discard static constructors in object files if
1260 nothing else in the file is referenced, so look at them first. */
1262 const char **export_object_lst = (const char **)object_lst;
1264 while (export_object_lst < object)
1265 scan_prog_file (*export_object_lst++, PASS_OBJ);
1268 struct id *list = libs.first;
1270 for (; list; list = list->next)
1271 scan_prog_file (list->name, PASS_FIRST);
1274 if (exports.first)
1276 char *buf = concat ("-bE:", export_file, NULL);
1278 *ld1++ = buf;
1279 *ld2++ = buf;
1281 exportf = fopen (export_file, "w");
1282 if (exportf == (FILE *) 0)
1283 fatal_perror ("fopen %s", export_file);
1284 write_aix_file (exportf, exports.first);
1285 if (fclose (exportf))
1286 fatal_perror ("fclose %s", export_file);
1288 #endif
1290 *c_ptr++ = c_file;
1291 *c_ptr = *ld1 = *object = (char *) 0;
1293 if (vflag)
1295 notice ("collect2 version %s", version_string);
1296 #ifdef TARGET_VERSION
1297 TARGET_VERSION;
1298 #endif
1299 fprintf (stderr, "\n");
1302 if (debug)
1304 const char *ptr;
1305 fprintf (stderr, "ld_file_name = %s\n",
1306 (ld_file_name ? ld_file_name : "not found"));
1307 fprintf (stderr, "c_file_name = %s\n",
1308 (c_file_name ? c_file_name : "not found"));
1309 fprintf (stderr, "nm_file_name = %s\n",
1310 (nm_file_name ? nm_file_name : "not found"));
1311 #ifdef LDD_SUFFIX
1312 fprintf (stderr, "ldd_file_name = %s\n",
1313 (ldd_file_name ? ldd_file_name : "not found"));
1314 #endif
1315 fprintf (stderr, "strip_file_name = %s\n",
1316 (strip_file_name ? strip_file_name : "not found"));
1317 fprintf (stderr, "c_file = %s\n",
1318 (c_file ? c_file : "not found"));
1319 fprintf (stderr, "o_file = %s\n",
1320 (o_file ? o_file : "not found"));
1322 ptr = getenv ("COLLECT_GCC_OPTIONS");
1323 if (ptr)
1324 fprintf (stderr, "COLLECT_GCC_OPTIONS = %s\n", ptr);
1326 ptr = getenv ("COLLECT_GCC");
1327 if (ptr)
1328 fprintf (stderr, "COLLECT_GCC = %s\n", ptr);
1330 ptr = getenv ("COMPILER_PATH");
1331 if (ptr)
1332 fprintf (stderr, "COMPILER_PATH = %s\n", ptr);
1334 ptr = getenv (LIBRARY_PATH_ENV);
1335 if (ptr)
1336 fprintf (stderr, "%-20s= %s\n", LIBRARY_PATH_ENV, ptr);
1338 fprintf (stderr, "\n");
1341 /* Load the program, searching all libraries and attempting to provide
1342 undefined symbols from repository information. */
1344 /* On AIX we do this later. */
1345 #ifndef COLLECT_EXPORT_LIST
1346 do_tlink (ld1_argv, object_lst);
1347 #endif
1349 /* If -r or they will be run via some other method, do not build the
1350 constructor or destructor list, just return now. */
1351 if (rflag
1352 #ifndef COLLECT_EXPORT_LIST
1353 || ! do_collecting
1354 #endif
1357 #ifdef COLLECT_EXPORT_LIST
1358 /* Do the link we avoided above if we are exiting. */
1359 do_tlink (ld1_argv, object_lst);
1361 /* But make sure we delete the export file we may have created. */
1362 if (export_file != 0 && export_file[0])
1363 maybe_unlink (export_file);
1364 #endif
1365 maybe_unlink (c_file);
1366 maybe_unlink (o_file);
1367 return 0;
1370 /* Examine the namelist with nm and search it for static constructors
1371 and destructors to call.
1372 Write the constructor and destructor tables to a .s file and reload. */
1374 /* On AIX we already scanned for global constructors/destructors. */
1375 #ifndef COLLECT_EXPORT_LIST
1376 scan_prog_file (output_file, PASS_FIRST);
1377 #endif
1379 #ifdef SCAN_LIBRARIES
1380 scan_libraries (output_file);
1381 #endif
1383 if (debug)
1385 notice ("%d constructor(s) found\n", constructors.number);
1386 notice ("%d destructor(s) found\n", destructors.number);
1387 notice ("%d frame table(s) found\n", frame_tables.number);
1390 if (constructors.number == 0 && destructors.number == 0
1391 && frame_tables.number == 0
1392 #if defined (SCAN_LIBRARIES) || defined (COLLECT_EXPORT_LIST)
1393 /* If we will be running these functions ourselves, we want to emit
1394 stubs into the shared library so that we do not have to relink
1395 dependent programs when we add static objects. */
1396 && ! shared_obj
1397 #endif
1400 #ifdef COLLECT_EXPORT_LIST
1401 /* Do tlink without additional code generation. */
1402 do_tlink (ld1_argv, object_lst);
1403 #endif
1404 /* Strip now if it was requested on the command line. */
1405 if (strip_flag)
1407 char **real_strip_argv = XCNEWVEC (char *, 3);
1408 const char ** strip_argv = (const char **) real_strip_argv;
1410 strip_argv[0] = strip_file_name;
1411 strip_argv[1] = output_file;
1412 strip_argv[2] = (char *) 0;
1413 fork_execute ("strip", real_strip_argv);
1416 #ifdef COLLECT_EXPORT_LIST
1417 maybe_unlink (export_file);
1418 #endif
1419 maybe_unlink (c_file);
1420 maybe_unlink (o_file);
1421 return 0;
1424 /* Sort ctor and dtor lists by priority. */
1425 sort_ids (&constructors);
1426 sort_ids (&destructors);
1428 maybe_unlink(output_file);
1429 outf = fopen (c_file, "w");
1430 if (outf == (FILE *) 0)
1431 fatal_perror ("fopen %s", c_file);
1433 write_c_file (outf, c_file);
1435 if (fclose (outf))
1436 fatal_perror ("fclose %s", c_file);
1438 /* Tell the linker that we have initializer and finalizer functions. */
1439 #ifdef LD_INIT_SWITCH
1440 #ifdef COLLECT_EXPORT_LIST
1441 *ld2++ = concat (LD_INIT_SWITCH, ":", initname, ":", fininame, NULL);
1442 #else
1443 *ld2++ = LD_INIT_SWITCH;
1444 *ld2++ = initname;
1445 *ld2++ = LD_FINI_SWITCH;
1446 *ld2++ = fininame;
1447 #endif
1448 #endif
1450 #ifdef COLLECT_EXPORT_LIST
1451 if (shared_obj)
1453 /* If we did not add export flag to link arguments before, add it to
1454 second link phase now. No new exports should have been added. */
1455 if (! exports.first)
1456 *ld2++ = concat ("-bE:", export_file, NULL);
1458 #ifndef LD_INIT_SWITCH
1459 add_to_list (&exports, initname);
1460 add_to_list (&exports, fininame);
1461 add_to_list (&exports, "_GLOBAL__DI");
1462 add_to_list (&exports, "_GLOBAL__DD");
1463 #endif
1464 exportf = fopen (export_file, "w");
1465 if (exportf == (FILE *) 0)
1466 fatal_perror ("fopen %s", export_file);
1467 write_aix_file (exportf, exports.first);
1468 if (fclose (exportf))
1469 fatal_perror ("fclose %s", export_file);
1471 #endif
1473 /* End of arguments to second link phase. */
1474 *ld2 = (char*) 0;
1476 if (debug)
1478 fprintf (stderr, "\n========== output_file = %s, c_file = %s\n",
1479 output_file, c_file);
1480 write_c_file (stderr, "stderr");
1481 fprintf (stderr, "========== end of c_file\n\n");
1482 #ifdef COLLECT_EXPORT_LIST
1483 fprintf (stderr, "\n========== export_file = %s\n", export_file);
1484 write_aix_file (stderr, exports.first);
1485 fprintf (stderr, "========== end of export_file\n\n");
1486 #endif
1489 /* Assemble the constructor and destructor tables.
1490 Link the tables in with the rest of the program. */
1492 fork_execute ("gcc", c_argv);
1493 #ifdef COLLECT_EXPORT_LIST
1494 /* On AIX we must call tlink because of possible templates resolution. */
1495 do_tlink (ld2_argv, object_lst);
1496 #else
1497 /* Otherwise, simply call ld because tlink is already done. */
1498 fork_execute ("ld", ld2_argv);
1500 /* Let scan_prog_file do any final mods (OSF/rose needs this for
1501 constructors/destructors in shared libraries. */
1502 scan_prog_file (output_file, PASS_SECOND);
1503 #endif
1505 maybe_unlink (c_file);
1506 maybe_unlink (o_file);
1508 #ifdef COLLECT_EXPORT_LIST
1509 maybe_unlink (export_file);
1510 #endif
1512 return 0;
1516 /* Wait for a process to finish, and exit if a nonzero status is found. */
1519 collect_wait (const char *prog, struct pex_obj *pex)
1521 int status;
1523 if (!pex_get_status (pex, 1, &status))
1524 fatal_perror ("can't get program status");
1525 pex_free (pex);
1527 if (status)
1529 if (WIFSIGNALED (status))
1531 int sig = WTERMSIG (status);
1532 error ("%s terminated with signal %d [%s]%s",
1533 prog, sig, strsignal(sig),
1534 WCOREDUMP(status) ? ", core dumped" : "");
1535 collect_exit (FATAL_EXIT_CODE);
1538 if (WIFEXITED (status))
1539 return WEXITSTATUS (status);
1541 return 0;
1544 static void
1545 do_wait (const char *prog, struct pex_obj *pex)
1547 int ret = collect_wait (prog, pex);
1548 if (ret != 0)
1550 error ("%s returned %d exit status", prog, ret);
1551 collect_exit (ret);
1554 if (response_file)
1556 unlink (response_file);
1557 response_file = NULL;
1562 /* Execute a program, and wait for the reply. */
1564 struct pex_obj *
1565 collect_execute (const char *prog, char **argv, const char *outname,
1566 const char *errname)
1568 struct pex_obj *pex;
1569 const char *errmsg;
1570 int err;
1571 char *response_arg = NULL;
1572 char *response_argv[3] ATTRIBUTE_UNUSED;
1574 if (HAVE_GNU_LD && at_file_supplied && argv[0] != NULL)
1576 /* If using @file arguments, create a temporary file and put the
1577 contents of argv into it. Then change argv to an array corresponding
1578 to a single argument @FILE, where FILE is the temporary filename. */
1580 char **current_argv = argv + 1;
1581 char *argv0 = argv[0];
1582 int status;
1583 FILE *f;
1585 /* Note: we assume argv contains at least one element; this is
1586 checked above. */
1588 response_file = make_temp_file ("");
1590 f = fopen (response_file, "w");
1592 if (f == NULL)
1593 fatal ("could not open response file %s", response_file);
1595 status = writeargv (current_argv, f);
1597 if (status)
1598 fatal ("could not write to response file %s", response_file);
1600 status = fclose (f);
1602 if (EOF == status)
1603 fatal ("could not close response file %s", response_file);
1605 response_arg = concat ("@", response_file, NULL);
1606 response_argv[0] = argv0;
1607 response_argv[1] = response_arg;
1608 response_argv[2] = NULL;
1610 argv = response_argv;
1613 if (vflag || debug)
1615 char **p_argv;
1616 const char *str;
1618 if (argv[0])
1619 fprintf (stderr, "%s", argv[0]);
1620 else
1621 notice ("[cannot find %s]", prog);
1623 for (p_argv = &argv[1]; (str = *p_argv) != (char *) 0; p_argv++)
1624 fprintf (stderr, " %s", str);
1626 fprintf (stderr, "\n");
1629 fflush (stdout);
1630 fflush (stderr);
1632 /* If we cannot find a program we need, complain error. Do this here
1633 since we might not end up needing something that we could not find. */
1635 if (argv[0] == 0)
1636 fatal ("cannot find '%s'", prog);
1638 pex = pex_init (0, "collect2", NULL);
1639 if (pex == NULL)
1640 fatal_perror ("pex_init failed");
1642 errmsg = pex_run (pex, PEX_LAST | PEX_SEARCH, argv[0], argv, outname,
1643 errname, &err);
1644 if (errmsg != NULL)
1646 if (err != 0)
1648 errno = err;
1649 fatal_perror (errmsg);
1651 else
1652 fatal (errmsg);
1655 if (response_arg)
1656 free (response_arg);
1658 return pex;
1661 static void
1662 fork_execute (const char *prog, char **argv)
1664 struct pex_obj *pex;
1666 pex = collect_execute (prog, argv, NULL, NULL);
1667 do_wait (prog, pex);
1670 /* Unlink a file unless we are debugging. */
1672 static void
1673 maybe_unlink (const char *file)
1675 if (!debug)
1676 unlink_if_ordinary (file);
1677 else
1678 notice ("[Leaving %s]\n", file);
1682 static long sequence_number = 0;
1684 /* Add a name to a linked list. */
1686 static void
1687 add_to_list (struct head *head_ptr, const char *name)
1689 struct id *newid
1690 = (struct id *) xcalloc (sizeof (struct id) + strlen (name), 1);
1691 struct id *p;
1692 strcpy (newid->name, name);
1694 if (head_ptr->first)
1695 head_ptr->last->next = newid;
1696 else
1697 head_ptr->first = newid;
1699 /* Check for duplicate symbols. */
1700 for (p = head_ptr->first;
1701 strcmp (name, p->name) != 0;
1702 p = p->next)
1704 if (p != newid)
1706 head_ptr->last->next = 0;
1707 free (newid);
1708 return;
1711 newid->sequence = ++sequence_number;
1712 head_ptr->last = newid;
1713 head_ptr->number++;
1716 /* Grab the init priority number from an init function name that
1717 looks like "_GLOBAL_.I.12345.foo". */
1719 static int
1720 extract_init_priority (const char *name)
1722 int pos = 0, pri;
1724 while (name[pos] == '_')
1725 ++pos;
1726 pos += 10; /* strlen ("GLOBAL__X_") */
1728 /* Extract init_p number from ctor/dtor name. */
1729 pri = atoi (name + pos);
1730 return pri ? pri : DEFAULT_INIT_PRIORITY;
1733 /* Insertion sort the ids from ctor/dtor list HEAD_PTR in descending order.
1734 ctors will be run from right to left, dtors from left to right. */
1736 static void
1737 sort_ids (struct head *head_ptr)
1739 /* id holds the current element to insert. id_next holds the next
1740 element to insert. id_ptr iterates through the already sorted elements
1741 looking for the place to insert id. */
1742 struct id *id, *id_next, **id_ptr;
1744 id = head_ptr->first;
1746 /* We don't have any sorted elements yet. */
1747 head_ptr->first = NULL;
1749 for (; id; id = id_next)
1751 id_next = id->next;
1752 id->sequence = extract_init_priority (id->name);
1754 for (id_ptr = &(head_ptr->first); ; id_ptr = &((*id_ptr)->next))
1755 if (*id_ptr == NULL
1756 /* If the sequence numbers are the same, we put the id from the
1757 file later on the command line later in the list. */
1758 || id->sequence > (*id_ptr)->sequence
1759 /* Hack: do lexical compare, too.
1760 || (id->sequence == (*id_ptr)->sequence
1761 && strcmp (id->name, (*id_ptr)->name) > 0) */
1764 id->next = *id_ptr;
1765 *id_ptr = id;
1766 break;
1770 /* Now set the sequence numbers properly so write_c_file works. */
1771 for (id = head_ptr->first; id; id = id->next)
1772 id->sequence = ++sequence_number;
1775 /* Write: `prefix', the names on list LIST, `suffix'. */
1777 static void
1778 write_list (FILE *stream, const char *prefix, struct id *list)
1780 while (list)
1782 fprintf (stream, "%sx%d,\n", prefix, list->sequence);
1783 list = list->next;
1787 #if LINK_ELIMINATE_DUPLICATE_LDIRECTORIES
1788 /* Given a STRING, return nonzero if it occurs in the list in range
1789 [ARGS_BEGIN,ARGS_END). */
1791 static int
1792 is_in_args (const char *string, const char **args_begin,
1793 const char **args_end)
1795 const char **args_pointer;
1796 for (args_pointer = args_begin; args_pointer != args_end; ++args_pointer)
1797 if (strcmp (string, *args_pointer) == 0)
1798 return 1;
1799 return 0;
1801 #endif /* LINK_ELIMINATE_DUPLICATE_LDIRECTORIES */
1803 #ifdef COLLECT_EXPORT_LIST
1804 /* This function is really used only on AIX, but may be useful. */
1805 #if 0
1806 static int
1807 is_in_list (const char *prefix, struct id *list)
1809 while (list)
1811 if (!strcmp (prefix, list->name)) return 1;
1812 list = list->next;
1814 return 0;
1816 #endif
1817 #endif /* COLLECT_EXPORT_LIST */
1819 /* Added for debugging purpose. */
1820 #ifdef COLLECT_EXPORT_LIST
1821 static void
1822 dump_list (FILE *stream, const char *prefix, struct id *list)
1824 while (list)
1826 fprintf (stream, "%s%s,\n", prefix, list->name);
1827 list = list->next;
1830 #endif
1832 #if 0
1833 static void
1834 dump_prefix_list (FILE *stream, const char *prefix, struct prefix_list *list)
1836 while (list)
1838 fprintf (stream, "%s%s,\n", prefix, list->prefix);
1839 list = list->next;
1842 #endif
1844 static void
1845 write_list_with_asm (FILE *stream, const char *prefix, struct id *list)
1847 while (list)
1849 fprintf (stream, "%sx%d __asm__ (\"%s\");\n",
1850 prefix, list->sequence, list->name);
1851 list = list->next;
1855 /* Write out the constructor and destructor tables statically (for a shared
1856 object), along with the functions to execute them. */
1858 static void
1859 write_c_file_stat (FILE *stream, const char *name ATTRIBUTE_UNUSED)
1861 const char *p, *q;
1862 char *prefix, *r;
1863 int frames = (frame_tables.number > 0);
1865 /* Figure out name of output_file, stripping off .so version. */
1866 p = strrchr (output_file, '/');
1867 if (p == 0)
1868 p = output_file;
1869 else
1870 p++;
1871 q = p;
1872 while (q)
1874 q = strchr (q,'.');
1875 if (q == 0)
1877 q = p + strlen (p);
1878 break;
1880 else
1882 if (strncmp (q, SHLIB_SUFFIX, strlen (SHLIB_SUFFIX)) == 0)
1884 q += strlen (SHLIB_SUFFIX);
1885 break;
1887 else
1888 q++;
1891 /* q points to null at end of the string (or . of the .so version) */
1892 prefix = XNEWVEC (char, q - p + 1);
1893 strncpy (prefix, p, q - p);
1894 prefix[q - p] = 0;
1895 for (r = prefix; *r; r++)
1896 if (!ISALNUM ((unsigned char)*r))
1897 *r = '_';
1898 if (debug)
1899 notice ("\nwrite_c_file - output name is %s, prefix is %s\n",
1900 output_file, prefix);
1902 initname = concat ("_GLOBAL__FI_", prefix, NULL);
1903 fininame = concat ("_GLOBAL__FD_", prefix, NULL);
1905 free (prefix);
1907 /* Write the tables as C code. */
1909 fprintf (stream, "static int count;\n");
1910 fprintf (stream, "typedef void entry_pt();\n");
1911 write_list_with_asm (stream, "extern entry_pt ", constructors.first);
1913 if (frames)
1915 write_list_with_asm (stream, "extern void *", frame_tables.first);
1917 fprintf (stream, "\tstatic void *frame_table[] = {\n");
1918 write_list (stream, "\t\t&", frame_tables.first);
1919 fprintf (stream, "\t0\n};\n");
1921 /* This must match what's in frame.h. */
1922 fprintf (stream, "struct object {\n");
1923 fprintf (stream, " void *pc_begin;\n");
1924 fprintf (stream, " void *pc_end;\n");
1925 fprintf (stream, " void *fde_begin;\n");
1926 fprintf (stream, " void *fde_array;\n");
1927 fprintf (stream, " __SIZE_TYPE__ count;\n");
1928 fprintf (stream, " struct object *next;\n");
1929 fprintf (stream, "};\n");
1931 fprintf (stream, "extern void __register_frame_info_table (void *, struct object *);\n");
1932 fprintf (stream, "extern void *__deregister_frame_info (void *);\n");
1934 fprintf (stream, "static void reg_frame () {\n");
1935 fprintf (stream, "\tstatic struct object ob;\n");
1936 fprintf (stream, "\t__register_frame_info_table (frame_table, &ob);\n");
1937 fprintf (stream, "\t}\n");
1939 fprintf (stream, "static void dereg_frame () {\n");
1940 fprintf (stream, "\t__deregister_frame_info (frame_table);\n");
1941 fprintf (stream, "\t}\n");
1944 fprintf (stream, "void %s() {\n", initname);
1945 if (constructors.number > 0 || frames)
1947 fprintf (stream, "\tstatic entry_pt *ctors[] = {\n");
1948 write_list (stream, "\t\t", constructors.first);
1949 if (frames)
1950 fprintf (stream, "\treg_frame,\n");
1951 fprintf (stream, "\t};\n");
1952 fprintf (stream, "\tentry_pt **p;\n");
1953 fprintf (stream, "\tif (count++ != 0) return;\n");
1954 fprintf (stream, "\tp = ctors + %d;\n", constructors.number + frames);
1955 fprintf (stream, "\twhile (p > ctors) (*--p)();\n");
1957 else
1958 fprintf (stream, "\t++count;\n");
1959 fprintf (stream, "}\n");
1960 write_list_with_asm (stream, "extern entry_pt ", destructors.first);
1961 fprintf (stream, "void %s() {\n", fininame);
1962 if (destructors.number > 0 || frames)
1964 fprintf (stream, "\tstatic entry_pt *dtors[] = {\n");
1965 write_list (stream, "\t\t", destructors.first);
1966 if (frames)
1967 fprintf (stream, "\tdereg_frame,\n");
1968 fprintf (stream, "\t};\n");
1969 fprintf (stream, "\tentry_pt **p;\n");
1970 fprintf (stream, "\tif (--count != 0) return;\n");
1971 fprintf (stream, "\tp = dtors;\n");
1972 fprintf (stream, "\twhile (p < dtors + %d) (*p++)();\n",
1973 destructors.number + frames);
1975 fprintf (stream, "}\n");
1977 if (shared_obj)
1979 COLLECT_SHARED_INIT_FUNC(stream, initname);
1980 COLLECT_SHARED_FINI_FUNC(stream, fininame);
1984 /* Write the constructor/destructor tables. */
1986 #ifndef LD_INIT_SWITCH
1987 static void
1988 write_c_file_glob (FILE *stream, const char *name ATTRIBUTE_UNUSED)
1990 /* Write the tables as C code. */
1992 int frames = (frame_tables.number > 0);
1994 fprintf (stream, "typedef void entry_pt();\n\n");
1996 write_list_with_asm (stream, "extern entry_pt ", constructors.first);
1998 if (frames)
2000 write_list_with_asm (stream, "extern void *", frame_tables.first);
2002 fprintf (stream, "\tstatic void *frame_table[] = {\n");
2003 write_list (stream, "\t\t&", frame_tables.first);
2004 fprintf (stream, "\t0\n};\n");
2006 /* This must match what's in frame.h. */
2007 fprintf (stream, "struct object {\n");
2008 fprintf (stream, " void *pc_begin;\n");
2009 fprintf (stream, " void *pc_end;\n");
2010 fprintf (stream, " void *fde_begin;\n");
2011 fprintf (stream, " void *fde_array;\n");
2012 fprintf (stream, " __SIZE_TYPE__ count;\n");
2013 fprintf (stream, " struct object *next;\n");
2014 fprintf (stream, "};\n");
2016 fprintf (stream, "extern void __register_frame_info_table (void *, struct object *);\n");
2017 fprintf (stream, "extern void *__deregister_frame_info (void *);\n");
2019 fprintf (stream, "static void reg_frame () {\n");
2020 fprintf (stream, "\tstatic struct object ob;\n");
2021 fprintf (stream, "\t__register_frame_info_table (frame_table, &ob);\n");
2022 fprintf (stream, "\t}\n");
2024 fprintf (stream, "static void dereg_frame () {\n");
2025 fprintf (stream, "\t__deregister_frame_info (frame_table);\n");
2026 fprintf (stream, "\t}\n");
2029 fprintf (stream, "\nentry_pt * __CTOR_LIST__[] = {\n");
2030 fprintf (stream, "\t(entry_pt *) %d,\n", constructors.number + frames);
2031 write_list (stream, "\t", constructors.first);
2032 if (frames)
2033 fprintf (stream, "\treg_frame,\n");
2034 fprintf (stream, "\t0\n};\n\n");
2036 write_list_with_asm (stream, "extern entry_pt ", destructors.first);
2038 fprintf (stream, "\nentry_pt * __DTOR_LIST__[] = {\n");
2039 fprintf (stream, "\t(entry_pt *) %d,\n", destructors.number + frames);
2040 write_list (stream, "\t", destructors.first);
2041 if (frames)
2042 fprintf (stream, "\tdereg_frame,\n");
2043 fprintf (stream, "\t0\n};\n\n");
2045 fprintf (stream, "extern entry_pt %s;\n", NAME__MAIN);
2046 fprintf (stream, "entry_pt *__main_reference = %s;\n\n", NAME__MAIN);
2048 #endif /* ! LD_INIT_SWITCH */
2050 static void
2051 write_c_file (FILE *stream, const char *name)
2053 #ifndef LD_INIT_SWITCH
2054 if (! shared_obj)
2055 write_c_file_glob (stream, name);
2056 else
2057 #endif
2058 write_c_file_stat (stream, name);
2061 #ifdef COLLECT_EXPORT_LIST
2062 static void
2063 write_aix_file (FILE *stream, struct id *list)
2065 for (; list; list = list->next)
2067 fputs (list->name, stream);
2068 putc ('\n', stream);
2071 #endif
2073 #ifdef OBJECT_FORMAT_NONE
2075 /* Generic version to scan the name list of the loaded program for
2076 the symbols g++ uses for static constructors and destructors.
2078 The constructor table begins at __CTOR_LIST__ and contains a count
2079 of the number of pointers (or -1 if the constructors are built in a
2080 separate section by the linker), followed by the pointers to the
2081 constructor functions, terminated with a null pointer. The
2082 destructor table has the same format, and begins at __DTOR_LIST__. */
2084 static void
2085 scan_prog_file (const char *prog_name, enum pass which_pass)
2087 void (*int_handler) (int);
2088 #ifdef SIGQUIT
2089 void (*quit_handler) (int);
2090 #endif
2091 char *real_nm_argv[4];
2092 const char **nm_argv = (const char **) real_nm_argv;
2093 int argc = 0;
2094 struct pex_obj *pex;
2095 const char *errmsg;
2096 int err;
2097 char *p, buf[1024];
2098 FILE *inf;
2100 if (which_pass == PASS_SECOND)
2101 return;
2103 /* If we do not have an `nm', complain. */
2104 if (nm_file_name == 0)
2105 fatal ("cannot find 'nm'");
2107 nm_argv[argc++] = nm_file_name;
2108 if (NM_FLAGS[0] != '\0')
2109 nm_argv[argc++] = NM_FLAGS;
2111 nm_argv[argc++] = prog_name;
2112 nm_argv[argc++] = (char *) 0;
2114 /* Trace if needed. */
2115 if (vflag)
2117 const char **p_argv;
2118 const char *str;
2120 for (p_argv = &nm_argv[0]; (str = *p_argv) != (char *) 0; p_argv++)
2121 fprintf (stderr, " %s", str);
2123 fprintf (stderr, "\n");
2126 fflush (stdout);
2127 fflush (stderr);
2129 pex = pex_init (PEX_USE_PIPES, "collect2", NULL);
2130 if (pex == NULL)
2131 fatal_perror ("pex_init failed");
2133 errmsg = pex_run (pex, 0, nm_file_name, real_nm_argv, NULL, NULL, &err);
2134 if (errmsg != NULL)
2136 if (err != 0)
2138 errno = err;
2139 fatal_perror (errmsg);
2141 else
2142 fatal (errmsg);
2145 int_handler = (void (*) (int)) signal (SIGINT, SIG_IGN);
2146 #ifdef SIGQUIT
2147 quit_handler = (void (*) (int)) signal (SIGQUIT, SIG_IGN);
2148 #endif
2150 inf = pex_read_output (pex, 0);
2151 if (inf == NULL)
2152 fatal_perror ("can't open nm output");
2154 if (debug)
2155 fprintf (stderr, "\nnm output with constructors/destructors.\n");
2157 /* Read each line of nm output. */
2158 while (fgets (buf, sizeof buf, inf) != (char *) 0)
2160 int ch, ch2;
2161 char *name, *end;
2163 /* If it contains a constructor or destructor name, add the name
2164 to the appropriate list. */
2166 for (p = buf; (ch = *p) != '\0' && ch != '\n' && ch != '_'; p++)
2167 if (ch == ' ' && p[1] == 'U' && p[2] == ' ')
2168 break;
2170 if (ch != '_')
2171 continue;
2173 name = p;
2174 /* Find the end of the symbol name.
2175 Do not include `|', because Encore nm can tack that on the end. */
2176 for (end = p; (ch2 = *end) != '\0' && !ISSPACE (ch2) && ch2 != '|';
2177 end++)
2178 continue;
2181 *end = '\0';
2182 switch (is_ctor_dtor (name))
2184 case SYM_CTOR:
2185 if (which_pass != PASS_LIB)
2186 add_to_list (&constructors, name);
2187 break;
2189 case SYM_DTOR:
2190 if (which_pass != PASS_LIB)
2191 add_to_list (&destructors, name);
2192 break;
2194 case SYM_INIT:
2195 if (which_pass != PASS_LIB)
2196 fatal ("init function found in object %s", prog_name);
2197 #ifndef LD_INIT_SWITCH
2198 add_to_list (&constructors, name);
2199 #endif
2200 break;
2202 case SYM_FINI:
2203 if (which_pass != PASS_LIB)
2204 fatal ("fini function found in object %s", prog_name);
2205 #ifndef LD_FINI_SWITCH
2206 add_to_list (&destructors, name);
2207 #endif
2208 break;
2210 case SYM_DWEH:
2211 if (which_pass != PASS_LIB)
2212 add_to_list (&frame_tables, name);
2213 break;
2215 default: /* not a constructor or destructor */
2216 continue;
2219 if (debug)
2220 fprintf (stderr, "\t%s\n", buf);
2223 if (debug)
2224 fprintf (stderr, "\n");
2226 do_wait (nm_file_name, pex);
2228 signal (SIGINT, int_handler);
2229 #ifdef SIGQUIT
2230 signal (SIGQUIT, quit_handler);
2231 #endif
2234 #ifdef LDD_SUFFIX
2236 /* Use the List Dynamic Dependencies program to find shared libraries that
2237 the output file depends upon and their initialization/finalization
2238 routines, if any. */
2240 static void
2241 scan_libraries (const char *prog_name)
2243 static struct head libraries; /* list of shared libraries found */
2244 struct id *list;
2245 void (*int_handler) (int);
2246 #ifdef SIGQUIT
2247 void (*quit_handler) (int);
2248 #endif
2249 char *real_ldd_argv[4];
2250 const char **ldd_argv = (const char **) real_ldd_argv;
2251 int argc = 0;
2252 struct pex_obj *pex;
2253 const char *errmsg;
2254 int err;
2255 char buf[1024];
2256 FILE *inf;
2258 /* If we do not have an `ldd', complain. */
2259 if (ldd_file_name == 0)
2261 error ("cannot find 'ldd'");
2262 return;
2265 ldd_argv[argc++] = ldd_file_name;
2266 ldd_argv[argc++] = prog_name;
2267 ldd_argv[argc++] = (char *) 0;
2269 /* Trace if needed. */
2270 if (vflag)
2272 const char **p_argv;
2273 const char *str;
2275 for (p_argv = &ldd_argv[0]; (str = *p_argv) != (char *) 0; p_argv++)
2276 fprintf (stderr, " %s", str);
2278 fprintf (stderr, "\n");
2281 fflush (stdout);
2282 fflush (stderr);
2284 pex = pex_init (PEX_USE_PIPES, "collect2", NULL);
2285 if (pex == NULL)
2286 fatal_perror ("pex_init failed");
2288 errmsg = pex_run (pex, 0, ldd_file_name, real_ldd_argv, NULL, NULL, &err);
2289 if (errmsg != NULL)
2291 if (err != 0)
2293 errno = err;
2294 fatal_perror (errmsg);
2296 else
2297 fatal (errmsg);
2300 int_handler = (void (*) (int)) signal (SIGINT, SIG_IGN);
2301 #ifdef SIGQUIT
2302 quit_handler = (void (*) (int)) signal (SIGQUIT, SIG_IGN);
2303 #endif
2305 inf = pex_read_output (pex, 0);
2306 if (inf == NULL)
2307 fatal_perror ("can't open ldd output");
2309 if (debug)
2310 notice ("\nldd output with constructors/destructors.\n");
2312 /* Read each line of ldd output. */
2313 while (fgets (buf, sizeof buf, inf) != (char *) 0)
2315 int ch2;
2316 char *name, *end, *p = buf;
2318 /* Extract names of libraries and add to list. */
2319 PARSE_LDD_OUTPUT (p);
2320 if (p == 0)
2321 continue;
2323 name = p;
2324 if (strncmp (name, "not found", sizeof ("not found") - 1) == 0)
2325 fatal ("dynamic dependency %s not found", buf);
2327 /* Find the end of the symbol name. */
2328 for (end = p;
2329 (ch2 = *end) != '\0' && ch2 != '\n' && !ISSPACE (ch2) && ch2 != '|';
2330 end++)
2331 continue;
2332 *end = '\0';
2334 if (access (name, R_OK) == 0)
2335 add_to_list (&libraries, name);
2336 else
2337 fatal ("unable to open dynamic dependency '%s'", buf);
2339 if (debug)
2340 fprintf (stderr, "\t%s\n", buf);
2342 if (debug)
2343 fprintf (stderr, "\n");
2345 do_wait (ldd_file_name, pex);
2347 signal (SIGINT, int_handler);
2348 #ifdef SIGQUIT
2349 signal (SIGQUIT, quit_handler);
2350 #endif
2352 /* Now iterate through the library list adding their symbols to
2353 the list. */
2354 for (list = libraries.first; list; list = list->next)
2355 scan_prog_file (list->name, PASS_LIB);
2358 #endif /* LDD_SUFFIX */
2360 #endif /* OBJECT_FORMAT_NONE */
2364 * COFF specific stuff.
2367 #ifdef OBJECT_FORMAT_COFF
2369 #if defined (EXTENDED_COFF)
2371 # define GCC_SYMBOLS(X) (SYMHEADER(X).isymMax + SYMHEADER(X).iextMax)
2372 # define GCC_SYMENT SYMR
2373 # define GCC_OK_SYMBOL(X) ((X).st == stProc || (X).st == stGlobal)
2374 # define GCC_SYMINC(X) (1)
2375 # define GCC_SYMZERO(X) (SYMHEADER(X).isymMax)
2376 # define GCC_CHECK_HDR(X) (PSYMTAB(X) != 0)
2378 #else
2380 # define GCC_SYMBOLS(X) (HEADER(ldptr).f_nsyms)
2381 # define GCC_SYMENT SYMENT
2382 # if defined (C_WEAKEXT)
2383 # define GCC_OK_SYMBOL(X) \
2384 (((X).n_sclass == C_EXT || (X).n_sclass == C_WEAKEXT) && \
2385 ((X).n_scnum > N_UNDEF) && \
2386 (aix64_flag \
2387 || (((X).n_type & N_TMASK) == (DT_NON << N_BTSHFT) \
2388 || ((X).n_type & N_TMASK) == (DT_FCN << N_BTSHFT))))
2389 # define GCC_UNDEF_SYMBOL(X) \
2390 (((X).n_sclass == C_EXT || (X).n_sclass == C_WEAKEXT) && \
2391 ((X).n_scnum == N_UNDEF))
2392 # else
2393 # define GCC_OK_SYMBOL(X) \
2394 (((X).n_sclass == C_EXT) && \
2395 ((X).n_scnum > N_UNDEF) && \
2396 (aix64_flag \
2397 || (((X).n_type & N_TMASK) == (DT_NON << N_BTSHFT) \
2398 || ((X).n_type & N_TMASK) == (DT_FCN << N_BTSHFT))))
2399 # define GCC_UNDEF_SYMBOL(X) \
2400 (((X).n_sclass == C_EXT) && ((X).n_scnum == N_UNDEF))
2401 # endif
2402 # define GCC_SYMINC(X) ((X).n_numaux+1)
2403 # define GCC_SYMZERO(X) 0
2405 /* 0757 = U803XTOCMAGIC (AIX 4.3) and 0767 = U64_TOCMAGIC (AIX V5) */
2406 #ifdef _AIX51
2407 # define GCC_CHECK_HDR(X) \
2408 ((HEADER (X).f_magic == U802TOCMAGIC && ! aix64_flag) \
2409 || (HEADER (X).f_magic == 0767 && aix64_flag))
2410 #else
2411 # define GCC_CHECK_HDR(X) \
2412 ((HEADER (X).f_magic == U802TOCMAGIC && ! aix64_flag) \
2413 || (HEADER (X).f_magic == 0757 && aix64_flag))
2414 #endif
2416 #endif
2418 #ifdef COLLECT_EXPORT_LIST
2419 /* Array of standard AIX libraries which should not
2420 be scanned for ctors/dtors. */
2421 static const char *const aix_std_libs[] = {
2422 "/unix",
2423 "/lib/libc.a",
2424 "/lib/libm.a",
2425 "/lib/libc_r.a",
2426 "/lib/libm_r.a",
2427 "/usr/lib/libc.a",
2428 "/usr/lib/libm.a",
2429 "/usr/lib/libc_r.a",
2430 "/usr/lib/libm_r.a",
2431 "/usr/lib/threads/libc.a",
2432 "/usr/ccs/lib/libc.a",
2433 "/usr/ccs/lib/libm.a",
2434 "/usr/ccs/lib/libc_r.a",
2435 "/usr/ccs/lib/libm_r.a",
2436 NULL
2439 /* This function checks the filename and returns 1
2440 if this name matches the location of a standard AIX library. */
2441 static int ignore_library (const char *);
2442 static int
2443 ignore_library (const char *name)
2445 const char *const *p = &aix_std_libs[0];
2446 while (*p++ != NULL)
2447 if (! strcmp (name, *p)) return 1;
2448 return 0;
2450 #endif /* COLLECT_EXPORT_LIST */
2452 #if defined (HAVE_DECL_LDGETNAME) && !HAVE_DECL_LDGETNAME
2453 extern char *ldgetname (LDFILE *, GCC_SYMENT *);
2454 #endif
2456 /* COFF version to scan the name list of the loaded program for
2457 the symbols g++ uses for static constructors and destructors.
2459 The constructor table begins at __CTOR_LIST__ and contains a count
2460 of the number of pointers (or -1 if the constructors are built in a
2461 separate section by the linker), followed by the pointers to the
2462 constructor functions, terminated with a null pointer. The
2463 destructor table has the same format, and begins at __DTOR_LIST__. */
2465 static void
2466 scan_prog_file (const char *prog_name, enum pass which_pass)
2468 LDFILE *ldptr = NULL;
2469 int sym_index, sym_count;
2470 int is_shared = 0;
2472 if (which_pass != PASS_FIRST && which_pass != PASS_OBJ)
2473 return;
2475 #ifdef COLLECT_EXPORT_LIST
2476 /* We do not need scanning for some standard C libraries. */
2477 if (which_pass == PASS_FIRST && ignore_library (prog_name))
2478 return;
2480 /* On AIX we have a loop, because there is not much difference
2481 between an object and an archive. This trick allows us to
2482 eliminate scan_libraries() function. */
2485 #endif
2486 /* Some platforms (e.g. OSF4) declare ldopen as taking a
2487 non-const char * filename parameter, even though it will not
2488 modify that string. So we must cast away const-ness here,
2489 using CONST_CAST to prevent complaints from -Wcast-qual. */
2490 if ((ldptr = ldopen (CONST_CAST (char *, prog_name), ldptr)) != NULL)
2492 if (! MY_ISCOFF (HEADER (ldptr).f_magic))
2493 fatal ("%s: not a COFF file", prog_name);
2495 if (GCC_CHECK_HDR (ldptr))
2497 sym_count = GCC_SYMBOLS (ldptr);
2498 sym_index = GCC_SYMZERO (ldptr);
2500 #ifdef COLLECT_EXPORT_LIST
2501 /* Is current archive member a shared object? */
2502 is_shared = HEADER (ldptr).f_flags & F_SHROBJ;
2503 #endif
2505 while (sym_index < sym_count)
2507 GCC_SYMENT symbol;
2509 if (ldtbread (ldptr, sym_index, &symbol) <= 0)
2510 break;
2511 sym_index += GCC_SYMINC (symbol);
2513 if (GCC_OK_SYMBOL (symbol))
2515 char *name;
2517 if ((name = ldgetname (ldptr, &symbol)) == NULL)
2518 continue; /* Should never happen. */
2520 #ifdef XCOFF_DEBUGGING_INFO
2521 /* All AIX function names have a duplicate entry
2522 beginning with a dot. */
2523 if (*name == '.')
2524 ++name;
2525 #endif
2527 switch (is_ctor_dtor (name))
2529 case SYM_CTOR:
2530 if (! is_shared)
2531 add_to_list (&constructors, name);
2532 #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
2533 if (which_pass == PASS_OBJ)
2534 add_to_list (&exports, name);
2535 #endif
2536 break;
2538 case SYM_DTOR:
2539 if (! is_shared)
2540 add_to_list (&destructors, name);
2541 #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
2542 if (which_pass == PASS_OBJ)
2543 add_to_list (&exports, name);
2544 #endif
2545 break;
2547 #ifdef COLLECT_EXPORT_LIST
2548 case SYM_INIT:
2549 #ifndef LD_INIT_SWITCH
2550 if (is_shared)
2551 add_to_list (&constructors, name);
2552 #endif
2553 break;
2555 case SYM_FINI:
2556 #ifndef LD_INIT_SWITCH
2557 if (is_shared)
2558 add_to_list (&destructors, name);
2559 #endif
2560 break;
2561 #endif
2563 case SYM_DWEH:
2564 if (! is_shared)
2565 add_to_list (&frame_tables, name);
2566 #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
2567 if (which_pass == PASS_OBJ)
2568 add_to_list (&exports, name);
2569 #endif
2570 break;
2572 default: /* not a constructor or destructor */
2573 #ifdef COLLECT_EXPORT_LIST
2574 /* Explicitly export all global symbols when
2575 building a shared object on AIX, but do not
2576 re-export symbols from another shared object
2577 and do not export symbols if the user
2578 provides an explicit export list. */
2579 if (shared_obj && !is_shared
2580 && which_pass == PASS_OBJ && !export_flag)
2581 add_to_list (&exports, name);
2582 #endif
2583 continue;
2586 if (debug)
2587 #if !defined(EXTENDED_COFF)
2588 fprintf (stderr, "\tsec=%d class=%d type=%s%o %s\n",
2589 symbol.n_scnum, symbol.n_sclass,
2590 (symbol.n_type ? "0" : ""), symbol.n_type,
2591 name);
2592 #else
2593 fprintf (stderr,
2594 "\tiss = %5d, value = %5ld, index = %5d, name = %s\n",
2595 symbol.iss, (long) symbol.value, symbol.index, name);
2596 #endif
2600 #ifdef COLLECT_EXPORT_LIST
2601 else
2603 /* If archive contains both 32-bit and 64-bit objects,
2604 we want to skip objects in other mode so mismatch normal. */
2605 if (debug)
2606 fprintf (stderr, "%s : magic=%o aix64=%d mismatch\n",
2607 prog_name, HEADER (ldptr).f_magic, aix64_flag);
2609 #endif
2611 else
2613 fatal ("%s: cannot open as COFF file", prog_name);
2615 #ifdef COLLECT_EXPORT_LIST
2616 /* On AIX loop continues while there are more members in archive. */
2618 while (ldclose (ldptr) == FAILURE);
2619 #else
2620 /* Otherwise we simply close ldptr. */
2621 (void) ldclose(ldptr);
2622 #endif
2624 #endif /* OBJECT_FORMAT_COFF */
2626 #ifdef COLLECT_EXPORT_LIST
2627 /* Given a library name without "lib" prefix, this function
2628 returns a full library name including a path. */
2629 static char *
2630 resolve_lib_name (const char *name)
2632 char *lib_buf;
2633 int i, j, l = 0;
2634 /* Library extensions for AIX dynamic linking. */
2635 const char * const libexts[2] = {"a", "so"};
2637 for (i = 0; libpaths[i]; i++)
2638 if (libpaths[i]->max_len > l)
2639 l = libpaths[i]->max_len;
2641 lib_buf = XNEWVEC (char, l + strlen(name) + 10);
2643 for (i = 0; libpaths[i]; i++)
2645 struct prefix_list *list = libpaths[i]->plist;
2646 for (; list; list = list->next)
2648 /* The following lines are needed because path_prefix list
2649 may contain directories both with trailing '/' and
2650 without it. */
2651 const char *p = "";
2652 if (list->prefix[strlen(list->prefix)-1] != '/')
2653 p = "/";
2654 for (j = 0; j < 2; j++)
2656 sprintf (lib_buf, "%s%slib%s.%s",
2657 list->prefix, p, name,
2658 libexts[(j + aixrtl_flag) % 2]);
2659 if (debug) fprintf (stderr, "searching for: %s\n", lib_buf);
2660 if (file_exists (lib_buf))
2662 if (debug) fprintf (stderr, "found: %s\n", lib_buf);
2663 return (lib_buf);
2668 if (debug)
2669 fprintf (stderr, "not found\n");
2670 else
2671 fatal ("library lib%s not found", name);
2672 return (NULL);
2674 #endif /* COLLECT_EXPORT_LIST */