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