]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/collect2.c
gcc/
[thirdparty/gcc.git] / gcc / collect2.c
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).
9
10 This file is part of GCC.
11
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.
16
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.
21
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/>. */
25
26
27 /* Build tables of static constructors and destructors and run ld. */
28
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include <signal.h>
34 #if ! defined( SIGCHLD ) && defined( SIGCLD )
35 # define SIGCHLD SIGCLD
36 #endif
37
38 #ifndef LIBRARY_PATH_ENV
39 #define LIBRARY_PATH_ENV "LIBRARY_PATH"
40 #endif
41
42 #define COLLECT
43
44 #include "collect2.h"
45 #include "collect2-aix.h"
46 #include "demangle.h"
47 #include "obstack.h"
48 #include "intl.h"
49 #include "version.h"
50 \f
51 /* On certain systems, we have code that works by scanning the object file
52 directly. But this code uses system-specific header files and library
53 functions, so turn it off in a cross-compiler. Likewise, the names of
54 the utilities are not correct for a cross-compiler; we have to hope that
55 cross-versions are in the proper directories. */
56
57 #ifdef CROSS_DIRECTORY_STRUCTURE
58 #ifndef CROSS_AIX_SUPPORT
59 #undef OBJECT_FORMAT_COFF
60 #endif
61 #undef MD_EXEC_PREFIX
62 #undef REAL_LD_FILE_NAME
63 #undef REAL_NM_FILE_NAME
64 #undef REAL_STRIP_FILE_NAME
65 #endif
66
67 /* If we cannot use a special method, use the ordinary one:
68 run nm to find what symbols are present.
69 In a cross-compiler, this means you need a cross nm,
70 but that is not quite as unpleasant as special headers. */
71
72 #if !defined (OBJECT_FORMAT_COFF)
73 #define OBJECT_FORMAT_NONE
74 #endif
75
76 #ifdef OBJECT_FORMAT_COFF
77
78 #ifndef CROSS_DIRECTORY_STRUCTURE
79 #include <a.out.h>
80 #include <ar.h>
81
82 #ifdef UMAX
83 #include <sgs.h>
84 #endif
85
86 /* Many versions of ldfcn.h define these. */
87 #ifdef FREAD
88 #undef FREAD
89 #undef FWRITE
90 #endif
91
92 #include <ldfcn.h>
93 #endif
94
95 /* Some systems have an ISCOFF macro, but others do not. In some cases
96 the macro may be wrong. MY_ISCOFF is defined in tm.h files for machines
97 that either do not have an ISCOFF macro in /usr/include or for those
98 where it is wrong. */
99
100 #ifndef MY_ISCOFF
101 #define MY_ISCOFF(X) ISCOFF (X)
102 #endif
103
104 #endif /* OBJECT_FORMAT_COFF */
105
106 #ifdef OBJECT_FORMAT_NONE
107
108 /* Default flags to pass to nm. */
109 #ifndef NM_FLAGS
110 #define NM_FLAGS "-n"
111 #endif
112
113 #endif /* OBJECT_FORMAT_NONE */
114
115 /* Some systems use __main in a way incompatible with its use in gcc, in these
116 cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
117 give the same symbol without quotes for an alternative entry point. */
118 #ifndef NAME__MAIN
119 #define NAME__MAIN "__main"
120 #endif
121
122 /* This must match tree.h. */
123 #define DEFAULT_INIT_PRIORITY 65535
124
125 #ifndef COLLECT_SHARED_INIT_FUNC
126 #define COLLECT_SHARED_INIT_FUNC(STREAM, FUNC) \
127 fprintf ((STREAM), "void _GLOBAL__DI() {\n\t%s();\n}\n", (FUNC))
128 #endif
129 #ifndef COLLECT_SHARED_FINI_FUNC
130 #define COLLECT_SHARED_FINI_FUNC(STREAM, FUNC) \
131 fprintf ((STREAM), "void _GLOBAL__DD() {\n\t%s();\n}\n", (FUNC))
132 #endif
133
134 #ifdef LDD_SUFFIX
135 #define SCAN_LIBRARIES
136 #endif
137
138 #ifndef SHLIB_SUFFIX
139 #define SHLIB_SUFFIX ".so"
140 #endif
141
142 #ifdef USE_COLLECT2
143 int do_collecting = 1;
144 #else
145 int do_collecting = 0;
146 #endif
147
148 /* Nonzero if we should suppress the automatic demangling of identifiers
149 in linker error messages. Set from COLLECT_NO_DEMANGLE. */
150 int no_demangle;
151 \f
152 /* Linked lists of constructor and destructor names. */
153
154 struct id
155 {
156 struct id *next;
157 int sequence;
158 char name[1];
159 };
160
161 struct head
162 {
163 struct id *first;
164 struct id *last;
165 int number;
166 };
167
168 /* Enumeration giving which pass this is for scanning the program file. */
169
170 enum pass {
171 PASS_FIRST, /* without constructors */
172 PASS_OBJ, /* individual objects */
173 PASS_LIB, /* looking for shared libraries */
174 PASS_SECOND /* with constructors linked in */
175 };
176
177 int vflag; /* true if -v */
178 static int rflag; /* true if -r */
179 static int strip_flag; /* true if -s */
180 static const char *demangle_flag;
181 #ifdef COLLECT_EXPORT_LIST
182 static int export_flag; /* true if -bE */
183 static int aix64_flag; /* true if -b64 */
184 static int aixrtl_flag; /* true if -brtl */
185 #endif
186
187 int debug; /* true if -debug */
188
189 static int shared_obj; /* true if -shared */
190
191 static const char *c_file; /* <xxx>.c for constructor/destructor list. */
192 static const char *o_file; /* <xxx>.o for constructor/destructor list. */
193 #ifdef COLLECT_EXPORT_LIST
194 static const char *export_file; /* <xxx>.x for AIX export list. */
195 #endif
196 const char *ldout; /* File for ld stdout. */
197 const char *lderrout; /* File for ld stderr. */
198 static const char *output_file; /* Output file for ld. */
199 static const char *nm_file_name; /* pathname of nm */
200 #ifdef LDD_SUFFIX
201 static const char *ldd_file_name; /* pathname of ldd (or equivalent) */
202 #endif
203 static const char *strip_file_name; /* pathname of strip */
204 const char *c_file_name; /* pathname of gcc */
205 static char *initname, *fininame; /* names of init and fini funcs */
206
207 static struct head constructors; /* list of constructors found */
208 static struct head destructors; /* list of destructors found */
209 #ifdef COLLECT_EXPORT_LIST
210 static struct head exports; /* list of exported symbols */
211 #endif
212 static struct head frame_tables; /* list of frame unwind info tables */
213
214 static bool at_file_supplied; /* Whether to use @file arguments */
215 static char *response_file; /* Name of any current response file */
216
217 struct obstack temporary_obstack;
218 char * temporary_firstobj;
219
220 /* Structure to hold all the directories in which to search for files to
221 execute. */
222
223 struct prefix_list
224 {
225 const char *prefix; /* String to prepend to the path. */
226 struct prefix_list *next; /* Next in linked list. */
227 };
228
229 struct path_prefix
230 {
231 struct prefix_list *plist; /* List of prefixes to try */
232 int max_len; /* Max length of a prefix in PLIST */
233 const char *name; /* Name of this list (used in config stuff) */
234 };
235
236 #ifdef COLLECT_EXPORT_LIST
237 /* Lists to keep libraries to be scanned for global constructors/destructors. */
238 static struct head libs; /* list of libraries */
239 static struct path_prefix cmdline_lib_dirs; /* directories specified with -L */
240 static struct path_prefix libpath_lib_dirs; /* directories in LIBPATH */
241 static struct path_prefix *libpaths[3] = {&cmdline_lib_dirs,
242 &libpath_lib_dirs, NULL};
243 #endif
244
245 /* Special kinds of symbols that a name may denote. */
246
247 typedef enum {
248 SYM_REGULAR = 0, /* nothing special */
249
250 SYM_CTOR = 1, /* constructor */
251 SYM_DTOR = 2, /* destructor */
252 SYM_INIT = 3, /* shared object routine that calls all the ctors */
253 SYM_FINI = 4, /* shared object routine that calls all the dtors */
254 SYM_DWEH = 5 /* DWARF exception handling table */
255 } symkind;
256
257 static symkind is_ctor_dtor (const char *);
258
259 static void handler (int);
260 static char *find_a_file (struct path_prefix *, const char *);
261 static void add_prefix (struct path_prefix *, const char *);
262 static void prefix_from_env (const char *, struct path_prefix *);
263 static void prefix_from_string (const char *, struct path_prefix *);
264 static void do_wait (const char *, struct pex_obj *);
265 static void fork_execute (const char *, char **);
266 static void maybe_unlink (const char *);
267 static void add_to_list (struct head *, const char *);
268 static int extract_init_priority (const char *);
269 static void sort_ids (struct head *);
270 static void write_list (FILE *, const char *, struct id *);
271 #ifdef COLLECT_EXPORT_LIST
272 static void dump_list (FILE *, const char *, struct id *);
273 #endif
274 #if 0
275 static void dump_prefix_list (FILE *, const char *, struct prefix_list *);
276 #endif
277 static void write_list_with_asm (FILE *, const char *, struct id *);
278 static void write_c_file (FILE *, const char *);
279 static void write_c_file_stat (FILE *, const char *);
280 #ifndef LD_INIT_SWITCH
281 static void write_c_file_glob (FILE *, const char *);
282 #endif
283 static void scan_prog_file (const char *, enum pass);
284 #ifdef SCAN_LIBRARIES
285 static void scan_libraries (const char *);
286 #endif
287 #if LINK_ELIMINATE_DUPLICATE_LDIRECTORIES
288 static int is_in_args (const char *, const char **, const char **);
289 #endif
290 #ifdef COLLECT_EXPORT_LIST
291 #if 0
292 static int is_in_list (const char *, struct id *);
293 #endif
294 static void write_aix_file (FILE *, struct id *);
295 static char *resolve_lib_name (const char *);
296 #endif
297 static char *extract_string (const char **);
298 \f
299 /* Delete tempfiles and exit function. */
300
301 void
302 collect_exit (int status)
303 {
304 if (c_file != 0 && c_file[0])
305 maybe_unlink (c_file);
306
307 if (o_file != 0 && o_file[0])
308 maybe_unlink (o_file);
309
310 #ifdef COLLECT_EXPORT_LIST
311 if (export_file != 0 && export_file[0])
312 maybe_unlink (export_file);
313 #endif
314
315 if (ldout != 0 && ldout[0])
316 {
317 dump_file (ldout, stdout);
318 maybe_unlink (ldout);
319 }
320
321 if (lderrout != 0 && lderrout[0])
322 {
323 dump_file (lderrout, stderr);
324 maybe_unlink (lderrout);
325 }
326
327 if (status != 0 && output_file != 0 && output_file[0])
328 maybe_unlink (output_file);
329
330 if (response_file)
331 maybe_unlink (response_file);
332
333 exit (status);
334 }
335
336 \f
337 /* Notify user of a non-error. */
338 void
339 notice (const char *cmsgid, ...)
340 {
341 va_list ap;
342
343 va_start (ap, cmsgid);
344 vfprintf (stderr, _(cmsgid), ap);
345 va_end (ap);
346 }
347
348 /* Die when sys call fails. */
349
350 void
351 fatal_perror (const char * cmsgid, ...)
352 {
353 int e = errno;
354 va_list ap;
355
356 va_start (ap, cmsgid);
357 fprintf (stderr, "collect2: ");
358 vfprintf (stderr, _(cmsgid), ap);
359 fprintf (stderr, ": %s\n", xstrerror (e));
360 va_end (ap);
361
362 collect_exit (FATAL_EXIT_CODE);
363 }
364
365 /* Just die. */
366
367 void
368 fatal (const char * cmsgid, ...)
369 {
370 va_list ap;
371
372 va_start (ap, cmsgid);
373 fprintf (stderr, "collect2: ");
374 vfprintf (stderr, _(cmsgid), ap);
375 fprintf (stderr, "\n");
376 va_end (ap);
377
378 collect_exit (FATAL_EXIT_CODE);
379 }
380
381 /* Write error message. */
382
383 void
384 error (const char * gmsgid, ...)
385 {
386 va_list ap;
387
388 va_start (ap, gmsgid);
389 fprintf (stderr, "collect2: ");
390 vfprintf (stderr, _(gmsgid), ap);
391 fprintf (stderr, "\n");
392 va_end(ap);
393 }
394
395 /* In case obstack is linked in, and abort is defined to fancy_abort,
396 provide a default entry. */
397
398 void
399 fancy_abort (const char *file, int line, const char *func)
400 {
401 fatal ("internal gcc abort in %s, at %s:%d", func, file, line);
402 }
403 \f
404 static void
405 handler (int signo)
406 {
407 if (c_file != 0 && c_file[0])
408 maybe_unlink (c_file);
409
410 if (o_file != 0 && o_file[0])
411 maybe_unlink (o_file);
412
413 if (ldout != 0 && ldout[0])
414 maybe_unlink (ldout);
415
416 if (lderrout != 0 && lderrout[0])
417 maybe_unlink (lderrout);
418
419 #ifdef COLLECT_EXPORT_LIST
420 if (export_file != 0 && export_file[0])
421 maybe_unlink (export_file);
422 #endif
423
424 if (response_file)
425 maybe_unlink (response_file);
426
427 signal (signo, SIG_DFL);
428 raise (signo);
429 }
430
431 \f
432 int
433 file_exists (const char *name)
434 {
435 return access (name, R_OK) == 0;
436 }
437
438 /* Parse a reasonable subset of shell quoting syntax. */
439
440 static char *
441 extract_string (const char **pp)
442 {
443 const char *p = *pp;
444 int backquote = 0;
445 int inside = 0;
446
447 for (;;)
448 {
449 char c = *p;
450 if (c == '\0')
451 break;
452 ++p;
453 if (backquote)
454 obstack_1grow (&temporary_obstack, c);
455 else if (! inside && c == ' ')
456 break;
457 else if (! inside && c == '\\')
458 backquote = 1;
459 else if (c == '\'')
460 inside = !inside;
461 else
462 obstack_1grow (&temporary_obstack, c);
463 }
464
465 obstack_1grow (&temporary_obstack, '\0');
466 *pp = p;
467 return XOBFINISH (&temporary_obstack, char *);
468 }
469 \f
470 void
471 dump_file (const char *name, FILE *to)
472 {
473 FILE *stream = fopen (name, "r");
474
475 if (stream == 0)
476 return;
477 while (1)
478 {
479 int c;
480 while (c = getc (stream),
481 c != EOF && (ISIDNUM (c) || c == '$' || c == '.'))
482 obstack_1grow (&temporary_obstack, c);
483 if (obstack_object_size (&temporary_obstack) > 0)
484 {
485 const char *word, *p;
486 char *result;
487 obstack_1grow (&temporary_obstack, '\0');
488 word = XOBFINISH (&temporary_obstack, const char *);
489
490 if (*word == '.')
491 ++word, putc ('.', to);
492 p = word;
493 if (!strncmp (p, USER_LABEL_PREFIX, strlen (USER_LABEL_PREFIX)))
494 p += strlen (USER_LABEL_PREFIX);
495
496 #ifdef HAVE_LD_DEMANGLE
497 result = 0;
498 #else
499 if (no_demangle)
500 result = 0;
501 else
502 result = cplus_demangle (p, DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE);
503 #endif
504
505 if (result)
506 {
507 int diff;
508 fputs (result, to);
509
510 diff = strlen (word) - strlen (result);
511 while (diff > 0 && c == ' ')
512 --diff, putc (' ', to);
513 if (diff < 0 && c == ' ')
514 {
515 while (diff < 0 && c == ' ')
516 ++diff, c = getc (stream);
517 if (!ISSPACE (c))
518 {
519 /* Make sure we output at least one space, or
520 the demangled symbol name will run into
521 whatever text follows. */
522 putc (' ', to);
523 }
524 }
525
526 free (result);
527 }
528 else
529 fputs (word, to);
530
531 fflush (to);
532 obstack_free (&temporary_obstack, temporary_firstobj);
533 }
534 if (c == EOF)
535 break;
536 putc (c, to);
537 }
538 fclose (stream);
539 }
540 \f
541 /* Return the kind of symbol denoted by name S. */
542
543 static symkind
544 is_ctor_dtor (const char *s)
545 {
546 struct names { const char *const name; const int len; symkind ret;
547 const int two_underscores; };
548
549 const struct names *p;
550 int ch;
551 const char *orig_s = s;
552
553 static const struct names special[] = {
554 #ifndef NO_DOLLAR_IN_LABEL
555 { "GLOBAL__I$", sizeof ("GLOBAL__I$")-1, SYM_CTOR, 0 },
556 { "GLOBAL__D$", sizeof ("GLOBAL__D$")-1, SYM_DTOR, 0 },
557 #else
558 #ifndef NO_DOT_IN_LABEL
559 { "GLOBAL__I.", sizeof ("GLOBAL__I.")-1, SYM_CTOR, 0 },
560 { "GLOBAL__D.", sizeof ("GLOBAL__D.")-1, SYM_DTOR, 0 },
561 #endif /* NO_DOT_IN_LABEL */
562 #endif /* NO_DOLLAR_IN_LABEL */
563 { "GLOBAL__I_", sizeof ("GLOBAL__I_")-1, SYM_CTOR, 0 },
564 { "GLOBAL__D_", sizeof ("GLOBAL__D_")-1, SYM_DTOR, 0 },
565 { "GLOBAL__F_", sizeof ("GLOBAL__F_")-1, SYM_DWEH, 0 },
566 { "GLOBAL__FI_", sizeof ("GLOBAL__FI_")-1, SYM_INIT, 0 },
567 { "GLOBAL__FD_", sizeof ("GLOBAL__FD_")-1, SYM_FINI, 0 },
568 { NULL, 0, SYM_REGULAR, 0 }
569 };
570
571 while ((ch = *s) == '_')
572 ++s;
573
574 if (s == orig_s)
575 return SYM_REGULAR;
576
577 for (p = &special[0]; p->len > 0; p++)
578 {
579 if (ch == p->name[0]
580 && (!p->two_underscores || ((s - orig_s) >= 2))
581 && strncmp(s, p->name, p->len) == 0)
582 {
583 return p->ret;
584 }
585 }
586 return SYM_REGULAR;
587 }
588 \f
589 /* We maintain two prefix lists: one from COMPILER_PATH environment variable
590 and one from the PATH variable. */
591
592 static struct path_prefix cpath, path;
593
594 #ifdef CROSS_DIRECTORY_STRUCTURE
595 /* This is the name of the target machine. We use it to form the name
596 of the files to execute. */
597
598 static const char *const target_machine = TARGET_MACHINE;
599 #endif
600
601 /* Search for NAME using prefix list PPREFIX. We only look for executable
602 files.
603
604 Return 0 if not found, otherwise return its name, allocated with malloc. */
605
606 static char *
607 find_a_file (struct path_prefix *pprefix, const char *name)
608 {
609 char *temp;
610 struct prefix_list *pl;
611 int len = pprefix->max_len + strlen (name) + 1;
612
613 if (debug)
614 fprintf (stderr, "Looking for '%s'\n", name);
615
616 #ifdef HOST_EXECUTABLE_SUFFIX
617 len += strlen (HOST_EXECUTABLE_SUFFIX);
618 #endif
619
620 temp = XNEWVEC (char, len);
621
622 /* Determine the filename to execute (special case for absolute paths). */
623
624 if (IS_ABSOLUTE_PATH (name))
625 {
626 if (access (name, X_OK) == 0)
627 {
628 strcpy (temp, name);
629
630 if (debug)
631 fprintf (stderr, " - found: absolute path\n");
632
633 return temp;
634 }
635
636 #ifdef HOST_EXECUTABLE_SUFFIX
637 /* Some systems have a suffix for executable files.
638 So try appending that. */
639 strcpy (temp, name);
640 strcat (temp, HOST_EXECUTABLE_SUFFIX);
641
642 if (access (temp, X_OK) == 0)
643 return temp;
644 #endif
645
646 if (debug)
647 fprintf (stderr, " - failed to locate using absolute path\n");
648 }
649 else
650 for (pl = pprefix->plist; pl; pl = pl->next)
651 {
652 struct stat st;
653
654 strcpy (temp, pl->prefix);
655 strcat (temp, name);
656
657 if (stat (temp, &st) >= 0
658 && ! S_ISDIR (st.st_mode)
659 && access (temp, X_OK) == 0)
660 return temp;
661
662 #ifdef HOST_EXECUTABLE_SUFFIX
663 /* Some systems have a suffix for executable files.
664 So try appending that. */
665 strcat (temp, HOST_EXECUTABLE_SUFFIX);
666
667 if (stat (temp, &st) >= 0
668 && ! S_ISDIR (st.st_mode)
669 && access (temp, X_OK) == 0)
670 return temp;
671 #endif
672 }
673
674 if (debug && pprefix->plist == NULL)
675 fprintf (stderr, " - failed: no entries in prefix list\n");
676
677 free (temp);
678 return 0;
679 }
680
681 /* Add an entry for PREFIX to prefix list PPREFIX. */
682
683 static void
684 add_prefix (struct path_prefix *pprefix, const char *prefix)
685 {
686 struct prefix_list *pl, **prev;
687 int len;
688
689 if (pprefix->plist)
690 {
691 for (pl = pprefix->plist; pl->next; pl = pl->next)
692 ;
693 prev = &pl->next;
694 }
695 else
696 prev = &pprefix->plist;
697
698 /* Keep track of the longest prefix. */
699
700 len = strlen (prefix);
701 if (len > pprefix->max_len)
702 pprefix->max_len = len;
703
704 pl = XNEW (struct prefix_list);
705 pl->prefix = xstrdup (prefix);
706
707 if (*prev)
708 pl->next = *prev;
709 else
710 pl->next = (struct prefix_list *) 0;
711 *prev = pl;
712 }
713 \f
714 /* Take the value of the environment variable ENV, break it into a path, and
715 add of the entries to PPREFIX. */
716
717 static void
718 prefix_from_env (const char *env, struct path_prefix *pprefix)
719 {
720 const char *p;
721 GET_ENVIRONMENT (p, env);
722
723 if (p)
724 prefix_from_string (p, pprefix);
725 }
726
727 static void
728 prefix_from_string (const char *p, struct path_prefix *pprefix)
729 {
730 const char *startp, *endp;
731 char *nstore = XNEWVEC (char, strlen (p) + 3);
732
733 if (debug)
734 fprintf (stderr, "Convert string '%s' into prefixes, separator = '%c'\n", p, PATH_SEPARATOR);
735
736 startp = endp = p;
737 while (1)
738 {
739 if (*endp == PATH_SEPARATOR || *endp == 0)
740 {
741 strncpy (nstore, startp, endp-startp);
742 if (endp == startp)
743 {
744 strcpy (nstore, "./");
745 }
746 else if (! IS_DIR_SEPARATOR (endp[-1]))
747 {
748 nstore[endp-startp] = DIR_SEPARATOR;
749 nstore[endp-startp+1] = 0;
750 }
751 else
752 nstore[endp-startp] = 0;
753
754 if (debug)
755 fprintf (stderr, " - add prefix: %s\n", nstore);
756
757 add_prefix (pprefix, nstore);
758 if (*endp == 0)
759 break;
760 endp = startp = endp + 1;
761 }
762 else
763 endp++;
764 }
765 free (nstore);
766 }
767 \f
768 /* Main program. */
769
770 int
771 main (int argc, char **argv)
772 {
773 static const char *const ld_suffix = "ld";
774 static const char *const real_ld_suffix = "real-ld";
775 static const char *const collect_ld_suffix = "collect-ld";
776 static const char *const nm_suffix = "nm";
777 static const char *const gnm_suffix = "gnm";
778 #ifdef LDD_SUFFIX
779 static const char *const ldd_suffix = LDD_SUFFIX;
780 #endif
781 static const char *const strip_suffix = "strip";
782 static const char *const gstrip_suffix = "gstrip";
783
784 #ifdef CROSS_DIRECTORY_STRUCTURE
785 /* If we look for a program in the compiler directories, we just use
786 the short name, since these directories are already system-specific.
787 But it we look for a program in the system directories, we need to
788 qualify the program name with the target machine. */
789
790 const char *const full_ld_suffix =
791 concat(target_machine, "-", ld_suffix, NULL);
792 const char *const full_nm_suffix =
793 concat (target_machine, "-", nm_suffix, NULL);
794 const char *const full_gnm_suffix =
795 concat (target_machine, "-", gnm_suffix, NULL);
796 #ifdef LDD_SUFFIX
797 const char *const full_ldd_suffix =
798 concat (target_machine, "-", ldd_suffix, NULL);
799 #endif
800 const char *const full_strip_suffix =
801 concat (target_machine, "-", strip_suffix, NULL);
802 const char *const full_gstrip_suffix =
803 concat (target_machine, "-", gstrip_suffix, NULL);
804 #else
805 const char *const full_ld_suffix = ld_suffix;
806 const char *const full_nm_suffix = nm_suffix;
807 const char *const full_gnm_suffix = gnm_suffix;
808 #ifdef LDD_SUFFIX
809 const char *const full_ldd_suffix = ldd_suffix;
810 #endif
811 const char *const full_strip_suffix = strip_suffix;
812 const char *const full_gstrip_suffix = gstrip_suffix;
813 #endif /* CROSS_DIRECTORY_STRUCTURE */
814
815 const char *arg;
816 FILE *outf;
817 #ifdef COLLECT_EXPORT_LIST
818 FILE *exportf;
819 #endif
820 const char *ld_file_name;
821 const char *p;
822 char **c_argv;
823 const char **c_ptr;
824 char **ld1_argv;
825 const char **ld1;
826 char **ld2_argv;
827 const char **ld2;
828 char **object_lst;
829 const char **object;
830 int first_file;
831 int num_c_args;
832 char **old_argv;
833
834 old_argv = argv;
835 expandargv (&argc, &argv);
836 if (argv != old_argv)
837 at_file_supplied = 1;
838
839 num_c_args = argc + 9;
840
841 no_demangle = !! getenv ("COLLECT_NO_DEMANGLE");
842
843 /* Suppress demangling by the real linker, which may be broken. */
844 putenv (xstrdup ("COLLECT_NO_DEMANGLE="));
845
846 #if defined (COLLECT2_HOST_INITIALIZATION)
847 /* Perform system dependent initialization, if necessary. */
848 COLLECT2_HOST_INITIALIZATION;
849 #endif
850
851 #ifdef SIGCHLD
852 /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
853 receive the signal. A different setting is inheritable */
854 signal (SIGCHLD, SIG_DFL);
855 #endif
856
857 /* Unlock the stdio streams. */
858 unlock_std_streams ();
859
860 gcc_init_libintl ();
861
862 /* Do not invoke xcalloc before this point, since locale needs to be
863 set first, in case a diagnostic is issued. */
864
865 ld1_argv = XCNEWVEC (char *, argc + 4);
866 ld1 = CONST_CAST2 (const char **, char **, ld1_argv);
867 ld2_argv = XCNEWVEC (char *, argc + 11);
868 ld2 = CONST_CAST2 (const char **, char **, ld2_argv);
869 object_lst = XCNEWVEC (char *, argc);
870 object = CONST_CAST2 (const char **, char **, object_lst);
871
872 #ifdef DEBUG
873 debug = 1;
874 #endif
875
876 /* Parse command line early for instances of -debug. This allows
877 the debug flag to be set before functions like find_a_file()
878 are called. */
879 {
880 int i;
881
882 for (i = 1; argv[i] != NULL; i ++)
883 {
884 if (! strcmp (argv[i], "-debug"))
885 debug = 1;
886 }
887 vflag = debug;
888 }
889
890 #ifndef DEFAULT_A_OUT_NAME
891 output_file = "a.out";
892 #else
893 output_file = DEFAULT_A_OUT_NAME;
894 #endif
895
896 obstack_begin (&temporary_obstack, 0);
897 temporary_firstobj = (char *) obstack_alloc (&temporary_obstack, 0);
898
899 #ifndef HAVE_LD_DEMANGLE
900 current_demangling_style = auto_demangling;
901 #endif
902 p = getenv ("COLLECT_GCC_OPTIONS");
903 while (p && *p)
904 {
905 const char *q = extract_string (&p);
906 if (*q == '-' && (q[1] == 'm' || q[1] == 'f'))
907 num_c_args++;
908 }
909 obstack_free (&temporary_obstack, temporary_firstobj);
910
911 /* -fno-profile-arcs -fno-test-coverage -fno-branch-probabilities
912 -fno-exceptions -w */
913 num_c_args += 5;
914
915 c_argv = XCNEWVEC (char *, num_c_args);
916 c_ptr = CONST_CAST2 (const char **, char **, c_argv);
917
918 if (argc < 2)
919 fatal ("no arguments");
920
921 #ifdef SIGQUIT
922 if (signal (SIGQUIT, SIG_IGN) != SIG_IGN)
923 signal (SIGQUIT, handler);
924 #endif
925 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
926 signal (SIGINT, handler);
927 #ifdef SIGALRM
928 if (signal (SIGALRM, SIG_IGN) != SIG_IGN)
929 signal (SIGALRM, handler);
930 #endif
931 #ifdef SIGHUP
932 if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
933 signal (SIGHUP, handler);
934 #endif
935 if (signal (SIGSEGV, SIG_IGN) != SIG_IGN)
936 signal (SIGSEGV, handler);
937 #ifdef SIGBUS
938 if (signal (SIGBUS, SIG_IGN) != SIG_IGN)
939 signal (SIGBUS, handler);
940 #endif
941
942 /* Extract COMPILER_PATH and PATH into our prefix list. */
943 prefix_from_env ("COMPILER_PATH", &cpath);
944 prefix_from_env ("PATH", &path);
945
946 /* Try to discover a valid linker/nm/strip to use. */
947
948 /* Maybe we know the right file to use (if not cross). */
949 ld_file_name = 0;
950 #ifdef DEFAULT_LINKER
951 if (access (DEFAULT_LINKER, X_OK) == 0)
952 ld_file_name = DEFAULT_LINKER;
953 if (ld_file_name == 0)
954 #endif
955 #ifdef REAL_LD_FILE_NAME
956 ld_file_name = find_a_file (&path, REAL_LD_FILE_NAME);
957 if (ld_file_name == 0)
958 #endif
959 /* Search the (target-specific) compiler dirs for ld'. */
960 ld_file_name = find_a_file (&cpath, real_ld_suffix);
961 /* Likewise for `collect-ld'. */
962 if (ld_file_name == 0)
963 ld_file_name = find_a_file (&cpath, collect_ld_suffix);
964 /* Search the compiler directories for `ld'. We have protection against
965 recursive calls in find_a_file. */
966 if (ld_file_name == 0)
967 ld_file_name = find_a_file (&cpath, ld_suffix);
968 /* Search the ordinary system bin directories
969 for `ld' (if native linking) or `TARGET-ld' (if cross). */
970 if (ld_file_name == 0)
971 ld_file_name = find_a_file (&path, full_ld_suffix);
972
973 #ifdef REAL_NM_FILE_NAME
974 nm_file_name = find_a_file (&path, REAL_NM_FILE_NAME);
975 if (nm_file_name == 0)
976 #endif
977 nm_file_name = find_a_file (&cpath, gnm_suffix);
978 if (nm_file_name == 0)
979 nm_file_name = find_a_file (&path, full_gnm_suffix);
980 if (nm_file_name == 0)
981 nm_file_name = find_a_file (&cpath, nm_suffix);
982 if (nm_file_name == 0)
983 nm_file_name = find_a_file (&path, full_nm_suffix);
984
985 #ifdef LDD_SUFFIX
986 ldd_file_name = find_a_file (&cpath, ldd_suffix);
987 if (ldd_file_name == 0)
988 ldd_file_name = find_a_file (&path, full_ldd_suffix);
989 #endif
990
991 #ifdef REAL_STRIP_FILE_NAME
992 strip_file_name = find_a_file (&path, REAL_STRIP_FILE_NAME);
993 if (strip_file_name == 0)
994 #endif
995 strip_file_name = find_a_file (&cpath, gstrip_suffix);
996 if (strip_file_name == 0)
997 strip_file_name = find_a_file (&path, full_gstrip_suffix);
998 if (strip_file_name == 0)
999 strip_file_name = find_a_file (&cpath, strip_suffix);
1000 if (strip_file_name == 0)
1001 strip_file_name = find_a_file (&path, full_strip_suffix);
1002
1003 /* Determine the full path name of the C compiler to use. */
1004 c_file_name = getenv ("COLLECT_GCC");
1005 if (c_file_name == 0)
1006 {
1007 #ifdef CROSS_DIRECTORY_STRUCTURE
1008 c_file_name = concat (target_machine, "-gcc", NULL);
1009 #else
1010 c_file_name = "gcc";
1011 #endif
1012 }
1013
1014 p = find_a_file (&cpath, c_file_name);
1015
1016 /* Here it should be safe to use the system search path since we should have
1017 already qualified the name of the compiler when it is needed. */
1018 if (p == 0)
1019 p = find_a_file (&path, c_file_name);
1020
1021 if (p)
1022 c_file_name = p;
1023
1024 *ld1++ = *ld2++ = ld_file_name;
1025
1026 /* Make temp file names. */
1027 c_file = make_temp_file (".c");
1028 o_file = make_temp_file (".o");
1029 #ifdef COLLECT_EXPORT_LIST
1030 export_file = make_temp_file (".x");
1031 #endif
1032 ldout = make_temp_file (".ld");
1033 lderrout = make_temp_file (".le");
1034 *c_ptr++ = c_file_name;
1035 *c_ptr++ = "-x";
1036 *c_ptr++ = "c";
1037 *c_ptr++ = "-c";
1038 *c_ptr++ = "-o";
1039 *c_ptr++ = o_file;
1040
1041 #ifdef COLLECT_EXPORT_LIST
1042 /* Generate a list of directories from LIBPATH. */
1043 prefix_from_env ("LIBPATH", &libpath_lib_dirs);
1044 /* Add to this list also two standard directories where
1045 AIX loader always searches for libraries. */
1046 add_prefix (&libpath_lib_dirs, "/lib");
1047 add_prefix (&libpath_lib_dirs, "/usr/lib");
1048 #endif
1049
1050 /* Get any options that the upper GCC wants to pass to the sub-GCC.
1051
1052 AIX support needs to know if -shared has been specified before
1053 parsing commandline arguments. */
1054
1055 p = getenv ("COLLECT_GCC_OPTIONS");
1056 while (p && *p)
1057 {
1058 const char *q = extract_string (&p);
1059 if (*q == '-' && (q[1] == 'm' || q[1] == 'f'))
1060 *c_ptr++ = xstrdup (q);
1061 if (strcmp (q, "-EL") == 0 || strcmp (q, "-EB") == 0)
1062 *c_ptr++ = xstrdup (q);
1063 if (strcmp (q, "-shared") == 0)
1064 shared_obj = 1;
1065 if (*q == '-' && q[1] == 'B')
1066 {
1067 *c_ptr++ = xstrdup (q);
1068 if (q[2] == 0)
1069 {
1070 q = extract_string (&p);
1071 *c_ptr++ = xstrdup (q);
1072 }
1073 }
1074 }
1075 obstack_free (&temporary_obstack, temporary_firstobj);
1076 *c_ptr++ = "-fno-profile-arcs";
1077 *c_ptr++ = "-fno-test-coverage";
1078 *c_ptr++ = "-fno-branch-probabilities";
1079 *c_ptr++ = "-fno-exceptions";
1080 *c_ptr++ = "-w";
1081
1082 /* !!! When GCC calls collect2,
1083 it does not know whether it is calling collect2 or ld.
1084 So collect2 cannot meaningfully understand any options
1085 except those ld understands.
1086 If you propose to make GCC pass some other option,
1087 just imagine what will happen if ld is really ld!!! */
1088
1089 /* Parse arguments. Remember output file spec, pass the rest to ld. */
1090 /* After the first file, put in the c++ rt0. */
1091
1092 first_file = 1;
1093 #ifdef HAVE_LD_DEMANGLE
1094 if (!demangle_flag && !no_demangle)
1095 demangle_flag = "--demangle";
1096 if (demangle_flag)
1097 *ld1++ = *ld2++ = demangle_flag;
1098 #endif
1099 while ((arg = *++argv) != (char *) 0)
1100 {
1101 *ld1++ = *ld2++ = arg;
1102
1103 if (arg[0] == '-')
1104 {
1105 switch (arg[1])
1106 {
1107 #ifdef COLLECT_EXPORT_LIST
1108 /* We want to disable automatic exports on AIX when user
1109 explicitly puts an export list in command line */
1110 case 'b':
1111 if (arg[2] == 'E' || strncmp (&arg[2], "export", 6) == 0)
1112 export_flag = 1;
1113 else if (arg[2] == '6' && arg[3] == '4')
1114 aix64_flag = 1;
1115 else if (arg[2] == 'r' && arg[3] == 't' && arg[4] == 'l')
1116 aixrtl_flag = 1;
1117 break;
1118 #endif
1119
1120 case 'd':
1121 if (!strcmp (arg, "-debug"))
1122 {
1123 /* Already parsed. */
1124 ld1--;
1125 ld2--;
1126 }
1127 if (!strcmp (arg, "-dynamic-linker") && argv[1])
1128 {
1129 ++argv;
1130 *ld1++ = *ld2++ = *argv;
1131 }
1132 break;
1133
1134 case 'l':
1135 if (first_file)
1136 {
1137 /* place o_file BEFORE this argument! */
1138 first_file = 0;
1139 ld2--;
1140 *ld2++ = o_file;
1141 *ld2++ = arg;
1142 }
1143 #ifdef COLLECT_EXPORT_LIST
1144 {
1145 /* Resolving full library name. */
1146 const char *s = resolve_lib_name (arg+2);
1147
1148 /* Saving a full library name. */
1149 add_to_list (&libs, s);
1150 }
1151 #endif
1152 break;
1153
1154 #ifdef COLLECT_EXPORT_LIST
1155 /* Saving directories where to search for libraries. */
1156 case 'L':
1157 add_prefix (&cmdline_lib_dirs, arg+2);
1158 break;
1159 #else
1160 #if LINK_ELIMINATE_DUPLICATE_LDIRECTORIES
1161 case 'L':
1162 if (is_in_args (arg, (const char **) ld1_argv, ld1-1))
1163 --ld1;
1164 break;
1165 #endif /* LINK_ELIMINATE_DUPLICATE_LDIRECTORIES */
1166 #endif
1167
1168 case 'o':
1169 if (arg[2] == '\0')
1170 output_file = *ld1++ = *ld2++ = *++argv;
1171 else if (1
1172 #ifdef SWITCHES_NEED_SPACES
1173 && ! strchr (SWITCHES_NEED_SPACES, arg[1])
1174 #endif
1175 )
1176
1177 output_file = &arg[2];
1178 break;
1179
1180 case 'r':
1181 if (arg[2] == '\0')
1182 rflag = 1;
1183 break;
1184
1185 case 's':
1186 if (arg[2] == '\0' && do_collecting)
1187 {
1188 /* We must strip after the nm run, otherwise C++ linking
1189 will not work. Thus we strip in the second ld run, or
1190 else with strip if there is no second ld run. */
1191 strip_flag = 1;
1192 ld1--;
1193 }
1194 break;
1195
1196 case 'v':
1197 if (arg[2] == '\0')
1198 vflag = 1;
1199 break;
1200
1201 case '-':
1202 if (strcmp (arg, "--no-demangle") == 0)
1203 {
1204 demangle_flag = arg;
1205 no_demangle = 1;
1206 ld1--;
1207 ld2--;
1208 }
1209 else if (strncmp (arg, "--demangle", 10) == 0)
1210 {
1211 demangle_flag = arg;
1212 no_demangle = 0;
1213 #ifndef HAVE_LD_DEMANGLE
1214 if (arg[10] == '=')
1215 {
1216 enum demangling_styles style
1217 = cplus_demangle_name_to_style (arg+11);
1218 if (style == unknown_demangling)
1219 error ("unknown demangling style '%s'", arg+11);
1220 else
1221 current_demangling_style = style;
1222 }
1223 #endif
1224 ld1--;
1225 ld2--;
1226 }
1227 break;
1228 }
1229 }
1230 else if ((p = strrchr (arg, '.')) != (char *) 0
1231 && (strcmp (p, ".o") == 0 || strcmp (p, ".a") == 0
1232 || strcmp (p, ".so") == 0 || strcmp (p, ".lo") == 0
1233 || strcmp (p, ".obj") == 0))
1234 {
1235 if (first_file)
1236 {
1237 first_file = 0;
1238 if (p[1] == 'o')
1239 *ld2++ = o_file;
1240 else
1241 {
1242 /* place o_file BEFORE this argument! */
1243 ld2--;
1244 *ld2++ = o_file;
1245 *ld2++ = arg;
1246 }
1247 }
1248 if (p[1] == 'o' || p[1] == 'l')
1249 *object++ = arg;
1250 #ifdef COLLECT_EXPORT_LIST
1251 /* libraries can be specified directly, i.e. without -l flag. */
1252 else
1253 {
1254 /* Saving a full library name. */
1255 add_to_list (&libs, arg);
1256 }
1257 #endif
1258 }
1259 }
1260
1261 #ifdef COLLECT_EXPORT_LIST
1262 /* This is added only for debugging purposes. */
1263 if (debug)
1264 {
1265 fprintf (stderr, "List of libraries:\n");
1266 dump_list (stderr, "\t", libs.first);
1267 }
1268
1269 /* The AIX linker will discard static constructors in object files if
1270 nothing else in the file is referenced, so look at them first. */
1271 {
1272 const char **export_object_lst = (const char **)object_lst;
1273
1274 while (export_object_lst < object)
1275 scan_prog_file (*export_object_lst++, PASS_OBJ);
1276 }
1277 {
1278 struct id *list = libs.first;
1279
1280 for (; list; list = list->next)
1281 scan_prog_file (list->name, PASS_FIRST);
1282 }
1283
1284 if (exports.first)
1285 {
1286 char *buf = concat ("-bE:", export_file, NULL);
1287
1288 *ld1++ = buf;
1289 *ld2++ = buf;
1290
1291 exportf = fopen (export_file, "w");
1292 if (exportf == (FILE *) 0)
1293 fatal_perror ("fopen %s", export_file);
1294 write_aix_file (exportf, exports.first);
1295 if (fclose (exportf))
1296 fatal_perror ("fclose %s", export_file);
1297 }
1298 #endif
1299
1300 *c_ptr++ = c_file;
1301 *c_ptr = *ld1 = *object = (char *) 0;
1302
1303 if (vflag)
1304 {
1305 notice ("collect2 version %s", version_string);
1306 #ifdef TARGET_VERSION
1307 TARGET_VERSION;
1308 #endif
1309 fprintf (stderr, "\n");
1310 }
1311
1312 if (debug)
1313 {
1314 const char *ptr;
1315 fprintf (stderr, "ld_file_name = %s\n",
1316 (ld_file_name ? ld_file_name : "not found"));
1317 fprintf (stderr, "c_file_name = %s\n",
1318 (c_file_name ? c_file_name : "not found"));
1319 fprintf (stderr, "nm_file_name = %s\n",
1320 (nm_file_name ? nm_file_name : "not found"));
1321 #ifdef LDD_SUFFIX
1322 fprintf (stderr, "ldd_file_name = %s\n",
1323 (ldd_file_name ? ldd_file_name : "not found"));
1324 #endif
1325 fprintf (stderr, "strip_file_name = %s\n",
1326 (strip_file_name ? strip_file_name : "not found"));
1327 fprintf (stderr, "c_file = %s\n",
1328 (c_file ? c_file : "not found"));
1329 fprintf (stderr, "o_file = %s\n",
1330 (o_file ? o_file : "not found"));
1331
1332 ptr = getenv ("COLLECT_GCC_OPTIONS");
1333 if (ptr)
1334 fprintf (stderr, "COLLECT_GCC_OPTIONS = %s\n", ptr);
1335
1336 ptr = getenv ("COLLECT_GCC");
1337 if (ptr)
1338 fprintf (stderr, "COLLECT_GCC = %s\n", ptr);
1339
1340 ptr = getenv ("COMPILER_PATH");
1341 if (ptr)
1342 fprintf (stderr, "COMPILER_PATH = %s\n", ptr);
1343
1344 ptr = getenv (LIBRARY_PATH_ENV);
1345 if (ptr)
1346 fprintf (stderr, "%-20s= %s\n", LIBRARY_PATH_ENV, ptr);
1347
1348 fprintf (stderr, "\n");
1349 }
1350
1351 /* Load the program, searching all libraries and attempting to provide
1352 undefined symbols from repository information. */
1353
1354 /* On AIX we do this later. */
1355 #ifndef COLLECT_EXPORT_LIST
1356 do_tlink (ld1_argv, object_lst);
1357 #endif
1358
1359 /* If -r or they will be run via some other method, do not build the
1360 constructor or destructor list, just return now. */
1361 if (rflag
1362 #ifndef COLLECT_EXPORT_LIST
1363 || ! do_collecting
1364 #endif
1365 )
1366 {
1367 #ifdef COLLECT_EXPORT_LIST
1368 /* Do the link we avoided above if we are exiting. */
1369 do_tlink (ld1_argv, object_lst);
1370
1371 /* But make sure we delete the export file we may have created. */
1372 if (export_file != 0 && export_file[0])
1373 maybe_unlink (export_file);
1374 #endif
1375 maybe_unlink (c_file);
1376 maybe_unlink (o_file);
1377 return 0;
1378 }
1379
1380 /* Examine the namelist with nm and search it for static constructors
1381 and destructors to call.
1382 Write the constructor and destructor tables to a .s file and reload. */
1383
1384 /* On AIX we already scanned for global constructors/destructors. */
1385 #ifndef COLLECT_EXPORT_LIST
1386 scan_prog_file (output_file, PASS_FIRST);
1387 #endif
1388
1389 #ifdef SCAN_LIBRARIES
1390 scan_libraries (output_file);
1391 #endif
1392
1393 if (debug)
1394 {
1395 notice ("%d constructor(s) found\n", constructors.number);
1396 notice ("%d destructor(s) found\n", destructors.number);
1397 notice ("%d frame table(s) found\n", frame_tables.number);
1398 }
1399
1400 if (constructors.number == 0 && destructors.number == 0
1401 && frame_tables.number == 0
1402 #if defined (SCAN_LIBRARIES) || defined (COLLECT_EXPORT_LIST)
1403 /* If we will be running these functions ourselves, we want to emit
1404 stubs into the shared library so that we do not have to relink
1405 dependent programs when we add static objects. */
1406 && ! shared_obj
1407 #endif
1408 )
1409 {
1410 #ifdef COLLECT_EXPORT_LIST
1411 /* Do tlink without additional code generation. */
1412 do_tlink (ld1_argv, object_lst);
1413 #endif
1414 /* Strip now if it was requested on the command line. */
1415 if (strip_flag)
1416 {
1417 char **real_strip_argv = XCNEWVEC (char *, 3);
1418 const char ** strip_argv = CONST_CAST2 (const char **, char **,
1419 real_strip_argv);
1420
1421 strip_argv[0] = strip_file_name;
1422 strip_argv[1] = output_file;
1423 strip_argv[2] = (char *) 0;
1424 fork_execute ("strip", real_strip_argv);
1425 }
1426
1427 #ifdef COLLECT_EXPORT_LIST
1428 maybe_unlink (export_file);
1429 #endif
1430 maybe_unlink (c_file);
1431 maybe_unlink (o_file);
1432 return 0;
1433 }
1434
1435 /* Sort ctor and dtor lists by priority. */
1436 sort_ids (&constructors);
1437 sort_ids (&destructors);
1438
1439 maybe_unlink(output_file);
1440 outf = fopen (c_file, "w");
1441 if (outf == (FILE *) 0)
1442 fatal_perror ("fopen %s", c_file);
1443
1444 write_c_file (outf, c_file);
1445
1446 if (fclose (outf))
1447 fatal_perror ("fclose %s", c_file);
1448
1449 /* Tell the linker that we have initializer and finalizer functions. */
1450 #ifdef LD_INIT_SWITCH
1451 #ifdef COLLECT_EXPORT_LIST
1452 *ld2++ = concat (LD_INIT_SWITCH, ":", initname, ":", fininame, NULL);
1453 #else
1454 *ld2++ = LD_INIT_SWITCH;
1455 *ld2++ = initname;
1456 *ld2++ = LD_FINI_SWITCH;
1457 *ld2++ = fininame;
1458 #endif
1459 #endif
1460
1461 #ifdef COLLECT_EXPORT_LIST
1462 if (shared_obj)
1463 {
1464 /* If we did not add export flag to link arguments before, add it to
1465 second link phase now. No new exports should have been added. */
1466 if (! exports.first)
1467 *ld2++ = concat ("-bE:", export_file, NULL);
1468
1469 #ifndef LD_INIT_SWITCH
1470 add_to_list (&exports, initname);
1471 add_to_list (&exports, fininame);
1472 add_to_list (&exports, "_GLOBAL__DI");
1473 add_to_list (&exports, "_GLOBAL__DD");
1474 #endif
1475 exportf = fopen (export_file, "w");
1476 if (exportf == (FILE *) 0)
1477 fatal_perror ("fopen %s", export_file);
1478 write_aix_file (exportf, exports.first);
1479 if (fclose (exportf))
1480 fatal_perror ("fclose %s", export_file);
1481 }
1482 #endif
1483
1484 /* End of arguments to second link phase. */
1485 *ld2 = (char*) 0;
1486
1487 if (debug)
1488 {
1489 fprintf (stderr, "\n========== output_file = %s, c_file = %s\n",
1490 output_file, c_file);
1491 write_c_file (stderr, "stderr");
1492 fprintf (stderr, "========== end of c_file\n\n");
1493 #ifdef COLLECT_EXPORT_LIST
1494 fprintf (stderr, "\n========== export_file = %s\n", export_file);
1495 write_aix_file (stderr, exports.first);
1496 fprintf (stderr, "========== end of export_file\n\n");
1497 #endif
1498 }
1499
1500 /* Assemble the constructor and destructor tables.
1501 Link the tables in with the rest of the program. */
1502
1503 fork_execute ("gcc", c_argv);
1504 #ifdef COLLECT_EXPORT_LIST
1505 /* On AIX we must call tlink because of possible templates resolution. */
1506 do_tlink (ld2_argv, object_lst);
1507 #else
1508 /* Otherwise, simply call ld because tlink is already done. */
1509 fork_execute ("ld", ld2_argv);
1510
1511 /* Let scan_prog_file do any final mods (OSF/rose needs this for
1512 constructors/destructors in shared libraries. */
1513 scan_prog_file (output_file, PASS_SECOND);
1514 #endif
1515
1516 maybe_unlink (c_file);
1517 maybe_unlink (o_file);
1518
1519 #ifdef COLLECT_EXPORT_LIST
1520 maybe_unlink (export_file);
1521 #endif
1522
1523 return 0;
1524 }
1525
1526 \f
1527 /* Wait for a process to finish, and exit if a nonzero status is found. */
1528
1529 int
1530 collect_wait (const char *prog, struct pex_obj *pex)
1531 {
1532 int status;
1533
1534 if (!pex_get_status (pex, 1, &status))
1535 fatal_perror ("can't get program status");
1536 pex_free (pex);
1537
1538 if (status)
1539 {
1540 if (WIFSIGNALED (status))
1541 {
1542 int sig = WTERMSIG (status);
1543 error ("%s terminated with signal %d [%s]%s",
1544 prog, sig, strsignal(sig),
1545 WCOREDUMP(status) ? ", core dumped" : "");
1546 collect_exit (FATAL_EXIT_CODE);
1547 }
1548
1549 if (WIFEXITED (status))
1550 return WEXITSTATUS (status);
1551 }
1552 return 0;
1553 }
1554
1555 static void
1556 do_wait (const char *prog, struct pex_obj *pex)
1557 {
1558 int ret = collect_wait (prog, pex);
1559 if (ret != 0)
1560 {
1561 error ("%s returned %d exit status", prog, ret);
1562 collect_exit (ret);
1563 }
1564
1565 if (response_file)
1566 {
1567 unlink (response_file);
1568 response_file = NULL;
1569 }
1570 }
1571
1572 \f
1573 /* Execute a program, and wait for the reply. */
1574
1575 struct pex_obj *
1576 collect_execute (const char *prog, char **argv, const char *outname,
1577 const char *errname)
1578 {
1579 struct pex_obj *pex;
1580 const char *errmsg;
1581 int err;
1582 char *response_arg = NULL;
1583 char *response_argv[3] ATTRIBUTE_UNUSED;
1584
1585 if (HAVE_GNU_LD && at_file_supplied && argv[0] != NULL)
1586 {
1587 /* If using @file arguments, create a temporary file and put the
1588 contents of argv into it. Then change argv to an array corresponding
1589 to a single argument @FILE, where FILE is the temporary filename. */
1590
1591 char **current_argv = argv + 1;
1592 char *argv0 = argv[0];
1593 int status;
1594 FILE *f;
1595
1596 /* Note: we assume argv contains at least one element; this is
1597 checked above. */
1598
1599 response_file = make_temp_file ("");
1600
1601 f = fopen (response_file, "w");
1602
1603 if (f == NULL)
1604 fatal ("could not open response file %s", response_file);
1605
1606 status = writeargv (current_argv, f);
1607
1608 if (status)
1609 fatal ("could not write to response file %s", response_file);
1610
1611 status = fclose (f);
1612
1613 if (EOF == status)
1614 fatal ("could not close response file %s", response_file);
1615
1616 response_arg = concat ("@", response_file, NULL);
1617 response_argv[0] = argv0;
1618 response_argv[1] = response_arg;
1619 response_argv[2] = NULL;
1620
1621 argv = response_argv;
1622 }
1623
1624 if (vflag || debug)
1625 {
1626 char **p_argv;
1627 const char *str;
1628
1629 if (argv[0])
1630 fprintf (stderr, "%s", argv[0]);
1631 else
1632 notice ("[cannot find %s]", prog);
1633
1634 for (p_argv = &argv[1]; (str = *p_argv) != (char *) 0; p_argv++)
1635 fprintf (stderr, " %s", str);
1636
1637 fprintf (stderr, "\n");
1638 }
1639
1640 fflush (stdout);
1641 fflush (stderr);
1642
1643 /* If we cannot find a program we need, complain error. Do this here
1644 since we might not end up needing something that we could not find. */
1645
1646 if (argv[0] == 0)
1647 fatal ("cannot find '%s'", prog);
1648
1649 pex = pex_init (0, "collect2", NULL);
1650 if (pex == NULL)
1651 fatal_perror ("pex_init failed");
1652
1653 errmsg = pex_run (pex, PEX_LAST | PEX_SEARCH, argv[0], argv, outname,
1654 errname, &err);
1655 if (errmsg != NULL)
1656 {
1657 if (err != 0)
1658 {
1659 errno = err;
1660 fatal_perror (errmsg);
1661 }
1662 else
1663 fatal (errmsg);
1664 }
1665
1666 if (response_arg)
1667 free (response_arg);
1668
1669 return pex;
1670 }
1671
1672 static void
1673 fork_execute (const char *prog, char **argv)
1674 {
1675 struct pex_obj *pex;
1676
1677 pex = collect_execute (prog, argv, NULL, NULL);
1678 do_wait (prog, pex);
1679 }
1680 \f
1681 /* Unlink a file unless we are debugging. */
1682
1683 static void
1684 maybe_unlink (const char *file)
1685 {
1686 if (!debug)
1687 unlink_if_ordinary (file);
1688 else
1689 notice ("[Leaving %s]\n", file);
1690 }
1691
1692 \f
1693 static long sequence_number = 0;
1694
1695 /* Add a name to a linked list. */
1696
1697 static void
1698 add_to_list (struct head *head_ptr, const char *name)
1699 {
1700 struct id *newid
1701 = (struct id *) xcalloc (sizeof (struct id) + strlen (name), 1);
1702 struct id *p;
1703 strcpy (newid->name, name);
1704
1705 if (head_ptr->first)
1706 head_ptr->last->next = newid;
1707 else
1708 head_ptr->first = newid;
1709
1710 /* Check for duplicate symbols. */
1711 for (p = head_ptr->first;
1712 strcmp (name, p->name) != 0;
1713 p = p->next)
1714 ;
1715 if (p != newid)
1716 {
1717 head_ptr->last->next = 0;
1718 free (newid);
1719 return;
1720 }
1721
1722 newid->sequence = ++sequence_number;
1723 head_ptr->last = newid;
1724 head_ptr->number++;
1725 }
1726
1727 /* Grab the init priority number from an init function name that
1728 looks like "_GLOBAL_.I.12345.foo". */
1729
1730 static int
1731 extract_init_priority (const char *name)
1732 {
1733 int pos = 0, pri;
1734
1735 while (name[pos] == '_')
1736 ++pos;
1737 pos += 10; /* strlen ("GLOBAL__X_") */
1738
1739 /* Extract init_p number from ctor/dtor name. */
1740 pri = atoi (name + pos);
1741 return pri ? pri : DEFAULT_INIT_PRIORITY;
1742 }
1743
1744 /* Insertion sort the ids from ctor/dtor list HEAD_PTR in descending order.
1745 ctors will be run from right to left, dtors from left to right. */
1746
1747 static void
1748 sort_ids (struct head *head_ptr)
1749 {
1750 /* id holds the current element to insert. id_next holds the next
1751 element to insert. id_ptr iterates through the already sorted elements
1752 looking for the place to insert id. */
1753 struct id *id, *id_next, **id_ptr;
1754
1755 id = head_ptr->first;
1756
1757 /* We don't have any sorted elements yet. */
1758 head_ptr->first = NULL;
1759
1760 for (; id; id = id_next)
1761 {
1762 id_next = id->next;
1763 id->sequence = extract_init_priority (id->name);
1764
1765 for (id_ptr = &(head_ptr->first); ; id_ptr = &((*id_ptr)->next))
1766 if (*id_ptr == NULL
1767 /* If the sequence numbers are the same, we put the id from the
1768 file later on the command line later in the list. */
1769 || id->sequence > (*id_ptr)->sequence
1770 /* Hack: do lexical compare, too.
1771 || (id->sequence == (*id_ptr)->sequence
1772 && strcmp (id->name, (*id_ptr)->name) > 0) */
1773 )
1774 {
1775 id->next = *id_ptr;
1776 *id_ptr = id;
1777 break;
1778 }
1779 }
1780
1781 /* Now set the sequence numbers properly so write_c_file works. */
1782 for (id = head_ptr->first; id; id = id->next)
1783 id->sequence = ++sequence_number;
1784 }
1785
1786 /* Write: `prefix', the names on list LIST, `suffix'. */
1787
1788 static void
1789 write_list (FILE *stream, const char *prefix, struct id *list)
1790 {
1791 while (list)
1792 {
1793 fprintf (stream, "%sx%d,\n", prefix, list->sequence);
1794 list = list->next;
1795 }
1796 }
1797
1798 #if LINK_ELIMINATE_DUPLICATE_LDIRECTORIES
1799 /* Given a STRING, return nonzero if it occurs in the list in range
1800 [ARGS_BEGIN,ARGS_END). */
1801
1802 static int
1803 is_in_args (const char *string, const char **args_begin,
1804 const char **args_end)
1805 {
1806 const char **args_pointer;
1807 for (args_pointer = args_begin; args_pointer != args_end; ++args_pointer)
1808 if (strcmp (string, *args_pointer) == 0)
1809 return 1;
1810 return 0;
1811 }
1812 #endif /* LINK_ELIMINATE_DUPLICATE_LDIRECTORIES */
1813
1814 #ifdef COLLECT_EXPORT_LIST
1815 /* This function is really used only on AIX, but may be useful. */
1816 #if 0
1817 static int
1818 is_in_list (const char *prefix, struct id *list)
1819 {
1820 while (list)
1821 {
1822 if (!strcmp (prefix, list->name)) return 1;
1823 list = list->next;
1824 }
1825 return 0;
1826 }
1827 #endif
1828 #endif /* COLLECT_EXPORT_LIST */
1829
1830 /* Added for debugging purpose. */
1831 #ifdef COLLECT_EXPORT_LIST
1832 static void
1833 dump_list (FILE *stream, const char *prefix, struct id *list)
1834 {
1835 while (list)
1836 {
1837 fprintf (stream, "%s%s,\n", prefix, list->name);
1838 list = list->next;
1839 }
1840 }
1841 #endif
1842
1843 #if 0
1844 static void
1845 dump_prefix_list (FILE *stream, const char *prefix, struct prefix_list *list)
1846 {
1847 while (list)
1848 {
1849 fprintf (stream, "%s%s,\n", prefix, list->prefix);
1850 list = list->next;
1851 }
1852 }
1853 #endif
1854
1855 static void
1856 write_list_with_asm (FILE *stream, const char *prefix, struct id *list)
1857 {
1858 while (list)
1859 {
1860 fprintf (stream, "%sx%d __asm__ (\"%s\");\n",
1861 prefix, list->sequence, list->name);
1862 list = list->next;
1863 }
1864 }
1865
1866 /* Write out the constructor and destructor tables statically (for a shared
1867 object), along with the functions to execute them. */
1868
1869 static void
1870 write_c_file_stat (FILE *stream, const char *name ATTRIBUTE_UNUSED)
1871 {
1872 const char *p, *q;
1873 char *prefix, *r;
1874 int frames = (frame_tables.number > 0);
1875
1876 /* Figure out name of output_file, stripping off .so version. */
1877 p = strrchr (output_file, '/');
1878 if (p == 0)
1879 p = output_file;
1880 else
1881 p++;
1882 q = p;
1883 while (q)
1884 {
1885 q = strchr (q,'.');
1886 if (q == 0)
1887 {
1888 q = p + strlen (p);
1889 break;
1890 }
1891 else
1892 {
1893 if (strncmp (q, SHLIB_SUFFIX, strlen (SHLIB_SUFFIX)) == 0)
1894 {
1895 q += strlen (SHLIB_SUFFIX);
1896 break;
1897 }
1898 else
1899 q++;
1900 }
1901 }
1902 /* q points to null at end of the string (or . of the .so version) */
1903 prefix = XNEWVEC (char, q - p + 1);
1904 strncpy (prefix, p, q - p);
1905 prefix[q - p] = 0;
1906 for (r = prefix; *r; r++)
1907 if (!ISALNUM ((unsigned char)*r))
1908 *r = '_';
1909 if (debug)
1910 notice ("\nwrite_c_file - output name is %s, prefix is %s\n",
1911 output_file, prefix);
1912
1913 initname = concat ("_GLOBAL__FI_", prefix, NULL);
1914 fininame = concat ("_GLOBAL__FD_", prefix, NULL);
1915
1916 free (prefix);
1917
1918 /* Write the tables as C code. */
1919
1920 fprintf (stream, "static int count;\n");
1921 fprintf (stream, "typedef void entry_pt();\n");
1922 write_list_with_asm (stream, "extern entry_pt ", constructors.first);
1923
1924 if (frames)
1925 {
1926 write_list_with_asm (stream, "extern void *", frame_tables.first);
1927
1928 fprintf (stream, "\tstatic void *frame_table[] = {\n");
1929 write_list (stream, "\t\t&", frame_tables.first);
1930 fprintf (stream, "\t0\n};\n");
1931
1932 /* This must match what's in frame.h. */
1933 fprintf (stream, "struct object {\n");
1934 fprintf (stream, " void *pc_begin;\n");
1935 fprintf (stream, " void *pc_end;\n");
1936 fprintf (stream, " void *fde_begin;\n");
1937 fprintf (stream, " void *fde_array;\n");
1938 fprintf (stream, " __SIZE_TYPE__ count;\n");
1939 fprintf (stream, " struct object *next;\n");
1940 fprintf (stream, "};\n");
1941
1942 fprintf (stream, "extern void __register_frame_info_table (void *, struct object *);\n");
1943 fprintf (stream, "extern void *__deregister_frame_info (void *);\n");
1944
1945 fprintf (stream, "static void reg_frame () {\n");
1946 fprintf (stream, "\tstatic struct object ob;\n");
1947 fprintf (stream, "\t__register_frame_info_table (frame_table, &ob);\n");
1948 fprintf (stream, "\t}\n");
1949
1950 fprintf (stream, "static void dereg_frame () {\n");
1951 fprintf (stream, "\t__deregister_frame_info (frame_table);\n");
1952 fprintf (stream, "\t}\n");
1953 }
1954
1955 fprintf (stream, "void %s() {\n", initname);
1956 if (constructors.number > 0 || frames)
1957 {
1958 fprintf (stream, "\tstatic entry_pt *ctors[] = {\n");
1959 write_list (stream, "\t\t", constructors.first);
1960 if (frames)
1961 fprintf (stream, "\treg_frame,\n");
1962 fprintf (stream, "\t};\n");
1963 fprintf (stream, "\tentry_pt **p;\n");
1964 fprintf (stream, "\tif (count++ != 0) return;\n");
1965 fprintf (stream, "\tp = ctors + %d;\n", constructors.number + frames);
1966 fprintf (stream, "\twhile (p > ctors) (*--p)();\n");
1967 }
1968 else
1969 fprintf (stream, "\t++count;\n");
1970 fprintf (stream, "}\n");
1971 write_list_with_asm (stream, "extern entry_pt ", destructors.first);
1972 fprintf (stream, "void %s() {\n", fininame);
1973 if (destructors.number > 0 || frames)
1974 {
1975 fprintf (stream, "\tstatic entry_pt *dtors[] = {\n");
1976 write_list (stream, "\t\t", destructors.first);
1977 if (frames)
1978 fprintf (stream, "\tdereg_frame,\n");
1979 fprintf (stream, "\t};\n");
1980 fprintf (stream, "\tentry_pt **p;\n");
1981 fprintf (stream, "\tif (--count != 0) return;\n");
1982 fprintf (stream, "\tp = dtors;\n");
1983 fprintf (stream, "\twhile (p < dtors + %d) (*p++)();\n",
1984 destructors.number + frames);
1985 }
1986 fprintf (stream, "}\n");
1987
1988 if (shared_obj)
1989 {
1990 COLLECT_SHARED_INIT_FUNC(stream, initname);
1991 COLLECT_SHARED_FINI_FUNC(stream, fininame);
1992 }
1993 }
1994
1995 /* Write the constructor/destructor tables. */
1996
1997 #ifndef LD_INIT_SWITCH
1998 static void
1999 write_c_file_glob (FILE *stream, const char *name ATTRIBUTE_UNUSED)
2000 {
2001 /* Write the tables as C code. */
2002
2003 int frames = (frame_tables.number > 0);
2004
2005 fprintf (stream, "typedef void entry_pt();\n\n");
2006
2007 write_list_with_asm (stream, "extern entry_pt ", constructors.first);
2008
2009 if (frames)
2010 {
2011 write_list_with_asm (stream, "extern void *", frame_tables.first);
2012
2013 fprintf (stream, "\tstatic void *frame_table[] = {\n");
2014 write_list (stream, "\t\t&", frame_tables.first);
2015 fprintf (stream, "\t0\n};\n");
2016
2017 /* This must match what's in frame.h. */
2018 fprintf (stream, "struct object {\n");
2019 fprintf (stream, " void *pc_begin;\n");
2020 fprintf (stream, " void *pc_end;\n");
2021 fprintf (stream, " void *fde_begin;\n");
2022 fprintf (stream, " void *fde_array;\n");
2023 fprintf (stream, " __SIZE_TYPE__ count;\n");
2024 fprintf (stream, " struct object *next;\n");
2025 fprintf (stream, "};\n");
2026
2027 fprintf (stream, "extern void __register_frame_info_table (void *, struct object *);\n");
2028 fprintf (stream, "extern void *__deregister_frame_info (void *);\n");
2029
2030 fprintf (stream, "static void reg_frame () {\n");
2031 fprintf (stream, "\tstatic struct object ob;\n");
2032 fprintf (stream, "\t__register_frame_info_table (frame_table, &ob);\n");
2033 fprintf (stream, "\t}\n");
2034
2035 fprintf (stream, "static void dereg_frame () {\n");
2036 fprintf (stream, "\t__deregister_frame_info (frame_table);\n");
2037 fprintf (stream, "\t}\n");
2038 }
2039
2040 fprintf (stream, "\nentry_pt * __CTOR_LIST__[] = {\n");
2041 fprintf (stream, "\t(entry_pt *) %d,\n", constructors.number + frames);
2042 write_list (stream, "\t", constructors.first);
2043 if (frames)
2044 fprintf (stream, "\treg_frame,\n");
2045 fprintf (stream, "\t0\n};\n\n");
2046
2047 write_list_with_asm (stream, "extern entry_pt ", destructors.first);
2048
2049 fprintf (stream, "\nentry_pt * __DTOR_LIST__[] = {\n");
2050 fprintf (stream, "\t(entry_pt *) %d,\n", destructors.number + frames);
2051 write_list (stream, "\t", destructors.first);
2052 if (frames)
2053 fprintf (stream, "\tdereg_frame,\n");
2054 fprintf (stream, "\t0\n};\n\n");
2055
2056 fprintf (stream, "extern entry_pt %s;\n", NAME__MAIN);
2057 fprintf (stream, "entry_pt *__main_reference = %s;\n\n", NAME__MAIN);
2058 }
2059 #endif /* ! LD_INIT_SWITCH */
2060
2061 static void
2062 write_c_file (FILE *stream, const char *name)
2063 {
2064 #ifndef LD_INIT_SWITCH
2065 if (! shared_obj)
2066 write_c_file_glob (stream, name);
2067 else
2068 #endif
2069 write_c_file_stat (stream, name);
2070 }
2071
2072 #ifdef COLLECT_EXPORT_LIST
2073 static void
2074 write_aix_file (FILE *stream, struct id *list)
2075 {
2076 for (; list; list = list->next)
2077 {
2078 fputs (list->name, stream);
2079 putc ('\n', stream);
2080 }
2081 }
2082 #endif
2083 \f
2084 #ifdef OBJECT_FORMAT_NONE
2085
2086 /* Generic version to scan the name list of the loaded program for
2087 the symbols g++ uses for static constructors and destructors.
2088
2089 The constructor table begins at __CTOR_LIST__ and contains a count
2090 of the number of pointers (or -1 if the constructors are built in a
2091 separate section by the linker), followed by the pointers to the
2092 constructor functions, terminated with a null pointer. The
2093 destructor table has the same format, and begins at __DTOR_LIST__. */
2094
2095 static void
2096 scan_prog_file (const char *prog_name, enum pass which_pass)
2097 {
2098 void (*int_handler) (int);
2099 #ifdef SIGQUIT
2100 void (*quit_handler) (int);
2101 #endif
2102 char *real_nm_argv[4];
2103 const char **nm_argv = CONST_CAST2 (const char **, char**, real_nm_argv);
2104 int argc = 0;
2105 struct pex_obj *pex;
2106 const char *errmsg;
2107 int err;
2108 char *p, buf[1024];
2109 FILE *inf;
2110
2111 if (which_pass == PASS_SECOND)
2112 return;
2113
2114 /* If we do not have an `nm', complain. */
2115 if (nm_file_name == 0)
2116 fatal ("cannot find 'nm'");
2117
2118 nm_argv[argc++] = nm_file_name;
2119 if (NM_FLAGS[0] != '\0')
2120 nm_argv[argc++] = NM_FLAGS;
2121
2122 nm_argv[argc++] = prog_name;
2123 nm_argv[argc++] = (char *) 0;
2124
2125 /* Trace if needed. */
2126 if (vflag)
2127 {
2128 const char **p_argv;
2129 const char *str;
2130
2131 for (p_argv = &nm_argv[0]; (str = *p_argv) != (char *) 0; p_argv++)
2132 fprintf (stderr, " %s", str);
2133
2134 fprintf (stderr, "\n");
2135 }
2136
2137 fflush (stdout);
2138 fflush (stderr);
2139
2140 pex = pex_init (PEX_USE_PIPES, "collect2", NULL);
2141 if (pex == NULL)
2142 fatal_perror ("pex_init failed");
2143
2144 errmsg = pex_run (pex, 0, nm_file_name, real_nm_argv, NULL, NULL, &err);
2145 if (errmsg != NULL)
2146 {
2147 if (err != 0)
2148 {
2149 errno = err;
2150 fatal_perror (errmsg);
2151 }
2152 else
2153 fatal (errmsg);
2154 }
2155
2156 int_handler = (void (*) (int)) signal (SIGINT, SIG_IGN);
2157 #ifdef SIGQUIT
2158 quit_handler = (void (*) (int)) signal (SIGQUIT, SIG_IGN);
2159 #endif
2160
2161 inf = pex_read_output (pex, 0);
2162 if (inf == NULL)
2163 fatal_perror ("can't open nm output");
2164
2165 if (debug)
2166 fprintf (stderr, "\nnm output with constructors/destructors.\n");
2167
2168 /* Read each line of nm output. */
2169 while (fgets (buf, sizeof buf, inf) != (char *) 0)
2170 {
2171 int ch, ch2;
2172 char *name, *end;
2173
2174 /* If it contains a constructor or destructor name, add the name
2175 to the appropriate list. */
2176
2177 for (p = buf; (ch = *p) != '\0' && ch != '\n' && ch != '_'; p++)
2178 if (ch == ' ' && p[1] == 'U' && p[2] == ' ')
2179 break;
2180
2181 if (ch != '_')
2182 continue;
2183
2184 name = p;
2185 /* Find the end of the symbol name.
2186 Do not include `|', because Encore nm can tack that on the end. */
2187 for (end = p; (ch2 = *end) != '\0' && !ISSPACE (ch2) && ch2 != '|';
2188 end++)
2189 continue;
2190
2191
2192 *end = '\0';
2193 switch (is_ctor_dtor (name))
2194 {
2195 case SYM_CTOR:
2196 if (which_pass != PASS_LIB)
2197 add_to_list (&constructors, name);
2198 break;
2199
2200 case SYM_DTOR:
2201 if (which_pass != PASS_LIB)
2202 add_to_list (&destructors, name);
2203 break;
2204
2205 case SYM_INIT:
2206 if (which_pass != PASS_LIB)
2207 fatal ("init function found in object %s", prog_name);
2208 #ifndef LD_INIT_SWITCH
2209 add_to_list (&constructors, name);
2210 #endif
2211 break;
2212
2213 case SYM_FINI:
2214 if (which_pass != PASS_LIB)
2215 fatal ("fini function found in object %s", prog_name);
2216 #ifndef LD_FINI_SWITCH
2217 add_to_list (&destructors, name);
2218 #endif
2219 break;
2220
2221 case SYM_DWEH:
2222 if (which_pass != PASS_LIB)
2223 add_to_list (&frame_tables, name);
2224 break;
2225
2226 default: /* not a constructor or destructor */
2227 continue;
2228 }
2229
2230 if (debug)
2231 fprintf (stderr, "\t%s\n", buf);
2232 }
2233
2234 if (debug)
2235 fprintf (stderr, "\n");
2236
2237 do_wait (nm_file_name, pex);
2238
2239 signal (SIGINT, int_handler);
2240 #ifdef SIGQUIT
2241 signal (SIGQUIT, quit_handler);
2242 #endif
2243 }
2244
2245 #ifdef LDD_SUFFIX
2246
2247 /* Use the List Dynamic Dependencies program to find shared libraries that
2248 the output file depends upon and their initialization/finalization
2249 routines, if any. */
2250
2251 static void
2252 scan_libraries (const char *prog_name)
2253 {
2254 static struct head libraries; /* list of shared libraries found */
2255 struct id *list;
2256 void (*int_handler) (int);
2257 #ifdef SIGQUIT
2258 void (*quit_handler) (int);
2259 #endif
2260 char *real_ldd_argv[4];
2261 const char **ldd_argv = (const char **) real_ldd_argv;
2262 int argc = 0;
2263 struct pex_obj *pex;
2264 const char *errmsg;
2265 int err;
2266 char buf[1024];
2267 FILE *inf;
2268
2269 /* If we do not have an `ldd', complain. */
2270 if (ldd_file_name == 0)
2271 {
2272 error ("cannot find 'ldd'");
2273 return;
2274 }
2275
2276 ldd_argv[argc++] = ldd_file_name;
2277 ldd_argv[argc++] = prog_name;
2278 ldd_argv[argc++] = (char *) 0;
2279
2280 /* Trace if needed. */
2281 if (vflag)
2282 {
2283 const char **p_argv;
2284 const char *str;
2285
2286 for (p_argv = &ldd_argv[0]; (str = *p_argv) != (char *) 0; p_argv++)
2287 fprintf (stderr, " %s", str);
2288
2289 fprintf (stderr, "\n");
2290 }
2291
2292 fflush (stdout);
2293 fflush (stderr);
2294
2295 pex = pex_init (PEX_USE_PIPES, "collect2", NULL);
2296 if (pex == NULL)
2297 fatal_perror ("pex_init failed");
2298
2299 errmsg = pex_run (pex, 0, ldd_file_name, real_ldd_argv, NULL, NULL, &err);
2300 if (errmsg != NULL)
2301 {
2302 if (err != 0)
2303 {
2304 errno = err;
2305 fatal_perror (errmsg);
2306 }
2307 else
2308 fatal (errmsg);
2309 }
2310
2311 int_handler = (void (*) (int)) signal (SIGINT, SIG_IGN);
2312 #ifdef SIGQUIT
2313 quit_handler = (void (*) (int)) signal (SIGQUIT, SIG_IGN);
2314 #endif
2315
2316 inf = pex_read_output (pex, 0);
2317 if (inf == NULL)
2318 fatal_perror ("can't open ldd output");
2319
2320 if (debug)
2321 notice ("\nldd output with constructors/destructors.\n");
2322
2323 /* Read each line of ldd output. */
2324 while (fgets (buf, sizeof buf, inf) != (char *) 0)
2325 {
2326 int ch2;
2327 char *name, *end, *p = buf;
2328
2329 /* Extract names of libraries and add to list. */
2330 PARSE_LDD_OUTPUT (p);
2331 if (p == 0)
2332 continue;
2333
2334 name = p;
2335 if (strncmp (name, "not found", sizeof ("not found") - 1) == 0)
2336 fatal ("dynamic dependency %s not found", buf);
2337
2338 /* Find the end of the symbol name. */
2339 for (end = p;
2340 (ch2 = *end) != '\0' && ch2 != '\n' && !ISSPACE (ch2) && ch2 != '|';
2341 end++)
2342 continue;
2343 *end = '\0';
2344
2345 if (access (name, R_OK) == 0)
2346 add_to_list (&libraries, name);
2347 else
2348 fatal ("unable to open dynamic dependency '%s'", buf);
2349
2350 if (debug)
2351 fprintf (stderr, "\t%s\n", buf);
2352 }
2353 if (debug)
2354 fprintf (stderr, "\n");
2355
2356 do_wait (ldd_file_name, pex);
2357
2358 signal (SIGINT, int_handler);
2359 #ifdef SIGQUIT
2360 signal (SIGQUIT, quit_handler);
2361 #endif
2362
2363 /* Now iterate through the library list adding their symbols to
2364 the list. */
2365 for (list = libraries.first; list; list = list->next)
2366 scan_prog_file (list->name, PASS_LIB);
2367 }
2368
2369 #endif /* LDD_SUFFIX */
2370
2371 #endif /* OBJECT_FORMAT_NONE */
2372
2373 \f
2374 /*
2375 * COFF specific stuff.
2376 */
2377
2378 #ifdef OBJECT_FORMAT_COFF
2379
2380 #if defined (EXTENDED_COFF)
2381
2382 # define GCC_SYMBOLS(X) (SYMHEADER(X).isymMax + SYMHEADER(X).iextMax)
2383 # define GCC_SYMENT SYMR
2384 # define GCC_OK_SYMBOL(X) ((X).st == stProc || (X).st == stGlobal)
2385 # define GCC_SYMINC(X) (1)
2386 # define GCC_SYMZERO(X) (SYMHEADER(X).isymMax)
2387 # define GCC_CHECK_HDR(X) (PSYMTAB(X) != 0)
2388
2389 #else
2390
2391 # define GCC_SYMBOLS(X) (HEADER(ldptr).f_nsyms)
2392 # define GCC_SYMENT SYMENT
2393 # if defined (C_WEAKEXT)
2394 # define GCC_OK_SYMBOL(X) \
2395 (((X).n_sclass == C_EXT || (X).n_sclass == C_WEAKEXT) && \
2396 ((X).n_scnum > N_UNDEF) && \
2397 (aix64_flag \
2398 || (((X).n_type & N_TMASK) == (DT_NON << N_BTSHFT) \
2399 || ((X).n_type & N_TMASK) == (DT_FCN << N_BTSHFT))))
2400 # define GCC_UNDEF_SYMBOL(X) \
2401 (((X).n_sclass == C_EXT || (X).n_sclass == C_WEAKEXT) && \
2402 ((X).n_scnum == N_UNDEF))
2403 # else
2404 # define GCC_OK_SYMBOL(X) \
2405 (((X).n_sclass == C_EXT) && \
2406 ((X).n_scnum > N_UNDEF) && \
2407 (aix64_flag \
2408 || (((X).n_type & N_TMASK) == (DT_NON << N_BTSHFT) \
2409 || ((X).n_type & N_TMASK) == (DT_FCN << N_BTSHFT))))
2410 # define GCC_UNDEF_SYMBOL(X) \
2411 (((X).n_sclass == C_EXT) && ((X).n_scnum == N_UNDEF))
2412 # endif
2413 # define GCC_SYMINC(X) ((X).n_numaux+1)
2414 # define GCC_SYMZERO(X) 0
2415
2416 /* 0757 = U803XTOCMAGIC (AIX 4.3) and 0767 = U64_TOCMAGIC (AIX V5) */
2417 #if TARGET_AIX_VERSION >= 51
2418 # define GCC_CHECK_HDR(X) \
2419 ((HEADER (X).f_magic == U802TOCMAGIC && ! aix64_flag) \
2420 || (HEADER (X).f_magic == 0767 && aix64_flag))
2421 #else
2422 # define GCC_CHECK_HDR(X) \
2423 ((HEADER (X).f_magic == U802TOCMAGIC && ! aix64_flag) \
2424 || (HEADER (X).f_magic == 0757 && aix64_flag))
2425 #endif
2426
2427 #endif
2428
2429 #ifdef COLLECT_EXPORT_LIST
2430 /* Array of standard AIX libraries which should not
2431 be scanned for ctors/dtors. */
2432 static const char *const aix_std_libs[] = {
2433 "/unix",
2434 "/lib/libc.a",
2435 "/lib/libm.a",
2436 "/lib/libc_r.a",
2437 "/lib/libm_r.a",
2438 "/usr/lib/libc.a",
2439 "/usr/lib/libm.a",
2440 "/usr/lib/libc_r.a",
2441 "/usr/lib/libm_r.a",
2442 "/usr/lib/threads/libc.a",
2443 "/usr/ccs/lib/libc.a",
2444 "/usr/ccs/lib/libm.a",
2445 "/usr/ccs/lib/libc_r.a",
2446 "/usr/ccs/lib/libm_r.a",
2447 NULL
2448 };
2449
2450 /* This function checks the filename and returns 1
2451 if this name matches the location of a standard AIX library. */
2452 static int ignore_library (const char *);
2453 static int
2454 ignore_library (const char *name)
2455 {
2456 const char *const *p;
2457
2458 for (p = &aix_std_libs[0]; *p != NULL; ++p)
2459 if (strcmp (name, *p) == 0)
2460 return 1;
2461 return 0;
2462 }
2463 #endif /* COLLECT_EXPORT_LIST */
2464
2465 #if defined (HAVE_DECL_LDGETNAME) && !HAVE_DECL_LDGETNAME
2466 extern char *ldgetname (LDFILE *, GCC_SYMENT *);
2467 #endif
2468
2469 /* COFF version to scan the name list of the loaded program for
2470 the symbols g++ uses for static constructors and destructors.
2471
2472 The constructor table begins at __CTOR_LIST__ and contains a count
2473 of the number of pointers (or -1 if the constructors are built in a
2474 separate section by the linker), followed by the pointers to the
2475 constructor functions, terminated with a null pointer. The
2476 destructor table has the same format, and begins at __DTOR_LIST__. */
2477
2478 static void
2479 scan_prog_file (const char *prog_name, enum pass which_pass)
2480 {
2481 LDFILE *ldptr = NULL;
2482 int sym_index, sym_count;
2483 int is_shared = 0;
2484
2485 if (which_pass != PASS_FIRST && which_pass != PASS_OBJ)
2486 return;
2487
2488 #ifdef COLLECT_EXPORT_LIST
2489 /* We do not need scanning for some standard C libraries. */
2490 if (which_pass == PASS_FIRST && ignore_library (prog_name))
2491 return;
2492
2493 /* On AIX we have a loop, because there is not much difference
2494 between an object and an archive. This trick allows us to
2495 eliminate scan_libraries() function. */
2496 do
2497 {
2498 #endif
2499 /* Some platforms (e.g. OSF4) declare ldopen as taking a
2500 non-const char * filename parameter, even though it will not
2501 modify that string. So we must cast away const-ness here,
2502 using CONST_CAST to prevent complaints from -Wcast-qual. */
2503 if ((ldptr = ldopen (CONST_CAST (char *, prog_name), ldptr)) != NULL)
2504 {
2505 if (! MY_ISCOFF (HEADER (ldptr).f_magic))
2506 fatal ("%s: not a COFF file", prog_name);
2507
2508 if (GCC_CHECK_HDR (ldptr))
2509 {
2510 sym_count = GCC_SYMBOLS (ldptr);
2511 sym_index = GCC_SYMZERO (ldptr);
2512
2513 #ifdef COLLECT_EXPORT_LIST
2514 /* Is current archive member a shared object? */
2515 is_shared = HEADER (ldptr).f_flags & F_SHROBJ;
2516 #endif
2517
2518 while (sym_index < sym_count)
2519 {
2520 GCC_SYMENT symbol;
2521
2522 if (ldtbread (ldptr, sym_index, &symbol) <= 0)
2523 break;
2524 sym_index += GCC_SYMINC (symbol);
2525
2526 if (GCC_OK_SYMBOL (symbol))
2527 {
2528 char *name;
2529
2530 if ((name = ldgetname (ldptr, &symbol)) == NULL)
2531 continue; /* Should never happen. */
2532
2533 #ifdef XCOFF_DEBUGGING_INFO
2534 /* All AIX function names have a duplicate entry
2535 beginning with a dot. */
2536 if (*name == '.')
2537 ++name;
2538 #endif
2539
2540 switch (is_ctor_dtor (name))
2541 {
2542 case SYM_CTOR:
2543 if (! is_shared)
2544 add_to_list (&constructors, name);
2545 #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
2546 if (which_pass == PASS_OBJ)
2547 add_to_list (&exports, name);
2548 #endif
2549 break;
2550
2551 case SYM_DTOR:
2552 if (! is_shared)
2553 add_to_list (&destructors, name);
2554 #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
2555 if (which_pass == PASS_OBJ)
2556 add_to_list (&exports, name);
2557 #endif
2558 break;
2559
2560 #ifdef COLLECT_EXPORT_LIST
2561 case SYM_INIT:
2562 #ifndef LD_INIT_SWITCH
2563 if (is_shared)
2564 add_to_list (&constructors, name);
2565 #endif
2566 break;
2567
2568 case SYM_FINI:
2569 #ifndef LD_INIT_SWITCH
2570 if (is_shared)
2571 add_to_list (&destructors, name);
2572 #endif
2573 break;
2574 #endif
2575
2576 case SYM_DWEH:
2577 if (! is_shared)
2578 add_to_list (&frame_tables, name);
2579 #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
2580 if (which_pass == PASS_OBJ)
2581 add_to_list (&exports, name);
2582 #endif
2583 break;
2584
2585 default: /* not a constructor or destructor */
2586 #ifdef COLLECT_EXPORT_LIST
2587 /* Explicitly export all global symbols when
2588 building a shared object on AIX, but do not
2589 re-export symbols from another shared object
2590 and do not export symbols if the user
2591 provides an explicit export list. */
2592 if (shared_obj && !is_shared
2593 && which_pass == PASS_OBJ && !export_flag)
2594 add_to_list (&exports, name);
2595 #endif
2596 continue;
2597 }
2598
2599 if (debug)
2600 #if !defined(EXTENDED_COFF)
2601 fprintf (stderr, "\tsec=%d class=%d type=%s%o %s\n",
2602 symbol.n_scnum, symbol.n_sclass,
2603 (symbol.n_type ? "0" : ""), symbol.n_type,
2604 name);
2605 #else
2606 fprintf (stderr,
2607 "\tiss = %5d, value = %5ld, index = %5d, name = %s\n",
2608 symbol.iss, (long) symbol.value, symbol.index, name);
2609 #endif
2610 }
2611 }
2612 }
2613 #ifdef COLLECT_EXPORT_LIST
2614 else
2615 {
2616 /* If archive contains both 32-bit and 64-bit objects,
2617 we want to skip objects in other mode so mismatch normal. */
2618 if (debug)
2619 fprintf (stderr, "%s : magic=%o aix64=%d mismatch\n",
2620 prog_name, HEADER (ldptr).f_magic, aix64_flag);
2621 }
2622 #endif
2623 }
2624 else
2625 {
2626 fatal ("%s: cannot open as COFF file", prog_name);
2627 }
2628 #ifdef COLLECT_EXPORT_LIST
2629 /* On AIX loop continues while there are more members in archive. */
2630 }
2631 while (ldclose (ldptr) == FAILURE);
2632 #else
2633 /* Otherwise we simply close ldptr. */
2634 (void) ldclose(ldptr);
2635 #endif
2636 }
2637 #endif /* OBJECT_FORMAT_COFF */
2638
2639 #ifdef COLLECT_EXPORT_LIST
2640 /* Given a library name without "lib" prefix, this function
2641 returns a full library name including a path. */
2642 static char *
2643 resolve_lib_name (const char *name)
2644 {
2645 char *lib_buf;
2646 int i, j, l = 0;
2647 /* Library extensions for AIX dynamic linking. */
2648 const char * const libexts[2] = {"a", "so"};
2649
2650 for (i = 0; libpaths[i]; i++)
2651 if (libpaths[i]->max_len > l)
2652 l = libpaths[i]->max_len;
2653
2654 lib_buf = XNEWVEC (char, l + strlen(name) + 10);
2655
2656 for (i = 0; libpaths[i]; i++)
2657 {
2658 struct prefix_list *list = libpaths[i]->plist;
2659 for (; list; list = list->next)
2660 {
2661 /* The following lines are needed because path_prefix list
2662 may contain directories both with trailing '/' and
2663 without it. */
2664 const char *p = "";
2665 if (list->prefix[strlen(list->prefix)-1] != '/')
2666 p = "/";
2667 for (j = 0; j < 2; j++)
2668 {
2669 sprintf (lib_buf, "%s%slib%s.%s",
2670 list->prefix, p, name,
2671 libexts[(j + aixrtl_flag) % 2]);
2672 if (debug) fprintf (stderr, "searching for: %s\n", lib_buf);
2673 if (file_exists (lib_buf))
2674 {
2675 if (debug) fprintf (stderr, "found: %s\n", lib_buf);
2676 return (lib_buf);
2677 }
2678 }
2679 }
2680 }
2681 if (debug)
2682 fprintf (stderr, "not found\n");
2683 else
2684 fatal ("library lib%s not found", name);
2685 return (NULL);
2686 }
2687 #endif /* COLLECT_EXPORT_LIST */