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