xref: /openbsd-src/gnu/usr.bin/binutils-2.17/ld/ldmain.c (revision 505ee9ea3b177e2387d907a91ca7da069f3f14d8)
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   link_info.emit_hash = TRUE;
317 #ifndef __mips64__
318   link_info.emit_gnu_hash = TRUE;
319 #else
320   link_info.emit_gnu_hash = FALSE;
321 #endif
322   /* SVR4 linkers seem to set DT_INIT and DT_FINI based on magic _init
323      and _fini symbols.  We are compatible.  */
324   link_info.init_function = "_init";
325   link_info.fini_function = "_fini";
326   link_info.pei386_auto_import = -1;
327   link_info.pei386_runtime_pseudo_reloc = FALSE;
328   link_info.spare_dynamic_tags = 5;
329   link_info.flags = 0;
330   link_info.flags_1 = 0;
331   link_info.relax_pass = 1;
332   link_info.warn_shared_textrel = FALSE;
333   link_info.gc_sections = FALSE;
334   link_info.reduce_memory_overheads = FALSE;
335 
336   ldfile_add_arch ("");
337 
338   config.make_executable = TRUE;
339   force_make_executable = FALSE;
340   config.magic_demand_paged = TRUE;
341   config.text_read_only = TRUE;
342   config.data_bss_contig = FALSE;
343 
344   emulation = get_emulation (argc, argv);
345   ldemul_choose_mode (emulation);
346   default_target = ldemul_choose_target (argc, argv);
347   lang_init ();
348   ldemul_before_parse ();
349   lang_has_input_file = FALSE;
350   parse_args (argc, argv);
351 
352   if (config.hash_table_size != 0)
353     bfd_hash_set_default_size (config.hash_table_size);
354 
355   ldemul_set_symbols ();
356 
357   if (! link_info.shared && link_info.pie)
358     {
359       if (link_info.relocatable)
360         link_info.pie = FALSE;
361       else
362         link_info.shared = TRUE;
363     }
364 
365   if (link_info.relocatable)
366     {
367       if (link_info.gc_sections)
368 	einfo ("%P%F: --gc-sections and -r may not be used together\n");
369       else if (command_line.relax)
370 	einfo (_("%P%F: --relax and -r may not be used together\n"));
371       if (link_info.shared)
372 	einfo (_("%P%F: -r and -shared may not be used together\n"));
373     }
374 
375   if (! link_info.shared)
376     {
377       if (command_line.filter_shlib)
378 	einfo (_("%P%F: -F may not be used without -shared\n"));
379       if (command_line.auxiliary_filters)
380 	einfo (_("%P%F: -f may not be used without -shared\n"));
381     }
382 
383   if (! link_info.shared || link_info.pie)
384     link_info.executable = TRUE;
385 
386   if (! config.dynamic_link && link_info.pie)
387     link_info.static_link = TRUE;
388 
389   /* Treat ld -r -s as ld -r -S -x (i.e., strip all local symbols).  I
390      don't see how else this can be handled, since in this case we
391      must preserve all externally visible symbols.  */
392   if (link_info.relocatable && link_info.strip == strip_all)
393     {
394       link_info.strip = strip_debugger;
395       if (link_info.discard == discard_sec_merge)
396 	link_info.discard = discard_all;
397     }
398 
399   /* This essentially adds another -L directory so this must be done after
400      the -L's in argv have been processed.  */
401   set_scripts_dir ();
402 
403   /* If we have not already opened and parsed a linker script
404      read the emulation's appropriate default script.  */
405   if (saved_script_handle == NULL)
406     {
407       int isfile;
408       char *s = ldemul_get_script (&isfile);
409 
410       if (isfile)
411 	ldfile_open_command_file (s);
412       else
413 	{
414 	  lex_string = s;
415 	  lex_redirect (s);
416 	}
417       parser_input = input_script;
418       yyparse ();
419       lex_string = NULL;
420     }
421 
422   if (trace_file_tries)
423     {
424       if (saved_script_handle)
425 	info_msg (_("using external linker script:"));
426       else
427 	info_msg (_("using internal linker script:"));
428       info_msg ("\n==================================================\n");
429 
430       if (saved_script_handle)
431 	{
432 	  static const int ld_bufsz = 8193;
433 	  size_t n;
434 	  char *buf = xmalloc (ld_bufsz);
435 
436 	  rewind (saved_script_handle);
437 	  while ((n = fread (buf, 1, ld_bufsz - 1, saved_script_handle)) > 0)
438 	    {
439 	      buf[n] = 0;
440 	      info_msg (buf);
441 	    }
442 	  rewind (saved_script_handle);
443 	  free (buf);
444 	}
445       else
446 	{
447 	  int isfile;
448 
449 	  info_msg (ldemul_get_script (&isfile));
450 	}
451 
452       info_msg ("\n==================================================\n");
453     }
454 
455   lang_final ();
456 
457   if (!lang_has_input_file)
458     {
459       if (version_printed)
460 	xexit (0);
461       einfo (_("%P%F: no input files\n"));
462     }
463 
464   if (trace_files)
465     info_msg (_("%P: mode %s\n"), emulation);
466 
467   ldemul_after_parse ();
468 
469   if (config.map_filename)
470     {
471       if (strcmp (config.map_filename, "-") == 0)
472 	{
473 	  config.map_file = stdout;
474 	}
475       else
476 	{
477 	  config.map_file = fopen (config.map_filename, FOPEN_WT);
478 	  if (config.map_file == (FILE *) NULL)
479 	    {
480 	      bfd_set_error (bfd_error_system_call);
481 	      einfo (_("%P%F: cannot open map file %s: %E\n"),
482 		     config.map_filename);
483 	    }
484 	}
485     }
486 
487   lang_process ();
488 
489   /* Print error messages for any missing symbols, for any warning
490      symbols, and possibly multiple definitions.  */
491   if (link_info.relocatable)
492     output_bfd->flags &= ~EXEC_P;
493   else
494     output_bfd->flags |= EXEC_P;
495 
496   ldwrite ();
497 
498   if (config.map_file != NULL)
499     lang_map ();
500   if (command_line.cref)
501     output_cref (config.map_file != NULL ? config.map_file : stdout);
502   if (nocrossref_list != NULL)
503     check_nocrossrefs ();
504 
505   lang_finish ();
506 
507   /* Even if we're producing relocatable output, some non-fatal errors should
508      be reported in the exit status.  (What non-fatal errors, if any, do we
509      want to ignore for relocatable output?)  */
510   if (!config.make_executable && !force_make_executable)
511     {
512       if (trace_files)
513 	einfo (_("%P: link errors found, deleting executable `%s'\n"),
514 	       output_filename);
515 
516       /* The file will be removed by remove_output.  */
517       xexit (1);
518     }
519   else
520     {
521       if (! bfd_close (output_bfd))
522 	einfo (_("%F%B: final close failed: %E\n"), output_bfd);
523 
524       /* If the --force-exe-suffix is enabled, and we're making an
525 	 executable file and it doesn't end in .exe, copy it to one
526 	 which does.  */
527       if (! link_info.relocatable && command_line.force_exe_suffix)
528 	{
529 	  int len = strlen (output_filename);
530 
531 	  if (len < 4
532 	      || (strcasecmp (output_filename + len - 4, ".exe") != 0
533 		  && strcasecmp (output_filename + len - 4, ".dll") != 0))
534 	    {
535 	      FILE *src;
536 	      FILE *dst;
537 	      const int bsize = 4096;
538 	      char *buf = xmalloc (bsize);
539 	      int l;
540 	      char *dst_name = xmalloc (len + 5);
541 
542 	      strcpy (dst_name, output_filename);
543 	      strcat (dst_name, ".exe");
544 	      src = fopen (output_filename, FOPEN_RB);
545 	      dst = fopen (dst_name, FOPEN_WB);
546 
547 	      if (!src)
548 		einfo (_("%X%P: unable to open for source of copy `%s'\n"),
549 		       output_filename);
550 	      if (!dst)
551 		einfo (_("%X%P: unable to open for destination of copy `%s'\n"),
552 		       dst_name);
553 	      while ((l = fread (buf, 1, bsize, src)) > 0)
554 		{
555 		  int done = fwrite (buf, 1, l, dst);
556 
557 		  if (done != l)
558 		    einfo (_("%P: Error writing file `%s'\n"), dst_name);
559 		}
560 
561 	      fclose (src);
562 	      if (fclose (dst) == EOF)
563 		einfo (_("%P: Error closing file `%s'\n"), dst_name);
564 	      free (dst_name);
565 	      free (buf);
566 	    }
567 	}
568     }
569 
570   END_PROGRESS (program_name);
571 
572   if (config.stats)
573     {
574 #ifdef HAVE_SBRK
575       char *lim = sbrk (0);
576 #endif
577       long run_time = get_run_time () - start_time;
578 
579       fprintf (stderr, _("%s: total time in link: %ld.%06ld\n"),
580 	       program_name, run_time / 1000000, run_time % 1000000);
581 #ifdef HAVE_SBRK
582       fprintf (stderr, _("%s: data size %ld\n"), program_name,
583 	       (long) (lim - (char *) &environ));
584 #endif
585     }
586 
587   /* Prevent remove_output from doing anything, after a successful link.  */
588   output_filename = NULL;
589 
590   xexit (0);
591   return 0;
592 }
593 
594 /* If the configured sysroot is relocatable, try relocating it based on
595    default prefix FROM.  Return the relocated directory if it exists,
596    otherwise return null.  */
597 
598 static char *
599 get_relative_sysroot (const char *from ATTRIBUTE_UNUSED)
600 {
601 #ifdef TARGET_SYSTEM_ROOT_RELOCATABLE
602   char *path;
603   struct stat s;
604 
605   path = make_relative_prefix (program_name, from, TARGET_SYSTEM_ROOT);
606   if (path)
607     {
608       if (stat (path, &s) == 0 && S_ISDIR (s.st_mode))
609 	return path;
610       free (path);
611     }
612 #endif
613   return 0;
614 }
615 
616 /* Return the sysroot directory.  Return "" if no sysroot is being used.  */
617 
618 static const char *
619 get_sysroot (int argc, char **argv)
620 {
621   int i;
622   const char *path;
623 
624   for (i = 1; i < argc; i++)
625     if (strncmp (argv[i], "--sysroot=", strlen ("--sysroot=")) == 0)
626       return argv[i] + strlen ("--sysroot=");
627 
628   path = get_relative_sysroot (BINDIR);
629   if (path)
630     return path;
631 
632   path = get_relative_sysroot (TOOLBINDIR);
633   if (path)
634     return path;
635 
636   return TARGET_SYSTEM_ROOT;
637 }
638 
639 /* We need to find any explicitly given emulation in order to initialize the
640    state that's needed by the lex&yacc argument parser (parse_args).  */
641 
642 static char *
643 get_emulation (int argc, char **argv)
644 {
645   char *emulation;
646   int i;
647 
648   emulation = getenv (EMULATION_ENVIRON);
649   if (emulation == NULL)
650     emulation = DEFAULT_EMULATION;
651 
652   for (i = 1; i < argc; i++)
653     {
654       if (!strncmp (argv[i], "-m", 2))
655 	{
656 	  if (argv[i][2] == '\0')
657 	    {
658 	      /* -m EMUL */
659 	      if (i < argc - 1)
660 		{
661 		  emulation = argv[i + 1];
662 		  i++;
663 		}
664 	      else
665 		einfo (_("%P%F: missing argument to -m\n"));
666 	    }
667 	  else if (strcmp (argv[i], "-mips1") == 0
668 		   || strcmp (argv[i], "-mips2") == 0
669 		   || strcmp (argv[i], "-mips3") == 0
670 		   || strcmp (argv[i], "-mips4") == 0
671 		   || strcmp (argv[i], "-mips5") == 0
672 		   || strcmp (argv[i], "-mips32") == 0
673 		   || strcmp (argv[i], "-mips32r2") == 0
674 		   || strcmp (argv[i], "-mips64") == 0
675 		   || strcmp (argv[i], "-mips64r2") == 0)
676 	    {
677 	      /* FIXME: The arguments -mips1, -mips2, -mips3, etc. are
678 		 passed to the linker by some MIPS compilers.  They
679 		 generally tell the linker to use a slightly different
680 		 library path.  Perhaps someday these should be
681 		 implemented as emulations; until then, we just ignore
682 		 the arguments and hope that nobody ever creates
683 		 emulations named ips1, ips2 or ips3.  */
684 	    }
685 	  else if (strcmp (argv[i], "-m486") == 0)
686 	    {
687 	      /* FIXME: The argument -m486 is passed to the linker on
688 		 some Linux systems.  Hope that nobody creates an
689 		 emulation named 486.  */
690 	    }
691 	  else
692 	    {
693 	      /* -mEMUL */
694 	      emulation = &argv[i][2];
695 	    }
696 	}
697     }
698 
699   return emulation;
700 }
701 
702 /* If directory DIR contains an "ldscripts" subdirectory,
703    add DIR to the library search path and return TRUE,
704    else return FALSE.  */
705 
706 static bfd_boolean
707 check_for_scripts_dir (char *dir)
708 {
709   size_t dirlen;
710   char *buf;
711   struct stat s;
712   bfd_boolean res;
713 
714   dirlen = strlen (dir);
715   /* sizeof counts the terminating NUL.  */
716   buf = xmalloc (dirlen + sizeof ("/ldscripts"));
717   sprintf (buf, "%s/ldscripts", dir);
718 
719   res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode);
720   free (buf);
721   if (res)
722     ldfile_add_library_path (dir, FALSE);
723   return res;
724 }
725 
726 /* Set the default directory for finding script files.
727    Libraries will be searched for here too, but that's ok.
728    We look for the "ldscripts" directory in:
729 
730    SCRIPTDIR (passed from Makefile)
731 	     (adjusted according to the current location of the binary)
732    SCRIPTDIR (passed from Makefile)
733    the dir where this program is (for using it from the build tree)
734    the dir where this program is/../lib
735 	     (for installing the tool suite elsewhere).  */
736 
737 static void
738 set_scripts_dir (void)
739 {
740   char *end, *dir;
741   size_t dirlen;
742   bfd_boolean found;
743 
744   dir = make_relative_prefix (program_name, BINDIR, SCRIPTDIR);
745   if (dir)
746     {
747       found = check_for_scripts_dir (dir);
748       free (dir);
749       if (found)
750 	return;
751     }
752 
753   dir = make_relative_prefix (program_name, TOOLBINDIR, SCRIPTDIR);
754   if (dir)
755     {
756       found = check_for_scripts_dir (dir);
757       free (dir);
758       if (found)
759 	return;
760     }
761 
762   if (check_for_scripts_dir (SCRIPTDIR))
763     /* We've been installed normally.  */
764     return;
765 
766   /* Look for "ldscripts" in the dir where our binary is.  */
767   end = strrchr (program_name, '/');
768 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
769   {
770     /* We could have \foo\bar, or /foo\bar.  */
771     char *bslash = strrchr (program_name, '\\');
772 
773     if (end == NULL || (bslash != NULL && bslash > end))
774       end = bslash;
775   }
776 #endif
777 
778   if (end == NULL)
779     /* Don't look for ldscripts in the current directory.  There is
780        too much potential for confusion.  */
781     return;
782 
783   dirlen = end - program_name;
784   /* Make a copy of program_name in dir.
785      Leave room for later "/../lib".  */
786   dir = xmalloc (dirlen + 8);
787   strncpy (dir, program_name, dirlen);
788   dir[dirlen] = '\0';
789 
790   if (check_for_scripts_dir (dir))
791     {
792       free (dir);
793       return;
794     }
795 
796   /* Look for "ldscripts" in <the dir where our binary is>/../lib.  */
797   strcpy (dir + dirlen, "/../lib");
798   check_for_scripts_dir (dir);
799   free (dir);
800 }
801 
802 void
803 add_ysym (const char *name)
804 {
805   if (link_info.notice_hash == NULL)
806     {
807       link_info.notice_hash = xmalloc (sizeof (struct bfd_hash_table));
808       if (!bfd_hash_table_init_n (link_info.notice_hash,
809 				  bfd_hash_newfunc,
810 				  sizeof (struct bfd_hash_entry),
811 				  61))
812 	einfo (_("%P%F: bfd_hash_table_init failed: %E\n"));
813     }
814 
815   if (bfd_hash_lookup (link_info.notice_hash, name, TRUE, TRUE) == NULL)
816     einfo (_("%P%F: bfd_hash_lookup failed: %E\n"));
817 }
818 
819 /* Record a symbol to be wrapped, from the --wrap option.  */
820 
821 void
822 add_wrap (const char *name)
823 {
824   if (link_info.wrap_hash == NULL)
825     {
826       link_info.wrap_hash = xmalloc (sizeof (struct bfd_hash_table));
827       if (!bfd_hash_table_init_n (link_info.wrap_hash,
828 				  bfd_hash_newfunc,
829 				  sizeof (struct bfd_hash_entry),
830 				  61))
831 	einfo (_("%P%F: bfd_hash_table_init failed: %E\n"));
832     }
833 
834   if (bfd_hash_lookup (link_info.wrap_hash, name, TRUE, TRUE) == NULL)
835     einfo (_("%P%F: bfd_hash_lookup failed: %E\n"));
836 }
837 
838 /* Handle the -retain-symbols-file option.  */
839 
840 void
841 add_keepsyms_file (const char *filename)
842 {
843   FILE *file;
844   char *buf;
845   size_t bufsize;
846   int c;
847 
848   if (link_info.strip == strip_some)
849     einfo (_("%X%P: error: duplicate retain-symbols-file\n"));
850 
851   file = fopen (filename, "r");
852   if (file == NULL)
853     {
854       bfd_set_error (bfd_error_system_call);
855       einfo ("%X%P: %s: %E\n", filename);
856       return;
857     }
858 
859   link_info.keep_hash = xmalloc (sizeof (struct bfd_hash_table));
860   if (!bfd_hash_table_init (link_info.keep_hash, bfd_hash_newfunc,
861 			    sizeof (struct bfd_hash_entry)))
862     einfo (_("%P%F: bfd_hash_table_init failed: %E\n"));
863 
864   bufsize = 100;
865   buf = xmalloc (bufsize);
866 
867   c = getc (file);
868   while (c != EOF)
869     {
870       while (ISSPACE (c))
871 	c = getc (file);
872 
873       if (c != EOF)
874 	{
875 	  size_t len = 0;
876 
877 	  while (! ISSPACE (c) && c != EOF)
878 	    {
879 	      buf[len] = c;
880 	      ++len;
881 	      if (len >= bufsize)
882 		{
883 		  bufsize *= 2;
884 		  buf = xrealloc (buf, bufsize);
885 		}
886 	      c = getc (file);
887 	    }
888 
889 	  buf[len] = '\0';
890 
891 	  if (bfd_hash_lookup (link_info.keep_hash, buf, TRUE, TRUE) == NULL)
892 	    einfo (_("%P%F: bfd_hash_lookup for insertion failed: %E\n"));
893 	}
894     }
895 
896   if (link_info.strip != strip_none)
897     einfo (_("%P: `-retain-symbols-file' overrides `-s' and `-S'\n"));
898 
899   free (buf);
900   link_info.strip = strip_some;
901 }
902 
903 /* Callbacks from the BFD linker routines.  */
904 
905 /* This is called when BFD has decided to include an archive member in
906    a link.  */
907 
908 static bfd_boolean
909 add_archive_element (struct bfd_link_info *info,
910 		     bfd *abfd,
911 		     const char *name)
912 {
913   lang_input_statement_type *input;
914 
915   input = xmalloc (sizeof (lang_input_statement_type));
916   input->filename = abfd->filename;
917   input->local_sym_name = abfd->filename;
918   input->the_bfd = abfd;
919   input->asymbols = NULL;
920   input->next = NULL;
921   input->just_syms_flag = FALSE;
922   input->loaded = FALSE;
923   input->search_dirs_flag = FALSE;
924 
925   /* FIXME: The following fields are not set: header.next,
926      header.type, closed, passive_position, symbol_count,
927      next_real_file, is_archive, target, real.  This bit of code is
928      from the old decode_library_subfile function.  I don't know
929      whether any of those fields matters.  */
930 
931   ldlang_add_file (input);
932 
933   if (config.map_file != NULL)
934     {
935       static bfd_boolean header_printed;
936       struct bfd_link_hash_entry *h;
937       bfd *from;
938       int len;
939 
940       h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
941 
942       if (h == NULL)
943 	from = NULL;
944       else
945 	{
946 	  switch (h->type)
947 	    {
948 	    default:
949 	      from = NULL;
950 	      break;
951 
952 	    case bfd_link_hash_defined:
953 	    case bfd_link_hash_defweak:
954 	      from = h->u.def.section->owner;
955 	      break;
956 
957 	    case bfd_link_hash_undefined:
958 	    case bfd_link_hash_undefweak:
959 	      from = h->u.undef.abfd;
960 	      break;
961 
962 	    case bfd_link_hash_common:
963 	      from = h->u.c.p->section->owner;
964 	      break;
965 	    }
966 	}
967 
968       if (! header_printed)
969 	{
970 	  char buf[100];
971 
972 	  sprintf (buf, _("Archive member included because of file (symbol)\n\n"));
973 	  minfo ("%s", buf);
974 	  header_printed = TRUE;
975 	}
976 
977       if (bfd_my_archive (abfd) == NULL)
978 	{
979 	  minfo ("%s", bfd_get_filename (abfd));
980 	  len = strlen (bfd_get_filename (abfd));
981 	}
982       else
983 	{
984 	  minfo ("%s(%s)", bfd_get_filename (bfd_my_archive (abfd)),
985 		 bfd_get_filename (abfd));
986 	  len = (strlen (bfd_get_filename (bfd_my_archive (abfd)))
987 		 + strlen (bfd_get_filename (abfd))
988 		 + 2);
989 	}
990 
991       if (len >= 29)
992 	{
993 	  print_nl ();
994 	  len = 0;
995 	}
996       while (len < 30)
997 	{
998 	  print_space ();
999 	  ++len;
1000 	}
1001 
1002       if (from != NULL)
1003 	minfo ("%B ", from);
1004       if (h != NULL)
1005 	minfo ("(%T)\n", h->root.string);
1006       else
1007 	minfo ("(%s)\n", name);
1008     }
1009 
1010   if (trace_files || trace_file_tries)
1011     info_msg ("%I\n", input);
1012 
1013   return TRUE;
1014 }
1015 
1016 /* This is called when BFD has discovered a symbol which is defined
1017    multiple times.  */
1018 
1019 static bfd_boolean
1020 multiple_definition (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1021 		     const char *name,
1022 		     bfd *obfd,
1023 		     asection *osec,
1024 		     bfd_vma oval,
1025 		     bfd *nbfd,
1026 		     asection *nsec,
1027 		     bfd_vma nval)
1028 {
1029   /* If either section has the output_section field set to
1030      bfd_abs_section_ptr, it means that the section is being
1031      discarded, and this is not really a multiple definition at all.
1032      FIXME: It would be cleaner to somehow ignore symbols defined in
1033      sections which are being discarded.  */
1034   if ((osec->output_section != NULL
1035        && ! bfd_is_abs_section (osec)
1036        && bfd_is_abs_section (osec->output_section))
1037       || (nsec->output_section != NULL
1038 	  && ! bfd_is_abs_section (nsec)
1039 	  && bfd_is_abs_section (nsec->output_section)))
1040     return TRUE;
1041 
1042   einfo (_("%X%C: multiple definition of `%T'\n"),
1043 	 nbfd, nsec, nval, name);
1044   if (obfd != NULL)
1045     einfo (_("%D: first defined here\n"), obfd, osec, oval);
1046 
1047   if (command_line.relax)
1048     {
1049       einfo (_("%P: Disabling relaxation: it will not work with multiple definitions\n"));
1050       command_line.relax = 0;
1051     }
1052 
1053   return TRUE;
1054 }
1055 
1056 /* This is called when there is a definition of a common symbol, or
1057    when a common symbol is found for a symbol that is already defined,
1058    or when two common symbols are found.  We only do something if
1059    -warn-common was used.  */
1060 
1061 static bfd_boolean
1062 multiple_common (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1063 		 const char *name,
1064 		 bfd *obfd,
1065 		 enum bfd_link_hash_type otype,
1066 		 bfd_vma osize,
1067 		 bfd *nbfd,
1068 		 enum bfd_link_hash_type ntype,
1069 		 bfd_vma nsize)
1070 {
1071   if (! config.warn_common)
1072     return TRUE;
1073 
1074   if (ntype == bfd_link_hash_defined
1075       || ntype == bfd_link_hash_defweak
1076       || ntype == bfd_link_hash_indirect)
1077     {
1078       ASSERT (otype == bfd_link_hash_common);
1079       einfo (_("%B: warning: definition of `%T' overriding common\n"),
1080 	     nbfd, name);
1081       if (obfd != NULL)
1082 	einfo (_("%B: warning: common is here\n"), obfd);
1083     }
1084   else if (otype == bfd_link_hash_defined
1085 	   || otype == bfd_link_hash_defweak
1086 	   || otype == bfd_link_hash_indirect)
1087     {
1088       ASSERT (ntype == bfd_link_hash_common);
1089       einfo (_("%B: warning: common of `%T' overridden by definition\n"),
1090 	     nbfd, name);
1091       if (obfd != NULL)
1092 	einfo (_("%B: warning: defined here\n"), obfd);
1093     }
1094   else
1095     {
1096       ASSERT (otype == bfd_link_hash_common && ntype == bfd_link_hash_common);
1097       if (osize > nsize)
1098 	{
1099 	  einfo (_("%B: warning: common of `%T' overridden by larger common\n"),
1100 		 nbfd, name);
1101 	  if (obfd != NULL)
1102 	    einfo (_("%B: warning: larger common is here\n"), obfd);
1103 	}
1104       else if (nsize > osize)
1105 	{
1106 	  einfo (_("%B: warning: common of `%T' overriding smaller common\n"),
1107 		 nbfd, name);
1108 	  if (obfd != NULL)
1109 	    einfo (_("%B: warning: smaller common is here\n"), obfd);
1110 	}
1111       else
1112 	{
1113 	  einfo (_("%B: warning: multiple common of `%T'\n"), nbfd, name);
1114 	  if (obfd != NULL)
1115 	    einfo (_("%B: warning: previous common is here\n"), obfd);
1116 	}
1117     }
1118 
1119   return TRUE;
1120 }
1121 
1122 /* This is called when BFD has discovered a set element.  H is the
1123    entry in the linker hash table for the set.  SECTION and VALUE
1124    represent a value which should be added to the set.  */
1125 
1126 static bfd_boolean
1127 add_to_set (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1128 	    struct bfd_link_hash_entry *h,
1129 	    bfd_reloc_code_real_type reloc,
1130 	    bfd *abfd,
1131 	    asection *section,
1132 	    bfd_vma value)
1133 {
1134   if (config.warn_constructors)
1135     einfo (_("%P: warning: global constructor %s used\n"),
1136 	   h->root.string);
1137 
1138   if (! config.build_constructors)
1139     return TRUE;
1140 
1141   ldctor_add_set_entry (h, reloc, NULL, section, value);
1142 
1143   if (h->type == bfd_link_hash_new)
1144     {
1145       h->type = bfd_link_hash_undefined;
1146       h->u.undef.abfd = abfd;
1147       /* We don't call bfd_link_add_undef to add this to the list of
1148 	 undefined symbols because we are going to define it
1149 	 ourselves.  */
1150     }
1151 
1152   return TRUE;
1153 }
1154 
1155 /* This is called when BFD has discovered a constructor.  This is only
1156    called for some object file formats--those which do not handle
1157    constructors in some more clever fashion.  This is similar to
1158    adding an element to a set, but less general.  */
1159 
1160 static bfd_boolean
1161 constructor_callback (struct bfd_link_info *info,
1162 		      bfd_boolean constructor,
1163 		      const char *name,
1164 		      bfd *abfd,
1165 		      asection *section,
1166 		      bfd_vma value)
1167 {
1168   char *s;
1169   struct bfd_link_hash_entry *h;
1170   char set_name[1 + sizeof "__CTOR_LIST__"];
1171 
1172   if (config.warn_constructors)
1173     einfo (_("%P: warning: global constructor %s used\n"), name);
1174 
1175   if (! config.build_constructors)
1176     return TRUE;
1177 
1178   /* Ensure that BFD_RELOC_CTOR exists now, so that we can give a
1179      useful error message.  */
1180   if (bfd_reloc_type_lookup (output_bfd, BFD_RELOC_CTOR) == NULL
1181       && (info->relocatable
1182 	  || bfd_reloc_type_lookup (abfd, BFD_RELOC_CTOR) == NULL))
1183     einfo (_("%P%F: BFD backend error: BFD_RELOC_CTOR unsupported\n"));
1184 
1185   s = set_name;
1186   if (bfd_get_symbol_leading_char (abfd) != '\0')
1187     *s++ = bfd_get_symbol_leading_char (abfd);
1188   if (constructor)
1189     strcpy (s, "__CTOR_LIST__");
1190   else
1191     strcpy (s, "__DTOR_LIST__");
1192 
1193   h = bfd_link_hash_lookup (info->hash, set_name, TRUE, TRUE, TRUE);
1194   if (h == (struct bfd_link_hash_entry *) NULL)
1195     einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
1196   if (h->type == bfd_link_hash_new)
1197     {
1198       h->type = bfd_link_hash_undefined;
1199       h->u.undef.abfd = abfd;
1200       /* We don't call bfd_link_add_undef to add this to the list of
1201 	 undefined symbols because we are going to define it
1202 	 ourselves.  */
1203     }
1204 
1205   ldctor_add_set_entry (h, BFD_RELOC_CTOR, name, section, value);
1206   return TRUE;
1207 }
1208 
1209 /* A structure used by warning_callback to pass information through
1210    bfd_map_over_sections.  */
1211 
1212 struct warning_callback_info
1213 {
1214   bfd_boolean found;
1215   const char *warning;
1216   const char *symbol;
1217   asymbol **asymbols;
1218 };
1219 
1220 /* This is called when there is a reference to a warning symbol.  */
1221 
1222 static bfd_boolean
1223 warning_callback (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1224 		  const char *warning,
1225 		  const char *symbol,
1226 		  bfd *abfd,
1227 		  asection *section,
1228 		  bfd_vma address)
1229 {
1230   /* This is a hack to support warn_multiple_gp.  FIXME: This should
1231      have a cleaner interface, but what?  */
1232   if (! config.warn_multiple_gp
1233       && strcmp (warning, "using multiple gp values") == 0)
1234     return TRUE;
1235 
1236   if (section != NULL)
1237     einfo ("%C: %s%s\n", abfd, section, address, _("warning: "), warning);
1238   else if (abfd == NULL)
1239     einfo ("%P: %s%s\n", _("warning: "), warning);
1240   else if (symbol == NULL)
1241     einfo ("%B: %s%s\n", abfd, _("warning: "), warning);
1242   else
1243     {
1244       lang_input_statement_type *entry;
1245       asymbol **asymbols;
1246       struct warning_callback_info info;
1247 
1248       /* Look through the relocs to see if we can find a plausible
1249 	 address.  */
1250       entry = (lang_input_statement_type *) abfd->usrdata;
1251       if (entry != NULL && entry->asymbols != NULL)
1252 	asymbols = entry->asymbols;
1253       else
1254 	{
1255 	  long symsize;
1256 	  long symbol_count;
1257 
1258 	  symsize = bfd_get_symtab_upper_bound (abfd);
1259 	  if (symsize < 0)
1260 	    einfo (_("%B%F: could not read symbols: %E\n"), abfd);
1261 	  asymbols = xmalloc (symsize);
1262 	  symbol_count = bfd_canonicalize_symtab (abfd, asymbols);
1263 	  if (symbol_count < 0)
1264 	    einfo (_("%B%F: could not read symbols: %E\n"), abfd);
1265 	  if (entry != NULL)
1266 	    {
1267 	      entry->asymbols = asymbols;
1268 	      entry->symbol_count = symbol_count;
1269 	    }
1270 	}
1271 
1272       info.found = FALSE;
1273       info.warning = warning;
1274       info.symbol = symbol;
1275       info.asymbols = asymbols;
1276       bfd_map_over_sections (abfd, warning_find_reloc, &info);
1277 
1278       if (! info.found)
1279 	einfo ("%B: %s%s\n", abfd, _("warning: "), warning);
1280 
1281       if (entry == NULL)
1282 	free (asymbols);
1283     }
1284 
1285   return TRUE;
1286 }
1287 
1288 /* This is called by warning_callback for each section.  It checks the
1289    relocs of the section to see if it can find a reference to the
1290    symbol which triggered the warning.  If it can, it uses the reloc
1291    to give an error message with a file and line number.  */
1292 
1293 static void
1294 warning_find_reloc (bfd *abfd, asection *sec, void *iarg)
1295 {
1296   struct warning_callback_info *info = iarg;
1297   long relsize;
1298   arelent **relpp;
1299   long relcount;
1300   arelent **p, **pend;
1301 
1302   if (info->found)
1303     return;
1304 
1305   relsize = bfd_get_reloc_upper_bound (abfd, sec);
1306   if (relsize < 0)
1307     einfo (_("%B%F: could not read relocs: %E\n"), abfd);
1308   if (relsize == 0)
1309     return;
1310 
1311   relpp = xmalloc (relsize);
1312   relcount = bfd_canonicalize_reloc (abfd, sec, relpp, info->asymbols);
1313   if (relcount < 0)
1314     einfo (_("%B%F: could not read relocs: %E\n"), abfd);
1315 
1316   p = relpp;
1317   pend = p + relcount;
1318   for (; p < pend && *p != NULL; p++)
1319     {
1320       arelent *q = *p;
1321 
1322       if (q->sym_ptr_ptr != NULL
1323 	  && *q->sym_ptr_ptr != NULL
1324 	  && strcmp (bfd_asymbol_name (*q->sym_ptr_ptr), info->symbol) == 0)
1325 	{
1326 	  /* We found a reloc for the symbol we are looking for.  */
1327 	  einfo ("%C: %s%s\n", abfd, sec, q->address, _("warning: "),
1328 		 info->warning);
1329 	  info->found = TRUE;
1330 	  break;
1331 	}
1332     }
1333 
1334   free (relpp);
1335 }
1336 
1337 /* This is called when an undefined symbol is found.  */
1338 
1339 static bfd_boolean
1340 undefined_symbol (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1341 		  const char *name,
1342 		  bfd *abfd,
1343 		  asection *section,
1344 		  bfd_vma address,
1345 		  bfd_boolean error)
1346 {
1347   static char *error_name;
1348   static unsigned int error_count;
1349 
1350 #define MAX_ERRORS_IN_A_ROW 5
1351 
1352   if (config.warn_once)
1353     {
1354       static struct bfd_hash_table *hash;
1355 
1356       /* Only warn once about a particular undefined symbol.  */
1357       if (hash == NULL)
1358 	{
1359 	  hash = xmalloc (sizeof (struct bfd_hash_table));
1360 	  if (!bfd_hash_table_init (hash, bfd_hash_newfunc,
1361 				    sizeof (struct bfd_hash_entry)))
1362 	    einfo (_("%F%P: bfd_hash_table_init failed: %E\n"));
1363 	}
1364 
1365       if (bfd_hash_lookup (hash, name, FALSE, FALSE) != NULL)
1366 	return TRUE;
1367 
1368       if (bfd_hash_lookup (hash, name, TRUE, TRUE) == NULL)
1369 	einfo (_("%F%P: bfd_hash_lookup failed: %E\n"));
1370     }
1371 
1372   /* We never print more than a reasonable number of errors in a row
1373      for a single symbol.  */
1374   if (error_name != NULL
1375       && strcmp (name, error_name) == 0)
1376     ++error_count;
1377   else
1378     {
1379       error_count = 0;
1380       if (error_name != NULL)
1381 	free (error_name);
1382       error_name = xstrdup (name);
1383     }
1384 
1385   if (section != NULL)
1386     {
1387       if (error_count < MAX_ERRORS_IN_A_ROW)
1388 	{
1389 	  if (error)
1390 	    einfo (_("%X%C: undefined reference to `%T'\n"),
1391 		   abfd, section, address, name);
1392 	  else
1393 	    einfo (_("%C: warning: undefined reference to `%T'\n"),
1394 		   abfd, section, address, name);
1395 	}
1396       else if (error_count == MAX_ERRORS_IN_A_ROW)
1397 	{
1398 	  if (error)
1399 	    einfo (_("%X%D: more undefined references to `%T' follow\n"),
1400 		   abfd, section, address, name);
1401 	  else
1402 	    einfo (_("%D: warning: more undefined references to `%T' follow\n"),
1403 		   abfd, section, address, name);
1404 	}
1405       else if (error)
1406 	einfo ("%X");
1407     }
1408   else
1409     {
1410       if (error_count < MAX_ERRORS_IN_A_ROW)
1411 	{
1412 	  if (error)
1413 	    einfo (_("%X%B: undefined reference to `%T'\n"),
1414 		   abfd, name);
1415 	  else
1416 	    einfo (_("%B: warning: undefined reference to `%T'\n"),
1417 		   abfd, name);
1418 	}
1419       else if (error_count == MAX_ERRORS_IN_A_ROW)
1420 	{
1421 	  if (error)
1422 	    einfo (_("%X%B: more undefined references to `%T' follow\n"),
1423 		   abfd, name);
1424 	  else
1425 	    einfo (_("%B: warning: more undefined references to `%T' follow\n"),
1426 		   abfd, name);
1427 	}
1428       else if (error)
1429 	einfo ("%X");
1430     }
1431 
1432   return TRUE;
1433 }
1434 
1435 /* Counter to limit the number of relocation overflow error messages
1436    to print.  Errors are printed as it is decremented.  When it's
1437    called and the counter is zero, a final message is printed
1438    indicating more relocations were omitted.  When it gets to -1, no
1439    such errors are printed.  If it's initially set to a value less
1440    than -1, all such errors will be printed (--verbose does this).  */
1441 
1442 int overflow_cutoff_limit = 10;
1443 
1444 /* This is called when a reloc overflows.  */
1445 
1446 static bfd_boolean
1447 reloc_overflow (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1448 		struct bfd_link_hash_entry *entry,
1449 		const char *name,
1450 		const char *reloc_name,
1451 		bfd_vma addend,
1452 		bfd *abfd,
1453 		asection *section,
1454 		bfd_vma address)
1455 {
1456   if (overflow_cutoff_limit == -1)
1457     return TRUE;
1458 
1459   einfo ("%X%C:", abfd, section, address);
1460 
1461   if (overflow_cutoff_limit >= 0
1462       && overflow_cutoff_limit-- == 0)
1463     {
1464       einfo (_(" additional relocation overflows omitted from the output\n"));
1465       return TRUE;
1466     }
1467 
1468   if (entry)
1469     {
1470       while (entry->type == bfd_link_hash_indirect
1471 	     || entry->type == bfd_link_hash_warning)
1472 	entry = entry->u.i.link;
1473       switch (entry->type)
1474 	{
1475 	case bfd_link_hash_undefined:
1476 	case bfd_link_hash_undefweak:
1477 	  einfo (_(" relocation truncated to fit: %s against undefined symbol `%T'"),
1478 		 reloc_name, entry->root.string);
1479 	  break;
1480 	case bfd_link_hash_defined:
1481 	case bfd_link_hash_defweak:
1482 	  einfo (_(" relocation truncated to fit: %s against symbol `%T' defined in %A section in %B"),
1483 		 reloc_name, entry->root.string,
1484 		 entry->u.def.section,
1485 		 entry->u.def.section == bfd_abs_section_ptr
1486 		 ? output_bfd : entry->u.def.section->owner);
1487 	  break;
1488 	default:
1489 	  abort ();
1490 	  break;
1491 	}
1492     }
1493   else
1494     einfo (_(" relocation truncated to fit: %s against `%T'"),
1495 	   reloc_name, name);
1496   if (addend != 0)
1497     einfo ("+%v", addend);
1498   einfo ("\n");
1499   return TRUE;
1500 }
1501 
1502 /* This is called when a dangerous relocation is made.  */
1503 
1504 static bfd_boolean
1505 reloc_dangerous (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1506 		 const char *message,
1507 		 bfd *abfd,
1508 		 asection *section,
1509 		 bfd_vma address)
1510 {
1511   einfo (_("%X%C: dangerous relocation: %s\n"),
1512 	 abfd, section, address, message);
1513   return TRUE;
1514 }
1515 
1516 /* This is called when a reloc is being generated attached to a symbol
1517    that is not being output.  */
1518 
1519 static bfd_boolean
1520 unattached_reloc (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1521 		  const char *name,
1522 		  bfd *abfd,
1523 		  asection *section,
1524 		  bfd_vma address)
1525 {
1526   einfo (_("%X%C: reloc refers to symbol `%T' which is not being output\n"),
1527 	 abfd, section, address, name);
1528   return TRUE;
1529 }
1530 
1531 /* This is called if link_info.notice_all is set, or when a symbol in
1532    link_info.notice_hash is found.  Symbols are put in notice_hash
1533    using the -y option.  */
1534 
1535 static bfd_boolean
1536 notice (struct bfd_link_info *info,
1537 	const char *name,
1538 	bfd *abfd,
1539 	asection *section,
1540 	bfd_vma value)
1541 {
1542   if (! info->notice_all
1543       || (info->notice_hash != NULL
1544 	  && bfd_hash_lookup (info->notice_hash, name, FALSE, FALSE) != NULL))
1545     {
1546       if (bfd_is_und_section (section))
1547 	einfo ("%B: reference to %s\n", abfd, name);
1548       else
1549 	einfo ("%B: definition of %s\n", abfd, name);
1550     }
1551 
1552   if (command_line.cref || nocrossref_list != NULL)
1553     add_cref (name, abfd, section, value);
1554 
1555   return TRUE;
1556 }
1557