]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/collect2.c
[Ada] Fold Enum_Rep attribute in evaluation and not in expansion
[thirdparty/gcc.git] / gcc / collect2.c
CommitLineData
1c84128d
RK
1/* Collect static initialization info into data structures that can be
2 traversed by C++ initialization and finalization routines.
8d9254fc 3 Copyright (C) 1992-2020 Free Software Foundation, Inc.
4e81a331 4 Contributed by Chris Smith (csmith@convex.com).
96317659 5 Heavily modified by Michael Meissner (meissner@cygnus.com),
4e81a331
RS
6 Per Bothner (bothner@cygnus.com), and John Gilmore (gnu@cygnus.com).
7
1322177d 8This file is part of GCC.
4e81a331 9
1322177d
LB
10GCC is free software; you can redistribute it and/or modify it under
11the terms of the GNU General Public License as published by the Free
9dcd6f09 12Software Foundation; either version 3, or (at your option) any later
1322177d 13version.
4e81a331 14
1322177d
LB
15GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16WARRANTY; without even the implied warranty of MERCHANTABILITY or
17FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18for more details.
4e81a331
RS
19
20You should have received a copy of the GNU General Public License
9dcd6f09
NC
21along with GCC; see the file COPYING3. If not see
22<http://www.gnu.org/licenses/>. */
4e81a331
RS
23
24
0f41302f 25/* Build tables of static constructors and destructors and run ld. */
4e81a331 26
c102bba0 27#include "config.h"
670ee920 28#include "system.h"
4977bab6
ZW
29#include "coretypes.h"
30#include "tm.h"
ba78087b 31#include "filenames.h"
c2a73de2 32#include "file-find.h"
4098a6d4
IS
33#include "simple-object.h"
34#include "lto-section-names.h"
4e81a331 35
ad211091
KT
36/* TARGET_64BIT may be defined to use driver specific functionality. */
37#undef TARGET_64BIT
38#define TARGET_64BIT TARGET_64BIT_DEFAULT
39
512b62fb
JM
40#ifndef LIBRARY_PATH_ENV
41#define LIBRARY_PATH_ENV "LIBRARY_PATH"
42#endif
43
c102bba0
RK
44#define COLLECT
45
2edfd4ee 46#include "collect2.h"
dfb636dc 47#include "collect2-aix.h"
5f0ad6a5 48#include "collect-utils.h"
9e350e99 49#include "diagnostic.h"
c102bba0
RK
50#include "demangle.h"
51#include "obstack.h"
ab87f8c8 52#include "intl.h"
9f8f4efe 53#include "version.h"
ed5b9462
RS
54\f
55/* On certain systems, we have code that works by scanning the object file
56 directly. But this code uses system-specific header files and library
bb59f9a6 57 functions, so turn it off in a cross-compiler. Likewise, the names of
db126753 58 the utilities are not correct for a cross-compiler; we have to hope that
bb59f9a6 59 cross-versions are in the proper directories. */
ed5b9462 60
2989d30c 61#ifdef CROSS_DIRECTORY_STRUCTURE
dfb636dc 62#ifndef CROSS_AIX_SUPPORT
ed5b9462 63#undef OBJECT_FORMAT_COFF
dfb636dc 64#endif
bb59f9a6
RS
65#undef MD_EXEC_PREFIX
66#undef REAL_LD_FILE_NAME
67#undef REAL_NM_FILE_NAME
68#undef REAL_STRIP_FILE_NAME
ed5b9462
RS
69#endif
70
db126753 71/* If we cannot use a special method, use the ordinary one:
ed5b9462
RS
72 run nm to find what symbols are present.
73 In a cross-compiler, this means you need a cross nm,
db126753 74 but that is not quite as unpleasant as special headers. */
ed5b9462 75
4a023207 76#if !defined (OBJECT_FORMAT_COFF)
ed5b9462
RS
77#define OBJECT_FORMAT_NONE
78#endif
79
80#ifdef OBJECT_FORMAT_COFF
81
dfb636dc 82#ifndef CROSS_DIRECTORY_STRUCTURE
ed5b9462
RS
83#include <a.out.h>
84#include <ar.h>
85
86#ifdef UMAX
87#include <sgs.h>
88#endif
89
033ba47c
RS
90/* Many versions of ldfcn.h define these. */
91#ifdef FREAD
ed5b9462
RS
92#undef FREAD
93#undef FWRITE
94#endif
95
96#include <ldfcn.h>
dfb636dc 97#endif
ed5b9462 98
7243ba7d
RK
99/* Some systems have an ISCOFF macro, but others do not. In some cases
100 the macro may be wrong. MY_ISCOFF is defined in tm.h files for machines
159b3be1 101 that either do not have an ISCOFF macro in /usr/include or for those
7243ba7d
RK
102 where it is wrong. */
103
e99467d2
RS
104#ifndef MY_ISCOFF
105#define MY_ISCOFF(X) ISCOFF (X)
106#endif
107
ed5b9462
RS
108#endif /* OBJECT_FORMAT_COFF */
109
ed5b9462
RS
110#ifdef OBJECT_FORMAT_NONE
111
4e81a331
RS
112/* Default flags to pass to nm. */
113#ifndef NM_FLAGS
2ce3c6c6 114#define NM_FLAGS "-n"
4e81a331
RS
115#endif
116
ed5b9462 117#endif /* OBJECT_FORMAT_NONE */
d6cf3187
RS
118
119/* Some systems use __main in a way incompatible with its use in gcc, in these
120 cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
77bbd421 121 give the same symbol without quotes for an alternative entry point. */
d6cf3187
RS
122#ifndef NAME__MAIN
123#define NAME__MAIN "__main"
d6cf3187
RS
124#endif
125
6f87c7d8
JM
126/* This must match tree.h. */
127#define DEFAULT_INIT_PRIORITY 65535
128
414e05cf
RE
129#ifndef COLLECT_SHARED_INIT_FUNC
130#define COLLECT_SHARED_INIT_FUNC(STREAM, FUNC) \
131 fprintf ((STREAM), "void _GLOBAL__DI() {\n\t%s();\n}\n", (FUNC))
132#endif
133#ifndef COLLECT_SHARED_FINI_FUNC
134#define COLLECT_SHARED_FINI_FUNC(STREAM, FUNC) \
135 fprintf ((STREAM), "void _GLOBAL__DD() {\n\t%s();\n}\n", (FUNC))
136#endif
137
9acd4a03 138#ifdef LDD_SUFFIX
68d69835
JM
139#define SCAN_LIBRARIES
140#endif
a3184468 141
881466d8
JDA
142#ifndef SHLIB_SUFFIX
143#define SHLIB_SUFFIX ".so"
144#endif
145
a3184468
JM
146#ifdef USE_COLLECT2
147int do_collecting = 1;
148#else
149int do_collecting = 0;
150#endif
84db136d 151
7e49a4b3
OH
152/* Cook up an always defined indication of whether we proceed the
153 "EXPORT_LIST" way. */
154
155#ifdef COLLECT_EXPORT_LIST
156#define DO_COLLECT_EXPORT_LIST 1
157#else
158#define DO_COLLECT_EXPORT_LIST 0
159#endif
160
84db136d
JM
161/* Nonzero if we should suppress the automatic demangling of identifiers
162 in linker error messages. Set from COLLECT_NO_DEMANGLE. */
163int no_demangle;
4e81a331 164\f
0f41302f 165/* Linked lists of constructor and destructor names. */
4e81a331 166
159b3be1 167struct id
4e81a331
RS
168{
169 struct id *next;
170 int sequence;
171 char name[1];
172};
173
174struct head
175{
176 struct id *first;
177 struct id *last;
178 int number;
179};
180
4e81a331 181static int rflag; /* true if -r */
a87104d9 182static int strip_flag; /* true if -s */
db126753
DE
183#ifdef COLLECT_EXPORT_LIST
184static int export_flag; /* true if -bE */
f5e04914 185static int aix64_flag; /* true if -b64 */
7142ae77 186static int aixrtl_flag; /* true if -brtl */
44178879 187static int aixlazy_flag; /* true if -blazy */
db126753 188#endif
4e81a331 189
d7f09764
DN
190enum lto_mode_d {
191 LTO_MODE_NONE, /* Not doing LTO. */
192 LTO_MODE_LTO, /* Normal LTO. */
193 LTO_MODE_WHOPR /* WHOPR. */
194};
195
196/* Current LTO mode. */
f3ba16d0
RB
197#ifdef ENABLE_LTO
198static enum lto_mode_d lto_mode = LTO_MODE_WHOPR;
199#else
d7f09764 200static enum lto_mode_d lto_mode = LTO_MODE_NONE;
f3ba16d0 201#endif
d7f09764 202
7c6d86cc 203bool helpflag; /* true if --help */
4e81a331 204
c22cacf3 205static int shared_obj; /* true if -shared */
6a513e2a 206static int static_obj; /* true if -static */
68d69835 207
73d53120
BE
208static const char *c_file; /* <xxx>.c for constructor/destructor list. */
209static const char *o_file; /* <xxx>.o for constructor/destructor list. */
db126753 210#ifdef COLLECT_EXPORT_LIST
c22cacf3 211static const char *export_file; /* <xxx>.x for AIX export list. */
db126753 212#endif
d7f09764 213static char **lto_o_files; /* Output files for LTO. */
d4058195
KG
214static const char *output_file; /* Output file for ld. */
215static const char *nm_file_name; /* pathname of nm */
84bc3b4f 216#ifdef LDD_SUFFIX
d4058195 217static const char *ldd_file_name; /* pathname of ldd (or equivalent) */
84bc3b4f 218#endif
d4058195 219static const char *strip_file_name; /* pathname of strip */
c22cacf3 220const char *c_file_name; /* pathname of gcc */
cbbbd917 221static char *initname, *fininame; /* names of init and fini funcs */
4e81a331 222
44178879
DE
223
224#ifdef TARGET_AIX_VERSION
225static char *aix_shared_initname;
226static char *aix_shared_fininame; /* init/fini names as per the scheme
227 described in config/rs6000/aix.h */
228#endif
229
4e81a331
RS
230static struct head constructors; /* list of constructors found */
231static struct head destructors; /* list of destructors found */
db126753 232#ifdef COLLECT_EXPORT_LIST
cbbbd917 233static struct head exports; /* list of exported symbols */
db126753 234#endif
0021b564 235static struct head frame_tables; /* list of frame unwind info tables */
4e81a331 236
5f0ad6a5 237bool at_file_supplied; /* Whether to use @file arguments */
2091ff66 238
a3184468 239struct obstack temporary_obstack;
a3184468
JM
240char * temporary_firstobj;
241
781a1758
RS
242/* A string that must be prepended to a target OS path in order to find
243 it on the host system. */
244#ifdef TARGET_SYSTEM_ROOT
245static const char *target_system_root = TARGET_SYSTEM_ROOT;
246#else
247static const char *target_system_root = "";
248#endif
249
9178a345
OH
250/* Whether we may unlink the output file, which should be set as soon as we
251 know we have successfully produced it. This is typically useful to prevent
252 blindly attempting to unlink a read-only output that the target linker
253 would leave untouched. */
254bool may_unlink_output_file = false;
255
db126753 256#ifdef COLLECT_EXPORT_LIST
ec5c56db 257/* Lists to keep libraries to be scanned for global constructors/destructors. */
db126753 258static struct head libs; /* list of libraries */
6a513e2a 259static struct head static_libs; /* list of statically linked libraries */
db126753
DE
260static struct path_prefix cmdline_lib_dirs; /* directories specified with -L */
261static struct path_prefix libpath_lib_dirs; /* directories in LIBPATH */
262static struct path_prefix *libpaths[3] = {&cmdline_lib_dirs,
263 &libpath_lib_dirs, NULL};
db126753
DE
264#endif
265
d7f09764
DN
266/* List of names of object files containing LTO information.
267 These are a subset of the object file names appearing on the
268 command line, and must be identical, in the sense of pointer
269 equality, with the names passed to maybe_run_lto_and_relink(). */
270
271struct lto_object
272{
273 const char *name; /* Name of object file. */
274 struct lto_object *next; /* Next in linked list. */
275};
276
277struct lto_object_list
278{
279 struct lto_object *first; /* First list element. */
280 struct lto_object *last; /* Last list element. */
281};
282
283static struct lto_object_list lto_objects;
284
4326ea40
OH
285/* Special kinds of symbols that a name may denote. */
286
a79683d5 287enum symkind {
4326ea40
OH
288 SYM_REGULAR = 0, /* nothing special */
289
290 SYM_CTOR = 1, /* constructor */
291 SYM_DTOR = 2, /* destructor */
292 SYM_INIT = 3, /* shared object routine that calls all the ctors */
293 SYM_FINI = 4, /* shared object routine that calls all the dtors */
44178879
DE
294 SYM_DWEH = 5, /* DWARF exception handling table */
295 SYM_AIXI = 6,
296 SYM_AIXD = 7
a79683d5 297};
4326ea40 298
5f0ad6a5
BS
299const char tool_name[] = "collect2";
300
4326ea40
OH
301static symkind is_ctor_dtor (const char *);
302
159b3be1 303static void handler (int);
d7f09764 304static void maybe_unlink_list (char **);
159b3be1
AJ
305static void add_to_list (struct head *, const char *);
306static int extract_init_priority (const char *);
307static void sort_ids (struct head *);
308static void write_list (FILE *, const char *, struct id *);
487a6e06 309#ifdef COLLECT_EXPORT_LIST
159b3be1 310static void dump_list (FILE *, const char *, struct id *);
487a6e06
KG
311#endif
312#if 0
159b3be1 313static void dump_prefix_list (FILE *, const char *, struct prefix_list *);
d4058195 314#endif
159b3be1
AJ
315static void write_list_with_asm (FILE *, const char *, struct id *);
316static void write_c_file (FILE *, const char *);
317static void write_c_file_stat (FILE *, const char *);
d4058195 318#ifndef LD_INIT_SWITCH
159b3be1 319static void write_c_file_glob (FILE *, const char *);
487a6e06 320#endif
1d300e19 321#ifdef SCAN_LIBRARIES
159b3be1 322static void scan_libraries (const char *);
1d300e19 323#endif
db126753 324#ifdef COLLECT_EXPORT_LIST
159b3be1 325static int is_in_list (const char *, struct id *);
159b3be1
AJ
326static void write_aix_file (FILE *, struct id *);
327static char *resolve_lib_name (const char *);
db126753 328#endif
159b3be1 329static char *extract_string (const char **);
11afe8e8
MS
330static void post_ld_pass (bool);
331static void process_args (int *argcp, char **argv);
7e49a4b3
OH
332
333/* Enumerations describing which pass this is for scanning the
334 program file ... */
335
a79683d5 336enum scanpass {
7e49a4b3
OH
337 PASS_FIRST, /* without constructors */
338 PASS_OBJ, /* individual objects */
339 PASS_LIB, /* looking for shared libraries */
d7f09764
DN
340 PASS_SECOND, /* with constructors linked in */
341 PASS_LTOINFO /* looking for objects with LTO info */
a79683d5 342};
7e49a4b3
OH
343
344/* ... and which kinds of symbols are to be considered. */
345
346enum scanfilter_masks {
347 SCAN_NOTHING = 0,
348
b8698a0f 349 SCAN_CTOR = 1 << SYM_CTOR,
7e49a4b3
OH
350 SCAN_DTOR = 1 << SYM_DTOR,
351 SCAN_INIT = 1 << SYM_INIT,
352 SCAN_FINI = 1 << SYM_FINI,
353 SCAN_DWEH = 1 << SYM_DWEH,
44178879
DE
354 SCAN_AIXI = 1 << SYM_AIXI,
355 SCAN_AIXD = 1 << SYM_AIXD,
7e49a4b3
OH
356 SCAN_ALL = ~0
357};
358
359/* This type is used for parameters and variables which hold
360 combinations of the flags in enum scanfilter_masks. */
361typedef int scanfilter;
362
363/* Scan the name list of the loaded program for the symbols g++ uses for
364 static constructors and destructors.
365
366 The SCANPASS argument tells which collect processing pass this is for and
367 the SCANFILTER argument tells which kinds of symbols to consider in this
368 pass. Symbols of a special kind not in the filter mask are considered as
369 regular ones.
370
371 The constructor table begins at __CTOR_LIST__ and contains a count of the
372 number of pointers (or -1 if the constructors are built in a separate
373 section by the linker), followed by the pointers to the constructor
374 functions, terminated with a null pointer. The destructor table has the
375 same format, and begins at __DTOR_LIST__. */
376
377static void scan_prog_file (const char *, scanpass, scanfilter);
378
4e81a331 379\f
4e81a331
RS
380/* Delete tempfiles and exit function. */
381
5f0ad6a5 382void
608508a6 383tool_cleanup (bool from_signal)
4e81a331 384{
73d53120
BE
385 /* maybe_unlink may call notice, which is not signal safe. */
386 if (from_signal)
387 verbose = false;
388
ed5b9462 389 if (c_file != 0 && c_file[0])
4e81a331
RS
390 maybe_unlink (c_file);
391
ed5b9462 392 if (o_file != 0 && o_file[0])
4e81a331
RS
393 maybe_unlink (o_file);
394
db126753 395#ifdef COLLECT_EXPORT_LIST
cbbbd917
JM
396 if (export_file != 0 && export_file[0])
397 maybe_unlink (export_file);
db126753
DE
398#endif
399
d7f09764
DN
400 if (lto_o_files)
401 maybe_unlink_list (lto_o_files);
608508a6 402}
68ec3111 403
608508a6
BS
404static void
405collect_atexit (void)
406{
407 tool_cleanup (false);
4e81a331
RS
408}
409
608508a6
BS
410static void
411handler (int signo)
412{
413 tool_cleanup (true);
414
415 signal (signo, SIG_DFL);
416 raise (signo);
417}
0f667308
MP
418/* Notify user of a non-error, without translating the format string. */
419void
420notice_translated (const char *cmsgid, ...)
421{
422 va_list ap;
423
424 va_start (ap, cmsgid);
425 vfprintf (stderr, cmsgid, ap);
426 va_end (ap);
427}
4e81a331 428\f
a3184468 429int
159b3be1 430file_exists (const char *name)
a3184468
JM
431{
432 return access (name, R_OK) == 0;
4e81a331
RS
433}
434
41e16835
JM
435/* Parse a reasonable subset of shell quoting syntax. */
436
437static char *
159b3be1 438extract_string (const char **pp)
41e16835 439{
d4058195 440 const char *p = *pp;
41e16835
JM
441 int backquote = 0;
442 int inside = 0;
443
444 for (;;)
445 {
446 char c = *p;
447 if (c == '\0')
448 break;
449 ++p;
450 if (backquote)
451 obstack_1grow (&temporary_obstack, c);
452 else if (! inside && c == ' ')
453 break;
454 else if (! inside && c == '\\')
455 backquote = 1;
456 else if (c == '\'')
457 inside = !inside;
458 else
459 obstack_1grow (&temporary_obstack, c);
460 }
461
b9550223 462 obstack_1grow (&temporary_obstack, '\0');
41e16835 463 *pp = p;
7973fd2a 464 return XOBFINISH (&temporary_obstack, char *);
41e16835 465}
ed5b9462 466\f
4326ea40 467/* Return the kind of symbol denoted by name S. */
ed5b9462 468
4326ea40 469static symkind
159b3be1 470is_ctor_dtor (const char *s)
ed5b9462 471{
81f40b79 472 struct names { const char *const name; const int len; symkind ret;
8b60264b 473 const int two_underscores; };
ed5b9462 474
c083a819 475 const struct names *p;
b3694847
SS
476 int ch;
477 const char *orig_s = s;
ed5b9462 478
c083a819 479 static const struct names special[] = {
7488cc6d 480#ifndef NO_DOLLAR_IN_LABEL
4326ea40
OH
481 { "GLOBAL__I$", sizeof ("GLOBAL__I$")-1, SYM_CTOR, 0 },
482 { "GLOBAL__D$", sizeof ("GLOBAL__D$")-1, SYM_DTOR, 0 },
7488cc6d
DE
483#else
484#ifndef NO_DOT_IN_LABEL
4326ea40
OH
485 { "GLOBAL__I.", sizeof ("GLOBAL__I.")-1, SYM_CTOR, 0 },
486 { "GLOBAL__D.", sizeof ("GLOBAL__D.")-1, SYM_DTOR, 0 },
7488cc6d
DE
487#endif /* NO_DOT_IN_LABEL */
488#endif /* NO_DOLLAR_IN_LABEL */
4326ea40
OH
489 { "GLOBAL__I_", sizeof ("GLOBAL__I_")-1, SYM_CTOR, 0 },
490 { "GLOBAL__D_", sizeof ("GLOBAL__D_")-1, SYM_DTOR, 0 },
491 { "GLOBAL__F_", sizeof ("GLOBAL__F_")-1, SYM_DWEH, 0 },
492 { "GLOBAL__FI_", sizeof ("GLOBAL__FI_")-1, SYM_INIT, 0 },
493 { "GLOBAL__FD_", sizeof ("GLOBAL__FD_")-1, SYM_FINI, 0 },
44178879
DE
494#ifdef TARGET_AIX_VERSION
495 { "GLOBAL__AIXI_", sizeof ("GLOBAL__AIXI_")-1, SYM_AIXI, 0 },
496 { "GLOBAL__AIXD_", sizeof ("GLOBAL__AIXD_")-1, SYM_AIXD, 0 },
497#endif
4326ea40 498 { NULL, 0, SYM_REGULAR, 0 }
ed5b9462
RS
499 };
500
501 while ((ch = *s) == '_')
502 ++s;
503
504 if (s == orig_s)
4326ea40 505 return SYM_REGULAR;
ed5b9462
RS
506
507 for (p = &special[0]; p->len > 0; p++)
508 {
509 if (ch == p->name[0]
510 && (!p->two_underscores || ((s - orig_s) >= 2))
c3284718 511 && strncmp (s, p->name, p->len) == 0)
ed5b9462
RS
512 {
513 return p->ret;
514 }
515 }
4326ea40 516 return SYM_REGULAR;
ed5b9462 517}
bb59f9a6 518\f
bb59f9a6
RS
519/* We maintain two prefix lists: one from COMPILER_PATH environment variable
520 and one from the PATH variable. */
521
522static struct path_prefix cpath, path;
523
2989d30c 524#ifdef CROSS_DIRECTORY_STRUCTURE
bb59f9a6
RS
525/* This is the name of the target machine. We use it to form the name
526 of the files to execute. */
527
d4058195 528static const char *const target_machine = TARGET_MACHINE;
bb59f9a6 529#endif
720653a3 530
bb59f9a6 531/* Search for NAME using prefix list PPREFIX. We only look for executable
159b3be1 532 files.
bb59f9a6 533
0f41302f 534 Return 0 if not found, otherwise return its name, allocated with malloc. */
bb59f9a6 535
ce79110f 536#ifdef OBJECT_FORMAT_NONE
d72ff618 537
d7f09764
DN
538/* Add an entry for the object file NAME to object file list LIST.
539 New entries are added at the end of the list. The original pointer
540 value of NAME is preserved, i.e., no string copy is performed. */
541
542static void
543add_lto_object (struct lto_object_list *list, const char *name)
544{
545 struct lto_object *n = XNEW (struct lto_object);
546 n->name = name;
547 n->next = NULL;
548
549 if (list->last)
550 list->last->next = n;
551 else
552 list->first = n;
553
554 list->last = n;
555}
ce79110f 556#endif /* OBJECT_FORMAT_NONE */
d7f09764
DN
557
558
559/* Perform a link-time recompilation and relink if any of the object
560 files contain LTO info. The linker command line LTO_LD_ARGV
561 represents the linker command that would produce a final executable
562 without the use of LTO. OBJECT_LST is a vector of object file names
073a8998 563 appearing in LTO_LD_ARGV that are to be considered for link-time
d7f09764
DN
564 recompilation, where OBJECT is a pointer to the last valid element.
565 (This awkward convention avoids an impedance mismatch with the
566 usage of similarly-named variables in main().) The elements of
567 OBJECT_LST must be identical, i.e., pointer equal, to the
568 corresponding arguments in LTO_LD_ARGV.
569
570 Upon entry, at least one linker run has been performed without the
571 use of any LTO info that might be present. Any recompilations
572 necessary for template instantiations have been performed, and
573 initializer/finalizer tables have been created if needed and
574 included in the linker command line LTO_LD_ARGV. If any of the
575 object files contain LTO info, we run the LTO back end on all such
576 files, and perform the final link with the LTO back end output
577 substituted for the LTO-optimized files. In some cases, a final
578 link with all link-time generated code has already been performed,
579 so there is no need to relink if no LTO info is found. In other
580 cases, our caller has not produced the final executable, and is
581 relying on us to perform the required link whether LTO info is
582 present or not. In that case, the FORCE argument should be true.
583 Note that the linker command line argument LTO_LD_ARGV passed into
584 this function may be modified in place. */
585
586static void
587maybe_run_lto_and_relink (char **lto_ld_argv, char **object_lst,
588 const char **object, bool force)
589{
590 const char **object_file = CONST_CAST2 (const char **, char **, object_lst);
591
592 int num_lto_c_args = 1; /* Allow space for the terminating NULL. */
593
594 while (object_file < object)
595 {
596 /* If file contains LTO info, add it to the list of LTO objects. */
597 scan_prog_file (*object_file++, PASS_LTOINFO, SCAN_ALL);
598
599 /* Increment the argument count by the number of object file arguments
600 we will add. An upper bound suffices, so just count all of the
601 object files regardless of whether they contain LTO info. */
602 num_lto_c_args++;
603 }
604
605 if (lto_objects.first)
606 {
d7f09764
DN
607 char **lto_c_argv;
608 const char **lto_c_ptr;
ed2222c8
JH
609 char **p;
610 char **lto_o_ptr;
d7f09764
DN
611 struct lto_object *list;
612 char *lto_wrapper = getenv ("COLLECT_LTO_WRAPPER");
613 struct pex_obj *pex;
614 const char *prog = "lto-wrapper";
ed2222c8
JH
615 int lto_ld_argv_size = 0;
616 char **out_lto_ld_argv;
617 int out_lto_ld_argv_size;
618 size_t num_files;
d7f09764
DN
619
620 if (!lto_wrapper)
a9c697b8
MS
621 fatal_error (input_location, "environment variable "
622 "%<COLLECT_LTO_WRAPPER%> must be set");
d7f09764 623
cf96bae7
RG
624 num_lto_c_args++;
625
d7f09764 626 /* There is at least one object file containing LTO info,
ed2222c8
JH
627 so we need to run the LTO back end and relink.
628
629 To do so we build updated ld arguments with first
630 LTO object replaced by all partitions and other LTO
631 objects removed. */
d7f09764 632
d7f09764
DN
633 lto_c_argv = (char **) xcalloc (sizeof (char *), num_lto_c_args);
634 lto_c_ptr = CONST_CAST2 (const char **, char **, lto_c_argv);
635
636 *lto_c_ptr++ = lto_wrapper;
d7f09764
DN
637
638 /* Add LTO objects to the wrapper command line. */
639 for (list = lto_objects.first; list; list = list->next)
640 *lto_c_ptr++ = list->name;
641
642 *lto_c_ptr = NULL;
643
d7f09764 644 /* Run the LTO back end. */
5f0ad6a5
BS
645 pex = collect_execute (prog, lto_c_argv, NULL, NULL, PEX_SEARCH,
646 at_file_supplied);
d7f09764
DN
647 {
648 int c;
649 FILE *stream;
ed2222c8 650 size_t i;
d7f09764
DN
651 char *start, *end;
652
653 stream = pex_read_output (pex, 0);
654 gcc_assert (stream);
655
656 num_files = 0;
657 while ((c = getc (stream)) != EOF)
658 {
659 obstack_1grow (&temporary_obstack, c);
660 if (c == '\n')
661 ++num_files;
662 }
663
73d53120
BE
664 /* signal handler may access uninitialized memory
665 and delete whatever it points to, if lto_o_files
fd136f01 666 is not allocated with calloc. */
73d53120 667 lto_o_files = XCNEWVEC (char *, num_files + 1);
d7f09764
DN
668 lto_o_files[num_files] = NULL;
669 start = XOBFINISH (&temporary_obstack, char *);
670 for (i = 0; i < num_files; ++i)
671 {
672 end = start;
673 while (*end != '\n')
674 ++end;
675 *end = '\0';
676
677 lto_o_files[i] = xstrdup (start);
678
679 start = end + 1;
680 }
681
682 obstack_free (&temporary_obstack, temporary_firstobj);
683 }
684 do_wait (prog, pex);
685 pex = NULL;
686
5764ee3c 687 /* Compute memory needed for new LD arguments. At most number of original arguments
ed2222c8
JH
688 plus number of partitions. */
689 for (lto_ld_argv_size = 0; lto_ld_argv[lto_ld_argv_size]; lto_ld_argv_size++)
690 ;
c3284718 691 out_lto_ld_argv = XCNEWVEC (char *, num_files + lto_ld_argv_size + 1);
ed2222c8
JH
692 out_lto_ld_argv_size = 0;
693
d7f09764
DN
694 /* After running the LTO back end, we will relink, substituting
695 the LTO output for the object files that we submitted to the
696 LTO. Here, we modify the linker command line for the relink. */
d7f09764 697
ed2222c8
JH
698 /* Copy all arguments until we find first LTO file. */
699 p = lto_ld_argv;
d7f09764
DN
700 while (*p != NULL)
701 {
702 for (list = lto_objects.first; list; list = list->next)
ed2222c8
JH
703 if (*p == list->name) /* Note test for pointer equality! */
704 break;
705 if (list)
706 break;
707 out_lto_ld_argv[out_lto_ld_argv_size++] = *p++;
d7f09764
DN
708 }
709
ed2222c8
JH
710 /* Now insert all LTO partitions. */
711 lto_o_ptr = lto_o_files;
712 while (*lto_o_ptr)
713 out_lto_ld_argv[out_lto_ld_argv_size++] = *lto_o_ptr++;
714
715 /* ... and copy the rest. */
716 while (*p != NULL)
717 {
718 for (list = lto_objects.first; list; list = list->next)
719 if (*p == list->name) /* Note test for pointer equality! */
720 break;
721 if (!list)
722 out_lto_ld_argv[out_lto_ld_argv_size++] = *p;
723 p++;
724 }
725 out_lto_ld_argv[out_lto_ld_argv_size++] = 0;
d7f09764
DN
726
727 /* Run the linker again, this time replacing the object files
728 optimized by the LTO with the temporary file generated by the LTO. */
5f0ad6a5 729 fork_execute ("ld", out_lto_ld_argv, HAVE_GNU_LD && at_file_supplied);
4098a6d4
IS
730 /* We assume that temp files were created, and therefore we need to take
731 that into account (maybe run dsymutil). */
732 post_ld_pass (/*temp_file*/true);
ed2222c8 733 free (lto_ld_argv);
d7f09764
DN
734
735 maybe_unlink_list (lto_o_files);
736 }
737 else if (force)
738 {
739 /* Our caller is relying on us to do the link
740 even though there is no LTO back end work to be done. */
5f0ad6a5 741 fork_execute ("ld", lto_ld_argv, HAVE_GNU_LD && at_file_supplied);
4098a6d4
IS
742 /* No LTO objects were found, so no new temp file. */
743 post_ld_pass (/*temp_file*/false);
d7f09764 744 }
79abf19f 745 else
4098a6d4 746 post_ld_pass (false); /* No LTO objects were found, no temp file. */
d7f09764 747}
67f6e649
ML
748/* Entry point for linker invoation. Called from main in collect2.c.
749 LD_ARGV is an array of arguments for the linker. */
750
751static void
752do_link (char **ld_argv)
753{
754 struct pex_obj *pex;
755 const char *prog = "ld";
756 pex = collect_execute (prog, ld_argv, NULL, NULL,
757 PEX_LAST | PEX_SEARCH,
758 HAVE_GNU_LD && at_file_supplied);
759 int ret = collect_wait (prog, pex);
760 if (ret)
761 {
762 error ("ld returned %d exit status", ret);
763 exit (ret);
764 }
765 else
766 {
767 /* We have just successfully produced an output file, so assume that we
768 may unlink it if need be for now on. */
769 may_unlink_output_file = true;
770 }
771}
4e81a331 772\f
0f41302f 773/* Main program. */
4e81a331
RS
774
775int
159b3be1 776main (int argc, char **argv)
4e81a331 777{
b352afba
NC
778 enum linker_select
779 {
780 USE_DEFAULT_LD,
781 USE_PLUGIN_LD,
782 USE_GOLD_LD,
783 USE_BFD_LD,
d69ac8b7 784 USE_LLD_LD,
b352afba
NC
785 USE_LD_MAX
786 } selected_linker = USE_DEFAULT_LD;
787 static const char *const ld_suffixes[USE_LD_MAX] =
788 {
789 "ld",
790 PLUGIN_LD_SUFFIX,
791 "ld.gold",
d69ac8b7
RG
792 "ld.bfd",
793 "ld.lld"
b352afba 794 };
83182544
KG
795 static const char *const real_ld_suffix = "real-ld";
796 static const char *const collect_ld_suffix = "collect-ld";
797 static const char *const nm_suffix = "nm";
83182544 798 static const char *const gnm_suffix = "gnm";
68d69835 799#ifdef LDD_SUFFIX
83182544 800 static const char *const ldd_suffix = LDD_SUFFIX;
d4058195 801#endif
83182544 802 static const char *const strip_suffix = "strip";
83182544 803 static const char *const gstrip_suffix = "gstrip";
cb6edbcb 804
b352afba 805 const char *full_ld_suffixes[USE_LD_MAX];
2989d30c 806#ifdef CROSS_DIRECTORY_STRUCTURE
cb6edbcb
KG
807 /* If we look for a program in the compiler directories, we just use
808 the short name, since these directories are already system-specific.
809 But it we look for a program in the system directories, we need to
810 qualify the program name with the target machine. */
811
cb6edbcb
KG
812 const char *const full_nm_suffix =
813 concat (target_machine, "-", nm_suffix, NULL);
814 const char *const full_gnm_suffix =
815 concat (target_machine, "-", gnm_suffix, NULL);
816#ifdef LDD_SUFFIX
817 const char *const full_ldd_suffix =
818 concat (target_machine, "-", ldd_suffix, NULL);
819#endif
820 const char *const full_strip_suffix =
821 concat (target_machine, "-", strip_suffix, NULL);
822 const char *const full_gstrip_suffix =
823 concat (target_machine, "-", gstrip_suffix, NULL);
824#else
cb6edbcb
KG
825#ifdef LDD_SUFFIX
826 const char *const full_ldd_suffix = ldd_suffix;
827#endif
b352afba
NC
828 const char *const full_nm_suffix = nm_suffix;
829 const char *const full_gnm_suffix = gnm_suffix;
cb6edbcb
KG
830 const char *const full_strip_suffix = strip_suffix;
831 const char *const full_gstrip_suffix = gstrip_suffix;
2989d30c 832#endif /* CROSS_DIRECTORY_STRUCTURE */
cb6edbcb 833
d4058195 834 const char *arg;
db126753
DE
835 FILE *outf;
836#ifdef COLLECT_EXPORT_LIST
837 FILE *exportf;
db126753 838#endif
d4058195
KG
839 const char *ld_file_name;
840 const char *p;
ed5b9462 841 char **c_argv;
d4058195 842 const char **c_ptr;
ab87f8c8 843 char **ld1_argv;
d4058195 844 const char **ld1;
5938f74d 845 bool use_plugin = false;
b352afba 846 bool use_collect_ld = false;
b8698a0f 847
7e49a4b3
OH
848 /* The kinds of symbols we will have to consider when scanning the
849 outcome of a first pass link. This is ALL to start with, then might
850 be adjusted before getting to the first pass link per se, typically on
851 AIX where we perform an early scan of objects and libraries to fetch
852 the list of global ctors/dtors and make sure they are not garbage
853 collected. */
854 scanfilter ld1_filter = SCAN_ALL;
855
ab87f8c8 856 char **ld2_argv;
d4058195 857 const char **ld2;
ab87f8c8 858 char **object_lst;
d4058195 859 const char **object;
fa66fc02 860#ifdef TARGET_AIX_VERSION
4f44e38a 861 int object_nbr = argc;
fa66fc02 862#endif
4e81a331 863 int first_file;
2091ff66
NF
864 int num_c_args;
865 char **old_argv;
e56f4605 866#ifdef COLLECT_EXPORT_LIST
6a513e2a 867 bool is_static = false;
e56f4605 868#endif
b352afba
NC
869 int i;
870
871 for (i = 0; i < USE_LD_MAX; i++)
872 full_ld_suffixes[i]
873#ifdef CROSS_DIRECTORY_STRUCTURE
874 = concat (target_machine, "-", ld_suffixes[i], NULL);
875#else
876 = ld_suffixes[i];
877#endif
2091ff66 878
9e350e99
JM
879 p = argv[0] + strlen (argv[0]);
880 while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
881 --p;
882 progname = p;
883
884 xmalloc_set_program_name (progname);
885
2091ff66
NF
886 old_argv = argv;
887 expandargv (&argc, &argv);
888 if (argv != old_argv)
889 at_file_supplied = 1;
890
11afe8e8
MS
891 process_args (&argc, argv);
892
2091ff66 893 num_c_args = argc + 9;
4e81a331 894
df45fc34 895#ifndef HAVE_LD_DEMANGLE
84db136d
JM
896 no_demangle = !! getenv ("COLLECT_NO_DEMANGLE");
897
898 /* Suppress demangling by the real linker, which may be broken. */
df45fc34
SL
899 putenv (xstrdup ("COLLECT_NO_DEMANGLE=1"));
900#endif
84db136d 901
ef4969e0 902#if defined (COLLECT2_HOST_INITIALIZATION)
f63d1bf7 903 /* Perform system dependent initialization, if necessary. */
ef4969e0 904 COLLECT2_HOST_INITIALIZATION;
f3692274
ME
905#endif
906
798bdf70
BK
907#ifdef SIGCHLD
908 /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
909 receive the signal. A different setting is inheritable */
910 signal (SIGCHLD, SIG_DFL);
911#endif
912
98a3dad4 913 /* Unlock the stdio streams. */
2653bb0c 914 unlock_std_streams ();
98a3dad4 915
191bf464 916 gcc_init_libintl ();
ab87f8c8 917
9e350e99
JM
918 diagnostic_initialize (global_dc, 0);
919
877088b7 920 if (atexit (collect_atexit) != 0)
40fecdd6 921 fatal_error (input_location, "atexit failed");
877088b7 922
ab87f8c8
JL
923 /* Do not invoke xcalloc before this point, since locale needs to be
924 set first, in case a diagnostic is issued. */
925
67165eb3
ILT
926 ld1_argv = XCNEWVEC (char *, argc + 4);
927 ld1 = CONST_CAST2 (const char **, char **, ld1_argv);
928 ld2_argv = XCNEWVEC (char *, argc + 11);
929 ld2 = CONST_CAST2 (const char **, char **, ld2_argv);
930 object_lst = XCNEWVEC (char *, argc);
931 object = CONST_CAST2 (const char **, char **, object_lst);
ab87f8c8 932
4e81a331 933#ifdef DEBUG
f67eeb79 934 debug = true;
4e81a331
RS
935#endif
936
f67eeb79
IS
937 save_temps = false;
938 verbose = false;
513825f0
IS
939
940#ifndef DEFAULT_A_OUT_NAME
941 output_file = "a.out";
942#else
943 output_file = DEFAULT_A_OUT_NAME;
944#endif
945
f67eeb79
IS
946 /* Parse command line / environment for flags we want early.
947 This allows the debug flag to be set before functions like find_a_file()
948 are called. */
ae8c5963 949 {
014d92e1 950 bool no_partition = false;
159b3be1 951
ae8c5963 952 for (i = 1; argv[i] != NULL; i ++)
5f31e9bc
RO
953 {
954 if (! strcmp (argv[i], "-debug"))
7c6d86cc 955 debug = true;
595c8dfa
AK
956 else if (!strncmp (argv[i], "-fno-lto", 8))
957 lto_mode = LTO_MODE_NONE;
d7f09764
DN
958 else if (! strcmp (argv[i], "-plugin"))
959 {
960 use_plugin = true;
b352afba
NC
961 if (selected_linker == USE_DEFAULT_LD)
962 selected_linker = USE_PLUGIN_LD;
d7f09764 963 }
b352afba
NC
964 else if (strcmp (argv[i], "-fuse-ld=bfd") == 0)
965 selected_linker = USE_BFD_LD;
966 else if (strcmp (argv[i], "-fuse-ld=gold") == 0)
967 selected_linker = USE_GOLD_LD;
513825f0
IS
968 else if (strcmp (argv[i], "-fuse-ld=lld") == 0)
969 selected_linker = USE_LLD_LD;
970 else if (strncmp (argv[i], "-o", 2) == 0)
971 {
972 /* Parse the output filename if it's given so that we can make
973 meaningful temp filenames. */
974 if (argv[i][2] == '\0')
975 output_file = argv[i+1];
976 else
977 output_file = &argv[i][2];
978 }
b352afba 979
88febe35 980#ifdef COLLECT_EXPORT_LIST
d96d674b
MH
981 /* These flags are position independent, although their order
982 is important - subsequent flags override earlier ones. */
983 else if (strcmp (argv[i], "-b64") == 0)
984 aix64_flag = 1;
985 /* -bexport:filename always needs the :filename */
986 else if (strncmp (argv[i], "-bE:", 4) == 0
987 || strncmp (argv[i], "-bexport:", 9) == 0)
988 export_flag = 1;
989 else if (strcmp (argv[i], "-brtl") == 0
990 || strcmp (argv[i], "-bsvr4") == 0
991 || strcmp (argv[i], "-G") == 0)
992 aixrtl_flag = 1;
993 else if (strcmp (argv[i], "-bnortl") == 0)
994 aixrtl_flag = 0;
44178879
DE
995 else if (strcmp (argv[i], "-blazy") == 0)
996 aixlazy_flag = 1;
88febe35 997#endif
5f31e9bc 998 }
ae8c5963 999
f67eeb79
IS
1000 obstack_begin (&temporary_obstack, 0);
1001 temporary_firstobj = (char *) obstack_alloc (&temporary_obstack, 0);
588e387f 1002
d594623a 1003#ifndef HAVE_LD_DEMANGLE
a545190e 1004 current_demangling_style = auto_demangling;
d594623a 1005#endif
f67eeb79
IS
1006
1007 /* Now pick up any flags we want early from COLLECT_GCC_OPTIONS
1008 The LTO options are passed here as are other options that might
1009 be unsuitable for ld (e.g. -save-temps). */
1010 p = getenv ("COLLECT_GCC_OPTIONS");
1011 while (p && *p)
1012 {
1013 const char *q = extract_string (&p);
1014 if (*q == '-' && (q[1] == 'm' || q[1] == 'f'))
1015 num_c_args++;
1016 if (strncmp (q, "-flto-partition=none", 20) == 0)
1017 no_partition = true;
1018 else if (strncmp (q, "-fno-lto", 8) == 0)
1019 lto_mode = LTO_MODE_NONE;
513825f0
IS
1020 else if (strncmp (q, "-save-temps", 11) == 0)
1021 /* FIXME: Honour =obj. */
1022 save_temps = true;
41e16835 1023 }
f67eeb79
IS
1024 obstack_free (&temporary_obstack, temporary_firstobj);
1025
1026 verbose = verbose || debug;
1027 save_temps = save_temps || debug;
1028 find_file_set_debug (debug);
1029 if (use_plugin)
1030 lto_mode = LTO_MODE_NONE;
1031 if (no_partition && lto_mode == LTO_MODE_WHOPR)
1032 lto_mode = LTO_MODE_LTO;
1033 }
43a8f6d5 1034
77e86c91 1035 /* -fno-profile-arcs -fno-test-coverage -fno-branch-probabilities
13291c16
JH
1036 -fno-exceptions -w -fno-whole-program */
1037 num_c_args += 6;
ed5b9462 1038
67165eb3
ILT
1039 c_argv = XCNEWVEC (char *, num_c_args);
1040 c_ptr = CONST_CAST2 (const char **, char **, c_argv);
ed5b9462 1041
4e81a331 1042 if (argc < 2)
40fecdd6 1043 fatal_error (input_location, "no arguments");
4e81a331 1044
8ab861c7 1045#ifdef SIGQUIT
915ee623
JW
1046 if (signal (SIGQUIT, SIG_IGN) != SIG_IGN)
1047 signal (SIGQUIT, handler);
8ab861c7 1048#endif
915ee623
JW
1049 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
1050 signal (SIGINT, handler);
8ab861c7 1051#ifdef SIGALRM
915ee623
JW
1052 if (signal (SIGALRM, SIG_IGN) != SIG_IGN)
1053 signal (SIGALRM, handler);
8ab861c7
RK
1054#endif
1055#ifdef SIGHUP
915ee623
JW
1056 if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
1057 signal (SIGHUP, handler);
8ab861c7 1058#endif
915ee623
JW
1059 if (signal (SIGSEGV, SIG_IGN) != SIG_IGN)
1060 signal (SIGSEGV, handler);
8ab861c7 1061#ifdef SIGBUS
915ee623
JW
1062 if (signal (SIGBUS, SIG_IGN) != SIG_IGN)
1063 signal (SIGBUS, handler);
8ab861c7 1064#endif
4e81a331 1065
bb59f9a6
RS
1066 /* Extract COMPILER_PATH and PATH into our prefix list. */
1067 prefix_from_env ("COMPILER_PATH", &cpath);
1068 prefix_from_env ("PATH", &path);
4e81a331 1069
bb59f9a6
RS
1070 /* Try to discover a valid linker/nm/strip to use. */
1071
90d326bd 1072 /* Maybe we know the right file to use (if not cross). */
0eac1e43 1073 ld_file_name = 0;
ab339d62 1074#ifdef DEFAULT_LINKER
d69ac8b7
RG
1075 if (selected_linker == USE_BFD_LD || selected_linker == USE_GOLD_LD ||
1076 selected_linker == USE_LLD_LD)
b78e932d
L
1077 {
1078 char *linker_name;
1079# ifdef HOST_EXECUTABLE_SUFFIX
1080 int len = (sizeof (DEFAULT_LINKER)
1081 - sizeof (HOST_EXECUTABLE_SUFFIX));
1082 linker_name = NULL;
1083 if (len > 0)
1084 {
1085 char *default_linker = xstrdup (DEFAULT_LINKER);
1086 /* Strip HOST_EXECUTABLE_SUFFIX if DEFAULT_LINKER contains
1087 HOST_EXECUTABLE_SUFFIX. */
1088 if (! strcmp (&default_linker[len], HOST_EXECUTABLE_SUFFIX))
1089 {
1090 default_linker[len] = '\0';
1091 linker_name = concat (default_linker,
1092 &ld_suffixes[selected_linker][2],
1093 HOST_EXECUTABLE_SUFFIX, NULL);
1094 }
1095 }
1096 if (linker_name == NULL)
1097# endif
1098 linker_name = concat (DEFAULT_LINKER,
1099 &ld_suffixes[selected_linker][2],
1100 NULL);
1101 if (access (linker_name, X_OK) == 0)
1102 ld_file_name = linker_name;
1103 }
1104 if (ld_file_name == 0 && access (DEFAULT_LINKER, X_OK) == 0)
ab339d62
AO
1105 ld_file_name = DEFAULT_LINKER;
1106 if (ld_file_name == 0)
1107#endif
90d326bd 1108#ifdef REAL_LD_FILE_NAME
a09f1a76 1109 ld_file_name = find_a_file (&path, REAL_LD_FILE_NAME, X_OK);
90d326bd
JM
1110 if (ld_file_name == 0)
1111#endif
a3184468 1112 /* Search the (target-specific) compiler dirs for ld'. */
a09f1a76 1113 ld_file_name = find_a_file (&cpath, real_ld_suffix, X_OK);
a3184468 1114 /* Likewise for `collect-ld'. */
bb59f9a6 1115 if (ld_file_name == 0)
b352afba 1116 {
a09f1a76 1117 ld_file_name = find_a_file (&cpath, collect_ld_suffix, X_OK);
b352afba
NC
1118 use_collect_ld = ld_file_name != 0;
1119 }
b3a2ef94
RK
1120 /* Search the compiler directories for `ld'. We have protection against
1121 recursive calls in find_a_file. */
f820b898 1122 if (ld_file_name == 0)
a09f1a76 1123 ld_file_name = find_a_file (&cpath, ld_suffixes[selected_linker], X_OK);
bb59f9a6 1124 /* Search the ordinary system bin directories
46c74083 1125 for `ld' (if native linking) or `TARGET-ld' (if cross). */
bb59f9a6 1126 if (ld_file_name == 0)
a09f1a76 1127 ld_file_name = find_a_file (&path, full_ld_suffixes[selected_linker], X_OK);
bb59f9a6 1128
90d326bd 1129#ifdef REAL_NM_FILE_NAME
a09f1a76 1130 nm_file_name = find_a_file (&path, REAL_NM_FILE_NAME, X_OK);
90d326bd
JM
1131 if (nm_file_name == 0)
1132#endif
a09f1a76 1133 nm_file_name = find_a_file (&cpath, gnm_suffix, X_OK);
bb59f9a6 1134 if (nm_file_name == 0)
a09f1a76 1135 nm_file_name = find_a_file (&path, full_gnm_suffix, X_OK);
bb59f9a6 1136 if (nm_file_name == 0)
a09f1a76 1137 nm_file_name = find_a_file (&cpath, nm_suffix, X_OK);
bb59f9a6 1138 if (nm_file_name == 0)
a09f1a76 1139 nm_file_name = find_a_file (&path, full_nm_suffix, X_OK);
bb59f9a6 1140
68d69835 1141#ifdef LDD_SUFFIX
a09f1a76 1142 ldd_file_name = find_a_file (&cpath, ldd_suffix, X_OK);
68d69835 1143 if (ldd_file_name == 0)
a09f1a76 1144 ldd_file_name = find_a_file (&path, full_ldd_suffix, X_OK);
68d69835
JM
1145#endif
1146
90d326bd 1147#ifdef REAL_STRIP_FILE_NAME
a09f1a76 1148 strip_file_name = find_a_file (&path, REAL_STRIP_FILE_NAME, X_OK);
90d326bd
JM
1149 if (strip_file_name == 0)
1150#endif
a09f1a76 1151 strip_file_name = find_a_file (&cpath, gstrip_suffix, X_OK);
bb59f9a6 1152 if (strip_file_name == 0)
a09f1a76 1153 strip_file_name = find_a_file (&path, full_gstrip_suffix, X_OK);
bb59f9a6 1154 if (strip_file_name == 0)
a09f1a76 1155 strip_file_name = find_a_file (&cpath, strip_suffix, X_OK);
bb59f9a6 1156 if (strip_file_name == 0)
a09f1a76 1157 strip_file_name = find_a_file (&path, full_strip_suffix, X_OK);
4e81a331 1158
a87104d9 1159 /* Determine the full path name of the C compiler to use. */
ed5b9462 1160 c_file_name = getenv ("COLLECT_GCC");
5365d6ee 1161 if (c_file_name == 0)
4e81a331 1162 {
2989d30c 1163#ifdef CROSS_DIRECTORY_STRUCTURE
d4058195 1164 c_file_name = concat (target_machine, "-gcc", NULL);
4e81a331 1165#else
bb59f9a6 1166 c_file_name = "gcc";
a87104d9 1167#endif
a87104d9
MM
1168 }
1169
a09f1a76 1170 p = find_a_file (&cpath, c_file_name, X_OK);
bb59f9a6
RS
1171
1172 /* Here it should be safe to use the system search path since we should have
1173 already qualified the name of the compiler when it is needed. */
1174 if (p == 0)
a09f1a76 1175 p = find_a_file (&path, c_file_name, X_OK);
bb59f9a6
RS
1176
1177 if (p)
1178 c_file_name = p;
4e81a331 1179
45840c57 1180 *ld1++ = *ld2++ = ld_file_name;
4e81a331 1181
0f41302f 1182 /* Make temp file names. */
513825f0
IS
1183 if (save_temps)
1184 {
73d53120
BE
1185 c_file = concat (output_file, ".cdtor.c", NULL);
1186 o_file = concat (output_file, ".cdtor.o", NULL);
1187#ifdef COLLECT_EXPORT_LIST
1188 export_file = concat (output_file, ".x", NULL);
1189#endif
513825f0
IS
1190 }
1191 else
1192 {
1193 c_file = make_temp_file (".cdtor.c");
1194 o_file = make_temp_file (".cdtor.o");
db126753 1195#ifdef COLLECT_EXPORT_LIST
73d53120 1196 export_file = make_temp_file (".x");
db126753 1197#endif
888f0920 1198 }
513825f0 1199 /* Build the command line to compile the ctor/dtor list. */
ed5b9462 1200 *c_ptr++ = c_file_name;
cb689bb6
JL
1201 *c_ptr++ = "-x";
1202 *c_ptr++ = "c";
4e81a331
RS
1203 *c_ptr++ = "-c";
1204 *c_ptr++ = "-o";
1205 *c_ptr++ = o_file;
1206
db126753
DE
1207#ifdef COLLECT_EXPORT_LIST
1208 /* Generate a list of directories from LIBPATH. */
1209 prefix_from_env ("LIBPATH", &libpath_lib_dirs);
1210 /* Add to this list also two standard directories where
1211 AIX loader always searches for libraries. */
1212 add_prefix (&libpath_lib_dirs, "/lib");
1213 add_prefix (&libpath_lib_dirs, "/usr/lib");
1214#endif
1215
159b3be1 1216 /* Get any options that the upper GCC wants to pass to the sub-GCC.
db126753
DE
1217
1218 AIX support needs to know if -shared has been specified before
1219 parsing commandline arguments. */
1220
670ee920 1221 p = getenv ("COLLECT_GCC_OPTIONS");
db126753
DE
1222 while (p && *p)
1223 {
d4058195 1224 const char *q = extract_string (&p);
db126753 1225 if (*q == '-' && (q[1] == 'm' || q[1] == 'f'))
6d9f628e 1226 *c_ptr++ = xstrdup (q);
fb0b29ad 1227 if (strcmp (q, "-EL") == 0 || strcmp (q, "-EB") == 0)
6d9f628e 1228 *c_ptr++ = xstrdup (q);
518c1311 1229 if (strcmp (q, "-shared") == 0)
db126753 1230 shared_obj = 1;
6a513e2a
TR
1231 if (strcmp (q, "-static") == 0)
1232 static_obj = 1;
d9068c61
HPN
1233 if (*q == '-' && q[1] == 'B')
1234 {
6d9f628e 1235 *c_ptr++ = xstrdup (q);
d9068c61
HPN
1236 if (q[2] == 0)
1237 {
1238 q = extract_string (&p);
6d9f628e 1239 *c_ptr++ = xstrdup (q);
d9068c61
HPN
1240 }
1241 }
db126753
DE
1242 }
1243 obstack_free (&temporary_obstack, temporary_firstobj);
77e86c91
DE
1244 *c_ptr++ = "-fno-profile-arcs";
1245 *c_ptr++ = "-fno-test-coverage";
1246 *c_ptr++ = "-fno-branch-probabilities";
db126753 1247 *c_ptr++ = "-fno-exceptions";
43a8f6d5 1248 *c_ptr++ = "-w";
13291c16 1249 *c_ptr++ = "-fno-whole-program";
db126753 1250
ed5b9462
RS
1251 /* !!! When GCC calls collect2,
1252 it does not know whether it is calling collect2 or ld.
1253 So collect2 cannot meaningfully understand any options
1254 except those ld understands.
1255 If you propose to make GCC pass some other option,
1256 just imagine what will happen if ld is really ld!!! */
1257
0f41302f 1258 /* Parse arguments. Remember output file spec, pass the rest to ld. */
ed5b9462
RS
1259 /* After the first file, put in the c++ rt0. */
1260
e56f4605 1261#ifdef COLLECT_EXPORT_LIST
6a513e2a 1262 is_static = static_obj;
e56f4605 1263#endif
4e81a331 1264 first_file = 1;
0f41302f 1265 while ((arg = *++argv) != (char *) 0)
4e81a331
RS
1266 {
1267 *ld1++ = *ld2++ = arg;
1268
1269 if (arg[0] == '-')
2d5e74ca 1270 {
4e81a331
RS
1271 switch (arg[1])
1272 {
1273 case 'd':
1274 if (!strcmp (arg, "-debug"))
1275 {
ae8c5963 1276 /* Already parsed. */
4e81a331
RS
1277 ld1--;
1278 ld2--;
1279 }
60828cbd 1280 if (!strcmp (arg, "-dynamic-linker") && argv[1])
c22cacf3 1281 {
60828cbd
ME
1282 ++argv;
1283 *ld1++ = *ld2++ = *argv;
1284 }
4e81a331
RS
1285 break;
1286
d7f09764 1287 case 'f':
014d92e1 1288 if (strncmp (arg, "-flto", 5) == 0)
d7f09764
DN
1289 {
1290#ifdef ENABLE_LTO
1291 /* Do not pass LTO flag to the linker. */
1292 ld1--;
1293 ld2--;
1294#else
1295 error ("LTO support has not been enabled in this "
1296 "configuration");
1297#endif
1298 }
b352afba
NC
1299 else if (!use_collect_ld
1300 && strncmp (arg, "-fuse-ld=", 9) == 0)
1301 {
d69ac8b7 1302 /* Do not pass -fuse-ld={bfd|gold|lld} to the linker. */
b352afba
NC
1303 ld1--;
1304 ld2--;
1305 }
fa608e77
MT
1306 else if (strncmp (arg, "-fno-lto", 8) == 0)
1307 {
1308 /* Do not pass -fno-lto to the linker. */
1309 ld1--;
1310 ld2--;
1311 }
4f44e38a
TG
1312#ifdef TARGET_AIX_VERSION
1313 else
1314 {
1315 /* File containing a list of input files to process. */
1316
1317 FILE *stream;
1318 char buf[MAXPATHLEN + 2];
1319 /* Number of additionnal object files. */
1320 int add_nbr = 0;
1321 /* Maximum of additionnal object files before vector
1322 expansion. */
1323 int add_max = 0;
1324 const char *list_filename = arg + 2;
1325
1326 /* Accept -fFILENAME and -f FILENAME. */
1327 if (*list_filename == '\0' && argv[1])
1328 {
1329 ++argv;
1330 list_filename = *argv;
1331 *ld1++ = *ld2++ = *argv;
1332 }
1333
1334 stream = fopen (list_filename, "r");
1335 if (stream == NULL)
a9c697b8 1336 fatal_error (input_location, "cannot open %s: %m",
40fecdd6 1337 list_filename);
4f44e38a
TG
1338
1339 while (fgets (buf, sizeof buf, stream) != NULL)
1340 {
1341 /* Remove end of line. */
1342 int len = strlen (buf);
1343 if (len >= 1 && buf[len - 1] =='\n')
1344 buf[len - 1] = '\0';
1345
1346 /* Put on object vector.
1347 Note: we only expanse vector here, so we must keep
1348 extra space for remaining arguments. */
1349 if (add_nbr >= add_max)
1350 {
6588d711
OH
1351 int pos =
1352 object - CONST_CAST2 (const char **, char **,
1353 object_lst);
4f44e38a
TG
1354 add_max = (add_max == 0) ? 16 : add_max * 2;
1355 object_lst = XRESIZEVEC (char *, object_lst,
1356 object_nbr + add_max);
6588d711
OH
1357 object = CONST_CAST2 (const char **, char **,
1358 object_lst) + pos;
4f44e38a
TG
1359 object_nbr += add_max;
1360 }
1361 *object++ = xstrdup (buf);
1362 add_nbr++;
1363 }
1364 fclose (stream);
1365 }
1366#endif
d7f09764
DN
1367 break;
1368
6a513e2a
TR
1369#ifdef COLLECT_EXPORT_LIST
1370 case 'b':
1371 if (!strcmp (arg, "-bstatic"))
1372 {
1373 is_static = true;
1374 }
1375 else if (!strcmp (arg, "-bdynamic") || !strcmp (arg, "-bshared"))
1376 {
1377 is_static = false;
1378 }
1379 break;
1380#endif
68d69835
JM
1381 case 'l':
1382 if (first_file)
1383 {
1384 /* place o_file BEFORE this argument! */
1385 first_file = 0;
1386 ld2--;
1387 *ld2++ = o_file;
1388 *ld2++ = arg;
1389 }
db126753
DE
1390#ifdef COLLECT_EXPORT_LIST
1391 {
c22cacf3 1392 /* Resolving full library name. */
d4058195 1393 const char *s = resolve_lib_name (arg+2);
db126753 1394
db126753
DE
1395 /* Saving a full library name. */
1396 add_to_list (&libs, s);
6a513e2a
TR
1397 if (is_static)
1398 add_to_list (&static_libs, s);
db126753
DE
1399 }
1400#endif
1401 break;
1402
1403#ifdef COLLECT_EXPORT_LIST
1404 /* Saving directories where to search for libraries. */
159b3be1 1405 case 'L':
db126753 1406 add_prefix (&cmdline_lib_dirs, arg+2);
68d69835 1407 break;
db126753 1408#endif
68d69835 1409
4e81a331 1410 case 'o':
603b4064
RK
1411 if (arg[2] == '\0')
1412 output_file = *ld1++ = *ld2++ = *++argv;
d5d8b111 1413 else
603b4064 1414 output_file = &arg[2];
4e81a331
RS
1415 break;
1416
1417 case 'r':
1418 if (arg[2] == '\0')
1419 rflag = 1;
1420 break;
1421
a87104d9 1422 case 's':
a3184468 1423 if (arg[2] == '\0' && do_collecting)
a87104d9
MM
1424 {
1425 /* We must strip after the nm run, otherwise C++ linking
db126753 1426 will not work. Thus we strip in the second ld run, or
a87104d9
MM
1427 else with strip if there is no second ld run. */
1428 strip_flag = 1;
1429 ld1--;
1430 }
1431 break;
1432
4e81a331
RS
1433 case 'v':
1434 if (arg[2] == '\0')
608508a6 1435 verbose = true;
4e81a331 1436 break;
27605cbe
PB
1437
1438 case '-':
1439 if (strcmp (arg, "--no-demangle") == 0)
1440 {
df45fc34 1441#ifndef HAVE_LD_DEMANGLE
27605cbe
PB
1442 no_demangle = 1;
1443 ld1--;
1444 ld2--;
df45fc34 1445#endif
27605cbe
PB
1446 }
1447 else if (strncmp (arg, "--demangle", 10) == 0)
1448 {
27605cbe 1449#ifndef HAVE_LD_DEMANGLE
df45fc34 1450 no_demangle = 0;
27605cbe
PB
1451 if (arg[10] == '=')
1452 {
1453 enum demangling_styles style
1454 = cplus_demangle_name_to_style (arg+11);
1455 if (style == unknown_demangling)
904f3daa 1456 error ("unknown demangling style %qs", arg+11);
27605cbe
PB
1457 else
1458 current_demangling_style = style;
1459 }
27605cbe
PB
1460 ld1--;
1461 ld2--;
df45fc34 1462#endif
27605cbe 1463 }
781a1758
RS
1464 else if (strncmp (arg, "--sysroot=", 10) == 0)
1465 target_system_root = arg + 10;
2dcef31e 1466 else if (strcmp (arg, "--version") == 0)
608508a6 1467 verbose = true;
2dcef31e 1468 else if (strcmp (arg, "--help") == 0)
7c6d86cc 1469 helpflag = true;
27605cbe 1470 break;
4e81a331 1471 }
2d5e74ca 1472 }
9473c522 1473 else if ((p = strrchr (arg, '.')) != (char *) 0
db126753 1474 && (strcmp (p, ".o") == 0 || strcmp (p, ".a") == 0
05dba9ee
DE
1475 || strcmp (p, ".so") == 0 || strcmp (p, ".lo") == 0
1476 || strcmp (p, ".obj") == 0))
4e81a331 1477 {
9f21696b
JM
1478 if (first_file)
1479 {
1480 first_file = 0;
603b4064
RK
1481 if (p[1] == 'o')
1482 *ld2++ = o_file;
1483 else
1484 {
1485 /* place o_file BEFORE this argument! */
1486 ld2--;
1487 *ld2++ = o_file;
1488 *ld2++ = arg;
1489 }
9f21696b 1490 }
31d432e4 1491 if (p[1] == 'o' || p[1] == 'l')
9f21696b 1492 *object++ = arg;
db126753
DE
1493#ifdef COLLECT_EXPORT_LIST
1494 /* libraries can be specified directly, i.e. without -l flag. */
159b3be1
AJ
1495 else
1496 {
db126753 1497 /* Saving a full library name. */
c22cacf3 1498 add_to_list (&libs, arg);
6a513e2a
TR
1499 if (is_static)
1500 add_to_list (&static_libs, arg);
c22cacf3 1501 }
db126753 1502#endif
4e81a331
RS
1503 }
1504 }
1505
db126753
DE
1506#ifdef COLLECT_EXPORT_LIST
1507 /* This is added only for debugging purposes. */
1508 if (debug)
41e16835 1509 {
db126753
DE
1510 fprintf (stderr, "List of libraries:\n");
1511 dump_list (stderr, "\t", libs.first);
6a513e2a
TR
1512 fprintf (stderr, "List of statically linked libraries:\n");
1513 dump_list (stderr, "\t", static_libs.first);
41e16835 1514 }
ed5b9462 1515
cbbbd917 1516 /* The AIX linker will discard static constructors in object files if
7e49a4b3
OH
1517 nothing else in the file is referenced, so look at them first. Unless
1518 we are building a shared object, ignore the eh frame tables, as we
1519 would otherwise reference them all, hence drag all the corresponding
1520 objects even if nothing else is referenced. */
db126753 1521 {
b8698a0f 1522 const char **export_object_lst
7e49a4b3 1523 = CONST_CAST2 (const char **, char **, object_lst);
b8698a0f 1524
db126753 1525 struct id *list = libs.first;
9d4d5d58 1526
7e49a4b3
OH
1527 /* Compute the filter to use from the current one, do scan, then adjust
1528 the "current" filter to remove what we just included here. This will
1529 control whether we need a first pass link later on or not, and what
1530 will remain to be scanned there. */
b8698a0f 1531
636cf8b1
RS
1532 scanfilter this_filter = ld1_filter;
1533#if HAVE_AS_REF
1534 if (!shared_obj)
1535 this_filter &= ~SCAN_DWEH;
1536#endif
b8698a0f 1537
6a513e2a 1538 /* Scan object files. */
7e49a4b3
OH
1539 while (export_object_lst < object)
1540 scan_prog_file (*export_object_lst++, PASS_OBJ, this_filter);
b8698a0f 1541
6a513e2a 1542 /* Scan libraries. */
db126753 1543 for (; list; list = list->next)
7e49a4b3 1544 scan_prog_file (list->name, PASS_FIRST, this_filter);
b8698a0f 1545
7e49a4b3 1546 ld1_filter = ld1_filter & ~this_filter;
db126753 1547 }
9d4d5d58
DE
1548
1549 if (exports.first)
1550 {
d4c3ec27 1551 char *buf = concat ("-bE:", export_file, NULL);
159b3be1 1552
9d4d5d58
DE
1553 *ld1++ = buf;
1554 *ld2++ = buf;
1555
1556 exportf = fopen (export_file, "w");
1557 if (exportf == (FILE *) 0)
40fecdd6 1558 fatal_error (input_location, "fopen %s: %m", export_file);
9d4d5d58
DE
1559 write_aix_file (exportf, exports.first);
1560 if (fclose (exportf))
40fecdd6 1561 fatal_error (input_location, "fclose %s: %m", export_file);
9d4d5d58 1562 }
cbbbd917
JM
1563#endif
1564
4e81a331 1565 *c_ptr++ = c_file;
d4058195 1566 *c_ptr = *ld1 = *object = (char *) 0;
4e81a331 1567
608508a6 1568 if (verbose)
eb1eb914 1569 notice ("collect2 version %s\n", version_string);
4e81a331 1570
7c6d86cc
JG
1571 if (helpflag)
1572 {
df10a535
RW
1573 printf ("Usage: collect2 [options]\n");
1574 printf (" Wrap linker and generate constructor code if needed.\n");
1575 printf (" Options:\n");
1576 printf (" -debug Enable debug output\n");
1577 printf (" --help Display this information\n");
1578 printf (" -v, --version Display this program's version number\n");
1579 printf ("\n");
e050689b 1580 printf ("Overview: https://gcc.gnu.org/onlinedocs/gccint/Collect2.html\n");
df10a535
RW
1581 printf ("Report bugs: %s\n", bug_report_url);
1582 printf ("\n");
7c6d86cc
JG
1583 }
1584
4e81a331
RS
1585 if (debug)
1586 {
d4058195 1587 const char *ptr;
a1549b15
RS
1588 fprintf (stderr, "ld_file_name = %s\n",
1589 (ld_file_name ? ld_file_name : "not found"));
1590 fprintf (stderr, "c_file_name = %s\n",
1591 (c_file_name ? c_file_name : "not found"));
1592 fprintf (stderr, "nm_file_name = %s\n",
1593 (nm_file_name ? nm_file_name : "not found"));
68d69835
JM
1594#ifdef LDD_SUFFIX
1595 fprintf (stderr, "ldd_file_name = %s\n",
1596 (ldd_file_name ? ldd_file_name : "not found"));
1597#endif
a1549b15
RS
1598 fprintf (stderr, "strip_file_name = %s\n",
1599 (strip_file_name ? strip_file_name : "not found"));
1600 fprintf (stderr, "c_file = %s\n",
1601 (c_file ? c_file : "not found"));
1602 fprintf (stderr, "o_file = %s\n",
1603 (o_file ? o_file : "not found"));
ed5b9462
RS
1604
1605 ptr = getenv ("COLLECT_GCC_OPTIONS");
1606 if (ptr)
1607 fprintf (stderr, "COLLECT_GCC_OPTIONS = %s\n", ptr);
1608
1609 ptr = getenv ("COLLECT_GCC");
1610 if (ptr)
1611 fprintf (stderr, "COLLECT_GCC = %s\n", ptr);
1612
1613 ptr = getenv ("COMPILER_PATH");
1614 if (ptr)
1615 fprintf (stderr, "COMPILER_PATH = %s\n", ptr);
1616
512b62fb 1617 ptr = getenv (LIBRARY_PATH_ENV);
ed5b9462 1618 if (ptr)
512b62fb 1619 fprintf (stderr, "%-20s= %s\n", LIBRARY_PATH_ENV, ptr);
ed5b9462
RS
1620
1621 fprintf (stderr, "\n");
4e81a331
RS
1622 }
1623
aa32d841 1624 /* Load the program, searching all libraries and attempting to provide
7e49a4b3 1625 undefined symbols from repository information.
b8698a0f 1626
7e49a4b3 1627 If -r or they will be run via some other method, do not build the
b8698a0f 1628 constructor or destructor list, just return now. */
7e49a4b3
OH
1629 {
1630 bool early_exit
1631 = rflag || (! DO_COLLECT_EXPORT_LIST && ! do_collecting);
1632
1633 /* Perform the first pass link now, if we're about to exit or if we need
1634 to scan for things we haven't collected yet before pursuing further.
1635
1636 On AIX, the latter typically includes nothing for shared objects or
1637 frame tables for an executable, out of what the required early scan on
1638 objects and libraries has performed above. In the !shared_obj case, we
1639 expect the relevant tables to be dragged together with their associated
1640 functions from precise cross reference insertions by the compiler. */
b8698a0f 1641
7e49a4b3 1642 if (early_exit || ld1_filter != SCAN_NOTHING)
67f6e649 1643 do_link (ld1_argv);
b8698a0f 1644
7e49a4b3
OH
1645 if (early_exit)
1646 {
1647#ifdef COLLECT_EXPORT_LIST
1648 /* Make sure we delete the export file we may have created. */
1649 if (export_file != 0 && export_file[0])
1650 maybe_unlink (export_file);
63f38a90 1651#endif
c082f9f3 1652 if (lto_mode != LTO_MODE_NONE)
d7f09764 1653 maybe_run_lto_and_relink (ld1_argv, object_lst, object, false);
11afe8e8 1654 else
4098a6d4 1655 post_ld_pass (/*temp_file*/false);
d7f09764 1656
7e49a4b3
OH
1657 return 0;
1658 }
1659 }
4e81a331 1660
7e49a4b3
OH
1661 /* Unless we have done it all already, examine the namelist and search for
1662 static constructors and destructors to call. Write the constructor and
1663 destructor tables to a .s file and reload. */
a3184468 1664
7e49a4b3
OH
1665 if (ld1_filter != SCAN_NOTHING)
1666 scan_prog_file (output_file, PASS_FIRST, ld1_filter);
4e81a331 1667
68d69835
JM
1668#ifdef SCAN_LIBRARIES
1669 scan_libraries (output_file);
1670#endif
1671
4e81a331
RS
1672 if (debug)
1673 {
0f667308
MP
1674 notice_translated (ngettext ("%d constructor found\n",
1675 "%d constructors found\n",
1676 constructors.number),
1677 constructors.number);
1678 notice_translated (ngettext ("%d destructor found\n",
1679 "%d destructors found\n",
1680 destructors.number),
1681 destructors.number);
c3284718
RS
1682 notice_translated (ngettext ("%d frame table found\n",
1683 "%d frame tables found\n",
1684 frame_tables.number),
0f667308 1685 frame_tables.number);
4e81a331
RS
1686 }
1687
7e49a4b3
OH
1688 /* If the scan exposed nothing of special interest, there's no need to
1689 generate the glue code and relink so return now. */
1690
68d69835 1691 if (constructors.number == 0 && destructors.number == 0
0021b564 1692 && frame_tables.number == 0
db126753 1693#if defined (SCAN_LIBRARIES) || defined (COLLECT_EXPORT_LIST)
68d69835 1694 /* If we will be running these functions ourselves, we want to emit
db126753 1695 stubs into the shared library so that we do not have to relink
68d69835
JM
1696 dependent programs when we add static objects. */
1697 && ! shared_obj
1698#endif
1699 )
a87104d9 1700 {
67f6e649 1701 /* Do link without additional code generation now if we didn't
7e49a4b3
OH
1702 do it earlier for scanning purposes. */
1703 if (ld1_filter == SCAN_NOTHING)
67f6e649 1704 do_link (ld1_argv);
7e49a4b3 1705
d7f09764
DN
1706 if (lto_mode)
1707 maybe_run_lto_and_relink (ld1_argv, object_lst, object, false);
1708
a87104d9
MM
1709 /* Strip now if it was requested on the command line. */
1710 if (strip_flag)
1711 {
5ed6ace5 1712 char **real_strip_argv = XCNEWVEC (char *, 3);
67165eb3
ILT
1713 const char ** strip_argv = CONST_CAST2 (const char **, char **,
1714 real_strip_argv);
159b3be1 1715
aee3a549 1716 strip_argv[0] = strip_file_name;
0cdd3ddd 1717 strip_argv[1] = output_file;
a87104d9 1718 strip_argv[2] = (char *) 0;
5f0ad6a5 1719 fork_execute ("strip", real_strip_argv, false);
a87104d9 1720 }
eccd41bb
RK
1721
1722#ifdef COLLECT_EXPORT_LIST
1723 maybe_unlink (export_file);
1724#endif
4098a6d4 1725 post_ld_pass (/*temp_file*/false);
a87104d9
MM
1726 return 0;
1727 }
4e81a331 1728
ec5c56db 1729 /* Sort ctor and dtor lists by priority. */
2ce3c6c6
JM
1730 sort_ids (&constructors);
1731 sort_ids (&destructors);
1732
c3284718 1733 maybe_unlink (output_file);
4e81a331 1734 outf = fopen (c_file, "w");
0f41302f 1735 if (outf == (FILE *) 0)
40fecdd6 1736 fatal_error (input_location, "fopen %s: %m", c_file);
4e81a331
RS
1737
1738 write_c_file (outf, c_file);
1739
1740 if (fclose (outf))
40fecdd6 1741 fatal_error (input_location, "fclose %s: %m", c_file);
4e81a331 1742
603b4064
RK
1743 /* Tell the linker that we have initializer and finalizer functions. */
1744#ifdef LD_INIT_SWITCH
c71791e0 1745#ifdef COLLECT_EXPORT_LIST
d4c3ec27 1746 *ld2++ = concat (LD_INIT_SWITCH, ":", initname, ":", fininame, NULL);
c71791e0 1747#else
603b4064
RK
1748 *ld2++ = LD_INIT_SWITCH;
1749 *ld2++ = initname;
1750 *ld2++ = LD_FINI_SWITCH;
1751 *ld2++ = fininame;
1752#endif
c71791e0 1753#endif
603b4064 1754
cbbbd917
JM
1755#ifdef COLLECT_EXPORT_LIST
1756 if (shared_obj)
1757 {
9d4d5d58
DE
1758 /* If we did not add export flag to link arguments before, add it to
1759 second link phase now. No new exports should have been added. */
1760 if (! exports.first)
d4c3ec27 1761 *ld2++ = concat ("-bE:", export_file, NULL);
9d4d5d58 1762
44178879
DE
1763#ifdef TARGET_AIX_VERSION
1764 add_to_list (&exports, aix_shared_initname);
1765 add_to_list (&exports, aix_shared_fininame);
1766#endif
1767
aaef9b06 1768#ifndef LD_INIT_SWITCH
cbbbd917
JM
1769 add_to_list (&exports, initname);
1770 add_to_list (&exports, fininame);
603b4064
RK
1771 add_to_list (&exports, "_GLOBAL__DI");
1772 add_to_list (&exports, "_GLOBAL__DD");
aaef9b06 1773#endif
cbbbd917 1774 exportf = fopen (export_file, "w");
0f41302f 1775 if (exportf == (FILE *) 0)
40fecdd6 1776 fatal_error (input_location, "fopen %s: %m", export_file);
9d4d5d58 1777 write_aix_file (exportf, exports.first);
cbbbd917 1778 if (fclose (exportf))
40fecdd6 1779 fatal_error (input_location, "fclose %s: %m", export_file);
cbbbd917
JM
1780 }
1781#endif
1782
9d4d5d58
DE
1783 /* End of arguments to second link phase. */
1784 *ld2 = (char*) 0;
1785
4e81a331
RS
1786 if (debug)
1787 {
0cdd3ddd
RS
1788 fprintf (stderr, "\n========== output_file = %s, c_file = %s\n",
1789 output_file, c_file);
4e81a331
RS
1790 write_c_file (stderr, "stderr");
1791 fprintf (stderr, "========== end of c_file\n\n");
cbbbd917
JM
1792#ifdef COLLECT_EXPORT_LIST
1793 fprintf (stderr, "\n========== export_file = %s\n", export_file);
9d4d5d58 1794 write_aix_file (stderr, exports.first);
cbbbd917
JM
1795 fprintf (stderr, "========== end of export_file\n\n");
1796#endif
4e81a331
RS
1797 }
1798
1799 /* Assemble the constructor and destructor tables.
0f41302f 1800 Link the tables in with the rest of the program. */
4e81a331 1801
5f0ad6a5 1802 fork_execute ("gcc", c_argv, at_file_supplied);
db126753 1803#ifdef COLLECT_EXPORT_LIST
67f6e649
ML
1804 /* On AIX we must call link because of possible templates resolution. */
1805 do_link (ld2_argv);
d7f09764
DN
1806
1807 if (lto_mode)
1808 maybe_run_lto_and_relink (ld2_argv, object_lst, object, false);
db126753 1809#else
67f6e649 1810 /* Otherwise, simply call ld because link is already done. */
d7f09764
DN
1811 if (lto_mode)
1812 maybe_run_lto_and_relink (ld2_argv, object_lst, object, true);
1813 else
11afe8e8 1814 {
5f0ad6a5 1815 fork_execute ("ld", ld2_argv, HAVE_GNU_LD && at_file_supplied);
4098a6d4 1816 post_ld_pass (/*temp_file*/false);
11afe8e8 1817 }
4e81a331
RS
1818
1819 /* Let scan_prog_file do any final mods (OSF/rose needs this for
1820 constructors/destructors in shared libraries. */
7e49a4b3 1821 scan_prog_file (output_file, PASS_SECOND, SCAN_ALL);
159b3be1 1822#endif
4e81a331 1823
4e81a331
RS
1824 return 0;
1825}
1826
4e81a331 1827\f
9178a345
OH
1828/* Unlink FILE unless we are debugging or this is the output_file
1829 and we may not unlink it. */
4e81a331 1830
5f0ad6a5 1831void
159b3be1 1832maybe_unlink (const char *file)
4e81a331 1833{
513825f0 1834 if (save_temps && file_exists (file))
9178a345 1835 {
513825f0
IS
1836 if (verbose)
1837 notice ("[Leaving %s]\n", file);
9178a345
OH
1838 return;
1839 }
1840
1841 if (file == output_file && !may_unlink_output_file)
1842 return;
1843
1844 unlink_if_ordinary (file);
4e81a331
RS
1845}
1846
d7f09764
DN
1847/* Call maybe_unlink on the NULL-terminated list, FILE_LIST. */
1848
1849static void
1850maybe_unlink_list (char **file_list)
1851{
1852 char **tmp = file_list;
1853
1854 while (*tmp)
1855 maybe_unlink (*(tmp++));
1856}
1857
4e81a331 1858\f
2ce3c6c6
JM
1859static long sequence_number = 0;
1860
4e81a331
RS
1861/* Add a name to a linked list. */
1862
1863static void
159b3be1 1864add_to_list (struct head *head_ptr, const char *name)
4e81a331 1865{
e1e4cdc4
KG
1866 struct id *newid
1867 = (struct id *) xcalloc (sizeof (struct id) + strlen (name), 1);
0393b857 1868 struct id *p;
4e81a331
RS
1869 strcpy (newid->name, name);
1870
1871 if (head_ptr->first)
1872 head_ptr->last->next = newid;
1873 else
1874 head_ptr->first = newid;
1875
0393b857
JM
1876 /* Check for duplicate symbols. */
1877 for (p = head_ptr->first;
1878 strcmp (name, p->name) != 0;
1879 p = p->next)
1880 ;
1881 if (p != newid)
1882 {
1883 head_ptr->last->next = 0;
1884 free (newid);
1885 return;
1886 }
1887
1888 newid->sequence = ++sequence_number;
4e81a331
RS
1889 head_ptr->last = newid;
1890 head_ptr->number++;
1891}
1892
2ce3c6c6
JM
1893/* Grab the init priority number from an init function name that
1894 looks like "_GLOBAL_.I.12345.foo". */
1895
1896static int
159b3be1 1897extract_init_priority (const char *name)
2ce3c6c6 1898{
6f87c7d8 1899 int pos = 0, pri;
2ce3c6c6 1900
44178879
DE
1901#ifdef TARGET_AIX_VERSION
1902 /* Run dependent module initializers before any constructors in this
1903 module. */
1904 switch (is_ctor_dtor (name))
1905 {
1906 case SYM_AIXI:
1907 case SYM_AIXD:
1908 return INT_MIN;
1909 default:
1910 break;
1911 }
1912#endif
1913
2ce3c6c6
JM
1914 while (name[pos] == '_')
1915 ++pos;
1916 pos += 10; /* strlen ("GLOBAL__X_") */
1917
ec5c56db 1918 /* Extract init_p number from ctor/dtor name. */
6f87c7d8
JM
1919 pri = atoi (name + pos);
1920 return pri ? pri : DEFAULT_INIT_PRIORITY;
2ce3c6c6
JM
1921}
1922
1923/* Insertion sort the ids from ctor/dtor list HEAD_PTR in descending order.
1924 ctors will be run from right to left, dtors from left to right. */
1925
1926static void
159b3be1 1927sort_ids (struct head *head_ptr)
2ce3c6c6
JM
1928{
1929 /* id holds the current element to insert. id_next holds the next
1930 element to insert. id_ptr iterates through the already sorted elements
1931 looking for the place to insert id. */
1932 struct id *id, *id_next, **id_ptr;
2ce3c6c6
JM
1933
1934 id = head_ptr->first;
1935
1936 /* We don't have any sorted elements yet. */
1937 head_ptr->first = NULL;
1938
1939 for (; id; id = id_next)
1940 {
1941 id_next = id->next;
1942 id->sequence = extract_init_priority (id->name);
1943
1944 for (id_ptr = &(head_ptr->first); ; id_ptr = &((*id_ptr)->next))
1945 if (*id_ptr == NULL
1946 /* If the sequence numbers are the same, we put the id from the
1947 file later on the command line later in the list. */
1948 || id->sequence > (*id_ptr)->sequence
1949 /* Hack: do lexical compare, too.
1950 || (id->sequence == (*id_ptr)->sequence
c22cacf3 1951 && strcmp (id->name, (*id_ptr)->name) > 0) */
2ce3c6c6
JM
1952 )
1953 {
1954 id->next = *id_ptr;
1955 *id_ptr = id;
1956 break;
1957 }
1958 }
1959
1960 /* Now set the sequence numbers properly so write_c_file works. */
1961 for (id = head_ptr->first; id; id = id->next)
1962 id->sequence = ++sequence_number;
1963}
1964
4e81a331
RS
1965/* Write: `prefix', the names on list LIST, `suffix'. */
1966
1967static void
159b3be1 1968write_list (FILE *stream, const char *prefix, struct id *list)
4e81a331
RS
1969{
1970 while (list)
1971 {
1972 fprintf (stream, "%sx%d,\n", prefix, list->sequence);
1973 list = list->next;
1974 }
1975}
1976
40c3a857 1977#ifdef COLLECT_EXPORT_LIST
db126753
DE
1978/* This function is really used only on AIX, but may be useful. */
1979static int
159b3be1 1980is_in_list (const char *prefix, struct id *list)
db126753
DE
1981{
1982 while (list)
1983 {
1984 if (!strcmp (prefix, list->name)) return 1;
1985 list = list->next;
1986 }
1987 return 0;
1988}
c3fb23f4 1989#endif /* COLLECT_EXPORT_LIST */
db126753
DE
1990
1991/* Added for debugging purpose. */
487a6e06 1992#ifdef COLLECT_EXPORT_LIST
db126753 1993static void
159b3be1 1994dump_list (FILE *stream, const char *prefix, struct id *list)
db126753
DE
1995{
1996 while (list)
1997 {
1998 fprintf (stream, "%s%s,\n", prefix, list->name);
1999 list = list->next;
2000 }
2001}
487a6e06 2002#endif
db126753 2003
487a6e06 2004#if 0
db126753 2005static void
159b3be1 2006dump_prefix_list (FILE *stream, const char *prefix, struct prefix_list *list)
db126753
DE
2007{
2008 while (list)
2009 {
2010 fprintf (stream, "%s%s,\n", prefix, list->prefix);
2011 list = list->next;
2012 }
2013}
487a6e06 2014#endif
db126753 2015
4e81a331 2016static void
159b3be1 2017write_list_with_asm (FILE *stream, const char *prefix, struct id *list)
4e81a331
RS
2018{
2019 while (list)
2020 {
c7af43d8 2021 fprintf (stream, "%sx%d __asm__ (\"%s\");\n",
4e81a331
RS
2022 prefix, list->sequence, list->name);
2023 list = list->next;
2024 }
2025}
2026
68d69835
JM
2027/* Write out the constructor and destructor tables statically (for a shared
2028 object), along with the functions to execute them. */
2029
2030static void
159b3be1 2031write_c_file_stat (FILE *stream, const char *name ATTRIBUTE_UNUSED)
68d69835 2032{
d4058195
KG
2033 const char *p, *q;
2034 char *prefix, *r;
0021b564 2035 int frames = (frame_tables.number > 0);
68d69835
JM
2036
2037 /* Figure out name of output_file, stripping off .so version. */
ba78087b
KT
2038 q = p = lbasename (output_file);
2039
68d69835
JM
2040 while (q)
2041 {
9473c522 2042 q = strchr (q,'.');
68d69835
JM
2043 if (q == 0)
2044 {
2045 q = p + strlen (p);
2046 break;
2047 }
2048 else
2049 {
ba78087b 2050 if (filename_ncmp (q, SHLIB_SUFFIX, strlen (SHLIB_SUFFIX)) == 0)
68d69835 2051 {
881466d8 2052 q += strlen (SHLIB_SUFFIX);
68d69835
JM
2053 break;
2054 }
2055 else
2056 q++;
2057 }
2058 }
2059 /* q points to null at end of the string (or . of the .so version) */
5ed6ace5 2060 prefix = XNEWVEC (char, q - p + 1);
68d69835
JM
2061 strncpy (prefix, p, q - p);
2062 prefix[q - p] = 0;
d4058195
KG
2063 for (r = prefix; *r; r++)
2064 if (!ISALNUM ((unsigned char)*r))
2065 *r = '_';
68d69835 2066 if (debug)
ab87f8c8
JL
2067 notice ("\nwrite_c_file - output name is %s, prefix is %s\n",
2068 output_file, prefix);
68d69835 2069
d4c3ec27
KG
2070 initname = concat ("_GLOBAL__FI_", prefix, NULL);
2071 fininame = concat ("_GLOBAL__FD_", prefix, NULL);
44178879
DE
2072#ifdef TARGET_AIX_VERSION
2073 aix_shared_initname = concat ("_GLOBAL__AIXI_", prefix, NULL);
2074 aix_shared_fininame = concat ("_GLOBAL__AIXD_", prefix, NULL);
2075#endif
68d69835
JM
2076
2077 free (prefix);
2078
f9da5064 2079 /* Write the tables as C code. */
68d69835 2080
44178879
DE
2081 /* This count variable is used to prevent multiple calls to the
2082 constructors/destructors.
2083 This guard against multiple calls is important on AIX as the initfini
2084 functions are deliberately invoked multiple times as part of the
2085 mechanisms GCC uses to order constructors across different dependent
2086 shared libraries (see config/rs6000/aix.h).
2087 */
68d69835
JM
2088 fprintf (stream, "static int count;\n");
2089 fprintf (stream, "typedef void entry_pt();\n");
2090 write_list_with_asm (stream, "extern entry_pt ", constructors.first);
0021b564
JM
2091
2092 if (frames)
2093 {
2094 write_list_with_asm (stream, "extern void *", frame_tables.first);
2095
2096 fprintf (stream, "\tstatic void *frame_table[] = {\n");
2097 write_list (stream, "\t\t&", frame_tables.first);
2098 fprintf (stream, "\t0\n};\n");
2099
956d6950
JL
2100 /* This must match what's in frame.h. */
2101 fprintf (stream, "struct object {\n");
2102 fprintf (stream, " void *pc_begin;\n");
2103 fprintf (stream, " void *pc_end;\n");
2104 fprintf (stream, " void *fde_begin;\n");
2105 fprintf (stream, " void *fde_array;\n");
14cd4d23 2106 fprintf (stream, " __SIZE_TYPE__ count;\n");
956d6950
JL
2107 fprintf (stream, " struct object *next;\n");
2108 fprintf (stream, "};\n");
2109
04218b35 2110 fprintf (stream, "extern void __register_frame_info_table_bases (void *, struct object *, void *tbase, void *dbase);\n");
6d8ccdbb 2111 fprintf (stream, "extern void __register_frame_info_table (void *, struct object *);\n");
a3fd4e75 2112 fprintf (stream, "extern void *__deregister_frame_info (void *);\n");
04218b35
AD
2113#ifdef TARGET_AIX_VERSION
2114 fprintf (stream, "extern void *__gcc_unwind_dbase;\n");
2115#endif
0021b564
JM
2116
2117 fprintf (stream, "static void reg_frame () {\n");
956d6950 2118 fprintf (stream, "\tstatic struct object ob;\n");
04218b35
AD
2119#ifdef TARGET_AIX_VERSION
2120 /* Use __gcc_unwind_dbase as the base address for data on AIX.
2121 This might not be the start of the segment, signed offsets assumed.
2122 */
2123 fprintf (stream, "\t__register_frame_info_table_bases (frame_table, &ob, (void *)0, &__gcc_unwind_dbase);\n");
2124#else
6d8ccdbb 2125 fprintf (stream, "\t__register_frame_info_table (frame_table, &ob);\n");
04218b35 2126#endif
0021b564
JM
2127 fprintf (stream, "\t}\n");
2128
2129 fprintf (stream, "static void dereg_frame () {\n");
6d8ccdbb 2130 fprintf (stream, "\t__deregister_frame_info (frame_table);\n");
0021b564
JM
2131 fprintf (stream, "\t}\n");
2132 }
2133
68d69835 2134 fprintf (stream, "void %s() {\n", initname);
0021b564 2135 if (constructors.number > 0 || frames)
68d69835
JM
2136 {
2137 fprintf (stream, "\tstatic entry_pt *ctors[] = {\n");
2138 write_list (stream, "\t\t", constructors.first);
0021b564
JM
2139 if (frames)
2140 fprintf (stream, "\treg_frame,\n");
68d69835
JM
2141 fprintf (stream, "\t};\n");
2142 fprintf (stream, "\tentry_pt **p;\n");
2143 fprintf (stream, "\tif (count++ != 0) return;\n");
0021b564 2144 fprintf (stream, "\tp = ctors + %d;\n", constructors.number + frames);
68d69835
JM
2145 fprintf (stream, "\twhile (p > ctors) (*--p)();\n");
2146 }
603b4064
RK
2147 else
2148 fprintf (stream, "\t++count;\n");
68d69835
JM
2149 fprintf (stream, "}\n");
2150 write_list_with_asm (stream, "extern entry_pt ", destructors.first);
2151 fprintf (stream, "void %s() {\n", fininame);
0021b564 2152 if (destructors.number > 0 || frames)
68d69835
JM
2153 {
2154 fprintf (stream, "\tstatic entry_pt *dtors[] = {\n");
2155 write_list (stream, "\t\t", destructors.first);
0021b564
JM
2156 if (frames)
2157 fprintf (stream, "\tdereg_frame,\n");
68d69835
JM
2158 fprintf (stream, "\t};\n");
2159 fprintf (stream, "\tentry_pt **p;\n");
2160 fprintf (stream, "\tif (--count != 0) return;\n");
2161 fprintf (stream, "\tp = dtors;\n");
2162 fprintf (stream, "\twhile (p < dtors + %d) (*p++)();\n",
0021b564 2163 destructors.number + frames);
68d69835
JM
2164 }
2165 fprintf (stream, "}\n");
2166
603b4064
RK
2167 if (shared_obj)
2168 {
c3284718
RS
2169 COLLECT_SHARED_INIT_FUNC (stream, initname);
2170 COLLECT_SHARED_FINI_FUNC (stream, fininame);
603b4064 2171 }
68d69835
JM
2172}
2173
0f41302f 2174/* Write the constructor/destructor tables. */
4e81a331 2175
081f5e7e 2176#ifndef LD_INIT_SWITCH
4e81a331 2177static void
159b3be1 2178write_c_file_glob (FILE *stream, const char *name ATTRIBUTE_UNUSED)
4e81a331 2179{
f9da5064 2180 /* Write the tables as C code. */
4e81a331 2181
0021b564
JM
2182 int frames = (frame_tables.number > 0);
2183
4e81a331 2184 fprintf (stream, "typedef void entry_pt();\n\n");
159b3be1 2185
17704846 2186 write_list_with_asm (stream, "extern entry_pt ", constructors.first);
0021b564
JM
2187
2188 if (frames)
2189 {
2190 write_list_with_asm (stream, "extern void *", frame_tables.first);
2191
2192 fprintf (stream, "\tstatic void *frame_table[] = {\n");
2193 write_list (stream, "\t\t&", frame_tables.first);
2194 fprintf (stream, "\t0\n};\n");
2195
5a5ab947
PE
2196 /* This must match what's in frame.h. */
2197 fprintf (stream, "struct object {\n");
2198 fprintf (stream, " void *pc_begin;\n");
2199 fprintf (stream, " void *pc_end;\n");
2200 fprintf (stream, " void *fde_begin;\n");
2201 fprintf (stream, " void *fde_array;\n");
2202 fprintf (stream, " __SIZE_TYPE__ count;\n");
2203 fprintf (stream, " struct object *next;\n");
2204 fprintf (stream, "};\n");
2205
6d8ccdbb 2206 fprintf (stream, "extern void __register_frame_info_table (void *, struct object *);\n");
a3fd4e75 2207 fprintf (stream, "extern void *__deregister_frame_info (void *);\n");
0021b564
JM
2208
2209 fprintf (stream, "static void reg_frame () {\n");
5a5ab947 2210 fprintf (stream, "\tstatic struct object ob;\n");
6d8ccdbb 2211 fprintf (stream, "\t__register_frame_info_table (frame_table, &ob);\n");
0021b564
JM
2212 fprintf (stream, "\t}\n");
2213
2214 fprintf (stream, "static void dereg_frame () {\n");
6d8ccdbb 2215 fprintf (stream, "\t__deregister_frame_info (frame_table);\n");
0021b564
JM
2216 fprintf (stream, "\t}\n");
2217 }
2218
4e81a331 2219 fprintf (stream, "\nentry_pt * __CTOR_LIST__[] = {\n");
0021b564 2220 fprintf (stream, "\t(entry_pt *) %d,\n", constructors.number + frames);
ed5b9462 2221 write_list (stream, "\t", constructors.first);
0021b564
JM
2222 if (frames)
2223 fprintf (stream, "\treg_frame,\n");
4e81a331
RS
2224 fprintf (stream, "\t0\n};\n\n");
2225
17704846 2226 write_list_with_asm (stream, "extern entry_pt ", destructors.first);
4e81a331
RS
2227
2228 fprintf (stream, "\nentry_pt * __DTOR_LIST__[] = {\n");
0021b564 2229 fprintf (stream, "\t(entry_pt *) %d,\n", destructors.number + frames);
ed5b9462 2230 write_list (stream, "\t", destructors.first);
0021b564
JM
2231 if (frames)
2232 fprintf (stream, "\tdereg_frame,\n");
4e81a331
RS
2233 fprintf (stream, "\t0\n};\n\n");
2234
d6cf3187
RS
2235 fprintf (stream, "extern entry_pt %s;\n", NAME__MAIN);
2236 fprintf (stream, "entry_pt *__main_reference = %s;\n\n", NAME__MAIN);
4e81a331 2237}
081f5e7e 2238#endif /* ! LD_INIT_SWITCH */
4e81a331 2239
68d69835 2240static void
159b3be1 2241write_c_file (FILE *stream, const char *name)
68d69835 2242{
603b4064
RK
2243#ifndef LD_INIT_SWITCH
2244 if (! shared_obj)
68d69835 2245 write_c_file_glob (stream, name);
603b4064
RK
2246 else
2247#endif
2248 write_c_file_stat (stream, name);
68d69835 2249}
cbbbd917 2250
db126753 2251#ifdef COLLECT_EXPORT_LIST
cbbbd917 2252static void
159b3be1 2253write_aix_file (FILE *stream, struct id *list)
db126753 2254{
db126753 2255 for (; list; list = list->next)
9d4d5d58
DE
2256 {
2257 fputs (list->name, stream);
2258 putc ('\n', stream);
2259 }
db126753
DE
2260}
2261#endif
4e81a331 2262\f
ed5b9462 2263#ifdef OBJECT_FORMAT_NONE
4e81a331 2264
c082f9f3 2265/* Check to make sure the file is an LTO object file. */
d7f09764 2266
4098a6d4
IS
2267static int
2268has_lto_section (void *data, const char *name ATTRIBUTE_UNUSED,
2269 off_t offset ATTRIBUTE_UNUSED,
2270 off_t length ATTRIBUTE_UNUSED)
2271{
2272 int *found = (int *) data;
2273
2274 if (strncmp (name, LTO_SECTION_NAME_PREFIX,
2275 sizeof (LTO_SECTION_NAME_PREFIX) - 1) != 0)
2276 {
2277 if (strncmp (name, OFFLOAD_SECTION_NAME_PREFIX,
2278 sizeof (OFFLOAD_SECTION_NAME_PREFIX) - 1) != 0)
2279 return 1;
2280 }
2281
2282 *found = 1;
2283
2284 /* Stop iteration. */
2285 return 0;
2286}
2287
d7f09764 2288static bool
4098a6d4 2289is_lto_object_file (const char *prog_name)
d7f09764 2290{
4098a6d4
IS
2291 const char *errmsg;
2292 int err;
2293 int found = 0;
2294 off_t inoff = 0;
2295 int infd = open (prog_name, O_RDONLY | O_BINARY);
c082f9f3 2296
4098a6d4
IS
2297 if (infd == -1)
2298 return false;
d7f09764 2299
4098a6d4
IS
2300 simple_object_read *inobj = simple_object_start_read (infd, inoff,
2301 LTO_SEGMENT_NAME,
2302 &errmsg, &err);
2303 if (!inobj)
d7f09764 2304 return false;
c082f9f3 2305
4098a6d4
IS
2306 errmsg = simple_object_find_sections (inobj, has_lto_section,
2307 (void *) &found, &err);
2308 if (! errmsg && found)
c082f9f3 2309 return true;
c082f9f3 2310
4098a6d4 2311 if (errmsg)
a9c697b8 2312 fatal_error (0, "%s: %s", errmsg, xstrerror (err));
c082f9f3 2313 return false;
d7f09764
DN
2314}
2315
ed5b9462 2316/* Generic version to scan the name list of the loaded program for
7e49a4b3 2317 the symbols g++ uses for static constructors and destructors. */
4e81a331
RS
2318
2319static void
7e49a4b3
OH
2320scan_prog_file (const char *prog_name, scanpass which_pass,
2321 scanfilter filter)
4e81a331 2322{
159b3be1 2323 void (*int_handler) (int);
1fff69c9 2324#ifdef SIGQUIT
159b3be1 2325 void (*quit_handler) (int);
1fff69c9 2326#endif
d4058195 2327 char *real_nm_argv[4];
67165eb3 2328 const char **nm_argv = CONST_CAST2 (const char **, char**, real_nm_argv);
4e81a331 2329 int argc = 0;
1fff69c9
ILT
2330 struct pex_obj *pex;
2331 const char *errmsg;
2332 int err;
4e81a331
RS
2333 char *p, buf[1024];
2334 FILE *inf;
2335
68d69835 2336 if (which_pass == PASS_SECOND)
4e81a331
RS
2337 return;
2338
3bec79c5 2339 /* LTO objects must be in a known format. This check prevents
d7f09764 2340 us from accepting an archive containing LTO objects, which
073a8998 2341 gcc cannot currently handle. */
4098a6d4
IS
2342 if (which_pass == PASS_LTOINFO)
2343 {
2344 if(is_lto_object_file (prog_name)) {
2345 add_lto_object (&lto_objects, prog_name);
2346 }
2347 return;
2348 }
d7f09764 2349
db126753 2350 /* If we do not have an `nm', complain. */
bb59f9a6 2351 if (nm_file_name == 0)
904f3daa 2352 fatal_error (input_location, "cannot find %<nm%>");
bb59f9a6 2353
68d69835 2354 nm_argv[argc++] = nm_file_name;
4e81a331 2355 if (NM_FLAGS[0] != '\0')
ed5b9462 2356 nm_argv[argc++] = NM_FLAGS;
4e81a331 2357
ed5b9462 2358 nm_argv[argc++] = prog_name;
0f41302f 2359 nm_argv[argc++] = (char *) 0;
4e81a331 2360
4e81a331 2361 /* Trace if needed. */
608508a6 2362 if (verbose)
4e81a331 2363 {
d4058195
KG
2364 const char **p_argv;
2365 const char *str;
4e81a331 2366
0f41302f 2367 for (p_argv = &nm_argv[0]; (str = *p_argv) != (char *) 0; p_argv++)
4e81a331
RS
2368 fprintf (stderr, " %s", str);
2369
2370 fprintf (stderr, "\n");
2371 }
2372
2373 fflush (stdout);
2374 fflush (stderr);
2375
1fff69c9
ILT
2376 pex = pex_init (PEX_USE_PIPES, "collect2", NULL);
2377 if (pex == NULL)
a9c697b8 2378 fatal_error (input_location, "%<pex_init%> failed: %m");
4e81a331 2379
d7f09764
DN
2380 errmsg = pex_run (pex, 0, nm_file_name, real_nm_argv, NULL, HOST_BIT_BUCKET,
2381 &err);
1fff69c9 2382 if (errmsg != NULL)
4e81a331 2383 {
1fff69c9
ILT
2384 if (err != 0)
2385 {
2386 errno = err;
40fecdd6 2387 fatal_error (input_location, "%s: %m", _(errmsg));
1fff69c9
ILT
2388 }
2389 else
40fecdd6 2390 fatal_error (input_location, errmsg);
4e81a331
RS
2391 }
2392
159b3be1 2393 int_handler = (void (*) (int)) signal (SIGINT, SIG_IGN);
8ab861c7 2394#ifdef SIGQUIT
159b3be1 2395 quit_handler = (void (*) (int)) signal (SIGQUIT, SIG_IGN);
8ab861c7 2396#endif
4e81a331 2397
1fff69c9
ILT
2398 inf = pex_read_output (pex, 0);
2399 if (inf == NULL)
a9c697b8 2400 fatal_error (input_location, "cannot open nm output: %m");
4e81a331
RS
2401
2402 if (debug)
4098a6d4 2403 fprintf (stderr, "\nnm output with constructors/destructors.\n");
4e81a331
RS
2404
2405 /* Read each line of nm output. */
0f41302f 2406 while (fgets (buf, sizeof buf, inf) != (char *) 0)
4e81a331
RS
2407 {
2408 int ch, ch2;
ed5b9462 2409 char *name, *end;
4e81a331 2410
d7f09764
DN
2411 if (debug)
2412 fprintf (stderr, "\t%s\n", buf);
2413
4e81a331 2414 /* If it contains a constructor or destructor name, add the name
7e49a4b3
OH
2415 to the appropriate list unless this is a kind of symbol we're
2416 not supposed to even consider. */
4e81a331
RS
2417
2418 for (p = buf; (ch = *p) != '\0' && ch != '\n' && ch != '_'; p++)
68d69835
JM
2419 if (ch == ' ' && p[1] == 'U' && p[2] == ' ')
2420 break;
4e81a331 2421
68d69835 2422 if (ch != '_')
4e81a331 2423 continue;
159b3be1 2424
ed5b9462
RS
2425 name = p;
2426 /* Find the end of the symbol name.
db126753 2427 Do not include `|', because Encore nm can tack that on the end. */
e9a780ec 2428 for (end = p; (ch2 = *end) != '\0' && !ISSPACE (ch2) && ch2 != '|';
ed5b9462
RS
2429 end++)
2430 continue;
4e81a331 2431
90d326bd 2432
4e81a331 2433 *end = '\0';
44178879 2434
ed5b9462 2435 switch (is_ctor_dtor (name))
4e81a331 2436 {
4326ea40 2437 case SYM_CTOR:
7e49a4b3
OH
2438 if (! (filter & SCAN_CTOR))
2439 break;
68d69835
JM
2440 if (which_pass != PASS_LIB)
2441 add_to_list (&constructors, name);
ed5b9462 2442 break;
4e81a331 2443
4326ea40 2444 case SYM_DTOR:
7e49a4b3
OH
2445 if (! (filter & SCAN_DTOR))
2446 break;
68d69835
JM
2447 if (which_pass != PASS_LIB)
2448 add_to_list (&destructors, name);
2449 break;
2450
4326ea40 2451 case SYM_INIT:
7e49a4b3
OH
2452 if (! (filter & SCAN_INIT))
2453 break;
68d69835 2454 if (which_pass != PASS_LIB)
40fecdd6
JM
2455 fatal_error (input_location, "init function found in object %s",
2456 prog_name);
35e530c5 2457#ifndef LD_INIT_SWITCH
68d69835 2458 add_to_list (&constructors, name);
35e530c5 2459#endif
68d69835
JM
2460 break;
2461
4326ea40 2462 case SYM_FINI:
7e49a4b3
OH
2463 if (! (filter & SCAN_FINI))
2464 break;
68d69835 2465 if (which_pass != PASS_LIB)
40fecdd6
JM
2466 fatal_error (input_location, "fini function found in object %s",
2467 prog_name);
35e530c5 2468#ifndef LD_FINI_SWITCH
ed5b9462 2469 add_to_list (&destructors, name);
35e530c5 2470#endif
ed5b9462 2471 break;
4e81a331 2472
4326ea40 2473 case SYM_DWEH:
7e49a4b3
OH
2474 if (! (filter & SCAN_DWEH))
2475 break;
0021b564
JM
2476 if (which_pass != PASS_LIB)
2477 add_to_list (&frame_tables, name);
6690d24c 2478 break;
0021b564 2479
ed5b9462
RS
2480 default: /* not a constructor or destructor */
2481 continue;
4e81a331 2482 }
4e81a331
RS
2483 }
2484
2485 if (debug)
2486 fprintf (stderr, "\n");
2487
1fff69c9 2488 do_wait (nm_file_name, pex);
4e81a331
RS
2489
2490 signal (SIGINT, int_handler);
8ab861c7 2491#ifdef SIGQUIT
4e81a331 2492 signal (SIGQUIT, quit_handler);
8ab861c7 2493#endif
4e81a331
RS
2494}
2495
68d69835
JM
2496#ifdef LDD_SUFFIX
2497
2498/* Use the List Dynamic Dependencies program to find shared libraries that
2499 the output file depends upon and their initialization/finalization
2500 routines, if any. */
2501
159b3be1
AJ
2502static void
2503scan_libraries (const char *prog_name)
68d69835
JM
2504{
2505 static struct head libraries; /* list of shared libraries found */
2506 struct id *list;
159b3be1 2507 void (*int_handler) (int);
1fff69c9 2508#ifdef SIGQUIT
159b3be1 2509 void (*quit_handler) (int);
1fff69c9 2510#endif
421cba92 2511 char *real_ldd_argv[4];
db9369bb 2512 const char **ldd_argv = CONST_CAST2 (const char **, char **, real_ldd_argv);
68d69835 2513 int argc = 0;
1fff69c9
ILT
2514 struct pex_obj *pex;
2515 const char *errmsg;
2516 int err;
68d69835
JM
2517 char buf[1024];
2518 FILE *inf;
2519
db126753 2520 /* If we do not have an `ldd', complain. */
68d69835
JM
2521 if (ldd_file_name == 0)
2522 {
904f3daa 2523 error ("cannot find %<ldd%>");
68d69835
JM
2524 return;
2525 }
2526
2527 ldd_argv[argc++] = ldd_file_name;
2528 ldd_argv[argc++] = prog_name;
2529 ldd_argv[argc++] = (char *) 0;
2530
68d69835 2531 /* Trace if needed. */
608508a6 2532 if (verbose)
68d69835 2533 {
d4058195
KG
2534 const char **p_argv;
2535 const char *str;
68d69835
JM
2536
2537 for (p_argv = &ldd_argv[0]; (str = *p_argv) != (char *) 0; p_argv++)
2538 fprintf (stderr, " %s", str);
2539
2540 fprintf (stderr, "\n");
2541 }
2542
2543 fflush (stdout);
2544 fflush (stderr);
2545
1fff69c9
ILT
2546 pex = pex_init (PEX_USE_PIPES, "collect2", NULL);
2547 if (pex == NULL)
40fecdd6 2548 fatal_error (input_location, "pex_init failed: %m");
68d69835 2549
1fff69c9
ILT
2550 errmsg = pex_run (pex, 0, ldd_file_name, real_ldd_argv, NULL, NULL, &err);
2551 if (errmsg != NULL)
68d69835 2552 {
1fff69c9
ILT
2553 if (err != 0)
2554 {
2555 errno = err;
40fecdd6 2556 fatal_error (input_location, "%s: %m", _(errmsg));
1fff69c9
ILT
2557 }
2558 else
40fecdd6 2559 fatal_error (input_location, errmsg);
68d69835
JM
2560 }
2561
94d54276 2562 int_handler = (void (*) (int)) signal (SIGINT, SIG_IGN);
68d69835 2563#ifdef SIGQUIT
94d54276 2564 quit_handler = (void (*) (int)) signal (SIGQUIT, SIG_IGN);
68d69835
JM
2565#endif
2566
1fff69c9
ILT
2567 inf = pex_read_output (pex, 0);
2568 if (inf == NULL)
a9c697b8 2569 fatal_error (input_location, "cannot open ldd output: %m");
68d69835
JM
2570
2571 if (debug)
ab87f8c8 2572 notice ("\nldd output with constructors/destructors.\n");
68d69835
JM
2573
2574 /* Read each line of ldd output. */
2575 while (fgets (buf, sizeof buf, inf) != (char *) 0)
2576 {
f5720527 2577 int ch2;
68d69835
JM
2578 char *name, *end, *p = buf;
2579
0f41302f 2580 /* Extract names of libraries and add to list. */
68d69835
JM
2581 PARSE_LDD_OUTPUT (p);
2582 if (p == 0)
2583 continue;
2584
2585 name = p;
2586 if (strncmp (name, "not found", sizeof ("not found") - 1) == 0)
40fecdd6 2587 fatal_error (input_location, "dynamic dependency %s not found", buf);
68d69835 2588
0f41302f 2589 /* Find the end of the symbol name. */
159b3be1 2590 for (end = p;
e9a780ec 2591 (ch2 = *end) != '\0' && ch2 != '\n' && !ISSPACE (ch2) && ch2 != '|';
68d69835
JM
2592 end++)
2593 continue;
2594 *end = '\0';
2595
2596 if (access (name, R_OK) == 0)
c22cacf3 2597 add_to_list (&libraries, name);
68d69835 2598 else
904f3daa
ML
2599 fatal_error (input_location, "unable to open dynamic dependency "
2600 "%qs", buf);
68d69835
JM
2601
2602 if (debug)
2603 fprintf (stderr, "\t%s\n", buf);
2604 }
2605 if (debug)
2606 fprintf (stderr, "\n");
2607
1fff69c9 2608 do_wait (ldd_file_name, pex);
68d69835
JM
2609
2610 signal (SIGINT, int_handler);
2611#ifdef SIGQUIT
2612 signal (SIGQUIT, quit_handler);
2613#endif
2614
e0a21ab9 2615 /* Now iterate through the library list adding their symbols to
68d69835
JM
2616 the list. */
2617 for (list = libraries.first; list; list = list->next)
91e1a80a 2618 scan_prog_file (list->name, PASS_LIB, SCAN_ALL);
68d69835
JM
2619}
2620
2621#endif /* LDD_SUFFIX */
68d69835 2622
ed5b9462
RS
2623#endif /* OBJECT_FORMAT_NONE */
2624
2625\f
2626/*
2627 * COFF specific stuff.
2628 */
2629
2630#ifdef OBJECT_FORMAT_COFF
2631
c3284718 2632# define GCC_SYMBOLS(X) (HEADER (ldptr).f_nsyms)
17704846 2633# define GCC_SYMENT SYMENT
0eb31ee3
DE
2634# if defined (C_WEAKEXT)
2635# define GCC_OK_SYMBOL(X) \
2636 (((X).n_sclass == C_EXT || (X).n_sclass == C_WEAKEXT) && \
c22cacf3
MS
2637 ((X).n_scnum > N_UNDEF) && \
2638 (aix64_flag \
2639 || (((X).n_type & N_TMASK) == (DT_NON << N_BTSHFT) \
2640 || ((X).n_type & N_TMASK) == (DT_FCN << N_BTSHFT))))
0eb31ee3
DE
2641# define GCC_UNDEF_SYMBOL(X) \
2642 (((X).n_sclass == C_EXT || (X).n_sclass == C_WEAKEXT) && \
c22cacf3 2643 ((X).n_scnum == N_UNDEF))
0eb31ee3
DE
2644# else
2645# define GCC_OK_SYMBOL(X) \
2646 (((X).n_sclass == C_EXT) && \
c22cacf3
MS
2647 ((X).n_scnum > N_UNDEF) && \
2648 (aix64_flag \
2649 || (((X).n_type & N_TMASK) == (DT_NON << N_BTSHFT) \
2650 || ((X).n_type & N_TMASK) == (DT_FCN << N_BTSHFT))))
0eb31ee3
DE
2651# define GCC_UNDEF_SYMBOL(X) \
2652 (((X).n_sclass == C_EXT) && ((X).n_scnum == N_UNDEF))
2653# endif
17704846
JW
2654# define GCC_SYMINC(X) ((X).n_numaux+1)
2655# define GCC_SYMZERO(X) 0
05dba9ee
DE
2656
2657/* 0757 = U803XTOCMAGIC (AIX 4.3) and 0767 = U64_TOCMAGIC (AIX V5) */
dfb636dc 2658#if TARGET_AIX_VERSION >= 51
05dba9ee 2659# define GCC_CHECK_HDR(X) \
3bf65591
DE
2660 (((HEADER (X).f_magic == U802TOCMAGIC && ! aix64_flag) \
2661 || (HEADER (X).f_magic == 0767 && aix64_flag)) \
2662 && !(HEADER (X).f_flags & F_LOADONLY))
05dba9ee 2663#else
f5e04914 2664# define GCC_CHECK_HDR(X) \
3bf65591
DE
2665 (((HEADER (X).f_magic == U802TOCMAGIC && ! aix64_flag) \
2666 || (HEADER (X).f_magic == 0757 && aix64_flag)) \
2667 && !(HEADER (X).f_flags & F_LOADONLY))
ed5b9462
RS
2668#endif
2669
4977bab6
ZW
2670#ifdef COLLECT_EXPORT_LIST
2671/* Array of standard AIX libraries which should not
2672 be scanned for ctors/dtors. */
2673static const char *const aix_std_libs[] = {
2674 "/unix",
2675 "/lib/libc.a",
2676 "/lib/libm.a",
2677 "/lib/libc_r.a",
2678 "/lib/libm_r.a",
2679 "/usr/lib/libc.a",
2680 "/usr/lib/libm.a",
2681 "/usr/lib/libc_r.a",
2682 "/usr/lib/libm_r.a",
2683 "/usr/lib/threads/libc.a",
2684 "/usr/ccs/lib/libc.a",
2685 "/usr/ccs/lib/libm.a",
2686 "/usr/ccs/lib/libc_r.a",
2687 "/usr/ccs/lib/libm_r.a",
2688 NULL
2689};
2690
2691/* This function checks the filename and returns 1
2692 if this name matches the location of a standard AIX library. */
159b3be1 2693static int ignore_library (const char *);
4977bab6 2694static int
159b3be1 2695ignore_library (const char *name)
4977bab6 2696{
51136ae8 2697 const char *const *p;
781a1758 2698 size_t length;
51136ae8 2699
781a1758
RS
2700 if (target_system_root[0] != '\0')
2701 {
2702 length = strlen (target_system_root);
2703 if (strncmp (name, target_system_root, length) != 0)
2704 return 0;
2705 name += length;
2706 }
51136ae8
RS
2707 for (p = &aix_std_libs[0]; *p != NULL; ++p)
2708 if (strcmp (name, *p) == 0)
2709 return 1;
4977bab6
ZW
2710 return 0;
2711}
2712#endif /* COLLECT_EXPORT_LIST */
2713
351df804 2714#if defined (HAVE_DECL_LDGETNAME) && !HAVE_DECL_LDGETNAME
159b3be1 2715extern char *ldgetname (LDFILE *, GCC_SYMENT *);
351df804 2716#endif
ed5b9462
RS
2717
2718/* COFF version to scan the name list of the loaded program for
7e49a4b3 2719 the symbols g++ uses for static constructors and destructors. */
ed5b9462
RS
2720
2721static void
7e49a4b3
OH
2722scan_prog_file (const char *prog_name, scanpass which_pass,
2723 scanfilter filter)
ed5b9462 2724{
f3c3d3df 2725 LDFILE *ldptr = NULL;
ed5b9462 2726 int sym_index, sym_count;
db126753 2727 int is_shared = 0;
ed5b9462 2728
ce79110f 2729 if (which_pass != PASS_FIRST && which_pass != PASS_OBJ)
ed5b9462
RS
2730 return;
2731
db126753
DE
2732#ifdef COLLECT_EXPORT_LIST
2733 /* We do not need scanning for some standard C libraries. */
2734 if (which_pass == PASS_FIRST && ignore_library (prog_name))
2735 return;
ed5b9462 2736
db126753
DE
2737 /* On AIX we have a loop, because there is not much difference
2738 between an object and an archive. This trick allows us to
2739 eliminate scan_libraries() function. */
2740 do
ed5b9462 2741 {
db126753 2742#endif
d4058195 2743 /* Some platforms (e.g. OSF4) declare ldopen as taking a
c22cacf3
MS
2744 non-const char * filename parameter, even though it will not
2745 modify that string. So we must cast away const-ness here,
bb95b4e7
OH
2746 using CONST_CAST to prevent complaints from -Wcast-qual. */
2747 if ((ldptr = ldopen (CONST_CAST (char *, prog_name), ldptr)) != NULL)
17704846 2748 {
f5e04914 2749 if (! MY_ISCOFF (HEADER (ldptr).f_magic))
40fecdd6 2750 fatal_error (input_location, "%s: not a COFF file", prog_name);
ed5b9462 2751
db126753 2752 if (GCC_CHECK_HDR (ldptr))
17704846 2753 {
db126753
DE
2754 sym_count = GCC_SYMBOLS (ldptr);
2755 sym_index = GCC_SYMZERO (ldptr);
f5e04914
DE
2756
2757#ifdef COLLECT_EXPORT_LIST
2758 /* Is current archive member a shared object? */
2759 is_shared = HEADER (ldptr).f_flags & F_SHROBJ;
2760#endif
2761
db126753
DE
2762 while (sym_index < sym_count)
2763 {
2764 GCC_SYMENT symbol;
2765
2766 if (ldtbread (ldptr, sym_index, &symbol) <= 0)
2767 break;
2768 sym_index += GCC_SYMINC (symbol);
2769
2770 if (GCC_OK_SYMBOL (symbol))
2771 {
2772 char *name;
ed5b9462 2773
db126753 2774 if ((name = ldgetname (ldptr, &symbol)) == NULL)
e0a21ab9 2775 continue; /* Should never happen. */
ed5b9462 2776
cbbbd917 2777#ifdef XCOFF_DEBUGGING_INFO
db126753
DE
2778 /* All AIX function names have a duplicate entry
2779 beginning with a dot. */
2780 if (*name == '.')
2781 ++name;
ed5b9462
RS
2782#endif
2783
db126753
DE
2784 switch (is_ctor_dtor (name))
2785 {
44178879
DE
2786#if TARGET_AIX_VERSION
2787 /* Add AIX shared library initalisers/finalisers
2788 to the constructors/destructors list of the
2789 current module. */
2790 case SYM_AIXI:
2791 if (! (filter & SCAN_CTOR))
2792 break;
6a513e2a
TR
2793 if (is_shared && !aixlazy_flag
2794#ifdef COLLECT_EXPORT_LIST
2795 && ! static_obj
2796 && ! is_in_list (prog_name, static_libs.first)
2797#endif
2798 )
44178879
DE
2799 add_to_list (&constructors, name);
2800 break;
2801
2802 case SYM_AIXD:
2803 if (! (filter & SCAN_DTOR))
2804 break;
2805 if (is_shared && !aixlazy_flag)
2806 add_to_list (&destructors, name);
2807 break;
2808#endif
2809
4326ea40 2810 case SYM_CTOR:
7e49a4b3
OH
2811 if (! (filter & SCAN_CTOR))
2812 break;
c71791e0
DE
2813 if (! is_shared)
2814 add_to_list (&constructors, name);
aaef9b06 2815#if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
db126753
DE
2816 if (which_pass == PASS_OBJ)
2817 add_to_list (&exports, name);
db126753
DE
2818#endif
2819 break;
2820
4326ea40 2821 case SYM_DTOR:
7e49a4b3
OH
2822 if (! (filter & SCAN_DTOR))
2823 break;
c71791e0
DE
2824 if (! is_shared)
2825 add_to_list (&destructors, name);
aaef9b06 2826#if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
db126753
DE
2827 if (which_pass == PASS_OBJ)
2828 add_to_list (&exports, name);
db126753
DE
2829#endif
2830 break;
2831
2832#ifdef COLLECT_EXPORT_LIST
4326ea40 2833 case SYM_INIT:
7e49a4b3
OH
2834 if (! (filter & SCAN_INIT))
2835 break;
c71791e0 2836#ifndef LD_INIT_SWITCH
db126753
DE
2837 if (is_shared)
2838 add_to_list (&constructors, name);
c71791e0 2839#endif
db126753
DE
2840 break;
2841
4326ea40 2842 case SYM_FINI:
7e49a4b3
OH
2843 if (! (filter & SCAN_FINI))
2844 break;
c71791e0 2845#ifndef LD_INIT_SWITCH
db126753
DE
2846 if (is_shared)
2847 add_to_list (&destructors, name);
c71791e0 2848#endif
db126753
DE
2849 break;
2850#endif
2851
4326ea40 2852 case SYM_DWEH:
7e49a4b3
OH
2853 if (! (filter & SCAN_DWEH))
2854 break;
6690d24c
RH
2855 if (! is_shared)
2856 add_to_list (&frame_tables, name);
aaef9b06 2857#if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
59309a85
GK
2858 if (which_pass == PASS_OBJ)
2859 add_to_list (&exports, name);
59309a85 2860#endif
6690d24c
RH
2861 break;
2862
db126753
DE
2863 default: /* not a constructor or destructor */
2864#ifdef COLLECT_EXPORT_LIST
aaef9b06
DE
2865 /* Explicitly export all global symbols when
2866 building a shared object on AIX, but do not
2867 re-export symbols from another shared object
2868 and do not export symbols if the user
2869 provides an explicit export list. */
2870 if (shared_obj && !is_shared
2871 && which_pass == PASS_OBJ && !export_flag)
04218b35
AD
2872 {
2873 /* Do not auto-export __dso_handle or
2874 __gcc_unwind_dbase. They are required
2875 to be local to each module. */
2876 if (strcmp(name, "__dso_handle") != 0
2877 && strcmp(name, "__gcc_unwind_dbase") != 0)
2878 {
2879 add_to_list (&exports, name);
2880 }
2881 }
db126753
DE
2882#endif
2883 continue;
2884 }
ed5b9462 2885
db126753
DE
2886 if (debug)
2887 fprintf (stderr, "\tsec=%d class=%d type=%s%o %s\n",
2888 symbol.n_scnum, symbol.n_sclass,
2889 (symbol.n_type ? "0" : ""), symbol.n_type,
2890 name);
db126753 2891 }
db126753 2892 }
17704846 2893 }
f5e04914
DE
2894#ifdef COLLECT_EXPORT_LIST
2895 else
2896 {
2897 /* If archive contains both 32-bit and 64-bit objects,
2898 we want to skip objects in other mode so mismatch normal. */
2899 if (debug)
2900 fprintf (stderr, "%s : magic=%o aix64=%d mismatch\n",
2901 prog_name, HEADER (ldptr).f_magic, aix64_flag);
2902 }
2903#endif
ed5b9462 2904 }
db126753
DE
2905 else
2906 {
40fecdd6
JM
2907 fatal_error (input_location, "%s: cannot open as COFF file",
2908 prog_name);
db126753
DE
2909 }
2910#ifdef COLLECT_EXPORT_LIST
2911 /* On AIX loop continues while there are more members in archive. */
ed5b9462 2912 }
db126753
DE
2913 while (ldclose (ldptr) == FAILURE);
2914#else
2915 /* Otherwise we simply close ldptr. */
c3284718 2916 (void) ldclose (ldptr);
db126753 2917#endif
ed5b9462 2918}
5b8619f8 2919#endif /* OBJECT_FORMAT_COFF */
1a8463c9 2920
db126753 2921#ifdef COLLECT_EXPORT_LIST
db126753
DE
2922/* Given a library name without "lib" prefix, this function
2923 returns a full library name including a path. */
2924static char *
159b3be1 2925resolve_lib_name (const char *name)
db126753
DE
2926{
2927 char *lib_buf;
2928 int i, j, l = 0;
7142ae77
DE
2929 /* Library extensions for AIX dynamic linking. */
2930 const char * const libexts[2] = {"a", "so"};
d9518857 2931
db126753
DE
2932 for (i = 0; libpaths[i]; i++)
2933 if (libpaths[i]->max_len > l)
2934 l = libpaths[i]->max_len;
2935
c3284718 2936 lib_buf = XNEWVEC (char, l + strlen (name) + 10);
db126753
DE
2937
2938 for (i = 0; libpaths[i]; i++)
2939 {
2940 struct prefix_list *list = libpaths[i]->plist;
2941 for (; list; list = list->next)
2942 {
be78ffb2 2943 /* The following lines are needed because path_prefix list
ba78087b 2944 may contain directories both with trailing DIR_SEPARATOR and
be78ffb2
JM
2945 without it. */
2946 const char *p = "";
c3284718 2947 if (!IS_DIR_SEPARATOR (list->prefix[strlen (list->prefix)-1]))
be78ffb2 2948 p = "/";
7142ae77 2949 for (j = 0; j < 2; j++)
1a8463c9 2950 {
159b3be1 2951 sprintf (lib_buf, "%s%slib%s.%s",
7142ae77
DE
2952 list->prefix, p, name,
2953 libexts[(j + aixrtl_flag) % 2]);
2954 if (debug) fprintf (stderr, "searching for: %s\n", lib_buf);
db126753 2955 if (file_exists (lib_buf))
1a8463c9 2956 {
7142ae77 2957 if (debug) fprintf (stderr, "found: %s\n", lib_buf);
db126753 2958 return (lib_buf);
1a8463c9 2959 }
1a8463c9 2960 }
1a8463c9
RK
2961 }
2962 }
db126753
DE
2963 if (debug)
2964 fprintf (stderr, "not found\n");
2965 else
40fecdd6 2966 fatal_error (input_location, "library lib%s not found", name);
db126753
DE
2967 return (NULL);
2968}
5b8619f8 2969#endif /* COLLECT_EXPORT_LIST */
11afe8e8
MS
2970
2971#ifdef COLLECT_RUN_DSYMUTIL
2972static int flag_dsym = false;
2973static int flag_idsym = false;
2974
2975static void
2976process_args (int *argcp, char **argv) {
2977 int i, j;
2978 int argc = *argcp;
2979 for (i=0; i<argc; ++i)
2980 {
2981 if (strcmp (argv[i], "-dsym") == 0)
2982 {
2983 flag_dsym = true;
2984 /* Remove the flag, as we handle all processing for it. */
2985 j = i;
2986 do
2987 argv[j] = argv[j+1];
2988 while (++j < argc);
2989 --i;
2990 argc = --(*argcp);
2991 }
2992 else if (strcmp (argv[i], "-idsym") == 0)
2993 {
2994 flag_idsym = true;
2995 /* Remove the flag, as we handle all processing for it. */
2996 j = i;
2997 do
2998 argv[j] = argv[j+1];
2999 while (++j < argc);
3000 --i;
3001 argc = --(*argcp);
3002 }
3003 }
3004}
3005
3006static void
3007do_dsymutil (const char *output_file) {
3008 const char *dsymutil = DSYMUTIL + 1;
3009 struct pex_obj *pex;
3010 char **real_argv = XCNEWVEC (char *, 3);
3011 const char ** argv = CONST_CAST2 (const char **, char **,
3012 real_argv);
3013
3014 argv[0] = dsymutil;
3015 argv[1] = output_file;
3016 argv[2] = (char *) 0;
3017
5f0ad6a5
BS
3018 pex = collect_execute (dsymutil, real_argv, NULL, NULL,
3019 PEX_LAST | PEX_SEARCH, false);
11afe8e8
MS
3020 do_wait (dsymutil, pex);
3021}
3022
3023static void
3024post_ld_pass (bool temp_file) {
3025 if (!(temp_file && flag_idsym) && !flag_dsym)
3026 return;
3027
3028 do_dsymutil (output_file);
3029}
3030#else
3031static void
3032process_args (int *argcp ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) { }
3033static void post_ld_pass (bool temp_file ATTRIBUTE_UNUSED) { }
3034#endif