source: trunk/src/binutils/ld/ldmain.c@ 99

Last change on this file since 99 was 86, checked in by bird, 22 years ago

Applied the original 2.11.2 diff.

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