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