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