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, 1999, 2000,
4 2001, 2002, 2003, 2004, 2005, 2007 Free Software Foundation, Inc.
5 Contributed by Chris Smith (csmith@convex.com).
6 Heavily modified by Michael Meissner (meissner@cygnus.com),
7 Per Bothner (bothner@cygnus.com), and John Gilmore (gnu@cygnus.com).
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
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 COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
26 /* Build tables of static constructors and destructors and run ld. */
30 #include "coretypes.h"
33 #if ! defined( SIGCHLD ) && defined( SIGCLD )
34 # define SIGCHLD SIGCLD
37 #ifndef LIBRARY_PATH_ENV
38 #define LIBRARY_PATH_ENV "LIBRARY_PATH"
49 /* On certain systems, we have code that works by scanning the object file
50 directly. But this code uses system-specific header files and library
51 functions, so turn it off in a cross-compiler. Likewise, the names of
52 the utilities are not correct for a cross-compiler; we have to hope that
53 cross-versions are in the proper directories. */
56 #undef OBJECT_FORMAT_COFF
58 #undef REAL_LD_FILE_NAME
59 #undef REAL_NM_FILE_NAME
60 #undef REAL_STRIP_FILE_NAME
63 /* If we cannot use a special method, use the ordinary one:
64 run nm to find what symbols are present.
65 In a cross-compiler, this means you need a cross nm,
66 but that is not quite as unpleasant as special headers. */
68 #if !defined (OBJECT_FORMAT_COFF)
69 #define OBJECT_FORMAT_NONE
72 #ifdef OBJECT_FORMAT_COFF
81 /* Many versions of ldfcn.h define these. */
89 /* Some systems have an ISCOFF macro, but others do not. In some cases
90 the macro may be wrong. MY_ISCOFF is defined in tm.h files for machines
91 that either do not have an ISCOFF macro in /usr/include or for those
95 #define MY_ISCOFF(X) ISCOFF (X)
98 #endif /* OBJECT_FORMAT_COFF */
100 #ifdef OBJECT_FORMAT_NONE
102 /* Default flags to pass to nm. */
104 #define NM_FLAGS "-n"
107 #endif /* OBJECT_FORMAT_NONE */
109 /* Some systems use __main in a way incompatible with its use in gcc, in these
110 cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
111 give the same symbol without quotes for an alternative entry point. */
113 #define NAME__MAIN "__main"
116 /* This must match tree.h. */
117 #define DEFAULT_INIT_PRIORITY 65535
119 #ifndef COLLECT_SHARED_INIT_FUNC
120 #define COLLECT_SHARED_INIT_FUNC(STREAM, FUNC) \
121 fprintf ((STREAM), "void _GLOBAL__DI() {\n\t%s();\n}\n", (FUNC))
123 #ifndef COLLECT_SHARED_FINI_FUNC
124 #define COLLECT_SHARED_FINI_FUNC(STREAM, FUNC) \
125 fprintf ((STREAM), "void _GLOBAL__DD() {\n\t%s();\n}\n", (FUNC))
129 #define SCAN_LIBRARIES
133 int do_collecting
= 1;
135 int do_collecting
= 0;
138 /* Nonzero if we should suppress the automatic demangling of identifiers
139 in linker error messages. Set from COLLECT_NO_DEMANGLE. */
142 /* Linked lists of constructor and destructor names. */
158 /* Enumeration giving which pass this is for scanning the program file. */
161 PASS_FIRST
, /* without constructors */
162 PASS_OBJ
, /* individual objects */
163 PASS_LIB
, /* looking for shared libraries */
164 PASS_SECOND
/* with constructors linked in */
167 int vflag
; /* true if -v */
168 static int rflag
; /* true if -r */
169 static int strip_flag
; /* true if -s */
170 static const char *demangle_flag
;
171 #ifdef COLLECT_EXPORT_LIST
172 static int export_flag
; /* true if -bE */
173 static int aix64_flag
; /* true if -b64 */
174 static int aixrtl_flag
; /* true if -brtl */
177 int debug
; /* true if -debug */
179 static int shared_obj
; /* true if -shared */
181 static const char *c_file
; /* <xxx>.c for constructor/destructor list. */
182 static const char *o_file
; /* <xxx>.o for constructor/destructor list. */
183 #ifdef COLLECT_EXPORT_LIST
184 static const char *export_file
; /* <xxx>.x for AIX export list. */
186 const char *ldout
; /* File for ld stdout. */
187 const char *lderrout
; /* File for ld stderr. */
188 static const char *output_file
; /* Output file for ld. */
189 static const char *nm_file_name
; /* pathname of nm */
191 static const char *ldd_file_name
; /* pathname of ldd (or equivalent) */
193 static const char *strip_file_name
; /* pathname of strip */
194 const char *c_file_name
; /* pathname of gcc */
195 static char *initname
, *fininame
; /* names of init and fini funcs */
197 static struct head constructors
; /* list of constructors found */
198 static struct head destructors
; /* list of destructors found */
199 #ifdef COLLECT_EXPORT_LIST
200 static struct head exports
; /* list of exported symbols */
202 static struct head frame_tables
; /* list of frame unwind info tables */
204 struct obstack temporary_obstack
;
205 char * temporary_firstobj
;
207 /* Structure to hold all the directories in which to search for files to
212 const char *prefix
; /* String to prepend to the path. */
213 struct prefix_list
*next
; /* Next in linked list. */
218 struct prefix_list
*plist
; /* List of prefixes to try */
219 int max_len
; /* Max length of a prefix in PLIST */
220 const char *name
; /* Name of this list (used in config stuff) */
223 #ifdef COLLECT_EXPORT_LIST
224 /* Lists to keep libraries to be scanned for global constructors/destructors. */
225 static struct head libs
; /* list of libraries */
226 static struct path_prefix cmdline_lib_dirs
; /* directories specified with -L */
227 static struct path_prefix libpath_lib_dirs
; /* directories in LIBPATH */
228 static struct path_prefix
*libpaths
[3] = {&cmdline_lib_dirs
,
229 &libpath_lib_dirs
, NULL
};
232 static void handler (int);
233 static int is_ctor_dtor (const char *);
234 static char *find_a_file (struct path_prefix
*, const char *);
235 static void add_prefix (struct path_prefix
*, const char *);
236 static void prefix_from_env (const char *, struct path_prefix
*);
237 static void prefix_from_string (const char *, struct path_prefix
*);
238 static void do_wait (const char *, struct pex_obj
*);
239 static void fork_execute (const char *, char **);
240 static void maybe_unlink (const char *);
241 static void add_to_list (struct head
*, const char *);
242 static int extract_init_priority (const char *);
243 static void sort_ids (struct head
*);
244 static void write_list (FILE *, const char *, struct id
*);
245 #ifdef COLLECT_EXPORT_LIST
246 static void dump_list (FILE *, const char *, struct id
*);
249 static void dump_prefix_list (FILE *, const char *, struct prefix_list
*);
251 static void write_list_with_asm (FILE *, const char *, struct id
*);
252 static void write_c_file (FILE *, const char *);
253 static void write_c_file_stat (FILE *, const char *);
254 #ifndef LD_INIT_SWITCH
255 static void write_c_file_glob (FILE *, const char *);
257 static void scan_prog_file (const char *, enum pass
);
258 #ifdef SCAN_LIBRARIES
259 static void scan_libraries (const char *);
261 #if LINK_ELIMINATE_DUPLICATE_LDIRECTORIES
262 static int is_in_args (const char *, const char **, const char **);
264 #ifdef COLLECT_EXPORT_LIST
266 static int is_in_list (const char *, struct id
*);
268 static void write_aix_file (FILE *, struct id
*);
269 static char *resolve_lib_name (const char *);
271 static char *extract_string (const char **);
273 /* Delete tempfiles and exit function. */
276 collect_exit (int status
)
278 if (c_file
!= 0 && c_file
[0])
279 maybe_unlink (c_file
);
281 if (o_file
!= 0 && o_file
[0])
282 maybe_unlink (o_file
);
284 #ifdef COLLECT_EXPORT_LIST
285 if (export_file
!= 0 && export_file
[0])
286 maybe_unlink (export_file
);
289 if (ldout
!= 0 && ldout
[0])
291 dump_file (ldout
, stdout
);
292 maybe_unlink (ldout
);
295 if (lderrout
!= 0 && lderrout
[0])
297 dump_file (lderrout
, stderr
);
298 maybe_unlink (lderrout
);
301 if (status
!= 0 && output_file
!= 0 && output_file
[0])
302 maybe_unlink (output_file
);
308 /* Notify user of a non-error. */
310 notice (const char *cmsgid
, ...)
314 va_start (ap
, cmsgid
);
315 vfprintf (stderr
, _(cmsgid
), ap
);
319 /* Die when sys call fails. */
322 fatal_perror (const char * cmsgid
, ...)
327 va_start (ap
, cmsgid
);
328 fprintf (stderr
, "collect2: ");
329 vfprintf (stderr
, _(cmsgid
), ap
);
330 fprintf (stderr
, ": %s\n", xstrerror (e
));
333 collect_exit (FATAL_EXIT_CODE
);
339 fatal (const char * cmsgid
, ...)
343 va_start (ap
, cmsgid
);
344 fprintf (stderr
, "collect2: ");
345 vfprintf (stderr
, _(cmsgid
), ap
);
346 fprintf (stderr
, "\n");
349 collect_exit (FATAL_EXIT_CODE
);
352 /* Write error message. */
355 error (const char * gmsgid
, ...)
359 va_start (ap
, gmsgid
);
360 fprintf (stderr
, "collect2: ");
361 vfprintf (stderr
, _(gmsgid
), ap
);
362 fprintf (stderr
, "\n");
366 /* In case obstack is linked in, and abort is defined to fancy_abort,
367 provide a default entry. */
370 fancy_abort (const char *file
, int line
, const char *func
)
372 fatal ("internal gcc abort in %s, at %s:%d", func
, file
, line
);
378 if (c_file
!= 0 && c_file
[0])
379 maybe_unlink (c_file
);
381 if (o_file
!= 0 && o_file
[0])
382 maybe_unlink (o_file
);
384 if (ldout
!= 0 && ldout
[0])
385 maybe_unlink (ldout
);
387 if (lderrout
!= 0 && lderrout
[0])
388 maybe_unlink (lderrout
);
390 #ifdef COLLECT_EXPORT_LIST
391 if (export_file
!= 0 && export_file
[0])
392 maybe_unlink (export_file
);
395 signal (signo
, SIG_DFL
);
401 file_exists (const char *name
)
403 return access (name
, R_OK
) == 0;
406 /* Parse a reasonable subset of shell quoting syntax. */
409 extract_string (const char **pp
)
422 obstack_1grow (&temporary_obstack
, c
);
423 else if (! inside
&& c
== ' ')
425 else if (! inside
&& c
== '\\')
430 obstack_1grow (&temporary_obstack
, c
);
433 obstack_1grow (&temporary_obstack
, '\0');
435 return XOBFINISH (&temporary_obstack
, char *);
439 dump_file (const char *name
, FILE *to
)
441 FILE *stream
= fopen (name
, "r");
448 while (c
= getc (stream
),
449 c
!= EOF
&& (ISIDNUM (c
) || c
== '$' || c
== '.'))
450 obstack_1grow (&temporary_obstack
, c
);
451 if (obstack_object_size (&temporary_obstack
) > 0)
453 const char *word
, *p
;
455 obstack_1grow (&temporary_obstack
, '\0');
456 word
= XOBFINISH (&temporary_obstack
, const char *);
459 ++word
, putc ('.', to
);
461 if (!strncmp (p
, USER_LABEL_PREFIX
, strlen (USER_LABEL_PREFIX
)))
462 p
+= strlen (USER_LABEL_PREFIX
);
464 #ifdef HAVE_LD_DEMANGLE
470 result
= cplus_demangle (p
, DMGL_PARAMS
| DMGL_ANSI
| DMGL_VERBOSE
);
478 diff
= strlen (word
) - strlen (result
);
479 while (diff
> 0 && c
== ' ')
480 --diff
, putc (' ', to
);
481 while (diff
< 0 && c
== ' ')
482 ++diff
, c
= getc (stream
);
490 obstack_free (&temporary_obstack
, temporary_firstobj
);
499 /* Decide whether the given symbol is: a constructor (1), a destructor
500 (2), a routine in a shared object that calls all the constructors
501 (3) or destructors (4), a DWARF exception-handling table (5), or
502 nothing special (0). */
505 is_ctor_dtor (const char *s
)
507 struct names
{ const char *const name
; const int len
; const int ret
;
508 const int two_underscores
; };
510 const struct names
*p
;
512 const char *orig_s
= s
;
514 static const struct names special
[] = {
515 #ifndef NO_DOLLAR_IN_LABEL
516 { "GLOBAL__I$", sizeof ("GLOBAL__I$")-1, 1, 0 },
517 { "GLOBAL__D$", sizeof ("GLOBAL__D$")-1, 2, 0 },
519 #ifndef NO_DOT_IN_LABEL
520 { "GLOBAL__I.", sizeof ("GLOBAL__I.")-1, 1, 0 },
521 { "GLOBAL__D.", sizeof ("GLOBAL__D.")-1, 2, 0 },
522 #endif /* NO_DOT_IN_LABEL */
523 #endif /* NO_DOLLAR_IN_LABEL */
524 { "GLOBAL__I_", sizeof ("GLOBAL__I_")-1, 1, 0 },
525 { "GLOBAL__D_", sizeof ("GLOBAL__D_")-1, 2, 0 },
526 { "GLOBAL__F_", sizeof ("GLOBAL__F_")-1, 5, 0 },
527 { "GLOBAL__FI_", sizeof ("GLOBAL__FI_")-1, 3, 0 },
528 { "GLOBAL__FD_", sizeof ("GLOBAL__FD_")-1, 4, 0 },
532 while ((ch
= *s
) == '_')
538 for (p
= &special
[0]; p
->len
> 0; p
++)
541 && (!p
->two_underscores
|| ((s
- orig_s
) >= 2))
542 && strncmp(s
, p
->name
, p
->len
) == 0)
550 /* We maintain two prefix lists: one from COMPILER_PATH environment variable
551 and one from the PATH variable. */
553 static struct path_prefix cpath
, path
;
556 /* This is the name of the target machine. We use it to form the name
557 of the files to execute. */
559 static const char *const target_machine
= TARGET_MACHINE
;
562 /* Search for NAME using prefix list PPREFIX. We only look for executable
565 Return 0 if not found, otherwise return its name, allocated with malloc. */
568 find_a_file (struct path_prefix
*pprefix
, const char *name
)
571 struct prefix_list
*pl
;
572 int len
= pprefix
->max_len
+ strlen (name
) + 1;
575 fprintf (stderr
, "Looking for '%s'\n", name
);
577 #ifdef HOST_EXECUTABLE_SUFFIX
578 len
+= strlen (HOST_EXECUTABLE_SUFFIX
);
581 temp
= XNEWVEC (char, len
);
583 /* Determine the filename to execute (special case for absolute paths). */
586 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
587 || (*name
&& name
[1] == ':')
591 if (access (name
, X_OK
) == 0)
596 fprintf (stderr
, " - found: absolute path\n");
601 #ifdef HOST_EXECUTABLE_SUFFIX
602 /* Some systems have a suffix for executable files.
603 So try appending that. */
605 strcat (temp
, HOST_EXECUTABLE_SUFFIX
);
607 if (access (temp
, X_OK
) == 0)
612 fprintf (stderr
, " - failed to locate using absolute path\n");
615 for (pl
= pprefix
->plist
; pl
; pl
= pl
->next
)
619 strcpy (temp
, pl
->prefix
);
622 if (stat (temp
, &st
) >= 0
623 && ! S_ISDIR (st
.st_mode
)
624 && access (temp
, X_OK
) == 0)
627 #ifdef HOST_EXECUTABLE_SUFFIX
628 /* Some systems have a suffix for executable files.
629 So try appending that. */
630 strcat (temp
, HOST_EXECUTABLE_SUFFIX
);
632 if (stat (temp
, &st
) >= 0
633 && ! S_ISDIR (st
.st_mode
)
634 && access (temp
, X_OK
) == 0)
639 if (debug
&& pprefix
->plist
== NULL
)
640 fprintf (stderr
, " - failed: no entries in prefix list\n");
646 /* Add an entry for PREFIX to prefix list PPREFIX. */
649 add_prefix (struct path_prefix
*pprefix
, const char *prefix
)
651 struct prefix_list
*pl
, **prev
;
656 for (pl
= pprefix
->plist
; pl
->next
; pl
= pl
->next
)
661 prev
= &pprefix
->plist
;
663 /* Keep track of the longest prefix. */
665 len
= strlen (prefix
);
666 if (len
> pprefix
->max_len
)
667 pprefix
->max_len
= len
;
669 pl
= XNEW (struct prefix_list
);
670 pl
->prefix
= xstrdup (prefix
);
675 pl
->next
= (struct prefix_list
*) 0;
679 /* Take the value of the environment variable ENV, break it into a path, and
680 add of the entries to PPREFIX. */
683 prefix_from_env (const char *env
, struct path_prefix
*pprefix
)
686 GET_ENVIRONMENT (p
, env
);
689 prefix_from_string (p
, pprefix
);
693 prefix_from_string (const char *p
, struct path_prefix
*pprefix
)
695 const char *startp
, *endp
;
696 char *nstore
= XNEWVEC (char, strlen (p
) + 3);
699 fprintf (stderr
, "Convert string '%s' into prefixes, separator = '%c'\n", p
, PATH_SEPARATOR
);
704 if (*endp
== PATH_SEPARATOR
|| *endp
== 0)
706 strncpy (nstore
, startp
, endp
-startp
);
709 strcpy (nstore
, "./");
711 else if (! IS_DIR_SEPARATOR (endp
[-1]))
713 nstore
[endp
-startp
] = DIR_SEPARATOR
;
714 nstore
[endp
-startp
+1] = 0;
717 nstore
[endp
-startp
] = 0;
720 fprintf (stderr
, " - add prefix: %s\n", nstore
);
722 add_prefix (pprefix
, nstore
);
725 endp
= startp
= endp
+ 1;
735 main (int argc
, char **argv
)
737 static const char *const ld_suffix
= "ld";
738 static const char *const real_ld_suffix
= "real-ld";
739 static const char *const collect_ld_suffix
= "collect-ld";
740 static const char *const nm_suffix
= "nm";
741 static const char *const gnm_suffix
= "gnm";
743 static const char *const ldd_suffix
= LDD_SUFFIX
;
745 static const char *const strip_suffix
= "strip";
746 static const char *const gstrip_suffix
= "gstrip";
749 /* If we look for a program in the compiler directories, we just use
750 the short name, since these directories are already system-specific.
751 But it we look for a program in the system directories, we need to
752 qualify the program name with the target machine. */
754 const char *const full_ld_suffix
=
755 concat(target_machine
, "-", ld_suffix
, NULL
);
756 const char *const full_nm_suffix
=
757 concat (target_machine
, "-", nm_suffix
, NULL
);
758 const char *const full_gnm_suffix
=
759 concat (target_machine
, "-", gnm_suffix
, NULL
);
761 const char *const full_ldd_suffix
=
762 concat (target_machine
, "-", ldd_suffix
, NULL
);
764 const char *const full_strip_suffix
=
765 concat (target_machine
, "-", strip_suffix
, NULL
);
766 const char *const full_gstrip_suffix
=
767 concat (target_machine
, "-", gstrip_suffix
, NULL
);
769 const char *const full_ld_suffix
= ld_suffix
;
770 const char *const full_nm_suffix
= nm_suffix
;
771 const char *const full_gnm_suffix
= gnm_suffix
;
773 const char *const full_ldd_suffix
= ldd_suffix
;
775 const char *const full_strip_suffix
= strip_suffix
;
776 const char *const full_gstrip_suffix
= gstrip_suffix
;
777 #endif /* CROSS_COMPILE */
781 #ifdef COLLECT_EXPORT_LIST
784 const char *ld_file_name
;
795 int num_c_args
= argc
+9;
797 no_demangle
= !! getenv ("COLLECT_NO_DEMANGLE");
799 /* Suppress demangling by the real linker, which may be broken. */
800 putenv (xstrdup ("COLLECT_NO_DEMANGLE="));
802 #if defined (COLLECT2_HOST_INITIALIZATION)
803 /* Perform system dependent initialization, if necessary. */
804 COLLECT2_HOST_INITIALIZATION
;
808 /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
809 receive the signal. A different setting is inheritable */
810 signal (SIGCHLD
, SIG_DFL
);
813 /* Unlock the stdio streams. */
814 unlock_std_streams ();
818 /* Do not invoke xcalloc before this point, since locale needs to be
819 set first, in case a diagnostic is issued. */
821 ld1
= (const char **)(ld1_argv
= xcalloc(sizeof (char *), argc
+4));
822 ld2
= (const char **)(ld2_argv
= xcalloc(sizeof (char *), argc
+11));
823 object
= (const char **)(object_lst
= xcalloc(sizeof (char *), argc
));
829 /* Parse command line early for instances of -debug. This allows
830 the debug flag to be set before functions like find_a_file()
835 for (i
= 1; argv
[i
] != NULL
; i
++)
837 if (! strcmp (argv
[i
], "-debug"))
843 #ifndef DEFAULT_A_OUT_NAME
844 output_file
= "a.out";
846 output_file
= DEFAULT_A_OUT_NAME
;
849 obstack_begin (&temporary_obstack
, 0);
850 temporary_firstobj
= obstack_alloc (&temporary_obstack
, 0);
852 #ifndef HAVE_LD_DEMANGLE
853 current_demangling_style
= auto_demangling
;
855 p
= getenv ("COLLECT_GCC_OPTIONS");
858 const char *q
= extract_string (&p
);
859 if (*q
== '-' && (q
[1] == 'm' || q
[1] == 'f'))
862 obstack_free (&temporary_obstack
, temporary_firstobj
);
864 /* -fno-profile-arcs -fno-test-coverage -fno-branch-probabilities
865 -fno-exceptions -w */
868 c_ptr
= (const char **) (c_argv
= xcalloc (sizeof (char *), num_c_args
));
871 fatal ("no arguments");
874 if (signal (SIGQUIT
, SIG_IGN
) != SIG_IGN
)
875 signal (SIGQUIT
, handler
);
877 if (signal (SIGINT
, SIG_IGN
) != SIG_IGN
)
878 signal (SIGINT
, handler
);
880 if (signal (SIGALRM
, SIG_IGN
) != SIG_IGN
)
881 signal (SIGALRM
, handler
);
884 if (signal (SIGHUP
, SIG_IGN
) != SIG_IGN
)
885 signal (SIGHUP
, handler
);
887 if (signal (SIGSEGV
, SIG_IGN
) != SIG_IGN
)
888 signal (SIGSEGV
, handler
);
890 if (signal (SIGBUS
, SIG_IGN
) != SIG_IGN
)
891 signal (SIGBUS
, handler
);
894 /* Extract COMPILER_PATH and PATH into our prefix list. */
895 prefix_from_env ("COMPILER_PATH", &cpath
);
896 prefix_from_env ("PATH", &path
);
898 /* Try to discover a valid linker/nm/strip to use. */
900 /* Maybe we know the right file to use (if not cross). */
902 #ifdef DEFAULT_LINKER
903 if (access (DEFAULT_LINKER
, X_OK
) == 0)
904 ld_file_name
= DEFAULT_LINKER
;
905 if (ld_file_name
== 0)
907 #ifdef REAL_LD_FILE_NAME
908 ld_file_name
= find_a_file (&path
, REAL_LD_FILE_NAME
);
909 if (ld_file_name
== 0)
911 /* Search the (target-specific) compiler dirs for ld'. */
912 ld_file_name
= find_a_file (&cpath
, real_ld_suffix
);
913 /* Likewise for `collect-ld'. */
914 if (ld_file_name
== 0)
915 ld_file_name
= find_a_file (&cpath
, collect_ld_suffix
);
916 /* Search the compiler directories for `ld'. We have protection against
917 recursive calls in find_a_file. */
918 if (ld_file_name
== 0)
919 ld_file_name
= find_a_file (&cpath
, ld_suffix
);
920 /* Search the ordinary system bin directories
921 for `ld' (if native linking) or `TARGET-ld' (if cross). */
922 if (ld_file_name
== 0)
923 ld_file_name
= find_a_file (&path
, full_ld_suffix
);
925 #ifdef REAL_NM_FILE_NAME
926 nm_file_name
= find_a_file (&path
, REAL_NM_FILE_NAME
);
927 if (nm_file_name
== 0)
929 nm_file_name
= find_a_file (&cpath
, gnm_suffix
);
930 if (nm_file_name
== 0)
931 nm_file_name
= find_a_file (&path
, full_gnm_suffix
);
932 if (nm_file_name
== 0)
933 nm_file_name
= find_a_file (&cpath
, nm_suffix
);
934 if (nm_file_name
== 0)
935 nm_file_name
= find_a_file (&path
, full_nm_suffix
);
938 ldd_file_name
= find_a_file (&cpath
, ldd_suffix
);
939 if (ldd_file_name
== 0)
940 ldd_file_name
= find_a_file (&path
, full_ldd_suffix
);
943 #ifdef REAL_STRIP_FILE_NAME
944 strip_file_name
= find_a_file (&path
, REAL_STRIP_FILE_NAME
);
945 if (strip_file_name
== 0)
947 strip_file_name
= find_a_file (&cpath
, gstrip_suffix
);
948 if (strip_file_name
== 0)
949 strip_file_name
= find_a_file (&path
, full_gstrip_suffix
);
950 if (strip_file_name
== 0)
951 strip_file_name
= find_a_file (&cpath
, strip_suffix
);
952 if (strip_file_name
== 0)
953 strip_file_name
= find_a_file (&path
, full_strip_suffix
);
955 /* Determine the full path name of the C compiler to use. */
956 c_file_name
= getenv ("COLLECT_GCC");
957 if (c_file_name
== 0)
960 c_file_name
= concat (target_machine
, "-gcc", NULL
);
966 p
= find_a_file (&cpath
, c_file_name
);
968 /* Here it should be safe to use the system search path since we should have
969 already qualified the name of the compiler when it is needed. */
971 p
= find_a_file (&path
, c_file_name
);
976 *ld1
++ = *ld2
++ = ld_file_name
;
978 /* Make temp file names. */
979 c_file
= make_temp_file (".c");
980 o_file
= make_temp_file (".o");
981 #ifdef COLLECT_EXPORT_LIST
982 export_file
= make_temp_file (".x");
984 ldout
= make_temp_file (".ld");
985 lderrout
= make_temp_file (".le");
986 *c_ptr
++ = c_file_name
;
993 #ifdef COLLECT_EXPORT_LIST
994 /* Generate a list of directories from LIBPATH. */
995 prefix_from_env ("LIBPATH", &libpath_lib_dirs
);
996 /* Add to this list also two standard directories where
997 AIX loader always searches for libraries. */
998 add_prefix (&libpath_lib_dirs
, "/lib");
999 add_prefix (&libpath_lib_dirs
, "/usr/lib");
1002 /* Get any options that the upper GCC wants to pass to the sub-GCC.
1004 AIX support needs to know if -shared has been specified before
1005 parsing commandline arguments. */
1007 p
= getenv ("COLLECT_GCC_OPTIONS");
1010 const char *q
= extract_string (&p
);
1011 if (*q
== '-' && (q
[1] == 'm' || q
[1] == 'f'))
1012 *c_ptr
++ = xstrdup (q
);
1013 if (strcmp (q
, "-EL") == 0 || strcmp (q
, "-EB") == 0)
1014 *c_ptr
++ = xstrdup (q
);
1015 if (strcmp (q
, "-shared") == 0)
1017 if (*q
== '-' && q
[1] == 'B')
1019 *c_ptr
++ = xstrdup (q
);
1022 q
= extract_string (&p
);
1023 *c_ptr
++ = xstrdup (q
);
1027 obstack_free (&temporary_obstack
, temporary_firstobj
);
1028 *c_ptr
++ = "-fno-profile-arcs";
1029 *c_ptr
++ = "-fno-test-coverage";
1030 *c_ptr
++ = "-fno-branch-probabilities";
1031 *c_ptr
++ = "-fno-exceptions";
1034 /* !!! When GCC calls collect2,
1035 it does not know whether it is calling collect2 or ld.
1036 So collect2 cannot meaningfully understand any options
1037 except those ld understands.
1038 If you propose to make GCC pass some other option,
1039 just imagine what will happen if ld is really ld!!! */
1041 /* Parse arguments. Remember output file spec, pass the rest to ld. */
1042 /* After the first file, put in the c++ rt0. */
1045 #ifdef HAVE_LD_DEMANGLE
1046 if (!demangle_flag
&& !no_demangle
)
1047 demangle_flag
= "--demangle";
1049 *ld1
++ = *ld2
++ = demangle_flag
;
1051 while ((arg
= *++argv
) != (char *) 0)
1053 *ld1
++ = *ld2
++ = arg
;
1059 #ifdef COLLECT_EXPORT_LIST
1060 /* We want to disable automatic exports on AIX when user
1061 explicitly puts an export list in command line */
1063 if (arg
[2] == 'E' || strncmp (&arg
[2], "export", 6) == 0)
1065 else if (arg
[2] == '6' && arg
[3] == '4')
1067 else if (arg
[2] == 'r' && arg
[3] == 't' && arg
[4] == 'l')
1073 if (!strcmp (arg
, "-debug"))
1075 /* Already parsed. */
1079 if (!strcmp (arg
, "-dynamic-linker") && argv
[1])
1082 *ld1
++ = *ld2
++ = *argv
;
1089 /* place o_file BEFORE this argument! */
1095 #ifdef COLLECT_EXPORT_LIST
1097 /* Resolving full library name. */
1098 const char *s
= resolve_lib_name (arg
+2);
1100 /* Saving a full library name. */
1101 add_to_list (&libs
, s
);
1106 #ifdef COLLECT_EXPORT_LIST
1107 /* Saving directories where to search for libraries. */
1109 add_prefix (&cmdline_lib_dirs
, arg
+2);
1112 #if LINK_ELIMINATE_DUPLICATE_LDIRECTORIES
1114 if (is_in_args (arg
, (const char **) ld1_argv
, ld1
-1))
1117 #endif /* LINK_ELIMINATE_DUPLICATE_LDIRECTORIES */
1122 output_file
= *ld1
++ = *ld2
++ = *++argv
;
1124 #ifdef SWITCHES_NEED_SPACES
1125 && ! strchr (SWITCHES_NEED_SPACES
, arg
[1])
1129 output_file
= &arg
[2];
1138 if (arg
[2] == '\0' && do_collecting
)
1140 /* We must strip after the nm run, otherwise C++ linking
1141 will not work. Thus we strip in the second ld run, or
1142 else with strip if there is no second ld run. */
1154 if (strcmp (arg
, "--no-demangle") == 0)
1156 demangle_flag
= arg
;
1161 else if (strncmp (arg
, "--demangle", 10) == 0)
1163 demangle_flag
= arg
;
1165 #ifndef HAVE_LD_DEMANGLE
1168 enum demangling_styles style
1169 = cplus_demangle_name_to_style (arg
+11);
1170 if (style
== unknown_demangling
)
1171 error ("unknown demangling style '%s'", arg
+11);
1173 current_demangling_style
= style
;
1182 else if ((p
= strrchr (arg
, '.')) != (char *) 0
1183 && (strcmp (p
, ".o") == 0 || strcmp (p
, ".a") == 0
1184 || strcmp (p
, ".so") == 0 || strcmp (p
, ".lo") == 0
1185 || strcmp (p
, ".obj") == 0))
1194 /* place o_file BEFORE this argument! */
1200 if (p
[1] == 'o' || p
[1] == 'l')
1202 #ifdef COLLECT_EXPORT_LIST
1203 /* libraries can be specified directly, i.e. without -l flag. */
1206 /* Saving a full library name. */
1207 add_to_list (&libs
, arg
);
1213 #ifdef COLLECT_EXPORT_LIST
1214 /* This is added only for debugging purposes. */
1217 fprintf (stderr
, "List of libraries:\n");
1218 dump_list (stderr
, "\t", libs
.first
);
1221 /* The AIX linker will discard static constructors in object files if
1222 nothing else in the file is referenced, so look at them first. */
1224 const char **export_object_lst
= (const char **)object_lst
;
1226 while (export_object_lst
< object
)
1227 scan_prog_file (*export_object_lst
++, PASS_OBJ
);
1230 struct id
*list
= libs
.first
;
1232 for (; list
; list
= list
->next
)
1233 scan_prog_file (list
->name
, PASS_FIRST
);
1238 char *buf
= concat ("-bE:", export_file
, NULL
);
1243 exportf
= fopen (export_file
, "w");
1244 if (exportf
== (FILE *) 0)
1245 fatal_perror ("fopen %s", export_file
);
1246 write_aix_file (exportf
, exports
.first
);
1247 if (fclose (exportf
))
1248 fatal_perror ("fclose %s", export_file
);
1253 *c_ptr
= *ld1
= *object
= (char *) 0;
1257 notice ("collect2 version %s", version_string
);
1258 #ifdef TARGET_VERSION
1261 fprintf (stderr
, "\n");
1267 fprintf (stderr
, "ld_file_name = %s\n",
1268 (ld_file_name
? ld_file_name
: "not found"));
1269 fprintf (stderr
, "c_file_name = %s\n",
1270 (c_file_name
? c_file_name
: "not found"));
1271 fprintf (stderr
, "nm_file_name = %s\n",
1272 (nm_file_name
? nm_file_name
: "not found"));
1274 fprintf (stderr
, "ldd_file_name = %s\n",
1275 (ldd_file_name
? ldd_file_name
: "not found"));
1277 fprintf (stderr
, "strip_file_name = %s\n",
1278 (strip_file_name
? strip_file_name
: "not found"));
1279 fprintf (stderr
, "c_file = %s\n",
1280 (c_file
? c_file
: "not found"));
1281 fprintf (stderr
, "o_file = %s\n",
1282 (o_file
? o_file
: "not found"));
1284 ptr
= getenv ("COLLECT_GCC_OPTIONS");
1286 fprintf (stderr
, "COLLECT_GCC_OPTIONS = %s\n", ptr
);
1288 ptr
= getenv ("COLLECT_GCC");
1290 fprintf (stderr
, "COLLECT_GCC = %s\n", ptr
);
1292 ptr
= getenv ("COMPILER_PATH");
1294 fprintf (stderr
, "COMPILER_PATH = %s\n", ptr
);
1296 ptr
= getenv (LIBRARY_PATH_ENV
);
1298 fprintf (stderr
, "%-20s= %s\n", LIBRARY_PATH_ENV
, ptr
);
1300 fprintf (stderr
, "\n");
1303 /* Load the program, searching all libraries and attempting to provide
1304 undefined symbols from repository information. */
1306 /* On AIX we do this later. */
1307 #ifndef COLLECT_EXPORT_LIST
1308 do_tlink (ld1_argv
, object_lst
);
1311 /* If -r or they will be run via some other method, do not build the
1312 constructor or destructor list, just return now. */
1314 #ifndef COLLECT_EXPORT_LIST
1319 #ifdef COLLECT_EXPORT_LIST
1320 /* Do the link we avoided above if we are exiting. */
1321 do_tlink (ld1_argv
, object_lst
);
1323 /* But make sure we delete the export file we may have created. */
1324 if (export_file
!= 0 && export_file
[0])
1325 maybe_unlink (export_file
);
1327 maybe_unlink (c_file
);
1328 maybe_unlink (o_file
);
1332 /* Examine the namelist with nm and search it for static constructors
1333 and destructors to call.
1334 Write the constructor and destructor tables to a .s file and reload. */
1336 /* On AIX we already scanned for global constructors/destructors. */
1337 #ifndef COLLECT_EXPORT_LIST
1338 scan_prog_file (output_file
, PASS_FIRST
);
1341 #ifdef SCAN_LIBRARIES
1342 scan_libraries (output_file
);
1347 notice ("%d constructor(s) found\n", constructors
.number
);
1348 notice ("%d destructor(s) found\n", destructors
.number
);
1349 notice ("%d frame table(s) found\n", frame_tables
.number
);
1352 if (constructors
.number
== 0 && destructors
.number
== 0
1353 && frame_tables
.number
== 0
1354 #if defined (SCAN_LIBRARIES) || defined (COLLECT_EXPORT_LIST)
1355 /* If we will be running these functions ourselves, we want to emit
1356 stubs into the shared library so that we do not have to relink
1357 dependent programs when we add static objects. */
1362 #ifdef COLLECT_EXPORT_LIST
1363 /* Do tlink without additional code generation. */
1364 do_tlink (ld1_argv
, object_lst
);
1366 /* Strip now if it was requested on the command line. */
1369 char **real_strip_argv
= XCNEWVEC (char *, 3);
1370 const char ** strip_argv
= (const char **) real_strip_argv
;
1372 strip_argv
[0] = strip_file_name
;
1373 strip_argv
[1] = output_file
;
1374 strip_argv
[2] = (char *) 0;
1375 fork_execute ("strip", real_strip_argv
);
1378 #ifdef COLLECT_EXPORT_LIST
1379 maybe_unlink (export_file
);
1381 maybe_unlink (c_file
);
1382 maybe_unlink (o_file
);
1386 /* Sort ctor and dtor lists by priority. */
1387 sort_ids (&constructors
);
1388 sort_ids (&destructors
);
1390 maybe_unlink(output_file
);
1391 outf
= fopen (c_file
, "w");
1392 if (outf
== (FILE *) 0)
1393 fatal_perror ("fopen %s", c_file
);
1395 write_c_file (outf
, c_file
);
1398 fatal_perror ("fclose %s", c_file
);
1400 /* Tell the linker that we have initializer and finalizer functions. */
1401 #ifdef LD_INIT_SWITCH
1402 #ifdef COLLECT_EXPORT_LIST
1403 *ld2
++ = concat (LD_INIT_SWITCH
, ":", initname
, ":", fininame
, NULL
);
1405 *ld2
++ = LD_INIT_SWITCH
;
1407 *ld2
++ = LD_FINI_SWITCH
;
1412 #ifdef COLLECT_EXPORT_LIST
1415 /* If we did not add export flag to link arguments before, add it to
1416 second link phase now. No new exports should have been added. */
1417 if (! exports
.first
)
1418 *ld2
++ = concat ("-bE:", export_file
, NULL
);
1420 #ifndef LD_INIT_SWITCH
1421 add_to_list (&exports
, initname
);
1422 add_to_list (&exports
, fininame
);
1423 add_to_list (&exports
, "_GLOBAL__DI");
1424 add_to_list (&exports
, "_GLOBAL__DD");
1426 exportf
= fopen (export_file
, "w");
1427 if (exportf
== (FILE *) 0)
1428 fatal_perror ("fopen %s", export_file
);
1429 write_aix_file (exportf
, exports
.first
);
1430 if (fclose (exportf
))
1431 fatal_perror ("fclose %s", export_file
);
1435 /* End of arguments to second link phase. */
1440 fprintf (stderr
, "\n========== output_file = %s, c_file = %s\n",
1441 output_file
, c_file
);
1442 write_c_file (stderr
, "stderr");
1443 fprintf (stderr
, "========== end of c_file\n\n");
1444 #ifdef COLLECT_EXPORT_LIST
1445 fprintf (stderr
, "\n========== export_file = %s\n", export_file
);
1446 write_aix_file (stderr
, exports
.first
);
1447 fprintf (stderr
, "========== end of export_file\n\n");
1451 /* Assemble the constructor and destructor tables.
1452 Link the tables in with the rest of the program. */
1454 fork_execute ("gcc", c_argv
);
1455 #ifdef COLLECT_EXPORT_LIST
1456 /* On AIX we must call tlink because of possible templates resolution. */
1457 do_tlink (ld2_argv
, object_lst
);
1459 /* Otherwise, simply call ld because tlink is already done. */
1460 fork_execute ("ld", ld2_argv
);
1462 /* Let scan_prog_file do any final mods (OSF/rose needs this for
1463 constructors/destructors in shared libraries. */
1464 scan_prog_file (output_file
, PASS_SECOND
);
1467 maybe_unlink (c_file
);
1468 maybe_unlink (o_file
);
1470 #ifdef COLLECT_EXPORT_LIST
1471 maybe_unlink (export_file
);
1478 /* Wait for a process to finish, and exit if a nonzero status is found. */
1481 collect_wait (const char *prog
, struct pex_obj
*pex
)
1485 if (!pex_get_status (pex
, 1, &status
))
1486 fatal_perror ("can't get program status");
1491 if (WIFSIGNALED (status
))
1493 int sig
= WTERMSIG (status
);
1494 error ("%s terminated with signal %d [%s]%s",
1495 prog
, sig
, strsignal(sig
),
1496 WCOREDUMP(status
) ? ", core dumped" : "");
1497 collect_exit (FATAL_EXIT_CODE
);
1500 if (WIFEXITED (status
))
1501 return WEXITSTATUS (status
);
1507 do_wait (const char *prog
, struct pex_obj
*pex
)
1509 int ret
= collect_wait (prog
, pex
);
1512 error ("%s returned %d exit status", prog
, ret
);
1518 /* Execute a program, and wait for the reply. */
1521 collect_execute (const char *prog
, char **argv
, const char *outname
,
1522 const char *errname
)
1524 struct pex_obj
*pex
;
1534 fprintf (stderr
, "%s", argv
[0]);
1536 notice ("[cannot find %s]", prog
);
1538 for (p_argv
= &argv
[1]; (str
= *p_argv
) != (char *) 0; p_argv
++)
1539 fprintf (stderr
, " %s", str
);
1541 fprintf (stderr
, "\n");
1547 /* If we cannot find a program we need, complain error. Do this here
1548 since we might not end up needing something that we could not find. */
1551 fatal ("cannot find '%s'", prog
);
1553 pex
= pex_init (0, "collect2", NULL
);
1555 fatal_perror ("pex_init failed");
1557 errmsg
= pex_run (pex
, PEX_LAST
| PEX_SEARCH
, argv
[0], argv
, outname
,
1564 fatal_perror (errmsg
);
1574 fork_execute (const char *prog
, char **argv
)
1576 struct pex_obj
*pex
;
1578 pex
= collect_execute (prog
, argv
, NULL
, NULL
);
1579 do_wait (prog
, pex
);
1582 /* Unlink a file unless we are debugging. */
1585 maybe_unlink (const char *file
)
1588 unlink_if_ordinary (file
);
1590 notice ("[Leaving %s]\n", file
);
1594 static long sequence_number
= 0;
1596 /* Add a name to a linked list. */
1599 add_to_list (struct head
*head_ptr
, const char *name
)
1601 struct id
*newid
= xcalloc (sizeof (struct id
) + strlen (name
), 1);
1603 strcpy (newid
->name
, name
);
1605 if (head_ptr
->first
)
1606 head_ptr
->last
->next
= newid
;
1608 head_ptr
->first
= newid
;
1610 /* Check for duplicate symbols. */
1611 for (p
= head_ptr
->first
;
1612 strcmp (name
, p
->name
) != 0;
1617 head_ptr
->last
->next
= 0;
1622 newid
->sequence
= ++sequence_number
;
1623 head_ptr
->last
= newid
;
1627 /* Grab the init priority number from an init function name that
1628 looks like "_GLOBAL_.I.12345.foo". */
1631 extract_init_priority (const char *name
)
1635 while (name
[pos
] == '_')
1637 pos
+= 10; /* strlen ("GLOBAL__X_") */
1639 /* Extract init_p number from ctor/dtor name. */
1640 pri
= atoi (name
+ pos
);
1641 return pri
? pri
: DEFAULT_INIT_PRIORITY
;
1644 /* Insertion sort the ids from ctor/dtor list HEAD_PTR in descending order.
1645 ctors will be run from right to left, dtors from left to right. */
1648 sort_ids (struct head
*head_ptr
)
1650 /* id holds the current element to insert. id_next holds the next
1651 element to insert. id_ptr iterates through the already sorted elements
1652 looking for the place to insert id. */
1653 struct id
*id
, *id_next
, **id_ptr
;
1655 id
= head_ptr
->first
;
1657 /* We don't have any sorted elements yet. */
1658 head_ptr
->first
= NULL
;
1660 for (; id
; id
= id_next
)
1663 id
->sequence
= extract_init_priority (id
->name
);
1665 for (id_ptr
= &(head_ptr
->first
); ; id_ptr
= &((*id_ptr
)->next
))
1667 /* If the sequence numbers are the same, we put the id from the
1668 file later on the command line later in the list. */
1669 || id
->sequence
> (*id_ptr
)->sequence
1670 /* Hack: do lexical compare, too.
1671 || (id->sequence == (*id_ptr)->sequence
1672 && strcmp (id->name, (*id_ptr)->name) > 0) */
1681 /* Now set the sequence numbers properly so write_c_file works. */
1682 for (id
= head_ptr
->first
; id
; id
= id
->next
)
1683 id
->sequence
= ++sequence_number
;
1686 /* Write: `prefix', the names on list LIST, `suffix'. */
1689 write_list (FILE *stream
, const char *prefix
, struct id
*list
)
1693 fprintf (stream
, "%sx%d,\n", prefix
, list
->sequence
);
1698 #if LINK_ELIMINATE_DUPLICATE_LDIRECTORIES
1699 /* Given a STRING, return nonzero if it occurs in the list in range
1700 [ARGS_BEGIN,ARGS_END). */
1703 is_in_args (const char *string
, const char **args_begin
,
1704 const char **args_end
)
1706 const char **args_pointer
;
1707 for (args_pointer
= args_begin
; args_pointer
!= args_end
; ++args_pointer
)
1708 if (strcmp (string
, *args_pointer
) == 0)
1712 #endif /* LINK_ELIMINATE_DUPLICATE_LDIRECTORIES */
1714 #ifdef COLLECT_EXPORT_LIST
1715 /* This function is really used only on AIX, but may be useful. */
1718 is_in_list (const char *prefix
, struct id
*list
)
1722 if (!strcmp (prefix
, list
->name
)) return 1;
1728 #endif /* COLLECT_EXPORT_LIST */
1730 /* Added for debugging purpose. */
1731 #ifdef COLLECT_EXPORT_LIST
1733 dump_list (FILE *stream
, const char *prefix
, struct id
*list
)
1737 fprintf (stream
, "%s%s,\n", prefix
, list
->name
);
1745 dump_prefix_list (FILE *stream
, const char *prefix
, struct prefix_list
*list
)
1749 fprintf (stream
, "%s%s,\n", prefix
, list
->prefix
);
1756 write_list_with_asm (FILE *stream
, const char *prefix
, struct id
*list
)
1760 fprintf (stream
, "%sx%d __asm__ (\"%s\");\n",
1761 prefix
, list
->sequence
, list
->name
);
1766 /* Write out the constructor and destructor tables statically (for a shared
1767 object), along with the functions to execute them. */
1770 write_c_file_stat (FILE *stream
, const char *name ATTRIBUTE_UNUSED
)
1774 int frames
= (frame_tables
.number
> 0);
1776 /* Figure out name of output_file, stripping off .so version. */
1777 p
= strrchr (output_file
, '/');
1793 if (strncmp (q
, ".so", 3) == 0)
1802 /* q points to null at end of the string (or . of the .so version) */
1803 prefix
= XNEWVEC (char, q
- p
+ 1);
1804 strncpy (prefix
, p
, q
- p
);
1806 for (r
= prefix
; *r
; r
++)
1807 if (!ISALNUM ((unsigned char)*r
))
1810 notice ("\nwrite_c_file - output name is %s, prefix is %s\n",
1811 output_file
, prefix
);
1813 initname
= concat ("_GLOBAL__FI_", prefix
, NULL
);
1814 fininame
= concat ("_GLOBAL__FD_", prefix
, NULL
);
1818 /* Write the tables as C code. */
1820 fprintf (stream
, "static int count;\n");
1821 fprintf (stream
, "typedef void entry_pt();\n");
1822 write_list_with_asm (stream
, "extern entry_pt ", constructors
.first
);
1826 write_list_with_asm (stream
, "extern void *", frame_tables
.first
);
1828 fprintf (stream
, "\tstatic void *frame_table[] = {\n");
1829 write_list (stream
, "\t\t&", frame_tables
.first
);
1830 fprintf (stream
, "\t0\n};\n");
1832 /* This must match what's in frame.h. */
1833 fprintf (stream
, "struct object {\n");
1834 fprintf (stream
, " void *pc_begin;\n");
1835 fprintf (stream
, " void *pc_end;\n");
1836 fprintf (stream
, " void *fde_begin;\n");
1837 fprintf (stream
, " void *fde_array;\n");
1838 fprintf (stream
, " __SIZE_TYPE__ count;\n");
1839 fprintf (stream
, " struct object *next;\n");
1840 fprintf (stream
, "};\n");
1842 fprintf (stream
, "extern void __register_frame_info_table (void *, struct object *);\n");
1843 fprintf (stream
, "extern void *__deregister_frame_info (void *);\n");
1845 fprintf (stream
, "static void reg_frame () {\n");
1846 fprintf (stream
, "\tstatic struct object ob;\n");
1847 fprintf (stream
, "\t__register_frame_info_table (frame_table, &ob);\n");
1848 fprintf (stream
, "\t}\n");
1850 fprintf (stream
, "static void dereg_frame () {\n");
1851 fprintf (stream
, "\t__deregister_frame_info (frame_table);\n");
1852 fprintf (stream
, "\t}\n");
1855 fprintf (stream
, "void %s() {\n", initname
);
1856 if (constructors
.number
> 0 || frames
)
1858 fprintf (stream
, "\tstatic entry_pt *ctors[] = {\n");
1859 write_list (stream
, "\t\t", constructors
.first
);
1861 fprintf (stream
, "\treg_frame,\n");
1862 fprintf (stream
, "\t};\n");
1863 fprintf (stream
, "\tentry_pt **p;\n");
1864 fprintf (stream
, "\tif (count++ != 0) return;\n");
1865 fprintf (stream
, "\tp = ctors + %d;\n", constructors
.number
+ frames
);
1866 fprintf (stream
, "\twhile (p > ctors) (*--p)();\n");
1869 fprintf (stream
, "\t++count;\n");
1870 fprintf (stream
, "}\n");
1871 write_list_with_asm (stream
, "extern entry_pt ", destructors
.first
);
1872 fprintf (stream
, "void %s() {\n", fininame
);
1873 if (destructors
.number
> 0 || frames
)
1875 fprintf (stream
, "\tstatic entry_pt *dtors[] = {\n");
1876 write_list (stream
, "\t\t", destructors
.first
);
1878 fprintf (stream
, "\tdereg_frame,\n");
1879 fprintf (stream
, "\t};\n");
1880 fprintf (stream
, "\tentry_pt **p;\n");
1881 fprintf (stream
, "\tif (--count != 0) return;\n");
1882 fprintf (stream
, "\tp = dtors;\n");
1883 fprintf (stream
, "\twhile (p < dtors + %d) (*p++)();\n",
1884 destructors
.number
+ frames
);
1886 fprintf (stream
, "}\n");
1890 COLLECT_SHARED_INIT_FUNC(stream
, initname
);
1891 COLLECT_SHARED_FINI_FUNC(stream
, fininame
);
1895 /* Write the constructor/destructor tables. */
1897 #ifndef LD_INIT_SWITCH
1899 write_c_file_glob (FILE *stream
, const char *name ATTRIBUTE_UNUSED
)
1901 /* Write the tables as C code. */
1903 int frames
= (frame_tables
.number
> 0);
1905 fprintf (stream
, "typedef void entry_pt();\n\n");
1907 write_list_with_asm (stream
, "extern entry_pt ", constructors
.first
);
1911 write_list_with_asm (stream
, "extern void *", frame_tables
.first
);
1913 fprintf (stream
, "\tstatic void *frame_table[] = {\n");
1914 write_list (stream
, "\t\t&", frame_tables
.first
);
1915 fprintf (stream
, "\t0\n};\n");
1917 /* This must match what's in frame.h. */
1918 fprintf (stream
, "struct object {\n");
1919 fprintf (stream
, " void *pc_begin;\n");
1920 fprintf (stream
, " void *pc_end;\n");
1921 fprintf (stream
, " void *fde_begin;\n");
1922 fprintf (stream
, " void *fde_array;\n");
1923 fprintf (stream
, " __SIZE_TYPE__ count;\n");
1924 fprintf (stream
, " struct object *next;\n");
1925 fprintf (stream
, "};\n");
1927 fprintf (stream
, "extern void __register_frame_info_table (void *, struct object *);\n");
1928 fprintf (stream
, "extern void *__deregister_frame_info (void *);\n");
1930 fprintf (stream
, "static void reg_frame () {\n");
1931 fprintf (stream
, "\tstatic struct object ob;\n");
1932 fprintf (stream
, "\t__register_frame_info_table (frame_table, &ob);\n");
1933 fprintf (stream
, "\t}\n");
1935 fprintf (stream
, "static void dereg_frame () {\n");
1936 fprintf (stream
, "\t__deregister_frame_info (frame_table);\n");
1937 fprintf (stream
, "\t}\n");
1940 fprintf (stream
, "\nentry_pt * __CTOR_LIST__[] = {\n");
1941 fprintf (stream
, "\t(entry_pt *) %d,\n", constructors
.number
+ frames
);
1942 write_list (stream
, "\t", constructors
.first
);
1944 fprintf (stream
, "\treg_frame,\n");
1945 fprintf (stream
, "\t0\n};\n\n");
1947 write_list_with_asm (stream
, "extern entry_pt ", destructors
.first
);
1949 fprintf (stream
, "\nentry_pt * __DTOR_LIST__[] = {\n");
1950 fprintf (stream
, "\t(entry_pt *) %d,\n", destructors
.number
+ frames
);
1951 write_list (stream
, "\t", destructors
.first
);
1953 fprintf (stream
, "\tdereg_frame,\n");
1954 fprintf (stream
, "\t0\n};\n\n");
1956 fprintf (stream
, "extern entry_pt %s;\n", NAME__MAIN
);
1957 fprintf (stream
, "entry_pt *__main_reference = %s;\n\n", NAME__MAIN
);
1959 #endif /* ! LD_INIT_SWITCH */
1962 write_c_file (FILE *stream
, const char *name
)
1964 fprintf (stream
, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n");
1965 #ifndef LD_INIT_SWITCH
1967 write_c_file_glob (stream
, name
);
1970 write_c_file_stat (stream
, name
);
1971 fprintf (stream
, "#ifdef __cplusplus\n}\n#endif\n");
1974 #ifdef COLLECT_EXPORT_LIST
1976 write_aix_file (FILE *stream
, struct id
*list
)
1978 for (; list
; list
= list
->next
)
1980 fputs (list
->name
, stream
);
1981 putc ('\n', stream
);
1986 #ifdef OBJECT_FORMAT_NONE
1988 /* Generic version to scan the name list of the loaded program for
1989 the symbols g++ uses for static constructors and destructors.
1991 The constructor table begins at __CTOR_LIST__ and contains a count
1992 of the number of pointers (or -1 if the constructors are built in a
1993 separate section by the linker), followed by the pointers to the
1994 constructor functions, terminated with a null pointer. The
1995 destructor table has the same format, and begins at __DTOR_LIST__. */
1998 scan_prog_file (const char *prog_name
, enum pass which_pass
)
2000 void (*int_handler
) (int);
2002 void (*quit_handler
) (int);
2004 char *real_nm_argv
[4];
2005 const char **nm_argv
= (const char **) real_nm_argv
;
2007 struct pex_obj
*pex
;
2013 if (which_pass
== PASS_SECOND
)
2016 /* If we do not have an `nm', complain. */
2017 if (nm_file_name
== 0)
2018 fatal ("cannot find 'nm'");
2020 nm_argv
[argc
++] = nm_file_name
;
2021 if (NM_FLAGS
[0] != '\0')
2022 nm_argv
[argc
++] = NM_FLAGS
;
2024 nm_argv
[argc
++] = prog_name
;
2025 nm_argv
[argc
++] = (char *) 0;
2027 /* Trace if needed. */
2030 const char **p_argv
;
2033 for (p_argv
= &nm_argv
[0]; (str
= *p_argv
) != (char *) 0; p_argv
++)
2034 fprintf (stderr
, " %s", str
);
2036 fprintf (stderr
, "\n");
2042 pex
= pex_init (PEX_USE_PIPES
, "collect2", NULL
);
2044 fatal_perror ("pex_init failed");
2046 errmsg
= pex_run (pex
, 0, nm_file_name
, real_nm_argv
, NULL
, NULL
, &err
);
2052 fatal_perror (errmsg
);
2058 int_handler
= (void (*) (int)) signal (SIGINT
, SIG_IGN
);
2060 quit_handler
= (void (*) (int)) signal (SIGQUIT
, SIG_IGN
);
2063 inf
= pex_read_output (pex
, 0);
2065 fatal_perror ("can't open nm output");
2068 fprintf (stderr
, "\nnm output with constructors/destructors.\n");
2070 /* Read each line of nm output. */
2071 while (fgets (buf
, sizeof buf
, inf
) != (char *) 0)
2076 /* If it contains a constructor or destructor name, add the name
2077 to the appropriate list. */
2079 for (p
= buf
; (ch
= *p
) != '\0' && ch
!= '\n' && ch
!= '_'; p
++)
2080 if (ch
== ' ' && p
[1] == 'U' && p
[2] == ' ')
2087 /* Find the end of the symbol name.
2088 Do not include `|', because Encore nm can tack that on the end. */
2089 for (end
= p
; (ch2
= *end
) != '\0' && !ISSPACE (ch2
) && ch2
!= '|';
2095 switch (is_ctor_dtor (name
))
2098 if (which_pass
!= PASS_LIB
)
2099 add_to_list (&constructors
, name
);
2103 if (which_pass
!= PASS_LIB
)
2104 add_to_list (&destructors
, name
);
2108 if (which_pass
!= PASS_LIB
)
2109 fatal ("init function found in object %s", prog_name
);
2110 #ifndef LD_INIT_SWITCH
2111 add_to_list (&constructors
, name
);
2116 if (which_pass
!= PASS_LIB
)
2117 fatal ("fini function found in object %s", prog_name
);
2118 #ifndef LD_FINI_SWITCH
2119 add_to_list (&destructors
, name
);
2124 if (which_pass
!= PASS_LIB
)
2125 add_to_list (&frame_tables
, name
);
2128 default: /* not a constructor or destructor */
2133 fprintf (stderr
, "\t%s\n", buf
);
2137 fprintf (stderr
, "\n");
2139 do_wait (nm_file_name
, pex
);
2141 signal (SIGINT
, int_handler
);
2143 signal (SIGQUIT
, quit_handler
);
2149 /* Use the List Dynamic Dependencies program to find shared libraries that
2150 the output file depends upon and their initialization/finalization
2151 routines, if any. */
2154 scan_libraries (const char *prog_name
)
2156 static struct head libraries
; /* list of shared libraries found */
2158 void (*int_handler
) (int);
2160 void (*quit_handler
) (int);
2162 char *real_ldd_argv
[4];
2163 const char **ldd_argv
= (const char **) real_ldd_argv
;
2165 struct pex_obj
*pex
;
2171 /* If we do not have an `ldd', complain. */
2172 if (ldd_file_name
== 0)
2174 error ("cannot find 'ldd'");
2178 ldd_argv
[argc
++] = ldd_file_name
;
2179 ldd_argv
[argc
++] = prog_name
;
2180 ldd_argv
[argc
++] = (char *) 0;
2182 /* Trace if needed. */
2185 const char **p_argv
;
2188 for (p_argv
= &ldd_argv
[0]; (str
= *p_argv
) != (char *) 0; p_argv
++)
2189 fprintf (stderr
, " %s", str
);
2191 fprintf (stderr
, "\n");
2197 pex
= pex_init (PEX_USE_PIPES
, "collect2", NULL
);
2199 fatal_perror ("pex_init failed");
2201 errmsg
= pex_run (pex
, 0, ldd_file_name
, real_ldd_argv
, NULL
, NULL
, &err
);
2207 fatal_perror (errmsg
);
2213 int_handler
= (void (*) (int)) signal (SIGINT
, SIG_IGN
);
2215 quit_handler
= (void (*) (int)) signal (SIGQUIT
, SIG_IGN
);
2218 inf
= pex_read_output (pex
, 0);
2220 fatal_perror ("can't open ldd output");
2223 notice ("\nldd output with constructors/destructors.\n");
2225 /* Read each line of ldd output. */
2226 while (fgets (buf
, sizeof buf
, inf
) != (char *) 0)
2229 char *name
, *end
, *p
= buf
;
2231 /* Extract names of libraries and add to list. */
2232 PARSE_LDD_OUTPUT (p
);
2237 if (strncmp (name
, "not found", sizeof ("not found") - 1) == 0)
2238 fatal ("dynamic dependency %s not found", buf
);
2240 /* Find the end of the symbol name. */
2242 (ch2
= *end
) != '\0' && ch2
!= '\n' && !ISSPACE (ch2
) && ch2
!= '|';
2247 if (access (name
, R_OK
) == 0)
2248 add_to_list (&libraries
, name
);
2250 fatal ("unable to open dynamic dependency '%s'", buf
);
2253 fprintf (stderr
, "\t%s\n", buf
);
2256 fprintf (stderr
, "\n");
2258 do_wait (ldd_file_name
, pex
);
2260 signal (SIGINT
, int_handler
);
2262 signal (SIGQUIT
, quit_handler
);
2265 /* Now iterate through the library list adding their symbols to
2267 for (list
= libraries
.first
; list
; list
= list
->next
)
2268 scan_prog_file (list
->name
, PASS_LIB
);
2271 #endif /* LDD_SUFFIX */
2273 #endif /* OBJECT_FORMAT_NONE */
2277 * COFF specific stuff.
2280 #ifdef OBJECT_FORMAT_COFF
2282 #if defined (EXTENDED_COFF)
2284 # define GCC_SYMBOLS(X) (SYMHEADER(X).isymMax + SYMHEADER(X).iextMax)
2285 # define GCC_SYMENT SYMR
2286 # define GCC_OK_SYMBOL(X) ((X).st == stProc || (X).st == stGlobal)
2287 # define GCC_SYMINC(X) (1)
2288 # define GCC_SYMZERO(X) (SYMHEADER(X).isymMax)
2289 # define GCC_CHECK_HDR(X) (PSYMTAB(X) != 0)
2293 # define GCC_SYMBOLS(X) (HEADER(ldptr).f_nsyms)
2294 # define GCC_SYMENT SYMENT
2295 # if defined (C_WEAKEXT)
2296 # define GCC_OK_SYMBOL(X) \
2297 (((X).n_sclass == C_EXT || (X).n_sclass == C_WEAKEXT) && \
2298 ((X).n_scnum > N_UNDEF) && \
2300 || (((X).n_type & N_TMASK) == (DT_NON << N_BTSHFT) \
2301 || ((X).n_type & N_TMASK) == (DT_FCN << N_BTSHFT))))
2302 # define GCC_UNDEF_SYMBOL(X) \
2303 (((X).n_sclass == C_EXT || (X).n_sclass == C_WEAKEXT) && \
2304 ((X).n_scnum == N_UNDEF))
2306 # define GCC_OK_SYMBOL(X) \
2307 (((X).n_sclass == C_EXT) && \
2308 ((X).n_scnum > N_UNDEF) && \
2310 || (((X).n_type & N_TMASK) == (DT_NON << N_BTSHFT) \
2311 || ((X).n_type & N_TMASK) == (DT_FCN << N_BTSHFT))))
2312 # define GCC_UNDEF_SYMBOL(X) \
2313 (((X).n_sclass == C_EXT) && ((X).n_scnum == N_UNDEF))
2315 # define GCC_SYMINC(X) ((X).n_numaux+1)
2316 # define GCC_SYMZERO(X) 0
2318 /* 0757 = U803XTOCMAGIC (AIX 4.3) and 0767 = U64_TOCMAGIC (AIX V5) */
2320 # define GCC_CHECK_HDR(X) \
2321 ((HEADER (X).f_magic == U802TOCMAGIC && ! aix64_flag) \
2322 || (HEADER (X).f_magic == 0767 && aix64_flag))
2324 # define GCC_CHECK_HDR(X) \
2325 ((HEADER (X).f_magic == U802TOCMAGIC && ! aix64_flag) \
2326 || (HEADER (X).f_magic == 0757 && aix64_flag))
2331 #ifdef COLLECT_EXPORT_LIST
2332 /* Array of standard AIX libraries which should not
2333 be scanned for ctors/dtors. */
2334 static const char *const aix_std_libs
[] = {
2342 "/usr/lib/libc_r.a",
2343 "/usr/lib/libm_r.a",
2344 "/usr/lib/threads/libc.a",
2345 "/usr/ccs/lib/libc.a",
2346 "/usr/ccs/lib/libm.a",
2347 "/usr/ccs/lib/libc_r.a",
2348 "/usr/ccs/lib/libm_r.a",
2352 /* This function checks the filename and returns 1
2353 if this name matches the location of a standard AIX library. */
2354 static int ignore_library (const char *);
2356 ignore_library (const char *name
)
2358 const char *const *p
= &aix_std_libs
[0];
2359 while (*p
++ != NULL
)
2360 if (! strcmp (name
, *p
)) return 1;
2363 #endif /* COLLECT_EXPORT_LIST */
2365 #if defined (HAVE_DECL_LDGETNAME) && !HAVE_DECL_LDGETNAME
2366 extern char *ldgetname (LDFILE
*, GCC_SYMENT
*);
2369 /* COFF version to scan the name list of the loaded program for
2370 the symbols g++ uses for static constructors and destructors.
2372 The constructor table begins at __CTOR_LIST__ and contains a count
2373 of the number of pointers (or -1 if the constructors are built in a
2374 separate section by the linker), followed by the pointers to the
2375 constructor functions, terminated with a null pointer. The
2376 destructor table has the same format, and begins at __DTOR_LIST__. */
2379 scan_prog_file (const char *prog_name
, enum pass which_pass
)
2381 LDFILE
*ldptr
= NULL
;
2382 int sym_index
, sym_count
;
2385 if (which_pass
!= PASS_FIRST
&& which_pass
!= PASS_OBJ
)
2388 #ifdef COLLECT_EXPORT_LIST
2389 /* We do not need scanning for some standard C libraries. */
2390 if (which_pass
== PASS_FIRST
&& ignore_library (prog_name
))
2393 /* On AIX we have a loop, because there is not much difference
2394 between an object and an archive. This trick allows us to
2395 eliminate scan_libraries() function. */
2399 /* Some platforms (e.g. OSF4) declare ldopen as taking a
2400 non-const char * filename parameter, even though it will not
2401 modify that string. So we must cast away const-ness here,
2402 which will cause -Wcast-qual to burp. */
2403 if ((ldptr
= ldopen ((char *)prog_name
, ldptr
)) != NULL
)
2405 if (! MY_ISCOFF (HEADER (ldptr
).f_magic
))
2406 fatal ("%s: not a COFF file", prog_name
);
2408 if (GCC_CHECK_HDR (ldptr
))
2410 sym_count
= GCC_SYMBOLS (ldptr
);
2411 sym_index
= GCC_SYMZERO (ldptr
);
2413 #ifdef COLLECT_EXPORT_LIST
2414 /* Is current archive member a shared object? */
2415 is_shared
= HEADER (ldptr
).f_flags
& F_SHROBJ
;
2418 while (sym_index
< sym_count
)
2422 if (ldtbread (ldptr
, sym_index
, &symbol
) <= 0)
2424 sym_index
+= GCC_SYMINC (symbol
);
2426 if (GCC_OK_SYMBOL (symbol
))
2430 if ((name
= ldgetname (ldptr
, &symbol
)) == NULL
)
2431 continue; /* Should never happen. */
2433 #ifdef XCOFF_DEBUGGING_INFO
2434 /* All AIX function names have a duplicate entry
2435 beginning with a dot. */
2440 switch (is_ctor_dtor (name
))
2444 add_to_list (&constructors
, name
);
2445 #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
2446 if (which_pass
== PASS_OBJ
)
2447 add_to_list (&exports
, name
);
2453 add_to_list (&destructors
, name
);
2454 #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
2455 if (which_pass
== PASS_OBJ
)
2456 add_to_list (&exports
, name
);
2460 #ifdef COLLECT_EXPORT_LIST
2462 #ifndef LD_INIT_SWITCH
2464 add_to_list (&constructors
, name
);
2469 #ifndef LD_INIT_SWITCH
2471 add_to_list (&destructors
, name
);
2478 add_to_list (&frame_tables
, name
);
2479 #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
2480 if (which_pass
== PASS_OBJ
)
2481 add_to_list (&exports
, name
);
2485 default: /* not a constructor or destructor */
2486 #ifdef COLLECT_EXPORT_LIST
2487 /* Explicitly export all global symbols when
2488 building a shared object on AIX, but do not
2489 re-export symbols from another shared object
2490 and do not export symbols if the user
2491 provides an explicit export list. */
2492 if (shared_obj
&& !is_shared
2493 && which_pass
== PASS_OBJ
&& !export_flag
)
2494 add_to_list (&exports
, name
);
2500 #if !defined(EXTENDED_COFF)
2501 fprintf (stderr
, "\tsec=%d class=%d type=%s%o %s\n",
2502 symbol
.n_scnum
, symbol
.n_sclass
,
2503 (symbol
.n_type
? "0" : ""), symbol
.n_type
,
2507 "\tiss = %5d, value = %5ld, index = %5d, name = %s\n",
2508 symbol
.iss
, (long) symbol
.value
, symbol
.index
, name
);
2513 #ifdef COLLECT_EXPORT_LIST
2516 /* If archive contains both 32-bit and 64-bit objects,
2517 we want to skip objects in other mode so mismatch normal. */
2519 fprintf (stderr
, "%s : magic=%o aix64=%d mismatch\n",
2520 prog_name
, HEADER (ldptr
).f_magic
, aix64_flag
);
2526 fatal ("%s: cannot open as COFF file", prog_name
);
2528 #ifdef COLLECT_EXPORT_LIST
2529 /* On AIX loop continues while there are more members in archive. */
2531 while (ldclose (ldptr
) == FAILURE
);
2533 /* Otherwise we simply close ldptr. */
2534 (void) ldclose(ldptr
);
2537 #endif /* OBJECT_FORMAT_COFF */
2539 #ifdef COLLECT_EXPORT_LIST
2540 /* Given a library name without "lib" prefix, this function
2541 returns a full library name including a path. */
2543 resolve_lib_name (const char *name
)
2547 /* Library extensions for AIX dynamic linking. */
2548 const char * const libexts
[2] = {"a", "so"};
2550 for (i
= 0; libpaths
[i
]; i
++)
2551 if (libpaths
[i
]->max_len
> l
)
2552 l
= libpaths
[i
]->max_len
;
2554 lib_buf
= xmalloc (l
+ strlen(name
) + 10);
2556 for (i
= 0; libpaths
[i
]; i
++)
2558 struct prefix_list
*list
= libpaths
[i
]->plist
;
2559 for (; list
; list
= list
->next
)
2561 /* The following lines are needed because path_prefix list
2562 may contain directories both with trailing '/' and
2565 if (list
->prefix
[strlen(list
->prefix
)-1] != '/')
2567 for (j
= 0; j
< 2; j
++)
2569 sprintf (lib_buf
, "%s%slib%s.%s",
2570 list
->prefix
, p
, name
,
2571 libexts
[(j
+ aixrtl_flag
) % 2]);
2572 if (debug
) fprintf (stderr
, "searching for: %s\n", lib_buf
);
2573 if (file_exists (lib_buf
))
2575 if (debug
) fprintf (stderr
, "found: %s\n", lib_buf
);
2582 fprintf (stderr
, "not found\n");
2584 fatal ("library lib%s not found", name
);
2587 #endif /* COLLECT_EXPORT_LIST */