* ldmain.c (main): Initialize new field link_info.static_link.
[external/binutils.git] / ld / ldmain.c
1 /* Main program of GNU linker.
2    Copyright (C) 1991, 92, 93, 94, 1995 Free Software Foundation, Inc.
3    Written by Steve Chamberlain steve@cygnus.com
4
5 This file is part of GLD, the Gnu Linker.
6
7 GLD is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GLD is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GLD; see the file COPYING.  If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include <stdio.h>
25 #include <ctype.h>
26 #include "libiberty.h"
27 #include "progress.h"
28 #include "bfdlink.h"
29
30 #include "ld.h"
31 #include "ldmain.h"
32 #include "ldmisc.h"
33 #include "ldwrite.h"
34 #include "ldgram.h"
35 #include "ldexp.h"
36 #include "ldlang.h"
37 #include "ldemul.h"
38 #include "ldlex.h"
39 #include "ldfile.h"
40 #include "ldctor.h"
41
42 /* Somewhere above, sys/stat.h got included . . . . */
43 #if !defined(S_ISDIR) && defined(S_IFDIR)
44 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
45 #endif
46
47 #include <string.h>
48
49 static char *get_emulation PARAMS ((int, char **));
50 static void set_scripts_dir PARAMS ((void));
51
52 /* EXPORTS */
53
54 char *default_target;
55 const char *output_filename = "a.out";
56
57 /* Name this program was invoked by.  */
58 char *program_name;
59
60 /* The file that we're creating */
61 bfd *output_bfd = 0;
62
63 /* Set by -G argument, for MIPS ECOFF target.  */
64 int g_switch_value = 8;
65
66 /* Nonzero means print names of input files as processed.  */
67 boolean trace_files;
68
69 /* Nonzero means same, but note open failures, too.  */
70 boolean trace_file_tries;
71
72 /* Nonzero means version number was printed, so exit successfully
73    instead of complaining if no input files are given.  */
74 boolean version_printed;
75
76 /* Nonzero means link in every member of an archive.  */
77 boolean whole_archive;
78
79 args_type command_line;
80
81 ld_config_type config;
82
83 static boolean check_for_scripts_dir PARAMS ((char *dir));
84 static boolean add_archive_element PARAMS ((struct bfd_link_info *, bfd *,
85                                             const char *));
86 static boolean multiple_definition PARAMS ((struct bfd_link_info *,
87                                             const char *,
88                                             bfd *, asection *, bfd_vma,
89                                             bfd *, asection *, bfd_vma));
90 static boolean multiple_common PARAMS ((struct bfd_link_info *,
91                                         const char *, bfd *,
92                                         enum bfd_link_hash_type, bfd_vma,
93                                         bfd *, enum bfd_link_hash_type,
94                                         bfd_vma));
95 static boolean add_to_set PARAMS ((struct bfd_link_info *,
96                                    struct bfd_link_hash_entry *,
97                                    bfd_reloc_code_real_type,
98                                    bfd *, asection *, bfd_vma));
99 static boolean constructor_callback PARAMS ((struct bfd_link_info *,
100                                              boolean constructor,
101                                              const char *name,
102                                              bfd *, asection *, bfd_vma));
103 static boolean warning_callback PARAMS ((struct bfd_link_info *,
104                                          const char *, const char *, bfd *,
105                                          asection *, bfd_vma));
106 static void warning_find_reloc PARAMS ((bfd *, asection *, PTR));
107 static boolean undefined_symbol PARAMS ((struct bfd_link_info *,
108                                          const char *, bfd *,
109                                          asection *, bfd_vma));
110 static boolean reloc_overflow PARAMS ((struct bfd_link_info *, const char *,
111                                        const char *, bfd_vma,
112                                        bfd *, asection *, bfd_vma));
113 static boolean reloc_dangerous PARAMS ((struct bfd_link_info *, const char *,
114                                         bfd *, asection *, bfd_vma));
115 static boolean unattached_reloc PARAMS ((struct bfd_link_info *,
116                                          const char *, bfd *, asection *,
117                                          bfd_vma));
118 static boolean notice_ysym PARAMS ((struct bfd_link_info *, const char *,
119                                     bfd *, asection *, bfd_vma));
120
121 static struct bfd_link_callbacks link_callbacks =
122 {
123   add_archive_element,
124   multiple_definition,
125   multiple_common,
126   add_to_set,
127   constructor_callback,
128   warning_callback,
129   undefined_symbol,
130   reloc_overflow,
131   reloc_dangerous,
132   unattached_reloc,
133   notice_ysym
134 };
135
136 struct bfd_link_info link_info;
137 \f
138 static void
139 remove_output ()
140 {
141   if (output_filename) 
142     {
143       if (output_bfd && output_bfd->iostream)
144         fclose((FILE *)(output_bfd->iostream));
145       if (delete_output_file_on_failure)
146         unlink (output_filename);
147     }
148 }
149
150 int
151 main (argc, argv)
152      int argc;
153      char **argv;
154 {
155   char *emulation;
156   long start_time = get_run_time ();
157
158   program_name = argv[0];
159   xmalloc_set_program_name (program_name);
160
161   START_PROGRESS (program_name, 0);
162
163   bfd_init ();
164
165   xatexit (remove_output);
166
167   /* Initialize the data about options.  */
168   trace_files = trace_file_tries = version_printed = false;
169   whole_archive = false;
170   config.traditional_format = false;
171   config.build_constructors = true;
172   config.dynamic_link = false;
173   command_line.force_common_definition = false;
174   command_line.interpreter = NULL;
175   command_line.rpath = NULL;
176
177   link_info.callbacks = &link_callbacks;
178   link_info.relocateable = false;
179   link_info.shared = false;
180   link_info.symbolic = false;
181   link_info.static_link = false;
182   link_info.strip = strip_none;
183   link_info.discard = discard_none;
184   link_info.lprefix_len = 1;
185   link_info.lprefix = "L";
186   link_info.keep_memory = true;
187   link_info.input_bfds = NULL;
188   link_info.create_object_symbols_section = NULL;
189   link_info.hash = NULL;
190   link_info.keep_hash = NULL;
191   link_info.notice_hash = NULL;
192
193   
194   ldfile_add_arch ("");
195
196   config.make_executable = true;
197   force_make_executable = false;
198   config.magic_demand_paged = true;
199   config.text_read_only = true;
200   config.make_executable = true;
201
202   emulation = get_emulation (argc, argv);
203   ldemul_choose_mode (emulation);
204   default_target = ldemul_choose_target ();
205   lang_init ();
206   ldemul_before_parse ();
207   lang_has_input_file = false;
208   parse_args (argc, argv);
209
210   ldemul_set_symbols ();
211
212   if (link_info.relocateable)
213     {
214       if (command_line.relax)
215         einfo ("%P%F: -relax and -r may not be used together\n");
216       if (link_info.shared)
217         einfo ("%P%F: -r and -shared may not be used together\n");
218     }
219
220   /* Treat ld -r -s as ld -r -S -x (i.e., strip all local symbols).  I
221      don't see how else this can be handled, since in this case we
222      must preserve all externally visible symbols.  */
223   if (link_info.relocateable && link_info.strip == strip_all)
224     {
225       link_info.strip = strip_debugger;
226       if (link_info.discard == discard_none)
227         link_info.discard = discard_all;
228     }
229
230   /* This essentially adds another -L directory so this must be done after
231      the -L's in argv have been processed.  */
232   set_scripts_dir ();
233
234   if (had_script == false)
235     {
236       /* Read the emulation's appropriate default script.  */
237       int isfile;
238       char *s = ldemul_get_script (&isfile);
239
240       if (isfile)
241         ldfile_open_command_file (s);
242       else
243         {
244           if (trace_file_tries)
245             {
246               info_msg ("using internal linker script:\n");
247               info_msg ("==================================================\n");
248               info_msg (s);
249               info_msg ("\n==================================================\n");
250             }
251           lex_string = s;
252           lex_redirect (s);
253         }
254       parser_input = input_script;
255       yyparse ();
256       lex_string = NULL;
257     }
258
259   lang_final ();
260
261   if (lang_has_input_file == false)
262     {
263       if (version_printed)
264         xexit (0);
265       einfo ("%P%F: no input files\n");
266     }
267
268   if (trace_files)
269     {
270       info_msg ("%P: mode %s\n", emulation);
271     }
272
273   ldemul_after_parse ();
274
275
276   if (config.map_filename)
277     {
278       if (strcmp (config.map_filename, "-") == 0)
279         {
280           config.map_file = stdout;
281         }
282       else
283         {
284           config.map_file = fopen (config.map_filename, FOPEN_WT);
285           if (config.map_file == (FILE *) NULL)
286             {
287               bfd_set_error (bfd_error_system_call);
288               einfo ("%P%F: cannot open map file %s: %E\n",
289                      config.map_filename);
290             }
291         }
292     }
293
294
295   lang_process ();
296
297   /* Print error messages for any missing symbols, for any warning
298      symbols, and possibly multiple definitions */
299
300
301   if (config.text_read_only)
302     {
303       /* Look for a text section and mark the readonly attribute in it */
304       asection *found = bfd_get_section_by_name (output_bfd, ".text");
305
306       if (found != (asection *) NULL)
307         {
308           found->flags |= SEC_READONLY;
309         }
310     }
311
312   if (link_info.relocateable)
313     output_bfd->flags &= ~EXEC_P;
314   else
315     output_bfd->flags |= EXEC_P;
316
317   ldwrite ();
318
319   /* Even if we're producing relocateable output, some non-fatal errors should
320      be reported in the exit status.  (What non-fatal errors, if any, do we
321      want to ignore for relocateable output?)  */
322
323   if (config.make_executable == false && force_make_executable == false)
324     {
325       if (trace_files == true)
326         {
327           einfo ("%P: link errors found, deleting executable `%s'\n",
328                  output_filename);
329         }
330
331       if (output_bfd->iostream)
332         fclose ((FILE *) (output_bfd->iostream));
333
334       unlink (output_filename);
335       xexit (1);
336     }
337   else
338     {
339       if (! bfd_close (output_bfd))
340         einfo ("%F%B: final close failed: %E\n", output_bfd);
341     }
342
343   END_PROGRESS (program_name);
344
345   if (config.stats)
346     {
347       extern char **environ;
348 #ifdef HAVE_SBRK
349       char *lim = (char *) sbrk (0);
350 #endif
351       long run_time = get_run_time () - start_time;
352
353       fprintf (stderr, "%s: total time in link: %ld.%06ld\n",
354                program_name, run_time / 1000000, run_time % 1000000);
355 #ifdef HAVE_SBRK
356       fprintf (stderr, "%s: data size %ld\n", program_name,
357                (long) (lim - (char *) &environ));
358 #endif
359     }
360
361   /* Prevent remove_output from doing anything, after a successful link.  */
362   output_filename = NULL;
363
364   xexit (0);
365   return 0;
366 }
367
368 /* We need to find any explicitly given emulation in order to initialize the
369    state that's needed by the lex&yacc argument parser (parse_args).  */
370
371 static char *
372 get_emulation (argc, argv)
373      int argc;
374      char **argv;
375 {
376   char *emulation;
377   int i;
378
379   emulation = (char *) getenv (EMULATION_ENVIRON);
380   if (emulation == NULL)
381     emulation = DEFAULT_EMULATION;
382
383   for (i = 1; i < argc; i++)
384     {
385       if (!strncmp (argv[i], "-m", 2))
386         {
387           if (argv[i][2] == '\0')
388             {
389               /* -m EMUL */
390               if (i < argc - 1)
391                 {
392                   emulation = argv[i + 1];
393                   i++;
394                 }
395               else
396                 {
397                   einfo("%P%F: missing argument to -m\n");
398                 }
399             }
400           else if (strcmp (argv[i], "-mips1") == 0
401                    || strcmp (argv[i], "-mips2") == 0
402                    || strcmp (argv[i], "-mips3") == 0)
403             {
404               /* FIXME: The arguments -mips1, -mips2 and -mips3 are
405                  passed to the linker by some MIPS compilers.  They
406                  generally tell the linker to use a slightly different
407                  library path.  Perhaps someday these should be
408                  implemented as emulations; until then, we just ignore
409                  the arguments and hope that nobody ever creates
410                  emulations named ips1, ips2 or ips3.  */
411             }
412           else if (strcmp (argv[i], "-m486") == 0)
413             {
414               /* FIXME: The argument -m486 is passed to the linker on
415                  some Linux systems.  Hope that nobody creates an
416                  emulation named 486.  */
417             }
418           else
419             {
420               /* -mEMUL */
421               emulation = &argv[i][2];
422             }
423         }
424     }
425
426   return emulation;
427 }
428
429 /* If directory DIR contains an "ldscripts" subdirectory,
430    add DIR to the library search path and return true,
431    else return false.  */
432
433 static boolean
434 check_for_scripts_dir (dir)
435      char *dir;
436 {
437   size_t dirlen;
438   char *buf;
439   struct stat s;
440   boolean res;
441
442   dirlen = strlen (dir);
443   /* sizeof counts the terminating NUL.  */
444   buf = (char *) xmalloc (dirlen + sizeof("/ldscripts"));
445   sprintf (buf, "%s/ldscripts", dir);
446
447   res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode);
448   free (buf);
449   if (res)
450     ldfile_add_library_path (dir, false);
451   return res;
452 }
453
454 /* Set the default directory for finding script files.
455    Libraries will be searched for here too, but that's ok.
456    We look for the "ldscripts" directory in:
457
458    SCRIPTDIR (passed from Makefile)
459    the dir where this program is (for using it from the build tree)
460    the dir where this program is/../lib (for installing the tool suite elsewhere) */
461
462 static void
463 set_scripts_dir ()
464 {
465   char *end, *dir;
466   size_t dirlen;
467
468   if (check_for_scripts_dir (SCRIPTDIR))
469     return;                     /* We've been installed normally.  */
470
471   /* Look for "ldscripts" in the dir where our binary is.  */
472   end = strrchr (program_name, '/');
473
474   if (end == NULL)
475     {
476       /* Don't look for ldscripts in the current directory.  There is
477          too much potential for confusion.  */
478       return;
479     }
480
481   dirlen = end - program_name;
482   /* Make a copy of program_name in dir.
483      Leave room for later "/../lib".  */
484   dir = (char *) xmalloc (dirlen + 8);
485   strncpy (dir, program_name, dirlen);
486   dir[dirlen] = '\0';
487
488   if (check_for_scripts_dir (dir))
489     return;                     /* Don't free dir.  */
490
491   /* Look for "ldscripts" in <the dir where our binary is>/../lib.  */
492   strcpy (dir + dirlen, "/../lib");
493   if (check_for_scripts_dir (dir))
494     return;
495
496   free (dir);                   /* Well, we tried.  */
497 }
498
499 void
500 add_ysym (name)
501      const char *name;
502 {
503   if (link_info.notice_hash == (struct bfd_hash_table *) NULL)
504     {
505       link_info.notice_hash = ((struct bfd_hash_table *)
506                                xmalloc (sizeof (struct bfd_hash_table)));
507       if (! bfd_hash_table_init_n (link_info.notice_hash,
508                                    bfd_hash_newfunc,
509                                    61))
510         einfo ("%P%F: bfd_hash_table_init failed: %E\n");
511     }      
512
513   if (bfd_hash_lookup (link_info.notice_hash, name, true, true)
514       == (struct bfd_hash_entry *) NULL)
515     einfo ("%P%F: bfd_hash_lookup failed: %E\n");
516 }
517
518 /* Handle the -retain-symbols-file option.  */
519
520 void
521 add_keepsyms_file (filename)
522      const char *filename;
523 {
524   FILE *file;
525   char *buf;
526   size_t bufsize;
527   int c;
528
529   if (link_info.strip == strip_some)
530     einfo ("%X%P: error: duplicate retain-symbols-file\n");
531
532   file = fopen (filename, "r");
533   if (file == (FILE *) NULL)
534     {
535       bfd_set_error (bfd_error_system_call);
536       einfo ("%X%P: %s: %E\n", filename);
537       return;
538     }
539
540   link_info.keep_hash = ((struct bfd_hash_table *)
541                          xmalloc (sizeof (struct bfd_hash_table)));
542   if (! bfd_hash_table_init (link_info.keep_hash, bfd_hash_newfunc))
543     einfo ("%P%F: bfd_hash_table_init failed: %E\n");
544
545   bufsize = 100;
546   buf = (char *) xmalloc (bufsize);
547
548   c = getc (file);
549   while (c != EOF)
550     {
551       while (isspace (c))
552         c = getc (file);
553
554       if (c != EOF)
555         {
556           size_t len = 0;
557
558           while (! isspace (c) && c != EOF)
559             {
560               buf[len] = c;
561               ++len;
562               if (len >= bufsize)
563                 {
564                   bufsize *= 2;
565                   buf = xrealloc (buf, bufsize);
566                 }
567               c = getc (file);
568             }
569
570           buf[len] = '\0';
571
572           if (bfd_hash_lookup (link_info.keep_hash, buf, true, true)
573               == (struct bfd_hash_entry *) NULL)
574             einfo ("%P%F: bfd_hash_lookup for insertion failed: %E\n");
575         }
576     }
577
578   if (link_info.strip != strip_none)
579     einfo ("%P: `-retain-symbols-file' overrides `-s' and `-S'\n");
580
581   link_info.strip = strip_some;
582 }
583 \f
584 /* Callbacks from the BFD linker routines.  */
585
586 /* This is called when BFD has decided to include an archive member in
587    a link.  */
588
589 /*ARGSUSED*/
590 static boolean
591 add_archive_element (info, abfd, name)
592      struct bfd_link_info *info;
593      bfd *abfd;
594      const char *name;
595 {
596   lang_input_statement_type *input;
597
598   input = ((lang_input_statement_type *)
599            xmalloc (sizeof (lang_input_statement_type)));
600   input->filename = abfd->filename;
601   input->local_sym_name = abfd->filename;
602   input->the_bfd = abfd;
603   input->asymbols = NULL;
604   input->next = NULL;
605   input->just_syms_flag = false;
606   input->loaded = false;
607   input->search_dirs_flag = false;
608
609   /* FIXME: The following fields are not set: header.next,
610      header.type, closed, passive_position, symbol_count,
611      next_real_file, is_archive, target, real, common_section,
612      common_output_section, complained.  This bit of code is from the
613      old decode_library_subfile function.  I don't know whether any of
614      those fields matters.  */
615
616   ldlang_add_file (input);
617
618   if (config.map_file != (FILE *) NULL)
619     minfo ("%s needed due to %T\n", abfd->filename, name);
620
621   if (trace_files || trace_file_tries)
622     info_msg ("%I\n", input);
623
624   return true;
625 }
626
627 /* This is called when BFD has discovered a symbol which is defined
628    multiple times.  */
629
630 /*ARGSUSED*/
631 static boolean
632 multiple_definition (info, name, obfd, osec, oval, nbfd, nsec, nval)
633      struct bfd_link_info *info;
634      const char *name;
635      bfd *obfd;
636      asection *osec;
637      bfd_vma oval;
638      bfd *nbfd;
639      asection *nsec;
640      bfd_vma nval;
641 {
642   einfo ("%X%C: multiple definition of `%T'\n",
643          nbfd, nsec, nval, name);
644   if (obfd != (bfd *) NULL)
645     einfo ("%D: first defined here\n", obfd, osec, oval);
646   return true;
647 }
648
649 /* This is called when there is a definition of a common symbol, or
650    when a common symbol is found for a symbol that is already defined,
651    or when two common symbols are found.  We only do something if
652    -warn-common was used.  */
653
654 /*ARGSUSED*/
655 static boolean
656 multiple_common (info, name, obfd, otype, osize, nbfd, ntype, nsize)
657      struct bfd_link_info *info;
658      const char *name;
659      bfd *obfd;
660      enum bfd_link_hash_type otype;
661      bfd_vma osize;
662      bfd *nbfd;
663      enum bfd_link_hash_type ntype;
664      bfd_vma nsize;
665 {
666   if (! config.warn_common)
667     return true;
668
669   if (ntype == bfd_link_hash_defined
670       || ntype == bfd_link_hash_defweak
671       || ntype == bfd_link_hash_indirect)
672     {
673       ASSERT (otype == bfd_link_hash_common);
674       einfo ("%B: warning: definition of `%T' overriding common\n",
675              nbfd, name);
676       if (obfd != NULL)
677         einfo ("%B: warning: common is here\n", obfd);
678     }
679   else if (otype == bfd_link_hash_defined
680            || otype == bfd_link_hash_defweak
681            || otype == bfd_link_hash_indirect)
682     {
683       ASSERT (ntype == bfd_link_hash_common);
684       einfo ("%B: warning: common of `%T' overridden by definition\n",
685              nbfd, name);
686       if (obfd != NULL)
687         einfo ("%B: warning: defined here\n", obfd);
688     }
689   else
690     {
691       ASSERT (otype == bfd_link_hash_common && ntype == bfd_link_hash_common);
692       if (osize > nsize)
693         {
694           einfo ("%B: warning: common of `%T' overridden by larger common\n",
695                  nbfd, name);
696           if (obfd != NULL)
697             einfo ("%B: warning: larger common is here\n", obfd);
698         }
699       else if (nsize > osize)
700         {
701           einfo ("%B: warning: common of `%T' overriding smaller common\n",
702                  nbfd, name);
703           if (obfd != NULL)
704             einfo ("%B: warning: smaller common is here\n", obfd);
705         }
706       else
707         {
708           einfo ("%B: warning: multiple common of `%T'\n", nbfd, name);
709           if (obfd != NULL)
710             einfo ("%B: warning: previous common is here\n", obfd);
711         }
712     }
713
714   return true;
715 }
716
717 /* This is called when BFD has discovered a set element.  H is the
718    entry in the linker hash table for the set.  SECTION and VALUE
719    represent a value which should be added to the set.  */
720
721 /*ARGSUSED*/
722 static boolean
723 add_to_set (info, h, reloc, abfd, section, value)
724      struct bfd_link_info *info;
725      struct bfd_link_hash_entry *h;
726      bfd_reloc_code_real_type reloc;
727      bfd *abfd;
728      asection *section;
729      bfd_vma value;
730 {
731   if (config.warn_constructors)
732     einfo ("%P: warning: global constructor %s used\n",
733            h->root.string);
734
735   if (! config.build_constructors)
736     return true;
737
738   ldctor_add_set_entry (h, reloc, section, value);
739
740   if (h->type == bfd_link_hash_new)
741     {
742       h->type = bfd_link_hash_undefined;
743       h->u.undef.abfd = abfd;
744       /* We don't call bfd_link_add_undef to add this to the list of
745          undefined symbols because we are going to define it
746          ourselves.  */
747     }
748
749   return true;
750 }
751
752 /* This is called when BFD has discovered a constructor.  This is only
753    called for some object file formats--those which do not handle
754    constructors in some more clever fashion.  This is similar to
755    adding an element to a set, but less general.  */
756
757 static boolean
758 constructor_callback (info, constructor, name, abfd, section, value)
759      struct bfd_link_info *info;
760      boolean constructor;
761      const char *name;
762      bfd *abfd;
763      asection *section;
764      bfd_vma value;
765 {
766   char *s;
767   struct bfd_link_hash_entry *h;
768   char set_name[1 + sizeof "__CTOR_LIST__"];
769
770   if (config.warn_constructors)
771     einfo ("%P: warning: global constructor %s used\n", name);
772
773   if (! config.build_constructors)
774     return true;
775
776   /* Ensure that BFD_RELOC_CTOR exists now, so that we can give a
777      useful error message.  */
778   if (bfd_reloc_type_lookup (output_bfd, BFD_RELOC_CTOR) == NULL)
779     einfo ("%P%F: BFD backend error: BFD_RELOC_CTOR unsupported\n");
780
781   s = set_name;
782   if (bfd_get_symbol_leading_char (abfd) != '\0')
783     *s++ = bfd_get_symbol_leading_char (abfd);
784   if (constructor)
785     strcpy (s, "__CTOR_LIST__");
786   else
787     strcpy (s, "__DTOR_LIST__");
788
789   if (config.map_file != (FILE *) NULL)
790     fprintf (config.map_file,
791              "Adding %s to constructor/destructor set %s\n", name, set_name);
792
793   h = bfd_link_hash_lookup (info->hash, set_name, true, true, true);
794   if (h == (struct bfd_link_hash_entry *) NULL)
795     einfo ("%P%F: bfd_link_hash_lookup failed: %E\n");
796   if (h->type == bfd_link_hash_new)
797     {
798       h->type = bfd_link_hash_undefined;
799       h->u.undef.abfd = abfd;
800       /* We don't call bfd_link_add_undef to add this to the list of
801          undefined symbols because we are going to define it
802          ourselves.  */
803     }
804
805   ldctor_add_set_entry (h, BFD_RELOC_CTOR, section, value);
806   return true;
807 }
808
809 /* A structure used by warning_callback to pass information through
810    bfd_map_over_sections.  */
811
812 struct warning_callback_info
813 {
814   boolean found;
815   const char *warning;
816   const char *symbol;
817   asymbol **asymbols;
818 };
819
820 /* This is called when there is a reference to a warning symbol.  */
821
822 /*ARGSUSED*/
823 static boolean
824 warning_callback (info, warning, symbol, abfd, section, address)
825      struct bfd_link_info *info;
826      const char *warning;
827      const char *symbol;
828      bfd *abfd;
829      asection *section;
830      bfd_vma address;
831 {
832   if (section != NULL)
833     einfo ("%C: %s\n", abfd, section, address, warning);
834   else if (abfd == NULL)
835     einfo ("%P: %s\n", warning);
836   else if (symbol == NULL)
837     einfo ("%B: %s\n", abfd, warning);
838   else
839     {
840       lang_input_statement_type *entry;
841       asymbol **asymbols;
842       struct warning_callback_info info;
843
844       /* Look through the relocs to see if we can find a plausible
845          address.  */
846
847       entry = (lang_input_statement_type *) abfd->usrdata;
848       if (entry != NULL && entry->asymbols != NULL)
849         asymbols = entry->asymbols;
850       else
851         {
852           long symsize;
853           long symbol_count;
854
855           symsize = bfd_get_symtab_upper_bound (abfd);
856           if (symsize < 0)
857             einfo ("%B%F: could not read symbols: %E\n", abfd);
858           asymbols = (asymbol **) xmalloc (symsize);
859           symbol_count = bfd_canonicalize_symtab (abfd, asymbols);
860           if (symbol_count < 0)
861             einfo ("%B%F: could not read symbols: %E\n", abfd);
862           if (entry != NULL)
863             {
864               entry->asymbols = asymbols;
865               entry->symbol_count = symbol_count;
866             }
867         }
868
869       info.found = false;
870       info.warning = warning;
871       info.symbol = symbol;
872       info.asymbols = asymbols;
873       bfd_map_over_sections (abfd, warning_find_reloc, (PTR) &info);
874
875       if (! info.found)
876         einfo ("%B: %s\n", abfd, warning);
877     }
878
879   return true;
880 }
881
882 /* This is called by warning_callback for each section.  It checks the
883    relocs of the section to see if it can find a reference to the
884    symbol which triggered the warning.  If it can, it uses the reloc
885    to give an error message with a file and line number.  */
886
887 static void
888 warning_find_reloc (abfd, sec, iarg)
889      bfd *abfd;
890      asection *sec;
891      PTR iarg;
892 {
893   struct warning_callback_info *info = (struct warning_callback_info *) iarg;
894   long relsize;
895   arelent **relpp;
896   long relcount;
897   arelent **p, **pend;
898
899   if (info->found)
900     return;
901
902   relsize = bfd_get_reloc_upper_bound (abfd, sec);
903   if (relsize < 0)
904     einfo ("%B%F: could not read relocs: %E\n", abfd);
905   if (relsize == 0)
906     return;
907
908   relpp = (arelent **) xmalloc (relsize);
909   relcount = bfd_canonicalize_reloc (abfd, sec, relpp, info->asymbols);
910   if (relcount < 0)
911     einfo ("%B%F: could not read relocs: %E\n", abfd);
912
913   p = relpp;
914   pend = p + relcount;
915   for (; p < pend && *p != NULL; p++)
916     {
917       arelent *q = *p;
918
919       if (q->sym_ptr_ptr != NULL
920           && *q->sym_ptr_ptr != NULL
921           && strcmp (bfd_asymbol_name (*q->sym_ptr_ptr), info->symbol) == 0)
922         {
923           /* We found a reloc for the symbol we are looking for.  */
924           einfo ("%C: %s\n", abfd, sec, q->address, info->warning);
925           info->found = true;
926           break;
927         }
928     }
929
930   free (relpp);
931 }
932
933 /* This is called when an undefined symbol is found.  */
934
935 /*ARGSUSED*/
936 static boolean
937 undefined_symbol (info, name, abfd, section, address)
938      struct bfd_link_info *info;
939      const char *name;
940      bfd *abfd;
941      asection *section;
942      bfd_vma address;
943 {
944   static char *error_name;
945   static unsigned int error_count;
946
947 #define MAX_ERRORS_IN_A_ROW 5
948
949   if (config.warn_once)
950     {
951       static struct bfd_hash_table *hash;
952
953       /* Only warn once about a particular undefined symbol.  */
954
955       if (hash == NULL)
956         {
957           hash = ((struct bfd_hash_table *)
958                   xmalloc (sizeof (struct bfd_hash_table)));
959           if (! bfd_hash_table_init (hash, bfd_hash_newfunc))
960             einfo ("%F%P: bfd_hash_table_init failed: %E\n");
961         }
962
963       if (bfd_hash_lookup (hash, name, false, false) != NULL)
964         return true;
965
966       if (bfd_hash_lookup (hash, name, true, true) == NULL)
967         einfo ("%F%P: bfd_hash_lookup failed: %E\n");
968     }
969
970   /* We never print more than a reasonable number of errors in a row
971      for a single symbol.  */
972   if (error_name != (char *) NULL
973       && strcmp (name, error_name) == 0)
974     ++error_count;
975   else
976     {
977       error_count = 0;
978       if (error_name != (char *) NULL)
979         free (error_name);
980       error_name = buystring (name);
981     }
982
983   if (section != NULL)
984     {
985       if (error_count < MAX_ERRORS_IN_A_ROW)
986         einfo ("%X%C: undefined reference to `%T'\n",
987                abfd, section, address, name);
988       else if (error_count == MAX_ERRORS_IN_A_ROW)
989         einfo ("%D: more undefined references to `%T' follow\n",
990                abfd, section, address, name);
991     }
992   else
993     {
994       if (error_count < MAX_ERRORS_IN_A_ROW)
995         einfo ("%X%B: undefined reference to `%T'\n",
996                abfd, name);
997       else if (error_count == MAX_ERRORS_IN_A_ROW)
998         einfo ("%B: more undefined references to `%T' follow\n",
999                abfd, name);
1000     }
1001
1002   return true;
1003 }
1004
1005 /* This is called when a reloc overflows.  */
1006
1007 /*ARGSUSED*/
1008 static boolean
1009 reloc_overflow (info, name, reloc_name, addend, abfd, section, address)
1010      struct bfd_link_info *info;
1011      const char *name;
1012      const char *reloc_name;
1013      bfd_vma addend;
1014      bfd *abfd;
1015      asection *section;
1016      bfd_vma address;
1017 {
1018   if (abfd == (bfd *) NULL)
1019     einfo ("%P%X: generated");
1020   else
1021     einfo ("%X%C:", abfd, section, address);
1022   einfo (" relocation truncated to fit: %s %T", reloc_name, name);
1023   if (addend != 0)
1024     einfo ("+%v", addend);
1025   einfo ("\n");
1026   return true;
1027 }
1028
1029 /* This is called when a dangerous relocation is made.  */
1030
1031 /*ARGSUSED*/
1032 static boolean
1033 reloc_dangerous (info, message, abfd, section, address)
1034      struct bfd_link_info *info;
1035      const char *message;
1036      bfd *abfd;
1037      asection *section;
1038      bfd_vma address;
1039 {
1040   if (abfd == (bfd *) NULL)
1041     einfo ("%P%X: generated");
1042   else
1043     einfo ("%X%C:", abfd, section, address);
1044   einfo ("dangerous relocation: %s\n", message);
1045   return true;
1046 }
1047
1048 /* This is called when a reloc is being generated attached to a symbol
1049    that is not being output.  */
1050
1051 /*ARGSUSED*/
1052 static boolean
1053 unattached_reloc (info, name, abfd, section, address)
1054      struct bfd_link_info *info;
1055      const char *name;
1056      bfd *abfd;
1057      asection *section;
1058      bfd_vma address;
1059 {
1060   if (abfd == (bfd *) NULL)
1061     einfo ("%P%X: generated");
1062   else
1063     einfo ("%X%C:", abfd, section, address);
1064   einfo (" reloc refers to symbol `%T' which is not being output\n", name);
1065   return true;
1066 }
1067
1068 /* This is called when a symbol in notice_hash is found.  Symbols are
1069    put in notice_hash using the -y option.  */
1070
1071 /*ARGSUSED*/
1072 static boolean
1073 notice_ysym (info, name, abfd, section, value)
1074      struct bfd_link_info *info;
1075      const char *name;
1076      bfd *abfd;
1077      asection *section;
1078      bfd_vma value;
1079 {
1080   einfo ("%B: %s %s\n", abfd,
1081          bfd_is_und_section (section) ? "reference to" : "definition of",
1082          name);
1083   return true;
1084 }