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