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