gcc/cp/
[official-gcc.git] / gcc / collect2.c
blob713c8f2d5627ff94793c1b25c8b906d2d2315752
1 /* Collect static initialization info into data structures that can be
2 traversed by C++ initialization and finalization routines.
3 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007 Free Software Foundation, Inc.
5 Contributed by Chris Smith (csmith@convex.com).
6 Heavily modified by Michael Meissner (meissner@cygnus.com),
7 Per Bothner (bothner@cygnus.com), and John Gilmore (gnu@cygnus.com).
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 for more details.
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
26 /* Build tables of static constructors and destructors and run ld. */
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include <signal.h>
33 #if ! defined( SIGCHLD ) && defined( SIGCLD )
34 # define SIGCHLD SIGCLD
35 #endif
37 #ifndef LIBRARY_PATH_ENV
38 #define LIBRARY_PATH_ENV "LIBRARY_PATH"
39 #endif
41 #define COLLECT
43 #include "collect2.h"
44 #include "demangle.h"
45 #include "obstack.h"
46 #include "intl.h"
47 #include "version.h"
49 /* On certain systems, we have code that works by scanning the object file
50 directly. But this code uses system-specific header files and library
51 functions, so turn it off in a cross-compiler. Likewise, the names of
52 the utilities are not correct for a cross-compiler; we have to hope that
53 cross-versions are in the proper directories. */
55 #ifdef CROSS_DIRECTORY_STRUCTURE
56 #undef OBJECT_FORMAT_COFF
57 #undef MD_EXEC_PREFIX
58 #undef REAL_LD_FILE_NAME
59 #undef REAL_NM_FILE_NAME
60 #undef REAL_STRIP_FILE_NAME
61 #endif
63 /* If we cannot use a special method, use the ordinary one:
64 run nm to find what symbols are present.
65 In a cross-compiler, this means you need a cross nm,
66 but that is not quite as unpleasant as special headers. */
68 #if !defined (OBJECT_FORMAT_COFF)
69 #define OBJECT_FORMAT_NONE
70 #endif
72 #ifdef OBJECT_FORMAT_COFF
74 #include <a.out.h>
75 #include <ar.h>
77 #ifdef UMAX
78 #include <sgs.h>
79 #endif
81 /* Many versions of ldfcn.h define these. */
82 #ifdef FREAD
83 #undef FREAD
84 #undef FWRITE
85 #endif
87 #include <ldfcn.h>
89 /* Some systems have an ISCOFF macro, but others do not. In some cases
90 the macro may be wrong. MY_ISCOFF is defined in tm.h files for machines
91 that either do not have an ISCOFF macro in /usr/include or for those
92 where it is wrong. */
94 #ifndef MY_ISCOFF
95 #define MY_ISCOFF(X) ISCOFF (X)
96 #endif
98 #endif /* OBJECT_FORMAT_COFF */
100 #ifdef OBJECT_FORMAT_NONE
102 /* Default flags to pass to nm. */
103 #ifndef NM_FLAGS
104 #define NM_FLAGS "-n"
105 #endif
107 #endif /* OBJECT_FORMAT_NONE */
109 /* Some systems use __main in a way incompatible with its use in gcc, in these
110 cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
111 give the same symbol without quotes for an alternative entry point. */
112 #ifndef NAME__MAIN
113 #define NAME__MAIN "__main"
114 #endif
116 /* This must match tree.h. */
117 #define DEFAULT_INIT_PRIORITY 65535
119 #ifndef COLLECT_SHARED_INIT_FUNC
120 #define COLLECT_SHARED_INIT_FUNC(STREAM, FUNC) \
121 fprintf ((STREAM), "void _GLOBAL__DI() {\n\t%s();\n}\n", (FUNC))
122 #endif
123 #ifndef COLLECT_SHARED_FINI_FUNC
124 #define COLLECT_SHARED_FINI_FUNC(STREAM, FUNC) \
125 fprintf ((STREAM), "void _GLOBAL__DD() {\n\t%s();\n}\n", (FUNC))
126 #endif
128 #ifdef LDD_SUFFIX
129 #define SCAN_LIBRARIES
130 #endif
132 #ifndef SHLIB_SUFFIX
133 #define SHLIB_SUFFIX ".so"
134 #endif
136 #ifdef USE_COLLECT2
137 int do_collecting = 1;
138 #else
139 int do_collecting = 0;
140 #endif
142 /* Nonzero if we should suppress the automatic demangling of identifiers
143 in linker error messages. Set from COLLECT_NO_DEMANGLE. */
144 int no_demangle;
146 /* Linked lists of constructor and destructor names. */
148 struct id
150 struct id *next;
151 int sequence;
152 char name[1];
155 struct head
157 struct id *first;
158 struct id *last;
159 int number;
162 /* Enumeration giving which pass this is for scanning the program file. */
164 enum pass {
165 PASS_FIRST, /* without constructors */
166 PASS_OBJ, /* individual objects */
167 PASS_LIB, /* looking for shared libraries */
168 PASS_SECOND /* with constructors linked in */
171 int vflag; /* true if -v */
172 static int rflag; /* true if -r */
173 static int strip_flag; /* true if -s */
174 static const char *demangle_flag;
175 #ifdef COLLECT_EXPORT_LIST
176 static int export_flag; /* true if -bE */
177 static int aix64_flag; /* true if -b64 */
178 static int aixrtl_flag; /* true if -brtl */
179 #endif
181 int debug; /* true if -debug */
183 static int shared_obj; /* true if -shared */
185 static const char *c_file; /* <xxx>.c for constructor/destructor list. */
186 static const char *o_file; /* <xxx>.o for constructor/destructor list. */
187 #ifdef COLLECT_EXPORT_LIST
188 static const char *export_file; /* <xxx>.x for AIX export list. */
189 #endif
190 const char *ldout; /* File for ld stdout. */
191 const char *lderrout; /* File for ld stderr. */
192 static const char *output_file; /* Output file for ld. */
193 static const char *nm_file_name; /* pathname of nm */
194 #ifdef LDD_SUFFIX
195 static const char *ldd_file_name; /* pathname of ldd (or equivalent) */
196 #endif
197 static const char *strip_file_name; /* pathname of strip */
198 const char *c_file_name; /* pathname of gcc */
199 static char *initname, *fininame; /* names of init and fini funcs */
201 static struct head constructors; /* list of constructors found */
202 static struct head destructors; /* list of destructors found */
203 #ifdef COLLECT_EXPORT_LIST
204 static struct head exports; /* list of exported symbols */
205 #endif
206 static struct head frame_tables; /* list of frame unwind info tables */
208 static bool at_file_supplied; /* Whether to use @file arguments */
209 static char *response_file; /* Name of any current response file */
211 struct obstack temporary_obstack;
212 char * temporary_firstobj;
214 /* Structure to hold all the directories in which to search for files to
215 execute. */
217 struct prefix_list
219 const char *prefix; /* String to prepend to the path. */
220 struct prefix_list *next; /* Next in linked list. */
223 struct path_prefix
225 struct prefix_list *plist; /* List of prefixes to try */
226 int max_len; /* Max length of a prefix in PLIST */
227 const char *name; /* Name of this list (used in config stuff) */
230 #ifdef COLLECT_EXPORT_LIST
231 /* Lists to keep libraries to be scanned for global constructors/destructors. */
232 static struct head libs; /* list of libraries */
233 static struct path_prefix cmdline_lib_dirs; /* directories specified with -L */
234 static struct path_prefix libpath_lib_dirs; /* directories in LIBPATH */
235 static struct path_prefix *libpaths[3] = {&cmdline_lib_dirs,
236 &libpath_lib_dirs, NULL};
237 #endif
239 static void handler (int);
240 static int is_ctor_dtor (const char *);
241 static char *find_a_file (struct path_prefix *, const char *);
242 static void add_prefix (struct path_prefix *, const char *);
243 static void prefix_from_env (const char *, struct path_prefix *);
244 static void prefix_from_string (const char *, struct path_prefix *);
245 static void do_wait (const char *, struct pex_obj *);
246 static void fork_execute (const char *, char **);
247 static void maybe_unlink (const char *);
248 static void add_to_list (struct head *, const char *);
249 static int extract_init_priority (const char *);
250 static void sort_ids (struct head *);
251 static void write_list (FILE *, const char *, struct id *);
252 #ifdef COLLECT_EXPORT_LIST
253 static void dump_list (FILE *, const char *, struct id *);
254 #endif
255 #if 0
256 static void dump_prefix_list (FILE *, const char *, struct prefix_list *);
257 #endif
258 static void write_list_with_asm (FILE *, const char *, struct id *);
259 static void write_c_file (FILE *, const char *);
260 static void write_c_file_stat (FILE *, const char *);
261 #ifndef LD_INIT_SWITCH
262 static void write_c_file_glob (FILE *, const char *);
263 #endif
264 static void scan_prog_file (const char *, enum pass);
265 #ifdef SCAN_LIBRARIES
266 static void scan_libraries (const char *);
267 #endif
268 #if LINK_ELIMINATE_DUPLICATE_LDIRECTORIES
269 static int is_in_args (const char *, const char **, const char **);
270 #endif
271 #ifdef COLLECT_EXPORT_LIST
272 #if 0
273 static int is_in_list (const char *, struct id *);
274 #endif
275 static void write_aix_file (FILE *, struct id *);
276 static char *resolve_lib_name (const char *);
277 #endif
278 static char *extract_string (const char **);
280 /* Delete tempfiles and exit function. */
282 void
283 collect_exit (int status)
285 if (c_file != 0 && c_file[0])
286 maybe_unlink (c_file);
288 if (o_file != 0 && o_file[0])
289 maybe_unlink (o_file);
291 #ifdef COLLECT_EXPORT_LIST
292 if (export_file != 0 && export_file[0])
293 maybe_unlink (export_file);
294 #endif
296 if (ldout != 0 && ldout[0])
298 dump_file (ldout, stdout);
299 maybe_unlink (ldout);
302 if (lderrout != 0 && lderrout[0])
304 dump_file (lderrout, stderr);
305 maybe_unlink (lderrout);
308 if (status != 0 && output_file != 0 && output_file[0])
309 maybe_unlink (output_file);
311 if (response_file)
312 maybe_unlink (response_file);
314 exit (status);
318 /* Notify user of a non-error. */
319 void
320 notice (const char *cmsgid, ...)
322 va_list ap;
324 va_start (ap, cmsgid);
325 vfprintf (stderr, _(cmsgid), ap);
326 va_end (ap);
329 /* Die when sys call fails. */
331 void
332 fatal_perror (const char * cmsgid, ...)
334 int e = errno;
335 va_list ap;
337 va_start (ap, cmsgid);
338 fprintf (stderr, "collect2: ");
339 vfprintf (stderr, _(cmsgid), ap);
340 fprintf (stderr, ": %s\n", xstrerror (e));
341 va_end (ap);
343 collect_exit (FATAL_EXIT_CODE);
346 /* Just die. */
348 void
349 fatal (const char * cmsgid, ...)
351 va_list ap;
353 va_start (ap, cmsgid);
354 fprintf (stderr, "collect2: ");
355 vfprintf (stderr, _(cmsgid), ap);
356 fprintf (stderr, "\n");
357 va_end (ap);
359 collect_exit (FATAL_EXIT_CODE);
362 /* Write error message. */
364 void
365 error (const char * gmsgid, ...)
367 va_list ap;
369 va_start (ap, gmsgid);
370 fprintf (stderr, "collect2: ");
371 vfprintf (stderr, _(gmsgid), ap);
372 fprintf (stderr, "\n");
373 va_end(ap);
376 /* In case obstack is linked in, and abort is defined to fancy_abort,
377 provide a default entry. */
379 void
380 fancy_abort (const char *file, int line, const char *func)
382 fatal ("internal gcc abort in %s, at %s:%d", func, file, line);
385 static void
386 handler (int signo)
388 if (c_file != 0 && c_file[0])
389 maybe_unlink (c_file);
391 if (o_file != 0 && o_file[0])
392 maybe_unlink (o_file);
394 if (ldout != 0 && ldout[0])
395 maybe_unlink (ldout);
397 if (lderrout != 0 && lderrout[0])
398 maybe_unlink (lderrout);
400 #ifdef COLLECT_EXPORT_LIST
401 if (export_file != 0 && export_file[0])
402 maybe_unlink (export_file);
403 #endif
405 if (response_file)
406 maybe_unlink (response_file);
408 signal (signo, SIG_DFL);
409 raise (signo);
414 file_exists (const char *name)
416 return access (name, R_OK) == 0;
419 /* Parse a reasonable subset of shell quoting syntax. */
421 static char *
422 extract_string (const char **pp)
424 const char *p = *pp;
425 int backquote = 0;
426 int inside = 0;
428 for (;;)
430 char c = *p;
431 if (c == '\0')
432 break;
433 ++p;
434 if (backquote)
435 obstack_1grow (&temporary_obstack, c);
436 else if (! inside && c == ' ')
437 break;
438 else if (! inside && c == '\\')
439 backquote = 1;
440 else if (c == '\'')
441 inside = !inside;
442 else
443 obstack_1grow (&temporary_obstack, c);
446 obstack_1grow (&temporary_obstack, '\0');
447 *pp = p;
448 return XOBFINISH (&temporary_obstack, char *);
451 void
452 dump_file (const char *name, FILE *to)
454 FILE *stream = fopen (name, "r");
456 if (stream == 0)
457 return;
458 while (1)
460 int c;
461 while (c = getc (stream),
462 c != EOF && (ISIDNUM (c) || c == '$' || c == '.'))
463 obstack_1grow (&temporary_obstack, c);
464 if (obstack_object_size (&temporary_obstack) > 0)
466 const char *word, *p;
467 char *result;
468 obstack_1grow (&temporary_obstack, '\0');
469 word = XOBFINISH (&temporary_obstack, const char *);
471 if (*word == '.')
472 ++word, putc ('.', to);
473 p = word;
474 if (!strncmp (p, USER_LABEL_PREFIX, strlen (USER_LABEL_PREFIX)))
475 p += strlen (USER_LABEL_PREFIX);
477 #ifdef HAVE_LD_DEMANGLE
478 result = 0;
479 #else
480 if (no_demangle)
481 result = 0;
482 else
483 result = cplus_demangle (p, DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE);
484 #endif
486 if (result)
488 int diff;
489 fputs (result, to);
491 diff = strlen (word) - strlen (result);
492 while (diff > 0 && c == ' ')
493 --diff, putc (' ', to);
494 if (diff < 0 && c == ' ')
496 while (diff < 0 && c == ' ')
497 ++diff, c = getc (stream);
498 if (!ISSPACE (c))
500 /* Make sure we output at least one space, or
501 the demangled symbol name will run into
502 whatever text follows. */
503 putc (' ', to);
507 free (result);
509 else
510 fputs (word, to);
512 fflush (to);
513 obstack_free (&temporary_obstack, temporary_firstobj);
515 if (c == EOF)
516 break;
517 putc (c, to);
519 fclose (stream);
522 /* Decide whether the given symbol is: a constructor (1), a destructor
523 (2), a routine in a shared object that calls all the constructors
524 (3) or destructors (4), a DWARF exception-handling table (5), or
525 nothing special (0). */
527 static int
528 is_ctor_dtor (const char *s)
530 struct names { const char *const name; const int len; const int ret;
531 const int two_underscores; };
533 const struct names *p;
534 int ch;
535 const char *orig_s = s;
537 static const struct names special[] = {
538 #ifndef NO_DOLLAR_IN_LABEL
539 { "GLOBAL__I$", sizeof ("GLOBAL__I$")-1, 1, 0 },
540 { "GLOBAL__D$", sizeof ("GLOBAL__D$")-1, 2, 0 },
541 #else
542 #ifndef NO_DOT_IN_LABEL
543 { "GLOBAL__I.", sizeof ("GLOBAL__I.")-1, 1, 0 },
544 { "GLOBAL__D.", sizeof ("GLOBAL__D.")-1, 2, 0 },
545 #endif /* NO_DOT_IN_LABEL */
546 #endif /* NO_DOLLAR_IN_LABEL */
547 { "GLOBAL__I_", sizeof ("GLOBAL__I_")-1, 1, 0 },
548 { "GLOBAL__D_", sizeof ("GLOBAL__D_")-1, 2, 0 },
549 { "GLOBAL__F_", sizeof ("GLOBAL__F_")-1, 5, 0 },
550 { "GLOBAL__FI_", sizeof ("GLOBAL__FI_")-1, 3, 0 },
551 { "GLOBAL__FD_", sizeof ("GLOBAL__FD_")-1, 4, 0 },
552 { NULL, 0, 0, 0 }
555 while ((ch = *s) == '_')
556 ++s;
558 if (s == orig_s)
559 return 0;
561 for (p = &special[0]; p->len > 0; p++)
563 if (ch == p->name[0]
564 && (!p->two_underscores || ((s - orig_s) >= 2))
565 && strncmp(s, p->name, p->len) == 0)
567 return p->ret;
570 return 0;
573 /* We maintain two prefix lists: one from COMPILER_PATH environment variable
574 and one from the PATH variable. */
576 static struct path_prefix cpath, path;
578 #ifdef CROSS_DIRECTORY_STRUCTURE
579 /* This is the name of the target machine. We use it to form the name
580 of the files to execute. */
582 static const char *const target_machine = TARGET_MACHINE;
583 #endif
585 /* Search for NAME using prefix list PPREFIX. We only look for executable
586 files.
588 Return 0 if not found, otherwise return its name, allocated with malloc. */
590 static char *
591 find_a_file (struct path_prefix *pprefix, const char *name)
593 char *temp;
594 struct prefix_list *pl;
595 int len = pprefix->max_len + strlen (name) + 1;
597 if (debug)
598 fprintf (stderr, "Looking for '%s'\n", name);
600 #ifdef HOST_EXECUTABLE_SUFFIX
601 len += strlen (HOST_EXECUTABLE_SUFFIX);
602 #endif
604 temp = XNEWVEC (char, len);
606 /* Determine the filename to execute (special case for absolute paths). */
608 if (IS_ABSOLUTE_PATH (name))
610 if (access (name, X_OK) == 0)
612 strcpy (temp, name);
614 if (debug)
615 fprintf (stderr, " - found: absolute path\n");
617 return temp;
620 #ifdef HOST_EXECUTABLE_SUFFIX
621 /* Some systems have a suffix for executable files.
622 So try appending that. */
623 strcpy (temp, name);
624 strcat (temp, HOST_EXECUTABLE_SUFFIX);
626 if (access (temp, X_OK) == 0)
627 return temp;
628 #endif
630 if (debug)
631 fprintf (stderr, " - failed to locate using absolute path\n");
633 else
634 for (pl = pprefix->plist; pl; pl = pl->next)
636 struct stat st;
638 strcpy (temp, pl->prefix);
639 strcat (temp, name);
641 if (stat (temp, &st) >= 0
642 && ! S_ISDIR (st.st_mode)
643 && access (temp, X_OK) == 0)
644 return temp;
646 #ifdef HOST_EXECUTABLE_SUFFIX
647 /* Some systems have a suffix for executable files.
648 So try appending that. */
649 strcat (temp, HOST_EXECUTABLE_SUFFIX);
651 if (stat (temp, &st) >= 0
652 && ! S_ISDIR (st.st_mode)
653 && access (temp, X_OK) == 0)
654 return temp;
655 #endif
658 if (debug && pprefix->plist == NULL)
659 fprintf (stderr, " - failed: no entries in prefix list\n");
661 free (temp);
662 return 0;
665 /* Add an entry for PREFIX to prefix list PPREFIX. */
667 static void
668 add_prefix (struct path_prefix *pprefix, const char *prefix)
670 struct prefix_list *pl, **prev;
671 int len;
673 if (pprefix->plist)
675 for (pl = pprefix->plist; pl->next; pl = pl->next)
677 prev = &pl->next;
679 else
680 prev = &pprefix->plist;
682 /* Keep track of the longest prefix. */
684 len = strlen (prefix);
685 if (len > pprefix->max_len)
686 pprefix->max_len = len;
688 pl = XNEW (struct prefix_list);
689 pl->prefix = xstrdup (prefix);
691 if (*prev)
692 pl->next = *prev;
693 else
694 pl->next = (struct prefix_list *) 0;
695 *prev = pl;
698 /* Take the value of the environment variable ENV, break it into a path, and
699 add of the entries to PPREFIX. */
701 static void
702 prefix_from_env (const char *env, struct path_prefix *pprefix)
704 const char *p;
705 GET_ENVIRONMENT (p, env);
707 if (p)
708 prefix_from_string (p, pprefix);
711 static void
712 prefix_from_string (const char *p, struct path_prefix *pprefix)
714 const char *startp, *endp;
715 char *nstore = XNEWVEC (char, strlen (p) + 3);
717 if (debug)
718 fprintf (stderr, "Convert string '%s' into prefixes, separator = '%c'\n", p, PATH_SEPARATOR);
720 startp = endp = p;
721 while (1)
723 if (*endp == PATH_SEPARATOR || *endp == 0)
725 strncpy (nstore, startp, endp-startp);
726 if (endp == startp)
728 strcpy (nstore, "./");
730 else if (! IS_DIR_SEPARATOR (endp[-1]))
732 nstore[endp-startp] = DIR_SEPARATOR;
733 nstore[endp-startp+1] = 0;
735 else
736 nstore[endp-startp] = 0;
738 if (debug)
739 fprintf (stderr, " - add prefix: %s\n", nstore);
741 add_prefix (pprefix, nstore);
742 if (*endp == 0)
743 break;
744 endp = startp = endp + 1;
746 else
747 endp++;
749 free (nstore);
752 /* Main program. */
755 main (int argc, char **argv)
757 static const char *const ld_suffix = "ld";
758 static const char *const real_ld_suffix = "real-ld";
759 static const char *const collect_ld_suffix = "collect-ld";
760 static const char *const nm_suffix = "nm";
761 static const char *const gnm_suffix = "gnm";
762 #ifdef LDD_SUFFIX
763 static const char *const ldd_suffix = LDD_SUFFIX;
764 #endif
765 static const char *const strip_suffix = "strip";
766 static const char *const gstrip_suffix = "gstrip";
768 #ifdef CROSS_DIRECTORY_STRUCTURE
769 /* If we look for a program in the compiler directories, we just use
770 the short name, since these directories are already system-specific.
771 But it we look for a program in the system directories, we need to
772 qualify the program name with the target machine. */
774 const char *const full_ld_suffix =
775 concat(target_machine, "-", ld_suffix, NULL);
776 const char *const full_nm_suffix =
777 concat (target_machine, "-", nm_suffix, NULL);
778 const char *const full_gnm_suffix =
779 concat (target_machine, "-", gnm_suffix, NULL);
780 #ifdef LDD_SUFFIX
781 const char *const full_ldd_suffix =
782 concat (target_machine, "-", ldd_suffix, NULL);
783 #endif
784 const char *const full_strip_suffix =
785 concat (target_machine, "-", strip_suffix, NULL);
786 const char *const full_gstrip_suffix =
787 concat (target_machine, "-", gstrip_suffix, NULL);
788 #else
789 const char *const full_ld_suffix = ld_suffix;
790 const char *const full_nm_suffix = nm_suffix;
791 const char *const full_gnm_suffix = gnm_suffix;
792 #ifdef LDD_SUFFIX
793 const char *const full_ldd_suffix = ldd_suffix;
794 #endif
795 const char *const full_strip_suffix = strip_suffix;
796 const char *const full_gstrip_suffix = gstrip_suffix;
797 #endif /* CROSS_DIRECTORY_STRUCTURE */
799 const char *arg;
800 FILE *outf;
801 #ifdef COLLECT_EXPORT_LIST
802 FILE *exportf;
803 #endif
804 const char *ld_file_name;
805 const char *p;
806 char **c_argv;
807 const char **c_ptr;
808 char **ld1_argv;
809 const char **ld1;
810 char **ld2_argv;
811 const char **ld2;
812 char **object_lst;
813 const char **object;
814 int first_file;
815 int num_c_args;
816 char **old_argv;
818 old_argv = argv;
819 expandargv (&argc, &argv);
820 if (argv != old_argv)
821 at_file_supplied = 1;
823 num_c_args = argc + 9;
825 no_demangle = !! getenv ("COLLECT_NO_DEMANGLE");
827 /* Suppress demangling by the real linker, which may be broken. */
828 putenv (xstrdup ("COLLECT_NO_DEMANGLE="));
830 #if defined (COLLECT2_HOST_INITIALIZATION)
831 /* Perform system dependent initialization, if necessary. */
832 COLLECT2_HOST_INITIALIZATION;
833 #endif
835 #ifdef SIGCHLD
836 /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
837 receive the signal. A different setting is inheritable */
838 signal (SIGCHLD, SIG_DFL);
839 #endif
841 /* Unlock the stdio streams. */
842 unlock_std_streams ();
844 gcc_init_libintl ();
846 /* Do not invoke xcalloc before this point, since locale needs to be
847 set first, in case a diagnostic is issued. */
849 ld1 = (const char **)(ld1_argv = XCNEWVEC (char *, argc+4));
850 ld2 = (const char **)(ld2_argv = XCNEWVEC (char *, argc+11));
851 object = (const char **)(object_lst = XCNEWVEC (char *, argc));
853 #ifdef DEBUG
854 debug = 1;
855 #endif
857 /* Parse command line early for instances of -debug. This allows
858 the debug flag to be set before functions like find_a_file()
859 are called. */
861 int i;
863 for (i = 1; argv[i] != NULL; i ++)
865 if (! strcmp (argv[i], "-debug"))
866 debug = 1;
868 vflag = debug;
871 #ifndef DEFAULT_A_OUT_NAME
872 output_file = "a.out";
873 #else
874 output_file = DEFAULT_A_OUT_NAME;
875 #endif
877 obstack_begin (&temporary_obstack, 0);
878 temporary_firstobj = (char *) obstack_alloc (&temporary_obstack, 0);
880 #ifndef HAVE_LD_DEMANGLE
881 current_demangling_style = auto_demangling;
882 #endif
883 p = getenv ("COLLECT_GCC_OPTIONS");
884 while (p && *p)
886 const char *q = extract_string (&p);
887 if (*q == '-' && (q[1] == 'm' || q[1] == 'f'))
888 num_c_args++;
890 obstack_free (&temporary_obstack, temporary_firstobj);
892 /* -fno-profile-arcs -fno-test-coverage -fno-branch-probabilities
893 -fno-exceptions -w */
894 num_c_args += 5;
896 c_ptr = (const char **) (c_argv = XCNEWVEC (char *, num_c_args));
898 if (argc < 2)
899 fatal ("no arguments");
901 #ifdef SIGQUIT
902 if (signal (SIGQUIT, SIG_IGN) != SIG_IGN)
903 signal (SIGQUIT, handler);
904 #endif
905 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
906 signal (SIGINT, handler);
907 #ifdef SIGALRM
908 if (signal (SIGALRM, SIG_IGN) != SIG_IGN)
909 signal (SIGALRM, handler);
910 #endif
911 #ifdef SIGHUP
912 if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
913 signal (SIGHUP, handler);
914 #endif
915 if (signal (SIGSEGV, SIG_IGN) != SIG_IGN)
916 signal (SIGSEGV, handler);
917 #ifdef SIGBUS
918 if (signal (SIGBUS, SIG_IGN) != SIG_IGN)
919 signal (SIGBUS, handler);
920 #endif
922 /* Extract COMPILER_PATH and PATH into our prefix list. */
923 prefix_from_env ("COMPILER_PATH", &cpath);
924 prefix_from_env ("PATH", &path);
926 /* Try to discover a valid linker/nm/strip to use. */
928 /* Maybe we know the right file to use (if not cross). */
929 ld_file_name = 0;
930 #ifdef DEFAULT_LINKER
931 if (access (DEFAULT_LINKER, X_OK) == 0)
932 ld_file_name = DEFAULT_LINKER;
933 if (ld_file_name == 0)
934 #endif
935 #ifdef REAL_LD_FILE_NAME
936 ld_file_name = find_a_file (&path, REAL_LD_FILE_NAME);
937 if (ld_file_name == 0)
938 #endif
939 /* Search the (target-specific) compiler dirs for ld'. */
940 ld_file_name = find_a_file (&cpath, real_ld_suffix);
941 /* Likewise for `collect-ld'. */
942 if (ld_file_name == 0)
943 ld_file_name = find_a_file (&cpath, collect_ld_suffix);
944 /* Search the compiler directories for `ld'. We have protection against
945 recursive calls in find_a_file. */
946 if (ld_file_name == 0)
947 ld_file_name = find_a_file (&cpath, ld_suffix);
948 /* Search the ordinary system bin directories
949 for `ld' (if native linking) or `TARGET-ld' (if cross). */
950 if (ld_file_name == 0)
951 ld_file_name = find_a_file (&path, full_ld_suffix);
953 #ifdef REAL_NM_FILE_NAME
954 nm_file_name = find_a_file (&path, REAL_NM_FILE_NAME);
955 if (nm_file_name == 0)
956 #endif
957 nm_file_name = find_a_file (&cpath, gnm_suffix);
958 if (nm_file_name == 0)
959 nm_file_name = find_a_file (&path, full_gnm_suffix);
960 if (nm_file_name == 0)
961 nm_file_name = find_a_file (&cpath, nm_suffix);
962 if (nm_file_name == 0)
963 nm_file_name = find_a_file (&path, full_nm_suffix);
965 #ifdef LDD_SUFFIX
966 ldd_file_name = find_a_file (&cpath, ldd_suffix);
967 if (ldd_file_name == 0)
968 ldd_file_name = find_a_file (&path, full_ldd_suffix);
969 #endif
971 #ifdef REAL_STRIP_FILE_NAME
972 strip_file_name = find_a_file (&path, REAL_STRIP_FILE_NAME);
973 if (strip_file_name == 0)
974 #endif
975 strip_file_name = find_a_file (&cpath, gstrip_suffix);
976 if (strip_file_name == 0)
977 strip_file_name = find_a_file (&path, full_gstrip_suffix);
978 if (strip_file_name == 0)
979 strip_file_name = find_a_file (&cpath, strip_suffix);
980 if (strip_file_name == 0)
981 strip_file_name = find_a_file (&path, full_strip_suffix);
983 /* Determine the full path name of the C compiler to use. */
984 c_file_name = getenv ("COLLECT_GCC");
985 if (c_file_name == 0)
987 #ifdef CROSS_DIRECTORY_STRUCTURE
988 c_file_name = concat (target_machine, "-gcc", NULL);
989 #else
990 c_file_name = "gcc";
991 #endif
994 p = find_a_file (&cpath, c_file_name);
996 /* Here it should be safe to use the system search path since we should have
997 already qualified the name of the compiler when it is needed. */
998 if (p == 0)
999 p = find_a_file (&path, c_file_name);
1001 if (p)
1002 c_file_name = p;
1004 *ld1++ = *ld2++ = ld_file_name;
1006 /* Make temp file names. */
1007 c_file = make_temp_file (".c");
1008 o_file = make_temp_file (".o");
1009 #ifdef COLLECT_EXPORT_LIST
1010 export_file = make_temp_file (".x");
1011 #endif
1012 ldout = make_temp_file (".ld");
1013 lderrout = make_temp_file (".le");
1014 *c_ptr++ = c_file_name;
1015 *c_ptr++ = "-x";
1016 *c_ptr++ = "c";
1017 *c_ptr++ = "-c";
1018 *c_ptr++ = "-o";
1019 *c_ptr++ = o_file;
1021 #ifdef COLLECT_EXPORT_LIST
1022 /* Generate a list of directories from LIBPATH. */
1023 prefix_from_env ("LIBPATH", &libpath_lib_dirs);
1024 /* Add to this list also two standard directories where
1025 AIX loader always searches for libraries. */
1026 add_prefix (&libpath_lib_dirs, "/lib");
1027 add_prefix (&libpath_lib_dirs, "/usr/lib");
1028 #endif
1030 /* Get any options that the upper GCC wants to pass to the sub-GCC.
1032 AIX support needs to know if -shared has been specified before
1033 parsing commandline arguments. */
1035 p = getenv ("COLLECT_GCC_OPTIONS");
1036 while (p && *p)
1038 const char *q = extract_string (&p);
1039 if (*q == '-' && (q[1] == 'm' || q[1] == 'f'))
1040 *c_ptr++ = xstrdup (q);
1041 if (strcmp (q, "-EL") == 0 || strcmp (q, "-EB") == 0)
1042 *c_ptr++ = xstrdup (q);
1043 if (strcmp (q, "-shared") == 0)
1044 shared_obj = 1;
1045 if (*q == '-' && q[1] == 'B')
1047 *c_ptr++ = xstrdup (q);
1048 if (q[2] == 0)
1050 q = extract_string (&p);
1051 *c_ptr++ = xstrdup (q);
1055 obstack_free (&temporary_obstack, temporary_firstobj);
1056 *c_ptr++ = "-fno-profile-arcs";
1057 *c_ptr++ = "-fno-test-coverage";
1058 *c_ptr++ = "-fno-branch-probabilities";
1059 *c_ptr++ = "-fno-exceptions";
1060 *c_ptr++ = "-w";
1062 /* !!! When GCC calls collect2,
1063 it does not know whether it is calling collect2 or ld.
1064 So collect2 cannot meaningfully understand any options
1065 except those ld understands.
1066 If you propose to make GCC pass some other option,
1067 just imagine what will happen if ld is really ld!!! */
1069 /* Parse arguments. Remember output file spec, pass the rest to ld. */
1070 /* After the first file, put in the c++ rt0. */
1072 first_file = 1;
1073 #ifdef HAVE_LD_DEMANGLE
1074 if (!demangle_flag && !no_demangle)
1075 demangle_flag = "--demangle";
1076 if (demangle_flag)
1077 *ld1++ = *ld2++ = demangle_flag;
1078 #endif
1079 while ((arg = *++argv) != (char *) 0)
1081 *ld1++ = *ld2++ = arg;
1083 if (arg[0] == '-')
1085 switch (arg[1])
1087 #ifdef COLLECT_EXPORT_LIST
1088 /* We want to disable automatic exports on AIX when user
1089 explicitly puts an export list in command line */
1090 case 'b':
1091 if (arg[2] == 'E' || strncmp (&arg[2], "export", 6) == 0)
1092 export_flag = 1;
1093 else if (arg[2] == '6' && arg[3] == '4')
1094 aix64_flag = 1;
1095 else if (arg[2] == 'r' && arg[3] == 't' && arg[4] == 'l')
1096 aixrtl_flag = 1;
1097 break;
1098 #endif
1100 case 'd':
1101 if (!strcmp (arg, "-debug"))
1103 /* Already parsed. */
1104 ld1--;
1105 ld2--;
1107 if (!strcmp (arg, "-dynamic-linker") && argv[1])
1109 ++argv;
1110 *ld1++ = *ld2++ = *argv;
1112 break;
1114 case 'l':
1115 if (first_file)
1117 /* place o_file BEFORE this argument! */
1118 first_file = 0;
1119 ld2--;
1120 *ld2++ = o_file;
1121 *ld2++ = arg;
1123 #ifdef COLLECT_EXPORT_LIST
1125 /* Resolving full library name. */
1126 const char *s = resolve_lib_name (arg+2);
1128 /* Saving a full library name. */
1129 add_to_list (&libs, s);
1131 #endif
1132 break;
1134 #ifdef COLLECT_EXPORT_LIST
1135 /* Saving directories where to search for libraries. */
1136 case 'L':
1137 add_prefix (&cmdline_lib_dirs, arg+2);
1138 break;
1139 #else
1140 #if LINK_ELIMINATE_DUPLICATE_LDIRECTORIES
1141 case 'L':
1142 if (is_in_args (arg, (const char **) ld1_argv, ld1-1))
1143 --ld1;
1144 break;
1145 #endif /* LINK_ELIMINATE_DUPLICATE_LDIRECTORIES */
1146 #endif
1148 case 'o':
1149 if (arg[2] == '\0')
1150 output_file = *ld1++ = *ld2++ = *++argv;
1151 else if (1
1152 #ifdef SWITCHES_NEED_SPACES
1153 && ! strchr (SWITCHES_NEED_SPACES, arg[1])
1154 #endif
1157 output_file = &arg[2];
1158 break;
1160 case 'r':
1161 if (arg[2] == '\0')
1162 rflag = 1;
1163 break;
1165 case 's':
1166 if (arg[2] == '\0' && do_collecting)
1168 /* We must strip after the nm run, otherwise C++ linking
1169 will not work. Thus we strip in the second ld run, or
1170 else with strip if there is no second ld run. */
1171 strip_flag = 1;
1172 ld1--;
1174 break;
1176 case 'v':
1177 if (arg[2] == '\0')
1178 vflag = 1;
1179 break;
1181 case '-':
1182 if (strcmp (arg, "--no-demangle") == 0)
1184 demangle_flag = arg;
1185 no_demangle = 1;
1186 ld1--;
1187 ld2--;
1189 else if (strncmp (arg, "--demangle", 10) == 0)
1191 demangle_flag = arg;
1192 no_demangle = 0;
1193 #ifndef HAVE_LD_DEMANGLE
1194 if (arg[10] == '=')
1196 enum demangling_styles style
1197 = cplus_demangle_name_to_style (arg+11);
1198 if (style == unknown_demangling)
1199 error ("unknown demangling style '%s'", arg+11);
1200 else
1201 current_demangling_style = style;
1203 #endif
1204 ld1--;
1205 ld2--;
1207 break;
1210 else if ((p = strrchr (arg, '.')) != (char *) 0
1211 && (strcmp (p, ".o") == 0 || strcmp (p, ".a") == 0
1212 || strcmp (p, ".so") == 0 || strcmp (p, ".lo") == 0
1213 || strcmp (p, ".obj") == 0))
1215 if (first_file)
1217 first_file = 0;
1218 if (p[1] == 'o')
1219 *ld2++ = o_file;
1220 else
1222 /* place o_file BEFORE this argument! */
1223 ld2--;
1224 *ld2++ = o_file;
1225 *ld2++ = arg;
1228 if (p[1] == 'o' || p[1] == 'l')
1229 *object++ = arg;
1230 #ifdef COLLECT_EXPORT_LIST
1231 /* libraries can be specified directly, i.e. without -l flag. */
1232 else
1234 /* Saving a full library name. */
1235 add_to_list (&libs, arg);
1237 #endif
1241 #ifdef COLLECT_EXPORT_LIST
1242 /* This is added only for debugging purposes. */
1243 if (debug)
1245 fprintf (stderr, "List of libraries:\n");
1246 dump_list (stderr, "\t", libs.first);
1249 /* The AIX linker will discard static constructors in object files if
1250 nothing else in the file is referenced, so look at them first. */
1252 const char **export_object_lst = (const char **)object_lst;
1254 while (export_object_lst < object)
1255 scan_prog_file (*export_object_lst++, PASS_OBJ);
1258 struct id *list = libs.first;
1260 for (; list; list = list->next)
1261 scan_prog_file (list->name, PASS_FIRST);
1264 if (exports.first)
1266 char *buf = concat ("-bE:", export_file, NULL);
1268 *ld1++ = buf;
1269 *ld2++ = buf;
1271 exportf = fopen (export_file, "w");
1272 if (exportf == (FILE *) 0)
1273 fatal_perror ("fopen %s", export_file);
1274 write_aix_file (exportf, exports.first);
1275 if (fclose (exportf))
1276 fatal_perror ("fclose %s", export_file);
1278 #endif
1280 *c_ptr++ = c_file;
1281 *c_ptr = *ld1 = *object = (char *) 0;
1283 if (vflag)
1285 notice ("collect2 version %s", version_string);
1286 #ifdef TARGET_VERSION
1287 TARGET_VERSION;
1288 #endif
1289 fprintf (stderr, "\n");
1292 if (debug)
1294 const char *ptr;
1295 fprintf (stderr, "ld_file_name = %s\n",
1296 (ld_file_name ? ld_file_name : "not found"));
1297 fprintf (stderr, "c_file_name = %s\n",
1298 (c_file_name ? c_file_name : "not found"));
1299 fprintf (stderr, "nm_file_name = %s\n",
1300 (nm_file_name ? nm_file_name : "not found"));
1301 #ifdef LDD_SUFFIX
1302 fprintf (stderr, "ldd_file_name = %s\n",
1303 (ldd_file_name ? ldd_file_name : "not found"));
1304 #endif
1305 fprintf (stderr, "strip_file_name = %s\n",
1306 (strip_file_name ? strip_file_name : "not found"));
1307 fprintf (stderr, "c_file = %s\n",
1308 (c_file ? c_file : "not found"));
1309 fprintf (stderr, "o_file = %s\n",
1310 (o_file ? o_file : "not found"));
1312 ptr = getenv ("COLLECT_GCC_OPTIONS");
1313 if (ptr)
1314 fprintf (stderr, "COLLECT_GCC_OPTIONS = %s\n", ptr);
1316 ptr = getenv ("COLLECT_GCC");
1317 if (ptr)
1318 fprintf (stderr, "COLLECT_GCC = %s\n", ptr);
1320 ptr = getenv ("COMPILER_PATH");
1321 if (ptr)
1322 fprintf (stderr, "COMPILER_PATH = %s\n", ptr);
1324 ptr = getenv (LIBRARY_PATH_ENV);
1325 if (ptr)
1326 fprintf (stderr, "%-20s= %s\n", LIBRARY_PATH_ENV, ptr);
1328 fprintf (stderr, "\n");
1331 /* Load the program, searching all libraries and attempting to provide
1332 undefined symbols from repository information. */
1334 /* On AIX we do this later. */
1335 #ifndef COLLECT_EXPORT_LIST
1336 do_tlink (ld1_argv, object_lst);
1337 #endif
1339 /* If -r or they will be run via some other method, do not build the
1340 constructor or destructor list, just return now. */
1341 if (rflag
1342 #ifndef COLLECT_EXPORT_LIST
1343 || ! do_collecting
1344 #endif
1347 #ifdef COLLECT_EXPORT_LIST
1348 /* Do the link we avoided above if we are exiting. */
1349 do_tlink (ld1_argv, object_lst);
1351 /* But make sure we delete the export file we may have created. */
1352 if (export_file != 0 && export_file[0])
1353 maybe_unlink (export_file);
1354 #endif
1355 maybe_unlink (c_file);
1356 maybe_unlink (o_file);
1357 return 0;
1360 /* Examine the namelist with nm and search it for static constructors
1361 and destructors to call.
1362 Write the constructor and destructor tables to a .s file and reload. */
1364 /* On AIX we already scanned for global constructors/destructors. */
1365 #ifndef COLLECT_EXPORT_LIST
1366 scan_prog_file (output_file, PASS_FIRST);
1367 #endif
1369 #ifdef SCAN_LIBRARIES
1370 scan_libraries (output_file);
1371 #endif
1373 if (debug)
1375 notice ("%d constructor(s) found\n", constructors.number);
1376 notice ("%d destructor(s) found\n", destructors.number);
1377 notice ("%d frame table(s) found\n", frame_tables.number);
1380 if (constructors.number == 0 && destructors.number == 0
1381 && frame_tables.number == 0
1382 #if defined (SCAN_LIBRARIES) || defined (COLLECT_EXPORT_LIST)
1383 /* If we will be running these functions ourselves, we want to emit
1384 stubs into the shared library so that we do not have to relink
1385 dependent programs when we add static objects. */
1386 && ! shared_obj
1387 #endif
1390 #ifdef COLLECT_EXPORT_LIST
1391 /* Do tlink without additional code generation. */
1392 do_tlink (ld1_argv, object_lst);
1393 #endif
1394 /* Strip now if it was requested on the command line. */
1395 if (strip_flag)
1397 char **real_strip_argv = XCNEWVEC (char *, 3);
1398 const char ** strip_argv = (const char **) real_strip_argv;
1400 strip_argv[0] = strip_file_name;
1401 strip_argv[1] = output_file;
1402 strip_argv[2] = (char *) 0;
1403 fork_execute ("strip", real_strip_argv);
1406 #ifdef COLLECT_EXPORT_LIST
1407 maybe_unlink (export_file);
1408 #endif
1409 maybe_unlink (c_file);
1410 maybe_unlink (o_file);
1411 return 0;
1414 /* Sort ctor and dtor lists by priority. */
1415 sort_ids (&constructors);
1416 sort_ids (&destructors);
1418 maybe_unlink(output_file);
1419 outf = fopen (c_file, "w");
1420 if (outf == (FILE *) 0)
1421 fatal_perror ("fopen %s", c_file);
1423 write_c_file (outf, c_file);
1425 if (fclose (outf))
1426 fatal_perror ("fclose %s", c_file);
1428 /* Tell the linker that we have initializer and finalizer functions. */
1429 #ifdef LD_INIT_SWITCH
1430 #ifdef COLLECT_EXPORT_LIST
1431 *ld2++ = concat (LD_INIT_SWITCH, ":", initname, ":", fininame, NULL);
1432 #else
1433 *ld2++ = LD_INIT_SWITCH;
1434 *ld2++ = initname;
1435 *ld2++ = LD_FINI_SWITCH;
1436 *ld2++ = fininame;
1437 #endif
1438 #endif
1440 #ifdef COLLECT_EXPORT_LIST
1441 if (shared_obj)
1443 /* If we did not add export flag to link arguments before, add it to
1444 second link phase now. No new exports should have been added. */
1445 if (! exports.first)
1446 *ld2++ = concat ("-bE:", export_file, NULL);
1448 #ifndef LD_INIT_SWITCH
1449 add_to_list (&exports, initname);
1450 add_to_list (&exports, fininame);
1451 add_to_list (&exports, "_GLOBAL__DI");
1452 add_to_list (&exports, "_GLOBAL__DD");
1453 #endif
1454 exportf = fopen (export_file, "w");
1455 if (exportf == (FILE *) 0)
1456 fatal_perror ("fopen %s", export_file);
1457 write_aix_file (exportf, exports.first);
1458 if (fclose (exportf))
1459 fatal_perror ("fclose %s", export_file);
1461 #endif
1463 /* End of arguments to second link phase. */
1464 *ld2 = (char*) 0;
1466 if (debug)
1468 fprintf (stderr, "\n========== output_file = %s, c_file = %s\n",
1469 output_file, c_file);
1470 write_c_file (stderr, "stderr");
1471 fprintf (stderr, "========== end of c_file\n\n");
1472 #ifdef COLLECT_EXPORT_LIST
1473 fprintf (stderr, "\n========== export_file = %s\n", export_file);
1474 write_aix_file (stderr, exports.first);
1475 fprintf (stderr, "========== end of export_file\n\n");
1476 #endif
1479 /* Assemble the constructor and destructor tables.
1480 Link the tables in with the rest of the program. */
1482 fork_execute ("gcc", c_argv);
1483 #ifdef COLLECT_EXPORT_LIST
1484 /* On AIX we must call tlink because of possible templates resolution. */
1485 do_tlink (ld2_argv, object_lst);
1486 #else
1487 /* Otherwise, simply call ld because tlink is already done. */
1488 fork_execute ("ld", ld2_argv);
1490 /* Let scan_prog_file do any final mods (OSF/rose needs this for
1491 constructors/destructors in shared libraries. */
1492 scan_prog_file (output_file, PASS_SECOND);
1493 #endif
1495 maybe_unlink (c_file);
1496 maybe_unlink (o_file);
1498 #ifdef COLLECT_EXPORT_LIST
1499 maybe_unlink (export_file);
1500 #endif
1502 return 0;
1506 /* Wait for a process to finish, and exit if a nonzero status is found. */
1509 collect_wait (const char *prog, struct pex_obj *pex)
1511 int status;
1513 if (!pex_get_status (pex, 1, &status))
1514 fatal_perror ("can't get program status");
1515 pex_free (pex);
1517 if (status)
1519 if (WIFSIGNALED (status))
1521 int sig = WTERMSIG (status);
1522 error ("%s terminated with signal %d [%s]%s",
1523 prog, sig, strsignal(sig),
1524 WCOREDUMP(status) ? ", core dumped" : "");
1525 collect_exit (FATAL_EXIT_CODE);
1528 if (WIFEXITED (status))
1529 return WEXITSTATUS (status);
1531 return 0;
1534 static void
1535 do_wait (const char *prog, struct pex_obj *pex)
1537 int ret = collect_wait (prog, pex);
1538 if (ret != 0)
1540 error ("%s returned %d exit status", prog, ret);
1541 collect_exit (ret);
1544 if (response_file)
1546 unlink (response_file);
1547 response_file = NULL;
1552 /* Execute a program, and wait for the reply. */
1554 struct pex_obj *
1555 collect_execute (const char *prog, char **argv, const char *outname,
1556 const char *errname)
1558 struct pex_obj *pex;
1559 const char *errmsg;
1560 int err;
1561 char *response_arg = NULL;
1562 char *response_argv[3] ATTRIBUTE_UNUSED;
1564 if (HAVE_GNU_LD && at_file_supplied && argv[0] != NULL)
1566 /* If using @file arguments, create a temporary file and put the
1567 contents of argv into it. Then change argv to an array corresponding
1568 to a single argument @FILE, where FILE is the temporary filename. */
1570 char **current_argv = argv + 1;
1571 char *argv0 = argv[0];
1572 int status;
1573 FILE *f;
1575 /* Note: we assume argv contains at least one element; this is
1576 checked above. */
1578 response_file = make_temp_file ("");
1580 f = fopen (response_file, "w");
1582 if (f == NULL)
1583 fatal ("could not open response file %s", response_file);
1585 status = writeargv (current_argv, f);
1587 if (status)
1588 fatal ("could not write to response file %s", response_file);
1590 status = fclose (f);
1592 if (EOF == status)
1593 fatal ("could not close response file %s", response_file);
1595 response_arg = concat ("@", response_file, NULL);
1596 response_argv[0] = argv0;
1597 response_argv[1] = response_arg;
1598 response_argv[2] = NULL;
1600 argv = response_argv;
1603 if (vflag || debug)
1605 char **p_argv;
1606 const char *str;
1608 if (argv[0])
1609 fprintf (stderr, "%s", argv[0]);
1610 else
1611 notice ("[cannot find %s]", prog);
1613 for (p_argv = &argv[1]; (str = *p_argv) != (char *) 0; p_argv++)
1614 fprintf (stderr, " %s", str);
1616 fprintf (stderr, "\n");
1619 fflush (stdout);
1620 fflush (stderr);
1622 /* If we cannot find a program we need, complain error. Do this here
1623 since we might not end up needing something that we could not find. */
1625 if (argv[0] == 0)
1626 fatal ("cannot find '%s'", prog);
1628 pex = pex_init (0, "collect2", NULL);
1629 if (pex == NULL)
1630 fatal_perror ("pex_init failed");
1632 errmsg = pex_run (pex, PEX_LAST | PEX_SEARCH, argv[0], argv, outname,
1633 errname, &err);
1634 if (errmsg != NULL)
1636 if (err != 0)
1638 errno = err;
1639 fatal_perror (errmsg);
1641 else
1642 fatal (errmsg);
1645 if (response_arg)
1646 free (response_arg);
1648 return pex;
1651 static void
1652 fork_execute (const char *prog, char **argv)
1654 struct pex_obj *pex;
1656 pex = collect_execute (prog, argv, NULL, NULL);
1657 do_wait (prog, pex);
1660 /* Unlink a file unless we are debugging. */
1662 static void
1663 maybe_unlink (const char *file)
1665 if (!debug)
1666 unlink_if_ordinary (file);
1667 else
1668 notice ("[Leaving %s]\n", file);
1672 static long sequence_number = 0;
1674 /* Add a name to a linked list. */
1676 static void
1677 add_to_list (struct head *head_ptr, const char *name)
1679 struct id *newid
1680 = (struct id *) xcalloc (sizeof (struct id) + strlen (name), 1);
1681 struct id *p;
1682 strcpy (newid->name, name);
1684 if (head_ptr->first)
1685 head_ptr->last->next = newid;
1686 else
1687 head_ptr->first = newid;
1689 /* Check for duplicate symbols. */
1690 for (p = head_ptr->first;
1691 strcmp (name, p->name) != 0;
1692 p = p->next)
1694 if (p != newid)
1696 head_ptr->last->next = 0;
1697 free (newid);
1698 return;
1701 newid->sequence = ++sequence_number;
1702 head_ptr->last = newid;
1703 head_ptr->number++;
1706 /* Grab the init priority number from an init function name that
1707 looks like "_GLOBAL_.I.12345.foo". */
1709 static int
1710 extract_init_priority (const char *name)
1712 int pos = 0, pri;
1714 while (name[pos] == '_')
1715 ++pos;
1716 pos += 10; /* strlen ("GLOBAL__X_") */
1718 /* Extract init_p number from ctor/dtor name. */
1719 pri = atoi (name + pos);
1720 return pri ? pri : DEFAULT_INIT_PRIORITY;
1723 /* Insertion sort the ids from ctor/dtor list HEAD_PTR in descending order.
1724 ctors will be run from right to left, dtors from left to right. */
1726 static void
1727 sort_ids (struct head *head_ptr)
1729 /* id holds the current element to insert. id_next holds the next
1730 element to insert. id_ptr iterates through the already sorted elements
1731 looking for the place to insert id. */
1732 struct id *id, *id_next, **id_ptr;
1734 id = head_ptr->first;
1736 /* We don't have any sorted elements yet. */
1737 head_ptr->first = NULL;
1739 for (; id; id = id_next)
1741 id_next = id->next;
1742 id->sequence = extract_init_priority (id->name);
1744 for (id_ptr = &(head_ptr->first); ; id_ptr = &((*id_ptr)->next))
1745 if (*id_ptr == NULL
1746 /* If the sequence numbers are the same, we put the id from the
1747 file later on the command line later in the list. */
1748 || id->sequence > (*id_ptr)->sequence
1749 /* Hack: do lexical compare, too.
1750 || (id->sequence == (*id_ptr)->sequence
1751 && strcmp (id->name, (*id_ptr)->name) > 0) */
1754 id->next = *id_ptr;
1755 *id_ptr = id;
1756 break;
1760 /* Now set the sequence numbers properly so write_c_file works. */
1761 for (id = head_ptr->first; id; id = id->next)
1762 id->sequence = ++sequence_number;
1765 /* Write: `prefix', the names on list LIST, `suffix'. */
1767 static void
1768 write_list (FILE *stream, const char *prefix, struct id *list)
1770 while (list)
1772 fprintf (stream, "%sx%d,\n", prefix, list->sequence);
1773 list = list->next;
1777 #if LINK_ELIMINATE_DUPLICATE_LDIRECTORIES
1778 /* Given a STRING, return nonzero if it occurs in the list in range
1779 [ARGS_BEGIN,ARGS_END). */
1781 static int
1782 is_in_args (const char *string, const char **args_begin,
1783 const char **args_end)
1785 const char **args_pointer;
1786 for (args_pointer = args_begin; args_pointer != args_end; ++args_pointer)
1787 if (strcmp (string, *args_pointer) == 0)
1788 return 1;
1789 return 0;
1791 #endif /* LINK_ELIMINATE_DUPLICATE_LDIRECTORIES */
1793 #ifdef COLLECT_EXPORT_LIST
1794 /* This function is really used only on AIX, but may be useful. */
1795 #if 0
1796 static int
1797 is_in_list (const char *prefix, struct id *list)
1799 while (list)
1801 if (!strcmp (prefix, list->name)) return 1;
1802 list = list->next;
1804 return 0;
1806 #endif
1807 #endif /* COLLECT_EXPORT_LIST */
1809 /* Added for debugging purpose. */
1810 #ifdef COLLECT_EXPORT_LIST
1811 static void
1812 dump_list (FILE *stream, const char *prefix, struct id *list)
1814 while (list)
1816 fprintf (stream, "%s%s,\n", prefix, list->name);
1817 list = list->next;
1820 #endif
1822 #if 0
1823 static void
1824 dump_prefix_list (FILE *stream, const char *prefix, struct prefix_list *list)
1826 while (list)
1828 fprintf (stream, "%s%s,\n", prefix, list->prefix);
1829 list = list->next;
1832 #endif
1834 static void
1835 write_list_with_asm (FILE *stream, const char *prefix, struct id *list)
1837 while (list)
1839 fprintf (stream, "%sx%d __asm__ (\"%s\");\n",
1840 prefix, list->sequence, list->name);
1841 list = list->next;
1845 /* Write out the constructor and destructor tables statically (for a shared
1846 object), along with the functions to execute them. */
1848 static void
1849 write_c_file_stat (FILE *stream, const char *name ATTRIBUTE_UNUSED)
1851 const char *p, *q;
1852 char *prefix, *r;
1853 int frames = (frame_tables.number > 0);
1855 /* Figure out name of output_file, stripping off .so version. */
1856 p = strrchr (output_file, '/');
1857 if (p == 0)
1858 p = output_file;
1859 else
1860 p++;
1861 q = p;
1862 while (q)
1864 q = strchr (q,'.');
1865 if (q == 0)
1867 q = p + strlen (p);
1868 break;
1870 else
1872 if (strncmp (q, SHLIB_SUFFIX, strlen (SHLIB_SUFFIX)) == 0)
1874 q += strlen (SHLIB_SUFFIX);
1875 break;
1877 else
1878 q++;
1881 /* q points to null at end of the string (or . of the .so version) */
1882 prefix = XNEWVEC (char, q - p + 1);
1883 strncpy (prefix, p, q - p);
1884 prefix[q - p] = 0;
1885 for (r = prefix; *r; r++)
1886 if (!ISALNUM ((unsigned char)*r))
1887 *r = '_';
1888 if (debug)
1889 notice ("\nwrite_c_file - output name is %s, prefix is %s\n",
1890 output_file, prefix);
1892 initname = concat ("_GLOBAL__FI_", prefix, NULL);
1893 fininame = concat ("_GLOBAL__FD_", prefix, NULL);
1895 free (prefix);
1897 /* Write the tables as C code. */
1899 fprintf (stream, "static int count;\n");
1900 fprintf (stream, "typedef void entry_pt();\n");
1901 write_list_with_asm (stream, "extern entry_pt ", constructors.first);
1903 if (frames)
1905 write_list_with_asm (stream, "extern void *", frame_tables.first);
1907 fprintf (stream, "\tstatic void *frame_table[] = {\n");
1908 write_list (stream, "\t\t&", frame_tables.first);
1909 fprintf (stream, "\t0\n};\n");
1911 /* This must match what's in frame.h. */
1912 fprintf (stream, "struct object {\n");
1913 fprintf (stream, " void *pc_begin;\n");
1914 fprintf (stream, " void *pc_end;\n");
1915 fprintf (stream, " void *fde_begin;\n");
1916 fprintf (stream, " void *fde_array;\n");
1917 fprintf (stream, " __SIZE_TYPE__ count;\n");
1918 fprintf (stream, " struct object *next;\n");
1919 fprintf (stream, "};\n");
1921 fprintf (stream, "extern void __register_frame_info_table (void *, struct object *);\n");
1922 fprintf (stream, "extern void *__deregister_frame_info (void *);\n");
1924 fprintf (stream, "static void reg_frame () {\n");
1925 fprintf (stream, "\tstatic struct object ob;\n");
1926 fprintf (stream, "\t__register_frame_info_table (frame_table, &ob);\n");
1927 fprintf (stream, "\t}\n");
1929 fprintf (stream, "static void dereg_frame () {\n");
1930 fprintf (stream, "\t__deregister_frame_info (frame_table);\n");
1931 fprintf (stream, "\t}\n");
1934 fprintf (stream, "void %s() {\n", initname);
1935 if (constructors.number > 0 || frames)
1937 fprintf (stream, "\tstatic entry_pt *ctors[] = {\n");
1938 write_list (stream, "\t\t", constructors.first);
1939 if (frames)
1940 fprintf (stream, "\treg_frame,\n");
1941 fprintf (stream, "\t};\n");
1942 fprintf (stream, "\tentry_pt **p;\n");
1943 fprintf (stream, "\tif (count++ != 0) return;\n");
1944 fprintf (stream, "\tp = ctors + %d;\n", constructors.number + frames);
1945 fprintf (stream, "\twhile (p > ctors) (*--p)();\n");
1947 else
1948 fprintf (stream, "\t++count;\n");
1949 fprintf (stream, "}\n");
1950 write_list_with_asm (stream, "extern entry_pt ", destructors.first);
1951 fprintf (stream, "void %s() {\n", fininame);
1952 if (destructors.number > 0 || frames)
1954 fprintf (stream, "\tstatic entry_pt *dtors[] = {\n");
1955 write_list (stream, "\t\t", destructors.first);
1956 if (frames)
1957 fprintf (stream, "\tdereg_frame,\n");
1958 fprintf (stream, "\t};\n");
1959 fprintf (stream, "\tentry_pt **p;\n");
1960 fprintf (stream, "\tif (--count != 0) return;\n");
1961 fprintf (stream, "\tp = dtors;\n");
1962 fprintf (stream, "\twhile (p < dtors + %d) (*p++)();\n",
1963 destructors.number + frames);
1965 fprintf (stream, "}\n");
1967 if (shared_obj)
1969 COLLECT_SHARED_INIT_FUNC(stream, initname);
1970 COLLECT_SHARED_FINI_FUNC(stream, fininame);
1974 /* Write the constructor/destructor tables. */
1976 #ifndef LD_INIT_SWITCH
1977 static void
1978 write_c_file_glob (FILE *stream, const char *name ATTRIBUTE_UNUSED)
1980 /* Write the tables as C code. */
1982 int frames = (frame_tables.number > 0);
1984 fprintf (stream, "typedef void entry_pt();\n\n");
1986 write_list_with_asm (stream, "extern entry_pt ", constructors.first);
1988 if (frames)
1990 write_list_with_asm (stream, "extern void *", frame_tables.first);
1992 fprintf (stream, "\tstatic void *frame_table[] = {\n");
1993 write_list (stream, "\t\t&", frame_tables.first);
1994 fprintf (stream, "\t0\n};\n");
1996 /* This must match what's in frame.h. */
1997 fprintf (stream, "struct object {\n");
1998 fprintf (stream, " void *pc_begin;\n");
1999 fprintf (stream, " void *pc_end;\n");
2000 fprintf (stream, " void *fde_begin;\n");
2001 fprintf (stream, " void *fde_array;\n");
2002 fprintf (stream, " __SIZE_TYPE__ count;\n");
2003 fprintf (stream, " struct object *next;\n");
2004 fprintf (stream, "};\n");
2006 fprintf (stream, "extern void __register_frame_info_table (void *, struct object *);\n");
2007 fprintf (stream, "extern void *__deregister_frame_info (void *);\n");
2009 fprintf (stream, "static void reg_frame () {\n");
2010 fprintf (stream, "\tstatic struct object ob;\n");
2011 fprintf (stream, "\t__register_frame_info_table (frame_table, &ob);\n");
2012 fprintf (stream, "\t}\n");
2014 fprintf (stream, "static void dereg_frame () {\n");
2015 fprintf (stream, "\t__deregister_frame_info (frame_table);\n");
2016 fprintf (stream, "\t}\n");
2019 fprintf (stream, "\nentry_pt * __CTOR_LIST__[] = {\n");
2020 fprintf (stream, "\t(entry_pt *) %d,\n", constructors.number + frames);
2021 write_list (stream, "\t", constructors.first);
2022 if (frames)
2023 fprintf (stream, "\treg_frame,\n");
2024 fprintf (stream, "\t0\n};\n\n");
2026 write_list_with_asm (stream, "extern entry_pt ", destructors.first);
2028 fprintf (stream, "\nentry_pt * __DTOR_LIST__[] = {\n");
2029 fprintf (stream, "\t(entry_pt *) %d,\n", destructors.number + frames);
2030 write_list (stream, "\t", destructors.first);
2031 if (frames)
2032 fprintf (stream, "\tdereg_frame,\n");
2033 fprintf (stream, "\t0\n};\n\n");
2035 fprintf (stream, "extern entry_pt %s;\n", NAME__MAIN);
2036 fprintf (stream, "entry_pt *__main_reference = %s;\n\n", NAME__MAIN);
2038 #endif /* ! LD_INIT_SWITCH */
2040 static void
2041 write_c_file (FILE *stream, const char *name)
2043 #ifndef LD_INIT_SWITCH
2044 if (! shared_obj)
2045 write_c_file_glob (stream, name);
2046 else
2047 #endif
2048 write_c_file_stat (stream, name);
2051 #ifdef COLLECT_EXPORT_LIST
2052 static void
2053 write_aix_file (FILE *stream, struct id *list)
2055 for (; list; list = list->next)
2057 fputs (list->name, stream);
2058 putc ('\n', stream);
2061 #endif
2063 #ifdef OBJECT_FORMAT_NONE
2065 /* Generic version to scan the name list of the loaded program for
2066 the symbols g++ uses for static constructors and destructors.
2068 The constructor table begins at __CTOR_LIST__ and contains a count
2069 of the number of pointers (or -1 if the constructors are built in a
2070 separate section by the linker), followed by the pointers to the
2071 constructor functions, terminated with a null pointer. The
2072 destructor table has the same format, and begins at __DTOR_LIST__. */
2074 static void
2075 scan_prog_file (const char *prog_name, enum pass which_pass)
2077 void (*int_handler) (int);
2078 #ifdef SIGQUIT
2079 void (*quit_handler) (int);
2080 #endif
2081 char *real_nm_argv[4];
2082 const char **nm_argv = (const char **) real_nm_argv;
2083 int argc = 0;
2084 struct pex_obj *pex;
2085 const char *errmsg;
2086 int err;
2087 char *p, buf[1024];
2088 FILE *inf;
2090 if (which_pass == PASS_SECOND)
2091 return;
2093 /* If we do not have an `nm', complain. */
2094 if (nm_file_name == 0)
2095 fatal ("cannot find 'nm'");
2097 nm_argv[argc++] = nm_file_name;
2098 if (NM_FLAGS[0] != '\0')
2099 nm_argv[argc++] = NM_FLAGS;
2101 nm_argv[argc++] = prog_name;
2102 nm_argv[argc++] = (char *) 0;
2104 /* Trace if needed. */
2105 if (vflag)
2107 const char **p_argv;
2108 const char *str;
2110 for (p_argv = &nm_argv[0]; (str = *p_argv) != (char *) 0; p_argv++)
2111 fprintf (stderr, " %s", str);
2113 fprintf (stderr, "\n");
2116 fflush (stdout);
2117 fflush (stderr);
2119 pex = pex_init (PEX_USE_PIPES, "collect2", NULL);
2120 if (pex == NULL)
2121 fatal_perror ("pex_init failed");
2123 errmsg = pex_run (pex, 0, nm_file_name, real_nm_argv, NULL, NULL, &err);
2124 if (errmsg != NULL)
2126 if (err != 0)
2128 errno = err;
2129 fatal_perror (errmsg);
2131 else
2132 fatal (errmsg);
2135 int_handler = (void (*) (int)) signal (SIGINT, SIG_IGN);
2136 #ifdef SIGQUIT
2137 quit_handler = (void (*) (int)) signal (SIGQUIT, SIG_IGN);
2138 #endif
2140 inf = pex_read_output (pex, 0);
2141 if (inf == NULL)
2142 fatal_perror ("can't open nm output");
2144 if (debug)
2145 fprintf (stderr, "\nnm output with constructors/destructors.\n");
2147 /* Read each line of nm output. */
2148 while (fgets (buf, sizeof buf, inf) != (char *) 0)
2150 int ch, ch2;
2151 char *name, *end;
2153 /* If it contains a constructor or destructor name, add the name
2154 to the appropriate list. */
2156 for (p = buf; (ch = *p) != '\0' && ch != '\n' && ch != '_'; p++)
2157 if (ch == ' ' && p[1] == 'U' && p[2] == ' ')
2158 break;
2160 if (ch != '_')
2161 continue;
2163 name = p;
2164 /* Find the end of the symbol name.
2165 Do not include `|', because Encore nm can tack that on the end. */
2166 for (end = p; (ch2 = *end) != '\0' && !ISSPACE (ch2) && ch2 != '|';
2167 end++)
2168 continue;
2171 *end = '\0';
2172 switch (is_ctor_dtor (name))
2174 case 1:
2175 if (which_pass != PASS_LIB)
2176 add_to_list (&constructors, name);
2177 break;
2179 case 2:
2180 if (which_pass != PASS_LIB)
2181 add_to_list (&destructors, name);
2182 break;
2184 case 3:
2185 if (which_pass != PASS_LIB)
2186 fatal ("init function found in object %s", prog_name);
2187 #ifndef LD_INIT_SWITCH
2188 add_to_list (&constructors, name);
2189 #endif
2190 break;
2192 case 4:
2193 if (which_pass != PASS_LIB)
2194 fatal ("fini function found in object %s", prog_name);
2195 #ifndef LD_FINI_SWITCH
2196 add_to_list (&destructors, name);
2197 #endif
2198 break;
2200 case 5:
2201 if (which_pass != PASS_LIB)
2202 add_to_list (&frame_tables, name);
2203 break;
2205 default: /* not a constructor or destructor */
2206 continue;
2209 if (debug)
2210 fprintf (stderr, "\t%s\n", buf);
2213 if (debug)
2214 fprintf (stderr, "\n");
2216 do_wait (nm_file_name, pex);
2218 signal (SIGINT, int_handler);
2219 #ifdef SIGQUIT
2220 signal (SIGQUIT, quit_handler);
2221 #endif
2224 #ifdef LDD_SUFFIX
2226 /* Use the List Dynamic Dependencies program to find shared libraries that
2227 the output file depends upon and their initialization/finalization
2228 routines, if any. */
2230 static void
2231 scan_libraries (const char *prog_name)
2233 static struct head libraries; /* list of shared libraries found */
2234 struct id *list;
2235 void (*int_handler) (int);
2236 #ifdef SIGQUIT
2237 void (*quit_handler) (int);
2238 #endif
2239 char *real_ldd_argv[4];
2240 const char **ldd_argv = (const char **) real_ldd_argv;
2241 int argc = 0;
2242 struct pex_obj *pex;
2243 const char *errmsg;
2244 int err;
2245 char buf[1024];
2246 FILE *inf;
2248 /* If we do not have an `ldd', complain. */
2249 if (ldd_file_name == 0)
2251 error ("cannot find 'ldd'");
2252 return;
2255 ldd_argv[argc++] = ldd_file_name;
2256 ldd_argv[argc++] = prog_name;
2257 ldd_argv[argc++] = (char *) 0;
2259 /* Trace if needed. */
2260 if (vflag)
2262 const char **p_argv;
2263 const char *str;
2265 for (p_argv = &ldd_argv[0]; (str = *p_argv) != (char *) 0; p_argv++)
2266 fprintf (stderr, " %s", str);
2268 fprintf (stderr, "\n");
2271 fflush (stdout);
2272 fflush (stderr);
2274 pex = pex_init (PEX_USE_PIPES, "collect2", NULL);
2275 if (pex == NULL)
2276 fatal_perror ("pex_init failed");
2278 errmsg = pex_run (pex, 0, ldd_file_name, real_ldd_argv, NULL, NULL, &err);
2279 if (errmsg != NULL)
2281 if (err != 0)
2283 errno = err;
2284 fatal_perror (errmsg);
2286 else
2287 fatal (errmsg);
2290 int_handler = (void (*) (int)) signal (SIGINT, SIG_IGN);
2291 #ifdef SIGQUIT
2292 quit_handler = (void (*) (int)) signal (SIGQUIT, SIG_IGN);
2293 #endif
2295 inf = pex_read_output (pex, 0);
2296 if (inf == NULL)
2297 fatal_perror ("can't open ldd output");
2299 if (debug)
2300 notice ("\nldd output with constructors/destructors.\n");
2302 /* Read each line of ldd output. */
2303 while (fgets (buf, sizeof buf, inf) != (char *) 0)
2305 int ch2;
2306 char *name, *end, *p = buf;
2308 /* Extract names of libraries and add to list. */
2309 PARSE_LDD_OUTPUT (p);
2310 if (p == 0)
2311 continue;
2313 name = p;
2314 if (strncmp (name, "not found", sizeof ("not found") - 1) == 0)
2315 fatal ("dynamic dependency %s not found", buf);
2317 /* Find the end of the symbol name. */
2318 for (end = p;
2319 (ch2 = *end) != '\0' && ch2 != '\n' && !ISSPACE (ch2) && ch2 != '|';
2320 end++)
2321 continue;
2322 *end = '\0';
2324 if (access (name, R_OK) == 0)
2325 add_to_list (&libraries, name);
2326 else
2327 fatal ("unable to open dynamic dependency '%s'", buf);
2329 if (debug)
2330 fprintf (stderr, "\t%s\n", buf);
2332 if (debug)
2333 fprintf (stderr, "\n");
2335 do_wait (ldd_file_name, pex);
2337 signal (SIGINT, int_handler);
2338 #ifdef SIGQUIT
2339 signal (SIGQUIT, quit_handler);
2340 #endif
2342 /* Now iterate through the library list adding their symbols to
2343 the list. */
2344 for (list = libraries.first; list; list = list->next)
2345 scan_prog_file (list->name, PASS_LIB);
2348 #endif /* LDD_SUFFIX */
2350 #endif /* OBJECT_FORMAT_NONE */
2354 * COFF specific stuff.
2357 #ifdef OBJECT_FORMAT_COFF
2359 #if defined (EXTENDED_COFF)
2361 # define GCC_SYMBOLS(X) (SYMHEADER(X).isymMax + SYMHEADER(X).iextMax)
2362 # define GCC_SYMENT SYMR
2363 # define GCC_OK_SYMBOL(X) ((X).st == stProc || (X).st == stGlobal)
2364 # define GCC_SYMINC(X) (1)
2365 # define GCC_SYMZERO(X) (SYMHEADER(X).isymMax)
2366 # define GCC_CHECK_HDR(X) (PSYMTAB(X) != 0)
2368 #else
2370 # define GCC_SYMBOLS(X) (HEADER(ldptr).f_nsyms)
2371 # define GCC_SYMENT SYMENT
2372 # if defined (C_WEAKEXT)
2373 # define GCC_OK_SYMBOL(X) \
2374 (((X).n_sclass == C_EXT || (X).n_sclass == C_WEAKEXT) && \
2375 ((X).n_scnum > N_UNDEF) && \
2376 (aix64_flag \
2377 || (((X).n_type & N_TMASK) == (DT_NON << N_BTSHFT) \
2378 || ((X).n_type & N_TMASK) == (DT_FCN << N_BTSHFT))))
2379 # define GCC_UNDEF_SYMBOL(X) \
2380 (((X).n_sclass == C_EXT || (X).n_sclass == C_WEAKEXT) && \
2381 ((X).n_scnum == N_UNDEF))
2382 # else
2383 # define GCC_OK_SYMBOL(X) \
2384 (((X).n_sclass == C_EXT) && \
2385 ((X).n_scnum > N_UNDEF) && \
2386 (aix64_flag \
2387 || (((X).n_type & N_TMASK) == (DT_NON << N_BTSHFT) \
2388 || ((X).n_type & N_TMASK) == (DT_FCN << N_BTSHFT))))
2389 # define GCC_UNDEF_SYMBOL(X) \
2390 (((X).n_sclass == C_EXT) && ((X).n_scnum == N_UNDEF))
2391 # endif
2392 # define GCC_SYMINC(X) ((X).n_numaux+1)
2393 # define GCC_SYMZERO(X) 0
2395 /* 0757 = U803XTOCMAGIC (AIX 4.3) and 0767 = U64_TOCMAGIC (AIX V5) */
2396 #ifdef _AIX51
2397 # define GCC_CHECK_HDR(X) \
2398 ((HEADER (X).f_magic == U802TOCMAGIC && ! aix64_flag) \
2399 || (HEADER (X).f_magic == 0767 && aix64_flag))
2400 #else
2401 # define GCC_CHECK_HDR(X) \
2402 ((HEADER (X).f_magic == U802TOCMAGIC && ! aix64_flag) \
2403 || (HEADER (X).f_magic == 0757 && aix64_flag))
2404 #endif
2406 #endif
2408 #ifdef COLLECT_EXPORT_LIST
2409 /* Array of standard AIX libraries which should not
2410 be scanned for ctors/dtors. */
2411 static const char *const aix_std_libs[] = {
2412 "/unix",
2413 "/lib/libc.a",
2414 "/lib/libm.a",
2415 "/lib/libc_r.a",
2416 "/lib/libm_r.a",
2417 "/usr/lib/libc.a",
2418 "/usr/lib/libm.a",
2419 "/usr/lib/libc_r.a",
2420 "/usr/lib/libm_r.a",
2421 "/usr/lib/threads/libc.a",
2422 "/usr/ccs/lib/libc.a",
2423 "/usr/ccs/lib/libm.a",
2424 "/usr/ccs/lib/libc_r.a",
2425 "/usr/ccs/lib/libm_r.a",
2426 NULL
2429 /* This function checks the filename and returns 1
2430 if this name matches the location of a standard AIX library. */
2431 static int ignore_library (const char *);
2432 static int
2433 ignore_library (const char *name)
2435 const char *const *p = &aix_std_libs[0];
2436 while (*p++ != NULL)
2437 if (! strcmp (name, *p)) return 1;
2438 return 0;
2440 #endif /* COLLECT_EXPORT_LIST */
2442 #if defined (HAVE_DECL_LDGETNAME) && !HAVE_DECL_LDGETNAME
2443 extern char *ldgetname (LDFILE *, GCC_SYMENT *);
2444 #endif
2446 /* COFF version to scan the name list of the loaded program for
2447 the symbols g++ uses for static constructors and destructors.
2449 The constructor table begins at __CTOR_LIST__ and contains a count
2450 of the number of pointers (or -1 if the constructors are built in a
2451 separate section by the linker), followed by the pointers to the
2452 constructor functions, terminated with a null pointer. The
2453 destructor table has the same format, and begins at __DTOR_LIST__. */
2455 static void
2456 scan_prog_file (const char *prog_name, enum pass which_pass)
2458 LDFILE *ldptr = NULL;
2459 int sym_index, sym_count;
2460 int is_shared = 0;
2462 if (which_pass != PASS_FIRST && which_pass != PASS_OBJ)
2463 return;
2465 #ifdef COLLECT_EXPORT_LIST
2466 /* We do not need scanning for some standard C libraries. */
2467 if (which_pass == PASS_FIRST && ignore_library (prog_name))
2468 return;
2470 /* On AIX we have a loop, because there is not much difference
2471 between an object and an archive. This trick allows us to
2472 eliminate scan_libraries() function. */
2475 #endif
2476 /* Some platforms (e.g. OSF4) declare ldopen as taking a
2477 non-const char * filename parameter, even though it will not
2478 modify that string. So we must cast away const-ness here,
2479 using CONST_CAST to prevent complaints from -Wcast-qual. */
2480 if ((ldptr = ldopen (CONST_CAST (char *, prog_name), ldptr)) != NULL)
2482 if (! MY_ISCOFF (HEADER (ldptr).f_magic))
2483 fatal ("%s: not a COFF file", prog_name);
2485 if (GCC_CHECK_HDR (ldptr))
2487 sym_count = GCC_SYMBOLS (ldptr);
2488 sym_index = GCC_SYMZERO (ldptr);
2490 #ifdef COLLECT_EXPORT_LIST
2491 /* Is current archive member a shared object? */
2492 is_shared = HEADER (ldptr).f_flags & F_SHROBJ;
2493 #endif
2495 while (sym_index < sym_count)
2497 GCC_SYMENT symbol;
2499 if (ldtbread (ldptr, sym_index, &symbol) <= 0)
2500 break;
2501 sym_index += GCC_SYMINC (symbol);
2503 if (GCC_OK_SYMBOL (symbol))
2505 char *name;
2507 if ((name = ldgetname (ldptr, &symbol)) == NULL)
2508 continue; /* Should never happen. */
2510 #ifdef XCOFF_DEBUGGING_INFO
2511 /* All AIX function names have a duplicate entry
2512 beginning with a dot. */
2513 if (*name == '.')
2514 ++name;
2515 #endif
2517 switch (is_ctor_dtor (name))
2519 case 1:
2520 if (! is_shared)
2521 add_to_list (&constructors, name);
2522 #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
2523 if (which_pass == PASS_OBJ)
2524 add_to_list (&exports, name);
2525 #endif
2526 break;
2528 case 2:
2529 if (! is_shared)
2530 add_to_list (&destructors, name);
2531 #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
2532 if (which_pass == PASS_OBJ)
2533 add_to_list (&exports, name);
2534 #endif
2535 break;
2537 #ifdef COLLECT_EXPORT_LIST
2538 case 3:
2539 #ifndef LD_INIT_SWITCH
2540 if (is_shared)
2541 add_to_list (&constructors, name);
2542 #endif
2543 break;
2545 case 4:
2546 #ifndef LD_INIT_SWITCH
2547 if (is_shared)
2548 add_to_list (&destructors, name);
2549 #endif
2550 break;
2551 #endif
2553 case 5:
2554 if (! is_shared)
2555 add_to_list (&frame_tables, name);
2556 #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
2557 if (which_pass == PASS_OBJ)
2558 add_to_list (&exports, name);
2559 #endif
2560 break;
2562 default: /* not a constructor or destructor */
2563 #ifdef COLLECT_EXPORT_LIST
2564 /* Explicitly export all global symbols when
2565 building a shared object on AIX, but do not
2566 re-export symbols from another shared object
2567 and do not export symbols if the user
2568 provides an explicit export list. */
2569 if (shared_obj && !is_shared
2570 && which_pass == PASS_OBJ && !export_flag)
2571 add_to_list (&exports, name);
2572 #endif
2573 continue;
2576 if (debug)
2577 #if !defined(EXTENDED_COFF)
2578 fprintf (stderr, "\tsec=%d class=%d type=%s%o %s\n",
2579 symbol.n_scnum, symbol.n_sclass,
2580 (symbol.n_type ? "0" : ""), symbol.n_type,
2581 name);
2582 #else
2583 fprintf (stderr,
2584 "\tiss = %5d, value = %5ld, index = %5d, name = %s\n",
2585 symbol.iss, (long) symbol.value, symbol.index, name);
2586 #endif
2590 #ifdef COLLECT_EXPORT_LIST
2591 else
2593 /* If archive contains both 32-bit and 64-bit objects,
2594 we want to skip objects in other mode so mismatch normal. */
2595 if (debug)
2596 fprintf (stderr, "%s : magic=%o aix64=%d mismatch\n",
2597 prog_name, HEADER (ldptr).f_magic, aix64_flag);
2599 #endif
2601 else
2603 fatal ("%s: cannot open as COFF file", prog_name);
2605 #ifdef COLLECT_EXPORT_LIST
2606 /* On AIX loop continues while there are more members in archive. */
2608 while (ldclose (ldptr) == FAILURE);
2609 #else
2610 /* Otherwise we simply close ldptr. */
2611 (void) ldclose(ldptr);
2612 #endif
2614 #endif /* OBJECT_FORMAT_COFF */
2616 #ifdef COLLECT_EXPORT_LIST
2617 /* Given a library name without "lib" prefix, this function
2618 returns a full library name including a path. */
2619 static char *
2620 resolve_lib_name (const char *name)
2622 char *lib_buf;
2623 int i, j, l = 0;
2624 /* Library extensions for AIX dynamic linking. */
2625 const char * const libexts[2] = {"a", "so"};
2627 for (i = 0; libpaths[i]; i++)
2628 if (libpaths[i]->max_len > l)
2629 l = libpaths[i]->max_len;
2631 lib_buf = XNEWVEC (char, l + strlen(name) + 10);
2633 for (i = 0; libpaths[i]; i++)
2635 struct prefix_list *list = libpaths[i]->plist;
2636 for (; list; list = list->next)
2638 /* The following lines are needed because path_prefix list
2639 may contain directories both with trailing '/' and
2640 without it. */
2641 const char *p = "";
2642 if (list->prefix[strlen(list->prefix)-1] != '/')
2643 p = "/";
2644 for (j = 0; j < 2; j++)
2646 sprintf (lib_buf, "%s%slib%s.%s",
2647 list->prefix, p, name,
2648 libexts[(j + aixrtl_flag) % 2]);
2649 if (debug) fprintf (stderr, "searching for: %s\n", lib_buf);
2650 if (file_exists (lib_buf))
2652 if (debug) fprintf (stderr, "found: %s\n", lib_buf);
2653 return (lib_buf);
2658 if (debug)
2659 fprintf (stderr, "not found\n");
2660 else
2661 fatal ("library lib%s not found", name);
2662 return (NULL);
2664 #endif /* COLLECT_EXPORT_LIST */