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