1 /* Main program of GNU linker. 2 Copyright (C) 1991, 92, 93, 94, 95, 96, 97, 98, 99, 2000 3 Free Software Foundation, Inc. 4 Written by Steve Chamberlain steve@cygnus.com 5 6 This file is part of GLD, the Gnu Linker. 7 8 GLD is free software; you can redistribute it and/or modify 9 it under the terms of the GNU General Public License as published by 10 the Free Software Foundation; either version 2, or (at your option) 11 any later version. 12 13 GLD is distributed in the hope that it will be useful, 14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 GNU General Public License for more details. 17 18 You should have received a copy of the GNU General Public License 19 along with GLD; see the file COPYING. If not, write to the Free 20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 21 02111-1307, USA. */ 22 23 #include "bfd.h" 24 #include "sysdep.h" 25 #include <stdio.h> 26 #include <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 "ldgram.h" 37 #include "ldexp.h" 38 #include "ldlang.h" 39 #include "ldemul.h" 40 #include "ldlex.h" 41 #include "ldfile.h" 42 #include "ldctor.h" 43 44 /* Somewhere above, sys/stat.h got included . . . . */ 45 #if !defined(S_ISDIR) && defined(S_IFDIR) 46 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) 47 #endif 48 49 #include <string.h> 50 51 #ifdef HAVE_SBRK 52 #ifdef NEED_DECLARATION_SBRK 53 extern PTR sbrk (); 54 #endif 55 #endif 56 57 static char *get_emulation PARAMS ((int, char **)); 58 static void set_scripts_dir PARAMS ((void)); 59 60 /* EXPORTS */ 61 62 char *default_target; 63 const char *output_filename = "a.out"; 64 65 /* Name this program was invoked by. */ 66 char *program_name; 67 68 /* The file that we're creating */ 69 bfd *output_bfd = 0; 70 71 /* Set by -G argument, for MIPS ECOFF target. */ 72 int g_switch_value = 8; 73 74 /* Nonzero means print names of input files as processed. */ 75 boolean trace_files; 76 77 /* Nonzero means same, but note open failures, too. */ 78 boolean trace_file_tries; 79 80 /* Nonzero means version number was printed, so exit successfully 81 instead of complaining if no input files are given. */ 82 boolean version_printed; 83 84 /* Nonzero means link in every member of an archive. */ 85 boolean whole_archive; 86 87 /* True if we should demangle symbol names. */ 88 boolean demangling; 89 90 args_type command_line; 91 92 ld_config_type config; 93 94 static void remove_output PARAMS ((void)); 95 static boolean check_for_scripts_dir PARAMS ((char *dir)); 96 static boolean add_archive_element PARAMS ((struct bfd_link_info *, bfd *, 97 const char *)); 98 static boolean multiple_definition PARAMS ((struct bfd_link_info *, 99 const char *, 100 bfd *, asection *, bfd_vma, 101 bfd *, asection *, bfd_vma)); 102 static boolean multiple_common PARAMS ((struct bfd_link_info *, 103 const char *, bfd *, 104 enum bfd_link_hash_type, bfd_vma, 105 bfd *, enum bfd_link_hash_type, 106 bfd_vma)); 107 static boolean add_to_set PARAMS ((struct bfd_link_info *, 108 struct bfd_link_hash_entry *, 109 bfd_reloc_code_real_type, 110 bfd *, asection *, bfd_vma)); 111 static boolean constructor_callback PARAMS ((struct bfd_link_info *, 112 boolean constructor, 113 const char *name, 114 bfd *, asection *, bfd_vma)); 115 static boolean warning_callback PARAMS ((struct bfd_link_info *, 116 const char *, const char *, bfd *, 117 asection *, bfd_vma)); 118 static void warning_find_reloc PARAMS ((bfd *, asection *, PTR)); 119 static boolean undefined_symbol PARAMS ((struct bfd_link_info *, 120 const char *, bfd *, 121 asection *, bfd_vma, boolean)); 122 static boolean reloc_overflow PARAMS ((struct bfd_link_info *, const char *, 123 const char *, bfd_vma, 124 bfd *, asection *, bfd_vma)); 125 static boolean reloc_dangerous PARAMS ((struct bfd_link_info *, const char *, 126 bfd *, asection *, bfd_vma)); 127 static boolean unattached_reloc PARAMS ((struct bfd_link_info *, 128 const char *, bfd *, asection *, 129 bfd_vma)); 130 static boolean notice PARAMS ((struct bfd_link_info *, const char *, 131 bfd *, asection *, bfd_vma)); 132 133 static struct bfd_link_callbacks link_callbacks = 134 { 135 add_archive_element, 136 multiple_definition, 137 multiple_common, 138 add_to_set, 139 constructor_callback, 140 warning_callback, 141 undefined_symbol, 142 reloc_overflow, 143 reloc_dangerous, 144 unattached_reloc, 145 notice 146 }; 147 148 struct bfd_link_info link_info; 149 150 static void 151 remove_output () 152 { 153 if (output_filename) 154 { 155 if (output_bfd && output_bfd->iostream) 156 fclose((FILE *)(output_bfd->iostream)); 157 if (delete_output_file_on_failure) 158 unlink (output_filename); 159 } 160 } 161 162 int 163 main (argc, argv) 164 int argc; 165 char **argv; 166 { 167 char *emulation; 168 long start_time = get_run_time (); 169 170 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES) 171 setlocale (LC_MESSAGES, ""); 172 #endif 173 bindtextdomain (PACKAGE, LOCALEDIR); 174 textdomain (PACKAGE); 175 176 program_name = argv[0]; 177 xmalloc_set_program_name (program_name); 178 179 START_PROGRESS (program_name, 0); 180 181 bfd_init (); 182 183 bfd_set_error_program_name (program_name); 184 185 xatexit (remove_output); 186 187 /* Set the default BFD target based on the configured target. Doing 188 this permits the linker to be configured for a particular target, 189 and linked against a shared BFD library which was configured for 190 a different target. The macro TARGET is defined by Makefile. */ 191 if (! bfd_set_default_target (TARGET)) 192 { 193 einfo (_("%X%P: can't set BFD default target to `%s': %E\n"), TARGET); 194 xexit (1); 195 } 196 197 /* Initialize the data about options. */ 198 trace_files = trace_file_tries = version_printed = false; 199 whole_archive = false; 200 config.build_constructors = true; 201 config.dynamic_link = false; 202 config.has_shared = false; 203 command_line.force_common_definition = false; 204 command_line.interpreter = NULL; 205 command_line.rpath = NULL; 206 command_line.warn_mismatch = true; 207 command_line.check_section_addresses = true; 208 209 /* We initialize DEMANGLING based on the environment variable 210 COLLECT_NO_DEMANGLE. The gcc collect2 program will demangle the 211 output of the linker, unless COLLECT_NO_DEMANGLE is set in the 212 environment. Acting the same way here lets us provide the same 213 interface by default. */ 214 demangling = getenv ("COLLECT_NO_DEMANGLE") == NULL; 215 216 link_info.callbacks = &link_callbacks; 217 link_info.relocateable = false; 218 link_info.shared = false; 219 link_info.symbolic = false; 220 link_info.static_link = false; 221 link_info.traditional_format = false; 222 link_info.optimize = false; 223 link_info.no_undefined = false; 224 link_info.strip = strip_none; 225 link_info.discard = discard_none; 226 link_info.keep_memory = true; 227 link_info.input_bfds = NULL; 228 link_info.create_object_symbols_section = NULL; 229 link_info.hash = NULL; 230 link_info.keep_hash = NULL; 231 link_info.notice_all = false; 232 link_info.notice_hash = NULL; 233 link_info.wrap_hash = NULL; 234 link_info.mpc860c0 = 0; 235 /* SVR4 linkers seem to set DT_INIT and DT_FINI based on magic _init 236 and _fini symbols. We are compatible. */ 237 link_info.init_function = "_init"; 238 link_info.fini_function = "_fini"; 239 240 ldfile_add_arch (""); 241 242 config.make_executable = true; 243 force_make_executable = false; 244 config.magic_demand_paged = true; 245 config.text_read_only = true; 246 config.make_executable = true; 247 248 emulation = get_emulation (argc, argv); 249 ldemul_choose_mode (emulation); 250 default_target = ldemul_choose_target (); 251 lang_init (); 252 ldemul_before_parse (); 253 lang_has_input_file = false; 254 parse_args (argc, argv); 255 256 ldemul_set_symbols (); 257 258 if (link_info.relocateable) 259 { 260 if (command_line.gc_sections) 261 einfo ("%P%F: --gc-sections and -r may not be used together\n"); 262 if (link_info.mpc860c0) 263 einfo (_("%P%F: -r and --mpc860c0 may not be used together\n")); 264 else if (command_line.relax) 265 einfo (_("%P%F: --relax and -r may not be used together\n")); 266 if (link_info.shared) 267 einfo (_("%P%F: -r and -shared may not be used together\n")); 268 } 269 270 /* Treat ld -r -s as ld -r -S -x (i.e., strip all local symbols). I 271 don't see how else this can be handled, since in this case we 272 must preserve all externally visible symbols. */ 273 if (link_info.relocateable && link_info.strip == strip_all) 274 { 275 link_info.strip = strip_debugger; 276 if (link_info.discard == discard_none) 277 link_info.discard = discard_all; 278 } 279 280 /* This essentially adds another -L directory so this must be done after 281 the -L's in argv have been processed. */ 282 set_scripts_dir (); 283 284 if (had_script == false) 285 { 286 /* Read the emulation's appropriate default script. */ 287 int isfile; 288 char *s = ldemul_get_script (&isfile); 289 290 if (isfile) 291 ldfile_open_command_file (s); 292 else 293 { 294 if (trace_file_tries) 295 { 296 info_msg (_("using internal linker script:\n")); 297 info_msg ("==================================================\n"); 298 info_msg (s); 299 info_msg ("\n==================================================\n"); 300 } 301 lex_string = s; 302 lex_redirect (s); 303 } 304 parser_input = input_script; 305 yyparse (); 306 lex_string = NULL; 307 } 308 309 lang_final (); 310 311 if (lang_has_input_file == false) 312 { 313 if (version_printed) 314 xexit (0); 315 einfo (_("%P%F: no input files\n")); 316 } 317 318 if (trace_files) 319 { 320 info_msg (_("%P: mode %s\n"), emulation); 321 } 322 323 ldemul_after_parse (); 324 325 326 if (config.map_filename) 327 { 328 if (strcmp (config.map_filename, "-") == 0) 329 { 330 config.map_file = stdout; 331 } 332 else 333 { 334 config.map_file = fopen (config.map_filename, FOPEN_WT); 335 if (config.map_file == (FILE *) NULL) 336 { 337 bfd_set_error (bfd_error_system_call); 338 einfo (_("%P%F: cannot open map file %s: %E\n"), 339 config.map_filename); 340 } 341 } 342 } 343 344 345 lang_process (); 346 347 /* Print error messages for any missing symbols, for any warning 348 symbols, and possibly multiple definitions */ 349 350 351 if (config.text_read_only) 352 { 353 /* Look for a text section and mark the readonly attribute in it */ 354 asection *found = bfd_get_section_by_name (output_bfd, ".text"); 355 356 if (found != (asection *) NULL) 357 { 358 found->flags |= SEC_READONLY; 359 } 360 } 361 362 if (link_info.relocateable) 363 output_bfd->flags &= ~EXEC_P; 364 else 365 output_bfd->flags |= EXEC_P; 366 367 ldwrite (); 368 369 if (config.map_file != NULL) 370 lang_map (); 371 if (command_line.cref) 372 output_cref (config.map_file != NULL ? config.map_file : stdout); 373 if (nocrossref_list != NULL) 374 check_nocrossrefs (); 375 376 /* Even if we're producing relocateable output, some non-fatal errors should 377 be reported in the exit status. (What non-fatal errors, if any, do we 378 want to ignore for relocateable output?) */ 379 380 if (config.make_executable == false && force_make_executable == false) 381 { 382 if (trace_files == true) 383 { 384 einfo (_("%P: link errors found, deleting executable `%s'\n"), 385 output_filename); 386 } 387 388 /* The file will be removed by remove_output. */ 389 390 xexit (1); 391 } 392 else 393 { 394 if (! bfd_close (output_bfd)) 395 einfo (_("%F%B: final close failed: %E\n"), output_bfd); 396 397 /* If the --force-exe-suffix is enabled, and we're making an 398 executable file and it doesn't end in .exe, copy it to one which does. */ 399 400 if (! link_info.relocateable && command_line.force_exe_suffix) 401 { 402 int len = strlen (output_filename); 403 if (len < 4 404 || (strcasecmp (output_filename + len - 4, ".exe") != 0 405 && strcasecmp (output_filename + len - 4, ".dll") != 0)) 406 { 407 FILE *src; 408 FILE *dst; 409 const int bsize = 4096; 410 char *buf = xmalloc (bsize); 411 int l; 412 char *dst_name = xmalloc (len + 5); 413 strcpy (dst_name, output_filename); 414 strcat (dst_name, ".exe"); 415 src = fopen (output_filename, FOPEN_RB); 416 dst = fopen (dst_name, FOPEN_WB); 417 418 if (!src) 419 einfo (_("%X%P: unable to open for source of copy `%s'\n"), output_filename); 420 if (!dst) 421 einfo (_("%X%P: unable to open for destination of copy `%s'\n"), dst_name); 422 while ((l = fread (buf, 1, bsize, src)) > 0) 423 { 424 int done = fwrite (buf, 1, l, dst); 425 if (done != l) 426 { 427 einfo (_("%P: Error writing file `%s'\n"), dst_name); 428 } 429 } 430 fclose (src); 431 if (fclose (dst) == EOF) 432 { 433 einfo (_("%P: Error closing file `%s'\n"), dst_name); 434 } 435 free (dst_name); 436 free (buf); 437 } 438 } 439 } 440 441 END_PROGRESS (program_name); 442 443 if (config.stats) 444 { 445 #ifdef HAVE_SBRK 446 char *lim = (char *) sbrk (0); 447 #endif 448 long run_time = get_run_time () - start_time; 449 450 fprintf (stderr, _("%s: total time in link: %ld.%06ld\n"), 451 program_name, run_time / 1000000, run_time % 1000000); 452 #ifdef HAVE_SBRK 453 fprintf (stderr, _("%s: data size %ld\n"), program_name, 454 (long) (lim - (char *) &environ)); 455 #endif 456 } 457 458 /* Prevent remove_output from doing anything, after a successful link. */ 459 output_filename = NULL; 460 461 xexit (0); 462 return 0; 463 } 464 465 /* We need to find any explicitly given emulation in order to initialize the 466 state that's needed by the lex&yacc argument parser (parse_args). */ 467 468 static char * 469 get_emulation (argc, argv) 470 int argc; 471 char **argv; 472 { 473 char *emulation; 474 int i; 475 476 emulation = getenv (EMULATION_ENVIRON); 477 if (emulation == NULL) 478 emulation = DEFAULT_EMULATION; 479 480 for (i = 1; i < argc; i++) 481 { 482 if (!strncmp (argv[i], "-m", 2)) 483 { 484 if (argv[i][2] == '\0') 485 { 486 /* -m EMUL */ 487 if (i < argc - 1) 488 { 489 emulation = argv[i + 1]; 490 i++; 491 } 492 else 493 { 494 einfo(_("%P%F: missing argument to -m\n")); 495 } 496 } 497 else if (strcmp (argv[i], "-mips1") == 0 498 || strcmp (argv[i], "-mips2") == 0 499 || strcmp (argv[i], "-mips3") == 0 500 || strcmp (argv[i], "-mips4") == 0) 501 { 502 /* FIXME: The arguments -mips1, -mips2 and -mips3 are 503 passed to the linker by some MIPS compilers. They 504 generally tell the linker to use a slightly different 505 library path. Perhaps someday these should be 506 implemented as emulations; until then, we just ignore 507 the arguments and hope that nobody ever creates 508 emulations named ips1, ips2 or ips3. */ 509 } 510 else if (strcmp (argv[i], "-m486") == 0) 511 { 512 /* FIXME: The argument -m486 is passed to the linker on 513 some Linux systems. Hope that nobody creates an 514 emulation named 486. */ 515 } 516 else 517 { 518 /* -mEMUL */ 519 emulation = &argv[i][2]; 520 } 521 } 522 } 523 524 return emulation; 525 } 526 527 /* If directory DIR contains an "ldscripts" subdirectory, 528 add DIR to the library search path and return true, 529 else return false. */ 530 531 static boolean 532 check_for_scripts_dir (dir) 533 char *dir; 534 { 535 size_t dirlen; 536 char *buf; 537 struct stat s; 538 boolean res; 539 540 dirlen = strlen (dir); 541 /* sizeof counts the terminating NUL. */ 542 buf = (char *) xmalloc (dirlen + sizeof("/ldscripts")); 543 sprintf (buf, "%s/ldscripts", dir); 544 545 res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode); 546 free (buf); 547 if (res) 548 ldfile_add_library_path (dir, false); 549 return res; 550 } 551 552 /* Set the default directory for finding script files. 553 Libraries will be searched for here too, but that's ok. 554 We look for the "ldscripts" directory in: 555 556 SCRIPTDIR (passed from Makefile) 557 the dir where this program is (for using it from the build tree) 558 the dir where this program is/../lib (for installing the tool suite elsewhere) */ 559 560 static void 561 set_scripts_dir () 562 { 563 char *end, *dir; 564 size_t dirlen; 565 566 if (check_for_scripts_dir (SCRIPTDIR)) 567 return; /* We've been installed normally. */ 568 569 /* Look for "ldscripts" in the dir where our binary is. */ 570 end = strrchr (program_name, '/'); 571 #ifdef HAVE_DOS_BASED_FILE_SYSTEM 572 { 573 /* We could have \foo\bar, or /foo\bar. */ 574 char *bslash = strrchr (program_name, '\\'); 575 if (bslash > end) 576 end = bslash; 577 } 578 #endif 579 580 if (end == NULL) 581 { 582 /* Don't look for ldscripts in the current directory. There is 583 too much potential for confusion. */ 584 return; 585 } 586 587 dirlen = end - program_name; 588 /* Make a copy of program_name in dir. 589 Leave room for later "/../lib". */ 590 dir = (char *) xmalloc (dirlen + 8); 591 strncpy (dir, program_name, dirlen); 592 dir[dirlen] = '\0'; 593 594 if (check_for_scripts_dir (dir)) 595 return; /* Don't free dir. */ 596 597 /* Look for "ldscripts" in <the dir where our binary is>/../lib. */ 598 strcpy (dir + dirlen, "/../lib"); 599 if (check_for_scripts_dir (dir)) 600 return; 601 602 free (dir); /* Well, we tried. */ 603 } 604 605 void 606 add_ysym (name) 607 const char *name; 608 { 609 if (link_info.notice_hash == (struct bfd_hash_table *) NULL) 610 { 611 link_info.notice_hash = ((struct bfd_hash_table *) 612 xmalloc (sizeof (struct bfd_hash_table))); 613 if (! bfd_hash_table_init_n (link_info.notice_hash, 614 bfd_hash_newfunc, 615 61)) 616 einfo (_("%P%F: bfd_hash_table_init failed: %E\n")); 617 } 618 619 if (bfd_hash_lookup (link_info.notice_hash, name, true, true) 620 == (struct bfd_hash_entry *) NULL) 621 einfo (_("%P%F: bfd_hash_lookup failed: %E\n")); 622 } 623 624 /* Record a symbol to be wrapped, from the --wrap option. */ 625 626 void 627 add_wrap (name) 628 const char *name; 629 { 630 if (link_info.wrap_hash == NULL) 631 { 632 link_info.wrap_hash = ((struct bfd_hash_table *) 633 xmalloc (sizeof (struct bfd_hash_table))); 634 if (! bfd_hash_table_init_n (link_info.wrap_hash, 635 bfd_hash_newfunc, 636 61)) 637 einfo (_("%P%F: bfd_hash_table_init failed: %E\n")); 638 } 639 if (bfd_hash_lookup (link_info.wrap_hash, name, true, true) == NULL) 640 einfo (_("%P%F: bfd_hash_lookup failed: %E\n")); 641 } 642 643 /* Handle the -retain-symbols-file option. */ 644 645 void 646 add_keepsyms_file (filename) 647 const char *filename; 648 { 649 FILE *file; 650 char *buf; 651 size_t bufsize; 652 int c; 653 654 if (link_info.strip == strip_some) 655 einfo (_("%X%P: error: duplicate retain-symbols-file\n")); 656 657 file = fopen (filename, "r"); 658 if (file == (FILE *) NULL) 659 { 660 bfd_set_error (bfd_error_system_call); 661 einfo ("%X%P: %s: %E\n", filename); 662 return; 663 } 664 665 link_info.keep_hash = ((struct bfd_hash_table *) 666 xmalloc (sizeof (struct bfd_hash_table))); 667 if (! bfd_hash_table_init (link_info.keep_hash, bfd_hash_newfunc)) 668 einfo (_("%P%F: bfd_hash_table_init failed: %E\n")); 669 670 bufsize = 100; 671 buf = (char *) xmalloc (bufsize); 672 673 c = getc (file); 674 while (c != EOF) 675 { 676 while (isspace (c)) 677 c = getc (file); 678 679 if (c != EOF) 680 { 681 size_t len = 0; 682 683 while (! isspace (c) && c != EOF) 684 { 685 buf[len] = c; 686 ++len; 687 if (len >= bufsize) 688 { 689 bufsize *= 2; 690 buf = xrealloc (buf, bufsize); 691 } 692 c = getc (file); 693 } 694 695 buf[len] = '\0'; 696 697 if (bfd_hash_lookup (link_info.keep_hash, buf, true, true) 698 == (struct bfd_hash_entry *) NULL) 699 einfo (_("%P%F: bfd_hash_lookup for insertion failed: %E\n")); 700 } 701 } 702 703 if (link_info.strip != strip_none) 704 einfo (_("%P: `-retain-symbols-file' overrides `-s' and `-S'\n")); 705 706 link_info.strip = strip_some; 707 } 708 709 /* Callbacks from the BFD linker routines. */ 710 711 /* This is called when BFD has decided to include an archive member in 712 a link. */ 713 714 /*ARGSUSED*/ 715 static boolean 716 add_archive_element (info, abfd, name) 717 struct bfd_link_info *info ATTRIBUTE_UNUSED; 718 bfd *abfd; 719 const char *name; 720 { 721 lang_input_statement_type *input; 722 723 input = ((lang_input_statement_type *) 724 xmalloc (sizeof (lang_input_statement_type))); 725 input->filename = abfd->filename; 726 input->local_sym_name = abfd->filename; 727 input->the_bfd = abfd; 728 input->asymbols = NULL; 729 input->next = NULL; 730 input->just_syms_flag = false; 731 input->loaded = false; 732 input->search_dirs_flag = false; 733 734 /* FIXME: The following fields are not set: header.next, 735 header.type, closed, passive_position, symbol_count, 736 next_real_file, is_archive, target, real. This bit of code is 737 from the old decode_library_subfile function. I don't know 738 whether any of those fields matters. */ 739 740 ldlang_add_file (input); 741 742 if (config.map_file != (FILE *) NULL) 743 { 744 static boolean header_printed; 745 struct bfd_link_hash_entry *h; 746 bfd *from; 747 int len; 748 749 h = bfd_link_hash_lookup (link_info.hash, name, false, false, true); 750 751 if (h == NULL) 752 from = NULL; 753 else 754 { 755 switch (h->type) 756 { 757 default: 758 from = NULL; 759 break; 760 761 case bfd_link_hash_defined: 762 case bfd_link_hash_defweak: 763 from = h->u.def.section->owner; 764 break; 765 766 case bfd_link_hash_undefined: 767 case bfd_link_hash_undefweak: 768 from = h->u.undef.abfd; 769 break; 770 771 case bfd_link_hash_common: 772 from = h->u.c.p->section->owner; 773 break; 774 } 775 } 776 777 if (! header_printed) 778 { 779 char buf[100]; 780 781 sprintf (buf, "%-29s %s\n\n", _("Archive member included"), 782 _("because of file (symbol)")); 783 minfo ("%s", buf); 784 header_printed = true; 785 } 786 787 if (bfd_my_archive (abfd) == NULL) 788 { 789 minfo ("%s", bfd_get_filename (abfd)); 790 len = strlen (bfd_get_filename (abfd)); 791 } 792 else 793 { 794 minfo ("%s(%s)", bfd_get_filename (bfd_my_archive (abfd)), 795 bfd_get_filename (abfd)); 796 len = (strlen (bfd_get_filename (bfd_my_archive (abfd))) 797 + strlen (bfd_get_filename (abfd)) 798 + 2); 799 } 800 801 if (len >= 29) 802 { 803 print_nl (); 804 len = 0; 805 } 806 while (len < 30) 807 { 808 print_space (); 809 ++len; 810 } 811 812 if (from != NULL) 813 minfo ("%B ", from); 814 if (h != NULL) 815 minfo ("(%T)\n", h->root.string); 816 else 817 minfo ("(%s)\n", name); 818 } 819 820 if (trace_files || trace_file_tries) 821 info_msg ("%I\n", input); 822 823 return true; 824 } 825 826 /* This is called when BFD has discovered a symbol which is defined 827 multiple times. */ 828 829 /*ARGSUSED*/ 830 static boolean 831 multiple_definition (info, name, obfd, osec, oval, nbfd, nsec, nval) 832 struct bfd_link_info *info ATTRIBUTE_UNUSED; 833 const char *name; 834 bfd *obfd; 835 asection *osec; 836 bfd_vma oval; 837 bfd *nbfd; 838 asection *nsec; 839 bfd_vma nval; 840 { 841 /* If either section has the output_section field set to 842 bfd_abs_section_ptr, it means that the section is being 843 discarded, and this is not really a multiple definition at all. 844 FIXME: It would be cleaner to somehow ignore symbols defined in 845 sections which are being discarded. */ 846 if ((osec->output_section != NULL 847 && ! bfd_is_abs_section (osec) 848 && bfd_is_abs_section (osec->output_section)) 849 || (nsec->output_section != NULL 850 && ! bfd_is_abs_section (nsec) 851 && bfd_is_abs_section (nsec->output_section))) 852 return true; 853 854 einfo (_("%X%C: multiple definition of `%T'\n"), 855 nbfd, nsec, nval, name); 856 if (obfd != (bfd *) NULL) 857 einfo (_("%D: first defined here\n"), obfd, osec, oval); 858 return true; 859 } 860 861 /* This is called when there is a definition of a common symbol, or 862 when a common symbol is found for a symbol that is already defined, 863 or when two common symbols are found. We only do something if 864 -warn-common was used. */ 865 866 /*ARGSUSED*/ 867 static boolean 868 multiple_common (info, name, obfd, otype, osize, nbfd, ntype, nsize) 869 struct bfd_link_info *info ATTRIBUTE_UNUSED; 870 const char *name; 871 bfd *obfd; 872 enum bfd_link_hash_type otype; 873 bfd_vma osize; 874 bfd *nbfd; 875 enum bfd_link_hash_type ntype; 876 bfd_vma nsize; 877 { 878 if (! config.warn_common) 879 return true; 880 881 if (ntype == bfd_link_hash_defined 882 || ntype == bfd_link_hash_defweak 883 || ntype == bfd_link_hash_indirect) 884 { 885 ASSERT (otype == bfd_link_hash_common); 886 einfo (_("%B: warning: definition of `%T' overriding common\n"), 887 nbfd, name); 888 if (obfd != NULL) 889 einfo (_("%B: warning: common is here\n"), obfd); 890 } 891 else if (otype == bfd_link_hash_defined 892 || otype == bfd_link_hash_defweak 893 || otype == bfd_link_hash_indirect) 894 { 895 ASSERT (ntype == bfd_link_hash_common); 896 einfo (_("%B: warning: common of `%T' overridden by definition\n"), 897 nbfd, name); 898 if (obfd != NULL) 899 einfo (_("%B: warning: defined here\n"), obfd); 900 } 901 else 902 { 903 ASSERT (otype == bfd_link_hash_common && ntype == bfd_link_hash_common); 904 if (osize > nsize) 905 { 906 einfo (_("%B: warning: common of `%T' overridden by larger common\n"), 907 nbfd, name); 908 if (obfd != NULL) 909 einfo (_("%B: warning: larger common is here\n"), obfd); 910 } 911 else if (nsize > osize) 912 { 913 einfo (_("%B: warning: common of `%T' overriding smaller common\n"), 914 nbfd, name); 915 if (obfd != NULL) 916 einfo (_("%B: warning: smaller common is here\n"), obfd); 917 } 918 else 919 { 920 einfo (_("%B: warning: multiple common of `%T'\n"), nbfd, name); 921 if (obfd != NULL) 922 einfo (_("%B: warning: previous common is here\n"), obfd); 923 } 924 } 925 926 return true; 927 } 928 929 /* This is called when BFD has discovered a set element. H is the 930 entry in the linker hash table for the set. SECTION and VALUE 931 represent a value which should be added to the set. */ 932 933 /*ARGSUSED*/ 934 static boolean 935 add_to_set (info, h, reloc, abfd, section, value) 936 struct bfd_link_info *info ATTRIBUTE_UNUSED; 937 struct bfd_link_hash_entry *h; 938 bfd_reloc_code_real_type reloc; 939 bfd *abfd; 940 asection *section; 941 bfd_vma value; 942 { 943 if (config.warn_constructors) 944 einfo (_("%P: warning: global constructor %s used\n"), 945 h->root.string); 946 947 if (! config.build_constructors) 948 return true; 949 950 ldctor_add_set_entry (h, reloc, (const char *) NULL, section, value); 951 952 if (h->type == bfd_link_hash_new) 953 { 954 h->type = bfd_link_hash_undefined; 955 h->u.undef.abfd = abfd; 956 /* We don't call bfd_link_add_undef to add this to the list of 957 undefined symbols because we are going to define it 958 ourselves. */ 959 } 960 961 return true; 962 } 963 964 /* This is called when BFD has discovered a constructor. This is only 965 called for some object file formats--those which do not handle 966 constructors in some more clever fashion. This is similar to 967 adding an element to a set, but less general. */ 968 969 static boolean 970 constructor_callback (info, constructor, name, abfd, section, value) 971 struct bfd_link_info *info; 972 boolean constructor; 973 const char *name; 974 bfd *abfd; 975 asection *section; 976 bfd_vma value; 977 { 978 char *s; 979 struct bfd_link_hash_entry *h; 980 char set_name[1 + sizeof "__CTOR_LIST__"]; 981 982 if (config.warn_constructors) 983 einfo (_("%P: warning: global constructor %s used\n"), name); 984 985 if (! config.build_constructors) 986 return true; 987 988 /* Ensure that BFD_RELOC_CTOR exists now, so that we can give a 989 useful error message. */ 990 if (bfd_reloc_type_lookup (output_bfd, BFD_RELOC_CTOR) == NULL 991 && (link_info.relocateable 992 || bfd_reloc_type_lookup (abfd, BFD_RELOC_CTOR) == NULL)) 993 einfo (_("%P%F: BFD backend error: BFD_RELOC_CTOR unsupported\n")); 994 995 s = set_name; 996 if (bfd_get_symbol_leading_char (abfd) != '\0') 997 *s++ = bfd_get_symbol_leading_char (abfd); 998 if (constructor) 999 strcpy (s, "__CTOR_LIST__"); 1000 else 1001 strcpy (s, "__DTOR_LIST__"); 1002 1003 h = bfd_link_hash_lookup (info->hash, set_name, true, true, true); 1004 if (h == (struct bfd_link_hash_entry *) NULL) 1005 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n")); 1006 if (h->type == bfd_link_hash_new) 1007 { 1008 h->type = bfd_link_hash_undefined; 1009 h->u.undef.abfd = abfd; 1010 /* We don't call bfd_link_add_undef to add this to the list of 1011 undefined symbols because we are going to define it 1012 ourselves. */ 1013 } 1014 1015 ldctor_add_set_entry (h, BFD_RELOC_CTOR, name, section, value); 1016 return true; 1017 } 1018 1019 /* A structure used by warning_callback to pass information through 1020 bfd_map_over_sections. */ 1021 1022 struct warning_callback_info 1023 { 1024 boolean found; 1025 const char *warning; 1026 const char *symbol; 1027 asymbol **asymbols; 1028 }; 1029 1030 /* This is called when there is a reference to a warning symbol. */ 1031 1032 /*ARGSUSED*/ 1033 static boolean 1034 warning_callback (info, warning, symbol, abfd, section, address) 1035 struct bfd_link_info *info ATTRIBUTE_UNUSED; 1036 const char *warning; 1037 const char *symbol; 1038 bfd *abfd; 1039 asection *section; 1040 bfd_vma address; 1041 { 1042 /* This is a hack to support warn_multiple_gp. FIXME: This should 1043 have a cleaner interface, but what? */ 1044 if (! config.warn_multiple_gp 1045 && strcmp (warning, "using multiple gp values") == 0) 1046 return true; 1047 1048 if (section != NULL) 1049 einfo ("%C: %s\n", abfd, section, address, warning); 1050 else if (abfd == NULL) 1051 einfo ("%P: %s\n", warning); 1052 else if (symbol == NULL) 1053 einfo ("%B: %s\n", abfd, warning); 1054 else 1055 { 1056 lang_input_statement_type *entry; 1057 asymbol **asymbols; 1058 struct warning_callback_info info; 1059 1060 /* Look through the relocs to see if we can find a plausible 1061 address. */ 1062 1063 entry = (lang_input_statement_type *) abfd->usrdata; 1064 if (entry != NULL && entry->asymbols != NULL) 1065 asymbols = entry->asymbols; 1066 else 1067 { 1068 long symsize; 1069 long symbol_count; 1070 1071 symsize = bfd_get_symtab_upper_bound (abfd); 1072 if (symsize < 0) 1073 einfo (_("%B%F: could not read symbols: %E\n"), abfd); 1074 asymbols = (asymbol **) xmalloc (symsize); 1075 symbol_count = bfd_canonicalize_symtab (abfd, asymbols); 1076 if (symbol_count < 0) 1077 einfo (_("%B%F: could not read symbols: %E\n"), abfd); 1078 if (entry != NULL) 1079 { 1080 entry->asymbols = asymbols; 1081 entry->symbol_count = symbol_count; 1082 } 1083 } 1084 1085 info.found = false; 1086 info.warning = warning; 1087 info.symbol = symbol; 1088 info.asymbols = asymbols; 1089 bfd_map_over_sections (abfd, warning_find_reloc, (PTR) &info); 1090 1091 if (! info.found) 1092 einfo ("%B: %s\n", abfd, warning); 1093 1094 if (entry == NULL) 1095 free (asymbols); 1096 } 1097 1098 return true; 1099 } 1100 1101 /* This is called by warning_callback for each section. It checks the 1102 relocs of the section to see if it can find a reference to the 1103 symbol which triggered the warning. If it can, it uses the reloc 1104 to give an error message with a file and line number. */ 1105 1106 static void 1107 warning_find_reloc (abfd, sec, iarg) 1108 bfd *abfd; 1109 asection *sec; 1110 PTR iarg; 1111 { 1112 struct warning_callback_info *info = (struct warning_callback_info *) iarg; 1113 long relsize; 1114 arelent **relpp; 1115 long relcount; 1116 arelent **p, **pend; 1117 1118 if (info->found) 1119 return; 1120 1121 relsize = bfd_get_reloc_upper_bound (abfd, sec); 1122 if (relsize < 0) 1123 einfo (_("%B%F: could not read relocs: %E\n"), abfd); 1124 if (relsize == 0) 1125 return; 1126 1127 relpp = (arelent **) xmalloc (relsize); 1128 relcount = bfd_canonicalize_reloc (abfd, sec, relpp, info->asymbols); 1129 if (relcount < 0) 1130 einfo (_("%B%F: could not read relocs: %E\n"), abfd); 1131 1132 p = relpp; 1133 pend = p + relcount; 1134 for (; p < pend && *p != NULL; p++) 1135 { 1136 arelent *q = *p; 1137 1138 if (q->sym_ptr_ptr != NULL 1139 && *q->sym_ptr_ptr != NULL 1140 && strcmp (bfd_asymbol_name (*q->sym_ptr_ptr), info->symbol) == 0) 1141 { 1142 /* We found a reloc for the symbol we are looking for. */ 1143 einfo ("%C: %s\n", abfd, sec, q->address, info->warning); 1144 info->found = true; 1145 break; 1146 } 1147 } 1148 1149 free (relpp); 1150 } 1151 1152 /* This is called when an undefined symbol is found. */ 1153 1154 /*ARGSUSED*/ 1155 static boolean 1156 undefined_symbol (info, name, abfd, section, address, fatal) 1157 struct bfd_link_info *info ATTRIBUTE_UNUSED; 1158 const char *name; 1159 bfd *abfd; 1160 asection *section; 1161 bfd_vma address; 1162 boolean fatal; 1163 { 1164 static char *error_name; 1165 static unsigned int error_count; 1166 1167 #define MAX_ERRORS_IN_A_ROW 5 1168 1169 if (config.warn_once) 1170 { 1171 static struct bfd_hash_table *hash; 1172 1173 /* Only warn once about a particular undefined symbol. */ 1174 1175 if (hash == NULL) 1176 { 1177 hash = ((struct bfd_hash_table *) 1178 xmalloc (sizeof (struct bfd_hash_table))); 1179 if (! bfd_hash_table_init (hash, bfd_hash_newfunc)) 1180 einfo (_("%F%P: bfd_hash_table_init failed: %E\n")); 1181 } 1182 1183 if (bfd_hash_lookup (hash, name, false, false) != NULL) 1184 return true; 1185 1186 if (bfd_hash_lookup (hash, name, true, true) == NULL) 1187 einfo (_("%F%P: bfd_hash_lookup failed: %E\n")); 1188 } 1189 1190 /* We never print more than a reasonable number of errors in a row 1191 for a single symbol. */ 1192 if (error_name != (char *) NULL 1193 && strcmp (name, error_name) == 0) 1194 ++error_count; 1195 else 1196 { 1197 error_count = 0; 1198 if (error_name != (char *) NULL) 1199 free (error_name); 1200 error_name = buystring (name); 1201 } 1202 1203 if (section != NULL) 1204 { 1205 if (error_count < MAX_ERRORS_IN_A_ROW) 1206 { 1207 einfo (_("%C: undefined reference to `%T'\n"), 1208 abfd, section, address, name); 1209 if (fatal) 1210 einfo ("%X"); 1211 } 1212 else if (error_count == MAX_ERRORS_IN_A_ROW) 1213 einfo (_("%D: more undefined references to `%T' follow\n"), 1214 abfd, section, address, name); 1215 } 1216 else 1217 { 1218 if (error_count < MAX_ERRORS_IN_A_ROW) 1219 { 1220 einfo (_("%B: undefined reference to `%T'\n"), 1221 abfd, name); 1222 if (fatal) 1223 einfo ("%X"); 1224 } 1225 else if (error_count == MAX_ERRORS_IN_A_ROW) 1226 einfo (_("%B: more undefined references to `%T' follow\n"), 1227 abfd, name); 1228 } 1229 1230 return true; 1231 } 1232 1233 /* This is called when a reloc overflows. */ 1234 1235 /*ARGSUSED*/ 1236 static boolean 1237 reloc_overflow (info, name, reloc_name, addend, abfd, section, address) 1238 struct bfd_link_info *info ATTRIBUTE_UNUSED; 1239 const char *name; 1240 const char *reloc_name; 1241 bfd_vma addend; 1242 bfd *abfd; 1243 asection *section; 1244 bfd_vma address; 1245 { 1246 if (abfd == (bfd *) NULL) 1247 einfo (_("%P%X: generated")); 1248 else 1249 einfo ("%X%C:", abfd, section, address); 1250 einfo (_(" relocation truncated to fit: %s %T"), reloc_name, name); 1251 if (addend != 0) 1252 einfo ("+%v", addend); 1253 einfo ("\n"); 1254 return true; 1255 } 1256 1257 /* This is called when a dangerous relocation is made. */ 1258 1259 /*ARGSUSED*/ 1260 static boolean 1261 reloc_dangerous (info, message, abfd, section, address) 1262 struct bfd_link_info *info ATTRIBUTE_UNUSED; 1263 const char *message; 1264 bfd *abfd; 1265 asection *section; 1266 bfd_vma address; 1267 { 1268 if (abfd == (bfd *) NULL) 1269 einfo (_("%P%X: generated")); 1270 else 1271 einfo ("%X%C:", abfd, section, address); 1272 einfo (_("dangerous relocation: %s\n"), message); 1273 return true; 1274 } 1275 1276 /* This is called when a reloc is being generated attached to a symbol 1277 that is not being output. */ 1278 1279 /*ARGSUSED*/ 1280 static boolean 1281 unattached_reloc (info, name, abfd, section, address) 1282 struct bfd_link_info *info ATTRIBUTE_UNUSED; 1283 const char *name; 1284 bfd *abfd; 1285 asection *section; 1286 bfd_vma address; 1287 { 1288 if (abfd == (bfd *) NULL) 1289 einfo (_("%P%X: generated")); 1290 else 1291 einfo ("%X%C:", abfd, section, address); 1292 einfo (_(" reloc refers to symbol `%T' which is not being output\n"), name); 1293 return true; 1294 } 1295 1296 /* This is called if link_info.notice_all is set, or when a symbol in 1297 link_info.notice_hash is found. Symbols are put in notice_hash 1298 using the -y option. */ 1299 1300 static boolean 1301 notice (info, name, abfd, section, value) 1302 struct bfd_link_info *info; 1303 const char *name; 1304 bfd *abfd; 1305 asection *section; 1306 bfd_vma value; 1307 { 1308 if (! info->notice_all 1309 || (info->notice_hash != NULL 1310 && bfd_hash_lookup (info->notice_hash, name, false, false) != NULL)) 1311 { 1312 if (bfd_is_und_section (section)) 1313 einfo ("%B: reference to %s\n", abfd, name); 1314 else 1315 einfo ("%B: definition of %s\n", abfd, name); 1316 } 1317 1318 if (command_line.cref || nocrossref_list != NULL) 1319 add_cref (name, abfd, section, value); 1320 1321 return true; 1322 } 1323