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