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, 51 Franklin Street, Fifth Floor, 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. */
56 #ifdef CROSS_DIRECTORY_STRUCTURE
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 **);
274 /* Delete tempfiles and exit function. */
277 collect_exit (int status
)
279 if (c_file
!= 0 && c_file
[0])
280 maybe_unlink (c_file
);
282 if (o_file
!= 0 && o_file
[0])
283 maybe_unlink (o_file
);
285 #ifdef COLLECT_EXPORT_LIST
286 if (export_file
!= 0 && export_file
[0])
287 maybe_unlink (export_file
);
290 if (ldout
!= 0 && ldout
[0])
292 dump_file (ldout
, stdout
);
293 maybe_unlink (ldout
);
296 if (lderrout
!= 0 && lderrout
[0])
298 dump_file (lderrout
, stderr
);
299 maybe_unlink (lderrout
);
302 if (status
!= 0 && output_file
!= 0 && output_file
[0])
303 maybe_unlink (output_file
);
309 /* Notify user of a non-error. */
311 notice (const char *cmsgid
, ...)
315 va_start (ap
, cmsgid
);
316 vfprintf (stderr
, _(cmsgid
), ap
);
320 /* Die when sys call fails. */
323 fatal_perror (const char * cmsgid
, ...)
328 va_start (ap
, cmsgid
);
329 fprintf (stderr
, "collect2: ");
330 vfprintf (stderr
, _(cmsgid
), ap
);
331 fprintf (stderr
, ": %s\n", xstrerror (e
));
334 collect_exit (FATAL_EXIT_CODE
);
340 fatal (const char * cmsgid
, ...)
344 va_start (ap
, cmsgid
);
345 fprintf (stderr
, "collect2: ");
346 vfprintf (stderr
, _(cmsgid
), ap
);
347 fprintf (stderr
, "\n");
350 collect_exit (FATAL_EXIT_CODE
);
353 /* Write error message. */
356 error (const char * gmsgid
, ...)
360 va_start (ap
, gmsgid
);
361 fprintf (stderr
, "collect2: ");
362 vfprintf (stderr
, _(gmsgid
), ap
);
363 fprintf (stderr
, "\n");
367 /* In case obstack is linked in, and abort is defined to fancy_abort,
368 provide a default entry. */
371 fancy_abort (const char *file
, int line
, const char *func
)
373 fatal ("internal gcc abort in %s, at %s:%d", func
, file
, line
);
379 if (c_file
!= 0 && c_file
[0])
380 maybe_unlink (c_file
);
382 if (o_file
!= 0 && o_file
[0])
383 maybe_unlink (o_file
);
385 if (ldout
!= 0 && ldout
[0])
386 maybe_unlink (ldout
);
388 if (lderrout
!= 0 && lderrout
[0])
389 maybe_unlink (lderrout
);
391 #ifdef COLLECT_EXPORT_LIST
392 if (export_file
!= 0 && export_file
[0])
393 maybe_unlink (export_file
);
396 signal (signo
, SIG_DFL
);
402 file_exists (const char *name
)
404 return access (name
, R_OK
) == 0;
407 /* Parse a reasonable subset of shell quoting syntax. */
410 extract_string (const char **pp
)
423 obstack_1grow (&temporary_obstack
, c
);
424 else if (! inside
&& c
== ' ')
426 else if (! inside
&& c
== '\\')
431 obstack_1grow (&temporary_obstack
, c
);
434 obstack_1grow (&temporary_obstack
, '\0');
436 return XOBFINISH (&temporary_obstack
, char *);
440 dump_file (const char *name
, FILE *to
)
442 FILE *stream
= fopen (name
, "r");
449 while (c
= getc (stream
),
450 c
!= EOF
&& (ISIDNUM (c
) || c
== '$' || c
== '.'))
451 obstack_1grow (&temporary_obstack
, c
);
452 if (obstack_object_size (&temporary_obstack
) > 0)
454 const char *word
, *p
;
456 obstack_1grow (&temporary_obstack
, '\0');
457 word
= XOBFINISH (&temporary_obstack
, const char *);
460 ++word
, putc ('.', to
);
462 if (!strncmp (p
, USER_LABEL_PREFIX
, strlen (USER_LABEL_PREFIX
)))
463 p
+= strlen (USER_LABEL_PREFIX
);
465 #ifdef HAVE_LD_DEMANGLE
471 result
= cplus_demangle (p
, DMGL_PARAMS
| DMGL_ANSI
| DMGL_VERBOSE
);
479 diff
= strlen (word
) - strlen (result
);
480 while (diff
> 0 && c
== ' ')
481 --diff
, putc (' ', to
);
482 while (diff
< 0 && c
== ' ')
483 ++diff
, c
= getc (stream
);
491 obstack_free (&temporary_obstack
, temporary_firstobj
);
500 /* Decide whether the given symbol is: a constructor (1), a destructor
501 (2), a routine in a shared object that calls all the constructors
502 (3) or destructors (4), a DWARF exception-handling table (5), or
503 nothing special (0). */
506 is_ctor_dtor (const char *s
)
508 struct names
{ const char *const name
; const int len
; const int ret
;
509 const int two_underscores
; };
511 const struct names
*p
;
513 const char *orig_s
= s
;
515 static const struct names special
[] = {
516 #ifndef NO_DOLLAR_IN_LABEL
517 { "GLOBAL__I$", sizeof ("GLOBAL__I$")-1, 1, 0 },
518 { "GLOBAL__D$", sizeof ("GLOBAL__D$")-1, 2, 0 },
520 #ifndef NO_DOT_IN_LABEL
521 { "GLOBAL__I.", sizeof ("GLOBAL__I.")-1, 1, 0 },
522 { "GLOBAL__D.", sizeof ("GLOBAL__D.")-1, 2, 0 },
523 #endif /* NO_DOT_IN_LABEL */
524 #endif /* NO_DOLLAR_IN_LABEL */
525 { "GLOBAL__I_", sizeof ("GLOBAL__I_")-1, 1, 0 },
526 { "GLOBAL__D_", sizeof ("GLOBAL__D_")-1, 2, 0 },
527 { "GLOBAL__F_", sizeof ("GLOBAL__F_")-1, 5, 0 },
528 { "GLOBAL__FI_", sizeof ("GLOBAL__FI_")-1, 3, 0 },
529 { "GLOBAL__FD_", sizeof ("GLOBAL__FD_")-1, 4, 0 },
533 while ((ch
= *s
) == '_')
539 for (p
= &special
[0]; p
->len
> 0; p
++)
542 && (!p
->two_underscores
|| ((s
- orig_s
) >= 2))
543 && strncmp(s
, p
->name
, p
->len
) == 0)
551 /* We maintain two prefix lists: one from COMPILER_PATH environment variable
552 and one from the PATH variable. */
554 static struct path_prefix cpath
, path
;
556 #ifdef CROSS_DIRECTORY_STRUCTURE
557 /* This is the name of the target machine. We use it to form the name
558 of the files to execute. */
560 static const char *const target_machine
= TARGET_MACHINE
;
563 /* Search for NAME using prefix list PPREFIX. We only look for executable
566 Return 0 if not found, otherwise return its name, allocated with malloc. */
569 find_a_file (struct path_prefix
*pprefix
, const char *name
)
572 struct prefix_list
*pl
;
573 int len
= pprefix
->max_len
+ strlen (name
) + 1;
576 fprintf (stderr
, "Looking for '%s'\n", name
);
578 #ifdef HOST_EXECUTABLE_SUFFIX
579 len
+= strlen (HOST_EXECUTABLE_SUFFIX
);
582 temp
= XNEWVEC (char, len
);
584 /* Determine the filename to execute (special case for absolute paths). */
587 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
588 || (*name
&& name
[1] == ':')
592 if (access (name
, X_OK
) == 0)
597 fprintf (stderr
, " - found: absolute path\n");
602 #ifdef HOST_EXECUTABLE_SUFFIX
603 /* Some systems have a suffix for executable files.
604 So try appending that. */
606 strcat (temp
, HOST_EXECUTABLE_SUFFIX
);
608 if (access (temp
, X_OK
) == 0)
613 fprintf (stderr
, " - failed to locate using absolute path\n");
616 for (pl
= pprefix
->plist
; pl
; pl
= pl
->next
)
620 strcpy (temp
, pl
->prefix
);
623 if (stat (temp
, &st
) >= 0
624 && ! S_ISDIR (st
.st_mode
)
625 && access (temp
, X_OK
) == 0)
628 #ifdef HOST_EXECUTABLE_SUFFIX
629 /* Some systems have a suffix for executable files.
630 So try appending that. */
631 strcat (temp
, HOST_EXECUTABLE_SUFFIX
);
633 if (stat (temp
, &st
) >= 0
634 && ! S_ISDIR (st
.st_mode
)
635 && access (temp
, X_OK
) == 0)
640 if (debug
&& pprefix
->plist
== NULL
)
641 fprintf (stderr
, " - failed: no entries in prefix list\n");
647 /* Add an entry for PREFIX to prefix list PPREFIX. */
650 add_prefix (struct path_prefix
*pprefix
, const char *prefix
)
652 struct prefix_list
*pl
, **prev
;
657 for (pl
= pprefix
->plist
; pl
->next
; pl
= pl
->next
)
662 prev
= &pprefix
->plist
;
664 /* Keep track of the longest prefix. */
666 len
= strlen (prefix
);
667 if (len
> pprefix
->max_len
)
668 pprefix
->max_len
= len
;
670 pl
= XNEW (struct prefix_list
);
671 pl
->prefix
= xstrdup (prefix
);
676 pl
->next
= (struct prefix_list
*) 0;
680 /* Take the value of the environment variable ENV, break it into a path, and
681 add of the entries to PPREFIX. */
684 prefix_from_env (const char *env
, struct path_prefix
*pprefix
)
687 GET_ENVIRONMENT (p
, env
);
690 prefix_from_string (p
, pprefix
);
694 prefix_from_string (const char *p
, struct path_prefix
*pprefix
)
696 const char *startp
, *endp
;
697 char *nstore
= XNEWVEC (char, strlen (p
) + 3);
700 fprintf (stderr
, "Convert string '%s' into prefixes, separator = '%c'\n", p
, PATH_SEPARATOR
);
705 if (*endp
== PATH_SEPARATOR
|| *endp
== 0)
707 strncpy (nstore
, startp
, endp
-startp
);
710 strcpy (nstore
, "./");
712 else if (! IS_DIR_SEPARATOR (endp
[-1]))
714 nstore
[endp
-startp
] = DIR_SEPARATOR
;
715 nstore
[endp
-startp
+1] = 0;
718 nstore
[endp
-startp
] = 0;
721 fprintf (stderr
, " - add prefix: %s\n", nstore
);
723 add_prefix (pprefix
, nstore
);
726 endp
= startp
= endp
+ 1;
736 main (int argc
, char **argv
)
738 static const char *const ld_suffix
= "ld";
739 static const char *const real_ld_suffix
= "real-ld";
740 static const char *const collect_ld_suffix
= "collect-ld";
741 static const char *const nm_suffix
= "nm";
742 static const char *const gnm_suffix
= "gnm";
744 static const char *const ldd_suffix
= LDD_SUFFIX
;
746 static const char *const strip_suffix
= "strip";
747 static const char *const gstrip_suffix
= "gstrip";
749 #ifdef CROSS_DIRECTORY_STRUCTURE
750 /* If we look for a program in the compiler directories, we just use
751 the short name, since these directories are already system-specific.
752 But it we look for a program in the system directories, we need to
753 qualify the program name with the target machine. */
755 const char *const full_ld_suffix
=
756 concat(target_machine
, "-", ld_suffix
, NULL
);
757 const char *const full_nm_suffix
=
758 concat (target_machine
, "-", nm_suffix
, NULL
);
759 const char *const full_gnm_suffix
=
760 concat (target_machine
, "-", gnm_suffix
, NULL
);
762 const char *const full_ldd_suffix
=
763 concat (target_machine
, "-", ldd_suffix
, NULL
);
765 const char *const full_strip_suffix
=
766 concat (target_machine
, "-", strip_suffix
, NULL
);
767 const char *const full_gstrip_suffix
=
768 concat (target_machine
, "-", gstrip_suffix
, NULL
);
770 const char *const full_ld_suffix
= ld_suffix
;
771 const char *const full_nm_suffix
= nm_suffix
;
772 const char *const full_gnm_suffix
= gnm_suffix
;
774 const char *const full_ldd_suffix
= ldd_suffix
;
776 const char *const full_strip_suffix
= strip_suffix
;
777 const char *const full_gstrip_suffix
= gstrip_suffix
;
778 #endif /* CROSS_DIRECTORY_STRUCTURE */
782 #ifdef COLLECT_EXPORT_LIST
785 const char *ld_file_name
;
796 int num_c_args
= argc
+9;
798 no_demangle
= !! getenv ("COLLECT_NO_DEMANGLE");
800 /* Suppress demangling by the real linker, which may be broken. */
801 putenv (xstrdup ("COLLECT_NO_DEMANGLE="));
803 #if defined (COLLECT2_HOST_INITIALIZATION)
804 /* Perform system dependent initialization, if necessary. */
805 COLLECT2_HOST_INITIALIZATION
;
809 /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
810 receive the signal. A different setting is inheritable */
811 signal (SIGCHLD
, SIG_DFL
);
814 /* Unlock the stdio streams. */
815 unlock_std_streams ();
819 /* Do not invoke xcalloc before this point, since locale needs to be
820 set first, in case a diagnostic is issued. */
822 ld1
= (const char **)(ld1_argv
= xcalloc(sizeof (char *), argc
+4));
823 ld2
= (const char **)(ld2_argv
= xcalloc(sizeof (char *), argc
+11));
824 object
= (const char **)(object_lst
= xcalloc(sizeof (char *), argc
));
830 /* Parse command line early for instances of -debug. This allows
831 the debug flag to be set before functions like find_a_file()
836 for (i
= 1; argv
[i
] != NULL
; i
++)
838 if (! strcmp (argv
[i
], "-debug"))
844 #ifndef DEFAULT_A_OUT_NAME
845 output_file
= "a.out";
847 output_file
= DEFAULT_A_OUT_NAME
;
850 obstack_begin (&temporary_obstack
, 0);
851 temporary_firstobj
= obstack_alloc (&temporary_obstack
, 0);
853 #ifndef HAVE_LD_DEMANGLE
854 current_demangling_style
= auto_demangling
;
856 p
= getenv ("COLLECT_GCC_OPTIONS");
859 const char *q
= extract_string (&p
);
860 if (*q
== '-' && (q
[1] == 'm' || q
[1] == 'f'))
863 obstack_free (&temporary_obstack
, temporary_firstobj
);
865 /* -fno-profile-arcs -fno-test-coverage -fno-branch-probabilities
866 -fno-exceptions -w */
869 c_ptr
= (const char **) (c_argv
= xcalloc (sizeof (char *), num_c_args
));
872 fatal ("no arguments");
875 if (signal (SIGQUIT
, SIG_IGN
) != SIG_IGN
)
876 signal (SIGQUIT
, handler
);
878 if (signal (SIGINT
, SIG_IGN
) != SIG_IGN
)
879 signal (SIGINT
, handler
);
881 if (signal (SIGALRM
, SIG_IGN
) != SIG_IGN
)
882 signal (SIGALRM
, handler
);
885 if (signal (SIGHUP
, SIG_IGN
) != SIG_IGN
)
886 signal (SIGHUP
, handler
);
888 if (signal (SIGSEGV
, SIG_IGN
) != SIG_IGN
)
889 signal (SIGSEGV
, handler
);
891 if (signal (SIGBUS
, SIG_IGN
) != SIG_IGN
)
892 signal (SIGBUS
, handler
);
895 /* Extract COMPILER_PATH and PATH into our prefix list. */
896 prefix_from_env ("COMPILER_PATH", &cpath
);
897 prefix_from_env ("PATH", &path
);
899 /* Try to discover a valid linker/nm/strip to use. */
901 /* Maybe we know the right file to use (if not cross). */
903 #ifdef DEFAULT_LINKER
904 if (access (DEFAULT_LINKER
, X_OK
) == 0)
905 ld_file_name
= DEFAULT_LINKER
;
906 if (ld_file_name
== 0)
908 #ifdef REAL_LD_FILE_NAME
909 ld_file_name
= find_a_file (&path
, REAL_LD_FILE_NAME
);
910 if (ld_file_name
== 0)
912 /* Search the (target-specific) compiler dirs for ld'. */
913 ld_file_name
= find_a_file (&cpath
, real_ld_suffix
);
914 /* Likewise for `collect-ld'. */
915 if (ld_file_name
== 0)
916 ld_file_name
= find_a_file (&cpath
, collect_ld_suffix
);
917 /* Search the compiler directories for `ld'. We have protection against
918 recursive calls in find_a_file. */
919 if (ld_file_name
== 0)
920 ld_file_name
= find_a_file (&cpath
, ld_suffix
);
921 /* Search the ordinary system bin directories
922 for `ld' (if native linking) or `TARGET-ld' (if cross). */
923 if (ld_file_name
== 0)
924 ld_file_name
= find_a_file (&path
, full_ld_suffix
);
926 #ifdef REAL_NM_FILE_NAME
927 nm_file_name
= find_a_file (&path
, REAL_NM_FILE_NAME
);
928 if (nm_file_name
== 0)
930 nm_file_name
= find_a_file (&cpath
, gnm_suffix
);
931 if (nm_file_name
== 0)
932 nm_file_name
= find_a_file (&path
, full_gnm_suffix
);
933 if (nm_file_name
== 0)
934 nm_file_name
= find_a_file (&cpath
, nm_suffix
);
935 if (nm_file_name
== 0)
936 nm_file_name
= find_a_file (&path
, full_nm_suffix
);
939 ldd_file_name
= find_a_file (&cpath
, ldd_suffix
);
940 if (ldd_file_name
== 0)
941 ldd_file_name
= find_a_file (&path
, full_ldd_suffix
);
944 #ifdef REAL_STRIP_FILE_NAME
945 strip_file_name
= find_a_file (&path
, REAL_STRIP_FILE_NAME
);
946 if (strip_file_name
== 0)
948 strip_file_name
= find_a_file (&cpath
, gstrip_suffix
);
949 if (strip_file_name
== 0)
950 strip_file_name
= find_a_file (&path
, full_gstrip_suffix
);
951 if (strip_file_name
== 0)
952 strip_file_name
= find_a_file (&cpath
, strip_suffix
);
953 if (strip_file_name
== 0)
954 strip_file_name
= find_a_file (&path
, full_strip_suffix
);
956 /* Determine the full path name of the C compiler to use. */
957 c_file_name
= getenv ("COLLECT_GCC");
958 if (c_file_name
== 0)
960 #ifdef CROSS_DIRECTORY_STRUCTURE
961 c_file_name
= concat (target_machine
, "-gcc", NULL
);
967 p
= find_a_file (&cpath
, c_file_name
);
969 /* Here it should be safe to use the system search path since we should have
970 already qualified the name of the compiler when it is needed. */
972 p
= find_a_file (&path
, c_file_name
);
977 *ld1
++ = *ld2
++ = ld_file_name
;
979 /* Make temp file names. */
980 c_file
= make_temp_file (".c");
981 o_file
= make_temp_file (".o");
982 #ifdef COLLECT_EXPORT_LIST
983 export_file
= make_temp_file (".x");
985 ldout
= make_temp_file (".ld");
986 lderrout
= make_temp_file (".le");
987 *c_ptr
++ = c_file_name
;
994 #ifdef COLLECT_EXPORT_LIST
995 /* Generate a list of directories from LIBPATH. */
996 prefix_from_env ("LIBPATH", &libpath_lib_dirs
);
997 /* Add to this list also two standard directories where
998 AIX loader always searches for libraries. */
999 add_prefix (&libpath_lib_dirs
, "/lib");
1000 add_prefix (&libpath_lib_dirs
, "/usr/lib");
1003 /* Get any options that the upper GCC wants to pass to the sub-GCC.
1005 AIX support needs to know if -shared has been specified before
1006 parsing commandline arguments. */
1008 p
= getenv ("COLLECT_GCC_OPTIONS");
1011 const char *q
= extract_string (&p
);
1012 if (*q
== '-' && (q
[1] == 'm' || q
[1] == 'f'))
1013 *c_ptr
++ = xstrdup (q
);
1014 if (strcmp (q
, "-EL") == 0 || strcmp (q
, "-EB") == 0)
1015 *c_ptr
++ = xstrdup (q
);
1016 if (strcmp (q
, "-shared") == 0)
1018 if (*q
== '-' && q
[1] == 'B')
1020 *c_ptr
++ = xstrdup (q
);
1023 q
= extract_string (&p
);
1024 *c_ptr
++ = xstrdup (q
);
1028 obstack_free (&temporary_obstack
, temporary_firstobj
);
1029 *c_ptr
++ = "-fno-profile-arcs";
1030 *c_ptr
++ = "-fno-test-coverage";
1031 *c_ptr
++ = "-fno-branch-probabilities";
1032 *c_ptr
++ = "-fno-exceptions";
1035 /* !!! When GCC calls collect2,
1036 it does not know whether it is calling collect2 or ld.
1037 So collect2 cannot meaningfully understand any options
1038 except those ld understands.
1039 If you propose to make GCC pass some other option,
1040 just imagine what will happen if ld is really ld!!! */
1042 /* Parse arguments. Remember output file spec, pass the rest to ld. */
1043 /* After the first file, put in the c++ rt0. */
1046 #ifdef HAVE_LD_DEMANGLE
1047 if (!demangle_flag
&& !no_demangle
)
1048 demangle_flag
= "--demangle";
1050 *ld1
++ = *ld2
++ = demangle_flag
;
1052 while ((arg
= *++argv
) != (char *) 0)
1054 *ld1
++ = *ld2
++ = arg
;
1060 #ifdef COLLECT_EXPORT_LIST
1061 /* We want to disable automatic exports on AIX when user
1062 explicitly puts an export list in command line */
1064 if (arg
[2] == 'E' || strncmp (&arg
[2], "export", 6) == 0)
1066 else if (arg
[2] == '6' && arg
[3] == '4')
1068 else if (arg
[2] == 'r' && arg
[3] == 't' && arg
[4] == 'l')
1074 if (!strcmp (arg
, "-debug"))
1076 /* Already parsed. */
1080 if (!strcmp (arg
, "-dynamic-linker") && argv
[1])
1083 *ld1
++ = *ld2
++ = *argv
;
1090 /* place o_file BEFORE this argument! */
1096 #ifdef COLLECT_EXPORT_LIST
1098 /* Resolving full library name. */
1099 const char *s
= resolve_lib_name (arg
+2);
1101 /* Saving a full library name. */
1102 add_to_list (&libs
, s
);
1107 #ifdef COLLECT_EXPORT_LIST
1108 /* Saving directories where to search for libraries. */
1110 add_prefix (&cmdline_lib_dirs
, arg
+2);
1113 #if LINK_ELIMINATE_DUPLICATE_LDIRECTORIES
1115 if (is_in_args (arg
, (const char **) ld1_argv
, ld1
-1))
1118 #endif /* LINK_ELIMINATE_DUPLICATE_LDIRECTORIES */
1123 output_file
= *ld1
++ = *ld2
++ = *++argv
;
1125 #ifdef SWITCHES_NEED_SPACES
1126 && ! strchr (SWITCHES_NEED_SPACES
, arg
[1])
1130 output_file
= &arg
[2];
1139 if (arg
[2] == '\0' && do_collecting
)
1141 /* We must strip after the nm run, otherwise C++ linking
1142 will not work. Thus we strip in the second ld run, or
1143 else with strip if there is no second ld run. */
1155 if (strcmp (arg
, "--no-demangle") == 0)
1157 demangle_flag
= arg
;
1162 else if (strncmp (arg
, "--demangle", 10) == 0)
1164 demangle_flag
= arg
;
1166 #ifndef HAVE_LD_DEMANGLE
1169 enum demangling_styles style
1170 = cplus_demangle_name_to_style (arg
+11);
1171 if (style
== unknown_demangling
)
1172 error ("unknown demangling style '%s'", arg
+11);
1174 current_demangling_style
= style
;
1183 else if ((p
= strrchr (arg
, '.')) != (char *) 0
1184 && (strcmp (p
, ".o") == 0 || strcmp (p
, ".a") == 0
1185 || strcmp (p
, ".so") == 0 || strcmp (p
, ".lo") == 0
1186 || strcmp (p
, ".obj") == 0))
1195 /* place o_file BEFORE this argument! */
1201 if (p
[1] == 'o' || p
[1] == 'l')
1203 #ifdef COLLECT_EXPORT_LIST
1204 /* libraries can be specified directly, i.e. without -l flag. */
1207 /* Saving a full library name. */
1208 add_to_list (&libs
, arg
);
1214 #ifdef COLLECT_EXPORT_LIST
1215 /* This is added only for debugging purposes. */
1218 fprintf (stderr
, "List of libraries:\n");
1219 dump_list (stderr
, "\t", libs
.first
);
1222 /* The AIX linker will discard static constructors in object files if
1223 nothing else in the file is referenced, so look at them first. */
1225 const char **export_object_lst
= (const char **)object_lst
;
1227 while (export_object_lst
< object
)
1228 scan_prog_file (*export_object_lst
++, PASS_OBJ
);
1231 struct id
*list
= libs
.first
;
1233 for (; list
; list
= list
->next
)
1234 scan_prog_file (list
->name
, PASS_FIRST
);
1239 char *buf
= concat ("-bE:", export_file
, NULL
);
1244 exportf
= fopen (export_file
, "w");
1245 if (exportf
== (FILE *) 0)
1246 fatal_perror ("fopen %s", export_file
);
1247 write_aix_file (exportf
, exports
.first
);
1248 if (fclose (exportf
))
1249 fatal_perror ("fclose %s", export_file
);
1254 *c_ptr
= *ld1
= *object
= (char *) 0;
1258 notice ("collect2 version %s", version_string
);
1259 #ifdef TARGET_VERSION
1262 fprintf (stderr
, "\n");
1268 fprintf (stderr
, "ld_file_name = %s\n",
1269 (ld_file_name
? ld_file_name
: "not found"));
1270 fprintf (stderr
, "c_file_name = %s\n",
1271 (c_file_name
? c_file_name
: "not found"));
1272 fprintf (stderr
, "nm_file_name = %s\n",
1273 (nm_file_name
? nm_file_name
: "not found"));
1275 fprintf (stderr
, "ldd_file_name = %s\n",
1276 (ldd_file_name
? ldd_file_name
: "not found"));
1278 fprintf (stderr
, "strip_file_name = %s\n",
1279 (strip_file_name
? strip_file_name
: "not found"));
1280 fprintf (stderr
, "c_file = %s\n",
1281 (c_file
? c_file
: "not found"));
1282 fprintf (stderr
, "o_file = %s\n",
1283 (o_file
? o_file
: "not found"));
1285 ptr
= getenv ("COLLECT_GCC_OPTIONS");
1287 fprintf (stderr
, "COLLECT_GCC_OPTIONS = %s\n", ptr
);
1289 ptr
= getenv ("COLLECT_GCC");
1291 fprintf (stderr
, "COLLECT_GCC = %s\n", ptr
);
1293 ptr
= getenv ("COMPILER_PATH");
1295 fprintf (stderr
, "COMPILER_PATH = %s\n", ptr
);
1297 ptr
= getenv (LIBRARY_PATH_ENV
);
1299 fprintf (stderr
, "%-20s= %s\n", LIBRARY_PATH_ENV
, ptr
);
1301 fprintf (stderr
, "\n");
1304 /* Load the program, searching all libraries and attempting to provide
1305 undefined symbols from repository information. */
1307 /* On AIX we do this later. */
1308 #ifndef COLLECT_EXPORT_LIST
1309 do_tlink (ld1_argv
, object_lst
);
1312 /* If -r or they will be run via some other method, do not build the
1313 constructor or destructor list, just return now. */
1315 #ifndef COLLECT_EXPORT_LIST
1320 #ifdef COLLECT_EXPORT_LIST
1321 /* Do the link we avoided above if we are exiting. */
1322 do_tlink (ld1_argv
, object_lst
);
1324 /* But make sure we delete the export file we may have created. */
1325 if (export_file
!= 0 && export_file
[0])
1326 maybe_unlink (export_file
);
1328 maybe_unlink (c_file
);
1329 maybe_unlink (o_file
);
1333 /* Examine the namelist with nm and search it for static constructors
1334 and destructors to call.
1335 Write the constructor and destructor tables to a .s file and reload. */
1337 /* On AIX we already scanned for global constructors/destructors. */
1338 #ifndef COLLECT_EXPORT_LIST
1339 scan_prog_file (output_file
, PASS_FIRST
);
1342 #ifdef SCAN_LIBRARIES
1343 scan_libraries (output_file
);
1348 notice ("%d constructor(s) found\n", constructors
.number
);
1349 notice ("%d destructor(s) found\n", destructors
.number
);
1350 notice ("%d frame table(s) found\n", frame_tables
.number
);
1353 if (constructors
.number
== 0 && destructors
.number
== 0
1354 && frame_tables
.number
== 0
1355 #if defined (SCAN_LIBRARIES) || defined (COLLECT_EXPORT_LIST)
1356 /* If we will be running these functions ourselves, we want to emit
1357 stubs into the shared library so that we do not have to relink
1358 dependent programs when we add static objects. */
1363 #ifdef COLLECT_EXPORT_LIST
1364 /* Do tlink without additional code generation. */
1365 do_tlink (ld1_argv
, object_lst
);
1367 /* Strip now if it was requested on the command line. */
1370 char **real_strip_argv
= XCNEWVEC (char *, 3);
1371 const char ** strip_argv
= (const char **) real_strip_argv
;
1373 strip_argv
[0] = strip_file_name
;
1374 strip_argv
[1] = output_file
;
1375 strip_argv
[2] = (char *) 0;
1376 fork_execute ("strip", real_strip_argv
);
1379 #ifdef COLLECT_EXPORT_LIST
1380 maybe_unlink (export_file
);
1382 maybe_unlink (c_file
);
1383 maybe_unlink (o_file
);
1387 /* Sort ctor and dtor lists by priority. */
1388 sort_ids (&constructors
);
1389 sort_ids (&destructors
);
1391 maybe_unlink(output_file
);
1392 outf
= fopen (c_file
, "w");
1393 if (outf
== (FILE *) 0)
1394 fatal_perror ("fopen %s", c_file
);
1396 write_c_file (outf
, c_file
);
1399 fatal_perror ("fclose %s", c_file
);
1401 /* Tell the linker that we have initializer and finalizer functions. */
1402 #ifdef LD_INIT_SWITCH
1403 #ifdef COLLECT_EXPORT_LIST
1404 *ld2
++ = concat (LD_INIT_SWITCH
, ":", initname
, ":", fininame
, NULL
);
1406 *ld2
++ = LD_INIT_SWITCH
;
1408 *ld2
++ = LD_FINI_SWITCH
;
1413 #ifdef COLLECT_EXPORT_LIST
1416 /* If we did not add export flag to link arguments before, add it to
1417 second link phase now. No new exports should have been added. */
1418 if (! exports
.first
)
1419 *ld2
++ = concat ("-bE:", export_file
, NULL
);
1421 #ifndef LD_INIT_SWITCH
1422 add_to_list (&exports
, initname
);
1423 add_to_list (&exports
, fininame
);
1424 add_to_list (&exports
, "_GLOBAL__DI");
1425 add_to_list (&exports
, "_GLOBAL__DD");
1427 exportf
= fopen (export_file
, "w");
1428 if (exportf
== (FILE *) 0)
1429 fatal_perror ("fopen %s", export_file
);
1430 write_aix_file (exportf
, exports
.first
);
1431 if (fclose (exportf
))
1432 fatal_perror ("fclose %s", export_file
);
1436 /* End of arguments to second link phase. */
1441 fprintf (stderr
, "\n========== output_file = %s, c_file = %s\n",
1442 output_file
, c_file
);
1443 write_c_file (stderr
, "stderr");
1444 fprintf (stderr
, "========== end of c_file\n\n");
1445 #ifdef COLLECT_EXPORT_LIST
1446 fprintf (stderr
, "\n========== export_file = %s\n", export_file
);
1447 write_aix_file (stderr
, exports
.first
);
1448 fprintf (stderr
, "========== end of export_file\n\n");
1452 /* Assemble the constructor and destructor tables.
1453 Link the tables in with the rest of the program. */
1455 fork_execute ("gcc", c_argv
);
1456 #ifdef COLLECT_EXPORT_LIST
1457 /* On AIX we must call tlink because of possible templates resolution. */
1458 do_tlink (ld2_argv
, object_lst
);
1460 /* Otherwise, simply call ld because tlink is already done. */
1461 fork_execute ("ld", ld2_argv
);
1463 /* Let scan_prog_file do any final mods (OSF/rose needs this for
1464 constructors/destructors in shared libraries. */
1465 scan_prog_file (output_file
, PASS_SECOND
);
1468 maybe_unlink (c_file
);
1469 maybe_unlink (o_file
);
1471 #ifdef COLLECT_EXPORT_LIST
1472 maybe_unlink (export_file
);
1479 /* Wait for a process to finish, and exit if a nonzero status is found. */
1482 collect_wait (const char *prog
, struct pex_obj
*pex
)
1486 if (!pex_get_status (pex
, 1, &status
))
1487 fatal_perror ("can't get program status");
1492 if (WIFSIGNALED (status
))
1494 int sig
= WTERMSIG (status
);
1495 error ("%s terminated with signal %d [%s]%s",
1496 prog
, sig
, strsignal(sig
),
1497 WCOREDUMP(status
) ? ", core dumped" : "");
1498 collect_exit (FATAL_EXIT_CODE
);
1501 if (WIFEXITED (status
))
1502 return WEXITSTATUS (status
);
1508 do_wait (const char *prog
, struct pex_obj
*pex
)
1510 int ret
= collect_wait (prog
, pex
);
1513 error ("%s returned %d exit status", prog
, ret
);
1519 /* Execute a program, and wait for the reply. */
1522 collect_execute (const char *prog
, char **argv
, const char *outname
,
1523 const char *errname
)
1525 struct pex_obj
*pex
;
1535 fprintf (stderr
, "%s", argv
[0]);
1537 notice ("[cannot find %s]", prog
);
1539 for (p_argv
= &argv
[1]; (str
= *p_argv
) != (char *) 0; p_argv
++)
1540 fprintf (stderr
, " %s", str
);
1542 fprintf (stderr
, "\n");
1548 /* If we cannot find a program we need, complain error. Do this here
1549 since we might not end up needing something that we could not find. */
1552 fatal ("cannot find '%s'", prog
);
1554 pex
= pex_init (0, "collect2", NULL
);
1556 fatal_perror ("pex_init failed");
1558 errmsg
= pex_run (pex
, PEX_LAST
| PEX_SEARCH
, argv
[0], argv
, outname
,
1565 fatal_perror (errmsg
);
1575 fork_execute (const char *prog
, char **argv
)
1577 struct pex_obj
*pex
;
1579 pex
= collect_execute (prog
, argv
, NULL
, NULL
);
1580 do_wait (prog
, pex
);
1583 /* Unlink a file unless we are debugging. */
1586 maybe_unlink (const char *file
)
1589 unlink_if_ordinary (file
);
1591 notice ("[Leaving %s]\n", file
);
1595 static long sequence_number
= 0;
1597 /* Add a name to a linked list. */
1600 add_to_list (struct head
*head_ptr
, const char *name
)
1602 struct id
*newid
= xcalloc (sizeof (struct id
) + strlen (name
), 1);
1604 strcpy (newid
->name
, name
);
1606 if (head_ptr
->first
)
1607 head_ptr
->last
->next
= newid
;
1609 head_ptr
->first
= newid
;
1611 /* Check for duplicate symbols. */
1612 for (p
= head_ptr
->first
;
1613 strcmp (name
, p
->name
) != 0;
1618 head_ptr
->last
->next
= 0;
1623 newid
->sequence
= ++sequence_number
;
1624 head_ptr
->last
= newid
;
1628 /* Grab the init priority number from an init function name that
1629 looks like "_GLOBAL_.I.12345.foo". */
1632 extract_init_priority (const char *name
)
1636 while (name
[pos
] == '_')
1638 pos
+= 10; /* strlen ("GLOBAL__X_") */
1640 /* Extract init_p number from ctor/dtor name. */
1641 pri
= atoi (name
+ pos
);
1642 return pri
? pri
: DEFAULT_INIT_PRIORITY
;
1645 /* Insertion sort the ids from ctor/dtor list HEAD_PTR in descending order.
1646 ctors will be run from right to left, dtors from left to right. */
1649 sort_ids (struct head
*head_ptr
)
1651 /* id holds the current element to insert. id_next holds the next
1652 element to insert. id_ptr iterates through the already sorted elements
1653 looking for the place to insert id. */
1654 struct id
*id
, *id_next
, **id_ptr
;
1656 id
= head_ptr
->first
;
1658 /* We don't have any sorted elements yet. */
1659 head_ptr
->first
= NULL
;
1661 for (; id
; id
= id_next
)
1664 id
->sequence
= extract_init_priority (id
->name
);
1666 for (id_ptr
= &(head_ptr
->first
); ; id_ptr
= &((*id_ptr
)->next
))
1668 /* If the sequence numbers are the same, we put the id from the
1669 file later on the command line later in the list. */
1670 || id
->sequence
> (*id_ptr
)->sequence
1671 /* Hack: do lexical compare, too.
1672 || (id->sequence == (*id_ptr)->sequence
1673 && strcmp (id->name, (*id_ptr)->name) > 0) */
1682 /* Now set the sequence numbers properly so write_c_file works. */
1683 for (id
= head_ptr
->first
; id
; id
= id
->next
)
1684 id
->sequence
= ++sequence_number
;
1687 /* Write: `prefix', the names on list LIST, `suffix'. */
1690 write_list (FILE *stream
, const char *prefix
, struct id
*list
)
1694 fprintf (stream
, "%sx%d,\n", prefix
, list
->sequence
);
1699 #if LINK_ELIMINATE_DUPLICATE_LDIRECTORIES
1700 /* Given a STRING, return nonzero if it occurs in the list in range
1701 [ARGS_BEGIN,ARGS_END). */
1704 is_in_args (const char *string
, const char **args_begin
,
1705 const char **args_end
)
1707 const char **args_pointer
;
1708 for (args_pointer
= args_begin
; args_pointer
!= args_end
; ++args_pointer
)
1709 if (strcmp (string
, *args_pointer
) == 0)
1713 #endif /* LINK_ELIMINATE_DUPLICATE_LDIRECTORIES */
1715 #ifdef COLLECT_EXPORT_LIST
1716 /* This function is really used only on AIX, but may be useful. */
1719 is_in_list (const char *prefix
, struct id
*list
)
1723 if (!strcmp (prefix
, list
->name
)) return 1;
1729 #endif /* COLLECT_EXPORT_LIST */
1731 /* Added for debugging purpose. */
1732 #ifdef COLLECT_EXPORT_LIST
1734 dump_list (FILE *stream
, const char *prefix
, struct id
*list
)
1738 fprintf (stream
, "%s%s,\n", prefix
, list
->name
);
1746 dump_prefix_list (FILE *stream
, const char *prefix
, struct prefix_list
*list
)
1750 fprintf (stream
, "%s%s,\n", prefix
, list
->prefix
);
1757 write_list_with_asm (FILE *stream
, const char *prefix
, struct id
*list
)
1761 fprintf (stream
, "%sx%d __asm__ (\"%s\");\n",
1762 prefix
, list
->sequence
, list
->name
);
1767 /* Write out the constructor and destructor tables statically (for a shared
1768 object), along with the functions to execute them. */
1771 write_c_file_stat (FILE *stream
, const char *name ATTRIBUTE_UNUSED
)
1775 int frames
= (frame_tables
.number
> 0);
1777 /* Figure out name of output_file, stripping off .so version. */
1778 p
= strrchr (output_file
, '/');
1794 if (strncmp (q
, ".so", 3) == 0)
1803 /* q points to null at end of the string (or . of the .so version) */
1804 prefix
= XNEWVEC (char, q
- p
+ 1);
1805 strncpy (prefix
, p
, q
- p
);
1807 for (r
= prefix
; *r
; r
++)
1808 if (!ISALNUM ((unsigned char)*r
))
1811 notice ("\nwrite_c_file - output name is %s, prefix is %s\n",
1812 output_file
, prefix
);
1814 initname
= concat ("_GLOBAL__FI_", prefix
, NULL
);
1815 fininame
= concat ("_GLOBAL__FD_", prefix
, NULL
);
1819 /* Write the tables as C code. */
1821 fprintf (stream
, "static int count;\n");
1822 fprintf (stream
, "typedef void entry_pt();\n");
1823 write_list_with_asm (stream
, "extern entry_pt ", constructors
.first
);
1827 write_list_with_asm (stream
, "extern void *", frame_tables
.first
);
1829 fprintf (stream
, "\tstatic void *frame_table[] = {\n");
1830 write_list (stream
, "\t\t&", frame_tables
.first
);
1831 fprintf (stream
, "\t0\n};\n");
1833 /* This must match what's in frame.h. */
1834 fprintf (stream
, "struct object {\n");
1835 fprintf (stream
, " void *pc_begin;\n");
1836 fprintf (stream
, " void *pc_end;\n");
1837 fprintf (stream
, " void *fde_begin;\n");
1838 fprintf (stream
, " void *fde_array;\n");
1839 fprintf (stream
, " __SIZE_TYPE__ count;\n");
1840 fprintf (stream
, " struct object *next;\n");
1841 fprintf (stream
, "};\n");
1843 fprintf (stream
, "extern void __register_frame_info_table (void *, struct object *);\n");
1844 fprintf (stream
, "extern void *__deregister_frame_info (void *);\n");
1846 fprintf (stream
, "static void reg_frame () {\n");
1847 fprintf (stream
, "\tstatic struct object ob;\n");
1848 fprintf (stream
, "\t__register_frame_info_table (frame_table, &ob);\n");
1849 fprintf (stream
, "\t}\n");
1851 fprintf (stream
, "static void dereg_frame () {\n");
1852 fprintf (stream
, "\t__deregister_frame_info (frame_table);\n");
1853 fprintf (stream
, "\t}\n");
1856 fprintf (stream
, "void %s() {\n", initname
);
1857 if (constructors
.number
> 0 || frames
)
1859 fprintf (stream
, "\tstatic entry_pt *ctors[] = {\n");
1860 write_list (stream
, "\t\t", constructors
.first
);
1862 fprintf (stream
, "\treg_frame,\n");
1863 fprintf (stream
, "\t};\n");
1864 fprintf (stream
, "\tentry_pt **p;\n");
1865 fprintf (stream
, "\tif (count++ != 0) return;\n");
1866 fprintf (stream
, "\tp = ctors + %d;\n", constructors
.number
+ frames
);
1867 fprintf (stream
, "\twhile (p > ctors) (*--p)();\n");
1870 fprintf (stream
, "\t++count;\n");
1871 fprintf (stream
, "}\n");
1872 write_list_with_asm (stream
, "extern entry_pt ", destructors
.first
);
1873 fprintf (stream
, "void %s() {\n", fininame
);
1874 if (destructors
.number
> 0 || frames
)
1876 fprintf (stream
, "\tstatic entry_pt *dtors[] = {\n");
1877 write_list (stream
, "\t\t", destructors
.first
);
1879 fprintf (stream
, "\tdereg_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 = dtors;\n");
1884 fprintf (stream
, "\twhile (p < dtors + %d) (*p++)();\n",
1885 destructors
.number
+ frames
);
1887 fprintf (stream
, "}\n");
1891 COLLECT_SHARED_INIT_FUNC(stream
, initname
);
1892 COLLECT_SHARED_FINI_FUNC(stream
, fininame
);
1896 /* Write the constructor/destructor tables. */
1898 #ifndef LD_INIT_SWITCH
1900 write_c_file_glob (FILE *stream
, const char *name ATTRIBUTE_UNUSED
)
1902 /* Write the tables as C code. */
1904 int frames
= (frame_tables
.number
> 0);
1906 fprintf (stream
, "typedef void entry_pt();\n\n");
1908 write_list_with_asm (stream
, "extern entry_pt ", constructors
.first
);
1912 write_list_with_asm (stream
, "extern void *", frame_tables
.first
);
1914 fprintf (stream
, "\tstatic void *frame_table[] = {\n");
1915 write_list (stream
, "\t\t&", frame_tables
.first
);
1916 fprintf (stream
, "\t0\n};\n");
1918 /* This must match what's in frame.h. */
1919 fprintf (stream
, "struct object {\n");
1920 fprintf (stream
, " void *pc_begin;\n");
1921 fprintf (stream
, " void *pc_end;\n");
1922 fprintf (stream
, " void *fde_begin;\n");
1923 fprintf (stream
, " void *fde_array;\n");
1924 fprintf (stream
, " __SIZE_TYPE__ count;\n");
1925 fprintf (stream
, " struct object *next;\n");
1926 fprintf (stream
, "};\n");
1928 fprintf (stream
, "extern void __register_frame_info_table (void *, struct object *);\n");
1929 fprintf (stream
, "extern void *__deregister_frame_info (void *);\n");
1931 fprintf (stream
, "static void reg_frame () {\n");
1932 fprintf (stream
, "\tstatic struct object ob;\n");
1933 fprintf (stream
, "\t__register_frame_info_table (frame_table, &ob);\n");
1934 fprintf (stream
, "\t}\n");
1936 fprintf (stream
, "static void dereg_frame () {\n");
1937 fprintf (stream
, "\t__deregister_frame_info (frame_table);\n");
1938 fprintf (stream
, "\t}\n");
1941 fprintf (stream
, "\nentry_pt * __CTOR_LIST__[] = {\n");
1942 fprintf (stream
, "\t(entry_pt *) %d,\n", constructors
.number
+ frames
);
1943 write_list (stream
, "\t", constructors
.first
);
1945 fprintf (stream
, "\treg_frame,\n");
1946 fprintf (stream
, "\t0\n};\n\n");
1948 write_list_with_asm (stream
, "extern entry_pt ", destructors
.first
);
1950 fprintf (stream
, "\nentry_pt * __DTOR_LIST__[] = {\n");
1951 fprintf (stream
, "\t(entry_pt *) %d,\n", destructors
.number
+ frames
);
1952 write_list (stream
, "\t", destructors
.first
);
1954 fprintf (stream
, "\tdereg_frame,\n");
1955 fprintf (stream
, "\t0\n};\n\n");
1957 fprintf (stream
, "extern entry_pt %s;\n", NAME__MAIN
);
1958 fprintf (stream
, "entry_pt *__main_reference = %s;\n\n", NAME__MAIN
);
1960 #endif /* ! LD_INIT_SWITCH */
1963 write_c_file (FILE *stream
, const char *name
)
1965 fprintf (stream
, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n");
1966 #ifndef LD_INIT_SWITCH
1968 write_c_file_glob (stream
, name
);
1971 write_c_file_stat (stream
, name
);
1972 fprintf (stream
, "#ifdef __cplusplus\n}\n#endif\n");
1975 #ifdef COLLECT_EXPORT_LIST
1977 write_aix_file (FILE *stream
, struct id
*list
)
1979 for (; list
; list
= list
->next
)
1981 fputs (list
->name
, stream
);
1982 putc ('\n', stream
);
1987 #ifdef OBJECT_FORMAT_NONE
1989 /* Generic version to scan the name list of the loaded program for
1990 the symbols g++ uses for static constructors and destructors.
1992 The constructor table begins at __CTOR_LIST__ and contains a count
1993 of the number of pointers (or -1 if the constructors are built in a
1994 separate section by the linker), followed by the pointers to the
1995 constructor functions, terminated with a null pointer. The
1996 destructor table has the same format, and begins at __DTOR_LIST__. */
1999 scan_prog_file (const char *prog_name
, enum pass which_pass
)
2001 void (*int_handler
) (int);
2003 void (*quit_handler
) (int);
2005 char *real_nm_argv
[4];
2006 const char **nm_argv
= (const char **) real_nm_argv
;
2008 struct pex_obj
*pex
;
2014 if (which_pass
== PASS_SECOND
)
2017 /* If we do not have an `nm', complain. */
2018 if (nm_file_name
== 0)
2019 fatal ("cannot find 'nm'");
2021 nm_argv
[argc
++] = nm_file_name
;
2022 if (NM_FLAGS
[0] != '\0')
2023 nm_argv
[argc
++] = NM_FLAGS
;
2025 nm_argv
[argc
++] = prog_name
;
2026 nm_argv
[argc
++] = (char *) 0;
2028 /* Trace if needed. */
2031 const char **p_argv
;
2034 for (p_argv
= &nm_argv
[0]; (str
= *p_argv
) != (char *) 0; p_argv
++)
2035 fprintf (stderr
, " %s", str
);
2037 fprintf (stderr
, "\n");
2043 pex
= pex_init (PEX_USE_PIPES
, "collect2", NULL
);
2045 fatal_perror ("pex_init failed");
2047 errmsg
= pex_run (pex
, 0, nm_file_name
, real_nm_argv
, NULL
, NULL
, &err
);
2053 fatal_perror (errmsg
);
2059 int_handler
= (void (*) (int)) signal (SIGINT
, SIG_IGN
);
2061 quit_handler
= (void (*) (int)) signal (SIGQUIT
, SIG_IGN
);
2064 inf
= pex_read_output (pex
, 0);
2066 fatal_perror ("can't open nm output");
2069 fprintf (stderr
, "\nnm output with constructors/destructors.\n");
2071 /* Read each line of nm output. */
2072 while (fgets (buf
, sizeof buf
, inf
) != (char *) 0)
2077 /* If it contains a constructor or destructor name, add the name
2078 to the appropriate list. */
2080 for (p
= buf
; (ch
= *p
) != '\0' && ch
!= '\n' && ch
!= '_'; p
++)
2081 if (ch
== ' ' && p
[1] == 'U' && p
[2] == ' ')
2088 /* Find the end of the symbol name.
2089 Do not include `|', because Encore nm can tack that on the end. */
2090 for (end
= p
; (ch2
= *end
) != '\0' && !ISSPACE (ch2
) && ch2
!= '|';
2096 switch (is_ctor_dtor (name
))
2099 if (which_pass
!= PASS_LIB
)
2100 add_to_list (&constructors
, name
);
2104 if (which_pass
!= PASS_LIB
)
2105 add_to_list (&destructors
, name
);
2109 if (which_pass
!= PASS_LIB
)
2110 fatal ("init function found in object %s", prog_name
);
2111 #ifndef LD_INIT_SWITCH
2112 add_to_list (&constructors
, name
);
2117 if (which_pass
!= PASS_LIB
)
2118 fatal ("fini function found in object %s", prog_name
);
2119 #ifndef LD_FINI_SWITCH
2120 add_to_list (&destructors
, name
);
2125 if (which_pass
!= PASS_LIB
)
2126 add_to_list (&frame_tables
, name
);
2129 default: /* not a constructor or destructor */
2134 fprintf (stderr
, "\t%s\n", buf
);
2138 fprintf (stderr
, "\n");
2140 do_wait (nm_file_name
, pex
);
2142 signal (SIGINT
, int_handler
);
2144 signal (SIGQUIT
, quit_handler
);
2150 /* Use the List Dynamic Dependencies program to find shared libraries that
2151 the output file depends upon and their initialization/finalization
2152 routines, if any. */
2155 scan_libraries (const char *prog_name
)
2157 static struct head libraries
; /* list of shared libraries found */
2159 void (*int_handler
) (int);
2161 void (*quit_handler
) (int);
2163 char *real_ldd_argv
[4];
2164 const char **ldd_argv
= (const char **) real_ldd_argv
;
2166 struct pex_obj
*pex
;
2172 /* If we do not have an `ldd', complain. */
2173 if (ldd_file_name
== 0)
2175 error ("cannot find 'ldd'");
2179 ldd_argv
[argc
++] = ldd_file_name
;
2180 ldd_argv
[argc
++] = prog_name
;
2181 ldd_argv
[argc
++] = (char *) 0;
2183 /* Trace if needed. */
2186 const char **p_argv
;
2189 for (p_argv
= &ldd_argv
[0]; (str
= *p_argv
) != (char *) 0; p_argv
++)
2190 fprintf (stderr
, " %s", str
);
2192 fprintf (stderr
, "\n");
2198 pex
= pex_init (PEX_USE_PIPES
, "collect2", NULL
);
2200 fatal_perror ("pex_init failed");
2202 errmsg
= pex_run (pex
, 0, ldd_file_name
, real_ldd_argv
, NULL
, NULL
, &err
);
2208 fatal_perror (errmsg
);
2214 int_handler
= (void (*) (int)) signal (SIGINT
, SIG_IGN
);
2216 quit_handler
= (void (*) (int)) signal (SIGQUIT
, SIG_IGN
);
2219 inf
= pex_read_output (pex
, 0);
2221 fatal_perror ("can't open ldd output");
2224 notice ("\nldd output with constructors/destructors.\n");
2226 /* Read each line of ldd output. */
2227 while (fgets (buf
, sizeof buf
, inf
) != (char *) 0)
2230 char *name
, *end
, *p
= buf
;
2232 /* Extract names of libraries and add to list. */
2233 PARSE_LDD_OUTPUT (p
);
2238 if (strncmp (name
, "not found", sizeof ("not found") - 1) == 0)
2239 fatal ("dynamic dependency %s not found", buf
);
2241 /* Find the end of the symbol name. */
2243 (ch2
= *end
) != '\0' && ch2
!= '\n' && !ISSPACE (ch2
) && ch2
!= '|';
2248 if (access (name
, R_OK
) == 0)
2249 add_to_list (&libraries
, name
);
2251 fatal ("unable to open dynamic dependency '%s'", buf
);
2254 fprintf (stderr
, "\t%s\n", buf
);
2257 fprintf (stderr
, "\n");
2259 do_wait (ldd_file_name
, pex
);
2261 signal (SIGINT
, int_handler
);
2263 signal (SIGQUIT
, quit_handler
);
2266 /* Now iterate through the library list adding their symbols to
2268 for (list
= libraries
.first
; list
; list
= list
->next
)
2269 scan_prog_file (list
->name
, PASS_LIB
);
2272 #endif /* LDD_SUFFIX */
2274 #endif /* OBJECT_FORMAT_NONE */
2278 * COFF specific stuff.
2281 #ifdef OBJECT_FORMAT_COFF
2283 #if defined (EXTENDED_COFF)
2285 # define GCC_SYMBOLS(X) (SYMHEADER(X).isymMax + SYMHEADER(X).iextMax)
2286 # define GCC_SYMENT SYMR
2287 # define GCC_OK_SYMBOL(X) ((X).st == stProc || (X).st == stGlobal)
2288 # define GCC_SYMINC(X) (1)
2289 # define GCC_SYMZERO(X) (SYMHEADER(X).isymMax)
2290 # define GCC_CHECK_HDR(X) (PSYMTAB(X) != 0)
2294 # define GCC_SYMBOLS(X) (HEADER(ldptr).f_nsyms)
2295 # define GCC_SYMENT SYMENT
2296 # if defined (C_WEAKEXT)
2297 # define GCC_OK_SYMBOL(X) \
2298 (((X).n_sclass == C_EXT || (X).n_sclass == C_WEAKEXT) && \
2299 ((X).n_scnum > N_UNDEF) && \
2301 || (((X).n_type & N_TMASK) == (DT_NON << N_BTSHFT) \
2302 || ((X).n_type & N_TMASK) == (DT_FCN << N_BTSHFT))))
2303 # define GCC_UNDEF_SYMBOL(X) \
2304 (((X).n_sclass == C_EXT || (X).n_sclass == C_WEAKEXT) && \
2305 ((X).n_scnum == N_UNDEF))
2307 # define GCC_OK_SYMBOL(X) \
2308 (((X).n_sclass == C_EXT) && \
2309 ((X).n_scnum > N_UNDEF) && \
2311 || (((X).n_type & N_TMASK) == (DT_NON << N_BTSHFT) \
2312 || ((X).n_type & N_TMASK) == (DT_FCN << N_BTSHFT))))
2313 # define GCC_UNDEF_SYMBOL(X) \
2314 (((X).n_sclass == C_EXT) && ((X).n_scnum == N_UNDEF))
2316 # define GCC_SYMINC(X) ((X).n_numaux+1)
2317 # define GCC_SYMZERO(X) 0
2319 /* 0757 = U803XTOCMAGIC (AIX 4.3) and 0767 = U64_TOCMAGIC (AIX V5) */
2321 # define GCC_CHECK_HDR(X) \
2322 ((HEADER (X).f_magic == U802TOCMAGIC && ! aix64_flag) \
2323 || (HEADER (X).f_magic == 0767 && aix64_flag))
2325 # define GCC_CHECK_HDR(X) \
2326 ((HEADER (X).f_magic == U802TOCMAGIC && ! aix64_flag) \
2327 || (HEADER (X).f_magic == 0757 && aix64_flag))
2332 #ifdef COLLECT_EXPORT_LIST
2333 /* Array of standard AIX libraries which should not
2334 be scanned for ctors/dtors. */
2335 static const char *const aix_std_libs
[] = {
2343 "/usr/lib/libc_r.a",
2344 "/usr/lib/libm_r.a",
2345 "/usr/lib/threads/libc.a",
2346 "/usr/ccs/lib/libc.a",
2347 "/usr/ccs/lib/libm.a",
2348 "/usr/ccs/lib/libc_r.a",
2349 "/usr/ccs/lib/libm_r.a",
2353 /* This function checks the filename and returns 1
2354 if this name matches the location of a standard AIX library. */
2355 static int ignore_library (const char *);
2357 ignore_library (const char *name
)
2359 const char *const *p
= &aix_std_libs
[0];
2360 while (*p
++ != NULL
)
2361 if (! strcmp (name
, *p
)) return 1;
2364 #endif /* COLLECT_EXPORT_LIST */
2366 #if defined (HAVE_DECL_LDGETNAME) && !HAVE_DECL_LDGETNAME
2367 extern char *ldgetname (LDFILE
*, GCC_SYMENT
*);
2370 /* COFF version to scan the name list of the loaded program for
2371 the symbols g++ uses for static constructors and destructors.
2373 The constructor table begins at __CTOR_LIST__ and contains a count
2374 of the number of pointers (or -1 if the constructors are built in a
2375 separate section by the linker), followed by the pointers to the
2376 constructor functions, terminated with a null pointer. The
2377 destructor table has the same format, and begins at __DTOR_LIST__. */
2380 scan_prog_file (const char *prog_name
, enum pass which_pass
)
2382 LDFILE
*ldptr
= NULL
;
2383 int sym_index
, sym_count
;
2386 if (which_pass
!= PASS_FIRST
&& which_pass
!= PASS_OBJ
)
2389 #ifdef COLLECT_EXPORT_LIST
2390 /* We do not need scanning for some standard C libraries. */
2391 if (which_pass
== PASS_FIRST
&& ignore_library (prog_name
))
2394 /* On AIX we have a loop, because there is not much difference
2395 between an object and an archive. This trick allows us to
2396 eliminate scan_libraries() function. */
2400 /* Some platforms (e.g. OSF4) declare ldopen as taking a
2401 non-const char * filename parameter, even though it will not
2402 modify that string. So we must cast away const-ness here,
2403 which will cause -Wcast-qual to burp. */
2404 if ((ldptr
= ldopen ((char *)prog_name
, ldptr
)) != NULL
)
2406 if (! MY_ISCOFF (HEADER (ldptr
).f_magic
))
2407 fatal ("%s: not a COFF file", prog_name
);
2409 if (GCC_CHECK_HDR (ldptr
))
2411 sym_count
= GCC_SYMBOLS (ldptr
);
2412 sym_index
= GCC_SYMZERO (ldptr
);
2414 #ifdef COLLECT_EXPORT_LIST
2415 /* Is current archive member a shared object? */
2416 is_shared
= HEADER (ldptr
).f_flags
& F_SHROBJ
;
2419 while (sym_index
< sym_count
)
2423 if (ldtbread (ldptr
, sym_index
, &symbol
) <= 0)
2425 sym_index
+= GCC_SYMINC (symbol
);
2427 if (GCC_OK_SYMBOL (symbol
))
2431 if ((name
= ldgetname (ldptr
, &symbol
)) == NULL
)
2432 continue; /* Should never happen. */
2434 #ifdef XCOFF_DEBUGGING_INFO
2435 /* All AIX function names have a duplicate entry
2436 beginning with a dot. */
2441 switch (is_ctor_dtor (name
))
2445 add_to_list (&constructors
, name
);
2446 #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
2447 if (which_pass
== PASS_OBJ
)
2448 add_to_list (&exports
, name
);
2454 add_to_list (&destructors
, name
);
2455 #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
2456 if (which_pass
== PASS_OBJ
)
2457 add_to_list (&exports
, name
);
2461 #ifdef COLLECT_EXPORT_LIST
2463 #ifndef LD_INIT_SWITCH
2465 add_to_list (&constructors
, name
);
2470 #ifndef LD_INIT_SWITCH
2472 add_to_list (&destructors
, name
);
2479 add_to_list (&frame_tables
, name
);
2480 #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
2481 if (which_pass
== PASS_OBJ
)
2482 add_to_list (&exports
, name
);
2486 default: /* not a constructor or destructor */
2487 #ifdef COLLECT_EXPORT_LIST
2488 /* Explicitly export all global symbols when
2489 building a shared object on AIX, but do not
2490 re-export symbols from another shared object
2491 and do not export symbols if the user
2492 provides an explicit export list. */
2493 if (shared_obj
&& !is_shared
2494 && which_pass
== PASS_OBJ
&& !export_flag
)
2495 add_to_list (&exports
, name
);
2501 #if !defined(EXTENDED_COFF)
2502 fprintf (stderr
, "\tsec=%d class=%d type=%s%o %s\n",
2503 symbol
.n_scnum
, symbol
.n_sclass
,
2504 (symbol
.n_type
? "0" : ""), symbol
.n_type
,
2508 "\tiss = %5d, value = %5ld, index = %5d, name = %s\n",
2509 symbol
.iss
, (long) symbol
.value
, symbol
.index
, name
);
2514 #ifdef COLLECT_EXPORT_LIST
2517 /* If archive contains both 32-bit and 64-bit objects,
2518 we want to skip objects in other mode so mismatch normal. */
2520 fprintf (stderr
, "%s : magic=%o aix64=%d mismatch\n",
2521 prog_name
, HEADER (ldptr
).f_magic
, aix64_flag
);
2527 fatal ("%s: cannot open as COFF file", prog_name
);
2529 #ifdef COLLECT_EXPORT_LIST
2530 /* On AIX loop continues while there are more members in archive. */
2532 while (ldclose (ldptr
) == FAILURE
);
2534 /* Otherwise we simply close ldptr. */
2535 (void) ldclose(ldptr
);
2538 #endif /* OBJECT_FORMAT_COFF */
2540 #ifdef COLLECT_EXPORT_LIST
2541 /* Given a library name without "lib" prefix, this function
2542 returns a full library name including a path. */
2544 resolve_lib_name (const char *name
)
2548 /* Library extensions for AIX dynamic linking. */
2549 const char * const libexts
[2] = {"a", "so"};
2551 for (i
= 0; libpaths
[i
]; i
++)
2552 if (libpaths
[i
]->max_len
> l
)
2553 l
= libpaths
[i
]->max_len
;
2555 lib_buf
= xmalloc (l
+ strlen(name
) + 10);
2557 for (i
= 0; libpaths
[i
]; i
++)
2559 struct prefix_list
*list
= libpaths
[i
]->plist
;
2560 for (; list
; list
= list
->next
)
2562 /* The following lines are needed because path_prefix list
2563 may contain directories both with trailing '/' and
2566 if (list
->prefix
[strlen(list
->prefix
)-1] != '/')
2568 for (j
= 0; j
< 2; j
++)
2570 sprintf (lib_buf
, "%s%slib%s.%s",
2571 list
->prefix
, p
, name
,
2572 libexts
[(j
+ aixrtl_flag
) % 2]);
2573 if (debug
) fprintf (stderr
, "searching for: %s\n", lib_buf
);
2574 if (file_exists (lib_buf
))
2576 if (debug
) fprintf (stderr
, "found: %s\n", lib_buf
);
2583 fprintf (stderr
, "not found\n");
2585 fatal ("library lib%s not found", name
);
2588 #endif /* COLLECT_EXPORT_LIST */