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