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, 2008, 2009
5 Free Software Foundation, Inc.
6 Contributed by Chris Smith (csmith@convex.com).
7 Heavily modified by Michael Meissner (meissner@cygnus.com),
8 Per Bothner (bothner@cygnus.com), and John Gilmore (gnu@cygnus.com).
10 This file is part of GCC.
12 GCC is free software; you can redistribute it and/or modify it under
13 the terms of the GNU General Public License as published by the Free
14 Software Foundation; either version 3, or (at your option) any later
17 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
18 WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 You should have received a copy of the GNU General Public License
23 along with GCC; see the file COPYING3. If not see
24 <http://www.gnu.org/licenses/>. */
27 /* Build tables of static constructors and destructors and run ld. */
31 #include "coretypes.h"
34 #if ! defined( SIGCHLD ) && defined( SIGCLD )
35 # define SIGCHLD SIGCLD
38 #ifndef LIBRARY_PATH_ENV
39 #define LIBRARY_PATH_ENV "LIBRARY_PATH"
50 /* On certain systems, we have code that works by scanning the object file
51 directly. But this code uses system-specific header files and library
52 functions, so turn it off in a cross-compiler. Likewise, the names of
53 the utilities are not correct for a cross-compiler; we have to hope that
54 cross-versions are in the proper directories. */
56 #ifdef CROSS_DIRECTORY_STRUCTURE
57 #undef OBJECT_FORMAT_COFF
59 #undef REAL_LD_FILE_NAME
60 #undef REAL_NM_FILE_NAME
61 #undef REAL_STRIP_FILE_NAME
64 /* If we cannot use a special method, use the ordinary one:
65 run nm to find what symbols are present.
66 In a cross-compiler, this means you need a cross nm,
67 but that is not quite as unpleasant as special headers. */
69 #if !defined (OBJECT_FORMAT_COFF)
70 #define OBJECT_FORMAT_NONE
73 #ifdef OBJECT_FORMAT_COFF
82 /* Many versions of ldfcn.h define these. */
90 /* Some systems have an ISCOFF macro, but others do not. In some cases
91 the macro may be wrong. MY_ISCOFF is defined in tm.h files for machines
92 that either do not have an ISCOFF macro in /usr/include or for those
96 #define MY_ISCOFF(X) ISCOFF (X)
99 #endif /* OBJECT_FORMAT_COFF */
101 #ifdef OBJECT_FORMAT_NONE
103 /* Default flags to pass to nm. */
105 #define NM_FLAGS "-n"
108 #endif /* OBJECT_FORMAT_NONE */
110 /* Some systems use __main in a way incompatible with its use in gcc, in these
111 cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
112 give the same symbol without quotes for an alternative entry point. */
114 #define NAME__MAIN "__main"
117 /* This must match tree.h. */
118 #define DEFAULT_INIT_PRIORITY 65535
120 #ifndef COLLECT_SHARED_INIT_FUNC
121 #define COLLECT_SHARED_INIT_FUNC(STREAM, FUNC) \
122 fprintf ((STREAM), "void _GLOBAL__DI() {\n\t%s();\n}\n", (FUNC))
124 #ifndef COLLECT_SHARED_FINI_FUNC
125 #define COLLECT_SHARED_FINI_FUNC(STREAM, FUNC) \
126 fprintf ((STREAM), "void _GLOBAL__DD() {\n\t%s();\n}\n", (FUNC))
130 #define SCAN_LIBRARIES
134 #define SHLIB_SUFFIX ".so"
138 int do_collecting
= 1;
140 int do_collecting
= 0;
143 /* Nonzero if we should suppress the automatic demangling of identifiers
144 in linker error messages. Set from COLLECT_NO_DEMANGLE. */
147 /* Linked lists of constructor and destructor names. */
163 /* Enumeration giving which pass this is for scanning the program file. */
166 PASS_FIRST
, /* without constructors */
167 PASS_OBJ
, /* individual objects */
168 PASS_LIB
, /* looking for shared libraries */
169 PASS_SECOND
/* with constructors linked in */
172 int vflag
; /* true if -v */
173 static int rflag
; /* true if -r */
174 static int strip_flag
; /* true if -s */
175 static const char *demangle_flag
;
176 #ifdef COLLECT_EXPORT_LIST
177 static int export_flag
; /* true if -bE */
178 static int aix64_flag
; /* true if -b64 */
179 static int aixrtl_flag
; /* true if -brtl */
182 int debug
; /* true if -debug */
184 static int shared_obj
; /* true if -shared */
186 static const char *c_file
; /* <xxx>.c for constructor/destructor list. */
187 static const char *o_file
; /* <xxx>.o for constructor/destructor list. */
188 #ifdef COLLECT_EXPORT_LIST
189 static const char *export_file
; /* <xxx>.x for AIX export list. */
191 const char *ldout
; /* File for ld stdout. */
192 const char *lderrout
; /* File for ld stderr. */
193 static const char *output_file
; /* Output file for ld. */
194 static const char *nm_file_name
; /* pathname of nm */
196 static const char *ldd_file_name
; /* pathname of ldd (or equivalent) */
198 static const char *strip_file_name
; /* pathname of strip */
199 const char *c_file_name
; /* pathname of gcc */
200 static char *initname
, *fininame
; /* names of init and fini funcs */
202 static struct head constructors
; /* list of constructors found */
203 static struct head destructors
; /* list of destructors found */
204 #ifdef COLLECT_EXPORT_LIST
205 static struct head exports
; /* list of exported symbols */
207 static struct head frame_tables
; /* list of frame unwind info tables */
209 static bool at_file_supplied
; /* Whether to use @file arguments */
210 static char *response_file
; /* Name of any current response file */
212 struct obstack temporary_obstack
;
213 char * temporary_firstobj
;
215 /* Structure to hold all the directories in which to search for files to
220 const char *prefix
; /* String to prepend to the path. */
221 struct prefix_list
*next
; /* Next in linked list. */
226 struct prefix_list
*plist
; /* List of prefixes to try */
227 int max_len
; /* Max length of a prefix in PLIST */
228 const char *name
; /* Name of this list (used in config stuff) */
231 #ifdef COLLECT_EXPORT_LIST
232 /* Lists to keep libraries to be scanned for global constructors/destructors. */
233 static struct head libs
; /* list of libraries */
234 static struct path_prefix cmdline_lib_dirs
; /* directories specified with -L */
235 static struct path_prefix libpath_lib_dirs
; /* directories in LIBPATH */
236 static struct path_prefix
*libpaths
[3] = {&cmdline_lib_dirs
,
237 &libpath_lib_dirs
, NULL
};
240 /* Special kinds of symbols that a name may denote. */
243 SYM_REGULAR
= 0, /* nothing special */
245 SYM_CTOR
= 1, /* constructor */
246 SYM_DTOR
= 2, /* destructor */
247 SYM_INIT
= 3, /* shared object routine that calls all the ctors */
248 SYM_FINI
= 4, /* shared object routine that calls all the dtors */
249 SYM_DWEH
= 5 /* DWARF exception handling table */
252 static symkind
is_ctor_dtor (const char *);
254 static void handler (int);
255 static char *find_a_file (struct path_prefix
*, const char *);
256 static void add_prefix (struct path_prefix
*, const char *);
257 static void prefix_from_env (const char *, struct path_prefix
*);
258 static void prefix_from_string (const char *, struct path_prefix
*);
259 static void do_wait (const char *, struct pex_obj
*);
260 static void fork_execute (const char *, char **);
261 static void maybe_unlink (const char *);
262 static void add_to_list (struct head
*, const char *);
263 static int extract_init_priority (const char *);
264 static void sort_ids (struct head
*);
265 static void write_list (FILE *, const char *, struct id
*);
266 #ifdef COLLECT_EXPORT_LIST
267 static void dump_list (FILE *, const char *, struct id
*);
270 static void dump_prefix_list (FILE *, const char *, struct prefix_list
*);
272 static void write_list_with_asm (FILE *, const char *, struct id
*);
273 static void write_c_file (FILE *, const char *);
274 static void write_c_file_stat (FILE *, const char *);
275 #ifndef LD_INIT_SWITCH
276 static void write_c_file_glob (FILE *, const char *);
278 static void scan_prog_file (const char *, enum pass
);
279 #ifdef SCAN_LIBRARIES
280 static void scan_libraries (const char *);
282 #if LINK_ELIMINATE_DUPLICATE_LDIRECTORIES
283 static int is_in_args (const char *, const char **, const char **);
285 #ifdef COLLECT_EXPORT_LIST
287 static int is_in_list (const char *, struct id
*);
289 static void write_aix_file (FILE *, struct id
*);
290 static char *resolve_lib_name (const char *);
292 static char *extract_string (const char **);
294 /* Delete tempfiles and exit function. */
297 collect_exit (int status
)
299 if (c_file
!= 0 && c_file
[0])
300 maybe_unlink (c_file
);
302 if (o_file
!= 0 && o_file
[0])
303 maybe_unlink (o_file
);
305 #ifdef COLLECT_EXPORT_LIST
306 if (export_file
!= 0 && export_file
[0])
307 maybe_unlink (export_file
);
310 if (ldout
!= 0 && ldout
[0])
312 dump_file (ldout
, stdout
);
313 maybe_unlink (ldout
);
316 if (lderrout
!= 0 && lderrout
[0])
318 dump_file (lderrout
, stderr
);
319 maybe_unlink (lderrout
);
322 if (status
!= 0 && output_file
!= 0 && output_file
[0])
323 maybe_unlink (output_file
);
326 maybe_unlink (response_file
);
332 /* Notify user of a non-error. */
334 notice (const char *cmsgid
, ...)
338 va_start (ap
, cmsgid
);
339 vfprintf (stderr
, _(cmsgid
), ap
);
343 /* Die when sys call fails. */
346 fatal_perror (const char * cmsgid
, ...)
351 va_start (ap
, cmsgid
);
352 fprintf (stderr
, "collect2: ");
353 vfprintf (stderr
, _(cmsgid
), ap
);
354 fprintf (stderr
, ": %s\n", xstrerror (e
));
357 collect_exit (FATAL_EXIT_CODE
);
363 fatal (const char * cmsgid
, ...)
367 va_start (ap
, cmsgid
);
368 fprintf (stderr
, "collect2: ");
369 vfprintf (stderr
, _(cmsgid
), ap
);
370 fprintf (stderr
, "\n");
373 collect_exit (FATAL_EXIT_CODE
);
376 /* Write error message. */
379 error (const char * gmsgid
, ...)
383 va_start (ap
, gmsgid
);
384 fprintf (stderr
, "collect2: ");
385 vfprintf (stderr
, _(gmsgid
), ap
);
386 fprintf (stderr
, "\n");
390 /* In case obstack is linked in, and abort is defined to fancy_abort,
391 provide a default entry. */
394 fancy_abort (const char *file
, int line
, const char *func
)
396 fatal ("internal gcc abort in %s, at %s:%d", func
, file
, line
);
402 if (c_file
!= 0 && c_file
[0])
403 maybe_unlink (c_file
);
405 if (o_file
!= 0 && o_file
[0])
406 maybe_unlink (o_file
);
408 if (ldout
!= 0 && ldout
[0])
409 maybe_unlink (ldout
);
411 if (lderrout
!= 0 && lderrout
[0])
412 maybe_unlink (lderrout
);
414 #ifdef COLLECT_EXPORT_LIST
415 if (export_file
!= 0 && export_file
[0])
416 maybe_unlink (export_file
);
420 maybe_unlink (response_file
);
422 signal (signo
, SIG_DFL
);
428 file_exists (const char *name
)
430 return access (name
, R_OK
) == 0;
433 /* Parse a reasonable subset of shell quoting syntax. */
436 extract_string (const char **pp
)
449 obstack_1grow (&temporary_obstack
, c
);
450 else if (! inside
&& c
== ' ')
452 else if (! inside
&& c
== '\\')
457 obstack_1grow (&temporary_obstack
, c
);
460 obstack_1grow (&temporary_obstack
, '\0');
462 return XOBFINISH (&temporary_obstack
, char *);
466 dump_file (const char *name
, FILE *to
)
468 FILE *stream
= fopen (name
, "r");
475 while (c
= getc (stream
),
476 c
!= EOF
&& (ISIDNUM (c
) || c
== '$' || c
== '.'))
477 obstack_1grow (&temporary_obstack
, c
);
478 if (obstack_object_size (&temporary_obstack
) > 0)
480 const char *word
, *p
;
482 obstack_1grow (&temporary_obstack
, '\0');
483 word
= XOBFINISH (&temporary_obstack
, const char *);
486 ++word
, putc ('.', to
);
488 if (!strncmp (p
, USER_LABEL_PREFIX
, strlen (USER_LABEL_PREFIX
)))
489 p
+= strlen (USER_LABEL_PREFIX
);
491 #ifdef HAVE_LD_DEMANGLE
497 result
= cplus_demangle (p
, DMGL_PARAMS
| DMGL_ANSI
| DMGL_VERBOSE
);
505 diff
= strlen (word
) - strlen (result
);
506 while (diff
> 0 && c
== ' ')
507 --diff
, putc (' ', to
);
508 if (diff
< 0 && c
== ' ')
510 while (diff
< 0 && c
== ' ')
511 ++diff
, c
= getc (stream
);
514 /* Make sure we output at least one space, or
515 the demangled symbol name will run into
516 whatever text follows. */
527 obstack_free (&temporary_obstack
, temporary_firstobj
);
536 /* Return the kind of symbol denoted by name S. */
539 is_ctor_dtor (const char *s
)
541 struct names
{ const char *const name
; const int len
; symkind ret
;
542 const int two_underscores
; };
544 const struct names
*p
;
546 const char *orig_s
= s
;
548 static const struct names special
[] = {
549 #ifndef NO_DOLLAR_IN_LABEL
550 { "GLOBAL__I$", sizeof ("GLOBAL__I$")-1, SYM_CTOR
, 0 },
551 { "GLOBAL__D$", sizeof ("GLOBAL__D$")-1, SYM_DTOR
, 0 },
553 #ifndef NO_DOT_IN_LABEL
554 { "GLOBAL__I.", sizeof ("GLOBAL__I.")-1, SYM_CTOR
, 0 },
555 { "GLOBAL__D.", sizeof ("GLOBAL__D.")-1, SYM_DTOR
, 0 },
556 #endif /* NO_DOT_IN_LABEL */
557 #endif /* NO_DOLLAR_IN_LABEL */
558 { "GLOBAL__I_", sizeof ("GLOBAL__I_")-1, SYM_CTOR
, 0 },
559 { "GLOBAL__D_", sizeof ("GLOBAL__D_")-1, SYM_DTOR
, 0 },
560 { "GLOBAL__F_", sizeof ("GLOBAL__F_")-1, SYM_DWEH
, 0 },
561 { "GLOBAL__FI_", sizeof ("GLOBAL__FI_")-1, SYM_INIT
, 0 },
562 { "GLOBAL__FD_", sizeof ("GLOBAL__FD_")-1, SYM_FINI
, 0 },
563 { NULL
, 0, SYM_REGULAR
, 0 }
566 while ((ch
= *s
) == '_')
572 for (p
= &special
[0]; p
->len
> 0; p
++)
575 && (!p
->two_underscores
|| ((s
- orig_s
) >= 2))
576 && strncmp(s
, p
->name
, p
->len
) == 0)
584 /* We maintain two prefix lists: one from COMPILER_PATH environment variable
585 and one from the PATH variable. */
587 static struct path_prefix cpath
, path
;
589 #ifdef CROSS_DIRECTORY_STRUCTURE
590 /* This is the name of the target machine. We use it to form the name
591 of the files to execute. */
593 static const char *const target_machine
= TARGET_MACHINE
;
596 /* Search for NAME using prefix list PPREFIX. We only look for executable
599 Return 0 if not found, otherwise return its name, allocated with malloc. */
602 find_a_file (struct path_prefix
*pprefix
, const char *name
)
605 struct prefix_list
*pl
;
606 int len
= pprefix
->max_len
+ strlen (name
) + 1;
609 fprintf (stderr
, "Looking for '%s'\n", name
);
611 #ifdef HOST_EXECUTABLE_SUFFIX
612 len
+= strlen (HOST_EXECUTABLE_SUFFIX
);
615 temp
= XNEWVEC (char, len
);
617 /* Determine the filename to execute (special case for absolute paths). */
619 if (IS_ABSOLUTE_PATH (name
))
621 if (access (name
, X_OK
) == 0)
626 fprintf (stderr
, " - found: absolute path\n");
631 #ifdef HOST_EXECUTABLE_SUFFIX
632 /* Some systems have a suffix for executable files.
633 So try appending that. */
635 strcat (temp
, HOST_EXECUTABLE_SUFFIX
);
637 if (access (temp
, X_OK
) == 0)
642 fprintf (stderr
, " - failed to locate using absolute path\n");
645 for (pl
= pprefix
->plist
; pl
; pl
= pl
->next
)
649 strcpy (temp
, pl
->prefix
);
652 if (stat (temp
, &st
) >= 0
653 && ! S_ISDIR (st
.st_mode
)
654 && access (temp
, X_OK
) == 0)
657 #ifdef HOST_EXECUTABLE_SUFFIX
658 /* Some systems have a suffix for executable files.
659 So try appending that. */
660 strcat (temp
, HOST_EXECUTABLE_SUFFIX
);
662 if (stat (temp
, &st
) >= 0
663 && ! S_ISDIR (st
.st_mode
)
664 && access (temp
, X_OK
) == 0)
669 if (debug
&& pprefix
->plist
== NULL
)
670 fprintf (stderr
, " - failed: no entries in prefix list\n");
676 /* Add an entry for PREFIX to prefix list PPREFIX. */
679 add_prefix (struct path_prefix
*pprefix
, const char *prefix
)
681 struct prefix_list
*pl
, **prev
;
686 for (pl
= pprefix
->plist
; pl
->next
; pl
= pl
->next
)
691 prev
= &pprefix
->plist
;
693 /* Keep track of the longest prefix. */
695 len
= strlen (prefix
);
696 if (len
> pprefix
->max_len
)
697 pprefix
->max_len
= len
;
699 pl
= XNEW (struct prefix_list
);
700 pl
->prefix
= xstrdup (prefix
);
705 pl
->next
= (struct prefix_list
*) 0;
709 /* Take the value of the environment variable ENV, break it into a path, and
710 add of the entries to PPREFIX. */
713 prefix_from_env (const char *env
, struct path_prefix
*pprefix
)
716 GET_ENVIRONMENT (p
, env
);
719 prefix_from_string (p
, pprefix
);
723 prefix_from_string (const char *p
, struct path_prefix
*pprefix
)
725 const char *startp
, *endp
;
726 char *nstore
= XNEWVEC (char, strlen (p
) + 3);
729 fprintf (stderr
, "Convert string '%s' into prefixes, separator = '%c'\n", p
, PATH_SEPARATOR
);
734 if (*endp
== PATH_SEPARATOR
|| *endp
== 0)
736 strncpy (nstore
, startp
, endp
-startp
);
739 strcpy (nstore
, "./");
741 else if (! IS_DIR_SEPARATOR (endp
[-1]))
743 nstore
[endp
-startp
] = DIR_SEPARATOR
;
744 nstore
[endp
-startp
+1] = 0;
747 nstore
[endp
-startp
] = 0;
750 fprintf (stderr
, " - add prefix: %s\n", nstore
);
752 add_prefix (pprefix
, nstore
);
755 endp
= startp
= endp
+ 1;
766 main (int argc
, char **argv
)
768 static const char *const ld_suffix
= "ld";
769 static const char *const real_ld_suffix
= "real-ld";
770 static const char *const collect_ld_suffix
= "collect-ld";
771 static const char *const nm_suffix
= "nm";
772 static const char *const gnm_suffix
= "gnm";
774 static const char *const ldd_suffix
= LDD_SUFFIX
;
776 static const char *const strip_suffix
= "strip";
777 static const char *const gstrip_suffix
= "gstrip";
779 #ifdef CROSS_DIRECTORY_STRUCTURE
780 /* If we look for a program in the compiler directories, we just use
781 the short name, since these directories are already system-specific.
782 But it we look for a program in the system directories, we need to
783 qualify the program name with the target machine. */
785 const char *const full_ld_suffix
=
786 concat(target_machine
, "-", ld_suffix
, NULL
);
787 const char *const full_nm_suffix
=
788 concat (target_machine
, "-", nm_suffix
, NULL
);
789 const char *const full_gnm_suffix
=
790 concat (target_machine
, "-", gnm_suffix
, NULL
);
792 const char *const full_ldd_suffix
=
793 concat (target_machine
, "-", ldd_suffix
, NULL
);
795 const char *const full_strip_suffix
=
796 concat (target_machine
, "-", strip_suffix
, NULL
);
797 const char *const full_gstrip_suffix
=
798 concat (target_machine
, "-", gstrip_suffix
, NULL
);
800 const char *const full_ld_suffix
= ld_suffix
;
801 const char *const full_nm_suffix
= nm_suffix
;
802 const char *const full_gnm_suffix
= gnm_suffix
;
804 const char *const full_ldd_suffix
= ldd_suffix
;
806 const char *const full_strip_suffix
= strip_suffix
;
807 const char *const full_gstrip_suffix
= gstrip_suffix
;
808 #endif /* CROSS_DIRECTORY_STRUCTURE */
812 #ifdef COLLECT_EXPORT_LIST
815 const char *ld_file_name
;
830 expandargv (&argc
, &argv
);
831 if (argv
!= old_argv
)
832 at_file_supplied
= 1;
834 num_c_args
= argc
+ 9;
836 no_demangle
= !! getenv ("COLLECT_NO_DEMANGLE");
838 /* Suppress demangling by the real linker, which may be broken. */
839 putenv (xstrdup ("COLLECT_NO_DEMANGLE="));
841 #if defined (COLLECT2_HOST_INITIALIZATION)
842 /* Perform system dependent initialization, if necessary. */
843 COLLECT2_HOST_INITIALIZATION
;
847 /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
848 receive the signal. A different setting is inheritable */
849 signal (SIGCHLD
, SIG_DFL
);
852 /* Unlock the stdio streams. */
853 unlock_std_streams ();
857 /* Do not invoke xcalloc before this point, since locale needs to be
858 set first, in case a diagnostic is issued. */
860 ld1_argv
= XCNEWVEC (char *, argc
+ 4);
861 ld1
= CONST_CAST2 (const char **, char **, ld1_argv
);
862 ld2_argv
= XCNEWVEC (char *, argc
+ 11);
863 ld2
= CONST_CAST2 (const char **, char **, ld2_argv
);
864 object_lst
= XCNEWVEC (char *, argc
);
865 object
= CONST_CAST2 (const char **, char **, object_lst
);
871 /* Parse command line early for instances of -debug. This allows
872 the debug flag to be set before functions like find_a_file()
877 for (i
= 1; argv
[i
] != NULL
; i
++)
879 if (! strcmp (argv
[i
], "-debug"))
885 #ifndef DEFAULT_A_OUT_NAME
886 output_file
= "a.out";
888 output_file
= DEFAULT_A_OUT_NAME
;
891 obstack_begin (&temporary_obstack
, 0);
892 temporary_firstobj
= (char *) obstack_alloc (&temporary_obstack
, 0);
894 #ifndef HAVE_LD_DEMANGLE
895 current_demangling_style
= auto_demangling
;
897 p
= getenv ("COLLECT_GCC_OPTIONS");
900 const char *q
= extract_string (&p
);
901 if (*q
== '-' && (q
[1] == 'm' || q
[1] == 'f'))
904 obstack_free (&temporary_obstack
, temporary_firstobj
);
906 /* -fno-profile-arcs -fno-test-coverage -fno-branch-probabilities
907 -fno-exceptions -w */
910 c_argv
= XCNEWVEC (char *, num_c_args
);
911 c_ptr
= CONST_CAST2 (const char **, char **, c_argv
);
914 fatal ("no arguments");
917 if (signal (SIGQUIT
, SIG_IGN
) != SIG_IGN
)
918 signal (SIGQUIT
, handler
);
920 if (signal (SIGINT
, SIG_IGN
) != SIG_IGN
)
921 signal (SIGINT
, handler
);
923 if (signal (SIGALRM
, SIG_IGN
) != SIG_IGN
)
924 signal (SIGALRM
, handler
);
927 if (signal (SIGHUP
, SIG_IGN
) != SIG_IGN
)
928 signal (SIGHUP
, handler
);
930 if (signal (SIGSEGV
, SIG_IGN
) != SIG_IGN
)
931 signal (SIGSEGV
, handler
);
933 if (signal (SIGBUS
, SIG_IGN
) != SIG_IGN
)
934 signal (SIGBUS
, handler
);
937 /* Extract COMPILER_PATH and PATH into our prefix list. */
938 prefix_from_env ("COMPILER_PATH", &cpath
);
939 prefix_from_env ("PATH", &path
);
941 /* Try to discover a valid linker/nm/strip to use. */
943 /* Maybe we know the right file to use (if not cross). */
945 #ifdef DEFAULT_LINKER
946 if (access (DEFAULT_LINKER
, X_OK
) == 0)
947 ld_file_name
= DEFAULT_LINKER
;
948 if (ld_file_name
== 0)
950 #ifdef REAL_LD_FILE_NAME
951 ld_file_name
= find_a_file (&path
, REAL_LD_FILE_NAME
);
952 if (ld_file_name
== 0)
954 /* Search the (target-specific) compiler dirs for ld'. */
955 ld_file_name
= find_a_file (&cpath
, real_ld_suffix
);
956 /* Likewise for `collect-ld'. */
957 if (ld_file_name
== 0)
958 ld_file_name
= find_a_file (&cpath
, collect_ld_suffix
);
959 /* Search the compiler directories for `ld'. We have protection against
960 recursive calls in find_a_file. */
961 if (ld_file_name
== 0)
962 ld_file_name
= find_a_file (&cpath
, ld_suffix
);
963 /* Search the ordinary system bin directories
964 for `ld' (if native linking) or `TARGET-ld' (if cross). */
965 if (ld_file_name
== 0)
966 ld_file_name
= find_a_file (&path
, full_ld_suffix
);
968 #ifdef REAL_NM_FILE_NAME
969 nm_file_name
= find_a_file (&path
, REAL_NM_FILE_NAME
);
970 if (nm_file_name
== 0)
972 nm_file_name
= find_a_file (&cpath
, gnm_suffix
);
973 if (nm_file_name
== 0)
974 nm_file_name
= find_a_file (&path
, full_gnm_suffix
);
975 if (nm_file_name
== 0)
976 nm_file_name
= find_a_file (&cpath
, nm_suffix
);
977 if (nm_file_name
== 0)
978 nm_file_name
= find_a_file (&path
, full_nm_suffix
);
981 ldd_file_name
= find_a_file (&cpath
, ldd_suffix
);
982 if (ldd_file_name
== 0)
983 ldd_file_name
= find_a_file (&path
, full_ldd_suffix
);
986 #ifdef REAL_STRIP_FILE_NAME
987 strip_file_name
= find_a_file (&path
, REAL_STRIP_FILE_NAME
);
988 if (strip_file_name
== 0)
990 strip_file_name
= find_a_file (&cpath
, gstrip_suffix
);
991 if (strip_file_name
== 0)
992 strip_file_name
= find_a_file (&path
, full_gstrip_suffix
);
993 if (strip_file_name
== 0)
994 strip_file_name
= find_a_file (&cpath
, strip_suffix
);
995 if (strip_file_name
== 0)
996 strip_file_name
= find_a_file (&path
, full_strip_suffix
);
998 /* Determine the full path name of the C compiler to use. */
999 c_file_name
= getenv ("COLLECT_GCC");
1000 if (c_file_name
== 0)
1002 #ifdef CROSS_DIRECTORY_STRUCTURE
1003 c_file_name
= concat (target_machine
, "-gcc", NULL
);
1005 c_file_name
= "gcc";
1009 p
= find_a_file (&cpath
, c_file_name
);
1011 /* Here it should be safe to use the system search path since we should have
1012 already qualified the name of the compiler when it is needed. */
1014 p
= find_a_file (&path
, c_file_name
);
1019 *ld1
++ = *ld2
++ = ld_file_name
;
1021 /* Make temp file names. */
1022 c_file
= make_temp_file (".c");
1023 o_file
= make_temp_file (".o");
1024 #ifdef COLLECT_EXPORT_LIST
1025 export_file
= make_temp_file (".x");
1027 ldout
= make_temp_file (".ld");
1028 lderrout
= make_temp_file (".le");
1029 *c_ptr
++ = c_file_name
;
1036 #ifdef COLLECT_EXPORT_LIST
1037 /* Generate a list of directories from LIBPATH. */
1038 prefix_from_env ("LIBPATH", &libpath_lib_dirs
);
1039 /* Add to this list also two standard directories where
1040 AIX loader always searches for libraries. */
1041 add_prefix (&libpath_lib_dirs
, "/lib");
1042 add_prefix (&libpath_lib_dirs
, "/usr/lib");
1045 /* Get any options that the upper GCC wants to pass to the sub-GCC.
1047 AIX support needs to know if -shared has been specified before
1048 parsing commandline arguments. */
1050 p
= getenv ("COLLECT_GCC_OPTIONS");
1053 const char *q
= extract_string (&p
);
1054 if (*q
== '-' && (q
[1] == 'm' || q
[1] == 'f'))
1055 *c_ptr
++ = xstrdup (q
);
1056 if (strcmp (q
, "-EL") == 0 || strcmp (q
, "-EB") == 0)
1057 *c_ptr
++ = xstrdup (q
);
1058 if (strcmp (q
, "-shared") == 0)
1060 if (*q
== '-' && q
[1] == 'B')
1062 *c_ptr
++ = xstrdup (q
);
1065 q
= extract_string (&p
);
1066 *c_ptr
++ = xstrdup (q
);
1070 obstack_free (&temporary_obstack
, temporary_firstobj
);
1071 *c_ptr
++ = "-fno-profile-arcs";
1072 *c_ptr
++ = "-fno-test-coverage";
1073 *c_ptr
++ = "-fno-branch-probabilities";
1074 *c_ptr
++ = "-fno-exceptions";
1077 /* !!! When GCC calls collect2,
1078 it does not know whether it is calling collect2 or ld.
1079 So collect2 cannot meaningfully understand any options
1080 except those ld understands.
1081 If you propose to make GCC pass some other option,
1082 just imagine what will happen if ld is really ld!!! */
1084 /* Parse arguments. Remember output file spec, pass the rest to ld. */
1085 /* After the first file, put in the c++ rt0. */
1088 #ifdef HAVE_LD_DEMANGLE
1089 if (!demangle_flag
&& !no_demangle
)
1090 demangle_flag
= "--demangle";
1092 *ld1
++ = *ld2
++ = demangle_flag
;
1094 while ((arg
= *++argv
) != (char *) 0)
1096 *ld1
++ = *ld2
++ = arg
;
1102 #ifdef COLLECT_EXPORT_LIST
1103 /* We want to disable automatic exports on AIX when user
1104 explicitly puts an export list in command line */
1106 if (arg
[2] == 'E' || strncmp (&arg
[2], "export", 6) == 0)
1108 else if (arg
[2] == '6' && arg
[3] == '4')
1110 else if (arg
[2] == 'r' && arg
[3] == 't' && arg
[4] == 'l')
1116 if (!strcmp (arg
, "-debug"))
1118 /* Already parsed. */
1122 if (!strcmp (arg
, "-dynamic-linker") && argv
[1])
1125 *ld1
++ = *ld2
++ = *argv
;
1132 /* place o_file BEFORE this argument! */
1138 #ifdef COLLECT_EXPORT_LIST
1140 /* Resolving full library name. */
1141 const char *s
= resolve_lib_name (arg
+2);
1143 /* Saving a full library name. */
1144 add_to_list (&libs
, s
);
1149 #ifdef COLLECT_EXPORT_LIST
1150 /* Saving directories where to search for libraries. */
1152 add_prefix (&cmdline_lib_dirs
, arg
+2);
1155 #if LINK_ELIMINATE_DUPLICATE_LDIRECTORIES
1157 if (is_in_args (arg
, (const char **) ld1_argv
, ld1
-1))
1160 #endif /* LINK_ELIMINATE_DUPLICATE_LDIRECTORIES */
1165 output_file
= *ld1
++ = *ld2
++ = *++argv
;
1167 #ifdef SWITCHES_NEED_SPACES
1168 && ! strchr (SWITCHES_NEED_SPACES
, arg
[1])
1172 output_file
= &arg
[2];
1181 if (arg
[2] == '\0' && do_collecting
)
1183 /* We must strip after the nm run, otherwise C++ linking
1184 will not work. Thus we strip in the second ld run, or
1185 else with strip if there is no second ld run. */
1197 if (strcmp (arg
, "--no-demangle") == 0)
1199 demangle_flag
= arg
;
1204 else if (strncmp (arg
, "--demangle", 10) == 0)
1206 demangle_flag
= arg
;
1208 #ifndef HAVE_LD_DEMANGLE
1211 enum demangling_styles style
1212 = cplus_demangle_name_to_style (arg
+11);
1213 if (style
== unknown_demangling
)
1214 error ("unknown demangling style '%s'", arg
+11);
1216 current_demangling_style
= style
;
1225 else if ((p
= strrchr (arg
, '.')) != (char *) 0
1226 && (strcmp (p
, ".o") == 0 || strcmp (p
, ".a") == 0
1227 || strcmp (p
, ".so") == 0 || strcmp (p
, ".lo") == 0
1228 || strcmp (p
, ".obj") == 0))
1237 /* place o_file BEFORE this argument! */
1243 if (p
[1] == 'o' || p
[1] == 'l')
1245 #ifdef COLLECT_EXPORT_LIST
1246 /* libraries can be specified directly, i.e. without -l flag. */
1249 /* Saving a full library name. */
1250 add_to_list (&libs
, arg
);
1256 #ifdef COLLECT_EXPORT_LIST
1257 /* This is added only for debugging purposes. */
1260 fprintf (stderr
, "List of libraries:\n");
1261 dump_list (stderr
, "\t", libs
.first
);
1264 /* The AIX linker will discard static constructors in object files if
1265 nothing else in the file is referenced, so look at them first. */
1267 const char **export_object_lst
= (const char **)object_lst
;
1269 while (export_object_lst
< object
)
1270 scan_prog_file (*export_object_lst
++, PASS_OBJ
);
1273 struct id
*list
= libs
.first
;
1275 for (; list
; list
= list
->next
)
1276 scan_prog_file (list
->name
, PASS_FIRST
);
1281 char *buf
= concat ("-bE:", export_file
, NULL
);
1286 exportf
= fopen (export_file
, "w");
1287 if (exportf
== (FILE *) 0)
1288 fatal_perror ("fopen %s", export_file
);
1289 write_aix_file (exportf
, exports
.first
);
1290 if (fclose (exportf
))
1291 fatal_perror ("fclose %s", export_file
);
1296 *c_ptr
= *ld1
= *object
= (char *) 0;
1300 notice ("collect2 version %s", version_string
);
1301 #ifdef TARGET_VERSION
1304 fprintf (stderr
, "\n");
1310 fprintf (stderr
, "ld_file_name = %s\n",
1311 (ld_file_name
? ld_file_name
: "not found"));
1312 fprintf (stderr
, "c_file_name = %s\n",
1313 (c_file_name
? c_file_name
: "not found"));
1314 fprintf (stderr
, "nm_file_name = %s\n",
1315 (nm_file_name
? nm_file_name
: "not found"));
1317 fprintf (stderr
, "ldd_file_name = %s\n",
1318 (ldd_file_name
? ldd_file_name
: "not found"));
1320 fprintf (stderr
, "strip_file_name = %s\n",
1321 (strip_file_name
? strip_file_name
: "not found"));
1322 fprintf (stderr
, "c_file = %s\n",
1323 (c_file
? c_file
: "not found"));
1324 fprintf (stderr
, "o_file = %s\n",
1325 (o_file
? o_file
: "not found"));
1327 ptr
= getenv ("COLLECT_GCC_OPTIONS");
1329 fprintf (stderr
, "COLLECT_GCC_OPTIONS = %s\n", ptr
);
1331 ptr
= getenv ("COLLECT_GCC");
1333 fprintf (stderr
, "COLLECT_GCC = %s\n", ptr
);
1335 ptr
= getenv ("COMPILER_PATH");
1337 fprintf (stderr
, "COMPILER_PATH = %s\n", ptr
);
1339 ptr
= getenv (LIBRARY_PATH_ENV
);
1341 fprintf (stderr
, "%-20s= %s\n", LIBRARY_PATH_ENV
, ptr
);
1343 fprintf (stderr
, "\n");
1346 /* Load the program, searching all libraries and attempting to provide
1347 undefined symbols from repository information. */
1349 /* On AIX we do this later. */
1350 #ifndef COLLECT_EXPORT_LIST
1351 do_tlink (ld1_argv
, object_lst
);
1354 /* If -r or they will be run via some other method, do not build the
1355 constructor or destructor list, just return now. */
1357 #ifndef COLLECT_EXPORT_LIST
1362 #ifdef COLLECT_EXPORT_LIST
1363 /* Do the link we avoided above if we are exiting. */
1364 do_tlink (ld1_argv
, object_lst
);
1366 /* But make sure we delete the export file we may have created. */
1367 if (export_file
!= 0 && export_file
[0])
1368 maybe_unlink (export_file
);
1370 maybe_unlink (c_file
);
1371 maybe_unlink (o_file
);
1375 /* Examine the namelist with nm and search it for static constructors
1376 and destructors to call.
1377 Write the constructor and destructor tables to a .s file and reload. */
1379 /* On AIX we already scanned for global constructors/destructors. */
1380 #ifndef COLLECT_EXPORT_LIST
1381 scan_prog_file (output_file
, PASS_FIRST
);
1384 #ifdef SCAN_LIBRARIES
1385 scan_libraries (output_file
);
1390 notice ("%d constructor(s) found\n", constructors
.number
);
1391 notice ("%d destructor(s) found\n", destructors
.number
);
1392 notice ("%d frame table(s) found\n", frame_tables
.number
);
1395 if (constructors
.number
== 0 && destructors
.number
== 0
1396 && frame_tables
.number
== 0
1397 #if defined (SCAN_LIBRARIES) || defined (COLLECT_EXPORT_LIST)
1398 /* If we will be running these functions ourselves, we want to emit
1399 stubs into the shared library so that we do not have to relink
1400 dependent programs when we add static objects. */
1405 #ifdef COLLECT_EXPORT_LIST
1406 /* Do tlink without additional code generation. */
1407 do_tlink (ld1_argv
, object_lst
);
1409 /* Strip now if it was requested on the command line. */
1412 char **real_strip_argv
= XCNEWVEC (char *, 3);
1413 const char ** strip_argv
= CONST_CAST2 (const char **, char **,
1416 strip_argv
[0] = strip_file_name
;
1417 strip_argv
[1] = output_file
;
1418 strip_argv
[2] = (char *) 0;
1419 fork_execute ("strip", real_strip_argv
);
1422 #ifdef COLLECT_EXPORT_LIST
1423 maybe_unlink (export_file
);
1425 maybe_unlink (c_file
);
1426 maybe_unlink (o_file
);
1430 /* Sort ctor and dtor lists by priority. */
1431 sort_ids (&constructors
);
1432 sort_ids (&destructors
);
1434 maybe_unlink(output_file
);
1435 outf
= fopen (c_file
, "w");
1436 if (outf
== (FILE *) 0)
1437 fatal_perror ("fopen %s", c_file
);
1439 write_c_file (outf
, c_file
);
1442 fatal_perror ("fclose %s", c_file
);
1444 /* Tell the linker that we have initializer and finalizer functions. */
1445 #ifdef LD_INIT_SWITCH
1446 #ifdef COLLECT_EXPORT_LIST
1447 *ld2
++ = concat (LD_INIT_SWITCH
, ":", initname
, ":", fininame
, NULL
);
1449 *ld2
++ = LD_INIT_SWITCH
;
1451 *ld2
++ = LD_FINI_SWITCH
;
1456 #ifdef COLLECT_EXPORT_LIST
1459 /* If we did not add export flag to link arguments before, add it to
1460 second link phase now. No new exports should have been added. */
1461 if (! exports
.first
)
1462 *ld2
++ = concat ("-bE:", export_file
, NULL
);
1464 #ifndef LD_INIT_SWITCH
1465 add_to_list (&exports
, initname
);
1466 add_to_list (&exports
, fininame
);
1467 add_to_list (&exports
, "_GLOBAL__DI");
1468 add_to_list (&exports
, "_GLOBAL__DD");
1470 exportf
= fopen (export_file
, "w");
1471 if (exportf
== (FILE *) 0)
1472 fatal_perror ("fopen %s", export_file
);
1473 write_aix_file (exportf
, exports
.first
);
1474 if (fclose (exportf
))
1475 fatal_perror ("fclose %s", export_file
);
1479 /* End of arguments to second link phase. */
1484 fprintf (stderr
, "\n========== output_file = %s, c_file = %s\n",
1485 output_file
, c_file
);
1486 write_c_file (stderr
, "stderr");
1487 fprintf (stderr
, "========== end of c_file\n\n");
1488 #ifdef COLLECT_EXPORT_LIST
1489 fprintf (stderr
, "\n========== export_file = %s\n", export_file
);
1490 write_aix_file (stderr
, exports
.first
);
1491 fprintf (stderr
, "========== end of export_file\n\n");
1495 /* Assemble the constructor and destructor tables.
1496 Link the tables in with the rest of the program. */
1498 fork_execute ("gcc", c_argv
);
1499 #ifdef COLLECT_EXPORT_LIST
1500 /* On AIX we must call tlink because of possible templates resolution. */
1501 do_tlink (ld2_argv
, object_lst
);
1503 /* Otherwise, simply call ld because tlink is already done. */
1504 fork_execute ("ld", ld2_argv
);
1506 /* Let scan_prog_file do any final mods (OSF/rose needs this for
1507 constructors/destructors in shared libraries. */
1508 scan_prog_file (output_file
, PASS_SECOND
);
1511 maybe_unlink (c_file
);
1512 maybe_unlink (o_file
);
1514 #ifdef COLLECT_EXPORT_LIST
1515 maybe_unlink (export_file
);
1522 /* Wait for a process to finish, and exit if a nonzero status is found. */
1525 collect_wait (const char *prog
, struct pex_obj
*pex
)
1529 if (!pex_get_status (pex
, 1, &status
))
1530 fatal_perror ("can't get program status");
1535 if (WIFSIGNALED (status
))
1537 int sig
= WTERMSIG (status
);
1538 error ("%s terminated with signal %d [%s]%s",
1539 prog
, sig
, strsignal(sig
),
1540 WCOREDUMP(status
) ? ", core dumped" : "");
1541 collect_exit (FATAL_EXIT_CODE
);
1544 if (WIFEXITED (status
))
1545 return WEXITSTATUS (status
);
1551 do_wait (const char *prog
, struct pex_obj
*pex
)
1553 int ret
= collect_wait (prog
, pex
);
1556 error ("%s returned %d exit status", prog
, ret
);
1562 unlink (response_file
);
1563 response_file
= NULL
;
1568 /* Execute a program, and wait for the reply. */
1571 collect_execute (const char *prog
, char **argv
, const char *outname
,
1572 const char *errname
)
1574 struct pex_obj
*pex
;
1577 char *response_arg
= NULL
;
1578 char *response_argv
[3] ATTRIBUTE_UNUSED
;
1580 if (HAVE_GNU_LD
&& at_file_supplied
&& argv
[0] != NULL
)
1582 /* If using @file arguments, create a temporary file and put the
1583 contents of argv into it. Then change argv to an array corresponding
1584 to a single argument @FILE, where FILE is the temporary filename. */
1586 char **current_argv
= argv
+ 1;
1587 char *argv0
= argv
[0];
1591 /* Note: we assume argv contains at least one element; this is
1594 response_file
= make_temp_file ("");
1596 f
= fopen (response_file
, "w");
1599 fatal ("could not open response file %s", response_file
);
1601 status
= writeargv (current_argv
, f
);
1604 fatal ("could not write to response file %s", response_file
);
1606 status
= fclose (f
);
1609 fatal ("could not close response file %s", response_file
);
1611 response_arg
= concat ("@", response_file
, NULL
);
1612 response_argv
[0] = argv0
;
1613 response_argv
[1] = response_arg
;
1614 response_argv
[2] = NULL
;
1616 argv
= response_argv
;
1625 fprintf (stderr
, "%s", argv
[0]);
1627 notice ("[cannot find %s]", prog
);
1629 for (p_argv
= &argv
[1]; (str
= *p_argv
) != (char *) 0; p_argv
++)
1630 fprintf (stderr
, " %s", str
);
1632 fprintf (stderr
, "\n");
1638 /* If we cannot find a program we need, complain error. Do this here
1639 since we might not end up needing something that we could not find. */
1642 fatal ("cannot find '%s'", prog
);
1644 pex
= pex_init (0, "collect2", NULL
);
1646 fatal_perror ("pex_init failed");
1648 errmsg
= pex_run (pex
, PEX_LAST
| PEX_SEARCH
, argv
[0], argv
, outname
,
1655 fatal_perror (errmsg
);
1662 free (response_arg
);
1668 fork_execute (const char *prog
, char **argv
)
1670 struct pex_obj
*pex
;
1672 pex
= collect_execute (prog
, argv
, NULL
, NULL
);
1673 do_wait (prog
, pex
);
1676 /* Unlink a file unless we are debugging. */
1679 maybe_unlink (const char *file
)
1682 unlink_if_ordinary (file
);
1684 notice ("[Leaving %s]\n", file
);
1688 static long sequence_number
= 0;
1690 /* Add a name to a linked list. */
1693 add_to_list (struct head
*head_ptr
, const char *name
)
1696 = (struct id
*) xcalloc (sizeof (struct id
) + strlen (name
), 1);
1698 strcpy (newid
->name
, name
);
1700 if (head_ptr
->first
)
1701 head_ptr
->last
->next
= newid
;
1703 head_ptr
->first
= newid
;
1705 /* Check for duplicate symbols. */
1706 for (p
= head_ptr
->first
;
1707 strcmp (name
, p
->name
) != 0;
1712 head_ptr
->last
->next
= 0;
1717 newid
->sequence
= ++sequence_number
;
1718 head_ptr
->last
= newid
;
1722 /* Grab the init priority number from an init function name that
1723 looks like "_GLOBAL_.I.12345.foo". */
1726 extract_init_priority (const char *name
)
1730 while (name
[pos
] == '_')
1732 pos
+= 10; /* strlen ("GLOBAL__X_") */
1734 /* Extract init_p number from ctor/dtor name. */
1735 pri
= atoi (name
+ pos
);
1736 return pri
? pri
: DEFAULT_INIT_PRIORITY
;
1739 /* Insertion sort the ids from ctor/dtor list HEAD_PTR in descending order.
1740 ctors will be run from right to left, dtors from left to right. */
1743 sort_ids (struct head
*head_ptr
)
1745 /* id holds the current element to insert. id_next holds the next
1746 element to insert. id_ptr iterates through the already sorted elements
1747 looking for the place to insert id. */
1748 struct id
*id
, *id_next
, **id_ptr
;
1750 id
= head_ptr
->first
;
1752 /* We don't have any sorted elements yet. */
1753 head_ptr
->first
= NULL
;
1755 for (; id
; id
= id_next
)
1758 id
->sequence
= extract_init_priority (id
->name
);
1760 for (id_ptr
= &(head_ptr
->first
); ; id_ptr
= &((*id_ptr
)->next
))
1762 /* If the sequence numbers are the same, we put the id from the
1763 file later on the command line later in the list. */
1764 || id
->sequence
> (*id_ptr
)->sequence
1765 /* Hack: do lexical compare, too.
1766 || (id->sequence == (*id_ptr)->sequence
1767 && strcmp (id->name, (*id_ptr)->name) > 0) */
1776 /* Now set the sequence numbers properly so write_c_file works. */
1777 for (id
= head_ptr
->first
; id
; id
= id
->next
)
1778 id
->sequence
= ++sequence_number
;
1781 /* Write: `prefix', the names on list LIST, `suffix'. */
1784 write_list (FILE *stream
, const char *prefix
, struct id
*list
)
1788 fprintf (stream
, "%sx%d,\n", prefix
, list
->sequence
);
1793 #if LINK_ELIMINATE_DUPLICATE_LDIRECTORIES
1794 /* Given a STRING, return nonzero if it occurs in the list in range
1795 [ARGS_BEGIN,ARGS_END). */
1798 is_in_args (const char *string
, const char **args_begin
,
1799 const char **args_end
)
1801 const char **args_pointer
;
1802 for (args_pointer
= args_begin
; args_pointer
!= args_end
; ++args_pointer
)
1803 if (strcmp (string
, *args_pointer
) == 0)
1807 #endif /* LINK_ELIMINATE_DUPLICATE_LDIRECTORIES */
1809 #ifdef COLLECT_EXPORT_LIST
1810 /* This function is really used only on AIX, but may be useful. */
1813 is_in_list (const char *prefix
, struct id
*list
)
1817 if (!strcmp (prefix
, list
->name
)) return 1;
1823 #endif /* COLLECT_EXPORT_LIST */
1825 /* Added for debugging purpose. */
1826 #ifdef COLLECT_EXPORT_LIST
1828 dump_list (FILE *stream
, const char *prefix
, struct id
*list
)
1832 fprintf (stream
, "%s%s,\n", prefix
, list
->name
);
1840 dump_prefix_list (FILE *stream
, const char *prefix
, struct prefix_list
*list
)
1844 fprintf (stream
, "%s%s,\n", prefix
, list
->prefix
);
1851 write_list_with_asm (FILE *stream
, const char *prefix
, struct id
*list
)
1855 fprintf (stream
, "%sx%d __asm__ (\"%s\");\n",
1856 prefix
, list
->sequence
, list
->name
);
1861 /* Write out the constructor and destructor tables statically (for a shared
1862 object), along with the functions to execute them. */
1865 write_c_file_stat (FILE *stream
, const char *name ATTRIBUTE_UNUSED
)
1869 int frames
= (frame_tables
.number
> 0);
1871 /* Figure out name of output_file, stripping off .so version. */
1872 p
= strrchr (output_file
, '/');
1888 if (strncmp (q
, SHLIB_SUFFIX
, strlen (SHLIB_SUFFIX
)) == 0)
1890 q
+= strlen (SHLIB_SUFFIX
);
1897 /* q points to null at end of the string (or . of the .so version) */
1898 prefix
= XNEWVEC (char, q
- p
+ 1);
1899 strncpy (prefix
, p
, q
- p
);
1901 for (r
= prefix
; *r
; r
++)
1902 if (!ISALNUM ((unsigned char)*r
))
1905 notice ("\nwrite_c_file - output name is %s, prefix is %s\n",
1906 output_file
, prefix
);
1908 initname
= concat ("_GLOBAL__FI_", prefix
, NULL
);
1909 fininame
= concat ("_GLOBAL__FD_", prefix
, NULL
);
1913 /* Write the tables as C code. */
1915 fprintf (stream
, "static int count;\n");
1916 fprintf (stream
, "typedef void entry_pt();\n");
1917 write_list_with_asm (stream
, "extern entry_pt ", constructors
.first
);
1921 write_list_with_asm (stream
, "extern void *", frame_tables
.first
);
1923 fprintf (stream
, "\tstatic void *frame_table[] = {\n");
1924 write_list (stream
, "\t\t&", frame_tables
.first
);
1925 fprintf (stream
, "\t0\n};\n");
1927 /* This must match what's in frame.h. */
1928 fprintf (stream
, "struct object {\n");
1929 fprintf (stream
, " void *pc_begin;\n");
1930 fprintf (stream
, " void *pc_end;\n");
1931 fprintf (stream
, " void *fde_begin;\n");
1932 fprintf (stream
, " void *fde_array;\n");
1933 fprintf (stream
, " __SIZE_TYPE__ count;\n");
1934 fprintf (stream
, " struct object *next;\n");
1935 fprintf (stream
, "};\n");
1937 fprintf (stream
, "extern void __register_frame_info_table (void *, struct object *);\n");
1938 fprintf (stream
, "extern void *__deregister_frame_info (void *);\n");
1940 fprintf (stream
, "static void reg_frame () {\n");
1941 fprintf (stream
, "\tstatic struct object ob;\n");
1942 fprintf (stream
, "\t__register_frame_info_table (frame_table, &ob);\n");
1943 fprintf (stream
, "\t}\n");
1945 fprintf (stream
, "static void dereg_frame () {\n");
1946 fprintf (stream
, "\t__deregister_frame_info (frame_table);\n");
1947 fprintf (stream
, "\t}\n");
1950 fprintf (stream
, "void %s() {\n", initname
);
1951 if (constructors
.number
> 0 || frames
)
1953 fprintf (stream
, "\tstatic entry_pt *ctors[] = {\n");
1954 write_list (stream
, "\t\t", constructors
.first
);
1956 fprintf (stream
, "\treg_frame,\n");
1957 fprintf (stream
, "\t};\n");
1958 fprintf (stream
, "\tentry_pt **p;\n");
1959 fprintf (stream
, "\tif (count++ != 0) return;\n");
1960 fprintf (stream
, "\tp = ctors + %d;\n", constructors
.number
+ frames
);
1961 fprintf (stream
, "\twhile (p > ctors) (*--p)();\n");
1964 fprintf (stream
, "\t++count;\n");
1965 fprintf (stream
, "}\n");
1966 write_list_with_asm (stream
, "extern entry_pt ", destructors
.first
);
1967 fprintf (stream
, "void %s() {\n", fininame
);
1968 if (destructors
.number
> 0 || frames
)
1970 fprintf (stream
, "\tstatic entry_pt *dtors[] = {\n");
1971 write_list (stream
, "\t\t", destructors
.first
);
1973 fprintf (stream
, "\tdereg_frame,\n");
1974 fprintf (stream
, "\t};\n");
1975 fprintf (stream
, "\tentry_pt **p;\n");
1976 fprintf (stream
, "\tif (--count != 0) return;\n");
1977 fprintf (stream
, "\tp = dtors;\n");
1978 fprintf (stream
, "\twhile (p < dtors + %d) (*p++)();\n",
1979 destructors
.number
+ frames
);
1981 fprintf (stream
, "}\n");
1985 COLLECT_SHARED_INIT_FUNC(stream
, initname
);
1986 COLLECT_SHARED_FINI_FUNC(stream
, fininame
);
1990 /* Write the constructor/destructor tables. */
1992 #ifndef LD_INIT_SWITCH
1994 write_c_file_glob (FILE *stream
, const char *name ATTRIBUTE_UNUSED
)
1996 /* Write the tables as C code. */
1998 int frames
= (frame_tables
.number
> 0);
2000 fprintf (stream
, "typedef void entry_pt();\n\n");
2002 write_list_with_asm (stream
, "extern entry_pt ", constructors
.first
);
2006 write_list_with_asm (stream
, "extern void *", frame_tables
.first
);
2008 fprintf (stream
, "\tstatic void *frame_table[] = {\n");
2009 write_list (stream
, "\t\t&", frame_tables
.first
);
2010 fprintf (stream
, "\t0\n};\n");
2012 /* This must match what's in frame.h. */
2013 fprintf (stream
, "struct object {\n");
2014 fprintf (stream
, " void *pc_begin;\n");
2015 fprintf (stream
, " void *pc_end;\n");
2016 fprintf (stream
, " void *fde_begin;\n");
2017 fprintf (stream
, " void *fde_array;\n");
2018 fprintf (stream
, " __SIZE_TYPE__ count;\n");
2019 fprintf (stream
, " struct object *next;\n");
2020 fprintf (stream
, "};\n");
2022 fprintf (stream
, "extern void __register_frame_info_table (void *, struct object *);\n");
2023 fprintf (stream
, "extern void *__deregister_frame_info (void *);\n");
2025 fprintf (stream
, "static void reg_frame () {\n");
2026 fprintf (stream
, "\tstatic struct object ob;\n");
2027 fprintf (stream
, "\t__register_frame_info_table (frame_table, &ob);\n");
2028 fprintf (stream
, "\t}\n");
2030 fprintf (stream
, "static void dereg_frame () {\n");
2031 fprintf (stream
, "\t__deregister_frame_info (frame_table);\n");
2032 fprintf (stream
, "\t}\n");
2035 fprintf (stream
, "\nentry_pt * __CTOR_LIST__[] = {\n");
2036 fprintf (stream
, "\t(entry_pt *) %d,\n", constructors
.number
+ frames
);
2037 write_list (stream
, "\t", constructors
.first
);
2039 fprintf (stream
, "\treg_frame,\n");
2040 fprintf (stream
, "\t0\n};\n\n");
2042 write_list_with_asm (stream
, "extern entry_pt ", destructors
.first
);
2044 fprintf (stream
, "\nentry_pt * __DTOR_LIST__[] = {\n");
2045 fprintf (stream
, "\t(entry_pt *) %d,\n", destructors
.number
+ frames
);
2046 write_list (stream
, "\t", destructors
.first
);
2048 fprintf (stream
, "\tdereg_frame,\n");
2049 fprintf (stream
, "\t0\n};\n\n");
2051 fprintf (stream
, "extern entry_pt %s;\n", NAME__MAIN
);
2052 fprintf (stream
, "entry_pt *__main_reference = %s;\n\n", NAME__MAIN
);
2054 #endif /* ! LD_INIT_SWITCH */
2057 write_c_file (FILE *stream
, const char *name
)
2059 #ifndef LD_INIT_SWITCH
2061 write_c_file_glob (stream
, name
);
2064 write_c_file_stat (stream
, name
);
2067 #ifdef COLLECT_EXPORT_LIST
2069 write_aix_file (FILE *stream
, struct id
*list
)
2071 for (; list
; list
= list
->next
)
2073 fputs (list
->name
, stream
);
2074 putc ('\n', stream
);
2079 #ifdef OBJECT_FORMAT_NONE
2081 /* Generic version to scan the name list of the loaded program for
2082 the symbols g++ uses for static constructors and destructors.
2084 The constructor table begins at __CTOR_LIST__ and contains a count
2085 of the number of pointers (or -1 if the constructors are built in a
2086 separate section by the linker), followed by the pointers to the
2087 constructor functions, terminated with a null pointer. The
2088 destructor table has the same format, and begins at __DTOR_LIST__. */
2091 scan_prog_file (const char *prog_name
, enum pass which_pass
)
2093 void (*int_handler
) (int);
2095 void (*quit_handler
) (int);
2097 char *real_nm_argv
[4];
2098 const char **nm_argv
= CONST_CAST2 (const char **, char**, real_nm_argv
);
2100 struct pex_obj
*pex
;
2106 if (which_pass
== PASS_SECOND
)
2109 /* If we do not have an `nm', complain. */
2110 if (nm_file_name
== 0)
2111 fatal ("cannot find 'nm'");
2113 nm_argv
[argc
++] = nm_file_name
;
2114 if (NM_FLAGS
[0] != '\0')
2115 nm_argv
[argc
++] = NM_FLAGS
;
2117 nm_argv
[argc
++] = prog_name
;
2118 nm_argv
[argc
++] = (char *) 0;
2120 /* Trace if needed. */
2123 const char **p_argv
;
2126 for (p_argv
= &nm_argv
[0]; (str
= *p_argv
) != (char *) 0; p_argv
++)
2127 fprintf (stderr
, " %s", str
);
2129 fprintf (stderr
, "\n");
2135 pex
= pex_init (PEX_USE_PIPES
, "collect2", NULL
);
2137 fatal_perror ("pex_init failed");
2139 errmsg
= pex_run (pex
, 0, nm_file_name
, real_nm_argv
, NULL
, NULL
, &err
);
2145 fatal_perror (errmsg
);
2151 int_handler
= (void (*) (int)) signal (SIGINT
, SIG_IGN
);
2153 quit_handler
= (void (*) (int)) signal (SIGQUIT
, SIG_IGN
);
2156 inf
= pex_read_output (pex
, 0);
2158 fatal_perror ("can't open nm output");
2161 fprintf (stderr
, "\nnm output with constructors/destructors.\n");
2163 /* Read each line of nm output. */
2164 while (fgets (buf
, sizeof buf
, inf
) != (char *) 0)
2169 /* If it contains a constructor or destructor name, add the name
2170 to the appropriate list. */
2172 for (p
= buf
; (ch
= *p
) != '\0' && ch
!= '\n' && ch
!= '_'; p
++)
2173 if (ch
== ' ' && p
[1] == 'U' && p
[2] == ' ')
2180 /* Find the end of the symbol name.
2181 Do not include `|', because Encore nm can tack that on the end. */
2182 for (end
= p
; (ch2
= *end
) != '\0' && !ISSPACE (ch2
) && ch2
!= '|';
2188 switch (is_ctor_dtor (name
))
2191 if (which_pass
!= PASS_LIB
)
2192 add_to_list (&constructors
, name
);
2196 if (which_pass
!= PASS_LIB
)
2197 add_to_list (&destructors
, name
);
2201 if (which_pass
!= PASS_LIB
)
2202 fatal ("init function found in object %s", prog_name
);
2203 #ifndef LD_INIT_SWITCH
2204 add_to_list (&constructors
, name
);
2209 if (which_pass
!= PASS_LIB
)
2210 fatal ("fini function found in object %s", prog_name
);
2211 #ifndef LD_FINI_SWITCH
2212 add_to_list (&destructors
, name
);
2217 if (which_pass
!= PASS_LIB
)
2218 add_to_list (&frame_tables
, name
);
2221 default: /* not a constructor or destructor */
2226 fprintf (stderr
, "\t%s\n", buf
);
2230 fprintf (stderr
, "\n");
2232 do_wait (nm_file_name
, pex
);
2234 signal (SIGINT
, int_handler
);
2236 signal (SIGQUIT
, quit_handler
);
2242 /* Use the List Dynamic Dependencies program to find shared libraries that
2243 the output file depends upon and their initialization/finalization
2244 routines, if any. */
2247 scan_libraries (const char *prog_name
)
2249 static struct head libraries
; /* list of shared libraries found */
2251 void (*int_handler
) (int);
2253 void (*quit_handler
) (int);
2255 char *real_ldd_argv
[4];
2256 const char **ldd_argv
= (const char **) real_ldd_argv
;
2258 struct pex_obj
*pex
;
2264 /* If we do not have an `ldd', complain. */
2265 if (ldd_file_name
== 0)
2267 error ("cannot find 'ldd'");
2271 ldd_argv
[argc
++] = ldd_file_name
;
2272 ldd_argv
[argc
++] = prog_name
;
2273 ldd_argv
[argc
++] = (char *) 0;
2275 /* Trace if needed. */
2278 const char **p_argv
;
2281 for (p_argv
= &ldd_argv
[0]; (str
= *p_argv
) != (char *) 0; p_argv
++)
2282 fprintf (stderr
, " %s", str
);
2284 fprintf (stderr
, "\n");
2290 pex
= pex_init (PEX_USE_PIPES
, "collect2", NULL
);
2292 fatal_perror ("pex_init failed");
2294 errmsg
= pex_run (pex
, 0, ldd_file_name
, real_ldd_argv
, NULL
, NULL
, &err
);
2300 fatal_perror (errmsg
);
2306 int_handler
= (void (*) (int)) signal (SIGINT
, SIG_IGN
);
2308 quit_handler
= (void (*) (int)) signal (SIGQUIT
, SIG_IGN
);
2311 inf
= pex_read_output (pex
, 0);
2313 fatal_perror ("can't open ldd output");
2316 notice ("\nldd output with constructors/destructors.\n");
2318 /* Read each line of ldd output. */
2319 while (fgets (buf
, sizeof buf
, inf
) != (char *) 0)
2322 char *name
, *end
, *p
= buf
;
2324 /* Extract names of libraries and add to list. */
2325 PARSE_LDD_OUTPUT (p
);
2330 if (strncmp (name
, "not found", sizeof ("not found") - 1) == 0)
2331 fatal ("dynamic dependency %s not found", buf
);
2333 /* Find the end of the symbol name. */
2335 (ch2
= *end
) != '\0' && ch2
!= '\n' && !ISSPACE (ch2
) && ch2
!= '|';
2340 if (access (name
, R_OK
) == 0)
2341 add_to_list (&libraries
, name
);
2343 fatal ("unable to open dynamic dependency '%s'", buf
);
2346 fprintf (stderr
, "\t%s\n", buf
);
2349 fprintf (stderr
, "\n");
2351 do_wait (ldd_file_name
, pex
);
2353 signal (SIGINT
, int_handler
);
2355 signal (SIGQUIT
, quit_handler
);
2358 /* Now iterate through the library list adding their symbols to
2360 for (list
= libraries
.first
; list
; list
= list
->next
)
2361 scan_prog_file (list
->name
, PASS_LIB
);
2364 #endif /* LDD_SUFFIX */
2366 #endif /* OBJECT_FORMAT_NONE */
2370 * COFF specific stuff.
2373 #ifdef OBJECT_FORMAT_COFF
2375 #if defined (EXTENDED_COFF)
2377 # define GCC_SYMBOLS(X) (SYMHEADER(X).isymMax + SYMHEADER(X).iextMax)
2378 # define GCC_SYMENT SYMR
2379 # define GCC_OK_SYMBOL(X) ((X).st == stProc || (X).st == stGlobal)
2380 # define GCC_SYMINC(X) (1)
2381 # define GCC_SYMZERO(X) (SYMHEADER(X).isymMax)
2382 # define GCC_CHECK_HDR(X) (PSYMTAB(X) != 0)
2386 # define GCC_SYMBOLS(X) (HEADER(ldptr).f_nsyms)
2387 # define GCC_SYMENT SYMENT
2388 # if defined (C_WEAKEXT)
2389 # define GCC_OK_SYMBOL(X) \
2390 (((X).n_sclass == C_EXT || (X).n_sclass == C_WEAKEXT) && \
2391 ((X).n_scnum > N_UNDEF) && \
2393 || (((X).n_type & N_TMASK) == (DT_NON << N_BTSHFT) \
2394 || ((X).n_type & N_TMASK) == (DT_FCN << N_BTSHFT))))
2395 # define GCC_UNDEF_SYMBOL(X) \
2396 (((X).n_sclass == C_EXT || (X).n_sclass == C_WEAKEXT) && \
2397 ((X).n_scnum == N_UNDEF))
2399 # define GCC_OK_SYMBOL(X) \
2400 (((X).n_sclass == C_EXT) && \
2401 ((X).n_scnum > N_UNDEF) && \
2403 || (((X).n_type & N_TMASK) == (DT_NON << N_BTSHFT) \
2404 || ((X).n_type & N_TMASK) == (DT_FCN << N_BTSHFT))))
2405 # define GCC_UNDEF_SYMBOL(X) \
2406 (((X).n_sclass == C_EXT) && ((X).n_scnum == N_UNDEF))
2408 # define GCC_SYMINC(X) ((X).n_numaux+1)
2409 # define GCC_SYMZERO(X) 0
2411 /* 0757 = U803XTOCMAGIC (AIX 4.3) and 0767 = U64_TOCMAGIC (AIX V5) */
2413 # define GCC_CHECK_HDR(X) \
2414 ((HEADER (X).f_magic == U802TOCMAGIC && ! aix64_flag) \
2415 || (HEADER (X).f_magic == 0767 && aix64_flag))
2417 # define GCC_CHECK_HDR(X) \
2418 ((HEADER (X).f_magic == U802TOCMAGIC && ! aix64_flag) \
2419 || (HEADER (X).f_magic == 0757 && aix64_flag))
2424 #ifdef COLLECT_EXPORT_LIST
2425 /* Array of standard AIX libraries which should not
2426 be scanned for ctors/dtors. */
2427 static const char *const aix_std_libs
[] = {
2435 "/usr/lib/libc_r.a",
2436 "/usr/lib/libm_r.a",
2437 "/usr/lib/threads/libc.a",
2438 "/usr/ccs/lib/libc.a",
2439 "/usr/ccs/lib/libm.a",
2440 "/usr/ccs/lib/libc_r.a",
2441 "/usr/ccs/lib/libm_r.a",
2445 /* This function checks the filename and returns 1
2446 if this name matches the location of a standard AIX library. */
2447 static int ignore_library (const char *);
2449 ignore_library (const char *name
)
2451 const char *const *p
= &aix_std_libs
[0];
2452 while (*p
++ != NULL
)
2453 if (! strcmp (name
, *p
)) return 1;
2456 #endif /* COLLECT_EXPORT_LIST */
2458 #if defined (HAVE_DECL_LDGETNAME) && !HAVE_DECL_LDGETNAME
2459 extern char *ldgetname (LDFILE
*, GCC_SYMENT
*);
2462 /* COFF version to scan the name list of the loaded program for
2463 the symbols g++ uses for static constructors and destructors.
2465 The constructor table begins at __CTOR_LIST__ and contains a count
2466 of the number of pointers (or -1 if the constructors are built in a
2467 separate section by the linker), followed by the pointers to the
2468 constructor functions, terminated with a null pointer. The
2469 destructor table has the same format, and begins at __DTOR_LIST__. */
2472 scan_prog_file (const char *prog_name
, enum pass which_pass
)
2474 LDFILE
*ldptr
= NULL
;
2475 int sym_index
, sym_count
;
2478 if (which_pass
!= PASS_FIRST
&& which_pass
!= PASS_OBJ
)
2481 #ifdef COLLECT_EXPORT_LIST
2482 /* We do not need scanning for some standard C libraries. */
2483 if (which_pass
== PASS_FIRST
&& ignore_library (prog_name
))
2486 /* On AIX we have a loop, because there is not much difference
2487 between an object and an archive. This trick allows us to
2488 eliminate scan_libraries() function. */
2492 /* Some platforms (e.g. OSF4) declare ldopen as taking a
2493 non-const char * filename parameter, even though it will not
2494 modify that string. So we must cast away const-ness here,
2495 using CONST_CAST to prevent complaints from -Wcast-qual. */
2496 if ((ldptr
= ldopen (CONST_CAST (char *, prog_name
), ldptr
)) != NULL
)
2498 if (! MY_ISCOFF (HEADER (ldptr
).f_magic
))
2499 fatal ("%s: not a COFF file", prog_name
);
2501 if (GCC_CHECK_HDR (ldptr
))
2503 sym_count
= GCC_SYMBOLS (ldptr
);
2504 sym_index
= GCC_SYMZERO (ldptr
);
2506 #ifdef COLLECT_EXPORT_LIST
2507 /* Is current archive member a shared object? */
2508 is_shared
= HEADER (ldptr
).f_flags
& F_SHROBJ
;
2511 while (sym_index
< sym_count
)
2515 if (ldtbread (ldptr
, sym_index
, &symbol
) <= 0)
2517 sym_index
+= GCC_SYMINC (symbol
);
2519 if (GCC_OK_SYMBOL (symbol
))
2523 if ((name
= ldgetname (ldptr
, &symbol
)) == NULL
)
2524 continue; /* Should never happen. */
2526 #ifdef XCOFF_DEBUGGING_INFO
2527 /* All AIX function names have a duplicate entry
2528 beginning with a dot. */
2533 switch (is_ctor_dtor (name
))
2537 add_to_list (&constructors
, name
);
2538 #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
2539 if (which_pass
== PASS_OBJ
)
2540 add_to_list (&exports
, name
);
2546 add_to_list (&destructors
, name
);
2547 #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
2548 if (which_pass
== PASS_OBJ
)
2549 add_to_list (&exports
, name
);
2553 #ifdef COLLECT_EXPORT_LIST
2555 #ifndef LD_INIT_SWITCH
2557 add_to_list (&constructors
, name
);
2562 #ifndef LD_INIT_SWITCH
2564 add_to_list (&destructors
, name
);
2571 add_to_list (&frame_tables
, name
);
2572 #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
2573 if (which_pass
== PASS_OBJ
)
2574 add_to_list (&exports
, name
);
2578 default: /* not a constructor or destructor */
2579 #ifdef COLLECT_EXPORT_LIST
2580 /* Explicitly export all global symbols when
2581 building a shared object on AIX, but do not
2582 re-export symbols from another shared object
2583 and do not export symbols if the user
2584 provides an explicit export list. */
2585 if (shared_obj
&& !is_shared
2586 && which_pass
== PASS_OBJ
&& !export_flag
)
2587 add_to_list (&exports
, name
);
2593 #if !defined(EXTENDED_COFF)
2594 fprintf (stderr
, "\tsec=%d class=%d type=%s%o %s\n",
2595 symbol
.n_scnum
, symbol
.n_sclass
,
2596 (symbol
.n_type
? "0" : ""), symbol
.n_type
,
2600 "\tiss = %5d, value = %5ld, index = %5d, name = %s\n",
2601 symbol
.iss
, (long) symbol
.value
, symbol
.index
, name
);
2606 #ifdef COLLECT_EXPORT_LIST
2609 /* If archive contains both 32-bit and 64-bit objects,
2610 we want to skip objects in other mode so mismatch normal. */
2612 fprintf (stderr
, "%s : magic=%o aix64=%d mismatch\n",
2613 prog_name
, HEADER (ldptr
).f_magic
, aix64_flag
);
2619 fatal ("%s: cannot open as COFF file", prog_name
);
2621 #ifdef COLLECT_EXPORT_LIST
2622 /* On AIX loop continues while there are more members in archive. */
2624 while (ldclose (ldptr
) == FAILURE
);
2626 /* Otherwise we simply close ldptr. */
2627 (void) ldclose(ldptr
);
2630 #endif /* OBJECT_FORMAT_COFF */
2632 #ifdef COLLECT_EXPORT_LIST
2633 /* Given a library name without "lib" prefix, this function
2634 returns a full library name including a path. */
2636 resolve_lib_name (const char *name
)
2640 /* Library extensions for AIX dynamic linking. */
2641 const char * const libexts
[2] = {"a", "so"};
2643 for (i
= 0; libpaths
[i
]; i
++)
2644 if (libpaths
[i
]->max_len
> l
)
2645 l
= libpaths
[i
]->max_len
;
2647 lib_buf
= XNEWVEC (char, l
+ strlen(name
) + 10);
2649 for (i
= 0; libpaths
[i
]; i
++)
2651 struct prefix_list
*list
= libpaths
[i
]->plist
;
2652 for (; list
; list
= list
->next
)
2654 /* The following lines are needed because path_prefix list
2655 may contain directories both with trailing '/' and
2658 if (list
->prefix
[strlen(list
->prefix
)-1] != '/')
2660 for (j
= 0; j
< 2; j
++)
2662 sprintf (lib_buf
, "%s%slib%s.%s",
2663 list
->prefix
, p
, name
,
2664 libexts
[(j
+ aixrtl_flag
) % 2]);
2665 if (debug
) fprintf (stderr
, "searching for: %s\n", lib_buf
);
2666 if (file_exists (lib_buf
))
2668 if (debug
) fprintf (stderr
, "found: %s\n", lib_buf
);
2675 fprintf (stderr
, "not found\n");
2677 fatal ("library lib%s not found", name
);
2680 #endif /* COLLECT_EXPORT_LIST */