comparison gcc/collect2.c @ 63:b7f97abdc517 gcc-4.6-20100522

update gcc from gcc-4.5.0 to gcc-4.6
author ryoma <e075725@ie.u-ryukyu.ac.jp>
date Mon, 24 May 2010 12:47:05 +0900
parents 77e2b8dfacca
children f6334be47118
comparison
equal deleted inserted replaced
56:3c8a44c06a95 63:b7f97abdc517
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, 2009 4 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010
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
32 #include "tm.h" 32 #include "tm.h"
33 #include <signal.h> 33 #include <signal.h>
34 #if ! defined( SIGCHLD ) && defined( SIGCLD ) 34 #if ! defined( SIGCHLD ) && defined( SIGCLD )
35 # define SIGCHLD SIGCLD 35 # define SIGCHLD SIGCLD
36 #endif 36 #endif
37
38 /* TARGET_64BIT may be defined to use driver specific functionality. */
39 #undef TARGET_64BIT
40 #define TARGET_64BIT TARGET_64BIT_DEFAULT
37 41
38 #ifndef LIBRARY_PATH_ENV 42 #ifndef LIBRARY_PATH_ENV
39 #define LIBRARY_PATH_ENV "LIBRARY_PATH" 43 #define LIBRARY_PATH_ENV "LIBRARY_PATH"
40 #endif 44 #endif
41 45
172 struct id *first; 176 struct id *first;
173 struct id *last; 177 struct id *last;
174 int number; 178 int number;
175 }; 179 };
176 180
177 int vflag; /* true if -v */ 181 bool vflag; /* true if -v or --version */
178 static int rflag; /* true if -r */ 182 static int rflag; /* true if -r */
179 static int strip_flag; /* true if -s */ 183 static int strip_flag; /* true if -s */
180 static const char *demangle_flag; 184 static const char *demangle_flag;
181 #ifdef COLLECT_EXPORT_LIST 185 #ifdef COLLECT_EXPORT_LIST
182 static int export_flag; /* true if -bE */ 186 static int export_flag; /* true if -bE */
191 }; 195 };
192 196
193 /* Current LTO mode. */ 197 /* Current LTO mode. */
194 static enum lto_mode_d lto_mode = LTO_MODE_NONE; 198 static enum lto_mode_d lto_mode = LTO_MODE_NONE;
195 199
196 int debug; /* true if -debug */ 200 bool debug; /* true if -debug */
201 bool helpflag; /* true if --help */
197 202
198 static int shared_obj; /* true if -shared */ 203 static int shared_obj; /* true if -shared */
199 204
200 static const char *c_file; /* <xxx>.c for constructor/destructor list. */ 205 static const char *c_file; /* <xxx>.c for constructor/destructor list. */
201 static const char *o_file; /* <xxx>.o for constructor/destructor list. */ 206 static const char *o_file; /* <xxx>.o for constructor/destructor list. */
425 { 430 {
426 va_list ap; 431 va_list ap;
427 432
428 va_start (ap, cmsgid); 433 va_start (ap, cmsgid);
429 vfprintf (stderr, _(cmsgid), ap); 434 vfprintf (stderr, _(cmsgid), ap);
435 va_end (ap);
436 }
437
438 /* Notify user of a non-error, without translating the format string. */
439 void
440 notice_translated (const char *cmsgid, ...)
441 {
442 va_list ap;
443
444 va_start (ap, cmsgid);
445 vfprintf (stderr, cmsgid, ap);
430 va_end (ap); 446 va_end (ap);
431 } 447 }
432 448
433 /* Die when sys call fails. */ 449 /* Die when sys call fails. */
434 450
922 num_lto_c_args++; 938 num_lto_c_args++;
923 } 939 }
924 940
925 if (lto_objects.first) 941 if (lto_objects.first)
926 { 942 {
927 const char *opts;
928 char **lto_c_argv; 943 char **lto_c_argv;
929 const char **lto_c_ptr; 944 const char **lto_c_ptr;
930 const char *cp;
931 const char **p, **q, **r; 945 const char **p, **q, **r;
932 const char **lto_o_ptr; 946 const char **lto_o_ptr;
933 struct lto_object *list; 947 struct lto_object *list;
934 char *lto_wrapper = getenv ("COLLECT_LTO_WRAPPER"); 948 char *lto_wrapper = getenv ("COLLECT_LTO_WRAPPER");
935 struct pex_obj *pex; 949 struct pex_obj *pex;
936 const char *prog = "lto-wrapper"; 950 const char *prog = "lto-wrapper";
937 951
938 if (!lto_wrapper) 952 if (!lto_wrapper)
939 fatal ("COLLECT_LTO_WRAPPER must be set."); 953 fatal ("COLLECT_LTO_WRAPPER must be set.");
940 954
955 num_lto_c_args++;
956
941 /* There is at least one object file containing LTO info, 957 /* There is at least one object file containing LTO info,
942 so we need to run the LTO back end and relink. */ 958 so we need to run the LTO back end and relink. */
943 959
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); 960 lto_c_argv = (char **) xcalloc (sizeof (char *), num_lto_c_args);
969 lto_c_ptr = CONST_CAST2 (const char **, char **, lto_c_argv); 961 lto_c_ptr = CONST_CAST2 (const char **, char **, lto_c_argv);
970 962
971 *lto_c_ptr++ = lto_wrapper; 963 *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 964
988 /* Add LTO objects to the wrapper command line. */ 965 /* Add LTO objects to the wrapper command line. */
989 for (list = lto_objects.first; list; list = list->next) 966 for (list = lto_objects.first; list; list = list->next)
990 *lto_c_ptr++ = list->name; 967 *lto_c_ptr++ = list->name;
991 968
992 *lto_c_ptr = NULL; 969 *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 970
998 /* Run the LTO back end. */ 971 /* Run the LTO back end. */
999 pex = collect_execute (prog, lto_c_argv, NULL, NULL, PEX_SEARCH); 972 pex = collect_execute (prog, lto_c_argv, NULL, NULL, PEX_SEARCH);
1000 { 973 {
1001 int c; 974 int c;
1172 const char **object; 1145 const char **object;
1173 int first_file; 1146 int first_file;
1174 int num_c_args; 1147 int num_c_args;
1175 char **old_argv; 1148 char **old_argv;
1176 1149
1150 bool use_verbose = false;
1151
1177 old_argv = argv; 1152 old_argv = argv;
1178 expandargv (&argc, &argv); 1153 expandargv (&argc, &argv);
1179 if (argv != old_argv) 1154 if (argv != old_argv)
1180 at_file_supplied = 1; 1155 at_file_supplied = 1;
1181 1156
1224 int i; 1199 int i;
1225 1200
1226 for (i = 1; argv[i] != NULL; i ++) 1201 for (i = 1; argv[i] != NULL; i ++)
1227 { 1202 {
1228 if (! strcmp (argv[i], "-debug")) 1203 if (! strcmp (argv[i], "-debug"))
1229 debug = 1; 1204 debug = true;
1230 else if (! strcmp (argv[i], "-flto") && ! use_plugin) 1205 else if (! strcmp (argv[i], "-flto") && ! use_plugin)
1231 lto_mode = LTO_MODE_LTO; 1206 {
1232 else if (! strcmp (argv[i], "-fwhopr") && ! use_plugin) 1207 use_verbose = true;
1233 lto_mode = LTO_MODE_WHOPR; 1208 lto_mode = LTO_MODE_LTO;
1209 }
1210 else if (! strncmp (argv[i], "-fwhopr", 7) && ! use_plugin)
1211 {
1212 use_verbose = true;
1213 lto_mode = LTO_MODE_WHOPR;
1214 }
1234 else if (! strcmp (argv[i], "-plugin")) 1215 else if (! strcmp (argv[i], "-plugin"))
1235 { 1216 {
1236 use_plugin = true; 1217 use_plugin = true;
1218 use_verbose = true;
1237 lto_mode = LTO_MODE_NONE; 1219 lto_mode = LTO_MODE_NONE;
1238 } 1220 }
1239 #ifdef COLLECT_EXPORT_LIST 1221 #ifdef COLLECT_EXPORT_LIST
1240 /* since -brtl, -bexport, -b64 are not position dependent 1222 /* since -brtl, -bexport, -b64 are not position dependent
1241 also check for them here */ 1223 also check for them here */
1444 { 1426 {
1445 q = extract_string (&p); 1427 q = extract_string (&p);
1446 *c_ptr++ = xstrdup (q); 1428 *c_ptr++ = xstrdup (q);
1447 } 1429 }
1448 } 1430 }
1431 if (use_verbose && *q == '-' && q[1] == 'v' && q[2] == 0)
1432 {
1433 /* Turn on trace in collect2 if needed. */
1434 vflag = true;
1435 }
1449 } 1436 }
1450 obstack_free (&temporary_obstack, temporary_firstobj); 1437 obstack_free (&temporary_obstack, temporary_firstobj);
1451 *c_ptr++ = "-fno-profile-arcs"; 1438 *c_ptr++ = "-fno-profile-arcs";
1452 *c_ptr++ = "-fno-test-coverage"; 1439 *c_ptr++ = "-fno-test-coverage";
1453 *c_ptr++ = "-fno-branch-probabilities"; 1440 *c_ptr++ = "-fno-branch-probabilities";
1493 *ld1++ = *ld2++ = *argv; 1480 *ld1++ = *ld2++ = *argv;
1494 } 1481 }
1495 break; 1482 break;
1496 1483
1497 case 'f': 1484 case 'f':
1498 if (strcmp (arg, "-flto") == 0 || strcmp (arg, "-fwhopr") == 0) 1485 if (strcmp (arg, "-flto") == 0
1486 || strncmp (arg, "-fwhopr", 7) == 0)
1499 { 1487 {
1500 #ifdef ENABLE_LTO 1488 #ifdef ENABLE_LTO
1501 /* Do not pass LTO flag to the linker. */ 1489 /* Do not pass LTO flag to the linker. */
1502 ld1--; 1490 ld1--;
1503 ld2--; 1491 ld2--;
1572 } 1560 }
1573 break; 1561 break;
1574 1562
1575 case 'v': 1563 case 'v':
1576 if (arg[2] == '\0') 1564 if (arg[2] == '\0')
1577 vflag = 1; 1565 vflag = true;
1578 break; 1566 break;
1579 1567
1580 case '-': 1568 case '-':
1581 if (strcmp (arg, "--no-demangle") == 0) 1569 if (strcmp (arg, "--no-demangle") == 0)
1582 { 1570 {
1603 ld1--; 1591 ld1--;
1604 ld2--; 1592 ld2--;
1605 } 1593 }
1606 else if (strncmp (arg, "--sysroot=", 10) == 0) 1594 else if (strncmp (arg, "--sysroot=", 10) == 0)
1607 target_system_root = arg + 10; 1595 target_system_root = arg + 10;
1596 else if (strncmp (arg, "--version", 9) == 0)
1597 vflag = true;
1598 else if (strncmp (arg, "--help", 9) == 0)
1599 helpflag = true;
1608 break; 1600 break;
1609 } 1601 }
1610 } 1602 }
1611 else if ((p = strrchr (arg, '.')) != (char *) 0 1603 else if ((p = strrchr (arg, '.')) != (char *) 0
1612 && (strcmp (p, ".o") == 0 || strcmp (p, ".a") == 0 1604 && (strcmp (p, ".o") == 0 || strcmp (p, ".a") == 0
1661 /* Compute the filter to use from the current one, do scan, then adjust 1653 /* 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 1654 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 1655 control whether we need a first pass link later on or not, and what
1664 will remain to be scanned there. */ 1656 will remain to be scanned there. */
1665 1657
1666 scanfilter this_filter 1658 scanfilter this_filter = ld1_filter;
1667 = shared_obj ? ld1_filter : (ld1_filter & ~SCAN_DWEH); 1659 #if HAVE_AS_REF
1660 if (!shared_obj)
1661 this_filter &= ~SCAN_DWEH;
1662 #endif
1668 1663
1669 while (export_object_lst < object) 1664 while (export_object_lst < object)
1670 scan_prog_file (*export_object_lst++, PASS_OBJ, this_filter); 1665 scan_prog_file (*export_object_lst++, PASS_OBJ, this_filter);
1671 1666
1672 for (; list; list = list->next) 1667 for (; list; list = list->next)
1699 notice ("collect2 version %s", version_string); 1694 notice ("collect2 version %s", version_string);
1700 #ifdef TARGET_VERSION 1695 #ifdef TARGET_VERSION
1701 TARGET_VERSION; 1696 TARGET_VERSION;
1702 #endif 1697 #endif
1703 fprintf (stderr, "\n"); 1698 fprintf (stderr, "\n");
1699 }
1700
1701 if (helpflag)
1702 {
1703 fprintf (stderr, "Usage: collect2 [options]\n");
1704 fprintf (stderr, " Wrap linker and generate constructor code if needed.\n");
1705 fprintf (stderr, " Options:\n");
1706 fprintf (stderr, " -debug Enable debug output\n");
1707 fprintf (stderr, " --help Display this information\n");
1708 fprintf (stderr, " -v, --version Display this program's version number\n");
1709 fprintf (stderr, "Overview: http://gcc.gnu.org/onlinedocs/gccint/Collect2.html\n");
1710 fprintf (stderr, "Report bugs: %s\n", bug_report_url);
1711
1712 collect_exit (0);
1704 } 1713 }
1705 1714
1706 if (debug) 1715 if (debug)
1707 { 1716 {
1708 const char *ptr; 1717 const char *ptr;
1768 #ifdef COLLECT_EXPORT_LIST 1777 #ifdef COLLECT_EXPORT_LIST
1769 /* Make sure we delete the export file we may have created. */ 1778 /* Make sure we delete the export file we may have created. */
1770 if (export_file != 0 && export_file[0]) 1779 if (export_file != 0 && export_file[0])
1771 maybe_unlink (export_file); 1780 maybe_unlink (export_file);
1772 #endif 1781 #endif
1773 if (lto_mode) 1782 if (lto_mode != LTO_MODE_NONE)
1774 maybe_run_lto_and_relink (ld1_argv, object_lst, object, false); 1783 maybe_run_lto_and_relink (ld1_argv, object_lst, object, false);
1775 1784
1776 maybe_unlink (c_file); 1785 maybe_unlink (c_file);
1777 maybe_unlink (o_file); 1786 maybe_unlink (o_file);
1778 return 0; 1787 return 0;
1790 scan_libraries (output_file); 1799 scan_libraries (output_file);
1791 #endif 1800 #endif
1792 1801
1793 if (debug) 1802 if (debug)
1794 { 1803 {
1795 notice ("%d constructor(s) found\n", constructors.number); 1804 notice_translated (ngettext ("%d constructor found\n",
1796 notice ("%d destructor(s) found\n", destructors.number); 1805 "%d constructors found\n",
1797 notice ("%d frame table(s) found\n", frame_tables.number); 1806 constructors.number),
1807 constructors.number);
1808 notice_translated (ngettext ("%d destructor found\n",
1809 "%d destructors found\n",
1810 destructors.number),
1811 destructors.number);
1812 notice_translated (ngettext("%d frame table found\n",
1813 "%d frame tables found\n",
1814 frame_tables.number),
1815 frame_tables.number);
1798 } 1816 }
1799 1817
1800 /* If the scan exposed nothing of special interest, there's no need to 1818 /* If the scan exposed nothing of special interest, there's no need to
1801 generate the glue code and relink so return now. */ 1819 generate the glue code and relink so return now. */
1802 1820
2505 } 2523 }
2506 #endif 2524 #endif
2507 2525
2508 #ifdef OBJECT_FORMAT_NONE 2526 #ifdef OBJECT_FORMAT_NONE
2509 2527
2510 /* Check to make sure the file is an ELF file. LTO objects must 2528 /* Check to make sure the file is an LTO object file. */
2511 be in ELF format. */
2512 2529
2513 static bool 2530 static bool
2514 is_elf (const char *prog_name) 2531 maybe_lto_object_file (const char *prog_name)
2515 { 2532 {
2516 FILE *f; 2533 FILE *f;
2517 char buf[4]; 2534 unsigned char buf[4];
2518 static char magic[4] = { 0x7f, 'E', 'L', 'F' }; 2535 int i;
2519 2536
2520 f = fopen (prog_name, "r"); 2537 static unsigned char elfmagic[4] = { 0x7f, 'E', 'L', 'F' };
2538 static unsigned char coffmagic[2] = { 0x4c, 0x01 };
2539 static unsigned char coffmagic_x64[2] = { 0x64, 0x86 };
2540 static unsigned char machomagic[4][4] = {
2541 { 0xcf, 0xfa, 0xed, 0xfe },
2542 { 0xce, 0xfa, 0xed, 0xfe },
2543 { 0xfe, 0xed, 0xfa, 0xcf },
2544 { 0xfe, 0xed, 0xfa, 0xce }
2545 };
2546
2547 f = fopen (prog_name, "rb");
2521 if (f == NULL) 2548 if (f == NULL)
2522 return false; 2549 return false;
2523 if (fread (buf, sizeof (buf), 1, f) != 1) 2550 if (fread (buf, sizeof (buf), 1, f) != 1)
2524 buf[0] = 0; 2551 buf[0] = 0;
2525 fclose (f); 2552 fclose (f);
2526 return memcmp (buf, magic, sizeof (magic)) == 0; 2553
2554 if (memcmp (buf, elfmagic, sizeof (elfmagic)) == 0
2555 || memcmp (buf, coffmagic, sizeof (coffmagic)) == 0
2556 || memcmp (buf, coffmagic_x64, sizeof (coffmagic_x64)) == 0)
2557 return true;
2558 for (i = 0; i < 4; i++)
2559 if (memcmp (buf, machomagic[i], sizeof (machomagic[i])) == 0)
2560 return true;
2561
2562 return false;
2527 } 2563 }
2528 2564
2529 /* Generic version to scan the name list of the loaded program for 2565 /* Generic version to scan the name list of the loaded program for
2530 the symbols g++ uses for static constructors and destructors. */ 2566 the symbols g++ uses for static constructors and destructors. */
2531 2567
2548 int found_lto = 0; 2584 int found_lto = 0;
2549 2585
2550 if (which_pass == PASS_SECOND) 2586 if (which_pass == PASS_SECOND)
2551 return; 2587 return;
2552 2588
2553 /* LTO objects must be in ELF format. This check prevents 2589 /* LTO objects must be in a known format. This check prevents
2554 us from accepting an archive containing LTO objects, which 2590 us from accepting an archive containing LTO objects, which
2555 gcc cannnot currently handle. */ 2591 gcc cannnot currently handle. */
2556 if (which_pass == PASS_LTOINFO && !is_elf (prog_name)) 2592 if (which_pass == PASS_LTOINFO && !maybe_lto_object_file (prog_name))
2557 return; 2593 return;
2558 2594
2559 /* If we do not have an `nm', complain. */ 2595 /* If we do not have an `nm', complain. */
2560 if (nm_file_name == 0) 2596 if (nm_file_name == 0)
2561 fatal ("cannot find 'nm'"); 2597 fatal ("cannot find 'nm'");
2631 continue; 2667 continue;
2632 2668
2633 /* Look for the LTO info marker symbol, and add filename to 2669 /* Look for the LTO info marker symbol, and add filename to
2634 the LTO objects list if found. */ 2670 the LTO objects list if found. */
2635 for (p = buf; (ch = *p) != '\0' && ch != '\n'; p++) 2671 for (p = buf; (ch = *p) != '\0' && ch != '\n'; p++)
2636 if (ch == ' ' 2672 if (ch == ' ' && p[1] == '_' && p[2] == '_'
2637 && (strncmp (p +1 , "gnu_lto_v1", 10) == 0) 2673 && (strncmp (p + (p[3] == '_' ? 2 : 1), "__gnu_lto_v1", 12) == 0)
2638 && ISSPACE( p[11])) 2674 && ISSPACE (p[p[3] == '_' ? 14 : 13]))
2639 { 2675 {
2640 add_lto_object (&lto_objects, prog_name); 2676 add_lto_object (&lto_objects, prog_name);
2641 2677
2642 /* We need to read all the input, so we can't just 2678 /* We need to read all the input, so we can't just
2643 return here. But we can avoid useless work. */ 2679 return here. But we can avoid useless work. */
2847 #endif 2883 #endif
2848 2884
2849 /* Now iterate through the library list adding their symbols to 2885 /* Now iterate through the library list adding their symbols to
2850 the list. */ 2886 the list. */
2851 for (list = libraries.first; list; list = list->next) 2887 for (list = libraries.first; list; list = list->next)
2852 scan_prog_file (list->name, PASS_LIB); 2888 scan_prog_file (list->name, PASS_LIB, SCAN_ALL);
2853 } 2889 }
2854 2890
2855 #endif /* LDD_SUFFIX */ 2891 #endif /* LDD_SUFFIX */
2856 2892
2857 #endif /* OBJECT_FORMAT_NONE */ 2893 #endif /* OBJECT_FORMAT_NONE */