comparison gcc/collect2.c @ 145:1830386684a0

gcc-9.2.0
author anatofuz
date Thu, 13 Feb 2020 11:34:05 +0900
parents 84e7813d76e9
children
comparison
equal deleted inserted replaced
131:84e7813d76e9 145:1830386684a0
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-2018 Free Software Foundation, Inc. 3 Copyright (C) 1992-2020 Free Software Foundation, Inc.
4 Contributed by Chris Smith (csmith@convex.com). 4 Contributed by Chris Smith (csmith@convex.com).
5 Heavily modified by Michael Meissner (meissner@cygnus.com), 5 Heavily modified by Michael Meissner (meissner@cygnus.com),
6 Per Bothner (bothner@cygnus.com), and John Gilmore (gnu@cygnus.com). 6 Per Bothner (bothner@cygnus.com), and John Gilmore (gnu@cygnus.com).
7 7
8 This file is part of GCC. 8 This file is part of GCC.
28 #include "system.h" 28 #include "system.h"
29 #include "coretypes.h" 29 #include "coretypes.h"
30 #include "tm.h" 30 #include "tm.h"
31 #include "filenames.h" 31 #include "filenames.h"
32 #include "file-find.h" 32 #include "file-find.h"
33 #include "simple-object.h"
34 #include "lto-section-names.h"
33 35
34 /* TARGET_64BIT may be defined to use driver specific functionality. */ 36 /* TARGET_64BIT may be defined to use driver specific functionality. */
35 #undef TARGET_64BIT 37 #undef TARGET_64BIT
36 #define TARGET_64BIT TARGET_64BIT_DEFAULT 38 #define TARGET_64BIT TARGET_64BIT_DEFAULT
37 39
201 bool helpflag; /* true if --help */ 203 bool helpflag; /* true if --help */
202 204
203 static int shared_obj; /* true if -shared */ 205 static int shared_obj; /* true if -shared */
204 static int static_obj; /* true if -static */ 206 static int static_obj; /* true if -static */
205 207
206 static const char *c_file; /* <xxx>.c for constructor/destructor list. */ 208 static char *c_file; /* <xxx>.c for constructor/destructor list. */
207 static const char *o_file; /* <xxx>.o for constructor/destructor list. */ 209 static char *o_file; /* <xxx>.o for constructor/destructor list. */
208 #ifdef COLLECT_EXPORT_LIST 210 #ifdef COLLECT_EXPORT_LIST
209 static const char *export_file; /* <xxx>.x for AIX export list. */ 211 static const char *export_file; /* <xxx>.x for AIX export list. */
210 #endif 212 #endif
211 static char **lto_o_files; /* Output files for LTO. */ 213 static char **lto_o_files; /* Output files for LTO. */
212 const char *ldout; /* File for ld stdout. */ 214 const char *ldout; /* File for ld stdout. */
612 /* Search for NAME using prefix list PPREFIX. We only look for executable 614 /* Search for NAME using prefix list PPREFIX. We only look for executable
613 files. 615 files.
614 616
615 Return 0 if not found, otherwise return its name, allocated with malloc. */ 617 Return 0 if not found, otherwise return its name, allocated with malloc. */
616 618
617 #if defined (OBJECT_FORMAT_NONE) || defined (OBJECT_FORMAT_COFF) 619 #ifdef OBJECT_FORMAT_NONE
618 620
619 /* Add an entry for the object file NAME to object file list LIST. 621 /* Add an entry for the object file NAME to object file list LIST.
620 New entries are added at the end of the list. The original pointer 622 New entries are added at the end of the list. The original pointer
621 value of NAME is preserved, i.e., no string copy is performed. */ 623 value of NAME is preserved, i.e., no string copy is performed. */
622 624
632 else 634 else
633 list->first = n; 635 list->first = n;
634 636
635 list->last = n; 637 list->last = n;
636 } 638 }
637 #endif 639 #endif /* OBJECT_FORMAT_NONE */
638 640
639 641
640 /* Perform a link-time recompilation and relink if any of the object 642 /* Perform a link-time recompilation and relink if any of the object
641 files contain LTO info. The linker command line LTO_LD_ARGV 643 files contain LTO info. The linker command line LTO_LD_ARGV
642 represents the linker command that would produce a final executable 644 represents the linker command that would produce a final executable
697 char **out_lto_ld_argv; 699 char **out_lto_ld_argv;
698 int out_lto_ld_argv_size; 700 int out_lto_ld_argv_size;
699 size_t num_files; 701 size_t num_files;
700 702
701 if (!lto_wrapper) 703 if (!lto_wrapper)
702 fatal_error (input_location, "COLLECT_LTO_WRAPPER must be set"); 704 fatal_error (input_location, "environment variable "
705 "%<COLLECT_LTO_WRAPPER%> must be set");
703 706
704 num_lto_c_args++; 707 num_lto_c_args++;
705 708
706 /* There is at least one object file containing LTO info, 709 /* There is at least one object file containing LTO info,
707 so we need to run the LTO back end and relink. 710 so we need to run the LTO back end and relink.
802 out_lto_ld_argv[out_lto_ld_argv_size++] = 0; 805 out_lto_ld_argv[out_lto_ld_argv_size++] = 0;
803 806
804 /* Run the linker again, this time replacing the object files 807 /* Run the linker again, this time replacing the object files
805 optimized by the LTO with the temporary file generated by the LTO. */ 808 optimized by the LTO with the temporary file generated by the LTO. */
806 fork_execute ("ld", out_lto_ld_argv, HAVE_GNU_LD && at_file_supplied); 809 fork_execute ("ld", out_lto_ld_argv, HAVE_GNU_LD && at_file_supplied);
807 post_ld_pass (true); 810 /* We assume that temp files were created, and therefore we need to take
811 that into account (maybe run dsymutil). */
812 post_ld_pass (/*temp_file*/true);
808 free (lto_ld_argv); 813 free (lto_ld_argv);
809 814
810 maybe_unlink_list (lto_o_files); 815 maybe_unlink_list (lto_o_files);
811 } 816 }
812 else if (force) 817 else if (force)
813 { 818 {
814 /* Our caller is relying on us to do the link 819 /* Our caller is relying on us to do the link
815 even though there is no LTO back end work to be done. */ 820 even though there is no LTO back end work to be done. */
816 fork_execute ("ld", lto_ld_argv, HAVE_GNU_LD && at_file_supplied); 821 fork_execute ("ld", lto_ld_argv, HAVE_GNU_LD && at_file_supplied);
817 post_ld_pass (false); 822 /* No LTO objects were found, so no new temp file. */
823 post_ld_pass (/*temp_file*/false);
818 } 824 }
819 else 825 else
820 post_ld_pass (true); 826 post_ld_pass (false); /* No LTO objects were found, no temp file. */
827 }
828 /* Entry point for linker invoation. Called from main in collect2.c.
829 LD_ARGV is an array of arguments for the linker. */
830
831 static void
832 do_link (char **ld_argv)
833 {
834 struct pex_obj *pex;
835 const char *prog = "ld";
836 pex = collect_execute (prog, ld_argv, NULL, NULL,
837 PEX_LAST | PEX_SEARCH,
838 HAVE_GNU_LD && at_file_supplied);
839 int ret = collect_wait (prog, pex);
840 if (ret)
841 {
842 error ("ld returned %d exit status", ret);
843 exit (ret);
844 }
845 else
846 {
847 /* We have just successfully produced an output file, so assume that we
848 may unlink it if need be for now on. */
849 may_unlink_output_file = true;
850 }
821 } 851 }
822 852
823 /* Main program. */ 853 /* Main program. */
824 854
825 int 855 int
829 { 859 {
830 USE_DEFAULT_LD, 860 USE_DEFAULT_LD,
831 USE_PLUGIN_LD, 861 USE_PLUGIN_LD,
832 USE_GOLD_LD, 862 USE_GOLD_LD,
833 USE_BFD_LD, 863 USE_BFD_LD,
864 USE_LLD_LD,
834 USE_LD_MAX 865 USE_LD_MAX
835 } selected_linker = USE_DEFAULT_LD; 866 } selected_linker = USE_DEFAULT_LD;
836 static const char *const ld_suffixes[USE_LD_MAX] = 867 static const char *const ld_suffixes[USE_LD_MAX] =
837 { 868 {
838 "ld", 869 "ld",
839 PLUGIN_LD_SUFFIX, 870 PLUGIN_LD_SUFFIX,
840 "ld.gold", 871 "ld.gold",
841 "ld.bfd" 872 "ld.bfd",
873 "ld.lld"
842 }; 874 };
843 static const char *const real_ld_suffix = "real-ld"; 875 static const char *const real_ld_suffix = "real-ld";
844 static const char *const collect_ld_suffix = "collect-ld"; 876 static const char *const collect_ld_suffix = "collect-ld";
845 static const char *const nm_suffix = "nm"; 877 static const char *const nm_suffix = "nm";
846 static const char *const gnm_suffix = "gnm"; 878 static const char *const gnm_suffix = "gnm";
977 ld2 = CONST_CAST2 (const char **, char **, ld2_argv); 1009 ld2 = CONST_CAST2 (const char **, char **, ld2_argv);
978 object_lst = XCNEWVEC (char *, argc); 1010 object_lst = XCNEWVEC (char *, argc);
979 object = CONST_CAST2 (const char **, char **, object_lst); 1011 object = CONST_CAST2 (const char **, char **, object_lst);
980 1012
981 #ifdef DEBUG 1013 #ifdef DEBUG
982 debug = 1; 1014 debug = true;
983 #endif 1015 #endif
984 1016
985 /* Parse command line early for instances of -debug. This allows 1017 save_temps = false;
986 the debug flag to be set before functions like find_a_file() 1018 verbose = false;
987 are called. We also look for the -flto or -flto-partition=none flag to know 1019
988 what LTO mode we are in. */ 1020 #ifndef DEFAULT_A_OUT_NAME
1021 output_file = "a.out";
1022 #else
1023 output_file = DEFAULT_A_OUT_NAME;
1024 #endif
1025
1026 /* Parse command line / environment for flags we want early.
1027 This allows the debug flag to be set before functions like find_a_file()
1028 are called. */
989 { 1029 {
990 bool no_partition = false; 1030 bool no_partition = false;
991 1031
992 for (i = 1; argv[i] != NULL; i ++) 1032 for (i = 1; argv[i] != NULL; i ++)
993 { 1033 {
994 if (! strcmp (argv[i], "-debug")) 1034 if (! strcmp (argv[i], "-debug"))
995 debug = true; 1035 debug = true;
996 else if (! strcmp (argv[i], "-flto-partition=none"))
997 no_partition = true;
998 else if (!strncmp (argv[i], "-fno-lto", 8)) 1036 else if (!strncmp (argv[i], "-fno-lto", 8))
999 lto_mode = LTO_MODE_NONE; 1037 lto_mode = LTO_MODE_NONE;
1000 else if (! strcmp (argv[i], "-plugin")) 1038 else if (! strcmp (argv[i], "-plugin"))
1001 { 1039 {
1002 use_plugin = true; 1040 use_plugin = true;
1005 } 1043 }
1006 else if (strcmp (argv[i], "-fuse-ld=bfd") == 0) 1044 else if (strcmp (argv[i], "-fuse-ld=bfd") == 0)
1007 selected_linker = USE_BFD_LD; 1045 selected_linker = USE_BFD_LD;
1008 else if (strcmp (argv[i], "-fuse-ld=gold") == 0) 1046 else if (strcmp (argv[i], "-fuse-ld=gold") == 0)
1009 selected_linker = USE_GOLD_LD; 1047 selected_linker = USE_GOLD_LD;
1048 else if (strcmp (argv[i], "-fuse-ld=lld") == 0)
1049 selected_linker = USE_LLD_LD;
1050 else if (strncmp (argv[i], "-o", 2) == 0)
1051 {
1052 /* Parse the output filename if it's given so that we can make
1053 meaningful temp filenames. */
1054 if (argv[i][2] == '\0')
1055 output_file = argv[i+1];
1056 else
1057 output_file = &argv[i][2];
1058 }
1010 1059
1011 #ifdef COLLECT_EXPORT_LIST 1060 #ifdef COLLECT_EXPORT_LIST
1012 /* These flags are position independent, although their order 1061 /* These flags are position independent, although their order
1013 is important - subsequent flags override earlier ones. */ 1062 is important - subsequent flags override earlier ones. */
1014 else if (strcmp (argv[i], "-b64") == 0) 1063 else if (strcmp (argv[i], "-b64") == 0)
1025 aixrtl_flag = 0; 1074 aixrtl_flag = 0;
1026 else if (strcmp (argv[i], "-blazy") == 0) 1075 else if (strcmp (argv[i], "-blazy") == 0)
1027 aixlazy_flag = 1; 1076 aixlazy_flag = 1;
1028 #endif 1077 #endif
1029 } 1078 }
1030 verbose = debug; 1079
1080 obstack_begin (&temporary_obstack, 0);
1081 temporary_firstobj = (char *) obstack_alloc (&temporary_obstack, 0);
1082
1083 #ifndef HAVE_LD_DEMANGLE
1084 current_demangling_style = auto_demangling;
1085 #endif
1086
1087 /* Now pick up any flags we want early from COLLECT_GCC_OPTIONS
1088 The LTO options are passed here as are other options that might
1089 be unsuitable for ld (e.g. -save-temps). */
1090 p = getenv ("COLLECT_GCC_OPTIONS");
1091 while (p && *p)
1092 {
1093 const char *q = extract_string (&p);
1094 if (*q == '-' && (q[1] == 'm' || q[1] == 'f'))
1095 num_c_args++;
1096 if (strncmp (q, "-flto-partition=none", 20) == 0)
1097 no_partition = true;
1098 else if (strncmp (q, "-fno-lto", 8) == 0)
1099 lto_mode = LTO_MODE_NONE;
1100 else if (strncmp (q, "-save-temps", 11) == 0)
1101 /* FIXME: Honour =obj. */
1102 save_temps = true;
1103 }
1104 obstack_free (&temporary_obstack, temporary_firstobj);
1105
1106 verbose = verbose || debug;
1107 save_temps = save_temps || debug;
1031 find_file_set_debug (debug); 1108 find_file_set_debug (debug);
1032 if (use_plugin) 1109 if (use_plugin)
1033 lto_mode = LTO_MODE_NONE; 1110 lto_mode = LTO_MODE_NONE;
1034 if (no_partition && lto_mode == LTO_MODE_WHOPR) 1111 if (no_partition && lto_mode == LTO_MODE_WHOPR)
1035 lto_mode = LTO_MODE_LTO; 1112 lto_mode = LTO_MODE_LTO;
1036 } 1113 }
1037 1114
1038 #ifndef DEFAULT_A_OUT_NAME
1039 output_file = "a.out";
1040 #else
1041 output_file = DEFAULT_A_OUT_NAME;
1042 #endif
1043
1044 obstack_begin (&temporary_obstack, 0);
1045 temporary_firstobj = (char *) obstack_alloc (&temporary_obstack, 0);
1046
1047 #ifndef HAVE_LD_DEMANGLE
1048 current_demangling_style = auto_demangling;
1049 #endif
1050 p = getenv ("COLLECT_GCC_OPTIONS");
1051 while (p && *p)
1052 {
1053 const char *q = extract_string (&p);
1054 if (*q == '-' && (q[1] == 'm' || q[1] == 'f'))
1055 num_c_args++;
1056 }
1057 obstack_free (&temporary_obstack, temporary_firstobj);
1058
1059 /* -fno-profile-arcs -fno-test-coverage -fno-branch-probabilities 1115 /* -fno-profile-arcs -fno-test-coverage -fno-branch-probabilities
1060 -fno-exceptions -w -fno-whole-program */ 1116 -fno-exceptions -w -fno-whole-program */
1061 num_c_args += 6; 1117 num_c_args += 6;
1062 1118
1063 c_argv = XCNEWVEC (char *, num_c_args); 1119 c_argv = XCNEWVEC (char *, num_c_args);
1094 /* Try to discover a valid linker/nm/strip to use. */ 1150 /* Try to discover a valid linker/nm/strip to use. */
1095 1151
1096 /* Maybe we know the right file to use (if not cross). */ 1152 /* Maybe we know the right file to use (if not cross). */
1097 ld_file_name = 0; 1153 ld_file_name = 0;
1098 #ifdef DEFAULT_LINKER 1154 #ifdef DEFAULT_LINKER
1099 if (selected_linker == USE_BFD_LD || selected_linker == USE_GOLD_LD) 1155 if (selected_linker == USE_BFD_LD || selected_linker == USE_GOLD_LD ||
1156 selected_linker == USE_LLD_LD)
1100 { 1157 {
1101 char *linker_name; 1158 char *linker_name;
1102 # ifdef HOST_EXECUTABLE_SUFFIX 1159 # ifdef HOST_EXECUTABLE_SUFFIX
1103 int len = (sizeof (DEFAULT_LINKER) 1160 int len = (sizeof (DEFAULT_LINKER)
1104 - sizeof (HOST_EXECUTABLE_SUFFIX)); 1161 - sizeof (HOST_EXECUTABLE_SUFFIX));
1201 c_file_name = p; 1258 c_file_name = p;
1202 1259
1203 *ld1++ = *ld2++ = ld_file_name; 1260 *ld1++ = *ld2++ = ld_file_name;
1204 1261
1205 /* Make temp file names. */ 1262 /* Make temp file names. */
1206 c_file = make_temp_file (".c"); 1263 if (save_temps)
1207 o_file = make_temp_file (".o"); 1264 {
1265 c_file = (char *) xmalloc (strlen (output_file)
1266 + sizeof (".cdtor.c") + 1);
1267 strcpy (c_file, output_file);
1268 strcat (c_file, ".cdtor.c");
1269 o_file = (char *) xmalloc (strlen (output_file)
1270 + sizeof (".cdtor.o") + 1);
1271 strcpy (o_file, output_file);
1272 strcat (o_file, ".cdtor.o");
1273 }
1274 else
1275 {
1276 c_file = make_temp_file (".cdtor.c");
1277 o_file = make_temp_file (".cdtor.o");
1278 }
1208 #ifdef COLLECT_EXPORT_LIST 1279 #ifdef COLLECT_EXPORT_LIST
1209 export_file = make_temp_file (".x"); 1280 export_file = make_temp_file (".x");
1210 #endif 1281 #endif
1211 if (!debug) 1282 if (!debug)
1212 { 1283 {
1213 ldout = make_temp_file (".ld"); 1284 ldout = make_temp_file (".ld");
1214 lderrout = make_temp_file (".le"); 1285 lderrout = make_temp_file (".le");
1215 } 1286 }
1287 /* Build the command line to compile the ctor/dtor list. */
1216 *c_ptr++ = c_file_name; 1288 *c_ptr++ = c_file_name;
1217 *c_ptr++ = "-x"; 1289 *c_ptr++ = "-x";
1218 *c_ptr++ = "c"; 1290 *c_ptr++ = "c";
1219 *c_ptr++ = "-c"; 1291 *c_ptr++ = "-c";
1220 *c_ptr++ = "-o"; 1292 *c_ptr++ = "-o";
1313 #endif 1385 #endif
1314 } 1386 }
1315 else if (!use_collect_ld 1387 else if (!use_collect_ld
1316 && strncmp (arg, "-fuse-ld=", 9) == 0) 1388 && strncmp (arg, "-fuse-ld=", 9) == 0)
1317 { 1389 {
1318 /* Do not pass -fuse-ld={bfd|gold} to the linker. */ 1390 /* Do not pass -fuse-ld={bfd|gold|lld} to the linker. */
1319 ld1--; 1391 ld1--;
1320 ld2--; 1392 ld2--;
1321 } 1393 }
1322 else if (strncmp (arg, "-fno-lto", 8) == 0) 1394 else if (strncmp (arg, "-fno-lto", 8) == 0)
1323 { 1395 {
1347 *ld1++ = *ld2++ = *argv; 1419 *ld1++ = *ld2++ = *argv;
1348 } 1420 }
1349 1421
1350 stream = fopen (list_filename, "r"); 1422 stream = fopen (list_filename, "r");
1351 if (stream == NULL) 1423 if (stream == NULL)
1352 fatal_error (input_location, "can't open %s: %m", 1424 fatal_error (input_location, "cannot open %s: %m",
1353 list_filename); 1425 list_filename);
1354 1426
1355 while (fgets (buf, sizeof buf, stream) != NULL) 1427 while (fgets (buf, sizeof buf, stream) != NULL)
1356 { 1428 {
1357 /* Remove end of line. */ 1429 /* Remove end of line. */
1467 if (arg[10] == '=') 1539 if (arg[10] == '=')
1468 { 1540 {
1469 enum demangling_styles style 1541 enum demangling_styles style
1470 = cplus_demangle_name_to_style (arg+11); 1542 = cplus_demangle_name_to_style (arg+11);
1471 if (style == unknown_demangling) 1543 if (style == unknown_demangling)
1472 error ("unknown demangling style '%s'", arg+11); 1544 error ("unknown demangling style %qs", arg+11);
1473 else 1545 else
1474 current_demangling_style = style; 1546 current_demangling_style = style;
1475 } 1547 }
1476 ld1--; 1548 ld1--;
1477 ld2--; 1549 ld2--;
1591 printf (" Options:\n"); 1663 printf (" Options:\n");
1592 printf (" -debug Enable debug output\n"); 1664 printf (" -debug Enable debug output\n");
1593 printf (" --help Display this information\n"); 1665 printf (" --help Display this information\n");
1594 printf (" -v, --version Display this program's version number\n"); 1666 printf (" -v, --version Display this program's version number\n");
1595 printf ("\n"); 1667 printf ("\n");
1596 printf ("Overview: http://gcc.gnu.org/onlinedocs/gccint/Collect2.html\n"); 1668 printf ("Overview: https://gcc.gnu.org/onlinedocs/gccint/Collect2.html\n");
1597 printf ("Report bugs: %s\n", bug_report_url); 1669 printf ("Report bugs: %s\n", bug_report_url);
1598 printf ("\n"); 1670 printf ("\n");
1599 } 1671 }
1600 1672
1601 if (debug) 1673 if (debug)
1654 objects and libraries has performed above. In the !shared_obj case, we 1726 objects and libraries has performed above. In the !shared_obj case, we
1655 expect the relevant tables to be dragged together with their associated 1727 expect the relevant tables to be dragged together with their associated
1656 functions from precise cross reference insertions by the compiler. */ 1728 functions from precise cross reference insertions by the compiler. */
1657 1729
1658 if (early_exit || ld1_filter != SCAN_NOTHING) 1730 if (early_exit || ld1_filter != SCAN_NOTHING)
1659 do_tlink (ld1_argv, object_lst); 1731 do_link (ld1_argv);
1660 1732
1661 if (early_exit) 1733 if (early_exit)
1662 { 1734 {
1663 #ifdef COLLECT_EXPORT_LIST 1735 #ifdef COLLECT_EXPORT_LIST
1664 /* Make sure we delete the export file we may have created. */ 1736 /* Make sure we delete the export file we may have created. */
1666 maybe_unlink (export_file); 1738 maybe_unlink (export_file);
1667 #endif 1739 #endif
1668 if (lto_mode != LTO_MODE_NONE) 1740 if (lto_mode != LTO_MODE_NONE)
1669 maybe_run_lto_and_relink (ld1_argv, object_lst, object, false); 1741 maybe_run_lto_and_relink (ld1_argv, object_lst, object, false);
1670 else 1742 else
1671 post_ld_pass (false); 1743 post_ld_pass (/*temp_file*/false);
1672 1744
1673 maybe_unlink (c_file);
1674 maybe_unlink (o_file);
1675 return 0; 1745 return 0;
1676 } 1746 }
1677 } 1747 }
1678 1748
1679 /* Unless we have done it all already, examine the namelist and search for 1749 /* Unless we have done it all already, examine the namelist and search for
1714 dependent programs when we add static objects. */ 1784 dependent programs when we add static objects. */
1715 && ! shared_obj 1785 && ! shared_obj
1716 #endif 1786 #endif
1717 ) 1787 )
1718 { 1788 {
1719 /* Do tlink without additional code generation now if we didn't 1789 /* Do link without additional code generation now if we didn't
1720 do it earlier for scanning purposes. */ 1790 do it earlier for scanning purposes. */
1721 if (ld1_filter == SCAN_NOTHING) 1791 if (ld1_filter == SCAN_NOTHING)
1722 do_tlink (ld1_argv, object_lst); 1792 do_link (ld1_argv);
1723 1793
1724 if (lto_mode) 1794 if (lto_mode)
1725 maybe_run_lto_and_relink (ld1_argv, object_lst, object, false); 1795 maybe_run_lto_and_relink (ld1_argv, object_lst, object, false);
1726 1796
1727 /* Strip now if it was requested on the command line. */ 1797 /* Strip now if it was requested on the command line. */
1738 } 1808 }
1739 1809
1740 #ifdef COLLECT_EXPORT_LIST 1810 #ifdef COLLECT_EXPORT_LIST
1741 maybe_unlink (export_file); 1811 maybe_unlink (export_file);
1742 #endif 1812 #endif
1743 post_ld_pass (false); 1813 post_ld_pass (/*temp_file*/false);
1744 1814
1745 maybe_unlink (c_file); 1815 maybe_unlink (c_file);
1746 maybe_unlink (o_file); 1816 maybe_unlink (o_file);
1747 return 0; 1817 return 0;
1748 } 1818 }
1820 /* Assemble the constructor and destructor tables. 1890 /* Assemble the constructor and destructor tables.
1821 Link the tables in with the rest of the program. */ 1891 Link the tables in with the rest of the program. */
1822 1892
1823 fork_execute ("gcc", c_argv, at_file_supplied); 1893 fork_execute ("gcc", c_argv, at_file_supplied);
1824 #ifdef COLLECT_EXPORT_LIST 1894 #ifdef COLLECT_EXPORT_LIST
1825 /* On AIX we must call tlink because of possible templates resolution. */ 1895 /* On AIX we must call link because of possible templates resolution. */
1826 do_tlink (ld2_argv, object_lst); 1896 do_link (ld2_argv);
1827 1897
1828 if (lto_mode) 1898 if (lto_mode)
1829 maybe_run_lto_and_relink (ld2_argv, object_lst, object, false); 1899 maybe_run_lto_and_relink (ld2_argv, object_lst, object, false);
1830 #else 1900 #else
1831 /* Otherwise, simply call ld because tlink is already done. */ 1901 /* Otherwise, simply call ld because link is already done. */
1832 if (lto_mode) 1902 if (lto_mode)
1833 maybe_run_lto_and_relink (ld2_argv, object_lst, object, true); 1903 maybe_run_lto_and_relink (ld2_argv, object_lst, object, true);
1834 else 1904 else
1835 { 1905 {
1836 fork_execute ("ld", ld2_argv, HAVE_GNU_LD && at_file_supplied); 1906 fork_execute ("ld", ld2_argv, HAVE_GNU_LD && at_file_supplied);
1837 post_ld_pass (false); 1907 post_ld_pass (/*temp_file*/false);
1838 } 1908 }
1839 1909
1840 /* Let scan_prog_file do any final mods (OSF/rose needs this for 1910 /* Let scan_prog_file do any final mods (OSF/rose needs this for
1841 constructors/destructors in shared libraries. */ 1911 constructors/destructors in shared libraries. */
1842 scan_prog_file (output_file, PASS_SECOND, SCAN_ALL); 1912 scan_prog_file (output_file, PASS_SECOND, SCAN_ALL);
1857 and we may not unlink it. */ 1927 and we may not unlink it. */
1858 1928
1859 void 1929 void
1860 maybe_unlink (const char *file) 1930 maybe_unlink (const char *file)
1861 { 1931 {
1862 if (debug) 1932 if (save_temps && file_exists (file))
1863 { 1933 {
1864 notice ("[Leaving %s]\n", file); 1934 if (verbose)
1935 notice ("[Leaving %s]\n", file);
1865 return; 1936 return;
1866 } 1937 }
1867 1938
1868 if (file == output_file && !may_unlink_output_file) 1939 if (file == output_file && !may_unlink_output_file)
1869 return; 1940 return;
2289 2360
2290 #ifdef OBJECT_FORMAT_NONE 2361 #ifdef OBJECT_FORMAT_NONE
2291 2362
2292 /* Check to make sure the file is an LTO object file. */ 2363 /* Check to make sure the file is an LTO object file. */
2293 2364
2365 static int
2366 has_lto_section (void *data, const char *name ATTRIBUTE_UNUSED,
2367 off_t offset ATTRIBUTE_UNUSED,
2368 off_t length ATTRIBUTE_UNUSED)
2369 {
2370 int *found = (int *) data;
2371
2372 if (strncmp (name, LTO_SECTION_NAME_PREFIX,
2373 sizeof (LTO_SECTION_NAME_PREFIX) - 1) != 0)
2374 {
2375 if (strncmp (name, OFFLOAD_SECTION_NAME_PREFIX,
2376 sizeof (OFFLOAD_SECTION_NAME_PREFIX) - 1) != 0)
2377 return 1;
2378 }
2379
2380 *found = 1;
2381
2382 /* Stop iteration. */
2383 return 0;
2384 }
2385
2294 static bool 2386 static bool
2295 maybe_lto_object_file (const char *prog_name) 2387 is_lto_object_file (const char *prog_name)
2296 { 2388 {
2297 FILE *f; 2389 const char *errmsg;
2298 unsigned char buf[4]; 2390 int err;
2299 int i; 2391 int found = 0;
2300 2392 off_t inoff = 0;
2301 static unsigned char elfmagic[4] = { 0x7f, 'E', 'L', 'F' }; 2393 int infd = open (prog_name, O_RDONLY | O_BINARY);
2302 static unsigned char coffmagic[2] = { 0x4c, 0x01 }; 2394
2303 static unsigned char coffmagic_x64[2] = { 0x64, 0x86 }; 2395 if (infd == -1)
2304 static unsigned char machomagic[4][4] = {
2305 { 0xcf, 0xfa, 0xed, 0xfe },
2306 { 0xce, 0xfa, 0xed, 0xfe },
2307 { 0xfe, 0xed, 0xfa, 0xcf },
2308 { 0xfe, 0xed, 0xfa, 0xce }
2309 };
2310
2311 f = fopen (prog_name, "rb");
2312 if (f == NULL)
2313 return false; 2396 return false;
2314 if (fread (buf, sizeof (buf), 1, f) != 1) 2397
2315 buf[0] = 0; 2398 simple_object_read *inobj = simple_object_start_read (infd, inoff,
2316 fclose (f); 2399 LTO_SEGMENT_NAME,
2317 2400 &errmsg, &err);
2318 if (memcmp (buf, elfmagic, sizeof (elfmagic)) == 0 2401 if (!inobj)
2319 || memcmp (buf, coffmagic, sizeof (coffmagic)) == 0 2402 return false;
2320 || memcmp (buf, coffmagic_x64, sizeof (coffmagic_x64)) == 0) 2403
2404 errmsg = simple_object_find_sections (inobj, has_lto_section,
2405 (void *) &found, &err);
2406 if (! errmsg && found)
2321 return true; 2407 return true;
2322 for (i = 0; i < 4; i++) 2408
2323 if (memcmp (buf, machomagic[i], sizeof (machomagic[i])) == 0) 2409 if (errmsg)
2324 return true; 2410 fatal_error (0, "%s: %s", errmsg, xstrerror (err));
2325
2326 return false; 2411 return false;
2327 } 2412 }
2328 2413
2329 /* Generic version to scan the name list of the loaded program for 2414 /* Generic version to scan the name list of the loaded program for
2330 the symbols g++ uses for static constructors and destructors. */ 2415 the symbols g++ uses for static constructors and destructors. */
2343 struct pex_obj *pex; 2428 struct pex_obj *pex;
2344 const char *errmsg; 2429 const char *errmsg;
2345 int err; 2430 int err;
2346 char *p, buf[1024]; 2431 char *p, buf[1024];
2347 FILE *inf; 2432 FILE *inf;
2348 int found_lto = 0;
2349 2433
2350 if (which_pass == PASS_SECOND) 2434 if (which_pass == PASS_SECOND)
2351 return; 2435 return;
2352 2436
2353 /* LTO objects must be in a known format. This check prevents 2437 /* LTO objects must be in a known format. This check prevents
2354 us from accepting an archive containing LTO objects, which 2438 us from accepting an archive containing LTO objects, which
2355 gcc cannot currently handle. */ 2439 gcc cannot currently handle. */
2356 if (which_pass == PASS_LTOINFO && !maybe_lto_object_file (prog_name)) 2440 if (which_pass == PASS_LTOINFO)
2357 return; 2441 {
2442 if(is_lto_object_file (prog_name)) {
2443 add_lto_object (&lto_objects, prog_name);
2444 }
2445 return;
2446 }
2358 2447
2359 /* If we do not have an `nm', complain. */ 2448 /* If we do not have an `nm', complain. */
2360 if (nm_file_name == 0) 2449 if (nm_file_name == 0)
2361 fatal_error (input_location, "cannot find 'nm'"); 2450 fatal_error (input_location, "cannot find %<nm%>");
2362 2451
2363 nm_argv[argc++] = nm_file_name; 2452 nm_argv[argc++] = nm_file_name;
2364 if (NM_FLAGS[0] != '\0') 2453 if (NM_FLAGS[0] != '\0')
2365 nm_argv[argc++] = NM_FLAGS; 2454 nm_argv[argc++] = NM_FLAGS;
2366 2455
2382 fflush (stdout); 2471 fflush (stdout);
2383 fflush (stderr); 2472 fflush (stderr);
2384 2473
2385 pex = pex_init (PEX_USE_PIPES, "collect2", NULL); 2474 pex = pex_init (PEX_USE_PIPES, "collect2", NULL);
2386 if (pex == NULL) 2475 if (pex == NULL)
2387 fatal_error (input_location, "pex_init failed: %m"); 2476 fatal_error (input_location, "%<pex_init%> failed: %m");
2388 2477
2389 errmsg = pex_run (pex, 0, nm_file_name, real_nm_argv, NULL, HOST_BIT_BUCKET, 2478 errmsg = pex_run (pex, 0, nm_file_name, real_nm_argv, NULL, HOST_BIT_BUCKET,
2390 &err); 2479 &err);
2391 if (errmsg != NULL) 2480 if (errmsg != NULL)
2392 { 2481 {
2404 quit_handler = (void (*) (int)) signal (SIGQUIT, SIG_IGN); 2493 quit_handler = (void (*) (int)) signal (SIGQUIT, SIG_IGN);
2405 #endif 2494 #endif
2406 2495
2407 inf = pex_read_output (pex, 0); 2496 inf = pex_read_output (pex, 0);
2408 if (inf == NULL) 2497 if (inf == NULL)
2409 fatal_error (input_location, "can't open nm output: %m"); 2498 fatal_error (input_location, "cannot open nm output: %m");
2410 2499
2411 if (debug) 2500 if (debug)
2412 { 2501 fprintf (stderr, "\nnm output with constructors/destructors.\n");
2413 if (which_pass == PASS_LTOINFO)
2414 fprintf (stderr, "\nnm output with LTO info marker symbol.\n");
2415 else
2416 fprintf (stderr, "\nnm output with constructors/destructors.\n");
2417 }
2418 2502
2419 /* Read each line of nm output. */ 2503 /* Read each line of nm output. */
2420 while (fgets (buf, sizeof buf, inf) != (char *) 0) 2504 while (fgets (buf, sizeof buf, inf) != (char *) 0)
2421 { 2505 {
2422 int ch, ch2; 2506 int ch, ch2;
2423 char *name, *end; 2507 char *name, *end;
2424 2508
2425 if (debug) 2509 if (debug)
2426 fprintf (stderr, "\t%s\n", buf); 2510 fprintf (stderr, "\t%s\n", buf);
2427
2428 if (which_pass == PASS_LTOINFO)
2429 {
2430 if (found_lto)
2431 continue;
2432
2433 /* Look for the LTO info marker symbol, and add filename to
2434 the LTO objects list if found. */
2435 for (p = buf; (ch = *p) != '\0' && ch != '\n'; p++)
2436 if (ch == ' ' && p[1] == '_' && p[2] == '_'
2437 && (strncmp (p + (p[3] == '_' ? 2 : 1), "__gnu_lto_v1", 12) == 0)
2438 && ISSPACE (p[p[3] == '_' ? 14 : 13]))
2439 {
2440 add_lto_object (&lto_objects, prog_name);
2441
2442 /* We need to read all the input, so we can't just
2443 return here. But we can avoid useless work. */
2444 found_lto = 1;
2445
2446 break;
2447 }
2448
2449 continue;
2450 }
2451 2511
2452 /* If it contains a constructor or destructor name, add the name 2512 /* If it contains a constructor or destructor name, add the name
2453 to the appropriate list unless this is a kind of symbol we're 2513 to the appropriate list unless this is a kind of symbol we're
2454 not supposed to even consider. */ 2514 not supposed to even consider. */
2455 2515
2556 FILE *inf; 2616 FILE *inf;
2557 2617
2558 /* If we do not have an `ldd', complain. */ 2618 /* If we do not have an `ldd', complain. */
2559 if (ldd_file_name == 0) 2619 if (ldd_file_name == 0)
2560 { 2620 {
2561 error ("cannot find 'ldd'"); 2621 error ("cannot find %<ldd%>");
2562 return; 2622 return;
2563 } 2623 }
2564 2624
2565 ldd_argv[argc++] = ldd_file_name; 2625 ldd_argv[argc++] = ldd_file_name;
2566 ldd_argv[argc++] = prog_name; 2626 ldd_argv[argc++] = prog_name;
2602 quit_handler = (void (*) (int)) signal (SIGQUIT, SIG_IGN); 2662 quit_handler = (void (*) (int)) signal (SIGQUIT, SIG_IGN);
2603 #endif 2663 #endif
2604 2664
2605 inf = pex_read_output (pex, 0); 2665 inf = pex_read_output (pex, 0);
2606 if (inf == NULL) 2666 if (inf == NULL)
2607 fatal_error (input_location, "can't open ldd output: %m"); 2667 fatal_error (input_location, "cannot open ldd output: %m");
2608 2668
2609 if (debug) 2669 if (debug)
2610 notice ("\nldd output with constructors/destructors.\n"); 2670 notice ("\nldd output with constructors/destructors.\n");
2611 2671
2612 /* Read each line of ldd output. */ 2672 /* Read each line of ldd output. */
2632 *end = '\0'; 2692 *end = '\0';
2633 2693
2634 if (access (name, R_OK) == 0) 2694 if (access (name, R_OK) == 0)
2635 add_to_list (&libraries, name); 2695 add_to_list (&libraries, name);
2636 else 2696 else
2637 fatal_error (input_location, "unable to open dynamic dependency '%s'", 2697 fatal_error (input_location, "unable to open dynamic dependency "
2638 buf); 2698 "%qs", buf);
2639 2699
2640 if (debug) 2700 if (debug)
2641 fprintf (stderr, "\t%s\n", buf); 2701 fprintf (stderr, "\t%s\n", buf);
2642 } 2702 }
2643 if (debug) 2703 if (debug)
2761 scanfilter filter) 2821 scanfilter filter)
2762 { 2822 {
2763 LDFILE *ldptr = NULL; 2823 LDFILE *ldptr = NULL;
2764 int sym_index, sym_count; 2824 int sym_index, sym_count;
2765 int is_shared = 0; 2825 int is_shared = 0;
2766 int found_lto = 0; 2826
2767 2827 if (which_pass != PASS_FIRST && which_pass != PASS_OBJ)
2768 if (which_pass != PASS_FIRST && which_pass != PASS_OBJ
2769 && which_pass != PASS_LTOINFO)
2770 return; 2828 return;
2771 2829
2772 #ifdef COLLECT_EXPORT_LIST 2830 #ifdef COLLECT_EXPORT_LIST
2773 /* We do not need scanning for some standard C libraries. */ 2831 /* We do not need scanning for some standard C libraries. */
2774 if (which_pass == PASS_FIRST && ignore_library (prog_name)) 2832 if (which_pass == PASS_FIRST && ignore_library (prog_name))
2777 /* On AIX we have a loop, because there is not much difference 2835 /* On AIX we have a loop, because there is not much difference
2778 between an object and an archive. This trick allows us to 2836 between an object and an archive. This trick allows us to
2779 eliminate scan_libraries() function. */ 2837 eliminate scan_libraries() function. */
2780 do 2838 do
2781 { 2839 {
2782 found_lto = 0;
2783 #endif 2840 #endif
2784 /* Some platforms (e.g. OSF4) declare ldopen as taking a 2841 /* Some platforms (e.g. OSF4) declare ldopen as taking a
2785 non-const char * filename parameter, even though it will not 2842 non-const char * filename parameter, even though it will not
2786 modify that string. So we must cast away const-ness here, 2843 modify that string. So we must cast away const-ness here,
2787 using CONST_CAST to prevent complaints from -Wcast-qual. */ 2844 using CONST_CAST to prevent complaints from -Wcast-qual. */
2819 /* All AIX function names have a duplicate entry 2876 /* All AIX function names have a duplicate entry
2820 beginning with a dot. */ 2877 beginning with a dot. */
2821 if (*name == '.') 2878 if (*name == '.')
2822 ++name; 2879 ++name;
2823 #endif 2880 #endif
2824
2825 if (which_pass == PASS_LTOINFO)
2826 {
2827 if (found_lto)
2828 continue;
2829 if (strncmp (name, "__gnu_lto_v1", 12) == 0)
2830 {
2831 add_lto_object (&lto_objects, prog_name);
2832 found_lto = 1;
2833 break;
2834 }
2835 continue;
2836 }
2837 2881
2838 switch (is_ctor_dtor (name)) 2882 switch (is_ctor_dtor (name))
2839 { 2883 {
2840 #if TARGET_AIX_VERSION 2884 #if TARGET_AIX_VERSION
2841 /* Add AIX shared library initalisers/finalisers 2885 /* Add AIX shared library initalisers/finalisers