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