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