1 /* Collect static initialization info into data structures that can be
2 traversed by C++ initialization and finalization routines.
3 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5 Contributed by Chris Smith (csmith@convex.com).
6 Heavily modified by Michael Meissner (meissner@cygnus.com),
7 Per Bothner (bothner@cygnus.com), and John Gilmore (gnu@cygnus.com).
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 2, or (at your option) any later
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING. If not, write to the Free
23 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
27 /* Build tables of static constructors and destructors and run ld. */
31 #include "coretypes.h"
34 #if ! defined( SIGCHLD ) && defined( SIGCLD )
35 # define SIGCHLD SIGCLD
38 #ifndef LIBRARY_PATH_ENV
39 #define LIBRARY_PATH_ENV "LIBRARY_PATH"
50 /* On certain systems, we have code that works by scanning the object file
51 directly. But this code uses system-specific header files and library
52 functions, so turn it off in a cross-compiler. Likewise, the names of
53 the utilities are not correct for a cross-compiler; we have to hope that
54 cross-versions are in the proper directories. */
57 #undef OBJECT_FORMAT_COFF
59 #undef REAL_LD_FILE_NAME
60 #undef REAL_NM_FILE_NAME
61 #undef REAL_STRIP_FILE_NAME
64 /* If we cannot use a special method, use the ordinary one:
65 run nm to find what symbols are present.
66 In a cross-compiler, this means you need a cross nm,
67 but that is not quite as unpleasant as special headers. */
69 #if !defined (OBJECT_FORMAT_COFF)
70 #define OBJECT_FORMAT_NONE
73 #ifdef OBJECT_FORMAT_COFF
82 /* Many versions of ldfcn.h define these. */
90 /* Some systems have an ISCOFF macro, but others do not. In some cases
91 the macro may be wrong. MY_ISCOFF is defined in tm.h files for machines
92 that either do not have an ISCOFF macro in /usr/include or for those
96 #define MY_ISCOFF(X) ISCOFF (X)
99 #endif /* OBJECT_FORMAT_COFF */
101 #ifdef OBJECT_FORMAT_NONE
103 /* Default flags to pass to nm. */
105 #define NM_FLAGS "-n"
108 #endif /* OBJECT_FORMAT_NONE */
110 /* Some systems use __main in a way incompatible with its use in gcc, in these
111 cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
112 give the same symbol without quotes for an alternative entry point. */
114 #define NAME__MAIN "__main"
117 /* This must match tree.h. */
118 #define DEFAULT_INIT_PRIORITY 65535
120 #ifndef COLLECT_SHARED_INIT_FUNC
121 #define COLLECT_SHARED_INIT_FUNC(STREAM, FUNC) \
122 fprintf ((STREAM), "void _GLOBAL__DI() {\n\t%s();\n}\n", (FUNC))
124 #ifndef COLLECT_SHARED_FINI_FUNC
125 #define COLLECT_SHARED_FINI_FUNC(STREAM, FUNC) \
126 fprintf ((STREAM), "void _GLOBAL__DD() {\n\t%s();\n}\n", (FUNC))
130 #define SCAN_LIBRARIES
134 int do_collecting
= 1;
136 int do_collecting
= 0;
139 /* Nonzero if we should suppress the automatic demangling of identifiers
140 in linker error messages. Set from COLLECT_NO_DEMANGLE. */
143 /* Linked lists of constructor and destructor names. */
159 /* Enumeration giving which pass this is for scanning the program file. */
162 PASS_FIRST
, /* without constructors */
163 PASS_OBJ
, /* individual objects */
164 PASS_LIB
, /* looking for shared libraries */
165 PASS_SECOND
/* with constructors linked in */
168 int vflag
; /* true if -v */
169 static int rflag
; /* true if -r */
170 static int strip_flag
; /* true if -s */
171 static const char *demangle_flag
;
172 #ifdef COLLECT_EXPORT_LIST
173 static int export_flag
; /* true if -bE */
174 static int aix64_flag
; /* true if -b64 */
175 static int aixrtl_flag
; /* true if -brtl */
178 int debug
; /* true if -debug */
180 static int shared_obj
; /* true if -shared */
182 static const char *c_file
; /* <xxx>.c for constructor/destructor list. */
183 static const char *o_file
; /* <xxx>.o for constructor/destructor list. */
184 #ifdef COLLECT_EXPORT_LIST
185 static const char *export_file
; /* <xxx>.x for AIX export list. */
187 const char *ldout
; /* File for ld 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 */
192 static const char *ldd_file_name
; /* pathname of ldd (or equivalent) */
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 */
203 static struct head frame_tables
; /* list of frame unwind info tables */
205 struct obstack temporary_obstack
;
206 char * temporary_firstobj
;
208 /* Structure to hold all the directories in which to search for files to
213 const char *prefix
; /* String to prepend to the path. */
214 struct prefix_list
*next
; /* Next in linked list. */
219 struct prefix_list
*plist
; /* List of prefixes to try */
220 int max_len
; /* Max length of a prefix in PLIST */
221 const char *name
; /* Name of this list (used in config stuff) */
224 #ifdef COLLECT_EXPORT_LIST
225 /* Lists to keep libraries to be scanned for global constructors/destructors. */
226 static struct head libs
; /* list of libraries */
227 static struct path_prefix cmdline_lib_dirs
; /* directories specified with -L */
228 static struct path_prefix libpath_lib_dirs
; /* directories in LIBPATH */
229 static struct path_prefix
*libpaths
[3] = {&cmdline_lib_dirs
,
230 &libpath_lib_dirs
, NULL
};
233 static void handler (int);
234 static int is_ctor_dtor (const char *);
235 static char *find_a_file (struct path_prefix
*, const char *);
236 static void add_prefix (struct path_prefix
*, const char *);
237 static void prefix_from_env (const char *, struct path_prefix
*);
238 static void prefix_from_string (const char *, struct path_prefix
*);
239 static void do_wait (const char *, struct pex_obj
*);
240 static void fork_execute (const char *, char **);
241 static void maybe_unlink (const char *);
242 static void add_to_list (struct head
*, const char *);
243 static int extract_init_priority (const char *);
244 static void sort_ids (struct head
*);
245 static void write_list (FILE *, const char *, struct id
*);
246 #ifdef COLLECT_EXPORT_LIST
247 static void dump_list (FILE *, const char *, struct id
*);
250 static void dump_prefix_list (FILE *, const char *, struct prefix_list
*);
252 static void write_list_with_asm (FILE *, const char *, struct id
*);
253 static void write_c_file (FILE *, const char *);
254 static void write_c_file_stat (FILE *, const char *);
255 #ifndef LD_INIT_SWITCH
256 static void write_c_file_glob (FILE *, const char *);
258 static void scan_prog_file (const char *, enum pass
);
259 #ifdef SCAN_LIBRARIES
260 static void scan_libraries (const char *);
262 #if LINK_ELIMINATE_DUPLICATE_LDIRECTORIES
263 static int is_in_args (const char *, const char **, const char **);
265 #ifdef COLLECT_EXPORT_LIST
267 static int is_in_list (const char *, struct id
*);
269 static void write_aix_file (FILE *, struct id
*);
270 static char *resolve_lib_name (const char *);
272 static char *extract_string (const char **);
276 dup2 (int oldfd
, int newfd
)
285 while ((fd
= dup (oldfd
)) != newfd
&& fd
>= 0) /* good enough for low fd's */
288 close (fdtmp
[--fdx
]);
292 #endif /* ! HAVE_DUP2 */
294 /* Delete tempfiles and exit function. */
297 collect_exit (int status
)
299 if (c_file
!= 0 && c_file
[0])
300 maybe_unlink (c_file
);
302 if (o_file
!= 0 && o_file
[0])
303 maybe_unlink (o_file
);
305 #ifdef COLLECT_EXPORT_LIST
306 if (export_file
!= 0 && export_file
[0])
307 maybe_unlink (export_file
);
310 if (ldout
!= 0 && ldout
[0])
312 dump_file (ldout
, stdout
);
313 maybe_unlink (ldout
);
316 if (lderrout
!= 0 && lderrout
[0])
318 dump_file (lderrout
, stderr
);
319 maybe_unlink (lderrout
);
322 if (status
!= 0 && output_file
!= 0 && output_file
[0])
323 maybe_unlink (output_file
);
329 /* Notify user of a non-error. */
331 notice (const char *msgid
, ...)
335 va_start (ap
, msgid
);
336 vfprintf (stderr
, _(msgid
), ap
);
340 /* Die when sys call fails. */
343 fatal_perror (const char * msgid
, ...)
348 va_start (ap
, msgid
);
349 fprintf (stderr
, "collect2: ");
350 vfprintf (stderr
, _(msgid
), ap
);
351 fprintf (stderr
, ": %s\n", xstrerror (e
));
354 collect_exit (FATAL_EXIT_CODE
);
360 fatal (const char * msgid
, ...)
364 va_start (ap
, msgid
);
365 fprintf (stderr
, "collect2: ");
366 vfprintf (stderr
, _(msgid
), ap
);
367 fprintf (stderr
, "\n");
370 collect_exit (FATAL_EXIT_CODE
);
373 /* Write error message. */
376 error (const char * msgid
, ...)
380 va_start (ap
, msgid
);
381 fprintf (stderr
, "collect2: ");
382 vfprintf (stderr
, _(msgid
), ap
);
383 fprintf (stderr
, "\n");
387 /* In case obstack is linked in, and abort is defined to fancy_abort,
388 provide a default entry. */
391 fancy_abort (const char *file
, int line
, const char *func
)
393 fatal ("internal gcc abort in %s, at %s:%d", func
, file
, line
);
399 if (c_file
!= 0 && c_file
[0])
400 maybe_unlink (c_file
);
402 if (o_file
!= 0 && o_file
[0])
403 maybe_unlink (o_file
);
405 if (ldout
!= 0 && ldout
[0])
406 maybe_unlink (ldout
);
408 if (lderrout
!= 0 && lderrout
[0])
409 maybe_unlink (lderrout
);
411 #ifdef COLLECT_EXPORT_LIST
412 if (export_file
!= 0 && export_file
[0])
413 maybe_unlink (export_file
);
416 signal (signo
, SIG_DFL
);
422 file_exists (const char *name
)
424 return access (name
, R_OK
) == 0;
427 /* Parse a reasonable subset of shell quoting syntax. */
430 extract_string (const char **pp
)
443 obstack_1grow (&temporary_obstack
, c
);
444 else if (! inside
&& c
== ' ')
446 else if (! inside
&& c
== '\\')
451 obstack_1grow (&temporary_obstack
, c
);
454 obstack_1grow (&temporary_obstack
, '\0');
456 return obstack_finish (&temporary_obstack
);
460 dump_file (const char *name
, FILE *to
)
462 FILE *stream
= fopen (name
, "r");
469 while (c
= getc (stream
),
470 c
!= EOF
&& (ISIDNUM (c
) || c
== '$' || c
== '.'))
471 obstack_1grow (&temporary_obstack
, c
);
472 if (obstack_object_size (&temporary_obstack
) > 0)
474 const char *word
, *p
;
476 obstack_1grow (&temporary_obstack
, '\0');
477 word
= obstack_finish (&temporary_obstack
);
480 ++word
, putc ('.', to
);
482 if (!strncmp (p
, USER_LABEL_PREFIX
, strlen (USER_LABEL_PREFIX
)))
483 p
+= strlen (USER_LABEL_PREFIX
);
485 #ifdef HAVE_LD_DEMANGLE
491 result
= cplus_demangle (p
, DMGL_PARAMS
| DMGL_ANSI
| DMGL_VERBOSE
);
499 diff
= strlen (word
) - strlen (result
);
500 while (diff
> 0 && c
== ' ')
501 --diff
, putc (' ', to
);
502 while (diff
< 0 && c
== ' ')
503 ++diff
, c
= getc (stream
);
511 obstack_free (&temporary_obstack
, temporary_firstobj
);
520 /* Decide whether the given symbol is: a constructor (1), a destructor
521 (2), a routine in a shared object that calls all the constructors
522 (3) or destructors (4), a DWARF exception-handling table (5), or
523 nothing special (0). */
526 is_ctor_dtor (const char *s
)
528 struct names
{ const char *const name
; const int len
; const int ret
;
529 const int two_underscores
; };
531 const struct names
*p
;
533 const char *orig_s
= s
;
535 static const struct names special
[] = {
536 #ifndef NO_DOLLAR_IN_LABEL
537 { "GLOBAL__I$", sizeof ("GLOBAL__I$")-1, 1, 0 },
538 { "GLOBAL__D$", sizeof ("GLOBAL__D$")-1, 2, 0 },
540 #ifndef NO_DOT_IN_LABEL
541 { "GLOBAL__I.", sizeof ("GLOBAL__I.")-1, 1, 0 },
542 { "GLOBAL__D.", sizeof ("GLOBAL__D.")-1, 2, 0 },
543 #endif /* NO_DOT_IN_LABEL */
544 #endif /* NO_DOLLAR_IN_LABEL */
545 { "GLOBAL__I_", sizeof ("GLOBAL__I_")-1, 1, 0 },
546 { "GLOBAL__D_", sizeof ("GLOBAL__D_")-1, 2, 0 },
547 { "GLOBAL__F_", sizeof ("GLOBAL__F_")-1, 5, 0 },
548 { "GLOBAL__FI_", sizeof ("GLOBAL__FI_")-1, 3, 0 },
549 { "GLOBAL__FD_", sizeof ("GLOBAL__FD_")-1, 4, 0 },
553 while ((ch
= *s
) == '_')
559 for (p
= &special
[0]; p
->len
> 0; p
++)
562 && (!p
->two_underscores
|| ((s
- orig_s
) >= 2))
563 && strncmp(s
, p
->name
, p
->len
) == 0)
571 /* We maintain two prefix lists: one from COMPILER_PATH environment variable
572 and one from the PATH variable. */
574 static struct path_prefix cpath
, path
;
577 /* This is the name of the target machine. We use it to form the name
578 of the files to execute. */
580 static const char *const target_machine
= TARGET_MACHINE
;
583 /* Search for NAME using prefix list PPREFIX. We only look for executable
586 Return 0 if not found, otherwise return its name, allocated with malloc. */
589 find_a_file (struct path_prefix
*pprefix
, const char *name
)
592 struct prefix_list
*pl
;
593 int len
= pprefix
->max_len
+ strlen (name
) + 1;
596 fprintf (stderr
, "Looking for '%s'\n", name
);
598 #ifdef HOST_EXECUTABLE_SUFFIX
599 len
+= strlen (HOST_EXECUTABLE_SUFFIX
);
602 temp
= xmalloc (len
);
604 /* Determine the filename to execute (special case for absolute paths). */
607 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
608 || (*name
&& name
[1] == ':')
612 if (access (name
, X_OK
) == 0)
617 fprintf (stderr
, " - found: absolute path\n");
622 #ifdef HOST_EXECUTABLE_SUFFIX
623 /* Some systems have a suffix for executable files.
624 So try appending that. */
626 strcat (temp
, HOST_EXECUTABLE_SUFFIX
);
628 if (access (temp
, X_OK
) == 0)
633 fprintf (stderr
, " - failed to locate using absolute path\n");
636 for (pl
= pprefix
->plist
; pl
; pl
= pl
->next
)
640 strcpy (temp
, pl
->prefix
);
643 if (stat (temp
, &st
) >= 0
644 && ! S_ISDIR (st
.st_mode
)
645 && access (temp
, X_OK
) == 0)
648 #ifdef HOST_EXECUTABLE_SUFFIX
649 /* Some systems have a suffix for executable files.
650 So try appending that. */
651 strcat (temp
, HOST_EXECUTABLE_SUFFIX
);
653 if (stat (temp
, &st
) >= 0
654 && ! S_ISDIR (st
.st_mode
)
655 && access (temp
, X_OK
) == 0)
660 if (debug
&& pprefix
->plist
== NULL
)
661 fprintf (stderr
, " - failed: no entries in prefix list\n");
667 /* Add an entry for PREFIX to prefix list PPREFIX. */
670 add_prefix (struct path_prefix
*pprefix
, const char *prefix
)
672 struct prefix_list
*pl
, **prev
;
677 for (pl
= pprefix
->plist
; pl
->next
; pl
= pl
->next
)
682 prev
= &pprefix
->plist
;
684 /* Keep track of the longest prefix. */
686 len
= strlen (prefix
);
687 if (len
> pprefix
->max_len
)
688 pprefix
->max_len
= len
;
690 pl
= xmalloc (sizeof (struct prefix_list
));
691 pl
->prefix
= xstrdup (prefix
);
696 pl
->next
= (struct prefix_list
*) 0;
700 /* Take the value of the environment variable ENV, break it into a path, and
701 add of the entries to PPREFIX. */
704 prefix_from_env (const char *env
, struct path_prefix
*pprefix
)
707 GET_ENVIRONMENT (p
, env
);
710 prefix_from_string (p
, pprefix
);
714 prefix_from_string (const char *p
, struct path_prefix
*pprefix
)
716 const char *startp
, *endp
;
717 char *nstore
= xmalloc (strlen (p
) + 3);
720 fprintf (stderr
, "Convert string '%s' into prefixes, separator = '%c'\n", p
, PATH_SEPARATOR
);
725 if (*endp
== PATH_SEPARATOR
|| *endp
== 0)
727 strncpy (nstore
, startp
, endp
-startp
);
730 strcpy (nstore
, "./");
732 else if (! IS_DIR_SEPARATOR (endp
[-1]))
734 nstore
[endp
-startp
] = DIR_SEPARATOR
;
735 nstore
[endp
-startp
+1] = 0;
738 nstore
[endp
-startp
] = 0;
741 fprintf (stderr
, " - add prefix: %s\n", nstore
);
743 add_prefix (pprefix
, nstore
);
746 endp
= startp
= endp
+ 1;
756 main (int argc
, char **argv
)
758 static const char *const ld_suffix
= "ld";
759 static const char *const real_ld_suffix
= "real-ld";
760 static const char *const collect_ld_suffix
= "collect-ld";
761 static const char *const nm_suffix
= "nm";
762 static const char *const gnm_suffix
= "gnm";
764 static const char *const ldd_suffix
= LDD_SUFFIX
;
766 static const char *const strip_suffix
= "strip";
767 static const char *const gstrip_suffix
= "gstrip";
770 /* If we look for a program in the compiler directories, we just use
771 the short name, since these directories are already system-specific.
772 But it we look for a program in the system directories, we need to
773 qualify the program name with the target machine. */
775 const char *const full_ld_suffix
=
776 concat(target_machine
, "-", ld_suffix
, NULL
);
777 const char *const full_nm_suffix
=
778 concat (target_machine
, "-", nm_suffix
, NULL
);
779 const char *const full_gnm_suffix
=
780 concat (target_machine
, "-", gnm_suffix
, NULL
);
782 const char *const full_ldd_suffix
=
783 concat (target_machine
, "-", ldd_suffix
, NULL
);
785 const char *const full_strip_suffix
=
786 concat (target_machine
, "-", strip_suffix
, NULL
);
787 const char *const full_gstrip_suffix
=
788 concat (target_machine
, "-", gstrip_suffix
, NULL
);
790 const char *const full_ld_suffix
= ld_suffix
;
791 const char *const full_nm_suffix
= nm_suffix
;
792 const char *const full_gnm_suffix
= gnm_suffix
;
794 const char *const full_ldd_suffix
= ldd_suffix
;
796 const char *const full_strip_suffix
= strip_suffix
;
797 const char *const full_gstrip_suffix
= gstrip_suffix
;
798 #endif /* CROSS_COMPILE */
802 #ifdef COLLECT_EXPORT_LIST
805 const char *ld_file_name
;
816 int num_c_args
= argc
+9;
818 no_demangle
= !! getenv ("COLLECT_NO_DEMANGLE");
820 /* Suppress demangling by the real linker, which may be broken. */
821 putenv (xstrdup ("COLLECT_NO_DEMANGLE="));
823 #if defined (COLLECT2_HOST_INITIALIZATION)
824 /* Perform system dependent initialization, if necessary. */
825 COLLECT2_HOST_INITIALIZATION
;
829 /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
830 receive the signal. A different setting is inheritable */
831 signal (SIGCHLD
, SIG_DFL
);
836 /* Do not invoke xcalloc before this point, since locale needs to be
837 set first, in case a diagnostic is issued. */
839 ld1
= (const char **)(ld1_argv
= xcalloc(sizeof (char *), argc
+4));
840 ld2
= (const char **)(ld2_argv
= xcalloc(sizeof (char *), argc
+11));
841 object
= (const char **)(object_lst
= xcalloc(sizeof (char *), argc
));
847 /* Parse command line early for instances of -debug. This allows
848 the debug flag to be set before functions like find_a_file()
853 for (i
= 1; argv
[i
] != NULL
; i
++)
855 if (! strcmp (argv
[i
], "-debug"))
861 #ifndef DEFAULT_A_OUT_NAME
862 output_file
= "a.out";
864 output_file
= DEFAULT_A_OUT_NAME
;
867 obstack_begin (&temporary_obstack
, 0);
868 temporary_firstobj
= obstack_alloc (&temporary_obstack
, 0);
870 #ifndef HAVE_LD_DEMANGLE
871 current_demangling_style
= auto_demangling
;
873 p
= getenv ("COLLECT_GCC_OPTIONS");
876 const char *q
= extract_string (&p
);
877 if (*q
== '-' && (q
[1] == 'm' || q
[1] == 'f'))
880 obstack_free (&temporary_obstack
, temporary_firstobj
);
882 /* -fno-profile-arcs -fno-test-coverage -fno-branch-probabilities
883 -fno-exceptions -w */
886 c_ptr
= (const char **) (c_argv
= xcalloc (sizeof (char *), num_c_args
));
889 fatal ("no arguments");
892 if (signal (SIGQUIT
, SIG_IGN
) != SIG_IGN
)
893 signal (SIGQUIT
, handler
);
895 if (signal (SIGINT
, SIG_IGN
) != SIG_IGN
)
896 signal (SIGINT
, handler
);
898 if (signal (SIGALRM
, SIG_IGN
) != SIG_IGN
)
899 signal (SIGALRM
, handler
);
902 if (signal (SIGHUP
, SIG_IGN
) != SIG_IGN
)
903 signal (SIGHUP
, handler
);
905 if (signal (SIGSEGV
, SIG_IGN
) != SIG_IGN
)
906 signal (SIGSEGV
, handler
);
908 if (signal (SIGBUS
, SIG_IGN
) != SIG_IGN
)
909 signal (SIGBUS
, handler
);
912 /* Extract COMPILER_PATH and PATH into our prefix list. */
913 prefix_from_env ("COMPILER_PATH", &cpath
);
914 prefix_from_env ("PATH", &path
);
916 /* Try to discover a valid linker/nm/strip to use. */
918 /* Maybe we know the right file to use (if not cross). */
920 #ifdef DEFAULT_LINKER
921 if (access (DEFAULT_LINKER
, X_OK
) == 0)
922 ld_file_name
= DEFAULT_LINKER
;
923 if (ld_file_name
== 0)
925 #ifdef REAL_LD_FILE_NAME
926 ld_file_name
= find_a_file (&path
, REAL_LD_FILE_NAME
);
927 if (ld_file_name
== 0)
929 /* Search the (target-specific) compiler dirs for ld'. */
930 ld_file_name
= find_a_file (&cpath
, real_ld_suffix
);
931 /* Likewise for `collect-ld'. */
932 if (ld_file_name
== 0)
933 ld_file_name
= find_a_file (&cpath
, collect_ld_suffix
);
934 /* Search the compiler directories for `ld'. We have protection against
935 recursive calls in find_a_file. */
936 if (ld_file_name
== 0)
937 ld_file_name
= find_a_file (&cpath
, ld_suffix
);
938 /* Search the ordinary system bin directories
939 for `ld' (if native linking) or `TARGET-ld' (if cross). */
940 if (ld_file_name
== 0)
941 ld_file_name
= find_a_file (&path
, full_ld_suffix
);
943 #ifdef REAL_NM_FILE_NAME
944 nm_file_name
= find_a_file (&path
, REAL_NM_FILE_NAME
);
945 if (nm_file_name
== 0)
947 nm_file_name
= find_a_file (&cpath
, gnm_suffix
);
948 if (nm_file_name
== 0)
949 nm_file_name
= find_a_file (&path
, full_gnm_suffix
);
950 if (nm_file_name
== 0)
951 nm_file_name
= find_a_file (&cpath
, nm_suffix
);
952 if (nm_file_name
== 0)
953 nm_file_name
= find_a_file (&path
, full_nm_suffix
);
956 ldd_file_name
= find_a_file (&cpath
, ldd_suffix
);
957 if (ldd_file_name
== 0)
958 ldd_file_name
= find_a_file (&path
, full_ldd_suffix
);
961 #ifdef REAL_STRIP_FILE_NAME
962 strip_file_name
= find_a_file (&path
, REAL_STRIP_FILE_NAME
);
963 if (strip_file_name
== 0)
965 strip_file_name
= find_a_file (&cpath
, gstrip_suffix
);
966 if (strip_file_name
== 0)
967 strip_file_name
= find_a_file (&path
, full_gstrip_suffix
);
968 if (strip_file_name
== 0)
969 strip_file_name
= find_a_file (&cpath
, strip_suffix
);
970 if (strip_file_name
== 0)
971 strip_file_name
= find_a_file (&path
, full_strip_suffix
);
973 /* Determine the full path name of the C compiler to use. */
974 c_file_name
= getenv ("COLLECT_GCC");
975 if (c_file_name
== 0)
978 c_file_name
= concat (target_machine
, "-gcc", NULL
);
984 p
= find_a_file (&cpath
, c_file_name
);
986 /* Here it should be safe to use the system search path since we should have
987 already qualified the name of the compiler when it is needed. */
989 p
= find_a_file (&path
, c_file_name
);
994 *ld1
++ = *ld2
++ = ld_file_name
;
996 /* Make temp file names. */
997 c_file
= make_temp_file (".c");
998 o_file
= make_temp_file (".o");
999 #ifdef COLLECT_EXPORT_LIST
1000 export_file
= make_temp_file (".x");
1002 ldout
= make_temp_file (".ld");
1003 lderrout
= make_temp_file (".le");
1004 *c_ptr
++ = c_file_name
;
1011 #ifdef COLLECT_EXPORT_LIST
1012 /* Generate a list of directories from LIBPATH. */
1013 prefix_from_env ("LIBPATH", &libpath_lib_dirs
);
1014 /* Add to this list also two standard directories where
1015 AIX loader always searches for libraries. */
1016 add_prefix (&libpath_lib_dirs
, "/lib");
1017 add_prefix (&libpath_lib_dirs
, "/usr/lib");
1020 /* Get any options that the upper GCC wants to pass to the sub-GCC.
1022 AIX support needs to know if -shared has been specified before
1023 parsing commandline arguments. */
1025 p
= getenv ("COLLECT_GCC_OPTIONS");
1028 const char *q
= extract_string (&p
);
1029 if (*q
== '-' && (q
[1] == 'm' || q
[1] == 'f'))
1030 *c_ptr
++ = xstrdup (q
);
1031 if (strcmp (q
, "-EL") == 0 || strcmp (q
, "-EB") == 0)
1032 *c_ptr
++ = xstrdup (q
);
1033 if (strcmp (q
, "-shared") == 0)
1035 if (*q
== '-' && q
[1] == 'B')
1037 *c_ptr
++ = xstrdup (q
);
1040 q
= extract_string (&p
);
1041 *c_ptr
++ = xstrdup (q
);
1045 obstack_free (&temporary_obstack
, temporary_firstobj
);
1046 *c_ptr
++ = "-fno-profile-arcs";
1047 *c_ptr
++ = "-fno-test-coverage";
1048 *c_ptr
++ = "-fno-branch-probabilities";
1049 *c_ptr
++ = "-fno-exceptions";
1052 /* !!! When GCC calls collect2,
1053 it does not know whether it is calling collect2 or ld.
1054 So collect2 cannot meaningfully understand any options
1055 except those ld understands.
1056 If you propose to make GCC pass some other option,
1057 just imagine what will happen if ld is really ld!!! */
1059 /* Parse arguments. Remember output file spec, pass the rest to ld. */
1060 /* After the first file, put in the c++ rt0. */
1063 #ifdef HAVE_LD_DEMANGLE
1064 if (!demangle_flag
&& !no_demangle
)
1065 demangle_flag
= "--demangle";
1067 *ld1
++ = *ld2
++ = demangle_flag
;
1069 while ((arg
= *++argv
) != (char *) 0)
1071 *ld1
++ = *ld2
++ = arg
;
1077 #ifdef COLLECT_EXPORT_LIST
1078 /* We want to disable automatic exports on AIX when user
1079 explicitly puts an export list in command line */
1081 if (arg
[2] == 'E' || strncmp (&arg
[2], "export", 6) == 0)
1083 else if (arg
[2] == '6' && arg
[3] == '4')
1085 else if (arg
[2] == 'r' && arg
[3] == 't' && arg
[4] == 'l')
1091 if (!strcmp (arg
, "-debug"))
1093 /* Already parsed. */
1097 if (!strcmp (arg
, "-dynamic-linker") && argv
[1])
1100 *ld1
++ = *ld2
++ = *argv
;
1107 /* place o_file BEFORE this argument! */
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
);
1124 #ifdef COLLECT_EXPORT_LIST
1125 /* Saving directories where to search for libraries. */
1127 add_prefix (&cmdline_lib_dirs
, arg
+2);
1130 #if LINK_ELIMINATE_DUPLICATE_LDIRECTORIES
1132 if (is_in_args (arg
, (const char **) ld1_argv
, ld1
-1))
1135 #endif /* LINK_ELIMINATE_DUPLICATE_LDIRECTORIES */
1140 output_file
= *ld1
++ = *ld2
++ = *++argv
;
1142 #ifdef SWITCHES_NEED_SPACES
1143 && ! strchr (SWITCHES_NEED_SPACES
, arg
[1])
1147 output_file
= &arg
[2];
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. */
1172 if (strcmp (arg
, "--no-demangle") == 0)
1174 demangle_flag
= arg
;
1179 else if (strncmp (arg
, "--demangle", 10) == 0)
1181 demangle_flag
= arg
;
1183 #ifndef HAVE_LD_DEMANGLE
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);
1191 current_demangling_style
= style
;
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))
1212 /* place o_file BEFORE this argument! */
1218 if (p
[1] == 'o' || p
[1] == 'l')
1220 #ifdef COLLECT_EXPORT_LIST
1221 /* libraries can be specified directly, i.e. without -l flag. */
1224 /* Saving a full library name. */
1225 add_to_list (&libs
, arg
);
1231 #ifdef COLLECT_EXPORT_LIST
1232 /* This is added only for debugging purposes. */
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
);
1256 char *buf
= concat ("-bE:", export_file
, NULL
);
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
);
1271 *c_ptr
= *ld1
= *object
= (char *) 0;
1275 notice ("collect2 version %s", version_string
);
1276 #ifdef TARGET_VERSION
1279 fprintf (stderr
, "\n");
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"));
1292 fprintf (stderr
, "ldd_file_name = %s\n",
1293 (ldd_file_name
? ldd_file_name
: "not found"));
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");
1304 fprintf (stderr
, "COLLECT_GCC_OPTIONS = %s\n", ptr
);
1306 ptr
= getenv ("COLLECT_GCC");
1308 fprintf (stderr
, "COLLECT_GCC = %s\n", ptr
);
1310 ptr
= getenv ("COMPILER_PATH");
1312 fprintf (stderr
, "COMPILER_PATH = %s\n", ptr
);
1314 ptr
= getenv (LIBRARY_PATH_ENV
);
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
);
1329 /* If -r or they will be run via some other method, do not build the
1330 constructor or destructor list, just return now. */
1332 #ifndef COLLECT_EXPORT_LIST
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
);
1345 maybe_unlink (c_file
);
1346 maybe_unlink (o_file
);
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
);
1359 #ifdef SCAN_LIBRARIES
1360 scan_libraries (output_file
);
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. */
1380 #ifdef COLLECT_EXPORT_LIST
1381 /* Do tlink without additional code generation. */
1382 do_tlink (ld1_argv
, object_lst
);
1384 /* Strip now if it was requested on the command line. */
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
);
1399 maybe_unlink (c_file
);
1400 maybe_unlink (o_file
);
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
);
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
);
1423 *ld2
++ = LD_INIT_SWITCH
;
1425 *ld2
++ = LD_FINI_SWITCH
;
1430 #ifdef COLLECT_EXPORT_LIST
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");
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
);
1453 /* End of arguments to second link phase. */
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");
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
);
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
);
1485 maybe_unlink (c_file
);
1486 maybe_unlink (o_file
);
1488 #ifdef COLLECT_EXPORT_LIST
1489 maybe_unlink (export_file
);
1496 /* Wait for a process to finish, and exit if a nonzero status is found. */
1499 collect_wait (const char *prog
, struct pex_obj
*pex
)
1503 if (!pex_get_status (pex
, 1, &status
))
1504 fatal_perror ("can't get program status");
1509 if (WIFSIGNALED (status
))
1511 int sig
= WTERMSIG (status
);
1512 error ("%s terminated with signal %d [%s]%s",
1513 prog
, sig
, strsignal(sig
),
1514 WCOREDUMP(status
) ? ", core dumped" : "");
1515 collect_exit (FATAL_EXIT_CODE
);
1518 if (WIFEXITED (status
))
1519 return WEXITSTATUS (status
);
1525 do_wait (const char *prog
, struct pex_obj
*pex
)
1527 int ret
= collect_wait (prog
, pex
);
1530 error ("%s returned %d exit status", prog
, ret
);
1536 /* Execute a program, and wait for the reply. */
1539 collect_execute (const char *prog
, char **argv
, const char *outname
,
1540 const char *errname
)
1542 struct pex_obj
*pex
;
1552 fprintf (stderr
, "%s", argv
[0]);
1554 notice ("[cannot find %s]", prog
);
1556 for (p_argv
= &argv
[1]; (str
= *p_argv
) != (char *) 0; p_argv
++)
1557 fprintf (stderr
, " %s", str
);
1559 fprintf (stderr
, "\n");
1565 /* If we cannot find a program we need, complain error. Do this here
1566 since we might not end up needing something that we could not find. */
1569 fatal ("cannot find '%s'", prog
);
1571 pex
= pex_init (0, "collect2", NULL
);
1573 fatal_perror ("pex_init failed");
1575 errmsg
= pex_run (pex
, PEX_LAST
| PEX_SEARCH
, argv
[0], argv
, outname
,
1582 fatal_perror (errmsg
);
1592 fork_execute (const char *prog
, char **argv
)
1594 struct pex_obj
*pex
;
1596 pex
= collect_execute (prog
, argv
, NULL
, NULL
);
1597 do_wait (prog
, pex
);
1600 /* Unlink a file unless we are debugging. */
1603 maybe_unlink (const char *file
)
1608 notice ("[Leaving %s]\n", file
);
1612 static long sequence_number
= 0;
1614 /* Add a name to a linked list. */
1617 add_to_list (struct head
*head_ptr
, const char *name
)
1619 struct id
*newid
= xcalloc (sizeof (struct id
) + strlen (name
), 1);
1621 strcpy (newid
->name
, name
);
1623 if (head_ptr
->first
)
1624 head_ptr
->last
->next
= newid
;
1626 head_ptr
->first
= newid
;
1628 /* Check for duplicate symbols. */
1629 for (p
= head_ptr
->first
;
1630 strcmp (name
, p
->name
) != 0;
1635 head_ptr
->last
->next
= 0;
1640 newid
->sequence
= ++sequence_number
;
1641 head_ptr
->last
= newid
;
1645 /* Grab the init priority number from an init function name that
1646 looks like "_GLOBAL_.I.12345.foo". */
1649 extract_init_priority (const char *name
)
1653 while (name
[pos
] == '_')
1655 pos
+= 10; /* strlen ("GLOBAL__X_") */
1657 /* Extract init_p number from ctor/dtor name. */
1658 pri
= atoi (name
+ pos
);
1659 return pri
? pri
: DEFAULT_INIT_PRIORITY
;
1662 /* Insertion sort the ids from ctor/dtor list HEAD_PTR in descending order.
1663 ctors will be run from right to left, dtors from left to right. */
1666 sort_ids (struct head
*head_ptr
)
1668 /* id holds the current element to insert. id_next holds the next
1669 element to insert. id_ptr iterates through the already sorted elements
1670 looking for the place to insert id. */
1671 struct id
*id
, *id_next
, **id_ptr
;
1673 id
= head_ptr
->first
;
1675 /* We don't have any sorted elements yet. */
1676 head_ptr
->first
= NULL
;
1678 for (; id
; id
= id_next
)
1681 id
->sequence
= extract_init_priority (id
->name
);
1683 for (id_ptr
= &(head_ptr
->first
); ; id_ptr
= &((*id_ptr
)->next
))
1685 /* If the sequence numbers are the same, we put the id from the
1686 file later on the command line later in the list. */
1687 || id
->sequence
> (*id_ptr
)->sequence
1688 /* Hack: do lexical compare, too.
1689 || (id->sequence == (*id_ptr)->sequence
1690 && strcmp (id->name, (*id_ptr)->name) > 0) */
1699 /* Now set the sequence numbers properly so write_c_file works. */
1700 for (id
= head_ptr
->first
; id
; id
= id
->next
)
1701 id
->sequence
= ++sequence_number
;
1704 /* Write: `prefix', the names on list LIST, `suffix'. */
1707 write_list (FILE *stream
, const char *prefix
, struct id
*list
)
1711 fprintf (stream
, "%sx%d,\n", prefix
, list
->sequence
);
1716 #if LINK_ELIMINATE_DUPLICATE_LDIRECTORIES
1717 /* Given a STRING, return nonzero if it occurs in the list in range
1718 [ARGS_BEGIN,ARGS_END). */
1721 is_in_args (const char *string
, const char **args_begin
,
1722 const char **args_end
)
1724 const char **args_pointer
;
1725 for (args_pointer
= args_begin
; args_pointer
!= args_end
; ++args_pointer
)
1726 if (strcmp (string
, *args_pointer
) == 0)
1730 #endif /* LINK_ELIMINATE_DUPLICATE_LDIRECTORIES */
1732 #ifdef COLLECT_EXPORT_LIST
1733 /* This function is really used only on AIX, but may be useful. */
1736 is_in_list (const char *prefix
, struct id
*list
)
1740 if (!strcmp (prefix
, list
->name
)) return 1;
1746 #endif /* COLLECT_EXPORT_LIST */
1748 /* Added for debugging purpose. */
1749 #ifdef COLLECT_EXPORT_LIST
1751 dump_list (FILE *stream
, const char *prefix
, struct id
*list
)
1755 fprintf (stream
, "%s%s,\n", prefix
, list
->name
);
1763 dump_prefix_list (FILE *stream
, const char *prefix
, struct prefix_list
*list
)
1767 fprintf (stream
, "%s%s,\n", prefix
, list
->prefix
);
1774 write_list_with_asm (FILE *stream
, const char *prefix
, struct id
*list
)
1778 fprintf (stream
, "%sx%d __asm__ (\"%s\");\n",
1779 prefix
, list
->sequence
, list
->name
);
1784 /* Write out the constructor and destructor tables statically (for a shared
1785 object), along with the functions to execute them. */
1788 write_c_file_stat (FILE *stream
, const char *name ATTRIBUTE_UNUSED
)
1792 int frames
= (frame_tables
.number
> 0);
1794 /* Figure out name of output_file, stripping off .so version. */
1795 p
= strrchr (output_file
, '/');
1811 if (strncmp (q
, ".so", 3) == 0)
1820 /* q points to null at end of the string (or . of the .so version) */
1821 prefix
= xmalloc (q
- p
+ 1);
1822 strncpy (prefix
, p
, q
- p
);
1824 for (r
= prefix
; *r
; r
++)
1825 if (!ISALNUM ((unsigned char)*r
))
1828 notice ("\nwrite_c_file - output name is %s, prefix is %s\n",
1829 output_file
, prefix
);
1831 initname
= concat ("_GLOBAL__FI_", prefix
, NULL
);
1832 fininame
= concat ("_GLOBAL__FD_", prefix
, NULL
);
1836 /* Write the tables as C code. */
1838 fprintf (stream
, "static int count;\n");
1839 fprintf (stream
, "typedef void entry_pt();\n");
1840 write_list_with_asm (stream
, "extern entry_pt ", constructors
.first
);
1844 write_list_with_asm (stream
, "extern void *", frame_tables
.first
);
1846 fprintf (stream
, "\tstatic void *frame_table[] = {\n");
1847 write_list (stream
, "\t\t&", frame_tables
.first
);
1848 fprintf (stream
, "\t0\n};\n");
1850 /* This must match what's in frame.h. */
1851 fprintf (stream
, "struct object {\n");
1852 fprintf (stream
, " void *pc_begin;\n");
1853 fprintf (stream
, " void *pc_end;\n");
1854 fprintf (stream
, " void *fde_begin;\n");
1855 fprintf (stream
, " void *fde_array;\n");
1856 fprintf (stream
, " __SIZE_TYPE__ count;\n");
1857 fprintf (stream
, " struct object *next;\n");
1858 fprintf (stream
, "};\n");
1860 fprintf (stream
, "extern void __register_frame_info_table (void *, struct object *);\n");
1861 fprintf (stream
, "extern void *__deregister_frame_info (void *);\n");
1863 fprintf (stream
, "static void reg_frame () {\n");
1864 fprintf (stream
, "\tstatic struct object ob;\n");
1865 fprintf (stream
, "\t__register_frame_info_table (frame_table, &ob);\n");
1866 fprintf (stream
, "\t}\n");
1868 fprintf (stream
, "static void dereg_frame () {\n");
1869 fprintf (stream
, "\t__deregister_frame_info (frame_table);\n");
1870 fprintf (stream
, "\t}\n");
1873 fprintf (stream
, "void %s() {\n", initname
);
1874 if (constructors
.number
> 0 || frames
)
1876 fprintf (stream
, "\tstatic entry_pt *ctors[] = {\n");
1877 write_list (stream
, "\t\t", constructors
.first
);
1879 fprintf (stream
, "\treg_frame,\n");
1880 fprintf (stream
, "\t};\n");
1881 fprintf (stream
, "\tentry_pt **p;\n");
1882 fprintf (stream
, "\tif (count++ != 0) return;\n");
1883 fprintf (stream
, "\tp = ctors + %d;\n", constructors
.number
+ frames
);
1884 fprintf (stream
, "\twhile (p > ctors) (*--p)();\n");
1887 fprintf (stream
, "\t++count;\n");
1888 fprintf (stream
, "}\n");
1889 write_list_with_asm (stream
, "extern entry_pt ", destructors
.first
);
1890 fprintf (stream
, "void %s() {\n", fininame
);
1891 if (destructors
.number
> 0 || frames
)
1893 fprintf (stream
, "\tstatic entry_pt *dtors[] = {\n");
1894 write_list (stream
, "\t\t", destructors
.first
);
1896 fprintf (stream
, "\tdereg_frame,\n");
1897 fprintf (stream
, "\t};\n");
1898 fprintf (stream
, "\tentry_pt **p;\n");
1899 fprintf (stream
, "\tif (--count != 0) return;\n");
1900 fprintf (stream
, "\tp = dtors;\n");
1901 fprintf (stream
, "\twhile (p < dtors + %d) (*p++)();\n",
1902 destructors
.number
+ frames
);
1904 fprintf (stream
, "}\n");
1908 COLLECT_SHARED_INIT_FUNC(stream
, initname
);
1909 COLLECT_SHARED_FINI_FUNC(stream
, fininame
);
1913 /* Write the constructor/destructor tables. */
1915 #ifndef LD_INIT_SWITCH
1917 write_c_file_glob (FILE *stream
, const char *name ATTRIBUTE_UNUSED
)
1919 /* Write the tables as C code. */
1921 int frames
= (frame_tables
.number
> 0);
1923 fprintf (stream
, "typedef void entry_pt();\n\n");
1925 write_list_with_asm (stream
, "extern entry_pt ", constructors
.first
);
1929 write_list_with_asm (stream
, "extern void *", frame_tables
.first
);
1931 fprintf (stream
, "\tstatic void *frame_table[] = {\n");
1932 write_list (stream
, "\t\t&", frame_tables
.first
);
1933 fprintf (stream
, "\t0\n};\n");
1935 /* This must match what's in frame.h. */
1936 fprintf (stream
, "struct object {\n");
1937 fprintf (stream
, " void *pc_begin;\n");
1938 fprintf (stream
, " void *pc_end;\n");
1939 fprintf (stream
, " void *fde_begin;\n");
1940 fprintf (stream
, " void *fde_array;\n");
1941 fprintf (stream
, " __SIZE_TYPE__ count;\n");
1942 fprintf (stream
, " struct object *next;\n");
1943 fprintf (stream
, "};\n");
1945 fprintf (stream
, "extern void __register_frame_info_table (void *, struct object *);\n");
1946 fprintf (stream
, "extern void *__deregister_frame_info (void *);\n");
1948 fprintf (stream
, "static void reg_frame () {\n");
1949 fprintf (stream
, "\tstatic struct object ob;\n");
1950 fprintf (stream
, "\t__register_frame_info_table (frame_table, &ob);\n");
1951 fprintf (stream
, "\t}\n");
1953 fprintf (stream
, "static void dereg_frame () {\n");
1954 fprintf (stream
, "\t__deregister_frame_info (frame_table);\n");
1955 fprintf (stream
, "\t}\n");
1958 fprintf (stream
, "\nentry_pt * __CTOR_LIST__[] = {\n");
1959 fprintf (stream
, "\t(entry_pt *) %d,\n", constructors
.number
+ frames
);
1960 write_list (stream
, "\t", constructors
.first
);
1962 fprintf (stream
, "\treg_frame,\n");
1963 fprintf (stream
, "\t0\n};\n\n");
1965 write_list_with_asm (stream
, "extern entry_pt ", destructors
.first
);
1967 fprintf (stream
, "\nentry_pt * __DTOR_LIST__[] = {\n");
1968 fprintf (stream
, "\t(entry_pt *) %d,\n", destructors
.number
+ frames
);
1969 write_list (stream
, "\t", destructors
.first
);
1971 fprintf (stream
, "\tdereg_frame,\n");
1972 fprintf (stream
, "\t0\n};\n\n");
1974 fprintf (stream
, "extern entry_pt %s;\n", NAME__MAIN
);
1975 fprintf (stream
, "entry_pt *__main_reference = %s;\n\n", NAME__MAIN
);
1977 #endif /* ! LD_INIT_SWITCH */
1980 write_c_file (FILE *stream
, const char *name
)
1982 fprintf (stream
, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n");
1983 #ifndef LD_INIT_SWITCH
1985 write_c_file_glob (stream
, name
);
1988 write_c_file_stat (stream
, name
);
1989 fprintf (stream
, "#ifdef __cplusplus\n}\n#endif\n");
1992 #ifdef COLLECT_EXPORT_LIST
1994 write_aix_file (FILE *stream
, struct id
*list
)
1996 for (; list
; list
= list
->next
)
1998 fputs (list
->name
, stream
);
1999 putc ('\n', stream
);
2004 #ifdef OBJECT_FORMAT_NONE
2006 /* Generic version to scan the name list of the loaded program for
2007 the symbols g++ uses for static constructors and destructors.
2009 The constructor table begins at __CTOR_LIST__ and contains a count
2010 of the number of pointers (or -1 if the constructors are built in a
2011 separate section by the linker), followed by the pointers to the
2012 constructor functions, terminated with a null pointer. The
2013 destructor table has the same format, and begins at __DTOR_LIST__. */
2016 scan_prog_file (const char *prog_name
, enum pass which_pass
)
2018 void (*int_handler
) (int);
2020 void (*quit_handler
) (int);
2022 char *real_nm_argv
[4];
2023 const char **nm_argv
= (const char **) real_nm_argv
;
2025 struct pex_obj
*pex
;
2031 if (which_pass
== PASS_SECOND
)
2034 /* If we do not have an `nm', complain. */
2035 if (nm_file_name
== 0)
2036 fatal ("cannot find 'nm'");
2038 nm_argv
[argc
++] = nm_file_name
;
2039 if (NM_FLAGS
[0] != '\0')
2040 nm_argv
[argc
++] = NM_FLAGS
;
2042 nm_argv
[argc
++] = prog_name
;
2043 nm_argv
[argc
++] = (char *) 0;
2045 /* Trace if needed. */
2048 const char **p_argv
;
2051 for (p_argv
= &nm_argv
[0]; (str
= *p_argv
) != (char *) 0; p_argv
++)
2052 fprintf (stderr
, " %s", str
);
2054 fprintf (stderr
, "\n");
2060 pex
= pex_init (PEX_USE_PIPES
, "collect2", NULL
);
2062 fatal_perror ("pex_init failed");
2064 errmsg
= pex_run (pex
, 0, nm_file_name
, real_nm_argv
, NULL
, NULL
, &err
);
2070 fatal_perror (errmsg
);
2076 int_handler
= (void (*) (int)) signal (SIGINT
, SIG_IGN
);
2078 quit_handler
= (void (*) (int)) signal (SIGQUIT
, SIG_IGN
);
2081 inf
= pex_read_output (pex
, 0);
2083 fatal_perror ("can't open nm output");
2086 fprintf (stderr
, "\nnm output with constructors/destructors.\n");
2088 /* Read each line of nm output. */
2089 while (fgets (buf
, sizeof buf
, inf
) != (char *) 0)
2094 /* If it contains a constructor or destructor name, add the name
2095 to the appropriate list. */
2097 for (p
= buf
; (ch
= *p
) != '\0' && ch
!= '\n' && ch
!= '_'; p
++)
2098 if (ch
== ' ' && p
[1] == 'U' && p
[2] == ' ')
2105 /* Find the end of the symbol name.
2106 Do not include `|', because Encore nm can tack that on the end. */
2107 for (end
= p
; (ch2
= *end
) != '\0' && !ISSPACE (ch2
) && ch2
!= '|';
2113 switch (is_ctor_dtor (name
))
2116 if (which_pass
!= PASS_LIB
)
2117 add_to_list (&constructors
, name
);
2121 if (which_pass
!= PASS_LIB
)
2122 add_to_list (&destructors
, name
);
2126 if (which_pass
!= PASS_LIB
)
2127 fatal ("init function found in object %s", prog_name
);
2128 #ifndef LD_INIT_SWITCH
2129 add_to_list (&constructors
, name
);
2134 if (which_pass
!= PASS_LIB
)
2135 fatal ("fini function found in object %s", prog_name
);
2136 #ifndef LD_FINI_SWITCH
2137 add_to_list (&destructors
, name
);
2142 if (which_pass
!= PASS_LIB
)
2143 add_to_list (&frame_tables
, name
);
2146 default: /* not a constructor or destructor */
2151 fprintf (stderr
, "\t%s\n", buf
);
2155 fprintf (stderr
, "\n");
2157 do_wait (nm_file_name
, pex
);
2159 signal (SIGINT
, int_handler
);
2161 signal (SIGQUIT
, quit_handler
);
2167 /* Use the List Dynamic Dependencies program to find shared libraries that
2168 the output file depends upon and their initialization/finalization
2169 routines, if any. */
2172 scan_libraries (const char *prog_name
)
2174 static struct head libraries
; /* list of shared libraries found */
2176 void (*int_handler
) (int);
2178 void (*quit_handler
) (int);
2180 char *real_ldd_argv
[4];
2181 const char **ldd_argv
= (const char **) real_ldd_argv
;
2183 struct pex_obj
*pex
;
2189 /* If we do not have an `ldd', complain. */
2190 if (ldd_file_name
== 0)
2192 error ("cannot find 'ldd'");
2196 ldd_argv
[argc
++] = ldd_file_name
;
2197 ldd_argv
[argc
++] = prog_name
;
2198 ldd_argv
[argc
++] = (char *) 0;
2200 /* Trace if needed. */
2203 const char **p_argv
;
2206 for (p_argv
= &ldd_argv
[0]; (str
= *p_argv
) != (char *) 0; p_argv
++)
2207 fprintf (stderr
, " %s", str
);
2209 fprintf (stderr
, "\n");
2215 pex
= pex_init (PEX_USE_PIPES
, "collect2", NULL
);
2217 fatal_perror ("pex_init failed");
2219 errmsg
= pex_run (pex
, 0, ldd_file_name
, real_ldd_argv
, NULL
, NULL
, &err
);
2225 fatal_perror (errmsg
);
2231 int_handler
= (void (*) (int)) signal (SIGINT
, SIG_IGN
);
2233 quit_handler
= (void (*) (int)) signal (SIGQUIT
, SIG_IGN
);
2236 inf
= pex_read_output (pex
, 0);
2238 fatal_perror ("can't open ldd output");
2241 notice ("\nldd output with constructors/destructors.\n");
2243 /* Read each line of ldd output. */
2244 while (fgets (buf
, sizeof buf
, inf
) != (char *) 0)
2247 char *name
, *end
, *p
= buf
;
2249 /* Extract names of libraries and add to list. */
2250 PARSE_LDD_OUTPUT (p
);
2255 if (strncmp (name
, "not found", sizeof ("not found") - 1) == 0)
2256 fatal ("dynamic dependency %s not found", buf
);
2258 /* Find the end of the symbol name. */
2260 (ch2
= *end
) != '\0' && ch2
!= '\n' && !ISSPACE (ch2
) && ch2
!= '|';
2265 if (access (name
, R_OK
) == 0)
2266 add_to_list (&libraries
, name
);
2268 fatal ("unable to open dynamic dependency '%s'", buf
);
2271 fprintf (stderr
, "\t%s\n", buf
);
2274 fprintf (stderr
, "\n");
2276 do_wait (ldd_file_name
, pex
);
2278 signal (SIGINT
, int_handler
);
2280 signal (SIGQUIT
, quit_handler
);
2283 /* Now iterate through the library list adding their symbols to
2285 for (list
= libraries
.first
; list
; list
= list
->next
)
2286 scan_prog_file (list
->name
, PASS_LIB
);
2289 #endif /* LDD_SUFFIX */
2291 #endif /* OBJECT_FORMAT_NONE */
2295 * COFF specific stuff.
2298 #ifdef OBJECT_FORMAT_COFF
2300 #if defined (EXTENDED_COFF)
2302 # define GCC_SYMBOLS(X) (SYMHEADER(X).isymMax + SYMHEADER(X).iextMax)
2303 # define GCC_SYMENT SYMR
2304 # define GCC_OK_SYMBOL(X) ((X).st == stProc || (X).st == stGlobal)
2305 # define GCC_SYMINC(X) (1)
2306 # define GCC_SYMZERO(X) (SYMHEADER(X).isymMax)
2307 # define GCC_CHECK_HDR(X) (PSYMTAB(X) != 0)
2311 # define GCC_SYMBOLS(X) (HEADER(ldptr).f_nsyms)
2312 # define GCC_SYMENT SYMENT
2313 # if defined (C_WEAKEXT)
2314 # define GCC_OK_SYMBOL(X) \
2315 (((X).n_sclass == C_EXT || (X).n_sclass == C_WEAKEXT) && \
2316 ((X).n_scnum > N_UNDEF) && \
2318 || (((X).n_type & N_TMASK) == (DT_NON << N_BTSHFT) \
2319 || ((X).n_type & N_TMASK) == (DT_FCN << N_BTSHFT))))
2320 # define GCC_UNDEF_SYMBOL(X) \
2321 (((X).n_sclass == C_EXT || (X).n_sclass == C_WEAKEXT) && \
2322 ((X).n_scnum == N_UNDEF))
2324 # define GCC_OK_SYMBOL(X) \
2325 (((X).n_sclass == C_EXT) && \
2326 ((X).n_scnum > N_UNDEF) && \
2328 || (((X).n_type & N_TMASK) == (DT_NON << N_BTSHFT) \
2329 || ((X).n_type & N_TMASK) == (DT_FCN << N_BTSHFT))))
2330 # define GCC_UNDEF_SYMBOL(X) \
2331 (((X).n_sclass == C_EXT) && ((X).n_scnum == N_UNDEF))
2333 # define GCC_SYMINC(X) ((X).n_numaux+1)
2334 # define GCC_SYMZERO(X) 0
2336 /* 0757 = U803XTOCMAGIC (AIX 4.3) and 0767 = U64_TOCMAGIC (AIX V5) */
2338 # define GCC_CHECK_HDR(X) \
2339 ((HEADER (X).f_magic == U802TOCMAGIC && ! aix64_flag) \
2340 || (HEADER (X).f_magic == 0767 && aix64_flag))
2342 # define GCC_CHECK_HDR(X) \
2343 ((HEADER (X).f_magic == U802TOCMAGIC && ! aix64_flag) \
2344 || (HEADER (X).f_magic == 0757 && aix64_flag))
2349 #ifdef COLLECT_EXPORT_LIST
2350 /* Array of standard AIX libraries which should not
2351 be scanned for ctors/dtors. */
2352 static const char *const aix_std_libs
[] = {
2360 "/usr/lib/libc_r.a",
2361 "/usr/lib/libm_r.a",
2362 "/usr/lib/threads/libc.a",
2363 "/usr/ccs/lib/libc.a",
2364 "/usr/ccs/lib/libm.a",
2365 "/usr/ccs/lib/libc_r.a",
2366 "/usr/ccs/lib/libm_r.a",
2370 /* This function checks the filename and returns 1
2371 if this name matches the location of a standard AIX library. */
2372 static int ignore_library (const char *);
2374 ignore_library (const char *name
)
2376 const char *const *p
= &aix_std_libs
[0];
2377 while (*p
++ != NULL
)
2378 if (! strcmp (name
, *p
)) return 1;
2381 #endif /* COLLECT_EXPORT_LIST */
2383 #if defined (HAVE_DECL_LDGETNAME) && !HAVE_DECL_LDGETNAME
2384 extern char *ldgetname (LDFILE
*, GCC_SYMENT
*);
2387 /* COFF version to scan the name list of the loaded program for
2388 the symbols g++ uses for static constructors and destructors.
2390 The constructor table begins at __CTOR_LIST__ and contains a count
2391 of the number of pointers (or -1 if the constructors are built in a
2392 separate section by the linker), followed by the pointers to the
2393 constructor functions, terminated with a null pointer. The
2394 destructor table has the same format, and begins at __DTOR_LIST__. */
2397 scan_prog_file (const char *prog_name
, enum pass which_pass
)
2399 LDFILE
*ldptr
= NULL
;
2400 int sym_index
, sym_count
;
2403 if (which_pass
!= PASS_FIRST
&& which_pass
!= PASS_OBJ
)
2406 #ifdef COLLECT_EXPORT_LIST
2407 /* We do not need scanning for some standard C libraries. */
2408 if (which_pass
== PASS_FIRST
&& ignore_library (prog_name
))
2411 /* On AIX we have a loop, because there is not much difference
2412 between an object and an archive. This trick allows us to
2413 eliminate scan_libraries() function. */
2417 /* Some platforms (e.g. OSF4) declare ldopen as taking a
2418 non-const char * filename parameter, even though it will not
2419 modify that string. So we must cast away const-ness here,
2420 which will cause -Wcast-qual to burp. */
2421 if ((ldptr
= ldopen ((char *)prog_name
, ldptr
)) != NULL
)
2423 if (! MY_ISCOFF (HEADER (ldptr
).f_magic
))
2424 fatal ("%s: not a COFF file", prog_name
);
2426 if (GCC_CHECK_HDR (ldptr
))
2428 sym_count
= GCC_SYMBOLS (ldptr
);
2429 sym_index
= GCC_SYMZERO (ldptr
);
2431 #ifdef COLLECT_EXPORT_LIST
2432 /* Is current archive member a shared object? */
2433 is_shared
= HEADER (ldptr
).f_flags
& F_SHROBJ
;
2436 while (sym_index
< sym_count
)
2440 if (ldtbread (ldptr
, sym_index
, &symbol
) <= 0)
2442 sym_index
+= GCC_SYMINC (symbol
);
2444 if (GCC_OK_SYMBOL (symbol
))
2448 if ((name
= ldgetname (ldptr
, &symbol
)) == NULL
)
2449 continue; /* Should never happen. */
2451 #ifdef XCOFF_DEBUGGING_INFO
2452 /* All AIX function names have a duplicate entry
2453 beginning with a dot. */
2458 switch (is_ctor_dtor (name
))
2462 add_to_list (&constructors
, name
);
2463 #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
2464 if (which_pass
== PASS_OBJ
)
2465 add_to_list (&exports
, name
);
2471 add_to_list (&destructors
, name
);
2472 #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
2473 if (which_pass
== PASS_OBJ
)
2474 add_to_list (&exports
, name
);
2478 #ifdef COLLECT_EXPORT_LIST
2480 #ifndef LD_INIT_SWITCH
2482 add_to_list (&constructors
, name
);
2487 #ifndef LD_INIT_SWITCH
2489 add_to_list (&destructors
, name
);
2496 add_to_list (&frame_tables
, name
);
2497 #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
2498 if (which_pass
== PASS_OBJ
)
2499 add_to_list (&exports
, name
);
2503 default: /* not a constructor or destructor */
2504 #ifdef COLLECT_EXPORT_LIST
2505 /* Explicitly export all global symbols when
2506 building a shared object on AIX, but do not
2507 re-export symbols from another shared object
2508 and do not export symbols if the user
2509 provides an explicit export list. */
2510 if (shared_obj
&& !is_shared
2511 && which_pass
== PASS_OBJ
&& !export_flag
)
2512 add_to_list (&exports
, name
);
2518 #if !defined(EXTENDED_COFF)
2519 fprintf (stderr
, "\tsec=%d class=%d type=%s%o %s\n",
2520 symbol
.n_scnum
, symbol
.n_sclass
,
2521 (symbol
.n_type
? "0" : ""), symbol
.n_type
,
2525 "\tiss = %5d, value = %5ld, index = %5d, name = %s\n",
2526 symbol
.iss
, (long) symbol
.value
, symbol
.index
, name
);
2531 #ifdef COLLECT_EXPORT_LIST
2534 /* If archive contains both 32-bit and 64-bit objects,
2535 we want to skip objects in other mode so mismatch normal. */
2537 fprintf (stderr
, "%s : magic=%o aix64=%d mismatch\n",
2538 prog_name
, HEADER (ldptr
).f_magic
, aix64_flag
);
2544 fatal ("%s: cannot open as COFF file", prog_name
);
2546 #ifdef COLLECT_EXPORT_LIST
2547 /* On AIX loop continues while there are more members in archive. */
2549 while (ldclose (ldptr
) == FAILURE
);
2551 /* Otherwise we simply close ldptr. */
2552 (void) ldclose(ldptr
);
2555 #endif /* OBJECT_FORMAT_COFF */
2557 #ifdef COLLECT_EXPORT_LIST
2558 /* Given a library name without "lib" prefix, this function
2559 returns a full library name including a path. */
2561 resolve_lib_name (const char *name
)
2565 /* Library extensions for AIX dynamic linking. */
2566 const char * const libexts
[2] = {"a", "so"};
2568 for (i
= 0; libpaths
[i
]; i
++)
2569 if (libpaths
[i
]->max_len
> l
)
2570 l
= libpaths
[i
]->max_len
;
2572 lib_buf
= xmalloc (l
+ strlen(name
) + 10);
2574 for (i
= 0; libpaths
[i
]; i
++)
2576 struct prefix_list
*list
= libpaths
[i
]->plist
;
2577 for (; list
; list
= list
->next
)
2579 /* The following lines are needed because path_prefix list
2580 may contain directories both with trailing '/' and
2583 if (list
->prefix
[strlen(list
->prefix
)-1] != '/')
2585 for (j
= 0; j
< 2; j
++)
2587 sprintf (lib_buf
, "%s%slib%s.%s",
2588 list
->prefix
, p
, name
,
2589 libexts
[(j
+ aixrtl_flag
) % 2]);
2590 if (debug
) fprintf (stderr
, "searching for: %s\n", lib_buf
);
2591 if (file_exists (lib_buf
))
2593 if (debug
) fprintf (stderr
, "found: %s\n", lib_buf
);
2600 fprintf (stderr
, "not found\n");
2602 fatal ("library lib%s not found", name
);
2605 #endif /* COLLECT_EXPORT_LIST */