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