1 /* Collect static initialization info into data structures that can be
2 traversed by C++ initialization and finalization routines.
3 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5 Contributed by Chris Smith (csmith@convex.com).
6 Heavily modified by Michael Meissner (meissner@cygnus.com),
7 Per Bothner (bothner@cygnus.com), and John Gilmore (gnu@cygnus.com).
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 2, 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 COPYING. If not, write to the Free
23 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
27 /* Build tables of static constructors and destructors and run ld. */
31 #include "coretypes.h"
34 #if ! defined( SIGCHLD ) && defined( SIGCLD )
35 # define SIGCHLD SIGCLD
38 #ifndef LIBRARY_PATH_ENV
39 #define LIBRARY_PATH_ENV "LIBRARY_PATH"
50 /* On certain systems, we have code that works by scanning the object file
51 directly. But this code uses system-specific header files and library
52 functions, so turn it off in a cross-compiler. Likewise, the names of
53 the utilities are not correct for a cross-compiler; we have to hope that
54 cross-versions are in the proper directories. */
57 #undef OBJECT_FORMAT_COFF
59 #undef REAL_LD_FILE_NAME
60 #undef REAL_NM_FILE_NAME
61 #undef REAL_STRIP_FILE_NAME
64 /* If we cannot use a special method, use the ordinary one:
65 run nm to find what symbols are present.
66 In a cross-compiler, this means you need a cross nm,
67 but that is not quite as unpleasant as special headers. */
69 #if !defined (OBJECT_FORMAT_COFF)
70 #define OBJECT_FORMAT_NONE
73 #ifdef OBJECT_FORMAT_COFF
82 /* Many versions of ldfcn.h define these. */
90 /* Some systems have an ISCOFF macro, but others do not. In some cases
91 the macro may be wrong. MY_ISCOFF is defined in tm.h files for machines
92 that either do not have an ISCOFF macro in /usr/include or for those
96 #define MY_ISCOFF(X) ISCOFF (X)
99 #endif /* OBJECT_FORMAT_COFF */
101 #ifdef OBJECT_FORMAT_NONE
103 /* Default flags to pass to nm. */
105 #define NM_FLAGS "-n"
108 #endif /* OBJECT_FORMAT_NONE */
110 /* Some systems use __main in a way incompatible with its use in gcc, in these
111 cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
112 give the same symbol without quotes for an alternative entry point. */
114 #define NAME__MAIN "__main"
117 /* This must match tree.h. */
118 #define DEFAULT_INIT_PRIORITY 65535
120 #ifndef COLLECT_SHARED_INIT_FUNC
121 #define COLLECT_SHARED_INIT_FUNC(STREAM, FUNC) \
122 fprintf ((STREAM), "void _GLOBAL__DI() {\n\t%s();\n}\n", (FUNC))
124 #ifndef COLLECT_SHARED_FINI_FUNC
125 #define COLLECT_SHARED_FINI_FUNC(STREAM, FUNC) \
126 fprintf ((STREAM), "void _GLOBAL__DD() {\n\t%s();\n}\n", (FUNC))
130 #define SCAN_LIBRARIES
134 int do_collecting = 1;
136 int do_collecting = 0;
139 /* Nonzero if we should suppress the automatic demangling of identifiers
140 in linker error messages. Set from COLLECT_NO_DEMANGLE. */
143 /* Linked lists of constructor and destructor names. */
159 /* Enumeration giving which pass this is for scanning the program file. */
162 PASS_FIRST, /* without constructors */
163 PASS_OBJ, /* individual objects */
164 PASS_LIB, /* looking for shared libraries */
165 PASS_SECOND /* with constructors linked in */
168 int vflag; /* true if -v */
169 static int rflag; /* true if -r */
170 static int strip_flag; /* true if -s */
171 static const char *demangle_flag;
172 #ifdef COLLECT_EXPORT_LIST
173 static int export_flag; /* true if -bE */
174 static int aix64_flag; /* true if -b64 */
175 static int aixrtl_flag; /* true if -brtl */
178 int debug; /* true if -debug */
180 static int shared_obj; /* true if -shared */
182 static const char *c_file; /* <xxx>.c for constructor/destructor list. */
183 static const char *o_file; /* <xxx>.o for constructor/destructor list. */
184 #ifdef COLLECT_EXPORT_LIST
185 static const char *export_file; /* <xxx>.x for AIX export list. */
187 const char *ldout; /* File for ld errors. */
188 static const char *output_file; /* Output file for ld. */
189 static const char *nm_file_name; /* pathname of nm */
191 static const char *ldd_file_name; /* pathname of ldd (or equivalent) */
193 static const char *strip_file_name; /* pathname of strip */
194 const char *c_file_name; /* pathname of gcc */
195 static char *initname, *fininame; /* names of init and fini funcs */
197 static struct head constructors; /* list of constructors found */
198 static struct head destructors; /* list of destructors found */
199 #ifdef COLLECT_EXPORT_LIST
200 static struct head exports; /* list of exported symbols */
202 static struct head frame_tables; /* list of frame unwind info tables */
204 struct obstack temporary_obstack;
205 char * temporary_firstobj;
207 /* Structure to hold all the directories in which to search for files to
212 const char *prefix; /* String to prepend to the path. */
213 struct prefix_list *next; /* Next in linked list. */
218 struct prefix_list *plist; /* List of prefixes to try */
219 int max_len; /* Max length of a prefix in PLIST */
220 const char *name; /* Name of this list (used in config stuff) */
223 #ifdef COLLECT_EXPORT_LIST
224 /* Lists to keep libraries to be scanned for global constructors/destructors. */
225 static struct head libs; /* list of libraries */
226 static struct path_prefix cmdline_lib_dirs; /* directories specified with -L */
227 static struct path_prefix libpath_lib_dirs; /* directories in LIBPATH */
228 static struct path_prefix *libpaths[3] = {&cmdline_lib_dirs,
229 &libpath_lib_dirs, NULL};
232 static void handler (int);
233 static int is_ctor_dtor (const char *);
234 static char *find_a_file (struct path_prefix *, const char *);
235 static void add_prefix (struct path_prefix *, const char *);
236 static void prefix_from_env (const char *, struct path_prefix *);
237 static void prefix_from_string (const char *, struct path_prefix *);
238 static void do_wait (const char *, struct pex_obj *);
239 static void fork_execute (const char *, char **);
240 static void maybe_unlink (const char *);
241 static void add_to_list (struct head *, const char *);
242 static int extract_init_priority (const char *);
243 static void sort_ids (struct head *);
244 static void write_list (FILE *, const char *, struct id *);
245 #ifdef COLLECT_EXPORT_LIST
246 static void dump_list (FILE *, const char *, struct id *);
249 static void dump_prefix_list (FILE *, const char *, struct prefix_list *);
251 static void write_list_with_asm (FILE *, const char *, struct id *);
252 static void write_c_file (FILE *, const char *);
253 static void write_c_file_stat (FILE *, const char *);
254 #ifndef LD_INIT_SWITCH
255 static void write_c_file_glob (FILE *, const char *);
257 static void scan_prog_file (const char *, enum pass);
258 #ifdef SCAN_LIBRARIES
259 static void scan_libraries (const char *);
261 #if LINK_ELIMINATE_DUPLICATE_LDIRECTORIES
262 static int is_in_args (const char *, const char **, const char **);
264 #ifdef COLLECT_EXPORT_LIST
266 static int is_in_list (const char *, struct id *);
268 static void write_aix_file (FILE *, struct id *);
269 static char *resolve_lib_name (const char *);
271 static char *extract_string (const char **);
275 dup2 (int oldfd, int newfd)
284 while ((fd = dup (oldfd)) != newfd && fd >= 0) /* good enough for low fd's */
287 close (fdtmp[--fdx]);
291 #endif /* ! HAVE_DUP2 */
293 /* Delete tempfiles and exit function. */
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);
309 if (ldout != 0 && ldout[0])
312 maybe_unlink (ldout);
315 if (status != 0 && output_file != 0 && output_file[0])
316 maybe_unlink (output_file);
322 /* Notify user of a non-error. */
324 notice (const char *msgid, ...)
328 va_start (ap, msgid);
329 vfprintf (stderr, _(msgid), ap);
333 /* Die when sys call fails. */
336 fatal_perror (const char * msgid, ...)
341 va_start (ap, msgid);
342 fprintf (stderr, "collect2: ");
343 vfprintf (stderr, _(msgid), ap);
344 fprintf (stderr, ": %s\n", xstrerror (e));
347 collect_exit (FATAL_EXIT_CODE);
353 fatal (const char * msgid, ...)
357 va_start (ap, msgid);
358 fprintf (stderr, "collect2: ");
359 vfprintf (stderr, _(msgid), ap);
360 fprintf (stderr, "\n");
363 collect_exit (FATAL_EXIT_CODE);
366 /* Write error message. */
369 error (const char * msgid, ...)
373 va_start (ap, msgid);
374 fprintf (stderr, "collect2: ");
375 vfprintf (stderr, _(msgid), ap);
376 fprintf (stderr, "\n");
380 /* In case obstack is linked in, and abort is defined to fancy_abort,
381 provide a default entry. */
384 fancy_abort (const char *file, int line, const char *func)
386 fatal ("internal gcc abort in %s, at %s:%d", func, file, line);
392 if (c_file != 0 && c_file[0])
393 maybe_unlink (c_file);
395 if (o_file != 0 && o_file[0])
396 maybe_unlink (o_file);
398 if (ldout != 0 && ldout[0])
399 maybe_unlink (ldout);
401 #ifdef COLLECT_EXPORT_LIST
402 if (export_file != 0 && export_file[0])
403 maybe_unlink (export_file);
406 signal (signo, SIG_DFL);
412 file_exists (const char *name)
414 return access (name, R_OK) == 0;
417 /* Parse a reasonable subset of shell quoting syntax. */
420 extract_string (const char **pp)
433 obstack_1grow (&temporary_obstack, c);
434 else if (! inside && c == ' ')
436 else if (! inside && c == '\\')
441 obstack_1grow (&temporary_obstack, c);
444 obstack_1grow (&temporary_obstack, '\0');
446 return obstack_finish (&temporary_obstack);
450 dump_file (const char *name)
452 FILE *stream = fopen (name, "r");
459 while (c = getc (stream),
460 c != EOF && (ISIDNUM (c) || c == '$' || c == '.'))
461 obstack_1grow (&temporary_obstack, c);
462 if (obstack_object_size (&temporary_obstack) > 0)
464 const char *word, *p;
466 obstack_1grow (&temporary_obstack, '\0');
467 word = obstack_finish (&temporary_obstack);
470 ++word, putc ('.', stderr);
472 if (!strncmp (p, USER_LABEL_PREFIX, strlen (USER_LABEL_PREFIX)))
473 p += strlen (USER_LABEL_PREFIX);
475 #ifdef HAVE_LD_DEMANGLE
481 result = cplus_demangle (p, DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE);
487 fputs (result, stderr);
489 diff = strlen (word) - strlen (result);
490 while (diff > 0 && c == ' ')
491 --diff, putc (' ', stderr);
492 while (diff < 0 && c == ' ')
493 ++diff, c = getc (stream);
498 fputs (word, stderr);
501 obstack_free (&temporary_obstack, temporary_firstobj);
510 /* Decide whether the given symbol is: a constructor (1), a destructor
511 (2), a routine in a shared object that calls all the constructors
512 (3) or destructors (4), a DWARF exception-handling table (5), or
513 nothing special (0). */
516 is_ctor_dtor (const char *s)
518 struct names { const char *const name; const int len; const int ret;
519 const int two_underscores; };
521 const struct names *p;
523 const char *orig_s = s;
525 static const struct names special[] = {
526 #ifndef NO_DOLLAR_IN_LABEL
527 { "GLOBAL__I$", sizeof ("GLOBAL__I$")-1, 1, 0 },
528 { "GLOBAL__D$", sizeof ("GLOBAL__D$")-1, 2, 0 },
530 #ifndef NO_DOT_IN_LABEL
531 { "GLOBAL__I.", sizeof ("GLOBAL__I.")-1, 1, 0 },
532 { "GLOBAL__D.", sizeof ("GLOBAL__D.")-1, 2, 0 },
533 #endif /* NO_DOT_IN_LABEL */
534 #endif /* NO_DOLLAR_IN_LABEL */
535 { "GLOBAL__I_", sizeof ("GLOBAL__I_")-1, 1, 0 },
536 { "GLOBAL__D_", sizeof ("GLOBAL__D_")-1, 2, 0 },
537 { "GLOBAL__F_", sizeof ("GLOBAL__F_")-1, 5, 0 },
538 { "GLOBAL__FI_", sizeof ("GLOBAL__FI_")-1, 3, 0 },
539 { "GLOBAL__FD_", sizeof ("GLOBAL__FD_")-1, 4, 0 },
543 while ((ch = *s) == '_')
549 for (p = &special[0]; p->len > 0; p++)
552 && (!p->two_underscores || ((s - orig_s) >= 2))
553 && strncmp(s, p->name, p->len) == 0)
561 /* We maintain two prefix lists: one from COMPILER_PATH environment variable
562 and one from the PATH variable. */
564 static struct path_prefix cpath, path;
567 /* This is the name of the target machine. We use it to form the name
568 of the files to execute. */
570 static const char *const target_machine = TARGET_MACHINE;
573 /* Search for NAME using prefix list PPREFIX. We only look for executable
576 Return 0 if not found, otherwise return its name, allocated with malloc. */
579 find_a_file (struct path_prefix *pprefix, const char *name)
582 struct prefix_list *pl;
583 int len = pprefix->max_len + strlen (name) + 1;
586 fprintf (stderr, "Looking for '%s'\n", name);
588 #ifdef HOST_EXECUTABLE_SUFFIX
589 len += strlen (HOST_EXECUTABLE_SUFFIX);
592 temp = xmalloc (len);
594 /* Determine the filename to execute (special case for absolute paths). */
597 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
598 || (*name && name[1] == ':')
602 if (access (name, X_OK) == 0)
607 fprintf (stderr, " - found: absolute path\n");
612 #ifdef HOST_EXECUTABLE_SUFFIX
613 /* Some systems have a suffix for executable files.
614 So try appending that. */
616 strcat (temp, HOST_EXECUTABLE_SUFFIX);
618 if (access (temp, X_OK) == 0)
623 fprintf (stderr, " - failed to locate using absolute path\n");
626 for (pl = pprefix->plist; pl; pl = pl->next)
630 strcpy (temp, pl->prefix);
633 if (stat (temp, &st) >= 0
634 && ! S_ISDIR (st.st_mode)
635 && access (temp, X_OK) == 0)
638 #ifdef HOST_EXECUTABLE_SUFFIX
639 /* Some systems have a suffix for executable files.
640 So try appending that. */
641 strcat (temp, HOST_EXECUTABLE_SUFFIX);
643 if (stat (temp, &st) >= 0
644 && ! S_ISDIR (st.st_mode)
645 && access (temp, X_OK) == 0)
650 if (debug && pprefix->plist == NULL)
651 fprintf (stderr, " - failed: no entries in prefix list\n");
657 /* Add an entry for PREFIX to prefix list PPREFIX. */
660 add_prefix (struct path_prefix *pprefix, const char *prefix)
662 struct prefix_list *pl, **prev;
667 for (pl = pprefix->plist; pl->next; pl = pl->next)
672 prev = &pprefix->plist;
674 /* Keep track of the longest prefix. */
676 len = strlen (prefix);
677 if (len > pprefix->max_len)
678 pprefix->max_len = len;
680 pl = xmalloc (sizeof (struct prefix_list));
681 pl->prefix = xstrdup (prefix);
686 pl->next = (struct prefix_list *) 0;
690 /* Take the value of the environment variable ENV, break it into a path, and
691 add of the entries to PPREFIX. */
694 prefix_from_env (const char *env, struct path_prefix *pprefix)
697 GET_ENVIRONMENT (p, env);
700 prefix_from_string (p, pprefix);
704 prefix_from_string (const char *p, struct path_prefix *pprefix)
706 const char *startp, *endp;
707 char *nstore = xmalloc (strlen (p) + 3);
710 fprintf (stderr, "Convert string '%s' into prefixes, separator = '%c'\n", p, PATH_SEPARATOR);
715 if (*endp == PATH_SEPARATOR || *endp == 0)
717 strncpy (nstore, startp, endp-startp);
720 strcpy (nstore, "./");
722 else if (! IS_DIR_SEPARATOR (endp[-1]))
724 nstore[endp-startp] = DIR_SEPARATOR;
725 nstore[endp-startp+1] = 0;
728 nstore[endp-startp] = 0;
731 fprintf (stderr, " - add prefix: %s\n", nstore);
733 add_prefix (pprefix, nstore);
736 endp = startp = endp + 1;
746 main (int argc, char **argv)
748 static const char *const ld_suffix = "ld";
749 static const char *const real_ld_suffix = "real-ld";
750 static const char *const collect_ld_suffix = "collect-ld";
751 static const char *const nm_suffix = "nm";
752 static const char *const gnm_suffix = "gnm";
754 static const char *const ldd_suffix = LDD_SUFFIX;
756 static const char *const strip_suffix = "strip";
757 static const char *const gstrip_suffix = "gstrip";
760 /* If we look for a program in the compiler directories, we just use
761 the short name, since these directories are already system-specific.
762 But it we look for a program in the system directories, we need to
763 qualify the program name with the target machine. */
765 const char *const full_ld_suffix =
766 concat(target_machine, "-", ld_suffix, NULL);
767 const char *const full_nm_suffix =
768 concat (target_machine, "-", nm_suffix, NULL);
769 const char *const full_gnm_suffix =
770 concat (target_machine, "-", gnm_suffix, NULL);
772 const char *const full_ldd_suffix =
773 concat (target_machine, "-", ldd_suffix, NULL);
775 const char *const full_strip_suffix =
776 concat (target_machine, "-", strip_suffix, NULL);
777 const char *const full_gstrip_suffix =
778 concat (target_machine, "-", gstrip_suffix, NULL);
780 const char *const full_ld_suffix = ld_suffix;
781 const char *const full_nm_suffix = nm_suffix;
782 const char *const full_gnm_suffix = gnm_suffix;
784 const char *const full_ldd_suffix = ldd_suffix;
786 const char *const full_strip_suffix = strip_suffix;
787 const char *const full_gstrip_suffix = gstrip_suffix;
788 #endif /* CROSS_COMPILE */
792 #ifdef COLLECT_EXPORT_LIST
795 const char *ld_file_name;
806 int num_c_args = argc+9;
808 no_demangle = !! getenv ("COLLECT_NO_DEMANGLE");
810 /* Suppress demangling by the real linker, which may be broken. */
811 putenv (xstrdup ("COLLECT_NO_DEMANGLE="));
813 #if defined (COLLECT2_HOST_INITIALIZATION)
814 /* Perform system dependent initialization, if necessary. */
815 COLLECT2_HOST_INITIALIZATION;
819 /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
820 receive the signal. A different setting is inheritable */
821 signal (SIGCHLD, SIG_DFL);
826 /* Do not invoke xcalloc before this point, since locale needs to be
827 set first, in case a diagnostic is issued. */
829 ld1 = (const char **)(ld1_argv = xcalloc(sizeof (char *), argc+4));
830 ld2 = (const char **)(ld2_argv = xcalloc(sizeof (char *), argc+11));
831 object = (const char **)(object_lst = xcalloc(sizeof (char *), argc));
837 /* Parse command line early for instances of -debug. This allows
838 the debug flag to be set before functions like find_a_file()
843 for (i = 1; argv[i] != NULL; i ++)
845 if (! strcmp (argv[i], "-debug"))
851 #ifndef DEFAULT_A_OUT_NAME
852 output_file = "a.out";
854 output_file = DEFAULT_A_OUT_NAME;
857 obstack_begin (&temporary_obstack, 0);
858 temporary_firstobj = obstack_alloc (&temporary_obstack, 0);
860 #ifndef HAVE_LD_DEMANGLE
861 current_demangling_style = auto_demangling;
863 p = getenv ("COLLECT_GCC_OPTIONS");
866 const char *q = extract_string (&p);
867 if (*q == '-' && (q[1] == 'm' || q[1] == 'f'))
870 obstack_free (&temporary_obstack, temporary_firstobj);
872 /* -fno-profile-arcs -fno-test-coverage -fno-branch-probabilities
873 -fno-exceptions -w */
876 c_ptr = (const char **) (c_argv = xcalloc (sizeof (char *), num_c_args));
879 fatal ("no arguments");
882 if (signal (SIGQUIT, SIG_IGN) != SIG_IGN)
883 signal (SIGQUIT, handler);
885 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
886 signal (SIGINT, handler);
888 if (signal (SIGALRM, SIG_IGN) != SIG_IGN)
889 signal (SIGALRM, handler);
892 if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
893 signal (SIGHUP, handler);
895 if (signal (SIGSEGV, SIG_IGN) != SIG_IGN)
896 signal (SIGSEGV, handler);
898 if (signal (SIGBUS, SIG_IGN) != SIG_IGN)
899 signal (SIGBUS, handler);
902 /* Extract COMPILER_PATH and PATH into our prefix list. */
903 prefix_from_env ("COMPILER_PATH", &cpath);
904 prefix_from_env ("PATH", &path);
906 /* Try to discover a valid linker/nm/strip to use. */
908 /* Maybe we know the right file to use (if not cross). */
910 #ifdef DEFAULT_LINKER
911 if (access (DEFAULT_LINKER, X_OK) == 0)
912 ld_file_name = DEFAULT_LINKER;
913 if (ld_file_name == 0)
915 #ifdef REAL_LD_FILE_NAME
916 ld_file_name = find_a_file (&path, REAL_LD_FILE_NAME);
917 if (ld_file_name == 0)
919 /* Search the (target-specific) compiler dirs for ld'. */
920 ld_file_name = find_a_file (&cpath, real_ld_suffix);
921 /* Likewise for `collect-ld'. */
922 if (ld_file_name == 0)
923 ld_file_name = find_a_file (&cpath, collect_ld_suffix);
924 /* Search the compiler directories for `ld'. We have protection against
925 recursive calls in find_a_file. */
926 if (ld_file_name == 0)
927 ld_file_name = find_a_file (&cpath, ld_suffix);
928 /* Search the ordinary system bin directories
929 for `ld' (if native linking) or `TARGET-ld' (if cross). */
930 if (ld_file_name == 0)
931 ld_file_name = find_a_file (&path, full_ld_suffix);
933 #ifdef REAL_NM_FILE_NAME
934 nm_file_name = find_a_file (&path, REAL_NM_FILE_NAME);
935 if (nm_file_name == 0)
937 nm_file_name = find_a_file (&cpath, gnm_suffix);
938 if (nm_file_name == 0)
939 nm_file_name = find_a_file (&path, full_gnm_suffix);
940 if (nm_file_name == 0)
941 nm_file_name = find_a_file (&cpath, nm_suffix);
942 if (nm_file_name == 0)
943 nm_file_name = find_a_file (&path, full_nm_suffix);
946 ldd_file_name = find_a_file (&cpath, ldd_suffix);
947 if (ldd_file_name == 0)
948 ldd_file_name = find_a_file (&path, full_ldd_suffix);
951 #ifdef REAL_STRIP_FILE_NAME
952 strip_file_name = find_a_file (&path, REAL_STRIP_FILE_NAME);
953 if (strip_file_name == 0)
955 strip_file_name = find_a_file (&cpath, gstrip_suffix);
956 if (strip_file_name == 0)
957 strip_file_name = find_a_file (&path, full_gstrip_suffix);
958 if (strip_file_name == 0)
959 strip_file_name = find_a_file (&cpath, strip_suffix);
960 if (strip_file_name == 0)
961 strip_file_name = find_a_file (&path, full_strip_suffix);
963 /* Determine the full path name of the C compiler to use. */
964 c_file_name = getenv ("COLLECT_GCC");
965 if (c_file_name == 0)
968 c_file_name = concat (target_machine, "-gcc", NULL);
974 p = find_a_file (&cpath, c_file_name);
976 /* Here it should be safe to use the system search path since we should have
977 already qualified the name of the compiler when it is needed. */
979 p = find_a_file (&path, c_file_name);
984 *ld1++ = *ld2++ = ld_file_name;
986 /* Make temp file names. */
987 c_file = make_temp_file (".c");
988 o_file = make_temp_file (".o");
989 #ifdef COLLECT_EXPORT_LIST
990 export_file = make_temp_file (".x");
992 ldout = make_temp_file (".ld");
993 *c_ptr++ = c_file_name;
1000 #ifdef COLLECT_EXPORT_LIST
1001 /* Generate a list of directories from LIBPATH. */
1002 prefix_from_env ("LIBPATH", &libpath_lib_dirs);
1003 /* Add to this list also two standard directories where
1004 AIX loader always searches for libraries. */
1005 add_prefix (&libpath_lib_dirs, "/lib");
1006 add_prefix (&libpath_lib_dirs, "/usr/lib");
1009 /* Get any options that the upper GCC wants to pass to the sub-GCC.
1011 AIX support needs to know if -shared has been specified before
1012 parsing commandline arguments. */
1014 p = getenv ("COLLECT_GCC_OPTIONS");
1017 const char *q = extract_string (&p);
1018 if (*q == '-' && (q[1] == 'm' || q[1] == 'f'))
1019 *c_ptr++ = xstrdup (q);
1020 if (strcmp (q, "-EL") == 0 || strcmp (q, "-EB") == 0)
1021 *c_ptr++ = xstrdup (q);
1022 if (strcmp (q, "-shared") == 0)
1024 if (*q == '-' && q[1] == 'B')
1026 *c_ptr++ = xstrdup (q);
1029 q = extract_string (&p);
1030 *c_ptr++ = xstrdup (q);
1034 obstack_free (&temporary_obstack, temporary_firstobj);
1035 *c_ptr++ = "-fno-profile-arcs";
1036 *c_ptr++ = "-fno-test-coverage";
1037 *c_ptr++ = "-fno-branch-probabilities";
1038 *c_ptr++ = "-fno-exceptions";
1041 /* !!! When GCC calls collect2,
1042 it does not know whether it is calling collect2 or ld.
1043 So collect2 cannot meaningfully understand any options
1044 except those ld understands.
1045 If you propose to make GCC pass some other option,
1046 just imagine what will happen if ld is really ld!!! */
1048 /* Parse arguments. Remember output file spec, pass the rest to ld. */
1049 /* After the first file, put in the c++ rt0. */
1052 #ifdef HAVE_LD_DEMANGLE
1053 if (!demangle_flag && !no_demangle)
1054 demangle_flag = "--demangle";
1056 *ld1++ = *ld2++ = demangle_flag;
1058 while ((arg = *++argv) != (char *) 0)
1060 *ld1++ = *ld2++ = arg;
1066 #ifdef COLLECT_EXPORT_LIST
1067 /* We want to disable automatic exports on AIX when user
1068 explicitly puts an export list in command line */
1070 if (arg[2] == 'E' || strncmp (&arg[2], "export", 6) == 0)
1072 else if (arg[2] == '6' && arg[3] == '4')
1074 else if (arg[2] == 'r' && arg[3] == 't' && arg[4] == 'l')
1080 if (!strcmp (arg, "-debug"))
1082 /* Already parsed. */
1086 if (!strcmp (arg, "-dynamic-linker") && argv[1])
1089 *ld1++ = *ld2++ = *argv;
1096 /* place o_file BEFORE this argument! */
1102 #ifdef COLLECT_EXPORT_LIST
1104 /* Resolving full library name. */
1105 const char *s = resolve_lib_name (arg+2);
1107 /* Saving a full library name. */
1108 add_to_list (&libs, s);
1113 #ifdef COLLECT_EXPORT_LIST
1114 /* Saving directories where to search for libraries. */
1116 add_prefix (&cmdline_lib_dirs, arg+2);
1119 #if LINK_ELIMINATE_DUPLICATE_LDIRECTORIES
1121 if (is_in_args (arg, (const char **) ld1_argv, ld1-1))
1124 #endif /* LINK_ELIMINATE_DUPLICATE_LDIRECTORIES */
1129 output_file = *ld1++ = *ld2++ = *++argv;
1131 #ifdef SWITCHES_NEED_SPACES
1132 && ! strchr (SWITCHES_NEED_SPACES, arg[1])
1136 output_file = &arg[2];
1145 if (arg[2] == '\0' && do_collecting)
1147 /* We must strip after the nm run, otherwise C++ linking
1148 will not work. Thus we strip in the second ld run, or
1149 else with strip if there is no second ld run. */
1161 if (strcmp (arg, "--no-demangle") == 0)
1163 demangle_flag = arg;
1168 else if (strncmp (arg, "--demangle", 10) == 0)
1170 demangle_flag = arg;
1172 #ifndef HAVE_LD_DEMANGLE
1175 enum demangling_styles style
1176 = cplus_demangle_name_to_style (arg+11);
1177 if (style == unknown_demangling)
1178 error ("unknown demangling style '%s'", arg+11);
1180 current_demangling_style = style;
1189 else if ((p = strrchr (arg, '.')) != (char *) 0
1190 && (strcmp (p, ".o") == 0 || strcmp (p, ".a") == 0
1191 || strcmp (p, ".so") == 0 || strcmp (p, ".lo") == 0
1192 || strcmp (p, ".obj") == 0))
1201 /* place o_file BEFORE this argument! */
1207 if (p[1] == 'o' || p[1] == 'l')
1209 #ifdef COLLECT_EXPORT_LIST
1210 /* libraries can be specified directly, i.e. without -l flag. */
1213 /* Saving a full library name. */
1214 add_to_list (&libs, arg);
1220 #ifdef COLLECT_EXPORT_LIST
1221 /* This is added only for debugging purposes. */
1224 fprintf (stderr, "List of libraries:\n");
1225 dump_list (stderr, "\t", libs.first);
1228 /* The AIX linker will discard static constructors in object files if
1229 nothing else in the file is referenced, so look at them first. */
1231 const char **export_object_lst = (const char **)object_lst;
1233 while (export_object_lst < object)
1234 scan_prog_file (*export_object_lst++, PASS_OBJ);
1237 struct id *list = libs.first;
1239 for (; list; list = list->next)
1240 scan_prog_file (list->name, PASS_FIRST);
1245 char *buf = concat ("-bE:", export_file, NULL);
1250 exportf = fopen (export_file, "w");
1251 if (exportf == (FILE *) 0)
1252 fatal_perror ("fopen %s", export_file);
1253 write_aix_file (exportf, exports.first);
1254 if (fclose (exportf))
1255 fatal_perror ("fclose %s", export_file);
1260 *c_ptr = *ld1 = *object = (char *) 0;
1264 notice ("collect2 version %s", version_string);
1265 #ifdef TARGET_VERSION
1268 fprintf (stderr, "\n");
1274 fprintf (stderr, "ld_file_name = %s\n",
1275 (ld_file_name ? ld_file_name : "not found"));
1276 fprintf (stderr, "c_file_name = %s\n",
1277 (c_file_name ? c_file_name : "not found"));
1278 fprintf (stderr, "nm_file_name = %s\n",
1279 (nm_file_name ? nm_file_name : "not found"));
1281 fprintf (stderr, "ldd_file_name = %s\n",
1282 (ldd_file_name ? ldd_file_name : "not found"));
1284 fprintf (stderr, "strip_file_name = %s\n",
1285 (strip_file_name ? strip_file_name : "not found"));
1286 fprintf (stderr, "c_file = %s\n",
1287 (c_file ? c_file : "not found"));
1288 fprintf (stderr, "o_file = %s\n",
1289 (o_file ? o_file : "not found"));
1291 ptr = getenv ("COLLECT_GCC_OPTIONS");
1293 fprintf (stderr, "COLLECT_GCC_OPTIONS = %s\n", ptr);
1295 ptr = getenv ("COLLECT_GCC");
1297 fprintf (stderr, "COLLECT_GCC = %s\n", ptr);
1299 ptr = getenv ("COMPILER_PATH");
1301 fprintf (stderr, "COMPILER_PATH = %s\n", ptr);
1303 ptr = getenv (LIBRARY_PATH_ENV);
1305 fprintf (stderr, "%-20s= %s\n", LIBRARY_PATH_ENV, ptr);
1307 fprintf (stderr, "\n");
1310 /* Load the program, searching all libraries and attempting to provide
1311 undefined symbols from repository information. */
1313 /* On AIX we do this later. */
1314 #ifndef COLLECT_EXPORT_LIST
1315 do_tlink (ld1_argv, object_lst);
1318 /* If -r or they will be run via some other method, do not build the
1319 constructor or destructor list, just return now. */
1321 #ifndef COLLECT_EXPORT_LIST
1326 #ifdef COLLECT_EXPORT_LIST
1327 /* Do the link we avoided above if we are exiting. */
1328 do_tlink (ld1_argv, object_lst);
1330 /* But make sure we delete the export file we may have created. */
1331 if (export_file != 0 && export_file[0])
1332 maybe_unlink (export_file);
1334 maybe_unlink (c_file);
1335 maybe_unlink (o_file);
1339 /* Examine the namelist with nm and search it for static constructors
1340 and destructors to call.
1341 Write the constructor and destructor tables to a .s file and reload. */
1343 /* On AIX we already scanned for global constructors/destructors. */
1344 #ifndef COLLECT_EXPORT_LIST
1345 scan_prog_file (output_file, PASS_FIRST);
1348 #ifdef SCAN_LIBRARIES
1349 scan_libraries (output_file);
1354 notice ("%d constructor(s) found\n", constructors.number);
1355 notice ("%d destructor(s) found\n", destructors.number);
1356 notice ("%d frame table(s) found\n", frame_tables.number);
1359 if (constructors.number == 0 && destructors.number == 0
1360 && frame_tables.number == 0
1361 #if defined (SCAN_LIBRARIES) || defined (COLLECT_EXPORT_LIST)
1362 /* If we will be running these functions ourselves, we want to emit
1363 stubs into the shared library so that we do not have to relink
1364 dependent programs when we add static objects. */
1369 #ifdef COLLECT_EXPORT_LIST
1370 /* Do tlink without additional code generation. */
1371 do_tlink (ld1_argv, object_lst);
1373 /* Strip now if it was requested on the command line. */
1376 char **real_strip_argv = xcalloc (sizeof (char *), 3);
1377 const char ** strip_argv = (const char **) real_strip_argv;
1379 strip_argv[0] = strip_file_name;
1380 strip_argv[1] = output_file;
1381 strip_argv[2] = (char *) 0;
1382 fork_execute ("strip", real_strip_argv);
1385 #ifdef COLLECT_EXPORT_LIST
1386 maybe_unlink (export_file);
1388 maybe_unlink (c_file);
1389 maybe_unlink (o_file);
1393 /* Sort ctor and dtor lists by priority. */
1394 sort_ids (&constructors);
1395 sort_ids (&destructors);
1397 maybe_unlink(output_file);
1398 outf = fopen (c_file, "w");
1399 if (outf == (FILE *) 0)
1400 fatal_perror ("fopen %s", c_file);
1402 write_c_file (outf, c_file);
1405 fatal_perror ("fclose %s", c_file);
1407 /* Tell the linker that we have initializer and finalizer functions. */
1408 #ifdef LD_INIT_SWITCH
1409 #ifdef COLLECT_EXPORT_LIST
1410 *ld2++ = concat (LD_INIT_SWITCH, ":", initname, ":", fininame, NULL);
1412 *ld2++ = LD_INIT_SWITCH;
1414 *ld2++ = LD_FINI_SWITCH;
1419 #ifdef COLLECT_EXPORT_LIST
1422 /* If we did not add export flag to link arguments before, add it to
1423 second link phase now. No new exports should have been added. */
1424 if (! exports.first)
1425 *ld2++ = concat ("-bE:", export_file, NULL);
1427 #ifndef LD_INIT_SWITCH
1428 add_to_list (&exports, initname);
1429 add_to_list (&exports, fininame);
1430 add_to_list (&exports, "_GLOBAL__DI");
1431 add_to_list (&exports, "_GLOBAL__DD");
1433 exportf = fopen (export_file, "w");
1434 if (exportf == (FILE *) 0)
1435 fatal_perror ("fopen %s", export_file);
1436 write_aix_file (exportf, exports.first);
1437 if (fclose (exportf))
1438 fatal_perror ("fclose %s", export_file);
1442 /* End of arguments to second link phase. */
1447 fprintf (stderr, "\n========== output_file = %s, c_file = %s\n",
1448 output_file, c_file);
1449 write_c_file (stderr, "stderr");
1450 fprintf (stderr, "========== end of c_file\n\n");
1451 #ifdef COLLECT_EXPORT_LIST
1452 fprintf (stderr, "\n========== export_file = %s\n", export_file);
1453 write_aix_file (stderr, exports.first);
1454 fprintf (stderr, "========== end of export_file\n\n");
1458 /* Assemble the constructor and destructor tables.
1459 Link the tables in with the rest of the program. */
1461 fork_execute ("gcc", c_argv);
1462 #ifdef COLLECT_EXPORT_LIST
1463 /* On AIX we must call tlink because of possible templates resolution. */
1464 do_tlink (ld2_argv, object_lst);
1466 /* Otherwise, simply call ld because tlink is already done. */
1467 fork_execute ("ld", ld2_argv);
1469 /* Let scan_prog_file do any final mods (OSF/rose needs this for
1470 constructors/destructors in shared libraries. */
1471 scan_prog_file (output_file, PASS_SECOND);
1474 maybe_unlink (c_file);
1475 maybe_unlink (o_file);
1477 #ifdef COLLECT_EXPORT_LIST
1478 maybe_unlink (export_file);
1485 /* Wait for a process to finish, and exit if a nonzero status is found. */
1488 collect_wait (const char *prog, struct pex_obj *pex)
1492 if (!pex_get_status (pex, 1, &status))
1493 fatal_perror ("can't get program status");
1498 if (WIFSIGNALED (status))
1500 int sig = WTERMSIG (status);
1501 error ("%s terminated with signal %d [%s]%s",
1502 prog, sig, strsignal(sig),
1503 WCOREDUMP(status) ? ", core dumped" : "");
1504 collect_exit (FATAL_EXIT_CODE);
1507 if (WIFEXITED (status))
1508 return WEXITSTATUS (status);
1514 do_wait (const char *prog, struct pex_obj *pex)
1516 int ret = collect_wait (prog, pex);
1519 error ("%s returned %d exit status", prog, ret);
1525 /* Execute a program, and wait for the reply. */
1528 collect_execute (const char *prog, char **argv, const char *redir)
1530 struct pex_obj *pex;
1540 fprintf (stderr, "%s", argv[0]);
1542 notice ("[cannot find %s]", prog);
1544 for (p_argv = &argv[1]; (str = *p_argv) != (char *) 0; p_argv++)
1545 fprintf (stderr, " %s", str);
1547 fprintf (stderr, "\n");
1553 /* If we cannot find a program we need, complain error. Do this here
1554 since we might not end up needing something that we could not find. */
1557 fatal ("cannot find '%s'", prog);
1559 pex = pex_init (0, "collect2", NULL);
1561 fatal_perror ("pex_init failed");
1563 errmsg = pex_run (pex,
1564 (PEX_LAST | PEX_SEARCH
1565 | (redir ? PEX_STDERR_TO_STDOUT : 0)),
1566 argv[0], argv, redir, NULL, &err);
1572 fatal_perror (errmsg);
1582 fork_execute (const char *prog, char **argv)
1584 struct pex_obj *pex;
1586 pex = collect_execute (prog, argv, NULL);
1587 do_wait (prog, pex);
1590 /* Unlink a file unless we are debugging. */
1593 maybe_unlink (const char *file)
1598 notice ("[Leaving %s]\n", file);
1602 static long sequence_number = 0;
1604 /* Add a name to a linked list. */
1607 add_to_list (struct head *head_ptr, const char *name)
1609 struct id *newid = xcalloc (sizeof (struct id) + strlen (name), 1);
1611 strcpy (newid->name, name);
1613 if (head_ptr->first)
1614 head_ptr->last->next = newid;
1616 head_ptr->first = newid;
1618 /* Check for duplicate symbols. */
1619 for (p = head_ptr->first;
1620 strcmp (name, p->name) != 0;
1625 head_ptr->last->next = 0;
1630 newid->sequence = ++sequence_number;
1631 head_ptr->last = newid;
1635 /* Grab the init priority number from an init function name that
1636 looks like "_GLOBAL_.I.12345.foo". */
1639 extract_init_priority (const char *name)
1643 while (name[pos] == '_')
1645 pos += 10; /* strlen ("GLOBAL__X_") */
1647 /* Extract init_p number from ctor/dtor name. */
1648 pri = atoi (name + pos);
1649 return pri ? pri : DEFAULT_INIT_PRIORITY;
1652 /* Insertion sort the ids from ctor/dtor list HEAD_PTR in descending order.
1653 ctors will be run from right to left, dtors from left to right. */
1656 sort_ids (struct head *head_ptr)
1658 /* id holds the current element to insert. id_next holds the next
1659 element to insert. id_ptr iterates through the already sorted elements
1660 looking for the place to insert id. */
1661 struct id *id, *id_next, **id_ptr;
1663 id = head_ptr->first;
1665 /* We don't have any sorted elements yet. */
1666 head_ptr->first = NULL;
1668 for (; id; id = id_next)
1671 id->sequence = extract_init_priority (id->name);
1673 for (id_ptr = &(head_ptr->first); ; id_ptr = &((*id_ptr)->next))
1675 /* If the sequence numbers are the same, we put the id from the
1676 file later on the command line later in the list. */
1677 || id->sequence > (*id_ptr)->sequence
1678 /* Hack: do lexical compare, too.
1679 || (id->sequence == (*id_ptr)->sequence
1680 && strcmp (id->name, (*id_ptr)->name) > 0) */
1689 /* Now set the sequence numbers properly so write_c_file works. */
1690 for (id = head_ptr->first; id; id = id->next)
1691 id->sequence = ++sequence_number;
1694 /* Write: `prefix', the names on list LIST, `suffix'. */
1697 write_list (FILE *stream, const char *prefix, struct id *list)
1701 fprintf (stream, "%sx%d,\n", prefix, list->sequence);
1706 #if LINK_ELIMINATE_DUPLICATE_LDIRECTORIES
1707 /* Given a STRING, return nonzero if it occurs in the list in range
1708 [ARGS_BEGIN,ARGS_END). */
1711 is_in_args (const char *string, const char **args_begin,
1712 const char **args_end)
1714 const char **args_pointer;
1715 for (args_pointer = args_begin; args_pointer != args_end; ++args_pointer)
1716 if (strcmp (string, *args_pointer) == 0)
1720 #endif /* LINK_ELIMINATE_DUPLICATE_LDIRECTORIES */
1722 #ifdef COLLECT_EXPORT_LIST
1723 /* This function is really used only on AIX, but may be useful. */
1726 is_in_list (const char *prefix, struct id *list)
1730 if (!strcmp (prefix, list->name)) return 1;
1736 #endif /* COLLECT_EXPORT_LIST */
1738 /* Added for debugging purpose. */
1739 #ifdef COLLECT_EXPORT_LIST
1741 dump_list (FILE *stream, const char *prefix, struct id *list)
1745 fprintf (stream, "%s%s,\n", prefix, list->name);
1753 dump_prefix_list (FILE *stream, const char *prefix, struct prefix_list *list)
1757 fprintf (stream, "%s%s,\n", prefix, list->prefix);
1764 write_list_with_asm (FILE *stream, const char *prefix, struct id *list)
1768 fprintf (stream, "%sx%d __asm__ (\"%s\");\n",
1769 prefix, list->sequence, list->name);
1774 /* Write out the constructor and destructor tables statically (for a shared
1775 object), along with the functions to execute them. */
1778 write_c_file_stat (FILE *stream, const char *name ATTRIBUTE_UNUSED)
1782 int frames = (frame_tables.number > 0);
1784 /* Figure out name of output_file, stripping off .so version. */
1785 p = strrchr (output_file, '/');
1801 if (strncmp (q, ".so", 3) == 0)
1810 /* q points to null at end of the string (or . of the .so version) */
1811 prefix = xmalloc (q - p + 1);
1812 strncpy (prefix, p, q - p);
1814 for (r = prefix; *r; r++)
1815 if (!ISALNUM ((unsigned char)*r))
1818 notice ("\nwrite_c_file - output name is %s, prefix is %s\n",
1819 output_file, prefix);
1821 initname = concat ("_GLOBAL__FI_", prefix, NULL);
1822 fininame = concat ("_GLOBAL__FD_", prefix, NULL);
1826 /* Write the tables as C code. */
1828 fprintf (stream, "static int count;\n");
1829 fprintf (stream, "typedef void entry_pt();\n");
1830 write_list_with_asm (stream, "extern entry_pt ", constructors.first);
1834 write_list_with_asm (stream, "extern void *", frame_tables.first);
1836 fprintf (stream, "\tstatic void *frame_table[] = {\n");
1837 write_list (stream, "\t\t&", frame_tables.first);
1838 fprintf (stream, "\t0\n};\n");
1840 /* This must match what's in frame.h. */
1841 fprintf (stream, "struct object {\n");
1842 fprintf (stream, " void *pc_begin;\n");
1843 fprintf (stream, " void *pc_end;\n");
1844 fprintf (stream, " void *fde_begin;\n");
1845 fprintf (stream, " void *fde_array;\n");
1846 fprintf (stream, " __SIZE_TYPE__ count;\n");
1847 fprintf (stream, " struct object *next;\n");
1848 fprintf (stream, "};\n");
1850 fprintf (stream, "extern void __register_frame_info_table (void *, struct object *);\n");
1851 fprintf (stream, "extern void *__deregister_frame_info (void *);\n");
1853 fprintf (stream, "static void reg_frame () {\n");
1854 fprintf (stream, "\tstatic struct object ob;\n");
1855 fprintf (stream, "\t__register_frame_info_table (frame_table, &ob);\n");
1856 fprintf (stream, "\t}\n");
1858 fprintf (stream, "static void dereg_frame () {\n");
1859 fprintf (stream, "\t__deregister_frame_info (frame_table);\n");
1860 fprintf (stream, "\t}\n");
1863 fprintf (stream, "void %s() {\n", initname);
1864 if (constructors.number > 0 || frames)
1866 fprintf (stream, "\tstatic entry_pt *ctors[] = {\n");
1867 write_list (stream, "\t\t", constructors.first);
1869 fprintf (stream, "\treg_frame,\n");
1870 fprintf (stream, "\t};\n");
1871 fprintf (stream, "\tentry_pt **p;\n");
1872 fprintf (stream, "\tif (count++ != 0) return;\n");
1873 fprintf (stream, "\tp = ctors + %d;\n", constructors.number + frames);
1874 fprintf (stream, "\twhile (p > ctors) (*--p)();\n");
1877 fprintf (stream, "\t++count;\n");
1878 fprintf (stream, "}\n");
1879 write_list_with_asm (stream, "extern entry_pt ", destructors.first);
1880 fprintf (stream, "void %s() {\n", fininame);
1881 if (destructors.number > 0 || frames)
1883 fprintf (stream, "\tstatic entry_pt *dtors[] = {\n");
1884 write_list (stream, "\t\t", destructors.first);
1886 fprintf (stream, "\tdereg_frame,\n");
1887 fprintf (stream, "\t};\n");
1888 fprintf (stream, "\tentry_pt **p;\n");
1889 fprintf (stream, "\tif (--count != 0) return;\n");
1890 fprintf (stream, "\tp = dtors;\n");
1891 fprintf (stream, "\twhile (p < dtors + %d) (*p++)();\n",
1892 destructors.number + frames);
1894 fprintf (stream, "}\n");
1898 COLLECT_SHARED_INIT_FUNC(stream, initname);
1899 COLLECT_SHARED_FINI_FUNC(stream, fininame);
1903 /* Write the constructor/destructor tables. */
1905 #ifndef LD_INIT_SWITCH
1907 write_c_file_glob (FILE *stream, const char *name ATTRIBUTE_UNUSED)
1909 /* Write the tables as C code. */
1911 int frames = (frame_tables.number > 0);
1913 fprintf (stream, "typedef void entry_pt();\n\n");
1915 write_list_with_asm (stream, "extern entry_pt ", constructors.first);
1919 write_list_with_asm (stream, "extern void *", frame_tables.first);
1921 fprintf (stream, "\tstatic void *frame_table[] = {\n");
1922 write_list (stream, "\t\t&", frame_tables.first);
1923 fprintf (stream, "\t0\n};\n");
1925 /* This must match what's in frame.h. */
1926 fprintf (stream, "struct object {\n");
1927 fprintf (stream, " void *pc_begin;\n");
1928 fprintf (stream, " void *pc_end;\n");
1929 fprintf (stream, " void *fde_begin;\n");
1930 fprintf (stream, " void *fde_array;\n");
1931 fprintf (stream, " __SIZE_TYPE__ count;\n");
1932 fprintf (stream, " struct object *next;\n");
1933 fprintf (stream, "};\n");
1935 fprintf (stream, "extern void __register_frame_info_table (void *, struct object *);\n");
1936 fprintf (stream, "extern void *__deregister_frame_info (void *);\n");
1938 fprintf (stream, "static void reg_frame () {\n");
1939 fprintf (stream, "\tstatic struct object ob;\n");
1940 fprintf (stream, "\t__register_frame_info_table (frame_table, &ob);\n");
1941 fprintf (stream, "\t}\n");
1943 fprintf (stream, "static void dereg_frame () {\n");
1944 fprintf (stream, "\t__deregister_frame_info (frame_table);\n");
1945 fprintf (stream, "\t}\n");
1948 fprintf (stream, "\nentry_pt * __CTOR_LIST__[] = {\n");
1949 fprintf (stream, "\t(entry_pt *) %d,\n", constructors.number + frames);
1950 write_list (stream, "\t", constructors.first);
1952 fprintf (stream, "\treg_frame,\n");
1953 fprintf (stream, "\t0\n};\n\n");
1955 write_list_with_asm (stream, "extern entry_pt ", destructors.first);
1957 fprintf (stream, "\nentry_pt * __DTOR_LIST__[] = {\n");
1958 fprintf (stream, "\t(entry_pt *) %d,\n", destructors.number + frames);
1959 write_list (stream, "\t", destructors.first);
1961 fprintf (stream, "\tdereg_frame,\n");
1962 fprintf (stream, "\t0\n};\n\n");
1964 fprintf (stream, "extern entry_pt %s;\n", NAME__MAIN);
1965 fprintf (stream, "entry_pt *__main_reference = %s;\n\n", NAME__MAIN);
1967 #endif /* ! LD_INIT_SWITCH */
1970 write_c_file (FILE *stream, const char *name)
1972 fprintf (stream, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n");
1973 #ifndef LD_INIT_SWITCH
1975 write_c_file_glob (stream, name);
1978 write_c_file_stat (stream, name);
1979 fprintf (stream, "#ifdef __cplusplus\n}\n#endif\n");
1982 #ifdef COLLECT_EXPORT_LIST
1984 write_aix_file (FILE *stream, struct id *list)
1986 for (; list; list = list->next)
1988 fputs (list->name, stream);
1989 putc ('\n', stream);
1994 #ifdef OBJECT_FORMAT_NONE
1996 /* Generic version to scan the name list of the loaded program for
1997 the symbols g++ uses for static constructors and destructors.
1999 The constructor table begins at __CTOR_LIST__ and contains a count
2000 of the number of pointers (or -1 if the constructors are built in a
2001 separate section by the linker), followed by the pointers to the
2002 constructor functions, terminated with a null pointer. The
2003 destructor table has the same format, and begins at __DTOR_LIST__. */
2006 scan_prog_file (const char *prog_name, enum pass which_pass)
2008 void (*int_handler) (int);
2010 void (*quit_handler) (int);
2012 char *real_nm_argv[4];
2013 const char **nm_argv = (const char **) real_nm_argv;
2015 struct pex_obj *pex;
2021 if (which_pass == PASS_SECOND)
2024 /* If we do not have an `nm', complain. */
2025 if (nm_file_name == 0)
2026 fatal ("cannot find 'nm'");
2028 nm_argv[argc++] = nm_file_name;
2029 if (NM_FLAGS[0] != '\0')
2030 nm_argv[argc++] = NM_FLAGS;
2032 nm_argv[argc++] = prog_name;
2033 nm_argv[argc++] = (char *) 0;
2035 /* Trace if needed. */
2038 const char **p_argv;
2041 for (p_argv = &nm_argv[0]; (str = *p_argv) != (char *) 0; p_argv++)
2042 fprintf (stderr, " %s", str);
2044 fprintf (stderr, "\n");
2050 pex = pex_init (PEX_USE_PIPES, "collect2", NULL);
2052 fatal_perror ("pex_init failed");
2054 errmsg = pex_run (pex, 0, nm_file_name, real_nm_argv, NULL, NULL, &err);
2060 fatal_perror (errmsg);
2066 int_handler = (void (*) (int)) signal (SIGINT, SIG_IGN);
2068 quit_handler = (void (*) (int)) signal (SIGQUIT, SIG_IGN);
2071 inf = pex_read_output (pex, 0);
2073 fatal_perror ("can't open nm output");
2076 fprintf (stderr, "\nnm output with constructors/destructors.\n");
2078 /* Read each line of nm output. */
2079 while (fgets (buf, sizeof buf, inf) != (char *) 0)
2084 /* If it contains a constructor or destructor name, add the name
2085 to the appropriate list. */
2087 for (p = buf; (ch = *p) != '\0' && ch != '\n' && ch != '_'; p++)
2088 if (ch == ' ' && p[1] == 'U' && p[2] == ' ')
2095 /* Find the end of the symbol name.
2096 Do not include `|', because Encore nm can tack that on the end. */
2097 for (end = p; (ch2 = *end) != '\0' && !ISSPACE (ch2) && ch2 != '|';
2103 switch (is_ctor_dtor (name))
2106 if (which_pass != PASS_LIB)
2107 add_to_list (&constructors, name);
2111 if (which_pass != PASS_LIB)
2112 add_to_list (&destructors, name);
2116 if (which_pass != PASS_LIB)
2117 fatal ("init function found in object %s", prog_name);
2118 #ifndef LD_INIT_SWITCH
2119 add_to_list (&constructors, name);
2124 if (which_pass != PASS_LIB)
2125 fatal ("fini function found in object %s", prog_name);
2126 #ifndef LD_FINI_SWITCH
2127 add_to_list (&destructors, name);
2132 if (which_pass != PASS_LIB)
2133 add_to_list (&frame_tables, name);
2136 default: /* not a constructor or destructor */
2141 fprintf (stderr, "\t%s\n", buf);
2145 fprintf (stderr, "\n");
2147 do_wait (nm_file_name, pex);
2149 signal (SIGINT, int_handler);
2151 signal (SIGQUIT, quit_handler);
2157 /* Use the List Dynamic Dependencies program to find shared libraries that
2158 the output file depends upon and their initialization/finalization
2159 routines, if any. */
2162 scan_libraries (const char *prog_name)
2164 static struct head libraries; /* list of shared libraries found */
2166 void (*int_handler) (int);
2168 void (*quit_handler) (int);
2170 char *real_ldd_argv[4];
2171 const char **ldd_argv = (const char **) real_ldd_argv;
2173 struct pex_obj *pex;
2179 /* If we do not have an `ldd', complain. */
2180 if (ldd_file_name == 0)
2182 error ("cannot find 'ldd'");
2186 ldd_argv[argc++] = ldd_file_name;
2187 ldd_argv[argc++] = prog_name;
2188 ldd_argv[argc++] = (char *) 0;
2190 /* Trace if needed. */
2193 const char **p_argv;
2196 for (p_argv = &ldd_argv[0]; (str = *p_argv) != (char *) 0; p_argv++)
2197 fprintf (stderr, " %s", str);
2199 fprintf (stderr, "\n");
2205 pex = pex_init (PEX_USE_PIPES, "collect2", NULL);
2207 fatal_perror ("pex_init failed");
2209 errmsg = pex_run (pex, 0, ldd_file_name, real_ldd_argv, NULL, NULL, &err);
2215 fatal_perror (errmsg);
2221 int_handler = (void (*) (int)) signal (SIGINT, SIG_IGN);
2223 quit_handler = (void (*) (int)) signal (SIGQUIT, SIG_IGN);
2226 inf = pex_read_output (pex, 0);
2228 fatal_perror ("can't open ldd output");
2231 notice ("\nldd output with constructors/destructors.\n");
2233 /* Read each line of ldd output. */
2234 while (fgets (buf, sizeof buf, inf) != (char *) 0)
2237 char *name, *end, *p = buf;
2239 /* Extract names of libraries and add to list. */
2240 PARSE_LDD_OUTPUT (p);
2245 if (strncmp (name, "not found", sizeof ("not found") - 1) == 0)
2246 fatal ("dynamic dependency %s not found", buf);
2248 /* Find the end of the symbol name. */
2250 (ch2 = *end) != '\0' && ch2 != '\n' && !ISSPACE (ch2) && ch2 != '|';
2255 if (access (name, R_OK) == 0)
2256 add_to_list (&libraries, name);
2258 fatal ("unable to open dynamic dependency '%s'", buf);
2261 fprintf (stderr, "\t%s\n", buf);
2264 fprintf (stderr, "\n");
2266 do_wait (ldd_file_name, pex);
2268 signal (SIGINT, int_handler);
2270 signal (SIGQUIT, quit_handler);
2273 /* Now iterate through the library list adding their symbols to
2275 for (list = libraries.first; list; list = list->next)
2276 scan_prog_file (list->name, PASS_LIB);
2279 #endif /* LDD_SUFFIX */
2281 #endif /* OBJECT_FORMAT_NONE */
2285 * COFF specific stuff.
2288 #ifdef OBJECT_FORMAT_COFF
2290 #if defined (EXTENDED_COFF)
2292 # define GCC_SYMBOLS(X) (SYMHEADER(X).isymMax + SYMHEADER(X).iextMax)
2293 # define GCC_SYMENT SYMR
2294 # define GCC_OK_SYMBOL(X) ((X).st == stProc || (X).st == stGlobal)
2295 # define GCC_SYMINC(X) (1)
2296 # define GCC_SYMZERO(X) (SYMHEADER(X).isymMax)
2297 # define GCC_CHECK_HDR(X) (PSYMTAB(X) != 0)
2301 # define GCC_SYMBOLS(X) (HEADER(ldptr).f_nsyms)
2302 # define GCC_SYMENT SYMENT
2303 # if defined (C_WEAKEXT)
2304 # define GCC_OK_SYMBOL(X) \
2305 (((X).n_sclass == C_EXT || (X).n_sclass == C_WEAKEXT) && \
2306 ((X).n_scnum > N_UNDEF) && \
2308 || (((X).n_type & N_TMASK) == (DT_NON << N_BTSHFT) \
2309 || ((X).n_type & N_TMASK) == (DT_FCN << N_BTSHFT))))
2310 # define GCC_UNDEF_SYMBOL(X) \
2311 (((X).n_sclass == C_EXT || (X).n_sclass == C_WEAKEXT) && \
2312 ((X).n_scnum == N_UNDEF))
2314 # define GCC_OK_SYMBOL(X) \
2315 (((X).n_sclass == C_EXT) && \
2316 ((X).n_scnum > N_UNDEF) && \
2318 || (((X).n_type & N_TMASK) == (DT_NON << N_BTSHFT) \
2319 || ((X).n_type & N_TMASK) == (DT_FCN << N_BTSHFT))))
2320 # define GCC_UNDEF_SYMBOL(X) \
2321 (((X).n_sclass == C_EXT) && ((X).n_scnum == N_UNDEF))
2323 # define GCC_SYMINC(X) ((X).n_numaux+1)
2324 # define GCC_SYMZERO(X) 0
2326 /* 0757 = U803XTOCMAGIC (AIX 4.3) and 0767 = U64_TOCMAGIC (AIX V5) */
2328 # define GCC_CHECK_HDR(X) \
2329 ((HEADER (X).f_magic == U802TOCMAGIC && ! aix64_flag) \
2330 || (HEADER (X).f_magic == 0767 && aix64_flag))
2332 # define GCC_CHECK_HDR(X) \
2333 ((HEADER (X).f_magic == U802TOCMAGIC && ! aix64_flag) \
2334 || (HEADER (X).f_magic == 0757 && aix64_flag))
2339 #ifdef COLLECT_EXPORT_LIST
2340 /* Array of standard AIX libraries which should not
2341 be scanned for ctors/dtors. */
2342 static const char *const aix_std_libs[] = {
2350 "/usr/lib/libc_r.a",
2351 "/usr/lib/libm_r.a",
2352 "/usr/lib/threads/libc.a",
2353 "/usr/ccs/lib/libc.a",
2354 "/usr/ccs/lib/libm.a",
2355 "/usr/ccs/lib/libc_r.a",
2356 "/usr/ccs/lib/libm_r.a",
2360 /* This function checks the filename and returns 1
2361 if this name matches the location of a standard AIX library. */
2362 static int ignore_library (const char *);
2364 ignore_library (const char *name)
2366 const char *const *p = &aix_std_libs[0];
2367 while (*p++ != NULL)
2368 if (! strcmp (name, *p)) return 1;
2371 #endif /* COLLECT_EXPORT_LIST */
2373 #if defined (HAVE_DECL_LDGETNAME) && !HAVE_DECL_LDGETNAME
2374 extern char *ldgetname (LDFILE *, GCC_SYMENT *);
2377 /* COFF version to scan the name list of the loaded program for
2378 the symbols g++ uses for static constructors and destructors.
2380 The constructor table begins at __CTOR_LIST__ and contains a count
2381 of the number of pointers (or -1 if the constructors are built in a
2382 separate section by the linker), followed by the pointers to the
2383 constructor functions, terminated with a null pointer. The
2384 destructor table has the same format, and begins at __DTOR_LIST__. */
2387 scan_prog_file (const char *prog_name, enum pass which_pass)
2389 LDFILE *ldptr = NULL;
2390 int sym_index, sym_count;
2393 if (which_pass != PASS_FIRST && which_pass != PASS_OBJ)
2396 #ifdef COLLECT_EXPORT_LIST
2397 /* We do not need scanning for some standard C libraries. */
2398 if (which_pass == PASS_FIRST && ignore_library (prog_name))
2401 /* On AIX we have a loop, because there is not much difference
2402 between an object and an archive. This trick allows us to
2403 eliminate scan_libraries() function. */
2407 /* Some platforms (e.g. OSF4) declare ldopen as taking a
2408 non-const char * filename parameter, even though it will not
2409 modify that string. So we must cast away const-ness here,
2410 which will cause -Wcast-qual to burp. */
2411 if ((ldptr = ldopen ((char *)prog_name, ldptr)) != NULL)
2413 if (! MY_ISCOFF (HEADER (ldptr).f_magic))
2414 fatal ("%s: not a COFF file", prog_name);
2416 if (GCC_CHECK_HDR (ldptr))
2418 sym_count = GCC_SYMBOLS (ldptr);
2419 sym_index = GCC_SYMZERO (ldptr);
2421 #ifdef COLLECT_EXPORT_LIST
2422 /* Is current archive member a shared object? */
2423 is_shared = HEADER (ldptr).f_flags & F_SHROBJ;
2426 while (sym_index < sym_count)
2430 if (ldtbread (ldptr, sym_index, &symbol) <= 0)
2432 sym_index += GCC_SYMINC (symbol);
2434 if (GCC_OK_SYMBOL (symbol))
2438 if ((name = ldgetname (ldptr, &symbol)) == NULL)
2439 continue; /* Should never happen. */
2441 #ifdef XCOFF_DEBUGGING_INFO
2442 /* All AIX function names have a duplicate entry
2443 beginning with a dot. */
2448 switch (is_ctor_dtor (name))
2452 add_to_list (&constructors, name);
2453 #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
2454 if (which_pass == PASS_OBJ)
2455 add_to_list (&exports, name);
2461 add_to_list (&destructors, name);
2462 #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
2463 if (which_pass == PASS_OBJ)
2464 add_to_list (&exports, name);
2468 #ifdef COLLECT_EXPORT_LIST
2470 #ifndef LD_INIT_SWITCH
2472 add_to_list (&constructors, name);
2477 #ifndef LD_INIT_SWITCH
2479 add_to_list (&destructors, name);
2486 add_to_list (&frame_tables, name);
2487 #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
2488 if (which_pass == PASS_OBJ)
2489 add_to_list (&exports, name);
2493 default: /* not a constructor or destructor */
2494 #ifdef COLLECT_EXPORT_LIST
2495 /* Explicitly export all global symbols when
2496 building a shared object on AIX, but do not
2497 re-export symbols from another shared object
2498 and do not export symbols if the user
2499 provides an explicit export list. */
2500 if (shared_obj && !is_shared
2501 && which_pass == PASS_OBJ && !export_flag)
2502 add_to_list (&exports, name);
2508 #if !defined(EXTENDED_COFF)
2509 fprintf (stderr, "\tsec=%d class=%d type=%s%o %s\n",
2510 symbol.n_scnum, symbol.n_sclass,
2511 (symbol.n_type ? "0" : ""), symbol.n_type,
2515 "\tiss = %5d, value = %5ld, index = %5d, name = %s\n",
2516 symbol.iss, (long) symbol.value, symbol.index, name);
2521 #ifdef COLLECT_EXPORT_LIST
2524 /* If archive contains both 32-bit and 64-bit objects,
2525 we want to skip objects in other mode so mismatch normal. */
2527 fprintf (stderr, "%s : magic=%o aix64=%d mismatch\n",
2528 prog_name, HEADER (ldptr).f_magic, aix64_flag);
2534 fatal ("%s: cannot open as COFF file", prog_name);
2536 #ifdef COLLECT_EXPORT_LIST
2537 /* On AIX loop continues while there are more members in archive. */
2539 while (ldclose (ldptr) == FAILURE);
2541 /* Otherwise we simply close ldptr. */
2542 (void) ldclose(ldptr);
2545 #endif /* OBJECT_FORMAT_COFF */
2547 #ifdef COLLECT_EXPORT_LIST
2548 /* Given a library name without "lib" prefix, this function
2549 returns a full library name including a path. */
2551 resolve_lib_name (const char *name)
2555 /* Library extensions for AIX dynamic linking. */
2556 const char * const libexts[2] = {"a", "so"};
2558 for (i = 0; libpaths[i]; i++)
2559 if (libpaths[i]->max_len > l)
2560 l = libpaths[i]->max_len;
2562 lib_buf = xmalloc (l + strlen(name) + 10);
2564 for (i = 0; libpaths[i]; i++)
2566 struct prefix_list *list = libpaths[i]->plist;
2567 for (; list; list = list->next)
2569 /* The following lines are needed because path_prefix list
2570 may contain directories both with trailing '/' and
2573 if (list->prefix[strlen(list->prefix)-1] != '/')
2575 for (j = 0; j < 2; j++)
2577 sprintf (lib_buf, "%s%slib%s.%s",
2578 list->prefix, p, name,
2579 libexts[(j + aixrtl_flag) % 2]);
2580 if (debug) fprintf (stderr, "searching for: %s\n", lib_buf);
2581 if (file_exists (lib_buf))
2583 if (debug) fprintf (stderr, "found: %s\n", lib_buf);
2590 fprintf (stderr, "not found\n");
2592 fatal ("library lib%s not found", name);
2595 #endif /* COLLECT_EXPORT_LIST */