Subversion Repositories Kolibri OS

Rev

Rev 5211 | Blame | Compare with Previous | Last modification | View Log | RSS feed

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