Sync usage with man page.
[netbsd-mini2440.git] / gnu / dist / gcc4 / gcc / collect2.c
blobb39e61eb961a17313ee6fdad6a16d5c928df51af
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
14 version.
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 for more details.
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING. If not, write to the Free
23 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
24 02110-1301, USA. */
27 /* Build tables of static constructors and destructors and run ld. */
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include <signal.h>
34 #if ! defined( SIGCHLD ) && defined( SIGCLD )
35 # define SIGCHLD SIGCLD
36 #endif
38 #ifndef LIBRARY_PATH_ENV
39 #define LIBRARY_PATH_ENV "LIBRARY_PATH"
40 #endif
42 #define COLLECT
44 #include "collect2.h"
45 #include "demangle.h"
46 #include "obstack.h"
47 #include "intl.h"
48 #include "version.h"
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. */
56 #ifdef CROSS_COMPILE
57 #undef OBJECT_FORMAT_COFF
58 #undef MD_EXEC_PREFIX
59 #undef REAL_LD_FILE_NAME
60 #undef REAL_NM_FILE_NAME
61 #undef REAL_STRIP_FILE_NAME
62 #endif
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
71 #endif
73 #ifdef OBJECT_FORMAT_COFF
75 #include <a.out.h>
76 #include <ar.h>
78 #ifdef UMAX
79 #include <sgs.h>
80 #endif
82 /* Many versions of ldfcn.h define these. */
83 #ifdef FREAD
84 #undef FREAD
85 #undef FWRITE
86 #endif
88 #include <ldfcn.h>
90 /* Some systems have an ISCOFF macro, but others do not. In some cases
91 the macro may be wrong. MY_ISCOFF is defined in tm.h files for machines
92 that either do not have an ISCOFF macro in /usr/include or for those
93 where it is wrong. */
95 #ifndef MY_ISCOFF
96 #define MY_ISCOFF(X) ISCOFF (X)
97 #endif
99 #endif /* OBJECT_FORMAT_COFF */
101 #ifdef OBJECT_FORMAT_NONE
103 /* Default flags to pass to nm. */
104 #ifndef NM_FLAGS
105 #define NM_FLAGS "-n"
106 #endif
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. */
113 #ifndef NAME__MAIN
114 #define NAME__MAIN "__main"
115 #endif
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))
123 #endif
124 #ifndef COLLECT_SHARED_FINI_FUNC
125 #define COLLECT_SHARED_FINI_FUNC(STREAM, FUNC) \
126 fprintf ((STREAM), "void _GLOBAL__DD() {\n\t%s();\n}\n", (FUNC))
127 #endif
129 #ifdef LDD_SUFFIX
130 #define SCAN_LIBRARIES
131 #endif
133 #ifndef SHLIB_SUFFIX
134 #define SHLIB_SUFFIX ".so"
135 #endif
137 #ifdef USE_COLLECT2
138 int do_collecting = 1;
139 #else
140 int do_collecting = 0;
141 #endif
143 /* Nonzero if we should suppress the automatic demangling of identifiers
144 in linker error messages. Set from COLLECT_NO_DEMANGLE. */
145 int no_demangle;
147 /* Linked lists of constructor and destructor names. */
149 struct id
151 struct id *next;
152 int sequence;
153 char name[1];
156 struct head
158 struct id *first;
159 struct id *last;
160 int number;
163 /* Enumeration giving which pass this is for scanning the program file. */
165 enum pass {
166 PASS_FIRST, /* without constructors */
167 PASS_OBJ, /* individual objects */
168 PASS_LIB, /* looking for shared libraries */
169 PASS_SECOND /* with constructors linked in */
172 int vflag; /* true if -v */
173 static int rflag; /* true if -r */
174 static int strip_flag; /* true if -s */
175 static const char *demangle_flag;
176 #ifdef COLLECT_EXPORT_LIST
177 static int export_flag; /* true if -bE */
178 static int aix64_flag; /* true if -b64 */
179 static int aixrtl_flag; /* true if -brtl */
180 #endif
182 int debug; /* true if -debug */
184 static int shared_obj; /* true if -shared */
186 static const char *c_file; /* <xxx>.c for constructor/destructor list. */
187 static const char *o_file; /* <xxx>.o for constructor/destructor list. */
188 #ifdef COLLECT_EXPORT_LIST
189 static const char *export_file; /* <xxx>.x for AIX export list. */
190 #endif
191 const char *ldout; /* File for ld stdout. */
192 const char *lderrout; /* File for ld stderr. */
193 static const char *output_file; /* Output file for ld. */
194 static const char *nm_file_name; /* pathname of nm */
195 #ifdef LDD_SUFFIX
196 static const char *ldd_file_name; /* pathname of ldd (or equivalent) */
197 #endif
198 static const char *strip_file_name; /* pathname of strip */
199 const char *c_file_name; /* pathname of gcc */
200 static char *initname, *fininame; /* names of init and fini funcs */
202 static struct head constructors; /* list of constructors found */
203 static struct head destructors; /* list of destructors found */
204 #ifdef COLLECT_EXPORT_LIST
205 static struct head exports; /* list of exported symbols */
206 #endif
207 static struct head frame_tables; /* list of frame unwind info tables */
209 struct obstack temporary_obstack;
210 char * temporary_firstobj;
212 /* Structure to hold all the directories in which to search for files to
213 execute. */
215 struct prefix_list
217 const char *prefix; /* String to prepend to the path. */
218 struct prefix_list *next; /* Next in linked list. */
221 struct path_prefix
223 struct prefix_list *plist; /* List of prefixes to try */
224 int max_len; /* Max length of a prefix in PLIST */
225 const char *name; /* Name of this list (used in config stuff) */
228 #ifdef COLLECT_EXPORT_LIST
229 /* Lists to keep libraries to be scanned for global constructors/destructors. */
230 static struct head libs; /* list of libraries */
231 static struct path_prefix cmdline_lib_dirs; /* directories specified with -L */
232 static struct path_prefix libpath_lib_dirs; /* directories in LIBPATH */
233 static struct path_prefix *libpaths[3] = {&cmdline_lib_dirs,
234 &libpath_lib_dirs, NULL};
235 #endif
237 static void handler (int);
238 static int is_ctor_dtor (const char *);
239 static char *find_a_file (struct path_prefix *, const char *);
240 static void add_prefix (struct path_prefix *, const char *);
241 static void prefix_from_env (const char *, struct path_prefix *);
242 static void prefix_from_string (const char *, struct path_prefix *);
243 static void do_wait (const char *, struct pex_obj *);
244 static void fork_execute (const char *, char **);
245 static void maybe_unlink (const char *);
246 static void add_to_list (struct head *, const char *);
247 static int extract_init_priority (const char *);
248 static void sort_ids (struct head *);
249 static void write_list (FILE *, const char *, struct id *);
250 #ifdef COLLECT_EXPORT_LIST
251 static void dump_list (FILE *, const char *, struct id *);
252 #endif
253 #if 0
254 static void dump_prefix_list (FILE *, const char *, struct prefix_list *);
255 #endif
256 static void write_list_with_asm (FILE *, const char *, struct id *);
257 static void write_c_file (FILE *, const char *);
258 static void write_c_file_stat (FILE *, const char *);
259 #ifndef LD_INIT_SWITCH
260 static void write_c_file_glob (FILE *, const char *);
261 #endif
262 static void scan_prog_file (const char *, enum pass);
263 #ifdef SCAN_LIBRARIES
264 static void scan_libraries (const char *);
265 #endif
266 #if LINK_ELIMINATE_DUPLICATE_LDIRECTORIES
267 static int is_in_args (const char *, const char **, const char **);
268 #endif
269 #ifdef COLLECT_EXPORT_LIST
270 #if 0
271 static int is_in_list (const char *, struct id *);
272 #endif
273 static void write_aix_file (FILE *, struct id *);
274 static char *resolve_lib_name (const char *);
275 #endif
276 static char *extract_string (const char **);
278 /* Delete tempfiles and exit function. */
280 void
281 collect_exit (int status)
283 if (c_file != 0 && c_file[0])
284 maybe_unlink (c_file);
286 if (o_file != 0 && o_file[0])
287 maybe_unlink (o_file);
289 #ifdef COLLECT_EXPORT_LIST
290 if (export_file != 0 && export_file[0])
291 maybe_unlink (export_file);
292 #endif
294 if (ldout != 0 && ldout[0])
296 dump_file (ldout, stdout);
297 maybe_unlink (ldout);
300 if (lderrout != 0 && lderrout[0])
302 dump_file (lderrout, stderr);
303 maybe_unlink (lderrout);
306 if (status != 0 && output_file != 0 && output_file[0])
307 maybe_unlink (output_file);
309 exit (status);
313 /* Notify user of a non-error. */
314 void
315 notice (const char *cmsgid, ...)
317 va_list ap;
319 va_start (ap, cmsgid);
320 vfprintf (stderr, _(cmsgid), ap);
321 va_end (ap);
324 /* Die when sys call fails. */
326 void
327 fatal_perror (const char * cmsgid, ...)
329 int e = errno;
330 va_list ap;
332 va_start (ap, cmsgid);
333 fprintf (stderr, "collect2: ");
334 vfprintf (stderr, _(cmsgid), ap);
335 fprintf (stderr, ": %s\n", xstrerror (e));
336 va_end (ap);
338 collect_exit (FATAL_EXIT_CODE);
341 /* Just die. */
343 void
344 fatal (const char * cmsgid, ...)
346 va_list ap;
348 va_start (ap, cmsgid);
349 fprintf (stderr, "collect2: ");
350 vfprintf (stderr, _(cmsgid), ap);
351 fprintf (stderr, "\n");
352 va_end (ap);
354 collect_exit (FATAL_EXIT_CODE);
357 /* Write error message. */
359 void
360 error (const char * gmsgid, ...)
362 va_list ap;
364 va_start (ap, gmsgid);
365 fprintf (stderr, "collect2: ");
366 vfprintf (stderr, _(gmsgid), ap);
367 fprintf (stderr, "\n");
368 va_end(ap);
371 /* In case obstack is linked in, and abort is defined to fancy_abort,
372 provide a default entry. */
374 void
375 fancy_abort (const char *file, int line, const char *func)
377 fatal ("internal gcc abort in %s, at %s:%d", func, file, line);
380 static void
381 handler (int signo)
383 if (c_file != 0 && c_file[0])
384 maybe_unlink (c_file);
386 if (o_file != 0 && o_file[0])
387 maybe_unlink (o_file);
389 if (ldout != 0 && ldout[0])
390 maybe_unlink (ldout);
392 if (lderrout != 0 && lderrout[0])
393 maybe_unlink (lderrout);
395 #ifdef COLLECT_EXPORT_LIST
396 if (export_file != 0 && export_file[0])
397 maybe_unlink (export_file);
398 #endif
400 signal (signo, SIG_DFL);
401 raise (signo);
406 file_exists (const char *name)
408 return access (name, R_OK) == 0;
411 /* Parse a reasonable subset of shell quoting syntax. */
413 static char *
414 extract_string (const char **pp)
416 const char *p = *pp;
417 int backquote = 0;
418 int inside = 0;
420 for (;;)
422 char c = *p;
423 if (c == '\0')
424 break;
425 ++p;
426 if (backquote)
427 obstack_1grow (&temporary_obstack, c);
428 else if (! inside && c == ' ')
429 break;
430 else if (! inside && c == '\\')
431 backquote = 1;
432 else if (c == '\'')
433 inside = !inside;
434 else
435 obstack_1grow (&temporary_obstack, c);
438 obstack_1grow (&temporary_obstack, '\0');
439 *pp = p;
440 return XOBFINISH (&temporary_obstack, char *);
443 void
444 dump_file (const char *name, FILE *to)
446 FILE *stream = fopen (name, "r");
448 if (stream == 0)
449 return;
450 while (1)
452 int c;
453 while (c = getc (stream),
454 c != EOF && (ISIDNUM (c) || c == '$' || c == '.'))
455 obstack_1grow (&temporary_obstack, c);
456 if (obstack_object_size (&temporary_obstack) > 0)
458 const char *word, *p;
459 char *result;
460 obstack_1grow (&temporary_obstack, '\0');
461 word = XOBFINISH (&temporary_obstack, const char *);
463 if (*word == '.')
464 ++word, putc ('.', to);
465 p = word;
466 if (!strncmp (p, USER_LABEL_PREFIX, strlen (USER_LABEL_PREFIX)))
467 p += strlen (USER_LABEL_PREFIX);
469 #ifdef HAVE_LD_DEMANGLE
470 result = 0;
471 #else
472 if (no_demangle)
473 result = 0;
474 else
475 result = cplus_demangle (p, DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE);
476 #endif
478 if (result)
480 int diff;
481 fputs (result, to);
483 diff = strlen (word) - strlen (result);
484 while (diff > 0 && c == ' ')
485 --diff, putc (' ', to);
486 while (diff < 0 && c == ' ')
487 ++diff, c = getc (stream);
489 free (result);
491 else
492 fputs (word, to);
494 fflush (to);
495 obstack_free (&temporary_obstack, temporary_firstobj);
497 if (c == EOF)
498 break;
499 putc (c, to);
501 fclose (stream);
504 /* Decide whether the given symbol is: a constructor (1), a destructor
505 (2), a routine in a shared object that calls all the constructors
506 (3) or destructors (4), a DWARF exception-handling table (5), or
507 nothing special (0). */
509 static int
510 is_ctor_dtor (const char *s)
512 struct names { const char *const name; const int len; const int ret;
513 const int two_underscores; };
515 const struct names *p;
516 int ch;
517 const char *orig_s = s;
519 static const struct names special[] = {
520 #ifndef NO_DOLLAR_IN_LABEL
521 { "GLOBAL__I$", sizeof ("GLOBAL__I$")-1, 1, 0 },
522 { "GLOBAL__D$", sizeof ("GLOBAL__D$")-1, 2, 0 },
523 #else
524 #ifndef NO_DOT_IN_LABEL
525 { "GLOBAL__I.", sizeof ("GLOBAL__I.")-1, 1, 0 },
526 { "GLOBAL__D.", sizeof ("GLOBAL__D.")-1, 2, 0 },
527 #endif /* NO_DOT_IN_LABEL */
528 #endif /* NO_DOLLAR_IN_LABEL */
529 { "GLOBAL__I_", sizeof ("GLOBAL__I_")-1, 1, 0 },
530 { "GLOBAL__D_", sizeof ("GLOBAL__D_")-1, 2, 0 },
531 { "GLOBAL__F_", sizeof ("GLOBAL__F_")-1, 5, 0 },
532 { "GLOBAL__FI_", sizeof ("GLOBAL__FI_")-1, 3, 0 },
533 { "GLOBAL__FD_", sizeof ("GLOBAL__FD_")-1, 4, 0 },
534 { NULL, 0, 0, 0 }
537 while ((ch = *s) == '_')
538 ++s;
540 if (s == orig_s)
541 return 0;
543 for (p = &special[0]; p->len > 0; p++)
545 if (ch == p->name[0]
546 && (!p->two_underscores || ((s - orig_s) >= 2))
547 && strncmp(s, p->name, p->len) == 0)
549 return p->ret;
552 return 0;
555 /* We maintain two prefix lists: one from COMPILER_PATH environment variable
556 and one from the PATH variable. */
558 static struct path_prefix cpath, path;
560 #ifdef CROSS_COMPILE
561 /* This is the name of the target machine. We use it to form the name
562 of the files to execute. */
564 static const char *const target_machine = TARGET_MACHINE;
565 #endif
567 /* Search for NAME using prefix list PPREFIX. We only look for executable
568 files.
570 Return 0 if not found, otherwise return its name, allocated with malloc. */
572 static char *
573 find_a_file (struct path_prefix *pprefix, const char *name)
575 char *temp;
576 struct prefix_list *pl;
577 int len = pprefix->max_len + strlen (name) + 1;
579 if (debug)
580 fprintf (stderr, "Looking for '%s'\n", name);
582 #ifdef HOST_EXECUTABLE_SUFFIX
583 len += strlen (HOST_EXECUTABLE_SUFFIX);
584 #endif
586 temp = xmalloc (len);
588 /* Determine the filename to execute (special case for absolute paths). */
590 if (*name == '/'
591 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
592 || (*name && name[1] == ':')
593 #endif
596 if (access (name, X_OK) == 0)
598 strcpy (temp, name);
600 if (debug)
601 fprintf (stderr, " - found: absolute path\n");
603 return temp;
606 #ifdef HOST_EXECUTABLE_SUFFIX
607 /* Some systems have a suffix for executable files.
608 So try appending that. */
609 strcpy (temp, name);
610 strcat (temp, HOST_EXECUTABLE_SUFFIX);
612 if (access (temp, X_OK) == 0)
613 return temp;
614 #endif
616 if (debug)
617 fprintf (stderr, " - failed to locate using absolute path\n");
619 else
620 for (pl = pprefix->plist; pl; pl = pl->next)
622 struct stat st;
624 strcpy (temp, pl->prefix);
625 strcat (temp, name);
627 if (stat (temp, &st) >= 0
628 && ! S_ISDIR (st.st_mode)
629 && access (temp, X_OK) == 0)
630 return temp;
632 #ifdef HOST_EXECUTABLE_SUFFIX
633 /* Some systems have a suffix for executable files.
634 So try appending that. */
635 strcat (temp, HOST_EXECUTABLE_SUFFIX);
637 if (stat (temp, &st) >= 0
638 && ! S_ISDIR (st.st_mode)
639 && access (temp, X_OK) == 0)
640 return temp;
641 #endif
644 if (debug && pprefix->plist == NULL)
645 fprintf (stderr, " - failed: no entries in prefix list\n");
647 free (temp);
648 return 0;
651 /* Add an entry for PREFIX to prefix list PPREFIX. */
653 static void
654 add_prefix (struct path_prefix *pprefix, const char *prefix)
656 struct prefix_list *pl, **prev;
657 int len;
659 if (pprefix->plist)
661 for (pl = pprefix->plist; pl->next; pl = pl->next)
663 prev = &pl->next;
665 else
666 prev = &pprefix->plist;
668 /* Keep track of the longest prefix. */
670 len = strlen (prefix);
671 if (len > pprefix->max_len)
672 pprefix->max_len = len;
674 pl = xmalloc (sizeof (struct prefix_list));
675 pl->prefix = xstrdup (prefix);
677 if (*prev)
678 pl->next = *prev;
679 else
680 pl->next = (struct prefix_list *) 0;
681 *prev = pl;
684 /* Take the value of the environment variable ENV, break it into a path, and
685 add of the entries to PPREFIX. */
687 static void
688 prefix_from_env (const char *env, struct path_prefix *pprefix)
690 const char *p;
691 GET_ENVIRONMENT (p, env);
693 if (p)
694 prefix_from_string (p, pprefix);
697 static void
698 prefix_from_string (const char *p, struct path_prefix *pprefix)
700 const char *startp, *endp;
701 char *nstore = xmalloc (strlen (p) + 3);
703 if (debug)
704 fprintf (stderr, "Convert string '%s' into prefixes, separator = '%c'\n", p, PATH_SEPARATOR);
706 startp = endp = p;
707 while (1)
709 if (*endp == PATH_SEPARATOR || *endp == 0)
711 strncpy (nstore, startp, endp-startp);
712 if (endp == startp)
714 strcpy (nstore, "./");
716 else if (! IS_DIR_SEPARATOR (endp[-1]))
718 nstore[endp-startp] = DIR_SEPARATOR;
719 nstore[endp-startp+1] = 0;
721 else
722 nstore[endp-startp] = 0;
724 if (debug)
725 fprintf (stderr, " - add prefix: %s\n", nstore);
727 add_prefix (pprefix, nstore);
728 if (*endp == 0)
729 break;
730 endp = startp = endp + 1;
732 else
733 endp++;
737 /* Main program. */
740 main (int argc, char **argv)
742 static const char *const ld_suffix = "ld";
743 static const char *const real_ld_suffix = "real-ld";
744 static const char *const collect_ld_suffix = "collect-ld";
745 static const char *const nm_suffix = "nm";
746 static const char *const gnm_suffix = "gnm";
747 #ifdef LDD_SUFFIX
748 static const char *const ldd_suffix = LDD_SUFFIX;
749 #endif
750 static const char *const strip_suffix = "strip";
751 static const char *const gstrip_suffix = "gstrip";
753 #ifdef CROSS_COMPILE
754 /* If we look for a program in the compiler directories, we just use
755 the short name, since these directories are already system-specific.
756 But it we look for a program in the system directories, we need to
757 qualify the program name with the target machine. */
759 const char *const full_ld_suffix =
760 concat(target_machine, "-", ld_suffix, NULL);
761 const char *const full_nm_suffix =
762 concat (target_machine, "-", nm_suffix, NULL);
763 const char *const full_gnm_suffix =
764 concat (target_machine, "-", gnm_suffix, NULL);
765 #ifdef LDD_SUFFIX
766 const char *const full_ldd_suffix =
767 concat (target_machine, "-", ldd_suffix, NULL);
768 #endif
769 const char *const full_strip_suffix =
770 concat (target_machine, "-", strip_suffix, NULL);
771 const char *const full_gstrip_suffix =
772 concat (target_machine, "-", gstrip_suffix, NULL);
773 #else
774 const char *const full_ld_suffix = ld_suffix;
775 const char *const full_nm_suffix = nm_suffix;
776 const char *const full_gnm_suffix = gnm_suffix;
777 #ifdef LDD_SUFFIX
778 const char *const full_ldd_suffix = ldd_suffix;
779 #endif
780 const char *const full_strip_suffix = strip_suffix;
781 const char *const full_gstrip_suffix = gstrip_suffix;
782 #endif /* CROSS_COMPILE */
784 const char *arg;
785 FILE *outf;
786 #ifdef COLLECT_EXPORT_LIST
787 FILE *exportf;
788 #endif
789 const char *ld_file_name;
790 const char *p;
791 char **c_argv;
792 const char **c_ptr;
793 char **ld1_argv;
794 const char **ld1;
795 char **ld2_argv;
796 const char **ld2;
797 char **object_lst;
798 const char **object;
799 int first_file;
800 int num_c_args = argc+9;
802 no_demangle = !! getenv ("COLLECT_NO_DEMANGLE");
804 /* Suppress demangling by the real linker, which may be broken. */
805 putenv (xstrdup ("COLLECT_NO_DEMANGLE="));
807 #if defined (COLLECT2_HOST_INITIALIZATION)
808 /* Perform system dependent initialization, if necessary. */
809 COLLECT2_HOST_INITIALIZATION;
810 #endif
812 #ifdef SIGCHLD
813 /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
814 receive the signal. A different setting is inheritable */
815 signal (SIGCHLD, SIG_DFL);
816 #endif
818 /* Unlock the stdio streams. */
819 unlock_std_streams ();
821 gcc_init_libintl ();
823 /* Do not invoke xcalloc before this point, since locale needs to be
824 set first, in case a diagnostic is issued. */
826 ld1 = (const char **)(ld1_argv = xcalloc(sizeof (char *), argc+4));
827 ld2 = (const char **)(ld2_argv = xcalloc(sizeof (char *), argc+11));
828 object = (const char **)(object_lst = xcalloc(sizeof (char *), argc));
830 #ifdef DEBUG
831 debug = 1;
832 #endif
834 /* Parse command line early for instances of -debug. This allows
835 the debug flag to be set before functions like find_a_file()
836 are called. */
838 int i;
840 for (i = 1; argv[i] != NULL; i ++)
842 if (! strcmp (argv[i], "-debug"))
843 debug = 1;
845 vflag = debug;
848 #ifndef DEFAULT_A_OUT_NAME
849 output_file = "a.out";
850 #else
851 output_file = DEFAULT_A_OUT_NAME;
852 #endif
854 obstack_begin (&temporary_obstack, 0);
855 temporary_firstobj = obstack_alloc (&temporary_obstack, 0);
857 #ifndef HAVE_LD_DEMANGLE
858 current_demangling_style = auto_demangling;
859 #endif
860 p = getenv ("COLLECT_GCC_OPTIONS");
861 while (p && *p)
863 const char *q = extract_string (&p);
864 if (*q == '-' && (q[1] == 'm' || q[1] == 'f'))
865 num_c_args++;
867 obstack_free (&temporary_obstack, temporary_firstobj);
869 /* -fno-profile-arcs -fno-test-coverage -fno-branch-probabilities
870 -fno-exceptions -w */
871 num_c_args += 5;
873 c_ptr = (const char **) (c_argv = xcalloc (sizeof (char *), num_c_args));
875 if (argc < 2)
876 fatal ("no arguments");
878 #ifdef SIGQUIT
879 if (signal (SIGQUIT, SIG_IGN) != SIG_IGN)
880 signal (SIGQUIT, handler);
881 #endif
882 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
883 signal (SIGINT, handler);
884 #ifdef SIGALRM
885 if (signal (SIGALRM, SIG_IGN) != SIG_IGN)
886 signal (SIGALRM, handler);
887 #endif
888 #ifdef SIGHUP
889 if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
890 signal (SIGHUP, handler);
891 #endif
892 if (signal (SIGSEGV, SIG_IGN) != SIG_IGN)
893 signal (SIGSEGV, handler);
894 #ifdef SIGBUS
895 if (signal (SIGBUS, SIG_IGN) != SIG_IGN)
896 signal (SIGBUS, handler);
897 #endif
899 /* Extract COMPILER_PATH and PATH into our prefix list. */
900 prefix_from_env ("COMPILER_PATH", &cpath);
901 prefix_from_env ("PATH", &path);
903 /* Try to discover a valid linker/nm/strip to use. */
905 /* Maybe we know the right file to use (if not cross). */
906 ld_file_name = 0;
907 #ifdef DEFAULT_LINKER
908 if (access (DEFAULT_LINKER, X_OK) == 0)
909 ld_file_name = DEFAULT_LINKER;
910 if (ld_file_name == 0)
911 #endif
912 #ifdef REAL_LD_FILE_NAME
913 ld_file_name = find_a_file (&path, REAL_LD_FILE_NAME);
914 if (ld_file_name == 0)
915 #endif
916 /* Search the (target-specific) compiler dirs for ld'. */
917 ld_file_name = find_a_file (&cpath, real_ld_suffix);
918 /* Likewise for `collect-ld'. */
919 if (ld_file_name == 0)
920 ld_file_name = find_a_file (&cpath, collect_ld_suffix);
921 /* Search the compiler directories for `ld'. We have protection against
922 recursive calls in find_a_file. */
923 if (ld_file_name == 0)
924 ld_file_name = find_a_file (&cpath, ld_suffix);
925 /* Search the ordinary system bin directories
926 for `ld' (if native linking) or `TARGET-ld' (if cross). */
927 if (ld_file_name == 0)
928 ld_file_name = find_a_file (&path, full_ld_suffix);
930 #ifdef REAL_NM_FILE_NAME
931 nm_file_name = find_a_file (&path, REAL_NM_FILE_NAME);
932 if (nm_file_name == 0)
933 #endif
934 nm_file_name = find_a_file (&cpath, gnm_suffix);
935 if (nm_file_name == 0)
936 nm_file_name = find_a_file (&path, full_gnm_suffix);
937 if (nm_file_name == 0)
938 nm_file_name = find_a_file (&cpath, nm_suffix);
939 if (nm_file_name == 0)
940 nm_file_name = find_a_file (&path, full_nm_suffix);
942 #ifdef LDD_SUFFIX
943 ldd_file_name = find_a_file (&cpath, ldd_suffix);
944 if (ldd_file_name == 0)
945 ldd_file_name = find_a_file (&path, full_ldd_suffix);
946 #endif
948 #ifdef REAL_STRIP_FILE_NAME
949 strip_file_name = find_a_file (&path, REAL_STRIP_FILE_NAME);
950 if (strip_file_name == 0)
951 #endif
952 strip_file_name = find_a_file (&cpath, gstrip_suffix);
953 if (strip_file_name == 0)
954 strip_file_name = find_a_file (&path, full_gstrip_suffix);
955 if (strip_file_name == 0)
956 strip_file_name = find_a_file (&cpath, strip_suffix);
957 if (strip_file_name == 0)
958 strip_file_name = find_a_file (&path, full_strip_suffix);
960 /* Determine the full path name of the C compiler to use. */
961 c_file_name = getenv ("COLLECT_GCC");
962 if (c_file_name == 0)
964 #ifdef CROSS_COMPILE
965 c_file_name = concat (target_machine, "-gcc", NULL);
966 #else
967 c_file_name = "gcc";
968 #endif
971 p = find_a_file (&cpath, c_file_name);
973 /* Here it should be safe to use the system search path since we should have
974 already qualified the name of the compiler when it is needed. */
975 if (p == 0)
976 p = find_a_file (&path, c_file_name);
978 if (p)
979 c_file_name = p;
981 *ld1++ = *ld2++ = ld_file_name;
983 /* Make temp file names. */
984 c_file = make_temp_file (".c");
985 o_file = make_temp_file (".o");
986 #ifdef COLLECT_EXPORT_LIST
987 export_file = make_temp_file (".x");
988 #endif
989 ldout = make_temp_file (".ld");
990 lderrout = make_temp_file (".le");
991 *c_ptr++ = c_file_name;
992 *c_ptr++ = "-x";
993 *c_ptr++ = "c";
994 *c_ptr++ = "-c";
995 *c_ptr++ = "-o";
996 *c_ptr++ = o_file;
998 #ifdef COLLECT_EXPORT_LIST
999 /* Generate a list of directories from LIBPATH. */
1000 prefix_from_env ("LIBPATH", &libpath_lib_dirs);
1001 /* Add to this list also two standard directories where
1002 AIX loader always searches for libraries. */
1003 add_prefix (&libpath_lib_dirs, "/lib");
1004 add_prefix (&libpath_lib_dirs, "/usr/lib");
1005 #endif
1007 /* Get any options that the upper GCC wants to pass to the sub-GCC.
1009 AIX support needs to know if -shared has been specified before
1010 parsing commandline arguments. */
1012 p = getenv ("COLLECT_GCC_OPTIONS");
1013 while (p && *p)
1015 const char *q = extract_string (&p);
1016 if (*q == '-' && (q[1] == 'm' || q[1] == 'f'))
1017 *c_ptr++ = xstrdup (q);
1018 if (strcmp (q, "-EL") == 0 || strcmp (q, "-EB") == 0)
1019 *c_ptr++ = xstrdup (q);
1020 if (strcmp (q, "-shared") == 0)
1021 shared_obj = 1;
1022 if (*q == '-' && q[1] == 'B')
1024 *c_ptr++ = xstrdup (q);
1025 if (q[2] == 0)
1027 q = extract_string (&p);
1028 *c_ptr++ = xstrdup (q);
1032 obstack_free (&temporary_obstack, temporary_firstobj);
1033 *c_ptr++ = "-fno-profile-arcs";
1034 *c_ptr++ = "-fno-test-coverage";
1035 *c_ptr++ = "-fno-branch-probabilities";
1036 *c_ptr++ = "-fno-exceptions";
1037 *c_ptr++ = "-w";
1039 /* !!! When GCC calls collect2,
1040 it does not know whether it is calling collect2 or ld.
1041 So collect2 cannot meaningfully understand any options
1042 except those ld understands.
1043 If you propose to make GCC pass some other option,
1044 just imagine what will happen if ld is really ld!!! */
1046 /* Parse arguments. Remember output file spec, pass the rest to ld. */
1047 /* After the first file, put in the c++ rt0. */
1049 first_file = 1;
1050 #ifdef HAVE_LD_DEMANGLE
1051 if (!demangle_flag && !no_demangle)
1052 demangle_flag = "--demangle";
1053 if (demangle_flag)
1054 *ld1++ = *ld2++ = demangle_flag;
1055 #endif
1056 while ((arg = *++argv) != (char *) 0)
1058 *ld1++ = *ld2++ = arg;
1060 if (arg[0] == '-')
1062 switch (arg[1])
1064 #ifdef COLLECT_EXPORT_LIST
1065 /* We want to disable automatic exports on AIX when user
1066 explicitly puts an export list in command line */
1067 case 'b':
1068 if (arg[2] == 'E' || strncmp (&arg[2], "export", 6) == 0)
1069 export_flag = 1;
1070 else if (arg[2] == '6' && arg[3] == '4')
1071 aix64_flag = 1;
1072 else if (arg[2] == 'r' && arg[3] == 't' && arg[4] == 'l')
1073 aixrtl_flag = 1;
1074 break;
1075 #endif
1077 case 'd':
1078 if (!strcmp (arg, "-debug"))
1080 /* Already parsed. */
1081 ld1--;
1082 ld2--;
1084 if (!strcmp (arg, "-dynamic-linker") && argv[1])
1086 ++argv;
1087 *ld1++ = *ld2++ = *argv;
1089 break;
1091 case 'l':
1092 if (first_file)
1094 /* place o_file BEFORE this argument! */
1095 first_file = 0;
1096 ld2--;
1097 *ld2++ = o_file;
1098 *ld2++ = arg;
1100 #ifdef COLLECT_EXPORT_LIST
1102 /* Resolving full library name. */
1103 const char *s = resolve_lib_name (arg+2);
1105 /* Saving a full library name. */
1106 add_to_list (&libs, s);
1108 #endif
1109 break;
1111 #ifdef COLLECT_EXPORT_LIST
1112 /* Saving directories where to search for libraries. */
1113 case 'L':
1114 add_prefix (&cmdline_lib_dirs, arg+2);
1115 break;
1116 #else
1117 #if LINK_ELIMINATE_DUPLICATE_LDIRECTORIES
1118 case 'L':
1119 if (is_in_args (arg, (const char **) ld1_argv, ld1-1))
1120 --ld1;
1121 break;
1122 #endif /* LINK_ELIMINATE_DUPLICATE_LDIRECTORIES */
1123 #endif
1125 case 'o':
1126 if (arg[2] == '\0')
1127 output_file = *ld1++ = *ld2++ = *++argv;
1128 else if (1
1129 #ifdef SWITCHES_NEED_SPACES
1130 && ! strchr (SWITCHES_NEED_SPACES, arg[1])
1131 #endif
1134 output_file = &arg[2];
1135 break;
1137 case 'r':
1138 if (arg[2] == '\0')
1139 rflag = 1;
1140 break;
1142 case 's':
1143 if (arg[2] == '\0' && do_collecting)
1145 /* We must strip after the nm run, otherwise C++ linking
1146 will not work. Thus we strip in the second ld run, or
1147 else with strip if there is no second ld run. */
1148 strip_flag = 1;
1149 ld1--;
1151 break;
1153 case 'v':
1154 if (arg[2] == '\0')
1155 vflag = 1;
1156 break;
1158 case '-':
1159 if (strcmp (arg, "--no-demangle") == 0)
1161 demangle_flag = arg;
1162 no_demangle = 1;
1163 ld1--;
1164 ld2--;
1166 else if (strncmp (arg, "--demangle", 10) == 0)
1168 demangle_flag = arg;
1169 no_demangle = 0;
1170 #ifndef HAVE_LD_DEMANGLE
1171 if (arg[10] == '=')
1173 enum demangling_styles style
1174 = cplus_demangle_name_to_style (arg+11);
1175 if (style == unknown_demangling)
1176 error ("unknown demangling style '%s'", arg+11);
1177 else
1178 current_demangling_style = style;
1180 #endif
1181 ld1--;
1182 ld2--;
1184 break;
1187 else if ((p = strrchr (arg, '.')) != (char *) 0
1188 && (strcmp (p, ".o") == 0 || strcmp (p, ".a") == 0
1189 || strcmp (p, ".so") == 0 || strcmp (p, ".lo") == 0
1190 || strcmp (p, ".obj") == 0))
1192 if (first_file)
1194 first_file = 0;
1195 if (p[1] == 'o')
1196 *ld2++ = o_file;
1197 else
1199 /* place o_file BEFORE this argument! */
1200 ld2--;
1201 *ld2++ = o_file;
1202 *ld2++ = arg;
1205 if (p[1] == 'o' || p[1] == 'l')
1206 *object++ = arg;
1207 #ifdef COLLECT_EXPORT_LIST
1208 /* libraries can be specified directly, i.e. without -l flag. */
1209 else
1211 /* Saving a full library name. */
1212 add_to_list (&libs, arg);
1214 #endif
1218 #ifdef COLLECT_EXPORT_LIST
1219 /* This is added only for debugging purposes. */
1220 if (debug)
1222 fprintf (stderr, "List of libraries:\n");
1223 dump_list (stderr, "\t", libs.first);
1226 /* The AIX linker will discard static constructors in object files if
1227 nothing else in the file is referenced, so look at them first. */
1229 const char **export_object_lst = (const char **)object_lst;
1231 while (export_object_lst < object)
1232 scan_prog_file (*export_object_lst++, PASS_OBJ);
1235 struct id *list = libs.first;
1237 for (; list; list = list->next)
1238 scan_prog_file (list->name, PASS_FIRST);
1241 if (exports.first)
1243 char *buf = concat ("-bE:", export_file, NULL);
1245 *ld1++ = buf;
1246 *ld2++ = buf;
1248 exportf = fopen (export_file, "w");
1249 if (exportf == (FILE *) 0)
1250 fatal_perror ("fopen %s", export_file);
1251 write_aix_file (exportf, exports.first);
1252 if (fclose (exportf))
1253 fatal_perror ("fclose %s", export_file);
1255 #endif
1257 *c_ptr++ = c_file;
1258 *c_ptr = *ld1 = *object = (char *) 0;
1260 if (vflag)
1262 notice ("collect2 version %s", version_string);
1263 #ifdef TARGET_VERSION
1264 TARGET_VERSION;
1265 #endif
1266 fprintf (stderr, "\n");
1269 if (debug)
1271 const char *ptr;
1272 fprintf (stderr, "ld_file_name = %s\n",
1273 (ld_file_name ? ld_file_name : "not found"));
1274 fprintf (stderr, "c_file_name = %s\n",
1275 (c_file_name ? c_file_name : "not found"));
1276 fprintf (stderr, "nm_file_name = %s\n",
1277 (nm_file_name ? nm_file_name : "not found"));
1278 #ifdef LDD_SUFFIX
1279 fprintf (stderr, "ldd_file_name = %s\n",
1280 (ldd_file_name ? ldd_file_name : "not found"));
1281 #endif
1282 fprintf (stderr, "strip_file_name = %s\n",
1283 (strip_file_name ? strip_file_name : "not found"));
1284 fprintf (stderr, "c_file = %s\n",
1285 (c_file ? c_file : "not found"));
1286 fprintf (stderr, "o_file = %s\n",
1287 (o_file ? o_file : "not found"));
1289 ptr = getenv ("COLLECT_GCC_OPTIONS");
1290 if (ptr)
1291 fprintf (stderr, "COLLECT_GCC_OPTIONS = %s\n", ptr);
1293 ptr = getenv ("COLLECT_GCC");
1294 if (ptr)
1295 fprintf (stderr, "COLLECT_GCC = %s\n", ptr);
1297 ptr = getenv ("COMPILER_PATH");
1298 if (ptr)
1299 fprintf (stderr, "COMPILER_PATH = %s\n", ptr);
1301 ptr = getenv (LIBRARY_PATH_ENV);
1302 if (ptr)
1303 fprintf (stderr, "%-20s= %s\n", LIBRARY_PATH_ENV, ptr);
1305 fprintf (stderr, "\n");
1308 /* Load the program, searching all libraries and attempting to provide
1309 undefined symbols from repository information. */
1311 /* On AIX we do this later. */
1312 #ifndef COLLECT_EXPORT_LIST
1313 do_tlink (ld1_argv, object_lst);
1314 #endif
1316 /* If -r or they will be run via some other method, do not build the
1317 constructor or destructor list, just return now. */
1318 if (rflag
1319 #ifndef COLLECT_EXPORT_LIST
1320 || ! do_collecting
1321 #endif
1324 #ifdef COLLECT_EXPORT_LIST
1325 /* Do the link we avoided above if we are exiting. */
1326 do_tlink (ld1_argv, object_lst);
1328 /* But make sure we delete the export file we may have created. */
1329 if (export_file != 0 && export_file[0])
1330 maybe_unlink (export_file);
1331 #endif
1332 maybe_unlink (c_file);
1333 maybe_unlink (o_file);
1334 return 0;
1337 /* Examine the namelist with nm and search it for static constructors
1338 and destructors to call.
1339 Write the constructor and destructor tables to a .s file and reload. */
1341 /* On AIX we already scanned for global constructors/destructors. */
1342 #ifndef COLLECT_EXPORT_LIST
1343 scan_prog_file (output_file, PASS_FIRST);
1344 #endif
1346 #ifdef SCAN_LIBRARIES
1347 scan_libraries (output_file);
1348 #endif
1350 if (debug)
1352 notice ("%d constructor(s) found\n", constructors.number);
1353 notice ("%d destructor(s) found\n", destructors.number);
1354 notice ("%d frame table(s) found\n", frame_tables.number);
1357 if (constructors.number == 0 && destructors.number == 0
1358 && frame_tables.number == 0
1359 #if defined (SCAN_LIBRARIES) || defined (COLLECT_EXPORT_LIST)
1360 /* If we will be running these functions ourselves, we want to emit
1361 stubs into the shared library so that we do not have to relink
1362 dependent programs when we add static objects. */
1363 && ! shared_obj
1364 #endif
1367 #ifdef COLLECT_EXPORT_LIST
1368 /* Do tlink without additional code generation. */
1369 do_tlink (ld1_argv, object_lst);
1370 #endif
1371 /* Strip now if it was requested on the command line. */
1372 if (strip_flag)
1374 char **real_strip_argv = xcalloc (sizeof (char *), 3);
1375 const char ** strip_argv = (const char **) real_strip_argv;
1377 strip_argv[0] = strip_file_name;
1378 strip_argv[1] = output_file;
1379 strip_argv[2] = (char *) 0;
1380 fork_execute ("strip", real_strip_argv);
1383 #ifdef COLLECT_EXPORT_LIST
1384 maybe_unlink (export_file);
1385 #endif
1386 maybe_unlink (c_file);
1387 maybe_unlink (o_file);
1388 return 0;
1391 /* Sort ctor and dtor lists by priority. */
1392 sort_ids (&constructors);
1393 sort_ids (&destructors);
1395 maybe_unlink(output_file);
1396 outf = fopen (c_file, "w");
1397 if (outf == (FILE *) 0)
1398 fatal_perror ("fopen %s", c_file);
1400 write_c_file (outf, c_file);
1402 if (fclose (outf))
1403 fatal_perror ("fclose %s", c_file);
1405 /* Tell the linker that we have initializer and finalizer functions. */
1406 #ifdef LD_INIT_SWITCH
1407 #ifdef COLLECT_EXPORT_LIST
1408 *ld2++ = concat (LD_INIT_SWITCH, ":", initname, ":", fininame, NULL);
1409 #else
1410 *ld2++ = LD_INIT_SWITCH;
1411 *ld2++ = initname;
1412 *ld2++ = LD_FINI_SWITCH;
1413 *ld2++ = fininame;
1414 #endif
1415 #endif
1417 #ifdef COLLECT_EXPORT_LIST
1418 if (shared_obj)
1420 /* If we did not add export flag to link arguments before, add it to
1421 second link phase now. No new exports should have been added. */
1422 if (! exports.first)
1423 *ld2++ = concat ("-bE:", export_file, NULL);
1425 #ifndef LD_INIT_SWITCH
1426 add_to_list (&exports, initname);
1427 add_to_list (&exports, fininame);
1428 add_to_list (&exports, "_GLOBAL__DI");
1429 add_to_list (&exports, "_GLOBAL__DD");
1430 #endif
1431 exportf = fopen (export_file, "w");
1432 if (exportf == (FILE *) 0)
1433 fatal_perror ("fopen %s", export_file);
1434 write_aix_file (exportf, exports.first);
1435 if (fclose (exportf))
1436 fatal_perror ("fclose %s", export_file);
1438 #endif
1440 /* End of arguments to second link phase. */
1441 *ld2 = (char*) 0;
1443 if (debug)
1445 fprintf (stderr, "\n========== output_file = %s, c_file = %s\n",
1446 output_file, c_file);
1447 write_c_file (stderr, "stderr");
1448 fprintf (stderr, "========== end of c_file\n\n");
1449 #ifdef COLLECT_EXPORT_LIST
1450 fprintf (stderr, "\n========== export_file = %s\n", export_file);
1451 write_aix_file (stderr, exports.first);
1452 fprintf (stderr, "========== end of export_file\n\n");
1453 #endif
1456 /* Assemble the constructor and destructor tables.
1457 Link the tables in with the rest of the program. */
1459 fork_execute ("gcc", c_argv);
1460 #ifdef COLLECT_EXPORT_LIST
1461 /* On AIX we must call tlink because of possible templates resolution. */
1462 do_tlink (ld2_argv, object_lst);
1463 #else
1464 /* Otherwise, simply call ld because tlink is already done. */
1465 fork_execute ("ld", ld2_argv);
1467 /* Let scan_prog_file do any final mods (OSF/rose needs this for
1468 constructors/destructors in shared libraries. */
1469 scan_prog_file (output_file, PASS_SECOND);
1470 #endif
1472 maybe_unlink (c_file);
1473 maybe_unlink (o_file);
1475 #ifdef COLLECT_EXPORT_LIST
1476 maybe_unlink (export_file);
1477 #endif
1479 return 0;
1483 /* Wait for a process to finish, and exit if a nonzero status is found. */
1486 collect_wait (const char *prog, struct pex_obj *pex)
1488 int status;
1490 if (!pex_get_status (pex, 1, &status))
1491 fatal_perror ("can't get program status");
1492 pex_free (pex);
1494 if (status)
1496 if (WIFSIGNALED (status))
1498 int sig = WTERMSIG (status);
1499 error ("%s terminated with signal %d [%s]%s",
1500 prog, sig, strsignal(sig),
1501 WCOREDUMP(status) ? ", core dumped" : "");
1502 collect_exit (FATAL_EXIT_CODE);
1505 if (WIFEXITED (status))
1506 return WEXITSTATUS (status);
1508 return 0;
1511 static void
1512 do_wait (const char *prog, struct pex_obj *pex)
1514 int ret = collect_wait (prog, pex);
1515 if (ret != 0)
1517 error ("%s returned %d exit status", prog, ret);
1518 collect_exit (ret);
1523 /* Execute a program, and wait for the reply. */
1525 struct pex_obj *
1526 collect_execute (const char *prog, char **argv, const char *outname,
1527 const char *errname)
1529 struct pex_obj *pex;
1530 const char *errmsg;
1531 int err;
1533 if (vflag || debug)
1535 char **p_argv;
1536 const char *str;
1538 if (argv[0])
1539 fprintf (stderr, "%s", argv[0]);
1540 else
1541 notice ("[cannot find %s]", prog);
1543 for (p_argv = &argv[1]; (str = *p_argv) != (char *) 0; p_argv++)
1544 fprintf (stderr, " %s", str);
1546 fprintf (stderr, "\n");
1549 fflush (stdout);
1550 fflush (stderr);
1552 /* If we cannot find a program we need, complain error. Do this here
1553 since we might not end up needing something that we could not find. */
1555 if (argv[0] == 0)
1556 fatal ("cannot find '%s'", prog);
1558 pex = pex_init (0, "collect2", NULL);
1559 if (pex == NULL)
1560 fatal_perror ("pex_init failed");
1562 errmsg = pex_run (pex, PEX_LAST | PEX_SEARCH, argv[0], argv, outname,
1563 errname, &err);
1564 if (errmsg != NULL)
1566 if (err != 0)
1568 errno = err;
1569 fatal_perror (errmsg);
1571 else
1572 fatal (errmsg);
1575 return pex;
1578 static void
1579 fork_execute (const char *prog, char **argv)
1581 struct pex_obj *pex;
1583 pex = collect_execute (prog, argv, NULL, NULL);
1584 do_wait (prog, pex);
1587 /* Unlink a file unless we are debugging. */
1589 static void
1590 maybe_unlink (const char *file)
1592 if (!debug)
1593 unlink_if_ordinary (file);
1594 else
1595 notice ("[Leaving %s]\n", file);
1599 static long sequence_number = 0;
1601 /* Add a name to a linked list. */
1603 static void
1604 add_to_list (struct head *head_ptr, const char *name)
1606 struct id *newid = xcalloc (sizeof (struct id) + strlen (name), 1);
1607 struct id *p;
1608 strcpy (newid->name, name);
1610 if (head_ptr->first)
1611 head_ptr->last->next = newid;
1612 else
1613 head_ptr->first = newid;
1615 /* Check for duplicate symbols. */
1616 for (p = head_ptr->first;
1617 strcmp (name, p->name) != 0;
1618 p = p->next)
1620 if (p != newid)
1622 head_ptr->last->next = 0;
1623 free (newid);
1624 return;
1627 newid->sequence = ++sequence_number;
1628 head_ptr->last = newid;
1629 head_ptr->number++;
1632 /* Grab the init priority number from an init function name that
1633 looks like "_GLOBAL_.I.12345.foo". */
1635 static int
1636 extract_init_priority (const char *name)
1638 int pos = 0, pri;
1640 while (name[pos] == '_')
1641 ++pos;
1642 pos += 10; /* strlen ("GLOBAL__X_") */
1644 /* Extract init_p number from ctor/dtor name. */
1645 pri = atoi (name + pos);
1646 return pri ? pri : DEFAULT_INIT_PRIORITY;
1649 /* Insertion sort the ids from ctor/dtor list HEAD_PTR in descending order.
1650 ctors will be run from right to left, dtors from left to right. */
1652 static void
1653 sort_ids (struct head *head_ptr)
1655 /* id holds the current element to insert. id_next holds the next
1656 element to insert. id_ptr iterates through the already sorted elements
1657 looking for the place to insert id. */
1658 struct id *id, *id_next, **id_ptr;
1660 id = head_ptr->first;
1662 /* We don't have any sorted elements yet. */
1663 head_ptr->first = NULL;
1665 for (; id; id = id_next)
1667 id_next = id->next;
1668 id->sequence = extract_init_priority (id->name);
1670 for (id_ptr = &(head_ptr->first); ; id_ptr = &((*id_ptr)->next))
1671 if (*id_ptr == NULL
1672 /* If the sequence numbers are the same, we put the id from the
1673 file later on the command line later in the list. */
1674 || id->sequence > (*id_ptr)->sequence
1675 /* Hack: do lexical compare, too.
1676 || (id->sequence == (*id_ptr)->sequence
1677 && strcmp (id->name, (*id_ptr)->name) > 0) */
1680 id->next = *id_ptr;
1681 *id_ptr = id;
1682 break;
1686 /* Now set the sequence numbers properly so write_c_file works. */
1687 for (id = head_ptr->first; id; id = id->next)
1688 id->sequence = ++sequence_number;
1691 /* Write: `prefix', the names on list LIST, `suffix'. */
1693 static void
1694 write_list (FILE *stream, const char *prefix, struct id *list)
1696 while (list)
1698 fprintf (stream, "%sx%d,\n", prefix, list->sequence);
1699 list = list->next;
1703 #if LINK_ELIMINATE_DUPLICATE_LDIRECTORIES
1704 /* Given a STRING, return nonzero if it occurs in the list in range
1705 [ARGS_BEGIN,ARGS_END). */
1707 static int
1708 is_in_args (const char *string, const char **args_begin,
1709 const char **args_end)
1711 const char **args_pointer;
1712 for (args_pointer = args_begin; args_pointer != args_end; ++args_pointer)
1713 if (strcmp (string, *args_pointer) == 0)
1714 return 1;
1715 return 0;
1717 #endif /* LINK_ELIMINATE_DUPLICATE_LDIRECTORIES */
1719 #ifdef COLLECT_EXPORT_LIST
1720 /* This function is really used only on AIX, but may be useful. */
1721 #if 0
1722 static int
1723 is_in_list (const char *prefix, struct id *list)
1725 while (list)
1727 if (!strcmp (prefix, list->name)) return 1;
1728 list = list->next;
1730 return 0;
1732 #endif
1733 #endif /* COLLECT_EXPORT_LIST */
1735 /* Added for debugging purpose. */
1736 #ifdef COLLECT_EXPORT_LIST
1737 static void
1738 dump_list (FILE *stream, const char *prefix, struct id *list)
1740 while (list)
1742 fprintf (stream, "%s%s,\n", prefix, list->name);
1743 list = list->next;
1746 #endif
1748 #if 0
1749 static void
1750 dump_prefix_list (FILE *stream, const char *prefix, struct prefix_list *list)
1752 while (list)
1754 fprintf (stream, "%s%s,\n", prefix, list->prefix);
1755 list = list->next;
1758 #endif
1760 static void
1761 write_list_with_asm (FILE *stream, const char *prefix, struct id *list)
1763 while (list)
1765 fprintf (stream, "%sx%d __asm__ (\"%s\");\n",
1766 prefix, list->sequence, list->name);
1767 list = list->next;
1771 /* Write out the constructor and destructor tables statically (for a shared
1772 object), along with the functions to execute them. */
1774 static void
1775 write_c_file_stat (FILE *stream, const char *name ATTRIBUTE_UNUSED)
1777 const char *p, *q;
1778 char *prefix, *r;
1779 int frames = (frame_tables.number > 0);
1781 /* Figure out name of output_file, stripping off .so version. */
1782 p = strrchr (output_file, '/');
1783 if (p == 0)
1784 p = output_file;
1785 else
1786 p++;
1787 q = p;
1788 while (q)
1790 q = strchr (q,'.');
1791 if (q == 0)
1793 q = p + strlen (p);
1794 break;
1796 else
1798 if (strncmp (q, SHLIB_SUFFIX, strlen (SHLIB_SUFFIX)) == 0)
1800 q += strlen (SHLIB_SUFFIX);
1801 break;
1803 else
1804 q++;
1807 /* q points to null at end of the string (or . of the .so version) */
1808 prefix = xmalloc (q - p + 1);
1809 strncpy (prefix, p, q - p);
1810 prefix[q - p] = 0;
1811 for (r = prefix; *r; r++)
1812 if (!ISALNUM ((unsigned char)*r))
1813 *r = '_';
1814 if (debug)
1815 notice ("\nwrite_c_file - output name is %s, prefix is %s\n",
1816 output_file, prefix);
1818 initname = concat ("_GLOBAL__FI_", prefix, NULL);
1819 fininame = concat ("_GLOBAL__FD_", prefix, NULL);
1821 free (prefix);
1823 /* Write the tables as C code. */
1825 fprintf (stream, "static int count;\n");
1826 fprintf (stream, "typedef void entry_pt();\n");
1827 write_list_with_asm (stream, "extern entry_pt ", constructors.first);
1829 if (frames)
1831 write_list_with_asm (stream, "extern void *", frame_tables.first);
1833 fprintf (stream, "\tstatic void *frame_table[] = {\n");
1834 write_list (stream, "\t\t&", frame_tables.first);
1835 fprintf (stream, "\t0\n};\n");
1837 /* This must match what's in frame.h. */
1838 fprintf (stream, "struct object {\n");
1839 fprintf (stream, " void *pc_begin;\n");
1840 fprintf (stream, " void *pc_end;\n");
1841 fprintf (stream, " void *fde_begin;\n");
1842 fprintf (stream, " void *fde_array;\n");
1843 fprintf (stream, " __SIZE_TYPE__ count;\n");
1844 fprintf (stream, " struct object *next;\n");
1845 fprintf (stream, "};\n");
1847 fprintf (stream, "extern void __register_frame_info_table (void *, struct object *);\n");
1848 fprintf (stream, "extern void *__deregister_frame_info (void *);\n");
1850 fprintf (stream, "static void reg_frame () {\n");
1851 fprintf (stream, "\tstatic struct object ob;\n");
1852 fprintf (stream, "\t__register_frame_info_table (frame_table, &ob);\n");
1853 fprintf (stream, "\t}\n");
1855 fprintf (stream, "static void dereg_frame () {\n");
1856 fprintf (stream, "\t__deregister_frame_info (frame_table);\n");
1857 fprintf (stream, "\t}\n");
1860 fprintf (stream, "void %s() {\n", initname);
1861 if (constructors.number > 0 || frames)
1863 fprintf (stream, "\tstatic entry_pt *ctors[] = {\n");
1864 write_list (stream, "\t\t", constructors.first);
1865 if (frames)
1866 fprintf (stream, "\treg_frame,\n");
1867 fprintf (stream, "\t};\n");
1868 fprintf (stream, "\tentry_pt **p;\n");
1869 fprintf (stream, "\tif (count++ != 0) return;\n");
1870 fprintf (stream, "\tp = ctors + %d;\n", constructors.number + frames);
1871 fprintf (stream, "\twhile (p > ctors) (*--p)();\n");
1873 else
1874 fprintf (stream, "\t++count;\n");
1875 fprintf (stream, "}\n");
1876 write_list_with_asm (stream, "extern entry_pt ", destructors.first);
1877 fprintf (stream, "void %s() {\n", fininame);
1878 if (destructors.number > 0 || frames)
1880 fprintf (stream, "\tstatic entry_pt *dtors[] = {\n");
1881 write_list (stream, "\t\t", destructors.first);
1882 if (frames)
1883 fprintf (stream, "\tdereg_frame,\n");
1884 fprintf (stream, "\t};\n");
1885 fprintf (stream, "\tentry_pt **p;\n");
1886 fprintf (stream, "\tif (--count != 0) return;\n");
1887 fprintf (stream, "\tp = dtors;\n");
1888 fprintf (stream, "\twhile (p < dtors + %d) (*p++)();\n",
1889 destructors.number + frames);
1891 fprintf (stream, "}\n");
1893 if (shared_obj)
1895 COLLECT_SHARED_INIT_FUNC(stream, initname);
1896 COLLECT_SHARED_FINI_FUNC(stream, fininame);
1900 /* Write the constructor/destructor tables. */
1902 #ifndef LD_INIT_SWITCH
1903 static void
1904 write_c_file_glob (FILE *stream, const char *name ATTRIBUTE_UNUSED)
1906 /* Write the tables as C code. */
1908 int frames = (frame_tables.number > 0);
1910 fprintf (stream, "typedef void entry_pt();\n\n");
1912 write_list_with_asm (stream, "extern entry_pt ", constructors.first);
1914 if (frames)
1916 write_list_with_asm (stream, "extern void *", frame_tables.first);
1918 fprintf (stream, "\tstatic void *frame_table[] = {\n");
1919 write_list (stream, "\t\t&", frame_tables.first);
1920 fprintf (stream, "\t0\n};\n");
1922 /* This must match what's in frame.h. */
1923 fprintf (stream, "struct object {\n");
1924 fprintf (stream, " void *pc_begin;\n");
1925 fprintf (stream, " void *pc_end;\n");
1926 fprintf (stream, " void *fde_begin;\n");
1927 fprintf (stream, " void *fde_array;\n");
1928 fprintf (stream, " __SIZE_TYPE__ count;\n");
1929 fprintf (stream, " struct object *next;\n");
1930 fprintf (stream, "};\n");
1932 fprintf (stream, "extern void __register_frame_info_table (void *, struct object *);\n");
1933 fprintf (stream, "extern void *__deregister_frame_info (void *);\n");
1935 fprintf (stream, "static void reg_frame () {\n");
1936 fprintf (stream, "\tstatic struct object ob;\n");
1937 fprintf (stream, "\t__register_frame_info_table (frame_table, &ob);\n");
1938 fprintf (stream, "\t}\n");
1940 fprintf (stream, "static void dereg_frame () {\n");
1941 fprintf (stream, "\t__deregister_frame_info (frame_table);\n");
1942 fprintf (stream, "\t}\n");
1945 fprintf (stream, "\nentry_pt * __CTOR_LIST__[] = {\n");
1946 fprintf (stream, "\t(entry_pt *) %d,\n", constructors.number + frames);
1947 write_list (stream, "\t", constructors.first);
1948 if (frames)
1949 fprintf (stream, "\treg_frame,\n");
1950 fprintf (stream, "\t0\n};\n\n");
1952 write_list_with_asm (stream, "extern entry_pt ", destructors.first);
1954 fprintf (stream, "\nentry_pt * __DTOR_LIST__[] = {\n");
1955 fprintf (stream, "\t(entry_pt *) %d,\n", destructors.number + frames);
1956 write_list (stream, "\t", destructors.first);
1957 if (frames)
1958 fprintf (stream, "\tdereg_frame,\n");
1959 fprintf (stream, "\t0\n};\n\n");
1961 fprintf (stream, "extern entry_pt %s;\n", NAME__MAIN);
1962 fprintf (stream, "entry_pt *__main_reference = %s;\n\n", NAME__MAIN);
1964 #endif /* ! LD_INIT_SWITCH */
1966 static void
1967 write_c_file (FILE *stream, const char *name)
1969 fprintf (stream, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n");
1970 #ifndef LD_INIT_SWITCH
1971 if (! shared_obj)
1972 write_c_file_glob (stream, name);
1973 else
1974 #endif
1975 write_c_file_stat (stream, name);
1976 fprintf (stream, "#ifdef __cplusplus\n}\n#endif\n");
1979 #ifdef COLLECT_EXPORT_LIST
1980 static void
1981 write_aix_file (FILE *stream, struct id *list)
1983 for (; list; list = list->next)
1985 fputs (list->name, stream);
1986 putc ('\n', stream);
1989 #endif
1991 #ifdef OBJECT_FORMAT_NONE
1993 /* Generic version to scan the name list of the loaded program for
1994 the symbols g++ uses for static constructors and destructors.
1996 The constructor table begins at __CTOR_LIST__ and contains a count
1997 of the number of pointers (or -1 if the constructors are built in a
1998 separate section by the linker), followed by the pointers to the
1999 constructor functions, terminated with a null pointer. The
2000 destructor table has the same format, and begins at __DTOR_LIST__. */
2002 static void
2003 scan_prog_file (const char *prog_name, enum pass which_pass)
2005 void (*int_handler) (int);
2006 #ifdef SIGQUIT
2007 void (*quit_handler) (int);
2008 #endif
2009 char *real_nm_argv[4];
2010 const char **nm_argv = (const char **) real_nm_argv;
2011 int argc = 0;
2012 struct pex_obj *pex;
2013 const char *errmsg;
2014 int err;
2015 char *p, buf[1024];
2016 FILE *inf;
2018 if (which_pass == PASS_SECOND)
2019 return;
2021 /* If we do not have an `nm', complain. */
2022 if (nm_file_name == 0)
2023 fatal ("cannot find 'nm'");
2025 nm_argv[argc++] = nm_file_name;
2026 if (NM_FLAGS[0] != '\0')
2027 nm_argv[argc++] = NM_FLAGS;
2029 nm_argv[argc++] = prog_name;
2030 nm_argv[argc++] = (char *) 0;
2032 /* Trace if needed. */
2033 if (vflag)
2035 const char **p_argv;
2036 const char *str;
2038 for (p_argv = &nm_argv[0]; (str = *p_argv) != (char *) 0; p_argv++)
2039 fprintf (stderr, " %s", str);
2041 fprintf (stderr, "\n");
2044 fflush (stdout);
2045 fflush (stderr);
2047 pex = pex_init (PEX_USE_PIPES, "collect2", NULL);
2048 if (pex == NULL)
2049 fatal_perror ("pex_init failed");
2051 errmsg = pex_run (pex, 0, nm_file_name, real_nm_argv, NULL, NULL, &err);
2052 if (errmsg != NULL)
2054 if (err != 0)
2056 errno = err;
2057 fatal_perror (errmsg);
2059 else
2060 fatal (errmsg);
2063 int_handler = (void (*) (int)) signal (SIGINT, SIG_IGN);
2064 #ifdef SIGQUIT
2065 quit_handler = (void (*) (int)) signal (SIGQUIT, SIG_IGN);
2066 #endif
2068 inf = pex_read_output (pex, 0);
2069 if (inf == NULL)
2070 fatal_perror ("can't open nm output");
2072 if (debug)
2073 fprintf (stderr, "\nnm output with constructors/destructors.\n");
2075 /* Read each line of nm output. */
2076 while (fgets (buf, sizeof buf, inf) != (char *) 0)
2078 int ch, ch2;
2079 char *name, *end;
2081 /* If it contains a constructor or destructor name, add the name
2082 to the appropriate list. */
2084 for (p = buf; (ch = *p) != '\0' && ch != '\n' && ch != '_'; p++)
2085 if (ch == ' ' && p[1] == 'U' && p[2] == ' ')
2086 break;
2088 if (ch != '_')
2089 continue;
2091 name = p;
2092 /* Find the end of the symbol name.
2093 Do not include `|', because Encore nm can tack that on the end. */
2094 for (end = p; (ch2 = *end) != '\0' && !ISSPACE (ch2) && ch2 != '|';
2095 end++)
2096 continue;
2099 *end = '\0';
2100 switch (is_ctor_dtor (name))
2102 case 1:
2103 if (which_pass != PASS_LIB)
2104 add_to_list (&constructors, name);
2105 break;
2107 case 2:
2108 if (which_pass != PASS_LIB)
2109 add_to_list (&destructors, name);
2110 break;
2112 case 3:
2113 if (which_pass != PASS_LIB)
2114 fatal ("init function found in object %s", prog_name);
2115 #ifndef LD_INIT_SWITCH
2116 add_to_list (&constructors, name);
2117 #endif
2118 break;
2120 case 4:
2121 if (which_pass != PASS_LIB)
2122 fatal ("fini function found in object %s", prog_name);
2123 #ifndef LD_FINI_SWITCH
2124 add_to_list (&destructors, name);
2125 #endif
2126 break;
2128 case 5:
2129 if (which_pass != PASS_LIB)
2130 add_to_list (&frame_tables, name);
2131 break;
2133 default: /* not a constructor or destructor */
2134 continue;
2137 if (debug)
2138 fprintf (stderr, "\t%s\n", buf);
2141 if (debug)
2142 fprintf (stderr, "\n");
2144 do_wait (nm_file_name, pex);
2146 signal (SIGINT, int_handler);
2147 #ifdef SIGQUIT
2148 signal (SIGQUIT, quit_handler);
2149 #endif
2152 #ifdef LDD_SUFFIX
2154 /* Use the List Dynamic Dependencies program to find shared libraries that
2155 the output file depends upon and their initialization/finalization
2156 routines, if any. */
2158 static void
2159 scan_libraries (const char *prog_name)
2161 static struct head libraries; /* list of shared libraries found */
2162 struct id *list;
2163 void (*int_handler) (int);
2164 #ifdef SIGQUIT
2165 void (*quit_handler) (int);
2166 #endif
2167 char *real_ldd_argv[4];
2168 const char **ldd_argv = (const char **) real_ldd_argv;
2169 int argc = 0;
2170 struct pex_obj *pex;
2171 const char *errmsg;
2172 int err;
2173 char buf[1024];
2174 FILE *inf;
2176 /* If we do not have an `ldd', complain. */
2177 if (ldd_file_name == 0)
2179 error ("cannot find 'ldd'");
2180 return;
2183 ldd_argv[argc++] = ldd_file_name;
2184 ldd_argv[argc++] = prog_name;
2185 ldd_argv[argc++] = (char *) 0;
2187 /* Trace if needed. */
2188 if (vflag)
2190 const char **p_argv;
2191 const char *str;
2193 for (p_argv = &ldd_argv[0]; (str = *p_argv) != (char *) 0; p_argv++)
2194 fprintf (stderr, " %s", str);
2196 fprintf (stderr, "\n");
2199 fflush (stdout);
2200 fflush (stderr);
2202 pex = pex_init (PEX_USE_PIPES, "collect2", NULL);
2203 if (pex == NULL)
2204 fatal_perror ("pex_init failed");
2206 errmsg = pex_run (pex, 0, ldd_file_name, real_ldd_argv, NULL, NULL, &err);
2207 if (errmsg != NULL)
2209 if (err != 0)
2211 errno = err;
2212 fatal_perror (errmsg);
2214 else
2215 fatal (errmsg);
2218 int_handler = (void (*) (int)) signal (SIGINT, SIG_IGN);
2219 #ifdef SIGQUIT
2220 quit_handler = (void (*) (int)) signal (SIGQUIT, SIG_IGN);
2221 #endif
2223 inf = pex_read_output (pex, 0);
2224 if (inf == NULL)
2225 fatal_perror ("can't open ldd output");
2227 if (debug)
2228 notice ("\nldd output with constructors/destructors.\n");
2230 /* Read each line of ldd output. */
2231 while (fgets (buf, sizeof buf, inf) != (char *) 0)
2233 int ch2;
2234 char *name, *end, *p = buf;
2236 /* Extract names of libraries and add to list. */
2237 PARSE_LDD_OUTPUT (p);
2238 if (p == 0)
2239 continue;
2241 name = p;
2242 if (strncmp (name, "not found", sizeof ("not found") - 1) == 0)
2243 fatal ("dynamic dependency %s not found", buf);
2245 /* Find the end of the symbol name. */
2246 for (end = p;
2247 (ch2 = *end) != '\0' && ch2 != '\n' && !ISSPACE (ch2) && ch2 != '|';
2248 end++)
2249 continue;
2250 *end = '\0';
2252 if (access (name, R_OK) == 0)
2253 add_to_list (&libraries, name);
2254 else
2255 fatal ("unable to open dynamic dependency '%s'", buf);
2257 if (debug)
2258 fprintf (stderr, "\t%s\n", buf);
2260 if (debug)
2261 fprintf (stderr, "\n");
2263 do_wait (ldd_file_name, pex);
2265 signal (SIGINT, int_handler);
2266 #ifdef SIGQUIT
2267 signal (SIGQUIT, quit_handler);
2268 #endif
2270 /* Now iterate through the library list adding their symbols to
2271 the list. */
2272 for (list = libraries.first; list; list = list->next)
2273 scan_prog_file (list->name, PASS_LIB);
2276 #endif /* LDD_SUFFIX */
2278 #endif /* OBJECT_FORMAT_NONE */
2282 * COFF specific stuff.
2285 #ifdef OBJECT_FORMAT_COFF
2287 #if defined (EXTENDED_COFF)
2289 # define GCC_SYMBOLS(X) (SYMHEADER(X).isymMax + SYMHEADER(X).iextMax)
2290 # define GCC_SYMENT SYMR
2291 # define GCC_OK_SYMBOL(X) ((X).st == stProc || (X).st == stGlobal)
2292 # define GCC_SYMINC(X) (1)
2293 # define GCC_SYMZERO(X) (SYMHEADER(X).isymMax)
2294 # define GCC_CHECK_HDR(X) (PSYMTAB(X) != 0)
2296 #else
2298 # define GCC_SYMBOLS(X) (HEADER(ldptr).f_nsyms)
2299 # define GCC_SYMENT SYMENT
2300 # if defined (C_WEAKEXT)
2301 # define GCC_OK_SYMBOL(X) \
2302 (((X).n_sclass == C_EXT || (X).n_sclass == C_WEAKEXT) && \
2303 ((X).n_scnum > N_UNDEF) && \
2304 (aix64_flag \
2305 || (((X).n_type & N_TMASK) == (DT_NON << N_BTSHFT) \
2306 || ((X).n_type & N_TMASK) == (DT_FCN << N_BTSHFT))))
2307 # define GCC_UNDEF_SYMBOL(X) \
2308 (((X).n_sclass == C_EXT || (X).n_sclass == C_WEAKEXT) && \
2309 ((X).n_scnum == N_UNDEF))
2310 # else
2311 # define GCC_OK_SYMBOL(X) \
2312 (((X).n_sclass == C_EXT) && \
2313 ((X).n_scnum > N_UNDEF) && \
2314 (aix64_flag \
2315 || (((X).n_type & N_TMASK) == (DT_NON << N_BTSHFT) \
2316 || ((X).n_type & N_TMASK) == (DT_FCN << N_BTSHFT))))
2317 # define GCC_UNDEF_SYMBOL(X) \
2318 (((X).n_sclass == C_EXT) && ((X).n_scnum == N_UNDEF))
2319 # endif
2320 # define GCC_SYMINC(X) ((X).n_numaux+1)
2321 # define GCC_SYMZERO(X) 0
2323 /* 0757 = U803XTOCMAGIC (AIX 4.3) and 0767 = U64_TOCMAGIC (AIX V5) */
2324 #ifdef _AIX51
2325 # define GCC_CHECK_HDR(X) \
2326 ((HEADER (X).f_magic == U802TOCMAGIC && ! aix64_flag) \
2327 || (HEADER (X).f_magic == 0767 && aix64_flag))
2328 #else
2329 # define GCC_CHECK_HDR(X) \
2330 ((HEADER (X).f_magic == U802TOCMAGIC && ! aix64_flag) \
2331 || (HEADER (X).f_magic == 0757 && aix64_flag))
2332 #endif
2334 #endif
2336 #ifdef COLLECT_EXPORT_LIST
2337 /* Array of standard AIX libraries which should not
2338 be scanned for ctors/dtors. */
2339 static const char *const aix_std_libs[] = {
2340 "/unix",
2341 "/lib/libc.a",
2342 "/lib/libm.a",
2343 "/lib/libc_r.a",
2344 "/lib/libm_r.a",
2345 "/usr/lib/libc.a",
2346 "/usr/lib/libm.a",
2347 "/usr/lib/libc_r.a",
2348 "/usr/lib/libm_r.a",
2349 "/usr/lib/threads/libc.a",
2350 "/usr/ccs/lib/libc.a",
2351 "/usr/ccs/lib/libm.a",
2352 "/usr/ccs/lib/libc_r.a",
2353 "/usr/ccs/lib/libm_r.a",
2354 NULL
2357 /* This function checks the filename and returns 1
2358 if this name matches the location of a standard AIX library. */
2359 static int ignore_library (const char *);
2360 static int
2361 ignore_library (const char *name)
2363 const char *const *p = &aix_std_libs[0];
2364 while (*p++ != NULL)
2365 if (! strcmp (name, *p)) return 1;
2366 return 0;
2368 #endif /* COLLECT_EXPORT_LIST */
2370 #if defined (HAVE_DECL_LDGETNAME) && !HAVE_DECL_LDGETNAME
2371 extern char *ldgetname (LDFILE *, GCC_SYMENT *);
2372 #endif
2374 /* COFF version to scan the name list of the loaded program for
2375 the symbols g++ uses for static constructors and destructors.
2377 The constructor table begins at __CTOR_LIST__ and contains a count
2378 of the number of pointers (or -1 if the constructors are built in a
2379 separate section by the linker), followed by the pointers to the
2380 constructor functions, terminated with a null pointer. The
2381 destructor table has the same format, and begins at __DTOR_LIST__. */
2383 static void
2384 scan_prog_file (const char *prog_name, enum pass which_pass)
2386 LDFILE *ldptr = NULL;
2387 int sym_index, sym_count;
2388 int is_shared = 0;
2390 if (which_pass != PASS_FIRST && which_pass != PASS_OBJ)
2391 return;
2393 #ifdef COLLECT_EXPORT_LIST
2394 /* We do not need scanning for some standard C libraries. */
2395 if (which_pass == PASS_FIRST && ignore_library (prog_name))
2396 return;
2398 /* On AIX we have a loop, because there is not much difference
2399 between an object and an archive. This trick allows us to
2400 eliminate scan_libraries() function. */
2403 #endif
2404 /* Some platforms (e.g. OSF4) declare ldopen as taking a
2405 non-const char * filename parameter, even though it will not
2406 modify that string. So we must cast away const-ness here,
2407 which will cause -Wcast-qual to burp. */
2408 if ((ldptr = ldopen ((char *)prog_name, ldptr)) != NULL)
2410 if (! MY_ISCOFF (HEADER (ldptr).f_magic))
2411 fatal ("%s: not a COFF file", prog_name);
2413 if (GCC_CHECK_HDR (ldptr))
2415 sym_count = GCC_SYMBOLS (ldptr);
2416 sym_index = GCC_SYMZERO (ldptr);
2418 #ifdef COLLECT_EXPORT_LIST
2419 /* Is current archive member a shared object? */
2420 is_shared = HEADER (ldptr).f_flags & F_SHROBJ;
2421 #endif
2423 while (sym_index < sym_count)
2425 GCC_SYMENT symbol;
2427 if (ldtbread (ldptr, sym_index, &symbol) <= 0)
2428 break;
2429 sym_index += GCC_SYMINC (symbol);
2431 if (GCC_OK_SYMBOL (symbol))
2433 char *name;
2435 if ((name = ldgetname (ldptr, &symbol)) == NULL)
2436 continue; /* Should never happen. */
2438 #ifdef XCOFF_DEBUGGING_INFO
2439 /* All AIX function names have a duplicate entry
2440 beginning with a dot. */
2441 if (*name == '.')
2442 ++name;
2443 #endif
2445 switch (is_ctor_dtor (name))
2447 case 1:
2448 if (! is_shared)
2449 add_to_list (&constructors, name);
2450 #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
2451 if (which_pass == PASS_OBJ)
2452 add_to_list (&exports, name);
2453 #endif
2454 break;
2456 case 2:
2457 if (! is_shared)
2458 add_to_list (&destructors, name);
2459 #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
2460 if (which_pass == PASS_OBJ)
2461 add_to_list (&exports, name);
2462 #endif
2463 break;
2465 #ifdef COLLECT_EXPORT_LIST
2466 case 3:
2467 #ifndef LD_INIT_SWITCH
2468 if (is_shared)
2469 add_to_list (&constructors, name);
2470 #endif
2471 break;
2473 case 4:
2474 #ifndef LD_INIT_SWITCH
2475 if (is_shared)
2476 add_to_list (&destructors, name);
2477 #endif
2478 break;
2479 #endif
2481 case 5:
2482 if (! is_shared)
2483 add_to_list (&frame_tables, name);
2484 #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
2485 if (which_pass == PASS_OBJ)
2486 add_to_list (&exports, name);
2487 #endif
2488 break;
2490 default: /* not a constructor or destructor */
2491 #ifdef COLLECT_EXPORT_LIST
2492 /* Explicitly export all global symbols when
2493 building a shared object on AIX, but do not
2494 re-export symbols from another shared object
2495 and do not export symbols if the user
2496 provides an explicit export list. */
2497 if (shared_obj && !is_shared
2498 && which_pass == PASS_OBJ && !export_flag)
2499 add_to_list (&exports, name);
2500 #endif
2501 continue;
2504 if (debug)
2505 #if !defined(EXTENDED_COFF)
2506 fprintf (stderr, "\tsec=%d class=%d type=%s%o %s\n",
2507 symbol.n_scnum, symbol.n_sclass,
2508 (symbol.n_type ? "0" : ""), symbol.n_type,
2509 name);
2510 #else
2511 fprintf (stderr,
2512 "\tiss = %5d, value = %5ld, index = %5d, name = %s\n",
2513 symbol.iss, (long) symbol.value, symbol.index, name);
2514 #endif
2518 #ifdef COLLECT_EXPORT_LIST
2519 else
2521 /* If archive contains both 32-bit and 64-bit objects,
2522 we want to skip objects in other mode so mismatch normal. */
2523 if (debug)
2524 fprintf (stderr, "%s : magic=%o aix64=%d mismatch\n",
2525 prog_name, HEADER (ldptr).f_magic, aix64_flag);
2527 #endif
2529 else
2531 fatal ("%s: cannot open as COFF file", prog_name);
2533 #ifdef COLLECT_EXPORT_LIST
2534 /* On AIX loop continues while there are more members in archive. */
2536 while (ldclose (ldptr) == FAILURE);
2537 #else
2538 /* Otherwise we simply close ldptr. */
2539 (void) ldclose(ldptr);
2540 #endif
2542 #endif /* OBJECT_FORMAT_COFF */
2544 #ifdef COLLECT_EXPORT_LIST
2545 /* Given a library name without "lib" prefix, this function
2546 returns a full library name including a path. */
2547 static char *
2548 resolve_lib_name (const char *name)
2550 char *lib_buf;
2551 int i, j, l = 0;
2552 /* Library extensions for AIX dynamic linking. */
2553 const char * const libexts[2] = {"a", "so"};
2555 for (i = 0; libpaths[i]; i++)
2556 if (libpaths[i]->max_len > l)
2557 l = libpaths[i]->max_len;
2559 lib_buf = xmalloc (l + strlen(name) + 10);
2561 for (i = 0; libpaths[i]; i++)
2563 struct prefix_list *list = libpaths[i]->plist;
2564 for (; list; list = list->next)
2566 /* The following lines are needed because path_prefix list
2567 may contain directories both with trailing '/' and
2568 without it. */
2569 const char *p = "";
2570 if (list->prefix[strlen(list->prefix)-1] != '/')
2571 p = "/";
2572 for (j = 0; j < 2; j++)
2574 sprintf (lib_buf, "%s%slib%s.%s",
2575 list->prefix, p, name,
2576 libexts[(j + aixrtl_flag) % 2]);
2577 if (debug) fprintf (stderr, "searching for: %s\n", lib_buf);
2578 if (file_exists (lib_buf))
2580 if (debug) fprintf (stderr, "found: %s\n", lib_buf);
2581 return (lib_buf);
2586 if (debug)
2587 fprintf (stderr, "not found\n");
2588 else
2589 fatal ("library lib%s not found", name);
2590 return (NULL);
2592 #endif /* COLLECT_EXPORT_LIST */