1 /* Collect static initialization info into data structures that can be
2 traversed by C++ initialization and finalization routines.
3 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007 Free Software Foundation, Inc.
5 Contributed by Chris Smith (csmith@convex.com).
6 Heavily modified by Michael Meissner (meissner@cygnus.com),
7 Per Bothner (bothner@cygnus.com), and John Gilmore (gnu@cygnus.com).
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 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. */
55 #ifdef CROSS_DIRECTORY_STRUCTURE
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 #define SHLIB_SUFFIX ".so"
137 int do_collecting
= 1;
139 int do_collecting
= 0;
142 /* Nonzero if we should suppress the automatic demangling of identifiers
143 in linker error messages. Set from COLLECT_NO_DEMANGLE. */
146 /* Linked lists of constructor and destructor names. */
162 /* Enumeration giving which pass this is for scanning the program file. */
165 PASS_FIRST
, /* without constructors */
166 PASS_OBJ
, /* individual objects */
167 PASS_LIB
, /* looking for shared libraries */
168 PASS_SECOND
/* with constructors linked in */
171 int vflag
; /* true if -v */
172 static int rflag
; /* true if -r */
173 static int strip_flag
; /* true if -s */
174 static const char *demangle_flag
;
175 #ifdef COLLECT_EXPORT_LIST
176 static int export_flag
; /* true if -bE */
177 static int aix64_flag
; /* true if -b64 */
178 static int aixrtl_flag
; /* true if -brtl */
181 int debug
; /* true if -debug */
183 static int shared_obj
; /* true if -shared */
185 static const char *c_file
; /* <xxx>.c for constructor/destructor list. */
186 static const char *o_file
; /* <xxx>.o for constructor/destructor list. */
187 #ifdef COLLECT_EXPORT_LIST
188 static const char *export_file
; /* <xxx>.x for AIX export list. */
190 const char *ldout
; /* File for ld stdout. */
191 const char *lderrout
; /* File for ld stderr. */
192 static const char *output_file
; /* Output file for ld. */
193 static const char *nm_file_name
; /* pathname of nm */
195 static const char *ldd_file_name
; /* pathname of ldd (or equivalent) */
197 static const char *strip_file_name
; /* pathname of strip */
198 const char *c_file_name
; /* pathname of gcc */
199 static char *initname
, *fininame
; /* names of init and fini funcs */
201 static struct head constructors
; /* list of constructors found */
202 static struct head destructors
; /* list of destructors found */
203 #ifdef COLLECT_EXPORT_LIST
204 static struct head exports
; /* list of exported symbols */
206 static struct head frame_tables
; /* list of frame unwind info tables */
208 static bool at_file_supplied
; /* Whether to use @file arguments */
209 static char *response_file
; /* Name of any current response file */
211 struct obstack temporary_obstack
;
212 char * temporary_firstobj
;
214 /* Structure to hold all the directories in which to search for files to
219 const char *prefix
; /* String to prepend to the path. */
220 struct prefix_list
*next
; /* Next in linked list. */
225 struct prefix_list
*plist
; /* List of prefixes to try */
226 int max_len
; /* Max length of a prefix in PLIST */
227 const char *name
; /* Name of this list (used in config stuff) */
230 #ifdef COLLECT_EXPORT_LIST
231 /* Lists to keep libraries to be scanned for global constructors/destructors. */
232 static struct head libs
; /* list of libraries */
233 static struct path_prefix cmdline_lib_dirs
; /* directories specified with -L */
234 static struct path_prefix libpath_lib_dirs
; /* directories in LIBPATH */
235 static struct path_prefix
*libpaths
[3] = {&cmdline_lib_dirs
,
236 &libpath_lib_dirs
, NULL
};
239 /* Special kinds of symbols that a name may denote. */
242 SYM_REGULAR
= 0, /* nothing special */
244 SYM_CTOR
= 1, /* constructor */
245 SYM_DTOR
= 2, /* destructor */
246 SYM_INIT
= 3, /* shared object routine that calls all the ctors */
247 SYM_FINI
= 4, /* shared object routine that calls all the dtors */
248 SYM_DWEH
= 5 /* DWARF exception handling table */
251 static symkind
is_ctor_dtor (const char *);
253 static void handler (int);
254 static char *find_a_file (struct path_prefix
*, const char *);
255 static void add_prefix (struct path_prefix
*, const char *);
256 static void prefix_from_env (const char *, struct path_prefix
*);
257 static void prefix_from_string (const char *, struct path_prefix
*);
258 static void do_wait (const char *, struct pex_obj
*);
259 static void fork_execute (const char *, char **);
260 static void maybe_unlink (const char *);
261 static void add_to_list (struct head
*, const char *);
262 static int extract_init_priority (const char *);
263 static void sort_ids (struct head
*);
264 static void write_list (FILE *, const char *, struct id
*);
265 #ifdef COLLECT_EXPORT_LIST
266 static void dump_list (FILE *, const char *, struct id
*);
269 static void dump_prefix_list (FILE *, const char *, struct prefix_list
*);
271 static void write_list_with_asm (FILE *, const char *, struct id
*);
272 static void write_c_file (FILE *, const char *);
273 static void write_c_file_stat (FILE *, const char *);
274 #ifndef LD_INIT_SWITCH
275 static void write_c_file_glob (FILE *, const char *);
277 static void scan_prog_file (const char *, enum pass
);
278 #ifdef SCAN_LIBRARIES
279 static void scan_libraries (const char *);
281 #if LINK_ELIMINATE_DUPLICATE_LDIRECTORIES
282 static int is_in_args (const char *, const char **, const char **);
284 #ifdef COLLECT_EXPORT_LIST
286 static int is_in_list (const char *, struct id
*);
288 static void write_aix_file (FILE *, struct id
*);
289 static char *resolve_lib_name (const char *);
291 static char *extract_string (const char **);
293 /* Delete tempfiles and exit function. */
296 collect_exit (int status
)
298 if (c_file
!= 0 && c_file
[0])
299 maybe_unlink (c_file
);
301 if (o_file
!= 0 && o_file
[0])
302 maybe_unlink (o_file
);
304 #ifdef COLLECT_EXPORT_LIST
305 if (export_file
!= 0 && export_file
[0])
306 maybe_unlink (export_file
);
309 if (ldout
!= 0 && ldout
[0])
311 dump_file (ldout
, stdout
);
312 maybe_unlink (ldout
);
315 if (lderrout
!= 0 && lderrout
[0])
317 dump_file (lderrout
, stderr
);
318 maybe_unlink (lderrout
);
321 if (status
!= 0 && output_file
!= 0 && output_file
[0])
322 maybe_unlink (output_file
);
325 maybe_unlink (response_file
);
331 /* Notify user of a non-error. */
333 notice (const char *cmsgid
, ...)
337 va_start (ap
, cmsgid
);
338 vfprintf (stderr
, _(cmsgid
), ap
);
342 /* Die when sys call fails. */
345 fatal_perror (const char * cmsgid
, ...)
350 va_start (ap
, cmsgid
);
351 fprintf (stderr
, "collect2: ");
352 vfprintf (stderr
, _(cmsgid
), ap
);
353 fprintf (stderr
, ": %s\n", xstrerror (e
));
356 collect_exit (FATAL_EXIT_CODE
);
362 fatal (const char * cmsgid
, ...)
366 va_start (ap
, cmsgid
);
367 fprintf (stderr
, "collect2: ");
368 vfprintf (stderr
, _(cmsgid
), ap
);
369 fprintf (stderr
, "\n");
372 collect_exit (FATAL_EXIT_CODE
);
375 /* Write error message. */
378 error (const char * gmsgid
, ...)
382 va_start (ap
, gmsgid
);
383 fprintf (stderr
, "collect2: ");
384 vfprintf (stderr
, _(gmsgid
), ap
);
385 fprintf (stderr
, "\n");
389 /* In case obstack is linked in, and abort is defined to fancy_abort,
390 provide a default entry. */
393 fancy_abort (const char *file
, int line
, const char *func
)
395 fatal ("internal gcc abort in %s, at %s:%d", func
, file
, line
);
401 if (c_file
!= 0 && c_file
[0])
402 maybe_unlink (c_file
);
404 if (o_file
!= 0 && o_file
[0])
405 maybe_unlink (o_file
);
407 if (ldout
!= 0 && ldout
[0])
408 maybe_unlink (ldout
);
410 if (lderrout
!= 0 && lderrout
[0])
411 maybe_unlink (lderrout
);
413 #ifdef COLLECT_EXPORT_LIST
414 if (export_file
!= 0 && export_file
[0])
415 maybe_unlink (export_file
);
419 maybe_unlink (response_file
);
421 signal (signo
, SIG_DFL
);
427 file_exists (const char *name
)
429 return access (name
, R_OK
) == 0;
432 /* Parse a reasonable subset of shell quoting syntax. */
435 extract_string (const char **pp
)
448 obstack_1grow (&temporary_obstack
, c
);
449 else if (! inside
&& c
== ' ')
451 else if (! inside
&& c
== '\\')
456 obstack_1grow (&temporary_obstack
, c
);
459 obstack_1grow (&temporary_obstack
, '\0');
461 return XOBFINISH (&temporary_obstack
, char *);
465 dump_file (const char *name
, FILE *to
)
467 FILE *stream
= fopen (name
, "r");
474 while (c
= getc (stream
),
475 c
!= EOF
&& (ISIDNUM (c
) || c
== '$' || c
== '.'))
476 obstack_1grow (&temporary_obstack
, c
);
477 if (obstack_object_size (&temporary_obstack
) > 0)
479 const char *word
, *p
;
481 obstack_1grow (&temporary_obstack
, '\0');
482 word
= XOBFINISH (&temporary_obstack
, const char *);
485 ++word
, putc ('.', to
);
487 if (!strncmp (p
, USER_LABEL_PREFIX
, strlen (USER_LABEL_PREFIX
)))
488 p
+= strlen (USER_LABEL_PREFIX
);
490 #ifdef HAVE_LD_DEMANGLE
496 result
= cplus_demangle (p
, DMGL_PARAMS
| DMGL_ANSI
| DMGL_VERBOSE
);
504 diff
= strlen (word
) - strlen (result
);
505 while (diff
> 0 && c
== ' ')
506 --diff
, putc (' ', to
);
507 if (diff
< 0 && c
== ' ')
509 while (diff
< 0 && c
== ' ')
510 ++diff
, c
= getc (stream
);
513 /* Make sure we output at least one space, or
514 the demangled symbol name will run into
515 whatever text follows. */
526 obstack_free (&temporary_obstack
, temporary_firstobj
);
535 /* Return the kind of symbol denoted by name S. */
538 is_ctor_dtor (const char *s
)
540 struct names
{ const char *const name
; const int len
; const int ret
;
541 const int two_underscores
; };
543 const struct names
*p
;
545 const char *orig_s
= s
;
547 static const struct names special
[] = {
548 #ifndef NO_DOLLAR_IN_LABEL
549 { "GLOBAL__I$", sizeof ("GLOBAL__I$")-1, SYM_CTOR
, 0 },
550 { "GLOBAL__D$", sizeof ("GLOBAL__D$")-1, SYM_DTOR
, 0 },
552 #ifndef NO_DOT_IN_LABEL
553 { "GLOBAL__I.", sizeof ("GLOBAL__I.")-1, SYM_CTOR
, 0 },
554 { "GLOBAL__D.", sizeof ("GLOBAL__D.")-1, SYM_DTOR
, 0 },
555 #endif /* NO_DOT_IN_LABEL */
556 #endif /* NO_DOLLAR_IN_LABEL */
557 { "GLOBAL__I_", sizeof ("GLOBAL__I_")-1, SYM_CTOR
, 0 },
558 { "GLOBAL__D_", sizeof ("GLOBAL__D_")-1, SYM_DTOR
, 0 },
559 { "GLOBAL__F_", sizeof ("GLOBAL__F_")-1, SYM_DWEH
, 0 },
560 { "GLOBAL__FI_", sizeof ("GLOBAL__FI_")-1, SYM_INIT
, 0 },
561 { "GLOBAL__FD_", sizeof ("GLOBAL__FD_")-1, SYM_FINI
, 0 },
562 { NULL
, 0, SYM_REGULAR
, 0 }
565 while ((ch
= *s
) == '_')
571 for (p
= &special
[0]; p
->len
> 0; p
++)
574 && (!p
->two_underscores
|| ((s
- orig_s
) >= 2))
575 && strncmp(s
, p
->name
, p
->len
) == 0)
583 /* We maintain two prefix lists: one from COMPILER_PATH environment variable
584 and one from the PATH variable. */
586 static struct path_prefix cpath
, path
;
588 #ifdef CROSS_DIRECTORY_STRUCTURE
589 /* This is the name of the target machine. We use it to form the name
590 of the files to execute. */
592 static const char *const target_machine
= TARGET_MACHINE
;
595 /* Search for NAME using prefix list PPREFIX. We only look for executable
598 Return 0 if not found, otherwise return its name, allocated with malloc. */
601 find_a_file (struct path_prefix
*pprefix
, const char *name
)
604 struct prefix_list
*pl
;
605 int len
= pprefix
->max_len
+ strlen (name
) + 1;
608 fprintf (stderr
, "Looking for '%s'\n", name
);
610 #ifdef HOST_EXECUTABLE_SUFFIX
611 len
+= strlen (HOST_EXECUTABLE_SUFFIX
);
614 temp
= XNEWVEC (char, len
);
616 /* Determine the filename to execute (special case for absolute paths). */
618 if (IS_ABSOLUTE_PATH (name
))
620 if (access (name
, X_OK
) == 0)
625 fprintf (stderr
, " - found: absolute path\n");
630 #ifdef HOST_EXECUTABLE_SUFFIX
631 /* Some systems have a suffix for executable files.
632 So try appending that. */
634 strcat (temp
, HOST_EXECUTABLE_SUFFIX
);
636 if (access (temp
, X_OK
) == 0)
641 fprintf (stderr
, " - failed to locate using absolute path\n");
644 for (pl
= pprefix
->plist
; pl
; pl
= pl
->next
)
648 strcpy (temp
, pl
->prefix
);
651 if (stat (temp
, &st
) >= 0
652 && ! S_ISDIR (st
.st_mode
)
653 && access (temp
, X_OK
) == 0)
656 #ifdef HOST_EXECUTABLE_SUFFIX
657 /* Some systems have a suffix for executable files.
658 So try appending that. */
659 strcat (temp
, HOST_EXECUTABLE_SUFFIX
);
661 if (stat (temp
, &st
) >= 0
662 && ! S_ISDIR (st
.st_mode
)
663 && access (temp
, X_OK
) == 0)
668 if (debug
&& pprefix
->plist
== NULL
)
669 fprintf (stderr
, " - failed: no entries in prefix list\n");
675 /* Add an entry for PREFIX to prefix list PPREFIX. */
678 add_prefix (struct path_prefix
*pprefix
, const char *prefix
)
680 struct prefix_list
*pl
, **prev
;
685 for (pl
= pprefix
->plist
; pl
->next
; pl
= pl
->next
)
690 prev
= &pprefix
->plist
;
692 /* Keep track of the longest prefix. */
694 len
= strlen (prefix
);
695 if (len
> pprefix
->max_len
)
696 pprefix
->max_len
= len
;
698 pl
= XNEW (struct prefix_list
);
699 pl
->prefix
= xstrdup (prefix
);
704 pl
->next
= (struct prefix_list
*) 0;
708 /* Take the value of the environment variable ENV, break it into a path, and
709 add of the entries to PPREFIX. */
712 prefix_from_env (const char *env
, struct path_prefix
*pprefix
)
715 GET_ENVIRONMENT (p
, env
);
718 prefix_from_string (p
, pprefix
);
722 prefix_from_string (const char *p
, struct path_prefix
*pprefix
)
724 const char *startp
, *endp
;
725 char *nstore
= XNEWVEC (char, strlen (p
) + 3);
728 fprintf (stderr
, "Convert string '%s' into prefixes, separator = '%c'\n", p
, PATH_SEPARATOR
);
733 if (*endp
== PATH_SEPARATOR
|| *endp
== 0)
735 strncpy (nstore
, startp
, endp
-startp
);
738 strcpy (nstore
, "./");
740 else if (! IS_DIR_SEPARATOR (endp
[-1]))
742 nstore
[endp
-startp
] = DIR_SEPARATOR
;
743 nstore
[endp
-startp
+1] = 0;
746 nstore
[endp
-startp
] = 0;
749 fprintf (stderr
, " - add prefix: %s\n", nstore
);
751 add_prefix (pprefix
, nstore
);
754 endp
= startp
= endp
+ 1;
765 main (int argc
, char **argv
)
767 static const char *const ld_suffix
= "ld";
768 static const char *const real_ld_suffix
= "real-ld";
769 static const char *const collect_ld_suffix
= "collect-ld";
770 static const char *const nm_suffix
= "nm";
771 static const char *const gnm_suffix
= "gnm";
773 static const char *const ldd_suffix
= LDD_SUFFIX
;
775 static const char *const strip_suffix
= "strip";
776 static const char *const gstrip_suffix
= "gstrip";
778 #ifdef CROSS_DIRECTORY_STRUCTURE
779 /* If we look for a program in the compiler directories, we just use
780 the short name, since these directories are already system-specific.
781 But it we look for a program in the system directories, we need to
782 qualify the program name with the target machine. */
784 const char *const full_ld_suffix
=
785 concat(target_machine
, "-", ld_suffix
, NULL
);
786 const char *const full_nm_suffix
=
787 concat (target_machine
, "-", nm_suffix
, NULL
);
788 const char *const full_gnm_suffix
=
789 concat (target_machine
, "-", gnm_suffix
, NULL
);
791 const char *const full_ldd_suffix
=
792 concat (target_machine
, "-", ldd_suffix
, NULL
);
794 const char *const full_strip_suffix
=
795 concat (target_machine
, "-", strip_suffix
, NULL
);
796 const char *const full_gstrip_suffix
=
797 concat (target_machine
, "-", gstrip_suffix
, NULL
);
799 const char *const full_ld_suffix
= ld_suffix
;
800 const char *const full_nm_suffix
= nm_suffix
;
801 const char *const full_gnm_suffix
= gnm_suffix
;
803 const char *const full_ldd_suffix
= ldd_suffix
;
805 const char *const full_strip_suffix
= strip_suffix
;
806 const char *const full_gstrip_suffix
= gstrip_suffix
;
807 #endif /* CROSS_DIRECTORY_STRUCTURE */
811 #ifdef COLLECT_EXPORT_LIST
814 const char *ld_file_name
;
829 expandargv (&argc
, &argv
);
830 if (argv
!= old_argv
)
831 at_file_supplied
= 1;
833 num_c_args
= argc
+ 9;
835 no_demangle
= !! getenv ("COLLECT_NO_DEMANGLE");
837 /* Suppress demangling by the real linker, which may be broken. */
838 putenv (xstrdup ("COLLECT_NO_DEMANGLE="));
840 #if defined (COLLECT2_HOST_INITIALIZATION)
841 /* Perform system dependent initialization, if necessary. */
842 COLLECT2_HOST_INITIALIZATION
;
846 /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
847 receive the signal. A different setting is inheritable */
848 signal (SIGCHLD
, SIG_DFL
);
851 /* Unlock the stdio streams. */
852 unlock_std_streams ();
856 /* Do not invoke xcalloc before this point, since locale needs to be
857 set first, in case a diagnostic is issued. */
859 ld1
= (const char **)(ld1_argv
= XCNEWVEC (char *, argc
+4));
860 ld2
= (const char **)(ld2_argv
= XCNEWVEC (char *, argc
+11));
861 object
= (const char **)(object_lst
= XCNEWVEC (char *, argc
));
867 /* Parse command line early for instances of -debug. This allows
868 the debug flag to be set before functions like find_a_file()
873 for (i
= 1; argv
[i
] != NULL
; i
++)
875 if (! strcmp (argv
[i
], "-debug"))
881 #ifndef DEFAULT_A_OUT_NAME
882 output_file
= "a.out";
884 output_file
= DEFAULT_A_OUT_NAME
;
887 obstack_begin (&temporary_obstack
, 0);
888 temporary_firstobj
= (char *) obstack_alloc (&temporary_obstack
, 0);
890 #ifndef HAVE_LD_DEMANGLE
891 current_demangling_style
= auto_demangling
;
893 p
= getenv ("COLLECT_GCC_OPTIONS");
896 const char *q
= extract_string (&p
);
897 if (*q
== '-' && (q
[1] == 'm' || q
[1] == 'f'))
900 obstack_free (&temporary_obstack
, temporary_firstobj
);
902 /* -fno-profile-arcs -fno-test-coverage -fno-branch-probabilities
903 -fno-exceptions -w */
906 c_ptr
= (const char **) (c_argv
= XCNEWVEC (char *, num_c_args
));
909 fatal ("no arguments");
912 if (signal (SIGQUIT
, SIG_IGN
) != SIG_IGN
)
913 signal (SIGQUIT
, handler
);
915 if (signal (SIGINT
, SIG_IGN
) != SIG_IGN
)
916 signal (SIGINT
, handler
);
918 if (signal (SIGALRM
, SIG_IGN
) != SIG_IGN
)
919 signal (SIGALRM
, handler
);
922 if (signal (SIGHUP
, SIG_IGN
) != SIG_IGN
)
923 signal (SIGHUP
, handler
);
925 if (signal (SIGSEGV
, SIG_IGN
) != SIG_IGN
)
926 signal (SIGSEGV
, handler
);
928 if (signal (SIGBUS
, SIG_IGN
) != SIG_IGN
)
929 signal (SIGBUS
, handler
);
932 /* Extract COMPILER_PATH and PATH into our prefix list. */
933 prefix_from_env ("COMPILER_PATH", &cpath
);
934 prefix_from_env ("PATH", &path
);
936 /* Try to discover a valid linker/nm/strip to use. */
938 /* Maybe we know the right file to use (if not cross). */
940 #ifdef DEFAULT_LINKER
941 if (access (DEFAULT_LINKER
, X_OK
) == 0)
942 ld_file_name
= DEFAULT_LINKER
;
943 if (ld_file_name
== 0)
945 #ifdef REAL_LD_FILE_NAME
946 ld_file_name
= find_a_file (&path
, REAL_LD_FILE_NAME
);
947 if (ld_file_name
== 0)
949 /* Search the (target-specific) compiler dirs for ld'. */
950 ld_file_name
= find_a_file (&cpath
, real_ld_suffix
);
951 /* Likewise for `collect-ld'. */
952 if (ld_file_name
== 0)
953 ld_file_name
= find_a_file (&cpath
, collect_ld_suffix
);
954 /* Search the compiler directories for `ld'. We have protection against
955 recursive calls in find_a_file. */
956 if (ld_file_name
== 0)
957 ld_file_name
= find_a_file (&cpath
, ld_suffix
);
958 /* Search the ordinary system bin directories
959 for `ld' (if native linking) or `TARGET-ld' (if cross). */
960 if (ld_file_name
== 0)
961 ld_file_name
= find_a_file (&path
, full_ld_suffix
);
963 #ifdef REAL_NM_FILE_NAME
964 nm_file_name
= find_a_file (&path
, REAL_NM_FILE_NAME
);
965 if (nm_file_name
== 0)
967 nm_file_name
= find_a_file (&cpath
, gnm_suffix
);
968 if (nm_file_name
== 0)
969 nm_file_name
= find_a_file (&path
, full_gnm_suffix
);
970 if (nm_file_name
== 0)
971 nm_file_name
= find_a_file (&cpath
, nm_suffix
);
972 if (nm_file_name
== 0)
973 nm_file_name
= find_a_file (&path
, full_nm_suffix
);
976 ldd_file_name
= find_a_file (&cpath
, ldd_suffix
);
977 if (ldd_file_name
== 0)
978 ldd_file_name
= find_a_file (&path
, full_ldd_suffix
);
981 #ifdef REAL_STRIP_FILE_NAME
982 strip_file_name
= find_a_file (&path
, REAL_STRIP_FILE_NAME
);
983 if (strip_file_name
== 0)
985 strip_file_name
= find_a_file (&cpath
, gstrip_suffix
);
986 if (strip_file_name
== 0)
987 strip_file_name
= find_a_file (&path
, full_gstrip_suffix
);
988 if (strip_file_name
== 0)
989 strip_file_name
= find_a_file (&cpath
, strip_suffix
);
990 if (strip_file_name
== 0)
991 strip_file_name
= find_a_file (&path
, full_strip_suffix
);
993 /* Determine the full path name of the C compiler to use. */
994 c_file_name
= getenv ("COLLECT_GCC");
995 if (c_file_name
== 0)
997 #ifdef CROSS_DIRECTORY_STRUCTURE
998 c_file_name
= concat (target_machine
, "-gcc", NULL
);
1000 c_file_name
= "gcc";
1004 p
= find_a_file (&cpath
, c_file_name
);
1006 /* Here it should be safe to use the system search path since we should have
1007 already qualified the name of the compiler when it is needed. */
1009 p
= find_a_file (&path
, c_file_name
);
1014 *ld1
++ = *ld2
++ = ld_file_name
;
1016 /* Make temp file names. */
1017 c_file
= make_temp_file (".c");
1018 o_file
= make_temp_file (".o");
1019 #ifdef COLLECT_EXPORT_LIST
1020 export_file
= make_temp_file (".x");
1022 ldout
= make_temp_file (".ld");
1023 lderrout
= make_temp_file (".le");
1024 *c_ptr
++ = c_file_name
;
1031 #ifdef COLLECT_EXPORT_LIST
1032 /* Generate a list of directories from LIBPATH. */
1033 prefix_from_env ("LIBPATH", &libpath_lib_dirs
);
1034 /* Add to this list also two standard directories where
1035 AIX loader always searches for libraries. */
1036 add_prefix (&libpath_lib_dirs
, "/lib");
1037 add_prefix (&libpath_lib_dirs
, "/usr/lib");
1040 /* Get any options that the upper GCC wants to pass to the sub-GCC.
1042 AIX support needs to know if -shared has been specified before
1043 parsing commandline arguments. */
1045 p
= getenv ("COLLECT_GCC_OPTIONS");
1048 const char *q
= extract_string (&p
);
1049 if (*q
== '-' && (q
[1] == 'm' || q
[1] == 'f'))
1050 *c_ptr
++ = xstrdup (q
);
1051 if (strcmp (q
, "-EL") == 0 || strcmp (q
, "-EB") == 0)
1052 *c_ptr
++ = xstrdup (q
);
1053 if (strcmp (q
, "-shared") == 0)
1055 if (*q
== '-' && q
[1] == 'B')
1057 *c_ptr
++ = xstrdup (q
);
1060 q
= extract_string (&p
);
1061 *c_ptr
++ = xstrdup (q
);
1065 obstack_free (&temporary_obstack
, temporary_firstobj
);
1066 *c_ptr
++ = "-fno-profile-arcs";
1067 *c_ptr
++ = "-fno-test-coverage";
1068 *c_ptr
++ = "-fno-branch-probabilities";
1069 *c_ptr
++ = "-fno-exceptions";
1072 /* !!! When GCC calls collect2,
1073 it does not know whether it is calling collect2 or ld.
1074 So collect2 cannot meaningfully understand any options
1075 except those ld understands.
1076 If you propose to make GCC pass some other option,
1077 just imagine what will happen if ld is really ld!!! */
1079 /* Parse arguments. Remember output file spec, pass the rest to ld. */
1080 /* After the first file, put in the c++ rt0. */
1083 #ifdef HAVE_LD_DEMANGLE
1084 if (!demangle_flag
&& !no_demangle
)
1085 demangle_flag
= "--demangle";
1087 *ld1
++ = *ld2
++ = demangle_flag
;
1089 while ((arg
= *++argv
) != (char *) 0)
1091 *ld1
++ = *ld2
++ = arg
;
1097 #ifdef COLLECT_EXPORT_LIST
1098 /* We want to disable automatic exports on AIX when user
1099 explicitly puts an export list in command line */
1101 if (arg
[2] == 'E' || strncmp (&arg
[2], "export", 6) == 0)
1103 else if (arg
[2] == '6' && arg
[3] == '4')
1105 else if (arg
[2] == 'r' && arg
[3] == 't' && arg
[4] == 'l')
1111 if (!strcmp (arg
, "-debug"))
1113 /* Already parsed. */
1117 if (!strcmp (arg
, "-dynamic-linker") && argv
[1])
1120 *ld1
++ = *ld2
++ = *argv
;
1127 /* place o_file BEFORE this argument! */
1133 #ifdef COLLECT_EXPORT_LIST
1135 /* Resolving full library name. */
1136 const char *s
= resolve_lib_name (arg
+2);
1138 /* Saving a full library name. */
1139 add_to_list (&libs
, s
);
1144 #ifdef COLLECT_EXPORT_LIST
1145 /* Saving directories where to search for libraries. */
1147 add_prefix (&cmdline_lib_dirs
, arg
+2);
1150 #if LINK_ELIMINATE_DUPLICATE_LDIRECTORIES
1152 if (is_in_args (arg
, (const char **) ld1_argv
, ld1
-1))
1155 #endif /* LINK_ELIMINATE_DUPLICATE_LDIRECTORIES */
1160 output_file
= *ld1
++ = *ld2
++ = *++argv
;
1162 #ifdef SWITCHES_NEED_SPACES
1163 && ! strchr (SWITCHES_NEED_SPACES
, arg
[1])
1167 output_file
= &arg
[2];
1176 if (arg
[2] == '\0' && do_collecting
)
1178 /* We must strip after the nm run, otherwise C++ linking
1179 will not work. Thus we strip in the second ld run, or
1180 else with strip if there is no second ld run. */
1192 if (strcmp (arg
, "--no-demangle") == 0)
1194 demangle_flag
= arg
;
1199 else if (strncmp (arg
, "--demangle", 10) == 0)
1201 demangle_flag
= arg
;
1203 #ifndef HAVE_LD_DEMANGLE
1206 enum demangling_styles style
1207 = cplus_demangle_name_to_style (arg
+11);
1208 if (style
== unknown_demangling
)
1209 error ("unknown demangling style '%s'", arg
+11);
1211 current_demangling_style
= style
;
1220 else if ((p
= strrchr (arg
, '.')) != (char *) 0
1221 && (strcmp (p
, ".o") == 0 || strcmp (p
, ".a") == 0
1222 || strcmp (p
, ".so") == 0 || strcmp (p
, ".lo") == 0
1223 || strcmp (p
, ".obj") == 0))
1232 /* place o_file BEFORE this argument! */
1238 if (p
[1] == 'o' || p
[1] == 'l')
1240 #ifdef COLLECT_EXPORT_LIST
1241 /* libraries can be specified directly, i.e. without -l flag. */
1244 /* Saving a full library name. */
1245 add_to_list (&libs
, arg
);
1251 #ifdef COLLECT_EXPORT_LIST
1252 /* This is added only for debugging purposes. */
1255 fprintf (stderr
, "List of libraries:\n");
1256 dump_list (stderr
, "\t", libs
.first
);
1259 /* The AIX linker will discard static constructors in object files if
1260 nothing else in the file is referenced, so look at them first. */
1262 const char **export_object_lst
= (const char **)object_lst
;
1264 while (export_object_lst
< object
)
1265 scan_prog_file (*export_object_lst
++, PASS_OBJ
);
1268 struct id
*list
= libs
.first
;
1270 for (; list
; list
= list
->next
)
1271 scan_prog_file (list
->name
, PASS_FIRST
);
1276 char *buf
= concat ("-bE:", export_file
, NULL
);
1281 exportf
= fopen (export_file
, "w");
1282 if (exportf
== (FILE *) 0)
1283 fatal_perror ("fopen %s", export_file
);
1284 write_aix_file (exportf
, exports
.first
);
1285 if (fclose (exportf
))
1286 fatal_perror ("fclose %s", export_file
);
1291 *c_ptr
= *ld1
= *object
= (char *) 0;
1295 notice ("collect2 version %s", version_string
);
1296 #ifdef TARGET_VERSION
1299 fprintf (stderr
, "\n");
1305 fprintf (stderr
, "ld_file_name = %s\n",
1306 (ld_file_name
? ld_file_name
: "not found"));
1307 fprintf (stderr
, "c_file_name = %s\n",
1308 (c_file_name
? c_file_name
: "not found"));
1309 fprintf (stderr
, "nm_file_name = %s\n",
1310 (nm_file_name
? nm_file_name
: "not found"));
1312 fprintf (stderr
, "ldd_file_name = %s\n",
1313 (ldd_file_name
? ldd_file_name
: "not found"));
1315 fprintf (stderr
, "strip_file_name = %s\n",
1316 (strip_file_name
? strip_file_name
: "not found"));
1317 fprintf (stderr
, "c_file = %s\n",
1318 (c_file
? c_file
: "not found"));
1319 fprintf (stderr
, "o_file = %s\n",
1320 (o_file
? o_file
: "not found"));
1322 ptr
= getenv ("COLLECT_GCC_OPTIONS");
1324 fprintf (stderr
, "COLLECT_GCC_OPTIONS = %s\n", ptr
);
1326 ptr
= getenv ("COLLECT_GCC");
1328 fprintf (stderr
, "COLLECT_GCC = %s\n", ptr
);
1330 ptr
= getenv ("COMPILER_PATH");
1332 fprintf (stderr
, "COMPILER_PATH = %s\n", ptr
);
1334 ptr
= getenv (LIBRARY_PATH_ENV
);
1336 fprintf (stderr
, "%-20s= %s\n", LIBRARY_PATH_ENV
, ptr
);
1338 fprintf (stderr
, "\n");
1341 /* Load the program, searching all libraries and attempting to provide
1342 undefined symbols from repository information. */
1344 /* On AIX we do this later. */
1345 #ifndef COLLECT_EXPORT_LIST
1346 do_tlink (ld1_argv
, object_lst
);
1349 /* If -r or they will be run via some other method, do not build the
1350 constructor or destructor list, just return now. */
1352 #ifndef COLLECT_EXPORT_LIST
1357 #ifdef COLLECT_EXPORT_LIST
1358 /* Do the link we avoided above if we are exiting. */
1359 do_tlink (ld1_argv
, object_lst
);
1361 /* But make sure we delete the export file we may have created. */
1362 if (export_file
!= 0 && export_file
[0])
1363 maybe_unlink (export_file
);
1365 maybe_unlink (c_file
);
1366 maybe_unlink (o_file
);
1370 /* Examine the namelist with nm and search it for static constructors
1371 and destructors to call.
1372 Write the constructor and destructor tables to a .s file and reload. */
1374 /* On AIX we already scanned for global constructors/destructors. */
1375 #ifndef COLLECT_EXPORT_LIST
1376 scan_prog_file (output_file
, PASS_FIRST
);
1379 #ifdef SCAN_LIBRARIES
1380 scan_libraries (output_file
);
1385 notice ("%d constructor(s) found\n", constructors
.number
);
1386 notice ("%d destructor(s) found\n", destructors
.number
);
1387 notice ("%d frame table(s) found\n", frame_tables
.number
);
1390 if (constructors
.number
== 0 && destructors
.number
== 0
1391 && frame_tables
.number
== 0
1392 #if defined (SCAN_LIBRARIES) || defined (COLLECT_EXPORT_LIST)
1393 /* If we will be running these functions ourselves, we want to emit
1394 stubs into the shared library so that we do not have to relink
1395 dependent programs when we add static objects. */
1400 #ifdef COLLECT_EXPORT_LIST
1401 /* Do tlink without additional code generation. */
1402 do_tlink (ld1_argv
, object_lst
);
1404 /* Strip now if it was requested on the command line. */
1407 char **real_strip_argv
= XCNEWVEC (char *, 3);
1408 const char ** strip_argv
= (const char **) real_strip_argv
;
1410 strip_argv
[0] = strip_file_name
;
1411 strip_argv
[1] = output_file
;
1412 strip_argv
[2] = (char *) 0;
1413 fork_execute ("strip", real_strip_argv
);
1416 #ifdef COLLECT_EXPORT_LIST
1417 maybe_unlink (export_file
);
1419 maybe_unlink (c_file
);
1420 maybe_unlink (o_file
);
1424 /* Sort ctor and dtor lists by priority. */
1425 sort_ids (&constructors
);
1426 sort_ids (&destructors
);
1428 maybe_unlink(output_file
);
1429 outf
= fopen (c_file
, "w");
1430 if (outf
== (FILE *) 0)
1431 fatal_perror ("fopen %s", c_file
);
1433 write_c_file (outf
, c_file
);
1436 fatal_perror ("fclose %s", c_file
);
1438 /* Tell the linker that we have initializer and finalizer functions. */
1439 #ifdef LD_INIT_SWITCH
1440 #ifdef COLLECT_EXPORT_LIST
1441 *ld2
++ = concat (LD_INIT_SWITCH
, ":", initname
, ":", fininame
, NULL
);
1443 *ld2
++ = LD_INIT_SWITCH
;
1445 *ld2
++ = LD_FINI_SWITCH
;
1450 #ifdef COLLECT_EXPORT_LIST
1453 /* If we did not add export flag to link arguments before, add it to
1454 second link phase now. No new exports should have been added. */
1455 if (! exports
.first
)
1456 *ld2
++ = concat ("-bE:", export_file
, NULL
);
1458 #ifndef LD_INIT_SWITCH
1459 add_to_list (&exports
, initname
);
1460 add_to_list (&exports
, fininame
);
1461 add_to_list (&exports
, "_GLOBAL__DI");
1462 add_to_list (&exports
, "_GLOBAL__DD");
1464 exportf
= fopen (export_file
, "w");
1465 if (exportf
== (FILE *) 0)
1466 fatal_perror ("fopen %s", export_file
);
1467 write_aix_file (exportf
, exports
.first
);
1468 if (fclose (exportf
))
1469 fatal_perror ("fclose %s", export_file
);
1473 /* End of arguments to second link phase. */
1478 fprintf (stderr
, "\n========== output_file = %s, c_file = %s\n",
1479 output_file
, c_file
);
1480 write_c_file (stderr
, "stderr");
1481 fprintf (stderr
, "========== end of c_file\n\n");
1482 #ifdef COLLECT_EXPORT_LIST
1483 fprintf (stderr
, "\n========== export_file = %s\n", export_file
);
1484 write_aix_file (stderr
, exports
.first
);
1485 fprintf (stderr
, "========== end of export_file\n\n");
1489 /* Assemble the constructor and destructor tables.
1490 Link the tables in with the rest of the program. */
1492 fork_execute ("gcc", c_argv
);
1493 #ifdef COLLECT_EXPORT_LIST
1494 /* On AIX we must call tlink because of possible templates resolution. */
1495 do_tlink (ld2_argv
, object_lst
);
1497 /* Otherwise, simply call ld because tlink is already done. */
1498 fork_execute ("ld", ld2_argv
);
1500 /* Let scan_prog_file do any final mods (OSF/rose needs this for
1501 constructors/destructors in shared libraries. */
1502 scan_prog_file (output_file
, PASS_SECOND
);
1505 maybe_unlink (c_file
);
1506 maybe_unlink (o_file
);
1508 #ifdef COLLECT_EXPORT_LIST
1509 maybe_unlink (export_file
);
1516 /* Wait for a process to finish, and exit if a nonzero status is found. */
1519 collect_wait (const char *prog
, struct pex_obj
*pex
)
1523 if (!pex_get_status (pex
, 1, &status
))
1524 fatal_perror ("can't get program status");
1529 if (WIFSIGNALED (status
))
1531 int sig
= WTERMSIG (status
);
1532 error ("%s terminated with signal %d [%s]%s",
1533 prog
, sig
, strsignal(sig
),
1534 WCOREDUMP(status
) ? ", core dumped" : "");
1535 collect_exit (FATAL_EXIT_CODE
);
1538 if (WIFEXITED (status
))
1539 return WEXITSTATUS (status
);
1545 do_wait (const char *prog
, struct pex_obj
*pex
)
1547 int ret
= collect_wait (prog
, pex
);
1550 error ("%s returned %d exit status", prog
, ret
);
1556 unlink (response_file
);
1557 response_file
= NULL
;
1562 /* Execute a program, and wait for the reply. */
1565 collect_execute (const char *prog
, char **argv
, const char *outname
,
1566 const char *errname
)
1568 struct pex_obj
*pex
;
1571 char *response_arg
= NULL
;
1572 char *response_argv
[3] ATTRIBUTE_UNUSED
;
1574 if (HAVE_GNU_LD
&& at_file_supplied
&& argv
[0] != NULL
)
1576 /* If using @file arguments, create a temporary file and put the
1577 contents of argv into it. Then change argv to an array corresponding
1578 to a single argument @FILE, where FILE is the temporary filename. */
1580 char **current_argv
= argv
+ 1;
1581 char *argv0
= argv
[0];
1585 /* Note: we assume argv contains at least one element; this is
1588 response_file
= make_temp_file ("");
1590 f
= fopen (response_file
, "w");
1593 fatal ("could not open response file %s", response_file
);
1595 status
= writeargv (current_argv
, f
);
1598 fatal ("could not write to response file %s", response_file
);
1600 status
= fclose (f
);
1603 fatal ("could not close response file %s", response_file
);
1605 response_arg
= concat ("@", response_file
, NULL
);
1606 response_argv
[0] = argv0
;
1607 response_argv
[1] = response_arg
;
1608 response_argv
[2] = NULL
;
1610 argv
= response_argv
;
1619 fprintf (stderr
, "%s", argv
[0]);
1621 notice ("[cannot find %s]", prog
);
1623 for (p_argv
= &argv
[1]; (str
= *p_argv
) != (char *) 0; p_argv
++)
1624 fprintf (stderr
, " %s", str
);
1626 fprintf (stderr
, "\n");
1632 /* If we cannot find a program we need, complain error. Do this here
1633 since we might not end up needing something that we could not find. */
1636 fatal ("cannot find '%s'", prog
);
1638 pex
= pex_init (0, "collect2", NULL
);
1640 fatal_perror ("pex_init failed");
1642 errmsg
= pex_run (pex
, PEX_LAST
| PEX_SEARCH
, argv
[0], argv
, outname
,
1649 fatal_perror (errmsg
);
1656 free (response_arg
);
1662 fork_execute (const char *prog
, char **argv
)
1664 struct pex_obj
*pex
;
1666 pex
= collect_execute (prog
, argv
, NULL
, NULL
);
1667 do_wait (prog
, pex
);
1670 /* Unlink a file unless we are debugging. */
1673 maybe_unlink (const char *file
)
1676 unlink_if_ordinary (file
);
1678 notice ("[Leaving %s]\n", file
);
1682 static long sequence_number
= 0;
1684 /* Add a name to a linked list. */
1687 add_to_list (struct head
*head_ptr
, const char *name
)
1690 = (struct id
*) xcalloc (sizeof (struct id
) + strlen (name
), 1);
1692 strcpy (newid
->name
, name
);
1694 if (head_ptr
->first
)
1695 head_ptr
->last
->next
= newid
;
1697 head_ptr
->first
= newid
;
1699 /* Check for duplicate symbols. */
1700 for (p
= head_ptr
->first
;
1701 strcmp (name
, p
->name
) != 0;
1706 head_ptr
->last
->next
= 0;
1711 newid
->sequence
= ++sequence_number
;
1712 head_ptr
->last
= newid
;
1716 /* Grab the init priority number from an init function name that
1717 looks like "_GLOBAL_.I.12345.foo". */
1720 extract_init_priority (const char *name
)
1724 while (name
[pos
] == '_')
1726 pos
+= 10; /* strlen ("GLOBAL__X_") */
1728 /* Extract init_p number from ctor/dtor name. */
1729 pri
= atoi (name
+ pos
);
1730 return pri
? pri
: DEFAULT_INIT_PRIORITY
;
1733 /* Insertion sort the ids from ctor/dtor list HEAD_PTR in descending order.
1734 ctors will be run from right to left, dtors from left to right. */
1737 sort_ids (struct head
*head_ptr
)
1739 /* id holds the current element to insert. id_next holds the next
1740 element to insert. id_ptr iterates through the already sorted elements
1741 looking for the place to insert id. */
1742 struct id
*id
, *id_next
, **id_ptr
;
1744 id
= head_ptr
->first
;
1746 /* We don't have any sorted elements yet. */
1747 head_ptr
->first
= NULL
;
1749 for (; id
; id
= id_next
)
1752 id
->sequence
= extract_init_priority (id
->name
);
1754 for (id_ptr
= &(head_ptr
->first
); ; id_ptr
= &((*id_ptr
)->next
))
1756 /* If the sequence numbers are the same, we put the id from the
1757 file later on the command line later in the list. */
1758 || id
->sequence
> (*id_ptr
)->sequence
1759 /* Hack: do lexical compare, too.
1760 || (id->sequence == (*id_ptr)->sequence
1761 && strcmp (id->name, (*id_ptr)->name) > 0) */
1770 /* Now set the sequence numbers properly so write_c_file works. */
1771 for (id
= head_ptr
->first
; id
; id
= id
->next
)
1772 id
->sequence
= ++sequence_number
;
1775 /* Write: `prefix', the names on list LIST, `suffix'. */
1778 write_list (FILE *stream
, const char *prefix
, struct id
*list
)
1782 fprintf (stream
, "%sx%d,\n", prefix
, list
->sequence
);
1787 #if LINK_ELIMINATE_DUPLICATE_LDIRECTORIES
1788 /* Given a STRING, return nonzero if it occurs in the list in range
1789 [ARGS_BEGIN,ARGS_END). */
1792 is_in_args (const char *string
, const char **args_begin
,
1793 const char **args_end
)
1795 const char **args_pointer
;
1796 for (args_pointer
= args_begin
; args_pointer
!= args_end
; ++args_pointer
)
1797 if (strcmp (string
, *args_pointer
) == 0)
1801 #endif /* LINK_ELIMINATE_DUPLICATE_LDIRECTORIES */
1803 #ifdef COLLECT_EXPORT_LIST
1804 /* This function is really used only on AIX, but may be useful. */
1807 is_in_list (const char *prefix
, struct id
*list
)
1811 if (!strcmp (prefix
, list
->name
)) return 1;
1817 #endif /* COLLECT_EXPORT_LIST */
1819 /* Added for debugging purpose. */
1820 #ifdef COLLECT_EXPORT_LIST
1822 dump_list (FILE *stream
, const char *prefix
, struct id
*list
)
1826 fprintf (stream
, "%s%s,\n", prefix
, list
->name
);
1834 dump_prefix_list (FILE *stream
, const char *prefix
, struct prefix_list
*list
)
1838 fprintf (stream
, "%s%s,\n", prefix
, list
->prefix
);
1845 write_list_with_asm (FILE *stream
, const char *prefix
, struct id
*list
)
1849 fprintf (stream
, "%sx%d __asm__ (\"%s\");\n",
1850 prefix
, list
->sequence
, list
->name
);
1855 /* Write out the constructor and destructor tables statically (for a shared
1856 object), along with the functions to execute them. */
1859 write_c_file_stat (FILE *stream
, const char *name ATTRIBUTE_UNUSED
)
1863 int frames
= (frame_tables
.number
> 0);
1865 /* Figure out name of output_file, stripping off .so version. */
1866 p
= strrchr (output_file
, '/');
1882 if (strncmp (q
, SHLIB_SUFFIX
, strlen (SHLIB_SUFFIX
)) == 0)
1884 q
+= strlen (SHLIB_SUFFIX
);
1891 /* q points to null at end of the string (or . of the .so version) */
1892 prefix
= XNEWVEC (char, q
- p
+ 1);
1893 strncpy (prefix
, p
, q
- p
);
1895 for (r
= prefix
; *r
; r
++)
1896 if (!ISALNUM ((unsigned char)*r
))
1899 notice ("\nwrite_c_file - output name is %s, prefix is %s\n",
1900 output_file
, prefix
);
1902 initname
= concat ("_GLOBAL__FI_", prefix
, NULL
);
1903 fininame
= concat ("_GLOBAL__FD_", prefix
, NULL
);
1907 /* Write the tables as C code. */
1909 fprintf (stream
, "static int count;\n");
1910 fprintf (stream
, "typedef void entry_pt();\n");
1911 write_list_with_asm (stream
, "extern entry_pt ", constructors
.first
);
1915 write_list_with_asm (stream
, "extern void *", frame_tables
.first
);
1917 fprintf (stream
, "\tstatic void *frame_table[] = {\n");
1918 write_list (stream
, "\t\t&", frame_tables
.first
);
1919 fprintf (stream
, "\t0\n};\n");
1921 /* This must match what's in frame.h. */
1922 fprintf (stream
, "struct object {\n");
1923 fprintf (stream
, " void *pc_begin;\n");
1924 fprintf (stream
, " void *pc_end;\n");
1925 fprintf (stream
, " void *fde_begin;\n");
1926 fprintf (stream
, " void *fde_array;\n");
1927 fprintf (stream
, " __SIZE_TYPE__ count;\n");
1928 fprintf (stream
, " struct object *next;\n");
1929 fprintf (stream
, "};\n");
1931 fprintf (stream
, "extern void __register_frame_info_table (void *, struct object *);\n");
1932 fprintf (stream
, "extern void *__deregister_frame_info (void *);\n");
1934 fprintf (stream
, "static void reg_frame () {\n");
1935 fprintf (stream
, "\tstatic struct object ob;\n");
1936 fprintf (stream
, "\t__register_frame_info_table (frame_table, &ob);\n");
1937 fprintf (stream
, "\t}\n");
1939 fprintf (stream
, "static void dereg_frame () {\n");
1940 fprintf (stream
, "\t__deregister_frame_info (frame_table);\n");
1941 fprintf (stream
, "\t}\n");
1944 fprintf (stream
, "void %s() {\n", initname
);
1945 if (constructors
.number
> 0 || frames
)
1947 fprintf (stream
, "\tstatic entry_pt *ctors[] = {\n");
1948 write_list (stream
, "\t\t", constructors
.first
);
1950 fprintf (stream
, "\treg_frame,\n");
1951 fprintf (stream
, "\t};\n");
1952 fprintf (stream
, "\tentry_pt **p;\n");
1953 fprintf (stream
, "\tif (count++ != 0) return;\n");
1954 fprintf (stream
, "\tp = ctors + %d;\n", constructors
.number
+ frames
);
1955 fprintf (stream
, "\twhile (p > ctors) (*--p)();\n");
1958 fprintf (stream
, "\t++count;\n");
1959 fprintf (stream
, "}\n");
1960 write_list_with_asm (stream
, "extern entry_pt ", destructors
.first
);
1961 fprintf (stream
, "void %s() {\n", fininame
);
1962 if (destructors
.number
> 0 || frames
)
1964 fprintf (stream
, "\tstatic entry_pt *dtors[] = {\n");
1965 write_list (stream
, "\t\t", destructors
.first
);
1967 fprintf (stream
, "\tdereg_frame,\n");
1968 fprintf (stream
, "\t};\n");
1969 fprintf (stream
, "\tentry_pt **p;\n");
1970 fprintf (stream
, "\tif (--count != 0) return;\n");
1971 fprintf (stream
, "\tp = dtors;\n");
1972 fprintf (stream
, "\twhile (p < dtors + %d) (*p++)();\n",
1973 destructors
.number
+ frames
);
1975 fprintf (stream
, "}\n");
1979 COLLECT_SHARED_INIT_FUNC(stream
, initname
);
1980 COLLECT_SHARED_FINI_FUNC(stream
, fininame
);
1984 /* Write the constructor/destructor tables. */
1986 #ifndef LD_INIT_SWITCH
1988 write_c_file_glob (FILE *stream
, const char *name ATTRIBUTE_UNUSED
)
1990 /* Write the tables as C code. */
1992 int frames
= (frame_tables
.number
> 0);
1994 fprintf (stream
, "typedef void entry_pt();\n\n");
1996 write_list_with_asm (stream
, "extern entry_pt ", constructors
.first
);
2000 write_list_with_asm (stream
, "extern void *", frame_tables
.first
);
2002 fprintf (stream
, "\tstatic void *frame_table[] = {\n");
2003 write_list (stream
, "\t\t&", frame_tables
.first
);
2004 fprintf (stream
, "\t0\n};\n");
2006 /* This must match what's in frame.h. */
2007 fprintf (stream
, "struct object {\n");
2008 fprintf (stream
, " void *pc_begin;\n");
2009 fprintf (stream
, " void *pc_end;\n");
2010 fprintf (stream
, " void *fde_begin;\n");
2011 fprintf (stream
, " void *fde_array;\n");
2012 fprintf (stream
, " __SIZE_TYPE__ count;\n");
2013 fprintf (stream
, " struct object *next;\n");
2014 fprintf (stream
, "};\n");
2016 fprintf (stream
, "extern void __register_frame_info_table (void *, struct object *);\n");
2017 fprintf (stream
, "extern void *__deregister_frame_info (void *);\n");
2019 fprintf (stream
, "static void reg_frame () {\n");
2020 fprintf (stream
, "\tstatic struct object ob;\n");
2021 fprintf (stream
, "\t__register_frame_info_table (frame_table, &ob);\n");
2022 fprintf (stream
, "\t}\n");
2024 fprintf (stream
, "static void dereg_frame () {\n");
2025 fprintf (stream
, "\t__deregister_frame_info (frame_table);\n");
2026 fprintf (stream
, "\t}\n");
2029 fprintf (stream
, "\nentry_pt * __CTOR_LIST__[] = {\n");
2030 fprintf (stream
, "\t(entry_pt *) %d,\n", constructors
.number
+ frames
);
2031 write_list (stream
, "\t", constructors
.first
);
2033 fprintf (stream
, "\treg_frame,\n");
2034 fprintf (stream
, "\t0\n};\n\n");
2036 write_list_with_asm (stream
, "extern entry_pt ", destructors
.first
);
2038 fprintf (stream
, "\nentry_pt * __DTOR_LIST__[] = {\n");
2039 fprintf (stream
, "\t(entry_pt *) %d,\n", destructors
.number
+ frames
);
2040 write_list (stream
, "\t", destructors
.first
);
2042 fprintf (stream
, "\tdereg_frame,\n");
2043 fprintf (stream
, "\t0\n};\n\n");
2045 fprintf (stream
, "extern entry_pt %s;\n", NAME__MAIN
);
2046 fprintf (stream
, "entry_pt *__main_reference = %s;\n\n", NAME__MAIN
);
2048 #endif /* ! LD_INIT_SWITCH */
2051 write_c_file (FILE *stream
, const char *name
)
2053 #ifndef LD_INIT_SWITCH
2055 write_c_file_glob (stream
, name
);
2058 write_c_file_stat (stream
, name
);
2061 #ifdef COLLECT_EXPORT_LIST
2063 write_aix_file (FILE *stream
, struct id
*list
)
2065 for (; list
; list
= list
->next
)
2067 fputs (list
->name
, stream
);
2068 putc ('\n', stream
);
2073 #ifdef OBJECT_FORMAT_NONE
2075 /* Generic version to scan the name list of the loaded program for
2076 the symbols g++ uses for static constructors and destructors.
2078 The constructor table begins at __CTOR_LIST__ and contains a count
2079 of the number of pointers (or -1 if the constructors are built in a
2080 separate section by the linker), followed by the pointers to the
2081 constructor functions, terminated with a null pointer. The
2082 destructor table has the same format, and begins at __DTOR_LIST__. */
2085 scan_prog_file (const char *prog_name
, enum pass which_pass
)
2087 void (*int_handler
) (int);
2089 void (*quit_handler
) (int);
2091 char *real_nm_argv
[4];
2092 const char **nm_argv
= (const char **) real_nm_argv
;
2094 struct pex_obj
*pex
;
2100 if (which_pass
== PASS_SECOND
)
2103 /* If we do not have an `nm', complain. */
2104 if (nm_file_name
== 0)
2105 fatal ("cannot find 'nm'");
2107 nm_argv
[argc
++] = nm_file_name
;
2108 if (NM_FLAGS
[0] != '\0')
2109 nm_argv
[argc
++] = NM_FLAGS
;
2111 nm_argv
[argc
++] = prog_name
;
2112 nm_argv
[argc
++] = (char *) 0;
2114 /* Trace if needed. */
2117 const char **p_argv
;
2120 for (p_argv
= &nm_argv
[0]; (str
= *p_argv
) != (char *) 0; p_argv
++)
2121 fprintf (stderr
, " %s", str
);
2123 fprintf (stderr
, "\n");
2129 pex
= pex_init (PEX_USE_PIPES
, "collect2", NULL
);
2131 fatal_perror ("pex_init failed");
2133 errmsg
= pex_run (pex
, 0, nm_file_name
, real_nm_argv
, NULL
, NULL
, &err
);
2139 fatal_perror (errmsg
);
2145 int_handler
= (void (*) (int)) signal (SIGINT
, SIG_IGN
);
2147 quit_handler
= (void (*) (int)) signal (SIGQUIT
, SIG_IGN
);
2150 inf
= pex_read_output (pex
, 0);
2152 fatal_perror ("can't open nm output");
2155 fprintf (stderr
, "\nnm output with constructors/destructors.\n");
2157 /* Read each line of nm output. */
2158 while (fgets (buf
, sizeof buf
, inf
) != (char *) 0)
2163 /* If it contains a constructor or destructor name, add the name
2164 to the appropriate list. */
2166 for (p
= buf
; (ch
= *p
) != '\0' && ch
!= '\n' && ch
!= '_'; p
++)
2167 if (ch
== ' ' && p
[1] == 'U' && p
[2] == ' ')
2174 /* Find the end of the symbol name.
2175 Do not include `|', because Encore nm can tack that on the end. */
2176 for (end
= p
; (ch2
= *end
) != '\0' && !ISSPACE (ch2
) && ch2
!= '|';
2182 switch (is_ctor_dtor (name
))
2185 if (which_pass
!= PASS_LIB
)
2186 add_to_list (&constructors
, name
);
2190 if (which_pass
!= PASS_LIB
)
2191 add_to_list (&destructors
, name
);
2195 if (which_pass
!= PASS_LIB
)
2196 fatal ("init function found in object %s", prog_name
);
2197 #ifndef LD_INIT_SWITCH
2198 add_to_list (&constructors
, name
);
2203 if (which_pass
!= PASS_LIB
)
2204 fatal ("fini function found in object %s", prog_name
);
2205 #ifndef LD_FINI_SWITCH
2206 add_to_list (&destructors
, name
);
2211 if (which_pass
!= PASS_LIB
)
2212 add_to_list (&frame_tables
, name
);
2215 default: /* not a constructor or destructor */
2220 fprintf (stderr
, "\t%s\n", buf
);
2224 fprintf (stderr
, "\n");
2226 do_wait (nm_file_name
, pex
);
2228 signal (SIGINT
, int_handler
);
2230 signal (SIGQUIT
, quit_handler
);
2236 /* Use the List Dynamic Dependencies program to find shared libraries that
2237 the output file depends upon and their initialization/finalization
2238 routines, if any. */
2241 scan_libraries (const char *prog_name
)
2243 static struct head libraries
; /* list of shared libraries found */
2245 void (*int_handler
) (int);
2247 void (*quit_handler
) (int);
2249 char *real_ldd_argv
[4];
2250 const char **ldd_argv
= (const char **) real_ldd_argv
;
2252 struct pex_obj
*pex
;
2258 /* If we do not have an `ldd', complain. */
2259 if (ldd_file_name
== 0)
2261 error ("cannot find 'ldd'");
2265 ldd_argv
[argc
++] = ldd_file_name
;
2266 ldd_argv
[argc
++] = prog_name
;
2267 ldd_argv
[argc
++] = (char *) 0;
2269 /* Trace if needed. */
2272 const char **p_argv
;
2275 for (p_argv
= &ldd_argv
[0]; (str
= *p_argv
) != (char *) 0; p_argv
++)
2276 fprintf (stderr
, " %s", str
);
2278 fprintf (stderr
, "\n");
2284 pex
= pex_init (PEX_USE_PIPES
, "collect2", NULL
);
2286 fatal_perror ("pex_init failed");
2288 errmsg
= pex_run (pex
, 0, ldd_file_name
, real_ldd_argv
, NULL
, NULL
, &err
);
2294 fatal_perror (errmsg
);
2300 int_handler
= (void (*) (int)) signal (SIGINT
, SIG_IGN
);
2302 quit_handler
= (void (*) (int)) signal (SIGQUIT
, SIG_IGN
);
2305 inf
= pex_read_output (pex
, 0);
2307 fatal_perror ("can't open ldd output");
2310 notice ("\nldd output with constructors/destructors.\n");
2312 /* Read each line of ldd output. */
2313 while (fgets (buf
, sizeof buf
, inf
) != (char *) 0)
2316 char *name
, *end
, *p
= buf
;
2318 /* Extract names of libraries and add to list. */
2319 PARSE_LDD_OUTPUT (p
);
2324 if (strncmp (name
, "not found", sizeof ("not found") - 1) == 0)
2325 fatal ("dynamic dependency %s not found", buf
);
2327 /* Find the end of the symbol name. */
2329 (ch2
= *end
) != '\0' && ch2
!= '\n' && !ISSPACE (ch2
) && ch2
!= '|';
2334 if (access (name
, R_OK
) == 0)
2335 add_to_list (&libraries
, name
);
2337 fatal ("unable to open dynamic dependency '%s'", buf
);
2340 fprintf (stderr
, "\t%s\n", buf
);
2343 fprintf (stderr
, "\n");
2345 do_wait (ldd_file_name
, pex
);
2347 signal (SIGINT
, int_handler
);
2349 signal (SIGQUIT
, quit_handler
);
2352 /* Now iterate through the library list adding their symbols to
2354 for (list
= libraries
.first
; list
; list
= list
->next
)
2355 scan_prog_file (list
->name
, PASS_LIB
);
2358 #endif /* LDD_SUFFIX */
2360 #endif /* OBJECT_FORMAT_NONE */
2364 * COFF specific stuff.
2367 #ifdef OBJECT_FORMAT_COFF
2369 #if defined (EXTENDED_COFF)
2371 # define GCC_SYMBOLS(X) (SYMHEADER(X).isymMax + SYMHEADER(X).iextMax)
2372 # define GCC_SYMENT SYMR
2373 # define GCC_OK_SYMBOL(X) ((X).st == stProc || (X).st == stGlobal)
2374 # define GCC_SYMINC(X) (1)
2375 # define GCC_SYMZERO(X) (SYMHEADER(X).isymMax)
2376 # define GCC_CHECK_HDR(X) (PSYMTAB(X) != 0)
2380 # define GCC_SYMBOLS(X) (HEADER(ldptr).f_nsyms)
2381 # define GCC_SYMENT SYMENT
2382 # if defined (C_WEAKEXT)
2383 # define GCC_OK_SYMBOL(X) \
2384 (((X).n_sclass == C_EXT || (X).n_sclass == C_WEAKEXT) && \
2385 ((X).n_scnum > N_UNDEF) && \
2387 || (((X).n_type & N_TMASK) == (DT_NON << N_BTSHFT) \
2388 || ((X).n_type & N_TMASK) == (DT_FCN << N_BTSHFT))))
2389 # define GCC_UNDEF_SYMBOL(X) \
2390 (((X).n_sclass == C_EXT || (X).n_sclass == C_WEAKEXT) && \
2391 ((X).n_scnum == N_UNDEF))
2393 # define GCC_OK_SYMBOL(X) \
2394 (((X).n_sclass == C_EXT) && \
2395 ((X).n_scnum > N_UNDEF) && \
2397 || (((X).n_type & N_TMASK) == (DT_NON << N_BTSHFT) \
2398 || ((X).n_type & N_TMASK) == (DT_FCN << N_BTSHFT))))
2399 # define GCC_UNDEF_SYMBOL(X) \
2400 (((X).n_sclass == C_EXT) && ((X).n_scnum == N_UNDEF))
2402 # define GCC_SYMINC(X) ((X).n_numaux+1)
2403 # define GCC_SYMZERO(X) 0
2405 /* 0757 = U803XTOCMAGIC (AIX 4.3) and 0767 = U64_TOCMAGIC (AIX V5) */
2407 # define GCC_CHECK_HDR(X) \
2408 ((HEADER (X).f_magic == U802TOCMAGIC && ! aix64_flag) \
2409 || (HEADER (X).f_magic == 0767 && aix64_flag))
2411 # define GCC_CHECK_HDR(X) \
2412 ((HEADER (X).f_magic == U802TOCMAGIC && ! aix64_flag) \
2413 || (HEADER (X).f_magic == 0757 && aix64_flag))
2418 #ifdef COLLECT_EXPORT_LIST
2419 /* Array of standard AIX libraries which should not
2420 be scanned for ctors/dtors. */
2421 static const char *const aix_std_libs
[] = {
2429 "/usr/lib/libc_r.a",
2430 "/usr/lib/libm_r.a",
2431 "/usr/lib/threads/libc.a",
2432 "/usr/ccs/lib/libc.a",
2433 "/usr/ccs/lib/libm.a",
2434 "/usr/ccs/lib/libc_r.a",
2435 "/usr/ccs/lib/libm_r.a",
2439 /* This function checks the filename and returns 1
2440 if this name matches the location of a standard AIX library. */
2441 static int ignore_library (const char *);
2443 ignore_library (const char *name
)
2445 const char *const *p
= &aix_std_libs
[0];
2446 while (*p
++ != NULL
)
2447 if (! strcmp (name
, *p
)) return 1;
2450 #endif /* COLLECT_EXPORT_LIST */
2452 #if defined (HAVE_DECL_LDGETNAME) && !HAVE_DECL_LDGETNAME
2453 extern char *ldgetname (LDFILE
*, GCC_SYMENT
*);
2456 /* COFF version to scan the name list of the loaded program for
2457 the symbols g++ uses for static constructors and destructors.
2459 The constructor table begins at __CTOR_LIST__ and contains a count
2460 of the number of pointers (or -1 if the constructors are built in a
2461 separate section by the linker), followed by the pointers to the
2462 constructor functions, terminated with a null pointer. The
2463 destructor table has the same format, and begins at __DTOR_LIST__. */
2466 scan_prog_file (const char *prog_name
, enum pass which_pass
)
2468 LDFILE
*ldptr
= NULL
;
2469 int sym_index
, sym_count
;
2472 if (which_pass
!= PASS_FIRST
&& which_pass
!= PASS_OBJ
)
2475 #ifdef COLLECT_EXPORT_LIST
2476 /* We do not need scanning for some standard C libraries. */
2477 if (which_pass
== PASS_FIRST
&& ignore_library (prog_name
))
2480 /* On AIX we have a loop, because there is not much difference
2481 between an object and an archive. This trick allows us to
2482 eliminate scan_libraries() function. */
2486 /* Some platforms (e.g. OSF4) declare ldopen as taking a
2487 non-const char * filename parameter, even though it will not
2488 modify that string. So we must cast away const-ness here,
2489 using CONST_CAST to prevent complaints from -Wcast-qual. */
2490 if ((ldptr
= ldopen (CONST_CAST (char *, prog_name
), ldptr
)) != NULL
)
2492 if (! MY_ISCOFF (HEADER (ldptr
).f_magic
))
2493 fatal ("%s: not a COFF file", prog_name
);
2495 if (GCC_CHECK_HDR (ldptr
))
2497 sym_count
= GCC_SYMBOLS (ldptr
);
2498 sym_index
= GCC_SYMZERO (ldptr
);
2500 #ifdef COLLECT_EXPORT_LIST
2501 /* Is current archive member a shared object? */
2502 is_shared
= HEADER (ldptr
).f_flags
& F_SHROBJ
;
2505 while (sym_index
< sym_count
)
2509 if (ldtbread (ldptr
, sym_index
, &symbol
) <= 0)
2511 sym_index
+= GCC_SYMINC (symbol
);
2513 if (GCC_OK_SYMBOL (symbol
))
2517 if ((name
= ldgetname (ldptr
, &symbol
)) == NULL
)
2518 continue; /* Should never happen. */
2520 #ifdef XCOFF_DEBUGGING_INFO
2521 /* All AIX function names have a duplicate entry
2522 beginning with a dot. */
2527 switch (is_ctor_dtor (name
))
2531 add_to_list (&constructors
, name
);
2532 #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
2533 if (which_pass
== PASS_OBJ
)
2534 add_to_list (&exports
, name
);
2540 add_to_list (&destructors
, name
);
2541 #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
2542 if (which_pass
== PASS_OBJ
)
2543 add_to_list (&exports
, name
);
2547 #ifdef COLLECT_EXPORT_LIST
2549 #ifndef LD_INIT_SWITCH
2551 add_to_list (&constructors
, name
);
2556 #ifndef LD_INIT_SWITCH
2558 add_to_list (&destructors
, name
);
2565 add_to_list (&frame_tables
, name
);
2566 #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
2567 if (which_pass
== PASS_OBJ
)
2568 add_to_list (&exports
, name
);
2572 default: /* not a constructor or destructor */
2573 #ifdef COLLECT_EXPORT_LIST
2574 /* Explicitly export all global symbols when
2575 building a shared object on AIX, but do not
2576 re-export symbols from another shared object
2577 and do not export symbols if the user
2578 provides an explicit export list. */
2579 if (shared_obj
&& !is_shared
2580 && which_pass
== PASS_OBJ
&& !export_flag
)
2581 add_to_list (&exports
, name
);
2587 #if !defined(EXTENDED_COFF)
2588 fprintf (stderr
, "\tsec=%d class=%d type=%s%o %s\n",
2589 symbol
.n_scnum
, symbol
.n_sclass
,
2590 (symbol
.n_type
? "0" : ""), symbol
.n_type
,
2594 "\tiss = %5d, value = %5ld, index = %5d, name = %s\n",
2595 symbol
.iss
, (long) symbol
.value
, symbol
.index
, name
);
2600 #ifdef COLLECT_EXPORT_LIST
2603 /* If archive contains both 32-bit and 64-bit objects,
2604 we want to skip objects in other mode so mismatch normal. */
2606 fprintf (stderr
, "%s : magic=%o aix64=%d mismatch\n",
2607 prog_name
, HEADER (ldptr
).f_magic
, aix64_flag
);
2613 fatal ("%s: cannot open as COFF file", prog_name
);
2615 #ifdef COLLECT_EXPORT_LIST
2616 /* On AIX loop continues while there are more members in archive. */
2618 while (ldclose (ldptr
) == FAILURE
);
2620 /* Otherwise we simply close ldptr. */
2621 (void) ldclose(ldptr
);
2624 #endif /* OBJECT_FORMAT_COFF */
2626 #ifdef COLLECT_EXPORT_LIST
2627 /* Given a library name without "lib" prefix, this function
2628 returns a full library name including a path. */
2630 resolve_lib_name (const char *name
)
2634 /* Library extensions for AIX dynamic linking. */
2635 const char * const libexts
[2] = {"a", "so"};
2637 for (i
= 0; libpaths
[i
]; i
++)
2638 if (libpaths
[i
]->max_len
> l
)
2639 l
= libpaths
[i
]->max_len
;
2641 lib_buf
= XNEWVEC (char, l
+ strlen(name
) + 10);
2643 for (i
= 0; libpaths
[i
]; i
++)
2645 struct prefix_list
*list
= libpaths
[i
]->plist
;
2646 for (; list
; list
= list
->next
)
2648 /* The following lines are needed because path_prefix list
2649 may contain directories both with trailing '/' and
2652 if (list
->prefix
[strlen(list
->prefix
)-1] != '/')
2654 for (j
= 0; j
< 2; j
++)
2656 sprintf (lib_buf
, "%s%slib%s.%s",
2657 list
->prefix
, p
, name
,
2658 libexts
[(j
+ aixrtl_flag
) % 2]);
2659 if (debug
) fprintf (stderr
, "searching for: %s\n", lib_buf
);
2660 if (file_exists (lib_buf
))
2662 if (debug
) fprintf (stderr
, "found: %s\n", lib_buf
);
2669 fprintf (stderr
, "not found\n");
2671 fatal ("library lib%s not found", name
);
2674 #endif /* COLLECT_EXPORT_LIST */