* gcc.dg/torture/tls/tls-reload-1.c: Add tls options.
[official-gcc.git] / gcc / collect2.c
blob49c40301d6ca605cd290bb47d1ac029303ea70ef
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, 2010, 2011, 2012
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
15 version.
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
20 for more details.
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. */
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include "filenames.h"
34 #include "file-find.h"
36 /* TARGET_64BIT may be defined to use driver specific functionality. */
37 #undef TARGET_64BIT
38 #define TARGET_64BIT TARGET_64BIT_DEFAULT
40 #ifndef LIBRARY_PATH_ENV
41 #define LIBRARY_PATH_ENV "LIBRARY_PATH"
42 #endif
44 #define COLLECT
46 #include "collect2.h"
47 #include "collect2-aix.h"
48 #include "diagnostic.h"
49 #include "demangle.h"
50 #include "obstack.h"
51 #include "intl.h"
52 #include "version.h"
54 /* On certain systems, we have code that works by scanning the object file
55 directly. But this code uses system-specific header files and library
56 functions, so turn it off in a cross-compiler. Likewise, the names of
57 the utilities are not correct for a cross-compiler; we have to hope that
58 cross-versions are in the proper directories. */
60 #ifdef CROSS_DIRECTORY_STRUCTURE
61 #ifndef CROSS_AIX_SUPPORT
62 #undef OBJECT_FORMAT_COFF
63 #endif
64 #undef MD_EXEC_PREFIX
65 #undef REAL_LD_FILE_NAME
66 #undef REAL_NM_FILE_NAME
67 #undef REAL_STRIP_FILE_NAME
68 #endif
70 /* If we cannot use a special method, use the ordinary one:
71 run nm to find what symbols are present.
72 In a cross-compiler, this means you need a cross nm,
73 but that is not quite as unpleasant as special headers. */
75 #if !defined (OBJECT_FORMAT_COFF)
76 #define OBJECT_FORMAT_NONE
77 #endif
79 #ifdef OBJECT_FORMAT_COFF
81 #ifndef CROSS_DIRECTORY_STRUCTURE
82 #include <a.out.h>
83 #include <ar.h>
85 #ifdef UMAX
86 #include <sgs.h>
87 #endif
89 /* Many versions of ldfcn.h define these. */
90 #ifdef FREAD
91 #undef FREAD
92 #undef FWRITE
93 #endif
95 #include <ldfcn.h>
96 #endif
98 /* Some systems have an ISCOFF macro, but others do not. In some cases
99 the macro may be wrong. MY_ISCOFF is defined in tm.h files for machines
100 that either do not have an ISCOFF macro in /usr/include or for those
101 where it is wrong. */
103 #ifndef MY_ISCOFF
104 #define MY_ISCOFF(X) ISCOFF (X)
105 #endif
107 #endif /* OBJECT_FORMAT_COFF */
109 #ifdef OBJECT_FORMAT_NONE
111 /* Default flags to pass to nm. */
112 #ifndef NM_FLAGS
113 #define NM_FLAGS "-n"
114 #endif
116 #endif /* OBJECT_FORMAT_NONE */
118 /* Some systems use __main in a way incompatible with its use in gcc, in these
119 cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
120 give the same symbol without quotes for an alternative entry point. */
121 #ifndef NAME__MAIN
122 #define NAME__MAIN "__main"
123 #endif
125 /* This must match tree.h. */
126 #define DEFAULT_INIT_PRIORITY 65535
128 #ifndef COLLECT_SHARED_INIT_FUNC
129 #define COLLECT_SHARED_INIT_FUNC(STREAM, FUNC) \
130 fprintf ((STREAM), "void _GLOBAL__DI() {\n\t%s();\n}\n", (FUNC))
131 #endif
132 #ifndef COLLECT_SHARED_FINI_FUNC
133 #define COLLECT_SHARED_FINI_FUNC(STREAM, FUNC) \
134 fprintf ((STREAM), "void _GLOBAL__DD() {\n\t%s();\n}\n", (FUNC))
135 #endif
137 #ifdef LDD_SUFFIX
138 #define SCAN_LIBRARIES
139 #endif
141 #ifndef SHLIB_SUFFIX
142 #define SHLIB_SUFFIX ".so"
143 #endif
145 #ifdef USE_COLLECT2
146 int do_collecting = 1;
147 #else
148 int do_collecting = 0;
149 #endif
151 /* Cook up an always defined indication of whether we proceed the
152 "EXPORT_LIST" way. */
154 #ifdef COLLECT_EXPORT_LIST
155 #define DO_COLLECT_EXPORT_LIST 1
156 #else
157 #define DO_COLLECT_EXPORT_LIST 0
158 #endif
160 /* Nonzero if we should suppress the automatic demangling of identifiers
161 in linker error messages. Set from COLLECT_NO_DEMANGLE. */
162 int no_demangle;
164 /* Linked lists of constructor and destructor names. */
166 struct id
168 struct id *next;
169 int sequence;
170 char name[1];
173 struct head
175 struct id *first;
176 struct id *last;
177 int number;
180 bool vflag; /* true if -v or --version */
181 static int rflag; /* true if -r */
182 static int strip_flag; /* true if -s */
183 #ifdef COLLECT_EXPORT_LIST
184 static int export_flag; /* true if -bE */
185 static int aix64_flag; /* true if -b64 */
186 static int aixrtl_flag; /* true if -brtl */
187 #endif
189 enum lto_mode_d {
190 LTO_MODE_NONE, /* Not doing LTO. */
191 LTO_MODE_LTO, /* Normal LTO. */
192 LTO_MODE_WHOPR /* WHOPR. */
195 /* Current LTO mode. */
196 static enum lto_mode_d lto_mode = LTO_MODE_NONE;
198 bool debug; /* true if -debug */
199 bool helpflag; /* true if --help */
201 static int shared_obj; /* true if -shared */
203 static const char *c_file; /* <xxx>.c for constructor/destructor list. */
204 static const char *o_file; /* <xxx>.o for constructor/destructor list. */
205 #ifdef COLLECT_EXPORT_LIST
206 static const char *export_file; /* <xxx>.x for AIX export list. */
207 #endif
208 static char **lto_o_files; /* Output files for LTO. */
209 const char *ldout; /* File for ld stdout. */
210 const char *lderrout; /* File for ld stderr. */
211 static const char *output_file; /* Output file for ld. */
212 static const char *nm_file_name; /* pathname of nm */
213 #ifdef LDD_SUFFIX
214 static const char *ldd_file_name; /* pathname of ldd (or equivalent) */
215 #endif
216 static const char *strip_file_name; /* pathname of strip */
217 const char *c_file_name; /* pathname of gcc */
218 static char *initname, *fininame; /* names of init and fini funcs */
220 static struct head constructors; /* list of constructors found */
221 static struct head destructors; /* list of destructors found */
222 #ifdef COLLECT_EXPORT_LIST
223 static struct head exports; /* list of exported symbols */
224 #endif
225 static struct head frame_tables; /* list of frame unwind info tables */
227 static bool at_file_supplied; /* Whether to use @file arguments */
228 static char *response_file; /* Name of any current response file */
230 struct obstack temporary_obstack;
231 char * temporary_firstobj;
233 /* A string that must be prepended to a target OS path in order to find
234 it on the host system. */
235 #ifdef TARGET_SYSTEM_ROOT
236 static const char *target_system_root = TARGET_SYSTEM_ROOT;
237 #else
238 static const char *target_system_root = "";
239 #endif
241 /* Whether we may unlink the output file, which should be set as soon as we
242 know we have successfully produced it. This is typically useful to prevent
243 blindly attempting to unlink a read-only output that the target linker
244 would leave untouched. */
245 bool may_unlink_output_file = false;
247 #ifdef COLLECT_EXPORT_LIST
248 /* Lists to keep libraries to be scanned for global constructors/destructors. */
249 static struct head libs; /* list of libraries */
250 static struct path_prefix cmdline_lib_dirs; /* directories specified with -L */
251 static struct path_prefix libpath_lib_dirs; /* directories in LIBPATH */
252 static struct path_prefix *libpaths[3] = {&cmdline_lib_dirs,
253 &libpath_lib_dirs, NULL};
254 #endif
256 /* List of names of object files containing LTO information.
257 These are a subset of the object file names appearing on the
258 command line, and must be identical, in the sense of pointer
259 equality, with the names passed to maybe_run_lto_and_relink(). */
261 struct lto_object
263 const char *name; /* Name of object file. */
264 struct lto_object *next; /* Next in linked list. */
267 struct lto_object_list
269 struct lto_object *first; /* First list element. */
270 struct lto_object *last; /* Last list element. */
273 static struct lto_object_list lto_objects;
275 /* Special kinds of symbols that a name may denote. */
277 typedef enum {
278 SYM_REGULAR = 0, /* nothing special */
280 SYM_CTOR = 1, /* constructor */
281 SYM_DTOR = 2, /* destructor */
282 SYM_INIT = 3, /* shared object routine that calls all the ctors */
283 SYM_FINI = 4, /* shared object routine that calls all the dtors */
284 SYM_DWEH = 5 /* DWARF exception handling table */
285 } symkind;
287 static symkind is_ctor_dtor (const char *);
289 static void handler (int);
290 static void do_wait (const char *, struct pex_obj *);
291 static void fork_execute (const char *, char **);
292 static void maybe_unlink (const char *);
293 static void maybe_unlink_list (char **);
294 static void add_to_list (struct head *, const char *);
295 static int extract_init_priority (const char *);
296 static void sort_ids (struct head *);
297 static void write_list (FILE *, const char *, struct id *);
298 #ifdef COLLECT_EXPORT_LIST
299 static void dump_list (FILE *, const char *, struct id *);
300 #endif
301 #if 0
302 static void dump_prefix_list (FILE *, const char *, struct prefix_list *);
303 #endif
304 static void write_list_with_asm (FILE *, const char *, struct id *);
305 static void write_c_file (FILE *, const char *);
306 static void write_c_file_stat (FILE *, const char *);
307 #ifndef LD_INIT_SWITCH
308 static void write_c_file_glob (FILE *, const char *);
309 #endif
310 #ifdef SCAN_LIBRARIES
311 static void scan_libraries (const char *);
312 #endif
313 #ifdef COLLECT_EXPORT_LIST
314 #if 0
315 static int is_in_list (const char *, struct id *);
316 #endif
317 static void write_aix_file (FILE *, struct id *);
318 static char *resolve_lib_name (const char *);
319 #endif
320 static char *extract_string (const char **);
321 static void post_ld_pass (bool);
322 static void process_args (int *argcp, char **argv);
324 /* Enumerations describing which pass this is for scanning the
325 program file ... */
327 typedef enum {
328 PASS_FIRST, /* without constructors */
329 PASS_OBJ, /* individual objects */
330 PASS_LIB, /* looking for shared libraries */
331 PASS_SECOND, /* with constructors linked in */
332 PASS_LTOINFO /* looking for objects with LTO info */
333 } scanpass;
335 /* ... and which kinds of symbols are to be considered. */
337 enum scanfilter_masks {
338 SCAN_NOTHING = 0,
340 SCAN_CTOR = 1 << SYM_CTOR,
341 SCAN_DTOR = 1 << SYM_DTOR,
342 SCAN_INIT = 1 << SYM_INIT,
343 SCAN_FINI = 1 << SYM_FINI,
344 SCAN_DWEH = 1 << SYM_DWEH,
345 SCAN_ALL = ~0
348 /* This type is used for parameters and variables which hold
349 combinations of the flags in enum scanfilter_masks. */
350 typedef int scanfilter;
352 /* Scan the name list of the loaded program for the symbols g++ uses for
353 static constructors and destructors.
355 The SCANPASS argument tells which collect processing pass this is for and
356 the SCANFILTER argument tells which kinds of symbols to consider in this
357 pass. Symbols of a special kind not in the filter mask are considered as
358 regular ones.
360 The constructor table begins at __CTOR_LIST__ and contains a count of the
361 number of pointers (or -1 if the constructors are built in a separate
362 section by the linker), followed by the pointers to the constructor
363 functions, terminated with a null pointer. The destructor table has the
364 same format, and begins at __DTOR_LIST__. */
366 static void scan_prog_file (const char *, scanpass, scanfilter);
369 /* Delete tempfiles and exit function. */
371 void
372 collect_exit (int status)
374 if (c_file != 0 && c_file[0])
375 maybe_unlink (c_file);
377 if (o_file != 0 && o_file[0])
378 maybe_unlink (o_file);
380 #ifdef COLLECT_EXPORT_LIST
381 if (export_file != 0 && export_file[0])
382 maybe_unlink (export_file);
383 #endif
385 if (lto_o_files)
386 maybe_unlink_list (lto_o_files);
388 if (ldout != 0 && ldout[0])
390 dump_ld_file (ldout, stdout);
391 maybe_unlink (ldout);
394 if (lderrout != 0 && lderrout[0])
396 dump_ld_file (lderrout, stderr);
397 maybe_unlink (lderrout);
400 if (status != 0 && output_file != 0 && output_file[0])
401 maybe_unlink (output_file);
403 if (response_file)
404 maybe_unlink (response_file);
406 exit (status);
410 /* Notify user of a non-error. */
411 void
412 notice (const char *cmsgid, ...)
414 va_list ap;
416 va_start (ap, cmsgid);
417 vfprintf (stderr, _(cmsgid), ap);
418 va_end (ap);
421 /* Notify user of a non-error, without translating the format string. */
422 void
423 notice_translated (const char *cmsgid, ...)
425 va_list ap;
427 va_start (ap, cmsgid);
428 vfprintf (stderr, cmsgid, ap);
429 va_end (ap);
432 static void
433 handler (int signo)
435 if (c_file != 0 && c_file[0])
436 maybe_unlink (c_file);
438 if (o_file != 0 && o_file[0])
439 maybe_unlink (o_file);
441 if (ldout != 0 && ldout[0])
442 maybe_unlink (ldout);
444 if (lderrout != 0 && lderrout[0])
445 maybe_unlink (lderrout);
447 #ifdef COLLECT_EXPORT_LIST
448 if (export_file != 0 && export_file[0])
449 maybe_unlink (export_file);
450 #endif
452 if (lto_o_files)
453 maybe_unlink_list (lto_o_files);
455 if (response_file)
456 maybe_unlink (response_file);
458 signal (signo, SIG_DFL);
459 raise (signo);
464 file_exists (const char *name)
466 return access (name, R_OK) == 0;
469 /* Parse a reasonable subset of shell quoting syntax. */
471 static char *
472 extract_string (const char **pp)
474 const char *p = *pp;
475 int backquote = 0;
476 int inside = 0;
478 for (;;)
480 char c = *p;
481 if (c == '\0')
482 break;
483 ++p;
484 if (backquote)
485 obstack_1grow (&temporary_obstack, c);
486 else if (! inside && c == ' ')
487 break;
488 else if (! inside && c == '\\')
489 backquote = 1;
490 else if (c == '\'')
491 inside = !inside;
492 else
493 obstack_1grow (&temporary_obstack, c);
496 obstack_1grow (&temporary_obstack, '\0');
497 *pp = p;
498 return XOBFINISH (&temporary_obstack, char *);
501 void
502 dump_ld_file (const char *name, FILE *to)
504 FILE *stream = fopen (name, "r");
506 if (stream == 0)
507 return;
508 while (1)
510 int c;
511 while (c = getc (stream),
512 c != EOF && (ISIDNUM (c) || c == '$' || c == '.'))
513 obstack_1grow (&temporary_obstack, c);
514 if (obstack_object_size (&temporary_obstack) > 0)
516 const char *word, *p;
517 char *result;
518 obstack_1grow (&temporary_obstack, '\0');
519 word = XOBFINISH (&temporary_obstack, const char *);
521 if (*word == '.')
522 ++word, putc ('.', to);
523 p = word;
524 if (!strncmp (p, USER_LABEL_PREFIX, strlen (USER_LABEL_PREFIX)))
525 p += strlen (USER_LABEL_PREFIX);
527 #ifdef HAVE_LD_DEMANGLE
528 result = 0;
529 #else
530 if (no_demangle)
531 result = 0;
532 else
533 result = cplus_demangle (p, DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE);
534 #endif
536 if (result)
538 int diff;
539 fputs (result, to);
541 diff = strlen (word) - strlen (result);
542 while (diff > 0 && c == ' ')
543 --diff, putc (' ', to);
544 if (diff < 0 && c == ' ')
546 while (diff < 0 && c == ' ')
547 ++diff, c = getc (stream);
548 if (!ISSPACE (c))
550 /* Make sure we output at least one space, or
551 the demangled symbol name will run into
552 whatever text follows. */
553 putc (' ', to);
557 free (result);
559 else
560 fputs (word, to);
562 fflush (to);
563 obstack_free (&temporary_obstack, temporary_firstobj);
565 if (c == EOF)
566 break;
567 putc (c, to);
569 fclose (stream);
572 /* Return the kind of symbol denoted by name S. */
574 static symkind
575 is_ctor_dtor (const char *s)
577 struct names { const char *const name; const int len; symkind ret;
578 const int two_underscores; };
580 const struct names *p;
581 int ch;
582 const char *orig_s = s;
584 static const struct names special[] = {
585 #ifndef NO_DOLLAR_IN_LABEL
586 { "GLOBAL__I$", sizeof ("GLOBAL__I$")-1, SYM_CTOR, 0 },
587 { "GLOBAL__D$", sizeof ("GLOBAL__D$")-1, SYM_DTOR, 0 },
588 #else
589 #ifndef NO_DOT_IN_LABEL
590 { "GLOBAL__I.", sizeof ("GLOBAL__I.")-1, SYM_CTOR, 0 },
591 { "GLOBAL__D.", sizeof ("GLOBAL__D.")-1, SYM_DTOR, 0 },
592 #endif /* NO_DOT_IN_LABEL */
593 #endif /* NO_DOLLAR_IN_LABEL */
594 { "GLOBAL__I_", sizeof ("GLOBAL__I_")-1, SYM_CTOR, 0 },
595 { "GLOBAL__D_", sizeof ("GLOBAL__D_")-1, SYM_DTOR, 0 },
596 { "GLOBAL__F_", sizeof ("GLOBAL__F_")-1, SYM_DWEH, 0 },
597 { "GLOBAL__FI_", sizeof ("GLOBAL__FI_")-1, SYM_INIT, 0 },
598 { "GLOBAL__FD_", sizeof ("GLOBAL__FD_")-1, SYM_FINI, 0 },
599 { NULL, 0, SYM_REGULAR, 0 }
602 while ((ch = *s) == '_')
603 ++s;
605 if (s == orig_s)
606 return SYM_REGULAR;
608 for (p = &special[0]; p->len > 0; p++)
610 if (ch == p->name[0]
611 && (!p->two_underscores || ((s - orig_s) >= 2))
612 && strncmp(s, p->name, p->len) == 0)
614 return p->ret;
617 return SYM_REGULAR;
620 /* We maintain two prefix lists: one from COMPILER_PATH environment variable
621 and one from the PATH variable. */
623 static struct path_prefix cpath, path;
625 #ifdef CROSS_DIRECTORY_STRUCTURE
626 /* This is the name of the target machine. We use it to form the name
627 of the files to execute. */
629 static const char *const target_machine = TARGET_MACHINE;
630 #endif
632 /* Search for NAME using prefix list PPREFIX. We only look for executable
633 files.
635 Return 0 if not found, otherwise return its name, allocated with malloc. */
637 #ifdef OBJECT_FORMAT_NONE
639 /* Add an entry for the object file NAME to object file list LIST.
640 New entries are added at the end of the list. The original pointer
641 value of NAME is preserved, i.e., no string copy is performed. */
643 static void
644 add_lto_object (struct lto_object_list *list, const char *name)
646 struct lto_object *n = XNEW (struct lto_object);
647 n->name = name;
648 n->next = NULL;
650 if (list->last)
651 list->last->next = n;
652 else
653 list->first = n;
655 list->last = n;
657 #endif /* OBJECT_FORMAT_NONE */
660 /* Perform a link-time recompilation and relink if any of the object
661 files contain LTO info. The linker command line LTO_LD_ARGV
662 represents the linker command that would produce a final executable
663 without the use of LTO. OBJECT_LST is a vector of object file names
664 appearing in LTO_LD_ARGV that are to be considered for link-time
665 recompilation, where OBJECT is a pointer to the last valid element.
666 (This awkward convention avoids an impedance mismatch with the
667 usage of similarly-named variables in main().) The elements of
668 OBJECT_LST must be identical, i.e., pointer equal, to the
669 corresponding arguments in LTO_LD_ARGV.
671 Upon entry, at least one linker run has been performed without the
672 use of any LTO info that might be present. Any recompilations
673 necessary for template instantiations have been performed, and
674 initializer/finalizer tables have been created if needed and
675 included in the linker command line LTO_LD_ARGV. If any of the
676 object files contain LTO info, we run the LTO back end on all such
677 files, and perform the final link with the LTO back end output
678 substituted for the LTO-optimized files. In some cases, a final
679 link with all link-time generated code has already been performed,
680 so there is no need to relink if no LTO info is found. In other
681 cases, our caller has not produced the final executable, and is
682 relying on us to perform the required link whether LTO info is
683 present or not. In that case, the FORCE argument should be true.
684 Note that the linker command line argument LTO_LD_ARGV passed into
685 this function may be modified in place. */
687 static void
688 maybe_run_lto_and_relink (char **lto_ld_argv, char **object_lst,
689 const char **object, bool force)
691 const char **object_file = CONST_CAST2 (const char **, char **, object_lst);
693 int num_lto_c_args = 1; /* Allow space for the terminating NULL. */
695 while (object_file < object)
697 /* If file contains LTO info, add it to the list of LTO objects. */
698 scan_prog_file (*object_file++, PASS_LTOINFO, SCAN_ALL);
700 /* Increment the argument count by the number of object file arguments
701 we will add. An upper bound suffices, so just count all of the
702 object files regardless of whether they contain LTO info. */
703 num_lto_c_args++;
706 if (lto_objects.first)
708 char **lto_c_argv;
709 const char **lto_c_ptr;
710 char **p;
711 char **lto_o_ptr;
712 struct lto_object *list;
713 char *lto_wrapper = getenv ("COLLECT_LTO_WRAPPER");
714 struct pex_obj *pex;
715 const char *prog = "lto-wrapper";
716 int lto_ld_argv_size = 0;
717 char **out_lto_ld_argv;
718 int out_lto_ld_argv_size;
719 size_t num_files;
721 if (!lto_wrapper)
722 fatal_error ("COLLECT_LTO_WRAPPER must be set");
724 num_lto_c_args++;
726 /* There is at least one object file containing LTO info,
727 so we need to run the LTO back end and relink.
729 To do so we build updated ld arguments with first
730 LTO object replaced by all partitions and other LTO
731 objects removed. */
733 lto_c_argv = (char **) xcalloc (sizeof (char *), num_lto_c_args);
734 lto_c_ptr = CONST_CAST2 (const char **, char **, lto_c_argv);
736 *lto_c_ptr++ = lto_wrapper;
738 /* Add LTO objects to the wrapper command line. */
739 for (list = lto_objects.first; list; list = list->next)
740 *lto_c_ptr++ = list->name;
742 *lto_c_ptr = NULL;
744 /* Run the LTO back end. */
745 pex = collect_execute (prog, lto_c_argv, NULL, NULL, PEX_SEARCH);
747 int c;
748 FILE *stream;
749 size_t i;
750 char *start, *end;
752 stream = pex_read_output (pex, 0);
753 gcc_assert (stream);
755 num_files = 0;
756 while ((c = getc (stream)) != EOF)
758 obstack_1grow (&temporary_obstack, c);
759 if (c == '\n')
760 ++num_files;
763 lto_o_files = XNEWVEC (char *, num_files + 1);
764 lto_o_files[num_files] = NULL;
765 start = XOBFINISH (&temporary_obstack, char *);
766 for (i = 0; i < num_files; ++i)
768 end = start;
769 while (*end != '\n')
770 ++end;
771 *end = '\0';
773 lto_o_files[i] = xstrdup (start);
775 start = end + 1;
778 obstack_free (&temporary_obstack, temporary_firstobj);
780 do_wait (prog, pex);
781 pex = NULL;
783 /* Compute memory needed for new LD arguments. At most number of original arguemtns
784 plus number of partitions. */
785 for (lto_ld_argv_size = 0; lto_ld_argv[lto_ld_argv_size]; lto_ld_argv_size++)
787 out_lto_ld_argv = XCNEWVEC(char *, num_files + lto_ld_argv_size + 1);
788 out_lto_ld_argv_size = 0;
790 /* After running the LTO back end, we will relink, substituting
791 the LTO output for the object files that we submitted to the
792 LTO. Here, we modify the linker command line for the relink. */
794 /* Copy all arguments until we find first LTO file. */
795 p = lto_ld_argv;
796 while (*p != NULL)
798 for (list = lto_objects.first; list; list = list->next)
799 if (*p == list->name) /* Note test for pointer equality! */
800 break;
801 if (list)
802 break;
803 out_lto_ld_argv[out_lto_ld_argv_size++] = *p++;
806 /* Now insert all LTO partitions. */
807 lto_o_ptr = lto_o_files;
808 while (*lto_o_ptr)
809 out_lto_ld_argv[out_lto_ld_argv_size++] = *lto_o_ptr++;
811 /* ... and copy the rest. */
812 while (*p != NULL)
814 for (list = lto_objects.first; list; list = list->next)
815 if (*p == list->name) /* Note test for pointer equality! */
816 break;
817 if (!list)
818 out_lto_ld_argv[out_lto_ld_argv_size++] = *p;
819 p++;
821 out_lto_ld_argv[out_lto_ld_argv_size++] = 0;
823 /* Run the linker again, this time replacing the object files
824 optimized by the LTO with the temporary file generated by the LTO. */
825 fork_execute ("ld", out_lto_ld_argv);
826 post_ld_pass (true);
827 free (lto_ld_argv);
829 maybe_unlink_list (lto_o_files);
831 else if (force)
833 /* Our caller is relying on us to do the link
834 even though there is no LTO back end work to be done. */
835 fork_execute ("ld", lto_ld_argv);
836 post_ld_pass (false);
840 /* Main program. */
843 main (int argc, char **argv)
845 static const char *const ld_suffix = "ld";
846 static const char *const plugin_ld_suffix = PLUGIN_LD_SUFFIX;
847 static const char *const real_ld_suffix = "real-ld";
848 static const char *const collect_ld_suffix = "collect-ld";
849 static const char *const nm_suffix = "nm";
850 static const char *const gnm_suffix = "gnm";
851 #ifdef LDD_SUFFIX
852 static const char *const ldd_suffix = LDD_SUFFIX;
853 #endif
854 static const char *const strip_suffix = "strip";
855 static const char *const gstrip_suffix = "gstrip";
857 #ifdef CROSS_DIRECTORY_STRUCTURE
858 /* If we look for a program in the compiler directories, we just use
859 the short name, since these directories are already system-specific.
860 But it we look for a program in the system directories, we need to
861 qualify the program name with the target machine. */
863 const char *const full_ld_suffix =
864 concat(target_machine, "-", ld_suffix, NULL);
865 const char *const full_plugin_ld_suffix =
866 concat(target_machine, "-", plugin_ld_suffix, NULL);
867 const char *const full_nm_suffix =
868 concat (target_machine, "-", nm_suffix, NULL);
869 const char *const full_gnm_suffix =
870 concat (target_machine, "-", gnm_suffix, NULL);
871 #ifdef LDD_SUFFIX
872 const char *const full_ldd_suffix =
873 concat (target_machine, "-", ldd_suffix, NULL);
874 #endif
875 const char *const full_strip_suffix =
876 concat (target_machine, "-", strip_suffix, NULL);
877 const char *const full_gstrip_suffix =
878 concat (target_machine, "-", gstrip_suffix, NULL);
879 #else
880 const char *const full_ld_suffix = ld_suffix;
881 const char *const full_plugin_ld_suffix = plugin_ld_suffix;
882 const char *const full_nm_suffix = nm_suffix;
883 const char *const full_gnm_suffix = gnm_suffix;
884 #ifdef LDD_SUFFIX
885 const char *const full_ldd_suffix = ldd_suffix;
886 #endif
887 const char *const full_strip_suffix = strip_suffix;
888 const char *const full_gstrip_suffix = gstrip_suffix;
889 #endif /* CROSS_DIRECTORY_STRUCTURE */
891 const char *arg;
892 FILE *outf;
893 #ifdef COLLECT_EXPORT_LIST
894 FILE *exportf;
895 #endif
896 const char *ld_file_name;
897 const char *p;
898 char **c_argv;
899 const char **c_ptr;
900 char **ld1_argv;
901 const char **ld1;
902 bool use_plugin = false;
904 /* The kinds of symbols we will have to consider when scanning the
905 outcome of a first pass link. This is ALL to start with, then might
906 be adjusted before getting to the first pass link per se, typically on
907 AIX where we perform an early scan of objects and libraries to fetch
908 the list of global ctors/dtors and make sure they are not garbage
909 collected. */
910 scanfilter ld1_filter = SCAN_ALL;
912 char **ld2_argv;
913 const char **ld2;
914 char **object_lst;
915 const char **object;
916 #ifdef TARGET_AIX_VERSION
917 int object_nbr = argc;
918 #endif
919 int first_file;
920 int num_c_args;
921 char **old_argv;
923 p = argv[0] + strlen (argv[0]);
924 while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
925 --p;
926 progname = p;
928 xmalloc_set_program_name (progname);
930 old_argv = argv;
931 expandargv (&argc, &argv);
932 if (argv != old_argv)
933 at_file_supplied = 1;
935 process_args (&argc, argv);
937 num_c_args = argc + 9;
939 #ifndef HAVE_LD_DEMANGLE
940 no_demangle = !! getenv ("COLLECT_NO_DEMANGLE");
942 /* Suppress demangling by the real linker, which may be broken. */
943 putenv (xstrdup ("COLLECT_NO_DEMANGLE=1"));
944 #endif
946 #if defined (COLLECT2_HOST_INITIALIZATION)
947 /* Perform system dependent initialization, if necessary. */
948 COLLECT2_HOST_INITIALIZATION;
949 #endif
951 #ifdef SIGCHLD
952 /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
953 receive the signal. A different setting is inheritable */
954 signal (SIGCHLD, SIG_DFL);
955 #endif
957 /* Unlock the stdio streams. */
958 unlock_std_streams ();
960 gcc_init_libintl ();
962 diagnostic_initialize (global_dc, 0);
964 /* Do not invoke xcalloc before this point, since locale needs to be
965 set first, in case a diagnostic is issued. */
967 ld1_argv = XCNEWVEC (char *, argc + 4);
968 ld1 = CONST_CAST2 (const char **, char **, ld1_argv);
969 ld2_argv = XCNEWVEC (char *, argc + 11);
970 ld2 = CONST_CAST2 (const char **, char **, ld2_argv);
971 object_lst = XCNEWVEC (char *, argc);
972 object = CONST_CAST2 (const char **, char **, object_lst);
974 #ifdef DEBUG
975 debug = 1;
976 #endif
978 /* Parse command line early for instances of -debug. This allows
979 the debug flag to be set before functions like find_a_file()
980 are called. We also look for the -flto or -flto-partition=none flag to know
981 what LTO mode we are in. */
983 int i;
984 bool no_partition = false;
986 for (i = 1; argv[i] != NULL; i ++)
988 if (! strcmp (argv[i], "-debug"))
989 debug = true;
990 else if (! strcmp (argv[i], "-flto-partition=none"))
991 no_partition = true;
992 else if ((! strncmp (argv[i], "-flto=", 6)
993 || ! strcmp (argv[i], "-flto")) && ! use_plugin)
994 lto_mode = LTO_MODE_WHOPR;
995 else if (!strncmp (argv[i], "-fno-lto", 8))
996 lto_mode = LTO_MODE_NONE;
997 else if (! strcmp (argv[i], "-plugin"))
999 use_plugin = true;
1000 lto_mode = LTO_MODE_NONE;
1002 #ifdef COLLECT_EXPORT_LIST
1003 /* since -brtl, -bexport, -b64 are not position dependent
1004 also check for them here */
1005 if ((argv[i][0] == '-') && (argv[i][1] == 'b'))
1007 arg = argv[i];
1008 /* We want to disable automatic exports on AIX when user
1009 explicitly puts an export list in command line */
1010 if (arg[2] == 'E' || strncmp (&arg[2], "export", 6) == 0)
1011 export_flag = 1;
1012 else if (arg[2] == '6' && arg[3] == '4')
1013 aix64_flag = 1;
1014 else if (arg[2] == 'r' && arg[3] == 't' && arg[4] == 'l')
1015 aixrtl_flag = 1;
1017 #endif
1019 vflag = debug;
1020 find_file_set_debug (debug);
1021 if (no_partition && lto_mode == LTO_MODE_WHOPR)
1022 lto_mode = LTO_MODE_LTO;
1025 #ifndef DEFAULT_A_OUT_NAME
1026 output_file = "a.out";
1027 #else
1028 output_file = DEFAULT_A_OUT_NAME;
1029 #endif
1031 obstack_begin (&temporary_obstack, 0);
1032 temporary_firstobj = (char *) obstack_alloc (&temporary_obstack, 0);
1034 #ifndef HAVE_LD_DEMANGLE
1035 current_demangling_style = auto_demangling;
1036 #endif
1037 p = getenv ("COLLECT_GCC_OPTIONS");
1038 while (p && *p)
1040 const char *q = extract_string (&p);
1041 if (*q == '-' && (q[1] == 'm' || q[1] == 'f'))
1042 num_c_args++;
1044 obstack_free (&temporary_obstack, temporary_firstobj);
1046 /* -fno-profile-arcs -fno-test-coverage -fno-branch-probabilities
1047 -fno-exceptions -w -fno-whole-program */
1048 num_c_args += 6;
1050 c_argv = XCNEWVEC (char *, num_c_args);
1051 c_ptr = CONST_CAST2 (const char **, char **, c_argv);
1053 if (argc < 2)
1054 fatal_error ("no arguments");
1056 #ifdef SIGQUIT
1057 if (signal (SIGQUIT, SIG_IGN) != SIG_IGN)
1058 signal (SIGQUIT, handler);
1059 #endif
1060 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
1061 signal (SIGINT, handler);
1062 #ifdef SIGALRM
1063 if (signal (SIGALRM, SIG_IGN) != SIG_IGN)
1064 signal (SIGALRM, handler);
1065 #endif
1066 #ifdef SIGHUP
1067 if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
1068 signal (SIGHUP, handler);
1069 #endif
1070 if (signal (SIGSEGV, SIG_IGN) != SIG_IGN)
1071 signal (SIGSEGV, handler);
1072 #ifdef SIGBUS
1073 if (signal (SIGBUS, SIG_IGN) != SIG_IGN)
1074 signal (SIGBUS, handler);
1075 #endif
1077 /* Extract COMPILER_PATH and PATH into our prefix list. */
1078 prefix_from_env ("COMPILER_PATH", &cpath);
1079 prefix_from_env ("PATH", &path);
1081 /* Try to discover a valid linker/nm/strip to use. */
1083 /* Maybe we know the right file to use (if not cross). */
1084 ld_file_name = 0;
1085 #ifdef DEFAULT_LINKER
1086 if (access (DEFAULT_LINKER, X_OK) == 0)
1087 ld_file_name = DEFAULT_LINKER;
1088 if (ld_file_name == 0)
1089 #endif
1090 #ifdef REAL_LD_FILE_NAME
1091 ld_file_name = find_a_file (&path, REAL_LD_FILE_NAME);
1092 if (ld_file_name == 0)
1093 #endif
1094 /* Search the (target-specific) compiler dirs for ld'. */
1095 ld_file_name = find_a_file (&cpath, real_ld_suffix);
1096 /* Likewise for `collect-ld'. */
1097 if (ld_file_name == 0)
1098 ld_file_name = find_a_file (&cpath, collect_ld_suffix);
1099 /* Search the compiler directories for `ld'. We have protection against
1100 recursive calls in find_a_file. */
1101 if (ld_file_name == 0)
1102 ld_file_name = find_a_file (&cpath,
1103 use_plugin
1104 ? plugin_ld_suffix
1105 : ld_suffix);
1106 /* Search the ordinary system bin directories
1107 for `ld' (if native linking) or `TARGET-ld' (if cross). */
1108 if (ld_file_name == 0)
1109 ld_file_name = find_a_file (&path,
1110 use_plugin
1111 ? full_plugin_ld_suffix
1112 : full_ld_suffix);
1114 #ifdef REAL_NM_FILE_NAME
1115 nm_file_name = find_a_file (&path, REAL_NM_FILE_NAME);
1116 if (nm_file_name == 0)
1117 #endif
1118 nm_file_name = find_a_file (&cpath, gnm_suffix);
1119 if (nm_file_name == 0)
1120 nm_file_name = find_a_file (&path, full_gnm_suffix);
1121 if (nm_file_name == 0)
1122 nm_file_name = find_a_file (&cpath, nm_suffix);
1123 if (nm_file_name == 0)
1124 nm_file_name = find_a_file (&path, full_nm_suffix);
1126 #ifdef LDD_SUFFIX
1127 ldd_file_name = find_a_file (&cpath, ldd_suffix);
1128 if (ldd_file_name == 0)
1129 ldd_file_name = find_a_file (&path, full_ldd_suffix);
1130 #endif
1132 #ifdef REAL_STRIP_FILE_NAME
1133 strip_file_name = find_a_file (&path, REAL_STRIP_FILE_NAME);
1134 if (strip_file_name == 0)
1135 #endif
1136 strip_file_name = find_a_file (&cpath, gstrip_suffix);
1137 if (strip_file_name == 0)
1138 strip_file_name = find_a_file (&path, full_gstrip_suffix);
1139 if (strip_file_name == 0)
1140 strip_file_name = find_a_file (&cpath, strip_suffix);
1141 if (strip_file_name == 0)
1142 strip_file_name = find_a_file (&path, full_strip_suffix);
1144 /* Determine the full path name of the C compiler to use. */
1145 c_file_name = getenv ("COLLECT_GCC");
1146 if (c_file_name == 0)
1148 #ifdef CROSS_DIRECTORY_STRUCTURE
1149 c_file_name = concat (target_machine, "-gcc", NULL);
1150 #else
1151 c_file_name = "gcc";
1152 #endif
1155 p = find_a_file (&cpath, c_file_name);
1157 /* Here it should be safe to use the system search path since we should have
1158 already qualified the name of the compiler when it is needed. */
1159 if (p == 0)
1160 p = find_a_file (&path, c_file_name);
1162 if (p)
1163 c_file_name = p;
1165 *ld1++ = *ld2++ = ld_file_name;
1167 /* Make temp file names. */
1168 c_file = make_temp_file (".c");
1169 o_file = make_temp_file (".o");
1170 #ifdef COLLECT_EXPORT_LIST
1171 export_file = make_temp_file (".x");
1172 #endif
1173 ldout = make_temp_file (".ld");
1174 lderrout = make_temp_file (".le");
1175 *c_ptr++ = c_file_name;
1176 *c_ptr++ = "-x";
1177 *c_ptr++ = "c";
1178 *c_ptr++ = "-c";
1179 *c_ptr++ = "-o";
1180 *c_ptr++ = o_file;
1182 #ifdef COLLECT_EXPORT_LIST
1183 /* Generate a list of directories from LIBPATH. */
1184 prefix_from_env ("LIBPATH", &libpath_lib_dirs);
1185 /* Add to this list also two standard directories where
1186 AIX loader always searches for libraries. */
1187 add_prefix (&libpath_lib_dirs, "/lib");
1188 add_prefix (&libpath_lib_dirs, "/usr/lib");
1189 #endif
1191 /* Get any options that the upper GCC wants to pass to the sub-GCC.
1193 AIX support needs to know if -shared has been specified before
1194 parsing commandline arguments. */
1196 p = getenv ("COLLECT_GCC_OPTIONS");
1197 while (p && *p)
1199 const char *q = extract_string (&p);
1200 if (*q == '-' && (q[1] == 'm' || q[1] == 'f'))
1201 *c_ptr++ = xstrdup (q);
1202 if (strcmp (q, "-EL") == 0 || strcmp (q, "-EB") == 0)
1203 *c_ptr++ = xstrdup (q);
1204 if (strcmp (q, "-shared") == 0)
1205 shared_obj = 1;
1206 if (*q == '-' && q[1] == 'B')
1208 *c_ptr++ = xstrdup (q);
1209 if (q[2] == 0)
1211 q = extract_string (&p);
1212 *c_ptr++ = xstrdup (q);
1216 obstack_free (&temporary_obstack, temporary_firstobj);
1217 *c_ptr++ = "-fno-profile-arcs";
1218 *c_ptr++ = "-fno-test-coverage";
1219 *c_ptr++ = "-fno-branch-probabilities";
1220 *c_ptr++ = "-fno-exceptions";
1221 *c_ptr++ = "-w";
1222 *c_ptr++ = "-fno-whole-program";
1224 /* !!! When GCC calls collect2,
1225 it does not know whether it is calling collect2 or ld.
1226 So collect2 cannot meaningfully understand any options
1227 except those ld understands.
1228 If you propose to make GCC pass some other option,
1229 just imagine what will happen if ld is really ld!!! */
1231 /* Parse arguments. Remember output file spec, pass the rest to ld. */
1232 /* After the first file, put in the c++ rt0. */
1234 first_file = 1;
1235 while ((arg = *++argv) != (char *) 0)
1237 *ld1++ = *ld2++ = arg;
1239 if (arg[0] == '-')
1241 switch (arg[1])
1243 case 'd':
1244 if (!strcmp (arg, "-debug"))
1246 /* Already parsed. */
1247 ld1--;
1248 ld2--;
1250 if (!strcmp (arg, "-dynamic-linker") && argv[1])
1252 ++argv;
1253 *ld1++ = *ld2++ = *argv;
1255 break;
1257 case 'f':
1258 if (strncmp (arg, "-flto", 5) == 0)
1260 #ifdef ENABLE_LTO
1261 /* Do not pass LTO flag to the linker. */
1262 ld1--;
1263 ld2--;
1264 #else
1265 error ("LTO support has not been enabled in this "
1266 "configuration");
1267 #endif
1269 #ifdef TARGET_AIX_VERSION
1270 else
1272 /* File containing a list of input files to process. */
1274 FILE *stream;
1275 char buf[MAXPATHLEN + 2];
1276 /* Number of additionnal object files. */
1277 int add_nbr = 0;
1278 /* Maximum of additionnal object files before vector
1279 expansion. */
1280 int add_max = 0;
1281 const char *list_filename = arg + 2;
1283 /* Accept -fFILENAME and -f FILENAME. */
1284 if (*list_filename == '\0' && argv[1])
1286 ++argv;
1287 list_filename = *argv;
1288 *ld1++ = *ld2++ = *argv;
1291 stream = fopen (list_filename, "r");
1292 if (stream == NULL)
1293 fatal_error ("can't open %s: %m", list_filename);
1295 while (fgets (buf, sizeof buf, stream) != NULL)
1297 /* Remove end of line. */
1298 int len = strlen (buf);
1299 if (len >= 1 && buf[len - 1] =='\n')
1300 buf[len - 1] = '\0';
1302 /* Put on object vector.
1303 Note: we only expanse vector here, so we must keep
1304 extra space for remaining arguments. */
1305 if (add_nbr >= add_max)
1307 int pos =
1308 object - CONST_CAST2 (const char **, char **,
1309 object_lst);
1310 add_max = (add_max == 0) ? 16 : add_max * 2;
1311 object_lst = XRESIZEVEC (char *, object_lst,
1312 object_nbr + add_max);
1313 object = CONST_CAST2 (const char **, char **,
1314 object_lst) + pos;
1315 object_nbr += add_max;
1317 *object++ = xstrdup (buf);
1318 add_nbr++;
1320 fclose (stream);
1322 #endif
1323 break;
1325 case 'l':
1326 if (first_file)
1328 /* place o_file BEFORE this argument! */
1329 first_file = 0;
1330 ld2--;
1331 *ld2++ = o_file;
1332 *ld2++ = arg;
1334 #ifdef COLLECT_EXPORT_LIST
1336 /* Resolving full library name. */
1337 const char *s = resolve_lib_name (arg+2);
1339 /* Saving a full library name. */
1340 add_to_list (&libs, s);
1342 #endif
1343 break;
1345 #ifdef COLLECT_EXPORT_LIST
1346 /* Saving directories where to search for libraries. */
1347 case 'L':
1348 add_prefix (&cmdline_lib_dirs, arg+2);
1349 break;
1350 #endif
1352 case 'o':
1353 if (arg[2] == '\0')
1354 output_file = *ld1++ = *ld2++ = *++argv;
1355 else
1356 output_file = &arg[2];
1357 break;
1359 case 'r':
1360 if (arg[2] == '\0')
1361 rflag = 1;
1362 break;
1364 case 's':
1365 if (arg[2] == '\0' && do_collecting)
1367 /* We must strip after the nm run, otherwise C++ linking
1368 will not work. Thus we strip in the second ld run, or
1369 else with strip if there is no second ld run. */
1370 strip_flag = 1;
1371 ld1--;
1373 break;
1375 case 'v':
1376 if (arg[2] == '\0')
1377 vflag = true;
1378 break;
1380 case '-':
1381 if (strcmp (arg, "--no-demangle") == 0)
1383 #ifndef HAVE_LD_DEMANGLE
1384 no_demangle = 1;
1385 ld1--;
1386 ld2--;
1387 #endif
1389 else if (strncmp (arg, "--demangle", 10) == 0)
1391 #ifndef HAVE_LD_DEMANGLE
1392 no_demangle = 0;
1393 if (arg[10] == '=')
1395 enum demangling_styles style
1396 = cplus_demangle_name_to_style (arg+11);
1397 if (style == unknown_demangling)
1398 error ("unknown demangling style '%s'", arg+11);
1399 else
1400 current_demangling_style = style;
1402 ld1--;
1403 ld2--;
1404 #endif
1406 else if (strncmp (arg, "--sysroot=", 10) == 0)
1407 target_system_root = arg + 10;
1408 else if (strcmp (arg, "--version") == 0)
1409 vflag = true;
1410 else if (strcmp (arg, "--help") == 0)
1411 helpflag = true;
1412 break;
1415 else if ((p = strrchr (arg, '.')) != (char *) 0
1416 && (strcmp (p, ".o") == 0 || strcmp (p, ".a") == 0
1417 || strcmp (p, ".so") == 0 || strcmp (p, ".lo") == 0
1418 || strcmp (p, ".obj") == 0))
1420 if (first_file)
1422 first_file = 0;
1423 if (p[1] == 'o')
1424 *ld2++ = o_file;
1425 else
1427 /* place o_file BEFORE this argument! */
1428 ld2--;
1429 *ld2++ = o_file;
1430 *ld2++ = arg;
1433 if (p[1] == 'o' || p[1] == 'l')
1434 *object++ = arg;
1435 #ifdef COLLECT_EXPORT_LIST
1436 /* libraries can be specified directly, i.e. without -l flag. */
1437 else
1439 /* Saving a full library name. */
1440 add_to_list (&libs, arg);
1442 #endif
1446 #ifdef COLLECT_EXPORT_LIST
1447 /* This is added only for debugging purposes. */
1448 if (debug)
1450 fprintf (stderr, "List of libraries:\n");
1451 dump_list (stderr, "\t", libs.first);
1454 /* The AIX linker will discard static constructors in object files if
1455 nothing else in the file is referenced, so look at them first. Unless
1456 we are building a shared object, ignore the eh frame tables, as we
1457 would otherwise reference them all, hence drag all the corresponding
1458 objects even if nothing else is referenced. */
1460 const char **export_object_lst
1461 = CONST_CAST2 (const char **, char **, object_lst);
1463 struct id *list = libs.first;
1465 /* Compute the filter to use from the current one, do scan, then adjust
1466 the "current" filter to remove what we just included here. This will
1467 control whether we need a first pass link later on or not, and what
1468 will remain to be scanned there. */
1470 scanfilter this_filter = ld1_filter;
1471 #if HAVE_AS_REF
1472 if (!shared_obj)
1473 this_filter &= ~SCAN_DWEH;
1474 #endif
1476 while (export_object_lst < object)
1477 scan_prog_file (*export_object_lst++, PASS_OBJ, this_filter);
1479 for (; list; list = list->next)
1480 scan_prog_file (list->name, PASS_FIRST, this_filter);
1482 ld1_filter = ld1_filter & ~this_filter;
1485 if (exports.first)
1487 char *buf = concat ("-bE:", export_file, NULL);
1489 *ld1++ = buf;
1490 *ld2++ = buf;
1492 exportf = fopen (export_file, "w");
1493 if (exportf == (FILE *) 0)
1494 fatal_error ("fopen %s: %m", export_file);
1495 write_aix_file (exportf, exports.first);
1496 if (fclose (exportf))
1497 fatal_error ("fclose %s: %m", export_file);
1499 #endif
1501 *c_ptr++ = c_file;
1502 *c_ptr = *ld1 = *object = (char *) 0;
1504 if (vflag)
1505 notice ("collect2 version %s\n", version_string);
1507 if (helpflag)
1509 printf ("Usage: collect2 [options]\n");
1510 printf (" Wrap linker and generate constructor code if needed.\n");
1511 printf (" Options:\n");
1512 printf (" -debug Enable debug output\n");
1513 printf (" --help Display this information\n");
1514 printf (" -v, --version Display this program's version number\n");
1515 printf ("\n");
1516 printf ("Overview: http://gcc.gnu.org/onlinedocs/gccint/Collect2.html\n");
1517 printf ("Report bugs: %s\n", bug_report_url);
1518 printf ("\n");
1521 if (debug)
1523 const char *ptr;
1524 fprintf (stderr, "ld_file_name = %s\n",
1525 (ld_file_name ? ld_file_name : "not found"));
1526 fprintf (stderr, "c_file_name = %s\n",
1527 (c_file_name ? c_file_name : "not found"));
1528 fprintf (stderr, "nm_file_name = %s\n",
1529 (nm_file_name ? nm_file_name : "not found"));
1530 #ifdef LDD_SUFFIX
1531 fprintf (stderr, "ldd_file_name = %s\n",
1532 (ldd_file_name ? ldd_file_name : "not found"));
1533 #endif
1534 fprintf (stderr, "strip_file_name = %s\n",
1535 (strip_file_name ? strip_file_name : "not found"));
1536 fprintf (stderr, "c_file = %s\n",
1537 (c_file ? c_file : "not found"));
1538 fprintf (stderr, "o_file = %s\n",
1539 (o_file ? o_file : "not found"));
1541 ptr = getenv ("COLLECT_GCC_OPTIONS");
1542 if (ptr)
1543 fprintf (stderr, "COLLECT_GCC_OPTIONS = %s\n", ptr);
1545 ptr = getenv ("COLLECT_GCC");
1546 if (ptr)
1547 fprintf (stderr, "COLLECT_GCC = %s\n", ptr);
1549 ptr = getenv ("COMPILER_PATH");
1550 if (ptr)
1551 fprintf (stderr, "COMPILER_PATH = %s\n", ptr);
1553 ptr = getenv (LIBRARY_PATH_ENV);
1554 if (ptr)
1555 fprintf (stderr, "%-20s= %s\n", LIBRARY_PATH_ENV, ptr);
1557 fprintf (stderr, "\n");
1560 /* Load the program, searching all libraries and attempting to provide
1561 undefined symbols from repository information.
1563 If -r or they will be run via some other method, do not build the
1564 constructor or destructor list, just return now. */
1566 bool early_exit
1567 = rflag || (! DO_COLLECT_EXPORT_LIST && ! do_collecting);
1569 /* Perform the first pass link now, if we're about to exit or if we need
1570 to scan for things we haven't collected yet before pursuing further.
1572 On AIX, the latter typically includes nothing for shared objects or
1573 frame tables for an executable, out of what the required early scan on
1574 objects and libraries has performed above. In the !shared_obj case, we
1575 expect the relevant tables to be dragged together with their associated
1576 functions from precise cross reference insertions by the compiler. */
1578 if (early_exit || ld1_filter != SCAN_NOTHING)
1579 do_tlink (ld1_argv, object_lst);
1581 if (early_exit)
1583 #ifdef COLLECT_EXPORT_LIST
1584 /* Make sure we delete the export file we may have created. */
1585 if (export_file != 0 && export_file[0])
1586 maybe_unlink (export_file);
1587 #endif
1588 if (lto_mode != LTO_MODE_NONE)
1589 maybe_run_lto_and_relink (ld1_argv, object_lst, object, false);
1590 else
1591 post_ld_pass (false);
1593 maybe_unlink (c_file);
1594 maybe_unlink (o_file);
1595 return 0;
1599 /* Unless we have done it all already, examine the namelist and search for
1600 static constructors and destructors to call. Write the constructor and
1601 destructor tables to a .s file and reload. */
1603 if (ld1_filter != SCAN_NOTHING)
1604 scan_prog_file (output_file, PASS_FIRST, ld1_filter);
1606 #ifdef SCAN_LIBRARIES
1607 scan_libraries (output_file);
1608 #endif
1610 if (debug)
1612 notice_translated (ngettext ("%d constructor found\n",
1613 "%d constructors found\n",
1614 constructors.number),
1615 constructors.number);
1616 notice_translated (ngettext ("%d destructor found\n",
1617 "%d destructors found\n",
1618 destructors.number),
1619 destructors.number);
1620 notice_translated (ngettext("%d frame table found\n",
1621 "%d frame tables found\n",
1622 frame_tables.number),
1623 frame_tables.number);
1626 /* If the scan exposed nothing of special interest, there's no need to
1627 generate the glue code and relink so return now. */
1629 if (constructors.number == 0 && destructors.number == 0
1630 && frame_tables.number == 0
1631 #if defined (SCAN_LIBRARIES) || defined (COLLECT_EXPORT_LIST)
1632 /* If we will be running these functions ourselves, we want to emit
1633 stubs into the shared library so that we do not have to relink
1634 dependent programs when we add static objects. */
1635 && ! shared_obj
1636 #endif
1639 /* Do tlink without additional code generation now if we didn't
1640 do it earlier for scanning purposes. */
1641 if (ld1_filter == SCAN_NOTHING)
1642 do_tlink (ld1_argv, object_lst);
1644 if (lto_mode)
1645 maybe_run_lto_and_relink (ld1_argv, object_lst, object, false);
1647 /* Strip now if it was requested on the command line. */
1648 if (strip_flag)
1650 char **real_strip_argv = XCNEWVEC (char *, 3);
1651 const char ** strip_argv = CONST_CAST2 (const char **, char **,
1652 real_strip_argv);
1654 strip_argv[0] = strip_file_name;
1655 strip_argv[1] = output_file;
1656 strip_argv[2] = (char *) 0;
1657 fork_execute ("strip", real_strip_argv);
1660 #ifdef COLLECT_EXPORT_LIST
1661 maybe_unlink (export_file);
1662 #endif
1663 post_ld_pass (false);
1665 maybe_unlink (c_file);
1666 maybe_unlink (o_file);
1667 return 0;
1670 /* Sort ctor and dtor lists by priority. */
1671 sort_ids (&constructors);
1672 sort_ids (&destructors);
1674 maybe_unlink(output_file);
1675 outf = fopen (c_file, "w");
1676 if (outf == (FILE *) 0)
1677 fatal_error ("fopen %s: %m", c_file);
1679 write_c_file (outf, c_file);
1681 if (fclose (outf))
1682 fatal_error ("fclose %s: %m", c_file);
1684 /* Tell the linker that we have initializer and finalizer functions. */
1685 #ifdef LD_INIT_SWITCH
1686 #ifdef COLLECT_EXPORT_LIST
1687 *ld2++ = concat (LD_INIT_SWITCH, ":", initname, ":", fininame, NULL);
1688 #else
1689 *ld2++ = LD_INIT_SWITCH;
1690 *ld2++ = initname;
1691 *ld2++ = LD_FINI_SWITCH;
1692 *ld2++ = fininame;
1693 #endif
1694 #endif
1696 #ifdef COLLECT_EXPORT_LIST
1697 if (shared_obj)
1699 /* If we did not add export flag to link arguments before, add it to
1700 second link phase now. No new exports should have been added. */
1701 if (! exports.first)
1702 *ld2++ = concat ("-bE:", export_file, NULL);
1704 #ifndef LD_INIT_SWITCH
1705 add_to_list (&exports, initname);
1706 add_to_list (&exports, fininame);
1707 add_to_list (&exports, "_GLOBAL__DI");
1708 add_to_list (&exports, "_GLOBAL__DD");
1709 #endif
1710 exportf = fopen (export_file, "w");
1711 if (exportf == (FILE *) 0)
1712 fatal_error ("fopen %s: %m", export_file);
1713 write_aix_file (exportf, exports.first);
1714 if (fclose (exportf))
1715 fatal_error ("fclose %s: %m", export_file);
1717 #endif
1719 /* End of arguments to second link phase. */
1720 *ld2 = (char*) 0;
1722 if (debug)
1724 fprintf (stderr, "\n========== output_file = %s, c_file = %s\n",
1725 output_file, c_file);
1726 write_c_file (stderr, "stderr");
1727 fprintf (stderr, "========== end of c_file\n\n");
1728 #ifdef COLLECT_EXPORT_LIST
1729 fprintf (stderr, "\n========== export_file = %s\n", export_file);
1730 write_aix_file (stderr, exports.first);
1731 fprintf (stderr, "========== end of export_file\n\n");
1732 #endif
1735 /* Assemble the constructor and destructor tables.
1736 Link the tables in with the rest of the program. */
1738 fork_execute ("gcc", c_argv);
1739 #ifdef COLLECT_EXPORT_LIST
1740 /* On AIX we must call tlink because of possible templates resolution. */
1741 do_tlink (ld2_argv, object_lst);
1743 if (lto_mode)
1744 maybe_run_lto_and_relink (ld2_argv, object_lst, object, false);
1745 #else
1746 /* Otherwise, simply call ld because tlink is already done. */
1747 if (lto_mode)
1748 maybe_run_lto_and_relink (ld2_argv, object_lst, object, true);
1749 else
1751 fork_execute ("ld", ld2_argv);
1752 post_ld_pass (false);
1755 /* Let scan_prog_file do any final mods (OSF/rose needs this for
1756 constructors/destructors in shared libraries. */
1757 scan_prog_file (output_file, PASS_SECOND, SCAN_ALL);
1758 #endif
1760 maybe_unlink (c_file);
1761 maybe_unlink (o_file);
1763 #ifdef COLLECT_EXPORT_LIST
1764 maybe_unlink (export_file);
1765 #endif
1767 return 0;
1771 /* Wait for a process to finish, and exit if a nonzero status is found. */
1774 collect_wait (const char *prog, struct pex_obj *pex)
1776 int status;
1778 if (!pex_get_status (pex, 1, &status))
1779 fatal_error ("can't get program status: %m");
1780 pex_free (pex);
1782 if (status)
1784 if (WIFSIGNALED (status))
1786 int sig = WTERMSIG (status);
1787 error ("%s terminated with signal %d [%s]%s",
1788 prog, sig, strsignal(sig),
1789 WCOREDUMP(status) ? ", core dumped" : "");
1790 collect_exit (FATAL_EXIT_CODE);
1793 if (WIFEXITED (status))
1794 return WEXITSTATUS (status);
1796 return 0;
1799 static void
1800 do_wait (const char *prog, struct pex_obj *pex)
1802 int ret = collect_wait (prog, pex);
1803 if (ret != 0)
1805 error ("%s returned %d exit status", prog, ret);
1806 collect_exit (ret);
1809 if (response_file)
1811 unlink (response_file);
1812 response_file = NULL;
1817 /* Execute a program, and wait for the reply. */
1819 struct pex_obj *
1820 collect_execute (const char *prog, char **argv, const char *outname,
1821 const char *errname, int flags)
1823 struct pex_obj *pex;
1824 const char *errmsg;
1825 int err;
1826 char *response_arg = NULL;
1827 char *response_argv[3] ATTRIBUTE_UNUSED;
1829 if (HAVE_GNU_LD && at_file_supplied && argv[0] != NULL)
1831 /* If using @file arguments, create a temporary file and put the
1832 contents of argv into it. Then change argv to an array corresponding
1833 to a single argument @FILE, where FILE is the temporary filename. */
1835 char **current_argv = argv + 1;
1836 char *argv0 = argv[0];
1837 int status;
1838 FILE *f;
1840 /* Note: we assume argv contains at least one element; this is
1841 checked above. */
1843 response_file = make_temp_file ("");
1845 f = fopen (response_file, "w");
1847 if (f == NULL)
1848 fatal_error ("could not open response file %s", response_file);
1850 status = writeargv (current_argv, f);
1852 if (status)
1853 fatal_error ("could not write to response file %s", response_file);
1855 status = fclose (f);
1857 if (EOF == status)
1858 fatal_error ("could not close response file %s", response_file);
1860 response_arg = concat ("@", response_file, NULL);
1861 response_argv[0] = argv0;
1862 response_argv[1] = response_arg;
1863 response_argv[2] = NULL;
1865 argv = response_argv;
1868 if (vflag || debug)
1870 char **p_argv;
1871 const char *str;
1873 if (argv[0])
1874 fprintf (stderr, "%s", argv[0]);
1875 else
1876 notice ("[cannot find %s]", prog);
1878 for (p_argv = &argv[1]; (str = *p_argv) != (char *) 0; p_argv++)
1879 fprintf (stderr, " %s", str);
1881 fprintf (stderr, "\n");
1884 fflush (stdout);
1885 fflush (stderr);
1887 /* If we cannot find a program we need, complain error. Do this here
1888 since we might not end up needing something that we could not find. */
1890 if (argv[0] == 0)
1891 fatal_error ("cannot find '%s'", prog);
1893 pex = pex_init (0, "collect2", NULL);
1894 if (pex == NULL)
1895 fatal_error ("pex_init failed: %m");
1897 errmsg = pex_run (pex, flags, argv[0], argv, outname,
1898 errname, &err);
1899 if (errmsg != NULL)
1901 if (err != 0)
1903 errno = err;
1904 fatal_error ("%s: %m", _(errmsg));
1906 else
1907 fatal_error (errmsg);
1910 free (response_arg);
1912 return pex;
1915 static void
1916 fork_execute (const char *prog, char **argv)
1918 struct pex_obj *pex;
1920 pex = collect_execute (prog, argv, NULL, NULL, PEX_LAST | PEX_SEARCH);
1921 do_wait (prog, pex);
1924 /* Unlink FILE unless we are debugging or this is the output_file
1925 and we may not unlink it. */
1927 static void
1928 maybe_unlink (const char *file)
1930 if (debug)
1932 notice ("[Leaving %s]\n", file);
1933 return;
1936 if (file == output_file && !may_unlink_output_file)
1937 return;
1939 unlink_if_ordinary (file);
1942 /* Call maybe_unlink on the NULL-terminated list, FILE_LIST. */
1944 static void
1945 maybe_unlink_list (char **file_list)
1947 char **tmp = file_list;
1949 while (*tmp)
1950 maybe_unlink (*(tmp++));
1954 static long sequence_number = 0;
1956 /* Add a name to a linked list. */
1958 static void
1959 add_to_list (struct head *head_ptr, const char *name)
1961 struct id *newid
1962 = (struct id *) xcalloc (sizeof (struct id) + strlen (name), 1);
1963 struct id *p;
1964 strcpy (newid->name, name);
1966 if (head_ptr->first)
1967 head_ptr->last->next = newid;
1968 else
1969 head_ptr->first = newid;
1971 /* Check for duplicate symbols. */
1972 for (p = head_ptr->first;
1973 strcmp (name, p->name) != 0;
1974 p = p->next)
1976 if (p != newid)
1978 head_ptr->last->next = 0;
1979 free (newid);
1980 return;
1983 newid->sequence = ++sequence_number;
1984 head_ptr->last = newid;
1985 head_ptr->number++;
1988 /* Grab the init priority number from an init function name that
1989 looks like "_GLOBAL_.I.12345.foo". */
1991 static int
1992 extract_init_priority (const char *name)
1994 int pos = 0, pri;
1996 while (name[pos] == '_')
1997 ++pos;
1998 pos += 10; /* strlen ("GLOBAL__X_") */
2000 /* Extract init_p number from ctor/dtor name. */
2001 pri = atoi (name + pos);
2002 return pri ? pri : DEFAULT_INIT_PRIORITY;
2005 /* Insertion sort the ids from ctor/dtor list HEAD_PTR in descending order.
2006 ctors will be run from right to left, dtors from left to right. */
2008 static void
2009 sort_ids (struct head *head_ptr)
2011 /* id holds the current element to insert. id_next holds the next
2012 element to insert. id_ptr iterates through the already sorted elements
2013 looking for the place to insert id. */
2014 struct id *id, *id_next, **id_ptr;
2016 id = head_ptr->first;
2018 /* We don't have any sorted elements yet. */
2019 head_ptr->first = NULL;
2021 for (; id; id = id_next)
2023 id_next = id->next;
2024 id->sequence = extract_init_priority (id->name);
2026 for (id_ptr = &(head_ptr->first); ; id_ptr = &((*id_ptr)->next))
2027 if (*id_ptr == NULL
2028 /* If the sequence numbers are the same, we put the id from the
2029 file later on the command line later in the list. */
2030 || id->sequence > (*id_ptr)->sequence
2031 /* Hack: do lexical compare, too.
2032 || (id->sequence == (*id_ptr)->sequence
2033 && strcmp (id->name, (*id_ptr)->name) > 0) */
2036 id->next = *id_ptr;
2037 *id_ptr = id;
2038 break;
2042 /* Now set the sequence numbers properly so write_c_file works. */
2043 for (id = head_ptr->first; id; id = id->next)
2044 id->sequence = ++sequence_number;
2047 /* Write: `prefix', the names on list LIST, `suffix'. */
2049 static void
2050 write_list (FILE *stream, const char *prefix, struct id *list)
2052 while (list)
2054 fprintf (stream, "%sx%d,\n", prefix, list->sequence);
2055 list = list->next;
2059 #ifdef COLLECT_EXPORT_LIST
2060 /* This function is really used only on AIX, but may be useful. */
2061 #if 0
2062 static int
2063 is_in_list (const char *prefix, struct id *list)
2065 while (list)
2067 if (!strcmp (prefix, list->name)) return 1;
2068 list = list->next;
2070 return 0;
2072 #endif
2073 #endif /* COLLECT_EXPORT_LIST */
2075 /* Added for debugging purpose. */
2076 #ifdef COLLECT_EXPORT_LIST
2077 static void
2078 dump_list (FILE *stream, const char *prefix, struct id *list)
2080 while (list)
2082 fprintf (stream, "%s%s,\n", prefix, list->name);
2083 list = list->next;
2086 #endif
2088 #if 0
2089 static void
2090 dump_prefix_list (FILE *stream, const char *prefix, struct prefix_list *list)
2092 while (list)
2094 fprintf (stream, "%s%s,\n", prefix, list->prefix);
2095 list = list->next;
2098 #endif
2100 static void
2101 write_list_with_asm (FILE *stream, const char *prefix, struct id *list)
2103 while (list)
2105 fprintf (stream, "%sx%d __asm__ (\"%s\");\n",
2106 prefix, list->sequence, list->name);
2107 list = list->next;
2111 /* Write out the constructor and destructor tables statically (for a shared
2112 object), along with the functions to execute them. */
2114 static void
2115 write_c_file_stat (FILE *stream, const char *name ATTRIBUTE_UNUSED)
2117 const char *p, *q;
2118 char *prefix, *r;
2119 int frames = (frame_tables.number > 0);
2121 /* Figure out name of output_file, stripping off .so version. */
2122 q = p = lbasename (output_file);
2124 while (q)
2126 q = strchr (q,'.');
2127 if (q == 0)
2129 q = p + strlen (p);
2130 break;
2132 else
2134 if (filename_ncmp (q, SHLIB_SUFFIX, strlen (SHLIB_SUFFIX)) == 0)
2136 q += strlen (SHLIB_SUFFIX);
2137 break;
2139 else
2140 q++;
2143 /* q points to null at end of the string (or . of the .so version) */
2144 prefix = XNEWVEC (char, q - p + 1);
2145 strncpy (prefix, p, q - p);
2146 prefix[q - p] = 0;
2147 for (r = prefix; *r; r++)
2148 if (!ISALNUM ((unsigned char)*r))
2149 *r = '_';
2150 if (debug)
2151 notice ("\nwrite_c_file - output name is %s, prefix is %s\n",
2152 output_file, prefix);
2154 initname = concat ("_GLOBAL__FI_", prefix, NULL);
2155 fininame = concat ("_GLOBAL__FD_", prefix, NULL);
2157 free (prefix);
2159 /* Write the tables as C code. */
2161 fprintf (stream, "static int count;\n");
2162 fprintf (stream, "typedef void entry_pt();\n");
2163 write_list_with_asm (stream, "extern entry_pt ", constructors.first);
2165 if (frames)
2167 write_list_with_asm (stream, "extern void *", frame_tables.first);
2169 fprintf (stream, "\tstatic void *frame_table[] = {\n");
2170 write_list (stream, "\t\t&", frame_tables.first);
2171 fprintf (stream, "\t0\n};\n");
2173 /* This must match what's in frame.h. */
2174 fprintf (stream, "struct object {\n");
2175 fprintf (stream, " void *pc_begin;\n");
2176 fprintf (stream, " void *pc_end;\n");
2177 fprintf (stream, " void *fde_begin;\n");
2178 fprintf (stream, " void *fde_array;\n");
2179 fprintf (stream, " __SIZE_TYPE__ count;\n");
2180 fprintf (stream, " struct object *next;\n");
2181 fprintf (stream, "};\n");
2183 fprintf (stream, "extern void __register_frame_info_table (void *, struct object *);\n");
2184 fprintf (stream, "extern void *__deregister_frame_info (void *);\n");
2186 fprintf (stream, "static void reg_frame () {\n");
2187 fprintf (stream, "\tstatic struct object ob;\n");
2188 fprintf (stream, "\t__register_frame_info_table (frame_table, &ob);\n");
2189 fprintf (stream, "\t}\n");
2191 fprintf (stream, "static void dereg_frame () {\n");
2192 fprintf (stream, "\t__deregister_frame_info (frame_table);\n");
2193 fprintf (stream, "\t}\n");
2196 fprintf (stream, "void %s() {\n", initname);
2197 if (constructors.number > 0 || frames)
2199 fprintf (stream, "\tstatic entry_pt *ctors[] = {\n");
2200 write_list (stream, "\t\t", constructors.first);
2201 if (frames)
2202 fprintf (stream, "\treg_frame,\n");
2203 fprintf (stream, "\t};\n");
2204 fprintf (stream, "\tentry_pt **p;\n");
2205 fprintf (stream, "\tif (count++ != 0) return;\n");
2206 fprintf (stream, "\tp = ctors + %d;\n", constructors.number + frames);
2207 fprintf (stream, "\twhile (p > ctors) (*--p)();\n");
2209 else
2210 fprintf (stream, "\t++count;\n");
2211 fprintf (stream, "}\n");
2212 write_list_with_asm (stream, "extern entry_pt ", destructors.first);
2213 fprintf (stream, "void %s() {\n", fininame);
2214 if (destructors.number > 0 || frames)
2216 fprintf (stream, "\tstatic entry_pt *dtors[] = {\n");
2217 write_list (stream, "\t\t", destructors.first);
2218 if (frames)
2219 fprintf (stream, "\tdereg_frame,\n");
2220 fprintf (stream, "\t};\n");
2221 fprintf (stream, "\tentry_pt **p;\n");
2222 fprintf (stream, "\tif (--count != 0) return;\n");
2223 fprintf (stream, "\tp = dtors;\n");
2224 fprintf (stream, "\twhile (p < dtors + %d) (*p++)();\n",
2225 destructors.number + frames);
2227 fprintf (stream, "}\n");
2229 if (shared_obj)
2231 COLLECT_SHARED_INIT_FUNC(stream, initname);
2232 COLLECT_SHARED_FINI_FUNC(stream, fininame);
2236 /* Write the constructor/destructor tables. */
2238 #ifndef LD_INIT_SWITCH
2239 static void
2240 write_c_file_glob (FILE *stream, const char *name ATTRIBUTE_UNUSED)
2242 /* Write the tables as C code. */
2244 int frames = (frame_tables.number > 0);
2246 fprintf (stream, "typedef void entry_pt();\n\n");
2248 write_list_with_asm (stream, "extern entry_pt ", constructors.first);
2250 if (frames)
2252 write_list_with_asm (stream, "extern void *", frame_tables.first);
2254 fprintf (stream, "\tstatic void *frame_table[] = {\n");
2255 write_list (stream, "\t\t&", frame_tables.first);
2256 fprintf (stream, "\t0\n};\n");
2258 /* This must match what's in frame.h. */
2259 fprintf (stream, "struct object {\n");
2260 fprintf (stream, " void *pc_begin;\n");
2261 fprintf (stream, " void *pc_end;\n");
2262 fprintf (stream, " void *fde_begin;\n");
2263 fprintf (stream, " void *fde_array;\n");
2264 fprintf (stream, " __SIZE_TYPE__ count;\n");
2265 fprintf (stream, " struct object *next;\n");
2266 fprintf (stream, "};\n");
2268 fprintf (stream, "extern void __register_frame_info_table (void *, struct object *);\n");
2269 fprintf (stream, "extern void *__deregister_frame_info (void *);\n");
2271 fprintf (stream, "static void reg_frame () {\n");
2272 fprintf (stream, "\tstatic struct object ob;\n");
2273 fprintf (stream, "\t__register_frame_info_table (frame_table, &ob);\n");
2274 fprintf (stream, "\t}\n");
2276 fprintf (stream, "static void dereg_frame () {\n");
2277 fprintf (stream, "\t__deregister_frame_info (frame_table);\n");
2278 fprintf (stream, "\t}\n");
2281 fprintf (stream, "\nentry_pt * __CTOR_LIST__[] = {\n");
2282 fprintf (stream, "\t(entry_pt *) %d,\n", constructors.number + frames);
2283 write_list (stream, "\t", constructors.first);
2284 if (frames)
2285 fprintf (stream, "\treg_frame,\n");
2286 fprintf (stream, "\t0\n};\n\n");
2288 write_list_with_asm (stream, "extern entry_pt ", destructors.first);
2290 fprintf (stream, "\nentry_pt * __DTOR_LIST__[] = {\n");
2291 fprintf (stream, "\t(entry_pt *) %d,\n", destructors.number + frames);
2292 write_list (stream, "\t", destructors.first);
2293 if (frames)
2294 fprintf (stream, "\tdereg_frame,\n");
2295 fprintf (stream, "\t0\n};\n\n");
2297 fprintf (stream, "extern entry_pt %s;\n", NAME__MAIN);
2298 fprintf (stream, "entry_pt *__main_reference = %s;\n\n", NAME__MAIN);
2300 #endif /* ! LD_INIT_SWITCH */
2302 static void
2303 write_c_file (FILE *stream, const char *name)
2305 #ifndef LD_INIT_SWITCH
2306 if (! shared_obj)
2307 write_c_file_glob (stream, name);
2308 else
2309 #endif
2310 write_c_file_stat (stream, name);
2313 #ifdef COLLECT_EXPORT_LIST
2314 static void
2315 write_aix_file (FILE *stream, struct id *list)
2317 for (; list; list = list->next)
2319 fputs (list->name, stream);
2320 putc ('\n', stream);
2323 #endif
2325 #ifdef OBJECT_FORMAT_NONE
2327 /* Check to make sure the file is an LTO object file. */
2329 static bool
2330 maybe_lto_object_file (const char *prog_name)
2332 FILE *f;
2333 unsigned char buf[4];
2334 int i;
2336 static unsigned char elfmagic[4] = { 0x7f, 'E', 'L', 'F' };
2337 static unsigned char coffmagic[2] = { 0x4c, 0x01 };
2338 static unsigned char coffmagic_x64[2] = { 0x64, 0x86 };
2339 static unsigned char machomagic[4][4] = {
2340 { 0xcf, 0xfa, 0xed, 0xfe },
2341 { 0xce, 0xfa, 0xed, 0xfe },
2342 { 0xfe, 0xed, 0xfa, 0xcf },
2343 { 0xfe, 0xed, 0xfa, 0xce }
2346 f = fopen (prog_name, "rb");
2347 if (f == NULL)
2348 return false;
2349 if (fread (buf, sizeof (buf), 1, f) != 1)
2350 buf[0] = 0;
2351 fclose (f);
2353 if (memcmp (buf, elfmagic, sizeof (elfmagic)) == 0
2354 || memcmp (buf, coffmagic, sizeof (coffmagic)) == 0
2355 || memcmp (buf, coffmagic_x64, sizeof (coffmagic_x64)) == 0)
2356 return true;
2357 for (i = 0; i < 4; i++)
2358 if (memcmp (buf, machomagic[i], sizeof (machomagic[i])) == 0)
2359 return true;
2361 return false;
2364 /* Generic version to scan the name list of the loaded program for
2365 the symbols g++ uses for static constructors and destructors. */
2367 static void
2368 scan_prog_file (const char *prog_name, scanpass which_pass,
2369 scanfilter filter)
2371 void (*int_handler) (int);
2372 #ifdef SIGQUIT
2373 void (*quit_handler) (int);
2374 #endif
2375 char *real_nm_argv[4];
2376 const char **nm_argv = CONST_CAST2 (const char **, char**, real_nm_argv);
2377 int argc = 0;
2378 struct pex_obj *pex;
2379 const char *errmsg;
2380 int err;
2381 char *p, buf[1024];
2382 FILE *inf;
2383 int found_lto = 0;
2385 if (which_pass == PASS_SECOND)
2386 return;
2388 /* LTO objects must be in a known format. This check prevents
2389 us from accepting an archive containing LTO objects, which
2390 gcc cannot currently handle. */
2391 if (which_pass == PASS_LTOINFO && !maybe_lto_object_file (prog_name))
2392 return;
2394 /* If we do not have an `nm', complain. */
2395 if (nm_file_name == 0)
2396 fatal_error ("cannot find 'nm'");
2398 nm_argv[argc++] = nm_file_name;
2399 if (NM_FLAGS[0] != '\0')
2400 nm_argv[argc++] = NM_FLAGS;
2402 nm_argv[argc++] = prog_name;
2403 nm_argv[argc++] = (char *) 0;
2405 /* Trace if needed. */
2406 if (vflag)
2408 const char **p_argv;
2409 const char *str;
2411 for (p_argv = &nm_argv[0]; (str = *p_argv) != (char *) 0; p_argv++)
2412 fprintf (stderr, " %s", str);
2414 fprintf (stderr, "\n");
2417 fflush (stdout);
2418 fflush (stderr);
2420 pex = pex_init (PEX_USE_PIPES, "collect2", NULL);
2421 if (pex == NULL)
2422 fatal_error ("pex_init failed: %m");
2424 errmsg = pex_run (pex, 0, nm_file_name, real_nm_argv, NULL, HOST_BIT_BUCKET,
2425 &err);
2426 if (errmsg != NULL)
2428 if (err != 0)
2430 errno = err;
2431 fatal_error ("%s: %m", _(errmsg));
2433 else
2434 fatal_error (errmsg);
2437 int_handler = (void (*) (int)) signal (SIGINT, SIG_IGN);
2438 #ifdef SIGQUIT
2439 quit_handler = (void (*) (int)) signal (SIGQUIT, SIG_IGN);
2440 #endif
2442 inf = pex_read_output (pex, 0);
2443 if (inf == NULL)
2444 fatal_error ("can't open nm output: %m");
2446 if (debug)
2448 if (which_pass == PASS_LTOINFO)
2449 fprintf (stderr, "\nnm output with LTO info marker symbol.\n");
2450 else
2451 fprintf (stderr, "\nnm output with constructors/destructors.\n");
2454 /* Read each line of nm output. */
2455 while (fgets (buf, sizeof buf, inf) != (char *) 0)
2457 int ch, ch2;
2458 char *name, *end;
2460 if (debug)
2461 fprintf (stderr, "\t%s\n", buf);
2463 if (which_pass == PASS_LTOINFO)
2465 if (found_lto)
2466 continue;
2468 /* Look for the LTO info marker symbol, and add filename to
2469 the LTO objects list if found. */
2470 for (p = buf; (ch = *p) != '\0' && ch != '\n'; p++)
2471 if (ch == ' ' && p[1] == '_' && p[2] == '_'
2472 && (strncmp (p + (p[3] == '_' ? 2 : 1), "__gnu_lto_v1", 12) == 0)
2473 && ISSPACE (p[p[3] == '_' ? 14 : 13]))
2475 add_lto_object (&lto_objects, prog_name);
2477 /* We need to read all the input, so we can't just
2478 return here. But we can avoid useless work. */
2479 found_lto = 1;
2481 break;
2484 continue;
2487 /* If it contains a constructor or destructor name, add the name
2488 to the appropriate list unless this is a kind of symbol we're
2489 not supposed to even consider. */
2491 for (p = buf; (ch = *p) != '\0' && ch != '\n' && ch != '_'; p++)
2492 if (ch == ' ' && p[1] == 'U' && p[2] == ' ')
2493 break;
2495 if (ch != '_')
2496 continue;
2498 name = p;
2499 /* Find the end of the symbol name.
2500 Do not include `|', because Encore nm can tack that on the end. */
2501 for (end = p; (ch2 = *end) != '\0' && !ISSPACE (ch2) && ch2 != '|';
2502 end++)
2503 continue;
2506 *end = '\0';
2507 switch (is_ctor_dtor (name))
2509 case SYM_CTOR:
2510 if (! (filter & SCAN_CTOR))
2511 break;
2512 if (which_pass != PASS_LIB)
2513 add_to_list (&constructors, name);
2514 break;
2516 case SYM_DTOR:
2517 if (! (filter & SCAN_DTOR))
2518 break;
2519 if (which_pass != PASS_LIB)
2520 add_to_list (&destructors, name);
2521 break;
2523 case SYM_INIT:
2524 if (! (filter & SCAN_INIT))
2525 break;
2526 if (which_pass != PASS_LIB)
2527 fatal_error ("init function found in object %s", prog_name);
2528 #ifndef LD_INIT_SWITCH
2529 add_to_list (&constructors, name);
2530 #endif
2531 break;
2533 case SYM_FINI:
2534 if (! (filter & SCAN_FINI))
2535 break;
2536 if (which_pass != PASS_LIB)
2537 fatal_error ("fini function found in object %s", prog_name);
2538 #ifndef LD_FINI_SWITCH
2539 add_to_list (&destructors, name);
2540 #endif
2541 break;
2543 case SYM_DWEH:
2544 if (! (filter & SCAN_DWEH))
2545 break;
2546 if (which_pass != PASS_LIB)
2547 add_to_list (&frame_tables, name);
2548 break;
2550 default: /* not a constructor or destructor */
2551 continue;
2555 if (debug)
2556 fprintf (stderr, "\n");
2558 do_wait (nm_file_name, pex);
2560 signal (SIGINT, int_handler);
2561 #ifdef SIGQUIT
2562 signal (SIGQUIT, quit_handler);
2563 #endif
2566 #ifdef LDD_SUFFIX
2568 /* Use the List Dynamic Dependencies program to find shared libraries that
2569 the output file depends upon and their initialization/finalization
2570 routines, if any. */
2572 static void
2573 scan_libraries (const char *prog_name)
2575 static struct head libraries; /* list of shared libraries found */
2576 struct id *list;
2577 void (*int_handler) (int);
2578 #ifdef SIGQUIT
2579 void (*quit_handler) (int);
2580 #endif
2581 char *real_ldd_argv[4];
2582 const char **ldd_argv = CONST_CAST2 (const char **, char **, real_ldd_argv);
2583 int argc = 0;
2584 struct pex_obj *pex;
2585 const char *errmsg;
2586 int err;
2587 char buf[1024];
2588 FILE *inf;
2590 /* If we do not have an `ldd', complain. */
2591 if (ldd_file_name == 0)
2593 error ("cannot find 'ldd'");
2594 return;
2597 ldd_argv[argc++] = ldd_file_name;
2598 ldd_argv[argc++] = prog_name;
2599 ldd_argv[argc++] = (char *) 0;
2601 /* Trace if needed. */
2602 if (vflag)
2604 const char **p_argv;
2605 const char *str;
2607 for (p_argv = &ldd_argv[0]; (str = *p_argv) != (char *) 0; p_argv++)
2608 fprintf (stderr, " %s", str);
2610 fprintf (stderr, "\n");
2613 fflush (stdout);
2614 fflush (stderr);
2616 pex = pex_init (PEX_USE_PIPES, "collect2", NULL);
2617 if (pex == NULL)
2618 fatal_error ("pex_init failed: %m");
2620 errmsg = pex_run (pex, 0, ldd_file_name, real_ldd_argv, NULL, NULL, &err);
2621 if (errmsg != NULL)
2623 if (err != 0)
2625 errno = err;
2626 fatal_error ("%s: %m", _(errmsg));
2628 else
2629 fatal_error (errmsg);
2632 int_handler = (void (*) (int)) signal (SIGINT, SIG_IGN);
2633 #ifdef SIGQUIT
2634 quit_handler = (void (*) (int)) signal (SIGQUIT, SIG_IGN);
2635 #endif
2637 inf = pex_read_output (pex, 0);
2638 if (inf == NULL)
2639 fatal_error ("can't open ldd output: %m");
2641 if (debug)
2642 notice ("\nldd output with constructors/destructors.\n");
2644 /* Read each line of ldd output. */
2645 while (fgets (buf, sizeof buf, inf) != (char *) 0)
2647 int ch2;
2648 char *name, *end, *p = buf;
2650 /* Extract names of libraries and add to list. */
2651 PARSE_LDD_OUTPUT (p);
2652 if (p == 0)
2653 continue;
2655 name = p;
2656 if (strncmp (name, "not found", sizeof ("not found") - 1) == 0)
2657 fatal_error ("dynamic dependency %s not found", buf);
2659 /* Find the end of the symbol name. */
2660 for (end = p;
2661 (ch2 = *end) != '\0' && ch2 != '\n' && !ISSPACE (ch2) && ch2 != '|';
2662 end++)
2663 continue;
2664 *end = '\0';
2666 if (access (name, R_OK) == 0)
2667 add_to_list (&libraries, name);
2668 else
2669 fatal_error ("unable to open dynamic dependency '%s'", buf);
2671 if (debug)
2672 fprintf (stderr, "\t%s\n", buf);
2674 if (debug)
2675 fprintf (stderr, "\n");
2677 do_wait (ldd_file_name, pex);
2679 signal (SIGINT, int_handler);
2680 #ifdef SIGQUIT
2681 signal (SIGQUIT, quit_handler);
2682 #endif
2684 /* Now iterate through the library list adding their symbols to
2685 the list. */
2686 for (list = libraries.first; list; list = list->next)
2687 scan_prog_file (list->name, PASS_LIB, SCAN_ALL);
2690 #endif /* LDD_SUFFIX */
2692 #endif /* OBJECT_FORMAT_NONE */
2696 * COFF specific stuff.
2699 #ifdef OBJECT_FORMAT_COFF
2701 #if defined (EXTENDED_COFF)
2703 # define GCC_SYMBOLS(X) (SYMHEADER(X).isymMax + SYMHEADER(X).iextMax)
2704 # define GCC_SYMENT SYMR
2705 # define GCC_OK_SYMBOL(X) ((X).st == stProc || (X).st == stGlobal)
2706 # define GCC_SYMINC(X) (1)
2707 # define GCC_SYMZERO(X) (SYMHEADER(X).isymMax)
2708 # define GCC_CHECK_HDR(X) (PSYMTAB(X) != 0)
2710 #else
2712 # define GCC_SYMBOLS(X) (HEADER(ldptr).f_nsyms)
2713 # define GCC_SYMENT SYMENT
2714 # if defined (C_WEAKEXT)
2715 # define GCC_OK_SYMBOL(X) \
2716 (((X).n_sclass == C_EXT || (X).n_sclass == C_WEAKEXT) && \
2717 ((X).n_scnum > N_UNDEF) && \
2718 (aix64_flag \
2719 || (((X).n_type & N_TMASK) == (DT_NON << N_BTSHFT) \
2720 || ((X).n_type & N_TMASK) == (DT_FCN << N_BTSHFT))))
2721 # define GCC_UNDEF_SYMBOL(X) \
2722 (((X).n_sclass == C_EXT || (X).n_sclass == C_WEAKEXT) && \
2723 ((X).n_scnum == N_UNDEF))
2724 # else
2725 # define GCC_OK_SYMBOL(X) \
2726 (((X).n_sclass == C_EXT) && \
2727 ((X).n_scnum > N_UNDEF) && \
2728 (aix64_flag \
2729 || (((X).n_type & N_TMASK) == (DT_NON << N_BTSHFT) \
2730 || ((X).n_type & N_TMASK) == (DT_FCN << N_BTSHFT))))
2731 # define GCC_UNDEF_SYMBOL(X) \
2732 (((X).n_sclass == C_EXT) && ((X).n_scnum == N_UNDEF))
2733 # endif
2734 # define GCC_SYMINC(X) ((X).n_numaux+1)
2735 # define GCC_SYMZERO(X) 0
2737 /* 0757 = U803XTOCMAGIC (AIX 4.3) and 0767 = U64_TOCMAGIC (AIX V5) */
2738 #if TARGET_AIX_VERSION >= 51
2739 # define GCC_CHECK_HDR(X) \
2740 ((HEADER (X).f_magic == U802TOCMAGIC && ! aix64_flag) \
2741 || (HEADER (X).f_magic == 0767 && aix64_flag))
2742 #else
2743 # define GCC_CHECK_HDR(X) \
2744 ((HEADER (X).f_magic == U802TOCMAGIC && ! aix64_flag) \
2745 || (HEADER (X).f_magic == 0757 && aix64_flag))
2746 #endif
2748 #endif
2750 #ifdef COLLECT_EXPORT_LIST
2751 /* Array of standard AIX libraries which should not
2752 be scanned for ctors/dtors. */
2753 static const char *const aix_std_libs[] = {
2754 "/unix",
2755 "/lib/libc.a",
2756 "/lib/libm.a",
2757 "/lib/libc_r.a",
2758 "/lib/libm_r.a",
2759 "/usr/lib/libc.a",
2760 "/usr/lib/libm.a",
2761 "/usr/lib/libc_r.a",
2762 "/usr/lib/libm_r.a",
2763 "/usr/lib/threads/libc.a",
2764 "/usr/ccs/lib/libc.a",
2765 "/usr/ccs/lib/libm.a",
2766 "/usr/ccs/lib/libc_r.a",
2767 "/usr/ccs/lib/libm_r.a",
2768 NULL
2771 /* This function checks the filename and returns 1
2772 if this name matches the location of a standard AIX library. */
2773 static int ignore_library (const char *);
2774 static int
2775 ignore_library (const char *name)
2777 const char *const *p;
2778 size_t length;
2780 if (target_system_root[0] != '\0')
2782 length = strlen (target_system_root);
2783 if (strncmp (name, target_system_root, length) != 0)
2784 return 0;
2785 name += length;
2787 for (p = &aix_std_libs[0]; *p != NULL; ++p)
2788 if (strcmp (name, *p) == 0)
2789 return 1;
2790 return 0;
2792 #endif /* COLLECT_EXPORT_LIST */
2794 #if defined (HAVE_DECL_LDGETNAME) && !HAVE_DECL_LDGETNAME
2795 extern char *ldgetname (LDFILE *, GCC_SYMENT *);
2796 #endif
2798 /* COFF version to scan the name list of the loaded program for
2799 the symbols g++ uses for static constructors and destructors. */
2801 static void
2802 scan_prog_file (const char *prog_name, scanpass which_pass,
2803 scanfilter filter)
2805 LDFILE *ldptr = NULL;
2806 int sym_index, sym_count;
2807 int is_shared = 0;
2809 if (which_pass != PASS_FIRST && which_pass != PASS_OBJ)
2810 return;
2812 #ifdef COLLECT_EXPORT_LIST
2813 /* We do not need scanning for some standard C libraries. */
2814 if (which_pass == PASS_FIRST && ignore_library (prog_name))
2815 return;
2817 /* On AIX we have a loop, because there is not much difference
2818 between an object and an archive. This trick allows us to
2819 eliminate scan_libraries() function. */
2822 #endif
2823 /* Some platforms (e.g. OSF4) declare ldopen as taking a
2824 non-const char * filename parameter, even though it will not
2825 modify that string. So we must cast away const-ness here,
2826 using CONST_CAST to prevent complaints from -Wcast-qual. */
2827 if ((ldptr = ldopen (CONST_CAST (char *, prog_name), ldptr)) != NULL)
2829 if (! MY_ISCOFF (HEADER (ldptr).f_magic))
2830 fatal_error ("%s: not a COFF file", prog_name);
2832 if (GCC_CHECK_HDR (ldptr))
2834 sym_count = GCC_SYMBOLS (ldptr);
2835 sym_index = GCC_SYMZERO (ldptr);
2837 #ifdef COLLECT_EXPORT_LIST
2838 /* Is current archive member a shared object? */
2839 is_shared = HEADER (ldptr).f_flags & F_SHROBJ;
2840 #endif
2842 while (sym_index < sym_count)
2844 GCC_SYMENT symbol;
2846 if (ldtbread (ldptr, sym_index, &symbol) <= 0)
2847 break;
2848 sym_index += GCC_SYMINC (symbol);
2850 if (GCC_OK_SYMBOL (symbol))
2852 char *name;
2854 if ((name = ldgetname (ldptr, &symbol)) == NULL)
2855 continue; /* Should never happen. */
2857 #ifdef XCOFF_DEBUGGING_INFO
2858 /* All AIX function names have a duplicate entry
2859 beginning with a dot. */
2860 if (*name == '.')
2861 ++name;
2862 #endif
2864 switch (is_ctor_dtor (name))
2866 case SYM_CTOR:
2867 if (! (filter & SCAN_CTOR))
2868 break;
2869 if (! is_shared)
2870 add_to_list (&constructors, name);
2871 #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
2872 if (which_pass == PASS_OBJ)
2873 add_to_list (&exports, name);
2874 #endif
2875 break;
2877 case SYM_DTOR:
2878 if (! (filter & SCAN_DTOR))
2879 break;
2880 if (! is_shared)
2881 add_to_list (&destructors, name);
2882 #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
2883 if (which_pass == PASS_OBJ)
2884 add_to_list (&exports, name);
2885 #endif
2886 break;
2888 #ifdef COLLECT_EXPORT_LIST
2889 case SYM_INIT:
2890 if (! (filter & SCAN_INIT))
2891 break;
2892 #ifndef LD_INIT_SWITCH
2893 if (is_shared)
2894 add_to_list (&constructors, name);
2895 #endif
2896 break;
2898 case SYM_FINI:
2899 if (! (filter & SCAN_FINI))
2900 break;
2901 #ifndef LD_INIT_SWITCH
2902 if (is_shared)
2903 add_to_list (&destructors, name);
2904 #endif
2905 break;
2906 #endif
2908 case SYM_DWEH:
2909 if (! (filter & SCAN_DWEH))
2910 break;
2911 if (! is_shared)
2912 add_to_list (&frame_tables, name);
2913 #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
2914 if (which_pass == PASS_OBJ)
2915 add_to_list (&exports, name);
2916 #endif
2917 break;
2919 default: /* not a constructor or destructor */
2920 #ifdef COLLECT_EXPORT_LIST
2921 /* Explicitly export all global symbols when
2922 building a shared object on AIX, but do not
2923 re-export symbols from another shared object
2924 and do not export symbols if the user
2925 provides an explicit export list. */
2926 if (shared_obj && !is_shared
2927 && which_pass == PASS_OBJ && !export_flag)
2928 add_to_list (&exports, name);
2929 #endif
2930 continue;
2933 if (debug)
2934 #if !defined(EXTENDED_COFF)
2935 fprintf (stderr, "\tsec=%d class=%d type=%s%o %s\n",
2936 symbol.n_scnum, symbol.n_sclass,
2937 (symbol.n_type ? "0" : ""), symbol.n_type,
2938 name);
2939 #else
2940 fprintf (stderr,
2941 "\tiss = %5d, value = %5ld, index = %5d, name = %s\n",
2942 symbol.iss, (long) symbol.value, symbol.index, name);
2943 #endif
2947 #ifdef COLLECT_EXPORT_LIST
2948 else
2950 /* If archive contains both 32-bit and 64-bit objects,
2951 we want to skip objects in other mode so mismatch normal. */
2952 if (debug)
2953 fprintf (stderr, "%s : magic=%o aix64=%d mismatch\n",
2954 prog_name, HEADER (ldptr).f_magic, aix64_flag);
2956 #endif
2958 else
2960 fatal_error ("%s: cannot open as COFF file", prog_name);
2962 #ifdef COLLECT_EXPORT_LIST
2963 /* On AIX loop continues while there are more members in archive. */
2965 while (ldclose (ldptr) == FAILURE);
2966 #else
2967 /* Otherwise we simply close ldptr. */
2968 (void) ldclose(ldptr);
2969 #endif
2971 #endif /* OBJECT_FORMAT_COFF */
2973 #ifdef COLLECT_EXPORT_LIST
2974 /* Given a library name without "lib" prefix, this function
2975 returns a full library name including a path. */
2976 static char *
2977 resolve_lib_name (const char *name)
2979 char *lib_buf;
2980 int i, j, l = 0;
2981 /* Library extensions for AIX dynamic linking. */
2982 const char * const libexts[2] = {"a", "so"};
2984 for (i = 0; libpaths[i]; i++)
2985 if (libpaths[i]->max_len > l)
2986 l = libpaths[i]->max_len;
2988 lib_buf = XNEWVEC (char, l + strlen(name) + 10);
2990 for (i = 0; libpaths[i]; i++)
2992 struct prefix_list *list = libpaths[i]->plist;
2993 for (; list; list = list->next)
2995 /* The following lines are needed because path_prefix list
2996 may contain directories both with trailing DIR_SEPARATOR and
2997 without it. */
2998 const char *p = "";
2999 if (!IS_DIR_SEPARATOR (list->prefix[strlen(list->prefix)-1]))
3000 p = "/";
3001 for (j = 0; j < 2; j++)
3003 sprintf (lib_buf, "%s%slib%s.%s",
3004 list->prefix, p, name,
3005 libexts[(j + aixrtl_flag) % 2]);
3006 if (debug) fprintf (stderr, "searching for: %s\n", lib_buf);
3007 if (file_exists (lib_buf))
3009 if (debug) fprintf (stderr, "found: %s\n", lib_buf);
3010 return (lib_buf);
3015 if (debug)
3016 fprintf (stderr, "not found\n");
3017 else
3018 fatal_error ("library lib%s not found", name);
3019 return (NULL);
3021 #endif /* COLLECT_EXPORT_LIST */
3023 #ifdef COLLECT_RUN_DSYMUTIL
3024 static int flag_dsym = false;
3025 static int flag_idsym = false;
3027 static void
3028 process_args (int *argcp, char **argv) {
3029 int i, j;
3030 int argc = *argcp;
3031 for (i=0; i<argc; ++i)
3033 if (strcmp (argv[i], "-dsym") == 0)
3035 flag_dsym = true;
3036 /* Remove the flag, as we handle all processing for it. */
3037 j = i;
3039 argv[j] = argv[j+1];
3040 while (++j < argc);
3041 --i;
3042 argc = --(*argcp);
3044 else if (strcmp (argv[i], "-idsym") == 0)
3046 flag_idsym = true;
3047 /* Remove the flag, as we handle all processing for it. */
3048 j = i;
3050 argv[j] = argv[j+1];
3051 while (++j < argc);
3052 --i;
3053 argc = --(*argcp);
3058 static void
3059 do_dsymutil (const char *output_file) {
3060 const char *dsymutil = DSYMUTIL + 1;
3061 struct pex_obj *pex;
3062 char **real_argv = XCNEWVEC (char *, 3);
3063 const char ** argv = CONST_CAST2 (const char **, char **,
3064 real_argv);
3066 argv[0] = dsymutil;
3067 argv[1] = output_file;
3068 argv[2] = (char *) 0;
3070 pex = collect_execute (dsymutil, real_argv, NULL, NULL, PEX_LAST | PEX_SEARCH);
3071 do_wait (dsymutil, pex);
3074 static void
3075 post_ld_pass (bool temp_file) {
3076 if (!(temp_file && flag_idsym) && !flag_dsym)
3077 return;
3079 do_dsymutil (output_file);
3081 #else
3082 static void
3083 process_args (int *argcp ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) { }
3084 static void post_ld_pass (bool temp_file ATTRIBUTE_UNUSED) { }
3085 #endif