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