arm.md (movsi): Use can_create_pseudo_p instead of no_new_pseudos.
[official-gcc.git] / gcc / collect2.c
blob1ef3bbf2c126bd9df4089ebed9be1854acc5a91c
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 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_DIRECTORY_STRUCTURE
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 #ifdef USE_COLLECT2
134 int do_collecting = 1;
135 #else
136 int do_collecting = 0;
137 #endif
139 /* Nonzero if we should suppress the automatic demangling of identifiers
140 in linker error messages. Set from COLLECT_NO_DEMANGLE. */
141 int no_demangle;
143 /* Linked lists of constructor and destructor names. */
145 struct id
147 struct id *next;
148 int sequence;
149 char name[1];
152 struct head
154 struct id *first;
155 struct id *last;
156 int number;
159 /* Enumeration giving which pass this is for scanning the program file. */
161 enum pass {
162 PASS_FIRST, /* without constructors */
163 PASS_OBJ, /* individual objects */
164 PASS_LIB, /* looking for shared libraries */
165 PASS_SECOND /* with constructors linked in */
168 int vflag; /* true if -v */
169 static int rflag; /* true if -r */
170 static int strip_flag; /* true if -s */
171 static const char *demangle_flag;
172 #ifdef COLLECT_EXPORT_LIST
173 static int export_flag; /* true if -bE */
174 static int aix64_flag; /* true if -b64 */
175 static int aixrtl_flag; /* true if -brtl */
176 #endif
178 int debug; /* true if -debug */
180 static int shared_obj; /* true if -shared */
182 static const char *c_file; /* <xxx>.c for constructor/destructor list. */
183 static const char *o_file; /* <xxx>.o for constructor/destructor list. */
184 #ifdef COLLECT_EXPORT_LIST
185 static const char *export_file; /* <xxx>.x for AIX export list. */
186 #endif
187 const char *ldout; /* File for ld stdout. */
188 const char *lderrout; /* File for ld stderr. */
189 static const char *output_file; /* Output file for ld. */
190 static const char *nm_file_name; /* pathname of nm */
191 #ifdef LDD_SUFFIX
192 static const char *ldd_file_name; /* pathname of ldd (or equivalent) */
193 #endif
194 static const char *strip_file_name; /* pathname of strip */
195 const char *c_file_name; /* pathname of gcc */
196 static char *initname, *fininame; /* names of init and fini funcs */
198 static struct head constructors; /* list of constructors found */
199 static struct head destructors; /* list of destructors found */
200 #ifdef COLLECT_EXPORT_LIST
201 static struct head exports; /* list of exported symbols */
202 #endif
203 static struct head frame_tables; /* list of frame unwind info tables */
205 static bool at_file_supplied; /* Whether to use @file arguments */
206 static char *response_file; /* Name of any current response file */
208 struct obstack temporary_obstack;
209 char * temporary_firstobj;
211 /* Structure to hold all the directories in which to search for files to
212 execute. */
214 struct prefix_list
216 const char *prefix; /* String to prepend to the path. */
217 struct prefix_list *next; /* Next in linked list. */
220 struct path_prefix
222 struct prefix_list *plist; /* List of prefixes to try */
223 int max_len; /* Max length of a prefix in PLIST */
224 const char *name; /* Name of this list (used in config stuff) */
227 #ifdef COLLECT_EXPORT_LIST
228 /* Lists to keep libraries to be scanned for global constructors/destructors. */
229 static struct head libs; /* list of libraries */
230 static struct path_prefix cmdline_lib_dirs; /* directories specified with -L */
231 static struct path_prefix libpath_lib_dirs; /* directories in LIBPATH */
232 static struct path_prefix *libpaths[3] = {&cmdline_lib_dirs,
233 &libpath_lib_dirs, NULL};
234 #endif
236 static void handler (int);
237 static int is_ctor_dtor (const char *);
238 static char *find_a_file (struct path_prefix *, const char *);
239 static void add_prefix (struct path_prefix *, const char *);
240 static void prefix_from_env (const char *, struct path_prefix *);
241 static void prefix_from_string (const char *, struct path_prefix *);
242 static void do_wait (const char *, struct pex_obj *);
243 static void fork_execute (const char *, char **);
244 static void maybe_unlink (const char *);
245 static void add_to_list (struct head *, const char *);
246 static int extract_init_priority (const char *);
247 static void sort_ids (struct head *);
248 static void write_list (FILE *, const char *, struct id *);
249 #ifdef COLLECT_EXPORT_LIST
250 static void dump_list (FILE *, const char *, struct id *);
251 #endif
252 #if 0
253 static void dump_prefix_list (FILE *, const char *, struct prefix_list *);
254 #endif
255 static void write_list_with_asm (FILE *, const char *, struct id *);
256 static void write_c_file (FILE *, const char *);
257 static void write_c_file_stat (FILE *, const char *);
258 #ifndef LD_INIT_SWITCH
259 static void write_c_file_glob (FILE *, const char *);
260 #endif
261 static void scan_prog_file (const char *, enum pass);
262 #ifdef SCAN_LIBRARIES
263 static void scan_libraries (const char *);
264 #endif
265 #if LINK_ELIMINATE_DUPLICATE_LDIRECTORIES
266 static int is_in_args (const char *, const char **, const char **);
267 #endif
268 #ifdef COLLECT_EXPORT_LIST
269 #if 0
270 static int is_in_list (const char *, struct id *);
271 #endif
272 static void write_aix_file (FILE *, struct id *);
273 static char *resolve_lib_name (const char *);
274 #endif
275 static char *extract_string (const char **);
277 /* Delete tempfiles and exit function. */
279 void
280 collect_exit (int status)
282 if (c_file != 0 && c_file[0])
283 maybe_unlink (c_file);
285 if (o_file != 0 && o_file[0])
286 maybe_unlink (o_file);
288 #ifdef COLLECT_EXPORT_LIST
289 if (export_file != 0 && export_file[0])
290 maybe_unlink (export_file);
291 #endif
293 if (ldout != 0 && ldout[0])
295 dump_file (ldout, stdout);
296 maybe_unlink (ldout);
299 if (lderrout != 0 && lderrout[0])
301 dump_file (lderrout, stderr);
302 maybe_unlink (lderrout);
305 if (status != 0 && output_file != 0 && output_file[0])
306 maybe_unlink (output_file);
308 if (response_file)
309 maybe_unlink (response_file);
311 exit (status);
315 /* Notify user of a non-error. */
316 void
317 notice (const char *cmsgid, ...)
319 va_list ap;
321 va_start (ap, cmsgid);
322 vfprintf (stderr, _(cmsgid), ap);
323 va_end (ap);
326 /* Die when sys call fails. */
328 void
329 fatal_perror (const char * cmsgid, ...)
331 int e = errno;
332 va_list ap;
334 va_start (ap, cmsgid);
335 fprintf (stderr, "collect2: ");
336 vfprintf (stderr, _(cmsgid), ap);
337 fprintf (stderr, ": %s\n", xstrerror (e));
338 va_end (ap);
340 collect_exit (FATAL_EXIT_CODE);
343 /* Just die. */
345 void
346 fatal (const char * cmsgid, ...)
348 va_list ap;
350 va_start (ap, cmsgid);
351 fprintf (stderr, "collect2: ");
352 vfprintf (stderr, _(cmsgid), ap);
353 fprintf (stderr, "\n");
354 va_end (ap);
356 collect_exit (FATAL_EXIT_CODE);
359 /* Write error message. */
361 void
362 error (const char * gmsgid, ...)
364 va_list ap;
366 va_start (ap, gmsgid);
367 fprintf (stderr, "collect2: ");
368 vfprintf (stderr, _(gmsgid), ap);
369 fprintf (stderr, "\n");
370 va_end(ap);
373 /* In case obstack is linked in, and abort is defined to fancy_abort,
374 provide a default entry. */
376 void
377 fancy_abort (const char *file, int line, const char *func)
379 fatal ("internal gcc abort in %s, at %s:%d", func, file, line);
382 static void
383 handler (int signo)
385 if (c_file != 0 && c_file[0])
386 maybe_unlink (c_file);
388 if (o_file != 0 && o_file[0])
389 maybe_unlink (o_file);
391 if (ldout != 0 && ldout[0])
392 maybe_unlink (ldout);
394 if (lderrout != 0 && lderrout[0])
395 maybe_unlink (lderrout);
397 #ifdef COLLECT_EXPORT_LIST
398 if (export_file != 0 && export_file[0])
399 maybe_unlink (export_file);
400 #endif
402 if (response_file)
403 maybe_unlink (response_file);
405 signal (signo, SIG_DFL);
406 raise (signo);
411 file_exists (const char *name)
413 return access (name, R_OK) == 0;
416 /* Parse a reasonable subset of shell quoting syntax. */
418 static char *
419 extract_string (const char **pp)
421 const char *p = *pp;
422 int backquote = 0;
423 int inside = 0;
425 for (;;)
427 char c = *p;
428 if (c == '\0')
429 break;
430 ++p;
431 if (backquote)
432 obstack_1grow (&temporary_obstack, c);
433 else if (! inside && c == ' ')
434 break;
435 else if (! inside && c == '\\')
436 backquote = 1;
437 else if (c == '\'')
438 inside = !inside;
439 else
440 obstack_1grow (&temporary_obstack, c);
443 obstack_1grow (&temporary_obstack, '\0');
444 *pp = p;
445 return XOBFINISH (&temporary_obstack, char *);
448 void
449 dump_file (const char *name, FILE *to)
451 FILE *stream = fopen (name, "r");
453 if (stream == 0)
454 return;
455 while (1)
457 int c;
458 while (c = getc (stream),
459 c != EOF && (ISIDNUM (c) || c == '$' || c == '.'))
460 obstack_1grow (&temporary_obstack, c);
461 if (obstack_object_size (&temporary_obstack) > 0)
463 const char *word, *p;
464 char *result;
465 obstack_1grow (&temporary_obstack, '\0');
466 word = XOBFINISH (&temporary_obstack, const char *);
468 if (*word == '.')
469 ++word, putc ('.', to);
470 p = word;
471 if (!strncmp (p, USER_LABEL_PREFIX, strlen (USER_LABEL_PREFIX)))
472 p += strlen (USER_LABEL_PREFIX);
474 #ifdef HAVE_LD_DEMANGLE
475 result = 0;
476 #else
477 if (no_demangle)
478 result = 0;
479 else
480 result = cplus_demangle (p, DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE);
481 #endif
483 if (result)
485 int diff;
486 fputs (result, to);
488 diff = strlen (word) - strlen (result);
489 while (diff > 0 && c == ' ')
490 --diff, putc (' ', to);
491 while (diff < 0 && c == ' ')
492 ++diff, c = getc (stream);
494 free (result);
496 else
497 fputs (word, to);
499 fflush (to);
500 obstack_free (&temporary_obstack, temporary_firstobj);
502 if (c == EOF)
503 break;
504 putc (c, to);
506 fclose (stream);
509 /* Decide whether the given symbol is: a constructor (1), a destructor
510 (2), a routine in a shared object that calls all the constructors
511 (3) or destructors (4), a DWARF exception-handling table (5), or
512 nothing special (0). */
514 static int
515 is_ctor_dtor (const char *s)
517 struct names { const char *const name; const int len; const int ret;
518 const int two_underscores; };
520 const struct names *p;
521 int ch;
522 const char *orig_s = s;
524 static const struct names special[] = {
525 #ifndef NO_DOLLAR_IN_LABEL
526 { "GLOBAL__I$", sizeof ("GLOBAL__I$")-1, 1, 0 },
527 { "GLOBAL__D$", sizeof ("GLOBAL__D$")-1, 2, 0 },
528 #else
529 #ifndef NO_DOT_IN_LABEL
530 { "GLOBAL__I.", sizeof ("GLOBAL__I.")-1, 1, 0 },
531 { "GLOBAL__D.", sizeof ("GLOBAL__D.")-1, 2, 0 },
532 #endif /* NO_DOT_IN_LABEL */
533 #endif /* NO_DOLLAR_IN_LABEL */
534 { "GLOBAL__I_", sizeof ("GLOBAL__I_")-1, 1, 0 },
535 { "GLOBAL__D_", sizeof ("GLOBAL__D_")-1, 2, 0 },
536 { "GLOBAL__F_", sizeof ("GLOBAL__F_")-1, 5, 0 },
537 { "GLOBAL__FI_", sizeof ("GLOBAL__FI_")-1, 3, 0 },
538 { "GLOBAL__FD_", sizeof ("GLOBAL__FD_")-1, 4, 0 },
539 { NULL, 0, 0, 0 }
542 while ((ch = *s) == '_')
543 ++s;
545 if (s == orig_s)
546 return 0;
548 for (p = &special[0]; p->len > 0; p++)
550 if (ch == p->name[0]
551 && (!p->two_underscores || ((s - orig_s) >= 2))
552 && strncmp(s, p->name, p->len) == 0)
554 return p->ret;
557 return 0;
560 /* We maintain two prefix lists: one from COMPILER_PATH environment variable
561 and one from the PATH variable. */
563 static struct path_prefix cpath, path;
565 #ifdef CROSS_DIRECTORY_STRUCTURE
566 /* This is the name of the target machine. We use it to form the name
567 of the files to execute. */
569 static const char *const target_machine = TARGET_MACHINE;
570 #endif
572 /* Search for NAME using prefix list PPREFIX. We only look for executable
573 files.
575 Return 0 if not found, otherwise return its name, allocated with malloc. */
577 static char *
578 find_a_file (struct path_prefix *pprefix, const char *name)
580 char *temp;
581 struct prefix_list *pl;
582 int len = pprefix->max_len + strlen (name) + 1;
584 if (debug)
585 fprintf (stderr, "Looking for '%s'\n", name);
587 #ifdef HOST_EXECUTABLE_SUFFIX
588 len += strlen (HOST_EXECUTABLE_SUFFIX);
589 #endif
591 temp = XNEWVEC (char, len);
593 /* Determine the filename to execute (special case for absolute paths). */
595 if (*name == '/'
596 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
597 || (*name && name[1] == ':')
598 #endif
601 if (access (name, X_OK) == 0)
603 strcpy (temp, name);
605 if (debug)
606 fprintf (stderr, " - found: absolute path\n");
608 return temp;
611 #ifdef HOST_EXECUTABLE_SUFFIX
612 /* Some systems have a suffix for executable files.
613 So try appending that. */
614 strcpy (temp, name);
615 strcat (temp, HOST_EXECUTABLE_SUFFIX);
617 if (access (temp, X_OK) == 0)
618 return temp;
619 #endif
621 if (debug)
622 fprintf (stderr, " - failed to locate using absolute path\n");
624 else
625 for (pl = pprefix->plist; pl; pl = pl->next)
627 struct stat st;
629 strcpy (temp, pl->prefix);
630 strcat (temp, name);
632 if (stat (temp, &st) >= 0
633 && ! S_ISDIR (st.st_mode)
634 && access (temp, X_OK) == 0)
635 return temp;
637 #ifdef HOST_EXECUTABLE_SUFFIX
638 /* Some systems have a suffix for executable files.
639 So try appending that. */
640 strcat (temp, HOST_EXECUTABLE_SUFFIX);
642 if (stat (temp, &st) >= 0
643 && ! S_ISDIR (st.st_mode)
644 && access (temp, X_OK) == 0)
645 return temp;
646 #endif
649 if (debug && pprefix->plist == NULL)
650 fprintf (stderr, " - failed: no entries in prefix list\n");
652 free (temp);
653 return 0;
656 /* Add an entry for PREFIX to prefix list PPREFIX. */
658 static void
659 add_prefix (struct path_prefix *pprefix, const char *prefix)
661 struct prefix_list *pl, **prev;
662 int len;
664 if (pprefix->plist)
666 for (pl = pprefix->plist; pl->next; pl = pl->next)
668 prev = &pl->next;
670 else
671 prev = &pprefix->plist;
673 /* Keep track of the longest prefix. */
675 len = strlen (prefix);
676 if (len > pprefix->max_len)
677 pprefix->max_len = len;
679 pl = XNEW (struct prefix_list);
680 pl->prefix = xstrdup (prefix);
682 if (*prev)
683 pl->next = *prev;
684 else
685 pl->next = (struct prefix_list *) 0;
686 *prev = pl;
689 /* Take the value of the environment variable ENV, break it into a path, and
690 add of the entries to PPREFIX. */
692 static void
693 prefix_from_env (const char *env, struct path_prefix *pprefix)
695 const char *p;
696 GET_ENVIRONMENT (p, env);
698 if (p)
699 prefix_from_string (p, pprefix);
702 static void
703 prefix_from_string (const char *p, struct path_prefix *pprefix)
705 const char *startp, *endp;
706 char *nstore = XNEWVEC (char, strlen (p) + 3);
708 if (debug)
709 fprintf (stderr, "Convert string '%s' into prefixes, separator = '%c'\n", p, PATH_SEPARATOR);
711 startp = endp = p;
712 while (1)
714 if (*endp == PATH_SEPARATOR || *endp == 0)
716 strncpy (nstore, startp, endp-startp);
717 if (endp == startp)
719 strcpy (nstore, "./");
721 else if (! IS_DIR_SEPARATOR (endp[-1]))
723 nstore[endp-startp] = DIR_SEPARATOR;
724 nstore[endp-startp+1] = 0;
726 else
727 nstore[endp-startp] = 0;
729 if (debug)
730 fprintf (stderr, " - add prefix: %s\n", nstore);
732 add_prefix (pprefix, nstore);
733 if (*endp == 0)
734 break;
735 endp = startp = endp + 1;
737 else
738 endp++;
740 free (nstore);
743 /* Main program. */
746 main (int argc, char **argv)
748 static const char *const ld_suffix = "ld";
749 static const char *const real_ld_suffix = "real-ld";
750 static const char *const collect_ld_suffix = "collect-ld";
751 static const char *const nm_suffix = "nm";
752 static const char *const gnm_suffix = "gnm";
753 #ifdef LDD_SUFFIX
754 static const char *const ldd_suffix = LDD_SUFFIX;
755 #endif
756 static const char *const strip_suffix = "strip";
757 static const char *const gstrip_suffix = "gstrip";
759 #ifdef CROSS_DIRECTORY_STRUCTURE
760 /* If we look for a program in the compiler directories, we just use
761 the short name, since these directories are already system-specific.
762 But it we look for a program in the system directories, we need to
763 qualify the program name with the target machine. */
765 const char *const full_ld_suffix =
766 concat(target_machine, "-", ld_suffix, NULL);
767 const char *const full_nm_suffix =
768 concat (target_machine, "-", nm_suffix, NULL);
769 const char *const full_gnm_suffix =
770 concat (target_machine, "-", gnm_suffix, NULL);
771 #ifdef LDD_SUFFIX
772 const char *const full_ldd_suffix =
773 concat (target_machine, "-", ldd_suffix, NULL);
774 #endif
775 const char *const full_strip_suffix =
776 concat (target_machine, "-", strip_suffix, NULL);
777 const char *const full_gstrip_suffix =
778 concat (target_machine, "-", gstrip_suffix, NULL);
779 #else
780 const char *const full_ld_suffix = ld_suffix;
781 const char *const full_nm_suffix = nm_suffix;
782 const char *const full_gnm_suffix = gnm_suffix;
783 #ifdef LDD_SUFFIX
784 const char *const full_ldd_suffix = ldd_suffix;
785 #endif
786 const char *const full_strip_suffix = strip_suffix;
787 const char *const full_gstrip_suffix = gstrip_suffix;
788 #endif /* CROSS_DIRECTORY_STRUCTURE */
790 const char *arg;
791 FILE *outf;
792 #ifdef COLLECT_EXPORT_LIST
793 FILE *exportf;
794 #endif
795 const char *ld_file_name;
796 const char *p;
797 char **c_argv;
798 const char **c_ptr;
799 char **ld1_argv;
800 const char **ld1;
801 char **ld2_argv;
802 const char **ld2;
803 char **object_lst;
804 const char **object;
805 int first_file;
806 int num_c_args;
807 char **old_argv;
809 old_argv = argv;
810 expandargv (&argc, &argv);
811 if (argv != old_argv)
812 at_file_supplied = 1;
814 num_c_args = argc + 9;
816 no_demangle = !! getenv ("COLLECT_NO_DEMANGLE");
818 /* Suppress demangling by the real linker, which may be broken. */
819 putenv (xstrdup ("COLLECT_NO_DEMANGLE="));
821 #if defined (COLLECT2_HOST_INITIALIZATION)
822 /* Perform system dependent initialization, if necessary. */
823 COLLECT2_HOST_INITIALIZATION;
824 #endif
826 #ifdef SIGCHLD
827 /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
828 receive the signal. A different setting is inheritable */
829 signal (SIGCHLD, SIG_DFL);
830 #endif
832 /* Unlock the stdio streams. */
833 unlock_std_streams ();
835 gcc_init_libintl ();
837 /* Do not invoke xcalloc before this point, since locale needs to be
838 set first, in case a diagnostic is issued. */
840 ld1 = (const char **)(ld1_argv = xcalloc(sizeof (char *), argc+4));
841 ld2 = (const char **)(ld2_argv = xcalloc(sizeof (char *), argc+11));
842 object = (const char **)(object_lst = xcalloc(sizeof (char *), argc));
844 #ifdef DEBUG
845 debug = 1;
846 #endif
848 /* Parse command line early for instances of -debug. This allows
849 the debug flag to be set before functions like find_a_file()
850 are called. */
852 int i;
854 for (i = 1; argv[i] != NULL; i ++)
856 if (! strcmp (argv[i], "-debug"))
857 debug = 1;
859 vflag = debug;
862 #ifndef DEFAULT_A_OUT_NAME
863 output_file = "a.out";
864 #else
865 output_file = DEFAULT_A_OUT_NAME;
866 #endif
868 obstack_begin (&temporary_obstack, 0);
869 temporary_firstobj = obstack_alloc (&temporary_obstack, 0);
871 #ifndef HAVE_LD_DEMANGLE
872 current_demangling_style = auto_demangling;
873 #endif
874 p = getenv ("COLLECT_GCC_OPTIONS");
875 while (p && *p)
877 const char *q = extract_string (&p);
878 if (*q == '-' && (q[1] == 'm' || q[1] == 'f'))
879 num_c_args++;
881 obstack_free (&temporary_obstack, temporary_firstobj);
883 /* -fno-profile-arcs -fno-test-coverage -fno-branch-probabilities
884 -fno-exceptions -w */
885 num_c_args += 5;
887 c_ptr = (const char **) (c_argv = xcalloc (sizeof (char *), num_c_args));
889 if (argc < 2)
890 fatal ("no arguments");
892 #ifdef SIGQUIT
893 if (signal (SIGQUIT, SIG_IGN) != SIG_IGN)
894 signal (SIGQUIT, handler);
895 #endif
896 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
897 signal (SIGINT, handler);
898 #ifdef SIGALRM
899 if (signal (SIGALRM, SIG_IGN) != SIG_IGN)
900 signal (SIGALRM, handler);
901 #endif
902 #ifdef SIGHUP
903 if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
904 signal (SIGHUP, handler);
905 #endif
906 if (signal (SIGSEGV, SIG_IGN) != SIG_IGN)
907 signal (SIGSEGV, handler);
908 #ifdef SIGBUS
909 if (signal (SIGBUS, SIG_IGN) != SIG_IGN)
910 signal (SIGBUS, handler);
911 #endif
913 /* Extract COMPILER_PATH and PATH into our prefix list. */
914 prefix_from_env ("COMPILER_PATH", &cpath);
915 prefix_from_env ("PATH", &path);
917 /* Try to discover a valid linker/nm/strip to use. */
919 /* Maybe we know the right file to use (if not cross). */
920 ld_file_name = 0;
921 #ifdef DEFAULT_LINKER
922 if (access (DEFAULT_LINKER, X_OK) == 0)
923 ld_file_name = DEFAULT_LINKER;
924 if (ld_file_name == 0)
925 #endif
926 #ifdef REAL_LD_FILE_NAME
927 ld_file_name = find_a_file (&path, REAL_LD_FILE_NAME);
928 if (ld_file_name == 0)
929 #endif
930 /* Search the (target-specific) compiler dirs for ld'. */
931 ld_file_name = find_a_file (&cpath, real_ld_suffix);
932 /* Likewise for `collect-ld'. */
933 if (ld_file_name == 0)
934 ld_file_name = find_a_file (&cpath, collect_ld_suffix);
935 /* Search the compiler directories for `ld'. We have protection against
936 recursive calls in find_a_file. */
937 if (ld_file_name == 0)
938 ld_file_name = find_a_file (&cpath, ld_suffix);
939 /* Search the ordinary system bin directories
940 for `ld' (if native linking) or `TARGET-ld' (if cross). */
941 if (ld_file_name == 0)
942 ld_file_name = find_a_file (&path, full_ld_suffix);
944 #ifdef REAL_NM_FILE_NAME
945 nm_file_name = find_a_file (&path, REAL_NM_FILE_NAME);
946 if (nm_file_name == 0)
947 #endif
948 nm_file_name = find_a_file (&cpath, gnm_suffix);
949 if (nm_file_name == 0)
950 nm_file_name = find_a_file (&path, full_gnm_suffix);
951 if (nm_file_name == 0)
952 nm_file_name = find_a_file (&cpath, nm_suffix);
953 if (nm_file_name == 0)
954 nm_file_name = find_a_file (&path, full_nm_suffix);
956 #ifdef LDD_SUFFIX
957 ldd_file_name = find_a_file (&cpath, ldd_suffix);
958 if (ldd_file_name == 0)
959 ldd_file_name = find_a_file (&path, full_ldd_suffix);
960 #endif
962 #ifdef REAL_STRIP_FILE_NAME
963 strip_file_name = find_a_file (&path, REAL_STRIP_FILE_NAME);
964 if (strip_file_name == 0)
965 #endif
966 strip_file_name = find_a_file (&cpath, gstrip_suffix);
967 if (strip_file_name == 0)
968 strip_file_name = find_a_file (&path, full_gstrip_suffix);
969 if (strip_file_name == 0)
970 strip_file_name = find_a_file (&cpath, strip_suffix);
971 if (strip_file_name == 0)
972 strip_file_name = find_a_file (&path, full_strip_suffix);
974 /* Determine the full path name of the C compiler to use. */
975 c_file_name = getenv ("COLLECT_GCC");
976 if (c_file_name == 0)
978 #ifdef CROSS_DIRECTORY_STRUCTURE
979 c_file_name = concat (target_machine, "-gcc", NULL);
980 #else
981 c_file_name = "gcc";
982 #endif
985 p = find_a_file (&cpath, c_file_name);
987 /* Here it should be safe to use the system search path since we should have
988 already qualified the name of the compiler when it is needed. */
989 if (p == 0)
990 p = find_a_file (&path, c_file_name);
992 if (p)
993 c_file_name = p;
995 *ld1++ = *ld2++ = ld_file_name;
997 /* Make temp file names. */
998 c_file = make_temp_file (".c");
999 o_file = make_temp_file (".o");
1000 #ifdef COLLECT_EXPORT_LIST
1001 export_file = make_temp_file (".x");
1002 #endif
1003 ldout = make_temp_file (".ld");
1004 lderrout = make_temp_file (".le");
1005 *c_ptr++ = c_file_name;
1006 *c_ptr++ = "-x";
1007 *c_ptr++ = "c";
1008 *c_ptr++ = "-c";
1009 *c_ptr++ = "-o";
1010 *c_ptr++ = o_file;
1012 #ifdef COLLECT_EXPORT_LIST
1013 /* Generate a list of directories from LIBPATH. */
1014 prefix_from_env ("LIBPATH", &libpath_lib_dirs);
1015 /* Add to this list also two standard directories where
1016 AIX loader always searches for libraries. */
1017 add_prefix (&libpath_lib_dirs, "/lib");
1018 add_prefix (&libpath_lib_dirs, "/usr/lib");
1019 #endif
1021 /* Get any options that the upper GCC wants to pass to the sub-GCC.
1023 AIX support needs to know if -shared has been specified before
1024 parsing commandline arguments. */
1026 p = getenv ("COLLECT_GCC_OPTIONS");
1027 while (p && *p)
1029 const char *q = extract_string (&p);
1030 if (*q == '-' && (q[1] == 'm' || q[1] == 'f'))
1031 *c_ptr++ = xstrdup (q);
1032 if (strcmp (q, "-EL") == 0 || strcmp (q, "-EB") == 0)
1033 *c_ptr++ = xstrdup (q);
1034 if (strcmp (q, "-shared") == 0)
1035 shared_obj = 1;
1036 if (*q == '-' && q[1] == 'B')
1038 *c_ptr++ = xstrdup (q);
1039 if (q[2] == 0)
1041 q = extract_string (&p);
1042 *c_ptr++ = xstrdup (q);
1046 obstack_free (&temporary_obstack, temporary_firstobj);
1047 *c_ptr++ = "-fno-profile-arcs";
1048 *c_ptr++ = "-fno-test-coverage";
1049 *c_ptr++ = "-fno-branch-probabilities";
1050 *c_ptr++ = "-fno-exceptions";
1051 *c_ptr++ = "-w";
1053 /* !!! When GCC calls collect2,
1054 it does not know whether it is calling collect2 or ld.
1055 So collect2 cannot meaningfully understand any options
1056 except those ld understands.
1057 If you propose to make GCC pass some other option,
1058 just imagine what will happen if ld is really ld!!! */
1060 /* Parse arguments. Remember output file spec, pass the rest to ld. */
1061 /* After the first file, put in the c++ rt0. */
1063 first_file = 1;
1064 #ifdef HAVE_LD_DEMANGLE
1065 if (!demangle_flag && !no_demangle)
1066 demangle_flag = "--demangle";
1067 if (demangle_flag)
1068 *ld1++ = *ld2++ = demangle_flag;
1069 #endif
1070 while ((arg = *++argv) != (char *) 0)
1072 *ld1++ = *ld2++ = arg;
1074 if (arg[0] == '-')
1076 switch (arg[1])
1078 #ifdef COLLECT_EXPORT_LIST
1079 /* We want to disable automatic exports on AIX when user
1080 explicitly puts an export list in command line */
1081 case 'b':
1082 if (arg[2] == 'E' || strncmp (&arg[2], "export", 6) == 0)
1083 export_flag = 1;
1084 else if (arg[2] == '6' && arg[3] == '4')
1085 aix64_flag = 1;
1086 else if (arg[2] == 'r' && arg[3] == 't' && arg[4] == 'l')
1087 aixrtl_flag = 1;
1088 break;
1089 #endif
1091 case 'd':
1092 if (!strcmp (arg, "-debug"))
1094 /* Already parsed. */
1095 ld1--;
1096 ld2--;
1098 if (!strcmp (arg, "-dynamic-linker") && argv[1])
1100 ++argv;
1101 *ld1++ = *ld2++ = *argv;
1103 break;
1105 case 'l':
1106 if (first_file)
1108 /* place o_file BEFORE this argument! */
1109 first_file = 0;
1110 ld2--;
1111 *ld2++ = o_file;
1112 *ld2++ = arg;
1114 #ifdef COLLECT_EXPORT_LIST
1116 /* Resolving full library name. */
1117 const char *s = resolve_lib_name (arg+2);
1119 /* Saving a full library name. */
1120 add_to_list (&libs, s);
1122 #endif
1123 break;
1125 #ifdef COLLECT_EXPORT_LIST
1126 /* Saving directories where to search for libraries. */
1127 case 'L':
1128 add_prefix (&cmdline_lib_dirs, arg+2);
1129 break;
1130 #else
1131 #if LINK_ELIMINATE_DUPLICATE_LDIRECTORIES
1132 case 'L':
1133 if (is_in_args (arg, (const char **) ld1_argv, ld1-1))
1134 --ld1;
1135 break;
1136 #endif /* LINK_ELIMINATE_DUPLICATE_LDIRECTORIES */
1137 #endif
1139 case 'o':
1140 if (arg[2] == '\0')
1141 output_file = *ld1++ = *ld2++ = *++argv;
1142 else if (1
1143 #ifdef SWITCHES_NEED_SPACES
1144 && ! strchr (SWITCHES_NEED_SPACES, arg[1])
1145 #endif
1148 output_file = &arg[2];
1149 break;
1151 case 'r':
1152 if (arg[2] == '\0')
1153 rflag = 1;
1154 break;
1156 case 's':
1157 if (arg[2] == '\0' && do_collecting)
1159 /* We must strip after the nm run, otherwise C++ linking
1160 will not work. Thus we strip in the second ld run, or
1161 else with strip if there is no second ld run. */
1162 strip_flag = 1;
1163 ld1--;
1165 break;
1167 case 'v':
1168 if (arg[2] == '\0')
1169 vflag = 1;
1170 break;
1172 case '-':
1173 if (strcmp (arg, "--no-demangle") == 0)
1175 demangle_flag = arg;
1176 no_demangle = 1;
1177 ld1--;
1178 ld2--;
1180 else if (strncmp (arg, "--demangle", 10) == 0)
1182 demangle_flag = arg;
1183 no_demangle = 0;
1184 #ifndef HAVE_LD_DEMANGLE
1185 if (arg[10] == '=')
1187 enum demangling_styles style
1188 = cplus_demangle_name_to_style (arg+11);
1189 if (style == unknown_demangling)
1190 error ("unknown demangling style '%s'", arg+11);
1191 else
1192 current_demangling_style = style;
1194 #endif
1195 ld1--;
1196 ld2--;
1198 break;
1201 else if ((p = strrchr (arg, '.')) != (char *) 0
1202 && (strcmp (p, ".o") == 0 || strcmp (p, ".a") == 0
1203 || strcmp (p, ".so") == 0 || strcmp (p, ".lo") == 0
1204 || strcmp (p, ".obj") == 0))
1206 if (first_file)
1208 first_file = 0;
1209 if (p[1] == 'o')
1210 *ld2++ = o_file;
1211 else
1213 /* place o_file BEFORE this argument! */
1214 ld2--;
1215 *ld2++ = o_file;
1216 *ld2++ = arg;
1219 if (p[1] == 'o' || p[1] == 'l')
1220 *object++ = arg;
1221 #ifdef COLLECT_EXPORT_LIST
1222 /* libraries can be specified directly, i.e. without -l flag. */
1223 else
1225 /* Saving a full library name. */
1226 add_to_list (&libs, arg);
1228 #endif
1232 #ifdef COLLECT_EXPORT_LIST
1233 /* This is added only for debugging purposes. */
1234 if (debug)
1236 fprintf (stderr, "List of libraries:\n");
1237 dump_list (stderr, "\t", libs.first);
1240 /* The AIX linker will discard static constructors in object files if
1241 nothing else in the file is referenced, so look at them first. */
1243 const char **export_object_lst = (const char **)object_lst;
1245 while (export_object_lst < object)
1246 scan_prog_file (*export_object_lst++, PASS_OBJ);
1249 struct id *list = libs.first;
1251 for (; list; list = list->next)
1252 scan_prog_file (list->name, PASS_FIRST);
1255 if (exports.first)
1257 char *buf = concat ("-bE:", export_file, NULL);
1259 *ld1++ = buf;
1260 *ld2++ = buf;
1262 exportf = fopen (export_file, "w");
1263 if (exportf == (FILE *) 0)
1264 fatal_perror ("fopen %s", export_file);
1265 write_aix_file (exportf, exports.first);
1266 if (fclose (exportf))
1267 fatal_perror ("fclose %s", export_file);
1269 #endif
1271 *c_ptr++ = c_file;
1272 *c_ptr = *ld1 = *object = (char *) 0;
1274 if (vflag)
1276 notice ("collect2 version %s", version_string);
1277 #ifdef TARGET_VERSION
1278 TARGET_VERSION;
1279 #endif
1280 fprintf (stderr, "\n");
1283 if (debug)
1285 const char *ptr;
1286 fprintf (stderr, "ld_file_name = %s\n",
1287 (ld_file_name ? ld_file_name : "not found"));
1288 fprintf (stderr, "c_file_name = %s\n",
1289 (c_file_name ? c_file_name : "not found"));
1290 fprintf (stderr, "nm_file_name = %s\n",
1291 (nm_file_name ? nm_file_name : "not found"));
1292 #ifdef LDD_SUFFIX
1293 fprintf (stderr, "ldd_file_name = %s\n",
1294 (ldd_file_name ? ldd_file_name : "not found"));
1295 #endif
1296 fprintf (stderr, "strip_file_name = %s\n",
1297 (strip_file_name ? strip_file_name : "not found"));
1298 fprintf (stderr, "c_file = %s\n",
1299 (c_file ? c_file : "not found"));
1300 fprintf (stderr, "o_file = %s\n",
1301 (o_file ? o_file : "not found"));
1303 ptr = getenv ("COLLECT_GCC_OPTIONS");
1304 if (ptr)
1305 fprintf (stderr, "COLLECT_GCC_OPTIONS = %s\n", ptr);
1307 ptr = getenv ("COLLECT_GCC");
1308 if (ptr)
1309 fprintf (stderr, "COLLECT_GCC = %s\n", ptr);
1311 ptr = getenv ("COMPILER_PATH");
1312 if (ptr)
1313 fprintf (stderr, "COMPILER_PATH = %s\n", ptr);
1315 ptr = getenv (LIBRARY_PATH_ENV);
1316 if (ptr)
1317 fprintf (stderr, "%-20s= %s\n", LIBRARY_PATH_ENV, ptr);
1319 fprintf (stderr, "\n");
1322 /* Load the program, searching all libraries and attempting to provide
1323 undefined symbols from repository information. */
1325 /* On AIX we do this later. */
1326 #ifndef COLLECT_EXPORT_LIST
1327 do_tlink (ld1_argv, object_lst);
1328 #endif
1330 /* If -r or they will be run via some other method, do not build the
1331 constructor or destructor list, just return now. */
1332 if (rflag
1333 #ifndef COLLECT_EXPORT_LIST
1334 || ! do_collecting
1335 #endif
1338 #ifdef COLLECT_EXPORT_LIST
1339 /* Do the link we avoided above if we are exiting. */
1340 do_tlink (ld1_argv, object_lst);
1342 /* But make sure we delete the export file we may have created. */
1343 if (export_file != 0 && export_file[0])
1344 maybe_unlink (export_file);
1345 #endif
1346 maybe_unlink (c_file);
1347 maybe_unlink (o_file);
1348 return 0;
1351 /* Examine the namelist with nm and search it for static constructors
1352 and destructors to call.
1353 Write the constructor and destructor tables to a .s file and reload. */
1355 /* On AIX we already scanned for global constructors/destructors. */
1356 #ifndef COLLECT_EXPORT_LIST
1357 scan_prog_file (output_file, PASS_FIRST);
1358 #endif
1360 #ifdef SCAN_LIBRARIES
1361 scan_libraries (output_file);
1362 #endif
1364 if (debug)
1366 notice ("%d constructor(s) found\n", constructors.number);
1367 notice ("%d destructor(s) found\n", destructors.number);
1368 notice ("%d frame table(s) found\n", frame_tables.number);
1371 if (constructors.number == 0 && destructors.number == 0
1372 && frame_tables.number == 0
1373 #if defined (SCAN_LIBRARIES) || defined (COLLECT_EXPORT_LIST)
1374 /* If we will be running these functions ourselves, we want to emit
1375 stubs into the shared library so that we do not have to relink
1376 dependent programs when we add static objects. */
1377 && ! shared_obj
1378 #endif
1381 #ifdef COLLECT_EXPORT_LIST
1382 /* Do tlink without additional code generation. */
1383 do_tlink (ld1_argv, object_lst);
1384 #endif
1385 /* Strip now if it was requested on the command line. */
1386 if (strip_flag)
1388 char **real_strip_argv = XCNEWVEC (char *, 3);
1389 const char ** strip_argv = (const char **) real_strip_argv;
1391 strip_argv[0] = strip_file_name;
1392 strip_argv[1] = output_file;
1393 strip_argv[2] = (char *) 0;
1394 fork_execute ("strip", real_strip_argv);
1397 #ifdef COLLECT_EXPORT_LIST
1398 maybe_unlink (export_file);
1399 #endif
1400 maybe_unlink (c_file);
1401 maybe_unlink (o_file);
1402 return 0;
1405 /* Sort ctor and dtor lists by priority. */
1406 sort_ids (&constructors);
1407 sort_ids (&destructors);
1409 maybe_unlink(output_file);
1410 outf = fopen (c_file, "w");
1411 if (outf == (FILE *) 0)
1412 fatal_perror ("fopen %s", c_file);
1414 write_c_file (outf, c_file);
1416 if (fclose (outf))
1417 fatal_perror ("fclose %s", c_file);
1419 /* Tell the linker that we have initializer and finalizer functions. */
1420 #ifdef LD_INIT_SWITCH
1421 #ifdef COLLECT_EXPORT_LIST
1422 *ld2++ = concat (LD_INIT_SWITCH, ":", initname, ":", fininame, NULL);
1423 #else
1424 *ld2++ = LD_INIT_SWITCH;
1425 *ld2++ = initname;
1426 *ld2++ = LD_FINI_SWITCH;
1427 *ld2++ = fininame;
1428 #endif
1429 #endif
1431 #ifdef COLLECT_EXPORT_LIST
1432 if (shared_obj)
1434 /* If we did not add export flag to link arguments before, add it to
1435 second link phase now. No new exports should have been added. */
1436 if (! exports.first)
1437 *ld2++ = concat ("-bE:", export_file, NULL);
1439 #ifndef LD_INIT_SWITCH
1440 add_to_list (&exports, initname);
1441 add_to_list (&exports, fininame);
1442 add_to_list (&exports, "_GLOBAL__DI");
1443 add_to_list (&exports, "_GLOBAL__DD");
1444 #endif
1445 exportf = fopen (export_file, "w");
1446 if (exportf == (FILE *) 0)
1447 fatal_perror ("fopen %s", export_file);
1448 write_aix_file (exportf, exports.first);
1449 if (fclose (exportf))
1450 fatal_perror ("fclose %s", export_file);
1452 #endif
1454 /* End of arguments to second link phase. */
1455 *ld2 = (char*) 0;
1457 if (debug)
1459 fprintf (stderr, "\n========== output_file = %s, c_file = %s\n",
1460 output_file, c_file);
1461 write_c_file (stderr, "stderr");
1462 fprintf (stderr, "========== end of c_file\n\n");
1463 #ifdef COLLECT_EXPORT_LIST
1464 fprintf (stderr, "\n========== export_file = %s\n", export_file);
1465 write_aix_file (stderr, exports.first);
1466 fprintf (stderr, "========== end of export_file\n\n");
1467 #endif
1470 /* Assemble the constructor and destructor tables.
1471 Link the tables in with the rest of the program. */
1473 fork_execute ("gcc", c_argv);
1474 #ifdef COLLECT_EXPORT_LIST
1475 /* On AIX we must call tlink because of possible templates resolution. */
1476 do_tlink (ld2_argv, object_lst);
1477 #else
1478 /* Otherwise, simply call ld because tlink is already done. */
1479 fork_execute ("ld", ld2_argv);
1481 /* Let scan_prog_file do any final mods (OSF/rose needs this for
1482 constructors/destructors in shared libraries. */
1483 scan_prog_file (output_file, PASS_SECOND);
1484 #endif
1486 maybe_unlink (c_file);
1487 maybe_unlink (o_file);
1489 #ifdef COLLECT_EXPORT_LIST
1490 maybe_unlink (export_file);
1491 #endif
1493 return 0;
1497 /* Wait for a process to finish, and exit if a nonzero status is found. */
1500 collect_wait (const char *prog, struct pex_obj *pex)
1502 int status;
1504 if (!pex_get_status (pex, 1, &status))
1505 fatal_perror ("can't get program status");
1506 pex_free (pex);
1508 if (status)
1510 if (WIFSIGNALED (status))
1512 int sig = WTERMSIG (status);
1513 error ("%s terminated with signal %d [%s]%s",
1514 prog, sig, strsignal(sig),
1515 WCOREDUMP(status) ? ", core dumped" : "");
1516 collect_exit (FATAL_EXIT_CODE);
1519 if (WIFEXITED (status))
1520 return WEXITSTATUS (status);
1522 return 0;
1525 static void
1526 do_wait (const char *prog, struct pex_obj *pex)
1528 int ret = collect_wait (prog, pex);
1529 if (ret != 0)
1531 error ("%s returned %d exit status", prog, ret);
1532 collect_exit (ret);
1535 if (response_file)
1537 unlink (response_file);
1538 response_file = NULL;
1543 /* Execute a program, and wait for the reply. */
1545 struct pex_obj *
1546 collect_execute (const char *prog, char **argv, const char *outname,
1547 const char *errname)
1549 struct pex_obj *pex;
1550 const char *errmsg;
1551 int err;
1552 char *response_arg = NULL;
1553 char *response_argv[3] ATTRIBUTE_UNUSED;
1555 if (HAVE_GNU_LD && at_file_supplied && argv[0] != NULL)
1557 /* If using @file arguments, create a temporary file and put the
1558 contents of argv into it. Then change argv to an array corresponding
1559 to a single argument @FILE, where FILE is the temporary filename. */
1561 char **current_argv = argv + 1;
1562 char *argv0 = argv[0];
1563 int status;
1564 FILE *f;
1566 /* Note: we assume argv contains at least one element; this is
1567 checked above. */
1569 response_file = make_temp_file ("");
1571 f = fopen (response_file, "w");
1573 if (f == NULL)
1574 fatal ("could not open response file %s", response_file);
1576 status = writeargv (current_argv, f);
1578 if (status)
1579 fatal ("could not write to response file %s", response_file);
1581 status = fclose (f);
1583 if (EOF == status)
1584 fatal ("could not close response file %s", response_file);
1586 response_arg = concat ("@", response_file, NULL);
1587 response_argv[0] = argv0;
1588 response_argv[1] = response_arg;
1589 response_argv[2] = NULL;
1591 argv = response_argv;
1594 if (vflag || debug)
1596 char **p_argv;
1597 const char *str;
1599 if (argv[0])
1600 fprintf (stderr, "%s", argv[0]);
1601 else
1602 notice ("[cannot find %s]", prog);
1604 for (p_argv = &argv[1]; (str = *p_argv) != (char *) 0; p_argv++)
1605 fprintf (stderr, " %s", str);
1607 fprintf (stderr, "\n");
1610 fflush (stdout);
1611 fflush (stderr);
1613 /* If we cannot find a program we need, complain error. Do this here
1614 since we might not end up needing something that we could not find. */
1616 if (argv[0] == 0)
1617 fatal ("cannot find '%s'", prog);
1619 pex = pex_init (0, "collect2", NULL);
1620 if (pex == NULL)
1621 fatal_perror ("pex_init failed");
1623 errmsg = pex_run (pex, PEX_LAST | PEX_SEARCH, argv[0], argv, outname,
1624 errname, &err);
1625 if (errmsg != NULL)
1627 if (err != 0)
1629 errno = err;
1630 fatal_perror (errmsg);
1632 else
1633 fatal (errmsg);
1636 if (response_arg)
1637 free (response_arg);
1639 return pex;
1642 static void
1643 fork_execute (const char *prog, char **argv)
1645 struct pex_obj *pex;
1647 pex = collect_execute (prog, argv, NULL, NULL);
1648 do_wait (prog, pex);
1651 /* Unlink a file unless we are debugging. */
1653 static void
1654 maybe_unlink (const char *file)
1656 if (!debug)
1657 unlink_if_ordinary (file);
1658 else
1659 notice ("[Leaving %s]\n", file);
1663 static long sequence_number = 0;
1665 /* Add a name to a linked list. */
1667 static void
1668 add_to_list (struct head *head_ptr, const char *name)
1670 struct id *newid = xcalloc (sizeof (struct id) + strlen (name), 1);
1671 struct id *p;
1672 strcpy (newid->name, name);
1674 if (head_ptr->first)
1675 head_ptr->last->next = newid;
1676 else
1677 head_ptr->first = newid;
1679 /* Check for duplicate symbols. */
1680 for (p = head_ptr->first;
1681 strcmp (name, p->name) != 0;
1682 p = p->next)
1684 if (p != newid)
1686 head_ptr->last->next = 0;
1687 free (newid);
1688 return;
1691 newid->sequence = ++sequence_number;
1692 head_ptr->last = newid;
1693 head_ptr->number++;
1696 /* Grab the init priority number from an init function name that
1697 looks like "_GLOBAL_.I.12345.foo". */
1699 static int
1700 extract_init_priority (const char *name)
1702 int pos = 0, pri;
1704 while (name[pos] == '_')
1705 ++pos;
1706 pos += 10; /* strlen ("GLOBAL__X_") */
1708 /* Extract init_p number from ctor/dtor name. */
1709 pri = atoi (name + pos);
1710 return pri ? pri : DEFAULT_INIT_PRIORITY;
1713 /* Insertion sort the ids from ctor/dtor list HEAD_PTR in descending order.
1714 ctors will be run from right to left, dtors from left to right. */
1716 static void
1717 sort_ids (struct head *head_ptr)
1719 /* id holds the current element to insert. id_next holds the next
1720 element to insert. id_ptr iterates through the already sorted elements
1721 looking for the place to insert id. */
1722 struct id *id, *id_next, **id_ptr;
1724 id = head_ptr->first;
1726 /* We don't have any sorted elements yet. */
1727 head_ptr->first = NULL;
1729 for (; id; id = id_next)
1731 id_next = id->next;
1732 id->sequence = extract_init_priority (id->name);
1734 for (id_ptr = &(head_ptr->first); ; id_ptr = &((*id_ptr)->next))
1735 if (*id_ptr == NULL
1736 /* If the sequence numbers are the same, we put the id from the
1737 file later on the command line later in the list. */
1738 || id->sequence > (*id_ptr)->sequence
1739 /* Hack: do lexical compare, too.
1740 || (id->sequence == (*id_ptr)->sequence
1741 && strcmp (id->name, (*id_ptr)->name) > 0) */
1744 id->next = *id_ptr;
1745 *id_ptr = id;
1746 break;
1750 /* Now set the sequence numbers properly so write_c_file works. */
1751 for (id = head_ptr->first; id; id = id->next)
1752 id->sequence = ++sequence_number;
1755 /* Write: `prefix', the names on list LIST, `suffix'. */
1757 static void
1758 write_list (FILE *stream, const char *prefix, struct id *list)
1760 while (list)
1762 fprintf (stream, "%sx%d,\n", prefix, list->sequence);
1763 list = list->next;
1767 #if LINK_ELIMINATE_DUPLICATE_LDIRECTORIES
1768 /* Given a STRING, return nonzero if it occurs in the list in range
1769 [ARGS_BEGIN,ARGS_END). */
1771 static int
1772 is_in_args (const char *string, const char **args_begin,
1773 const char **args_end)
1775 const char **args_pointer;
1776 for (args_pointer = args_begin; args_pointer != args_end; ++args_pointer)
1777 if (strcmp (string, *args_pointer) == 0)
1778 return 1;
1779 return 0;
1781 #endif /* LINK_ELIMINATE_DUPLICATE_LDIRECTORIES */
1783 #ifdef COLLECT_EXPORT_LIST
1784 /* This function is really used only on AIX, but may be useful. */
1785 #if 0
1786 static int
1787 is_in_list (const char *prefix, struct id *list)
1789 while (list)
1791 if (!strcmp (prefix, list->name)) return 1;
1792 list = list->next;
1794 return 0;
1796 #endif
1797 #endif /* COLLECT_EXPORT_LIST */
1799 /* Added for debugging purpose. */
1800 #ifdef COLLECT_EXPORT_LIST
1801 static void
1802 dump_list (FILE *stream, const char *prefix, struct id *list)
1804 while (list)
1806 fprintf (stream, "%s%s,\n", prefix, list->name);
1807 list = list->next;
1810 #endif
1812 #if 0
1813 static void
1814 dump_prefix_list (FILE *stream, const char *prefix, struct prefix_list *list)
1816 while (list)
1818 fprintf (stream, "%s%s,\n", prefix, list->prefix);
1819 list = list->next;
1822 #endif
1824 static void
1825 write_list_with_asm (FILE *stream, const char *prefix, struct id *list)
1827 while (list)
1829 fprintf (stream, "%sx%d __asm__ (\"%s\");\n",
1830 prefix, list->sequence, list->name);
1831 list = list->next;
1835 /* Write out the constructor and destructor tables statically (for a shared
1836 object), along with the functions to execute them. */
1838 static void
1839 write_c_file_stat (FILE *stream, const char *name ATTRIBUTE_UNUSED)
1841 const char *p, *q;
1842 char *prefix, *r;
1843 int frames = (frame_tables.number > 0);
1845 /* Figure out name of output_file, stripping off .so version. */
1846 p = strrchr (output_file, '/');
1847 if (p == 0)
1848 p = output_file;
1849 else
1850 p++;
1851 q = p;
1852 while (q)
1854 q = strchr (q,'.');
1855 if (q == 0)
1857 q = p + strlen (p);
1858 break;
1860 else
1862 if (strncmp (q, ".so", 3) == 0)
1864 q += 3;
1865 break;
1867 else
1868 q++;
1871 /* q points to null at end of the string (or . of the .so version) */
1872 prefix = XNEWVEC (char, q - p + 1);
1873 strncpy (prefix, p, q - p);
1874 prefix[q - p] = 0;
1875 for (r = prefix; *r; r++)
1876 if (!ISALNUM ((unsigned char)*r))
1877 *r = '_';
1878 if (debug)
1879 notice ("\nwrite_c_file - output name is %s, prefix is %s\n",
1880 output_file, prefix);
1882 initname = concat ("_GLOBAL__FI_", prefix, NULL);
1883 fininame = concat ("_GLOBAL__FD_", prefix, NULL);
1885 free (prefix);
1887 /* Write the tables as C code. */
1889 fprintf (stream, "static int count;\n");
1890 fprintf (stream, "typedef void entry_pt();\n");
1891 write_list_with_asm (stream, "extern entry_pt ", constructors.first);
1893 if (frames)
1895 write_list_with_asm (stream, "extern void *", frame_tables.first);
1897 fprintf (stream, "\tstatic void *frame_table[] = {\n");
1898 write_list (stream, "\t\t&", frame_tables.first);
1899 fprintf (stream, "\t0\n};\n");
1901 /* This must match what's in frame.h. */
1902 fprintf (stream, "struct object {\n");
1903 fprintf (stream, " void *pc_begin;\n");
1904 fprintf (stream, " void *pc_end;\n");
1905 fprintf (stream, " void *fde_begin;\n");
1906 fprintf (stream, " void *fde_array;\n");
1907 fprintf (stream, " __SIZE_TYPE__ count;\n");
1908 fprintf (stream, " struct object *next;\n");
1909 fprintf (stream, "};\n");
1911 fprintf (stream, "extern void __register_frame_info_table (void *, struct object *);\n");
1912 fprintf (stream, "extern void *__deregister_frame_info (void *);\n");
1914 fprintf (stream, "static void reg_frame () {\n");
1915 fprintf (stream, "\tstatic struct object ob;\n");
1916 fprintf (stream, "\t__register_frame_info_table (frame_table, &ob);\n");
1917 fprintf (stream, "\t}\n");
1919 fprintf (stream, "static void dereg_frame () {\n");
1920 fprintf (stream, "\t__deregister_frame_info (frame_table);\n");
1921 fprintf (stream, "\t}\n");
1924 fprintf (stream, "void %s() {\n", initname);
1925 if (constructors.number > 0 || frames)
1927 fprintf (stream, "\tstatic entry_pt *ctors[] = {\n");
1928 write_list (stream, "\t\t", constructors.first);
1929 if (frames)
1930 fprintf (stream, "\treg_frame,\n");
1931 fprintf (stream, "\t};\n");
1932 fprintf (stream, "\tentry_pt **p;\n");
1933 fprintf (stream, "\tif (count++ != 0) return;\n");
1934 fprintf (stream, "\tp = ctors + %d;\n", constructors.number + frames);
1935 fprintf (stream, "\twhile (p > ctors) (*--p)();\n");
1937 else
1938 fprintf (stream, "\t++count;\n");
1939 fprintf (stream, "}\n");
1940 write_list_with_asm (stream, "extern entry_pt ", destructors.first);
1941 fprintf (stream, "void %s() {\n", fininame);
1942 if (destructors.number > 0 || frames)
1944 fprintf (stream, "\tstatic entry_pt *dtors[] = {\n");
1945 write_list (stream, "\t\t", destructors.first);
1946 if (frames)
1947 fprintf (stream, "\tdereg_frame,\n");
1948 fprintf (stream, "\t};\n");
1949 fprintf (stream, "\tentry_pt **p;\n");
1950 fprintf (stream, "\tif (--count != 0) return;\n");
1951 fprintf (stream, "\tp = dtors;\n");
1952 fprintf (stream, "\twhile (p < dtors + %d) (*p++)();\n",
1953 destructors.number + frames);
1955 fprintf (stream, "}\n");
1957 if (shared_obj)
1959 COLLECT_SHARED_INIT_FUNC(stream, initname);
1960 COLLECT_SHARED_FINI_FUNC(stream, fininame);
1964 /* Write the constructor/destructor tables. */
1966 #ifndef LD_INIT_SWITCH
1967 static void
1968 write_c_file_glob (FILE *stream, const char *name ATTRIBUTE_UNUSED)
1970 /* Write the tables as C code. */
1972 int frames = (frame_tables.number > 0);
1974 fprintf (stream, "typedef void entry_pt();\n\n");
1976 write_list_with_asm (stream, "extern entry_pt ", constructors.first);
1978 if (frames)
1980 write_list_with_asm (stream, "extern void *", frame_tables.first);
1982 fprintf (stream, "\tstatic void *frame_table[] = {\n");
1983 write_list (stream, "\t\t&", frame_tables.first);
1984 fprintf (stream, "\t0\n};\n");
1986 /* This must match what's in frame.h. */
1987 fprintf (stream, "struct object {\n");
1988 fprintf (stream, " void *pc_begin;\n");
1989 fprintf (stream, " void *pc_end;\n");
1990 fprintf (stream, " void *fde_begin;\n");
1991 fprintf (stream, " void *fde_array;\n");
1992 fprintf (stream, " __SIZE_TYPE__ count;\n");
1993 fprintf (stream, " struct object *next;\n");
1994 fprintf (stream, "};\n");
1996 fprintf (stream, "extern void __register_frame_info_table (void *, struct object *);\n");
1997 fprintf (stream, "extern void *__deregister_frame_info (void *);\n");
1999 fprintf (stream, "static void reg_frame () {\n");
2000 fprintf (stream, "\tstatic struct object ob;\n");
2001 fprintf (stream, "\t__register_frame_info_table (frame_table, &ob);\n");
2002 fprintf (stream, "\t}\n");
2004 fprintf (stream, "static void dereg_frame () {\n");
2005 fprintf (stream, "\t__deregister_frame_info (frame_table);\n");
2006 fprintf (stream, "\t}\n");
2009 fprintf (stream, "\nentry_pt * __CTOR_LIST__[] = {\n");
2010 fprintf (stream, "\t(entry_pt *) %d,\n", constructors.number + frames);
2011 write_list (stream, "\t", constructors.first);
2012 if (frames)
2013 fprintf (stream, "\treg_frame,\n");
2014 fprintf (stream, "\t0\n};\n\n");
2016 write_list_with_asm (stream, "extern entry_pt ", destructors.first);
2018 fprintf (stream, "\nentry_pt * __DTOR_LIST__[] = {\n");
2019 fprintf (stream, "\t(entry_pt *) %d,\n", destructors.number + frames);
2020 write_list (stream, "\t", destructors.first);
2021 if (frames)
2022 fprintf (stream, "\tdereg_frame,\n");
2023 fprintf (stream, "\t0\n};\n\n");
2025 fprintf (stream, "extern entry_pt %s;\n", NAME__MAIN);
2026 fprintf (stream, "entry_pt *__main_reference = %s;\n\n", NAME__MAIN);
2028 #endif /* ! LD_INIT_SWITCH */
2030 static void
2031 write_c_file (FILE *stream, const char *name)
2033 fprintf (stream, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n");
2034 #ifndef LD_INIT_SWITCH
2035 if (! shared_obj)
2036 write_c_file_glob (stream, name);
2037 else
2038 #endif
2039 write_c_file_stat (stream, name);
2040 fprintf (stream, "#ifdef __cplusplus\n}\n#endif\n");
2043 #ifdef COLLECT_EXPORT_LIST
2044 static void
2045 write_aix_file (FILE *stream, struct id *list)
2047 for (; list; list = list->next)
2049 fputs (list->name, stream);
2050 putc ('\n', stream);
2053 #endif
2055 #ifdef OBJECT_FORMAT_NONE
2057 /* Generic version to scan the name list of the loaded program for
2058 the symbols g++ uses for static constructors and destructors.
2060 The constructor table begins at __CTOR_LIST__ and contains a count
2061 of the number of pointers (or -1 if the constructors are built in a
2062 separate section by the linker), followed by the pointers to the
2063 constructor functions, terminated with a null pointer. The
2064 destructor table has the same format, and begins at __DTOR_LIST__. */
2066 static void
2067 scan_prog_file (const char *prog_name, enum pass which_pass)
2069 void (*int_handler) (int);
2070 #ifdef SIGQUIT
2071 void (*quit_handler) (int);
2072 #endif
2073 char *real_nm_argv[4];
2074 const char **nm_argv = (const char **) real_nm_argv;
2075 int argc = 0;
2076 struct pex_obj *pex;
2077 const char *errmsg;
2078 int err;
2079 char *p, buf[1024];
2080 FILE *inf;
2082 if (which_pass == PASS_SECOND)
2083 return;
2085 /* If we do not have an `nm', complain. */
2086 if (nm_file_name == 0)
2087 fatal ("cannot find 'nm'");
2089 nm_argv[argc++] = nm_file_name;
2090 if (NM_FLAGS[0] != '\0')
2091 nm_argv[argc++] = NM_FLAGS;
2093 nm_argv[argc++] = prog_name;
2094 nm_argv[argc++] = (char *) 0;
2096 /* Trace if needed. */
2097 if (vflag)
2099 const char **p_argv;
2100 const char *str;
2102 for (p_argv = &nm_argv[0]; (str = *p_argv) != (char *) 0; p_argv++)
2103 fprintf (stderr, " %s", str);
2105 fprintf (stderr, "\n");
2108 fflush (stdout);
2109 fflush (stderr);
2111 pex = pex_init (PEX_USE_PIPES, "collect2", NULL);
2112 if (pex == NULL)
2113 fatal_perror ("pex_init failed");
2115 errmsg = pex_run (pex, 0, nm_file_name, real_nm_argv, NULL, NULL, &err);
2116 if (errmsg != NULL)
2118 if (err != 0)
2120 errno = err;
2121 fatal_perror (errmsg);
2123 else
2124 fatal (errmsg);
2127 int_handler = (void (*) (int)) signal (SIGINT, SIG_IGN);
2128 #ifdef SIGQUIT
2129 quit_handler = (void (*) (int)) signal (SIGQUIT, SIG_IGN);
2130 #endif
2132 inf = pex_read_output (pex, 0);
2133 if (inf == NULL)
2134 fatal_perror ("can't open nm output");
2136 if (debug)
2137 fprintf (stderr, "\nnm output with constructors/destructors.\n");
2139 /* Read each line of nm output. */
2140 while (fgets (buf, sizeof buf, inf) != (char *) 0)
2142 int ch, ch2;
2143 char *name, *end;
2145 /* If it contains a constructor or destructor name, add the name
2146 to the appropriate list. */
2148 for (p = buf; (ch = *p) != '\0' && ch != '\n' && ch != '_'; p++)
2149 if (ch == ' ' && p[1] == 'U' && p[2] == ' ')
2150 break;
2152 if (ch != '_')
2153 continue;
2155 name = p;
2156 /* Find the end of the symbol name.
2157 Do not include `|', because Encore nm can tack that on the end. */
2158 for (end = p; (ch2 = *end) != '\0' && !ISSPACE (ch2) && ch2 != '|';
2159 end++)
2160 continue;
2163 *end = '\0';
2164 switch (is_ctor_dtor (name))
2166 case 1:
2167 if (which_pass != PASS_LIB)
2168 add_to_list (&constructors, name);
2169 break;
2171 case 2:
2172 if (which_pass != PASS_LIB)
2173 add_to_list (&destructors, name);
2174 break;
2176 case 3:
2177 if (which_pass != PASS_LIB)
2178 fatal ("init function found in object %s", prog_name);
2179 #ifndef LD_INIT_SWITCH
2180 add_to_list (&constructors, name);
2181 #endif
2182 break;
2184 case 4:
2185 if (which_pass != PASS_LIB)
2186 fatal ("fini function found in object %s", prog_name);
2187 #ifndef LD_FINI_SWITCH
2188 add_to_list (&destructors, name);
2189 #endif
2190 break;
2192 case 5:
2193 if (which_pass != PASS_LIB)
2194 add_to_list (&frame_tables, name);
2195 break;
2197 default: /* not a constructor or destructor */
2198 continue;
2201 if (debug)
2202 fprintf (stderr, "\t%s\n", buf);
2205 if (debug)
2206 fprintf (stderr, "\n");
2208 do_wait (nm_file_name, pex);
2210 signal (SIGINT, int_handler);
2211 #ifdef SIGQUIT
2212 signal (SIGQUIT, quit_handler);
2213 #endif
2216 #ifdef LDD_SUFFIX
2218 /* Use the List Dynamic Dependencies program to find shared libraries that
2219 the output file depends upon and their initialization/finalization
2220 routines, if any. */
2222 static void
2223 scan_libraries (const char *prog_name)
2225 static struct head libraries; /* list of shared libraries found */
2226 struct id *list;
2227 void (*int_handler) (int);
2228 #ifdef SIGQUIT
2229 void (*quit_handler) (int);
2230 #endif
2231 char *real_ldd_argv[4];
2232 const char **ldd_argv = (const char **) real_ldd_argv;
2233 int argc = 0;
2234 struct pex_obj *pex;
2235 const char *errmsg;
2236 int err;
2237 char buf[1024];
2238 FILE *inf;
2240 /* If we do not have an `ldd', complain. */
2241 if (ldd_file_name == 0)
2243 error ("cannot find 'ldd'");
2244 return;
2247 ldd_argv[argc++] = ldd_file_name;
2248 ldd_argv[argc++] = prog_name;
2249 ldd_argv[argc++] = (char *) 0;
2251 /* Trace if needed. */
2252 if (vflag)
2254 const char **p_argv;
2255 const char *str;
2257 for (p_argv = &ldd_argv[0]; (str = *p_argv) != (char *) 0; p_argv++)
2258 fprintf (stderr, " %s", str);
2260 fprintf (stderr, "\n");
2263 fflush (stdout);
2264 fflush (stderr);
2266 pex = pex_init (PEX_USE_PIPES, "collect2", NULL);
2267 if (pex == NULL)
2268 fatal_perror ("pex_init failed");
2270 errmsg = pex_run (pex, 0, ldd_file_name, real_ldd_argv, NULL, NULL, &err);
2271 if (errmsg != NULL)
2273 if (err != 0)
2275 errno = err;
2276 fatal_perror (errmsg);
2278 else
2279 fatal (errmsg);
2282 int_handler = (void (*) (int)) signal (SIGINT, SIG_IGN);
2283 #ifdef SIGQUIT
2284 quit_handler = (void (*) (int)) signal (SIGQUIT, SIG_IGN);
2285 #endif
2287 inf = pex_read_output (pex, 0);
2288 if (inf == NULL)
2289 fatal_perror ("can't open ldd output");
2291 if (debug)
2292 notice ("\nldd output with constructors/destructors.\n");
2294 /* Read each line of ldd output. */
2295 while (fgets (buf, sizeof buf, inf) != (char *) 0)
2297 int ch2;
2298 char *name, *end, *p = buf;
2300 /* Extract names of libraries and add to list. */
2301 PARSE_LDD_OUTPUT (p);
2302 if (p == 0)
2303 continue;
2305 name = p;
2306 if (strncmp (name, "not found", sizeof ("not found") - 1) == 0)
2307 fatal ("dynamic dependency %s not found", buf);
2309 /* Find the end of the symbol name. */
2310 for (end = p;
2311 (ch2 = *end) != '\0' && ch2 != '\n' && !ISSPACE (ch2) && ch2 != '|';
2312 end++)
2313 continue;
2314 *end = '\0';
2316 if (access (name, R_OK) == 0)
2317 add_to_list (&libraries, name);
2318 else
2319 fatal ("unable to open dynamic dependency '%s'", buf);
2321 if (debug)
2322 fprintf (stderr, "\t%s\n", buf);
2324 if (debug)
2325 fprintf (stderr, "\n");
2327 do_wait (ldd_file_name, pex);
2329 signal (SIGINT, int_handler);
2330 #ifdef SIGQUIT
2331 signal (SIGQUIT, quit_handler);
2332 #endif
2334 /* Now iterate through the library list adding their symbols to
2335 the list. */
2336 for (list = libraries.first; list; list = list->next)
2337 scan_prog_file (list->name, PASS_LIB);
2340 #endif /* LDD_SUFFIX */
2342 #endif /* OBJECT_FORMAT_NONE */
2346 * COFF specific stuff.
2349 #ifdef OBJECT_FORMAT_COFF
2351 #if defined (EXTENDED_COFF)
2353 # define GCC_SYMBOLS(X) (SYMHEADER(X).isymMax + SYMHEADER(X).iextMax)
2354 # define GCC_SYMENT SYMR
2355 # define GCC_OK_SYMBOL(X) ((X).st == stProc || (X).st == stGlobal)
2356 # define GCC_SYMINC(X) (1)
2357 # define GCC_SYMZERO(X) (SYMHEADER(X).isymMax)
2358 # define GCC_CHECK_HDR(X) (PSYMTAB(X) != 0)
2360 #else
2362 # define GCC_SYMBOLS(X) (HEADER(ldptr).f_nsyms)
2363 # define GCC_SYMENT SYMENT
2364 # if defined (C_WEAKEXT)
2365 # define GCC_OK_SYMBOL(X) \
2366 (((X).n_sclass == C_EXT || (X).n_sclass == C_WEAKEXT) && \
2367 ((X).n_scnum > N_UNDEF) && \
2368 (aix64_flag \
2369 || (((X).n_type & N_TMASK) == (DT_NON << N_BTSHFT) \
2370 || ((X).n_type & N_TMASK) == (DT_FCN << N_BTSHFT))))
2371 # define GCC_UNDEF_SYMBOL(X) \
2372 (((X).n_sclass == C_EXT || (X).n_sclass == C_WEAKEXT) && \
2373 ((X).n_scnum == N_UNDEF))
2374 # else
2375 # define GCC_OK_SYMBOL(X) \
2376 (((X).n_sclass == C_EXT) && \
2377 ((X).n_scnum > N_UNDEF) && \
2378 (aix64_flag \
2379 || (((X).n_type & N_TMASK) == (DT_NON << N_BTSHFT) \
2380 || ((X).n_type & N_TMASK) == (DT_FCN << N_BTSHFT))))
2381 # define GCC_UNDEF_SYMBOL(X) \
2382 (((X).n_sclass == C_EXT) && ((X).n_scnum == N_UNDEF))
2383 # endif
2384 # define GCC_SYMINC(X) ((X).n_numaux+1)
2385 # define GCC_SYMZERO(X) 0
2387 /* 0757 = U803XTOCMAGIC (AIX 4.3) and 0767 = U64_TOCMAGIC (AIX V5) */
2388 #ifdef _AIX51
2389 # define GCC_CHECK_HDR(X) \
2390 ((HEADER (X).f_magic == U802TOCMAGIC && ! aix64_flag) \
2391 || (HEADER (X).f_magic == 0767 && aix64_flag))
2392 #else
2393 # define GCC_CHECK_HDR(X) \
2394 ((HEADER (X).f_magic == U802TOCMAGIC && ! aix64_flag) \
2395 || (HEADER (X).f_magic == 0757 && aix64_flag))
2396 #endif
2398 #endif
2400 #ifdef COLLECT_EXPORT_LIST
2401 /* Array of standard AIX libraries which should not
2402 be scanned for ctors/dtors. */
2403 static const char *const aix_std_libs[] = {
2404 "/unix",
2405 "/lib/libc.a",
2406 "/lib/libm.a",
2407 "/lib/libc_r.a",
2408 "/lib/libm_r.a",
2409 "/usr/lib/libc.a",
2410 "/usr/lib/libm.a",
2411 "/usr/lib/libc_r.a",
2412 "/usr/lib/libm_r.a",
2413 "/usr/lib/threads/libc.a",
2414 "/usr/ccs/lib/libc.a",
2415 "/usr/ccs/lib/libm.a",
2416 "/usr/ccs/lib/libc_r.a",
2417 "/usr/ccs/lib/libm_r.a",
2418 NULL
2421 /* This function checks the filename and returns 1
2422 if this name matches the location of a standard AIX library. */
2423 static int ignore_library (const char *);
2424 static int
2425 ignore_library (const char *name)
2427 const char *const *p = &aix_std_libs[0];
2428 while (*p++ != NULL)
2429 if (! strcmp (name, *p)) return 1;
2430 return 0;
2432 #endif /* COLLECT_EXPORT_LIST */
2434 #if defined (HAVE_DECL_LDGETNAME) && !HAVE_DECL_LDGETNAME
2435 extern char *ldgetname (LDFILE *, GCC_SYMENT *);
2436 #endif
2438 /* COFF version to scan the name list of the loaded program for
2439 the symbols g++ uses for static constructors and destructors.
2441 The constructor table begins at __CTOR_LIST__ and contains a count
2442 of the number of pointers (or -1 if the constructors are built in a
2443 separate section by the linker), followed by the pointers to the
2444 constructor functions, terminated with a null pointer. The
2445 destructor table has the same format, and begins at __DTOR_LIST__. */
2447 static void
2448 scan_prog_file (const char *prog_name, enum pass which_pass)
2450 LDFILE *ldptr = NULL;
2451 int sym_index, sym_count;
2452 int is_shared = 0;
2454 if (which_pass != PASS_FIRST && which_pass != PASS_OBJ)
2455 return;
2457 #ifdef COLLECT_EXPORT_LIST
2458 /* We do not need scanning for some standard C libraries. */
2459 if (which_pass == PASS_FIRST && ignore_library (prog_name))
2460 return;
2462 /* On AIX we have a loop, because there is not much difference
2463 between an object and an archive. This trick allows us to
2464 eliminate scan_libraries() function. */
2467 #endif
2468 /* Some platforms (e.g. OSF4) declare ldopen as taking a
2469 non-const char * filename parameter, even though it will not
2470 modify that string. So we must cast away const-ness here,
2471 which will cause -Wcast-qual to burp. */
2472 if ((ldptr = ldopen ((char *)prog_name, ldptr)) != NULL)
2474 if (! MY_ISCOFF (HEADER (ldptr).f_magic))
2475 fatal ("%s: not a COFF file", prog_name);
2477 if (GCC_CHECK_HDR (ldptr))
2479 sym_count = GCC_SYMBOLS (ldptr);
2480 sym_index = GCC_SYMZERO (ldptr);
2482 #ifdef COLLECT_EXPORT_LIST
2483 /* Is current archive member a shared object? */
2484 is_shared = HEADER (ldptr).f_flags & F_SHROBJ;
2485 #endif
2487 while (sym_index < sym_count)
2489 GCC_SYMENT symbol;
2491 if (ldtbread (ldptr, sym_index, &symbol) <= 0)
2492 break;
2493 sym_index += GCC_SYMINC (symbol);
2495 if (GCC_OK_SYMBOL (symbol))
2497 char *name;
2499 if ((name = ldgetname (ldptr, &symbol)) == NULL)
2500 continue; /* Should never happen. */
2502 #ifdef XCOFF_DEBUGGING_INFO
2503 /* All AIX function names have a duplicate entry
2504 beginning with a dot. */
2505 if (*name == '.')
2506 ++name;
2507 #endif
2509 switch (is_ctor_dtor (name))
2511 case 1:
2512 if (! is_shared)
2513 add_to_list (&constructors, name);
2514 #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
2515 if (which_pass == PASS_OBJ)
2516 add_to_list (&exports, name);
2517 #endif
2518 break;
2520 case 2:
2521 if (! is_shared)
2522 add_to_list (&destructors, name);
2523 #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
2524 if (which_pass == PASS_OBJ)
2525 add_to_list (&exports, name);
2526 #endif
2527 break;
2529 #ifdef COLLECT_EXPORT_LIST
2530 case 3:
2531 #ifndef LD_INIT_SWITCH
2532 if (is_shared)
2533 add_to_list (&constructors, name);
2534 #endif
2535 break;
2537 case 4:
2538 #ifndef LD_INIT_SWITCH
2539 if (is_shared)
2540 add_to_list (&destructors, name);
2541 #endif
2542 break;
2543 #endif
2545 case 5:
2546 if (! is_shared)
2547 add_to_list (&frame_tables, name);
2548 #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
2549 if (which_pass == PASS_OBJ)
2550 add_to_list (&exports, name);
2551 #endif
2552 break;
2554 default: /* not a constructor or destructor */
2555 #ifdef COLLECT_EXPORT_LIST
2556 /* Explicitly export all global symbols when
2557 building a shared object on AIX, but do not
2558 re-export symbols from another shared object
2559 and do not export symbols if the user
2560 provides an explicit export list. */
2561 if (shared_obj && !is_shared
2562 && which_pass == PASS_OBJ && !export_flag)
2563 add_to_list (&exports, name);
2564 #endif
2565 continue;
2568 if (debug)
2569 #if !defined(EXTENDED_COFF)
2570 fprintf (stderr, "\tsec=%d class=%d type=%s%o %s\n",
2571 symbol.n_scnum, symbol.n_sclass,
2572 (symbol.n_type ? "0" : ""), symbol.n_type,
2573 name);
2574 #else
2575 fprintf (stderr,
2576 "\tiss = %5d, value = %5ld, index = %5d, name = %s\n",
2577 symbol.iss, (long) symbol.value, symbol.index, name);
2578 #endif
2582 #ifdef COLLECT_EXPORT_LIST
2583 else
2585 /* If archive contains both 32-bit and 64-bit objects,
2586 we want to skip objects in other mode so mismatch normal. */
2587 if (debug)
2588 fprintf (stderr, "%s : magic=%o aix64=%d mismatch\n",
2589 prog_name, HEADER (ldptr).f_magic, aix64_flag);
2591 #endif
2593 else
2595 fatal ("%s: cannot open as COFF file", prog_name);
2597 #ifdef COLLECT_EXPORT_LIST
2598 /* On AIX loop continues while there are more members in archive. */
2600 while (ldclose (ldptr) == FAILURE);
2601 #else
2602 /* Otherwise we simply close ldptr. */
2603 (void) ldclose(ldptr);
2604 #endif
2606 #endif /* OBJECT_FORMAT_COFF */
2608 #ifdef COLLECT_EXPORT_LIST
2609 /* Given a library name without "lib" prefix, this function
2610 returns a full library name including a path. */
2611 static char *
2612 resolve_lib_name (const char *name)
2614 char *lib_buf;
2615 int i, j, l = 0;
2616 /* Library extensions for AIX dynamic linking. */
2617 const char * const libexts[2] = {"a", "so"};
2619 for (i = 0; libpaths[i]; i++)
2620 if (libpaths[i]->max_len > l)
2621 l = libpaths[i]->max_len;
2623 lib_buf = xmalloc (l + strlen(name) + 10);
2625 for (i = 0; libpaths[i]; i++)
2627 struct prefix_list *list = libpaths[i]->plist;
2628 for (; list; list = list->next)
2630 /* The following lines are needed because path_prefix list
2631 may contain directories both with trailing '/' and
2632 without it. */
2633 const char *p = "";
2634 if (list->prefix[strlen(list->prefix)-1] != '/')
2635 p = "/";
2636 for (j = 0; j < 2; j++)
2638 sprintf (lib_buf, "%s%slib%s.%s",
2639 list->prefix, p, name,
2640 libexts[(j + aixrtl_flag) % 2]);
2641 if (debug) fprintf (stderr, "searching for: %s\n", lib_buf);
2642 if (file_exists (lib_buf))
2644 if (debug) fprintf (stderr, "found: %s\n", lib_buf);
2645 return (lib_buf);
2650 if (debug)
2651 fprintf (stderr, "not found\n");
2652 else
2653 fatal ("library lib%s not found", name);
2654 return (NULL);
2656 #endif /* COLLECT_EXPORT_LIST */