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