* ld.h (check_nocrossrefs): Declare.
[external/binutils.git] / ld / ldmain.c
1 /* Main program of GNU linker.
2    Copyright (C) 1991, 92, 93, 94, 95, 1996 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 the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.  */
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 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
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   bfd_set_error_program_name (program_name);
166
167   xatexit (remove_output);
168
169   /* Initialize the data about options.  */
170   trace_files = trace_file_tries = version_printed = false;
171   whole_archive = false;
172   config.build_constructors = true;
173   config.dynamic_link = false;
174   command_line.force_common_definition = false;
175   command_line.interpreter = NULL;
176   command_line.rpath = NULL;
177
178   link_info.callbacks = &link_callbacks;
179   link_info.relocateable = false;
180   link_info.shared = false;
181   link_info.symbolic = false;
182   link_info.static_link = false;
183   link_info.traditional_format = false;
184   link_info.strip = strip_none;
185   link_info.discard = discard_none;
186   link_info.lprefix_len = 1;
187   link_info.lprefix = "L";
188   link_info.keep_memory = true;
189   link_info.input_bfds = NULL;
190   link_info.create_object_symbols_section = NULL;
191   link_info.hash = NULL;
192   link_info.keep_hash = NULL;
193   link_info.notice_all = false;
194   link_info.notice_hash = NULL;
195   link_info.wrap_hash = NULL;
196   
197   ldfile_add_arch ("");
198
199   config.make_executable = true;
200   force_make_executable = false;
201   config.magic_demand_paged = true;
202   config.text_read_only = true;
203   config.make_executable = true;
204
205   emulation = get_emulation (argc, argv);
206   ldemul_choose_mode (emulation);
207   default_target = ldemul_choose_target ();
208   lang_init ();
209   ldemul_before_parse ();
210   lang_has_input_file = false;
211   parse_args (argc, argv);
212
213   ldemul_set_symbols ();
214
215   if (link_info.relocateable)
216     {
217       if (command_line.relax)
218         einfo ("%P%F: -relax and -r may not be used together\n");
219       if (link_info.shared)
220         einfo ("%P%F: -r and -shared may not be used together\n");
221     }
222
223   /* Treat ld -r -s as ld -r -S -x (i.e., strip all local symbols).  I
224      don't see how else this can be handled, since in this case we
225      must preserve all externally visible symbols.  */
226   if (link_info.relocateable && link_info.strip == strip_all)
227     {
228       link_info.strip = strip_debugger;
229       if (link_info.discard == discard_none)
230         link_info.discard = discard_all;
231     }
232
233   /* This essentially adds another -L directory so this must be done after
234      the -L's in argv have been processed.  */
235   set_scripts_dir ();
236
237   if (had_script == false)
238     {
239       /* Read the emulation's appropriate default script.  */
240       int isfile;
241       char *s = ldemul_get_script (&isfile);
242
243       if (isfile)
244         ldfile_open_command_file (s);
245       else
246         {
247           if (trace_file_tries)
248             {
249               info_msg ("using internal linker script:\n");
250               info_msg ("==================================================\n");
251               info_msg (s);
252               info_msg ("\n==================================================\n");
253             }
254           lex_string = s;
255           lex_redirect (s);
256         }
257       parser_input = input_script;
258       yyparse ();
259       lex_string = NULL;
260     }
261
262   lang_final ();
263
264   if (lang_has_input_file == false)
265     {
266       if (version_printed)
267         xexit (0);
268       einfo ("%P%F: no input files\n");
269     }
270
271   if (trace_files)
272     {
273       info_msg ("%P: mode %s\n", emulation);
274     }
275
276   ldemul_after_parse ();
277
278
279   if (config.map_filename)
280     {
281       if (strcmp (config.map_filename, "-") == 0)
282         {
283           config.map_file = stdout;
284         }
285       else
286         {
287           config.map_file = fopen (config.map_filename, FOPEN_WT);
288           if (config.map_file == (FILE *) NULL)
289             {
290               bfd_set_error (bfd_error_system_call);
291               einfo ("%P%F: cannot open map file %s: %E\n",
292                      config.map_filename);
293             }
294         }
295     }
296
297
298   lang_process ();
299
300   /* Print error messages for any missing symbols, for any warning
301      symbols, and possibly multiple definitions */
302
303
304   if (config.text_read_only)
305     {
306       /* Look for a text section and mark the readonly attribute in it */
307       asection *found = bfd_get_section_by_name (output_bfd, ".text");
308
309       if (found != (asection *) NULL)
310         {
311           found->flags |= SEC_READONLY;
312         }
313     }
314
315   if (link_info.relocateable)
316     output_bfd->flags &= ~EXEC_P;
317   else
318     output_bfd->flags |= EXEC_P;
319
320   ldwrite ();
321
322   if (config.map_file != NULL)
323     lang_map ();
324   if (command_line.cref)
325     output_cref (config.map_file != NULL ? config.map_file : stdout);
326   if (nocrossref_list != NULL)
327     check_nocrossrefs ();
328
329   /* Even if we're producing relocateable output, some non-fatal errors should
330      be reported in the exit status.  (What non-fatal errors, if any, do we
331      want to ignore for relocateable output?)  */
332
333   if (config.make_executable == false && force_make_executable == false)
334     {
335       if (trace_files == true)
336         {
337           einfo ("%P: link errors found, deleting executable `%s'\n",
338                  output_filename);
339         }
340
341       /* The file will be removed by remove_output.  */
342
343       xexit (1);
344     }
345   else
346     {
347       if (! bfd_close (output_bfd))
348         einfo ("%F%B: final close failed: %E\n", output_bfd);
349
350       /* If the --force-exe-suffix is enabled, and we're making an
351          executable file and it doesn't end in .exe, copy it to one which does. */
352
353       if (! link_info.relocateable && command_line.force_exe_suffix)
354         {
355           int len = strlen (output_filename);
356           if (len < 4 
357               || (strcasecmp (output_filename + len - 4, ".exe") != 0
358                   && strcasecmp (output_filename + len - 4, ".dll") != 0))
359             {
360               FILE *src;
361               FILE *dst;
362               const int bsize = 4096;
363               char *buf = xmalloc (bsize);
364               int l;
365               char *dst_name = xmalloc (len + 5);
366               strcpy (dst_name, output_filename);
367               strcat (dst_name, ".exe");
368               src = fopen (output_filename, FOPEN_RB);
369               dst = fopen (dst_name, FOPEN_WB);
370
371               if (!src)
372                 einfo ("%X%P: unable to open for source of copy `%s'\n", output_filename);
373               if (!dst)
374                 einfo ("%X%P: unable to open for destination of copy `%s'\n", dst_name);
375               while ((l = fread (buf, 1, bsize, src)) > 0)
376                 {
377                   int done = fwrite (buf, 1, l, dst);
378                   if (done != l)
379                     {
380                       einfo ("%P: Error writing file `%s'\n", dst_name);
381                     }
382                 }
383               fclose (src);
384               if (!fclose (dst))
385                 {
386                   einfo ("%P: Error closing file `%s'\n", dst_name);
387                 }
388               free (dst_name);
389               free (buf);
390             }
391         }
392     }
393
394   END_PROGRESS (program_name);
395
396   if (config.stats)
397     {
398       extern char **environ;
399 #ifdef HAVE_SBRK
400       char *lim = (char *) sbrk (0);
401 #endif
402       long run_time = get_run_time () - start_time;
403
404       fprintf (stderr, "%s: total time in link: %ld.%06ld\n",
405                program_name, run_time / 1000000, run_time % 1000000);
406 #ifdef HAVE_SBRK
407       fprintf (stderr, "%s: data size %ld\n", program_name,
408                (long) (lim - (char *) &environ));
409 #endif
410     }
411
412   /* Prevent remove_output from doing anything, after a successful link.  */
413   output_filename = NULL;
414
415   xexit (0);
416   return 0;
417 }
418
419 /* We need to find any explicitly given emulation in order to initialize the
420    state that's needed by the lex&yacc argument parser (parse_args).  */
421
422 static char *
423 get_emulation (argc, argv)
424      int argc;
425      char **argv;
426 {
427   char *emulation;
428   int i;
429
430   emulation = (char *) getenv (EMULATION_ENVIRON);
431   if (emulation == NULL)
432     emulation = DEFAULT_EMULATION;
433
434   for (i = 1; i < argc; i++)
435     {
436       if (!strncmp (argv[i], "-m", 2))
437         {
438           if (argv[i][2] == '\0')
439             {
440               /* -m EMUL */
441               if (i < argc - 1)
442                 {
443                   emulation = argv[i + 1];
444                   i++;
445                 }
446               else
447                 {
448                   einfo("%P%F: missing argument to -m\n");
449                 }
450             }
451           else if (strcmp (argv[i], "-mips1") == 0
452                    || strcmp (argv[i], "-mips2") == 0
453                    || strcmp (argv[i], "-mips3") == 0)
454             {
455               /* FIXME: The arguments -mips1, -mips2 and -mips3 are
456                  passed to the linker by some MIPS compilers.  They
457                  generally tell the linker to use a slightly different
458                  library path.  Perhaps someday these should be
459                  implemented as emulations; until then, we just ignore
460                  the arguments and hope that nobody ever creates
461                  emulations named ips1, ips2 or ips3.  */
462             }
463           else if (strcmp (argv[i], "-m486") == 0)
464             {
465               /* FIXME: The argument -m486 is passed to the linker on
466                  some Linux systems.  Hope that nobody creates an
467                  emulation named 486.  */
468             }
469           else
470             {
471               /* -mEMUL */
472               emulation = &argv[i][2];
473             }
474         }
475     }
476
477   return emulation;
478 }
479
480 /* If directory DIR contains an "ldscripts" subdirectory,
481    add DIR to the library search path and return true,
482    else return false.  */
483
484 static boolean
485 check_for_scripts_dir (dir)
486      char *dir;
487 {
488   size_t dirlen;
489   char *buf;
490   struct stat s;
491   boolean res;
492
493   dirlen = strlen (dir);
494   /* sizeof counts the terminating NUL.  */
495   buf = (char *) xmalloc (dirlen + sizeof("/ldscripts"));
496   sprintf (buf, "%s/ldscripts", dir);
497
498   res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode);
499   free (buf);
500   if (res)
501     ldfile_add_library_path (dir, false);
502   return res;
503 }
504
505 /* Set the default directory for finding script files.
506    Libraries will be searched for here too, but that's ok.
507    We look for the "ldscripts" directory in:
508
509    SCRIPTDIR (passed from Makefile)
510    the dir where this program is (for using it from the build tree)
511    the dir where this program is/../lib (for installing the tool suite elsewhere) */
512
513 static void
514 set_scripts_dir ()
515 {
516   char *end, *dir;
517   size_t dirlen;
518
519   if (check_for_scripts_dir (SCRIPTDIR))
520     return;                     /* We've been installed normally.  */
521
522   /* Look for "ldscripts" in the dir where our binary is.  */
523   end = strrchr (program_name, '/');
524
525   if (end == NULL)
526     {
527       /* Don't look for ldscripts in the current directory.  There is
528          too much potential for confusion.  */
529       return;
530     }
531
532   dirlen = end - program_name;
533   /* Make a copy of program_name in dir.
534      Leave room for later "/../lib".  */
535   dir = (char *) xmalloc (dirlen + 8);
536   strncpy (dir, program_name, dirlen);
537   dir[dirlen] = '\0';
538
539   if (check_for_scripts_dir (dir))
540     return;                     /* Don't free dir.  */
541
542   /* Look for "ldscripts" in <the dir where our binary is>/../lib.  */
543   strcpy (dir + dirlen, "/../lib");
544   if (check_for_scripts_dir (dir))
545     return;
546
547   free (dir);                   /* Well, we tried.  */
548 }
549
550 void
551 add_ysym (name)
552      const char *name;
553 {
554   if (link_info.notice_hash == (struct bfd_hash_table *) NULL)
555     {
556       link_info.notice_hash = ((struct bfd_hash_table *)
557                                xmalloc (sizeof (struct bfd_hash_table)));
558       if (! bfd_hash_table_init_n (link_info.notice_hash,
559                                    bfd_hash_newfunc,
560                                    61))
561         einfo ("%P%F: bfd_hash_table_init failed: %E\n");
562     }      
563
564   if (bfd_hash_lookup (link_info.notice_hash, name, true, true)
565       == (struct bfd_hash_entry *) NULL)
566     einfo ("%P%F: bfd_hash_lookup failed: %E\n");
567 }
568
569 /* Record a symbol to be wrapped, from the --wrap option.  */
570
571 void
572 add_wrap (name)
573      const char *name;
574 {
575   if (link_info.wrap_hash == NULL)
576     {
577       link_info.wrap_hash = ((struct bfd_hash_table *)
578                              xmalloc (sizeof (struct bfd_hash_table)));
579       if (! bfd_hash_table_init_n (link_info.wrap_hash,
580                                    bfd_hash_newfunc,
581                                    61))
582         einfo ("%P%F: bfd_hash_table_init failed: %E\n");
583     }
584   if (bfd_hash_lookup (link_info.wrap_hash, name, true, true) == NULL)
585     einfo ("%P%F: bfd_hash_lookup failed: %E\n");
586 }
587
588 /* Handle the -retain-symbols-file option.  */
589
590 void
591 add_keepsyms_file (filename)
592      const char *filename;
593 {
594   FILE *file;
595   char *buf;
596   size_t bufsize;
597   int c;
598
599   if (link_info.strip == strip_some)
600     einfo ("%X%P: error: duplicate retain-symbols-file\n");
601
602   file = fopen (filename, "r");
603   if (file == (FILE *) NULL)
604     {
605       bfd_set_error (bfd_error_system_call);
606       einfo ("%X%P: %s: %E\n", filename);
607       return;
608     }
609
610   link_info.keep_hash = ((struct bfd_hash_table *)
611                          xmalloc (sizeof (struct bfd_hash_table)));
612   if (! bfd_hash_table_init (link_info.keep_hash, bfd_hash_newfunc))
613     einfo ("%P%F: bfd_hash_table_init failed: %E\n");
614
615   bufsize = 100;
616   buf = (char *) xmalloc (bufsize);
617
618   c = getc (file);
619   while (c != EOF)
620     {
621       while (isspace (c))
622         c = getc (file);
623
624       if (c != EOF)
625         {
626           size_t len = 0;
627
628           while (! isspace (c) && c != EOF)
629             {
630               buf[len] = c;
631               ++len;
632               if (len >= bufsize)
633                 {
634                   bufsize *= 2;
635                   buf = xrealloc (buf, bufsize);
636                 }
637               c = getc (file);
638             }
639
640           buf[len] = '\0';
641
642           if (bfd_hash_lookup (link_info.keep_hash, buf, true, true)
643               == (struct bfd_hash_entry *) NULL)
644             einfo ("%P%F: bfd_hash_lookup for insertion failed: %E\n");
645         }
646     }
647
648   if (link_info.strip != strip_none)
649     einfo ("%P: `-retain-symbols-file' overrides `-s' and `-S'\n");
650
651   link_info.strip = strip_some;
652 }
653 \f
654 /* Callbacks from the BFD linker routines.  */
655
656 /* This is called when BFD has decided to include an archive member in
657    a link.  */
658
659 /*ARGSUSED*/
660 static boolean
661 add_archive_element (info, abfd, name)
662      struct bfd_link_info *info;
663      bfd *abfd;
664      const char *name;
665 {
666   lang_input_statement_type *input;
667
668   input = ((lang_input_statement_type *)
669            xmalloc (sizeof (lang_input_statement_type)));
670   input->filename = abfd->filename;
671   input->local_sym_name = abfd->filename;
672   input->the_bfd = abfd;
673   input->asymbols = NULL;
674   input->next = NULL;
675   input->just_syms_flag = false;
676   input->loaded = false;
677   input->search_dirs_flag = false;
678
679   /* FIXME: The following fields are not set: header.next,
680      header.type, closed, passive_position, symbol_count,
681      next_real_file, is_archive, target, real.  This bit of code is
682      from the old decode_library_subfile function.  I don't know
683      whether any of those fields matters.  */
684
685   ldlang_add_file (input);
686
687   if (config.map_file != (FILE *) NULL)
688     {
689       static boolean header_printed;
690       struct bfd_link_hash_entry *h;
691       bfd *from;
692       int len;
693
694       h = bfd_link_hash_lookup (link_info.hash, name, false, false, true);
695
696       if (h == NULL)
697         from = NULL;
698       else
699         {
700           switch (h->type)
701             {
702             default:
703               from = NULL;
704               break;
705
706             case bfd_link_hash_defined:
707             case bfd_link_hash_defweak:
708               from = h->u.def.section->owner;
709               break;
710
711             case bfd_link_hash_undefined:
712             case bfd_link_hash_undefweak:
713               from = h->u.undef.abfd;
714               break;
715
716             case bfd_link_hash_common:
717               from = h->u.c.p->section->owner;
718               break;
719             }
720         }
721
722       if (! header_printed)
723         {
724           char buf[100];
725
726           sprintf (buf, "%-29s %s\n\n", "Archive member included",
727                    "because of file (symbol)");
728           minfo ("%s", buf);
729           header_printed = true;
730         }
731
732       if (bfd_my_archive (abfd) == NULL)
733         {
734           minfo ("%s", bfd_get_filename (abfd));
735           len = strlen (bfd_get_filename (abfd));
736         }
737       else
738         {
739           minfo ("%s(%s)", bfd_get_filename (bfd_my_archive (abfd)),
740                  bfd_get_filename (abfd));
741           len = (strlen (bfd_get_filename (bfd_my_archive (abfd)))
742                  + strlen (bfd_get_filename (abfd))
743                  + 2);
744         }
745
746       if (len >= 29)
747         {
748           print_nl ();
749           len = 0;
750         }
751       while (len < 30)
752         {
753           print_space ();
754           ++len;
755         }
756
757       if (from != NULL)
758         minfo ("%B ", from);
759       if (h != NULL)
760         minfo ("(%T)\n", h->root.string);
761       else
762         minfo ("(%s)\n", name);
763     }
764
765   if (trace_files || trace_file_tries)
766     info_msg ("%I\n", input);
767
768   return true;
769 }
770
771 /* This is called when BFD has discovered a symbol which is defined
772    multiple times.  */
773
774 /*ARGSUSED*/
775 static boolean
776 multiple_definition (info, name, obfd, osec, oval, nbfd, nsec, nval)
777      struct bfd_link_info *info;
778      const char *name;
779      bfd *obfd;
780      asection *osec;
781      bfd_vma oval;
782      bfd *nbfd;
783      asection *nsec;
784      bfd_vma nval;
785 {
786   /* If either section has the output_section field set to
787      bfd_abs_section_ptr, it means that the section is being
788      discarded, and this is not really a multiple definition at all.
789      FIXME: It would be cleaner to somehow ignore symbols defined in
790      sections which are being discarded.  */
791   if ((osec->output_section != NULL
792        && bfd_is_abs_section (osec->output_section))
793       || (nsec->output_section != NULL
794           && bfd_is_abs_section (nsec->output_section)))
795     return true;
796
797   einfo ("%X%C: multiple definition of `%T'\n",
798          nbfd, nsec, nval, name);
799   if (obfd != (bfd *) NULL)
800     einfo ("%D: first defined here\n", obfd, osec, oval);
801   return true;
802 }
803
804 /* This is called when there is a definition of a common symbol, or
805    when a common symbol is found for a symbol that is already defined,
806    or when two common symbols are found.  We only do something if
807    -warn-common was used.  */
808
809 /*ARGSUSED*/
810 static boolean
811 multiple_common (info, name, obfd, otype, osize, nbfd, ntype, nsize)
812      struct bfd_link_info *info;
813      const char *name;
814      bfd *obfd;
815      enum bfd_link_hash_type otype;
816      bfd_vma osize;
817      bfd *nbfd;
818      enum bfd_link_hash_type ntype;
819      bfd_vma nsize;
820 {
821   if (! config.warn_common)
822     return true;
823
824   if (ntype == bfd_link_hash_defined
825       || ntype == bfd_link_hash_defweak
826       || ntype == bfd_link_hash_indirect)
827     {
828       ASSERT (otype == bfd_link_hash_common);
829       einfo ("%B: warning: definition of `%T' overriding common\n",
830              nbfd, name);
831       if (obfd != NULL)
832         einfo ("%B: warning: common is here\n", obfd);
833     }
834   else if (otype == bfd_link_hash_defined
835            || otype == bfd_link_hash_defweak
836            || otype == bfd_link_hash_indirect)
837     {
838       ASSERT (ntype == bfd_link_hash_common);
839       einfo ("%B: warning: common of `%T' overridden by definition\n",
840              nbfd, name);
841       if (obfd != NULL)
842         einfo ("%B: warning: defined here\n", obfd);
843     }
844   else
845     {
846       ASSERT (otype == bfd_link_hash_common && ntype == bfd_link_hash_common);
847       if (osize > nsize)
848         {
849           einfo ("%B: warning: common of `%T' overridden by larger common\n",
850                  nbfd, name);
851           if (obfd != NULL)
852             einfo ("%B: warning: larger common is here\n", obfd);
853         }
854       else if (nsize > osize)
855         {
856           einfo ("%B: warning: common of `%T' overriding smaller common\n",
857                  nbfd, name);
858           if (obfd != NULL)
859             einfo ("%B: warning: smaller common is here\n", obfd);
860         }
861       else
862         {
863           einfo ("%B: warning: multiple common of `%T'\n", nbfd, name);
864           if (obfd != NULL)
865             einfo ("%B: warning: previous common is here\n", obfd);
866         }
867     }
868
869   return true;
870 }
871
872 /* This is called when BFD has discovered a set element.  H is the
873    entry in the linker hash table for the set.  SECTION and VALUE
874    represent a value which should be added to the set.  */
875
876 /*ARGSUSED*/
877 static boolean
878 add_to_set (info, h, reloc, abfd, section, value)
879      struct bfd_link_info *info;
880      struct bfd_link_hash_entry *h;
881      bfd_reloc_code_real_type reloc;
882      bfd *abfd;
883      asection *section;
884      bfd_vma value;
885 {
886   if (config.warn_constructors)
887     einfo ("%P: warning: global constructor %s used\n",
888            h->root.string);
889
890   if (! config.build_constructors)
891     return true;
892
893   ldctor_add_set_entry (h, reloc, (const char *) NULL, section, value);
894
895   if (h->type == bfd_link_hash_new)
896     {
897       h->type = bfd_link_hash_undefined;
898       h->u.undef.abfd = abfd;
899       /* We don't call bfd_link_add_undef to add this to the list of
900          undefined symbols because we are going to define it
901          ourselves.  */
902     }
903
904   return true;
905 }
906
907 /* This is called when BFD has discovered a constructor.  This is only
908    called for some object file formats--those which do not handle
909    constructors in some more clever fashion.  This is similar to
910    adding an element to a set, but less general.  */
911
912 static boolean
913 constructor_callback (info, constructor, name, abfd, section, value)
914      struct bfd_link_info *info;
915      boolean constructor;
916      const char *name;
917      bfd *abfd;
918      asection *section;
919      bfd_vma value;
920 {
921   char *s;
922   struct bfd_link_hash_entry *h;
923   char set_name[1 + sizeof "__CTOR_LIST__"];
924
925   if (config.warn_constructors)
926     einfo ("%P: warning: global constructor %s used\n", name);
927
928   if (! config.build_constructors)
929     return true;
930
931   /* Ensure that BFD_RELOC_CTOR exists now, so that we can give a
932      useful error message.  */
933   if (bfd_reloc_type_lookup (output_bfd, BFD_RELOC_CTOR) == NULL
934       && (link_info.relocateable
935           || bfd_reloc_type_lookup (abfd, BFD_RELOC_CTOR) == NULL))
936     einfo ("%P%F: BFD backend error: BFD_RELOC_CTOR unsupported\n");
937
938   s = set_name;
939   if (bfd_get_symbol_leading_char (abfd) != '\0')
940     *s++ = bfd_get_symbol_leading_char (abfd);
941   if (constructor)
942     strcpy (s, "__CTOR_LIST__");
943   else
944     strcpy (s, "__DTOR_LIST__");
945
946   h = bfd_link_hash_lookup (info->hash, set_name, true, true, true);
947   if (h == (struct bfd_link_hash_entry *) NULL)
948     einfo ("%P%F: bfd_link_hash_lookup failed: %E\n");
949   if (h->type == bfd_link_hash_new)
950     {
951       h->type = bfd_link_hash_undefined;
952       h->u.undef.abfd = abfd;
953       /* We don't call bfd_link_add_undef to add this to the list of
954          undefined symbols because we are going to define it
955          ourselves.  */
956     }
957
958   ldctor_add_set_entry (h, BFD_RELOC_CTOR, name, section, value);
959   return true;
960 }
961
962 /* A structure used by warning_callback to pass information through
963    bfd_map_over_sections.  */
964
965 struct warning_callback_info
966 {
967   boolean found;
968   const char *warning;
969   const char *symbol;
970   asymbol **asymbols;
971 };
972
973 /* This is called when there is a reference to a warning symbol.  */
974
975 /*ARGSUSED*/
976 static boolean
977 warning_callback (info, warning, symbol, abfd, section, address)
978      struct bfd_link_info *info;
979      const char *warning;
980      const char *symbol;
981      bfd *abfd;
982      asection *section;
983      bfd_vma address;
984 {
985   /* This is a hack to support warn_multiple_gp.  FIXME: This should
986      have a cleaner interface, but what?  */
987   if (! config.warn_multiple_gp
988       && strcmp (warning, "using multiple gp values") == 0)
989     return true;
990
991   if (section != NULL)
992     einfo ("%C: %s\n", abfd, section, address, warning);
993   else if (abfd == NULL)
994     einfo ("%P: %s\n", warning);
995   else if (symbol == NULL)
996     einfo ("%B: %s\n", abfd, warning);
997   else
998     {
999       lang_input_statement_type *entry;
1000       asymbol **asymbols;
1001       struct warning_callback_info info;
1002
1003       /* Look through the relocs to see if we can find a plausible
1004          address.  */
1005
1006       entry = (lang_input_statement_type *) abfd->usrdata;
1007       if (entry != NULL && entry->asymbols != NULL)
1008         asymbols = entry->asymbols;
1009       else
1010         {
1011           long symsize;
1012           long symbol_count;
1013
1014           symsize = bfd_get_symtab_upper_bound (abfd);
1015           if (symsize < 0)
1016             einfo ("%B%F: could not read symbols: %E\n", abfd);
1017           asymbols = (asymbol **) xmalloc (symsize);
1018           symbol_count = bfd_canonicalize_symtab (abfd, asymbols);
1019           if (symbol_count < 0)
1020             einfo ("%B%F: could not read symbols: %E\n", abfd);
1021           if (entry != NULL)
1022             {
1023               entry->asymbols = asymbols;
1024               entry->symbol_count = symbol_count;
1025             }
1026         }
1027
1028       info.found = false;
1029       info.warning = warning;
1030       info.symbol = symbol;
1031       info.asymbols = asymbols;
1032       bfd_map_over_sections (abfd, warning_find_reloc, (PTR) &info);
1033
1034       if (! info.found)
1035         einfo ("%B: %s\n", abfd, warning);
1036
1037       if (entry == NULL)
1038         free (asymbols);
1039     }
1040
1041   return true;
1042 }
1043
1044 /* This is called by warning_callback for each section.  It checks the
1045    relocs of the section to see if it can find a reference to the
1046    symbol which triggered the warning.  If it can, it uses the reloc
1047    to give an error message with a file and line number.  */
1048
1049 static void
1050 warning_find_reloc (abfd, sec, iarg)
1051      bfd *abfd;
1052      asection *sec;
1053      PTR iarg;
1054 {
1055   struct warning_callback_info *info = (struct warning_callback_info *) iarg;
1056   long relsize;
1057   arelent **relpp;
1058   long relcount;
1059   arelent **p, **pend;
1060
1061   if (info->found)
1062     return;
1063
1064   relsize = bfd_get_reloc_upper_bound (abfd, sec);
1065   if (relsize < 0)
1066     einfo ("%B%F: could not read relocs: %E\n", abfd);
1067   if (relsize == 0)
1068     return;
1069
1070   relpp = (arelent **) xmalloc (relsize);
1071   relcount = bfd_canonicalize_reloc (abfd, sec, relpp, info->asymbols);
1072   if (relcount < 0)
1073     einfo ("%B%F: could not read relocs: %E\n", abfd);
1074
1075   p = relpp;
1076   pend = p + relcount;
1077   for (; p < pend && *p != NULL; p++)
1078     {
1079       arelent *q = *p;
1080
1081       if (q->sym_ptr_ptr != NULL
1082           && *q->sym_ptr_ptr != NULL
1083           && strcmp (bfd_asymbol_name (*q->sym_ptr_ptr), info->symbol) == 0)
1084         {
1085           /* We found a reloc for the symbol we are looking for.  */
1086           einfo ("%C: %s\n", abfd, sec, q->address, info->warning);
1087           info->found = true;
1088           break;
1089         }
1090     }
1091
1092   free (relpp);
1093 }
1094
1095 /* This is called when an undefined symbol is found.  */
1096
1097 /*ARGSUSED*/
1098 static boolean
1099 undefined_symbol (info, name, abfd, section, address)
1100      struct bfd_link_info *info;
1101      const char *name;
1102      bfd *abfd;
1103      asection *section;
1104      bfd_vma address;
1105 {
1106   static char *error_name;
1107   static unsigned int error_count;
1108
1109 #define MAX_ERRORS_IN_A_ROW 5
1110
1111   if (config.warn_once)
1112     {
1113       static struct bfd_hash_table *hash;
1114
1115       /* Only warn once about a particular undefined symbol.  */
1116
1117       if (hash == NULL)
1118         {
1119           hash = ((struct bfd_hash_table *)
1120                   xmalloc (sizeof (struct bfd_hash_table)));
1121           if (! bfd_hash_table_init (hash, bfd_hash_newfunc))
1122             einfo ("%F%P: bfd_hash_table_init failed: %E\n");
1123         }
1124
1125       if (bfd_hash_lookup (hash, name, false, false) != NULL)
1126         return true;
1127
1128       if (bfd_hash_lookup (hash, name, true, true) == NULL)
1129         einfo ("%F%P: bfd_hash_lookup failed: %E\n");
1130     }
1131
1132   /* We never print more than a reasonable number of errors in a row
1133      for a single symbol.  */
1134   if (error_name != (char *) NULL
1135       && strcmp (name, error_name) == 0)
1136     ++error_count;
1137   else
1138     {
1139       error_count = 0;
1140       if (error_name != (char *) NULL)
1141         free (error_name);
1142       error_name = buystring (name);
1143     }
1144
1145   if (section != NULL)
1146     {
1147       if (error_count < MAX_ERRORS_IN_A_ROW)
1148         einfo ("%X%C: undefined reference to `%T'\n",
1149                abfd, section, address, name);
1150       else if (error_count == MAX_ERRORS_IN_A_ROW)
1151         einfo ("%D: more undefined references to `%T' follow\n",
1152                abfd, section, address, name);
1153     }
1154   else
1155     {
1156       if (error_count < MAX_ERRORS_IN_A_ROW)
1157         einfo ("%X%B: undefined reference to `%T'\n",
1158                abfd, name);
1159       else if (error_count == MAX_ERRORS_IN_A_ROW)
1160         einfo ("%B: more undefined references to `%T' follow\n",
1161                abfd, name);
1162     }
1163
1164   return true;
1165 }
1166
1167 /* This is called when a reloc overflows.  */
1168
1169 /*ARGSUSED*/
1170 static boolean
1171 reloc_overflow (info, name, reloc_name, addend, abfd, section, address)
1172      struct bfd_link_info *info;
1173      const char *name;
1174      const char *reloc_name;
1175      bfd_vma addend;
1176      bfd *abfd;
1177      asection *section;
1178      bfd_vma address;
1179 {
1180   if (abfd == (bfd *) NULL)
1181     einfo ("%P%X: generated");
1182   else
1183     einfo ("%X%C:", abfd, section, address);
1184   einfo (" relocation truncated to fit: %s %T", reloc_name, name);
1185   if (addend != 0)
1186     einfo ("+%v", addend);
1187   einfo ("\n");
1188   return true;
1189 }
1190
1191 /* This is called when a dangerous relocation is made.  */
1192
1193 /*ARGSUSED*/
1194 static boolean
1195 reloc_dangerous (info, message, abfd, section, address)
1196      struct bfd_link_info *info;
1197      const char *message;
1198      bfd *abfd;
1199      asection *section;
1200      bfd_vma address;
1201 {
1202   if (abfd == (bfd *) NULL)
1203     einfo ("%P%X: generated");
1204   else
1205     einfo ("%X%C:", abfd, section, address);
1206   einfo ("dangerous relocation: %s\n", message);
1207   return true;
1208 }
1209
1210 /* This is called when a reloc is being generated attached to a symbol
1211    that is not being output.  */
1212
1213 /*ARGSUSED*/
1214 static boolean
1215 unattached_reloc (info, name, abfd, section, address)
1216      struct bfd_link_info *info;
1217      const char *name;
1218      bfd *abfd;
1219      asection *section;
1220      bfd_vma address;
1221 {
1222   if (abfd == (bfd *) NULL)
1223     einfo ("%P%X: generated");
1224   else
1225     einfo ("%X%C:", abfd, section, address);
1226   einfo (" reloc refers to symbol `%T' which is not being output\n", name);
1227   return true;
1228 }
1229
1230 /* This is called if link_info.notice_all is set, or when a symbol in
1231    link_info.notice_hash is found.  Symbols are put in notice_hash
1232    using the -y option.  */
1233
1234 static boolean
1235 notice (info, name, abfd, section, value)
1236      struct bfd_link_info *info;
1237      const char *name;
1238      bfd *abfd;
1239      asection *section;
1240      bfd_vma value;
1241 {
1242   if (! info->notice_all
1243       || (info->notice_hash != NULL
1244           && bfd_hash_lookup (info->notice_hash, name, false, false) != NULL))
1245     einfo ("%B: %s %s\n", abfd,
1246            bfd_is_und_section (section) ? "reference to" : "definition of",
1247            name);
1248
1249   if (command_line.cref || nocrossref_list != NULL)
1250     add_cref (name, abfd, section, value);
1251
1252   return true;
1253 }