Subversion Repositories Kolibri OS

Rev

Rev 5199 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

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