Back to index

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