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