Home | History | Annotate | Line # | Download | only in ld
ldmain.c revision 1.1.1.2
      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 /* Record a symbol to be wrapped, from the --wrap option.  */
    699 
    700 void
    701 add_wrap (const char *name)
    702 {
    703   if (link_info.wrap_hash == NULL)
    704     {
    705       link_info.wrap_hash =
    706           (struct bfd_hash_table *) xmalloc (sizeof (struct bfd_hash_table));
    707       if (!bfd_hash_table_init_n (link_info.wrap_hash,
    708 				  bfd_hash_newfunc,
    709 				  sizeof (struct bfd_hash_entry),
    710 				  61))
    711 	einfo (_("%P%F: bfd_hash_table_init failed: %E\n"));
    712     }
    713 
    714   if (bfd_hash_lookup (link_info.wrap_hash, name, TRUE, TRUE) == NULL)
    715     einfo (_("%P%F: bfd_hash_lookup failed: %E\n"));
    716 }
    717 
    718 /* Handle the -retain-symbols-file option.  */
    719 
    720 void
    721 add_keepsyms_file (const char *filename)
    722 {
    723   FILE *file;
    724   char *buf;
    725   size_t bufsize;
    726   int c;
    727 
    728   if (link_info.strip == strip_some)
    729     einfo (_("%X%P: error: duplicate retain-symbols-file\n"));
    730 
    731   file = fopen (filename, "r");
    732   if (file == NULL)
    733     {
    734       bfd_set_error (bfd_error_system_call);
    735       einfo ("%X%P: %s: %E\n", filename);
    736       return;
    737     }
    738 
    739   link_info.keep_hash = (struct bfd_hash_table *)
    740       xmalloc (sizeof (struct bfd_hash_table));
    741   if (!bfd_hash_table_init (link_info.keep_hash, bfd_hash_newfunc,
    742 			    sizeof (struct bfd_hash_entry)))
    743     einfo (_("%P%F: bfd_hash_table_init failed: %E\n"));
    744 
    745   bufsize = 100;
    746   buf = (char *) xmalloc (bufsize);
    747 
    748   c = getc (file);
    749   while (c != EOF)
    750     {
    751       while (ISSPACE (c))
    752 	c = getc (file);
    753 
    754       if (c != EOF)
    755 	{
    756 	  size_t len = 0;
    757 
    758 	  while (! ISSPACE (c) && c != EOF)
    759 	    {
    760 	      buf[len] = c;
    761 	      ++len;
    762 	      if (len >= bufsize)
    763 		{
    764 		  bufsize *= 2;
    765 		  buf = (char *) xrealloc (buf, bufsize);
    766 		}
    767 	      c = getc (file);
    768 	    }
    769 
    770 	  buf[len] = '\0';
    771 
    772 	  if (bfd_hash_lookup (link_info.keep_hash, buf, TRUE, TRUE) == NULL)
    773 	    einfo (_("%P%F: bfd_hash_lookup for insertion failed: %E\n"));
    774 	}
    775     }
    776 
    777   if (link_info.strip != strip_none)
    778     einfo (_("%P: `-retain-symbols-file' overrides `-s' and `-S'\n"));
    779 
    780   free (buf);
    781   link_info.strip = strip_some;
    782 }
    783 
    784 /* Callbacks from the BFD linker routines.  */
    786 
    787 /* This is called when BFD has decided to include an archive member in
    788    a link.  */
    789 
    790 static bfd_boolean
    791 add_archive_element (struct bfd_link_info *info,
    792 		     bfd *abfd,
    793 		     const char *name,
    794 		     bfd **subsbfd ATTRIBUTE_UNUSED)
    795 {
    796   lang_input_statement_type *input;
    797   lang_input_statement_type orig_input;
    798 
    799   input = (lang_input_statement_type *)
    800       xcalloc (1, sizeof (lang_input_statement_type));
    801   input->filename = abfd->filename;
    802   input->local_sym_name = abfd->filename;
    803   input->the_bfd = abfd;
    804 
    805   /* Save the original data for trace files/tries below, as plugins
    806      (if enabled) may possibly alter it to point to a replacement
    807      BFD, but we still want to output the original BFD filename.  */
    808   orig_input = *input;
    809 #ifdef ENABLE_PLUGINS
    810   if (bfd_my_archive (abfd) != NULL
    811       && plugin_active_plugins_p ()
    812       && !no_more_claiming)
    813     {
    814       /* We must offer this archive member to the plugins to claim.  */
    815       int fd = open (bfd_my_archive (abfd)->filename, O_RDONLY | O_BINARY);
    816       if (fd >= 0)
    817 	{
    818 	  struct ld_plugin_input_file file;
    819 
    820 	  /* Offset and filesize must refer to the individual archive
    821 	     member, not the whole file, and must exclude the header.
    822 	     Fortunately for us, that is how the data is stored in the
    823 	     origin field of the bfd and in the arelt_data.  */
    824 	  file.name = bfd_my_archive (abfd)->filename;
    825 	  file.offset = abfd->origin;
    826 	  file.filesize = arelt_size (abfd);
    827 	  file.fd = fd;
    828 	  plugin_maybe_claim (&file, input);
    829 	  if (input->claimed)
    830 	    {
    831 	      input->claim_archive = TRUE;
    832 	      *subsbfd = input->the_bfd;
    833 	    }
    834 	}
    835     }
    836 #endif /* ENABLE_PLUGINS */
    837 
    838   ldlang_add_file (input);
    839 
    840   if (config.map_file != NULL)
    841     {
    842       static bfd_boolean header_printed;
    843       struct bfd_link_hash_entry *h;
    844       bfd *from;
    845       int len;
    846 
    847       h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
    848 
    849       if (h == NULL)
    850 	from = NULL;
    851       else
    852 	{
    853 	  switch (h->type)
    854 	    {
    855 	    default:
    856 	      from = NULL;
    857 	      break;
    858 
    859 	    case bfd_link_hash_defined:
    860 	    case bfd_link_hash_defweak:
    861 	      from = h->u.def.section->owner;
    862 	      break;
    863 
    864 	    case bfd_link_hash_undefined:
    865 	    case bfd_link_hash_undefweak:
    866 	      from = h->u.undef.abfd;
    867 	      break;
    868 
    869 	    case bfd_link_hash_common:
    870 	      from = h->u.c.p->section->owner;
    871 	      break;
    872 	    }
    873 	}
    874 
    875       if (! header_printed)
    876 	{
    877 	  char buf[100];
    878 
    879 	  sprintf (buf, _("Archive member included because of file (symbol)\n\n"));
    880 	  minfo ("%s", buf);
    881 	  header_printed = TRUE;
    882 	}
    883 
    884       if (bfd_my_archive (abfd) == NULL)
    885 	{
    886 	  minfo ("%s", bfd_get_filename (abfd));
    887 	  len = strlen (bfd_get_filename (abfd));
    888 	}
    889       else
    890 	{
    891 	  minfo ("%s(%s)", bfd_get_filename (bfd_my_archive (abfd)),
    892 		 bfd_get_filename (abfd));
    893 	  len = (strlen (bfd_get_filename (bfd_my_archive (abfd)))
    894 		 + strlen (bfd_get_filename (abfd))
    895 		 + 2);
    896 	}
    897 
    898       if (len >= 29)
    899 	{
    900 	  print_nl ();
    901 	  len = 0;
    902 	}
    903       while (len < 30)
    904 	{
    905 	  print_space ();
    906 	  ++len;
    907 	}
    908 
    909       if (from != NULL)
    910 	minfo ("%B ", from);
    911       if (h != NULL)
    912 	minfo ("(%T)\n", h->root.string);
    913       else
    914 	minfo ("(%s)\n", name);
    915     }
    916 
    917   if (trace_files || trace_file_tries)
    918     info_msg ("%I\n", &orig_input);
    919   return TRUE;
    920 }
    921 
    922 /* This is called when BFD has discovered a symbol which is defined
    923    multiple times.  */
    924 
    925 static bfd_boolean
    926 multiple_definition (struct bfd_link_info *info,
    927 		     struct bfd_link_hash_entry *h,
    928 		     bfd *nbfd,
    929 		     asection *nsec,
    930 		     bfd_vma nval)
    931 {
    932   const char *name;
    933   bfd *obfd;
    934   asection *osec;
    935   bfd_vma oval;
    936 
    937   if (info->allow_multiple_definition)
    938     return TRUE;
    939 
    940   switch (h->type)
    941     {
    942     case bfd_link_hash_defined:
    943       osec = h->u.def.section;
    944       oval = h->u.def.value;
    945       obfd = h->u.def.section->owner;
    946       break;
    947     case bfd_link_hash_indirect:
    948       osec = bfd_ind_section_ptr;
    949       oval = 0;
    950       obfd = NULL;
    951       break;
    952     default:
    953       abort ();
    954     }
    955 
    956   /* Ignore a redefinition of an absolute symbol to the
    957      same value; it's harmless.  */
    958   if (h->type == bfd_link_hash_defined
    959       && bfd_is_abs_section (osec)
    960       && bfd_is_abs_section (nsec)
    961       && nval == oval)
    962     return TRUE;
    963 
    964   /* If either section has the output_section field set to
    965      bfd_abs_section_ptr, it means that the section is being
    966      discarded, and this is not really a multiple definition at all.
    967      FIXME: It would be cleaner to somehow ignore symbols defined in
    968      sections which are being discarded.  */
    969   if ((osec->output_section != NULL
    970        && ! bfd_is_abs_section (osec)
    971        && bfd_is_abs_section (osec->output_section))
    972       || (nsec->output_section != NULL
    973 	  && ! bfd_is_abs_section (nsec)
    974 	  && bfd_is_abs_section (nsec->output_section)))
    975     return TRUE;
    976 
    977   name = h->root.string;
    978   if (nbfd == NULL)
    979     {
    980       nbfd = obfd;
    981       nsec = osec;
    982       nval = oval;
    983       obfd = NULL;
    984     }
    985   einfo (_("%X%C: multiple definition of `%T'\n"),
    986 	 nbfd, nsec, nval, name);
    987   if (obfd != NULL)
    988     einfo (_("%D: first defined here\n"), obfd, osec, oval);
    989 
    990   if (RELAXATION_ENABLED)
    991     {
    992       einfo (_("%P: Disabling relaxation: it will not work with multiple definitions\n"));
    993       command_line.disable_target_specific_optimizations = -1;
    994     }
    995 
    996   return TRUE;
    997 }
    998 
    999 /* This is called when there is a definition of a common symbol, or
   1000    when a common symbol is found for a symbol that is already defined,
   1001    or when two common symbols are found.  We only do something if
   1002    -warn-common was used.  */
   1003 
   1004 static bfd_boolean
   1005 multiple_common (struct bfd_link_info *info ATTRIBUTE_UNUSED,
   1006 		 struct bfd_link_hash_entry *h,
   1007 		 bfd *nbfd,
   1008 		 enum bfd_link_hash_type ntype,
   1009 		 bfd_vma nsize)
   1010 {
   1011   const char *name;
   1012   bfd *obfd;
   1013   enum bfd_link_hash_type otype;
   1014   bfd_vma osize;
   1015 
   1016   if (!config.warn_common)
   1017     return TRUE;
   1018 
   1019   name = h->root.string;
   1020   otype = h->type;
   1021   if (otype == bfd_link_hash_common)
   1022     {
   1023       obfd = h->u.c.p->section->owner;
   1024       osize = h->u.c.size;
   1025     }
   1026   else if (otype == bfd_link_hash_defined
   1027 	   || otype == bfd_link_hash_defweak)
   1028     {
   1029       obfd = h->u.def.section->owner;
   1030       osize = 0;
   1031     }
   1032   else
   1033     {
   1034       /* FIXME: It would nice if we could report the BFD which defined
   1035 	 an indirect symbol, but we don't have anywhere to store the
   1036 	 information.  */
   1037       obfd = NULL;
   1038       osize = 0;
   1039     }
   1040 
   1041   if (ntype == bfd_link_hash_defined
   1042       || ntype == bfd_link_hash_defweak
   1043       || ntype == bfd_link_hash_indirect)
   1044     {
   1045       ASSERT (otype == bfd_link_hash_common);
   1046       einfo (_("%B: warning: definition of `%T' overriding common\n"),
   1047 	     nbfd, name);
   1048       if (obfd != NULL)
   1049 	einfo (_("%B: warning: common is here\n"), obfd);
   1050     }
   1051   else if (otype == bfd_link_hash_defined
   1052 	   || otype == bfd_link_hash_defweak
   1053 	   || otype == bfd_link_hash_indirect)
   1054     {
   1055       ASSERT (ntype == bfd_link_hash_common);
   1056       einfo (_("%B: warning: common of `%T' overridden by definition\n"),
   1057 	     nbfd, name);
   1058       if (obfd != NULL)
   1059 	einfo (_("%B: warning: defined here\n"), obfd);
   1060     }
   1061   else
   1062     {
   1063       ASSERT (otype == bfd_link_hash_common && ntype == bfd_link_hash_common);
   1064       if (osize > nsize)
   1065 	{
   1066 	  einfo (_("%B: warning: common of `%T' overridden by larger common\n"),
   1067 		 nbfd, name);
   1068 	  if (obfd != NULL)
   1069 	    einfo (_("%B: warning: larger common is here\n"), obfd);
   1070 	}
   1071       else if (nsize > osize)
   1072 	{
   1073 	  einfo (_("%B: warning: common of `%T' overriding smaller common\n"),
   1074 		 nbfd, name);
   1075 	  if (obfd != NULL)
   1076 	    einfo (_("%B: warning: smaller common is here\n"), obfd);
   1077 	}
   1078       else
   1079 	{
   1080 	  einfo (_("%B: warning: multiple common of `%T'\n"), nbfd, name);
   1081 	  if (obfd != NULL)
   1082 	    einfo (_("%B: warning: previous common is here\n"), obfd);
   1083 	}
   1084     }
   1085 
   1086   return TRUE;
   1087 }
   1088 
   1089 /* This is called when BFD has discovered a set element.  H is the
   1090    entry in the linker hash table for the set.  SECTION and VALUE
   1091    represent a value which should be added to the set.  */
   1092 
   1093 static bfd_boolean
   1094 add_to_set (struct bfd_link_info *info ATTRIBUTE_UNUSED,
   1095 	    struct bfd_link_hash_entry *h,
   1096 	    bfd_reloc_code_real_type reloc,
   1097 	    bfd *abfd,
   1098 	    asection *section,
   1099 	    bfd_vma value)
   1100 {
   1101   if (config.warn_constructors)
   1102     einfo (_("%P: warning: global constructor %s used\n"),
   1103 	   h->root.string);
   1104 
   1105   if (! config.build_constructors)
   1106     return TRUE;
   1107 
   1108   ldctor_add_set_entry (h, reloc, NULL, section, value);
   1109 
   1110   if (h->type == bfd_link_hash_new)
   1111     {
   1112       h->type = bfd_link_hash_undefined;
   1113       h->u.undef.abfd = abfd;
   1114       /* We don't call bfd_link_add_undef to add this to the list of
   1115 	 undefined symbols because we are going to define it
   1116 	 ourselves.  */
   1117     }
   1118 
   1119   return TRUE;
   1120 }
   1121 
   1122 /* This is called when BFD has discovered a constructor.  This is only
   1123    called for some object file formats--those which do not handle
   1124    constructors in some more clever fashion.  This is similar to
   1125    adding an element to a set, but less general.  */
   1126 
   1127 static bfd_boolean
   1128 constructor_callback (struct bfd_link_info *info,
   1129 		      bfd_boolean constructor,
   1130 		      const char *name,
   1131 		      bfd *abfd,
   1132 		      asection *section,
   1133 		      bfd_vma value)
   1134 {
   1135   char *s;
   1136   struct bfd_link_hash_entry *h;
   1137   char set_name[1 + sizeof "__CTOR_LIST__"];
   1138 
   1139   if (config.warn_constructors)
   1140     einfo (_("%P: warning: global constructor %s used\n"), name);
   1141 
   1142   if (! config.build_constructors)
   1143     return TRUE;
   1144 
   1145   /* Ensure that BFD_RELOC_CTOR exists now, so that we can give a
   1146      useful error message.  */
   1147   if (bfd_reloc_type_lookup (link_info.output_bfd, BFD_RELOC_CTOR) == NULL
   1148       && (info->relocatable
   1149 	  || bfd_reloc_type_lookup (abfd, BFD_RELOC_CTOR) == NULL))
   1150     einfo (_("%P%F: BFD backend error: BFD_RELOC_CTOR unsupported\n"));
   1151 
   1152   s = set_name;
   1153   if (bfd_get_symbol_leading_char (abfd) != '\0')
   1154     *s++ = bfd_get_symbol_leading_char (abfd);
   1155   if (constructor)
   1156     strcpy (s, "__CTOR_LIST__");
   1157   else
   1158     strcpy (s, "__DTOR_LIST__");
   1159 
   1160   h = bfd_link_hash_lookup (info->hash, set_name, TRUE, TRUE, TRUE);
   1161   if (h == (struct bfd_link_hash_entry *) NULL)
   1162     einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
   1163   if (h->type == bfd_link_hash_new)
   1164     {
   1165       h->type = bfd_link_hash_undefined;
   1166       h->u.undef.abfd = abfd;
   1167       /* We don't call bfd_link_add_undef to add this to the list of
   1168 	 undefined symbols because we are going to define it
   1169 	 ourselves.  */
   1170     }
   1171 
   1172   ldctor_add_set_entry (h, BFD_RELOC_CTOR, name, section, value);
   1173   return TRUE;
   1174 }
   1175 
   1176 /* A structure used by warning_callback to pass information through
   1177    bfd_map_over_sections.  */
   1178 
   1179 struct warning_callback_info
   1180 {
   1181   bfd_boolean found;
   1182   const char *warning;
   1183   const char *symbol;
   1184   asymbol **asymbols;
   1185 };
   1186 
   1187 /* This is called when there is a reference to a warning symbol.  */
   1188 
   1189 static bfd_boolean
   1190 warning_callback (struct bfd_link_info *info ATTRIBUTE_UNUSED,
   1191 		  const char *warning,
   1192 		  const char *symbol,
   1193 		  bfd *abfd,
   1194 		  asection *section,
   1195 		  bfd_vma address)
   1196 {
   1197   /* This is a hack to support warn_multiple_gp.  FIXME: This should
   1198      have a cleaner interface, but what?  */
   1199   if (! config.warn_multiple_gp
   1200       && strcmp (warning, "using multiple gp values") == 0)
   1201     return TRUE;
   1202 
   1203   if (section != NULL)
   1204     einfo ("%C: %s%s\n", abfd, section, address, _("warning: "), warning);
   1205   else if (abfd == NULL)
   1206     einfo ("%P: %s%s\n", _("warning: "), warning);
   1207   else if (symbol == NULL)
   1208     einfo ("%B: %s%s\n", abfd, _("warning: "), warning);
   1209   else
   1210     {
   1211       struct warning_callback_info cinfo;
   1212 
   1213       /* Look through the relocs to see if we can find a plausible
   1214 	 address.  */
   1215 
   1216       if (!bfd_generic_link_read_symbols (abfd))
   1217 	einfo (_("%B%F: could not read symbols: %E\n"), abfd);
   1218 
   1219       cinfo.found = FALSE;
   1220       cinfo.warning = warning;
   1221       cinfo.symbol = symbol;
   1222       cinfo.asymbols = bfd_get_outsymbols (abfd);
   1223       bfd_map_over_sections (abfd, warning_find_reloc, &cinfo);
   1224 
   1225       if (! cinfo.found)
   1226 	einfo ("%B: %s%s\n", abfd, _("warning: "), warning);
   1227     }
   1228 
   1229   return TRUE;
   1230 }
   1231 
   1232 /* This is called by warning_callback for each section.  It checks the
   1233    relocs of the section to see if it can find a reference to the
   1234    symbol which triggered the warning.  If it can, it uses the reloc
   1235    to give an error message with a file and line number.  */
   1236 
   1237 static void
   1238 warning_find_reloc (bfd *abfd, asection *sec, void *iarg)
   1239 {
   1240   struct warning_callback_info *info = (struct warning_callback_info *) iarg;
   1241   long relsize;
   1242   arelent **relpp;
   1243   long relcount;
   1244   arelent **p, **pend;
   1245 
   1246   if (info->found)
   1247     return;
   1248 
   1249   relsize = bfd_get_reloc_upper_bound (abfd, sec);
   1250   if (relsize < 0)
   1251     einfo (_("%B%F: could not read relocs: %E\n"), abfd);
   1252   if (relsize == 0)
   1253     return;
   1254 
   1255   relpp = (arelent **) xmalloc (relsize);
   1256   relcount = bfd_canonicalize_reloc (abfd, sec, relpp, info->asymbols);
   1257   if (relcount < 0)
   1258     einfo (_("%B%F: could not read relocs: %E\n"), abfd);
   1259 
   1260   p = relpp;
   1261   pend = p + relcount;
   1262   for (; p < pend && *p != NULL; p++)
   1263     {
   1264       arelent *q = *p;
   1265 
   1266       if (q->sym_ptr_ptr != NULL
   1267 	  && *q->sym_ptr_ptr != NULL
   1268 	  && strcmp (bfd_asymbol_name (*q->sym_ptr_ptr), info->symbol) == 0)
   1269 	{
   1270 	  /* We found a reloc for the symbol we are looking for.  */
   1271 	  einfo ("%C: %s%s\n", abfd, sec, q->address, _("warning: "),
   1272 		 info->warning);
   1273 	  info->found = TRUE;
   1274 	  break;
   1275 	}
   1276     }
   1277 
   1278   free (relpp);
   1279 }
   1280 
   1281 /* This is called when an undefined symbol is found.  */
   1282 
   1283 static bfd_boolean
   1284 undefined_symbol (struct bfd_link_info *info ATTRIBUTE_UNUSED,
   1285 		  const char *name,
   1286 		  bfd *abfd,
   1287 		  asection *section,
   1288 		  bfd_vma address,
   1289 		  bfd_boolean error)
   1290 {
   1291   static char *error_name;
   1292   static unsigned int error_count;
   1293 
   1294 #define MAX_ERRORS_IN_A_ROW 5
   1295 
   1296   if (config.warn_once)
   1297     {
   1298       static struct bfd_hash_table *hash;
   1299 
   1300       /* Only warn once about a particular undefined symbol.  */
   1301       if (hash == NULL)
   1302 	{
   1303 	  hash = (struct bfd_hash_table *)
   1304               xmalloc (sizeof (struct bfd_hash_table));
   1305 	  if (!bfd_hash_table_init (hash, bfd_hash_newfunc,
   1306 				    sizeof (struct bfd_hash_entry)))
   1307 	    einfo (_("%F%P: bfd_hash_table_init failed: %E\n"));
   1308 	}
   1309 
   1310       if (bfd_hash_lookup (hash, name, FALSE, FALSE) != NULL)
   1311 	return TRUE;
   1312 
   1313       if (bfd_hash_lookup (hash, name, TRUE, TRUE) == NULL)
   1314 	einfo (_("%F%P: bfd_hash_lookup failed: %E\n"));
   1315     }
   1316 
   1317   /* We never print more than a reasonable number of errors in a row
   1318      for a single symbol.  */
   1319   if (error_name != NULL
   1320       && strcmp (name, error_name) == 0)
   1321     ++error_count;
   1322   else
   1323     {
   1324       error_count = 0;
   1325       if (error_name != NULL)
   1326 	free (error_name);
   1327       error_name = xstrdup (name);
   1328     }
   1329 
   1330   if (section != NULL)
   1331     {
   1332       if (error_count < MAX_ERRORS_IN_A_ROW)
   1333 	{
   1334 	  if (error)
   1335 	    einfo (_("%X%C: undefined reference to `%T'\n"),
   1336 		   abfd, section, address, name);
   1337 	  else
   1338 	    einfo (_("%C: warning: undefined reference to `%T'\n"),
   1339 		   abfd, section, address, name);
   1340 	}
   1341       else if (error_count == MAX_ERRORS_IN_A_ROW)
   1342 	{
   1343 	  if (error)
   1344 	    einfo (_("%X%D: more undefined references to `%T' follow\n"),
   1345 		   abfd, section, address, name);
   1346 	  else
   1347 	    einfo (_("%D: warning: more undefined references to `%T' follow\n"),
   1348 		   abfd, section, address, name);
   1349 	}
   1350       else if (error)
   1351 	einfo ("%X");
   1352     }
   1353   else
   1354     {
   1355       if (error_count < MAX_ERRORS_IN_A_ROW)
   1356 	{
   1357 	  if (error)
   1358 	    einfo (_("%X%B: undefined reference to `%T'\n"),
   1359 		   abfd, name);
   1360 	  else
   1361 	    einfo (_("%B: warning: undefined reference to `%T'\n"),
   1362 		   abfd, name);
   1363 	}
   1364       else if (error_count == MAX_ERRORS_IN_A_ROW)
   1365 	{
   1366 	  if (error)
   1367 	    einfo (_("%X%B: more undefined references to `%T' follow\n"),
   1368 		   abfd, name);
   1369 	  else
   1370 	    einfo (_("%B: warning: more undefined references to `%T' follow\n"),
   1371 		   abfd, name);
   1372 	}
   1373       else if (error)
   1374 	einfo ("%X");
   1375     }
   1376 
   1377   return TRUE;
   1378 }
   1379 
   1380 /* Counter to limit the number of relocation overflow error messages
   1381    to print.  Errors are printed as it is decremented.  When it's
   1382    called and the counter is zero, a final message is printed
   1383    indicating more relocations were omitted.  When it gets to -1, no
   1384    such errors are printed.  If it's initially set to a value less
   1385    than -1, all such errors will be printed (--verbose does this).  */
   1386 
   1387 int overflow_cutoff_limit = 10;
   1388 
   1389 /* This is called when a reloc overflows.  */
   1390 
   1391 static bfd_boolean
   1392 reloc_overflow (struct bfd_link_info *info ATTRIBUTE_UNUSED,
   1393 		struct bfd_link_hash_entry *entry,
   1394 		const char *name,
   1395 		const char *reloc_name,
   1396 		bfd_vma addend,
   1397 		bfd *abfd,
   1398 		asection *section,
   1399 		bfd_vma address)
   1400 {
   1401   if (overflow_cutoff_limit == -1)
   1402     return TRUE;
   1403 
   1404   einfo ("%X%C:", abfd, section, address);
   1405 
   1406   if (overflow_cutoff_limit >= 0
   1407       && overflow_cutoff_limit-- == 0)
   1408     {
   1409       einfo (_(" additional relocation overflows omitted from the output\n"));
   1410       return TRUE;
   1411     }
   1412 
   1413   if (entry)
   1414     {
   1415       while (entry->type == bfd_link_hash_indirect
   1416 	     || entry->type == bfd_link_hash_warning)
   1417 	entry = entry->u.i.link;
   1418       switch (entry->type)
   1419 	{
   1420 	case bfd_link_hash_undefined:
   1421 	case bfd_link_hash_undefweak:
   1422 	  einfo (_(" relocation truncated to fit: %s against undefined symbol `%T'"),
   1423 		 reloc_name, entry->root.string);
   1424 	  break;
   1425 	case bfd_link_hash_defined:
   1426 	case bfd_link_hash_defweak:
   1427 	  einfo (_(" relocation truncated to fit: %s against symbol `%T' defined in %A section in %B"),
   1428 		 reloc_name, entry->root.string,
   1429 		 entry->u.def.section,
   1430 		 entry->u.def.section == bfd_abs_section_ptr
   1431 		 ? link_info.output_bfd : entry->u.def.section->owner);
   1432 	  break;
   1433 	default:
   1434 	  abort ();
   1435 	  break;
   1436 	}
   1437     }
   1438   else
   1439     einfo (_(" relocation truncated to fit: %s against `%T'"),
   1440 	   reloc_name, name);
   1441   if (addend != 0)
   1442     einfo ("+%v", addend);
   1443   einfo ("\n");
   1444   return TRUE;
   1445 }
   1446 
   1447 /* This is called when a dangerous relocation is made.  */
   1448 
   1449 static bfd_boolean
   1450 reloc_dangerous (struct bfd_link_info *info ATTRIBUTE_UNUSED,
   1451 		 const char *message,
   1452 		 bfd *abfd,
   1453 		 asection *section,
   1454 		 bfd_vma address)
   1455 {
   1456   einfo (_("%X%C: dangerous relocation: %s\n"),
   1457 	 abfd, section, address, message);
   1458   return TRUE;
   1459 }
   1460 
   1461 /* This is called when a reloc is being generated attached to a symbol
   1462    that is not being output.  */
   1463 
   1464 static bfd_boolean
   1465 unattached_reloc (struct bfd_link_info *info ATTRIBUTE_UNUSED,
   1466 		  const char *name,
   1467 		  bfd *abfd,
   1468 		  asection *section,
   1469 		  bfd_vma address)
   1470 {
   1471   einfo (_("%X%C: reloc refers to symbol `%T' which is not being output\n"),
   1472 	 abfd, section, address, name);
   1473   return TRUE;
   1474 }
   1475 
   1476 /* This is called if link_info.notice_all is set, or when a symbol in
   1477    link_info.notice_hash is found.  Symbols are put in notice_hash
   1478    using the -y option, while notice_all is set if the --cref option
   1479    has been supplied, or if there are any NOCROSSREFS sections in the
   1480    linker script; and if plugins are active, since they need to monitor
   1481    all references from non-IR files.  */
   1482 
   1483 static bfd_boolean
   1484 notice (struct bfd_link_info *info,
   1485 	struct bfd_link_hash_entry *h,
   1486 	bfd *abfd,
   1487 	asection *section,
   1488 	bfd_vma value,
   1489 	flagword flags ATTRIBUTE_UNUSED,
   1490 	const char *string ATTRIBUTE_UNUSED)
   1491 {
   1492   const char *name;
   1493 
   1494   if (h == NULL)
   1495     {
   1496       if (command_line.cref || nocrossref_list != NULL)
   1497 	return handle_asneeded_cref (abfd, (enum notice_asneeded_action) value);
   1498       return TRUE;
   1499     }
   1500 
   1501   name = h->root.string;
   1502   if (info->notice_hash != NULL
   1503       && bfd_hash_lookup (info->notice_hash, name, FALSE, FALSE) != NULL)
   1504     {
   1505       if (bfd_is_und_section (section))
   1506 	einfo ("%B: reference to %s\n", abfd, name);
   1507       else
   1508 	einfo ("%B: definition of %s\n", abfd, name);
   1509     }
   1510 
   1511   if (command_line.cref || nocrossref_list != NULL)
   1512     add_cref (name, abfd, section, value);
   1513 
   1514   return TRUE;
   1515 }
   1516