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