comparison gcc/collect2.c @ 55:77e2b8dfacca gcc-4.4.5

update it from 4.4.3 to 4.5.0
author ryoma <e075725@ie.u-ryukyu.ac.jp>
date Fri, 12 Feb 2010 23:39:51 +0900
parents a06113de4d67
children b7f97abdc517
comparison
equal deleted inserted replaced
52:c156f1bd5cd9 55:77e2b8dfacca
1 /* Collect static initialization info into data structures that can be 1 /* Collect static initialization info into data structures that can be
2 traversed by C++ initialization and finalization routines. 2 traversed by C++ initialization and finalization routines.
3 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 3 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008 4 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009
5 Free Software Foundation, Inc. 5 Free Software Foundation, Inc.
6 Contributed by Chris Smith (csmith@convex.com). 6 Contributed by Chris Smith (csmith@convex.com).
7 Heavily modified by Michael Meissner (meissner@cygnus.com), 7 Heavily modified by Michael Meissner (meissner@cygnus.com),
8 Per Bothner (bothner@cygnus.com), and John Gilmore (gnu@cygnus.com). 8 Per Bothner (bothner@cygnus.com), and John Gilmore (gnu@cygnus.com).
9 9
40 #endif 40 #endif
41 41
42 #define COLLECT 42 #define COLLECT
43 43
44 #include "collect2.h" 44 #include "collect2.h"
45 #include "collect2-aix.h"
45 #include "demangle.h" 46 #include "demangle.h"
46 #include "obstack.h" 47 #include "obstack.h"
47 #include "intl.h" 48 #include "intl.h"
48 #include "version.h" 49 #include "version.h"
49 50
52 functions, so turn it off in a cross-compiler. Likewise, the names of 53 functions, so turn it off in a cross-compiler. Likewise, the names of
53 the utilities are not correct for a cross-compiler; we have to hope that 54 the utilities are not correct for a cross-compiler; we have to hope that
54 cross-versions are in the proper directories. */ 55 cross-versions are in the proper directories. */
55 56
56 #ifdef CROSS_DIRECTORY_STRUCTURE 57 #ifdef CROSS_DIRECTORY_STRUCTURE
58 #ifndef CROSS_AIX_SUPPORT
57 #undef OBJECT_FORMAT_COFF 59 #undef OBJECT_FORMAT_COFF
60 #endif
58 #undef MD_EXEC_PREFIX 61 #undef MD_EXEC_PREFIX
59 #undef REAL_LD_FILE_NAME 62 #undef REAL_LD_FILE_NAME
60 #undef REAL_NM_FILE_NAME 63 #undef REAL_NM_FILE_NAME
61 #undef REAL_STRIP_FILE_NAME 64 #undef REAL_STRIP_FILE_NAME
62 #endif 65 #endif
70 #define OBJECT_FORMAT_NONE 73 #define OBJECT_FORMAT_NONE
71 #endif 74 #endif
72 75
73 #ifdef OBJECT_FORMAT_COFF 76 #ifdef OBJECT_FORMAT_COFF
74 77
78 #ifndef CROSS_DIRECTORY_STRUCTURE
75 #include <a.out.h> 79 #include <a.out.h>
76 #include <ar.h> 80 #include <ar.h>
77 81
78 #ifdef UMAX 82 #ifdef UMAX
79 #include <sgs.h> 83 #include <sgs.h>
84 #undef FREAD 88 #undef FREAD
85 #undef FWRITE 89 #undef FWRITE
86 #endif 90 #endif
87 91
88 #include <ldfcn.h> 92 #include <ldfcn.h>
93 #endif
89 94
90 /* Some systems have an ISCOFF macro, but others do not. In some cases 95 /* Some systems have an ISCOFF macro, but others do not. In some cases
91 the macro may be wrong. MY_ISCOFF is defined in tm.h files for machines 96 the macro may be wrong. MY_ISCOFF is defined in tm.h files for machines
92 that either do not have an ISCOFF macro in /usr/include or for those 97 that either do not have an ISCOFF macro in /usr/include or for those
93 where it is wrong. */ 98 where it is wrong. */
138 int do_collecting = 1; 143 int do_collecting = 1;
139 #else 144 #else
140 int do_collecting = 0; 145 int do_collecting = 0;
141 #endif 146 #endif
142 147
148 /* Cook up an always defined indication of whether we proceed the
149 "EXPORT_LIST" way. */
150
151 #ifdef COLLECT_EXPORT_LIST
152 #define DO_COLLECT_EXPORT_LIST 1
153 #else
154 #define DO_COLLECT_EXPORT_LIST 0
155 #endif
156
143 /* Nonzero if we should suppress the automatic demangling of identifiers 157 /* Nonzero if we should suppress the automatic demangling of identifiers
144 in linker error messages. Set from COLLECT_NO_DEMANGLE. */ 158 in linker error messages. Set from COLLECT_NO_DEMANGLE. */
145 int no_demangle; 159 int no_demangle;
146 160
147 /* Linked lists of constructor and destructor names. */ 161 /* Linked lists of constructor and destructor names. */
158 struct id *first; 172 struct id *first;
159 struct id *last; 173 struct id *last;
160 int number; 174 int number;
161 }; 175 };
162 176
163 /* Enumeration giving which pass this is for scanning the program file. */
164
165 enum pass {
166 PASS_FIRST, /* without constructors */
167 PASS_OBJ, /* individual objects */
168 PASS_LIB, /* looking for shared libraries */
169 PASS_SECOND /* with constructors linked in */
170 };
171
172 int vflag; /* true if -v */ 177 int vflag; /* true if -v */
173 static int rflag; /* true if -r */ 178 static int rflag; /* true if -r */
174 static int strip_flag; /* true if -s */ 179 static int strip_flag; /* true if -s */
175 static const char *demangle_flag; 180 static const char *demangle_flag;
176 #ifdef COLLECT_EXPORT_LIST 181 #ifdef COLLECT_EXPORT_LIST
177 static int export_flag; /* true if -bE */ 182 static int export_flag; /* true if -bE */
178 static int aix64_flag; /* true if -b64 */ 183 static int aix64_flag; /* true if -b64 */
179 static int aixrtl_flag; /* true if -brtl */ 184 static int aixrtl_flag; /* true if -brtl */
180 #endif 185 #endif
181 186
187 enum lto_mode_d {
188 LTO_MODE_NONE, /* Not doing LTO. */
189 LTO_MODE_LTO, /* Normal LTO. */
190 LTO_MODE_WHOPR /* WHOPR. */
191 };
192
193 /* Current LTO mode. */
194 static enum lto_mode_d lto_mode = LTO_MODE_NONE;
195
182 int debug; /* true if -debug */ 196 int debug; /* true if -debug */
183 197
184 static int shared_obj; /* true if -shared */ 198 static int shared_obj; /* true if -shared */
185 199
186 static const char *c_file; /* <xxx>.c for constructor/destructor list. */ 200 static const char *c_file; /* <xxx>.c for constructor/destructor list. */
187 static const char *o_file; /* <xxx>.o for constructor/destructor list. */ 201 static const char *o_file; /* <xxx>.o for constructor/destructor list. */
188 #ifdef COLLECT_EXPORT_LIST 202 #ifdef COLLECT_EXPORT_LIST
189 static const char *export_file; /* <xxx>.x for AIX export list. */ 203 static const char *export_file; /* <xxx>.x for AIX export list. */
190 #endif 204 #endif
205 static char **lto_o_files; /* Output files for LTO. */
191 const char *ldout; /* File for ld stdout. */ 206 const char *ldout; /* File for ld stdout. */
192 const char *lderrout; /* File for ld stderr. */ 207 const char *lderrout; /* File for ld stderr. */
193 static const char *output_file; /* Output file for ld. */ 208 static const char *output_file; /* Output file for ld. */
194 static const char *nm_file_name; /* pathname of nm */ 209 static const char *nm_file_name; /* pathname of nm */
195 #ifdef LDD_SUFFIX 210 #ifdef LDD_SUFFIX
210 static char *response_file; /* Name of any current response file */ 225 static char *response_file; /* Name of any current response file */
211 226
212 struct obstack temporary_obstack; 227 struct obstack temporary_obstack;
213 char * temporary_firstobj; 228 char * temporary_firstobj;
214 229
230 /* A string that must be prepended to a target OS path in order to find
231 it on the host system. */
232 #ifdef TARGET_SYSTEM_ROOT
233 static const char *target_system_root = TARGET_SYSTEM_ROOT;
234 #else
235 static const char *target_system_root = "";
236 #endif
237
215 /* Structure to hold all the directories in which to search for files to 238 /* Structure to hold all the directories in which to search for files to
216 execute. */ 239 execute. */
217 240
218 struct prefix_list 241 struct prefix_list
219 { 242 {
234 static struct path_prefix cmdline_lib_dirs; /* directories specified with -L */ 257 static struct path_prefix cmdline_lib_dirs; /* directories specified with -L */
235 static struct path_prefix libpath_lib_dirs; /* directories in LIBPATH */ 258 static struct path_prefix libpath_lib_dirs; /* directories in LIBPATH */
236 static struct path_prefix *libpaths[3] = {&cmdline_lib_dirs, 259 static struct path_prefix *libpaths[3] = {&cmdline_lib_dirs,
237 &libpath_lib_dirs, NULL}; 260 &libpath_lib_dirs, NULL};
238 #endif 261 #endif
262
263 /* List of names of object files containing LTO information.
264 These are a subset of the object file names appearing on the
265 command line, and must be identical, in the sense of pointer
266 equality, with the names passed to maybe_run_lto_and_relink(). */
267
268 struct lto_object
269 {
270 const char *name; /* Name of object file. */
271 struct lto_object *next; /* Next in linked list. */
272 };
273
274 struct lto_object_list
275 {
276 struct lto_object *first; /* First list element. */
277 struct lto_object *last; /* Last list element. */
278 };
279
280 static struct lto_object_list lto_objects;
239 281
240 /* Special kinds of symbols that a name may denote. */ 282 /* Special kinds of symbols that a name may denote. */
241 283
242 typedef enum { 284 typedef enum {
243 SYM_REGULAR = 0, /* nothing special */ 285 SYM_REGULAR = 0, /* nothing special */
257 static void prefix_from_env (const char *, struct path_prefix *); 299 static void prefix_from_env (const char *, struct path_prefix *);
258 static void prefix_from_string (const char *, struct path_prefix *); 300 static void prefix_from_string (const char *, struct path_prefix *);
259 static void do_wait (const char *, struct pex_obj *); 301 static void do_wait (const char *, struct pex_obj *);
260 static void fork_execute (const char *, char **); 302 static void fork_execute (const char *, char **);
261 static void maybe_unlink (const char *); 303 static void maybe_unlink (const char *);
304 static void maybe_unlink_list (char **);
262 static void add_to_list (struct head *, const char *); 305 static void add_to_list (struct head *, const char *);
263 static int extract_init_priority (const char *); 306 static int extract_init_priority (const char *);
264 static void sort_ids (struct head *); 307 static void sort_ids (struct head *);
265 static void write_list (FILE *, const char *, struct id *); 308 static void write_list (FILE *, const char *, struct id *);
266 #ifdef COLLECT_EXPORT_LIST 309 #ifdef COLLECT_EXPORT_LIST
273 static void write_c_file (FILE *, const char *); 316 static void write_c_file (FILE *, const char *);
274 static void write_c_file_stat (FILE *, const char *); 317 static void write_c_file_stat (FILE *, const char *);
275 #ifndef LD_INIT_SWITCH 318 #ifndef LD_INIT_SWITCH
276 static void write_c_file_glob (FILE *, const char *); 319 static void write_c_file_glob (FILE *, const char *);
277 #endif 320 #endif
278 static void scan_prog_file (const char *, enum pass);
279 #ifdef SCAN_LIBRARIES 321 #ifdef SCAN_LIBRARIES
280 static void scan_libraries (const char *); 322 static void scan_libraries (const char *);
281 #endif 323 #endif
282 #if LINK_ELIMINATE_DUPLICATE_LDIRECTORIES 324 #if LINK_ELIMINATE_DUPLICATE_LDIRECTORIES
283 static int is_in_args (const char *, const char **, const char **); 325 static int is_in_args (const char *, const char **, const char **);
288 #endif 330 #endif
289 static void write_aix_file (FILE *, struct id *); 331 static void write_aix_file (FILE *, struct id *);
290 static char *resolve_lib_name (const char *); 332 static char *resolve_lib_name (const char *);
291 #endif 333 #endif
292 static char *extract_string (const char **); 334 static char *extract_string (const char **);
335
336 /* Enumerations describing which pass this is for scanning the
337 program file ... */
338
339 typedef enum {
340 PASS_FIRST, /* without constructors */
341 PASS_OBJ, /* individual objects */
342 PASS_LIB, /* looking for shared libraries */
343 PASS_SECOND, /* with constructors linked in */
344 PASS_LTOINFO /* looking for objects with LTO info */
345 } scanpass;
346
347 /* ... and which kinds of symbols are to be considered. */
348
349 enum scanfilter_masks {
350 SCAN_NOTHING = 0,
351
352 SCAN_CTOR = 1 << SYM_CTOR,
353 SCAN_DTOR = 1 << SYM_DTOR,
354 SCAN_INIT = 1 << SYM_INIT,
355 SCAN_FINI = 1 << SYM_FINI,
356 SCAN_DWEH = 1 << SYM_DWEH,
357 SCAN_ALL = ~0
358 };
359
360 /* This type is used for parameters and variables which hold
361 combinations of the flags in enum scanfilter_masks. */
362 typedef int scanfilter;
363
364 /* Scan the name list of the loaded program for the symbols g++ uses for
365 static constructors and destructors.
366
367 The SCANPASS argument tells which collect processing pass this is for and
368 the SCANFILTER argument tells which kinds of symbols to consider in this
369 pass. Symbols of a special kind not in the filter mask are considered as
370 regular ones.
371
372 The constructor table begins at __CTOR_LIST__ and contains a count of the
373 number of pointers (or -1 if the constructors are built in a separate
374 section by the linker), followed by the pointers to the constructor
375 functions, terminated with a null pointer. The destructor table has the
376 same format, and begins at __DTOR_LIST__. */
377
378 static void scan_prog_file (const char *, scanpass, scanfilter);
379
293 380
294 /* Delete tempfiles and exit function. */ 381 /* Delete tempfiles and exit function. */
295 382
296 void 383 void
297 collect_exit (int status) 384 collect_exit (int status)
304 391
305 #ifdef COLLECT_EXPORT_LIST 392 #ifdef COLLECT_EXPORT_LIST
306 if (export_file != 0 && export_file[0]) 393 if (export_file != 0 && export_file[0])
307 maybe_unlink (export_file); 394 maybe_unlink (export_file);
308 #endif 395 #endif
396
397 if (lto_o_files)
398 maybe_unlink_list (lto_o_files);
309 399
310 if (ldout != 0 && ldout[0]) 400 if (ldout != 0 && ldout[0])
311 { 401 {
312 dump_file (ldout, stdout); 402 dump_file (ldout, stdout);
313 maybe_unlink (ldout); 403 maybe_unlink (ldout);
413 503
414 #ifdef COLLECT_EXPORT_LIST 504 #ifdef COLLECT_EXPORT_LIST
415 if (export_file != 0 && export_file[0]) 505 if (export_file != 0 && export_file[0])
416 maybe_unlink (export_file); 506 maybe_unlink (export_file);
417 #endif 507 #endif
508
509 if (lto_o_files)
510 maybe_unlink_list (lto_o_files);
418 511
419 if (response_file) 512 if (response_file)
420 maybe_unlink (response_file); 513 maybe_unlink (response_file);
421 514
422 signal (signo, SIG_DFL); 515 signal (signo, SIG_DFL);
536 /* Return the kind of symbol denoted by name S. */ 629 /* Return the kind of symbol denoted by name S. */
537 630
538 static symkind 631 static symkind
539 is_ctor_dtor (const char *s) 632 is_ctor_dtor (const char *s)
540 { 633 {
541 struct names { const char *const name; const int len; const int ret; 634 struct names { const char *const name; const int len; symkind ret;
542 const int two_underscores; }; 635 const int two_underscores; };
543 636
544 const struct names *p; 637 const struct names *p;
545 int ch; 638 int ch;
546 const char *orig_s = s; 639 const char *orig_s = s;
757 else 850 else
758 endp++; 851 endp++;
759 } 852 }
760 free (nstore); 853 free (nstore);
761 } 854 }
855
856 #ifdef OBJECT_FORMAT_NONE
857
858 /* Add an entry for the object file NAME to object file list LIST.
859 New entries are added at the end of the list. The original pointer
860 value of NAME is preserved, i.e., no string copy is performed. */
861
862 static void
863 add_lto_object (struct lto_object_list *list, const char *name)
864 {
865 struct lto_object *n = XNEW (struct lto_object);
866 n->name = name;
867 n->next = NULL;
868
869 if (list->last)
870 list->last->next = n;
871 else
872 list->first = n;
873
874 list->last = n;
875 }
876 #endif /* OBJECT_FORMAT_NONE */
877
878
879 /* Perform a link-time recompilation and relink if any of the object
880 files contain LTO info. The linker command line LTO_LD_ARGV
881 represents the linker command that would produce a final executable
882 without the use of LTO. OBJECT_LST is a vector of object file names
883 appearing in LTO_LD_ARGV that are to be considerd for link-time
884 recompilation, where OBJECT is a pointer to the last valid element.
885 (This awkward convention avoids an impedance mismatch with the
886 usage of similarly-named variables in main().) The elements of
887 OBJECT_LST must be identical, i.e., pointer equal, to the
888 corresponding arguments in LTO_LD_ARGV.
889
890 Upon entry, at least one linker run has been performed without the
891 use of any LTO info that might be present. Any recompilations
892 necessary for template instantiations have been performed, and
893 initializer/finalizer tables have been created if needed and
894 included in the linker command line LTO_LD_ARGV. If any of the
895 object files contain LTO info, we run the LTO back end on all such
896 files, and perform the final link with the LTO back end output
897 substituted for the LTO-optimized files. In some cases, a final
898 link with all link-time generated code has already been performed,
899 so there is no need to relink if no LTO info is found. In other
900 cases, our caller has not produced the final executable, and is
901 relying on us to perform the required link whether LTO info is
902 present or not. In that case, the FORCE argument should be true.
903 Note that the linker command line argument LTO_LD_ARGV passed into
904 this function may be modified in place. */
905
906 static void
907 maybe_run_lto_and_relink (char **lto_ld_argv, char **object_lst,
908 const char **object, bool force)
909 {
910 const char **object_file = CONST_CAST2 (const char **, char **, object_lst);
911
912 int num_lto_c_args = 1; /* Allow space for the terminating NULL. */
913
914 while (object_file < object)
915 {
916 /* If file contains LTO info, add it to the list of LTO objects. */
917 scan_prog_file (*object_file++, PASS_LTOINFO, SCAN_ALL);
918
919 /* Increment the argument count by the number of object file arguments
920 we will add. An upper bound suffices, so just count all of the
921 object files regardless of whether they contain LTO info. */
922 num_lto_c_args++;
923 }
924
925 if (lto_objects.first)
926 {
927 const char *opts;
928 char **lto_c_argv;
929 const char **lto_c_ptr;
930 const char *cp;
931 const char **p, **q, **r;
932 const char **lto_o_ptr;
933 struct lto_object *list;
934 char *lto_wrapper = getenv ("COLLECT_LTO_WRAPPER");
935 struct pex_obj *pex;
936 const char *prog = "lto-wrapper";
937
938 if (!lto_wrapper)
939 fatal ("COLLECT_LTO_WRAPPER must be set.");
940
941 /* There is at least one object file containing LTO info,
942 so we need to run the LTO back end and relink. */
943
944 /* Get compiler options passed down from the parent `gcc' command.
945 These must be passed to the LTO back end. */
946 opts = getenv ("COLLECT_GCC_OPTIONS");
947
948 /* Increment the argument count by the number of inherited options.
949 Some arguments may be filtered out later. Again, an upper bound
950 suffices. */
951
952 cp = opts;
953
954 while (cp && *cp)
955 {
956 extract_string (&cp);
957 num_lto_c_args++;
958 }
959 obstack_free (&temporary_obstack, temporary_firstobj);
960
961 if (debug)
962 num_lto_c_args++;
963
964 /* Increment the argument count by the number of initial
965 arguments added below. */
966 num_lto_c_args += 9;
967
968 lto_c_argv = (char **) xcalloc (sizeof (char *), num_lto_c_args);
969 lto_c_ptr = CONST_CAST2 (const char **, char **, lto_c_argv);
970
971 *lto_c_ptr++ = lto_wrapper;
972 *lto_c_ptr++ = c_file_name;
973
974 cp = opts;
975
976 while (cp && *cp)
977 {
978 const char *s = extract_string (&cp);
979
980 /* Pass the option or argument to the wrapper. */
981 *lto_c_ptr++ = xstrdup (s);
982 }
983 obstack_free (&temporary_obstack, temporary_firstobj);
984
985 if (debug)
986 *lto_c_ptr++ = xstrdup ("-debug");
987
988 /* Add LTO objects to the wrapper command line. */
989 for (list = lto_objects.first; list; list = list->next)
990 *lto_c_ptr++ = list->name;
991
992 *lto_c_ptr = NULL;
993
994 /* Save intermediate WPA files in lto1 if debug. */
995 if (debug)
996 putenv (xstrdup ("WPA_SAVE_LTRANS=1"));
997
998 /* Run the LTO back end. */
999 pex = collect_execute (prog, lto_c_argv, NULL, NULL, PEX_SEARCH);
1000 {
1001 int c;
1002 FILE *stream;
1003 size_t i, num_files;
1004 char *start, *end;
1005
1006 stream = pex_read_output (pex, 0);
1007 gcc_assert (stream);
1008
1009 num_files = 0;
1010 while ((c = getc (stream)) != EOF)
1011 {
1012 obstack_1grow (&temporary_obstack, c);
1013 if (c == '\n')
1014 ++num_files;
1015 }
1016
1017 lto_o_files = XNEWVEC (char *, num_files + 1);
1018 lto_o_files[num_files] = NULL;
1019 start = XOBFINISH (&temporary_obstack, char *);
1020 for (i = 0; i < num_files; ++i)
1021 {
1022 end = start;
1023 while (*end != '\n')
1024 ++end;
1025 *end = '\0';
1026
1027 lto_o_files[i] = xstrdup (start);
1028
1029 start = end + 1;
1030 }
1031
1032 obstack_free (&temporary_obstack, temporary_firstobj);
1033 }
1034 do_wait (prog, pex);
1035 pex = NULL;
1036
1037 /* After running the LTO back end, we will relink, substituting
1038 the LTO output for the object files that we submitted to the
1039 LTO. Here, we modify the linker command line for the relink. */
1040 p = CONST_CAST2 (const char **, char **, lto_ld_argv);
1041 lto_o_ptr = CONST_CAST2 (const char **, char **, lto_o_files);
1042
1043 while (*p != NULL)
1044 {
1045 for (list = lto_objects.first; list; list = list->next)
1046 {
1047 if (*p == list->name) /* Note test for pointer equality! */
1048 {
1049 /* Excise argument from linker command line. */
1050 if (*lto_o_ptr)
1051 {
1052 /* Replace first argument with LTO output file. */
1053 *p++ = *lto_o_ptr++;
1054 }
1055 else
1056 {
1057 /* Move following arguments one position earlier,
1058 overwriting the current argument. */
1059 q = p;
1060 r = p + 1;
1061 while (*r != NULL)
1062 *q++ = *r++;
1063 *q = NULL;
1064 }
1065
1066 /* No need to continue searching the LTO object list. */
1067 break;
1068 }
1069 }
1070
1071 /* If we didn't find a match, move on to the next argument.
1072 Otherwise, P has been set to the correct argument position
1073 at which to continue. */
1074 if (!list) ++p;
1075 }
1076
1077 /* The code above assumes we will never have more lto output files than
1078 input files. Otherwise, we need to resize lto_ld_argv. Check this
1079 assumption. */
1080 if (*lto_o_ptr)
1081 fatal ("too many lto output files");
1082
1083 /* Run the linker again, this time replacing the object files
1084 optimized by the LTO with the temporary file generated by the LTO. */
1085 fork_execute ("ld", lto_ld_argv);
1086
1087 maybe_unlink_list (lto_o_files);
1088 }
1089 else if (force)
1090 {
1091 /* Our caller is relying on us to do the link
1092 even though there is no LTO back end work to be done. */
1093 fork_execute ("ld", lto_ld_argv);
1094 }
1095 }
762 1096
763 /* Main program. */ 1097 /* Main program. */
764 1098
765 int 1099 int
766 main (int argc, char **argv) 1100 main (int argc, char **argv)
767 { 1101 {
768 static const char *const ld_suffix = "ld"; 1102 static const char *const ld_suffix = "ld";
1103 static const char *const plugin_ld_suffix = PLUGIN_LD;
769 static const char *const real_ld_suffix = "real-ld"; 1104 static const char *const real_ld_suffix = "real-ld";
770 static const char *const collect_ld_suffix = "collect-ld"; 1105 static const char *const collect_ld_suffix = "collect-ld";
771 static const char *const nm_suffix = "nm"; 1106 static const char *const nm_suffix = "nm";
772 static const char *const gnm_suffix = "gnm"; 1107 static const char *const gnm_suffix = "gnm";
773 #ifdef LDD_SUFFIX 1108 #ifdef LDD_SUFFIX
782 But it we look for a program in the system directories, we need to 1117 But it we look for a program in the system directories, we need to
783 qualify the program name with the target machine. */ 1118 qualify the program name with the target machine. */
784 1119
785 const char *const full_ld_suffix = 1120 const char *const full_ld_suffix =
786 concat(target_machine, "-", ld_suffix, NULL); 1121 concat(target_machine, "-", ld_suffix, NULL);
1122 const char *const full_plugin_ld_suffix =
1123 concat(target_machine, "-", plugin_ld_suffix, NULL);
787 const char *const full_nm_suffix = 1124 const char *const full_nm_suffix =
788 concat (target_machine, "-", nm_suffix, NULL); 1125 concat (target_machine, "-", nm_suffix, NULL);
789 const char *const full_gnm_suffix = 1126 const char *const full_gnm_suffix =
790 concat (target_machine, "-", gnm_suffix, NULL); 1127 concat (target_machine, "-", gnm_suffix, NULL);
791 #ifdef LDD_SUFFIX 1128 #ifdef LDD_SUFFIX
796 concat (target_machine, "-", strip_suffix, NULL); 1133 concat (target_machine, "-", strip_suffix, NULL);
797 const char *const full_gstrip_suffix = 1134 const char *const full_gstrip_suffix =
798 concat (target_machine, "-", gstrip_suffix, NULL); 1135 concat (target_machine, "-", gstrip_suffix, NULL);
799 #else 1136 #else
800 const char *const full_ld_suffix = ld_suffix; 1137 const char *const full_ld_suffix = ld_suffix;
1138 const char *const full_plugin_ld_suffix = plugin_ld_suffix;
801 const char *const full_nm_suffix = nm_suffix; 1139 const char *const full_nm_suffix = nm_suffix;
802 const char *const full_gnm_suffix = gnm_suffix; 1140 const char *const full_gnm_suffix = gnm_suffix;
803 #ifdef LDD_SUFFIX 1141 #ifdef LDD_SUFFIX
804 const char *const full_ldd_suffix = ldd_suffix; 1142 const char *const full_ldd_suffix = ldd_suffix;
805 #endif 1143 #endif
816 const char *p; 1154 const char *p;
817 char **c_argv; 1155 char **c_argv;
818 const char **c_ptr; 1156 const char **c_ptr;
819 char **ld1_argv; 1157 char **ld1_argv;
820 const char **ld1; 1158 const char **ld1;
1159 bool use_plugin = false;
1160
1161 /* The kinds of symbols we will have to consider when scanning the
1162 outcome of a first pass link. This is ALL to start with, then might
1163 be adjusted before getting to the first pass link per se, typically on
1164 AIX where we perform an early scan of objects and libraries to fetch
1165 the list of global ctors/dtors and make sure they are not garbage
1166 collected. */
1167 scanfilter ld1_filter = SCAN_ALL;
1168
821 char **ld2_argv; 1169 char **ld2_argv;
822 const char **ld2; 1170 const char **ld2;
823 char **object_lst; 1171 char **object_lst;
824 const char **object; 1172 const char **object;
825 int first_file; 1173 int first_file;
855 gcc_init_libintl (); 1203 gcc_init_libintl ();
856 1204
857 /* Do not invoke xcalloc before this point, since locale needs to be 1205 /* Do not invoke xcalloc before this point, since locale needs to be
858 set first, in case a diagnostic is issued. */ 1206 set first, in case a diagnostic is issued. */
859 1207
860 ld1 = (const char **)(ld1_argv = XCNEWVEC (char *, argc+4)); 1208 ld1_argv = XCNEWVEC (char *, argc + 4);
861 ld2 = (const char **)(ld2_argv = XCNEWVEC (char *, argc+11)); 1209 ld1 = CONST_CAST2 (const char **, char **, ld1_argv);
862 object = (const char **)(object_lst = XCNEWVEC (char *, argc)); 1210 ld2_argv = XCNEWVEC (char *, argc + 11);
1211 ld2 = CONST_CAST2 (const char **, char **, ld2_argv);
1212 object_lst = XCNEWVEC (char *, argc);
1213 object = CONST_CAST2 (const char **, char **, object_lst);
863 1214
864 #ifdef DEBUG 1215 #ifdef DEBUG
865 debug = 1; 1216 debug = 1;
866 #endif 1217 #endif
867 1218
868 /* Parse command line early for instances of -debug. This allows 1219 /* Parse command line early for instances of -debug. This allows
869 the debug flag to be set before functions like find_a_file() 1220 the debug flag to be set before functions like find_a_file()
870 are called. */ 1221 are called. We also look for the -flto or -fwhopr flag to know
1222 what LTO mode we are in. */
871 { 1223 {
872 int i; 1224 int i;
873 1225
874 for (i = 1; argv[i] != NULL; i ++) 1226 for (i = 1; argv[i] != NULL; i ++)
875 { 1227 {
876 if (! strcmp (argv[i], "-debug")) 1228 if (! strcmp (argv[i], "-debug"))
877 debug = 1; 1229 debug = 1;
1230 else if (! strcmp (argv[i], "-flto") && ! use_plugin)
1231 lto_mode = LTO_MODE_LTO;
1232 else if (! strcmp (argv[i], "-fwhopr") && ! use_plugin)
1233 lto_mode = LTO_MODE_WHOPR;
1234 else if (! strcmp (argv[i], "-plugin"))
1235 {
1236 use_plugin = true;
1237 lto_mode = LTO_MODE_NONE;
1238 }
1239 #ifdef COLLECT_EXPORT_LIST
1240 /* since -brtl, -bexport, -b64 are not position dependent
1241 also check for them here */
1242 if ((argv[i][0] == '-') && (argv[i][1] == 'b'))
1243 {
1244 arg = argv[i];
1245 /* We want to disable automatic exports on AIX when user
1246 explicitly puts an export list in command line */
1247 if (arg[2] == 'E' || strncmp (&arg[2], "export", 6) == 0)
1248 export_flag = 1;
1249 else if (arg[2] == '6' && arg[3] == '4')
1250 aix64_flag = 1;
1251 else if (arg[2] == 'r' && arg[3] == 't' && arg[4] == 'l')
1252 aixrtl_flag = 1;
1253 }
1254 #endif
878 } 1255 }
879 vflag = debug; 1256 vflag = debug;
880 } 1257 }
881 1258
882 #ifndef DEFAULT_A_OUT_NAME 1259 #ifndef DEFAULT_A_OUT_NAME
899 num_c_args++; 1276 num_c_args++;
900 } 1277 }
901 obstack_free (&temporary_obstack, temporary_firstobj); 1278 obstack_free (&temporary_obstack, temporary_firstobj);
902 1279
903 /* -fno-profile-arcs -fno-test-coverage -fno-branch-probabilities 1280 /* -fno-profile-arcs -fno-test-coverage -fno-branch-probabilities
904 -fno-exceptions -w */ 1281 -fno-exceptions -w -fno-whole-program */
905 num_c_args += 5; 1282 num_c_args += 6;
906 1283
907 c_ptr = (const char **) (c_argv = XCNEWVEC (char *, num_c_args)); 1284 c_argv = XCNEWVEC (char *, num_c_args);
1285 c_ptr = CONST_CAST2 (const char **, char **, c_argv);
908 1286
909 if (argc < 2) 1287 if (argc < 2)
910 fatal ("no arguments"); 1288 fatal ("no arguments");
911 1289
912 #ifdef SIGQUIT 1290 #ifdef SIGQUIT
953 if (ld_file_name == 0) 1331 if (ld_file_name == 0)
954 ld_file_name = find_a_file (&cpath, collect_ld_suffix); 1332 ld_file_name = find_a_file (&cpath, collect_ld_suffix);
955 /* Search the compiler directories for `ld'. We have protection against 1333 /* Search the compiler directories for `ld'. We have protection against
956 recursive calls in find_a_file. */ 1334 recursive calls in find_a_file. */
957 if (ld_file_name == 0) 1335 if (ld_file_name == 0)
958 ld_file_name = find_a_file (&cpath, ld_suffix); 1336 ld_file_name = find_a_file (&cpath,
1337 use_plugin
1338 ? plugin_ld_suffix
1339 : ld_suffix);
959 /* Search the ordinary system bin directories 1340 /* Search the ordinary system bin directories
960 for `ld' (if native linking) or `TARGET-ld' (if cross). */ 1341 for `ld' (if native linking) or `TARGET-ld' (if cross). */
961 if (ld_file_name == 0) 1342 if (ld_file_name == 0)
962 ld_file_name = find_a_file (&path, full_ld_suffix); 1343 ld_file_name = find_a_file (&path,
1344 use_plugin
1345 ? full_plugin_ld_suffix
1346 : full_ld_suffix);
963 1347
964 #ifdef REAL_NM_FILE_NAME 1348 #ifdef REAL_NM_FILE_NAME
965 nm_file_name = find_a_file (&path, REAL_NM_FILE_NAME); 1349 nm_file_name = find_a_file (&path, REAL_NM_FILE_NAME);
966 if (nm_file_name == 0) 1350 if (nm_file_name == 0)
967 #endif 1351 #endif
1067 *c_ptr++ = "-fno-profile-arcs"; 1451 *c_ptr++ = "-fno-profile-arcs";
1068 *c_ptr++ = "-fno-test-coverage"; 1452 *c_ptr++ = "-fno-test-coverage";
1069 *c_ptr++ = "-fno-branch-probabilities"; 1453 *c_ptr++ = "-fno-branch-probabilities";
1070 *c_ptr++ = "-fno-exceptions"; 1454 *c_ptr++ = "-fno-exceptions";
1071 *c_ptr++ = "-w"; 1455 *c_ptr++ = "-w";
1456 *c_ptr++ = "-fno-whole-program";
1072 1457
1073 /* !!! When GCC calls collect2, 1458 /* !!! When GCC calls collect2,
1074 it does not know whether it is calling collect2 or ld. 1459 it does not know whether it is calling collect2 or ld.
1075 So collect2 cannot meaningfully understand any options 1460 So collect2 cannot meaningfully understand any options
1076 except those ld understands. 1461 except those ld understands.
1093 1478
1094 if (arg[0] == '-') 1479 if (arg[0] == '-')
1095 { 1480 {
1096 switch (arg[1]) 1481 switch (arg[1])
1097 { 1482 {
1098 #ifdef COLLECT_EXPORT_LIST
1099 /* We want to disable automatic exports on AIX when user
1100 explicitly puts an export list in command line */
1101 case 'b':
1102 if (arg[2] == 'E' || strncmp (&arg[2], "export", 6) == 0)
1103 export_flag = 1;
1104 else if (arg[2] == '6' && arg[3] == '4')
1105 aix64_flag = 1;
1106 else if (arg[2] == 'r' && arg[3] == 't' && arg[4] == 'l')
1107 aixrtl_flag = 1;
1108 break;
1109 #endif
1110
1111 case 'd': 1483 case 'd':
1112 if (!strcmp (arg, "-debug")) 1484 if (!strcmp (arg, "-debug"))
1113 { 1485 {
1114 /* Already parsed. */ 1486 /* Already parsed. */
1115 ld1--; 1487 ld1--;
1120 ++argv; 1492 ++argv;
1121 *ld1++ = *ld2++ = *argv; 1493 *ld1++ = *ld2++ = *argv;
1122 } 1494 }
1123 break; 1495 break;
1124 1496
1497 case 'f':
1498 if (strcmp (arg, "-flto") == 0 || strcmp (arg, "-fwhopr") == 0)
1499 {
1500 #ifdef ENABLE_LTO
1501 /* Do not pass LTO flag to the linker. */
1502 ld1--;
1503 ld2--;
1504 #else
1505 error ("LTO support has not been enabled in this "
1506 "configuration");
1507 #endif
1508 }
1509 break;
1510
1125 case 'l': 1511 case 'l':
1126 if (first_file) 1512 if (first_file)
1127 { 1513 {
1128 /* place o_file BEFORE this argument! */ 1514 /* place o_file BEFORE this argument! */
1129 first_file = 0; 1515 first_file = 0;
1148 add_prefix (&cmdline_lib_dirs, arg+2); 1534 add_prefix (&cmdline_lib_dirs, arg+2);
1149 break; 1535 break;
1150 #else 1536 #else
1151 #if LINK_ELIMINATE_DUPLICATE_LDIRECTORIES 1537 #if LINK_ELIMINATE_DUPLICATE_LDIRECTORIES
1152 case 'L': 1538 case 'L':
1153 if (is_in_args (arg, (const char **) ld1_argv, ld1-1)) 1539 if (is_in_args (arg,
1540 CONST_CAST2 (const char **, char **, ld1_argv),
1541 ld1 - 1))
1154 --ld1; 1542 --ld1;
1155 break; 1543 break;
1156 #endif /* LINK_ELIMINATE_DUPLICATE_LDIRECTORIES */ 1544 #endif /* LINK_ELIMINATE_DUPLICATE_LDIRECTORIES */
1157 #endif 1545 #endif
1158 1546
1213 } 1601 }
1214 #endif 1602 #endif
1215 ld1--; 1603 ld1--;
1216 ld2--; 1604 ld2--;
1217 } 1605 }
1606 else if (strncmp (arg, "--sysroot=", 10) == 0)
1607 target_system_root = arg + 10;
1218 break; 1608 break;
1219 } 1609 }
1220 } 1610 }
1221 else if ((p = strrchr (arg, '.')) != (char *) 0 1611 else if ((p = strrchr (arg, '.')) != (char *) 0
1222 && (strcmp (p, ".o") == 0 || strcmp (p, ".a") == 0 1612 && (strcmp (p, ".o") == 0 || strcmp (p, ".a") == 0
1256 fprintf (stderr, "List of libraries:\n"); 1646 fprintf (stderr, "List of libraries:\n");
1257 dump_list (stderr, "\t", libs.first); 1647 dump_list (stderr, "\t", libs.first);
1258 } 1648 }
1259 1649
1260 /* The AIX linker will discard static constructors in object files if 1650 /* The AIX linker will discard static constructors in object files if
1261 nothing else in the file is referenced, so look at them first. */ 1651 nothing else in the file is referenced, so look at them first. Unless
1652 we are building a shared object, ignore the eh frame tables, as we
1653 would otherwise reference them all, hence drag all the corresponding
1654 objects even if nothing else is referenced. */
1262 { 1655 {
1263 const char **export_object_lst = (const char **)object_lst; 1656 const char **export_object_lst
1264 1657 = CONST_CAST2 (const char **, char **, object_lst);
1265 while (export_object_lst < object) 1658
1266 scan_prog_file (*export_object_lst++, PASS_OBJ);
1267 }
1268 {
1269 struct id *list = libs.first; 1659 struct id *list = libs.first;
1270 1660
1661 /* Compute the filter to use from the current one, do scan, then adjust
1662 the "current" filter to remove what we just included here. This will
1663 control whether we need a first pass link later on or not, and what
1664 will remain to be scanned there. */
1665
1666 scanfilter this_filter
1667 = shared_obj ? ld1_filter : (ld1_filter & ~SCAN_DWEH);
1668
1669 while (export_object_lst < object)
1670 scan_prog_file (*export_object_lst++, PASS_OBJ, this_filter);
1671
1271 for (; list; list = list->next) 1672 for (; list; list = list->next)
1272 scan_prog_file (list->name, PASS_FIRST); 1673 scan_prog_file (list->name, PASS_FIRST, this_filter);
1674
1675 ld1_filter = ld1_filter & ~this_filter;
1273 } 1676 }
1274 1677
1275 if (exports.first) 1678 if (exports.first)
1276 { 1679 {
1277 char *buf = concat ("-bE:", export_file, NULL); 1680 char *buf = concat ("-bE:", export_file, NULL);
1338 1741
1339 fprintf (stderr, "\n"); 1742 fprintf (stderr, "\n");
1340 } 1743 }
1341 1744
1342 /* Load the program, searching all libraries and attempting to provide 1745 /* Load the program, searching all libraries and attempting to provide
1343 undefined symbols from repository information. */ 1746 undefined symbols from repository information.
1344 1747
1345 /* On AIX we do this later. */ 1748 If -r or they will be run via some other method, do not build the
1346 #ifndef COLLECT_EXPORT_LIST
1347 do_tlink (ld1_argv, object_lst);
1348 #endif
1349
1350 /* If -r or they will be run via some other method, do not build the
1351 constructor or destructor list, just return now. */ 1749 constructor or destructor list, just return now. */
1352 if (rflag 1750 {
1353 #ifndef COLLECT_EXPORT_LIST 1751 bool early_exit
1354 || ! do_collecting 1752 = rflag || (! DO_COLLECT_EXPORT_LIST && ! do_collecting);
1355 #endif 1753
1356 ) 1754 /* Perform the first pass link now, if we're about to exit or if we need
1357 { 1755 to scan for things we haven't collected yet before pursuing further.
1358 #ifdef COLLECT_EXPORT_LIST 1756
1359 /* Do the link we avoided above if we are exiting. */ 1757 On AIX, the latter typically includes nothing for shared objects or
1758 frame tables for an executable, out of what the required early scan on
1759 objects and libraries has performed above. In the !shared_obj case, we
1760 expect the relevant tables to be dragged together with their associated
1761 functions from precise cross reference insertions by the compiler. */
1762
1763 if (early_exit || ld1_filter != SCAN_NOTHING)
1360 do_tlink (ld1_argv, object_lst); 1764 do_tlink (ld1_argv, object_lst);
1361 1765
1362 /* But make sure we delete the export file we may have created. */ 1766 if (early_exit)
1363 if (export_file != 0 && export_file[0]) 1767 {
1364 maybe_unlink (export_file); 1768 #ifdef COLLECT_EXPORT_LIST
1365 #endif 1769 /* Make sure we delete the export file we may have created. */
1366 maybe_unlink (c_file); 1770 if (export_file != 0 && export_file[0])
1367 maybe_unlink (o_file); 1771 maybe_unlink (export_file);
1368 return 0; 1772 #endif
1369 } 1773 if (lto_mode)
1370 1774 maybe_run_lto_and_relink (ld1_argv, object_lst, object, false);
1371 /* Examine the namelist with nm and search it for static constructors 1775
1372 and destructors to call. 1776 maybe_unlink (c_file);
1373 Write the constructor and destructor tables to a .s file and reload. */ 1777 maybe_unlink (o_file);
1374 1778 return 0;
1375 /* On AIX we already scanned for global constructors/destructors. */ 1779 }
1376 #ifndef COLLECT_EXPORT_LIST 1780 }
1377 scan_prog_file (output_file, PASS_FIRST); 1781
1378 #endif 1782 /* Unless we have done it all already, examine the namelist and search for
1783 static constructors and destructors to call. Write the constructor and
1784 destructor tables to a .s file and reload. */
1785
1786 if (ld1_filter != SCAN_NOTHING)
1787 scan_prog_file (output_file, PASS_FIRST, ld1_filter);
1379 1788
1380 #ifdef SCAN_LIBRARIES 1789 #ifdef SCAN_LIBRARIES
1381 scan_libraries (output_file); 1790 scan_libraries (output_file);
1382 #endif 1791 #endif
1383 1792
1385 { 1794 {
1386 notice ("%d constructor(s) found\n", constructors.number); 1795 notice ("%d constructor(s) found\n", constructors.number);
1387 notice ("%d destructor(s) found\n", destructors.number); 1796 notice ("%d destructor(s) found\n", destructors.number);
1388 notice ("%d frame table(s) found\n", frame_tables.number); 1797 notice ("%d frame table(s) found\n", frame_tables.number);
1389 } 1798 }
1799
1800 /* If the scan exposed nothing of special interest, there's no need to
1801 generate the glue code and relink so return now. */
1390 1802
1391 if (constructors.number == 0 && destructors.number == 0 1803 if (constructors.number == 0 && destructors.number == 0
1392 && frame_tables.number == 0 1804 && frame_tables.number == 0
1393 #if defined (SCAN_LIBRARIES) || defined (COLLECT_EXPORT_LIST) 1805 #if defined (SCAN_LIBRARIES) || defined (COLLECT_EXPORT_LIST)
1394 /* If we will be running these functions ourselves, we want to emit 1806 /* If we will be running these functions ourselves, we want to emit
1396 dependent programs when we add static objects. */ 1808 dependent programs when we add static objects. */
1397 && ! shared_obj 1809 && ! shared_obj
1398 #endif 1810 #endif
1399 ) 1811 )
1400 { 1812 {
1401 #ifdef COLLECT_EXPORT_LIST 1813 /* Do tlink without additional code generation now if we didn't
1402 /* Do tlink without additional code generation. */ 1814 do it earlier for scanning purposes. */
1403 do_tlink (ld1_argv, object_lst); 1815 if (ld1_filter == SCAN_NOTHING)
1404 #endif 1816 do_tlink (ld1_argv, object_lst);
1817
1818 if (lto_mode)
1819 maybe_run_lto_and_relink (ld1_argv, object_lst, object, false);
1820
1405 /* Strip now if it was requested on the command line. */ 1821 /* Strip now if it was requested on the command line. */
1406 if (strip_flag) 1822 if (strip_flag)
1407 { 1823 {
1408 char **real_strip_argv = XCNEWVEC (char *, 3); 1824 char **real_strip_argv = XCNEWVEC (char *, 3);
1409 const char ** strip_argv = (const char **) real_strip_argv; 1825 const char ** strip_argv = CONST_CAST2 (const char **, char **,
1826 real_strip_argv);
1410 1827
1411 strip_argv[0] = strip_file_name; 1828 strip_argv[0] = strip_file_name;
1412 strip_argv[1] = output_file; 1829 strip_argv[1] = output_file;
1413 strip_argv[2] = (char *) 0; 1830 strip_argv[2] = (char *) 0;
1414 fork_execute ("strip", real_strip_argv); 1831 fork_execute ("strip", real_strip_argv);
1492 1909
1493 fork_execute ("gcc", c_argv); 1910 fork_execute ("gcc", c_argv);
1494 #ifdef COLLECT_EXPORT_LIST 1911 #ifdef COLLECT_EXPORT_LIST
1495 /* On AIX we must call tlink because of possible templates resolution. */ 1912 /* On AIX we must call tlink because of possible templates resolution. */
1496 do_tlink (ld2_argv, object_lst); 1913 do_tlink (ld2_argv, object_lst);
1914
1915 if (lto_mode)
1916 maybe_run_lto_and_relink (ld2_argv, object_lst, object, false);
1497 #else 1917 #else
1498 /* Otherwise, simply call ld because tlink is already done. */ 1918 /* Otherwise, simply call ld because tlink is already done. */
1499 fork_execute ("ld", ld2_argv); 1919 if (lto_mode)
1920 maybe_run_lto_and_relink (ld2_argv, object_lst, object, true);
1921 else
1922 fork_execute ("ld", ld2_argv);
1500 1923
1501 /* Let scan_prog_file do any final mods (OSF/rose needs this for 1924 /* Let scan_prog_file do any final mods (OSF/rose needs this for
1502 constructors/destructors in shared libraries. */ 1925 constructors/destructors in shared libraries. */
1503 scan_prog_file (output_file, PASS_SECOND); 1926 scan_prog_file (output_file, PASS_SECOND, SCAN_ALL);
1504 #endif 1927 #endif
1505 1928
1506 maybe_unlink (c_file); 1929 maybe_unlink (c_file);
1507 maybe_unlink (o_file); 1930 maybe_unlink (o_file);
1508 1931
1562 1985
1563 /* Execute a program, and wait for the reply. */ 1986 /* Execute a program, and wait for the reply. */
1564 1987
1565 struct pex_obj * 1988 struct pex_obj *
1566 collect_execute (const char *prog, char **argv, const char *outname, 1989 collect_execute (const char *prog, char **argv, const char *outname,
1567 const char *errname) 1990 const char *errname, int flags)
1568 { 1991 {
1569 struct pex_obj *pex; 1992 struct pex_obj *pex;
1570 const char *errmsg; 1993 const char *errmsg;
1571 int err; 1994 int err;
1572 char *response_arg = NULL; 1995 char *response_arg = NULL;
1638 2061
1639 pex = pex_init (0, "collect2", NULL); 2062 pex = pex_init (0, "collect2", NULL);
1640 if (pex == NULL) 2063 if (pex == NULL)
1641 fatal_perror ("pex_init failed"); 2064 fatal_perror ("pex_init failed");
1642 2065
1643 errmsg = pex_run (pex, PEX_LAST | PEX_SEARCH, argv[0], argv, outname, 2066 errmsg = pex_run (pex, flags, argv[0], argv, outname,
1644 errname, &err); 2067 errname, &err);
1645 if (errmsg != NULL) 2068 if (errmsg != NULL)
1646 { 2069 {
1647 if (err != 0) 2070 if (err != 0)
1648 { 2071 {
1662 static void 2085 static void
1663 fork_execute (const char *prog, char **argv) 2086 fork_execute (const char *prog, char **argv)
1664 { 2087 {
1665 struct pex_obj *pex; 2088 struct pex_obj *pex;
1666 2089
1667 pex = collect_execute (prog, argv, NULL, NULL); 2090 pex = collect_execute (prog, argv, NULL, NULL, PEX_LAST | PEX_SEARCH);
1668 do_wait (prog, pex); 2091 do_wait (prog, pex);
1669 } 2092 }
1670 2093
1671 /* Unlink a file unless we are debugging. */ 2094 /* Unlink a file unless we are debugging. */
1672 2095
1675 { 2098 {
1676 if (!debug) 2099 if (!debug)
1677 unlink_if_ordinary (file); 2100 unlink_if_ordinary (file);
1678 else 2101 else
1679 notice ("[Leaving %s]\n", file); 2102 notice ("[Leaving %s]\n", file);
2103 }
2104
2105 /* Call maybe_unlink on the NULL-terminated list, FILE_LIST. */
2106
2107 static void
2108 maybe_unlink_list (char **file_list)
2109 {
2110 char **tmp = file_list;
2111
2112 while (*tmp)
2113 maybe_unlink (*(tmp++));
1680 } 2114 }
1681 2115
1682 2116
1683 static long sequence_number = 0; 2117 static long sequence_number = 0;
1684 2118
2071 } 2505 }
2072 #endif 2506 #endif
2073 2507
2074 #ifdef OBJECT_FORMAT_NONE 2508 #ifdef OBJECT_FORMAT_NONE
2075 2509
2510 /* Check to make sure the file is an ELF file. LTO objects must
2511 be in ELF format. */
2512
2513 static bool
2514 is_elf (const char *prog_name)
2515 {
2516 FILE *f;
2517 char buf[4];
2518 static char magic[4] = { 0x7f, 'E', 'L', 'F' };
2519
2520 f = fopen (prog_name, "r");
2521 if (f == NULL)
2522 return false;
2523 if (fread (buf, sizeof (buf), 1, f) != 1)
2524 buf[0] = 0;
2525 fclose (f);
2526 return memcmp (buf, magic, sizeof (magic)) == 0;
2527 }
2528
2076 /* Generic version to scan the name list of the loaded program for 2529 /* Generic version to scan the name list of the loaded program for
2077 the symbols g++ uses for static constructors and destructors. 2530 the symbols g++ uses for static constructors and destructors. */
2078
2079 The constructor table begins at __CTOR_LIST__ and contains a count
2080 of the number of pointers (or -1 if the constructors are built in a
2081 separate section by the linker), followed by the pointers to the
2082 constructor functions, terminated with a null pointer. The
2083 destructor table has the same format, and begins at __DTOR_LIST__. */
2084 2531
2085 static void 2532 static void
2086 scan_prog_file (const char *prog_name, enum pass which_pass) 2533 scan_prog_file (const char *prog_name, scanpass which_pass,
2534 scanfilter filter)
2087 { 2535 {
2088 void (*int_handler) (int); 2536 void (*int_handler) (int);
2089 #ifdef SIGQUIT 2537 #ifdef SIGQUIT
2090 void (*quit_handler) (int); 2538 void (*quit_handler) (int);
2091 #endif 2539 #endif
2092 char *real_nm_argv[4]; 2540 char *real_nm_argv[4];
2093 const char **nm_argv = (const char **) real_nm_argv; 2541 const char **nm_argv = CONST_CAST2 (const char **, char**, real_nm_argv);
2094 int argc = 0; 2542 int argc = 0;
2095 struct pex_obj *pex; 2543 struct pex_obj *pex;
2096 const char *errmsg; 2544 const char *errmsg;
2097 int err; 2545 int err;
2098 char *p, buf[1024]; 2546 char *p, buf[1024];
2099 FILE *inf; 2547 FILE *inf;
2548 int found_lto = 0;
2100 2549
2101 if (which_pass == PASS_SECOND) 2550 if (which_pass == PASS_SECOND)
2551 return;
2552
2553 /* LTO objects must be in ELF format. This check prevents
2554 us from accepting an archive containing LTO objects, which
2555 gcc cannnot currently handle. */
2556 if (which_pass == PASS_LTOINFO && !is_elf (prog_name))
2102 return; 2557 return;
2103 2558
2104 /* If we do not have an `nm', complain. */ 2559 /* If we do not have an `nm', complain. */
2105 if (nm_file_name == 0) 2560 if (nm_file_name == 0)
2106 fatal ("cannot find 'nm'"); 2561 fatal ("cannot find 'nm'");
2129 2584
2130 pex = pex_init (PEX_USE_PIPES, "collect2", NULL); 2585 pex = pex_init (PEX_USE_PIPES, "collect2", NULL);
2131 if (pex == NULL) 2586 if (pex == NULL)
2132 fatal_perror ("pex_init failed"); 2587 fatal_perror ("pex_init failed");
2133 2588
2134 errmsg = pex_run (pex, 0, nm_file_name, real_nm_argv, NULL, NULL, &err); 2589 errmsg = pex_run (pex, 0, nm_file_name, real_nm_argv, NULL, HOST_BIT_BUCKET,
2590 &err);
2135 if (errmsg != NULL) 2591 if (errmsg != NULL)
2136 { 2592 {
2137 if (err != 0) 2593 if (err != 0)
2138 { 2594 {
2139 errno = err; 2595 errno = err;
2151 inf = pex_read_output (pex, 0); 2607 inf = pex_read_output (pex, 0);
2152 if (inf == NULL) 2608 if (inf == NULL)
2153 fatal_perror ("can't open nm output"); 2609 fatal_perror ("can't open nm output");
2154 2610
2155 if (debug) 2611 if (debug)
2156 fprintf (stderr, "\nnm output with constructors/destructors.\n"); 2612 {
2613 if (which_pass == PASS_LTOINFO)
2614 fprintf (stderr, "\nnm output with LTO info marker symbol.\n");
2615 else
2616 fprintf (stderr, "\nnm output with constructors/destructors.\n");
2617 }
2157 2618
2158 /* Read each line of nm output. */ 2619 /* Read each line of nm output. */
2159 while (fgets (buf, sizeof buf, inf) != (char *) 0) 2620 while (fgets (buf, sizeof buf, inf) != (char *) 0)
2160 { 2621 {
2161 int ch, ch2; 2622 int ch, ch2;
2162 char *name, *end; 2623 char *name, *end;
2163 2624
2625 if (debug)
2626 fprintf (stderr, "\t%s\n", buf);
2627
2628 if (which_pass == PASS_LTOINFO)
2629 {
2630 if (found_lto)
2631 continue;
2632
2633 /* Look for the LTO info marker symbol, and add filename to
2634 the LTO objects list if found. */
2635 for (p = buf; (ch = *p) != '\0' && ch != '\n'; p++)
2636 if (ch == ' '
2637 && (strncmp (p +1 , "gnu_lto_v1", 10) == 0)
2638 && ISSPACE( p[11]))
2639 {
2640 add_lto_object (&lto_objects, prog_name);
2641
2642 /* We need to read all the input, so we can't just
2643 return here. But we can avoid useless work. */
2644 found_lto = 1;
2645
2646 break;
2647 }
2648
2649 continue;
2650 }
2651
2164 /* If it contains a constructor or destructor name, add the name 2652 /* If it contains a constructor or destructor name, add the name
2165 to the appropriate list. */ 2653 to the appropriate list unless this is a kind of symbol we're
2654 not supposed to even consider. */
2166 2655
2167 for (p = buf; (ch = *p) != '\0' && ch != '\n' && ch != '_'; p++) 2656 for (p = buf; (ch = *p) != '\0' && ch != '\n' && ch != '_'; p++)
2168 if (ch == ' ' && p[1] == 'U' && p[2] == ' ') 2657 if (ch == ' ' && p[1] == 'U' && p[2] == ' ')
2169 break; 2658 break;
2170 2659
2181 2670
2182 *end = '\0'; 2671 *end = '\0';
2183 switch (is_ctor_dtor (name)) 2672 switch (is_ctor_dtor (name))
2184 { 2673 {
2185 case SYM_CTOR: 2674 case SYM_CTOR:
2675 if (! (filter & SCAN_CTOR))
2676 break;
2186 if (which_pass != PASS_LIB) 2677 if (which_pass != PASS_LIB)
2187 add_to_list (&constructors, name); 2678 add_to_list (&constructors, name);
2188 break; 2679 break;
2189 2680
2190 case SYM_DTOR: 2681 case SYM_DTOR:
2682 if (! (filter & SCAN_DTOR))
2683 break;
2191 if (which_pass != PASS_LIB) 2684 if (which_pass != PASS_LIB)
2192 add_to_list (&destructors, name); 2685 add_to_list (&destructors, name);
2193 break; 2686 break;
2194 2687
2195 case SYM_INIT: 2688 case SYM_INIT:
2689 if (! (filter & SCAN_INIT))
2690 break;
2196 if (which_pass != PASS_LIB) 2691 if (which_pass != PASS_LIB)
2197 fatal ("init function found in object %s", prog_name); 2692 fatal ("init function found in object %s", prog_name);
2198 #ifndef LD_INIT_SWITCH 2693 #ifndef LD_INIT_SWITCH
2199 add_to_list (&constructors, name); 2694 add_to_list (&constructors, name);
2200 #endif 2695 #endif
2201 break; 2696 break;
2202 2697
2203 case SYM_FINI: 2698 case SYM_FINI:
2699 if (! (filter & SCAN_FINI))
2700 break;
2204 if (which_pass != PASS_LIB) 2701 if (which_pass != PASS_LIB)
2205 fatal ("fini function found in object %s", prog_name); 2702 fatal ("fini function found in object %s", prog_name);
2206 #ifndef LD_FINI_SWITCH 2703 #ifndef LD_FINI_SWITCH
2207 add_to_list (&destructors, name); 2704 add_to_list (&destructors, name);
2208 #endif 2705 #endif
2209 break; 2706 break;
2210 2707
2211 case SYM_DWEH: 2708 case SYM_DWEH:
2709 if (! (filter & SCAN_DWEH))
2710 break;
2212 if (which_pass != PASS_LIB) 2711 if (which_pass != PASS_LIB)
2213 add_to_list (&frame_tables, name); 2712 add_to_list (&frame_tables, name);
2214 break; 2713 break;
2215 2714
2216 default: /* not a constructor or destructor */ 2715 default: /* not a constructor or destructor */
2217 continue; 2716 continue;
2218 } 2717 }
2219
2220 if (debug)
2221 fprintf (stderr, "\t%s\n", buf);
2222 } 2718 }
2223 2719
2224 if (debug) 2720 if (debug)
2225 fprintf (stderr, "\n"); 2721 fprintf (stderr, "\n");
2226 2722
2246 void (*int_handler) (int); 2742 void (*int_handler) (int);
2247 #ifdef SIGQUIT 2743 #ifdef SIGQUIT
2248 void (*quit_handler) (int); 2744 void (*quit_handler) (int);
2249 #endif 2745 #endif
2250 char *real_ldd_argv[4]; 2746 char *real_ldd_argv[4];
2251 const char **ldd_argv = (const char **) real_ldd_argv; 2747 const char **ldd_argv = CONST_CAST2 (const char **, char **, real_ldd_argv);
2252 int argc = 0; 2748 int argc = 0;
2253 struct pex_obj *pex; 2749 struct pex_obj *pex;
2254 const char *errmsg; 2750 const char *errmsg;
2255 int err; 2751 int err;
2256 char buf[1024]; 2752 char buf[1024];
2402 # endif 2898 # endif
2403 # define GCC_SYMINC(X) ((X).n_numaux+1) 2899 # define GCC_SYMINC(X) ((X).n_numaux+1)
2404 # define GCC_SYMZERO(X) 0 2900 # define GCC_SYMZERO(X) 0
2405 2901
2406 /* 0757 = U803XTOCMAGIC (AIX 4.3) and 0767 = U64_TOCMAGIC (AIX V5) */ 2902 /* 0757 = U803XTOCMAGIC (AIX 4.3) and 0767 = U64_TOCMAGIC (AIX V5) */
2407 #ifdef _AIX51 2903 #if TARGET_AIX_VERSION >= 51
2408 # define GCC_CHECK_HDR(X) \ 2904 # define GCC_CHECK_HDR(X) \
2409 ((HEADER (X).f_magic == U802TOCMAGIC && ! aix64_flag) \ 2905 ((HEADER (X).f_magic == U802TOCMAGIC && ! aix64_flag) \
2410 || (HEADER (X).f_magic == 0767 && aix64_flag)) 2906 || (HEADER (X).f_magic == 0767 && aix64_flag))
2411 #else 2907 #else
2412 # define GCC_CHECK_HDR(X) \ 2908 # define GCC_CHECK_HDR(X) \
2441 if this name matches the location of a standard AIX library. */ 2937 if this name matches the location of a standard AIX library. */
2442 static int ignore_library (const char *); 2938 static int ignore_library (const char *);
2443 static int 2939 static int
2444 ignore_library (const char *name) 2940 ignore_library (const char *name)
2445 { 2941 {
2446 const char *const *p = &aix_std_libs[0]; 2942 const char *const *p;
2447 while (*p++ != NULL) 2943 size_t length;
2448 if (! strcmp (name, *p)) return 1; 2944
2945 if (target_system_root[0] != '\0')
2946 {
2947 length = strlen (target_system_root);
2948 if (strncmp (name, target_system_root, length) != 0)
2949 return 0;
2950 name += length;
2951 }
2952 for (p = &aix_std_libs[0]; *p != NULL; ++p)
2953 if (strcmp (name, *p) == 0)
2954 return 1;
2449 return 0; 2955 return 0;
2450 } 2956 }
2451 #endif /* COLLECT_EXPORT_LIST */ 2957 #endif /* COLLECT_EXPORT_LIST */
2452 2958
2453 #if defined (HAVE_DECL_LDGETNAME) && !HAVE_DECL_LDGETNAME 2959 #if defined (HAVE_DECL_LDGETNAME) && !HAVE_DECL_LDGETNAME
2454 extern char *ldgetname (LDFILE *, GCC_SYMENT *); 2960 extern char *ldgetname (LDFILE *, GCC_SYMENT *);
2455 #endif 2961 #endif
2456 2962
2457 /* COFF version to scan the name list of the loaded program for 2963 /* COFF version to scan the name list of the loaded program for
2458 the symbols g++ uses for static constructors and destructors. 2964 the symbols g++ uses for static constructors and destructors. */
2459
2460 The constructor table begins at __CTOR_LIST__ and contains a count
2461 of the number of pointers (or -1 if the constructors are built in a
2462 separate section by the linker), followed by the pointers to the
2463 constructor functions, terminated with a null pointer. The
2464 destructor table has the same format, and begins at __DTOR_LIST__. */
2465 2965
2466 static void 2966 static void
2467 scan_prog_file (const char *prog_name, enum pass which_pass) 2967 scan_prog_file (const char *prog_name, scanpass which_pass,
2968 scanfilter filter)
2468 { 2969 {
2469 LDFILE *ldptr = NULL; 2970 LDFILE *ldptr = NULL;
2470 int sym_index, sym_count; 2971 int sym_index, sym_count;
2471 int is_shared = 0; 2972 int is_shared = 0;
2472 2973
2526 #endif 3027 #endif
2527 3028
2528 switch (is_ctor_dtor (name)) 3029 switch (is_ctor_dtor (name))
2529 { 3030 {
2530 case SYM_CTOR: 3031 case SYM_CTOR:
3032 if (! (filter & SCAN_CTOR))
3033 break;
2531 if (! is_shared) 3034 if (! is_shared)
2532 add_to_list (&constructors, name); 3035 add_to_list (&constructors, name);
2533 #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH) 3036 #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
2534 if (which_pass == PASS_OBJ) 3037 if (which_pass == PASS_OBJ)
2535 add_to_list (&exports, name); 3038 add_to_list (&exports, name);
2536 #endif 3039 #endif
2537 break; 3040 break;
2538 3041
2539 case SYM_DTOR: 3042 case SYM_DTOR:
3043 if (! (filter & SCAN_DTOR))
3044 break;
2540 if (! is_shared) 3045 if (! is_shared)
2541 add_to_list (&destructors, name); 3046 add_to_list (&destructors, name);
2542 #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH) 3047 #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
2543 if (which_pass == PASS_OBJ) 3048 if (which_pass == PASS_OBJ)
2544 add_to_list (&exports, name); 3049 add_to_list (&exports, name);
2545 #endif 3050 #endif
2546 break; 3051 break;
2547 3052
2548 #ifdef COLLECT_EXPORT_LIST 3053 #ifdef COLLECT_EXPORT_LIST
2549 case SYM_INIT: 3054 case SYM_INIT:
3055 if (! (filter & SCAN_INIT))
3056 break;
2550 #ifndef LD_INIT_SWITCH 3057 #ifndef LD_INIT_SWITCH
2551 if (is_shared) 3058 if (is_shared)
2552 add_to_list (&constructors, name); 3059 add_to_list (&constructors, name);
2553 #endif 3060 #endif
2554 break; 3061 break;
2555 3062
2556 case SYM_FINI: 3063 case SYM_FINI:
3064 if (! (filter & SCAN_FINI))
3065 break;
2557 #ifndef LD_INIT_SWITCH 3066 #ifndef LD_INIT_SWITCH
2558 if (is_shared) 3067 if (is_shared)
2559 add_to_list (&destructors, name); 3068 add_to_list (&destructors, name);
2560 #endif 3069 #endif
2561 break; 3070 break;
2562 #endif 3071 #endif
2563 3072
2564 case SYM_DWEH: 3073 case SYM_DWEH:
3074 if (! (filter & SCAN_DWEH))
3075 break;
2565 if (! is_shared) 3076 if (! is_shared)
2566 add_to_list (&frame_tables, name); 3077 add_to_list (&frame_tables, name);
2567 #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH) 3078 #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
2568 if (which_pass == PASS_OBJ) 3079 if (which_pass == PASS_OBJ)
2569 add_to_list (&exports, name); 3080 add_to_list (&exports, name);