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