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