* gcc.dg/i386-asm-4.c: New test.
[official-gcc.git] / gcc / collect2.c
blob999e670fc7c70e5eb9eebf19547036bb200c5740
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 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, 59 Temple Place - Suite 330, Boston, MA
24 02111-1307, 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 #ifdef vfork /* Autoconf may define this to fork for us. */
39 # define VFORK_STRING "fork"
40 #else
41 # define VFORK_STRING "vfork"
42 #endif
43 #ifdef HAVE_VFORK_H
44 #include <vfork.h>
45 #endif
46 #ifdef VMS
47 #define vfork() (decc$$alloc_vfork_blocks() >= 0 ? \
48 lib$get_current_invo_context(decc$$get_vfork_jmpbuf()) : -1)
49 #endif /* VMS */
51 #ifndef LIBRARY_PATH_ENV
52 #define LIBRARY_PATH_ENV "LIBRARY_PATH"
53 #endif
55 #define COLLECT
57 #include "collect2.h"
58 #include "demangle.h"
59 #include "obstack.h"
60 #include "intl.h"
61 #include "version.h"
63 /* On certain systems, we have code that works by scanning the object file
64 directly. But this code uses system-specific header files and library
65 functions, so turn it off in a cross-compiler. Likewise, the names of
66 the utilities are not correct for a cross-compiler; we have to hope that
67 cross-versions are in the proper directories. */
69 #ifdef CROSS_COMPILE
70 #undef OBJECT_FORMAT_COFF
71 #undef MD_EXEC_PREFIX
72 #undef REAL_LD_FILE_NAME
73 #undef REAL_NM_FILE_NAME
74 #undef REAL_STRIP_FILE_NAME
75 #endif
77 /* If we cannot use a special method, use the ordinary one:
78 run nm to find what symbols are present.
79 In a cross-compiler, this means you need a cross nm,
80 but that is not quite as unpleasant as special headers. */
82 #if !defined (OBJECT_FORMAT_COFF)
83 #define OBJECT_FORMAT_NONE
84 #endif
86 #ifdef OBJECT_FORMAT_COFF
88 #include <a.out.h>
89 #include <ar.h>
91 #ifdef UMAX
92 #include <sgs.h>
93 #endif
95 /* Many versions of ldfcn.h define these. */
96 #ifdef FREAD
97 #undef FREAD
98 #undef FWRITE
99 #endif
101 #include <ldfcn.h>
103 /* Some systems have an ISCOFF macro, but others do not. In some cases
104 the macro may be wrong. MY_ISCOFF is defined in tm.h files for machines
105 that either do not have an ISCOFF macro in /usr/include or for those
106 where it is wrong. */
108 #ifndef MY_ISCOFF
109 #define MY_ISCOFF(X) ISCOFF (X)
110 #endif
112 #endif /* OBJECT_FORMAT_COFF */
114 #ifdef OBJECT_FORMAT_NONE
116 /* Default flags to pass to nm. */
117 #ifndef NM_FLAGS
118 #define NM_FLAGS "-n"
119 #endif
121 #endif /* OBJECT_FORMAT_NONE */
123 /* Some systems use __main in a way incompatible with its use in gcc, in these
124 cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
125 give the same symbol without quotes for an alternative entry point. */
126 #ifndef NAME__MAIN
127 #define NAME__MAIN "__main"
128 #endif
130 /* This must match tree.h. */
131 #define DEFAULT_INIT_PRIORITY 65535
133 #ifndef COLLECT_SHARED_INIT_FUNC
134 #define COLLECT_SHARED_INIT_FUNC(STREAM, FUNC) \
135 fprintf ((STREAM), "void _GLOBAL__DI() {\n\t%s();\n}\n", (FUNC))
136 #endif
137 #ifndef COLLECT_SHARED_FINI_FUNC
138 #define COLLECT_SHARED_FINI_FUNC(STREAM, FUNC) \
139 fprintf ((STREAM), "void _GLOBAL__DD() {\n\t%s();\n}\n", (FUNC))
140 #endif
142 #ifdef LDD_SUFFIX
143 #define SCAN_LIBRARIES
144 #endif
146 #ifdef USE_COLLECT2
147 int do_collecting = 1;
148 #else
149 int do_collecting = 0;
150 #endif
152 /* Nonzero if we should suppress the automatic demangling of identifiers
153 in linker error messages. Set from COLLECT_NO_DEMANGLE. */
154 int no_demangle;
156 /* Linked lists of constructor and destructor names. */
158 struct id
160 struct id *next;
161 int sequence;
162 char name[1];
165 struct head
167 struct id *first;
168 struct id *last;
169 int number;
172 /* Enumeration giving which pass this is for scanning the program file. */
174 enum pass {
175 PASS_FIRST, /* without constructors */
176 PASS_OBJ, /* individual objects */
177 PASS_LIB, /* looking for shared libraries */
178 PASS_SECOND /* with constructors linked in */
181 int vflag; /* true if -v */
182 static int rflag; /* true if -r */
183 static int strip_flag; /* true if -s */
184 static const char *demangle_flag;
185 #ifdef COLLECT_EXPORT_LIST
186 static int export_flag; /* true if -bE */
187 static int aix64_flag; /* true if -b64 */
188 static int aixrtl_flag; /* true if -brtl */
189 #endif
191 int debug; /* true if -debug */
193 static int shared_obj; /* true if -shared */
195 static const char *c_file; /* <xxx>.c for constructor/destructor list. */
196 static const char *o_file; /* <xxx>.o for constructor/destructor list. */
197 #ifdef COLLECT_EXPORT_LIST
198 static const char *export_file; /* <xxx>.x for AIX export list. */
199 #endif
200 const char *ldout; /* File for ld errors. */
201 static const char *output_file; /* Output file for ld. */
202 static const char *nm_file_name; /* pathname of nm */
203 #ifdef LDD_SUFFIX
204 static const char *ldd_file_name; /* pathname of ldd (or equivalent) */
205 #endif
206 static const char *strip_file_name; /* pathname of strip */
207 const char *c_file_name; /* pathname of gcc */
208 static char *initname, *fininame; /* names of init and fini funcs */
210 static struct head constructors; /* list of constructors found */
211 static struct head destructors; /* list of destructors found */
212 #ifdef COLLECT_EXPORT_LIST
213 static struct head exports; /* list of exported symbols */
214 #endif
215 static struct head frame_tables; /* list of frame unwind info tables */
217 struct obstack temporary_obstack;
218 char * temporary_firstobj;
220 /* Holds the return value of pexecute and fork. */
221 int pid;
223 /* Structure to hold all the directories in which to search for files to
224 execute. */
226 struct prefix_list
228 const char *prefix; /* String to prepend to the path. */
229 struct prefix_list *next; /* Next in linked list. */
232 struct path_prefix
234 struct prefix_list *plist; /* List of prefixes to try */
235 int max_len; /* Max length of a prefix in PLIST */
236 const char *name; /* Name of this list (used in config stuff) */
239 #ifdef COLLECT_EXPORT_LIST
240 /* Lists to keep libraries to be scanned for global constructors/destructors. */
241 static struct head libs; /* list of libraries */
242 static struct path_prefix cmdline_lib_dirs; /* directories specified with -L */
243 static struct path_prefix libpath_lib_dirs; /* directories in LIBPATH */
244 static struct path_prefix *libpaths[3] = {&cmdline_lib_dirs,
245 &libpath_lib_dirs, NULL};
246 #endif
248 static void handler (int);
249 static int is_ctor_dtor (const char *);
250 static char *find_a_file (struct path_prefix *, const char *);
251 static void add_prefix (struct path_prefix *, const char *);
252 static void prefix_from_env (const char *, struct path_prefix *);
253 static void prefix_from_string (const char *, struct path_prefix *);
254 static void do_wait (const char *);
255 static void fork_execute (const char *, char **);
256 static void maybe_unlink (const char *);
257 static void add_to_list (struct head *, const char *);
258 static int extract_init_priority (const char *);
259 static void sort_ids (struct head *);
260 static void write_list (FILE *, const char *, struct id *);
261 #ifdef COLLECT_EXPORT_LIST
262 static void dump_list (FILE *, const char *, struct id *);
263 #endif
264 #if 0
265 static void dump_prefix_list (FILE *, const char *, struct prefix_list *);
266 #endif
267 static void write_list_with_asm (FILE *, const char *, struct id *);
268 static void write_c_file (FILE *, const char *);
269 static void write_c_file_stat (FILE *, const char *);
270 #ifndef LD_INIT_SWITCH
271 static void write_c_file_glob (FILE *, const char *);
272 #endif
273 static void scan_prog_file (const char *, enum pass);
274 #ifdef SCAN_LIBRARIES
275 static void scan_libraries (const char *);
276 #endif
277 #if LINK_ELIMINATE_DUPLICATE_LDIRECTORIES
278 static int is_in_args (const char *, const char **, const char **);
279 #endif
280 #ifdef COLLECT_EXPORT_LIST
281 #if 0
282 static int is_in_list (const char *, struct id *);
283 #endif
284 static void write_aix_file (FILE *, struct id *);
285 static char *resolve_lib_name (const char *);
286 #endif
287 static char *extract_string (const char **);
289 #ifndef HAVE_DUP2
290 static int
291 dup2 (int oldfd, int newfd)
293 int fdtmp[256];
294 int fdx = 0;
295 int fd;
297 if (oldfd == newfd)
298 return oldfd;
299 close (newfd);
300 while ((fd = dup (oldfd)) != newfd && fd >= 0) /* good enough for low fd's */
301 fdtmp[fdx++] = fd;
302 while (fdx > 0)
303 close (fdtmp[--fdx]);
305 return fd;
307 #endif /* ! HAVE_DUP2 */
309 /* Delete tempfiles and exit function. */
311 void
312 collect_exit (int status)
314 if (c_file != 0 && c_file[0])
315 maybe_unlink (c_file);
317 if (o_file != 0 && o_file[0])
318 maybe_unlink (o_file);
320 #ifdef COLLECT_EXPORT_LIST
321 if (export_file != 0 && export_file[0])
322 maybe_unlink (export_file);
323 #endif
325 if (ldout != 0 && ldout[0])
327 dump_file (ldout);
328 maybe_unlink (ldout);
331 if (status != 0 && output_file != 0 && output_file[0])
332 maybe_unlink (output_file);
334 exit (status);
338 /* Notify user of a non-error. */
339 void
340 notice (const char *msgid, ...)
342 va_list ap;
344 va_start (ap, msgid);
345 vfprintf (stderr, _(msgid), ap);
346 va_end (ap);
349 /* Die when sys call fails. */
351 void
352 fatal_perror (const char * msgid, ...)
354 int e = errno;
355 va_list ap;
357 va_start (ap, msgid);
358 fprintf (stderr, "collect2: ");
359 vfprintf (stderr, _(msgid), ap);
360 fprintf (stderr, ": %s\n", xstrerror (e));
361 va_end (ap);
363 collect_exit (FATAL_EXIT_CODE);
366 /* Just die. */
368 void
369 fatal (const char * msgid, ...)
371 va_list ap;
373 va_start (ap, msgid);
374 fprintf (stderr, "collect2: ");
375 vfprintf (stderr, _(msgid), ap);
376 fprintf (stderr, "\n");
377 va_end (ap);
379 collect_exit (FATAL_EXIT_CODE);
382 /* Write error message. */
384 void
385 error (const char * msgid, ...)
387 va_list ap;
389 va_start (ap, msgid);
390 fprintf (stderr, "collect2: ");
391 vfprintf (stderr, _(msgid), ap);
392 fprintf (stderr, "\n");
393 va_end(ap);
396 /* In case obstack is linked in, and abort is defined to fancy_abort,
397 provide a default entry. */
399 void
400 fancy_abort (const char *file, int line, const char *func)
402 fatal ("internal gcc abort in %s, at %s:%d", func, file, line);
405 static void
406 handler (int signo)
408 if (c_file != 0 && c_file[0])
409 maybe_unlink (c_file);
411 if (o_file != 0 && o_file[0])
412 maybe_unlink (o_file);
414 if (ldout != 0 && ldout[0])
415 maybe_unlink (ldout);
417 #ifdef COLLECT_EXPORT_LIST
418 if (export_file != 0 && export_file[0])
419 maybe_unlink (export_file);
420 #endif
422 signal (signo, SIG_DFL);
423 kill (getpid (), signo);
428 file_exists (const char *name)
430 return access (name, R_OK) == 0;
433 /* Parse a reasonable subset of shell quoting syntax. */
435 static char *
436 extract_string (const char **pp)
438 const char *p = *pp;
439 int backquote = 0;
440 int inside = 0;
442 for (;;)
444 char c = *p;
445 if (c == '\0')
446 break;
447 ++p;
448 if (backquote)
449 obstack_1grow (&temporary_obstack, c);
450 else if (! inside && c == ' ')
451 break;
452 else if (! inside && c == '\\')
453 backquote = 1;
454 else if (c == '\'')
455 inside = !inside;
456 else
457 obstack_1grow (&temporary_obstack, c);
460 obstack_1grow (&temporary_obstack, '\0');
461 *pp = p;
462 return obstack_finish (&temporary_obstack);
465 void
466 dump_file (const char *name)
468 FILE *stream = fopen (name, "r");
470 if (stream == 0)
471 return;
472 while (1)
474 int c;
475 while (c = getc (stream),
476 c != EOF && (ISIDNUM (c) || c == '$' || c == '.'))
477 obstack_1grow (&temporary_obstack, c);
478 if (obstack_object_size (&temporary_obstack) > 0)
480 const char *word, *p;
481 char *result;
482 obstack_1grow (&temporary_obstack, '\0');
483 word = obstack_finish (&temporary_obstack);
485 if (*word == '.')
486 ++word, putc ('.', stderr);
487 p = word;
488 if (!strncmp (p, USER_LABEL_PREFIX, strlen (USER_LABEL_PREFIX)))
489 p += strlen (USER_LABEL_PREFIX);
491 #ifdef HAVE_LD_DEMANGLE
492 result = 0;
493 #else
494 if (no_demangle)
495 result = 0;
496 else
497 result = cplus_demangle (p, DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE);
498 #endif
500 if (result)
502 int diff;
503 fputs (result, stderr);
505 diff = strlen (word) - strlen (result);
506 while (diff > 0 && c == ' ')
507 --diff, putc (' ', stderr);
508 while (diff < 0 && c == ' ')
509 ++diff, c = getc (stream);
511 free (result);
513 else
514 fputs (word, stderr);
516 fflush (stderr);
517 obstack_free (&temporary_obstack, temporary_firstobj);
519 if (c == EOF)
520 break;
521 putc (c, stderr);
523 fclose (stream);
526 /* Decide whether the given symbol is: a constructor (1), a destructor
527 (2), a routine in a shared object that calls all the constructors
528 (3) or destructors (4), a DWARF exception-handling table (5), or
529 nothing special (0). */
531 static int
532 is_ctor_dtor (const char *s)
534 struct names { const char *const name; const int len; const int ret;
535 const int two_underscores; };
537 const struct names *p;
538 int ch;
539 const char *orig_s = s;
541 static const struct names special[] = {
542 #ifndef NO_DOLLAR_IN_LABEL
543 { "GLOBAL__I$", sizeof ("GLOBAL__I$")-1, 1, 0 },
544 { "GLOBAL__D$", sizeof ("GLOBAL__D$")-1, 2, 0 },
545 #else
546 #ifndef NO_DOT_IN_LABEL
547 { "GLOBAL__I.", sizeof ("GLOBAL__I.")-1, 1, 0 },
548 { "GLOBAL__D.", sizeof ("GLOBAL__D.")-1, 2, 0 },
549 #endif /* NO_DOT_IN_LABEL */
550 #endif /* NO_DOLLAR_IN_LABEL */
551 { "GLOBAL__I_", sizeof ("GLOBAL__I_")-1, 1, 0 },
552 { "GLOBAL__D_", sizeof ("GLOBAL__D_")-1, 2, 0 },
553 { "GLOBAL__F_", sizeof ("GLOBAL__F_")-1, 5, 0 },
554 { "GLOBAL__FI_", sizeof ("GLOBAL__FI_")-1, 3, 0 },
555 { "GLOBAL__FD_", sizeof ("GLOBAL__FD_")-1, 4, 0 },
556 { NULL, 0, 0, 0 }
559 while ((ch = *s) == '_')
560 ++s;
562 if (s == orig_s)
563 return 0;
565 for (p = &special[0]; p->len > 0; p++)
567 if (ch == p->name[0]
568 && (!p->two_underscores || ((s - orig_s) >= 2))
569 && strncmp(s, p->name, p->len) == 0)
571 return p->ret;
574 return 0;
577 /* We maintain two prefix lists: one from COMPILER_PATH environment variable
578 and one from the PATH variable. */
580 static struct path_prefix cpath, path;
582 #ifdef CROSS_COMPILE
583 /* This is the name of the target machine. We use it to form the name
584 of the files to execute. */
586 static const char *const target_machine = TARGET_MACHINE;
587 #endif
589 /* Search for NAME using prefix list PPREFIX. We only look for executable
590 files.
592 Return 0 if not found, otherwise return its name, allocated with malloc. */
594 static char *
595 find_a_file (struct path_prefix *pprefix, const char *name)
597 char *temp;
598 struct prefix_list *pl;
599 int len = pprefix->max_len + strlen (name) + 1;
601 if (debug)
602 fprintf (stderr, "Looking for '%s'\n", name);
604 #ifdef HOST_EXECUTABLE_SUFFIX
605 len += strlen (HOST_EXECUTABLE_SUFFIX);
606 #endif
608 temp = xmalloc (len);
610 /* Determine the filename to execute (special case for absolute paths). */
612 if (*name == '/'
613 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
614 || (*name && name[1] == ':')
615 #endif
618 if (access (name, X_OK) == 0)
620 strcpy (temp, name);
622 if (debug)
623 fprintf (stderr, " - found: absolute path\n");
625 return temp;
628 #ifdef HOST_EXECUTABLE_SUFFIX
629 /* Some systems have a suffix for executable files.
630 So try appending that. */
631 strcpy (temp, name);
632 strcat (temp, HOST_EXECUTABLE_SUFFIX);
634 if (access (temp, X_OK) == 0)
635 return temp;
636 #endif
638 if (debug)
639 fprintf (stderr, " - failed to locate using absolute path\n");
641 else
642 for (pl = pprefix->plist; pl; pl = pl->next)
644 struct stat st;
646 strcpy (temp, pl->prefix);
647 strcat (temp, name);
649 if (stat (temp, &st) >= 0
650 && ! S_ISDIR (st.st_mode)
651 && access (temp, X_OK) == 0)
652 return temp;
654 #ifdef HOST_EXECUTABLE_SUFFIX
655 /* Some systems have a suffix for executable files.
656 So try appending that. */
657 strcat (temp, HOST_EXECUTABLE_SUFFIX);
659 if (stat (temp, &st) >= 0
660 && ! S_ISDIR (st.st_mode)
661 && access (temp, X_OK) == 0)
662 return temp;
663 #endif
666 if (debug && pprefix->plist == NULL)
667 fprintf (stderr, " - failed: no entries in prefix list\n");
669 free (temp);
670 return 0;
673 /* Add an entry for PREFIX to prefix list PPREFIX. */
675 static void
676 add_prefix (struct path_prefix *pprefix, const char *prefix)
678 struct prefix_list *pl, **prev;
679 int len;
681 if (pprefix->plist)
683 for (pl = pprefix->plist; pl->next; pl = pl->next)
685 prev = &pl->next;
687 else
688 prev = &pprefix->plist;
690 /* Keep track of the longest prefix. */
692 len = strlen (prefix);
693 if (len > pprefix->max_len)
694 pprefix->max_len = len;
696 pl = xmalloc (sizeof (struct prefix_list));
697 pl->prefix = xstrdup (prefix);
699 if (*prev)
700 pl->next = *prev;
701 else
702 pl->next = (struct prefix_list *) 0;
703 *prev = pl;
706 /* Take the value of the environment variable ENV, break it into a path, and
707 add of the entries to PPREFIX. */
709 static void
710 prefix_from_env (const char *env, struct path_prefix *pprefix)
712 const char *p;
713 GET_ENVIRONMENT (p, env);
715 if (p)
716 prefix_from_string (p, pprefix);
719 static void
720 prefix_from_string (const char *p, struct path_prefix *pprefix)
722 const char *startp, *endp;
723 char *nstore = xmalloc (strlen (p) + 3);
725 if (debug)
726 fprintf (stderr, "Convert string '%s' into prefixes, separator = '%c'\n", p, PATH_SEPARATOR);
728 startp = endp = p;
729 while (1)
731 if (*endp == PATH_SEPARATOR || *endp == 0)
733 strncpy (nstore, startp, endp-startp);
734 if (endp == startp)
736 strcpy (nstore, "./");
738 else if (! IS_DIR_SEPARATOR (endp[-1]))
740 nstore[endp-startp] = DIR_SEPARATOR;
741 nstore[endp-startp+1] = 0;
743 else
744 nstore[endp-startp] = 0;
746 if (debug)
747 fprintf (stderr, " - add prefix: %s\n", nstore);
749 add_prefix (pprefix, nstore);
750 if (*endp == 0)
751 break;
752 endp = startp = endp + 1;
754 else
755 endp++;
759 /* Main program. */
762 main (int argc, char **argv)
764 static const char *const ld_suffix = "ld";
765 static const char *const real_ld_suffix = "real-ld";
766 static const char *const collect_ld_suffix = "collect-ld";
767 static const char *const nm_suffix = "nm";
768 static const char *const gnm_suffix = "gnm";
769 #ifdef LDD_SUFFIX
770 static const char *const ldd_suffix = LDD_SUFFIX;
771 #endif
772 static const char *const strip_suffix = "strip";
773 static const char *const gstrip_suffix = "gstrip";
775 #ifdef CROSS_COMPILE
776 /* If we look for a program in the compiler directories, we just use
777 the short name, since these directories are already system-specific.
778 But it we look for a program in the system directories, we need to
779 qualify the program name with the target machine. */
781 const char *const full_ld_suffix =
782 concat(target_machine, "-", ld_suffix, NULL);
783 const char *const full_nm_suffix =
784 concat (target_machine, "-", nm_suffix, NULL);
785 const char *const full_gnm_suffix =
786 concat (target_machine, "-", gnm_suffix, NULL);
787 #ifdef LDD_SUFFIX
788 const char *const full_ldd_suffix =
789 concat (target_machine, "-", ldd_suffix, NULL);
790 #endif
791 const char *const full_strip_suffix =
792 concat (target_machine, "-", strip_suffix, NULL);
793 const char *const full_gstrip_suffix =
794 concat (target_machine, "-", gstrip_suffix, NULL);
795 #else
796 const char *const full_ld_suffix = ld_suffix;
797 const char *const full_nm_suffix = nm_suffix;
798 const char *const full_gnm_suffix = gnm_suffix;
799 #ifdef LDD_SUFFIX
800 const char *const full_ldd_suffix = ldd_suffix;
801 #endif
802 const char *const full_strip_suffix = strip_suffix;
803 const char *const full_gstrip_suffix = gstrip_suffix;
804 #endif /* CROSS_COMPILE */
806 const char *arg;
807 FILE *outf;
808 #ifdef COLLECT_EXPORT_LIST
809 FILE *exportf;
810 #endif
811 const char *ld_file_name;
812 const char *p;
813 char **c_argv;
814 const char **c_ptr;
815 char **ld1_argv;
816 const char **ld1;
817 char **ld2_argv;
818 const char **ld2;
819 char **object_lst;
820 const char **object;
821 int first_file;
822 int num_c_args = argc+9;
824 no_demangle = !! getenv ("COLLECT_NO_DEMANGLE");
826 /* Suppress demangling by the real linker, which may be broken. */
827 putenv (xstrdup ("COLLECT_NO_DEMANGLE="));
829 #if defined (COLLECT2_HOST_INITIALIZATION)
830 /* Perform system dependent initialization, if necessary. */
831 COLLECT2_HOST_INITIALIZATION;
832 #endif
834 #ifdef SIGCHLD
835 /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
836 receive the signal. A different setting is inheritable */
837 signal (SIGCHLD, SIG_DFL);
838 #endif
840 gcc_init_libintl ();
842 /* Do not invoke xcalloc before this point, since locale needs to be
843 set first, in case a diagnostic is issued. */
845 ld1 = (const char **)(ld1_argv = xcalloc(sizeof (char *), argc+4));
846 ld2 = (const char **)(ld2_argv = xcalloc(sizeof (char *), argc+11));
847 object = (const char **)(object_lst = xcalloc(sizeof (char *), argc));
849 #ifdef DEBUG
850 debug = 1;
851 #endif
853 /* Parse command line early for instances of -debug. This allows
854 the debug flag to be set before functions like find_a_file()
855 are called. */
857 int i;
859 for (i = 1; argv[i] != NULL; i ++)
861 if (! strcmp (argv[i], "-debug"))
862 debug = 1;
864 vflag = debug;
867 #ifndef DEFAULT_A_OUT_NAME
868 output_file = "a.out";
869 #else
870 output_file = DEFAULT_A_OUT_NAME;
871 #endif
873 obstack_begin (&temporary_obstack, 0);
874 temporary_firstobj = obstack_alloc (&temporary_obstack, 0);
876 #ifndef HAVE_LD_DEMANGLE
877 current_demangling_style = auto_demangling;
878 #endif
879 p = getenv ("COLLECT_GCC_OPTIONS");
880 while (p && *p)
882 const char *q = extract_string (&p);
883 if (*q == '-' && (q[1] == 'm' || q[1] == 'f'))
884 num_c_args++;
886 obstack_free (&temporary_obstack, temporary_firstobj);
888 /* -fno-profile-arcs -fno-test-coverage -fno-branch-probabilities
889 -fno-exceptions -w */
890 num_c_args += 5;
892 c_ptr = (const char **) (c_argv = xcalloc (sizeof (char *), num_c_args));
894 if (argc < 2)
895 fatal ("no arguments");
897 #ifdef SIGQUIT
898 if (signal (SIGQUIT, SIG_IGN) != SIG_IGN)
899 signal (SIGQUIT, handler);
900 #endif
901 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
902 signal (SIGINT, handler);
903 #ifdef SIGALRM
904 if (signal (SIGALRM, SIG_IGN) != SIG_IGN)
905 signal (SIGALRM, handler);
906 #endif
907 #ifdef SIGHUP
908 if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
909 signal (SIGHUP, handler);
910 #endif
911 if (signal (SIGSEGV, SIG_IGN) != SIG_IGN)
912 signal (SIGSEGV, handler);
913 #ifdef SIGBUS
914 if (signal (SIGBUS, SIG_IGN) != SIG_IGN)
915 signal (SIGBUS, handler);
916 #endif
918 /* Extract COMPILER_PATH and PATH into our prefix list. */
919 prefix_from_env ("COMPILER_PATH", &cpath);
920 prefix_from_env ("PATH", &path);
922 /* Try to discover a valid linker/nm/strip to use. */
924 /* Maybe we know the right file to use (if not cross). */
925 ld_file_name = 0;
926 #ifdef DEFAULT_LINKER
927 if (access (DEFAULT_LINKER, X_OK) == 0)
928 ld_file_name = DEFAULT_LINKER;
929 if (ld_file_name == 0)
930 #endif
931 #ifdef REAL_LD_FILE_NAME
932 ld_file_name = find_a_file (&path, REAL_LD_FILE_NAME);
933 if (ld_file_name == 0)
934 #endif
935 /* Search the (target-specific) compiler dirs for ld'. */
936 ld_file_name = find_a_file (&cpath, real_ld_suffix);
937 /* Likewise for `collect-ld'. */
938 if (ld_file_name == 0)
939 ld_file_name = find_a_file (&cpath, collect_ld_suffix);
940 /* Search the compiler directories for `ld'. We have protection against
941 recursive calls in find_a_file. */
942 if (ld_file_name == 0)
943 ld_file_name = find_a_file (&cpath, ld_suffix);
944 /* Search the ordinary system bin directories
945 for `ld' (if native linking) or `TARGET-ld' (if cross). */
946 if (ld_file_name == 0)
947 ld_file_name = find_a_file (&path, full_ld_suffix);
949 #ifdef REAL_NM_FILE_NAME
950 nm_file_name = find_a_file (&path, REAL_NM_FILE_NAME);
951 if (nm_file_name == 0)
952 #endif
953 nm_file_name = find_a_file (&cpath, gnm_suffix);
954 if (nm_file_name == 0)
955 nm_file_name = find_a_file (&path, full_gnm_suffix);
956 if (nm_file_name == 0)
957 nm_file_name = find_a_file (&cpath, nm_suffix);
958 if (nm_file_name == 0)
959 nm_file_name = find_a_file (&path, full_nm_suffix);
961 #ifdef LDD_SUFFIX
962 ldd_file_name = find_a_file (&cpath, ldd_suffix);
963 if (ldd_file_name == 0)
964 ldd_file_name = find_a_file (&path, full_ldd_suffix);
965 #endif
967 #ifdef REAL_STRIP_FILE_NAME
968 strip_file_name = find_a_file (&path, REAL_STRIP_FILE_NAME);
969 if (strip_file_name == 0)
970 #endif
971 strip_file_name = find_a_file (&cpath, gstrip_suffix);
972 if (strip_file_name == 0)
973 strip_file_name = find_a_file (&path, full_gstrip_suffix);
974 if (strip_file_name == 0)
975 strip_file_name = find_a_file (&cpath, strip_suffix);
976 if (strip_file_name == 0)
977 strip_file_name = find_a_file (&path, full_strip_suffix);
979 /* Determine the full path name of the C compiler to use. */
980 c_file_name = getenv ("COLLECT_GCC");
981 if (c_file_name == 0)
983 #ifdef CROSS_COMPILE
984 c_file_name = concat (target_machine, "-gcc", NULL);
985 #else
986 c_file_name = "gcc";
987 #endif
990 p = find_a_file (&cpath, c_file_name);
992 /* Here it should be safe to use the system search path since we should have
993 already qualified the name of the compiler when it is needed. */
994 if (p == 0)
995 p = find_a_file (&path, c_file_name);
997 if (p)
998 c_file_name = p;
1000 *ld1++ = *ld2++ = ld_file_name;
1002 /* Make temp file names. */
1003 c_file = make_temp_file (".c");
1004 o_file = make_temp_file (".o");
1005 #ifdef COLLECT_EXPORT_LIST
1006 export_file = make_temp_file (".x");
1007 #endif
1008 ldout = make_temp_file (".ld");
1009 *c_ptr++ = c_file_name;
1010 *c_ptr++ = "-x";
1011 *c_ptr++ = "c";
1012 *c_ptr++ = "-c";
1013 *c_ptr++ = "-o";
1014 *c_ptr++ = o_file;
1016 #ifdef COLLECT_EXPORT_LIST
1017 /* Generate a list of directories from LIBPATH. */
1018 prefix_from_env ("LIBPATH", &libpath_lib_dirs);
1019 /* Add to this list also two standard directories where
1020 AIX loader always searches for libraries. */
1021 add_prefix (&libpath_lib_dirs, "/lib");
1022 add_prefix (&libpath_lib_dirs, "/usr/lib");
1023 #endif
1025 /* Get any options that the upper GCC wants to pass to the sub-GCC.
1027 AIX support needs to know if -shared has been specified before
1028 parsing commandline arguments. */
1030 p = getenv ("COLLECT_GCC_OPTIONS");
1031 while (p && *p)
1033 const char *q = extract_string (&p);
1034 if (*q == '-' && (q[1] == 'm' || q[1] == 'f'))
1035 *c_ptr++ = xstrdup (q);
1036 if (strcmp (q, "-EL") == 0 || strcmp (q, "-EB") == 0)
1037 *c_ptr++ = xstrdup (q);
1038 if (strcmp (q, "-shared") == 0)
1039 shared_obj = 1;
1040 if (*q == '-' && q[1] == 'B')
1042 *c_ptr++ = xstrdup (q);
1043 if (q[2] == 0)
1045 q = extract_string (&p);
1046 *c_ptr++ = xstrdup (q);
1050 obstack_free (&temporary_obstack, temporary_firstobj);
1051 *c_ptr++ = "-fno-profile-arcs";
1052 *c_ptr++ = "-fno-test-coverage";
1053 *c_ptr++ = "-fno-branch-probabilities";
1054 *c_ptr++ = "-fno-exceptions";
1055 *c_ptr++ = "-w";
1057 /* !!! When GCC calls collect2,
1058 it does not know whether it is calling collect2 or ld.
1059 So collect2 cannot meaningfully understand any options
1060 except those ld understands.
1061 If you propose to make GCC pass some other option,
1062 just imagine what will happen if ld is really ld!!! */
1064 /* Parse arguments. Remember output file spec, pass the rest to ld. */
1065 /* After the first file, put in the c++ rt0. */
1067 first_file = 1;
1068 #ifdef HAVE_LD_DEMANGLE
1069 if (!demangle_flag && !no_demangle)
1070 demangle_flag = "--demangle";
1071 if (demangle_flag)
1072 *ld1++ = *ld2++ = demangle_flag;
1073 #endif
1074 while ((arg = *++argv) != (char *) 0)
1076 *ld1++ = *ld2++ = arg;
1078 if (arg[0] == '-')
1080 switch (arg[1])
1082 #ifdef COLLECT_EXPORT_LIST
1083 /* We want to disable automatic exports on AIX when user
1084 explicitly puts an export list in command line */
1085 case 'b':
1086 if (arg[2] == 'E' || strncmp (&arg[2], "export", 6) == 0)
1087 export_flag = 1;
1088 else if (arg[2] == '6' && arg[3] == '4')
1089 aix64_flag = 1;
1090 else if (arg[2] == 'r' && arg[3] == 't' && arg[4] == 'l')
1091 aixrtl_flag = 1;
1092 break;
1093 #endif
1095 case 'd':
1096 if (!strcmp (arg, "-debug"))
1098 /* Already parsed. */
1099 ld1--;
1100 ld2--;
1102 break;
1104 case 'l':
1105 if (first_file)
1107 /* place o_file BEFORE this argument! */
1108 first_file = 0;
1109 ld2--;
1110 *ld2++ = o_file;
1111 *ld2++ = arg;
1113 #ifdef COLLECT_EXPORT_LIST
1115 /* Resolving full library name. */
1116 const char *s = resolve_lib_name (arg+2);
1118 /* Saving a full library name. */
1119 add_to_list (&libs, s);
1121 #endif
1122 break;
1124 #ifdef COLLECT_EXPORT_LIST
1125 /* Saving directories where to search for libraries. */
1126 case 'L':
1127 add_prefix (&cmdline_lib_dirs, arg+2);
1128 break;
1129 #else
1130 #if LINK_ELIMINATE_DUPLICATE_LDIRECTORIES
1131 case 'L':
1132 if (is_in_args (arg, (const char **) ld1_argv, ld1-1))
1133 --ld1;
1134 break;
1135 #endif /* LINK_ELIMINATE_DUPLICATE_LDIRECTORIES */
1136 #endif
1138 case 'o':
1139 if (arg[2] == '\0')
1140 output_file = *ld1++ = *ld2++ = *++argv;
1141 else if (1
1142 #ifdef SWITCHES_NEED_SPACES
1143 && ! strchr (SWITCHES_NEED_SPACES, arg[1])
1144 #endif
1147 output_file = &arg[2];
1148 break;
1150 case 'r':
1151 if (arg[2] == '\0')
1152 rflag = 1;
1153 break;
1155 case 's':
1156 if (arg[2] == '\0' && do_collecting)
1158 /* We must strip after the nm run, otherwise C++ linking
1159 will not work. Thus we strip in the second ld run, or
1160 else with strip if there is no second ld run. */
1161 strip_flag = 1;
1162 ld1--;
1164 break;
1166 case 'v':
1167 if (arg[2] == '\0')
1168 vflag = 1;
1169 break;
1171 case '-':
1172 if (strcmp (arg, "--no-demangle") == 0)
1174 demangle_flag = arg;
1175 no_demangle = 1;
1176 ld1--;
1177 ld2--;
1179 else if (strncmp (arg, "--demangle", 10) == 0)
1181 demangle_flag = arg;
1182 no_demangle = 0;
1183 #ifndef HAVE_LD_DEMANGLE
1184 if (arg[10] == '=')
1186 enum demangling_styles style
1187 = cplus_demangle_name_to_style (arg+11);
1188 if (style == unknown_demangling)
1189 error ("unknown demangling style '%s'", arg+11);
1190 else
1191 current_demangling_style = style;
1193 #endif
1194 ld1--;
1195 ld2--;
1197 break;
1200 else if ((p = strrchr (arg, '.')) != (char *) 0
1201 && (strcmp (p, ".o") == 0 || strcmp (p, ".a") == 0
1202 || strcmp (p, ".so") == 0 || strcmp (p, ".lo") == 0
1203 || strcmp (p, ".obj") == 0))
1205 if (first_file)
1207 first_file = 0;
1208 if (p[1] == 'o')
1209 *ld2++ = o_file;
1210 else
1212 /* place o_file BEFORE this argument! */
1213 ld2--;
1214 *ld2++ = o_file;
1215 *ld2++ = arg;
1218 if (p[1] == 'o' || p[1] == 'l')
1219 *object++ = arg;
1220 #ifdef COLLECT_EXPORT_LIST
1221 /* libraries can be specified directly, i.e. without -l flag. */
1222 else
1224 /* Saving a full library name. */
1225 add_to_list (&libs, arg);
1227 #endif
1231 #ifdef COLLECT_EXPORT_LIST
1232 /* This is added only for debugging purposes. */
1233 if (debug)
1235 fprintf (stderr, "List of libraries:\n");
1236 dump_list (stderr, "\t", libs.first);
1239 /* The AIX linker will discard static constructors in object files if
1240 nothing else in the file is referenced, so look at them first. */
1242 const char **export_object_lst = (const char **)object_lst;
1244 while (export_object_lst < object)
1245 scan_prog_file (*export_object_lst++, PASS_OBJ);
1248 struct id *list = libs.first;
1250 for (; list; list = list->next)
1251 scan_prog_file (list->name, PASS_FIRST);
1254 if (exports.first)
1256 char *buf = concat ("-bE:", export_file, NULL);
1258 *ld1++ = buf;
1259 *ld2++ = buf;
1261 exportf = fopen (export_file, "w");
1262 if (exportf == (FILE *) 0)
1263 fatal_perror ("fopen %s", export_file);
1264 write_aix_file (exportf, exports.first);
1265 if (fclose (exportf))
1266 fatal_perror ("fclose %s", export_file);
1268 #endif
1270 *c_ptr++ = c_file;
1271 *c_ptr = *ld1 = *object = (char *) 0;
1273 if (vflag)
1275 notice ("collect2 version %s", version_string);
1276 #ifdef TARGET_VERSION
1277 TARGET_VERSION;
1278 #endif
1279 fprintf (stderr, "\n");
1282 if (debug)
1284 const char *ptr;
1285 fprintf (stderr, "ld_file_name = %s\n",
1286 (ld_file_name ? ld_file_name : "not found"));
1287 fprintf (stderr, "c_file_name = %s\n",
1288 (c_file_name ? c_file_name : "not found"));
1289 fprintf (stderr, "nm_file_name = %s\n",
1290 (nm_file_name ? nm_file_name : "not found"));
1291 #ifdef LDD_SUFFIX
1292 fprintf (stderr, "ldd_file_name = %s\n",
1293 (ldd_file_name ? ldd_file_name : "not found"));
1294 #endif
1295 fprintf (stderr, "strip_file_name = %s\n",
1296 (strip_file_name ? strip_file_name : "not found"));
1297 fprintf (stderr, "c_file = %s\n",
1298 (c_file ? c_file : "not found"));
1299 fprintf (stderr, "o_file = %s\n",
1300 (o_file ? o_file : "not found"));
1302 ptr = getenv ("COLLECT_GCC_OPTIONS");
1303 if (ptr)
1304 fprintf (stderr, "COLLECT_GCC_OPTIONS = %s\n", ptr);
1306 ptr = getenv ("COLLECT_GCC");
1307 if (ptr)
1308 fprintf (stderr, "COLLECT_GCC = %s\n", ptr);
1310 ptr = getenv ("COMPILER_PATH");
1311 if (ptr)
1312 fprintf (stderr, "COMPILER_PATH = %s\n", ptr);
1314 ptr = getenv (LIBRARY_PATH_ENV);
1315 if (ptr)
1316 fprintf (stderr, "%-20s= %s\n", LIBRARY_PATH_ENV, ptr);
1318 fprintf (stderr, "\n");
1321 /* Load the program, searching all libraries and attempting to provide
1322 undefined symbols from repository information. */
1324 /* On AIX we do this later. */
1325 #ifndef COLLECT_EXPORT_LIST
1326 do_tlink (ld1_argv, object_lst);
1327 #endif
1329 /* If -r or they will be run via some other method, do not build the
1330 constructor or destructor list, just return now. */
1331 if (rflag
1332 #ifndef COLLECT_EXPORT_LIST
1333 || ! do_collecting
1334 #endif
1337 #ifdef COLLECT_EXPORT_LIST
1338 /* Do the link we avoided above if we are exiting. */
1339 do_tlink (ld1_argv, object_lst);
1341 /* But make sure we delete the export file we may have created. */
1342 if (export_file != 0 && export_file[0])
1343 maybe_unlink (export_file);
1344 #endif
1345 maybe_unlink (c_file);
1346 maybe_unlink (o_file);
1347 return 0;
1350 /* Examine the namelist with nm and search it for static constructors
1351 and destructors to call.
1352 Write the constructor and destructor tables to a .s file and reload. */
1354 /* On AIX we already scanned for global constructors/destructors. */
1355 #ifndef COLLECT_EXPORT_LIST
1356 scan_prog_file (output_file, PASS_FIRST);
1357 #endif
1359 #ifdef SCAN_LIBRARIES
1360 scan_libraries (output_file);
1361 #endif
1363 if (debug)
1365 notice ("%d constructor(s) found\n", constructors.number);
1366 notice ("%d destructor(s) found\n", destructors.number);
1367 notice ("%d frame table(s) found\n", frame_tables.number);
1370 if (constructors.number == 0 && destructors.number == 0
1371 && frame_tables.number == 0
1372 #if defined (SCAN_LIBRARIES) || defined (COLLECT_EXPORT_LIST)
1373 /* If we will be running these functions ourselves, we want to emit
1374 stubs into the shared library so that we do not have to relink
1375 dependent programs when we add static objects. */
1376 && ! shared_obj
1377 #endif
1380 #ifdef COLLECT_EXPORT_LIST
1381 /* Do tlink without additional code generation. */
1382 do_tlink (ld1_argv, object_lst);
1383 #endif
1384 /* Strip now if it was requested on the command line. */
1385 if (strip_flag)
1387 char **real_strip_argv = xcalloc (sizeof (char *), 3);
1388 const char ** strip_argv = (const char **) real_strip_argv;
1390 strip_argv[0] = strip_file_name;
1391 strip_argv[1] = output_file;
1392 strip_argv[2] = (char *) 0;
1393 fork_execute ("strip", real_strip_argv);
1396 #ifdef COLLECT_EXPORT_LIST
1397 maybe_unlink (export_file);
1398 #endif
1399 maybe_unlink (c_file);
1400 maybe_unlink (o_file);
1401 return 0;
1404 /* Sort ctor and dtor lists by priority. */
1405 sort_ids (&constructors);
1406 sort_ids (&destructors);
1408 maybe_unlink(output_file);
1409 outf = fopen (c_file, "w");
1410 if (outf == (FILE *) 0)
1411 fatal_perror ("fopen %s", c_file);
1413 write_c_file (outf, c_file);
1415 if (fclose (outf))
1416 fatal_perror ("fclose %s", c_file);
1418 /* Tell the linker that we have initializer and finalizer functions. */
1419 #ifdef LD_INIT_SWITCH
1420 #ifdef COLLECT_EXPORT_LIST
1421 *ld2++ = concat (LD_INIT_SWITCH, ":", initname, ":", fininame, NULL);
1422 #else
1423 *ld2++ = LD_INIT_SWITCH;
1424 *ld2++ = initname;
1425 *ld2++ = LD_FINI_SWITCH;
1426 *ld2++ = fininame;
1427 #endif
1428 #endif
1430 #ifdef COLLECT_EXPORT_LIST
1431 if (shared_obj)
1433 /* If we did not add export flag to link arguments before, add it to
1434 second link phase now. No new exports should have been added. */
1435 if (! exports.first)
1436 *ld2++ = concat ("-bE:", export_file, NULL);
1438 #ifndef LD_INIT_SWITCH
1439 add_to_list (&exports, initname);
1440 add_to_list (&exports, fininame);
1441 add_to_list (&exports, "_GLOBAL__DI");
1442 add_to_list (&exports, "_GLOBAL__DD");
1443 #endif
1444 exportf = fopen (export_file, "w");
1445 if (exportf == (FILE *) 0)
1446 fatal_perror ("fopen %s", export_file);
1447 write_aix_file (exportf, exports.first);
1448 if (fclose (exportf))
1449 fatal_perror ("fclose %s", export_file);
1451 #endif
1453 /* End of arguments to second link phase. */
1454 *ld2 = (char*) 0;
1456 if (debug)
1458 fprintf (stderr, "\n========== output_file = %s, c_file = %s\n",
1459 output_file, c_file);
1460 write_c_file (stderr, "stderr");
1461 fprintf (stderr, "========== end of c_file\n\n");
1462 #ifdef COLLECT_EXPORT_LIST
1463 fprintf (stderr, "\n========== export_file = %s\n", export_file);
1464 write_aix_file (stderr, exports.first);
1465 fprintf (stderr, "========== end of export_file\n\n");
1466 #endif
1469 /* Assemble the constructor and destructor tables.
1470 Link the tables in with the rest of the program. */
1472 fork_execute ("gcc", c_argv);
1473 #ifdef COLLECT_EXPORT_LIST
1474 /* On AIX we must call tlink because of possible templates resolution. */
1475 do_tlink (ld2_argv, object_lst);
1476 #else
1477 /* Otherwise, simply call ld because tlink is already done. */
1478 fork_execute ("ld", ld2_argv);
1480 /* Let scan_prog_file do any final mods (OSF/rose needs this for
1481 constructors/destructors in shared libraries. */
1482 scan_prog_file (output_file, PASS_SECOND);
1483 #endif
1485 maybe_unlink (c_file);
1486 maybe_unlink (o_file);
1488 #ifdef COLLECT_EXPORT_LIST
1489 maybe_unlink (export_file);
1490 #endif
1492 return 0;
1496 /* Wait for a process to finish, and exit if a nonzero status is found. */
1499 collect_wait (const char *prog)
1501 int status;
1503 pwait (pid, &status, 0);
1504 if (status)
1506 if (WIFSIGNALED (status))
1508 int sig = WTERMSIG (status);
1509 error ("%s terminated with signal %d [%s]%s",
1510 prog, sig, strsignal(sig),
1511 WCOREDUMP(status) ? ", core dumped" : "");
1512 collect_exit (FATAL_EXIT_CODE);
1515 if (WIFEXITED (status))
1516 return WEXITSTATUS (status);
1518 return 0;
1521 static void
1522 do_wait (const char *prog)
1524 int ret = collect_wait (prog);
1525 if (ret != 0)
1527 error ("%s returned %d exit status", prog, ret);
1528 collect_exit (ret);
1533 /* Execute a program, and wait for the reply. */
1535 void
1536 collect_execute (const char *prog, char **argv, const char *redir)
1538 char *errmsg_fmt;
1539 char *errmsg_arg;
1540 int redir_handle = -1;
1541 int stdout_save = -1;
1542 int stderr_save = -1;
1544 if (vflag || debug)
1546 char **p_argv;
1547 const char *str;
1549 if (argv[0])
1550 fprintf (stderr, "%s", argv[0]);
1551 else
1552 notice ("[cannot find %s]", prog);
1554 for (p_argv = &argv[1]; (str = *p_argv) != (char *) 0; p_argv++)
1555 fprintf (stderr, " %s", str);
1557 fprintf (stderr, "\n");
1560 fflush (stdout);
1561 fflush (stderr);
1563 /* If we cannot find a program we need, complain error. Do this here
1564 since we might not end up needing something that we could not find. */
1566 if (argv[0] == 0)
1567 fatal ("cannot find '%s'", prog);
1569 if (redir)
1571 /* Open response file. */
1572 redir_handle = open (redir, O_WRONLY | O_TRUNC | O_CREAT);
1574 /* Duplicate the stdout and stderr file handles
1575 so they can be restored later. */
1576 stdout_save = dup (STDOUT_FILENO);
1577 if (stdout_save == -1)
1578 fatal_perror ("redirecting stdout: %s", redir);
1579 stderr_save = dup (STDERR_FILENO);
1580 if (stderr_save == -1)
1581 fatal_perror ("redirecting stdout: %s", redir);
1583 /* Redirect stdout & stderr to our response file. */
1584 dup2 (redir_handle, STDOUT_FILENO);
1585 dup2 (redir_handle, STDERR_FILENO);
1588 pid = pexecute (argv[0], argv, argv[0], NULL, &errmsg_fmt, &errmsg_arg,
1589 (PEXECUTE_FIRST | PEXECUTE_LAST | PEXECUTE_SEARCH));
1591 if (redir)
1593 /* Restore stdout and stderr to their previous settings. */
1594 dup2 (stdout_save, STDOUT_FILENO);
1595 dup2 (stderr_save, STDERR_FILENO);
1597 /* Close response file. */
1598 close (redir_handle);
1601 if (pid == -1)
1602 fatal_perror (errmsg_fmt, errmsg_arg);
1605 static void
1606 fork_execute (const char *prog, char **argv)
1608 collect_execute (prog, argv, NULL);
1609 do_wait (prog);
1612 /* Unlink a file unless we are debugging. */
1614 static void
1615 maybe_unlink (const char *file)
1617 if (!debug)
1618 unlink (file);
1619 else
1620 notice ("[Leaving %s]\n", file);
1624 static long sequence_number = 0;
1626 /* Add a name to a linked list. */
1628 static void
1629 add_to_list (struct head *head_ptr, const char *name)
1631 struct id *newid = xcalloc (sizeof (struct id) + strlen (name), 1);
1632 struct id *p;
1633 strcpy (newid->name, name);
1635 if (head_ptr->first)
1636 head_ptr->last->next = newid;
1637 else
1638 head_ptr->first = newid;
1640 /* Check for duplicate symbols. */
1641 for (p = head_ptr->first;
1642 strcmp (name, p->name) != 0;
1643 p = p->next)
1645 if (p != newid)
1647 head_ptr->last->next = 0;
1648 free (newid);
1649 return;
1652 newid->sequence = ++sequence_number;
1653 head_ptr->last = newid;
1654 head_ptr->number++;
1657 /* Grab the init priority number from an init function name that
1658 looks like "_GLOBAL_.I.12345.foo". */
1660 static int
1661 extract_init_priority (const char *name)
1663 int pos = 0, pri;
1665 while (name[pos] == '_')
1666 ++pos;
1667 pos += 10; /* strlen ("GLOBAL__X_") */
1669 /* Extract init_p number from ctor/dtor name. */
1670 pri = atoi (name + pos);
1671 return pri ? pri : DEFAULT_INIT_PRIORITY;
1674 /* Insertion sort the ids from ctor/dtor list HEAD_PTR in descending order.
1675 ctors will be run from right to left, dtors from left to right. */
1677 static void
1678 sort_ids (struct head *head_ptr)
1680 /* id holds the current element to insert. id_next holds the next
1681 element to insert. id_ptr iterates through the already sorted elements
1682 looking for the place to insert id. */
1683 struct id *id, *id_next, **id_ptr;
1685 id = head_ptr->first;
1687 /* We don't have any sorted elements yet. */
1688 head_ptr->first = NULL;
1690 for (; id; id = id_next)
1692 id_next = id->next;
1693 id->sequence = extract_init_priority (id->name);
1695 for (id_ptr = &(head_ptr->first); ; id_ptr = &((*id_ptr)->next))
1696 if (*id_ptr == NULL
1697 /* If the sequence numbers are the same, we put the id from the
1698 file later on the command line later in the list. */
1699 || id->sequence > (*id_ptr)->sequence
1700 /* Hack: do lexical compare, too.
1701 || (id->sequence == (*id_ptr)->sequence
1702 && strcmp (id->name, (*id_ptr)->name) > 0) */
1705 id->next = *id_ptr;
1706 *id_ptr = id;
1707 break;
1711 /* Now set the sequence numbers properly so write_c_file works. */
1712 for (id = head_ptr->first; id; id = id->next)
1713 id->sequence = ++sequence_number;
1716 /* Write: `prefix', the names on list LIST, `suffix'. */
1718 static void
1719 write_list (FILE *stream, const char *prefix, struct id *list)
1721 while (list)
1723 fprintf (stream, "%sx%d,\n", prefix, list->sequence);
1724 list = list->next;
1728 #if LINK_ELIMINATE_DUPLICATE_LDIRECTORIES
1729 /* Given a STRING, return nonzero if it occurs in the list in range
1730 [ARGS_BEGIN,ARGS_END). */
1732 static int
1733 is_in_args (const char *string, const char **args_begin,
1734 const char **args_end)
1736 const char **args_pointer;
1737 for (args_pointer = args_begin; args_pointer != args_end; ++args_pointer)
1738 if (strcmp (string, *args_pointer) == 0)
1739 return 1;
1740 return 0;
1742 #endif /* LINK_ELIMINATE_DUPLICATE_LDIRECTORIES */
1744 #ifdef COLLECT_EXPORT_LIST
1745 /* This function is really used only on AIX, but may be useful. */
1746 #if 0
1747 static int
1748 is_in_list (const char *prefix, struct id *list)
1750 while (list)
1752 if (!strcmp (prefix, list->name)) return 1;
1753 list = list->next;
1755 return 0;
1757 #endif
1758 #endif /* COLLECT_EXPORT_LIST */
1760 /* Added for debugging purpose. */
1761 #ifdef COLLECT_EXPORT_LIST
1762 static void
1763 dump_list (FILE *stream, const char *prefix, struct id *list)
1765 while (list)
1767 fprintf (stream, "%s%s,\n", prefix, list->name);
1768 list = list->next;
1771 #endif
1773 #if 0
1774 static void
1775 dump_prefix_list (FILE *stream, const char *prefix, struct prefix_list *list)
1777 while (list)
1779 fprintf (stream, "%s%s,\n", prefix, list->prefix);
1780 list = list->next;
1783 #endif
1785 static void
1786 write_list_with_asm (FILE *stream, const char *prefix, struct id *list)
1788 while (list)
1790 fprintf (stream, "%sx%d __asm__ (\"%s\");\n",
1791 prefix, list->sequence, list->name);
1792 list = list->next;
1796 /* Write out the constructor and destructor tables statically (for a shared
1797 object), along with the functions to execute them. */
1799 static void
1800 write_c_file_stat (FILE *stream, const char *name ATTRIBUTE_UNUSED)
1802 const char *p, *q;
1803 char *prefix, *r;
1804 int frames = (frame_tables.number > 0);
1806 /* Figure out name of output_file, stripping off .so version. */
1807 p = strrchr (output_file, '/');
1808 if (p == 0)
1809 p = output_file;
1810 else
1811 p++;
1812 q = p;
1813 while (q)
1815 q = strchr (q,'.');
1816 if (q == 0)
1818 q = p + strlen (p);
1819 break;
1821 else
1823 if (strncmp (q, ".so", 3) == 0)
1825 q += 3;
1826 break;
1828 else
1829 q++;
1832 /* q points to null at end of the string (or . of the .so version) */
1833 prefix = xmalloc (q - p + 1);
1834 strncpy (prefix, p, q - p);
1835 prefix[q - p] = 0;
1836 for (r = prefix; *r; r++)
1837 if (!ISALNUM ((unsigned char)*r))
1838 *r = '_';
1839 if (debug)
1840 notice ("\nwrite_c_file - output name is %s, prefix is %s\n",
1841 output_file, prefix);
1843 initname = concat ("_GLOBAL__FI_", prefix, NULL);
1844 fininame = concat ("_GLOBAL__FD_", prefix, NULL);
1846 free (prefix);
1848 /* Write the tables as C code. */
1850 fprintf (stream, "static int count;\n");
1851 fprintf (stream, "typedef void entry_pt();\n");
1852 write_list_with_asm (stream, "extern entry_pt ", constructors.first);
1854 if (frames)
1856 write_list_with_asm (stream, "extern void *", frame_tables.first);
1858 fprintf (stream, "\tstatic void *frame_table[] = {\n");
1859 write_list (stream, "\t\t&", frame_tables.first);
1860 fprintf (stream, "\t0\n};\n");
1862 /* This must match what's in frame.h. */
1863 fprintf (stream, "struct object {\n");
1864 fprintf (stream, " void *pc_begin;\n");
1865 fprintf (stream, " void *pc_end;\n");
1866 fprintf (stream, " void *fde_begin;\n");
1867 fprintf (stream, " void *fde_array;\n");
1868 fprintf (stream, " __SIZE_TYPE__ count;\n");
1869 fprintf (stream, " struct object *next;\n");
1870 fprintf (stream, "};\n");
1872 fprintf (stream, "extern void __register_frame_info_table (void *, struct object *);\n");
1873 fprintf (stream, "extern void *__deregister_frame_info (void *);\n");
1875 fprintf (stream, "static void reg_frame () {\n");
1876 fprintf (stream, "\tstatic struct object ob;\n");
1877 fprintf (stream, "\t__register_frame_info_table (frame_table, &ob);\n");
1878 fprintf (stream, "\t}\n");
1880 fprintf (stream, "static void dereg_frame () {\n");
1881 fprintf (stream, "\t__deregister_frame_info (frame_table);\n");
1882 fprintf (stream, "\t}\n");
1885 fprintf (stream, "void %s() {\n", initname);
1886 if (constructors.number > 0 || frames)
1888 fprintf (stream, "\tstatic entry_pt *ctors[] = {\n");
1889 write_list (stream, "\t\t", constructors.first);
1890 if (frames)
1891 fprintf (stream, "\treg_frame,\n");
1892 fprintf (stream, "\t};\n");
1893 fprintf (stream, "\tentry_pt **p;\n");
1894 fprintf (stream, "\tif (count++ != 0) return;\n");
1895 fprintf (stream, "\tp = ctors + %d;\n", constructors.number + frames);
1896 fprintf (stream, "\twhile (p > ctors) (*--p)();\n");
1898 else
1899 fprintf (stream, "\t++count;\n");
1900 fprintf (stream, "}\n");
1901 write_list_with_asm (stream, "extern entry_pt ", destructors.first);
1902 fprintf (stream, "void %s() {\n", fininame);
1903 if (destructors.number > 0 || frames)
1905 fprintf (stream, "\tstatic entry_pt *dtors[] = {\n");
1906 write_list (stream, "\t\t", destructors.first);
1907 if (frames)
1908 fprintf (stream, "\tdereg_frame,\n");
1909 fprintf (stream, "\t};\n");
1910 fprintf (stream, "\tentry_pt **p;\n");
1911 fprintf (stream, "\tif (--count != 0) return;\n");
1912 fprintf (stream, "\tp = dtors;\n");
1913 fprintf (stream, "\twhile (p < dtors + %d) (*p++)();\n",
1914 destructors.number + frames);
1916 fprintf (stream, "}\n");
1918 if (shared_obj)
1920 COLLECT_SHARED_INIT_FUNC(stream, initname);
1921 COLLECT_SHARED_FINI_FUNC(stream, fininame);
1925 /* Write the constructor/destructor tables. */
1927 #ifndef LD_INIT_SWITCH
1928 static void
1929 write_c_file_glob (FILE *stream, const char *name ATTRIBUTE_UNUSED)
1931 /* Write the tables as C code. */
1933 int frames = (frame_tables.number > 0);
1935 fprintf (stream, "typedef void entry_pt();\n\n");
1937 write_list_with_asm (stream, "extern entry_pt ", constructors.first);
1939 if (frames)
1941 write_list_with_asm (stream, "extern void *", frame_tables.first);
1943 fprintf (stream, "\tstatic void *frame_table[] = {\n");
1944 write_list (stream, "\t\t&", frame_tables.first);
1945 fprintf (stream, "\t0\n};\n");
1947 /* This must match what's in frame.h. */
1948 fprintf (stream, "struct object {\n");
1949 fprintf (stream, " void *pc_begin;\n");
1950 fprintf (stream, " void *pc_end;\n");
1951 fprintf (stream, " void *fde_begin;\n");
1952 fprintf (stream, " void *fde_array;\n");
1953 fprintf (stream, " __SIZE_TYPE__ count;\n");
1954 fprintf (stream, " struct object *next;\n");
1955 fprintf (stream, "};\n");
1957 fprintf (stream, "extern void __register_frame_info_table (void *, struct object *);\n");
1958 fprintf (stream, "extern void *__deregister_frame_info (void *);\n");
1960 fprintf (stream, "static void reg_frame () {\n");
1961 fprintf (stream, "\tstatic struct object ob;\n");
1962 fprintf (stream, "\t__register_frame_info_table (frame_table, &ob);\n");
1963 fprintf (stream, "\t}\n");
1965 fprintf (stream, "static void dereg_frame () {\n");
1966 fprintf (stream, "\t__deregister_frame_info (frame_table);\n");
1967 fprintf (stream, "\t}\n");
1970 fprintf (stream, "\nentry_pt * __CTOR_LIST__[] = {\n");
1971 fprintf (stream, "\t(entry_pt *) %d,\n", constructors.number + frames);
1972 write_list (stream, "\t", constructors.first);
1973 if (frames)
1974 fprintf (stream, "\treg_frame,\n");
1975 fprintf (stream, "\t0\n};\n\n");
1977 write_list_with_asm (stream, "extern entry_pt ", destructors.first);
1979 fprintf (stream, "\nentry_pt * __DTOR_LIST__[] = {\n");
1980 fprintf (stream, "\t(entry_pt *) %d,\n", destructors.number + frames);
1981 write_list (stream, "\t", destructors.first);
1982 if (frames)
1983 fprintf (stream, "\tdereg_frame,\n");
1984 fprintf (stream, "\t0\n};\n\n");
1986 fprintf (stream, "extern entry_pt %s;\n", NAME__MAIN);
1987 fprintf (stream, "entry_pt *__main_reference = %s;\n\n", NAME__MAIN);
1989 #endif /* ! LD_INIT_SWITCH */
1991 static void
1992 write_c_file (FILE *stream, const char *name)
1994 fprintf (stream, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n");
1995 #ifndef LD_INIT_SWITCH
1996 if (! shared_obj)
1997 write_c_file_glob (stream, name);
1998 else
1999 #endif
2000 write_c_file_stat (stream, name);
2001 fprintf (stream, "#ifdef __cplusplus\n}\n#endif\n");
2004 #ifdef COLLECT_EXPORT_LIST
2005 static void
2006 write_aix_file (FILE *stream, struct id *list)
2008 for (; list; list = list->next)
2010 fputs (list->name, stream);
2011 putc ('\n', stream);
2014 #endif
2016 #ifdef OBJECT_FORMAT_NONE
2018 /* Generic version to scan the name list of the loaded program for
2019 the symbols g++ uses for static constructors and destructors.
2021 The constructor table begins at __CTOR_LIST__ and contains a count
2022 of the number of pointers (or -1 if the constructors are built in a
2023 separate section by the linker), followed by the pointers to the
2024 constructor functions, terminated with a null pointer. The
2025 destructor table has the same format, and begins at __DTOR_LIST__. */
2027 static void
2028 scan_prog_file (const char *prog_name, enum pass which_pass)
2030 void (*int_handler) (int);
2031 void (*quit_handler) (int);
2032 char *real_nm_argv[4];
2033 const char **nm_argv = (const char **) real_nm_argv;
2034 int argc = 0;
2035 int pipe_fd[2];
2036 char *p, buf[1024];
2037 FILE *inf;
2039 if (which_pass == PASS_SECOND)
2040 return;
2042 /* If we do not have an `nm', complain. */
2043 if (nm_file_name == 0)
2044 fatal ("cannot find 'nm'");
2046 nm_argv[argc++] = nm_file_name;
2047 if (NM_FLAGS[0] != '\0')
2048 nm_argv[argc++] = NM_FLAGS;
2050 nm_argv[argc++] = prog_name;
2051 nm_argv[argc++] = (char *) 0;
2053 if (pipe (pipe_fd) < 0)
2054 fatal_perror ("pipe");
2056 inf = fdopen (pipe_fd[0], "r");
2057 if (inf == (FILE *) 0)
2058 fatal_perror ("fdopen");
2060 /* Trace if needed. */
2061 if (vflag)
2063 const char **p_argv;
2064 const char *str;
2066 for (p_argv = &nm_argv[0]; (str = *p_argv) != (char *) 0; p_argv++)
2067 fprintf (stderr, " %s", str);
2069 fprintf (stderr, "\n");
2072 fflush (stdout);
2073 fflush (stderr);
2075 /* Spawn child nm on pipe. */
2076 pid = vfork ();
2077 if (pid == -1)
2078 fatal_perror (VFORK_STRING);
2080 if (pid == 0) /* child context */
2082 /* setup stdout */
2083 if (dup2 (pipe_fd[1], 1) < 0)
2084 fatal_perror ("dup2 %d 1", pipe_fd[1]);
2086 if (close (pipe_fd[0]) < 0)
2087 fatal_perror ("close %d", pipe_fd[0]);
2089 if (close (pipe_fd[1]) < 0)
2090 fatal_perror ("close %d", pipe_fd[1]);
2092 execv (nm_file_name, real_nm_argv);
2093 fatal_perror ("execv %s", nm_file_name);
2096 /* Parent context from here on. */
2097 int_handler = (void (*) (int)) signal (SIGINT, SIG_IGN);
2098 #ifdef SIGQUIT
2099 quit_handler = (void (*) (int)) signal (SIGQUIT, SIG_IGN);
2100 #endif
2102 if (close (pipe_fd[1]) < 0)
2103 fatal_perror ("close %d", pipe_fd[1]);
2105 if (debug)
2106 fprintf (stderr, "\nnm output with constructors/destructors.\n");
2108 /* Read each line of nm output. */
2109 while (fgets (buf, sizeof buf, inf) != (char *) 0)
2111 int ch, ch2;
2112 char *name, *end;
2114 /* If it contains a constructor or destructor name, add the name
2115 to the appropriate list. */
2117 for (p = buf; (ch = *p) != '\0' && ch != '\n' && ch != '_'; p++)
2118 if (ch == ' ' && p[1] == 'U' && p[2] == ' ')
2119 break;
2121 if (ch != '_')
2122 continue;
2124 name = p;
2125 /* Find the end of the symbol name.
2126 Do not include `|', because Encore nm can tack that on the end. */
2127 for (end = p; (ch2 = *end) != '\0' && !ISSPACE (ch2) && ch2 != '|';
2128 end++)
2129 continue;
2132 *end = '\0';
2133 switch (is_ctor_dtor (name))
2135 case 1:
2136 if (which_pass != PASS_LIB)
2137 add_to_list (&constructors, name);
2138 break;
2140 case 2:
2141 if (which_pass != PASS_LIB)
2142 add_to_list (&destructors, name);
2143 break;
2145 case 3:
2146 if (which_pass != PASS_LIB)
2147 fatal ("init function found in object %s", prog_name);
2148 #ifndef LD_INIT_SWITCH
2149 add_to_list (&constructors, name);
2150 #endif
2151 break;
2153 case 4:
2154 if (which_pass != PASS_LIB)
2155 fatal ("fini function found in object %s", prog_name);
2156 #ifndef LD_FINI_SWITCH
2157 add_to_list (&destructors, name);
2158 #endif
2159 break;
2161 case 5:
2162 if (which_pass != PASS_LIB)
2163 add_to_list (&frame_tables, name);
2164 break;
2166 default: /* not a constructor or destructor */
2167 continue;
2170 if (debug)
2171 fprintf (stderr, "\t%s\n", buf);
2174 if (debug)
2175 fprintf (stderr, "\n");
2177 if (fclose (inf) != 0)
2178 fatal_perror ("fclose");
2180 do_wait (nm_file_name);
2182 signal (SIGINT, int_handler);
2183 #ifdef SIGQUIT
2184 signal (SIGQUIT, quit_handler);
2185 #endif
2188 #ifdef LDD_SUFFIX
2190 /* Use the List Dynamic Dependencies program to find shared libraries that
2191 the output file depends upon and their initialization/finalization
2192 routines, if any. */
2194 static void
2195 scan_libraries (const char *prog_name)
2197 static struct head libraries; /* list of shared libraries found */
2198 struct id *list;
2199 void (*int_handler) (int);
2200 void (*quit_handler) (int);
2201 char *real_ldd_argv[4];
2202 const char **ldd_argv = (const char **) real_ldd_argv;
2203 int argc = 0;
2204 int pipe_fd[2];
2205 char buf[1024];
2206 FILE *inf;
2208 /* If we do not have an `ldd', complain. */
2209 if (ldd_file_name == 0)
2211 error ("cannot find 'ldd'");
2212 return;
2215 ldd_argv[argc++] = ldd_file_name;
2216 ldd_argv[argc++] = prog_name;
2217 ldd_argv[argc++] = (char *) 0;
2219 if (pipe (pipe_fd) < 0)
2220 fatal_perror ("pipe");
2222 inf = fdopen (pipe_fd[0], "r");
2223 if (inf == (FILE *) 0)
2224 fatal_perror ("fdopen");
2226 /* Trace if needed. */
2227 if (vflag)
2229 const char **p_argv;
2230 const char *str;
2232 for (p_argv = &ldd_argv[0]; (str = *p_argv) != (char *) 0; p_argv++)
2233 fprintf (stderr, " %s", str);
2235 fprintf (stderr, "\n");
2238 fflush (stdout);
2239 fflush (stderr);
2241 /* Spawn child ldd on pipe. */
2242 pid = vfork ();
2243 if (pid == -1)
2244 fatal_perror (VFORK_STRING);
2246 if (pid == 0) /* child context */
2248 /* setup stdout */
2249 if (dup2 (pipe_fd[1], 1) < 0)
2250 fatal_perror ("dup2 %d 1", pipe_fd[1]);
2252 if (close (pipe_fd[0]) < 0)
2253 fatal_perror ("close %d", pipe_fd[0]);
2255 if (close (pipe_fd[1]) < 0)
2256 fatal_perror ("close %d", pipe_fd[1]);
2258 execv (ldd_file_name, real_ldd_argv);
2259 fatal_perror ("execv %s", ldd_file_name);
2262 /* Parent context from here on. */
2263 int_handler = (void (*) (int)) signal (SIGINT, SIG_IGN);
2264 #ifdef SIGQUIT
2265 quit_handler = (void (*) (int)) signal (SIGQUIT, SIG_IGN);
2266 #endif
2268 if (close (pipe_fd[1]) < 0)
2269 fatal_perror ("close %d", pipe_fd[1]);
2271 if (debug)
2272 notice ("\nldd output with constructors/destructors.\n");
2274 /* Read each line of ldd output. */
2275 while (fgets (buf, sizeof buf, inf) != (char *) 0)
2277 int ch2;
2278 char *name, *end, *p = buf;
2280 /* Extract names of libraries and add to list. */
2281 PARSE_LDD_OUTPUT (p);
2282 if (p == 0)
2283 continue;
2285 name = p;
2286 if (strncmp (name, "not found", sizeof ("not found") - 1) == 0)
2287 fatal ("dynamic dependency %s not found", buf);
2289 /* Find the end of the symbol name. */
2290 for (end = p;
2291 (ch2 = *end) != '\0' && ch2 != '\n' && !ISSPACE (ch2) && ch2 != '|';
2292 end++)
2293 continue;
2294 *end = '\0';
2296 if (access (name, R_OK) == 0)
2297 add_to_list (&libraries, name);
2298 else
2299 fatal ("unable to open dynamic dependency '%s'", buf);
2301 if (debug)
2302 fprintf (stderr, "\t%s\n", buf);
2304 if (debug)
2305 fprintf (stderr, "\n");
2307 if (fclose (inf) != 0)
2308 fatal_perror ("fclose");
2310 do_wait (ldd_file_name);
2312 signal (SIGINT, int_handler);
2313 #ifdef SIGQUIT
2314 signal (SIGQUIT, quit_handler);
2315 #endif
2317 /* Now iterate through the library list adding their symbols to
2318 the list. */
2319 for (list = libraries.first; list; list = list->next)
2320 scan_prog_file (list->name, PASS_LIB);
2323 #endif /* LDD_SUFFIX */
2325 #endif /* OBJECT_FORMAT_NONE */
2329 * COFF specific stuff.
2332 #ifdef OBJECT_FORMAT_COFF
2334 #if defined (EXTENDED_COFF)
2336 # define GCC_SYMBOLS(X) (SYMHEADER(X).isymMax + SYMHEADER(X).iextMax)
2337 # define GCC_SYMENT SYMR
2338 # define GCC_OK_SYMBOL(X) ((X).st == stProc || (X).st == stGlobal)
2339 # define GCC_SYMINC(X) (1)
2340 # define GCC_SYMZERO(X) (SYMHEADER(X).isymMax)
2341 # define GCC_CHECK_HDR(X) (PSYMTAB(X) != 0)
2343 #else
2345 # define GCC_SYMBOLS(X) (HEADER(ldptr).f_nsyms)
2346 # define GCC_SYMENT SYMENT
2347 # if defined (C_WEAKEXT)
2348 # define GCC_OK_SYMBOL(X) \
2349 (((X).n_sclass == C_EXT || (X).n_sclass == C_WEAKEXT) && \
2350 ((X).n_scnum > N_UNDEF) && \
2351 (aix64_flag \
2352 || (((X).n_type & N_TMASK) == (DT_NON << N_BTSHFT) \
2353 || ((X).n_type & N_TMASK) == (DT_FCN << N_BTSHFT))))
2354 # define GCC_UNDEF_SYMBOL(X) \
2355 (((X).n_sclass == C_EXT || (X).n_sclass == C_WEAKEXT) && \
2356 ((X).n_scnum == N_UNDEF))
2357 # else
2358 # define GCC_OK_SYMBOL(X) \
2359 (((X).n_sclass == C_EXT) && \
2360 ((X).n_scnum > N_UNDEF) && \
2361 (aix64_flag \
2362 || (((X).n_type & N_TMASK) == (DT_NON << N_BTSHFT) \
2363 || ((X).n_type & N_TMASK) == (DT_FCN << N_BTSHFT))))
2364 # define GCC_UNDEF_SYMBOL(X) \
2365 (((X).n_sclass == C_EXT) && ((X).n_scnum == N_UNDEF))
2366 # endif
2367 # define GCC_SYMINC(X) ((X).n_numaux+1)
2368 # define GCC_SYMZERO(X) 0
2370 /* 0757 = U803XTOCMAGIC (AIX 4.3) and 0767 = U64_TOCMAGIC (AIX V5) */
2371 #ifdef _AIX51
2372 # define GCC_CHECK_HDR(X) \
2373 ((HEADER (X).f_magic == U802TOCMAGIC && ! aix64_flag) \
2374 || (HEADER (X).f_magic == 0767 && aix64_flag))
2375 #else
2376 # define GCC_CHECK_HDR(X) \
2377 ((HEADER (X).f_magic == U802TOCMAGIC && ! aix64_flag) \
2378 || (HEADER (X).f_magic == 0757 && aix64_flag))
2379 #endif
2381 #endif
2383 #ifdef COLLECT_EXPORT_LIST
2384 /* Array of standard AIX libraries which should not
2385 be scanned for ctors/dtors. */
2386 static const char *const aix_std_libs[] = {
2387 "/unix",
2388 "/lib/libc.a",
2389 "/lib/libm.a",
2390 "/lib/libc_r.a",
2391 "/lib/libm_r.a",
2392 "/usr/lib/libc.a",
2393 "/usr/lib/libm.a",
2394 "/usr/lib/libc_r.a",
2395 "/usr/lib/libm_r.a",
2396 "/usr/lib/threads/libc.a",
2397 "/usr/ccs/lib/libc.a",
2398 "/usr/ccs/lib/libm.a",
2399 "/usr/ccs/lib/libc_r.a",
2400 "/usr/ccs/lib/libm_r.a",
2401 NULL
2404 /* This function checks the filename and returns 1
2405 if this name matches the location of a standard AIX library. */
2406 static int ignore_library (const char *);
2407 static int
2408 ignore_library (const char *name)
2410 const char *const *p = &aix_std_libs[0];
2411 while (*p++ != NULL)
2412 if (! strcmp (name, *p)) return 1;
2413 return 0;
2415 #endif /* COLLECT_EXPORT_LIST */
2417 #if defined (HAVE_DECL_LDGETNAME) && !HAVE_DECL_LDGETNAME
2418 extern char *ldgetname (LDFILE *, GCC_SYMENT *);
2419 #endif
2421 /* COFF version to scan the name list of the loaded program for
2422 the symbols g++ uses for static constructors and destructors.
2424 The constructor table begins at __CTOR_LIST__ and contains a count
2425 of the number of pointers (or -1 if the constructors are built in a
2426 separate section by the linker), followed by the pointers to the
2427 constructor functions, terminated with a null pointer. The
2428 destructor table has the same format, and begins at __DTOR_LIST__. */
2430 static void
2431 scan_prog_file (const char *prog_name, enum pass which_pass)
2433 LDFILE *ldptr = NULL;
2434 int sym_index, sym_count;
2435 int is_shared = 0;
2437 if (which_pass != PASS_FIRST && which_pass != PASS_OBJ)
2438 return;
2440 #ifdef COLLECT_EXPORT_LIST
2441 /* We do not need scanning for some standard C libraries. */
2442 if (which_pass == PASS_FIRST && ignore_library (prog_name))
2443 return;
2445 /* On AIX we have a loop, because there is not much difference
2446 between an object and an archive. This trick allows us to
2447 eliminate scan_libraries() function. */
2450 #endif
2451 /* Some platforms (e.g. OSF4) declare ldopen as taking a
2452 non-const char * filename parameter, even though it will not
2453 modify that string. So we must cast away const-ness here,
2454 which will cause -Wcast-qual to burp. */
2455 if ((ldptr = ldopen ((char *)prog_name, ldptr)) != NULL)
2457 if (! MY_ISCOFF (HEADER (ldptr).f_magic))
2458 fatal ("%s: not a COFF file", prog_name);
2460 if (GCC_CHECK_HDR (ldptr))
2462 sym_count = GCC_SYMBOLS (ldptr);
2463 sym_index = GCC_SYMZERO (ldptr);
2465 #ifdef COLLECT_EXPORT_LIST
2466 /* Is current archive member a shared object? */
2467 is_shared = HEADER (ldptr).f_flags & F_SHROBJ;
2468 #endif
2470 while (sym_index < sym_count)
2472 GCC_SYMENT symbol;
2474 if (ldtbread (ldptr, sym_index, &symbol) <= 0)
2475 break;
2476 sym_index += GCC_SYMINC (symbol);
2478 if (GCC_OK_SYMBOL (symbol))
2480 char *name;
2482 if ((name = ldgetname (ldptr, &symbol)) == NULL)
2483 continue; /* Should never happen. */
2485 #ifdef XCOFF_DEBUGGING_INFO
2486 /* All AIX function names have a duplicate entry
2487 beginning with a dot. */
2488 if (*name == '.')
2489 ++name;
2490 #endif
2492 switch (is_ctor_dtor (name))
2494 case 1:
2495 if (! is_shared)
2496 add_to_list (&constructors, name);
2497 #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
2498 if (which_pass == PASS_OBJ)
2499 add_to_list (&exports, name);
2500 #endif
2501 break;
2503 case 2:
2504 if (! is_shared)
2505 add_to_list (&destructors, name);
2506 #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
2507 if (which_pass == PASS_OBJ)
2508 add_to_list (&exports, name);
2509 #endif
2510 break;
2512 #ifdef COLLECT_EXPORT_LIST
2513 case 3:
2514 #ifndef LD_INIT_SWITCH
2515 if (is_shared)
2516 add_to_list (&constructors, name);
2517 #endif
2518 break;
2520 case 4:
2521 #ifndef LD_INIT_SWITCH
2522 if (is_shared)
2523 add_to_list (&destructors, name);
2524 #endif
2525 break;
2526 #endif
2528 case 5:
2529 if (! is_shared)
2530 add_to_list (&frame_tables, 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 default: /* not a constructor or destructor */
2538 #ifdef COLLECT_EXPORT_LIST
2539 /* Explicitly export all global symbols when
2540 building a shared object on AIX, but do not
2541 re-export symbols from another shared object
2542 and do not export symbols if the user
2543 provides an explicit export list. */
2544 if (shared_obj && !is_shared
2545 && which_pass == PASS_OBJ && !export_flag)
2546 add_to_list (&exports, name);
2547 #endif
2548 continue;
2551 if (debug)
2552 #if !defined(EXTENDED_COFF)
2553 fprintf (stderr, "\tsec=%d class=%d type=%s%o %s\n",
2554 symbol.n_scnum, symbol.n_sclass,
2555 (symbol.n_type ? "0" : ""), symbol.n_type,
2556 name);
2557 #else
2558 fprintf (stderr,
2559 "\tiss = %5d, value = %5ld, index = %5d, name = %s\n",
2560 symbol.iss, (long) symbol.value, symbol.index, name);
2561 #endif
2565 #ifdef COLLECT_EXPORT_LIST
2566 else
2568 /* If archive contains both 32-bit and 64-bit objects,
2569 we want to skip objects in other mode so mismatch normal. */
2570 if (debug)
2571 fprintf (stderr, "%s : magic=%o aix64=%d mismatch\n",
2572 prog_name, HEADER (ldptr).f_magic, aix64_flag);
2574 #endif
2576 else
2578 fatal ("%s: cannot open as COFF file", prog_name);
2580 #ifdef COLLECT_EXPORT_LIST
2581 /* On AIX loop continues while there are more members in archive. */
2583 while (ldclose (ldptr) == FAILURE);
2584 #else
2585 /* Otherwise we simply close ldptr. */
2586 (void) ldclose(ldptr);
2587 #endif
2589 #endif /* OBJECT_FORMAT_COFF */
2591 #ifdef COLLECT_EXPORT_LIST
2592 /* Given a library name without "lib" prefix, this function
2593 returns a full library name including a path. */
2594 static char *
2595 resolve_lib_name (const char *name)
2597 char *lib_buf;
2598 int i, j, l = 0;
2599 /* Library extensions for AIX dynamic linking. */
2600 const char * const libexts[2] = {"a", "so"};
2602 for (i = 0; libpaths[i]; i++)
2603 if (libpaths[i]->max_len > l)
2604 l = libpaths[i]->max_len;
2606 lib_buf = xmalloc (l + strlen(name) + 10);
2608 for (i = 0; libpaths[i]; i++)
2610 struct prefix_list *list = libpaths[i]->plist;
2611 for (; list; list = list->next)
2613 /* The following lines are needed because path_prefix list
2614 may contain directories both with trailing '/' and
2615 without it. */
2616 const char *p = "";
2617 if (list->prefix[strlen(list->prefix)-1] != '/')
2618 p = "/";
2619 for (j = 0; j < 2; j++)
2621 sprintf (lib_buf, "%s%slib%s.%s",
2622 list->prefix, p, name,
2623 libexts[(j + aixrtl_flag) % 2]);
2624 if (debug) fprintf (stderr, "searching for: %s\n", lib_buf);
2625 if (file_exists (lib_buf))
2627 if (debug) fprintf (stderr, "found: %s\n", lib_buf);
2628 return (lib_buf);
2633 if (debug)
2634 fprintf (stderr, "not found\n");
2635 else
2636 fatal ("library lib%s not found", name);
2637 return (NULL);
2639 #endif /* COLLECT_EXPORT_LIST */