00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "bfd.h"
00029 #include "sysdep.h"
00030 #include <stdio.h>
00031 #include "safe-ctype.h"
00032 #include "libiberty.h"
00033 #include "progress.h"
00034 #include "bfdlink.h"
00035 #include "filenames.h"
00036
00037 #include "ld.h"
00038 #include "ldmain.h"
00039 #include "ldmisc.h"
00040 #include "ldwrite.h"
00041 #include "ldexp.h"
00042 #include "ldlang.h"
00043 #include <ldgram.h>
00044 #include "ldlex.h"
00045 #include "ldfile.h"
00046 #include "ldemul.h"
00047 #include "ldctor.h"
00048
00049 #ifdef IPA_LINK
00050 #include "ipa_ld.h"
00051 #include "ipa_cmdline.h"
00052 #endif
00053
00054
00055 #if !defined(S_ISDIR) && defined(S_IFDIR)
00056 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
00057 #endif
00058
00059 #include <string.h>
00060
00061 #ifdef HAVE_SBRK
00062 #ifdef NEED_DECLARATION_SBRK
00063 extern void *sbrk ();
00064 #endif
00065 #endif
00066
00067 #ifndef TARGET_SYSTEM_ROOT
00068 #define TARGET_SYSTEM_ROOT ""
00069 #endif
00070
00071
00072
00073 char *default_target;
00074 const char *output_filename = "a.out";
00075
00076
00077 char *program_name;
00078
00079
00080 const char *ld_sysroot;
00081
00082
00083 char * ld_canon_sysroot;
00084 int ld_canon_sysroot_len;
00085
00086
00087 bfd *output_bfd = 0;
00088
00089
00090 int g_switch_value = 8;
00091
00092
00093 bfd_boolean trace_files;
00094
00095
00096 bfd_boolean trace_file_tries;
00097
00098
00099
00100 bfd_boolean version_printed;
00101
00102
00103 bfd_boolean whole_archive;
00104
00105
00106
00107 bfd_boolean as_needed;
00108
00109
00110
00111 bfd_boolean add_needed = TRUE;
00112
00113
00114 bfd_boolean demangling;
00115
00116 args_type command_line;
00117
00118 ld_config_type config;
00119
00120 sort_type sort_section;
00121
00122 static const char *get_sysroot
00123 (int, char **);
00124 static char *get_emulation
00125 (int, char **);
00126 static void set_scripts_dir
00127 (void);
00128 static bfd_boolean add_archive_element
00129 (struct bfd_link_info *, bfd *, const char *);
00130 static bfd_boolean multiple_definition
00131 (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma,
00132 bfd *, asection *, bfd_vma);
00133 static bfd_boolean multiple_common
00134 (struct bfd_link_info *, const char *, bfd *, enum bfd_link_hash_type,
00135 bfd_vma, bfd *, enum bfd_link_hash_type, bfd_vma);
00136 static bfd_boolean add_to_set
00137 (struct bfd_link_info *, struct bfd_link_hash_entry *,
00138 bfd_reloc_code_real_type, bfd *, asection *, bfd_vma);
00139 static bfd_boolean constructor_callback
00140 (struct bfd_link_info *, bfd_boolean, const char *, bfd *,
00141 asection *, bfd_vma);
00142 static bfd_boolean warning_callback
00143 (struct bfd_link_info *, const char *, const char *, bfd *,
00144 asection *, bfd_vma);
00145 static void warning_find_reloc
00146 (bfd *, asection *, void *);
00147 static bfd_boolean undefined_symbol
00148 (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma,
00149 bfd_boolean);
00150 static bfd_boolean reloc_overflow
00151 (struct bfd_link_info *, struct bfd_link_hash_entry *, const char *,
00152 const char *, bfd_vma, bfd *, asection *, bfd_vma);
00153 static bfd_boolean reloc_dangerous
00154 (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma);
00155 static bfd_boolean unattached_reloc
00156 (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma);
00157 static bfd_boolean notice
00158 (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma);
00159
00160 #ifdef IPA_LINK
00161 bfd_boolean is_ipa = FALSE;
00162
00163
00164
00165
00166 int ld_set_ndx(bfd *abfd)
00167 {
00168 return ipa_set_ndx(abfd);
00169 }
00170 #endif
00171
00172 static struct bfd_link_callbacks link_callbacks =
00173 {
00174 add_archive_element,
00175 multiple_definition,
00176 multiple_common,
00177 add_to_set,
00178 constructor_callback,
00179 warning_callback,
00180 undefined_symbol,
00181 reloc_overflow,
00182 reloc_dangerous,
00183 unattached_reloc,
00184 notice
00185 };
00186
00187 struct bfd_link_info link_info;
00188
00189 static void
00190 remove_output (void)
00191 {
00192 if (output_filename)
00193 {
00194 if (output_bfd)
00195 bfd_cache_close (output_bfd);
00196 if (delete_output_file_on_failure)
00197 unlink_if_ordinary (output_filename);
00198 }
00199 }
00200
00201 int
00202 main (int argc, char **argv
00203 #ifdef IPA_LINK
00204 , char **envp
00205 #endif
00206 )
00207 {
00208 char *emulation;
00209 long start_time = get_run_time ();
00210
00211 #ifdef IPA_LINK
00212 ipa_search_command_line(argc, argv, envp);
00213 #endif
00214
00215 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
00216 setlocale (LC_MESSAGES, "");
00217 #endif
00218 #if defined (HAVE_SETLOCALE)
00219 setlocale (LC_CTYPE, "");
00220 #endif
00221 bindtextdomain (PACKAGE, LOCALEDIR);
00222 textdomain (PACKAGE);
00223
00224 program_name = argv[0];
00225 xmalloc_set_program_name (program_name);
00226
00227 START_PROGRESS (program_name, 0);
00228
00229 bfd_init ();
00230
00231 bfd_set_error_program_name (program_name);
00232
00233 xatexit (remove_output);
00234
00235
00236 ld_sysroot = get_sysroot (argc, argv);
00237 if (*ld_sysroot)
00238 {
00239 if (*TARGET_SYSTEM_ROOT == 0)
00240 {
00241 einfo ("%P%F: this linker was not configured to use sysroots");
00242 ld_sysroot = "";
00243 }
00244 else
00245 ld_canon_sysroot = lrealpath (ld_sysroot);
00246 }
00247 if (ld_canon_sysroot)
00248 ld_canon_sysroot_len = strlen (ld_canon_sysroot);
00249 else
00250 ld_canon_sysroot_len = -1;
00251
00252
00253
00254
00255
00256 if (! bfd_set_default_target (TARGET))
00257 {
00258 einfo (_("%X%P: can't set BFD default target to `%s': %E\n"), TARGET);
00259 xexit (1);
00260 }
00261
00262 #if YYDEBUG
00263 {
00264 extern int yydebug;
00265 yydebug = 1;
00266 }
00267 #endif
00268
00269
00270 trace_files = trace_file_tries = version_printed = FALSE;
00271 whole_archive = FALSE;
00272 config.build_constructors = TRUE;
00273 config.dynamic_link = FALSE;
00274 config.has_shared = FALSE;
00275 config.split_by_reloc = (unsigned) -1;
00276 config.split_by_file = (bfd_size_type) -1;
00277 config.hash_table_size = 0;
00278 command_line.force_common_definition = FALSE;
00279 command_line.inhibit_common_definition = FALSE;
00280 command_line.interpreter = NULL;
00281 command_line.rpath = NULL;
00282 command_line.warn_mismatch = TRUE;
00283 command_line.check_section_addresses = TRUE;
00284 command_line.accept_unknown_input_arch = FALSE;
00285 command_line.reduce_memory_overheads = FALSE;
00286
00287 sort_section = none;
00288
00289
00290
00291
00292
00293
00294 demangling = getenv ("COLLECT_NO_DEMANGLE") == NULL;
00295
00296 link_info.relocatable = FALSE;
00297 link_info.emitrelocations = FALSE;
00298 link_info.task_link = FALSE;
00299 link_info.shared = FALSE;
00300 link_info.pie = FALSE;
00301 link_info.executable = FALSE;
00302 link_info.symbolic = FALSE;
00303 link_info.export_dynamic = FALSE;
00304 link_info.static_link = FALSE;
00305 link_info.traditional_format = FALSE;
00306 link_info.optimize = FALSE;
00307 link_info.unresolved_syms_in_objects = RM_NOT_YET_SET;
00308 link_info.unresolved_syms_in_shared_libs = RM_NOT_YET_SET;
00309 link_info.allow_multiple_definition = FALSE;
00310 link_info.allow_undefined_version = TRUE;
00311 link_info.create_default_symver = FALSE;
00312 link_info.default_imported_symver = FALSE;
00313 link_info.keep_memory = TRUE;
00314 link_info.notice_all = FALSE;
00315 link_info.nocopyreloc = FALSE;
00316 link_info.new_dtags = FALSE;
00317 link_info.combreloc = TRUE;
00318 link_info.eh_frame_hdr = FALSE;
00319 link_info.relro = FALSE;
00320 link_info.strip_discarded = TRUE;
00321 link_info.strip = strip_none;
00322 link_info.discard = discard_sec_merge;
00323 link_info.common_skip_ar_aymbols = bfd_link_common_skip_none;
00324 link_info.callbacks = &link_callbacks;
00325 link_info.hash = NULL;
00326 link_info.keep_hash = NULL;
00327 link_info.notice_hash = NULL;
00328 link_info.wrap_hash = NULL;
00329 link_info.input_bfds = NULL;
00330 link_info.create_object_symbols_section = NULL;
00331 link_info.gc_sym_list = NULL;
00332 link_info.base_file = NULL;
00333
00334
00335 link_info.init_function = "_init";
00336 link_info.fini_function = "_fini";
00337 link_info.pei386_auto_import = -1;
00338 link_info.pei386_runtime_pseudo_reloc = FALSE;
00339 link_info.spare_dynamic_tags = 5;
00340 link_info.flags = 0;
00341 link_info.flags_1 = 0;
00342 link_info.need_relax_finalize = FALSE;
00343 link_info.warn_shared_textrel = FALSE;
00344
00345 ldfile_add_arch ("");
00346
00347 config.make_executable = TRUE;
00348 force_make_executable = FALSE;
00349 config.magic_demand_paged = TRUE;
00350 config.text_read_only = TRUE;
00351
00352 emulation = get_emulation (argc, argv);
00353 ldemul_choose_mode (emulation);
00354 default_target = ldemul_choose_target (argc, argv);
00355 lang_init ();
00356 ldemul_before_parse ();
00357 lang_has_input_file = FALSE;
00358 parse_args (argc, argv);
00359
00360 if (config.hash_table_size != 0)
00361 bfd_hash_set_default_size (config.hash_table_size);
00362
00363 ldemul_set_symbols ();
00364
00365 if (link_info.relocatable)
00366 {
00367 if (command_line.gc_sections)
00368 einfo ("%P%F: --gc-sections and -r may not be used together\n");
00369 else if (command_line.relax)
00370 einfo (_("%P%F: --relax and -r may not be used together\n"));
00371 if (link_info.shared)
00372 einfo (_("%P%F: -r and -shared may not be used together\n"));
00373 }
00374
00375 if (!config.dynamic_link && link_info.shared)
00376 einfo (_("%P%F: -static and -shared may not be used together\n"));
00377
00378 if (! link_info.shared)
00379 {
00380 if (command_line.filter_shlib)
00381 einfo (_("%P%F: -F may not be used without -shared\n"));
00382 if (command_line.auxiliary_filters)
00383 einfo (_("%P%F: -f may not be used without -shared\n"));
00384 }
00385
00386 if (! link_info.shared || link_info.pie)
00387 link_info.executable = TRUE;
00388
00389
00390
00391
00392 if (link_info.relocatable && link_info.strip == strip_all)
00393 {
00394 link_info.strip = strip_debugger;
00395 if (link_info.discard == discard_sec_merge)
00396 link_info.discard = discard_all;
00397 }
00398
00399
00400
00401 set_scripts_dir ();
00402
00403
00404
00405 if (saved_script_handle == NULL)
00406 {
00407 int isfile;
00408 char *s = ldemul_get_script (&isfile);
00409
00410 if (isfile)
00411 ldfile_open_command_file (s);
00412 else
00413 {
00414 lex_string = s;
00415 lex_redirect (s);
00416 }
00417 parser_input = input_script;
00418 yyparse ();
00419 lex_string = NULL;
00420 }
00421
00422 if (trace_file_tries)
00423 {
00424 if (saved_script_handle)
00425 info_msg (_("using external linker script:"));
00426 else
00427 info_msg (_("using internal linker script:"));
00428 info_msg ("\n==================================================\n");
00429
00430 if (saved_script_handle)
00431 {
00432 static const int ld_bufsz = 8193;
00433 size_t n;
00434 char *buf = xmalloc (ld_bufsz);
00435
00436 rewind (saved_script_handle);
00437 while ((n = fread (buf, 1, ld_bufsz - 1, saved_script_handle)) > 0)
00438 {
00439 buf[n] = 0;
00440 info_msg (buf);
00441 }
00442 rewind (saved_script_handle);
00443 free (buf);
00444 }
00445 else
00446 {
00447 int isfile;
00448
00449 info_msg (ldemul_get_script (&isfile));
00450 }
00451
00452 info_msg ("\n==================================================\n");
00453 }
00454
00455 lang_final ();
00456
00457 if (!lang_has_input_file)
00458 {
00459 if (version_printed)
00460 xexit (0);
00461 einfo (_("%P%F: no input files\n"));
00462 }
00463
00464 if (trace_files)
00465 info_msg (_("%P: mode %s\n"), emulation);
00466
00467 ldemul_after_parse ();
00468
00469 if (config.map_filename)
00470 {
00471 if (strcmp (config.map_filename, "-") == 0)
00472 {
00473 config.map_file = stdout;
00474 }
00475 else
00476 {
00477 config.map_file = fopen (config.map_filename, FOPEN_WT);
00478 if (config.map_file == (FILE *) NULL)
00479 {
00480 bfd_set_error (bfd_error_system_call);
00481 einfo (_("%P%F: cannot open map file %s: %E\n"),
00482 config.map_filename);
00483 }
00484 }
00485 }
00486
00487 lang_process ();
00488
00489 #ifdef IPA_LINK
00490 if (is_ipa) {
00491 cleanup_symtab_for_ipa();
00492 (*p_ipa_driver) (ipa_argc, ipa_argv);
00493 }
00494 #endif
00495
00496
00497
00498 if (link_info.relocatable)
00499 output_bfd->flags &= ~EXEC_P;
00500 else
00501 output_bfd->flags |= EXEC_P;
00502
00503 ldwrite ();
00504
00505 if (config.map_file != NULL)
00506 lang_map ();
00507 if (command_line.cref)
00508 output_cref (config.map_file != NULL ? config.map_file : stdout);
00509 if (nocrossref_list != NULL)
00510 check_nocrossrefs ();
00511
00512
00513
00514
00515 if (!config.make_executable && !force_make_executable)
00516 {
00517 if (trace_files)
00518 einfo (_("%P: link errors found, deleting executable `%s'\n"),
00519 output_filename);
00520
00521
00522 xexit (1);
00523 }
00524 else
00525 {
00526 if (! bfd_close (output_bfd))
00527 einfo (_("%F%B: final close failed: %E\n"), output_bfd);
00528
00529
00530
00531
00532 if (! link_info.relocatable && command_line.force_exe_suffix)
00533 {
00534 int len = strlen (output_filename);
00535
00536 if (len < 4
00537 || (strcasecmp (output_filename + len - 4, ".exe") != 0
00538 && strcasecmp (output_filename + len - 4, ".dll") != 0))
00539 {
00540 FILE *src;
00541 FILE *dst;
00542 const int bsize = 4096;
00543 char *buf = xmalloc (bsize);
00544 int l;
00545 char *dst_name = xmalloc (len + 5);
00546
00547 strcpy (dst_name, output_filename);
00548 strcat (dst_name, ".exe");
00549 src = fopen (output_filename, FOPEN_RB);
00550 dst = fopen (dst_name, FOPEN_WB);
00551
00552 if (!src)
00553 einfo (_("%X%P: unable to open for source of copy `%s'\n"),
00554 output_filename);
00555 if (!dst)
00556 einfo (_("%X%P: unable to open for destination of copy `%s'\n"),
00557 dst_name);
00558 while ((l = fread (buf, 1, bsize, src)) > 0)
00559 {
00560 int done = fwrite (buf, 1, l, dst);
00561
00562 if (done != l)
00563 einfo (_("%P: Error writing file `%s'\n"), dst_name);
00564 }
00565
00566 fclose (src);
00567 if (fclose (dst) == EOF)
00568 einfo (_("%P: Error closing file `%s'\n"), dst_name);
00569 free (dst_name);
00570 free (buf);
00571 }
00572 }
00573 }
00574
00575 END_PROGRESS (program_name);
00576
00577 if (config.stats)
00578 {
00579 #ifdef HAVE_SBRK
00580 char *lim = sbrk (0);
00581 #endif
00582 long run_time = get_run_time () - start_time;
00583
00584 fprintf (stderr, _("%s: total time in link: %ld.%06ld\n"),
00585 program_name, run_time / 1000000, run_time % 1000000);
00586 #ifdef HAVE_SBRK
00587 fprintf (stderr, _("%s: data size %ld\n"), program_name,
00588 (long) (lim - (char *) &environ));
00589 #endif
00590 }
00591
00592
00593 output_filename = NULL;
00594
00595 xexit (0);
00596 return 0;
00597 }
00598
00599
00600
00601
00602
00603 static char *
00604 get_relative_sysroot (const char *from ATTRIBUTE_UNUSED)
00605 {
00606 #ifdef TARGET_SYSTEM_ROOT_RELOCATABLE
00607 char *path;
00608 struct stat s;
00609
00610 path = make_relative_prefix (program_name, from, TARGET_SYSTEM_ROOT);
00611 if (path)
00612 {
00613 if (stat (path, &s) == 0 && S_ISDIR (s.st_mode))
00614 return path;
00615 free (path);
00616 }
00617 #endif
00618 return 0;
00619 }
00620
00621
00622
00623 static const char *
00624 get_sysroot (int argc, char **argv)
00625 {
00626 int i;
00627 const char *path;
00628
00629 for (i = 1; i < argc; i++)
00630 if (strncmp (argv[i], "--sysroot=", strlen ("--sysroot=")) == 0)
00631 return argv[i] + strlen ("--sysroot=");
00632
00633 path = get_relative_sysroot (BINDIR);
00634 if (path)
00635 return path;
00636
00637 path = get_relative_sysroot (TOOLBINDIR);
00638 if (path)
00639 return path;
00640
00641 return TARGET_SYSTEM_ROOT;
00642 }
00643
00644
00645
00646
00647 static char *
00648 get_emulation (int argc, char **argv)
00649 {
00650 char *emulation;
00651 int i;
00652
00653 emulation = getenv (EMULATION_ENVIRON);
00654 if (emulation == NULL)
00655 emulation = DEFAULT_EMULATION;
00656
00657 for (i = 1; i < argc; i++)
00658 {
00659 if (!strncmp (argv[i], "-m", 2))
00660 {
00661 if (argv[i][2] == '\0')
00662 {
00663
00664 if (i < argc - 1)
00665 {
00666 emulation = argv[i + 1];
00667 i++;
00668 }
00669 else
00670 einfo (_("%P%F: missing argument to -m\n"));
00671 }
00672 else if (strcmp (argv[i], "-mips1") == 0
00673 || strcmp (argv[i], "-mips2") == 0
00674 || strcmp (argv[i], "-mips3") == 0
00675 || strcmp (argv[i], "-mips4") == 0
00676 || strcmp (argv[i], "-mips5") == 0
00677 || strcmp (argv[i], "-mips32") == 0
00678 || strcmp (argv[i], "-mips32r2") == 0
00679 || strcmp (argv[i], "-mips64") == 0
00680 || strcmp (argv[i], "-mips64r2") == 0)
00681 {
00682
00683
00684
00685
00686
00687
00688
00689 }
00690 else if (strcmp (argv[i], "-m486") == 0)
00691 {
00692
00693
00694
00695 }
00696 else
00697 {
00698
00699 emulation = &argv[i][2];
00700 }
00701 }
00702 }
00703
00704 return emulation;
00705 }
00706
00707
00708
00709
00710
00711 static bfd_boolean
00712 check_for_scripts_dir (char *dir)
00713 {
00714 size_t dirlen;
00715 char *buf;
00716 struct stat s;
00717 bfd_boolean res;
00718
00719 dirlen = strlen (dir);
00720
00721 buf = xmalloc (dirlen + sizeof ("/ldscripts"));
00722 sprintf (buf, "%s/ldscripts", dir);
00723
00724 res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode);
00725 free (buf);
00726 if (res)
00727 ldfile_add_library_path (dir, FALSE);
00728 return res;
00729 }
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742 static void
00743 set_scripts_dir (void)
00744 {
00745 char *end, *dir;
00746 size_t dirlen;
00747 bfd_boolean found;
00748
00749 dir = make_relative_prefix (program_name, BINDIR, SCRIPTDIR);
00750 if (dir)
00751 {
00752 found = check_for_scripts_dir (dir);
00753 free (dir);
00754 if (found)
00755 return;
00756 }
00757
00758 dir = make_relative_prefix (program_name, TOOLBINDIR, SCRIPTDIR);
00759 if (dir)
00760 {
00761 found = check_for_scripts_dir (dir);
00762 free (dir);
00763 if (found)
00764 return;
00765 }
00766
00767 if (check_for_scripts_dir (SCRIPTDIR))
00768
00769 return;
00770
00771
00772 end = strrchr (program_name, '/');
00773 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
00774 {
00775
00776 char *bslash = strrchr (program_name, '\\');
00777
00778 if (end == NULL || (bslash != NULL && bslash > end))
00779 end = bslash;
00780 }
00781 #endif
00782
00783 if (end == NULL)
00784
00785
00786 return;
00787
00788 dirlen = end - program_name;
00789
00790
00791 dir = xmalloc (dirlen + 8);
00792 strncpy (dir, program_name, dirlen);
00793 dir[dirlen] = '\0';
00794
00795 if (check_for_scripts_dir (dir))
00796 {
00797 free (dir);
00798 return;
00799 }
00800
00801
00802 strcpy (dir + dirlen, "/../lib");
00803 check_for_scripts_dir (dir);
00804 free (dir);
00805 }
00806
00807 void
00808 add_ysym (const char *name)
00809 {
00810 if (link_info.notice_hash == NULL)
00811 {
00812 link_info.notice_hash = xmalloc (sizeof (struct bfd_hash_table));
00813 if (! bfd_hash_table_init_n (link_info.notice_hash,
00814 bfd_hash_newfunc,
00815 61))
00816 einfo (_("%P%F: bfd_hash_table_init failed: %E\n"));
00817 }
00818
00819 if (bfd_hash_lookup (link_info.notice_hash, name, TRUE, TRUE) == NULL)
00820 einfo (_("%P%F: bfd_hash_lookup failed: %E\n"));
00821 }
00822
00823
00824
00825 void
00826 add_wrap (const char *name)
00827 {
00828 if (link_info.wrap_hash == NULL)
00829 {
00830 link_info.wrap_hash = xmalloc (sizeof (struct bfd_hash_table));
00831 if (! bfd_hash_table_init_n (link_info.wrap_hash,
00832 bfd_hash_newfunc,
00833 61))
00834 einfo (_("%P%F: bfd_hash_table_init failed: %E\n"));
00835 }
00836
00837 if (bfd_hash_lookup (link_info.wrap_hash, name, TRUE, TRUE) == NULL)
00838 einfo (_("%P%F: bfd_hash_lookup failed: %E\n"));
00839 }
00840
00841
00842
00843 void
00844 add_keepsyms_file (const char *filename)
00845 {
00846 FILE *file;
00847 char *buf;
00848 size_t bufsize;
00849 int c;
00850
00851 if (link_info.strip == strip_some)
00852 einfo (_("%X%P: error: duplicate retain-symbols-file\n"));
00853
00854 file = fopen (filename, "r");
00855 if (file == NULL)
00856 {
00857 bfd_set_error (bfd_error_system_call);
00858 einfo ("%X%P: %s: %E\n", filename);
00859 return;
00860 }
00861
00862 link_info.keep_hash = xmalloc (sizeof (struct bfd_hash_table));
00863 if (! bfd_hash_table_init (link_info.keep_hash, bfd_hash_newfunc))
00864 einfo (_("%P%F: bfd_hash_table_init failed: %E\n"));
00865
00866 bufsize = 100;
00867 buf = xmalloc (bufsize);
00868
00869 c = getc (file);
00870 while (c != EOF)
00871 {
00872 while (ISSPACE (c))
00873 c = getc (file);
00874
00875 if (c != EOF)
00876 {
00877 size_t len = 0;
00878
00879 while (! ISSPACE (c) && c != EOF)
00880 {
00881 buf[len] = c;
00882 ++len;
00883 if (len >= bufsize)
00884 {
00885 bufsize *= 2;
00886 buf = xrealloc (buf, bufsize);
00887 }
00888 c = getc (file);
00889 }
00890
00891 buf[len] = '\0';
00892
00893 if (bfd_hash_lookup (link_info.keep_hash, buf, TRUE, TRUE) == NULL)
00894 einfo (_("%P%F: bfd_hash_lookup for insertion failed: %E\n"));
00895 }
00896 }
00897
00898 if (link_info.strip != strip_none)
00899 einfo (_("%P: `-retain-symbols-file' overrides `-s' and `-S'\n"));
00900
00901 free (buf);
00902 link_info.strip = strip_some;
00903 }
00904
00905
00906
00907
00908
00909
00910 static bfd_boolean
00911 add_archive_element (struct bfd_link_info *info ATTRIBUTE_UNUSED,
00912 bfd *abfd,
00913 const char *name)
00914 {
00915 lang_input_statement_type *input;
00916
00917 input = xmalloc (sizeof (lang_input_statement_type));
00918 input->filename = abfd->filename;
00919 input->local_sym_name = abfd->filename;
00920 input->the_bfd = abfd;
00921 input->asymbols = NULL;
00922 input->next = NULL;
00923 input->just_syms_flag = FALSE;
00924 input->loaded = FALSE;
00925 input->search_dirs_flag = FALSE;
00926
00927
00928
00929
00930
00931
00932
00933 ldlang_add_file (input);
00934
00935 if (config.map_file != NULL)
00936 {
00937 static bfd_boolean header_printed;
00938 struct bfd_link_hash_entry *h;
00939 bfd *from;
00940 int len;
00941
00942 h = bfd_link_hash_lookup (link_info.hash, name, FALSE, FALSE, TRUE);
00943
00944 if (h == NULL)
00945 from = NULL;
00946 else
00947 {
00948 switch (h->type)
00949 {
00950 default:
00951 from = NULL;
00952 break;
00953
00954 case bfd_link_hash_defined:
00955 case bfd_link_hash_defweak:
00956 from = h->u.def.section->owner;
00957 break;
00958
00959 case bfd_link_hash_undefined:
00960 case bfd_link_hash_undefweak:
00961 from = h->u.undef.abfd;
00962 break;
00963
00964 case bfd_link_hash_common:
00965 from = h->u.c.p->section->owner;
00966 break;
00967 }
00968 }
00969
00970 if (! header_printed)
00971 {
00972 char buf[100];
00973
00974 sprintf (buf, _("Archive member included because of file (symbol)\n\n"));
00975 minfo ("%s", buf);
00976 header_printed = TRUE;
00977 }
00978
00979 if (bfd_my_archive (abfd) == NULL)
00980 {
00981 minfo ("%s", bfd_get_filename (abfd));
00982 len = strlen (bfd_get_filename (abfd));
00983 }
00984 else
00985 {
00986 minfo ("%s(%s)", bfd_get_filename (bfd_my_archive (abfd)),
00987 bfd_get_filename (abfd));
00988 len = (strlen (bfd_get_filename (bfd_my_archive (abfd)))
00989 + strlen (bfd_get_filename (abfd))
00990 + 2);
00991 }
00992
00993 if (len >= 29)
00994 {
00995 print_nl ();
00996 len = 0;
00997 }
00998 while (len < 30)
00999 {
01000 print_space ();
01001 ++len;
01002 }
01003
01004 if (from != NULL)
01005 minfo ("%B ", from);
01006 if (h != NULL)
01007 minfo ("(%T)\n", h->root.string);
01008 else
01009 minfo ("(%s)\n", name);
01010 }
01011
01012 if (trace_files || trace_file_tries)
01013 info_msg ("%I\n", input);
01014
01015 return TRUE;
01016 }
01017
01018
01019
01020
01021 static bfd_boolean
01022 multiple_definition (struct bfd_link_info *info ATTRIBUTE_UNUSED,
01023 const char *name,
01024 bfd *obfd,
01025 asection *osec,
01026 bfd_vma oval,
01027 bfd *nbfd,
01028 asection *nsec,
01029 bfd_vma nval)
01030 {
01031
01032
01033
01034
01035
01036 if ((
01037 #ifdef KEY
01038 osec != NULL &&
01039 #endif
01040 osec->output_section != NULL
01041 && ! bfd_is_abs_section (osec)
01042 && bfd_is_abs_section (osec->output_section))
01043 || (
01044 #ifdef KEY
01045 nsec != NULL &&
01046 #endif
01047 nsec->output_section != NULL
01048 && ! bfd_is_abs_section (nsec)
01049 && bfd_is_abs_section (nsec->output_section)))
01050 return TRUE;
01051
01052 #ifdef KEY
01053
01054
01055
01056
01057 if (obfd != NULL)
01058 #endif
01059 einfo (_("%X%C: multiple definition of `%T'\n"),
01060 nbfd, nsec, nval, name);
01061 if (obfd != NULL)
01062 einfo (_("%D: first defined here\n"), obfd, osec, oval);
01063
01064 if (command_line.relax)
01065 {
01066 einfo (_("%P: Disabling relaxation: it will not work with multiple definitions\n"));
01067 command_line.relax = 0;
01068 }
01069
01070 return TRUE;
01071 }
01072
01073
01074
01075
01076
01077
01078 static bfd_boolean
01079 multiple_common (struct bfd_link_info *info ATTRIBUTE_UNUSED,
01080 const char *name,
01081 bfd *obfd,
01082 enum bfd_link_hash_type otype,
01083 bfd_vma osize,
01084 bfd *nbfd,
01085 enum bfd_link_hash_type ntype,
01086 bfd_vma nsize)
01087 {
01088 if (! config.warn_common)
01089 return TRUE;
01090
01091 if (ntype == bfd_link_hash_defined
01092 || ntype == bfd_link_hash_defweak
01093 || ntype == bfd_link_hash_indirect)
01094 {
01095 ASSERT (otype == bfd_link_hash_common);
01096 einfo (_("%B: warning: definition of `%T' overriding common\n"),
01097 nbfd, name);
01098 if (obfd != NULL)
01099 einfo (_("%B: warning: common is here\n"), obfd);
01100 }
01101 else if (otype == bfd_link_hash_defined
01102 || otype == bfd_link_hash_defweak
01103 || otype == bfd_link_hash_indirect)
01104 {
01105 ASSERT (ntype == bfd_link_hash_common);
01106 einfo (_("%B: warning: common of `%T' overridden by definition\n"),
01107 nbfd, name);
01108 if (obfd != NULL)
01109 einfo (_("%B: warning: defined here\n"), obfd);
01110 }
01111 else
01112 {
01113 ASSERT (otype == bfd_link_hash_common && ntype == bfd_link_hash_common);
01114 if (osize > nsize)
01115 {
01116 einfo (_("%B: warning: common of `%T' overridden by larger common\n"),
01117 nbfd, name);
01118 if (obfd != NULL)
01119 einfo (_("%B: warning: larger common is here\n"), obfd);
01120 }
01121 else if (nsize > osize)
01122 {
01123 einfo (_("%B: warning: common of `%T' overriding smaller common\n"),
01124 nbfd, name);
01125 if (obfd != NULL)
01126 einfo (_("%B: warning: smaller common is here\n"), obfd);
01127 }
01128 else
01129 {
01130 einfo (_("%B: warning: multiple common of `%T'\n"), nbfd, name);
01131 if (obfd != NULL)
01132 einfo (_("%B: warning: previous common is here\n"), obfd);
01133 }
01134 }
01135
01136 return TRUE;
01137 }
01138
01139
01140
01141
01142
01143 static bfd_boolean
01144 add_to_set (struct bfd_link_info *info ATTRIBUTE_UNUSED,
01145 struct bfd_link_hash_entry *h,
01146 bfd_reloc_code_real_type reloc,
01147 bfd *abfd,
01148 asection *section,
01149 bfd_vma value)
01150 {
01151 if (config.warn_constructors)
01152 einfo (_("%P: warning: global constructor %s used\n"),
01153 h->root.string);
01154
01155 if (! config.build_constructors)
01156 return TRUE;
01157
01158 ldctor_add_set_entry (h, reloc, NULL, section, value);
01159
01160 if (h->type == bfd_link_hash_new)
01161 {
01162 h->type = bfd_link_hash_undefined;
01163 h->u.undef.abfd = abfd;
01164
01165
01166
01167 }
01168
01169 return TRUE;
01170 }
01171
01172
01173
01174
01175
01176
01177 static bfd_boolean
01178 constructor_callback (struct bfd_link_info *info,
01179 bfd_boolean constructor,
01180 const char *name,
01181 bfd *abfd,
01182 asection *section,
01183 bfd_vma value)
01184 {
01185 char *s;
01186 struct bfd_link_hash_entry *h;
01187 char set_name[1 + sizeof "__CTOR_LIST__"];
01188
01189 if (config.warn_constructors)
01190 einfo (_("%P: warning: global constructor %s used\n"), name);
01191
01192 if (! config.build_constructors)
01193 return TRUE;
01194
01195
01196
01197 if (bfd_reloc_type_lookup (output_bfd, BFD_RELOC_CTOR) == NULL
01198 && (link_info.relocatable
01199 || bfd_reloc_type_lookup (abfd, BFD_RELOC_CTOR) == NULL))
01200 einfo (_("%P%F: BFD backend error: BFD_RELOC_CTOR unsupported\n"));
01201
01202 s = set_name;
01203 if (bfd_get_symbol_leading_char (abfd) != '\0')
01204 *s++ = bfd_get_symbol_leading_char (abfd);
01205 if (constructor)
01206 strcpy (s, "__CTOR_LIST__");
01207 else
01208 strcpy (s, "__DTOR_LIST__");
01209
01210 h = bfd_link_hash_lookup (info->hash, set_name, TRUE, TRUE, TRUE);
01211 if (h == (struct bfd_link_hash_entry *) NULL)
01212 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
01213 if (h->type == bfd_link_hash_new)
01214 {
01215 h->type = bfd_link_hash_undefined;
01216 h->u.undef.abfd = abfd;
01217
01218
01219
01220 }
01221
01222 ldctor_add_set_entry (h, BFD_RELOC_CTOR, name, section, value);
01223 return TRUE;
01224 }
01225
01226
01227
01228
01229 struct warning_callback_info
01230 {
01231 bfd_boolean found;
01232 const char *warning;
01233 const char *symbol;
01234 asymbol **asymbols;
01235 };
01236
01237
01238
01239 static bfd_boolean
01240 warning_callback (struct bfd_link_info *info ATTRIBUTE_UNUSED,
01241 const char *warning,
01242 const char *symbol,
01243 bfd *abfd,
01244 asection *section,
01245 bfd_vma address)
01246 {
01247
01248
01249 if (! config.warn_multiple_gp
01250 && strcmp (warning, "using multiple gp values") == 0)
01251 return TRUE;
01252
01253 if (section != NULL)
01254 einfo ("%C: %s%s\n", abfd, section, address, _("warning: "), warning);
01255 else if (abfd == NULL)
01256 einfo ("%P: %s%s\n", _("warning: "), warning);
01257 else if (symbol == NULL)
01258 einfo ("%B: %s%s\n", abfd, _("warning: "), warning);
01259 else
01260 {
01261 lang_input_statement_type *entry;
01262 asymbol **asymbols;
01263 struct warning_callback_info info;
01264
01265
01266
01267 entry = (lang_input_statement_type *) abfd->usrdata;
01268 if (entry != NULL && entry->asymbols != NULL)
01269 asymbols = entry->asymbols;
01270 else
01271 {
01272 long symsize;
01273 long symbol_count;
01274
01275 symsize = bfd_get_symtab_upper_bound (abfd);
01276 if (symsize < 0)
01277 einfo (_("%B%F: could not read symbols: %E\n"), abfd);
01278 asymbols = xmalloc (symsize);
01279 symbol_count = bfd_canonicalize_symtab (abfd, asymbols);
01280 if (symbol_count < 0)
01281 einfo (_("%B%F: could not read symbols: %E\n"), abfd);
01282 if (entry != NULL)
01283 {
01284 entry->asymbols = asymbols;
01285 entry->symbol_count = symbol_count;
01286 }
01287 }
01288
01289 info.found = FALSE;
01290 info.warning = warning;
01291 info.symbol = symbol;
01292 info.asymbols = asymbols;
01293 bfd_map_over_sections (abfd, warning_find_reloc, &info);
01294
01295 #ifndef KEY // Remove 'deprecated' warning.
01296 if (! info.found)
01297 einfo ("%B: %s%s\n", abfd, _("warning: "), warning);
01298 #endif
01299
01300 if (entry == NULL)
01301 free (asymbols);
01302 }
01303
01304 return TRUE;
01305 }
01306
01307
01308
01309
01310
01311
01312 static void
01313 warning_find_reloc (bfd *abfd, asection *sec, void *iarg)
01314 {
01315 struct warning_callback_info *info = iarg;
01316 long relsize;
01317 arelent **relpp;
01318 long relcount;
01319 arelent **p, **pend;
01320
01321 if (info->found)
01322 return;
01323
01324 relsize = bfd_get_reloc_upper_bound (abfd, sec);
01325 if (relsize < 0)
01326 einfo (_("%B%F: could not read relocs: %E\n"), abfd);
01327 if (relsize == 0)
01328 return;
01329
01330 relpp = xmalloc (relsize);
01331 relcount = bfd_canonicalize_reloc (abfd, sec, relpp, info->asymbols);
01332 if (relcount < 0)
01333 einfo (_("%B%F: could not read relocs: %E\n"), abfd);
01334
01335 p = relpp;
01336 pend = p + relcount;
01337 for (; p < pend && *p != NULL; p++)
01338 {
01339 arelent *q = *p;
01340
01341 if (q->sym_ptr_ptr != NULL
01342 && *q->sym_ptr_ptr != NULL
01343 && strcmp (bfd_asymbol_name (*q->sym_ptr_ptr), info->symbol) == 0)
01344 {
01345
01346 einfo ("%C: %s%s\n", abfd, sec, q->address, _("warning: "),
01347 info->warning);
01348 info->found = TRUE;
01349 break;
01350 }
01351 }
01352
01353 free (relpp);
01354 }
01355
01356
01357
01358 static bfd_boolean
01359 undefined_symbol (struct bfd_link_info *info ATTRIBUTE_UNUSED,
01360 const char *name,
01361 bfd *abfd,
01362 asection *section,
01363 bfd_vma address,
01364 bfd_boolean error)
01365 {
01366 static char *error_name;
01367 static unsigned int error_count;
01368
01369 #define MAX_ERRORS_IN_A_ROW 5
01370
01371 if (config.warn_once)
01372 {
01373 static struct bfd_hash_table *hash;
01374
01375
01376 if (hash == NULL)
01377 {
01378 hash = xmalloc (sizeof (struct bfd_hash_table));
01379 if (! bfd_hash_table_init (hash, bfd_hash_newfunc))
01380 einfo (_("%F%P: bfd_hash_table_init failed: %E\n"));
01381 }
01382
01383 if (bfd_hash_lookup (hash, name, FALSE, FALSE) != NULL)
01384 return TRUE;
01385
01386 if (bfd_hash_lookup (hash, name, TRUE, TRUE) == NULL)
01387 einfo (_("%F%P: bfd_hash_lookup failed: %E\n"));
01388 }
01389
01390
01391
01392 if (error_name != NULL
01393 && strcmp (name, error_name) == 0)
01394 ++error_count;
01395 else
01396 {
01397 error_count = 0;
01398 if (error_name != NULL)
01399 free (error_name);
01400 error_name = xstrdup (name);
01401 }
01402
01403 if (section != NULL)
01404 {
01405 if (error_count < MAX_ERRORS_IN_A_ROW)
01406 {
01407 if (error)
01408 einfo (_("%X%C: undefined reference to `%T'\n"),
01409 abfd, section, address, name);
01410 else
01411 einfo (_("%C: warning: undefined reference to `%T'\n"),
01412 abfd, section, address, name);
01413 }
01414 else if (error_count == MAX_ERRORS_IN_A_ROW)
01415 {
01416 if (error)
01417 einfo (_("%X%D: more undefined references to `%T' follow\n"),
01418 abfd, section, address, name);
01419 else
01420 einfo (_("%D: warning: more undefined references to `%T' follow\n"),
01421 abfd, section, address, name);
01422 }
01423 else if (error)
01424 einfo ("%X");
01425 }
01426 else
01427 {
01428 if (error_count < MAX_ERRORS_IN_A_ROW)
01429 {
01430 if (error)
01431 einfo (_("%X%B: undefined reference to `%T'\n"),
01432 abfd, name);
01433 else
01434 einfo (_("%B: warning: undefined reference to `%T'\n"),
01435 abfd, name);
01436 }
01437 else if (error_count == MAX_ERRORS_IN_A_ROW)
01438 {
01439 if (error)
01440 einfo (_("%X%B: more undefined references to `%T' follow\n"),
01441 abfd, name);
01442 else
01443 einfo (_("%B: warning: more undefined references to `%T' follow\n"),
01444 abfd, name);
01445 }
01446 else if (error)
01447 einfo ("%X");
01448 }
01449
01450 return TRUE;
01451 }
01452
01453
01454
01455
01456
01457
01458
01459
01460 int overflow_cutoff_limit = 10;
01461
01462
01463
01464 static bfd_boolean
01465 reloc_overflow (struct bfd_link_info *info ATTRIBUTE_UNUSED,
01466 struct bfd_link_hash_entry *entry,
01467 const char *name,
01468 const char *reloc_name,
01469 bfd_vma addend,
01470 bfd *abfd,
01471 asection *section,
01472 bfd_vma address)
01473 {
01474 if (overflow_cutoff_limit == -1)
01475 return TRUE;
01476
01477 if (abfd == NULL)
01478 einfo (_("%P%X: generated"));
01479 else
01480 einfo ("%X%C:", abfd, section, address);
01481
01482 if (overflow_cutoff_limit >= 0
01483 && overflow_cutoff_limit-- == 0)
01484 {
01485 einfo (_(" additional relocation overflows omitted from the output\n"));
01486 return TRUE;
01487 }
01488
01489 if (entry)
01490 {
01491 while (entry->type == bfd_link_hash_indirect
01492 || entry->type == bfd_link_hash_warning)
01493 entry = entry->u.i.link;
01494 switch (entry->type)
01495 {
01496 case bfd_link_hash_undefined:
01497 case bfd_link_hash_undefweak:
01498 einfo (_(" relocation truncated to fit: %s against undefined symbol `%T'"),
01499 reloc_name, entry->root.string);
01500 break;
01501 case bfd_link_hash_defined:
01502 case bfd_link_hash_defweak:
01503 einfo (_(" relocation truncated to fit: %s against symbol `%T' defined in %A section in %B"),
01504 reloc_name, entry->root.string,
01505 entry->u.def.section, entry->u.def.section->owner);
01506 break;
01507 default:
01508 abort ();
01509 break;
01510 }
01511 }
01512 else
01513 einfo (_(" relocation truncated to fit: %s against `%T'"),
01514 reloc_name, name);
01515 if (addend != 0)
01516 einfo ("+%v", addend);
01517 einfo ("\n");
01518 return TRUE;
01519 }
01520
01521
01522
01523 static bfd_boolean
01524 reloc_dangerous (struct bfd_link_info *info ATTRIBUTE_UNUSED,
01525 const char *message,
01526 bfd *abfd,
01527 asection *section,
01528 bfd_vma address)
01529 {
01530 if (abfd == NULL)
01531 einfo (_("%P%X: generated"));
01532 else
01533 einfo ("%X%C:", abfd, section, address);
01534 einfo (_("dangerous relocation: %s\n"), message);
01535 return TRUE;
01536 }
01537
01538
01539
01540
01541 static bfd_boolean
01542 unattached_reloc (struct bfd_link_info *info ATTRIBUTE_UNUSED,
01543 const char *name,
01544 bfd *abfd,
01545 asection *section,
01546 bfd_vma address)
01547 {
01548 if (abfd == NULL)
01549 einfo (_("%P%X: generated"));
01550 else
01551 einfo ("%X%C:", abfd, section, address);
01552 einfo (_(" reloc refers to symbol `%T' which is not being output\n"), name);
01553 return TRUE;
01554 }
01555
01556
01557
01558
01559
01560 static bfd_boolean
01561 notice (struct bfd_link_info *info,
01562 const char *name,
01563 bfd *abfd,
01564 asection *section,
01565 bfd_vma value)
01566 {
01567 if (! info->notice_all
01568 || (info->notice_hash != NULL
01569 && bfd_hash_lookup (info->notice_hash, name, FALSE, FALSE) != NULL))
01570 {
01571 if (bfd_is_und_section (section))
01572 einfo ("%B: reference to %s\n", abfd, name);
01573 else
01574 einfo ("%B: definition of %s\n", abfd, name);
01575 }
01576
01577 if (command_line.cref || nocrossref_list != NULL)
01578 add_cref (name, abfd, section, value);
01579
01580 return TRUE;
01581 }