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