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