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