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