1 /* Main program of GNU linker. 2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 3 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 4 Free Software Foundation, Inc. 5 Written by Steve Chamberlain steve@cygnus.com 6 7 This file is part of the GNU Binutils. 8 9 This program is free software; you can redistribute it and/or modify 10 it under the terms of the GNU General Public License as published by 11 the Free Software Foundation; either version 3 of the License, or 12 (at your option) any later version. 13 14 This program is distributed in the hope that it will be useful, 15 but WITHOUT ANY WARRANTY; without even the implied warranty of 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 GNU General Public License for more details. 18 19 You should have received a copy of the GNU General Public License 20 along with this program; if not, write to the Free Software 21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, 22 MA 02110-1301, USA. */ 23 24 #include "sysdep.h" 25 #include "bfd.h" 26 #include "safe-ctype.h" 27 #include "libiberty.h" 28 #include "progress.h" 29 #include "bfdlink.h" 30 #include "filenames.h" 31 32 #include "ld.h" 33 #include "ldmain.h" 34 #include "ldmisc.h" 35 #include "ldwrite.h" 36 #include "ldexp.h" 37 #include "ldlang.h" 38 #include <ldgram.h> 39 #include "ldlex.h" 40 #include "ldfile.h" 41 #include "ldemul.h" 42 #include "ldctor.h" 43 #ifdef ENABLE_PLUGINS 44 #include "plugin.h" 45 #include "plugin-api.h" 46 #include "libbfd.h" 47 #endif /* ENABLE_PLUGINS */ 48 49 /* Somewhere above, sys/stat.h got included. */ 50 #if !defined(S_ISDIR) && defined(S_IFDIR) 51 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) 52 #endif 53 54 #include <string.h> 55 56 #ifdef HAVE_SBRK 57 #if !HAVE_DECL_SBRK 58 extern void *sbrk (); 59 #endif 60 #endif 61 62 #ifndef TARGET_SYSTEM_ROOT 63 #define TARGET_SYSTEM_ROOT "" 64 #endif 65 66 /* EXPORTS */ 67 68 FILE *saved_script_handle = NULL; 69 FILE *previous_script_handle = NULL; 70 bfd_boolean force_make_executable = FALSE; 71 72 char *default_target; 73 const char *output_filename = "a.out"; 74 75 /* Name this program was invoked by. */ 76 char *program_name; 77 78 /* The prefix for system library directories. */ 79 const char *ld_sysroot; 80 81 /* The canonical representation of ld_sysroot. */ 82 char * ld_canon_sysroot; 83 int ld_canon_sysroot_len; 84 85 /* Set by -G argument, for MIPS ECOFF target. */ 86 int g_switch_value = 8; 87 88 /* Nonzero means print names of input files as processed. */ 89 bfd_boolean trace_files; 90 91 /* Nonzero means report actions taken by the linker, and describe the linker script in use. */ 92 bfd_boolean verbose; 93 94 /* Nonzero means version number was printed, so exit successfully 95 instead of complaining if no input files are given. */ 96 bfd_boolean version_printed; 97 98 /* TRUE if we should demangle symbol names. */ 99 bfd_boolean demangling; 100 101 args_type command_line; 102 103 ld_config_type config; 104 105 sort_type sort_section; 106 107 static const char *get_sysroot 108 (int, char **); 109 static char *get_emulation 110 (int, char **); 111 static bfd_boolean add_archive_element 112 (struct bfd_link_info *, bfd *, const char *, bfd **); 113 static bfd_boolean multiple_definition 114 (struct bfd_link_info *, struct bfd_link_hash_entry *, 115 bfd *, asection *, bfd_vma); 116 static bfd_boolean multiple_common 117 (struct bfd_link_info *, struct bfd_link_hash_entry *, 118 bfd *, enum bfd_link_hash_type, bfd_vma); 119 static bfd_boolean add_to_set 120 (struct bfd_link_info *, struct bfd_link_hash_entry *, 121 bfd_reloc_code_real_type, bfd *, asection *, bfd_vma); 122 static bfd_boolean constructor_callback 123 (struct bfd_link_info *, bfd_boolean, const char *, bfd *, 124 asection *, bfd_vma); 125 static bfd_boolean warning_callback 126 (struct bfd_link_info *, const char *, const char *, bfd *, 127 asection *, bfd_vma); 128 static void warning_find_reloc 129 (bfd *, asection *, void *); 130 static bfd_boolean undefined_symbol 131 (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma, 132 bfd_boolean); 133 static bfd_boolean reloc_overflow 134 (struct bfd_link_info *, struct bfd_link_hash_entry *, const char *, 135 const char *, bfd_vma, bfd *, asection *, bfd_vma); 136 static bfd_boolean reloc_dangerous 137 (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma); 138 static bfd_boolean unattached_reloc 139 (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma); 140 static bfd_boolean notice 141 (struct bfd_link_info *, struct bfd_link_hash_entry *, 142 bfd *, asection *, bfd_vma, flagword, const char *); 143 144 static struct bfd_link_callbacks link_callbacks = 145 { 146 add_archive_element, 147 multiple_definition, 148 multiple_common, 149 add_to_set, 150 constructor_callback, 151 warning_callback, 152 undefined_symbol, 153 reloc_overflow, 154 reloc_dangerous, 155 unattached_reloc, 156 notice, 157 einfo, 158 info_msg, 159 minfo, 160 ldlang_override_segment_assignment 161 }; 162 163 static bfd_assert_handler_type default_bfd_assert_handler; 164 165 struct bfd_link_info link_info; 166 167 static void 168 ld_cleanup (void) 169 { 170 bfd_cache_close_all (); 171 #ifdef ENABLE_PLUGINS 172 plugin_call_cleanup (); 173 #endif 174 if (output_filename && delete_output_file_on_failure) 175 unlink_if_ordinary (output_filename); 176 } 177 178 /* If there's a BFD assertion, we'll notice and exit with an error 179 unless otherwise instructed. */ 180 181 static void 182 ld_bfd_assert_handler (const char *fmt, const char *bfdver, 183 const char *file, int line) 184 { 185 (*default_bfd_assert_handler) (fmt, bfdver, file, line); 186 config.make_executable = FALSE; 187 } 188 189 int 190 main (int argc, char **argv) 191 { 192 char *emulation; 193 long start_time = get_run_time (); 194 195 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES) 196 setlocale (LC_MESSAGES, ""); 197 #endif 198 #if defined (HAVE_SETLOCALE) 199 setlocale (LC_CTYPE, ""); 200 #endif 201 bindtextdomain (PACKAGE, LOCALEDIR); 202 textdomain (PACKAGE); 203 204 program_name = argv[0]; 205 xmalloc_set_program_name (program_name); 206 207 START_PROGRESS (program_name, 0); 208 209 expandargv (&argc, &argv); 210 211 bfd_init (); 212 213 bfd_set_error_program_name (program_name); 214 215 /* We want to notice and fail on those nasty BFD assertions which are 216 likely to signal incorrect output being generated but otherwise may 217 leave no trace. */ 218 default_bfd_assert_handler = bfd_set_assert_handler (ld_bfd_assert_handler); 219 220 xatexit (ld_cleanup); 221 222 /* Set up the sysroot directory. */ 223 ld_sysroot = get_sysroot (argc, argv); 224 if (*ld_sysroot) 225 { 226 if (*TARGET_SYSTEM_ROOT == 0) 227 { 228 einfo ("%P%F: this linker was not configured to use sysroots\n"); 229 ld_sysroot = ""; 230 } 231 else 232 ld_canon_sysroot = lrealpath (ld_sysroot); 233 } 234 if (ld_canon_sysroot) 235 ld_canon_sysroot_len = strlen (ld_canon_sysroot); 236 else 237 ld_canon_sysroot_len = -1; 238 239 /* Set the default BFD target based on the configured target. Doing 240 this permits the linker to be configured for a particular target, 241 and linked against a shared BFD library which was configured for 242 a different target. The macro TARGET is defined by Makefile. */ 243 if (! bfd_set_default_target (TARGET)) 244 { 245 einfo (_("%X%P: can't set BFD default target to `%s': %E\n"), TARGET); 246 xexit (1); 247 } 248 249 #if YYDEBUG 250 { 251 extern int yydebug; 252 yydebug = 1; 253 } 254 #endif 255 256 config.build_constructors = TRUE; 257 config.rpath_separator = ':'; 258 config.split_by_reloc = (unsigned) -1; 259 config.split_by_file = (bfd_size_type) -1; 260 config.make_executable = TRUE; 261 config.magic_demand_paged = TRUE; 262 config.text_read_only = TRUE; 263 264 command_line.warn_mismatch = TRUE; 265 command_line.warn_search_mismatch = TRUE; 266 command_line.check_section_addresses = -1; 267 command_line.disable_target_specific_optimizations = -1; 268 269 /* We initialize DEMANGLING based on the environment variable 270 COLLECT_NO_DEMANGLE. The gcc collect2 program will demangle the 271 output of the linker, unless COLLECT_NO_DEMANGLE is set in the 272 environment. Acting the same way here lets us provide the same 273 interface by default. */ 274 demangling = getenv ("COLLECT_NO_DEMANGLE") == NULL; 275 276 link_info.allow_undefined_version = TRUE; 277 link_info.keep_memory = TRUE; 278 link_info.combreloc = TRUE; 279 link_info.strip_discarded = TRUE; 280 link_info.emit_hash = TRUE; 281 link_info.callbacks = &link_callbacks; 282 link_info.input_bfds_tail = &link_info.input_bfds; 283 /* SVR4 linkers seem to set DT_INIT and DT_FINI based on magic _init 284 and _fini symbols. We are compatible. */ 285 link_info.init_function = "_init"; 286 link_info.fini_function = "_fini"; 287 link_info.relax_pass = 1; 288 link_info.pei386_auto_import = -1; 289 link_info.spare_dynamic_tags = 5; 290 link_info.path_separator = ':'; 291 292 ldfile_add_arch (""); 293 emulation = get_emulation (argc, argv); 294 ldemul_choose_mode (emulation); 295 default_target = ldemul_choose_target (argc, argv); 296 config.maxpagesize = bfd_emul_get_maxpagesize (default_target); 297 config.commonpagesize = bfd_emul_get_commonpagesize (default_target); 298 lang_init (); 299 ldemul_before_parse (); 300 lang_has_input_file = FALSE; 301 parse_args (argc, argv); 302 303 if (config.hash_table_size != 0) 304 bfd_hash_set_default_size (config.hash_table_size); 305 306 #ifdef ENABLE_PLUGINS 307 /* Now all the plugin arguments have been gathered, we can load them. */ 308 plugin_load_plugins (); 309 #endif /* ENABLE_PLUGINS */ 310 311 ldemul_set_symbols (); 312 313 /* If we have not already opened and parsed a linker script, 314 try the default script from command line first. */ 315 if (saved_script_handle == NULL 316 && command_line.default_script != NULL) 317 { 318 ldfile_open_command_file (command_line.default_script); 319 parser_input = input_script; 320 yyparse (); 321 } 322 323 /* If we have not already opened and parsed a linker script 324 read the emulation's appropriate default script. */ 325 if (saved_script_handle == NULL) 326 { 327 int isfile; 328 char *s = ldemul_get_script (&isfile); 329 330 if (isfile) 331 ldfile_open_default_command_file (s); 332 else 333 { 334 lex_string = s; 335 lex_redirect (s, _("built in linker script"), 1); 336 } 337 parser_input = input_script; 338 yyparse (); 339 lex_string = NULL; 340 } 341 342 if (verbose) 343 { 344 if (saved_script_handle) 345 info_msg (_("using external linker script:")); 346 else 347 info_msg (_("using internal linker script:")); 348 info_msg ("\n==================================================\n"); 349 350 if (saved_script_handle) 351 { 352 static const int ld_bufsz = 8193; 353 size_t n; 354 char *buf = (char *) xmalloc (ld_bufsz); 355 356 rewind (saved_script_handle); 357 while ((n = fread (buf, 1, ld_bufsz - 1, saved_script_handle)) > 0) 358 { 359 buf[n] = 0; 360 info_msg (buf); 361 } 362 rewind (saved_script_handle); 363 free (buf); 364 } 365 else 366 { 367 int isfile; 368 369 info_msg (ldemul_get_script (&isfile)); 370 } 371 372 info_msg ("\n==================================================\n"); 373 } 374 375 if (command_line.print_output_format) 376 info_msg ("%s\n", lang_get_output_target ()); 377 378 lang_final (); 379 380 if (!lang_has_input_file) 381 { 382 if (version_printed || command_line.print_output_format) 383 xexit (0); 384 einfo (_("%P%F: no input files\n")); 385 } 386 387 if (trace_files) 388 info_msg (_("%P: mode %s\n"), emulation); 389 390 ldemul_after_parse (); 391 392 if (config.map_filename) 393 { 394 if (strcmp (config.map_filename, "-") == 0) 395 { 396 config.map_file = stdout; 397 } 398 else 399 { 400 config.map_file = fopen (config.map_filename, FOPEN_WT); 401 if (config.map_file == (FILE *) NULL) 402 { 403 bfd_set_error (bfd_error_system_call); 404 einfo (_("%P%F: cannot open map file %s: %E\n"), 405 config.map_filename); 406 } 407 } 408 } 409 410 lang_process (); 411 412 /* Print error messages for any missing symbols, for any warning 413 symbols, and possibly multiple definitions. */ 414 if (link_info.relocatable) 415 link_info.output_bfd->flags &= ~EXEC_P; 416 else 417 link_info.output_bfd->flags |= EXEC_P; 418 419 ldwrite (); 420 421 if (config.map_file != NULL) 422 lang_map (); 423 if (command_line.cref) 424 output_cref (config.map_file != NULL ? config.map_file : stdout); 425 if (nocrossref_list != NULL) 426 check_nocrossrefs (); 427 428 lang_finish (); 429 430 /* Even if we're producing relocatable output, some non-fatal errors should 431 be reported in the exit status. (What non-fatal errors, if any, do we 432 want to ignore for relocatable output?) */ 433 if (!config.make_executable && !force_make_executable) 434 { 435 if (trace_files) 436 einfo (_("%P: link errors found, deleting executable `%s'\n"), 437 output_filename); 438 439 /* The file will be removed by ld_cleanup. */ 440 xexit (1); 441 } 442 else 443 { 444 if (! bfd_close (link_info.output_bfd)) 445 einfo (_("%F%B: final close failed: %E\n"), link_info.output_bfd); 446 447 /* If the --force-exe-suffix is enabled, and we're making an 448 executable file and it doesn't end in .exe, copy it to one 449 which does. */ 450 if (! link_info.relocatable && command_line.force_exe_suffix) 451 { 452 int len = strlen (output_filename); 453 454 if (len < 4 455 || (strcasecmp (output_filename + len - 4, ".exe") != 0 456 && strcasecmp (output_filename + len - 4, ".dll") != 0)) 457 { 458 FILE *src; 459 FILE *dst; 460 const int bsize = 4096; 461 char *buf = (char *) xmalloc (bsize); 462 int l; 463 char *dst_name = (char *) xmalloc (len + 5); 464 465 strcpy (dst_name, output_filename); 466 strcat (dst_name, ".exe"); 467 src = fopen (output_filename, FOPEN_RB); 468 dst = fopen (dst_name, FOPEN_WB); 469 470 if (!src) 471 einfo (_("%X%P: unable to open for source of copy `%s'\n"), 472 output_filename); 473 if (!dst) 474 einfo (_("%X%P: unable to open for destination of copy `%s'\n"), 475 dst_name); 476 while ((l = fread (buf, 1, bsize, src)) > 0) 477 { 478 int done = fwrite (buf, 1, l, dst); 479 480 if (done != l) 481 einfo (_("%P: Error writing file `%s'\n"), dst_name); 482 } 483 484 fclose (src); 485 if (fclose (dst) == EOF) 486 einfo (_("%P: Error closing file `%s'\n"), dst_name); 487 free (dst_name); 488 free (buf); 489 } 490 } 491 } 492 493 END_PROGRESS (program_name); 494 495 if (config.stats) 496 { 497 #ifdef HAVE_SBRK 498 char *lim = (char *) sbrk (0); 499 #endif 500 long run_time = get_run_time () - start_time; 501 502 fflush (stdout); 503 fprintf (stderr, _("%s: total time in link: %ld.%06ld\n"), 504 program_name, run_time / 1000000, run_time % 1000000); 505 #ifdef HAVE_SBRK 506 fprintf (stderr, _("%s: data size %ld\n"), program_name, 507 (long) (lim - (char *) &environ)); 508 #endif 509 fflush (stderr); 510 } 511 512 /* Prevent ld_cleanup from doing anything, after a successful link. */ 513 output_filename = NULL; 514 515 xexit (0); 516 return 0; 517 } 518 519 /* If the configured sysroot is relocatable, try relocating it based on 520 default prefix FROM. Return the relocated directory if it exists, 521 otherwise return null. */ 522 523 static char * 524 get_relative_sysroot (const char *from ATTRIBUTE_UNUSED) 525 { 526 #ifdef TARGET_SYSTEM_ROOT_RELOCATABLE 527 char *path; 528 struct stat s; 529 530 path = make_relative_prefix (program_name, from, TARGET_SYSTEM_ROOT); 531 if (path) 532 { 533 if (stat (path, &s) == 0 && S_ISDIR (s.st_mode)) 534 return path; 535 free (path); 536 } 537 #endif 538 return 0; 539 } 540 541 /* Return the sysroot directory. Return "" if no sysroot is being used. */ 542 543 static const char * 544 get_sysroot (int argc, char **argv) 545 { 546 int i; 547 const char *path; 548 549 for (i = 1; i < argc; i++) 550 if (CONST_STRNEQ (argv[i], "--sysroot=")) 551 return argv[i] + strlen ("--sysroot="); 552 553 path = get_relative_sysroot (BINDIR); 554 if (path) 555 return path; 556 557 path = get_relative_sysroot (TOOLBINDIR); 558 if (path) 559 return path; 560 561 return TARGET_SYSTEM_ROOT; 562 } 563 564 /* We need to find any explicitly given emulation in order to initialize the 565 state that's needed by the lex&yacc argument parser (parse_args). */ 566 567 static char * 568 get_emulation (int argc, char **argv) 569 { 570 char *emulation; 571 int i; 572 573 emulation = getenv (EMULATION_ENVIRON); 574 if (emulation == NULL) 575 emulation = DEFAULT_EMULATION; 576 577 for (i = 1; i < argc; i++) 578 { 579 if (CONST_STRNEQ (argv[i], "-m")) 580 { 581 if (argv[i][2] == '\0') 582 { 583 /* -m EMUL */ 584 if (i < argc - 1) 585 { 586 emulation = argv[i + 1]; 587 i++; 588 } 589 else 590 einfo (_("%P%F: missing argument to -m\n")); 591 } 592 else if (strcmp (argv[i], "-mips1") == 0 593 || strcmp (argv[i], "-mips2") == 0 594 || strcmp (argv[i], "-mips3") == 0 595 || strcmp (argv[i], "-mips4") == 0 596 || strcmp (argv[i], "-mips5") == 0 597 || strcmp (argv[i], "-mips32") == 0 598 || strcmp (argv[i], "-mips32r2") == 0 599 || strcmp (argv[i], "-mips64") == 0 600 || strcmp (argv[i], "-mips64r2") == 0) 601 { 602 /* FIXME: The arguments -mips1, -mips2, -mips3, etc. are 603 passed to the linker by some MIPS compilers. They 604 generally tell the linker to use a slightly different 605 library path. Perhaps someday these should be 606 implemented as emulations; until then, we just ignore 607 the arguments and hope that nobody ever creates 608 emulations named ips1, ips2 or ips3. */ 609 } 610 else if (strcmp (argv[i], "-m486") == 0) 611 { 612 /* FIXME: The argument -m486 is passed to the linker on 613 some Linux systems. Hope that nobody creates an 614 emulation named 486. */ 615 } 616 else 617 { 618 /* -mEMUL */ 619 emulation = &argv[i][2]; 620 } 621 } 622 } 623 624 return emulation; 625 } 626 627 void 628 add_ysym (const char *name) 629 { 630 if (link_info.notice_hash == NULL) 631 { 632 link_info.notice_hash = 633 (struct bfd_hash_table *) xmalloc (sizeof (struct bfd_hash_table)); 634 if (!bfd_hash_table_init_n (link_info.notice_hash, 635 bfd_hash_newfunc, 636 sizeof (struct bfd_hash_entry), 637 61)) 638 einfo (_("%P%F: bfd_hash_table_init failed: %E\n")); 639 } 640 641 if (bfd_hash_lookup (link_info.notice_hash, name, TRUE, TRUE) == NULL) 642 einfo (_("%P%F: bfd_hash_lookup failed: %E\n")); 643 } 644 645 void 646 add_ignoresym (const char *name) 647 { 648 if (link_info.ignore_hash == NULL) 649 { 650 link_info.ignore_hash = xmalloc (sizeof (struct bfd_hash_table)); 651 if (! bfd_hash_table_init_n (link_info.ignore_hash, 652 bfd_hash_newfunc, 653 sizeof (struct bfd_hash_entry), 654 61)) 655 einfo (_("%P%F: bfd_hash_table_init failed: %E\n")); 656 } 657 658 if (bfd_hash_lookup (link_info.ignore_hash, name, TRUE, TRUE) == NULL) 659 einfo (_("%P%F: bfd_hash_lookup failed: %E\n")); 660 } 661 662 /* Record a symbol to be wrapped, from the --wrap option. */ 663 664 void 665 add_wrap (const char *name) 666 { 667 if (link_info.wrap_hash == NULL) 668 { 669 link_info.wrap_hash = 670 (struct bfd_hash_table *) xmalloc (sizeof (struct bfd_hash_table)); 671 if (!bfd_hash_table_init_n (link_info.wrap_hash, 672 bfd_hash_newfunc, 673 sizeof (struct bfd_hash_entry), 674 61)) 675 einfo (_("%P%F: bfd_hash_table_init failed: %E\n")); 676 } 677 678 if (bfd_hash_lookup (link_info.wrap_hash, name, TRUE, TRUE) == NULL) 679 einfo (_("%P%F: bfd_hash_lookup failed: %E\n")); 680 } 681 682 /* Handle the -retain-symbols-file option. */ 683 684 void 685 add_keepsyms_file (const char *filename) 686 { 687 FILE *file; 688 char *buf; 689 size_t bufsize; 690 int c; 691 692 if (link_info.strip == strip_some) 693 einfo (_("%X%P: error: duplicate retain-symbols-file\n")); 694 695 file = fopen (filename, "r"); 696 if (file == NULL) 697 { 698 bfd_set_error (bfd_error_system_call); 699 einfo ("%X%P: %s: %E\n", filename); 700 return; 701 } 702 703 link_info.keep_hash = (struct bfd_hash_table *) 704 xmalloc (sizeof (struct bfd_hash_table)); 705 if (!bfd_hash_table_init (link_info.keep_hash, bfd_hash_newfunc, 706 sizeof (struct bfd_hash_entry))) 707 einfo (_("%P%F: bfd_hash_table_init failed: %E\n")); 708 709 bufsize = 100; 710 buf = (char *) xmalloc (bufsize); 711 712 c = getc (file); 713 while (c != EOF) 714 { 715 while (ISSPACE (c)) 716 c = getc (file); 717 718 if (c != EOF) 719 { 720 size_t len = 0; 721 722 while (! ISSPACE (c) && c != EOF) 723 { 724 buf[len] = c; 725 ++len; 726 if (len >= bufsize) 727 { 728 bufsize *= 2; 729 buf = (char *) xrealloc (buf, bufsize); 730 } 731 c = getc (file); 732 } 733 734 buf[len] = '\0'; 735 736 if (bfd_hash_lookup (link_info.keep_hash, buf, TRUE, TRUE) == NULL) 737 einfo (_("%P%F: bfd_hash_lookup for insertion failed: %E\n")); 738 } 739 } 740 741 if (link_info.strip != strip_none) 742 einfo (_("%P: `-retain-symbols-file' overrides `-s' and `-S'\n")); 743 744 free (buf); 745 link_info.strip = strip_some; 746 } 747 748 /* Callbacks from the BFD linker routines. */ 749 750 /* This is called when BFD has decided to include an archive member in 751 a link. */ 752 753 static bfd_boolean 754 add_archive_element (struct bfd_link_info *info, 755 bfd *abfd, 756 const char *name, 757 bfd **subsbfd ATTRIBUTE_UNUSED) 758 { 759 lang_input_statement_type *input; 760 lang_input_statement_type orig_input; 761 762 input = (lang_input_statement_type *) 763 xcalloc (1, sizeof (lang_input_statement_type)); 764 input->filename = abfd->filename; 765 input->local_sym_name = abfd->filename; 766 input->the_bfd = abfd; 767 768 /* Save the original data for trace files/tries below, as plugins 769 (if enabled) may possibly alter it to point to a replacement 770 BFD, but we still want to output the original BFD filename. */ 771 orig_input = *input; 772 #ifdef ENABLE_PLUGINS 773 if (plugin_active_plugins_p () && !no_more_claiming) 774 { 775 /* We must offer this archive member to the plugins to claim. */ 776 const char *filename = (bfd_my_archive (abfd) != NULL 777 ? bfd_my_archive (abfd)->filename : abfd->filename); 778 int fd = open (filename, O_RDONLY | O_BINARY); 779 if (fd >= 0) 780 { 781 struct ld_plugin_input_file file; 782 783 /* Offset and filesize must refer to the individual archive 784 member, not the whole file, and must exclude the header. 785 Fortunately for us, that is how the data is stored in the 786 origin field of the bfd and in the arelt_data. */ 787 file.name = filename; 788 file.offset = abfd->origin; 789 file.filesize = arelt_size (abfd); 790 file.fd = fd; 791 plugin_maybe_claim (&file, input); 792 if (input->flags.claimed) 793 { 794 input->flags.claim_archive = TRUE; 795 *subsbfd = input->the_bfd; 796 } 797 } 798 } 799 #endif /* ENABLE_PLUGINS */ 800 801 ldlang_add_file (input); 802 803 if (config.map_file != NULL) 804 { 805 static bfd_boolean header_printed; 806 struct bfd_link_hash_entry *h; 807 bfd *from; 808 int len; 809 810 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE); 811 812 if (h == NULL) 813 from = NULL; 814 else 815 { 816 switch (h->type) 817 { 818 default: 819 from = NULL; 820 break; 821 822 case bfd_link_hash_defined: 823 case bfd_link_hash_defweak: 824 from = h->u.def.section->owner; 825 break; 826 827 case bfd_link_hash_undefined: 828 case bfd_link_hash_undefweak: 829 from = h->u.undef.abfd; 830 break; 831 832 case bfd_link_hash_common: 833 from = h->u.c.p->section->owner; 834 break; 835 } 836 } 837 838 if (! header_printed) 839 { 840 char buf[100]; 841 842 sprintf (buf, _("Archive member included because of file (symbol)\n\n")); 843 minfo ("%s", buf); 844 header_printed = TRUE; 845 } 846 847 if (bfd_my_archive (abfd) == NULL) 848 { 849 minfo ("%s", bfd_get_filename (abfd)); 850 len = strlen (bfd_get_filename (abfd)); 851 } 852 else 853 { 854 minfo ("%s(%s)", bfd_get_filename (bfd_my_archive (abfd)), 855 bfd_get_filename (abfd)); 856 len = (strlen (bfd_get_filename (bfd_my_archive (abfd))) 857 + strlen (bfd_get_filename (abfd)) 858 + 2); 859 } 860 861 if (len >= 29) 862 { 863 print_nl (); 864 len = 0; 865 } 866 while (len < 30) 867 { 868 print_space (); 869 ++len; 870 } 871 872 if (from != NULL) 873 minfo ("%B ", from); 874 if (h != NULL) 875 minfo ("(%T)\n", h->root.string); 876 else 877 minfo ("(%s)\n", name); 878 } 879 880 if (trace_files || verbose) 881 info_msg ("%I\n", &orig_input); 882 return TRUE; 883 } 884 885 /* This is called when BFD has discovered a symbol which is defined 886 multiple times. */ 887 888 static bfd_boolean 889 multiple_definition (struct bfd_link_info *info, 890 struct bfd_link_hash_entry *h, 891 bfd *nbfd, 892 asection *nsec, 893 bfd_vma nval) 894 { 895 const char *name; 896 bfd *obfd; 897 asection *osec; 898 bfd_vma oval; 899 900 if (info->allow_multiple_definition) 901 return TRUE; 902 903 switch (h->type) 904 { 905 case bfd_link_hash_defined: 906 osec = h->u.def.section; 907 oval = h->u.def.value; 908 obfd = h->u.def.section->owner; 909 break; 910 case bfd_link_hash_indirect: 911 osec = bfd_ind_section_ptr; 912 oval = 0; 913 obfd = NULL; 914 break; 915 default: 916 abort (); 917 } 918 919 /* Ignore a redefinition of an absolute symbol to the 920 same value; it's harmless. */ 921 if (h->type == bfd_link_hash_defined 922 && bfd_is_abs_section (osec) 923 && bfd_is_abs_section (nsec) 924 && nval == oval) 925 return TRUE; 926 927 /* If either section has the output_section field set to 928 bfd_abs_section_ptr, it means that the section is being 929 discarded, and this is not really a multiple definition at all. 930 FIXME: It would be cleaner to somehow ignore symbols defined in 931 sections which are being discarded. */ 932 if ((osec->output_section != NULL 933 && ! bfd_is_abs_section (osec) 934 && bfd_is_abs_section (osec->output_section)) 935 || (nsec->output_section != NULL 936 && ! bfd_is_abs_section (nsec) 937 && bfd_is_abs_section (nsec->output_section))) 938 return TRUE; 939 940 name = h->root.string; 941 if (nbfd == NULL) 942 { 943 nbfd = obfd; 944 nsec = osec; 945 nval = oval; 946 obfd = NULL; 947 } 948 einfo (_("%X%C: multiple definition of `%T'\n"), 949 nbfd, nsec, nval, name); 950 if (obfd != NULL) 951 einfo (_("%D: first defined here\n"), obfd, osec, oval); 952 953 if (RELAXATION_ENABLED) 954 { 955 einfo (_("%P: Disabling relaxation: it will not work with multiple definitions\n")); 956 command_line.disable_target_specific_optimizations = -1; 957 } 958 959 return TRUE; 960 } 961 962 /* This is called when there is a definition of a common symbol, or 963 when a common symbol is found for a symbol that is already defined, 964 or when two common symbols are found. We only do something if 965 -warn-common was used. */ 966 967 static bfd_boolean 968 multiple_common (struct bfd_link_info *info ATTRIBUTE_UNUSED, 969 struct bfd_link_hash_entry *h, 970 bfd *nbfd, 971 enum bfd_link_hash_type ntype, 972 bfd_vma nsize) 973 { 974 const char *name; 975 bfd *obfd; 976 enum bfd_link_hash_type otype; 977 bfd_vma osize; 978 979 if (!config.warn_common) 980 return TRUE; 981 982 name = h->root.string; 983 otype = h->type; 984 if (otype == bfd_link_hash_common) 985 { 986 obfd = h->u.c.p->section->owner; 987 osize = h->u.c.size; 988 } 989 else if (otype == bfd_link_hash_defined 990 || otype == bfd_link_hash_defweak) 991 { 992 obfd = h->u.def.section->owner; 993 osize = 0; 994 } 995 else 996 { 997 /* FIXME: It would nice if we could report the BFD which defined 998 an indirect symbol, but we don't have anywhere to store the 999 information. */ 1000 obfd = NULL; 1001 osize = 0; 1002 } 1003 1004 if (ntype == bfd_link_hash_defined 1005 || ntype == bfd_link_hash_defweak 1006 || ntype == bfd_link_hash_indirect) 1007 { 1008 ASSERT (otype == bfd_link_hash_common); 1009 einfo (_("%B: warning: definition of `%T' overriding common\n"), 1010 nbfd, name); 1011 if (obfd != NULL) 1012 einfo (_("%B: warning: common is here\n"), obfd); 1013 } 1014 else if (otype == bfd_link_hash_defined 1015 || otype == bfd_link_hash_defweak 1016 || otype == bfd_link_hash_indirect) 1017 { 1018 ASSERT (ntype == bfd_link_hash_common); 1019 einfo (_("%B: warning: common of `%T' overridden by definition\n"), 1020 nbfd, name); 1021 if (obfd != NULL) 1022 einfo (_("%B: warning: defined here\n"), obfd); 1023 } 1024 else 1025 { 1026 ASSERT (otype == bfd_link_hash_common && ntype == bfd_link_hash_common); 1027 if (osize > nsize) 1028 { 1029 einfo (_("%B: warning: common of `%T' overridden by larger common\n"), 1030 nbfd, name); 1031 if (obfd != NULL) 1032 einfo (_("%B: warning: larger common is here\n"), obfd); 1033 } 1034 else if (nsize > osize) 1035 { 1036 einfo (_("%B: warning: common of `%T' overriding smaller common\n"), 1037 nbfd, name); 1038 if (obfd != NULL) 1039 einfo (_("%B: warning: smaller common is here\n"), obfd); 1040 } 1041 else 1042 { 1043 einfo (_("%B: warning: multiple common of `%T'\n"), nbfd, name); 1044 if (obfd != NULL) 1045 einfo (_("%B: warning: previous common is here\n"), obfd); 1046 } 1047 } 1048 1049 return TRUE; 1050 } 1051 1052 /* This is called when BFD has discovered a set element. H is the 1053 entry in the linker hash table for the set. SECTION and VALUE 1054 represent a value which should be added to the set. */ 1055 1056 static bfd_boolean 1057 add_to_set (struct bfd_link_info *info ATTRIBUTE_UNUSED, 1058 struct bfd_link_hash_entry *h, 1059 bfd_reloc_code_real_type reloc, 1060 bfd *abfd, 1061 asection *section, 1062 bfd_vma value) 1063 { 1064 if (config.warn_constructors) 1065 einfo (_("%P: warning: global constructor %s used\n"), 1066 h->root.string); 1067 1068 if (! config.build_constructors) 1069 return TRUE; 1070 1071 ldctor_add_set_entry (h, reloc, NULL, section, value); 1072 1073 if (h->type == bfd_link_hash_new) 1074 { 1075 h->type = bfd_link_hash_undefined; 1076 h->u.undef.abfd = abfd; 1077 /* We don't call bfd_link_add_undef to add this to the list of 1078 undefined symbols because we are going to define it 1079 ourselves. */ 1080 } 1081 1082 return TRUE; 1083 } 1084 1085 /* This is called when BFD has discovered a constructor. This is only 1086 called for some object file formats--those which do not handle 1087 constructors in some more clever fashion. This is similar to 1088 adding an element to a set, but less general. */ 1089 1090 static bfd_boolean 1091 constructor_callback (struct bfd_link_info *info, 1092 bfd_boolean constructor, 1093 const char *name, 1094 bfd *abfd, 1095 asection *section, 1096 bfd_vma value) 1097 { 1098 char *s; 1099 struct bfd_link_hash_entry *h; 1100 char set_name[1 + sizeof "__CTOR_LIST__"]; 1101 1102 if (config.warn_constructors) 1103 einfo (_("%P: warning: global constructor %s used\n"), name); 1104 1105 if (! config.build_constructors) 1106 return TRUE; 1107 1108 /* Ensure that BFD_RELOC_CTOR exists now, so that we can give a 1109 useful error message. */ 1110 if (bfd_reloc_type_lookup (link_info.output_bfd, BFD_RELOC_CTOR) == NULL 1111 && (info->relocatable 1112 || bfd_reloc_type_lookup (abfd, BFD_RELOC_CTOR) == NULL)) 1113 einfo (_("%P%F: BFD backend error: BFD_RELOC_CTOR unsupported\n")); 1114 1115 s = set_name; 1116 if (bfd_get_symbol_leading_char (abfd) != '\0') 1117 *s++ = bfd_get_symbol_leading_char (abfd); 1118 if (constructor) 1119 strcpy (s, "__CTOR_LIST__"); 1120 else 1121 strcpy (s, "__DTOR_LIST__"); 1122 1123 h = bfd_link_hash_lookup (info->hash, set_name, TRUE, TRUE, TRUE); 1124 if (h == (struct bfd_link_hash_entry *) NULL) 1125 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n")); 1126 if (h->type == bfd_link_hash_new) 1127 { 1128 h->type = bfd_link_hash_undefined; 1129 h->u.undef.abfd = abfd; 1130 /* We don't call bfd_link_add_undef to add this to the list of 1131 undefined symbols because we are going to define it 1132 ourselves. */ 1133 } 1134 1135 ldctor_add_set_entry (h, BFD_RELOC_CTOR, name, section, value); 1136 return TRUE; 1137 } 1138 1139 /* A structure used by warning_callback to pass information through 1140 bfd_map_over_sections. */ 1141 1142 struct warning_callback_info 1143 { 1144 bfd_boolean found; 1145 const char *warning; 1146 const char *symbol; 1147 asymbol **asymbols; 1148 }; 1149 1150 /* This is called when there is a reference to a warning symbol. */ 1151 1152 static bfd_boolean 1153 warning_callback (struct bfd_link_info *info ATTRIBUTE_UNUSED, 1154 const char *warning, 1155 const char *symbol, 1156 bfd *abfd, 1157 asection *section, 1158 bfd_vma address) 1159 { 1160 /* This is a hack to support warn_multiple_gp. FIXME: This should 1161 have a cleaner interface, but what? */ 1162 if (! config.warn_multiple_gp 1163 && strcmp (warning, "using multiple gp values") == 0) 1164 return TRUE; 1165 1166 if (section != NULL) 1167 einfo ("%C: %s%s\n", abfd, section, address, _("warning: "), warning); 1168 else if (abfd == NULL) 1169 einfo ("%P: %s%s\n", _("warning: "), warning); 1170 else if (symbol == NULL) 1171 einfo ("%B: %s%s\n", abfd, _("warning: "), warning); 1172 else 1173 { 1174 struct warning_callback_info cinfo; 1175 1176 /* Look through the relocs to see if we can find a plausible 1177 address. */ 1178 1179 if (!bfd_generic_link_read_symbols (abfd)) 1180 einfo (_("%B%F: could not read symbols: %E\n"), abfd); 1181 1182 cinfo.found = FALSE; 1183 cinfo.warning = warning; 1184 cinfo.symbol = symbol; 1185 cinfo.asymbols = bfd_get_outsymbols (abfd); 1186 bfd_map_over_sections (abfd, warning_find_reloc, &cinfo); 1187 1188 if (! cinfo.found) 1189 einfo ("%B: %s%s\n", abfd, _("warning: "), warning); 1190 } 1191 1192 return TRUE; 1193 } 1194 1195 /* This is called by warning_callback for each section. It checks the 1196 relocs of the section to see if it can find a reference to the 1197 symbol which triggered the warning. If it can, it uses the reloc 1198 to give an error message with a file and line number. */ 1199 1200 static void 1201 warning_find_reloc (bfd *abfd, asection *sec, void *iarg) 1202 { 1203 struct warning_callback_info *info = (struct warning_callback_info *) iarg; 1204 long relsize; 1205 arelent **relpp; 1206 long relcount; 1207 arelent **p, **pend; 1208 1209 if (info->found) 1210 return; 1211 1212 relsize = bfd_get_reloc_upper_bound (abfd, sec); 1213 if (relsize < 0) 1214 einfo (_("%B%F: could not read relocs: %E\n"), abfd); 1215 if (relsize == 0) 1216 return; 1217 1218 relpp = (arelent **) xmalloc (relsize); 1219 relcount = bfd_canonicalize_reloc (abfd, sec, relpp, info->asymbols); 1220 if (relcount < 0) 1221 einfo (_("%B%F: could not read relocs: %E\n"), abfd); 1222 1223 p = relpp; 1224 pend = p + relcount; 1225 for (; p < pend && *p != NULL; p++) 1226 { 1227 arelent *q = *p; 1228 1229 if (q->sym_ptr_ptr != NULL 1230 && *q->sym_ptr_ptr != NULL 1231 && strcmp (bfd_asymbol_name (*q->sym_ptr_ptr), info->symbol) == 0) 1232 { 1233 /* We found a reloc for the symbol we are looking for. */ 1234 einfo ("%C: %s%s\n", abfd, sec, q->address, _("warning: "), 1235 info->warning); 1236 info->found = TRUE; 1237 break; 1238 } 1239 } 1240 1241 free (relpp); 1242 } 1243 1244 /* This is called when an undefined symbol is found. */ 1245 1246 static bfd_boolean 1247 undefined_symbol (struct bfd_link_info *info ATTRIBUTE_UNUSED, 1248 const char *name, 1249 bfd *abfd, 1250 asection *section, 1251 bfd_vma address, 1252 bfd_boolean error) 1253 { 1254 static char *error_name; 1255 static unsigned int error_count; 1256 1257 #define MAX_ERRORS_IN_A_ROW 5 1258 1259 if (info->ignore_hash != NULL 1260 && bfd_hash_lookup (info->ignore_hash, name, FALSE, FALSE) != NULL) 1261 return TRUE; 1262 1263 if (config.warn_once) 1264 { 1265 /* Only warn once about a particular undefined symbol. */ 1266 if (info->ignore_hash == NULL) 1267 { 1268 info->ignore_hash = xmalloc (sizeof (struct bfd_hash_table)); 1269 if (!bfd_hash_table_init (info->ignore_hash, bfd_hash_newfunc, 1270 sizeof (struct bfd_hash_entry))) 1271 einfo (_("%F%P: bfd_hash_table_init failed: %E\n")); 1272 } 1273 1274 if (bfd_hash_lookup (info->ignore_hash, name, FALSE, FALSE) != NULL) 1275 return TRUE; 1276 1277 if (bfd_hash_lookup (info->ignore_hash, name, TRUE, TRUE) == NULL) 1278 einfo (_("%F%P: bfd_hash_lookup failed: %E\n")); 1279 } 1280 1281 /* We never print more than a reasonable number of errors in a row 1282 for a single symbol. */ 1283 if (error_name != NULL 1284 && strcmp (name, error_name) == 0) 1285 ++error_count; 1286 else 1287 { 1288 error_count = 0; 1289 if (error_name != NULL) 1290 free (error_name); 1291 error_name = xstrdup (name); 1292 } 1293 1294 if (section != NULL) 1295 { 1296 if (error_count < MAX_ERRORS_IN_A_ROW) 1297 { 1298 if (error) 1299 einfo (_("%X%C: undefined reference to `%T'\n"), 1300 abfd, section, address, name); 1301 else 1302 einfo (_("%C: warning: undefined reference to `%T'\n"), 1303 abfd, section, address, name); 1304 } 1305 else if (error_count == MAX_ERRORS_IN_A_ROW) 1306 { 1307 if (error) 1308 einfo (_("%X%D: more undefined references to `%T' follow\n"), 1309 abfd, section, address, name); 1310 else 1311 einfo (_("%D: warning: more undefined references to `%T' follow\n"), 1312 abfd, section, address, name); 1313 } 1314 else if (error) 1315 einfo ("%X"); 1316 } 1317 else 1318 { 1319 if (error_count < MAX_ERRORS_IN_A_ROW) 1320 { 1321 if (error) 1322 einfo (_("%X%B: undefined reference to `%T'\n"), 1323 abfd, name); 1324 else 1325 einfo (_("%B: warning: undefined reference to `%T'\n"), 1326 abfd, name); 1327 } 1328 else if (error_count == MAX_ERRORS_IN_A_ROW) 1329 { 1330 if (error) 1331 einfo (_("%X%B: more undefined references to `%T' follow\n"), 1332 abfd, name); 1333 else 1334 einfo (_("%B: warning: more undefined references to `%T' follow\n"), 1335 abfd, name); 1336 } 1337 else if (error) 1338 einfo ("%X"); 1339 } 1340 1341 return TRUE; 1342 } 1343 1344 /* Counter to limit the number of relocation overflow error messages 1345 to print. Errors are printed as it is decremented. When it's 1346 called and the counter is zero, a final message is printed 1347 indicating more relocations were omitted. When it gets to -1, no 1348 such errors are printed. If it's initially set to a value less 1349 than -1, all such errors will be printed (--verbose does this). */ 1350 1351 int overflow_cutoff_limit = 10; 1352 1353 /* This is called when a reloc overflows. */ 1354 1355 static bfd_boolean 1356 reloc_overflow (struct bfd_link_info *info ATTRIBUTE_UNUSED, 1357 struct bfd_link_hash_entry *entry, 1358 const char *name, 1359 const char *reloc_name, 1360 bfd_vma addend, 1361 bfd *abfd, 1362 asection *section, 1363 bfd_vma address) 1364 { 1365 if (overflow_cutoff_limit == -1) 1366 return TRUE; 1367 1368 einfo ("%X%H:", abfd, section, address); 1369 1370 if (overflow_cutoff_limit >= 0 1371 && overflow_cutoff_limit-- == 0) 1372 { 1373 einfo (_(" additional relocation overflows omitted from the output\n")); 1374 return TRUE; 1375 } 1376 1377 if (entry) 1378 { 1379 while (entry->type == bfd_link_hash_indirect 1380 || entry->type == bfd_link_hash_warning) 1381 entry = entry->u.i.link; 1382 switch (entry->type) 1383 { 1384 case bfd_link_hash_undefined: 1385 case bfd_link_hash_undefweak: 1386 einfo (_(" relocation truncated to fit: %s against undefined symbol `%T'"), 1387 reloc_name, entry->root.string); 1388 break; 1389 case bfd_link_hash_defined: 1390 case bfd_link_hash_defweak: 1391 einfo (_(" relocation truncated to fit: %s against symbol `%T' defined in %A section in %B"), 1392 reloc_name, entry->root.string, 1393 entry->u.def.section, 1394 entry->u.def.section == bfd_abs_section_ptr 1395 ? link_info.output_bfd : entry->u.def.section->owner); 1396 break; 1397 default: 1398 abort (); 1399 break; 1400 } 1401 } 1402 else 1403 einfo (_(" relocation truncated to fit: %s against `%T'"), 1404 reloc_name, name); 1405 if (addend != 0) 1406 einfo ("+%v", addend); 1407 einfo ("\n"); 1408 return TRUE; 1409 } 1410 1411 /* This is called when a dangerous relocation is made. */ 1412 1413 static bfd_boolean 1414 reloc_dangerous (struct bfd_link_info *info ATTRIBUTE_UNUSED, 1415 const char *message, 1416 bfd *abfd, 1417 asection *section, 1418 bfd_vma address) 1419 { 1420 einfo (_("%X%H: dangerous relocation: %s\n"), 1421 abfd, section, address, message); 1422 return TRUE; 1423 } 1424 1425 /* This is called when a reloc is being generated attached to a symbol 1426 that is not being output. */ 1427 1428 static bfd_boolean 1429 unattached_reloc (struct bfd_link_info *info ATTRIBUTE_UNUSED, 1430 const char *name, 1431 bfd *abfd, 1432 asection *section, 1433 bfd_vma address) 1434 { 1435 einfo (_("%X%H: reloc refers to symbol `%T' which is not being output\n"), 1436 abfd, section, address, name); 1437 return TRUE; 1438 } 1439 1440 /* This is called if link_info.notice_all is set, or when a symbol in 1441 link_info.notice_hash is found. Symbols are put in notice_hash 1442 using the -y option, while notice_all is set if the --cref option 1443 has been supplied, or if there are any NOCROSSREFS sections in the 1444 linker script; and if plugins are active, since they need to monitor 1445 all references from non-IR files. */ 1446 1447 static bfd_boolean 1448 notice (struct bfd_link_info *info, 1449 struct bfd_link_hash_entry *h, 1450 bfd *abfd, 1451 asection *section, 1452 bfd_vma value, 1453 flagword flags ATTRIBUTE_UNUSED, 1454 const char *string ATTRIBUTE_UNUSED) 1455 { 1456 const char *name; 1457 1458 if (h == NULL) 1459 { 1460 if (command_line.cref || nocrossref_list != NULL) 1461 return handle_asneeded_cref (abfd, (enum notice_asneeded_action) value); 1462 return TRUE; 1463 } 1464 1465 name = h->root.string; 1466 if (info->notice_hash != NULL 1467 && bfd_hash_lookup (info->notice_hash, name, FALSE, FALSE) != NULL) 1468 { 1469 if (bfd_is_und_section (section)) 1470 einfo ("%B: reference to %s\n", abfd, name); 1471 else 1472 einfo ("%B: definition of %s\n", abfd, name); 1473 } 1474 1475 if (command_line.cref || nocrossref_list != NULL) 1476 add_cref (name, abfd, section, value); 1477 1478 return TRUE; 1479 } 1480