1 /* Main program of GNU linker. 2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 3 2002, 2003, 2004 4 Free Software Foundation, Inc. 5 Written by Steve Chamberlain steve@cygnus.com 6 7 This file is part of GLD, the Gnu Linker. 8 9 GLD 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 2, or (at your option) 12 any later version. 13 14 GLD 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 GLD; see the file COPYING. If not, write to the Free 21 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 22 02111-1307, USA. */ 23 24 #include "bfd.h" 25 #include "sysdep.h" 26 #include <stdio.h> 27 #include "safe-ctype.h" 28 #include "libiberty.h" 29 #include "progress.h" 30 #include "bfdlink.h" 31 #include "filenames.h" 32 33 #include "ld.h" 34 #include "ldmain.h" 35 #include "ldmisc.h" 36 #include "ldwrite.h" 37 #include "ldexp.h" 38 #include "ldlang.h" 39 #include <ldgram.h> 40 #include "ldlex.h" 41 #include "ldfile.h" 42 #include "ldemul.h" 43 #include "ldctor.h" 44 45 /* Somewhere above, sys/stat.h got included. */ 46 #if !defined(S_ISDIR) && defined(S_IFDIR) 47 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) 48 #endif 49 50 #include <string.h> 51 52 #ifdef HAVE_SBRK 53 #ifdef NEED_DECLARATION_SBRK 54 extern void *sbrk (); 55 #endif 56 #endif 57 58 #ifndef TARGET_SYSTEM_ROOT 59 #define TARGET_SYSTEM_ROOT "" 60 #endif 61 62 /* EXPORTS */ 63 64 char *default_target; 65 const char *output_filename = "a.out"; 66 67 /* Name this program was invoked by. */ 68 char *program_name; 69 70 /* The prefix for system library directories. */ 71 char *ld_sysroot; 72 73 /* The canonical representation of ld_sysroot. */ 74 char * ld_canon_sysroot; 75 int ld_canon_sysroot_len; 76 77 /* The file that we're creating. */ 78 bfd *output_bfd = 0; 79 80 /* Set by -G argument, for MIPS ECOFF target. */ 81 int g_switch_value = 8; 82 83 /* Nonzero means print names of input files as processed. */ 84 bfd_boolean trace_files; 85 86 /* Nonzero means same, but note open failures, too. */ 87 bfd_boolean trace_file_tries; 88 89 /* Nonzero means version number was printed, so exit successfully 90 instead of complaining if no input files are given. */ 91 bfd_boolean version_printed; 92 93 /* Nonzero means link in every member of an archive. */ 94 bfd_boolean whole_archive; 95 96 /* Nonzero means create DT_NEEDED entries only if a dynamic library 97 actually satisfies some reference in a regular object. */ 98 bfd_boolean as_needed; 99 100 /* TRUE if we should demangle symbol names. */ 101 bfd_boolean demangling; 102 103 args_type command_line; 104 105 ld_config_type config; 106 107 static char *get_emulation 108 (int, char **); 109 static void set_scripts_dir 110 (void); 111 static bfd_boolean add_archive_element 112 (struct bfd_link_info *, bfd *, const char *); 113 static bfd_boolean multiple_definition 114 (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma, 115 bfd *, asection *, bfd_vma); 116 static bfd_boolean multiple_common 117 (struct bfd_link_info *, const char *, bfd *, enum bfd_link_hash_type, 118 bfd_vma, 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 *, const char *, const char *, bfd_vma, 135 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 *, const char *, bfd *, asection *, bfd_vma); 142 143 static struct bfd_link_callbacks link_callbacks = 144 { 145 add_archive_element, 146 multiple_definition, 147 multiple_common, 148 add_to_set, 149 constructor_callback, 150 warning_callback, 151 undefined_symbol, 152 reloc_overflow, 153 reloc_dangerous, 154 unattached_reloc, 155 notice, 156 error_handler 157 }; 158 159 struct bfd_link_info link_info; 160 161 static void 162 remove_output (void) 163 { 164 if (output_filename) 165 { 166 if (output_bfd) 167 bfd_cache_close (output_bfd); 168 if (delete_output_file_on_failure) 169 unlink (output_filename); 170 } 171 } 172 173 int 174 main (int argc, char **argv) 175 { 176 char *emulation; 177 long start_time = get_run_time (); 178 179 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES) 180 setlocale (LC_MESSAGES, ""); 181 #endif 182 #if defined (HAVE_SETLOCALE) 183 setlocale (LC_CTYPE, ""); 184 #endif 185 bindtextdomain (PACKAGE, LOCALEDIR); 186 textdomain (PACKAGE); 187 188 program_name = argv[0]; 189 xmalloc_set_program_name (program_name); 190 191 START_PROGRESS (program_name, 0); 192 193 bfd_init (); 194 195 bfd_set_error_program_name (program_name); 196 197 xatexit (remove_output); 198 199 #ifdef TARGET_SYSTEM_ROOT_RELOCATABLE 200 ld_sysroot = make_relative_prefix (program_name, BINDIR, 201 TARGET_SYSTEM_ROOT); 202 203 if (ld_sysroot) 204 { 205 struct stat s; 206 int res = stat (ld_sysroot, &s) == 0 && S_ISDIR (s.st_mode); 207 208 if (!res) 209 { 210 free (ld_sysroot); 211 ld_sysroot = NULL; 212 } 213 } 214 215 if (! ld_sysroot) 216 { 217 ld_sysroot = make_relative_prefix (program_name, TOOLBINDIR, 218 TARGET_SYSTEM_ROOT); 219 220 if (ld_sysroot) 221 { 222 struct stat s; 223 int res = stat (ld_sysroot, &s) == 0 && S_ISDIR (s.st_mode); 224 225 if (!res) 226 { 227 free (ld_sysroot); 228 ld_sysroot = NULL; 229 } 230 } 231 } 232 233 if (! ld_sysroot) 234 #endif 235 ld_sysroot = TARGET_SYSTEM_ROOT; 236 237 if (ld_sysroot && *ld_sysroot) 238 ld_canon_sysroot = lrealpath (ld_sysroot); 239 240 if (ld_canon_sysroot) 241 ld_canon_sysroot_len = strlen (ld_canon_sysroot); 242 else 243 ld_canon_sysroot_len = -1; 244 245 /* Set the default BFD target based on the configured target. Doing 246 this permits the linker to be configured for a particular target, 247 and linked against a shared BFD library which was configured for 248 a different target. The macro TARGET is defined by Makefile. */ 249 if (! bfd_set_default_target (TARGET)) 250 { 251 einfo (_("%X%P: can't set BFD default target to `%s': %E\n"), TARGET); 252 xexit (1); 253 } 254 255 #if YYDEBUG 256 { 257 extern int yydebug; 258 yydebug = 1; 259 } 260 #endif 261 262 /* Initialize the data about options. */ 263 trace_files = trace_file_tries = version_printed = FALSE; 264 whole_archive = FALSE; 265 config.build_constructors = TRUE; 266 config.dynamic_link = FALSE; 267 config.has_shared = FALSE; 268 config.split_by_reloc = (unsigned) -1; 269 config.split_by_file = (bfd_size_type) -1; 270 command_line.force_common_definition = FALSE; 271 command_line.inhibit_common_definition = FALSE; 272 command_line.interpreter = NULL; 273 command_line.rpath = NULL; 274 command_line.warn_mismatch = TRUE; 275 command_line.check_section_addresses = TRUE; 276 command_line.accept_unknown_input_arch = FALSE; 277 278 /* We initialize DEMANGLING based on the environment variable 279 COLLECT_NO_DEMANGLE. The gcc collect2 program will demangle the 280 output of the linker, unless COLLECT_NO_DEMANGLE is set in the 281 environment. Acting the same way here lets us provide the same 282 interface by default. */ 283 demangling = getenv ("COLLECT_NO_DEMANGLE") == NULL; 284 285 link_info.relocatable = FALSE; 286 link_info.emitrelocations = FALSE; 287 link_info.task_link = FALSE; 288 link_info.shared = FALSE; 289 link_info.pie = FALSE; 290 link_info.executable = FALSE; 291 link_info.symbolic = FALSE; 292 link_info.export_dynamic = FALSE; 293 link_info.static_link = FALSE; 294 link_info.traditional_format = FALSE; 295 link_info.optimize = FALSE; 296 link_info.unresolved_syms_in_objects = RM_NOT_YET_SET; 297 link_info.unresolved_syms_in_shared_libs = RM_NOT_YET_SET; 298 link_info.allow_multiple_definition = FALSE; 299 link_info.allow_undefined_version = TRUE; 300 link_info.keep_memory = TRUE; 301 link_info.notice_all = FALSE; 302 link_info.nocopyreloc = FALSE; 303 link_info.new_dtags = FALSE; 304 link_info.combreloc = TRUE; 305 link_info.eh_frame_hdr = FALSE; 306 link_info.strip_discarded = TRUE; 307 link_info.strip = strip_none; 308 link_info.discard = discard_sec_merge; 309 link_info.common_skip_ar_aymbols = bfd_link_common_skip_none; 310 link_info.callbacks = &link_callbacks; 311 link_info.hash = NULL; 312 link_info.keep_hash = NULL; 313 link_info.notice_hash = NULL; 314 link_info.wrap_hash = NULL; 315 link_info.input_bfds = NULL; 316 link_info.create_object_symbols_section = NULL; 317 link_info.gc_sym_list = NULL; 318 link_info.base_file = NULL; 319 /* SVR4 linkers seem to set DT_INIT and DT_FINI based on magic _init 320 and _fini symbols. We are compatible. */ 321 link_info.init_function = "_init"; 322 link_info.fini_function = "_fini"; 323 link_info.pei386_auto_import = -1; 324 link_info.pei386_runtime_pseudo_reloc = FALSE; 325 link_info.spare_dynamic_tags = 5; 326 link_info.flags = 0; 327 link_info.flags_1 = 0; 328 link_info.need_relax_finalize = FALSE; 329 330 ldfile_add_arch (""); 331 332 config.make_executable = TRUE; 333 force_make_executable = FALSE; 334 config.magic_demand_paged = TRUE; 335 config.text_read_only = TRUE; 336 config.data_bss_contig = FALSE; 337 338 emulation = get_emulation (argc, argv); 339 ldemul_choose_mode (emulation); 340 default_target = ldemul_choose_target (argc, argv); 341 lang_init (); 342 ldemul_before_parse (); 343 lang_has_input_file = FALSE; 344 parse_args (argc, argv); 345 346 ldemul_set_symbols (); 347 348 if (link_info.relocatable) 349 { 350 if (command_line.gc_sections) 351 einfo ("%P%F: --gc-sections and -r may not be used together\n"); 352 else if (command_line.relax) 353 einfo (_("%P%F: --relax and -r may not be used together\n")); 354 if (link_info.shared) 355 einfo (_("%P%F: -r and -shared may not be used together\n")); 356 } 357 358 if (! link_info.shared) 359 { 360 if (command_line.filter_shlib) 361 einfo (_("%P%F: -F may not be used without -shared\n")); 362 if (command_line.auxiliary_filters) 363 einfo (_("%P%F: -f may not be used without -shared\n")); 364 } 365 366 if (! link_info.shared || link_info.pie) 367 link_info.executable = TRUE; 368 369 /* Treat ld -r -s as ld -r -S -x (i.e., strip all local symbols). I 370 don't see how else this can be handled, since in this case we 371 must preserve all externally visible symbols. */ 372 if (link_info.relocatable && link_info.strip == strip_all) 373 { 374 link_info.strip = strip_debugger; 375 if (link_info.discard == discard_sec_merge) 376 link_info.discard = discard_all; 377 } 378 379 /* This essentially adds another -L directory so this must be done after 380 the -L's in argv have been processed. */ 381 set_scripts_dir (); 382 383 /* If we have not already opened and parsed a linker script 384 read the emulation's appropriate default script. */ 385 if (saved_script_handle == NULL) 386 { 387 int isfile; 388 char *s = ldemul_get_script (&isfile); 389 390 if (isfile) 391 ldfile_open_command_file (s); 392 else 393 { 394 lex_string = s; 395 lex_redirect (s); 396 } 397 parser_input = input_script; 398 yyparse (); 399 lex_string = NULL; 400 } 401 402 if (trace_file_tries) 403 { 404 if (saved_script_handle) 405 info_msg (_("using external linker script:")); 406 else 407 info_msg (_("using internal linker script:")); 408 info_msg ("\n==================================================\n"); 409 410 if (saved_script_handle) 411 { 412 static const int ld_bufsz = 8193; 413 size_t n; 414 char *buf = xmalloc (ld_bufsz); 415 416 rewind (saved_script_handle); 417 while ((n = fread (buf, 1, ld_bufsz - 1, saved_script_handle)) > 0) 418 { 419 buf[n] = 0; 420 info_msg (buf); 421 } 422 rewind (saved_script_handle); 423 free (buf); 424 } 425 else 426 { 427 int isfile; 428 429 info_msg (ldemul_get_script (&isfile)); 430 } 431 432 info_msg ("\n==================================================\n"); 433 } 434 435 lang_final (); 436 437 if (!lang_has_input_file) 438 { 439 if (version_printed) 440 xexit (0); 441 einfo (_("%P%F: no input files\n")); 442 } 443 444 if (trace_files) 445 info_msg (_("%P: mode %s\n"), emulation); 446 447 ldemul_after_parse (); 448 449 if (config.map_filename) 450 { 451 if (strcmp (config.map_filename, "-") == 0) 452 { 453 config.map_file = stdout; 454 } 455 else 456 { 457 config.map_file = fopen (config.map_filename, FOPEN_WT); 458 if (config.map_file == (FILE *) NULL) 459 { 460 bfd_set_error (bfd_error_system_call); 461 einfo (_("%P%F: cannot open map file %s: %E\n"), 462 config.map_filename); 463 } 464 } 465 } 466 467 lang_process (); 468 469 /* Print error messages for any missing symbols, for any warning 470 symbols, and possibly multiple definitions. */ 471 if (link_info.relocatable) 472 output_bfd->flags &= ~EXEC_P; 473 else 474 output_bfd->flags |= EXEC_P; 475 476 ldwrite (); 477 478 if (config.map_file != NULL) 479 lang_map (); 480 if (command_line.cref) 481 output_cref (config.map_file != NULL ? config.map_file : stdout); 482 if (nocrossref_list != NULL) 483 check_nocrossrefs (); 484 485 /* Even if we're producing relocatable output, some non-fatal errors should 486 be reported in the exit status. (What non-fatal errors, if any, do we 487 want to ignore for relocatable output?) */ 488 if (!config.make_executable && !force_make_executable) 489 { 490 if (trace_files) 491 einfo (_("%P: link errors found, deleting executable `%s'\n"), 492 output_filename); 493 494 /* The file will be removed by remove_output. */ 495 xexit (1); 496 } 497 else 498 { 499 if (! bfd_close (output_bfd)) 500 einfo (_("%F%B: final close failed: %E\n"), output_bfd); 501 502 /* If the --force-exe-suffix is enabled, and we're making an 503 executable file and it doesn't end in .exe, copy it to one 504 which does. */ 505 if (! link_info.relocatable && command_line.force_exe_suffix) 506 { 507 int len = strlen (output_filename); 508 509 if (len < 4 510 || (strcasecmp (output_filename + len - 4, ".exe") != 0 511 && strcasecmp (output_filename + len - 4, ".dll") != 0)) 512 { 513 FILE *src; 514 FILE *dst; 515 const int bsize = 4096; 516 char *buf = xmalloc (bsize); 517 int l; 518 char *dst_name = xmalloc (len + 5); 519 520 strcpy (dst_name, output_filename); 521 strcat (dst_name, ".exe"); 522 src = fopen (output_filename, FOPEN_RB); 523 dst = fopen (dst_name, FOPEN_WB); 524 525 if (!src) 526 einfo (_("%X%P: unable to open for source of copy `%s'\n"), 527 output_filename); 528 if (!dst) 529 einfo (_("%X%P: unable to open for destination of copy `%s'\n"), 530 dst_name); 531 while ((l = fread (buf, 1, bsize, src)) > 0) 532 { 533 int done = fwrite (buf, 1, l, dst); 534 535 if (done != l) 536 einfo (_("%P: Error writing file `%s'\n"), dst_name); 537 } 538 539 fclose (src); 540 if (fclose (dst) == EOF) 541 einfo (_("%P: Error closing file `%s'\n"), dst_name); 542 free (dst_name); 543 free (buf); 544 } 545 } 546 } 547 548 END_PROGRESS (program_name); 549 550 if (config.stats) 551 { 552 #ifdef HAVE_SBRK 553 char *lim = sbrk (0); 554 #endif 555 long run_time = get_run_time () - start_time; 556 557 fprintf (stderr, _("%s: total time in link: %ld.%06ld\n"), 558 program_name, run_time / 1000000, run_time % 1000000); 559 #ifdef HAVE_SBRK 560 fprintf (stderr, _("%s: data size %ld\n"), program_name, 561 (long) (lim - (char *) &environ)); 562 #endif 563 } 564 565 /* Prevent remove_output from doing anything, after a successful link. */ 566 output_filename = NULL; 567 568 xexit (0); 569 return 0; 570 } 571 572 /* We need to find any explicitly given emulation in order to initialize the 573 state that's needed by the lex&yacc argument parser (parse_args). */ 574 575 static char * 576 get_emulation (int argc, char **argv) 577 { 578 char *emulation; 579 int i; 580 581 emulation = getenv (EMULATION_ENVIRON); 582 if (emulation == NULL) 583 emulation = DEFAULT_EMULATION; 584 585 for (i = 1; i < argc; i++) 586 { 587 if (!strncmp (argv[i], "-m", 2)) 588 { 589 if (argv[i][2] == '\0') 590 { 591 /* -m EMUL */ 592 if (i < argc - 1) 593 { 594 emulation = argv[i + 1]; 595 i++; 596 } 597 else 598 einfo (_("%P%F: missing argument to -m\n")); 599 } 600 else if (strcmp (argv[i], "-mips1") == 0 601 || strcmp (argv[i], "-mips2") == 0 602 || strcmp (argv[i], "-mips3") == 0 603 || strcmp (argv[i], "-mips4") == 0 604 || strcmp (argv[i], "-mips5") == 0 605 || strcmp (argv[i], "-mips32") == 0 606 || strcmp (argv[i], "-mips32r2") == 0 607 || strcmp (argv[i], "-mips64") == 0 608 || strcmp (argv[i], "-mips64r2") == 0) 609 { 610 /* FIXME: The arguments -mips1, -mips2, -mips3, etc. are 611 passed to the linker by some MIPS compilers. They 612 generally tell the linker to use a slightly different 613 library path. Perhaps someday these should be 614 implemented as emulations; until then, we just ignore 615 the arguments and hope that nobody ever creates 616 emulations named ips1, ips2 or ips3. */ 617 } 618 else if (strcmp (argv[i], "-m486") == 0) 619 { 620 /* FIXME: The argument -m486 is passed to the linker on 621 some Linux systems. Hope that nobody creates an 622 emulation named 486. */ 623 } 624 else 625 { 626 /* -mEMUL */ 627 emulation = &argv[i][2]; 628 } 629 } 630 } 631 632 return emulation; 633 } 634 635 /* If directory DIR contains an "ldscripts" subdirectory, 636 add DIR to the library search path and return TRUE, 637 else return FALSE. */ 638 639 static bfd_boolean 640 check_for_scripts_dir (char *dir) 641 { 642 size_t dirlen; 643 char *buf; 644 struct stat s; 645 bfd_boolean res; 646 647 dirlen = strlen (dir); 648 /* sizeof counts the terminating NUL. */ 649 buf = xmalloc (dirlen + sizeof ("/ldscripts")); 650 sprintf (buf, "%s/ldscripts", dir); 651 652 res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode); 653 free (buf); 654 if (res) 655 ldfile_add_library_path (dir, FALSE); 656 return res; 657 } 658 659 /* Set the default directory for finding script files. 660 Libraries will be searched for here too, but that's ok. 661 We look for the "ldscripts" directory in: 662 663 SCRIPTDIR (passed from Makefile) 664 (adjusted according to the current location of the binary) 665 SCRIPTDIR (passed from Makefile) 666 the dir where this program is (for using it from the build tree) 667 the dir where this program is/../lib 668 (for installing the tool suite elsewhere). */ 669 670 static void 671 set_scripts_dir (void) 672 { 673 char *end, *dir; 674 size_t dirlen; 675 bfd_boolean found; 676 677 dir = make_relative_prefix (program_name, BINDIR, SCRIPTDIR); 678 if (dir) 679 { 680 found = check_for_scripts_dir (dir); 681 free (dir); 682 if (found) 683 return; 684 } 685 686 dir = make_relative_prefix (program_name, TOOLBINDIR, SCRIPTDIR); 687 if (dir) 688 { 689 found = check_for_scripts_dir (dir); 690 free (dir); 691 if (found) 692 return; 693 } 694 695 if (check_for_scripts_dir (SCRIPTDIR)) 696 /* We've been installed normally. */ 697 return; 698 699 /* Look for "ldscripts" in the dir where our binary is. */ 700 end = strrchr (program_name, '/'); 701 #ifdef HAVE_DOS_BASED_FILE_SYSTEM 702 { 703 /* We could have \foo\bar, or /foo\bar. */ 704 char *bslash = strrchr (program_name, '\\'); 705 706 if (end == NULL || (bslash != NULL && bslash > end)) 707 end = bslash; 708 } 709 #endif 710 711 if (end == NULL) 712 /* Don't look for ldscripts in the current directory. There is 713 too much potential for confusion. */ 714 return; 715 716 dirlen = end - program_name; 717 /* Make a copy of program_name in dir. 718 Leave room for later "/../lib". */ 719 dir = xmalloc (dirlen + 8); 720 strncpy (dir, program_name, dirlen); 721 dir[dirlen] = '\0'; 722 723 if (check_for_scripts_dir (dir)) 724 { 725 free (dir); 726 return; 727 } 728 729 /* Look for "ldscripts" in <the dir where our binary is>/../lib. */ 730 strcpy (dir + dirlen, "/../lib"); 731 check_for_scripts_dir (dir); 732 free (dir); 733 } 734 735 void 736 add_ysym (const char *name) 737 { 738 if (link_info.notice_hash == NULL) 739 { 740 link_info.notice_hash = xmalloc (sizeof (struct bfd_hash_table)); 741 if (! bfd_hash_table_init_n (link_info.notice_hash, 742 bfd_hash_newfunc, 743 61)) 744 einfo (_("%P%F: bfd_hash_table_init failed: %E\n")); 745 } 746 747 if (bfd_hash_lookup (link_info.notice_hash, name, TRUE, TRUE) == NULL) 748 einfo (_("%P%F: bfd_hash_lookup failed: %E\n")); 749 } 750 751 /* Record a symbol to be wrapped, from the --wrap option. */ 752 753 void 754 add_wrap (const char *name) 755 { 756 if (link_info.wrap_hash == NULL) 757 { 758 link_info.wrap_hash = xmalloc (sizeof (struct bfd_hash_table)); 759 if (! bfd_hash_table_init_n (link_info.wrap_hash, 760 bfd_hash_newfunc, 761 61)) 762 einfo (_("%P%F: bfd_hash_table_init failed: %E\n")); 763 } 764 765 if (bfd_hash_lookup (link_info.wrap_hash, name, TRUE, TRUE) == NULL) 766 einfo (_("%P%F: bfd_hash_lookup failed: %E\n")); 767 } 768 769 /* Handle the -retain-symbols-file option. */ 770 771 void 772 add_keepsyms_file (const char *filename) 773 { 774 FILE *file; 775 char *buf; 776 size_t bufsize; 777 int c; 778 779 if (link_info.strip == strip_some) 780 einfo (_("%X%P: error: duplicate retain-symbols-file\n")); 781 782 file = fopen (filename, "r"); 783 if (file == NULL) 784 { 785 bfd_set_error (bfd_error_system_call); 786 einfo ("%X%P: %s: %E\n", filename); 787 return; 788 } 789 790 link_info.keep_hash = xmalloc (sizeof (struct bfd_hash_table)); 791 if (! bfd_hash_table_init (link_info.keep_hash, bfd_hash_newfunc)) 792 einfo (_("%P%F: bfd_hash_table_init failed: %E\n")); 793 794 bufsize = 100; 795 buf = xmalloc (bufsize); 796 797 c = getc (file); 798 while (c != EOF) 799 { 800 while (ISSPACE (c)) 801 c = getc (file); 802 803 if (c != EOF) 804 { 805 size_t len = 0; 806 807 while (! ISSPACE (c) && c != EOF) 808 { 809 buf[len] = c; 810 ++len; 811 if (len >= bufsize) 812 { 813 bufsize *= 2; 814 buf = xrealloc (buf, bufsize); 815 } 816 c = getc (file); 817 } 818 819 buf[len] = '\0'; 820 821 if (bfd_hash_lookup (link_info.keep_hash, buf, TRUE, TRUE) == NULL) 822 einfo (_("%P%F: bfd_hash_lookup for insertion failed: %E\n")); 823 } 824 } 825 826 if (link_info.strip != strip_none) 827 einfo (_("%P: `-retain-symbols-file' overrides `-s' and `-S'\n")); 828 829 free (buf); 830 link_info.strip = strip_some; 831 } 832 833 /* Callbacks from the BFD linker routines. */ 834 835 /* This is called when BFD has decided to include an archive member in 836 a link. */ 837 838 static bfd_boolean 839 add_archive_element (struct bfd_link_info *info ATTRIBUTE_UNUSED, 840 bfd *abfd, 841 const char *name) 842 { 843 lang_input_statement_type *input; 844 845 input = xmalloc (sizeof (lang_input_statement_type)); 846 input->filename = abfd->filename; 847 input->local_sym_name = abfd->filename; 848 input->the_bfd = abfd; 849 input->asymbols = NULL; 850 input->next = NULL; 851 input->just_syms_flag = FALSE; 852 input->loaded = FALSE; 853 input->search_dirs_flag = FALSE; 854 855 /* FIXME: The following fields are not set: header.next, 856 header.type, closed, passive_position, symbol_count, 857 next_real_file, is_archive, target, real. This bit of code is 858 from the old decode_library_subfile function. I don't know 859 whether any of those fields matters. */ 860 861 ldlang_add_file (input); 862 863 if (config.map_file != NULL) 864 { 865 static bfd_boolean header_printed; 866 struct bfd_link_hash_entry *h; 867 bfd *from; 868 int len; 869 870 h = bfd_link_hash_lookup (link_info.hash, name, FALSE, FALSE, TRUE); 871 872 if (h == NULL) 873 from = NULL; 874 else 875 { 876 switch (h->type) 877 { 878 default: 879 from = NULL; 880 break; 881 882 case bfd_link_hash_defined: 883 case bfd_link_hash_defweak: 884 from = h->u.def.section->owner; 885 break; 886 887 case bfd_link_hash_undefined: 888 case bfd_link_hash_undefweak: 889 from = h->u.undef.abfd; 890 break; 891 892 case bfd_link_hash_common: 893 from = h->u.c.p->section->owner; 894 break; 895 } 896 } 897 898 if (! header_printed) 899 { 900 char buf[100]; 901 902 sprintf (buf, _("Archive member included because of file (symbol)\n\n")); 903 minfo ("%s", buf); 904 header_printed = TRUE; 905 } 906 907 if (bfd_my_archive (abfd) == NULL) 908 { 909 minfo ("%s", bfd_get_filename (abfd)); 910 len = strlen (bfd_get_filename (abfd)); 911 } 912 else 913 { 914 minfo ("%s(%s)", bfd_get_filename (bfd_my_archive (abfd)), 915 bfd_get_filename (abfd)); 916 len = (strlen (bfd_get_filename (bfd_my_archive (abfd))) 917 + strlen (bfd_get_filename (abfd)) 918 + 2); 919 } 920 921 if (len >= 29) 922 { 923 print_nl (); 924 len = 0; 925 } 926 while (len < 30) 927 { 928 print_space (); 929 ++len; 930 } 931 932 if (from != NULL) 933 minfo ("%B ", from); 934 if (h != NULL) 935 minfo ("(%T)\n", h->root.string); 936 else 937 minfo ("(%s)\n", name); 938 } 939 940 if (trace_files || trace_file_tries) 941 info_msg ("%I\n", input); 942 943 return TRUE; 944 } 945 946 /* This is called when BFD has discovered a symbol which is defined 947 multiple times. */ 948 949 static bfd_boolean 950 multiple_definition (struct bfd_link_info *info ATTRIBUTE_UNUSED, 951 const char *name, 952 bfd *obfd, 953 asection *osec, 954 bfd_vma oval, 955 bfd *nbfd, 956 asection *nsec, 957 bfd_vma nval) 958 { 959 /* If either section has the output_section field set to 960 bfd_abs_section_ptr, it means that the section is being 961 discarded, and this is not really a multiple definition at all. 962 FIXME: It would be cleaner to somehow ignore symbols defined in 963 sections which are being discarded. */ 964 if ((osec->output_section != NULL 965 && ! bfd_is_abs_section (osec) 966 && bfd_is_abs_section (osec->output_section)) 967 || (nsec->output_section != NULL 968 && ! bfd_is_abs_section (nsec) 969 && bfd_is_abs_section (nsec->output_section))) 970 return TRUE; 971 972 einfo (_("%X%C: multiple definition of `%T'\n"), 973 nbfd, nsec, nval, name); 974 if (obfd != NULL) 975 einfo (_("%D: first defined here\n"), obfd, osec, oval); 976 977 if (command_line.relax) 978 { 979 einfo (_("%P: Disabling relaxation: it will not work with multiple definitions\n")); 980 command_line.relax = 0; 981 } 982 983 return TRUE; 984 } 985 986 /* This is called when there is a definition of a common symbol, or 987 when a common symbol is found for a symbol that is already defined, 988 or when two common symbols are found. We only do something if 989 -warn-common was used. */ 990 991 static bfd_boolean 992 multiple_common (struct bfd_link_info *info ATTRIBUTE_UNUSED, 993 const char *name, 994 bfd *obfd, 995 enum bfd_link_hash_type otype, 996 bfd_vma osize, 997 bfd *nbfd, 998 enum bfd_link_hash_type ntype, 999 bfd_vma nsize) 1000 { 1001 if (! config.warn_common) 1002 return TRUE; 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 (output_bfd, BFD_RELOC_CTOR) == NULL 1111 && (link_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\n", abfd, section, address, warning); 1168 else if (abfd == NULL) 1169 einfo ("%P: %s\n", warning); 1170 else if (symbol == NULL) 1171 einfo ("%B: %s\n", abfd, warning); 1172 else 1173 { 1174 lang_input_statement_type *entry; 1175 asymbol **asymbols; 1176 struct warning_callback_info info; 1177 1178 /* Look through the relocs to see if we can find a plausible 1179 address. */ 1180 entry = (lang_input_statement_type *) abfd->usrdata; 1181 if (entry != NULL && entry->asymbols != NULL) 1182 asymbols = entry->asymbols; 1183 else 1184 { 1185 long symsize; 1186 long symbol_count; 1187 1188 symsize = bfd_get_symtab_upper_bound (abfd); 1189 if (symsize < 0) 1190 einfo (_("%B%F: could not read symbols: %E\n"), abfd); 1191 asymbols = xmalloc (symsize); 1192 symbol_count = bfd_canonicalize_symtab (abfd, asymbols); 1193 if (symbol_count < 0) 1194 einfo (_("%B%F: could not read symbols: %E\n"), abfd); 1195 if (entry != NULL) 1196 { 1197 entry->asymbols = asymbols; 1198 entry->symbol_count = symbol_count; 1199 } 1200 } 1201 1202 info.found = FALSE; 1203 info.warning = warning; 1204 info.symbol = symbol; 1205 info.asymbols = asymbols; 1206 bfd_map_over_sections (abfd, warning_find_reloc, &info); 1207 1208 if (! info.found) 1209 einfo ("%B: %s\n", abfd, warning); 1210 1211 if (entry == NULL) 1212 free (asymbols); 1213 } 1214 1215 return TRUE; 1216 } 1217 1218 /* This is called by warning_callback for each section. It checks the 1219 relocs of the section to see if it can find a reference to the 1220 symbol which triggered the warning. If it can, it uses the reloc 1221 to give an error message with a file and line number. */ 1222 1223 static void 1224 warning_find_reloc (bfd *abfd, asection *sec, void *iarg) 1225 { 1226 struct warning_callback_info *info = iarg; 1227 long relsize; 1228 arelent **relpp; 1229 long relcount; 1230 arelent **p, **pend; 1231 1232 if (info->found) 1233 return; 1234 1235 relsize = bfd_get_reloc_upper_bound (abfd, sec); 1236 if (relsize < 0) 1237 einfo (_("%B%F: could not read relocs: %E\n"), abfd); 1238 if (relsize == 0) 1239 return; 1240 1241 relpp = xmalloc (relsize); 1242 relcount = bfd_canonicalize_reloc (abfd, sec, relpp, info->asymbols); 1243 if (relcount < 0) 1244 einfo (_("%B%F: could not read relocs: %E\n"), abfd); 1245 1246 p = relpp; 1247 pend = p + relcount; 1248 for (; p < pend && *p != NULL; p++) 1249 { 1250 arelent *q = *p; 1251 1252 if (q->sym_ptr_ptr != NULL 1253 && *q->sym_ptr_ptr != NULL 1254 && strcmp (bfd_asymbol_name (*q->sym_ptr_ptr), info->symbol) == 0) 1255 { 1256 /* We found a reloc for the symbol we are looking for. */ 1257 einfo ("%C: %s\n", abfd, sec, q->address, info->warning); 1258 info->found = TRUE; 1259 break; 1260 } 1261 } 1262 1263 free (relpp); 1264 } 1265 1266 /* This is called when an undefined symbol is found. */ 1267 1268 static bfd_boolean 1269 undefined_symbol (struct bfd_link_info *info ATTRIBUTE_UNUSED, 1270 const char *name, 1271 bfd *abfd, 1272 asection *section, 1273 bfd_vma address, 1274 bfd_boolean error) 1275 { 1276 static char *error_name; 1277 static unsigned int error_count; 1278 1279 #define MAX_ERRORS_IN_A_ROW 5 1280 1281 if (config.warn_once) 1282 { 1283 static struct bfd_hash_table *hash; 1284 1285 /* Only warn once about a particular undefined symbol. */ 1286 if (hash == NULL) 1287 { 1288 hash = xmalloc (sizeof (struct bfd_hash_table)); 1289 if (! bfd_hash_table_init (hash, bfd_hash_newfunc)) 1290 einfo (_("%F%P: bfd_hash_table_init failed: %E\n")); 1291 } 1292 1293 if (bfd_hash_lookup (hash, name, FALSE, FALSE) != NULL) 1294 return TRUE; 1295 1296 if (bfd_hash_lookup (hash, name, TRUE, TRUE) == NULL) 1297 einfo (_("%F%P: bfd_hash_lookup failed: %E\n")); 1298 } 1299 1300 /* We never print more than a reasonable number of errors in a row 1301 for a single symbol. */ 1302 if (error_name != NULL 1303 && strcmp (name, error_name) == 0) 1304 ++error_count; 1305 else 1306 { 1307 error_count = 0; 1308 if (error_name != NULL) 1309 free (error_name); 1310 error_name = xstrdup (name); 1311 } 1312 1313 if (section != NULL) 1314 { 1315 if (error_count < MAX_ERRORS_IN_A_ROW) 1316 { 1317 if (error) 1318 einfo (_("%X%C: undefined reference to `%T'\n"), 1319 abfd, section, address, name); 1320 else 1321 einfo (_("%C: warning: undefined reference to `%T'\n"), 1322 abfd, section, address, name); 1323 } 1324 else if (error_count == MAX_ERRORS_IN_A_ROW) 1325 { 1326 if (error) 1327 einfo (_("%X%D: more undefined references to `%T' follow\n"), 1328 abfd, section, address, name); 1329 else 1330 einfo (_("%D: warning: more undefined references to `%T' follow\n"), 1331 abfd, section, address, name); 1332 } 1333 else if (error) 1334 einfo ("%X"); 1335 } 1336 else 1337 { 1338 if (error_count < MAX_ERRORS_IN_A_ROW) 1339 { 1340 if (error) 1341 einfo (_("%X%B: undefined reference to `%T'\n"), 1342 abfd, name); 1343 else 1344 einfo (_("%B: warning: undefined reference to `%T'\n"), 1345 abfd, name); 1346 } 1347 else if (error_count == MAX_ERRORS_IN_A_ROW) 1348 { 1349 if (error) 1350 einfo (_("%X%B: more undefined references to `%T' follow\n"), 1351 abfd, name); 1352 else 1353 einfo (_("%B: warning: more undefined references to `%T' follow\n"), 1354 abfd, name); 1355 } 1356 else if (error) 1357 einfo ("%X"); 1358 } 1359 1360 return TRUE; 1361 } 1362 1363 /* Counter to limit the number of relocation overflow error messages 1364 to print. Errors are printed as it is decremented. When it's 1365 called and the counter is zero, a final message is printed 1366 indicating more relocations were omitted. When it gets to -1, no 1367 such errors are printed. If it's initially set to a value less 1368 than -1, all such errors will be printed (--verbose does this). */ 1369 1370 int overflow_cutoff_limit = 10; 1371 1372 /* This is called when a reloc overflows. */ 1373 1374 static bfd_boolean 1375 reloc_overflow (struct bfd_link_info *info ATTRIBUTE_UNUSED, 1376 const char *name, 1377 const char *reloc_name, 1378 bfd_vma addend, 1379 bfd *abfd, 1380 asection *section, 1381 bfd_vma address) 1382 { 1383 if (overflow_cutoff_limit == -1) 1384 return TRUE; 1385 1386 if (abfd == NULL) 1387 einfo (_("%P%X: generated")); 1388 else 1389 einfo ("%X%C:", abfd, section, address); 1390 1391 if (overflow_cutoff_limit >= 0 1392 && overflow_cutoff_limit-- == 0) 1393 { 1394 einfo (_(" additional relocation overflows omitted from the output\n")); 1395 return TRUE; 1396 } 1397 1398 einfo (_(" relocation truncated to fit: %s %T"), reloc_name, name); 1399 if (addend != 0) 1400 einfo ("+%v", addend); 1401 einfo ("\n"); 1402 return TRUE; 1403 } 1404 1405 /* This is called when a dangerous relocation is made. */ 1406 1407 static bfd_boolean 1408 reloc_dangerous (struct bfd_link_info *info ATTRIBUTE_UNUSED, 1409 const char *message, 1410 bfd *abfd, 1411 asection *section, 1412 bfd_vma address) 1413 { 1414 if (abfd == NULL) 1415 einfo (_("%P%X: generated")); 1416 else 1417 einfo ("%X%C:", abfd, section, address); 1418 einfo (_("dangerous relocation: %s\n"), message); 1419 return TRUE; 1420 } 1421 1422 /* This is called when a reloc is being generated attached to a symbol 1423 that is not being output. */ 1424 1425 static bfd_boolean 1426 unattached_reloc (struct bfd_link_info *info ATTRIBUTE_UNUSED, 1427 const char *name, 1428 bfd *abfd, 1429 asection *section, 1430 bfd_vma address) 1431 { 1432 if (abfd == NULL) 1433 einfo (_("%P%X: generated")); 1434 else 1435 einfo ("%X%C:", abfd, section, address); 1436 einfo (_(" reloc refers to symbol `%T' which is not being output\n"), 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. */ 1443 1444 static bfd_boolean 1445 notice (struct bfd_link_info *info, 1446 const char *name, 1447 bfd *abfd, 1448 asection *section, 1449 bfd_vma value) 1450 { 1451 if (! info->notice_all 1452 || (info->notice_hash != NULL 1453 && bfd_hash_lookup (info->notice_hash, name, FALSE, FALSE) != NULL)) 1454 { 1455 if (bfd_is_und_section (section)) 1456 einfo ("%B: reference to %s\n", abfd, name); 1457 else 1458 einfo ("%B: definition of %s\n", abfd, name); 1459 } 1460 1461 if (command_line.cref || nocrossref_list != NULL) 1462 add_cref (name, abfd, section, value); 1463 1464 return TRUE; 1465 } 1466