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
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
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. */
30 #include "coretypes.h"
33 #if ! defined( SIGCHLD ) && defined( SIGCLD )
34 # define SIGCHLD SIGCLD
37 #ifndef LIBRARY_PATH_ENV
38 #define LIBRARY_PATH_ENV "LIBRARY_PATH"
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
58 #undef REAL_LD_FILE_NAME
59 #undef REAL_NM_FILE_NAME
60 #undef REAL_STRIP_FILE_NAME
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
72 #ifdef OBJECT_FORMAT_COFF
81 /* Many versions of ldfcn.h define these. */
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
95 #define MY_ISCOFF(X) ISCOFF (X)
98 #endif /* OBJECT_FORMAT_COFF */
100 #ifdef OBJECT_FORMAT_NONE
102 /* Default flags to pass to nm. */
104 #define NM_FLAGS "-n"
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. */
113 #define NAME__MAIN "__main"
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))
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))
129 #define SCAN_LIBRARIES
133 #define SHLIB_SUFFIX ".so"
137 int do_collecting = 1;
139 int do_collecting = 0;
142 /* Nonzero if we should suppress the automatic demangling of identifiers
143 in linker error messages. Set from COLLECT_NO_DEMANGLE. */
146 /* Linked lists of constructor and destructor names. */
162 /* Enumeration giving which pass this is for scanning the program file. */
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 */
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. */
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 */
195 static const char *ldd_file_name; /* pathname of ldd (or equivalent) */
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 */
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
219 const char *prefix; /* String to prepend to the path. */
220 struct prefix_list *next; /* Next in linked list. */
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};
239 static void handler (int);
240 static int is_ctor_dtor (const char *);
241 static char *find_a_file (struct path_prefix *, const char *);
242 static void add_prefix (struct path_prefix *, const char *);
243 static void prefix_from_env (const char *, struct path_prefix *);
244 static void prefix_from_string (const char *, struct path_prefix *);
245 static void do_wait (const char *, struct pex_obj *);
246 static void fork_execute (const char *, char **);
247 static void maybe_unlink (const char *);
248 static void add_to_list (struct head *, const char *);
249 static int extract_init_priority (const char *);
250 static void sort_ids (struct head *);
251 static void write_list (FILE *, const char *, struct id *);
252 #ifdef COLLECT_EXPORT_LIST
253 static void dump_list (FILE *, const char *, struct id *);
256 static void dump_prefix_list (FILE *, const char *, struct prefix_list *);
258 static void write_list_with_asm (FILE *, const char *, struct id *);
259 static void write_c_file (FILE *, const char *);
260 static void write_c_file_stat (FILE *, const char *);
261 #ifndef LD_INIT_SWITCH
262 static void write_c_file_glob (FILE *, const char *);
264 static void scan_prog_file (const char *, enum pass);
265 #ifdef SCAN_LIBRARIES
266 static void scan_libraries (const char *);
268 #if LINK_ELIMINATE_DUPLICATE_LDIRECTORIES
269 static int is_in_args (const char *, const char **, const char **);
271 #ifdef COLLECT_EXPORT_LIST
273 static int is_in_list (const char *, struct id *);
275 static void write_aix_file (FILE *, struct id *);
276 static char *resolve_lib_name (const char *);
278 static char *extract_string (const char **);
280 /* Delete tempfiles and exit function. */
283 collect_exit (int status)
285 if (c_file != 0 && c_file[0])
286 maybe_unlink (c_file);
288 if (o_file != 0 && o_file[0])
289 maybe_unlink (o_file);
291 #ifdef COLLECT_EXPORT_LIST
292 if (export_file != 0 && export_file[0])
293 maybe_unlink (export_file);
296 if (ldout != 0 && ldout[0])
298 dump_file (ldout, stdout);
299 maybe_unlink (ldout);
302 if (lderrout != 0 && lderrout[0])
304 dump_file (lderrout, stderr);
305 maybe_unlink (lderrout);
308 if (status != 0 && output_file != 0 && output_file[0])
309 maybe_unlink (output_file);
312 maybe_unlink (response_file);
318 /* Notify user of a non-error. */
320 notice (const char *cmsgid, ...)
324 va_start (ap, cmsgid);
325 vfprintf (stderr, _(cmsgid), ap);
329 /* Die when sys call fails. */
332 fatal_perror (const char * cmsgid, ...)
337 va_start (ap, cmsgid);
338 fprintf (stderr, "collect2: ");
339 vfprintf (stderr, _(cmsgid), ap);
340 fprintf (stderr, ": %s\n", xstrerror (e));
343 collect_exit (FATAL_EXIT_CODE);
349 fatal (const char * cmsgid, ...)
353 va_start (ap, cmsgid);
354 fprintf (stderr, "collect2: ");
355 vfprintf (stderr, _(cmsgid), ap);
356 fprintf (stderr, "\n");
359 collect_exit (FATAL_EXIT_CODE);
362 /* Write error message. */
365 error (const char * gmsgid, ...)
369 va_start (ap, gmsgid);
370 fprintf (stderr, "collect2: ");
371 vfprintf (stderr, _(gmsgid), ap);
372 fprintf (stderr, "\n");
376 /* In case obstack is linked in, and abort is defined to fancy_abort,
377 provide a default entry. */
380 fancy_abort (const char *file, int line, const char *func)
382 fatal ("internal gcc abort in %s, at %s:%d", func, file, line);
388 if (c_file != 0 && c_file[0])
389 maybe_unlink (c_file);
391 if (o_file != 0 && o_file[0])
392 maybe_unlink (o_file);
394 if (ldout != 0 && ldout[0])
395 maybe_unlink (ldout);
397 if (lderrout != 0 && lderrout[0])
398 maybe_unlink (lderrout);
400 #ifdef COLLECT_EXPORT_LIST
401 if (export_file != 0 && export_file[0])
402 maybe_unlink (export_file);
406 maybe_unlink (response_file);
408 signal (signo, SIG_DFL);
414 file_exists (const char *name)
416 return access (name, R_OK) == 0;
419 /* Parse a reasonable subset of shell quoting syntax. */
422 extract_string (const char **pp)
435 obstack_1grow (&temporary_obstack, c);
436 else if (! inside && c == ' ')
438 else if (! inside && c == '\\')
443 obstack_1grow (&temporary_obstack, c);
446 obstack_1grow (&temporary_obstack, '\0');
448 return XOBFINISH (&temporary_obstack, char *);
452 dump_file (const char *name, FILE *to)
454 FILE *stream = fopen (name, "r");
461 while (c = getc (stream),
462 c != EOF && (ISIDNUM (c) || c == '$' || c == '.'))
463 obstack_1grow (&temporary_obstack, c);
464 if (obstack_object_size (&temporary_obstack) > 0)
466 const char *word, *p;
468 obstack_1grow (&temporary_obstack, '\0');
469 word = XOBFINISH (&temporary_obstack, const char *);
472 ++word, putc ('.', to);
474 if (!strncmp (p, USER_LABEL_PREFIX, strlen (USER_LABEL_PREFIX)))
475 p += strlen (USER_LABEL_PREFIX);
477 #ifdef HAVE_LD_DEMANGLE
483 result = cplus_demangle (p, DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE);
491 diff = strlen (word) - strlen (result);
492 while (diff > 0 && c == ' ')
493 --diff, putc (' ', to);
494 if (diff < 0 && c == ' ')
496 while (diff < 0 && c == ' ')
497 ++diff, c = getc (stream);
500 /* Make sure we output at least one space, or
501 the demangled symbol name will run into
502 whatever text follows. */
513 obstack_free (&temporary_obstack, temporary_firstobj);
522 /* Decide whether the given symbol is: a constructor (1), a destructor
523 (2), a routine in a shared object that calls all the constructors
524 (3) or destructors (4), a DWARF exception-handling table (5), or
525 nothing special (0). */
528 is_ctor_dtor (const char *s)
530 struct names { const char *const name; const int len; const int ret;
531 const int two_underscores; };
533 const struct names *p;
535 const char *orig_s = s;
537 static const struct names special[] = {
538 #ifndef NO_DOLLAR_IN_LABEL
539 { "GLOBAL__I$", sizeof ("GLOBAL__I$")-1, 1, 0 },
540 { "GLOBAL__D$", sizeof ("GLOBAL__D$")-1, 2, 0 },
542 #ifndef NO_DOT_IN_LABEL
543 { "GLOBAL__I.", sizeof ("GLOBAL__I.")-1, 1, 0 },
544 { "GLOBAL__D.", sizeof ("GLOBAL__D.")-1, 2, 0 },
545 #endif /* NO_DOT_IN_LABEL */
546 #endif /* NO_DOLLAR_IN_LABEL */
547 { "GLOBAL__I_", sizeof ("GLOBAL__I_")-1, 1, 0 },
548 { "GLOBAL__D_", sizeof ("GLOBAL__D_")-1, 2, 0 },
549 { "GLOBAL__F_", sizeof ("GLOBAL__F_")-1, 5, 0 },
550 { "GLOBAL__FI_", sizeof ("GLOBAL__FI_")-1, 3, 0 },
551 { "GLOBAL__FD_", sizeof ("GLOBAL__FD_")-1, 4, 0 },
555 while ((ch = *s) == '_')
561 for (p = &special[0]; p->len > 0; p++)
564 && (!p->two_underscores || ((s - orig_s) >= 2))
565 && strncmp(s, p->name, p->len) == 0)
573 /* We maintain two prefix lists: one from COMPILER_PATH environment variable
574 and one from the PATH variable. */
576 static struct path_prefix cpath, path;
578 #ifdef CROSS_DIRECTORY_STRUCTURE
579 /* This is the name of the target machine. We use it to form the name
580 of the files to execute. */
582 static const char *const target_machine = TARGET_MACHINE;
585 /* Search for NAME using prefix list PPREFIX. We only look for executable
588 Return 0 if not found, otherwise return its name, allocated with malloc. */
591 find_a_file (struct path_prefix *pprefix, const char *name)
594 struct prefix_list *pl;
595 int len = pprefix->max_len + strlen (name) + 1;
598 fprintf (stderr, "Looking for '%s'\n", name);
600 #ifdef HOST_EXECUTABLE_SUFFIX
601 len += strlen (HOST_EXECUTABLE_SUFFIX);
604 temp = XNEWVEC (char, len);
606 /* Determine the filename to execute (special case for absolute paths). */
608 if (IS_ABSOLUTE_PATH (name))
610 if (access (name, X_OK) == 0)
615 fprintf (stderr, " - found: absolute path\n");
620 #ifdef HOST_EXECUTABLE_SUFFIX
621 /* Some systems have a suffix for executable files.
622 So try appending that. */
624 strcat (temp, HOST_EXECUTABLE_SUFFIX);
626 if (access (temp, X_OK) == 0)
631 fprintf (stderr, " - failed to locate using absolute path\n");
634 for (pl = pprefix->plist; pl; pl = pl->next)
638 strcpy (temp, pl->prefix);
641 if (stat (temp, &st) >= 0
642 && ! S_ISDIR (st.st_mode)
643 && access (temp, X_OK) == 0)
646 #ifdef HOST_EXECUTABLE_SUFFIX
647 /* Some systems have a suffix for executable files.
648 So try appending that. */
649 strcat (temp, HOST_EXECUTABLE_SUFFIX);
651 if (stat (temp, &st) >= 0
652 && ! S_ISDIR (st.st_mode)
653 && access (temp, X_OK) == 0)
658 if (debug && pprefix->plist == NULL)
659 fprintf (stderr, " - failed: no entries in prefix list\n");
665 /* Add an entry for PREFIX to prefix list PPREFIX. */
668 add_prefix (struct path_prefix *pprefix, const char *prefix)
670 struct prefix_list *pl, **prev;
675 for (pl = pprefix->plist; pl->next; pl = pl->next)
680 prev = &pprefix->plist;
682 /* Keep track of the longest prefix. */
684 len = strlen (prefix);
685 if (len > pprefix->max_len)
686 pprefix->max_len = len;
688 pl = XNEW (struct prefix_list);
689 pl->prefix = xstrdup (prefix);
694 pl->next = (struct prefix_list *) 0;
698 /* Take the value of the environment variable ENV, break it into a path, and
699 add of the entries to PPREFIX. */
702 prefix_from_env (const char *env, struct path_prefix *pprefix)
705 GET_ENVIRONMENT (p, env);
708 prefix_from_string (p, pprefix);
712 prefix_from_string (const char *p, struct path_prefix *pprefix)
714 const char *startp, *endp;
715 char *nstore = XNEWVEC (char, strlen (p) + 3);
718 fprintf (stderr, "Convert string '%s' into prefixes, separator = '%c'\n", p, PATH_SEPARATOR);
723 if (*endp == PATH_SEPARATOR || *endp == 0)
725 strncpy (nstore, startp, endp-startp);
728 strcpy (nstore, "./");
730 else if (! IS_DIR_SEPARATOR (endp[-1]))
732 nstore[endp-startp] = DIR_SEPARATOR;
733 nstore[endp-startp+1] = 0;
736 nstore[endp-startp] = 0;
739 fprintf (stderr, " - add prefix: %s\n", nstore);
741 add_prefix (pprefix, nstore);
744 endp = startp = endp + 1;
755 main (int argc, char **argv)
757 static const char *const ld_suffix = "ld";
758 static const char *const real_ld_suffix = "real-ld";
759 static const char *const collect_ld_suffix = "collect-ld";
760 static const char *const nm_suffix = "nm";
761 static const char *const gnm_suffix = "gnm";
763 static const char *const ldd_suffix = LDD_SUFFIX;
765 static const char *const strip_suffix = "strip";
766 static const char *const gstrip_suffix = "gstrip";
768 #ifdef CROSS_DIRECTORY_STRUCTURE
769 /* If we look for a program in the compiler directories, we just use
770 the short name, since these directories are already system-specific.
771 But it we look for a program in the system directories, we need to
772 qualify the program name with the target machine. */
774 const char *const full_ld_suffix =
775 concat(target_machine, "-", ld_suffix, NULL);
776 const char *const full_nm_suffix =
777 concat (target_machine, "-", nm_suffix, NULL);
778 const char *const full_gnm_suffix =
779 concat (target_machine, "-", gnm_suffix, NULL);
781 const char *const full_ldd_suffix =
782 concat (target_machine, "-", ldd_suffix, NULL);
784 const char *const full_strip_suffix =
785 concat (target_machine, "-", strip_suffix, NULL);
786 const char *const full_gstrip_suffix =
787 concat (target_machine, "-", gstrip_suffix, NULL);
789 const char *const full_ld_suffix = ld_suffix;
790 const char *const full_nm_suffix = nm_suffix;
791 const char *const full_gnm_suffix = gnm_suffix;
793 const char *const full_ldd_suffix = ldd_suffix;
795 const char *const full_strip_suffix = strip_suffix;
796 const char *const full_gstrip_suffix = gstrip_suffix;
797 #endif /* CROSS_DIRECTORY_STRUCTURE */
801 #ifdef COLLECT_EXPORT_LIST
804 const char *ld_file_name;
819 expandargv (&argc, &argv);
820 if (argv != old_argv)
821 at_file_supplied = 1;
823 num_c_args = argc + 9;
825 no_demangle = !! getenv ("COLLECT_NO_DEMANGLE");
827 /* Suppress demangling by the real linker, which may be broken. */
828 putenv (xstrdup ("COLLECT_NO_DEMANGLE="));
830 #if defined (COLLECT2_HOST_INITIALIZATION)
831 /* Perform system dependent initialization, if necessary. */
832 COLLECT2_HOST_INITIALIZATION;
836 /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
837 receive the signal. A different setting is inheritable */
838 signal (SIGCHLD, SIG_DFL);
841 /* Unlock the stdio streams. */
842 unlock_std_streams ();
846 /* Do not invoke xcalloc before this point, since locale needs to be
847 set first, in case a diagnostic is issued. */
849 ld1 = (const char **)(ld1_argv = xcalloc(sizeof (char *), argc+4));
850 ld2 = (const char **)(ld2_argv = xcalloc(sizeof (char *), argc+11));
851 object = (const char **)(object_lst = xcalloc(sizeof (char *), argc));
857 /* Parse command line early for instances of -debug. This allows
858 the debug flag to be set before functions like find_a_file()
863 for (i = 1; argv[i] != NULL; i ++)
865 if (! strcmp (argv[i], "-debug"))
871 #ifndef DEFAULT_A_OUT_NAME
872 output_file = "a.out";
874 output_file = DEFAULT_A_OUT_NAME;
877 obstack_begin (&temporary_obstack, 0);
878 temporary_firstobj = obstack_alloc (&temporary_obstack, 0);
880 #ifndef HAVE_LD_DEMANGLE
881 current_demangling_style = auto_demangling;
883 p = getenv ("COLLECT_GCC_OPTIONS");
886 const char *q = extract_string (&p);
887 if (*q == '-' && (q[1] == 'm' || q[1] == 'f'))
890 obstack_free (&temporary_obstack, temporary_firstobj);
892 /* -fno-profile-arcs -fno-test-coverage -fno-branch-probabilities
893 -fno-exceptions -w */
896 c_ptr = (const char **) (c_argv = xcalloc (sizeof (char *), num_c_args));
899 fatal ("no arguments");
902 if (signal (SIGQUIT, SIG_IGN) != SIG_IGN)
903 signal (SIGQUIT, handler);
905 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
906 signal (SIGINT, handler);
908 if (signal (SIGALRM, SIG_IGN) != SIG_IGN)
909 signal (SIGALRM, handler);
912 if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
913 signal (SIGHUP, handler);
915 if (signal (SIGSEGV, SIG_IGN) != SIG_IGN)
916 signal (SIGSEGV, handler);
918 if (signal (SIGBUS, SIG_IGN) != SIG_IGN)
919 signal (SIGBUS, handler);
922 /* Extract COMPILER_PATH and PATH into our prefix list. */
923 prefix_from_env ("COMPILER_PATH", &cpath);
924 prefix_from_env ("PATH", &path);
926 /* Try to discover a valid linker/nm/strip to use. */
928 /* Maybe we know the right file to use (if not cross). */
930 #ifdef DEFAULT_LINKER
931 if (access (DEFAULT_LINKER, X_OK) == 0)
932 ld_file_name = DEFAULT_LINKER;
933 if (ld_file_name == 0)
935 #ifdef REAL_LD_FILE_NAME
936 ld_file_name = find_a_file (&path, REAL_LD_FILE_NAME);
937 if (ld_file_name == 0)
939 /* Search the (target-specific) compiler dirs for ld'. */
940 ld_file_name = find_a_file (&cpath, real_ld_suffix);
941 /* Likewise for `collect-ld'. */
942 if (ld_file_name == 0)
943 ld_file_name = find_a_file (&cpath, collect_ld_suffix);
944 /* Search the compiler directories for `ld'. We have protection against
945 recursive calls in find_a_file. */
946 if (ld_file_name == 0)
947 ld_file_name = find_a_file (&cpath, ld_suffix);
948 /* Search the ordinary system bin directories
949 for `ld' (if native linking) or `TARGET-ld' (if cross). */
950 if (ld_file_name == 0)
951 ld_file_name = find_a_file (&path, full_ld_suffix);
953 #ifdef REAL_NM_FILE_NAME
954 nm_file_name = find_a_file (&path, REAL_NM_FILE_NAME);
955 if (nm_file_name == 0)
957 nm_file_name = find_a_file (&cpath, gnm_suffix);
958 if (nm_file_name == 0)
959 nm_file_name = find_a_file (&path, full_gnm_suffix);
960 if (nm_file_name == 0)
961 nm_file_name = find_a_file (&cpath, nm_suffix);
962 if (nm_file_name == 0)
963 nm_file_name = find_a_file (&path, full_nm_suffix);
966 ldd_file_name = find_a_file (&cpath, ldd_suffix);
967 if (ldd_file_name == 0)
968 ldd_file_name = find_a_file (&path, full_ldd_suffix);
971 #ifdef REAL_STRIP_FILE_NAME
972 strip_file_name = find_a_file (&path, REAL_STRIP_FILE_NAME);
973 if (strip_file_name == 0)
975 strip_file_name = find_a_file (&cpath, gstrip_suffix);
976 if (strip_file_name == 0)
977 strip_file_name = find_a_file (&path, full_gstrip_suffix);
978 if (strip_file_name == 0)
979 strip_file_name = find_a_file (&cpath, strip_suffix);
980 if (strip_file_name == 0)
981 strip_file_name = find_a_file (&path, full_strip_suffix);
983 /* Determine the full path name of the C compiler to use. */
984 c_file_name = getenv ("COLLECT_GCC");
985 if (c_file_name == 0)
987 #ifdef CROSS_DIRECTORY_STRUCTURE
988 c_file_name = concat (target_machine, "-gcc", NULL);
994 p = find_a_file (&cpath, c_file_name);
996 /* Here it should be safe to use the system search path since we should have
997 already qualified the name of the compiler when it is needed. */
999 p = find_a_file (&path, c_file_name);
1004 *ld1++ = *ld2++ = ld_file_name;
1006 /* Make temp file names. */
1007 c_file = make_temp_file (".c");
1008 o_file = make_temp_file (".o");
1009 #ifdef COLLECT_EXPORT_LIST
1010 export_file = make_temp_file (".x");
1012 ldout = make_temp_file (".ld");
1013 lderrout = make_temp_file (".le");
1014 *c_ptr++ = c_file_name;
1021 #ifdef COLLECT_EXPORT_LIST
1022 /* Generate a list of directories from LIBPATH. */
1023 prefix_from_env ("LIBPATH", &libpath_lib_dirs);
1024 /* Add to this list also two standard directories where
1025 AIX loader always searches for libraries. */
1026 add_prefix (&libpath_lib_dirs, "/lib");
1027 add_prefix (&libpath_lib_dirs, "/usr/lib");
1030 /* Get any options that the upper GCC wants to pass to the sub-GCC.
1032 AIX support needs to know if -shared has been specified before
1033 parsing commandline arguments. */
1035 p = getenv ("COLLECT_GCC_OPTIONS");
1038 const char *q = extract_string (&p);
1039 if (*q == '-' && (q[1] == 'm' || q[1] == 'f'))
1040 *c_ptr++ = xstrdup (q);
1041 if (strcmp (q, "-EL") == 0 || strcmp (q, "-EB") == 0)
1042 *c_ptr++ = xstrdup (q);
1043 if (strcmp (q, "-shared") == 0)
1045 if (*q == '-' && q[1] == 'B')
1047 *c_ptr++ = xstrdup (q);
1050 q = extract_string (&p);
1051 *c_ptr++ = xstrdup (q);
1055 obstack_free (&temporary_obstack, temporary_firstobj);
1056 *c_ptr++ = "-fno-profile-arcs";
1057 *c_ptr++ = "-fno-test-coverage";
1058 *c_ptr++ = "-fno-branch-probabilities";
1059 *c_ptr++ = "-fno-exceptions";
1062 /* !!! When GCC calls collect2,
1063 it does not know whether it is calling collect2 or ld.
1064 So collect2 cannot meaningfully understand any options
1065 except those ld understands.
1066 If you propose to make GCC pass some other option,
1067 just imagine what will happen if ld is really ld!!! */
1069 /* Parse arguments. Remember output file spec, pass the rest to ld. */
1070 /* After the first file, put in the c++ rt0. */
1073 #ifdef HAVE_LD_DEMANGLE
1074 if (!demangle_flag && !no_demangle)
1075 demangle_flag = "--demangle";
1077 *ld1++ = *ld2++ = demangle_flag;
1079 while ((arg = *++argv) != (char *) 0)
1081 *ld1++ = *ld2++ = arg;
1087 #ifdef COLLECT_EXPORT_LIST
1088 /* We want to disable automatic exports on AIX when user
1089 explicitly puts an export list in command line */
1091 if (arg[2] == 'E' || strncmp (&arg[2], "export", 6) == 0)
1093 else if (arg[2] == '6' && arg[3] == '4')
1095 else if (arg[2] == 'r' && arg[3] == 't' && arg[4] == 'l')
1101 if (!strcmp (arg, "-debug"))
1103 /* Already parsed. */
1107 if (!strcmp (arg, "-dynamic-linker") && argv[1])
1110 *ld1++ = *ld2++ = *argv;
1117 /* place o_file BEFORE this argument! */
1123 #ifdef COLLECT_EXPORT_LIST
1125 /* Resolving full library name. */
1126 const char *s = resolve_lib_name (arg+2);
1128 /* Saving a full library name. */
1129 add_to_list (&libs, s);
1134 #ifdef COLLECT_EXPORT_LIST
1135 /* Saving directories where to search for libraries. */
1137 add_prefix (&cmdline_lib_dirs, arg+2);
1140 #if LINK_ELIMINATE_DUPLICATE_LDIRECTORIES
1142 if (is_in_args (arg, (const char **) ld1_argv, ld1-1))
1145 #endif /* LINK_ELIMINATE_DUPLICATE_LDIRECTORIES */
1150 output_file = *ld1++ = *ld2++ = *++argv;
1152 #ifdef SWITCHES_NEED_SPACES
1153 && ! strchr (SWITCHES_NEED_SPACES, arg[1])
1157 output_file = &arg[2];
1166 if (arg[2] == '\0' && do_collecting)
1168 /* We must strip after the nm run, otherwise C++ linking
1169 will not work. Thus we strip in the second ld run, or
1170 else with strip if there is no second ld run. */
1182 if (strcmp (arg, "--no-demangle") == 0)
1184 demangle_flag = arg;
1189 else if (strncmp (arg, "--demangle", 10) == 0)
1191 demangle_flag = arg;
1193 #ifndef HAVE_LD_DEMANGLE
1196 enum demangling_styles style
1197 = cplus_demangle_name_to_style (arg+11);
1198 if (style == unknown_demangling)
1199 error ("unknown demangling style '%s'", arg+11);
1201 current_demangling_style = style;
1210 else if ((p = strrchr (arg, '.')) != (char *) 0
1211 && (strcmp (p, ".o") == 0 || strcmp (p, ".a") == 0
1212 || strcmp (p, ".so") == 0 || strcmp (p, ".lo") == 0
1213 || strcmp (p, ".obj") == 0))
1222 /* place o_file BEFORE this argument! */
1228 if (p[1] == 'o' || p[1] == 'l')
1230 #ifdef COLLECT_EXPORT_LIST
1231 /* libraries can be specified directly, i.e. without -l flag. */
1234 /* Saving a full library name. */
1235 add_to_list (&libs, arg);
1241 #ifdef COLLECT_EXPORT_LIST
1242 /* This is added only for debugging purposes. */
1245 fprintf (stderr, "List of libraries:\n");
1246 dump_list (stderr, "\t", libs.first);
1249 /* The AIX linker will discard static constructors in object files if
1250 nothing else in the file is referenced, so look at them first. */
1252 const char **export_object_lst = (const char **)object_lst;
1254 while (export_object_lst < object)
1255 scan_prog_file (*export_object_lst++, PASS_OBJ);
1258 struct id *list = libs.first;
1260 for (; list; list = list->next)
1261 scan_prog_file (list->name, PASS_FIRST);
1266 char *buf = concat ("-bE:", export_file, NULL);
1271 exportf = fopen (export_file, "w");
1272 if (exportf == (FILE *) 0)
1273 fatal_perror ("fopen %s", export_file);
1274 write_aix_file (exportf, exports.first);
1275 if (fclose (exportf))
1276 fatal_perror ("fclose %s", export_file);
1281 *c_ptr = *ld1 = *object = (char *) 0;
1285 notice ("collect2 version %s", version_string);
1286 #ifdef TARGET_VERSION
1289 fprintf (stderr, "\n");
1295 fprintf (stderr, "ld_file_name = %s\n",
1296 (ld_file_name ? ld_file_name : "not found"));
1297 fprintf (stderr, "c_file_name = %s\n",
1298 (c_file_name ? c_file_name : "not found"));
1299 fprintf (stderr, "nm_file_name = %s\n",
1300 (nm_file_name ? nm_file_name : "not found"));
1302 fprintf (stderr, "ldd_file_name = %s\n",
1303 (ldd_file_name ? ldd_file_name : "not found"));
1305 fprintf (stderr, "strip_file_name = %s\n",
1306 (strip_file_name ? strip_file_name : "not found"));
1307 fprintf (stderr, "c_file = %s\n",
1308 (c_file ? c_file : "not found"));
1309 fprintf (stderr, "o_file = %s\n",
1310 (o_file ? o_file : "not found"));
1312 ptr = getenv ("COLLECT_GCC_OPTIONS");
1314 fprintf (stderr, "COLLECT_GCC_OPTIONS = %s\n", ptr);
1316 ptr = getenv ("COLLECT_GCC");
1318 fprintf (stderr, "COLLECT_GCC = %s\n", ptr);
1320 ptr = getenv ("COMPILER_PATH");
1322 fprintf (stderr, "COMPILER_PATH = %s\n", ptr);
1324 ptr = getenv (LIBRARY_PATH_ENV);
1326 fprintf (stderr, "%-20s= %s\n", LIBRARY_PATH_ENV, ptr);
1328 fprintf (stderr, "\n");
1331 /* Load the program, searching all libraries and attempting to provide
1332 undefined symbols from repository information. */
1334 /* On AIX we do this later. */
1335 #ifndef COLLECT_EXPORT_LIST
1336 do_tlink (ld1_argv, object_lst);
1339 /* If -r or they will be run via some other method, do not build the
1340 constructor or destructor list, just return now. */
1342 #ifndef COLLECT_EXPORT_LIST
1347 #ifdef COLLECT_EXPORT_LIST
1348 /* Do the link we avoided above if we are exiting. */
1349 do_tlink (ld1_argv, object_lst);
1351 /* But make sure we delete the export file we may have created. */
1352 if (export_file != 0 && export_file[0])
1353 maybe_unlink (export_file);
1355 maybe_unlink (c_file);
1356 maybe_unlink (o_file);
1360 /* Examine the namelist with nm and search it for static constructors
1361 and destructors to call.
1362 Write the constructor and destructor tables to a .s file and reload. */
1364 /* On AIX we already scanned for global constructors/destructors. */
1365 #ifndef COLLECT_EXPORT_LIST
1366 scan_prog_file (output_file, PASS_FIRST);
1369 #ifdef SCAN_LIBRARIES
1370 scan_libraries (output_file);
1375 notice ("%d constructor(s) found\n", constructors.number);
1376 notice ("%d destructor(s) found\n", destructors.number);
1377 notice ("%d frame table(s) found\n", frame_tables.number);
1380 if (constructors.number == 0 && destructors.number == 0
1381 && frame_tables.number == 0
1382 #if defined (SCAN_LIBRARIES) || defined (COLLECT_EXPORT_LIST)
1383 /* If we will be running these functions ourselves, we want to emit
1384 stubs into the shared library so that we do not have to relink
1385 dependent programs when we add static objects. */
1390 #ifdef COLLECT_EXPORT_LIST
1391 /* Do tlink without additional code generation. */
1392 do_tlink (ld1_argv, object_lst);
1394 /* Strip now if it was requested on the command line. */
1397 char **real_strip_argv = XCNEWVEC (char *, 3);
1398 const char ** strip_argv = (const char **) real_strip_argv;
1400 strip_argv[0] = strip_file_name;
1401 strip_argv[1] = output_file;
1402 strip_argv[2] = (char *) 0;
1403 fork_execute ("strip", real_strip_argv);
1406 #ifdef COLLECT_EXPORT_LIST
1407 maybe_unlink (export_file);
1409 maybe_unlink (c_file);
1410 maybe_unlink (o_file);
1414 /* Sort ctor and dtor lists by priority. */
1415 sort_ids (&constructors);
1416 sort_ids (&destructors);
1418 maybe_unlink(output_file);
1419 outf = fopen (c_file, "w");
1420 if (outf == (FILE *) 0)
1421 fatal_perror ("fopen %s", c_file);
1423 write_c_file (outf, c_file);
1426 fatal_perror ("fclose %s", c_file);
1428 /* Tell the linker that we have initializer and finalizer functions. */
1429 #ifdef LD_INIT_SWITCH
1430 #ifdef COLLECT_EXPORT_LIST
1431 *ld2++ = concat (LD_INIT_SWITCH, ":", initname, ":", fininame, NULL);
1433 *ld2++ = LD_INIT_SWITCH;
1435 *ld2++ = LD_FINI_SWITCH;
1440 #ifdef COLLECT_EXPORT_LIST
1443 /* If we did not add export flag to link arguments before, add it to
1444 second link phase now. No new exports should have been added. */
1445 if (! exports.first)
1446 *ld2++ = concat ("-bE:", export_file, NULL);
1448 #ifndef LD_INIT_SWITCH
1449 add_to_list (&exports, initname);
1450 add_to_list (&exports, fininame);
1451 add_to_list (&exports, "_GLOBAL__DI");
1452 add_to_list (&exports, "_GLOBAL__DD");
1454 exportf = fopen (export_file, "w");
1455 if (exportf == (FILE *) 0)
1456 fatal_perror ("fopen %s", export_file);
1457 write_aix_file (exportf, exports.first);
1458 if (fclose (exportf))
1459 fatal_perror ("fclose %s", export_file);
1463 /* End of arguments to second link phase. */
1468 fprintf (stderr, "\n========== output_file = %s, c_file = %s\n",
1469 output_file, c_file);
1470 write_c_file (stderr, "stderr");
1471 fprintf (stderr, "========== end of c_file\n\n");
1472 #ifdef COLLECT_EXPORT_LIST
1473 fprintf (stderr, "\n========== export_file = %s\n", export_file);
1474 write_aix_file (stderr, exports.first);
1475 fprintf (stderr, "========== end of export_file\n\n");
1479 /* Assemble the constructor and destructor tables.
1480 Link the tables in with the rest of the program. */
1482 fork_execute ("gcc", c_argv);
1483 #ifdef COLLECT_EXPORT_LIST
1484 /* On AIX we must call tlink because of possible templates resolution. */
1485 do_tlink (ld2_argv, object_lst);
1487 /* Otherwise, simply call ld because tlink is already done. */
1488 fork_execute ("ld", ld2_argv);
1490 /* Let scan_prog_file do any final mods (OSF/rose needs this for
1491 constructors/destructors in shared libraries. */
1492 scan_prog_file (output_file, PASS_SECOND);
1495 maybe_unlink (c_file);
1496 maybe_unlink (o_file);
1498 #ifdef COLLECT_EXPORT_LIST
1499 maybe_unlink (export_file);
1506 /* Wait for a process to finish, and exit if a nonzero status is found. */
1509 collect_wait (const char *prog, struct pex_obj *pex)
1513 if (!pex_get_status (pex, 1, &status))
1514 fatal_perror ("can't get program status");
1519 if (WIFSIGNALED (status))
1521 int sig = WTERMSIG (status);
1522 error ("%s terminated with signal %d [%s]%s",
1523 prog, sig, strsignal(sig),
1524 WCOREDUMP(status) ? ", core dumped" : "");
1525 collect_exit (FATAL_EXIT_CODE);
1528 if (WIFEXITED (status))
1529 return WEXITSTATUS (status);
1535 do_wait (const char *prog, struct pex_obj *pex)
1537 int ret = collect_wait (prog, pex);
1540 error ("%s returned %d exit status", prog, ret);
1546 unlink (response_file);
1547 response_file = NULL;
1552 /* Execute a program, and wait for the reply. */
1555 collect_execute (const char *prog, char **argv, const char *outname,
1556 const char *errname)
1558 struct pex_obj *pex;
1561 char *response_arg = NULL;
1562 char *response_argv[3] ATTRIBUTE_UNUSED;
1564 if (HAVE_GNU_LD && at_file_supplied && argv[0] != NULL)
1566 /* If using @file arguments, create a temporary file and put the
1567 contents of argv into it. Then change argv to an array corresponding
1568 to a single argument @FILE, where FILE is the temporary filename. */
1570 char **current_argv = argv + 1;
1571 char *argv0 = argv[0];
1575 /* Note: we assume argv contains at least one element; this is
1578 response_file = make_temp_file ("");
1580 f = fopen (response_file, "w");
1583 fatal ("could not open response file %s", response_file);
1585 status = writeargv (current_argv, f);
1588 fatal ("could not write to response file %s", response_file);
1590 status = fclose (f);
1593 fatal ("could not close response file %s", response_file);
1595 response_arg = concat ("@", response_file, NULL);
1596 response_argv[0] = argv0;
1597 response_argv[1] = response_arg;
1598 response_argv[2] = NULL;
1600 argv = response_argv;
1609 fprintf (stderr, "%s", argv[0]);
1611 notice ("[cannot find %s]", prog);
1613 for (p_argv = &argv[1]; (str = *p_argv) != (char *) 0; p_argv++)
1614 fprintf (stderr, " %s", str);
1616 fprintf (stderr, "\n");
1622 /* If we cannot find a program we need, complain error. Do this here
1623 since we might not end up needing something that we could not find. */
1626 fatal ("cannot find '%s'", prog);
1628 pex = pex_init (0, "collect2", NULL);
1630 fatal_perror ("pex_init failed");
1632 errmsg = pex_run (pex, PEX_LAST | PEX_SEARCH, argv[0], argv, outname,
1639 fatal_perror (errmsg);
1646 free (response_arg);
1652 fork_execute (const char *prog, char **argv)
1654 struct pex_obj *pex;
1656 pex = collect_execute (prog, argv, NULL, NULL);
1657 do_wait (prog, pex);
1660 /* Unlink a file unless we are debugging. */
1663 maybe_unlink (const char *file)
1666 unlink_if_ordinary (file);
1668 notice ("[Leaving %s]\n", file);
1672 static long sequence_number = 0;
1674 /* Add a name to a linked list. */
1677 add_to_list (struct head *head_ptr, const char *name)
1679 struct id *newid = xcalloc (sizeof (struct id) + strlen (name), 1);
1681 strcpy (newid->name, name);
1683 if (head_ptr->first)
1684 head_ptr->last->next = newid;
1686 head_ptr->first = newid;
1688 /* Check for duplicate symbols. */
1689 for (p = head_ptr->first;
1690 strcmp (name, p->name) != 0;
1695 head_ptr->last->next = 0;
1700 newid->sequence = ++sequence_number;
1701 head_ptr->last = newid;
1705 /* Grab the init priority number from an init function name that
1706 looks like "_GLOBAL_.I.12345.foo". */
1709 extract_init_priority (const char *name)
1713 while (name[pos] == '_')
1715 pos += 10; /* strlen ("GLOBAL__X_") */
1717 /* Extract init_p number from ctor/dtor name. */
1718 pri = atoi (name + pos);
1719 return pri ? pri : DEFAULT_INIT_PRIORITY;
1722 /* Insertion sort the ids from ctor/dtor list HEAD_PTR in descending order.
1723 ctors will be run from right to left, dtors from left to right. */
1726 sort_ids (struct head *head_ptr)
1728 /* id holds the current element to insert. id_next holds the next
1729 element to insert. id_ptr iterates through the already sorted elements
1730 looking for the place to insert id. */
1731 struct id *id, *id_next, **id_ptr;
1733 id = head_ptr->first;
1735 /* We don't have any sorted elements yet. */
1736 head_ptr->first = NULL;
1738 for (; id; id = id_next)
1741 id->sequence = extract_init_priority (id->name);
1743 for (id_ptr = &(head_ptr->first); ; id_ptr = &((*id_ptr)->next))
1745 /* If the sequence numbers are the same, we put the id from the
1746 file later on the command line later in the list. */
1747 || id->sequence > (*id_ptr)->sequence
1748 /* Hack: do lexical compare, too.
1749 || (id->sequence == (*id_ptr)->sequence
1750 && strcmp (id->name, (*id_ptr)->name) > 0) */
1759 /* Now set the sequence numbers properly so write_c_file works. */
1760 for (id = head_ptr->first; id; id = id->next)
1761 id->sequence = ++sequence_number;
1764 /* Write: `prefix', the names on list LIST, `suffix'. */
1767 write_list (FILE *stream, const char *prefix, struct id *list)
1771 fprintf (stream, "%sx%d,\n", prefix, list->sequence);
1776 #if LINK_ELIMINATE_DUPLICATE_LDIRECTORIES
1777 /* Given a STRING, return nonzero if it occurs in the list in range
1778 [ARGS_BEGIN,ARGS_END). */
1781 is_in_args (const char *string, const char **args_begin,
1782 const char **args_end)
1784 const char **args_pointer;
1785 for (args_pointer = args_begin; args_pointer != args_end; ++args_pointer)
1786 if (strcmp (string, *args_pointer) == 0)
1790 #endif /* LINK_ELIMINATE_DUPLICATE_LDIRECTORIES */
1792 #ifdef COLLECT_EXPORT_LIST
1793 /* This function is really used only on AIX, but may be useful. */
1796 is_in_list (const char *prefix, struct id *list)
1800 if (!strcmp (prefix, list->name)) return 1;
1806 #endif /* COLLECT_EXPORT_LIST */
1808 /* Added for debugging purpose. */
1809 #ifdef COLLECT_EXPORT_LIST
1811 dump_list (FILE *stream, const char *prefix, struct id *list)
1815 fprintf (stream, "%s%s,\n", prefix, list->name);
1823 dump_prefix_list (FILE *stream, const char *prefix, struct prefix_list *list)
1827 fprintf (stream, "%s%s,\n", prefix, list->prefix);
1834 write_list_with_asm (FILE *stream, const char *prefix, struct id *list)
1838 fprintf (stream, "%sx%d __asm__ (\"%s\");\n",
1839 prefix, list->sequence, list->name);
1844 /* Write out the constructor and destructor tables statically (for a shared
1845 object), along with the functions to execute them. */
1848 write_c_file_stat (FILE *stream, const char *name ATTRIBUTE_UNUSED)
1852 int frames = (frame_tables.number > 0);
1854 /* Figure out name of output_file, stripping off .so version. */
1855 p = strrchr (output_file, '/');
1871 if (strncmp (q, SHLIB_SUFFIX, strlen (SHLIB_SUFFIX)) == 0)
1873 q += strlen (SHLIB_SUFFIX);
1880 /* q points to null at end of the string (or . of the .so version) */
1881 prefix = XNEWVEC (char, q - p + 1);
1882 strncpy (prefix, p, q - p);
1884 for (r = prefix; *r; r++)
1885 if (!ISALNUM ((unsigned char)*r))
1888 notice ("\nwrite_c_file - output name is %s, prefix is %s\n",
1889 output_file, prefix);
1891 initname = concat ("_GLOBAL__FI_", prefix, NULL);
1892 fininame = concat ("_GLOBAL__FD_", prefix, NULL);
1896 /* Write the tables as C code. */
1898 fprintf (stream, "static int count;\n");
1899 fprintf (stream, "typedef void entry_pt();\n");
1900 write_list_with_asm (stream, "extern entry_pt ", constructors.first);
1904 write_list_with_asm (stream, "extern void *", frame_tables.first);
1906 fprintf (stream, "\tstatic void *frame_table[] = {\n");
1907 write_list (stream, "\t\t&", frame_tables.first);
1908 fprintf (stream, "\t0\n};\n");
1910 /* This must match what's in frame.h. */
1911 fprintf (stream, "struct object {\n");
1912 fprintf (stream, " void *pc_begin;\n");
1913 fprintf (stream, " void *pc_end;\n");
1914 fprintf (stream, " void *fde_begin;\n");
1915 fprintf (stream, " void *fde_array;\n");
1916 fprintf (stream, " __SIZE_TYPE__ count;\n");
1917 fprintf (stream, " struct object *next;\n");
1918 fprintf (stream, "};\n");
1920 fprintf (stream, "extern void __register_frame_info_table (void *, struct object *);\n");
1921 fprintf (stream, "extern void *__deregister_frame_info (void *);\n");
1923 fprintf (stream, "static void reg_frame () {\n");
1924 fprintf (stream, "\tstatic struct object ob;\n");
1925 fprintf (stream, "\t__register_frame_info_table (frame_table, &ob);\n");
1926 fprintf (stream, "\t}\n");
1928 fprintf (stream, "static void dereg_frame () {\n");
1929 fprintf (stream, "\t__deregister_frame_info (frame_table);\n");
1930 fprintf (stream, "\t}\n");
1933 fprintf (stream, "void %s() {\n", initname);
1934 if (constructors.number > 0 || frames)
1936 fprintf (stream, "\tstatic entry_pt *ctors[] = {\n");
1937 write_list (stream, "\t\t", constructors.first);
1939 fprintf (stream, "\treg_frame,\n");
1940 fprintf (stream, "\t};\n");
1941 fprintf (stream, "\tentry_pt **p;\n");
1942 fprintf (stream, "\tif (count++ != 0) return;\n");
1943 fprintf (stream, "\tp = ctors + %d;\n", constructors.number + frames);
1944 fprintf (stream, "\twhile (p > ctors) (*--p)();\n");
1947 fprintf (stream, "\t++count;\n");
1948 fprintf (stream, "}\n");
1949 write_list_with_asm (stream, "extern entry_pt ", destructors.first);
1950 fprintf (stream, "void %s() {\n", fininame);
1951 if (destructors.number > 0 || frames)
1953 fprintf (stream, "\tstatic entry_pt *dtors[] = {\n");
1954 write_list (stream, "\t\t", destructors.first);
1956 fprintf (stream, "\tdereg_frame,\n");
1957 fprintf (stream, "\t};\n");
1958 fprintf (stream, "\tentry_pt **p;\n");
1959 fprintf (stream, "\tif (--count != 0) return;\n");
1960 fprintf (stream, "\tp = dtors;\n");
1961 fprintf (stream, "\twhile (p < dtors + %d) (*p++)();\n",
1962 destructors.number + frames);
1964 fprintf (stream, "}\n");
1968 COLLECT_SHARED_INIT_FUNC(stream, initname);
1969 COLLECT_SHARED_FINI_FUNC(stream, fininame);
1973 /* Write the constructor/destructor tables. */
1975 #ifndef LD_INIT_SWITCH
1977 write_c_file_glob (FILE *stream, const char *name ATTRIBUTE_UNUSED)
1979 /* Write the tables as C code. */
1981 int frames = (frame_tables.number > 0);
1983 fprintf (stream, "typedef void entry_pt();\n\n");
1985 write_list_with_asm (stream, "extern entry_pt ", constructors.first);
1989 write_list_with_asm (stream, "extern void *", frame_tables.first);
1991 fprintf (stream, "\tstatic void *frame_table[] = {\n");
1992 write_list (stream, "\t\t&", frame_tables.first);
1993 fprintf (stream, "\t0\n};\n");
1995 /* This must match what's in frame.h. */
1996 fprintf (stream, "struct object {\n");
1997 fprintf (stream, " void *pc_begin;\n");
1998 fprintf (stream, " void *pc_end;\n");
1999 fprintf (stream, " void *fde_begin;\n");
2000 fprintf (stream, " void *fde_array;\n");
2001 fprintf (stream, " __SIZE_TYPE__ count;\n");
2002 fprintf (stream, " struct object *next;\n");
2003 fprintf (stream, "};\n");
2005 fprintf (stream, "extern void __register_frame_info_table (void *, struct object *);\n");
2006 fprintf (stream, "extern void *__deregister_frame_info (void *);\n");
2008 fprintf (stream, "static void reg_frame () {\n");
2009 fprintf (stream, "\tstatic struct object ob;\n");
2010 fprintf (stream, "\t__register_frame_info_table (frame_table, &ob);\n");
2011 fprintf (stream, "\t}\n");
2013 fprintf (stream, "static void dereg_frame () {\n");
2014 fprintf (stream, "\t__deregister_frame_info (frame_table);\n");
2015 fprintf (stream, "\t}\n");
2018 fprintf (stream, "\nentry_pt * __CTOR_LIST__[] = {\n");
2019 fprintf (stream, "\t(entry_pt *) %d,\n", constructors.number + frames);
2020 write_list (stream, "\t", constructors.first);
2022 fprintf (stream, "\treg_frame,\n");
2023 fprintf (stream, "\t0\n};\n\n");
2025 write_list_with_asm (stream, "extern entry_pt ", destructors.first);
2027 fprintf (stream, "\nentry_pt * __DTOR_LIST__[] = {\n");
2028 fprintf (stream, "\t(entry_pt *) %d,\n", destructors.number + frames);
2029 write_list (stream, "\t", destructors.first);
2031 fprintf (stream, "\tdereg_frame,\n");
2032 fprintf (stream, "\t0\n};\n\n");
2034 fprintf (stream, "extern entry_pt %s;\n", NAME__MAIN);
2035 fprintf (stream, "entry_pt *__main_reference = %s;\n\n", NAME__MAIN);
2037 #endif /* ! LD_INIT_SWITCH */
2040 write_c_file (FILE *stream, const char *name)
2042 #ifndef LD_INIT_SWITCH
2044 write_c_file_glob (stream, name);
2047 write_c_file_stat (stream, name);
2050 #ifdef COLLECT_EXPORT_LIST
2052 write_aix_file (FILE *stream, struct id *list)
2054 for (; list; list = list->next)
2056 fputs (list->name, stream);
2057 putc ('\n', stream);
2062 #ifdef OBJECT_FORMAT_NONE
2064 /* Generic version to scan the name list of the loaded program for
2065 the symbols g++ uses for static constructors and destructors.
2067 The constructor table begins at __CTOR_LIST__ and contains a count
2068 of the number of pointers (or -1 if the constructors are built in a
2069 separate section by the linker), followed by the pointers to the
2070 constructor functions, terminated with a null pointer. The
2071 destructor table has the same format, and begins at __DTOR_LIST__. */
2074 scan_prog_file (const char *prog_name, enum pass which_pass)
2076 void (*int_handler) (int);
2078 void (*quit_handler) (int);
2080 char *real_nm_argv[4];
2081 const char **nm_argv = (const char **) real_nm_argv;
2083 struct pex_obj *pex;
2089 if (which_pass == PASS_SECOND)
2092 /* If we do not have an `nm', complain. */
2093 if (nm_file_name == 0)
2094 fatal ("cannot find 'nm'");
2096 nm_argv[argc++] = nm_file_name;
2097 if (NM_FLAGS[0] != '\0')
2098 nm_argv[argc++] = NM_FLAGS;
2100 nm_argv[argc++] = prog_name;
2101 nm_argv[argc++] = (char *) 0;
2103 /* Trace if needed. */
2106 const char **p_argv;
2109 for (p_argv = &nm_argv[0]; (str = *p_argv) != (char *) 0; p_argv++)
2110 fprintf (stderr, " %s", str);
2112 fprintf (stderr, "\n");
2118 pex = pex_init (PEX_USE_PIPES, "collect2", NULL);
2120 fatal_perror ("pex_init failed");
2122 errmsg = pex_run (pex, 0, nm_file_name, real_nm_argv, NULL, NULL, &err);
2128 fatal_perror (errmsg);
2134 int_handler = (void (*) (int)) signal (SIGINT, SIG_IGN);
2136 quit_handler = (void (*) (int)) signal (SIGQUIT, SIG_IGN);
2139 inf = pex_read_output (pex, 0);
2141 fatal_perror ("can't open nm output");
2144 fprintf (stderr, "\nnm output with constructors/destructors.\n");
2146 /* Read each line of nm output. */
2147 while (fgets (buf, sizeof buf, inf) != (char *) 0)
2152 /* If it contains a constructor or destructor name, add the name
2153 to the appropriate list. */
2155 for (p = buf; (ch = *p) != '\0' && ch != '\n' && ch != '_'; p++)
2156 if (ch == ' ' && p[1] == 'U' && p[2] == ' ')
2163 /* Find the end of the symbol name.
2164 Do not include `|', because Encore nm can tack that on the end. */
2165 for (end = p; (ch2 = *end) != '\0' && !ISSPACE (ch2) && ch2 != '|';
2171 switch (is_ctor_dtor (name))
2174 if (which_pass != PASS_LIB)
2175 add_to_list (&constructors, name);
2179 if (which_pass != PASS_LIB)
2180 add_to_list (&destructors, name);
2184 if (which_pass != PASS_LIB)
2185 fatal ("init function found in object %s", prog_name);
2186 #ifndef LD_INIT_SWITCH
2187 add_to_list (&constructors, name);
2192 if (which_pass != PASS_LIB)
2193 fatal ("fini function found in object %s", prog_name);
2194 #ifndef LD_FINI_SWITCH
2195 add_to_list (&destructors, name);
2200 if (which_pass != PASS_LIB)
2201 add_to_list (&frame_tables, name);
2204 default: /* not a constructor or destructor */
2209 fprintf (stderr, "\t%s\n", buf);
2213 fprintf (stderr, "\n");
2215 do_wait (nm_file_name, pex);
2217 signal (SIGINT, int_handler);
2219 signal (SIGQUIT, quit_handler);
2225 /* Use the List Dynamic Dependencies program to find shared libraries that
2226 the output file depends upon and their initialization/finalization
2227 routines, if any. */
2230 scan_libraries (const char *prog_name)
2232 static struct head libraries; /* list of shared libraries found */
2234 void (*int_handler) (int);
2236 void (*quit_handler) (int);
2238 char *real_ldd_argv[4];
2239 const char **ldd_argv = (const char **) real_ldd_argv;
2241 struct pex_obj *pex;
2247 /* If we do not have an `ldd', complain. */
2248 if (ldd_file_name == 0)
2250 error ("cannot find 'ldd'");
2254 ldd_argv[argc++] = ldd_file_name;
2255 ldd_argv[argc++] = prog_name;
2256 ldd_argv[argc++] = (char *) 0;
2258 /* Trace if needed. */
2261 const char **p_argv;
2264 for (p_argv = &ldd_argv[0]; (str = *p_argv) != (char *) 0; p_argv++)
2265 fprintf (stderr, " %s", str);
2267 fprintf (stderr, "\n");
2273 pex = pex_init (PEX_USE_PIPES, "collect2", NULL);
2275 fatal_perror ("pex_init failed");
2277 errmsg = pex_run (pex, 0, ldd_file_name, real_ldd_argv, NULL, NULL, &err);
2283 fatal_perror (errmsg);
2289 int_handler = (void (*) (int)) signal (SIGINT, SIG_IGN);
2291 quit_handler = (void (*) (int)) signal (SIGQUIT, SIG_IGN);
2294 inf = pex_read_output (pex, 0);
2296 fatal_perror ("can't open ldd output");
2299 notice ("\nldd output with constructors/destructors.\n");
2301 /* Read each line of ldd output. */
2302 while (fgets (buf, sizeof buf, inf) != (char *) 0)
2305 char *name, *end, *p = buf;
2307 /* Extract names of libraries and add to list. */
2308 PARSE_LDD_OUTPUT (p);
2313 if (strncmp (name, "not found", sizeof ("not found") - 1) == 0)
2314 fatal ("dynamic dependency %s not found", buf);
2316 /* Find the end of the symbol name. */
2318 (ch2 = *end) != '\0' && ch2 != '\n' && !ISSPACE (ch2) && ch2 != '|';
2323 if (access (name, R_OK) == 0)
2324 add_to_list (&libraries, name);
2326 fatal ("unable to open dynamic dependency '%s'", buf);
2329 fprintf (stderr, "\t%s\n", buf);
2332 fprintf (stderr, "\n");
2334 do_wait (ldd_file_name, pex);
2336 signal (SIGINT, int_handler);
2338 signal (SIGQUIT, quit_handler);
2341 /* Now iterate through the library list adding their symbols to
2343 for (list = libraries.first; list; list = list->next)
2344 scan_prog_file (list->name, PASS_LIB);
2347 #endif /* LDD_SUFFIX */
2349 #endif /* OBJECT_FORMAT_NONE */
2353 * COFF specific stuff.
2356 #ifdef OBJECT_FORMAT_COFF
2358 #if defined (EXTENDED_COFF)
2360 # define GCC_SYMBOLS(X) (SYMHEADER(X).isymMax + SYMHEADER(X).iextMax)
2361 # define GCC_SYMENT SYMR
2362 # define GCC_OK_SYMBOL(X) ((X).st == stProc || (X).st == stGlobal)
2363 # define GCC_SYMINC(X) (1)
2364 # define GCC_SYMZERO(X) (SYMHEADER(X).isymMax)
2365 # define GCC_CHECK_HDR(X) (PSYMTAB(X) != 0)
2369 # define GCC_SYMBOLS(X) (HEADER(ldptr).f_nsyms)
2370 # define GCC_SYMENT SYMENT
2371 # if defined (C_WEAKEXT)
2372 # define GCC_OK_SYMBOL(X) \
2373 (((X).n_sclass == C_EXT || (X).n_sclass == C_WEAKEXT) && \
2374 ((X).n_scnum > N_UNDEF) && \
2376 || (((X).n_type & N_TMASK) == (DT_NON << N_BTSHFT) \
2377 || ((X).n_type & N_TMASK) == (DT_FCN << N_BTSHFT))))
2378 # define GCC_UNDEF_SYMBOL(X) \
2379 (((X).n_sclass == C_EXT || (X).n_sclass == C_WEAKEXT) && \
2380 ((X).n_scnum == N_UNDEF))
2382 # define GCC_OK_SYMBOL(X) \
2383 (((X).n_sclass == C_EXT) && \
2384 ((X).n_scnum > N_UNDEF) && \
2386 || (((X).n_type & N_TMASK) == (DT_NON << N_BTSHFT) \
2387 || ((X).n_type & N_TMASK) == (DT_FCN << N_BTSHFT))))
2388 # define GCC_UNDEF_SYMBOL(X) \
2389 (((X).n_sclass == C_EXT) && ((X).n_scnum == N_UNDEF))
2391 # define GCC_SYMINC(X) ((X).n_numaux+1)
2392 # define GCC_SYMZERO(X) 0
2394 /* 0757 = U803XTOCMAGIC (AIX 4.3) and 0767 = U64_TOCMAGIC (AIX V5) */
2396 # define GCC_CHECK_HDR(X) \
2397 ((HEADER (X).f_magic == U802TOCMAGIC && ! aix64_flag) \
2398 || (HEADER (X).f_magic == 0767 && aix64_flag))
2400 # define GCC_CHECK_HDR(X) \
2401 ((HEADER (X).f_magic == U802TOCMAGIC && ! aix64_flag) \
2402 || (HEADER (X).f_magic == 0757 && aix64_flag))
2407 #ifdef COLLECT_EXPORT_LIST
2408 /* Array of standard AIX libraries which should not
2409 be scanned for ctors/dtors. */
2410 static const char *const aix_std_libs[] = {
2418 "/usr/lib/libc_r.a",
2419 "/usr/lib/libm_r.a",
2420 "/usr/lib/threads/libc.a",
2421 "/usr/ccs/lib/libc.a",
2422 "/usr/ccs/lib/libm.a",
2423 "/usr/ccs/lib/libc_r.a",
2424 "/usr/ccs/lib/libm_r.a",
2428 /* This function checks the filename and returns 1
2429 if this name matches the location of a standard AIX library. */
2430 static int ignore_library (const char *);
2432 ignore_library (const char *name)
2434 const char *const *p = &aix_std_libs[0];
2435 while (*p++ != NULL)
2436 if (! strcmp (name, *p)) return 1;
2439 #endif /* COLLECT_EXPORT_LIST */
2441 #if defined (HAVE_DECL_LDGETNAME) && !HAVE_DECL_LDGETNAME
2442 extern char *ldgetname (LDFILE *, GCC_SYMENT *);
2445 /* COFF version to scan the name list of the loaded program for
2446 the symbols g++ uses for static constructors and destructors.
2448 The constructor table begins at __CTOR_LIST__ and contains a count
2449 of the number of pointers (or -1 if the constructors are built in a
2450 separate section by the linker), followed by the pointers to the
2451 constructor functions, terminated with a null pointer. The
2452 destructor table has the same format, and begins at __DTOR_LIST__. */
2455 scan_prog_file (const char *prog_name, enum pass which_pass)
2457 LDFILE *ldptr = NULL;
2458 int sym_index, sym_count;
2461 if (which_pass != PASS_FIRST && which_pass != PASS_OBJ)
2464 #ifdef COLLECT_EXPORT_LIST
2465 /* We do not need scanning for some standard C libraries. */
2466 if (which_pass == PASS_FIRST && ignore_library (prog_name))
2469 /* On AIX we have a loop, because there is not much difference
2470 between an object and an archive. This trick allows us to
2471 eliminate scan_libraries() function. */
2475 /* Some platforms (e.g. OSF4) declare ldopen as taking a
2476 non-const char * filename parameter, even though it will not
2477 modify that string. So we must cast away const-ness here,
2478 which will cause -Wcast-qual to burp. */
2479 if ((ldptr = ldopen ((char *)prog_name, ldptr)) != NULL)
2481 if (! MY_ISCOFF (HEADER (ldptr).f_magic))
2482 fatal ("%s: not a COFF file", prog_name);
2484 if (GCC_CHECK_HDR (ldptr))
2486 sym_count = GCC_SYMBOLS (ldptr);
2487 sym_index = GCC_SYMZERO (ldptr);
2489 #ifdef COLLECT_EXPORT_LIST
2490 /* Is current archive member a shared object? */
2491 is_shared = HEADER (ldptr).f_flags & F_SHROBJ;
2494 while (sym_index < sym_count)
2498 if (ldtbread (ldptr, sym_index, &symbol) <= 0)
2500 sym_index += GCC_SYMINC (symbol);
2502 if (GCC_OK_SYMBOL (symbol))
2506 if ((name = ldgetname (ldptr, &symbol)) == NULL)
2507 continue; /* Should never happen. */
2509 #ifdef XCOFF_DEBUGGING_INFO
2510 /* All AIX function names have a duplicate entry
2511 beginning with a dot. */
2516 switch (is_ctor_dtor (name))
2520 add_to_list (&constructors, name);
2521 #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
2522 if (which_pass == PASS_OBJ)
2523 add_to_list (&exports, name);
2529 add_to_list (&destructors, name);
2530 #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
2531 if (which_pass == PASS_OBJ)
2532 add_to_list (&exports, name);
2536 #ifdef COLLECT_EXPORT_LIST
2538 #ifndef LD_INIT_SWITCH
2540 add_to_list (&constructors, name);
2545 #ifndef LD_INIT_SWITCH
2547 add_to_list (&destructors, name);
2554 add_to_list (&frame_tables, name);
2555 #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
2556 if (which_pass == PASS_OBJ)
2557 add_to_list (&exports, name);
2561 default: /* not a constructor or destructor */
2562 #ifdef COLLECT_EXPORT_LIST
2563 /* Explicitly export all global symbols when
2564 building a shared object on AIX, but do not
2565 re-export symbols from another shared object
2566 and do not export symbols if the user
2567 provides an explicit export list. */
2568 if (shared_obj && !is_shared
2569 && which_pass == PASS_OBJ && !export_flag)
2570 add_to_list (&exports, name);
2576 #if !defined(EXTENDED_COFF)
2577 fprintf (stderr, "\tsec=%d class=%d type=%s%o %s\n",
2578 symbol.n_scnum, symbol.n_sclass,
2579 (symbol.n_type ? "0" : ""), symbol.n_type,
2583 "\tiss = %5d, value = %5ld, index = %5d, name = %s\n",
2584 symbol.iss, (long) symbol.value, symbol.index, name);
2589 #ifdef COLLECT_EXPORT_LIST
2592 /* If archive contains both 32-bit and 64-bit objects,
2593 we want to skip objects in other mode so mismatch normal. */
2595 fprintf (stderr, "%s : magic=%o aix64=%d mismatch\n",
2596 prog_name, HEADER (ldptr).f_magic, aix64_flag);
2602 fatal ("%s: cannot open as COFF file", prog_name);
2604 #ifdef COLLECT_EXPORT_LIST
2605 /* On AIX loop continues while there are more members in archive. */
2607 while (ldclose (ldptr) == FAILURE);
2609 /* Otherwise we simply close ldptr. */
2610 (void) ldclose(ldptr);
2613 #endif /* OBJECT_FORMAT_COFF */
2615 #ifdef COLLECT_EXPORT_LIST
2616 /* Given a library name without "lib" prefix, this function
2617 returns a full library name including a path. */
2619 resolve_lib_name (const char *name)
2623 /* Library extensions for AIX dynamic linking. */
2624 const char * const libexts[2] = {"a", "so"};
2626 for (i = 0; libpaths[i]; i++)
2627 if (libpaths[i]->max_len > l)
2628 l = libpaths[i]->max_len;
2630 lib_buf = xmalloc (l + strlen(name) + 10);
2632 for (i = 0; libpaths[i]; i++)
2634 struct prefix_list *list = libpaths[i]->plist;
2635 for (; list; list = list->next)
2637 /* The following lines are needed because path_prefix list
2638 may contain directories both with trailing '/' and
2641 if (list->prefix[strlen(list->prefix)-1] != '/')
2643 for (j = 0; j < 2; j++)
2645 sprintf (lib_buf, "%s%slib%s.%s",
2646 list->prefix, p, name,
2647 libexts[(j + aixrtl_flag) % 2]);
2648 if (debug) fprintf (stderr, "searching for: %s\n", lib_buf);
2649 if (file_exists (lib_buf))
2651 if (debug) fprintf (stderr, "found: %s\n", lib_buf);
2658 fprintf (stderr, "not found\n");
2660 fatal ("library lib%s not found", name);
2663 #endif /* COLLECT_EXPORT_LIST */