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